Lecture 36: Code Optimization

Size: px
Start display at page:

Download "Lecture 36: Code Optimization"

Transcription

1 troduction to Code Optimization n istheusualterm, but isgrosslymisnamed, sincecode timizers is not optimal in any reasonable sense. Pront would be more appropriate. e basic block 2 * x t + x > 0 goto L2 Basic-Block Example graphs (CFGs) plification ing -assignment form (SSA) xpression elimination (CSE) tion imination imizations (3) to be executed without (2) having been executed e nge (3) to w := 3 * x nate (2) as well? :49: CS164: Lecture #36 2 0:49: CS164: Lecture #36 4 Lecture 36: Code Optimization otes by R. Bodik and G. Necula] Basic Blocks k is a maximal sequence of instructions with: (except at the first instruction), and (except in the last instruction) mp into a basic block, except at the beginning. mp within a basic block, except at end. e, each instruction in a basic block is executed after all ding instructions have been executed :49: CS164: Lecture #36 1 0:49: CS164: Lecture #36 3

2 Control-Flow Graphs: Example A Classification of Optimizations 1 1 x 1 goto L The body of a method (or procedure) can be represented as a CFG There is one initial node All return nodes are terminal es like C and Java there are three granularities of optimizations: Apply to a basic block in isolation. timizations: Apply to a control-flow graph (single func- ) in isolation. ocedural optimizations: Apply across function boundaries. ers do (1), many do (2) and very few do (3) expense: (2) and (3) typically require superlinear time. handle that when limited to a single function, but gets for larger program. generally don t implement fanciest known optimizations: rd to implement (esp., hard to get right), some require a ation time. aximum improvement with minimum cost. :49: CS164: Lecture #36 6 0:49: CS164: Lecture #36 8 Control-Flow Graphs (CFGs) w graph is a directed graph with basic blocks as nodes dge from block A to block B if the execution can flow instruction in A to the first instruction in B: struction in A can be a jump to the label of B. ion can fall through from the end of block A to the of block B. Optimization Overview n seeks to improve a program s utilization of some ren time (most often) messages sent ower used, etc. n should not depart from the programming language s semantics of a particular program is deterministic, optist not change the answer. r hand, some program behavior is undefined (e.g., what n an unchecked rule in C is violated), and in those cases, may cause differences in results. :49: CS164: Lecture #36 5 0:49: CS164: Lecture #36 7

3 al Optimization: Constant Folding Single Assignment Form n constants can be computed at compile time. = becomes x := 4. < 0 jump L becomes a no-op. constant folding be dangerous? zations are simplified if each assignment is to a tempos not appeared already in the basic block. e code can be rewritten to be in (static) single assignform: y x a x := a + y a1 := x x1 := a1 * x b := x1 + a1 d a1 are fresh temporaries. :49: CS164: Lecture # :49: CS164: Lecture #36 12 ptimizations: Algebraic Simplification ents can be deleted 0 1 ents can be simplified or converted to use faster op- Simplified x := 0 y := y * y x := x << 3 t := x << 4; x := t - x hines << is faster than *; but not on all!) ptimization: Unreachable code elimination that are not reachable from the entry point of the CFG nated. uch basic blocks occur? reachable code makes the program smaller (sometimes due to instruction-cache effects, but this is probably y large effect.) :49: CS164: Lecture #36 9 0:49: CS164: Lecture #36 11

4 Copy Propagation pears in a block, can replace all subsequent uses of w x. Dead Code Elimination ement w := rhs appears in a basic block and w does not here else in the program, we say that the statement is n be eliminated; it does not contribute to the program s b:=z+y a := b x:=2*b make the program smaller or faster but might enable ations. For example, if a is not used after this stated not assign to it. is not used anywhere else) y b := z + y b := z + y a := b a x := 2 * b x := 2 * b used SSA here? b:=13 x:=2*13 iately enables constant folding. timization, as described, won t work unless the block is nment form. :49: CS164: Lecture # :49: CS164: Lecture #36 16 xpression (CSE) Elimination in Basic Blocks expression is an expression that appears multiple times nd side in contexts where the operands have the same case (so that the expression will yield the same value). the basic block on the left is in single assignment form. Example of Copy Propagation and Constant Folding a := 5 a := 5 a := 5 a := 5 x := 2 * 5 x := 10 x := 10 x := 10 y := x + 6 y := y := 16 y := 16 t := x * y t := 10 * y t := 10 * 16 t := 160 z z x := y + z w := x o assignments have the same right-hand side, we can econd instance of that right-hand side with the variassigned the first instance. se the assumption of single assignment here? :49: CS164: Lecture # :49: CS164: Lecture #36 15

5 An Example: Initial Code An Example: Copy propagation :49: CS164: Lecture # :49: CS164: Lecture #36 20 Applying Local Optimizations Example II: Algebraic simplification ples show, each local optimization does very little by timizations interact: performing one optimization entimizing compilers repeatedly perform optimizations vement is possible, or it is no longer cost effective. :49: CS164: Lecture # :49: CS164: Lecture #36 19

6 ple: Common Subexpression Elimination n Example: Dead code elimination nal form. :49: CS164: Lecture # :49: CS164: Lecture #36 24 An Example: Constant folding An Example: Copy propagation :49: CS164: Lecture # :49: CS164: Lecture #36 23

7 eephole optimization examples: Global Optimization for pattern variables. L: movl movl not the target of a jump., %@a; %@b %@b is dead. mov hack: we reuse the valueias both the immediate value t from ra. On the PDP11, the program counter is r7. ptimizations, peephole optimizations need to be applied get maximum effect. ization refers to program optimizations that encompass ic blocks in a function. the term galactic optimization to refer to going beyond ndaries, but it hasn t caught on; we call it just interpromization.) n t use the usual assumptions about basic blocks, global requires global flow analysis to see where values can nd get used. question is: When can local optimizations (from the last applied across multiple basic blocks? :49: CS164: Lecture # :49: CS164: Lecture #36 28 ole Optimizations on Assembly Code tions presented before work on intermediate code. imization is a technique for improving assembly code hole is a short subsequence of (usually contiguous) in-, either continguous, or linked together by the fact operate on certain registers that no intervening inmodify. izer replaces the sequence with another equivalent, but ) better one. phole optimizations as replacement rules n j1;...; jm us additional constraints. The j s are the improved veri s. Problems: blem: what to do with pointers? Problem is aliasing: two he same variable: lt, *t may change even if local variable t does not and assign to *t. anguage design: rules about overlapping parameters in and the restrict keyword in C. e a special case (address calculation): is A[i] the same Sometimes the compiler can tell, depending on what it out i and j. globals variables and calls? not exactly jumps, because they (almost) always return. fy global variables used by caller :49: CS164: Lecture # :49: CS164: Lecture #36 27

8 Issues ness condition is not trivial to check Conservative Program Analyses optimization requires P to be true, then cludes paths around loops and through branches of concondition requires global analysis: an analysis of the l-flow graph for one method body. l for optimizations that depend on some property P at oint in program execution. erty P is typically undecidable, so program optimization aking conservative (but not cowardly) approximations ow that P is definitely true, we can apply the optimizan t know whether P is true, we simply don t do the opn. Since optimizations are not supposed to change the f a program, this is safe. rds, in analyzing a program for properties like P, it is ct (albeit non-optimal) to say don t know. to say it as seldom as possible. low analysis is a standard technique for solving problems haracteristics. :49: CS164: Lecture # :49: CS164: Lecture #36 32 imple Example: Copy Propagation Y := Z + W Y := 0 X := 4 A := 2 * 3X r assignments to X, it is valid to treat the red parts as in the same basic block. as one other block on the path to the bottom block we can no longer do so. to apply copy propagation to a variable x from an astementa: x :=... to a given use of x in statement B st assignment to x in every path from to B is A. :49: CS164: Lecture #36 29 decidability of Program Properties rem: Most interesting dynamic properties of a program able. E.g., program halt on all (some) inputs? (Halting Problem) sult of a function F always positive? (Consider x): x) turn 1 positive iff H halts.) operties are typically decidable (e.g., How many occurare there? ). es not apply in absence of loops 0:49: CS164: Lecture #36 31

9 le of Result of Constant Propagation Transfer Functions Express the analysis of a complicated program as a comsimple rules relating the change in information between tements push or transfer information from one statement to Y := Z + W X := 4 Y := 0 atement s, we end up with information about the value ately before and after s: A := 2 * X value of x before s = value of x after s alues of x we use come from an abstract domain, conalues we care about #,*, k values computed statically sis. stant propagation problem, we ll compute Cout from Cin, Cin from the Couts of predecessor statements, Cout(X, X,p n ). :49: CS164: Lecture # :49: CS164: Lecture #36 36 mple: Global Constant Propagation nt propagation is just the restriction of copy propagaants. ple, we ll consider doing it for a single variable (X). gram point (i.e., before or after any instruction), we of the following values with X Interpretation Using Analysis Results constant information, it is easy to perform the optiint immediately before a statement using x tells us that en replace x with c. e, leave it alone (the conservative option). we compute these properties x =...? a bottom) No value has reached here (yet) r c a constant) X definitely has the value c. a top) Don t know what, if any, constant value X has. :49: CS164: Lecture # :49: CS164: Lecture #36 35

10 Constant Propagation: Rule 2 p 1 X = c p 2 X =? p 3 X = d p n X =? Constant Propagation: Rule 4 p 1 p 2 p 3 p n s s, p i ) = c and Cout(X, p j ) = d with constants c d, then Cin(X, s) = * Cout(X, p j ) = # for all j, then Cin(X, s) = # :49: CS164: Lecture # :49: CS164: Lecture #36 40 Constant Propagation: Rule 1 Constant Propagation: Rule 3 p 1 X =? p 2 X =? p 3 p n X =? p 1 X = c p 2 p 3 X = c p n s s X = c out(x, p i ) = * for some i, then Cin(X, s) = * If Cout(X, p i ) = c for some i and Cout(X, p j ) = c or Cout(X, p j ) = # for all j, then Cin(X, s) = c :49: CS164: Lecture # :49: CS164: Lecture #36 39

11 Constant Propagation: Rule 5 s Constant Propagation: Rule 7 X := f(...) X =? Cout(X, s) = # if Cin(X, s) = # X := f(...)) = * for any function call, if? is not #. eans so far, no value of X gets here, because we don t this statement ever gets executed. :49: CS164: Lecture # :49: CS164: Lecture #36 44 stant Propagation: Computing Cout Constant Propagation: Rule 6 ate the out of one statement to the in of the succests, thus propagating information forward across CFG local rulesrelatingthein andout ofasinglestatement information across statements. X := c X =? X = c X, X := c) = c if c is a constant and? is not #. :49: CS164: Lecture # :49: CS164: Lecture #36 43

12 Propagation Algorithm r Example of the Propagation Algorithm rules, we employ a standard technique: iteration to a ts in the program with current approximations of the interest (X in our examples). l approximations to for the program entry point erywhere else. pply rules 1 8 every place they are applicable until nothuntil the program is at a fixed point with respect to all rules. ever about this, keeping a list of all nodes any of whose Cout values have changed since the last rule applica- Y := Z + W X := 4 A := 2 * X A < B Y := 0 :49: CS164: Lecture # :49: CS164: Lecture #36 48 Constant Propagation: Rule 8 An Example of the Algorithm Y :=... X = α X = α 3 3 Y := Z + W Y := ) = Cin(X, Y :=...) if X and Y are different variables. A := 2 * X A < B place X with 3 in the bottom block. :49: CS164: Lecture # :49: CS164: Lecture #36 47

13 Another Example of the Propagation Algorithm 3 3 Y := Z + W X := 4 Y := 0 A := 2 * X A < B Last modified: Mon Apr 23 10:49: CS164: Lecture #36 48

14 Another Example of the Propagation Algorithm Y := Z + W X := 4 Y := A := 2 * X A < B Last modified: Mon Apr 23 10:49: CS164: Lecture #36 48

15 Another Example of the Propagation Algorithm Y := Z + W X := 4 Y := A := 2 * X A < B * * * Last modified: Mon Apr 23 10:49: CS164: Lecture #36 48

16 Another Example of the Propagation Algorithm Y := Z + W X := 4 Y := 0 3 * 3 * A := 2 * X A < B * * * Here, we cannot replace X in two of the basic blocks. Last modified: Mon Apr 23 10:49: CS164: Lecture #36 48

17 Comments Termination s used a breadth-first approach to considering possible ly the rules, starting from the entry point. order in which one looks at statements is irrelevant. e changed the Cout values after the assignments to X mple. s necessary to avoid deciding on a final value too soon. allows us to tentatively propagate constant values through g out what happens in paths we haven t looked at yet. g repeat until nothing changes doesn t guarantee that othing changes. of lub explains why the algorithm terminates: art as # and only increase ructure of the lattice, therefore, each value can only wice. orithm is linear in program size. The number of steps ber of Cin and Cout values computed ber of program statements. :49: CS164: Lecture # :49: CS164: Lecture #36 52 A Third Example Ordering the Abstract Domain 3 3 plify the presentation of the analysis by ordering the < *. y, with lower meaning less than, Y := Z + W Y := 0 3 * 3 * * A := 2 * X X := 4 A < B annot replace X. 3 * 3 * # atical structure known as a lattice. ur rule for computing Cin is simply a least upper bound: = lub { Cout(x, p) such that p is a predecessor of s }. :49: CS164: Lecture # :49: CS164: Lecture #36 51

18 m Terminology: Live and Dead /*(1)*/ X = 4; /*(2)*/ Y := X /*(3)*/ X is dead (never used) at point (1), live at point (2), and not be live at point (3), depending on the rest of the Liveness Rule 1 p L(X) = true lly, a variable x is live at statement s if sts a statement s that uses x; path from s to s ; and has no intervening assignment to x x :=... is dead code (and may be deleted) if x is e assignment. s 2 L(X) =? s 3 L(X) = true s n L(X) =? = lub { Lin(x, s) such that p is a predecessor of s }. upper bound (lub) is the same as or. :49: CS164: Lecture # :49: CS164: Lecture #36 56 Liveness Analysis have been globally propagated, we would like to elimi- Computing Liveness ress liveness as a function of information transferred acent statements, just as in copy propagation simpler than constant propagation, since it is a boolean ue or false). lattice has two values, with false<true. Y := Z + W Y := 0 rsinthatlivenessdependsonwhatcomesafter astatefore we propagate information backwards through the from Lout (liveness information at the end of a stat-. A := 2 * X A < B propagation, is dead code (assuming this is the :49: CS164: Lecture # :49: CS164: Lecture #36 55

19 Liveness Rule 3 X := e L(X) = false L(X) =? ropagation Algorithm for Liveness all Lin and Lout values be false. ) = false if e does not use the previous value of X. ue at the program exit to true iff x is going to be used e.g., if it is global and we are analyzing only one procerepeatedly pick s where one of 1 4 does not hold and the appropriate rule, until there are no more violations. done, we can eliminate assignments to X if X is dead at ter the assignment. :49: CS164: Lecture # :49: CS164: Lecture #36 60 Liveness Rule 2 Liveness Rule 4...:=...X... L(X) = true L(X) =? s L(X) = α L(X) = α, s) = true if s uses the previous value of X. ut(x, s) = Lin(X, s) if s does not mention X. e applies to any other statement that uses the value of ts (e.g., X < 0). :49: CS164: Lecture # :49: CS164: Lecture #36 59

20 Termination φ Functions value can only change a bounded number of times: the in this case. is guaranteed lysis is computed, it is simple to eliminate dead code, ne so, we must recompute the liveness information. device to allow SSA notation in CFGs. ock, each variable is associated with one definition, in effect associate each variable with a set of possible one tries to introduce them in strategic places so as to e total number of φs. is device increases number of assignments in IL, regisn can remove many by assigning related IL registers to al register. ables us to extend such optimizations as CSE elimination ks to Global CSE Elimination. orm, easy to tell (conservatively) if two IL assignments same value: just see if they have the same right-hand me variables indicate the same values. :49: CS164: Lecture # :49: CS164: Lecture #36 64 xample of Liveness Computation SSA and Global Analysis Y := Z + W L(X) = false L(X) = false true L(X) = false true L(X) = false true Y := 0 L(X) = false true imizations, the single static assignment (SSA) form was it to a full CFG is requires a trick. we avoid two assignments to the temporary holding x onditional? : a A := 2 * X X := X * X X := 4 A < B L(X) = false L(X) = false true L(X) = false true L(X) = false L(X) = false true L(X) = false true b is x at this point? mall kludge known as φ functions evious example into this: : a b 1, x2) :49: CS164: Lecture # :49: CS164: Lecture #36 63

21 Invariant Code Motion Summary loop < N) = A[i] + j * x; does not change in the loop, we can rewrite this as x; < N) = A[i] + tmp; ample of invariant code motion out of a loop. that j*x does not change? all assignments tojandxthat apply at the point where s computed are outside the loop. angetbyobservingwheretheassignmentstothessase variables are. two kinds of analysis: propagation is a forward analysis: information is pushed ts to outputs. s a backwards analysis: information is pushed from outtowards inputs. ke use of essentially the same algorithm. ther analyses fall into these categories, and allow us to formulation: act domain (abstract relative to actual values); s relating information between consecutive program points single statement; and perations like least upper bound (or join) or greatest nd (or meet) to relate inputs and outputs of adjoining ts. :49: CS164: Lecture # :49: CS164: Lecture #36 68 Loops op is simplyaset of basic blocks,l, containingan entry that from the entry node of the entire CFG to a block in L essors of a node in L are also in L (except for e, which a predecessor outside L). e in L has a path in L back to e. ample, < N) = A[j] / A[i] de contains the test j < n and the rest of the loop is taining the assigment to A[j], which then loops back to Code Motion Caveat is not always appropriate. to be moved, has side effects, or might cause an excephange the results. is expensive, you will increase the time required for the en the loop is not executed. will see compilers rewrite loops like this: N) { reheader */ e (i < N) A[i] = A[i] + j * x; eader marks a spot where the compiler can insert a new d code moved out of the loop. :49: CS164: Lecture # :49: CS164: Lecture #36 67

Introduction to Code Optimization. Lecture 36: Local Optimization. Basic Blocks. Basic-Block Example

Introduction to Code Optimization. Lecture 36: Local Optimization. Basic Blocks. Basic-Block Example Lecture 36: Local Optimization [Adapted from notes by R. Bodik and G. Necula] Introduction to Code Optimization Code optimization is the usual term, but is grossly misnamed, since code produced by optimizers

More information

Topics. Lecture 37: Global Optimization. Issues. A Simple Example: Copy Propagation X := 3 B > 0 Y := 0 X := 4 Y := Z + W A := 2 * 3X

Topics. Lecture 37: Global Optimization. Issues. A Simple Example: Copy Propagation X := 3 B > 0 Y := 0 X := 4 Y := Z + W A := 2 * 3X Lecture 37: Global Optimization [Adapted from note by R. Bodik and G. Necula] Topic Global optimization refer to program optimization that encompa multiple baic block in a function. (I have ued the term

More information

Multi-dimensional Arrays

Multi-dimensional Arrays Multi-dimensional Arrays IL for Arrays & Local Optimizations Lecture 26 (Adapted from notes by R. Bodik and G. Necula) A 2D array is a 1D array of 1D arrays Java uses arrays of pointers to arrays for >1D

More information

Lecture Outline. Intermediate code Intermediate Code & Local Optimizations. Local optimizations. Lecture 14. Next time: global optimizations

Lecture Outline. Intermediate code Intermediate Code & Local Optimizations. Local optimizations. Lecture 14. Next time: global optimizations Lecture Outline Intermediate code Intermediate Code & Local Optimizations Lecture 14 Local optimizations Next time: global optimizations Prof. Aiken CS 143 Lecture 14 1 Prof. Aiken CS 143 Lecture 14 2

More information

Other Forms of Intermediate Code. Local Optimizations. Lecture 34

Other Forms of Intermediate Code. Local Optimizations. Lecture 34 Other Forms of Intermediate Code. Local Optimizations Lecture 34 (Adapted from notes by R. Bodik and G. Necula) 4/18/08 Prof. Hilfinger CS 164 Lecture 34 1 Administrative HW #5 is now on-line. Due next

More information

Administrative. Other Forms of Intermediate Code. Local Optimizations. Lecture 34. Code Generation Summary. Why Intermediate Languages?

Administrative. Other Forms of Intermediate Code. Local Optimizations. Lecture 34. Code Generation Summary. Why Intermediate Languages? Administrative Other Forms of Intermediate Code. Local Optimizations HW #5 is now on-line. Due next Friday. If your test grade is not glookupable, please tell us. Please submit test regrading pleas to

More information

Intermediate Code & Local Optimizations. Lecture 20

Intermediate Code & Local Optimizations. Lecture 20 Intermediate Code & Local Optimizations Lecture 20 Lecture Outline Intermediate code Local optimizations Next time: global optimizations 2 Code Generation Summary We have discussed Runtime organization

More information

(How Not To Do) Global Optimizations

(How Not To Do) Global Optimizations (How Not To Do) Global Optimizations #1 One-Slide Summary A global optimization changes an entire method (consisting of multiple basic blocks). We must be conservative and only apply global optimizations

More information

Intermediate Code & Local Optimizations

Intermediate Code & Local Optimizations Lecture Outline Intermediate Code & Local Optimizations Intermediate code Local optimizations Compiler Design I (2011) 2 Code Generation Summary We have so far discussed Runtime organization Simple stack

More information

Global Optimization. Lecture Outline. Global flow analysis. Global constant propagation. Liveness analysis. Local Optimization. Global Optimization

Global Optimization. Lecture Outline. Global flow analysis. Global constant propagation. Liveness analysis. Local Optimization. Global Optimization Lecture Outline Global Optimization Global flow analysis Global constant propagation Liveness analysis Compiler Design I (2011) 2 Local Optimization Recall the simple basic-block optimizations Constant

More information

Compiler Passes. Optimization. The Role of the Optimizer. Optimizations. The Optimizer (or Middle End) Traditional Three-pass Compiler

Compiler Passes. Optimization. The Role of the Optimizer. Optimizations. The Optimizer (or Middle End) Traditional Three-pass Compiler Compiler Passes Analysis of input program (front-end) character stream Lexical Analysis Synthesis of output program (back-end) Intermediate Code Generation Optimization Before and after generating machine

More information

Operational Semantics of Cool

Operational Semantics of Cool Operational Semantics of Cool Lecture 23 Dr. Sean Peisert ECS 142 Spring 2009 1 Status Project 3 due on Friday Project 4 assigned on Friday. Due June 5, 11:55pm Office Hours this week Fri at 11am No Class

More information

A Bad Name. CS 2210: Optimization. Register Allocation. Optimization. Reaching Definitions. Dataflow Analyses 4/10/2013

A Bad Name. CS 2210: Optimization. Register Allocation. Optimization. Reaching Definitions. Dataflow Analyses 4/10/2013 A Bad Name Optimization is the process by which we turn a program into a better one, for some definition of better. CS 2210: Optimization This is impossible in the general case. For instance, a fully optimizing

More information

CODE GENERATION Monday, May 31, 2010

CODE GENERATION Monday, May 31, 2010 CODE GENERATION memory management returned value actual parameters commonly placed in registers (when possible) optional control link optional access link saved machine status local data temporaries A.R.

More information

Lecture 3 Local Optimizations, Intro to SSA

Lecture 3 Local Optimizations, Intro to SSA Lecture 3 Local Optimizations, Intro to SSA I. Basic blocks & Flow graphs II. Abstraction 1: DAG III. Abstraction 2: Value numbering IV. Intro to SSA ALSU 8.4-8.5, 6.2.4 Phillip B. Gibbons 15-745: Local

More information

Dataflow Analysis. Xiaokang Qiu Purdue University. October 17, 2018 ECE 468

Dataflow Analysis. Xiaokang Qiu Purdue University. October 17, 2018 ECE 468 Dataflow Analysis Xiaokang Qiu Purdue University ECE 468 October 17, 2018 Program optimizations So far we have talked about different kinds of optimizations Peephole optimizations Local common sub-expression

More information

7. Optimization! Prof. O. Nierstrasz! Lecture notes by Marcus Denker!

7. Optimization! Prof. O. Nierstrasz! Lecture notes by Marcus Denker! 7. Optimization! Prof. O. Nierstrasz! Lecture notes by Marcus Denker! Roadmap > Introduction! > Optimizations in the Back-end! > The Optimizer! > SSA Optimizations! > Advanced Optimizations! 2 Literature!

More information

Induction Variable Identification (cont)

Induction Variable Identification (cont) Loop Invariant Code Motion Last Time Uses of SSA: reaching constants, dead-code elimination, induction variable identification Today Finish up induction variable identification Loop invariant code motion

More information

Tour of common optimizations

Tour of common optimizations Tour of common optimizations Simple example foo(z) { x := 3 + 6; y := x 5 return z * y } Simple example foo(z) { x := 3 + 6; y := x 5; return z * y } x:=9; Applying Constant Folding Simple example foo(z)

More information

Local Optimizations #1

Local Optimizations #1 #1 Local Optimizations #2 One-Slide Summary An optimization changes a program so that it computes the same answer in less time (or using less of some other resource). We represent the program using a special

More information

Calvin Lin The University of Texas at Austin

Calvin Lin The University of Texas at Austin Loop Invariant Code Motion Last Time Loop invariant code motion Value numbering Today Finish value numbering More reuse optimization Common subession elimination Partial redundancy elimination Next Time

More information

Local Optimizations #1

Local Optimizations #1 #1 Local Optimizations #2 One-Slide Summary An optimization changes a program so that it computes the same answer in less time (or using less of some other resource). We represent the program using a special

More information

Data Flow Analysis. Agenda CS738: Advanced Compiler Optimizations. 3-address Code Format. Assumptions

Data Flow Analysis. Agenda CS738: Advanced Compiler Optimizations. 3-address Code Format. Assumptions Agenda CS738: Advanced Compiler Optimizations Data Flow Analysis Amey Karkare karkare@cse.iitk.ac.in http://www.cse.iitk.ac.in/~karkare/cs738 Department of CSE, IIT Kanpur Static analysis and compile-time

More information

Loop Optimizations. Outline. Loop Invariant Code Motion. Induction Variables. Loop Invariant Code Motion. Loop Invariant Code Motion

Loop Optimizations. Outline. Loop Invariant Code Motion. Induction Variables. Loop Invariant Code Motion. Loop Invariant Code Motion Outline Loop Optimizations Induction Variables Recognition Induction Variables Combination of Analyses Copyright 2010, Pedro C Diniz, all rights reserved Students enrolled in the Compilers class at the

More information

Intermediate representation

Intermediate representation Intermediate representation Goals: encode knowledge about the program facilitate analysis facilitate retargeting facilitate optimization scanning parsing HIR semantic analysis HIR intermediate code gen.

More information

CIS 341 Final Examination 3 May 2011

CIS 341 Final Examination 3 May 2011 CIS 341 Final Examination 3 May 2011 1 /16 2 /20 3 /40 4 /28 5 /16 Total /120 Do not begin the exam until you are told to do so. You have 120 minutes to complete the exam. There are 12 pages in this exam.

More information

Calvin Lin The University of Texas at Austin

Calvin Lin The University of Texas at Austin Loop Invariant Code Motion Last Time SSA Today Loop invariant code motion Reuse optimization Next Time More reuse optimization Common subexpression elimination Partial redundancy elimination February 23,

More information

Data Structures and Algorithms in Compiler Optimization. Comp314 Lecture Dave Peixotto

Data Structures and Algorithms in Compiler Optimization. Comp314 Lecture Dave Peixotto Data Structures and Algorithms in Compiler Optimization Comp314 Lecture Dave Peixotto 1 What is a compiler Compilers translate between program representations Interpreters evaluate their input to produce

More information

Middle End. Code Improvement (or Optimization) Analyzes IR and rewrites (or transforms) IR Primary goal is to reduce running time of the compiled code

Middle End. Code Improvement (or Optimization) Analyzes IR and rewrites (or transforms) IR Primary goal is to reduce running time of the compiled code Traditional Three-pass Compiler Source Code Front End IR Middle End IR Back End Machine code Errors Code Improvement (or Optimization) Analyzes IR and rewrites (or transforms) IR Primary goal is to reduce

More information

Goals of Program Optimization (1 of 2)

Goals of Program Optimization (1 of 2) Goals of Program Optimization (1 of 2) Goal: Improve program performance within some constraints Ask Three Key Questions for Every Optimization 1. Is it legal? 2. Is it profitable? 3. Is it compile-time

More information

What Do Compilers Do? How Can the Compiler Improve Performance? What Do We Mean By Optimization?

What Do Compilers Do? How Can the Compiler Improve Performance? What Do We Mean By Optimization? What Do Compilers Do? Lecture 1 Introduction I What would you get out of this course? II Structure of a Compiler III Optimization Example Reference: Muchnick 1.3-1.5 1. Translate one language into another

More information

Compiler Optimization Techniques

Compiler Optimization Techniques Compiler Optimization Techniques Department of Computer Science, Faculty of ICT February 5, 2014 Introduction Code optimisations usually involve the replacement (transformation) of code from one sequence

More information

Compiler Design. Fall Control-Flow Analysis. Prof. Pedro C. Diniz

Compiler Design. Fall Control-Flow Analysis. Prof. Pedro C. Diniz Compiler Design Fall 2015 Control-Flow Analysis Sample Exercises and Solutions Prof. Pedro C. Diniz USC / Information Sciences Institute 4676 Admiralty Way, Suite 1001 Marina del Rey, California 90292

More information

Code Optimization. Code Optimization

Code Optimization. Code Optimization 161 Code Optimization Code Optimization 162 Two steps: 1. Analysis (to uncover optimization opportunities) 2. Optimizing transformation Optimization: must be semantically correct. shall improve program

More information

Code optimization. Have we achieved optimal code? Impossible to answer! We make improvements to the code. Aim: faster code and/or less space

Code optimization. Have we achieved optimal code? Impossible to answer! We make improvements to the code. Aim: faster code and/or less space Code optimization Have we achieved optimal code? Impossible to answer! We make improvements to the code Aim: faster code and/or less space Types of optimization machine-independent In source code or internal

More information

Introduction to Optimization Local Value Numbering

Introduction to Optimization Local Value Numbering COMP 506 Rice University Spring 2018 Introduction to Optimization Local Value Numbering source IR IR target code Front End Optimizer Back End code Copyright 2018, Keith D. Cooper & Linda Torczon, all rights

More information

CS 406/534 Compiler Construction Putting It All Together

CS 406/534 Compiler Construction Putting It All Together CS 406/534 Compiler Construction Putting It All Together Prof. Li Xu Dept. of Computer Science UMass Lowell Fall 2004 Part of the course lecture notes are based on Prof. Keith Cooper, Prof. Ken Kennedy

More information

Introduction to Machine-Independent Optimizations - 6

Introduction to Machine-Independent Optimizations - 6 Introduction to Machine-Independent Optimizations - 6 Machine-Independent Optimization Algorithms Department of Computer Science and Automation Indian Institute of Science Bangalore 560 012 NPTEL Course

More information

Loop Invariant Code Motion. Background: ud- and du-chains. Upward Exposed Uses. Identifying Loop Invariant Code. Last Time Control flow analysis

Loop Invariant Code Motion. Background: ud- and du-chains. Upward Exposed Uses. Identifying Loop Invariant Code. Last Time Control flow analysis Loop Invariant Code Motion Loop Invariant Code Motion Background: ud- and du-chains Last Time Control flow analysis Today Loop invariant code motion ud-chains A ud-chain connects a use of a variable to

More information

Compiler Structure. Data Flow Analysis. Control-Flow Graph. Available Expressions. Data Flow Facts

Compiler Structure. Data Flow Analysis. Control-Flow Graph. Available Expressions. Data Flow Facts Compiler Structure Source Code Abstract Syntax Tree Control Flow Graph Object Code CMSC 631 Program Analysis and Understanding Fall 2003 Data Flow Analysis Source code parsed to produce AST AST transformed

More information

Lecture 21 CIS 341: COMPILERS

Lecture 21 CIS 341: COMPILERS Lecture 21 CIS 341: COMPILERS Announcements HW6: Analysis & Optimizations Alias analysis, constant propagation, dead code elimination, register allocation Available Soon Due: Wednesday, April 25 th Zdancewic

More information

CS553 Lecture Dynamic Optimizations 2

CS553 Lecture Dynamic Optimizations 2 Dynamic Optimizations Last time Predication and speculation Today Dynamic compilation CS553 Lecture Dynamic Optimizations 2 Motivation Limitations of static analysis Programs can have values and invariants

More information

Register Allocation. CS 502 Lecture 14 11/25/08

Register Allocation. CS 502 Lecture 14 11/25/08 Register Allocation CS 502 Lecture 14 11/25/08 Where we are... Reasonably low-level intermediate representation: sequence of simple instructions followed by a transfer of control. a representation of static

More information

Compilers. Optimization. Yannis Smaragdakis, U. Athens

Compilers. Optimization. Yannis Smaragdakis, U. Athens Compilers Optimization Yannis Smaragdakis, U. Athens (original slides by Sam Guyer@Tufts) What we already saw Lowering From language-level l l constructs t to machine-level l constructs t At this point

More information

CS553 Lecture Generalizing Data-flow Analysis 3

CS553 Lecture Generalizing Data-flow Analysis 3 Generalizing Data-flow Analysis Announcements Project 2 writeup is available Read Stephenson paper Last Time Control-flow analysis Today C-Breeze Introduction Other types of data-flow analysis Reaching

More information

CSC D70: Compiler Optimization

CSC D70: Compiler Optimization CSC D70: Compiler Optimization Prof. Gennady Pekhimenko University of Toronto Winter 2018 The content of this lecture is adapted from the lectures of Todd Mowry and Phillip Gibbons CSC D70: Compiler Optimization

More information

Comp 204: Computer Systems and Their Implementation. Lecture 22: Code Generation and Optimisation

Comp 204: Computer Systems and Their Implementation. Lecture 22: Code Generation and Optimisation Comp 204: Computer Systems and Their Implementation Lecture 22: Code Generation and Optimisation 1 Today Code generation Three address code Code optimisation Techniques Classification of optimisations

More information

Plan for Today. Concepts. Next Time. Some slides are from Calvin Lin s grad compiler slides. CS553 Lecture 2 Optimizations and LLVM 1

Plan for Today. Concepts. Next Time. Some slides are from Calvin Lin s grad compiler slides. CS553 Lecture 2 Optimizations and LLVM 1 Plan for Today Quiz 2 How to automate the process of performance optimization LLVM: Intro to Intermediate Representation Loops as iteration spaces Data-flow Analysis Intro Control-flow graph terminology

More information

CSE 501: Compiler Construction. Course outline. Goals for language implementation. Why study compilers? Models of compilation

CSE 501: Compiler Construction. Course outline. Goals for language implementation. Why study compilers? Models of compilation CSE 501: Compiler Construction Course outline Main focus: program analysis and transformation how to represent programs? how to analyze programs? what to analyze? how to transform programs? what transformations

More information

COMPILER DESIGN - CODE OPTIMIZATION

COMPILER DESIGN - CODE OPTIMIZATION COMPILER DESIGN - CODE OPTIMIZATION http://www.tutorialspoint.com/compiler_design/compiler_design_code_optimization.htm Copyright tutorialspoint.com Optimization is a program transformation technique,

More information

Machine-Independent Optimizations

Machine-Independent Optimizations Chapter 9 Machine-Independent Optimizations High-level language constructs can introduce substantial run-time overhead if we naively translate each construct independently into machine code. This chapter

More information

Compiler Optimization Intermediate Representation

Compiler Optimization Intermediate Representation Compiler Optimization Intermediate Representation Virendra Singh Associate Professor Computer Architecture and Dependable Systems Lab Department of Electrical Engineering Indian Institute of Technology

More information

COMS W4115 Programming Languages and Translators Lecture 21: Code Optimization April 15, 2013

COMS W4115 Programming Languages and Translators Lecture 21: Code Optimization April 15, 2013 1 COMS W4115 Programming Languages and Translators Lecture 21: Code Optimization April 15, 2013 Lecture Outline 1. Code optimization strategies 2. Peephole optimization 3. Common subexpression elimination

More information

Compiler Construction 2016/2017 Loop Optimizations

Compiler Construction 2016/2017 Loop Optimizations Compiler Construction 2016/2017 Loop Optimizations Peter Thiemann January 16, 2017 Outline 1 Loops 2 Dominators 3 Loop-Invariant Computations 4 Induction Variables 5 Array-Bounds Checks 6 Loop Unrolling

More information

CS577 Modern Language Processors. Spring 2018 Lecture Optimization

CS577 Modern Language Processors. Spring 2018 Lecture Optimization CS577 Modern Language Processors Spring 2018 Lecture Optimization 1 GENERATING BETTER CODE What does a conventional compiler do to improve quality of generated code? Eliminate redundant computation Move

More information

Compiler Construction 2009/2010 SSA Static Single Assignment Form

Compiler Construction 2009/2010 SSA Static Single Assignment Form Compiler Construction 2009/2010 SSA Static Single Assignment Form Peter Thiemann March 15, 2010 Outline 1 Static Single-Assignment Form 2 Converting to SSA Form 3 Optimization Algorithms Using SSA 4 Dependencies

More information

CS 701. Class Meets. Instructor. Teaching Assistant. Key Dates. Charles N. Fischer. Fall Tuesdays & Thursdays, 11:00 12: Engineering Hall

CS 701. Class Meets. Instructor. Teaching Assistant. Key Dates. Charles N. Fischer. Fall Tuesdays & Thursdays, 11:00 12: Engineering Hall CS 701 Charles N. Fischer Class Meets Tuesdays & Thursdays, 11:00 12:15 2321 Engineering Hall Fall 2003 Instructor http://www.cs.wisc.edu/~fischer/cs703.html Charles N. Fischer 5397 Computer Sciences Telephone:

More information

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13 Run-time Environments Lecture 13 by Prof. Vijay Ganesh) Lecture 13 1 What have we covered so far? We have covered the front-end phases Lexical analysis (Lexer, regular expressions,...) Parsing (CFG, Top-down,

More information

Intermediate Representations. Reading & Topics. Intermediate Representations CS2210

Intermediate Representations. Reading & Topics. Intermediate Representations CS2210 Intermediate Representations CS2210 Lecture 11 Reading & Topics Muchnick: chapter 6 Topics today: Intermediate representations Automatic code generation with pattern matching Optimization Overview Control

More information

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall CSE443 Compilers Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall http://www.cse.buffalo.edu/faculty/alphonce/sp17/cse443/index.php https://piazza.com/class/iybn4ndqa1s3ei Announcements Grading survey

More information

Lecture 25: Register Allocation

Lecture 25: Register Allocation Lecture 25: Register Allocation [Adapted from notes by R. Bodik and G. Necula] Topics: Memory Hierarchy Management Register Allocation: Register interference graph Graph coloring heuristics Spilling Cache

More information

Compiler Construction 2010/2011 Loop Optimizations

Compiler Construction 2010/2011 Loop Optimizations Compiler Construction 2010/2011 Loop Optimizations Peter Thiemann January 25, 2011 Outline 1 Loop Optimizations 2 Dominators 3 Loop-Invariant Computations 4 Induction Variables 5 Array-Bounds Checks 6

More information

An Overview of GCC Architecture (source: wikipedia) Control-Flow Analysis and Loop Detection

An Overview of GCC Architecture (source: wikipedia) Control-Flow Analysis and Loop Detection An Overview of GCC Architecture (source: wikipedia) CS553 Lecture Control-Flow, Dominators, Loop Detection, and SSA Control-Flow Analysis and Loop Detection Last time Lattice-theoretic framework for data-flow

More information

Data Flow Analysis. Program Analysis

Data Flow Analysis. Program Analysis Program Analysis https://www.cse.iitb.ac.in/~karkare/cs618/ Data Flow Analysis Amey Karkare Dept of Computer Science and Engg IIT Kanpur Visiting IIT Bombay karkare@cse.iitk.ac.in karkare@cse.iitb.ac.in

More information

Lecture Compiler Middle-End

Lecture Compiler Middle-End Lecture 16-18 18 Compiler Middle-End Jianwen Zhu Electrical and Computer Engineering University of Toronto Jianwen Zhu 2009 - P. 1 What We Have Done A lot! Compiler Frontend Defining language Generating

More information

Example. Example. Constant Propagation in SSA

Example. Example. Constant Propagation in SSA Example x=1 a=x x=2 b=x x=1 x==10 c=x x++ print x Original CFG x 1 =1 a=x 1 x 2 =2 x 3 =φ (x 1,x 2 ) b=x 3 x 4 =1 x 5 = φ(x 4,x 6 ) x 5 ==10 c=x 5 x 6 =x 5 +1 print x 5 CFG in SSA Form In SSA form computing

More information

MIT Introduction to Program Analysis and Optimization. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology

MIT Introduction to Program Analysis and Optimization. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology MIT 6.035 Introduction to Program Analysis and Optimization Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology Program Analysis Compile-time reasoning about run-time behavior

More information

CS 403 Compiler Construction Lecture 10 Code Optimization [Based on Chapter 8.5, 9.1 of Aho2]

CS 403 Compiler Construction Lecture 10 Code Optimization [Based on Chapter 8.5, 9.1 of Aho2] CS 403 Compiler Construction Lecture 10 Code Optimization [Based on Chapter 8.5, 9.1 of Aho2] 1 his Lecture 2 1 Remember: Phases of a Compiler his lecture: Code Optimization means floating point 3 What

More information

CSE 401 Final Exam. March 14, 2017 Happy π Day! (3/14) This exam is closed book, closed notes, closed electronics, closed neighbors, open mind,...

CSE 401 Final Exam. March 14, 2017 Happy π Day! (3/14) This exam is closed book, closed notes, closed electronics, closed neighbors, open mind,... CSE 401 Final Exam March 14, 2017 Happy π Day! (3/14) Name This exam is closed book, closed notes, closed electronics, closed neighbors, open mind,.... Please wait to turn the page until everyone has their

More information

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division CS 164 Spring 2010 P. N. Hilfinger CS 164: Final Examination (revised) Name: Login: You have

More information

Compiler Optimizations. Chapter 8, Section 8.5 Chapter 9, Section 9.1.7

Compiler Optimizations. Chapter 8, Section 8.5 Chapter 9, Section 9.1.7 Compiler Optimizations Chapter 8, Section 8.5 Chapter 9, Section 9.1.7 2 Local vs. Global Optimizations Local: inside a single basic block Simple forms of common subexpression elimination, dead code elimination,

More information

CSE P 501 Compilers. SSA Hal Perkins Spring UW CSE P 501 Spring 2018 V-1

CSE P 501 Compilers. SSA Hal Perkins Spring UW CSE P 501 Spring 2018 V-1 CSE P 0 Compilers SSA Hal Perkins Spring 0 UW CSE P 0 Spring 0 V- Agenda Overview of SSA IR Constructing SSA graphs Sample of SSA-based optimizations Converting back from SSA form Sources: Appel ch., also

More information

Fall Compiler Principles Lecture 12: Register Allocation. Roman Manevich Ben-Gurion University

Fall Compiler Principles Lecture 12: Register Allocation. Roman Manevich Ben-Gurion University Fall 2014-2015 Compiler Principles Lecture 12: Register Allocation Roman Manevich Ben-Gurion University Syllabus Front End Intermediate Representation Optimizations Code Generation Scanning Lowering Local

More information

Languages and Compiler Design II IR Code Optimization

Languages and Compiler Design II IR Code Optimization Languages and Compiler Design II IR Code Optimization Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU Spring 2010 rev.: 4/16/2010 PSU CS322 HM 1 Agenda IR Optimization

More information

Compiler Construction SMD163. Understanding Optimization: Optimization is not Magic: Goals of Optimization: Lecture 11: Introduction to optimization

Compiler Construction SMD163. Understanding Optimization: Optimization is not Magic: Goals of Optimization: Lecture 11: Introduction to optimization Compiler Construction SMD163 Understanding Optimization: Lecture 11: Introduction to optimization Viktor Leijon & Peter Jonsson with slides by Johan Nordlander Contains material generously provided by

More information

CS153: Compilers Lecture 17: Control Flow Graph and Data Flow Analysis

CS153: Compilers Lecture 17: Control Flow Graph and Data Flow Analysis CS153: Compilers Lecture 17: Control Flow Graph and Data Flow Analysis Stephen Chong https://www.seas.harvard.edu/courses/cs153 Announcements Project 5 out Due Tuesday Nov 13 (14 days) Project 6 out Due

More information

Data-Flow Analysis Foundations

Data-Flow Analysis Foundations CS 301 Spring 2016 Meetings April 11 Data-Flow Foundations Plan Source Program Lexical Syntax Semantic Intermediate Code Generation Machine- Independent Optimization Code Generation Target Program This

More information

Lecture Notes on Loop-Invariant Code Motion

Lecture Notes on Loop-Invariant Code Motion Lecture Notes on Loop-Invariant Code Motion 15-411: Compiler Design André Platzer Lecture 17 1 Introduction In this lecture we discuss an important instance of Partial Redundancy Elimination (PRE) and

More information

Register Allocation. Lecture 38

Register Allocation. Lecture 38 Register Allocation Lecture 38 (from notes by G. Necula and R. Bodik) 4/27/08 Prof. Hilfinger CS164 Lecture 38 1 Lecture Outline Memory Hierarchy Management Register Allocation Register interference graph

More information

CHAPTER 3. Register allocation

CHAPTER 3. Register allocation CHAPTER 3 Register allocation In chapter 1 we simplified the generation of x86 assembly by placing all variables on the stack. We can improve the performance of the generated code considerably if we instead

More information

Advanced Programming Methods. Introduction in program analysis

Advanced Programming Methods. Introduction in program analysis Advanced Programming Methods Introduction in program analysis What is Program Analysis? Very broad topic, but generally speaking, automated analysis of program behavior Program analysis is about developing

More information

Data Flow Analysis. CSCE Lecture 9-02/15/2018

Data Flow Analysis. CSCE Lecture 9-02/15/2018 Data Flow Analysis CSCE 747 - Lecture 9-02/15/2018 Data Flow Another view - program statements compute and transform data So, look at how that data is passed through the program. Reason about data dependence

More information

CSE 501 Midterm Exam: Sketch of Some Plausible Solutions Winter 1997

CSE 501 Midterm Exam: Sketch of Some Plausible Solutions Winter 1997 1) [10 pts] On homework 1, I asked about dead assignment elimination and gave the following sample solution: 8. Give an algorithm for dead assignment elimination that exploits def/use chains to work faster

More information

Abstract Interpretation Continued

Abstract Interpretation Continued Abstract Interpretation Continued Height of Lattice: Length of Max. Chain height=5 size=14 T height=2 size = T -2-1 0 1 2 Chain of Length n A set of elements x 0,x 1,..., x n in D that are linearly ordered,

More information

Running class Timing on Java HotSpot VM, 1

Running class Timing on Java HotSpot VM, 1 Compiler construction 2009 Lecture 3. A first look at optimization: Peephole optimization. A simple example A Java class public class A { public static int f (int x) { int r = 3; int s = r + 5; return

More information

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan Language Processing Systems Prof. Mohamed Hamada Software ngineering Lab. The University of Aizu Japan Intermediate/Code Generation Source language Scanner (lexical analysis) tokens Parser (syntax analysis)

More information

Lecture Notes on Liveness Analysis

Lecture Notes on Liveness Analysis Lecture Notes on Liveness Analysis 15-411: Compiler Design Frank Pfenning André Platzer Lecture 4 1 Introduction We will see different kinds of program analyses in the course, most of them for the purpose

More information

register allocation saves energy register allocation reduces memory accesses.

register allocation saves energy register allocation reduces memory accesses. Lesson 10 Register Allocation Full Compiler Structure Embedded systems need highly optimized code. This part of the course will focus on Back end code generation. Back end: generation of assembly instructions

More information

Lecture Notes on Value Propagation

Lecture Notes on Value Propagation Lecture Notes on Value Propagation 15-411: Compiler Design Frank Pfenning, Rob Simmons Lecture 17 October 27, 2015 1 Introduction The opportunities for optimizations 1 in compiler-generated code are plentiful.

More information

Compiler Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

Compiler Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore Compiler Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore Module No. # 10 Lecture No. # 16 Machine-Independent Optimizations Welcome to the

More information

Code generation and optimization

Code generation and optimization Code generation and timization Generating assembly How do we convert from three-address code to assembly? Seems easy! But easy solutions may not be the best tion What we will cover: Peephole timizations

More information

Control-Flow Analysis

Control-Flow Analysis Control-Flow Analysis Dragon book [Ch. 8, Section 8.4; Ch. 9, Section 9.6] Compilers: Principles, Techniques, and Tools, 2 nd ed. by Alfred V. Aho, Monica S. Lam, Ravi Sethi, and Jerey D. Ullman on reserve

More information

Compilation 2012 Static Analysis

Compilation 2012 Static Analysis Compilation 2012 Jan Midtgaard Michael I. Schwartzbach Aarhus University Interesting Questions Is every statement reachable? Does every non-void method return a value? Will local variables definitely be

More information

CS153: Compilers Lecture 15: Local Optimization

CS153: Compilers Lecture 15: Local Optimization CS153: Compilers Lecture 15: Local Optimization Stephen Chong https://www.seas.harvard.edu/courses/cs153 Announcements Project 4 out Due Thursday Oct 25 (2 days) Project 5 out Due Tuesday Nov 13 (21 days)

More information

Register Allocation. Lecture 16

Register Allocation. Lecture 16 Register Allocation Lecture 16 1 Register Allocation This is one of the most sophisticated things that compiler do to optimize performance Also illustrates many of the concepts we ve been discussing in

More information

Lecture Notes on Common Subexpression Elimination

Lecture Notes on Common Subexpression Elimination Lecture Notes on Common Subexpression Elimination 15-411: Compiler Design Frank Pfenning Lecture 18 October 29, 2015 1 Introduction Copy propagation allows us to have optimizations with this form: l :

More information

CS 426 Fall Machine Problem 4. Machine Problem 4. CS 426 Compiler Construction Fall Semester 2017

CS 426 Fall Machine Problem 4. Machine Problem 4. CS 426 Compiler Construction Fall Semester 2017 CS 426 Fall 2017 1 Machine Problem 4 Machine Problem 4 CS 426 Compiler Construction Fall Semester 2017 Handed out: November 16, 2017. Due: December 7, 2017, 5:00 p.m. This assignment deals with implementing

More information

Midterm II CS164, Spring 2006

Midterm II CS164, Spring 2006 Midterm II CS164, Spring 2006 April 11, 2006 Please read all instructions (including these) carefully. Write your name, login, SID, and circle the section time. There are 10 pages in this exam and 4 questions,

More information

Department of Computer and Mathematical Sciences

Department of Computer and Mathematical Sciences _ Unit 2: Programming in C++, pages 1 of 8 Department of Computer and Mathematical Sciences CS 1410 Intro to Computer Science with C++ 8 Lab 8: While Loops Objectives: The objective of this lab is to help

More information

Combining Analyses, Combining Optimizations - Summary

Combining Analyses, Combining Optimizations - Summary Combining Analyses, Combining Optimizations - Summary 1. INTRODUCTION Cliff Click s thesis Combining Analysis, Combining Optimizations [Click and Cooper 1995] uses a structurally different intermediate

More information