CH10.1 CSE 4100 Chap 10: Optimization Chap 10: Optimization Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut 371 Fairfield Way, Unit 2155 Storrs, CT 06269-3155 [email protected]http://www.engr.uconn.edu/~steve (860) 486 - 4818 Material for course thanks to: Laurent Michel Aggelos Kiayias Robert LeBarre
59
Embed
CH10.1 CSE 4100 Chap 10: Optimization Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut 371 Fairfield Way,
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.
Transcript
CH10.1
CSE4100
Chap 10: OptimizationChap 10: Optimization
Prof. Steven A. Demurjian Computer Science & Engineering Department
The University of Connecticut371 Fairfield Way, Unit 2155
MotivationMotivation What we achievedWhat we achieved
We have working machine code What is missingWhat is missing
Code generation does not see the “big” picture We can generate poor instruction sequences
What we needWhat we need A simple way to locally improve the code quality
Goal:Goal: Transition from “Lousy” Intermediate Code to
More Effective and Efficient Code Response Time, Performance (Algorithms), Memory
Usage Measured in terms of Number of Variables Saved,
Operands Saved, Memory Accesses, etc.
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.5
CSE4100
Code Level OptimizationCode Level Optimization First Look at OptimizationFirst Look at Optimization
Section 9.4 in 1st Edition Introduce and Discuss Basic Blocks
Requirements for OptimizationRequirements for Optimization Section 10.1 in 1st Edition Basic Blocks, Flow Graphs
Indepth Examination of OptimizationIndepth Examination of Optimization Section 10.2 in 1st Edition Function Preserving Transformations Loop Optimizations
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: A + B[i] * c t1 = b[i]
t2 = t1 * at3 = t2 * c
CH10.7
CSE4100
First Look at OptimizationFirst Look at Optimization Once Code has been Generated in 3AC, an Algorithm Once Code has been Generated in 3AC, an Algorithm
can be Applied to:can be Applied to: Identify each Basic Block which Represents a set of
Three Address Statements where Execution Enters at Top and Leaves at Bottom No Branches within Code
Represent the Control Flow Dependencies Among and Between Basic Blocks Defines what is Termed a “Flow Graph”
Let’s see an ExampleLet’s see an Example
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: Generate Equivalent Flow Graph w/Improved Perf.
CH10.9
CSE4100
First Look at OptimizationFirst Look at Optimization Optimization will Perform Transformations on Basic Optimization will Perform Transformations on Basic
Blocks/Flow GraphBlocks/Flow Graph Resulting Graph(s) Passed Through to Final Code Resulting Graph(s) Passed Through to Final Code
Generation to Obtain More Optimal CodeGeneration to Obtain More Optimal Code Two Fold Goal of OptimizationTwo Fold Goal of Optimization
Reduce Time Reduce Space
Optimization Used to Come at a Cost:Optimization Used to Come at a Cost: In “Old Days” Turning on Optimizer Could Double
the Compilation Time From 2 hours to 4 hours
Is this an Issue Today?Is this an Issue Today?
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 Algebraic
Elimination of Useless Expressionsx = x + 0 or y = y * 1
Replace Expensive OperatorsChange x = y ** 2 to x = y * yWhy?
We’ll Focus on Both …
CH10.11
CSE4100
Structure Preserving TransformationsStructure Preserving Transformations Common Sub-Expression EliminationCommon Sub-Expression Elimination
How can Following Code be Improved?a = b + cb = a – dc = b + cd = a – d
What Must Make Sure Doesn’t happen? Dead-Code EliminationDead-Code Elimination
If x is not Used in Block, Can it be Removed?x = y + z
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
What Do you have to Check?
CH10.13
CSE4100
Requirements for OptimizationRequirements for Optimization Identify Frequently Executed Portions of Code and Identify Frequently Executed Portions of Code and
Make them Perform BetterMake them Perform Better Rule-of-Thumb - Most Programs spend 80% of their Rule-of-Thumb - Most Programs spend 80% of their
Time in 20% of Code – Is this True?Time in 20% of Code – Is this True? We Focus on Loops since Every Gain in Space or We Focus on Loops since Every Gain in Space or
Time is Multiplied by Loop IterationsTime is Multiplied by Loop Iterations Reduce Loop’s Code and Improve Performance
What Other Programming Technique Should be a What Other Programming Technique Should be a Major Concern for Optimization?Major Concern for Optimization?
CH10.14
CSE4100
Requirements for OptimizationRequirements for Optimization Criteria for TransformationsCriteria for Transformations
Preserve Meaning of CodeDon’t Change Output, Introduce Errors, etc.
Speed up Programs by Measurable Amount(on Average for Entire Code)
Must be Work the EffortStick to Meaningful, Useful Transformations
Provide Different Versions of Compiler Non-Optimizing Optimizing Extra Optimization on Demand
CH10.15
CSE4100
Requirements for OptimizationRequirements for Optimization
Beware that Some Optimization Directives are Beware that Some Optimization Directives are Ignored!Ignored! In C, Define variable as “register int I;” While a Feature of Language, cc States that these
Instructions are Ignored and Compiler Controls Use of Registers
CH10.16
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.17
CSE4100
Example Source CodeExample Source Code
CH10.18
CSE4100
Generated Three Address CodingGenerated Three Address Coding
CH10.19
CSE4100
Flow Graph of Basic BlocksFlow Graph of Basic Blocks
CH10.20
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 InspectionData Flow Dependencies Determined by Inspection
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.21
CSE4100
Indepth Examination of OptimizationIndepth Examination of Optimization Function Preserving TransformationsFunction Preserving Transformations
Common Subexpressions Copy Propagation Deal Code Elimination
t11 := 4 * ix := a[t11] t13 := 4 * nt14 := a[t13] a[t11]:= t14 a[t13]:= x
CH10.24
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? Are we done? No t9 same as t5!Are we done? No 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.27
CSE4100
Resulting Flow DiagramResulting Flow Diagram
CH10.28
CSE4100
Copy PropagationCopy Propagation Introduce a Common Copy Statement to Replace an Introduce a Common Copy Statement to Replace an
Arithmetic Calculation with AssignmentArithmetic 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 + e b:= d + e
c:= d + e
a:= d + ea:= t
b:= d + ea:= t
c:= t
CH10.29
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:
We’ll come back to this shortly!We’ll come back to this shortly!
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.30
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.31
CSE4100
Loop OptimizationsLoop Optimizations Three Types: Code Motion, Induction Variables, and Three Types: Code Motion, Induction Variables, and
Flow of ControlFlow of Control What can be doneWhat can be done
Collapse the chain
if (x relop y) goto L4....
L2: goto L4L3: ....L4:
L4_BLOCK
if (x relop y) goto L4....
L2: goto L4L3: ....L4:
L4_BLOCK
CH10.56
CSE4100
Algebraic SimplificationAlgebraic Simplification Simple IdeaSimple Idea
Use algebraic rules to rewrite some code ExamplesExamples
x := y + 0x := y + 0
x := yx := y
x := y * 1x := y * 1
x := yx := y
x := y * 0x := y * 0
x := 0x := 0
CH10.57
CSE4100
Strength ReductionStrength Reduction IdeaIdea
Replace expensive operation By semantically equivalent cheaper ones.
ExamplesExamples Multiplication by 2 is equivalent to a left shift Left shift is much faster
CH10.58
CSE4100
Hardware IdiomHardware Idiom IdeaIdea
Replace expensive instructions by... Equivalent instruction that are optimized for the
platform ExampleExample
add eax,1add eax,1
inceax
inceax
CH10.59
CSE4100
Concluding Remarks/Looking AheadConcluding Remarks/Looking Ahead Optimization Techniques/Concepts are Not Only Optimization Techniques/Concepts are Not Only
Relevant to Programming LanguagesRelevant to Programming Languages Database Systems do Optimization to Reduce Access Database Systems do Optimization to Reduce Access
to Secondary Storageto Secondary Storage Concern when Asking for too Much Data Joining Three or More Tables at Once Doing a Cartesian Product Instead of a Join Doing Selections before Joins Termed Query Optimization