Dataflow AnalysisOpti IV Loop Optimizations, SSA Form - PowerPoint PPT Presentation

1 / 39
About This Presentation
Title:

Dataflow AnalysisOpti IV Loop Optimizations, SSA Form

Description:

Dataflow Analysis/Opti IV. Loop Optimizations, SSA Form. EECS 483 Lecture 21 ... IVE Example. Case 4: Same increment, unknown initial values ... – PowerPoint PPT presentation

Number of Views:108
Avg rating:3.0/5.0
Slides: 40
Provided by: scottm80
Category:

less

Transcript and Presenter's Notes

Title: Dataflow AnalysisOpti IV Loop Optimizations, SSA Form


1
Dataflow Analysis/Opti IVLoop Optimizations,
SSA Form
  • EECS 483 Lecture 21
  • University of Michigan
  • Monday, November 24, 2003

2
Class Problem From Last Time
Optimize this applying 1. constant propagation 2.
constant folding 3. strength reduction 4. dead
code elimination
1 r1 0 2 r2 10
1 r1 0 2 r2 10
3 r4 1 4 r7 r1 4 5 r6 8
3 4 5
6 r2 0 7 r3 r2 / r6
8 r3 r4 r6 9 r3 r3 r2
6 r2 0 7 r3 0
8 9 r3 8 r2
10 r2 r2 r1 11 r6 r7 r6 12 r1 r1 1
10 r2 r2 r1 11 12 r1 r1 1
13 store (r1, r3)
13 store (r1, r3)
3
Class Problem From Last Time
Optimize this applying 1. constant propagation 2.
constant folding 3. strength reduction 4. dead
code elimination 5. forward copy propagation 6.
backward copy propagation 7. CSE
r1 9 r4 4 r5 0 r6 16 r2 r3 r4 r8 r2
r5 r9 r3 r7 load(r2) r5 r9 r4 r3
load(r2) r10 r3 / r6 store (r8, r7) r11
r2 r12 load(r11) store(r12, r3)
r2 r3 ltlt 2 r7 load(r2) store (r2, r7) r12
load(r2) store(r12, r7)
4
Invariant Code Removal
  • Move operations whose source operands do not
    change within the loop to the loop preheader
  • Execute them only 1x per invocation of the loop
  • Be careful with memory operations!
  • Be careful with ops not executed every iteration

r1 3 r5 0
r4 load(r5) r7 r4 3
r8 r2 1 r7 r8 r4
r3 r2 1
r1 r1 r7
store (r1, r3)
5
Invariant Code Removal (2)
  • Rules
  • X can be moved
  • src(X) not modified in loop body
  • X is the only op to modify dest(X)
  • for all uses of dest(X), X is in the available
    defs set
  • for all exit BB, if dest(X) is live on the exit
    edge, X is in the available defs set on the edge
  • if X not executed on every iteration, then X must
    provably not cause exceptions
  • if X is a load or store, then there are no writes
    to address(X) in loop

r1 3 r5 0
r4 load(r5) r7 r4 3
r8 r2 1 r7 r8 r4
r3 r2 1
r1 r1 r7
store (r1, r3)
6
Invariant Code Removal (3)
  • Can you do LICM w/o available defs info?
  • Sure no problem!
  • Rules that need change
  • for all uses of dest(X), X is in the available
    defs set
  • for all exit BB, if dest(X) is live on the exit
    edge, X is in the available defs set on the edge
  • First rule approx
  • X dominates all uses of dest(X)
  • Second rule approx
  • X dominates all exit BBs where dest(X) is live
  • This is how the compiler that I work on does it..

7
Global Variable Migration
  • Assign a global variable temporarily to a
    register for the duration of the loop
  • Load in preheader
  • Store at exit points
  • Rules
  • X is a load or store
  • address(X) not modified in the loop
  • if X not executed on every iteration, then X must
    provably not cause an exception
  • All memory ops in loop whose address can equal
    address(X) must always have the same address as X

r4 load(r5) r4 r4 1
r8 load(r5) r7 r8 r4
store(r5, r4)
store(r5,r7)
8
Class Problem
Optimize this applying 1. constant
propagation 2. constant folding 3. strength
reduction 4. dead code elimination 5. forward
copy propagation 6. backward copy propagation 7.
CSE 8. constant combining 9. operation
folding 10. loop invariant removal 11. global
variable migration
r1 1 r2 10
r4 13 r7 r4 r8 r6 load(r10)
r2 1 r3 r2 / r6
r3 r4 r8 r3 r3 r2
r2 r2 r1 store(r10,r3)
store (r2, r3)
9
Induction Variable Strength Reduction
  • Create basic induction variables from derived
    induction variables
  • Rules
  • X is a , ltlt, or operation
  • src1(X) is a basic ind var
  • src2(X) is invariant
  • No other ops modify dest(X)
  • dest(X) ! src(X) for all srcs
  • dest(X) is a register

r5 r4 - 3 r4 r4 1
r7 r4 r9
r6 r4 ltlt 2
10
Induction Variable Strength Reduction (2)
  • Transformation
  • Insert the following into the bottom of preheader
  • new_reg RHS(X)
  • if opcode(X) is not add/sub, insert to the bottom
    of the preheader
  • new_inc inc(src1(X)) opcode(X) src2(X)
  • else
  • new_inc inc(src1(X))
  • Insert the following at each update of src1(X)
  • new_reg new_inc
  • Change X ? dest(X) new_reg

r5 r4 - 3 r4 r4 1
r7 r4 r9
r6 r4 ltlt 2
11
Induction Variable Elimination
  • Remove unnecessary basic induction variables from
    the loop by substituting uses with another BIV
  • Rules (same init val, same inc)
  • Find 2 basic induction vars x,y
  • x,y in same family
  • incremented in same places
  • increments equal
  • initial values equal
  • x not live when you exit loop
  • for each BB where x is defined, there are no uses
    of x between first/last defn of x and last/first
    defn of y

r1 0 r2 0
r1 r1 - 1 r2 r2 - 1
r9 r2 r4
r7 r1 r9
r4 load(r1)
store(r2, r7)
12
Induction Variable Elimination (2)
  • 5 variants
  • 1. Trivial induction variable that is never
    used except by the increments themselves, not
    live at loop exit
  • 2. Same increment, same initial value (prev
    slide)
  • 3. Same increment, initial values are a known
    constant offset from one another
  • 4. Same inc, no nothing about relation of initial
    values
  • 5. Different increments, no nothing about initial
    values
  • The higher the number, the more complex the
    elimination
  • Also, the more expensive it is
  • 1,2 are basically free, so always should be done
  • 3-5 require preheader operations

13
IVE Example
Case 4 Same increment, unknown initial values
For the ind var you are eliminating, look at
each non-increment use, need to regenerate the
same sequence of values as before. If you
can do that w/o adding any ops to the loop body,
the apply xform
r1 ??? r2 ??? rx r2 r1 8
r1 ??? r2 ???
r3 ld(r1 4) r4 ld(r2 8) ... r1 4 r2
4
r3 ld(r1 4) r4 ld(r1 rx) ... r1 4
elim r2
14
Class Problem
Optimize this applying everything ?
r1 0 r2 0
r5 r7 3 r11 r5 r10 r11 9 r9 r1 r4
r9 4 r3 load(r4) r3 r3 r10 r12 r3 r3
r3 r10 r8 r2 r6 r8 ltlt 2 store(r6, r3) r13
r12 - 1 r1 r1 1 r2 r2 1
store(r12, r2)
15
Static Single Assignment (SSA) Form
  • Difficulty with optimization
  • Multiple definitions of thesame register
  • Which definition reaches
  • Is expression available?
  • Static single assignment
  • Each assignment to a variable is given a unique
    name
  • All of the uses reached by that assignment are
    renamed
  • DU chains become obvious based on the register
    name!

r1 r2 r3 r6 r4 r5
r4 4 r6 8
r6 r2 r3 r7 r4 r5
16
Converting to SSA Form
  • Trivial for straight line code
  • More complex with control flow Must use Phi
    nodes

x -1 y x x 5 z x
x0 -1 y x0 x1 5 z x1
if ( ... ) x0 -1 else x1 5 x2
Phi(x0,x1) y x2
if ( ... ) x -1 else x 5 y x
17
Converting to SSA Form (2)
  • What about loops?
  • No problem!, use Phi nodes again

i0 0do i1 Phi(i0, i2) i2 i1
1 while (i2 lt 50)
i 0do i i 1 while (i lt 50)
18
SSA Plusses and Minuses
  • Advantages of SSA
  • Explicit DU chains Trivial to figure out what
    defs reach a use
  • Each use has exactly 1 definition!!!
  • Explicit merging of values
  • Makes optimizations easier
  • Disadvantages
  • When transform the code, must either recompute
    (slow) or incrementally update (tedious)

19
Phi Nodes (aka Phi Functions)
  • Special kind of copy that selects one of its
    inputs
  • Choice of input is governed by the CFG edge along
    which control flow reached the Phi node
  • Phi nodes are required when 2 non-null paths X?Z
    and Y?Z converge at node Z, and nodes X and Y
    contain assignments to V

x0
x1
x2 Phi(x0,x1)
20
SSA Construction
  • High-level algorithm
  • Insert Phi nodes
  • Rename variables
  • A dumb algorithm
  • Insert Phi functions at every join for every
    variable
  • Solve reaching definitions
  • Rename each use to the def that reaches it (will
    be unique)
  • Problems with the dumb algorithm
  • Too many Phi functions (precision)
  • Too many Phi functions (space)
  • Too many Phi functions (time)

21
Need Better Phi Node Insertion Algorithm
  • A definition at n forces a Phi node at m iff n
    not in DOM(m), but n in DOM(p) for some
    predecessors p of m

BB0
def in BB4 forces Phi in BB6 def in BB6 forces
Phi in BB7 def in BB7 forces Phi in BB1
BB1
BB2
BB3
Phi is placed in the block that is just outside
the dominated region of the definition BB
BB4
BB5
Dominance frontier The dominance frontier of node
X is the set of nodes Y such that X
dominates a predecessor of Y, but X does
not strictly dominate Y
BB6
BB7
22
Dominator Tree
BB DOM 0 0 1 0,1 2 0,1,2 3 0,1,3
BB DOM 4 0,1,3,4 5 0,1,3,5 6 0,1,3,6 7 0,1,7
First BB is the root node, each node dominates
all of its descendants
BB0
BB1
BB0
BB2
BB3
BB1
BB2
BB3
BB4
BB5
BB4
BB5
BB6
BB6
BB7
BB7
Dom tree
23
Computing Dominance Frontiers
BB0
BB0
BB DF 0 - 1 - 2 7 3 7 4 6 5 6 6 7 7 1
BB1
BB1
BB2
BB3
BB2
BB3
BB4
BB5
BB6
BB4
BB5
BB6
BB7
For each join point X in the CFG For each
predecessor of X in the CFG Run up to the
IDOM(X) in the dominator tree, adding X
to DF(N) for each N between X and IDOM(X)
BB7
24
Class Problem
Draw the dominator tree, calculate the dominance
frontier for each BB
BB0
BB1
BB2
BB3
BB4
BB5
25
Phi Node Insertion Algorithm
  • Compute dominance frontiers
  • Find global names (aka virtual registers)
  • Global if name live on entry to some block
  • For each name, build a list of blocks that define
    it
  • Insert Phi nodes
  • For each global name n
  • For each BB b in which n is defined
  • For each BB d in bs dominance frontier
  • Insert a Phi node for n in d
  • Add d to ns list of defining BBs

26
Phi Node Insertion - Example
BB DF 0 - 1 - 2 7 3 7 4 6 5 6 6 7 7 1
a is defined in 0,1,3 need Phi in 7 then a is
defined in 7 need Phi in 1 b is defined in 0,
2, 6 need Phi in 7 then b is defined in 7
need Phi in 1 c is defined in 0,1,2,5 need
Phi in 6,7 then c is defined in 7 need Phi in
1 d is defined in 2,3,4 need Phi in 6,7 then
d is defined in 7 need Phi in 1 i is defined
in BB7 need Phi in BB1
a b c i
a Phi(a,a) b Phi(b,b) c Phi(c,c) d
Phi(d,d) i Phi(i,i)
BB0
a c
BB1
b c d
a d
BB2
BB3
c
d
BB4
BB5
c Phi(c,c) d Phi(d,d)
b
BB6
i
BB7
a Phi(a,a) b Phi(b,b) c Phi(c,c) d
Phi(d,d)
27
Class Problem
Insert the Phi nodes
a b
BB0
BB1
c
b a
BB2
BB3
b
BB4
a c
BB5
28
SSA Step 2 Renaming Variables
  • Use an array of stacks, one stack per global
    variable (VR)
  • Algorithm sketch
  • For each BB b in a preorder traversal of the
    dominator tree
  • Generate unique names for each Phi node
  • Rewrite each operation in the BB
  • Uses of global name current name from stack
  • Defs of global name create and push new name
  • Fill in Phi node parameters of successor blocks
  • Recurse on bs children in the dominator tree
  • lton exit from bgt pop names generated in b from
    stacks

29
Renaming Example (Initial State)
a b c i
a Phi(a,a) b Phi(b,b) c Phi(c,c) d
Phi(d,d) i Phi(i,i)
BB0
a c
BB1
var a b c d i ctr 0 0 0
0 0 stk a0 b0 c0 d0 i0
b c d
a d
BB2
BB3
c
d
BB4
BB5
c Phi(c,c) d Phi(d,d)
b
BB6
i
BB7
a Phi(a,a) b Phi(b,b) c Phi(c,c) d
Phi(d,d)
30
Renaming Example (After BB0)
a0 b0 c0 i0
a Phi(a0,a) b Phi(b0,b) c Phi(c0,c) d
Phi(d0,d) i Phi(i0,i)
BB0
a c
BB1
var a b c d i ctr 1 1 1
1 1 stk a0 b0 c0 d0 i0
b c d
a d
BB2
BB3
c
d
BB4
BB5
c Phi(c,c) d Phi(d,d)
b
BB6
i
BB7
a Phi(a,a) b Phi(b,b) c Phi(c,c) d
Phi(d,d)
31
Renaming Example (After BB1)
a0 b0 c0 i0
a1 Phi(a0,a) b1 Phi(b0,b) c1 Phi(c0,c) d1
Phi(d0,d) i1 Phi(i0,i)
BB0
a2 c2
BB1
var a b c d i ctr 3 2 3
2 2 stk a0 b0 c0 d0 i0 a1 b1
c1 d1 i1 a2 c2
b c d
a d
BB2
BB3
c
d
BB4
BB5
c Phi(c,c) d Phi(d,d)
b
BB6
i
BB7
a Phi(a,a) b Phi(b,b) c Phi(c,c) d
Phi(d,d)
32
Renaming Example (After BB2)
a0 b0 c0 i0
a1 Phi(a0,a) b1 Phi(b0,b) c1 Phi(c0,c) d1
Phi(d0,d) i1 Phi(i0,i)
BB0
a2 c2
BB1
var a b c d i ctr 3 3 4
3 2 stk a0 b0 c0 d0 i0 a1 b1
c1 d1 i1 a2 b2 c2 d2
c3
b2 c3 d2
a d
BB2
BB3
c
d
BB4
BB5
c Phi(c,c) d Phi(d,d)
b
BB6
i
BB7
a Phi(a2,a) b Phi(b2,b) c Phi(c3,c) d
Phi(d2,d)
33
Renaming Example (Before BB3)
This just updates the stack to remove the stuff
from the left path out of BB1
a0 b0 c0 i0
a1 Phi(a0,a) b1 Phi(b0,b) c1 Phi(c0,c) d1
Phi(d0,d) i1 Phi(i0,i)
BB0
a2 c2
BB1
var a b c d i ctr 3 3 4
3 2 stk a0 b0 c0 d0 i0 a1 b1
c1 d1 i1 a2 c2
b2 c3 d2
a d
BB2
BB3
c
d
BB4
BB5
c Phi(c,c) d Phi(d,d)
b
BB6
i
BB7
a Phi(a2,a) b Phi(b2,b) c Phi(c3,c) d
Phi(d2,d)
34
Renaming Example (After BB3)
a0 b0 c0 i0
a1 Phi(a0,a) b1 Phi(b0,b) c1 Phi(c0,c) d1
Phi(d0,d) i1 Phi(i0,i)
BB0
a2 c2
BB1
var a b c d i ctr 4 3 4
4 2 stk a0 b0 c0 d0 i0 a1 b1
c1 d1 i1 a2 c2 d3 a3
b2 c3 d2
a3 d3
BB2
BB3
c
d
BB4
BB5
c Phi(c,c) d Phi(d,d)
b
BB6
i
BB7
a Phi(a2,a) b Phi(b2,b) c Phi(c3,c) d
Phi(d2,d)
35
Renaming Example (After BB4)
a0 b0 c0 i0
a1 Phi(a0,a) b1 Phi(b0,b) c1 Phi(c0,c) d1
Phi(d0,d) i1 Phi(i0,i)
BB0
a2 c2
BB1
var a b c d i ctr 4 3 4
5 2 stk a0 b0 c0 d0 i0 a1 b1
c1 d1 i1 a2 c2 d3 a3
d4
b2 c3 d2
a3 d3
BB2
BB3
c
d4
BB4
BB5
c Phi(c2,c) d Phi(d4,d)
b
BB6
i
BB7
a Phi(a2,a) b Phi(b2,b) c Phi(c3,c) d
Phi(d2,d)
36
Renaming Example (After BB5)
a0 b0 c0 i0
a1 Phi(a0,a) b1 Phi(b0,b) c1 Phi(c0,c) d1
Phi(d0,d) i1 Phi(i0,i)
BB0
a2 c2
BB1
var a b c d i ctr 4 3 5
5 2 stk a0 b0 c0 d0 i0 a1 b1
c1 d1 i1 a2 c2 d3 a3
c4
b2 c3 d2
a3 d3
BB2
BB3
c4
d4
BB4
BB5
c Phi(c2,c4) d Phi(d4,d3)
b
BB6
i
BB7
a Phi(a2,a) b Phi(b2,b) c Phi(c3,c) d
Phi(d2,d)
37
Renaming Example (After BB6)
a0 b0 c0 i0
a1 Phi(a0,a) b1 Phi(b0,b) c1 Phi(c0,c) d1
Phi(d0,d) i1 Phi(i0,i)
BB0
a2 c2
BB1
var a b c d i ctr 4 4 6
6 2 stk a0 b0 c0 d0 i0 a1 b1
c1 d1 i1 a2 b3 c2 d3 a3
c5 d5
b2 c3 d2
a3 d3
BB2
BB3
c4
d4
BB4
BB5
c5 Phi(c2,c4) d5 Phi(d4,d3)
b3
BB6
i
BB7
a Phi(a2,a3) b Phi(b2,b3) c Phi(c3,c5) d
Phi(d2,d5)
38
Renaming Example (After BB7)
a0 b0 c0 i0
a1 Phi(a0,a4) b1 Phi(b0,b4) c1
Phi(c0,c6) d1 Phi(d0,d6) i1 Phi(i0,i2)
BB0
a2 c2
BB1
var a b c d i ctr 5 5 7
7 3 stk a0 b0 c0 d0 i0 a1 b1
c1 d1 i1 a2 b4 c2 d6 i2 a4
c6
b2 c3 d2
a3 d3
BB2
BB3
c4
d4
BB4
BB5
c5 Phi(c2,c4) d5 Phi(d4,d3)
b3
BB6
i2
BB7
a4 Phi(a2,a3) b4 Phi(b2,b3) c6
Phi(c3,c5) d6 Phi(d2,d5)
Fin!
39
Class Problem
Rename the variables so this code is in SSA form
a b
BB0
BB1
c
b a
BB2
BB3
b
BB4
a c
BB5
Write a Comment
User Comments (0)
About PowerShow.com