introduction to code optimization comp 412 copyright 2010, keith d. cooper & linda torczon, all...
TRANSCRIPT
![Page 1: Introduction to Code Optimization Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice](https://reader035.vdocuments.net/reader035/viewer/2022071706/56649d345503460f94a0b09d/html5/thumbnails/1.jpg)
Introduction to Code Optimization
Comp 412
Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved.
Students enrolled in Comp 412 at Rice University have explicit permission to make copies of these materials for their personal use.
Faculty from other educational institutions may use these materials for nonprofit educational purposes, provided this copyright notice is preserved.
COMP 412FALL 2010
This lecture begins the material from Chapter 8 of EaC
This lecture begins the material from Chapter 8 of EaC
![Page 2: Introduction to Code Optimization Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice](https://reader035.vdocuments.net/reader035/viewer/2022071706/56649d345503460f94a0b09d/html5/thumbnails/2.jpg)
Comp 412, Fall 2010 2
Traditional Three-Phase Compiler
Optimization (or Code Improvement)
• Analyzes IR and rewrites (or transforms ) IR
• Primary goal is to reduce running time of the compiled code— May also improve space, power consumption, …
• Must preserve “meaning” of the code— Measured by values of named variables
— A course (or two) unto itself
Errors
SourceCode Optimizer
FrontEnd
Machinecode
BackEnd
IR IR
![Page 3: Introduction to Code Optimization Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice](https://reader035.vdocuments.net/reader035/viewer/2022071706/56649d345503460f94a0b09d/html5/thumbnails/3.jpg)
Comp 412, Fall 2010 3
The Optimizer
Typical Transformations
• Discover & propagate some constant value
• Move a computation to a less frequently executed place
• Specialize some computation based on context
• Discover a redundant computation & remove it
• Remove useless or unreachable code
• Encode an idiom in some particularly efficient form
Errors
Opt1
Opt3
Opt2
Optn
...IR IR IR IR IR
Modern optimizers are structured as a series of passes
![Page 4: Introduction to Code Optimization Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice](https://reader035.vdocuments.net/reader035/viewer/2022071706/56649d345503460f94a0b09d/html5/thumbnails/4.jpg)
Comp 412, Fall 2010 4
The Role of the Optimizer• The compiler can implement a procedure in many ways• The optimizer tries to find an implementation that is
“better”— Speed, code size, data space, …
To accomplish this, it• Analyzes the code to derive knowledge about run-time
behavior— Data-flow analysis, pointer disambiguation, …— General term is “static analysis”
• Uses that knowledge in an attempt to improve the code— Literally hundreds of transformations have been proposed— Large amount of overlap between them
Nothing “optimal” about optimization• Proofs of optimality assume restrictive & unrealistic
conditions
![Page 5: Introduction to Code Optimization Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice](https://reader035.vdocuments.net/reader035/viewer/2022071706/56649d345503460f94a0b09d/html5/thumbnails/5.jpg)
Comp 412, Fall 2010 5
Redundancy Elimination as an Example
An expression x+y is redundant if and only if, along everypath from the procedure’s entry, it has been evaluated, and
its constituent subexpressions (x & y) have not been re-
defined.
If the compiler can prove that an expression is redundant
• It can preserve the results of earlier evaluations
• It can replace the current evaluation with a reference
Two pieces to the problem
• Proving that x+y is redundant, or available
• Rewriting the code to eliminate the redundant evaluation
One technique for accomplishing both is called value numbering
Assume a low-level, linear IR such as ILOCAssume a low-level, linear IR such as ILOC
![Page 6: Introduction to Code Optimization Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice](https://reader035.vdocuments.net/reader035/viewer/2022071706/56649d345503460f94a0b09d/html5/thumbnails/6.jpg)
Comp 412, Fall 2010 6
Value Numbering
The key notion• Assign an identifying number, V(n), to each expression
— V(x+y) = V(j) iff x+y and j always have the same value— Use hashing over the value numbers to make it efficient
• Use these numbers to improve the code
Improving the code• Replace redundant expressions
— Same VN refer rather than recompute
• Simplify algebraic identities• Discover constant-valued expressions, fold & propagate
them• Technique designed for low-level, linear IRs, similar
methods exist for trees (e.g., build a DAG)
Local algorithm due to Balke (1968) or Ershov (1954)
Within a basic block; definition becomes more complex across blocks
Within a basic block; definition becomes more complex across blocks
![Page 7: Introduction to Code Optimization Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice](https://reader035.vdocuments.net/reader035/viewer/2022071706/56649d345503460f94a0b09d/html5/thumbnails/7.jpg)
Comp 412, Fall 2010 7
Local Value Numbering
The Algorithm
For each operation o = <operator, o1, o2> in the block, in order
1 Get value numbers for operands from hash lookup
2 Hash <operator,VN(o1),VN(o2)> to get a value number for o
3 If o already had a value number, replace o with a reference
4 If o1 & o2 are constant, evaluate it & replace with a loadI
If hashing behaves, the algorithm runs in linear time— If not, use multi-set discriminationt or acyclic DFAstt
Handling algebraic identities
• Case statement on operator type
• Handle special cases within each operatortsee p. 251 in EaC ttDFAs for REs without closure can be
built online to provide a “perfect hash”
![Page 8: Introduction to Code Optimization Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice](https://reader035.vdocuments.net/reader035/viewer/2022071706/56649d345503460f94a0b09d/html5/thumbnails/8.jpg)
Comp 412, Fall 2010 8
Local Value Numbering
An example
With VNs
a3 x1 + y2
b3 x1 + y2
a4 17 c3 x1 + y2
Rewritten
a3 x1 + y2
b3 a3
a4 17 c3 a3 (oops!)
Options
• Use c3 b3
• Save a3 in t3
• Rename around it
Options
• Use c3 b3
• Save a3 in t3
• Rename around it
Original Code a x + y b x + y a 17 c x + y
Two redundancies
• Eliminate stmts with a • Coalesce results ?
Two redundancies
• Eliminate stmts with a • Coalesce results ?
![Page 9: Introduction to Code Optimization Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice](https://reader035.vdocuments.net/reader035/viewer/2022071706/56649d345503460f94a0b09d/html5/thumbnails/9.jpg)
Comp 412, Fall 2010 9
Local Value Numbering
Example (continued):
With VNs
a03 x0
1 + y02
b03 x0
1 + y02
a14 17
c03 x0
1 + y02
Notation:
• While complex, the meaning is clear
Notation:
• While complex, the meaning is clear
Original Code
a0 x0 + y0
b0 x0 + y0
a1 17
c0 x0 + y0
Renaming:
• Give each value a unique name
• Makes it clear
Renaming:
• Give each value a unique name
• Makes it clear
Rewritten
a03 x0
1 + y02
b03 a0
3
a14 17
c03 a0
3
Result:
• a03 is
available
• Rewriting now works
Result:
• a03 is
available
• Rewriting now works
![Page 10: Introduction to Code Optimization Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice](https://reader035.vdocuments.net/reader035/viewer/2022071706/56649d345503460f94a0b09d/html5/thumbnails/10.jpg)
Comp 412, Fall 2010 10
Local Value Numbering
The Algorithm
For each operation o = <operator, o1, o2> in the block, in order
1 Get value numbers for operands from hash lookup
2 Hash <operator,VN(o1),VN(o2)> to get a value number for o
3 If o already had a value number, replace o with a reference4 If o1 & o2 are constant, evaluate it & replace with a loadI
Complexity & Speed Issues “Get value numbers” — linear search versus hash
“Hash <op,VN(o1),VN(o2)>” — linear search versus hash
Copy folding — set value number of result Commutative ops — double hash versus sorting the operands
asymptoticasymptotic
constantsconstants
![Page 11: Introduction to Code Optimization Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice](https://reader035.vdocuments.net/reader035/viewer/2022071706/56649d345503460f94a0b09d/html5/thumbnails/11.jpg)
Comp 412, Fall 2010 11
Simple Extensions to Value Numbering
Constant folding
• Add a bit that records when a value is constant
• Evaluate constant values at compile-time
• Replace with load immediate or immediate operand
• No stronger local algorithm
Algebraic identities
• Must check (many) special cases
• Replace result with input VN
• Build a decision tree on operation
Identities (on VNs)
xy, x+0, x-0, x1, x÷1, x-x, x0, xx, x0, x 0xFF…FF, max(x,MAXINT), min(x,MININT), max(x,x), min(y,y), and so on ...
Identities (on VNs)
xy, x+0, x-0, x1, x÷1, x-x, x0, xx, x0, x 0xFF…FF, max(x,MAXINT), min(x,MININT), max(x,x), min(y,y), and so on ...
![Page 12: Introduction to Code Optimization Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice](https://reader035.vdocuments.net/reader035/viewer/2022071706/56649d345503460f94a0b09d/html5/thumbnails/12.jpg)
Comp 412, Fall 2010
12
Missed opportunities(need stronger methods)
m a + bn a + b
A
p c + dr c + d
B
y a + bz c + d
G
q a + br c + d
C
e b + 18s a + bu e + f
D e a + 17t c + du e + f
E
v a + bw c + dx e + f
F
Value Numbering
Local Value Numbering
1 block at a time
Strong local results
No cross-block effects
LVN finds these redundant ops