Date post: | 19-Dec-2015 |
Category: |
Documents |
View: | 216 times |
Download: | 0 times |
1
Copy Propagation
What does it mean? Given an assignment x = y, replace later uses of x with
uses of y, provided there are no intervening assignments to x or y.
Similar to register coalescing, which eliminates copies from one register to another.
When is it performed? At any level, but usually early in the optimization
process.
What is the result? Smaller code
2
Local Copy Propagation
Local copy propagation Performed within basic blocks Algorithm sketch:
traverse BB from top to bottom maintain table of copies encountered so far modify applicable instructions as you go
3
Local Copy Propagation Algorithm sketch for a basic block containing instructions i1, i2, ..., in
for instr = i1 to in if instr is of the form 'res = opd1 op opd2' opd1 = REPLACE(opd1, copytable)
opd2 = REPLACE(opd2, copytable)else if instr is of the form 'res = var' var = REPLACE(var, copytable)if instr has a lhs res,
REMOVE from copytable all pairs involving res.
if instr is of the form 'res = var' /* i.e. a copy */ insert {(res, var2)} in the copytable
endforREPLACE(opd, copytable) if you find (opd, x) in copytable /* use hashing for faster access */
return xelse return opd
copytable is tablecontaining copy pairse.g. if there's an assignment x := a, thencopytable should contain(x,a)
4
Local Copy Propagation
step instruction updated instruction table contents
1 b = a b = a {(b,a)} 2 c = b + 1 c = a + 1 {(b,a)} 3 d = b d = a {(b,a), (d,a)} 4 b = d + c b = a + c {(d,a)} 5 b = d b = a {(d,a), (b,a)}
Example: Local copy propagation on basic block:
b = ac = b + 1d = bb = d + cb = d
Note: if there was a definition of 'a' between 3 and 4, then we would haveto remove (b,a) and (d,a) from the table. As a result, we wouldn'tbe able to perform local copy propagation at instructions 4 and 5.However, this will be taken care of when we perform global copypropagation.
5
Copy Propagation
Global copy propagation Performed on flow graph. Given copy statement x=y and use w=x,
we can replace w=x with w=y only if the following conditions are met:1. x=y must be the only definition of x
reaching w=x This can be determined through ud-
chains
2. There may be no definitions of y on any path from x=y to w=x. Use iterative data flow analysis to solve
this. Even, better, use iterative data flow
analysis to solve both problems at the same time.
x=y
w=x
6
Copy Propagation
Data flow analysis to determine which instructions are candidates for global copy propagation forward direction gen[Bi] = {(x,y,i,p) | p is the
position of x=y in block Bi and neither x nor y is assigned a value after p}
kill[Bi] = {(x,y,j,p) | x=y, located at position p in block BjBi, is killed due to a definition of x or y in Bi }
in[B]=out[P] over the predecessors P of B
Initialize in[B1]=, in[B]=U for BB1
p: x = y.........
generate x=y ifno definitions of x or y in this area
...q: x = z...s: y = w
kill all other definitions of xkill all other definitions of y
7
Copy Propagationentry
exit
1: c = a + b2: d = c3: e = d * d
4: f = a + c5: g = e6: a = g + d7: a < c
8: h = g + 1
9: f = d - g10: f > a
11: b = g * a12: h < f
entry
exit
c = a + bd = ce = c * c
f = a + cg = ea = e + ca < c
h = e + 1
f = c - ef > a
b = e * ah < f
dead code?
8
Copy Propagation
Copy propagation will not detect the opportunity to replace x with y in the last block below:
Copy propagation may generate code that does not need to be evaluated any longer. This will be handled by optimizations that perform
redundancy elimination.
z >0
x = y
x = y
w = x + z
Mini quiz: which optimization can handle this?Answer: If we perform an optimization similar to code hoisting (i.e. one that would move the copy either up or down the graph) then copy propagation will be able to update "w=y+z"
9
Constant Propagation
What does it mean? Given an assignment x = c, where c is a constant,
replace later uses of x with uses of c, provided there are no intervening assignments to x.
Similar to copy propagation Extra feature: It can analyze constant-value
conditionals to determine whether a branch should be executed or not.
When is it performed? Early in the optimization process.
What is the result? Smaller code Fewer registers
10
Common Subexpression Elimination
Local common subexpression elimination Performed within basic blocks Algorithm sketch:
Traverse BB from top to bottom Maintain table of expressions evaluated so far
if any operand of the expression is redefined, remove it from the table
Modify applicable instructions as you go generate temporary variable, store the expression
in it and use the variable next time the expression is encountered.
x = a + b...y = a + b
t = a + b x = t...y = t
11
Common Subexpression Elimination
c = a + bd = m * ne = b + df = a + bg = - bh = b + aa = j + ak = m * nj = b + da = - bif m * n go to L
t1 = a + bc = t1t2 = m * nd = t2t3 = b + de = t3f = t1g = -bh = t1 /* commutative */a = j + ak = t2j = t3a = -bif t2 go to L
the table contains quintuples:(pos, opd1, opr, opd2, tmp)
12
Common Subexpression Elimination
Global common subexpression elimination Performed on flow graph Requires available expression information
In addition to finding what expressions are available at the endpoints of basic blocks, we need to know where each of those expressions was most recently evaluated (which block and which position within that block).
13
Common Subexpression Elimination
Global common subexpression elimination Algorithm sketch:
For each block B and each statement x=y+z, s.t. {y+z}in[B]i. Find the evaluation of y+z that reaches B, say
w=x+yii. Create temporary variable tiii. Replace [w=y+z] with [t=y+z; w=t]iv. Replace [x=y+z] with [x=t]
Notes: This method will miss the fact that b and d have
the same value:a = x+yc = x+yb = a*zd = c*z
Answer: Value Numbering
Mini quiz: which optimization can handle this?
14
Common Subexpression Eliminationentry
exit
c = a + bd = a * ce = d * d
f = a + bc = c * 2c > d
g = a * c
g = d * d
g > 10
entry
exit
t1 = a + bc = t1d = a * ct2 = d * de = t2
f = t1c = c * 2c > d
g = a * c
g = t2
g > 10
15
Loop Optimizations
Important: Loop bodies execute frequently.
We will examine: Loop-invariant code motion
Identify computations that have the same value at every iteration and move them outside the loop, so they are performed only once.
Not always a good idea:
Induction variable elimination Induction variables are incremented/decremented by a constant
amount at each iteration, typically related to the loop control variable. If we can identify them and take advantage of this, we can reduce the number of calculations as well as certain data dependences.
cin >> n;for (int i=0; i<n; i++) x = y/z;
16
Loop-Invariant Code Motion
Step 1: Identify loop-invariant code.
An instruction is loop-invariant if, for each operand:1. The operand is constant, OR2. All definitions of that operand that reach the instruction
are outside the loop, OR3. There is exactly one in-loop definition of the operand that
reaches the instruction, and that definition is loop invariant. Is this necessary?
How do we determine #2? Use ud-chains
17
Loop-Invariant Code Motion
Step 2: Move it outside the loop. Algorithm sketch:
– For each loop-invariant instruction i: x=y+z in basic block B, check that it satisfies the conditions:i. B dominates all exits of the loopii. x is not defined anywhere else in the loopiii. B dominates all uses of x in the loop
(i.e. all uses of x in the loop can be reached only by i
– Move each instruction i that satisfies these requirements to a newly created pre-header of the loop, making certain that any non-constant operands (such as y, z) have already had their definitions moved to the pre-header.
Note: When applying loop-invariant code motion to nested loops, work
from the innermost loop outwards.
18
Loop-Invariant Code Motion
Why are these conditions necessary?i. B dominates all exits of the loop
i = 1x = 5
y = a+by > 0
x = 2
y--i++i<n
z = x
x=2 is loop invariant, but does not dominatethe exit. If we move it outside the loop, we willget different results in the cases when control flowsalong the green line.
19
Loop-Invariant Code Motion
Why are these conditions necessary?ii. x is not defined anywhere else in the loop
i = 1x = 5
y = a+bx = 3y > 0
x = 2
y--i++i<n
z = x
x=3 is loop invariant and dominates the exit.However, if we move it outside the loop, we willget different results in the cases when control flowsalong the green lines.
20
Loop-Invariant Code Motion
Why are these conditions necessary?iii. B dominates all uses of x in the loop
(i.e. all uses of x in the loop can be reached only by i
i = 1x = 5
y = x+bx = 3y > 0
print y
y--i++i<n
z = x
x=3 is loop invariant, it dominates the exit and x is notdefined anywhere else in the loop.However, if we move it outside the loop, we willget a different value for y at the first iteration, when x should be 5
21
Loop-Invariant Code Motion
entry
exit
b = 2i = 1
d = a + de = 1 + d
d = -cf = 1 + a
i = i+1 a < 2
a = b+1c = 2i mod 2 = 0
Exercise: What happens if you perform constant propagation followed by constant folding after the loop-invariant code motion in this loop?
TF
Reminder:
An instruction is loop-invariant if, for each operand:1. The operand is constant, OR2. All definitions of that operand that reach the instruction are outside the loop, OR3. There is exactly one in-loop definition of the operand that reaches the instruction, and that definition is loop invariant.
Reminder:Move a loop-invariant x=... located in B, if1. B dominates all exits of the loop2. x is not defined anywhere else in the loop3. B dominates all uses of x in the loop
22
Loop-Invariant Code Motion
entry
exit
b = 2i = 1
d = a + de = 1 + d
d = -cf = 1 + a
i = i+1 a < 2
a = b+1c = 2i mod 2 = 0
entry
exit
b = 2i = 1a = b+1c = 2t1 = a<2
d = a + de = 1 + d
d = -cf = 1 + a
i = i+1 t1
i mod 2 = 0
TF
TF
23
after constant propagationand constant folding
entry
exit
b = 2i = 1a = b+1c = 2t1 = a<2
d = a + de = 1 + d
d = -cf = 1 + a
i = i+1 t1
i mod 2 = 0
TF
entry
exit
b = 2i = 1a = 3c = 2t1 = false
d = a + de = 1 + d
d = -cf = 1 + a
i = i+1 t1
i mod 2 = 0
F
24
Induction variable elimination
We will concentrate on two kinds of induction variables: Basic
These are of the form i = i c, where c is a constant How about i=i*c or i=i/c ?
Derived These are defined only once in the loop, and their value
is a linear function of a basic induction variable. E.g. j = c1*i+c2 . j is expressed as (i, c1, c2) and is said to belong to the
family of i
What information do we need? Reaching definitions Loop-invariants
25
Induction variable elimination
Algorithm sketch: Find all basic induction variables Find all assignments of the form k=c op j, where
op{+, -, *, /} and j is an induction variable If j is basic, then k belongs to its family.
Determine its triple. If j is not basic, then it belongs to the family of a
basic induction variable i. Check that there is no assignment to i between j=
and k= Check that j= dominates k= Determine k's triple.
e.g. if j is (i, a, b) and k=c*j+d, then k's triple is (i, a*c, b*c+d)
Perform strength reduction on induction variables
26
Induction variable elimination
Performing strength reduction on induction variables Consider:
i=i+1 // (i, 1, 1) j=4*i // (i, 4, 4)
Goal:i=i+1
j=4*i // initially j=j+4 // afterwards
How? Create a new variable s Set s=4*i in the pre-header Set s=s+4 every time i is updated Replace j=4*i with j=s.