Advanced Compilers CMPSCI 710 Spring 2003 Using SSA

  • Slides: 18
Download presentation
Advanced Compilers CMPSCI 710 Spring 2003 Using SSA form Emery Berger University of Massachusetts,

Advanced Compilers CMPSCI 710 Spring 2003 Using SSA form Emery Berger University of Massachusetts, Amherst UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE

Topics n Last time n n Computing SSA form This time n Optimizations using

Topics n Last time n n Computing SSA form This time n Optimizations using SSA form n n Constant propagation & dead code elimination Loop invariant code motion UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE 2

Constant Propagation n Lattice for integer addition, multiplication, mod, etc. > false … -1

Constant Propagation n Lattice for integer addition, multiplication, mod, etc. > false … -1 0 1 … true ? UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE 3

Boolean Lattices, AND n meet functions example: true AND ? , false AND >

Boolean Lattices, AND n meet functions example: true AND ? , false AND > UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE 4

Boolean Lattices, OR n meet functions example: true OR ? , false OR >

Boolean Lattices, OR n meet functions example: true OR ? , false OR > UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE 5

Lattice for -Nodes n To propagate constants: if constant appears in conditional Ø Insert

Lattice for -Nodes n To propagate constants: if constant appears in conditional Ø Insert assignment on true branch UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE 6

Constant Propagation Using SSA Form n n Initialize all expressions to > Two work

Constant Propagation Using SSA Form n n Initialize all expressions to > Two work lists: n CFG edges = (entry, x) n n n x = first reachable node SSA edges = Pick edge from either list until empty n n Propagate constants when visiting either edge When we visit a node: n n -node: meet lattice values others: add SSA successors, CFG successors UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE 7

Sparse Conditional Constant Propagation Example entry a 1 Ã 2 B 1 b 1

Sparse Conditional Constant Propagation Example entry a 1 Ã 2 B 1 b 1 Ã 3 B 2 n if a 1 < b 1 B 3 n Initialize all expressions to > Two work lists: n B 4 c 1 Ã 4 c 2 Ã 5 B 5 n n Pick edge from either list until empty n c 3 Ã 6(c 1, c 2) B 6 exit CFG edges = (entry, x) SSA edges = n Propagate constants when visiting either edge When we visit a node: n n -node: meet lattice values others: add SSA successors, CFG successors UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE 8

Loop Optimizations n Why optimize loops? Ø Loops = frequently-accessed code n n Ø

Loop Optimizations n Why optimize loops? Ø Loops = frequently-accessed code n n Ø n regular patterns – can simplify optimizations rule of thumb: loop bodies execute 10 depth times optimizations pay off But why do we care if we aren’t using FORTRAN? n Loops aren’t just over arrays! n n Pointer-based data structures Text processing… UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE 9

Loop Invariant Code Motion n Classical loop optimization n avoids unnecessary computation while (z

Loop Invariant Code Motion n Classical loop optimization n avoids unnecessary computation while (z < 1000) t = a + b z += t t=a+b while (z < 1000) z += t UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE 10

Removing Loop Invariant Code n n Build SSA graph Simple test: n n no

Removing Loop Invariant Code n n Build SSA graph Simple test: n n no operands in statement are defined by node or have definition inside loop if match: n n assign computation new temporary name and move to loop pre-header, and add assignment to temp e. g. , l = r 1 op r 2 becomes t 1 = r 1 op r 2; l = t 1 UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE 11

Loop Invariant Code Motion Example n a 1 Ã 2 b 1 Ã 3

Loop Invariant Code Motion Example n a 1 Ã 2 b 1 Ã 3 n Build SSA graph Simple test: n n no operands in statement are defined by node or have definition inside loop if match: n n assign computation new temporary name and move to loop pre-header, and add assignment to temp e. g. , l = r 1 op r 2 becomes t 1 = r 1 op r 2; l = t 1 x = a 1 + b 1 y=x+y UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE 12

Finding More Invariants n n Build SSA graph If operands point to definition inside

Finding More Invariants n n Build SSA graph If operands point to definition inside loop and definition is function of invariants (recursively) Ø replace as before UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE 13

Loop Invariant Code Motion Example II a 1 Ã 2 xÃq b 1 Ã

Loop Invariant Code Motion Example II a 1 Ã 2 xÃq b 1 Ã 3 yÃr n n Build SSA graph If operands point to definition inside loop and definition is function of invariants (recursively) n replace as before x = a 1 + b 1 y=x+y z = x + a 1 UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE 14

Loop Induction Variables n Loop induction variable: increases or decreases by constant amount inside

Loop Induction Variables n Loop induction variable: increases or decreases by constant amount inside loop n n e. g. , for (i = 0; i < 100; i++) Opportunity for: n strength reduction n n e. g. , j = 2 * i becomes j = j + 2 identifying stride of accesses for prefetching n e. g. : array accesses UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE 15

Easy Detection of Loop Induction Variables n Pattern match & check: n n n

Easy Detection of Loop Induction Variables n Pattern match & check: n n n Search for “i = i + b” in loop i is induction variable if no other assignment to i in loop Pros & Cons: Easy! - Does not catch all loop induction variables e. g. , “i = a * c + 2” + UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE 16

Next Time n n n Finding loop induction variables Strength reduction Read ACDI ch.

Next Time n n n Finding loop induction variables Strength reduction Read ACDI ch. 12, pp 333 -342 Project Design documents due March 13: project presentations n n 5 -10 minutes 3 slides UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE 17

Taxonomy of Induction Variables n basic induction variable: n n only definition in loop

Taxonomy of Induction Variables n basic induction variable: n n only definition in loop is assignment j : = j § c, where c is loop invariant mutual induction variable: n definition is linear function of other induction variable i ‘: n n n i = c 1 * i‘ § c 2 i = i‘ / c 1 § c 2 family of basic induction variable j = set of induction variables i such that i always assigned linear function of j UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE 18