reaching definitions
play

Reaching Definitions Global Opt: Reaching Definitions Concept of - PDF document

Reaching Definitions Global Opt: Reaching Definitions Concept of definition and use Using Program Analysis a=x+y for Optimization is a definition of a. is a use of x and y. A definition reaches a use if value written Advanced


  1. Reaching Definitions Global Opt: Reaching Definitions ♦ Concept of definition and use Using Program Analysis ♦ a=x+y for Optimization ♦ is a definition of a. ♦ is a use of x and y. ♦ A definition reaches a use if value written Advanced Compiler Techniques 2005 by definition may be read by use. Erik Stenman Virtutech Advanced Compiler Techniques 2 ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er / Reaching Definitions and Computing Reaching Definitions Constant Propagation Global Opt: Reaching Definitions & Constant Prop Global Opt: Reaching Definitions ♦ Compute with sets of definitions: ♦ Is a use of a variable a constant? ♦ Represent sets using bit vectors. ♦ Check all reaching definitions. ♦ Each definition has a position in bit vector. ♦ If all assign variable to same constant. ♦ At each basic block, compute: ♦ Then use is in fact a constant. ♦ Definitions that reach start of block. ♦ Can replace variable with constant. ♦ Definitions that reach end of block. ♦ Do computation by simulating execution of program until the fixed point is reached. Advanced Compiler Techniques Advanced Compiler Techniques 3 4 ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er / ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er / Formalizing Analysis Dataflow Equations Global Opt: Reaching Definitions Global Opt: Reaching Definitions ♦ Each basic block has ♦ IN[b i ] = OUT[b 1 ] ∪ ... ∪ OUT[b n ] ♦ IN - set of definitions that reach beginning of where b 1 , ..., b n are predecessors of b i block ♦ OUT[b i ] = (IN[b i ] - KILL[b i ]) ∪ GEN[b i ] ♦ OUT - set of definitions that reach end of block ♦ IN[entry] = 0000000 ♦ GEN - set of definitions generated in block ♦ KILL - set of definitions killed in the block ♦ Result: system of equations. ♦ Compiler scans each basic block to derive GEN and KILL sets. Advanced Compiler Techniques Advanced Compiler Techniques 5 6 ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er / ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er /

  2. IN[0] = 0000000 Solving Equations GEN[0] = 1110000 KILL[0] = 0000011 s 1 =0; OUT[0] =(IN[0] -KILL[0]) ∪ GEN[0]= ♦ Use fix point algorithm. a 2 =4; 0000000-0000011 ∪ 1110000= 1110000 ♦ Initialize with solution of Global Opt: Reaching Definitions Global Opt: Reaching Definitions i 3 =0; IN[2]=OUT[0] OUT[b i ] = 0000000 k==0 GEN[2] = 0000100 KILL[2] = 0001000 ♦ Repeatedly apply equations: OUT[2]=(IN[2]-0001000) ∪ 0000100 IN[1]=OUT[0] GEN[1] = 0001000 b 4 =1; b 5 =2; ♦ IN[b i ] = OUT[b 1 ] ∪ ... ∪ OUT[b n ] KILL[1] = 0000100 OUT[1]=(IN[1]-0000100) ∪ 0001000 IN[3]=OUT[1] ∪ OUT[2] ♦ OUT[b i ] = (IN[b i ] - KILL[b i ]) ∪ GEN[b i ] GEN[3] = 0000000 i<n KILL[3] = 0000000 ♦ Until reach fixed point, i.e., until equation OUT[3]=IN[3] application has no further effect. s 6 =s+a*b; return s i 7 =i+1; ♦ Use a worklist to track which equation IN[5]=OUT[3] applications may have further effect. IN[4]=OUT[3] GEN[5] = 0000000 GEN[4] = 0000011 KILL[5] = 0000000 KILL[4] = 1010000 OUT[5]=IN[5] OUT[4]=(IN[4]-1010000) ∪ 0000011 Advanced Compiler Techniques Advanced Compiler Techniques 7 8 ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er / ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er / Reaching Definitions Algorithm Questions for all nodes n 2N Global Opt: Reaching Definitions, summary OUT[n] = ; ; // Or OUT[n] = GEN[n]; Global Opt: Reaching Definitions Changed = N ; // N = all nodes in graph ♦ Does the algorithm halt? while (Changed != ; ) // Until fixed point reached. ♦ yes, because transfer function is monotonic. choose n 2 Changed; // Node from worklist ♦ if increase IN, increase OUT. Changed=Changed-{n}; // Remove from worklist ♦ in limit, all bits are 1. OldOut = OUT[n] // Remember old result ♦ If bit is 1, is there always an execution in which IN[n] = ; ; // Calculate IN as join corresponding definition reaches basic block? for all nodes p 2 predecessors (n) // of predecessors. IN[n]=IN[n] ∪ OUT[p]; ♦ If bit is 0, does the corresponding definition ever OUT[n]=(IN[n]-KILL[n]) ∪ GEN[n]; // Recalculate OUT reach basic block? if (OUT[n] != OldOut) // If OUT[n] changed ♦ Concept of conservative analysis. for all nodes s 2 successors (n) Changed=Changed ∪ {s}; //Add succs to worklist Advanced Compiler Techniques Advanced Compiler Techniques 9 10 ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er / ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er / Computing Available Available Expressions Expressions Global Opt: Available Expressions Global Opt: Available Expressions ♦ An expression x+y is available at a point p if ♦ Represent sets of expressions using bit vectors. ♦ every path from the initial node to p evaluates x+y ♦ Each expression corresponds to a bit. before reaching p, ♦ Run dataflow algorithm similar to reaching ♦ and there are no assignments to x or y after the definitions. evaluation but before p. ♦ Big difference: ♦ Available Expression information can be used to ♦ Definition reaches a basic block if it comes from ANY do global (across basic blocks) CSE. predecessor in CFG. ♦ If an expression is available at use, there is no ♦ Expression is available at a basic block only if it is need to re-evaluate it. available from ALL predecessors in CFG. Advanced Compiler Techniques Advanced Compiler Techniques 11 12 ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er / ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er /

  3. Global CSE Transform 0000 0000 a=x+y; a=x+y; t 1 =a; Expressions x==0 Expressions Global Opt: Available Expressions & CSE x==0 1001 1001 1: x+y 1: x+y Global Opt: Available Expressions x=z; 2: i<n 2: i<n x=z; b=x+y; 3: i+c 3: i+c b=x+y; t 1 =b; 4: x==0 4: x==0 1000 1000 i=x+y; i=t 1 ; Must use same temp 1000 1000 for CSE in all blocks i<n i<n 1100 1100 1100 1100 c=x+y; c=t 1 ; d=x+y d=t 1 i=i+c; i=i+c; Advanced Compiler Techniques Advanced Compiler Techniques 13 14 ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er / ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er / Formalizing Analysis Dataflow Equations ♦ Each basic block has ♦ IN[b i ] = OUT[b 1 ] ∩ ... ∩ OUT[b n ] Global Opt: Available Expressions Global Opt: Available Expressions IN - set of expressions that reach beginning of block. ♦ where b 1 , ..., b n are predecessors of b i OUT - set of expressions that reach end of block. ♦ OUT[b i ] = (IN[b i ] - KILL[b i ]) ∪ GEN[b i ] GEN - set of expressions generated in block. ♦ IN[entry] = 0000 KILL - set of expressions killed in the block. ♦ GEN[ x=z; b=x+y ] = 1000 ♦ Result: system of equations. ♦ KILL[ x=z; b=x+y ] = 1001 ♦ Compiler scans each basic block to derive GEN and KILL sets. Advanced Compiler Techniques Advanced Compiler Techniques 15 16 ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er / ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er / Solving Equations Available Expressions Algorithm ♦ Use fix point algorithm. for all nodes n 2N // E is set of all expressions. Global Opt: Available Expressions Global Opt: Available Expressions OUT[n] = E ; // OUT[n] = E -KILL[n]; ♦ IN[entry]=0000 Changed = N ; // N = all nodes in graph ♦ Initialize with solution of while (Changed != ; ) OUT[b i ] = 1111 choose n 2 Changed; Changed=Changed-{n}; ♦ Repeatedly apply equations: IN[n] = E ; ♦ IN[b i ] = OUT[b 1 ] ∩ ... ∩ OUT[b n ] OldOut = OUT[n] ♦ OUT[b i ] = (IN[b i ] - KILL[b i ]) ∪ GEN[b i ] for all nodes p 2 predecessors (n) IN[n]=IN[n] ∩ OUT[p]; ♦ Use a worklist to track which equation OUT[n]=(IN[n]-KILL[n]) ∪ GEN[n]; applications may have further effect. if (OUT[n] != OldOut) for all nodes s 2 successors (n) Changed=Changed ∪ {s}; Advanced Compiler Techniques Advanced Compiler Techniques 17 18 ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er / ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er /

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend