Code Optimization Chapter 9 (1 st  ed. Ch.10) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2009
The Code Optimizer Control flow analysis: control flow graph Data-flow analysis Transformations Front end Code generator Code optimizer Control- flow analysis Data- flow analysis Transfor- mations
Determining Loops in Flow Graphs: Dominators Dominators:  d dom n Node  d  of a CFG  dominates  node  n  if  every  path from the initial node of the CFG to  n  goes through  d The loop entry dominates all nodes in the loop The  immediate dominator m  of a node  n  is the last dominator on the path from the initial node to  n If  d     n  and  d dom n  then  d dom m
Dominator Trees 1 2 3 4 5 6 7 8 9 10 1 2 3 4 6 5 7 8 9 10 CFG Dominator tree
Natural Loops A  back edge  is is an edge  a      b  whose head  b  dominates its tail  a Given a back edge  n      d   The  natural loop  consists of  d  plus the nodes that can reach  n  without going through  d The  loop header  is node  d Unless two loops have the same header, they are disjoint or one is nested within the other A nested loop is an  inner loop  if it contains no other loops
Natural Inner Loops Example 1 2 3 4 5 6 7 8 9 10 1 2 3 4 6 5 7 8 9 10 CFG Dominator tree Natural loop for 7  dom  10 Natural loop for 3  dom  4 Natural loop for 4  dom  7
Natural Outer Loops Example 1 2 3 4 5 6 7 8 9 10 1 2 3 4 6 5 7 8 9 10 CFG Dominator tree Natural loop for 1  dom  9 Natural loop for 3  dom  8
Pre-Headers To facilitate loop transformations, a compiler often adds a  preheader  to a loop Code motion, strength reduction, and other loop transformations populate the preheader Header Header Preheader
Reducible Flow Graphs Reducible graph  = disjoint partition in forward and back edges such that the forward edges form an acyclic (sub)graph 1 2 3 4 Example of a reducible CFG 1 2 3 Example of a nonreducible CFG
Global Data-Flow Analysis To apply global optimizations on basic blocks,  data-flow information  is collected by solving systems of  data-flow equations Suppose we need to determine the  reaching definitions  for a sequence of statements  S out [ S ]  = gen [ S ]     ( in [ S ]   -  kill [ S ]) d1:  i := m-1 d2:  j := n d3:  j := j-1 B1: B2: B3: out [B1] =  gen [B1] = {d1, d2} out [B2] =  gen [B2]    {d1} = {d1, d3} d1 reaches B2 and B3 and d2 reaches B2, but not B3 because d2 is killed in B2
Reaching Definitions S d :  a:=b+c Then, the data-flow equations for  S  are: gen [ S ] = { d } kill [ S ] =  D a  - { d } out [ S ] =  gen [ S ]    ( in [ S ] -  kill [ S ]) where  D a  = all definitions of  a  in the region of code is of the form
Reaching Definitions S gen [ S ] =  gen [ S 2 ]    ( gen [ S 1 ] -  kill [ S 2 ]) kill [ S ] =  kill [ S 2 ]    ( kill [ S 1 ] -  gen [ S 2 ]) in [ S 1 ] =  in [ S ] in [ S 2 ] =  out [ S 1 ] out [ S ] =  out [ S 2 ] is of the form S 2 S 1
Reaching Definitions S gen [ S ] =  gen [ S 1 ]     gen [ S 2 ]  kill [ S ] =  kill [ S 1 ]     kill [ S 2 ] in [ S 1 ] =  in [ S ] in [ S 2 ] =  in [ S ] out [ S ] =  out [ S 1 ]     out [ S 2 ] is of the form S 2 S 1
Reaching Definitions S gen [ S ] =  gen [ S 1 ]  kill [ S ] =  kill [ S 1 ] in [ S 1 ] =  in [ S ]     gen [ S 1 ] out [ S ] =  out [ S 1 ] is of the form S 1
Example Reaching Definitions d 1 :  i := m-1; d 2 :  j := n; d 3 :  a := u1;   do d 4 :  i := i+1; d 5 :  j := j-1;   if e1 then d 6 :  a := u2   else d 7 :  i := u3   while e2 ; gen ={ d 1 } kill ={ d 4 ,  d 7 } d 1 gen ={ d 2 } kill ={ d 5 } d 2 gen ={ d 1 , d 2 } kill ={ d 4 , d 5 , d 7 } ; d 3 gen ={ d 3 } kill ={ d 6 } gen ={ d 1 , d 2 , d 3 } kill ={ d 4 , d 5 , d 6 , d 7 } ; gen ={ d 3 , d 4 , d 5 , d 6 , d 7 } kill ={ d 1 , d 2 } do ; gen ={ d 4 } kill ={ d 1 ,  d 7 } d 4 ; gen ={ d 5 } kill ={ d 2 } d 5 if e1 d 6 d 7 e1 gen ={ d 6 } kill ={ d 3 } gen ={ d 7 } kill ={ d 1 , d 4 } gen ={ d 4 , d 5 } kill ={ d 1 , d 2 , d 7 } gen ={ d 4 , d 5 , d 6 , d 7 } kill ={ d 1 , d 2 } gen ={ d 4 , d 5 , d 6 , d 7 } kill ={ d 1 , d 2 } gen ={ d 6 , d 7 } kill ={}
Using Bit-Vectors to Compute Reaching Definitions d 1 :  i := m-1; d 2 :  j := n; d 3 :  a := u1;   do d 4 :  i := i+1; d 5 :  j := j-1;   if e1 then d 6 :  a := u2   else d 7 :  i := u3   while e2 ; d 1 d 2 ; d 3 ; 0011111 1100000 do ; d 4 ; d 5 if e1 d 6 d 7 e1 1110000 0001111 1100000 0001101 1000000 0001001 0100000 0000100 0010000 0000010 0001111 1100000 0001111 1100000 0001100 1100001 0001000 1000001 0000100 0100000 0000010 0010000 0000001 1001000 0000011 0000000
Accuracy, Safeness, and Conservative Estimations Conservative : refers to making safe assumptions when insufficient information is available at compile time, i.e. the compiler has to guarantee not to change the meaning of the optimized code Safe : refers to the fact that a superset of reaching definitions is safe (some may have been killed) Accuracy : more and better information enables more code optimizations
Reaching Definitions are a Conservative (Safe) Estimation S 2 S 1 Suppose this branch is never taken Estimation: gen [ S ] =  gen [ S 1 ]     gen [ S 2 ]  kill [ S ] =  kill [ S 1 ]     kill [ S 2 ] Accurate: gen’ [ S ] =  gen [ S 1 ] ⊆  gen [ S ]  kill’ [ S ] =  kill [ S 1 ] ⊇  kill [ S ]
Reaching Definitions are a Conservative (Safe) Estimation in [ S 1 ] =  in [ S ]     gen [ S 1 ] S 1 Why  gen ? S is of the form The problem is that in [ S 1 ] =  in [ S ]     out [ S 1 ] makes more sense, but we cannot solve this  directly, because  out [ S 1 ] depends on  in [ S 1 ]
Reaching Definitions are a Conservative (Safe) Estimation d :  a:=b+c We have: (1)  in [ S 1 ] =  in [ S ]     out [ S 1 ] (2)  out [ S 1 ] =  gen [ S 1 ]    ( in [ S 1 ] -  kill [ S 1 ]) Solve  in [ S 1 ] and  out [ S 1 ] by estimating  in 1 [ S 1 ] using safe but approximate  out [ S 1 ]=  , then re-compute  out 1 [ S 1 ] using (2) to estimate  in 2 [ S 1 ], etc. in 1 [ S 1 ] = (1)   in [ S ]     out [ S 1 ] =  in [ S ] out 1 [ S 1 ] = (2)   gen [ S 1 ]    ( in 1 [ S 1 ] -  kill [ S 1 ]) =  gen [ S 1 ]    ( in [ S ] -  kill [ S 1 ]) in 2 [ S 1 ] = (1)   in [ S ]     out 1 [ S 1 ] =  in [ S ]     gen [ S 1 ]    ( in [ S ] -  kill [ S 1 ]) =  in [ S ]     gen [ S 1 ]  out 2 [ S 1 ] = (2)   gen [ S 1 ]    ( in 2 [ S 1 ] -  kill [ S 1 ]) =  gen [ S 1 ]    ( in [ S ]     gen [ S 1 ] -  kill [ S 1 ]) =  gen [ S 1 ]    ( in [ S ] -  kill [ S 1 ])  Because  out 1 [ S 1 ] =  out 2 [ S 1 ], and therefore  in 3 [ S 1 ] =  in 2 [ S 1 ], we conclude that in [ S 1 ] =  in [ S ]     gen [ S 1 ]

More Related Content

PDF
PFDS 6.4.3
PPTX
Minimax Algorithm
DOC
Niem khuccuoi kyam
PDF
Reconstructing Textual Documents from n-grams
PDF
PRE: Datamining 2nd R
PDF
Datamining R 1st
PPTX
PDF
Datamining r 1st
PFDS 6.4.3
Minimax Algorithm
Niem khuccuoi kyam
Reconstructing Textual Documents from n-grams
PRE: Datamining 2nd R
Datamining R 1st
Datamining r 1st

What's hot (19)

PDF
ODP
Surds & indices in business mathematics
PDF
ゲーム理論BASIC 第20回 -無限回繰り返しゲーム-
PDF
Boudourides & Lenis, Distribution of Groups of Vertices Across Multilayer Net...
PDF
ゲーム理論NEXT コア第4回(最終回) -平衡ゲームとコア-
PDF
Multilayerity within multilayerity? On multilayer assortativity in social net...
PDF
Potencias resueltas 1eso (1)
PPT
Chapter 6
PDF
Short version of Dominating Sets, Multiple Egocentric Networks and Modularity...
PPT
The chain rule
PDF
Topics of Complex Social Networks: Domination, Influence and Assortativity
PDF
Dominating Sets, Multiple Egocentric Networks and Modularity Maximizing Clust...
PDF
Maths ms
PDF
solucionario de purcell 3
PDF
ゲーム理論BASIC 演習24 -公理から求めるナッシュ交渉解-
PPTX
Quadratic Expressions
PPT
2621008 - C++ 4
PDF
solucionario de purcell 2
PDF
Capitulo 4 Soluciones Purcell 9na Edicion
Surds & indices in business mathematics
ゲーム理論BASIC 第20回 -無限回繰り返しゲーム-
Boudourides & Lenis, Distribution of Groups of Vertices Across Multilayer Net...
ゲーム理論NEXT コア第4回(最終回) -平衡ゲームとコア-
Multilayerity within multilayerity? On multilayer assortativity in social net...
Potencias resueltas 1eso (1)
Chapter 6
Short version of Dominating Sets, Multiple Egocentric Networks and Modularity...
The chain rule
Topics of Complex Social Networks: Domination, Influence and Assortativity
Dominating Sets, Multiple Egocentric Networks and Modularity Maximizing Clust...
Maths ms
solucionario de purcell 3
ゲーム理論BASIC 演習24 -公理から求めるナッシュ交渉解-
Quadratic Expressions
2621008 - C++ 4
solucionario de purcell 2
Capitulo 4 Soluciones Purcell 9na Edicion
Ad

Similar to Ch10 (20)

DOCX
Divide-and-Conquer & Dynamic ProgrammingDivide-and-Conqu.docx
PDF
Obj. 7 Midpoint and Distance Formulas
PDF
Succesive differntiation
PPT
Linear differential equation with constant coefficient
PDF
Rsa documentation
PDF
Mathematical Modelling of Electro-Mechanical System in Matlab
PDF
第13回数学カフェ「素数!!」二次会 LT資料「乱数!!」
PDF
Understanding Reed-Solomon code
PDF
09 p.t (straight line + circle) solution
PPT
ADVANCED ALGORITHMS-UNIT-3-Final.ppt
PPT
Cryptography and Network Security chapter 4.ppt
PDF
3 complex numbers part 3 of 3
PPTX
K means clustering
PPT
Sequences, mathematical induction and recursion
PPT
E-All-pairs-Floyd.pptE-All-pairs-Floyd.ppt
PPT
PDF
On derivations of black scholes greek letters
PDF
The International Journal of Engineering and Science (IJES)
PDF
presentation
PPTX
Starr pvt. ltd. rachit's group ppt (1)
Divide-and-Conquer & Dynamic ProgrammingDivide-and-Conqu.docx
Obj. 7 Midpoint and Distance Formulas
Succesive differntiation
Linear differential equation with constant coefficient
Rsa documentation
Mathematical Modelling of Electro-Mechanical System in Matlab
第13回数学カフェ「素数!!」二次会 LT資料「乱数!!」
Understanding Reed-Solomon code
09 p.t (straight line + circle) solution
ADVANCED ALGORITHMS-UNIT-3-Final.ppt
Cryptography and Network Security chapter 4.ppt
3 complex numbers part 3 of 3
K means clustering
Sequences, mathematical induction and recursion
E-All-pairs-Floyd.pptE-All-pairs-Floyd.ppt
On derivations of black scholes greek letters
The International Journal of Engineering and Science (IJES)
presentation
Starr pvt. ltd. rachit's group ppt (1)
Ad

Recently uploaded (20)

PPTX
Modernising the Digital Integration Hub
PPTX
Chapter 5: Probability Theory and Statistics
PDF
How IoT Sensor Integration in 2025 is Transforming Industries Worldwide
PPTX
2018-HIPAA-Renewal-Training for executives
PPTX
TEXTILE technology diploma scope and career opportunities
PPT
What is a Computer? Input Devices /output devices
PDF
Getting started with AI Agents and Multi-Agent Systems
PPTX
Microsoft Excel 365/2024 Beginner's training
PPTX
Benefits of Physical activity for teenagers.pptx
DOCX
search engine optimization ppt fir known well about this
PDF
A proposed approach for plagiarism detection in Myanmar Unicode text
PDF
The influence of sentiment analysis in enhancing early warning system model f...
PDF
Credit Without Borders: AI and Financial Inclusion in Bangladesh
PPTX
Configure Apache Mutual Authentication
PDF
Consumable AI The What, Why & How for Small Teams.pdf
PDF
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
PDF
Produktkatalog für HOBO Datenlogger, Wetterstationen, Sensoren, Software und ...
PDF
sustainability-14-14877-v2.pddhzftheheeeee
PPTX
Build Your First AI Agent with UiPath.pptx
PDF
Five Habits of High-Impact Board Members
Modernising the Digital Integration Hub
Chapter 5: Probability Theory and Statistics
How IoT Sensor Integration in 2025 is Transforming Industries Worldwide
2018-HIPAA-Renewal-Training for executives
TEXTILE technology diploma scope and career opportunities
What is a Computer? Input Devices /output devices
Getting started with AI Agents and Multi-Agent Systems
Microsoft Excel 365/2024 Beginner's training
Benefits of Physical activity for teenagers.pptx
search engine optimization ppt fir known well about this
A proposed approach for plagiarism detection in Myanmar Unicode text
The influence of sentiment analysis in enhancing early warning system model f...
Credit Without Borders: AI and Financial Inclusion in Bangladesh
Configure Apache Mutual Authentication
Consumable AI The What, Why & How for Small Teams.pdf
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
Produktkatalog für HOBO Datenlogger, Wetterstationen, Sensoren, Software und ...
sustainability-14-14877-v2.pddhzftheheeeee
Build Your First AI Agent with UiPath.pptx
Five Habits of High-Impact Board Members

Ch10

  • 1. Code Optimization Chapter 9 (1 st ed. Ch.10) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2009
  • 2. The Code Optimizer Control flow analysis: control flow graph Data-flow analysis Transformations Front end Code generator Code optimizer Control- flow analysis Data- flow analysis Transfor- mations
  • 3. Determining Loops in Flow Graphs: Dominators Dominators: d dom n Node d of a CFG dominates node n if every path from the initial node of the CFG to n goes through d The loop entry dominates all nodes in the loop The immediate dominator m of a node n is the last dominator on the path from the initial node to n If d  n and d dom n then d dom m
  • 4. Dominator Trees 1 2 3 4 5 6 7 8 9 10 1 2 3 4 6 5 7 8 9 10 CFG Dominator tree
  • 5. Natural Loops A back edge is is an edge a  b whose head b dominates its tail a Given a back edge n  d The natural loop consists of d plus the nodes that can reach n without going through d The loop header is node d Unless two loops have the same header, they are disjoint or one is nested within the other A nested loop is an inner loop if it contains no other loops
  • 6. Natural Inner Loops Example 1 2 3 4 5 6 7 8 9 10 1 2 3 4 6 5 7 8 9 10 CFG Dominator tree Natural loop for 7 dom 10 Natural loop for 3 dom 4 Natural loop for 4 dom 7
  • 7. Natural Outer Loops Example 1 2 3 4 5 6 7 8 9 10 1 2 3 4 6 5 7 8 9 10 CFG Dominator tree Natural loop for 1 dom 9 Natural loop for 3 dom 8
  • 8. Pre-Headers To facilitate loop transformations, a compiler often adds a preheader to a loop Code motion, strength reduction, and other loop transformations populate the preheader Header Header Preheader
  • 9. Reducible Flow Graphs Reducible graph = disjoint partition in forward and back edges such that the forward edges form an acyclic (sub)graph 1 2 3 4 Example of a reducible CFG 1 2 3 Example of a nonreducible CFG
  • 10. Global Data-Flow Analysis To apply global optimizations on basic blocks, data-flow information is collected by solving systems of data-flow equations Suppose we need to determine the reaching definitions for a sequence of statements S out [ S ] = gen [ S ]  ( in [ S ] - kill [ S ]) d1: i := m-1 d2: j := n d3: j := j-1 B1: B2: B3: out [B1] = gen [B1] = {d1, d2} out [B2] = gen [B2]  {d1} = {d1, d3} d1 reaches B2 and B3 and d2 reaches B2, but not B3 because d2 is killed in B2
  • 11. Reaching Definitions S d : a:=b+c Then, the data-flow equations for S are: gen [ S ] = { d } kill [ S ] = D a - { d } out [ S ] = gen [ S ]  ( in [ S ] - kill [ S ]) where D a = all definitions of a in the region of code is of the form
  • 12. Reaching Definitions S gen [ S ] = gen [ S 2 ]  ( gen [ S 1 ] - kill [ S 2 ]) kill [ S ] = kill [ S 2 ]  ( kill [ S 1 ] - gen [ S 2 ]) in [ S 1 ] = in [ S ] in [ S 2 ] = out [ S 1 ] out [ S ] = out [ S 2 ] is of the form S 2 S 1
  • 13. Reaching Definitions S gen [ S ] = gen [ S 1 ]  gen [ S 2 ] kill [ S ] = kill [ S 1 ]  kill [ S 2 ] in [ S 1 ] = in [ S ] in [ S 2 ] = in [ S ] out [ S ] = out [ S 1 ]  out [ S 2 ] is of the form S 2 S 1
  • 14. Reaching Definitions S gen [ S ] = gen [ S 1 ] kill [ S ] = kill [ S 1 ] in [ S 1 ] = in [ S ]  gen [ S 1 ] out [ S ] = out [ S 1 ] is of the form S 1
  • 15. Example Reaching Definitions d 1 : i := m-1; d 2 : j := n; d 3 : a := u1; do d 4 : i := i+1; d 5 : j := j-1; if e1 then d 6 : a := u2 else d 7 : i := u3 while e2 ; gen ={ d 1 } kill ={ d 4 , d 7 } d 1 gen ={ d 2 } kill ={ d 5 } d 2 gen ={ d 1 , d 2 } kill ={ d 4 , d 5 , d 7 } ; d 3 gen ={ d 3 } kill ={ d 6 } gen ={ d 1 , d 2 , d 3 } kill ={ d 4 , d 5 , d 6 , d 7 } ; gen ={ d 3 , d 4 , d 5 , d 6 , d 7 } kill ={ d 1 , d 2 } do ; gen ={ d 4 } kill ={ d 1 , d 7 } d 4 ; gen ={ d 5 } kill ={ d 2 } d 5 if e1 d 6 d 7 e1 gen ={ d 6 } kill ={ d 3 } gen ={ d 7 } kill ={ d 1 , d 4 } gen ={ d 4 , d 5 } kill ={ d 1 , d 2 , d 7 } gen ={ d 4 , d 5 , d 6 , d 7 } kill ={ d 1 , d 2 } gen ={ d 4 , d 5 , d 6 , d 7 } kill ={ d 1 , d 2 } gen ={ d 6 , d 7 } kill ={}
  • 16. Using Bit-Vectors to Compute Reaching Definitions d 1 : i := m-1; d 2 : j := n; d 3 : a := u1; do d 4 : i := i+1; d 5 : j := j-1; if e1 then d 6 : a := u2 else d 7 : i := u3 while e2 ; d 1 d 2 ; d 3 ; 0011111 1100000 do ; d 4 ; d 5 if e1 d 6 d 7 e1 1110000 0001111 1100000 0001101 1000000 0001001 0100000 0000100 0010000 0000010 0001111 1100000 0001111 1100000 0001100 1100001 0001000 1000001 0000100 0100000 0000010 0010000 0000001 1001000 0000011 0000000
  • 17. Accuracy, Safeness, and Conservative Estimations Conservative : refers to making safe assumptions when insufficient information is available at compile time, i.e. the compiler has to guarantee not to change the meaning of the optimized code Safe : refers to the fact that a superset of reaching definitions is safe (some may have been killed) Accuracy : more and better information enables more code optimizations
  • 18. Reaching Definitions are a Conservative (Safe) Estimation S 2 S 1 Suppose this branch is never taken Estimation: gen [ S ] = gen [ S 1 ]  gen [ S 2 ] kill [ S ] = kill [ S 1 ]  kill [ S 2 ] Accurate: gen’ [ S ] = gen [ S 1 ] ⊆ gen [ S ] kill’ [ S ] = kill [ S 1 ] ⊇ kill [ S ]
  • 19. Reaching Definitions are a Conservative (Safe) Estimation in [ S 1 ] = in [ S ]  gen [ S 1 ] S 1 Why gen ? S is of the form The problem is that in [ S 1 ] = in [ S ]  out [ S 1 ] makes more sense, but we cannot solve this directly, because out [ S 1 ] depends on in [ S 1 ]
  • 20. Reaching Definitions are a Conservative (Safe) Estimation d : a:=b+c We have: (1) in [ S 1 ] = in [ S ]  out [ S 1 ] (2) out [ S 1 ] = gen [ S 1 ]  ( in [ S 1 ] - kill [ S 1 ]) Solve in [ S 1 ] and out [ S 1 ] by estimating in 1 [ S 1 ] using safe but approximate out [ S 1 ]=  , then re-compute out 1 [ S 1 ] using (2) to estimate in 2 [ S 1 ], etc. in 1 [ S 1 ] = (1) in [ S ]  out [ S 1 ] = in [ S ] out 1 [ S 1 ] = (2) gen [ S 1 ]  ( in 1 [ S 1 ] - kill [ S 1 ]) = gen [ S 1 ]  ( in [ S ] - kill [ S 1 ]) in 2 [ S 1 ] = (1) in [ S ]  out 1 [ S 1 ] = in [ S ]  gen [ S 1 ]  ( in [ S ] - kill [ S 1 ]) = in [ S ]  gen [ S 1 ] out 2 [ S 1 ] = (2) gen [ S 1 ]  ( in 2 [ S 1 ] - kill [ S 1 ]) = gen [ S 1 ]  ( in [ S ]  gen [ S 1 ] - kill [ S 1 ]) = gen [ S 1 ]  ( in [ S ] - kill [ S 1 ]) Because out 1 [ S 1 ] = out 2 [ S 1 ], and therefore in 3 [ S 1 ] = in 2 [ S 1 ], we conclude that in [ S 1 ] = in [ S ]  gen [ S 1 ]