Lazy Code Motion This lecture is primarily based on Konstantinos Sagonas set of slides (Advanced C Advanced Comp mpiler Techniques iler Techniques , (2AD518) at Uppsala University, January-February 2004) . Used with kind permission. (In turn based on Keith Cooper ’ s slides)
Lazy Code Motion The concept ♦ Solve data-flow problems that reveal limits of code motion Lazy Code Motion ♦ Compute I NSERT & D ELETE sets from solutions ♦ Linear pass over the code to rewrite it (using I NSERT & D ELETE ) The history ♦ Partial redundancy elimination ( Morel & Renvoise, CACM, 1979 ) ♦ Improvements by Drechsler & Stadel, Joshi & Dhamdhere, Chow, Knoop, Ruthing & Steffen, Dhamdhere, Sorkin, … ♦ All versions of PRE optimize placement ♦ Guarantee that no path is lengthened ♦ L CM was invented by Knoop et al. in P LDI , 1992 ♦ We will look at a variation by Drechsler & Stadel SIGPLAN Notices, 28(5), May, 1993 Advanced Compiler Techniques 23.04.04 09:45:19 2 ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er /
Lazy Code Motion The intuitions ♦ Compute available expressions Lazy Code Motion ♦ Compute anticipable expressions ♦ These lead to an earliest placement for each expression ♦ Push expressions down the CFG until it changes behavior Assumptions ♦ Uses a lexical notion of identity ( not value identity ) ♦ Code is in an Intermediate Representation with unlimited name space ♦ Consistent, disciplined use of names } ♦ Identical expressions define the same name Avoids copies ♦ No other expression defines that name Result serves as proxy Advanced Compiler Techniques 23.04.04 09:45:19 3 ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er /
Lazy Code Motion The Name Space ♦ r i +r j → r k , always ( hash to find k ) Lazy Code Motion ♦ We can refer to r i +r j by r k ( bit-vector sets ) ♦ Variables must be set by copies ♦ No consistent definition for a variable ♦ Break the rule for this case, but require r source < r destination ♦ To achieve this, assign register names to variables first Without this name space ♦ L CM must insert copies to preserve redundant values ♦ L CM must compute its own map of expressions to unique ids Advanced Compiler Techniques 23.04.04 09:45:19 4 ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er /
Lazy Code Motion: Running Example B 1 : r 1 ← 1 Variables: r 2 ← r 1 r 2 ,r 4 ,r 8 r 3 ← r 0 + @ m Lazy Code Motion Expressions: r 4 ← r 3 r 1 ,r 3 ,r 5 ,r 6 ,r 7 ,r 20 ,r 21 r 5 ← (r 1 < r 2 ) if r 5 then B 2 else B 3 B 2 : B 1 r 20 ← r 17 *r 18 r 21 ← r 19 +r 20 r 8 ← r 21 B 2 r 6 ← r 2 +1 r 2 ← r 6 r 7 ← (r 2 >r 4 ) B 3 if r 7 then B 3 else B 2 ... B 3 : Advanced Compiler Techniques 23.04.04 09:45:19 5 ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er /
Lazy Code Motion Predicates (computed by Local Analysis) ♦ DEE XPR (b) contains expressions defined in b that survive to the Lazy Code Motion end of b. e ∈ DEE XPR (b) ⇒ evaluating e at the end of b produces the same value for e as evaluating it in its original position. ♦ UEE XPR (b) contains expressions defined in b that have upward exposed arguments (both args). e ∈ UEE XPR (b) ⇒ evaluating e at the start of b produces the same value for e as evaluating it in its original position. ♦ K ILLED E XPR (b) contains those expressions whose arguments are (re)defined in b. e ∈ K ILLED E XPR (b) ⇒ evaluating e at the start of b does not produce the same result as evaluating it at its end. Advanced Compiler Techniques 23.04.04 09:45:19 6 ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er /
Lazy Code Motion: Running Example B 1 : Variables: r 1 ← 1 r 2 ,r 4 ,r 8 r 2 ← r 1 Expressions: r 3 ← r 0 + @ m Lazy Code Motion r 1 ,r 3 ,r 5 ,r 6 ,r 7 ,r 20 ,r 21 r 4 ← r 3 r 5 ← (r 1 < r 2 ) if r 5 then B 2 else B 3 B 1 B 2 B 3 B 2 : DE DEE XP XPR r1, r3, r5 r7, r20, r21 r 20 ← r 17 *r 18 UE UEE XP r1, r3 r6, r20 XPR K ILLE LLED E XP r5, r6, r7 r5, r6, r7, r21 r 21 ← r 19 +r 20 XPR r 8 ← r 21 r 6 ← r 2 +1 r 2 ← r 6 r 7 ← (r 2 >r 4 ) if r 7 then B 3 else B 2 ... B 3 : Advanced Compiler Techniques 23.04.04 09:45:19 7 ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er /
Lazy Code Motion Availability A VAIL I N (n) = ∩ m ∈ preds(n) A VAIL O UT (m), n ≠ n 0 Lazy Code Motion A VAIL O UT (m) = DEE XPR (m) ∪ (A VAIL I N (m) ∩ K ILLED E XPR (m)) Initialize A VAIL I N (n) to the set of all names, except at n 0 Set A VAIL I N (n 0 ) to Ø Interpreting A VAIL ♦ e ∈ A VAIL O UT (b) ⇔ evaluating e at end of b produces the same value for e. A VAIL O UT tells the compiler how far forward e can move the evaluation of e, ignoring any uses of e. ♦ This differs from the way we talk about A VAIL in global redundancy elimination. Advanced Compiler Techniques 23.04.04 09:45:19 8 ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er /
Lazy Code Motion Anticipability A NT O UT (n) = ∩ m ∈ succs(n) A NT I N (m), n not an exit block Lazy Code Motion A NT I N (m) = UEE XPR (m) ∪ (A NT O UT (m) ∩ K ILLED E XPR (m)) Initialize A NT O UT (n) to the set of all names, except at exit blocks Set A NT O UT (n) to Ø, for each exit block n Interpreting A NT O UT ♦ e ∈ A NT I N (b) ⇔ evaluating e at start of b produces the same value for e. A NT I N tells the compiler how far backward e can move ♦ This view shows that anticipability is, in some sense, the inverse of availability (& explains the new interpretation of A VAIL ). Advanced Compiler Techniques 23.04.04 09:45:19 9 ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er /
Lazy Code Motion Earliest placement E ARLIEST (i,j) = A NT I N (j) ∩ A VAIL O UT (i) ∩ (K ILLED E XPR (i) ∪ A NT O UT (i)) Lazy Code Motion E ARLIEST (n 0 ,j) = A NT I N (j) ∩ A VAIL O UT (n 0 ) E ARLIEST is a predicate ♦ Computed for edges rather than nodes ( placement ) ♦ e ∈ E ARLIEST (i,j) if ♦ It can move to head of j, ♦ It is not available at the end of i, and ♦ either it cannot move to the head of i ( K ILLED E XPR (i)) ♦ or another edge leaving i prevents its placement in i ( A NT O UT (i)) Advanced Compiler Techniques 23.04.04 09:45:19 10 ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er /
Lazy Code Motion Later (than earliest) placement L ATER I N (j) = ∩ i ∈ preds(j) L ATER (i,j), j ≠ n 0 Lazy Code Motion L ATER (i,j) = E ARLIEST (i,j) ∪ (L ATER I N (i) ∩ UEE XPR (i)) Initialize L ATER I N (n 0 ) to Ø x ∈ L ATER I N (k) ⇔ every path that reaches k has x ∈ E ARLIEST (m) for some block m, and the path from m to k is x-clear & does not evaluate x. ⇒ the compiler can move x through k without losing any benefit. x ∈ L ATER (i,j) ⇔ <i,j> is its earliest placement, or it can be moved forward from i ( L ATER (i)) and placement at entry to i does not anticipate a use in i ( moving it across the edge exposes that use ). Advanced Compiler Techniques 23.04.04 09:45:19 11 ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er /
Lazy Code Motion Rewriting the code I NSERT (i,j) = L ATER (i,j) ∩ L ATER I N (j) Lazy Code Motion D ELETE (k) = UEE XPR (k) ∩ L ATER I N (k), k ≠ n 0 I NSERT & D ELETE are predicates Compiler uses them to guide the rewrite step ♦ x ∈ I NSERT (i,j) ⇒ insert x at start of i, end of j, or new block ♦ x ∈ D ELETE (k) ⇒ delete first evaluation of x in k If local redundancy elimination has already been performed, only one copy of x exists. Otherwise, remove all upward exposed copies of x. Advanced Compiler Techniques 23.04.04 09:45:19 12 ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er /
Lazy Code Motion Edge placement ♦ x ∈ I NSERT (i,j) Lazy Code Motion B h B i B i B i x x x B j B k B j B j B k | succs (i) > 1 | succs (i)| = 1 | preds (j)| = 1 | preds (j)| > 1 Three cases ♦ | succs (i)| = 1 ⇒ insert x at end of i. ♦ | succs (i)| > 1 but | preds (j)| = 1 ⇒ insert x at start of j. ♦ | succs (i)| > 1 and | preds (j)| > 1 ⇒ create new block in < i,j > for x. Advanced Compiler Techniques 23.04.04 09:45:19 13 ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er /
Lazy Code Motion Example B1 B2 B3 B 1 :r 1 ← 1 DE DEE XP XPR r1, r3, r5 r7, r20, r21 r 2 ← r 1 UEE XP UE XPR r1, r3 r6, r20 B 1 r 3 ← r 0 +@m K IL ILLED E XP XPR r5, r6, r7 r5, r6, r7,r21 r 4 ← r 3 Lazy Code Motion r 5 ← (r 1 <r 2 ) if r 5 then B 2 else B 3 B 2 B1 B2 B3 B 2 :r 20 ← r 17 *r 18 A VA VAIL I N N { } r1, r3 r1, r3 r 21 ← r 19 +r 20 A VA VAIL O UT UT r1, r3, r5 r1, r3, r7, r20, r21 … r 8 ← r 21 A NT NT I N N r1, r3 r6, r20 { } r 6 ← r 2 +1 B 3 A NT NT O UT UT { } { } { } r 2 ← r 6 r 7 ← (r 2 >r 4 ) 1,2 1,3 2,2 2,3 if r 7 then B 3 else B 2 E ARL ARLIEST r20, r21 { } { } { } B 3 :... Example is too small to show off L ATER I NSERT (1,2) = { r 20 , r 21 } D ELETE (2) = { r 20 , r 21 } Advanced Compiler Techniques 23.04.04 09:45:19 14 ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er /
Recommend
More recommend