1 comp 144 programming language concepts felix hernandez-campos lecture 35: in-line expansion and...
Post on 22-Dec-2015
219 views
TRANSCRIPT
![Page 1: 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 35: In-line Expansion and Local Optimization COMP 144 Programming Language Concepts](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d775503460f94a5992c/html5/thumbnails/1.jpg)
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
11
Lecture 35: In-line Expansion and Lecture 35: In-line Expansion and Local OptimizationLocal Optimization
COMP 144 Programming Language ConceptsCOMP 144 Programming Language Concepts
Spring 2002Spring 2002
Felix Hernandez-CamposFelix Hernandez-Campos
April 19April 19
The University of North Carolina at Chapel HillThe University of North Carolina at Chapel Hill
![Page 2: 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 35: In-line Expansion and Local Optimization COMP 144 Programming Language Concepts](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d775503460f94a5992c/html5/thumbnails/2.jpg)
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
22
PhasesPhases
![Page 3: 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 35: In-line Expansion and Local Optimization COMP 144 Programming Language Concepts](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d775503460f94a5992c/html5/thumbnails/3.jpg)
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
33
Subroutine In-line ExpansionSubroutine In-line Expansion
• Subroutines may be expanded in-line at the point of Subroutines may be expanded in-line at the point of the callthe call
– Rather than use a stack-based calling conventionRather than use a stack-based calling convention
• In-line expansion saves subroutine overheads and In-line expansion saves subroutine overheads and help code improvementhelp code improvement
• In-line expansion may be decided by the compiler In-line expansion may be decided by the compiler based on some optimization heuristicsbased on some optimization heuristics
– E.g.E.g. short, non-recursive subroutines are always in-lined in short, non-recursive subroutines are always in-lined in some languagessome languages
![Page 4: 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 35: In-line Expansion and Local Optimization COMP 144 Programming Language Concepts](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d775503460f94a5992c/html5/thumbnails/4.jpg)
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
44
Subroutine In-line ExpansionSubroutine In-line Expansion
• In-line expansion can also be In-line expansion can also be suggestedsuggested by the by the programmerprogrammer
– E.g.E.g. C++ C++inlineinline int max (int a, int b) { int max (int a, int b) { return a > b ? a : b; return a > b ? a : b;
}}
– E.g.E.g. Ada Adafunction max(a, b : integer) return integer isfunction max(a, b : integer) return integer isbeginbeginif a > b then return a; else return b; end if;if a > b then return a; else return b; end if;
end max;end max;pragma inlinepragma inline (max); (max);
![Page 5: 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 35: In-line Expansion and Local Optimization COMP 144 Programming Language Concepts](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d775503460f94a5992c/html5/thumbnails/5.jpg)
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
55
Macros and In-line ExpansionMacros and In-line Expansion
• What is the difference between a macro and a What is the difference between a macro and a programmer suggested expansion?programmer suggested expansion?
– Optional in the second caseOptional in the second case– Most importantly, in-line expansion is an implementation Most importantly, in-line expansion is an implementation
technique with no effect in program semanticstechnique with no effect in program semantics
• E.g.E.g.
#define MAX(a,b) ((a) > (b) ? (a) : (b))#define MAX(a,b) ((a) > (b) ? (a) : (b))
– No type checkingNo type checking– What happens What happens after MAX(x++, y++)after MAX(x++, y++)??– The larger argument is incremented twiceThe larger argument is incremented twice
![Page 6: 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 35: In-line Expansion and Local Optimization COMP 144 Programming Language Concepts](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d775503460f94a5992c/html5/thumbnails/6.jpg)
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
66
In-line ExpansionIn-line Expansion
• In-line expansion has some disadvantagesIn-line expansion has some disadvantages– Increase in code sizeIncrease in code size– It cannot be used with recursive subroutinesIt cannot be used with recursive subroutines
• It is sometimes useful to expand the first case in a It is sometimes useful to expand the first case in a recursion subroutinerecursion subroutine
– Optimize the commonOptimize the commoncase rulecase rule
Most hash chains Most hash chains are onlyare only
one bucket longone bucket long
![Page 7: 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 35: In-line Expansion and Local Optimization COMP 144 Programming Language Concepts](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d775503460f94a5992c/html5/thumbnails/7.jpg)
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
77
PhasesPhases
![Page 8: 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 35: In-line Expansion and Local Optimization COMP 144 Programming Language Concepts](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d775503460f94a5992c/html5/thumbnails/8.jpg)
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
88
Example Example Control Flow GraphControl Flow Graph
• Basic blocksBasic blocks are are maximal-length set maximal-length set of sequential of sequential operationsoperations
– Operations on a set Operations on a set of of virtual registersvirtual registers
» UnlimitedUnlimited» A new one for each A new one for each
computed valuecomputed value
• Arcs represent Arcs represent interblock control interblock control flowflow
![Page 9: 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 35: In-line Expansion and Local Optimization COMP 144 Programming Language Concepts](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d775503460f94a5992c/html5/thumbnails/9.jpg)
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
99
Redundancy Elimination Redundancy Elimination in Basic Blocksin Basic Blocks
• We will consider the We will consider the example on the rightexample on the right
• It computes the It computes the binomial coefficientsbinomial coefficients
for 0 for 0 m mnn
• It is based onIt is based on
m
n
mn
n
m
n
![Page 10: 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 35: In-line Expansion and Local Optimization COMP 144 Programming Language Concepts](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d775503460f94a5992c/html5/thumbnails/10.jpg)
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
1010
Syntax Tree for the Syntax Tree for the combinations combinations subroutinesubroutine
![Page 11: 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 35: In-line Expansion and Local Optimization COMP 144 Programming Language Concepts](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d775503460f94a5992c/html5/thumbnails/11.jpg)
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
1111
Naïve Control Flow Graph Naïve Control Flow Graph for the for the combinations combinations subroutinesubroutine
![Page 12: 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 35: In-line Expansion and Local Optimization COMP 144 Programming Language Concepts](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d775503460f94a5992c/html5/thumbnails/12.jpg)
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
1212
Naïve Control Flow GraphNaïve Control Flow Graph
• Uses virtual registersUses virtual registers– A new register for each new valueA new register for each new value
• rara is the return addres, is the return addres, fpfp is the frame pointer is the frame pointer
• nn, , AA, , II and and tt perform the appropriate displacement perform the appropriate displacement addressing with respect to the stack pointer (addressing with respect to the stack pointer (spsp) ) registerregister
• Parameter passing using Parameter passing using r4r4 and and r5r5
![Page 13: 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 35: In-line Expansion and Local Optimization COMP 144 Programming Language Concepts](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d775503460f94a5992c/html5/thumbnails/13.jpg)
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
1313
Value NumberingValue Numbering
• How can we eliminate redundant loads and How can we eliminate redundant loads and computations?computations?
– Expression DAGExpression DAG– Value numberingValue numbering
• In value numbering, the compilers assigns the same In value numbering, the compilers assigns the same name (name (i.e.,i.e., numbernumber) to any two or symbolically ) to any two or symbolically equivalent computations (equivalent computations (i.e.,i.e., valuesvalues) )
• A dictionary is used to keep track of values that have A dictionary is used to keep track of values that have already been loaded or computedalready been loaded or computed
![Page 14: 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 35: In-line Expansion and Local Optimization COMP 144 Programming Language Concepts](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d775503460f94a5992c/html5/thumbnails/14.jpg)
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
1414
Value NumberingValue Numbering
• If a value is already in a register, reuse that registerIf a value is already in a register, reuse that register– E.g.,E.g., the load instruction can be eliminated v the load instruction can be eliminated vii := x if the := x if the
value x is already in register vvalue x is already in register vjj» Replace all uses of vReplace all uses of vii by v by vjj
• Similarly, we can get rid of small constants using Similarly, we can get rid of small constants using immediate valueimmediate value
![Page 15: 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 35: In-line Expansion and Local Optimization COMP 144 Programming Language Concepts](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d775503460f94a5992c/html5/thumbnails/15.jpg)
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
1515
Value NumberingValue Numbering
• In vIn vii := v := vjj op v op vkk, we can use constant folding if the , we can use constant folding if the values in vvalues in vjj and v and vkk are known to be constants are known to be constants
– Local constant folding and constant propagationLocal constant folding and constant propagation– At the same time, strength reduction and useless At the same time, strength reduction and useless
abstraction eliminationabstraction elimination
• A key that combine the registers and the operator is A key that combine the registers and the operator is used to keep track of the previous operation in the used to keep track of the previous operation in the dictionarydictionary
![Page 16: 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 35: In-line Expansion and Local Optimization COMP 144 Programming Language Concepts](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d775503460f94a5992c/html5/thumbnails/16.jpg)
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
1616
Control Flow Graph for Control Flow Graph for combinationscombinations after after local local redundancy eliminationredundancy elimination and and strength reductionstrength reduction
![Page 17: 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 35: In-line Expansion and Local Optimization COMP 144 Programming Language Concepts](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d775503460f94a5992c/html5/thumbnails/17.jpg)
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
1717
Reading AssignmentReading Assignment
• Read ScottRead Scott– Sect. 8.2.3Sect. 8.2.3– Ch. 13.3Ch. 13.3
![Page 18: 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 35: In-line Expansion and Local Optimization COMP 144 Programming Language Concepts](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d775503460f94a5992c/html5/thumbnails/18.jpg)
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
1818
Peephole OptimizationPeephole OptimizationCommon TechniquesCommon Techniques
![Page 19: 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 35: In-line Expansion and Local Optimization COMP 144 Programming Language Concepts](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d775503460f94a5992c/html5/thumbnails/19.jpg)
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
1919
Peephole OptimizationPeephole OptimizationCommon TechniquesCommon Techniques
![Page 20: 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 35: In-line Expansion and Local Optimization COMP 144 Programming Language Concepts](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d775503460f94a5992c/html5/thumbnails/20.jpg)
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
2020
Peephole OptimizationPeephole OptimizationCommon TechniquesCommon Techniques
![Page 21: 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 35: In-line Expansion and Local Optimization COMP 144 Programming Language Concepts](https://reader035.vdocuments.site/reader035/viewer/2022062407/56649d775503460f94a5992c/html5/thumbnails/21.jpg)
COMP 144 Programming Language ConceptsFelix Hernandez-Campos
2121
Peephole OptimizationPeephole OptimizationCommon TechniquesCommon Techniques