Code Optimization Overview and Examples Control Flow Graph

Slides:



Advertisements
Similar presentations
CSC 4181 Compiler Construction Code Generation & Optimization.
Advertisements

Data-Flow Analysis II CS 671 March 13, CS 671 – Spring Data-Flow Analysis Gather conservative, approximate information about what a program.
Synopsys University Courseware Copyright © 2012 Synopsys, Inc. All rights reserved. Compiler Optimization and Code Generation Lecture - 3 Developed By:
7. Optimization Prof. O. Nierstrasz Lecture notes by Marcus Denker.
Course Outline Traditional Static Program Analysis Software Testing
Lecture 11: Code Optimization CS 540 George Mason University.
Chapter 9 Code optimization Section 0 overview 1.Position of code optimizer 2.Purpose of code optimizer to get better efficiency –Run faster –Take less.
Loops or Lather, Rinse, Repeat… CS153: Compilers Greg Morrisett.
ECE 454 Computer Systems Programming Compiler and Optimization (I) Ding Yuan ECE Dept., University of Toronto
1 Chapter 8: Code Generation. 2 Generating Instructions from Three-address Code Example: D = (A*B)+C =* A B T1 =+ T1 C T2 = T2 D.
Jeffrey D. Ullman Stanford University. 2  A never-published Stanford technical report by Fran Allen in  Fran won the Turing award in  Flow.
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis – today’s class –Classic analyses.
Chapter 10 Code Optimization. A main goal is to achieve a better performance Front End Code Gen Intermediate Code source Code target Code user Machine-
1 Code Optimization Code produced by compilation algorithms can often be improved (ideally optimized) in terms of run-time speed and the amount of memory.
1 Code Optimization. 2 The Code Optimizer Control flow analysis: control flow graph Data-flow analysis Transformations Front end Code generator Code optimizer.
1 CS 201 Compiler Construction Lecture 5 Code Optimizations: Copy Propagation & Elimination.
Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point.
Recap from last time Saw several examples of optimizations –Constant folding –Constant Prop –Copy Prop –Common Sub-expression Elim –Partial Redundancy.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
CS 536 Spring Intermediate Code. Local Optimizations. Lecture 22.
1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning.
4/23/09Prof. Hilfinger CS 164 Lecture 261 IL for Arrays & Local Optimizations Lecture 26 (Adapted from notes by R. Bodik and G. Necula)
Administrative info Subscribe to the class mailing list –instructions are on the class web page, which is accessible from my home page, which is accessible.
9. Optimization Marcus Denker. 2 © Marcus Denker Optimization Roadmap  Introduction  Optimizations in the Back-end  The Optimizer  SSA Optimizations.
Another example p := &x; *p := 5 y := x + 1;. Another example p := &x; *p := 5 y := x + 1; x := 5; *p := 3 y := x + 1; ???
2015/6/24\course\cpeg421-10F\Topic1-b.ppt1 Topic 1b: Flow Analysis Some slides come from Prof. J. N. Amaral
CS 412/413 Spring 2007Introduction to Compilers1 Lecture 29: Control Flow Analysis 9 Apr 07 CS412/413 Introduction to Compilers Tim Teitelbaum.
1 Copy Propagation What does it mean? – Given an assignment x = y, replace later uses of x with uses of y, provided there are no intervening assignments.
Intermediate Code. Local Optimizations
Improving Code Generation Honors Compilers April 16 th 2002.
Compiler Construction A Compulsory Module for Students in Computer Science Department Faculty of IT / Al – Al Bayt University Second Semester 2008/2009.
Machine-Independent Optimizations Ⅰ CS308 Compiler Theory1.
Topic #10: Optimization EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Introduction For some compiler, the intermediate code is a pseudo code of a virtual machine. Interpreter of the virtual machine is invoked to execute the.
CSc 453 Final Code Generation Saumya Debray The University of Arizona Tucson.
1 Code optimization “Code optimization refers to the techniques used by the compiler to improve the execution efficiency of the generated object code”
1 CS 201 Compiler Construction Introduction. 2 Instructor Information Rajiv Gupta Office: WCH Room Tel: (951) Office.
Synopsys University Courseware Copyright © 2012 Synopsys, Inc. All rights reserved. Compiler Optimization and Code Generation Lecture - 1 Developed By:
CS412/413 Introduction to Compilers and Translators April 2, 1999 Lecture 24: Introduction to Optimization.
©SoftMoore ConsultingSlide 1 Code Optimization. ©SoftMoore ConsultingSlide 2 Code Optimization Code generation techniques and transformations that result.
CS 412/413 Spring 2005Introduction to Compilers1 CS412/CS413 Introduction to Compilers Tim Teitelbaum Lecture 30: Loop Optimizations and Pointer Analysis.
Code Optimization More Optimization Techniques. More Optimization Techniques  Loop optimization  Code motion  Strength reduction for induction variables.
More Code Generation and Optimization Pat Morin COMP 3002.
Simone Campanoni CFA Simone Campanoni
Code Optimization Code produced by compilation algorithms can often be improved (ideally optimized) in terms of run-time speed and the amount of memory.
Code Optimization Overview and Examples
High-level optimization Jakub Yaghob
Code Optimization.
Optimization Code Optimization ©SoftMoore Consulting.
Princeton University Spring 2016
Fall Compiler Principles Lecture 8: Loop Optimizations
Machine-Independent Optimization
Control Flow Analysis CS 4501 Baishakhi Ray.
Optimizing Transformations Hal Perkins Autumn 2011
Unit IV Code Generation
CS 201 Compiler Construction
Topic 4: Flow Analysis Some slides come from Prof. J. N. Amaral
Optimizing Transformations Hal Perkins Winter 2008
Compiler Code Optimizations
Optimizations using SSA
Control Flow Analysis (Chapter 7)
Fall Compiler Principles Lecture 10: Loop Optimizations
Optimization 薛智文 (textbook ch# 9) 薛智文 96 Spring.
Intermediate Code Generation
Lecture 19: Code Optimisation
Taken largely from University of Delaware Compiler Notes
Code Generation Part II
Introduction to Optimization
Code Optimization.
CS 201 Compiler Construction
Presentation transcript:

Code Optimization Overview and Examples Control Flow Graph

Code Optimization Why Target Types Reduce programmers’ burden Allow programmers to concentrate on high level concept Without worrying about performance issues Target Reduce execution time Reduce space Sometimes, these are tradeoffs Types Intermediate code level We are looking at this part now Assembly level Instruction selection, register allocation, etc.

Code Optimization Scope Peephole analysis Local analysis Within one or a few instructions Local analysis Within a basic block Global analysis Entire procedure or within a certain scope Inter-procedural analysis Beyond a procedure, consider the entire program

Code Optimization Techniques Constant propagation Constant folding Algebraic simplification, strength reduction Copy propagation Common subexpression elimination Unreacheable code elimination Dead code elimination Loop Optimization Function related Function inlining, function cloning Most of the optimization goals are identified through experience, but techniques requires theoretical foundation

Code Optimization Techniques Constant propagation If the value of a variable is a constant, then replace the variable by the constant E.g. N := 10; C := 2; for (i:=0; i<N; i++) { s := s + i*C; } for (i:=0; i<10; i++) { s := s + i*2; } Requirement: After a constant assignment to the variable Until next assignment of the variable Need data flow analysis to exam the propagation

Code Optimization Techniques Algebraic simplification More general form of constant folding, e.g., x + 0  x x – 0  x x * 1  x x / 1  x x * 0  0 Repeatedly apply the rules (y * 1 + 0) / 1  y Strength reduction Replace expensive operations E.g., x := x * 8  x := x << 3

Code Optimization Techniques Copy propagation Extension of constant propagation After y is assigned to x, use y to replace x till x is assigned again Example x := y;  s := y * f(y) s := x * f(x) Reduce the copying If y is reassigned in between, then this action cannot be performed

Code Optimization Techniques Common subexpression elimination Example: a := b + c a := b + c c := b + c  c := a d := b + c d := b + c Example in array index calculations c[i+1] := a[i+1] + b[i+1] During address computation, i+1 should be reused Not visible in high level code, but in intermediate code

Code Optimization Techniques Unreacheable code elimination Construct the control flow graph Unreachable code block will not have an incoming edge Dead code elimination Ineffective statements x := y + 1 (immediately redefined, eliminate!) y := 5  y := 5 x := 2 * z x := 2 * z A variable is dead if it is never used after last definition Eliminate assignments to dead variables Need to do data flow analysis to find dead variables

Code Optimization Techniques Function inlining Replace a function call with the body of the function Save a lot of copying of the parameters, return address, etc. Function cloning Create specialized code for a function for different calling parameters

Code Optimization Techniques Loop optimization Consumes 90% of the execution time  a larger payoff to optimize the code within a loop Techniques Loop invariant detection and code motion Induction variable elimination Strength reduction in loops Loop unrolling Loop peeling Loop fusion

Code Optimization Techniques Loop invariant detection and code motion If the result of a statement or expression does not change within a loop, and it has no external side-effect Computation can be moved to the outside of the loop Example for (i=0; i<n; i++) a[i] := a[i] + x/y;  for (i=0; i<n; i++) { c := x/y; a[i] := a[i] + c; } // three address code  c := x/y; for (i=0; i<n; i++) a[i] := a[i] + c; Induction variable elimination If there are multiple induction variables in a loop, can eliminate the ones which are used only in the test condition s := 0; for (i=0; i<n; i++) { s := s + 4; }  s := 0; while (s < 4*n) { s := s + 4; }

Code Optimization Techniques Strength reduction in loops Example s := 0; for (i=0; i<n; i++) { v := 4 * i; s := s + v; )  s := 0; for (i=0; i<n; i++) { v := v + 4; s := s + v; ) Loop unrolling Execute loop body multiple times at each iteration Get rid of the conditional branches, if possible Allow optimization to cross multiple iterations of the loop Especially for parallel instruction execution Space time tradeoff Increase in code size, reduce some instructions Loop peeling Similar to unrolling But unroll the first and/or last iterations

Code Optimization Techniques Loop fusion Example for i=1 to N do A[i] = B[i] + 1 endfor C[i] = A[i] / 2 D[i] = 1 / C[i+1] for i=1 to N do A[i] = B[i] + 1 C[i] = A[i] / 2 D[i] = 1 / C[i+1] endfor Is this correct? Actually, cannot fuse the third loop Before Loop Fusion

Code Optimization Will cover a small portion of the techniques Peephole and local analysis generally involve Constant folding Algebraic simplification, strength reduction Global analysis generally requires Control flow graph Data flow analysis Loop optimization

Control Flow Graph CFG Example Is a directed graph Nodes are basic blocks A sequence of statements No control flow within a basic block Edges represent execution flow Example (1) t := 2*x; (2) w := t+y; (3) if (w<0) goto 8 (4) … … … (8) w := –w (9) goto 4 (1) t:=2*x (2) w:=t+y (3) if (w<0) goto 8 W<0 !(W<0) (8) w := –w (9) goto 4 (4) … … …

Construct CFG from Three Address Code Identify leader statements First statement of the program Target of any branch statement (conditional or unconditional) Statement that immediately follows a branch or return statement Construct blocks Append the subsequent instructions, up to the statement before the next leader (1) prod := 0 (2) i := 1 (3) t1 := 4 * i (4) t2 := a[t1] (5) t3 := 4 * i (6) t4 := b[t3] (7) t5 := t2 * t4 (8) t6 := prod + t5 (9) prod := t6 (10) t7 := i + 1 (11) i := t7 (12) if i <= 20 goto (3) (13) … Easy to know that there is a branch How about a loop?

Finding a Loop in a CFG To facilitate loop optimization How to find a loop in a CFG? Need to define dominance relation Need to find a back edge of the loop Based on the back edge, find the loop body Identify loop at the high level Loop can also be recorded during the intermediate code generation phase But as long as a language allows goto statements, it is necessary to do loop identification

Dominate Relation Dominate relation Dominator set of node b In a single entry CFG, a node a dominates a node b if every path from the start node s to node b goes through a Expressed as a  b a < b  a  b and a  b Dominate relation is always acyclic Dominator set of node b The set of all nodes that dominate b, expressed as dom(b) By definition, b  dom(b) Each node dominates itself Immediate dominator a immediately dominates b if a < b and there is no c such that a < c < b Each node, besides the entry node, has a unique immediate dominator

Dominate Relation Dominator Sets: Immediate dominator dom(1) = {1} 1 immediate dominates 2 2 immediate dominates 3, 10 1 S 2 3 4 5 6 7 dom(5) = {1, 2, 3, 4, 5} dom(6) = {1, 2, 3, 4, 5, 6} dom(7) = {1, 2, 3, 4, 5, 7} dom(8) = {1, 2, 3, 4, 5, 8} dom(9) = {1, 2, 3, 4, 9} dom(10) = {1, 2, 10} 3 imm-dom 4 4 imm-dom 5, 9 5 imm-dom 6, 7, 8 8 9 10

Finding a Loop in a CFG 1 2 3 Strongly connected subgraph Consider a CFG G (directed graph) There exists a path from any node in G to any other node Then G is strongly connected Natural loop definition A strongly connected subgraph in a CFG There is an entry node that dominates all other nodes in the subgraph There is at least one back edge to allow iteration No, nodes 2 and 3 form a strongly connected subgraph, but there is no entry node that dominates all other nodes. 1 2 3 Do nodes 2 and 3 form a loop?

Finding a Loop in a CFG How to find natural loops in a CFG? Build the dominator tree Starting node is the root A node dominates its descendants Find the back edges There exists an edge (b, a) in the CFG a dominates b Then, edge (b, a) is a back edge Identify the nodes in a natural loop associated with a back edge (b, a), including all nodes: Dominated by a (a is the entry of the loop) can reach b without going through a

Finding a Loop in a CFG -- Example Build the dominator tree Follow the immediate dominate relation 1 2 3 1 Each node (beside the entry node) has a unique immediate dominator  always possible to build the dominator tree (each node has one and only one parent) 2 4 3 5 6 7 4 5 6 8 7 9 10 8 9 10

Finding a Loop in a CFG -- Example Find the back edges From the bottom of the tree, find outgoing edges that go up the tree 1 2 3 4 7 5 6 8 9 10 1 (10, 7) (9, 1) (8, 3) (7, 4) (4, 3) 2 3 4 5 6 7 8 9 10

Finding a Loop in a CFG -- Example Find the natural loop associated with each back edge (b, a) Dominated by a (a is the entry of the loop) can reach b without going through a (10, 7): entry is 7 {7, 8, 10} (9, 1) entry is 1 include all nodes (8, 3) {3, 4, 5, 6, 7, 8, 10 } (7, 4) {4, 5, 6, 7, 8, 10} (4, 3) {3, 4, 5, 6, 7, 8, 10} back edge (10,7) entry is 7 only 8, 9, 10 are dominated by 7 7 and 10 are in the loop 8 can reach 10 without going through 7 9 cannot reach 10 without going through 7 1 2 3 4 7 5 6 8 9 10 1 2 3 4 5 6 7 8 9 10