global optimizationscomp171/wiki.files/ps10.pdf · 1.1 basic blocks and control-flow graphs a basic...

18

Upload: others

Post on 23-Jul-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Global Optimizationscomp171/wiki.files/ps10.pdf · 1.1 Basic Blocks and Control-Flow graphs A basic block is a sequence of IR instructions that has no goto instructions in it, except,

Global Optimizations

Avi Hayoun, Ben Eyal and Lior Zur-Lotan

January 9, 2017

Contents

1 Global Optimizations 1

1.1 Basic Blocks and Control-Flow graphs . . . . . . . . . . . . . . . . . . . . . . . . . . 11.1.1 Control-Flow Graph Example . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

2 Global CSE and CP 2

2.1 Common Subexpression Elimination . . . . . . . . . . . . . . . . . . . . . . . . . . . 22.2 Copy Propagation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32.3 Available Expressions analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

3 Example 4

3.1 Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63.1.1 Initial state . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63.1.2 1st iteration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73.1.3 2nd iteration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83.1.4 3rd iteration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93.1.5 4th iteration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103.1.6 5th iteration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113.1.7 6th iteration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123.1.8 7th iteration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133.1.9 Last iteration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

3.2 Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

1 Global Optimizations

Global optimizations and analyses are performed in the context of a whole, single function - givenan entire control �ow graph of a function.

1.1 Basic Blocks and Control-Flow graphs

A basic block is a sequence of IR instructions that has no goto instructions in it, except, maybe, asits last instruction. It has a single entry point (the �rst instruction) and a single exit point (the lastinstruction).1 Local Optimizations and analyses are performed in the context of single Basic

1For the purpose of structuring the code of a given function into basic blocks, function calls are considered atomic

instructions and not control-�ow operators.

1

Page 2: Global Optimizationscomp171/wiki.files/ps10.pdf · 1.1 Basic Blocks and Control-Flow graphs A basic block is a sequence of IR instructions that has no goto instructions in it, except,

Blocks.

A Control Flow graph is a directed graph, where nodes correspond to Basic Blocks. There'san edge between one block and another if execution control can move between the two (if there isa goto from the end of the �rst to the beginning of the other). We also add 2 nodes: start (orEntry) and end (or Exit).

1.1.1 Control-Flow Graph Example

Consider the following IR:

1 x := y + 1;

2

3 t0 := call ReadInput;

4 ifZ t0 goto LElse;

5 z := x + 1;

6 goto LFinish;

7 LElse:

8 z := y + 1;

9 LFinish;

10 w := y;

11 LLoop:

12 a := w + 1;

13 t0 := call ReadInput;

14 b := a >= t0

15 x := t0;

16 ifNZ x goto LEnd:

17 ifZ b goto LLoop:

18 LEnd:

The CFG for the above code is:

2 Global CSE and CP

Common Subexpression Elimination (CSE) andCopy Propagation (CP) are optimizationsachieved by substituting expressions.

2.1 Common Subexpression Elimination

This optimization searches for common arithmetic expressions between statements and, if possible,substitutes the second arithmetic operation with the result of the �rst.

For instance: Given the code

1 _t0 := x + 1;

2 y := _t0 * 2;

3 z := x + 1;

2

Page 3: Global Optimizationscomp171/wiki.files/ps10.pdf · 1.1 Basic Blocks and Control-Flow graphs A basic block is a sequence of IR instructions that has no goto instructions in it, except,

We can reuse _t0 and eliminate the second computation of x + 1, and we get:

1 _t0 := x + 1;

2 y := _t0 * 2;

3 z := _t0

On top of removing redundant computations CSE is used as a pre-step for other optimizations (e.g.Dead Code Elimination)

2.2 Copy Propagation

This optimization means that we look for references to variables that were assigned to anothervariable (i.e. x := y). We replace the reference to one with the reference to the other.

For instance, consider the following code:

1 x := _tmp0 + 1;

2 y := x;

3 w := y + 1;

4 z := x + 1;

We can replace the x in z := x + 1 with y and get:

1 x := _tmp0 + 1;

2 y := x;

3 w := y + 1;

4 z := y + 1;

3

Page 4: Global Optimizationscomp171/wiki.files/ps10.pdf · 1.1 Basic Blocks and Control-Flow graphs A basic block is a sequence of IR instructions that has no goto instructions in it, except,

It may seem meaningless, but CP is used as a pre-step for other optimizations (e.g. CSE orDead Code Elimination). It removed redundent assignments between variables that hide possiblesubexpressions or assignments from being removed.

2.3 Available Expressions analysis

To perform globalCSE orCP we must take the entireCFG and apply theAvailable Expressionsanalysis on it.

Given a CFG we need to know which expression is stored in which variable at each point of thecode. Because we are handling code that includes branching, we need to take code branching intoaccount including, how to unite sets of available expressions from two branches.

For this we de�ne 2 notions for each statement:

� IN [s] = set of (var, expression) pairs that are available right before executing s

� OUT [s] = set of (var, expression) pairs that are available right after executing s

To calculate these sets, let s be a statement of the form "a := expr":

� $ OUT[s] = (IN[s] ∪ {(a, expr)}) \ {s' = (v, e) ∈ IN[s] | USE(s′)∈ DEF(s)\ ∨ (s' ̸= s ∧ v ∈DEF(s))}$

� $ IN[s] = ∩ {OUT[p] | p∈ CFG.Parents(s)}$

Reminder:DEF (s) - the variables possibly modi�ed by statement s.USE(s) - the variables read by statement s.

An algorithm for computing the IN and OUT sets for a CFG:

1 for var in CFG.vars:

2 for expression in CFG.expressions:

3 for block in CFG:

4 add (var, expression) to OUT[block]

5 OUT[Entry] = {}

6 while Not at fixed point:

7 block = choose SimpleBlock from CFG

8 for s=(var, expr) in block.statements: # Iterate block statements in order

9 IN[s] = Intersect(CFG.OUTs.Parents(s))

10 OUT[s] = Union((var, expr), (IN[s])) \ {(v, e) pairs where v = var or var in e}

If all every Basic Block is iterated (at any order) enough times, the algorithm will reach a �xedpoint where the IN and OUT sets no longer change for any block and that's when the algorithmcompletes.

4

Page 5: Global Optimizationscomp171/wiki.files/ps10.pdf · 1.1 Basic Blocks and Control-Flow graphs A basic block is a sequence of IR instructions that has no goto instructions in it, except,

3 Example

Lets take the IR from CFG example and run the Available Expressions analysis on it. We'llthen use the analysis result to apply CP and CSE on the code.

5

Page 6: Global Optimizationscomp171/wiki.files/ps10.pdf · 1.1 Basic Blocks and Control-Flow graphs A basic block is a sequence of IR instructions that has no goto instructions in it, except,

Here's the CFG for the code, as a reminder:

6

Page 7: Global Optimizationscomp171/wiki.files/ps10.pdf · 1.1 Basic Blocks and Control-Flow graphs A basic block is a sequence of IR instructions that has no goto instructions in it, except,

3.1 Analysis

3.1.1 Initial state

Before the �rst iteration (of the "while" loop) the sets look like this (we're using "{. . . }" to denotethe set of all (var, expr) pairs):

7

Page 8: Global Optimizationscomp171/wiki.files/ps10.pdf · 1.1 Basic Blocks and Control-Flow graphs A basic block is a sequence of IR instructions that has no goto instructions in it, except,

3.1.2 1st iteration

After the �rst iteration, the �rst block's IN set is reduced (emptied) and the OUT set representsthe expressions available to any following statement.

8

Page 9: Global Optimizationscomp171/wiki.files/ps10.pdf · 1.1 Basic Blocks and Control-Flow graphs A basic block is a sequence of IR instructions that has no goto instructions in it, except,

3.1.3 2nd iteration

We chose to iterate the left branch �rst, but the order is meaningless as long as no block is starved.

9

Page 10: Global Optimizationscomp171/wiki.files/ps10.pdf · 1.1 Basic Blocks and Control-Flow graphs A basic block is a sequence of IR instructions that has no goto instructions in it, except,

3.1.4 3rd iteration

Now the right branch.

10

Page 11: Global Optimizationscomp171/wiki.files/ps10.pdf · 1.1 Basic Blocks and Control-Flow graphs A basic block is a sequence of IR instructions that has no goto instructions in it, except,

3.1.5 4th iteration

Done with the "if" blocks.

11

Page 12: Global Optimizationscomp171/wiki.files/ps10.pdf · 1.1 Basic Blocks and Control-Flow graphs A basic block is a sequence of IR instructions that has no goto instructions in it, except,

3.1.6 5th iteration

Analyzing the loop block, remember that the parents of this block are the one containing "w := y;"as well as the loop block itself.

12

Page 13: Global Optimizationscomp171/wiki.files/ps10.pdf · 1.1 Basic Blocks and Control-Flow graphs A basic block is a sequence of IR instructions that has no goto instructions in it, except,

3.1.7 6th iteration

After a single iteration in the loop block, we choose to move on the the block right below it.

Entry{}

{}x := y + 1;{(x, y + 1)}

t0 = call ReadInput;{(x, y + 1)}

ifZ t0 goto LElse;

{(x, y + 1)}z := x + 1;

{(x, y + 1), (z, x + 1)}goto LFinish;

{(x, y + 1)}w := y;

{(x, y + 1), (w , y)}

{}Exit

{(x, y + 1)}z := y + 1;

{(x, y + 1), (z, y + 1)}

{(x, y+ 1), (w, y)}a := w + 1;

{(a, w + 1), (x, y + 1), (w, y)}t0 = call ReadInput;

{(a , w + 1), (x, y + 1), (w, y)}b := a >= t0;

{(a, w + 1), (b, a >= t0),(x, y + 1), (w, y)}x := t0;

{(a, w + 1), (b, a >= t0), (w, y), (x, t0)}ifNZ t0 goto LEnd;

{(a, w + 1), (b, a >= t0), (w, y),(x, t0)}

ifZ b goto LLoop;{(a, w + 1), (b, a >= t0)}

13

Page 14: Global Optimizationscomp171/wiki.files/ps10.pdf · 1.1 Basic Blocks and Control-Flow graphs A basic block is a sequence of IR instructions that has no goto instructions in it, except,

3.1.8 7th iteration

Going back to that loop.

Entry{}

{}x := y + 1;{(x, y + 1)}

t0 = call ReadInput;{(x, y + 1)}

ifZ t0 goto LElse;

{(x, y + 1)}z := x + 1;

{(x, y + 1), (z, x + 1)}goto LFinish;

{(x, y + 1)}w := y;

{(x, y + 1), (w , y)}

{}Exit

{(x, y + 1)}z := y + 1;

{(x, y + 1), (z, y + 1)}

{(w, y)}a := w + 1;

{(a, w + 1), (w, y)}t0 = call ReadInput;{(a , w + 1), (w, y)}

b := a >= t0;{(a, w + 1), (b, a >= t0),(w, y)}

x := t0;{(a, w + 1), (b, a >= t0), (w, y), (x, t0)}

ifNZ t0 goto LEnd;

{(a, w + 1), (b, a >= t0), (w, y),(x, t0)}

ifZ b goto LLoop;{(a, w + 1), (b, a >= t0)}

14

Page 15: Global Optimizationscomp171/wiki.files/ps10.pdf · 1.1 Basic Blocks and Control-Flow graphs A basic block is a sequence of IR instructions that has no goto instructions in it, except,

3.1.9 Last iteration

For consistancy with other analyses we also analyse the Exit block

Entry{}

{}x := y + 1;{(x, y + 1)}

t0 = call ReadInput;{(x, y + 1)}

ifZ t0 goto LElse;

{(x, y + 1)}z := x + 1;

{(x, y + 1), (z, x + 1)}goto LFinish;

{(x, y + 1)}w := y;

{(x, y + 1), (w , y)}

{(a, w + 1), (b, a >= t0)}Exit

{(x, y + 1)}z := y + 1;

{(x, y + 1), (z, y + 1)}

{(w, y)}a := w + 1;

{(a, w + 1), (w, y)}t0 = call ReadInput;{(a , w + 1), (w, y)}

b := a >= t0;{(a, w + 1), (b, a >= t0),(w, y)}

x := t0;{(a, w + 1), (b, a >= t0), (w, y), (x, t0)}

ifNZ t0 goto LEnd;

{(a, w + 1), (b, a >= t0), (w, y),(x, t0)}

ifZ b goto LLoop;{(a, w + 1), (b, a >= t0)}

15

Page 16: Global Optimizationscomp171/wiki.files/ps10.pdf · 1.1 Basic Blocks and Control-Flow graphs A basic block is a sequence of IR instructions that has no goto instructions in it, except,

3.2 Optimization

Now that we've completed the analysis, we can use the results to apply optimizations to the code.We'll start with CP (as a pre-step for CSE or Dead Code Eliminiation).

From the results we can see that at line 12 (a := w + 1;) we can replace w with y. Now if we re-runAvailable Expressions analysis again, we'll get the following result:

Entry{}

{}x := y + 1;{(x, y + 1)}

t0 = call ReadInput;{(x, y + 1)}

ifZ t0 goto LElse;

{(x, y + 1)}z := x + 1;

{(x, y + 1), (z, x + 1)}goto LFinish;

{(x, y + 1)}w := y;

{(x, y + 1), (w , y)}

{(a, y + 1), (b, a >= t0)}Exit

{(x, y + 1)}z := y + 1;

{(x, y + 1), (z, x)}

{(w, y)}a := y+1;

{(a, y + 1), (w, y)}t0 = call ReadInput;{(a , y + 1), (w, y)}

b := a >= t0;{(a, y + 1), (b, a >= t0),(w, y)}

x := t0;{(a, y + 1), (b, a >= t0), (w, y), (x, t0)}

ifNZ t0 goto LEnd;

{(a, w + 1), (b, a >= t0), (w, y),(x, t0)}

ifZ b goto LLoop;{(a, w + 1), (b, a >= t0)}

16

Page 17: Global Optimizationscomp171/wiki.files/ps10.pdf · 1.1 Basic Blocks and Control-Flow graphs A basic block is a sequence of IR instructions that has no goto instructions in it, except,

Running CSE on this result we see we can apply the optimization on line 8 (substitutingz := y + 1; with z := x), resulting in:

Entry{}

{}x := y + 1;{(x, y + 1)}

t0 = call ReadInput;{(x, y + 1)}

ifZ t0 goto LElse;

{(x, y + 1)}z := x + 1;

{(x, y + 1), (z, x + 1)}goto LFinish;

{(x, y + 1)}w := y;

{(x, y + 1), (w , y)}

{(a, y + 1), (b, a >= t0)}Exit

{(x, y + 1)}z := x;

{(x, y + 1), (z, x)}

{(w, y)}a := y+1;

{(a, y + 1), (w, y)}t0 = call ReadInput;{(a , y + 1), (w, y)}

b := a >= t0;{(a, y + 1), (b, a >= t0),(w, y)}

x := t0;{(a, y + 1), (b, a >= t0), (w, y), (x, t0)}

ifNZ t0 goto LEnd;

{(a, w + 1), (b, a >= t0), (w, y),(x, t0)}

ifZ b goto LLoop;{(a, w + 1), (b, a >= t0)}

17

Page 18: Global Optimizationscomp171/wiki.files/ps10.pdf · 1.1 Basic Blocks and Control-Flow graphs A basic block is a sequence of IR instructions that has no goto instructions in it, except,

In this instance CP didn't improve our CSE output, but if we run Liveness Analysis on the aboveCFG we'll see that the assignment at line 10 (w := y;) is now dead code, and can be removedsince we don't read this assigned value of w anywhere (we substituted its read operation at line 12with a read of y). After removing it we get this �nal CFG:

18