Lecture 22: Code Optimization Introduction to Code Optimization

Lecture 22: Code Optimization Introduction to Code Optimization

Lecture 22: Code Optimization Introduction to Code Optimization [Adapted from notes by R. Bodik and G. Necula] Code optimization is the usual term, but is grossly misnamed, since code produced by “optimizers” is not optimal in any reasonable sense. Pro- Administrivia gram improvement would be more appropriate. • Test on Wednesday. Topics: • Basic blocks • Control-flow graphs (CFGs) • Algebraic simplification • Constant folding • Static single-assignment form (SSA) • Common-subexpression elimination (CSE) • Copy propagation • Dead-code elimination • Peephole optimizations Last modified: Sun Nov 13 20:39:10 2011 CS164: Lecture #22 1 Last modified: Sun Nov 13 20:39:10 2011 CS164: Lecture #22 2 Basic Blocks Basic-Block Example • A basic block is a maximal sequence of instructions with: • Consider the basic block – no labels (except at the first instruction), and 1. L1: – no jumps (except in the last instruction) 2. t := 2 * x 3. w := t + x • Idea: 4. if w > 0 goto L2 – Cannot jump into a basic block, except at the beginning. • No way for (3) to be executed without (2) having been executed – Cannot jump within a basic block, except at end. right before – Therefore, each instruction in a basic block is executed after all • We can change (3) to w:=3*x the preceding instructions have been executed • Can we eliminate (2) as well? Last modified: Sun Nov 13 20:39:10 2011 CS164: Lecture #22 3 Last modified: Sun Nov 13 20:39:10 2011 CS164: Lecture #22 4 Control-Flow Graphs (CFGs) Control-Flow Graphs: Example • A control-flow graph is a directed graph with basic blocks as nodes • There is an edge from block A to block B if the execution can flow from the last instruction in A to the first instruction in B: x := 1 – The last instruction in A can be a jump to the label of B. i := 1 – Or execution can fall through from the end of block A to the beginning of block B. • The body of a method (or pro- cedure) can be represented as a CFG L: • There is one initial node x:=x*x i:=i+1 • All “return” nodes are terminal if i < 10 goto L Last modified: Sun Nov 13 20:39:10 2011 CS164: Lecture #22 5 Last modified: Sun Nov 13 20:39:10 2011 CS164: Lecture #22 6 Optimization Overview A Classification of Optimizations • Optimization seeks to improve a program’s utilization of some re- • For languages like C and Java there are three granularities of opti- source: mizations – Execution time (most often) 1. Local optimizations: Apply to a basic block in isolation. – Code size 2. Global optimizations: Apply to a control-flow graph (single func- – Network messages sent tion body) in isolation. – Battery power used, etc. 3. Inter-procedural optimizations: Apply across function boundaries. • Optimization should not depart from the programming language’s se- • Most compilers do (1), many do (2) and very few do (3) mantics • Problem is expense: (2) and (3) typically require superlinear time. • So if the semantics of a particular program is deterministic, opti- Can usually handle that when limited to a single function, but gets mization must not change the answer. problematic for larger program. • On the other hand, some program behavior is undefined (e.g., what • In practice, generally don’t implement fanciest known optimizations: happens when an unchecked rule in C is violated), and in those cases, some are hard to implement (esp., hard to get right), some require a optimization may cause differences in results. lot of compilation time. • The goal: maximum improvement with minimum cost. Last modified: Sun Nov 13 20:39:10 2011 CS164: Lecture #22 7 Last modified: Sun Nov 13 20:39:10 2011 CS164: Lecture #22 8 Local Optimizations: Algebraic Simplification Local Optimization: Constant Folding • Some statements can be deleted • Operations on constants can be computed at compile time. x:=x+0 • Example: x:=2+2 becomes x:= 4. x:=x*1 • Example: if2< 0jump L becomes a no-op. • Some statements can be simplified or converted to use faster op- • When might constant folding be dangerous? erations: Original Simplified x:=x*0 x := 0 y := y ** 2 y:=y*y x:=x*8 x := x << 3 x := x * 15 t := x << 4; x := t - x (on some machines << is faster than *; but not on all!) Last modified: Sun Nov 13 20:39:10 2011 CS164: Lecture #22 9 Last modified: Sun Nov 13 20:39:10 2011 CS164: Lecture #22 10 Global Optimization: Unreachable code elimination Single Assignment Form • Basic blocks that are not reachable from the entry point of the CFG • Some optimizations are simplified if each assignment is to a tempo- may be eliminated. rary that has not appeared already in the basic block. • Why would such basic blocks occur? • Intermediate code can be rewritten to be in (static) single assign- ment (SSA) form: • Removing unreachable code makes the program smaller (sometimes also faster, due to instruction-cache effects, but this is probably x:=a+y x:=a+y not a terribly large effect.) a:=x a1:=x x:=a*x x1:=a1*x b:=x+a b:=x1+a1 where x1 and a1 are fresh temporaries. Last modified: Sun Nov 13 20:39:10 2011 CS164: Lecture #22 11 Last modified: Sun Nov 13 20:39:10 2011 CS164: Lecture #22 12 Common SubExpression (CSE) Elimination in Basic Blocks Copy Propagation • A common subexpression is an expression that appears multiple times • If w := x appears in a block, can replace all subsequent uses of w on a right-hand side in contexts where the operands have the same with uses of x. values in each case (so that the expression will yield the same value). • Example: • Assume that the basic block on the left is in single assignment form. b:=z+y b:=z+y a:=b a:=b x:=y+z x:=y+z x:=2*a x:=2*b ... ... • This does not make the program smaller or faster but might enable w:=y+z w:=x other optimizations. For example, if a is not used after this state- ment, we need not assign to it. • That is, if two assignments have the same right-hand side, we can • Or consider: replace the second instance of that right-hand side with the vari- able that was assigned the first instance. b:=13 b:=13 x:=2*b x:=2*13 • How did we use the assumption of single assignment here? which immediately enables constant folding. • Again, the optimization, as described, won’t work unless the block is in single assignment form. Last modified: Sun Nov 13 20:39:10 2011 CS164: Lecture #22 13 Last modified: Sun Nov 13 20:39:10 2011 CS164: Lecture #22 14 Another Example of Copy Propagation and Constant Dead Code Elimination Folding • If that statement w := rhs appears in a basic block and w does not a:=5 a:=5 a:=5 a:=5 a:=5 appear anywhere else in the program, we say that the statement is x:=2*a x:=2*5 x:=10 x:=10 x:=10 dead and can be eliminated; it does not contribute to the program’s y:=x+6 y:=x+6 y:=10+6 y:=16 y:=16 result. t:=x*y t:=x*y t:=10*y t:=10*16 t:=160 • Example: (a is not used anywhere else) b:=z+y b:=z+y b:=z+y a:=b a:=b x:=2*a x:=2*b x:=2*b • How have I used SSA here? Last modified: Sun Nov 13 20:39:10 2011 CS164: Lecture #22 15 Last modified: Sun Nov 13 20:39:10 2011 CS164: Lecture #22 16 Applying Local Optimizations An Example: Initial Code • As the examples show, each local optimization does very little by a := x ** 2 itself. b := 3 c := x • Typically, optimizations interact: performing one optimization en- d:=c*c ables others. e:=b*2 • So typical optimizing compilers repeatedly perform optimizations f:=a+d until no improvement is possible, or it is no longer cost effective. g:=e*f Last modified: Sun Nov 13 20:39:10 2011 CS164: Lecture #22 17 Last modified: Sun Nov 13 20:39:10 2011 CS164: Lecture #22 18 An Example II: Algebraic simplification An Example: Copy propagation a:=x*x a:=x*x b := 3 b := 3 c := x c := x d:=c*c d := x*x e:=b+b e := 3+3 f:=a+d f:=a+d g:=e*f g:=e*f Last modified: Sun Nov 13 20:39:10 2011 CS164: Lecture #22 19 Last modified: Sun Nov 13 20:39:10 2011 CS164: Lecture #22 20 An Example: Constant folding An Example: Common Subexpression Elimination a:=x*x a:=x*x b := 3 b := 3 c := x c := x d:=x*x d := a e := 6 e := 6 f:=a+d f:=a+d g:=e*f g:=e*f Last modified: Sun Nov 13 20:39:10 2011 CS164: Lecture #22 21 Last modified: Sun Nov 13 20:39:10 2011 CS164: Lecture #22 22 An Example: Copy propagation An Example: Dead code elimination a:=x*x a:=x*x b := 3 c := x d := a e := 6 f := a + a f:=a+a g := 6 * f g:=6*f This is the final form. Last modified: Sun Nov 13 20:39:10 2011 CS164: Lecture #22 23 Last modified: Sun Nov 13 20:39:10 2011 CS164: Lecture #22 24 Peephole Optimizations on Assembly Code Peephole optimization examples: • The optimizations presented before work on intermediate code.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    21 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us