CS 412/413 Spring 2007Introduction to Compilers1 Lecture 29: Control Flow Analysis 9 Apr 07 CS412/413 Introduction to Compilers Tim Teitelbaum.

Slides:



Advertisements
Similar presentations
Data-Flow Analysis II CS 671 March 13, CS 671 – Spring Data-Flow Analysis Gather conservative, approximate information about what a program.
Advertisements

School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) SSA Guo, Yao.
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.
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis – today’s class –Classic analyses.
SSA.
CS412/413 Introduction to Compilers Radu Rugina Lecture 37: DU Chains and SSA Form 29 Apr 02.
1 Code Optimization. 2 The Code Optimizer Control flow analysis: control flow graph Data-flow analysis Transformations Front end Code generator Code optimizer.
Program Representations. Representing programs Goals.
1 CS 201 Compiler Construction Lecture 7 Code Optimizations: Partial Redundancy Elimination.
1 Data flow analysis Goal : collect information about how a procedure manipulates its data This information is used in various optimizations For example,
1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis.
Example in SSA X := Y op Z in out F X := Y op Z (in) = in [ { X ! Y op Z } X :=  (Y,Z) in 0 out F X :=   (in 0, in 1 ) = (in 0 Å in 1 ) [ { X ! E |
Common Sub-expression Elim Want to compute when an expression is available in a var Domain:
1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis.
Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point.
CS 536 Spring Global Optimizations Lecture 23.
PSUCS322 HM 1 Languages and Compiler Design II Basic Blocks Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU Spring.
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
Data Flow Analysis Compiler Design Nov. 3, 2005.
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
2015/6/24\course\cpeg421-10F\Topic1-b.ppt1 Topic 1b: Flow Analysis Some slides come from Prof. J. N. Amaral
Data Flow Analysis Compiler Design October 5, 2004 These slides live on the Web. I obtained them from Jeff Foster and he said that he obtained.
Loop invariant detection using SSA An expression is invariant in a loop L iff: (base cases) –it’s a constant –it’s a variable use, all of whose single.
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.
Prof. Fateman CS 164 Lecture 221 Global Optimization Lecture 22.
Common Sub-expression Elim Want to compute when an expression is available in a var Domain:
Recap from last time: live variables x := 5 y := x + 2 x := x + 1 y := x y...
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Loops Guo, Yao.
Machine-Independent Optimizations Ⅰ CS308 Compiler Theory1.
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
Direction of analysis Although constraints are not directional, flow functions are All flow functions we have seen so far are in the forward direction.
Prof. Bodik CS 164 Lecture 16, Fall Global Optimization Lecture 16.
1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving.
Precision Going back to constant prop, in what cases would we lose precision?
1 CS 201 Compiler Construction Data Flow Analysis.
1 Code Optimization Chapter 9 (1 st ed. Ch.10) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
Software (Program) Analysis. Automated Static Analysis Static analyzers are software tools for source text processing They parse the program text and.
U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Emery Berger University of Massachusetts, Amherst Advanced Compilers CMPSCI 710.
Program Representations. Representing programs Goals.
Dead Code Elimination This lecture presents the algorithm Dead from EaC2e, Chapter 10. That algorithm derives, in turn, from Rob Shillner’s unpublished.
CS412/413 Introduction to Compilers Radu Rugina Lecture 18: Control Flow Graphs 29 Feb 02.
1 Control Flow Graphs. 2 Optimizations Code transformations to improve program –Mainly: improve execution time –Also: reduce program size Can be done.
Compiler Principles Fall Compiler Principles Lecture 11: Loop Optimizations Roman Manevich Ben-Gurion University.
1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis.
Data Flow Analysis II AModel Checking and Abstract Interpretation Feb. 2, 2011.
1 Code Optimization Chapter 9 (1 st ed. Ch.10) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
Loops Simone Campanoni
CS 412/413 Spring 2005Introduction to Compilers1 CS412/CS413 Introduction to Compilers Tim Teitelbaum Lecture 30: Loop Optimizations and Pointer Analysis.
CS 201 Compiler Construction
Static Single Assignment
Program Representations
Princeton University Spring 2016
CSC D70: Compiler Optimization Dataflow-2 and Loops
CSC D70: Compiler Optimization LICM: Loop Invariant Code Motion
Fall Compiler Principles Lecture 8: Loop Optimizations
CS 201 Compiler Construction
Control Flow Analysis CS 4501 Baishakhi Ray.
Code Optimization Chapter 10
Code Optimization Chapter 9 (1st ed. Ch.10)
Topic 4: Flow Analysis Some slides come from Prof. J. N. Amaral
Code Optimization Overview and Examples Control Flow Graph
Control Flow Analysis (Chapter 7)
Fall Compiler Principles Lecture 10: Loop Optimizations
Data Flow Analysis Compiler Design
Static Single Assignment
Taken largely from University of Delaware Compiler Notes
CSC D70: Compiler Optimization LICM: Loop Invariant Code Motion
Live variables and copy propagation
Presentation transcript:

CS 412/413 Spring 2007Introduction to Compilers1 Lecture 29: Control Flow Analysis 9 Apr 07 CS412/413 Introduction to Compilers Tim Teitelbaum

CS 412/413 Spring 2007Introduction to Compilers2 Program Loops Loop = a computation repeatedly executed until a terminating condition is reached High-level loop constructs: –While loop: while(E) S –Do-while loop:do S while(E) –For loop: for(i=1, i<=u, i+=c) S Why are loops important: –Most of the execution time is spent in loops –Typically: 90/10 rule, 10% code is a loop Therefore, loops are important targets of optimizations

CS 412/413 Spring 2007Introduction to Compilers3 Detecting Loops Need to identify loops in the program –Easy to detect loops in high-level constructs –Harder to detect loops in low-level code or in general control-flow graphs Examples where loop detection is difficult: –Languages with unstructured “goto” constructs: structure of high-level loop constructs may be destroyed –Optimizing Java bytecodes (without high-level source program): only low-level code is available

CS 412/413 Spring 2007Introduction to Compilers4 Goal: identify loops in the control flow graph A loop in the CFG: –Is a set of CFG nodes (basic blocks) –Has a loop header such that control to all nodes in the loop always goes through the header –Has a back edge from one of its nodes to the header Control-Flow Analysis

CS 412/413 Spring 2007Introduction to Compilers5 Goal: identify loops in the control flow graph A loop in the CFG: –Is a set of CFG nodes (basic blocks) –Has a loop header such that control to all nodes in the loop always goes through the header –Has a back edge from one of its nodes to the header Control-Flow Analysis

CS 412/413 Spring 2007Introduction to Compilers6 Dominators Use concept of dominators in CFG to identify loops Node d dominates node n if all paths from the entry node to n go through d Intuition: –Header of a loop dominates all nodes in loop body –Back edges = edges whose heads dominate their tails –Loop identification = back edge identification Every node dominates itself 1 dominates 1, 2, 3, 4 2 doesn’t dominate 4 3 doesn’t dominate 4

CS 412/413 Spring 2007Introduction to Compilers7 Immediate Dominators Properties: 1. CFG entry node n 0 in dominates all CFG nodes 2. If d1 and d2 dominate n, then either –d1 dominates d2, or –d2 dominates d1 d strictly dominates n if d dominates n and d  n The immediate dominator idom(n) of a node n is the unique last strict dominator on any path from n 0 to n

CS 412/413 Spring 2007Introduction to Compilers8 Dominator Tree Build a dominator tree as follows: –Root is CFG entry node n 0 –m is child of node n iff n=idom(m) Example:

CS 412/413 Spring 2007Introduction to Compilers9 Computing Dominators Formulate problem as a system of constraints: –Define dom(n) = set of nodes that dominate n –dom(n 0 )= {n 0 } –dom(n) = ∩ { dom(m) | m  pred(n) } ∪ {n} i.e, the dominators of n are the dominators of all of n’s predecessors and n itself

CS 412/413 Spring 2007Introduction to Compilers10 Dominators as a Dataflow Problem Let N = set of all basic blocks Lattice: (2 N, ⊆ ); has finite height Meet is set intersection, top element is N Is a forward dataflow analysis Dataflow equations: out[B] = F B (in[B]), for all B in[B] = ∩ {out[B’] | B’  pred(B)}, for all B in[B s ] = {} Transfer functions: F B (X) = X ⋃ {B} - are monotonic and distributive Iterative solving of dataflow equation: - terminates - computes MOP solution {a} {b}{c} {a,b}{a,c}{b,c} {a,b,c} 

CS 412/413 Spring 2007Introduction to Compilers11 Natural Loops Back edge: edge n  h such that h dominates n Natural loop of a back edge n  h: –h is loop header –Set of loop nodes is set of all nodes that can reach n without going through h Algorithm to identify natural loops in CFG: –Compute dominator relation –Identify back edges –Compute the loop for each back edge for each node h in dominator tree for each node n for which there exists a back edge n  h define the loop with header h back edge n  h body consisting of all nodes reachable from n by a depth first search backwards from n that stops at h

CS 412/413 Spring 2007Introduction to Compilers12 Disjoint and Nested Loops Property: for any two natural loops in the flow graph, one of the following is true: 1. They are disjoint 2. They are nested 3. They have the same header Eliminate alternative 3: if two loops have the same header and none is nested in the other, combine all nodes into a single loop 1 23 Two loops: {1,2} and {1,3} Combine into one loop: {1,2,3}

CS 412/413 Spring 2007Introduction to Compilers13 Loop Preheader Several optimizations add code before header Insert a new basic block (called preheader) in the CFG to hold this code

CS 412/413 Spring 2007Introduction to Compilers14 Loop optimizations Now we know the loops Next: optimize these loops –Loop invariant code motion –Strength reduction of induction variables –Induction variable elimination

CS 412/413 Spring 2007Introduction to Compilers15 Loop Invariant Code Motion Idea: if a computation produces same result in all loop iterations, move it out of the loop Example: for (i=0; i<10; i++) a[i] = 10*i + x*x; Expression x*x produces the same result in each iteration; move it out of the loop: t = x*x; for (i=0; i<10; i++) a[i] = 10*i + t;

CS 412/413 Spring 2007Introduction to Compilers16 Loop Invariant Computation An instruction a = b OP c is loop-invariant if each operand is: –Constant, or –Has all definitions outside the loop, or –Has exactly one definition, and that is a loop-invariant computation Reaching definitions analysis computes all the definitions of x and y that may reach t = x OP y

CS 412/413 Spring 2007Introduction to Compilers17 Algorithm INV =  Repeat for each instruction I in loop such that i  INV if operands are constants, or have definitions outside the loop, or have exactly one definition d  INV then INV = INV U {i} Until no changes in INV

CS 412/413 Spring 2007Introduction to Compilers18 Code Motion Next: move loop-invariant code out of the loop Suppose a = b OP c is loop-invariant We want to hoist it out of the loop Code motion of a definition d: a = b OP c to pre-header is valid if: 1. Definition d dominates all loop exits where a is live 2. There is no other definition of a in loop 3. All uses of a in loop can only be reached from definition d

CS 412/413 Spring 2007Introduction to Compilers19 Other Issues Preserve dependencies between loop-invariant instructions when hoisting code out of the loop for (i=0; i<N; i++) {x = y+z; x = y+z;t = x*x; a[i] = 10*i + x*x;for(i=0; i<N; i++) } a[i] = 10*i + t; Nested loops: apply loop-invariant code motion algorithm multiple times for (i=0; i<N; i++) for (j=0; j<M; j++) a[i][j] = x*x + 10*i + 100*j; t1 = x*x; for (i=0; i<N; i++) { t2 = t1+ 10*i; for (j=0; j<M; j++) a[i][j] = t *j; }