This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Common Sub-expression elimination Copy Propagation Dead-code elimination
Peephole optimizationPeephole optimization Load/Store elimination Unreachable code Flow of Control Optimization Algebraic simplification Strength Reduction
CH10.4
CSE4100
Where can Optimation Occur?Where can Optimation Occur?
Software Engineer can:Software Engineer can: Profile Program Change Algorithm Data Transform/Improve Loops
Front EndLA, Parse,Int. Code
Code Generator
Int. Code TargetProgram
SourceProgram
Compiler Can:Compiler Can: Improve Loops/Proc
Calls Calculate Addresses Use Registers Selected Instructions Perform Peephole Opt.
All are OptimizationsAll are Optimizations 1st is User Controlled and Defined At Intermediate Code Level by Compiler At Assembly Level for Target Architecture (to take
advantage of different machine features)
CH10.6
CSE4100
First Look at OptimizationFirst Look at Optimization Optimization Applied to 3 Address Coding (3AC) Optimization Applied to 3 Address Coding (3AC)
Version of Source Program - Examples:Version of Source Program - Examples:
CH10.7
CSE4100
First Look at OptimizationFirst Look at Optimization Identify each Basic Block which
Represents a set of Three Address Statements whereExecution Enters at Top and
Leaves at BottomNo Branches within Code
Represent the Control FlowDependencies Among and
Between Basic BlocksDefines what is Termed a “Flow
Graph”
CH10.8
CSE4100
First Look at OptimizationFirst Look at Optimization Steps 1 to 12 from two Slides Back Represented as:Steps 1 to 12 from two Slides Back Represented as:
Optimization Works with Basic Blocks and Flow Optimization Works with Basic Blocks and Flow Graph to Perform Transformations that:Graph to Perform Transformations that:
CH10.9
CSE4100
First Look at OptimizationFirst Look at Optimization
Optimization will Perform Optimization will Perform Transformations on Basic Transformations on Basic Blocks/Flow GraphBlocks/Flow Graph
Resulting Graph(s) Passed Resulting Graph(s) Passed Through to Final Code Generation Through to Final Code Generation to Obtain More Optimal Codeto Obtain More Optimal Code
Two Fold Goal of OptimizationTwo Fold Goal of OptimizationReduce TimeReduce Space
CH10.10
CSE4100
First Look at OptimizationFirst Look at Optimization
Two Types of TransformationsTwo Types of Transformations Structure Preserving
Inherent Structure and Implicit Functionality of Basic Blocks is Unchanged
Renaming Temporary VariablesRenaming Temporary Variables Consider the code t = b + c Can be Changed to u = b + c May Reduce the Number of temporaries Make Change from all t’s to all u’s
Interchange of StatementsInterchange of Statements Consider and Change to:
t1 = b + c t2 = x + yt2 = x + y t1 = b + c
This can Occur as Long as: x and y not t1 b and c not t2
CH10.13
CSE4100
Requirements for OptimizationRequirements for Optimization
CH10.14
CSE4100
The Overall Optimization ProcessThe Overall Optimization Process
AdvantagesAdvantages Intermediate Code has Explicit Operations and Their
Identification Promotes Optimization Intermediate Code is Relatively Machine Independent Therefore, Optimization Doesn’t Impact Final Code
Generation
CH10.15
CSE4100
Example Source CodeExample Source Code
CH10.16
CSE4100
Generated Three Address CodingGenerated Three Address Coding
CH10.17
CSE4100
Flow Graph of Basic BlocksFlow Graph of Basic Blocks
CH10.18
CSE4100
Indepth Examination of OptimizationIndepth Examination of Optimization
Code-Transformation Techniques:Code-Transformation Techniques: Local – within a “Basic Block” Global – between “Basic Blocks”
Data Flow Dependencies Determined by Data Flow Dependencies Determined by InspectionInspection
what do i, a, and v refer to? what do i, a, and v refer to? Dependent in Another Basic BlockDependent in Another Basic Block Scoping is Very CriticalScoping is Very Critical
CH10.19
CSE4100
Indepth Examination of OptimizationIndepth Examination of Optimization
Function Preserving TransformationsFunction Preserving Transformations Common Subexpressions Copy Propagation Deal Code Elimination Constant Folding
What about B6?What about B6? t11 and t12 t13 and t15
Similar Savings as in B5Similar Savings as in B5t11 := 4 * ix := a[t11] t12 := 4 * it13 := 4 * nt14 := a[t13] a[t12]:= t14 t15 := 4 * na[t15]:= x
t11 := 4 * ix := a[t11] t13 := 4 * nt14 := a[t13] a[t11]:= t14 a[t13]:= x
CH10.22
CSE4100
Common Sub-ExpressionsCommon Sub-Expressions What else Can be Accomplished?What else Can be Accomplished? Where is Variable j Determined?Where is Variable j Determined?
In B3 – and when drop through B3 to B4 and into B5, no change occurs to j!
What Does B5 Become?What Does B5 Become? t9 same as t5!t9 same as t5! Again savings in access, variables, Again savings in access, variables,
Common Sub-ExpressionsCommon Sub-Expressions B6 is Similarly Changed ….B6 is Similarly Changed ….
t11 := 4 * ix := a[t11] t13 := 4 * nt14 := a[t13] a[t11]:= t14 a[t13]:= x
x := t3 t14 := a[t1] a[t2]:= t14 a[t1]:= x
CH10.25
CSE4100
Resulting Flow DiagramResulting Flow Diagram
CH10.26
CSE4100
Copy PropagationCopy Propagation Introduce a Common Copy Statement to Replace Introduce a Common Copy Statement to Replace
an Arithmetic Calculation with Assignmentan Arithmetic Calculation with Assignment
Regardless of the Path Chosen, the use of an Regardless of the Path Chosen, the use of an Assignment Saves Time and SpaceAssignment Saves Time and Space
a:= d + ea:= t
a:= d + e b:= d + e
c:= d + e
b:= d + ea:= t
c:= t
CH10.27
CSE4100
Copy PropagationCopy Propagation In our Example for B5 and B6 Below:In our Example for B5 and B6 Below:
Since x is t3, we can replace the use of x on right hand Since x is t3, we can replace the use of x on right hand side as below:side as below:
x := t3 t14 := a[t1] a[t2]:= t14 a[t1]:= x
x := t3 a[t2] := t5 a[t4]:= xGoto B2
x := t3 t14 := a[t1] a[t2] := t14 a[t1] := t3
x := t3 a[t2] := t5 a[t4] := t3Goto B2
CH10.28
CSE4100
Dead Code EliminationDead Code Elimination Variable is “Dead” if its Value will never be Utilized Variable is “Dead” if its Value will never be Utilized
Again SubsequentlyAgain Subsequently Otherwise, Variable is “Live”Otherwise, Variable is “Live” What’s True about B5 and B6?What’s True about B5 and B6?
Can Any Statements be Eliminated? Which Ones? Can Any Statements be Eliminated? Which Ones? Why?Why?
B5 and B6 are Now Optimized withB5 and B6 are Now Optimized with B5 has 9 Statements Reduced to 3 B56 has 8 Statements Reduced to 3
x := t3 t14 := a[t1] a[t2] := t14 a[t1] := t3
x := t3 a[t2] := t5 a[t4] := t3Goto B2
CH10.29
CSE4100
Loop OptimizationsLoop Optimizations Three Types: Code Motion, Induction Variables, and Three Types: Code Motion, Induction Variables, and