Download presentation
Presentation is loading. Please wait.
Published byBritton Jacobs Modified over 9 years ago
1
Terminology, Principles, and Concerns, II With examples from superlocal value numbering (Ch 8 in EaC2e) Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 512 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 512 Spring 2011
2
Last Lecture Scopes of optimization Local, regional, global (single procedure), whole program Redundancy elimination as an example Local value numbering algorithm Linear time in size of block Platform for folding constants, simplifying algebraic identities Good naming scheme can simplify algorithm Unique name for each definition of a variable COMP 512, Rice University2
3
This Lecture Considerations for optimization Safety Profitability Opportunity Optimization beyond a local scope Extended basic blocks Dominator trees COMP 512, Rice University3
4
4 Optimization In discussing any optimization, look for three issues Safety – Does it change the results of the computation? Safety is proven with results of analysis Data-flow analysis or other special case analysis Profitability – Is it expected to speed up execution? Many authors assume transformations are always profitable Use either heuristics or a careful algebra of costs Opportunity – Can we efficiently locate places to apply it? Can we find find all the places where the transformation works? Do we need to update safety information afterward? Should think about these issues with every transformation *
5
COMP 512, Rice University5 Safety The first principle of optimization The compiler must preserve the code’s “meaning” When can the compiler transform the code? Original & transformed code must have the same final state Variables that are visible at exit Equality of result, not equality of method ( ignore temporaries ) Formal notion Different translations with identical results are fine For two expressions, M and N, we say that M and N are observationally equivalent if and only if, in any context C where both M and N are closed (that is, have no free variables), evaluating C[M] and C[N] either produces identical results or neither terminates. Plotkin, 1975 compilation
6
COMP 512, Rice University6 Safety In practice, compilers use a simpler notion of equivalence This restatement ignores divergence If e’ is faster than e, the transformation is profitable Equivalence and context Compiled code always executes in some context Optimization is the art of capitalizing on context Lack of context fully general ( i.e., slow ) code Some compilers employ a worse standard (F ORTRAN ) Correct behavior for “standard conforming” code Undefined behavior for other code If, in their actual program context, the result of evaluating e’ cannot be distinguished from the result of evaluating e, the compiler can substitute e’ for e.
7
COMP 512, Rice University7 Safety My favorite bad quote on safety The point You must not violate the first principle Without the first principle, just compile a return and be done You, as a compiler writer, must decide if it’s worth the risk of doing this kind of optimization. It’s difficult for the compiler to distinguish between the safe and dangerous cases, here. For example, many C compilers perform risky optimizations because the compiler writer has assumed that a C programmer can understand the problems and take steps to remedy them at the source code level. It’s better to provide the maximum optimization, even if it’s dangerous, than to be conservative at the cost of less efficient code. A Pascal programmer may not have the same level of sophistication as a C programmer, however, so the better choice in this situation might be to avoid the risky optimization entirely or to require a special command-line switch to enable the optimization. Allen Holub, Compiler Design in C, Prentice Hall, 1990, p. 677 Like N. Wirth? Gone in second edition *
8
COMP 512, Rice University8 Safety & Value Numbering Why is local value numbering safe? Hash table starts empty Expressions placed in table as processed If is in the table, then It has already occurred at least once in the block Neither o 1 nor o 2 have been subsequently redefined The mapping uses VN( o 1 ) and VN( o 2 ), not o 1 and o 2 If either was redefined, it would have a new VN If has a VN, the compiler can safely use it Algorithm incrementally constructs a proof that is either redundant or not Algorithm modifies the code, but does not invalidate the table Critical property of a basic block: If any statement executes, they all execute, in a predetermined order
9
COMP 512, Rice University9 Profitability The compiler should only transform the code when it helps! Eliminating one or more operations Replacing an operation with a cheaper one Moving an operation to a place where it will execute fewer times Sometimes, we can prove profitability Fold a constant expression into an immediate operation Sometimes, we must guess Eliminating a redundant operation in a loop Sometimes, we cannot tell … Inlining in a Fortran compiler We need to consider the issue explicitly... Compiler writers should know when they cannot tell if some transformation is profitable ! *
10
COMP 512, Rice University10 When is local value numbering profitable? If reuse is cheaper than re-computation Does not cause a spill or a copy ( hard to determine ) In practice, assumed to be true Local constant folding is always profitable Re-computing uses a register, as does load immediate Immediate form of operation avoids even that cost Algebraic identities If it eliminates an operation, it is profitable ( x + 0 ) Profitability of simplification depends on target ( 2x x+x ) Easy to factor target machine costs into the implementation Do not perform the unprofitable rewrites Profitability & Value Numbering
11
COMP 512, Rice University11 Opportunity To perform an optimization, the compiler must locate all the places in the code where it can be applied Allows compiler to evaluate each possible application Leads to efficient application of the transformation Avoids additional search Approaches Perform analysis to find opportunities V ERY B USY expressions & code hoisting Look at every operation Value numbering, loop invariant code motion Iterate over subset of the IR Operator strength reduction on SSA … x+y … x+y … … …
12
COMP 512, Rice University12 Opportunity & Value Numbering How does local value numbering find opportunity? Linear scan over block, in execution order Incrementally constructs a model of program state At each operation, checks for opportunities Summary It performs an exhaustive search of the opportunities This answer is not satisfying, but it is true Search is linear in |operations| Must keep cost of checking each operation in check For example, use a tree of algebraic identities by operator Hashing keeps cost down to O(1) per operand + per operation
13
COMP 512, Rice University13 Missed opportunities (need stronger methods) m a + b n a + b A p c + d r c + d B y a + b z c + d G q a + b r c + d C e b + 18 s a + b u e + f D e a + 17 t c + d u e + f E v a + b w c + d x e + f F Value Numbering Local Value Numbering 1 block at a time Strong local results No cross-block effects *
14
COMP 512, Rice University14 An Aside on Terminology m a + b n a + b A p c + d r c + d B y a + b z c + d G q a + b r c + d C e b + 18 s a + b u e + f D e a + 17 t c + d u e + f E v a + b w c + d x e + f F Control-flow graph (CFG) Nodes for basic blocks Edges for branches Basis for much of program analysis & transformation This CFG, G = (N,E) N = {A,B,C,D,E,F,G} E = {(A,B),(A,C),(B,G),(C,D), (C,E),(D,F),(E,F),(F,E)} |N| = 7, |E| = 8
15
COMP 512, Rice University15 Regional Optimization: Extended Basic Blocks m a + b n a + b A p c + d r c + d B y a + b z c + d G q a + b r c + d C e b + 18 s a + b u e + f D e a + 17 t c + d u e + f E v a + b w c + d x e + f F An Extended Basic Block (EBB) Set of blocks b 1, b 2, …, b n b 1 has > 1 predecessor All other b i have 1 predecessor EBBs provide more context for optimization *
16
COMP 512, Rice University16 Regional Optimization: Extended Basic Blocks m a + b n a + b A p c + d r c + d B y a + b z c + d G q a + b r c + d C e b + 18 s a + b u e + f D e a + 17 t c + d u e + f E v a + b w c + d x e + f F An EBB contains 1 or more path If b 1, b 2, …, b n is a path b 1 has > 1 predecessor b i has 1 predecessor, b i-1 * {A, B, C, D, E} is an EBB {A,B}, {A,C,D}, and {A,C,E} are paths in {A, B, C, D, E} {F} and {G} are also EBBs They have only trivial paths
17
COMP 512, Rice University17 Superlocal Value Numbering m a + b n a + b A p c + d r c + d B y a + b z c + d G q a + b r c + d C e b + 18 s a + b u e + f D e a + 17 t c + d u e + f E v a + b w c + d x e + f F The Concept Apply local method to each path in the EBB Do { A,B }, { A,C,D }, & { A,C,E } Obtain reuse from ancestors Does not help with F or G Key: avoid re-analyzing A & C * Superlocal ≈ path in the EBB
18
COMP 512, Rice University18 Superlocal Value Numbering Efficiency Use A’s table to initialize tables for B & C To avoid duplication, use a scoped hash table A, AB, A, AC, ACD, AC, ACE, F, G Need a VN name mapping to handle kills Must restore map with scope Adds complication, not cost To simplify matters Unique name for each definition Makes name VN Use the SSA name space EaC: § 5.7.3 & App. B m a + b n a + b A p c + d r c + d B y a + b z c + d G q a + b r c + d C e b + 18 s a + b u e + f D e a + 17 t c + d u e + f E v a + b w c + d x e + f F Subscripted names from example in last lecture
19
COMP 512, Rice University19 Aside: SSA Name Space ( locally ) Example ( from lecture 3 ) With VNs a 0 3 x 0 1 + y 0 2 b 0 3 x 0 1 + y 0 2 a 1 4 17 c 0 3 x 0 1 + y 0 2 Notation: While complex, the meaning is clear Original Code a 0 x 0 + y 0 b 0 x 0 + y 0 a 1 17 c 0 x 0 + y 0 Renaming: Give each value a unique name Makes it clear Rewritten a 0 3 x 0 1 + y 0 2 b 0 3 a 0 3 a 1 4 17 c 0 3 a 0 3 Result: a 0 3 is available rewriting works These are SSA names …
20
COMP 512, Rice University20 Aside: SSA Name Space ( in general ) Two principles Each name is defined by exactly one operation Each operand refers to exactly one definition To reconcile these principles with real code Insert -functions at merge points to reconcile name space Add subscripts to variable names for uniqueness We’ll look at how to construct SSA form in a week or two x ...... x +... x 0 ... x 1 ... x 2 (x 0,x 1 ) x 2 +... becomes A -function selects one of its operands, based on the control-flow path used to reach the block.
21
COMP 512, Rice University21 This is in SSA Form Superlocal Value Numbering m 0 a + b n 0 a + b A p 0 c + d r 0 c + d B r 2 (r 0,r 1 ) y 0 a + b z 0 c + d G q 0 a + b r 1 c + d C e 0 b + 18 s 0 a + b u 0 e + f D e 1 a + 17 t 0 c + d u 1 e + f E e 3 (e 0,e 1 ) u 2 (u 0,u 1 ) v 0 a + b w 0 c + d x 0 e + f F With all the bells & whistles Find more redundancy Pay little additional cost Still does nothing for F & G Superlocal techniques Some local methods extend cleanly to superlocal scopes VN does not back up If C adds to A, it’s a problem
22
COMP 512, Rice University22 What About Larger Scopes? We have not helped with F or G Multiple predecessors Must decide what facts hold in F and in G For G, combine B & F? Merging state is expensive Fall back on what’s known G m 0 a + b n 0 a + b A p 0 c + d r 0 c + d B r 2 (r 0,r 1 ) y 0 a + b z 0 c + d q 0 a + b r 1 c + d C e 0 b + 18 s 0 a + b u 0 e + f D e 1 a + 17 t 0 c + d u 1 e + f E e 3 (e 0,e 1 ) u 2 (u 0,u 1 ) v 0 a + b w 0 c + d x 0 e + f F
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.