CS 412/413 Spring 2005Introduction to Compilers1 CS412/CS413 Introduction to Compilers Tim Teitelbaum Lecture 30: Loop Optimizations and Pointer Analysis.

Slides:



Advertisements
Similar presentations
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) SSA Guo, Yao.
Advertisements

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.
Loops or Lather, Rinse, Repeat… CS153: Compilers Greg Morrisett.
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis – today’s class –Classic analyses.
Control-Flow Graphs & Dataflow Analysis CS153: Compilers Greg Morrisett.
Analysis of programs with pointers. Simple example What are the dependences in this program? Problem: just looking at variable names will not give you.
CS412/413 Introduction to Compilers Radu Rugina Lecture 37: DU Chains and SSA Form 29 Apr 02.
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-
Program Representations. Representing programs Goals.
1 CS 201 Compiler Construction Lecture 7 Code Optimizations: Partial Redundancy Elimination.
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 |
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.
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.
CS 536 Spring Intermediate Code. Local Optimizations. Lecture 22.
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
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.
Code Generation Professor Yihjia Tsai Tamkang University.
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
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.
CS 412/413 Spring 2007Introduction to Compilers1 Lecture 29: Control Flow Analysis 9 Apr 07 CS412/413 Introduction to Compilers Tim Teitelbaum.
Class canceled next Tuesday. Recap: Components of IR Control dependencies: sequencing of operations –evaluation of if & then –side-effects of statements.
U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Emery Berger University of Massachusetts, Amherst Advanced Compilers CMPSCI 710.
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.
Common Sub-expression Elim Want to compute when an expression is available in a var Domain:
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.
PSUCS322 HM 1 Languages and Compiler Design II IR Code Optimization Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU.
Topic #10: Optimization EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Emery Berger University of Massachusetts, Amherst Advanced Compilers CMPSCI 710.
What’s in an optimizing compiler?
1 Code optimization “Code optimization refers to the techniques used by the compiler to improve the execution efficiency of the generated object code”
Program Representations. Representing programs Goals.
CS412/413 Introduction to Compilers Radu Rugina Lecture 18: Control Flow Graphs 29 Feb 02.
1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis.
CS412/413 Introduction to Compilers and Translators April 2, 1999 Lecture 24: Introduction to Optimization.
Optimization Simone Campanoni
©SoftMoore ConsultingSlide 1 Code Optimization. ©SoftMoore ConsultingSlide 2 Code Optimization Code generation techniques and transformations that result.
Credible Compilation With Pointers Martin Rinard and Darko Marinov Laboratory for Computer Science Massachusetts Institute of Technology.
Code Optimization More Optimization Techniques. More Optimization Techniques  Loop optimization  Code motion  Strength reduction for induction variables.
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.
Simone Campanoni Dependences Simone Campanoni
Simone Campanoni Loop transformations Simone Campanoni
Static Single Assignment
Program Representations
Optimization Code Optimization ©SoftMoore Consulting.
Princeton University Spring 2016
Princeton University Spring 2016
CSC D70: Compiler Optimization LICM: Loop Invariant Code Motion
Fall Compiler Principles Lecture 8: Loop Optimizations
Machine-Independent Optimization
Topic 10: Dataflow Analysis
Code Generation Part III
Compiler Code Optimizations
Code Optimization Overview and Examples Control Flow Graph
Code Generation Part III
Optimizations using SSA
Fall Compiler Principles Lecture 10: Loop Optimizations
Data Flow Analysis Compiler Design
Lecture 19: Code Optimisation
CSC D70: Compiler Optimization LICM: Loop Invariant Code Motion
Live variables and copy propagation
Code Optimization.
Presentation transcript:

CS 412/413 Spring 2005Introduction to Compilers1 CS412/CS413 Introduction to Compilers Tim Teitelbaum Lecture 30: Loop Optimizations and Pointer Analysis 15 Apr 04

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

CS 412/413 Spring 2005Introduction to Compilers3 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 of the loop: t = x*x; for (i=0; i<10; i++) a[i] = 10*i + t;

CS 412/413 Spring 2005Introduction to Compilers4 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 2005Introduction to Compilers5 Algorithm INV =  Repeat for each instruction 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 2005Introduction to Compilers6 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 in 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 2005Introduction to Compilers7 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; }

CS 412/413 Spring 2005Introduction to Compilers8 Induction Variables An induction variable is a variable in a loop, whose value is a function of the loop iteration number v = f(i) In compilers, this a linear function: f(i) = c*i + d Observation: linear combinations of linear functions are linear functions –Consequence: linear combinations of induction variables are induction variables

CS 412/413 Spring 2005Introduction to Compilers9 Induction Variables Two categories of induction variables Basic induction variables: only incremented in loop body i = i + c where c is a constant (positive or negative) Derived induction variables: expressed as a linear function of an induction variable k = c*j + d where: - either j is basic induction variable - or j is derived induction variable in the family of i and: 1. No definition of j outside the loop reaches definition of k 2. i is not defined between the definitions of j and k

CS 412/413 Spring 2005Introduction to Compilers10 Families of Induction Variables Each basic induction variable defines a family of induction variables –Each variable in the family of i is a linear function of i A variable k is in the family of basic variable i if: 1. k = i ( the basic variable itself) 2. k is a linear function of other variables in the family of i: k = c*j+d, where j  Family(i) A triple denotes an induction variable k in the family of i such that: k = i*a + b –Triple for basic variable i is

CS 412/413 Spring 2005Introduction to Compilers11 Dataflow Analysis Formulation Detection of induction variables: can formulate problem using the dataflow analysis framework –Analyze loop body sub-graph, except the back edge –Analysis is similar to constant folding Dataflow information: a function F that assigns a triple to each variable: F(k) =, if k is an induction variable in family of i F(k) =  : k is not an induction variable F(k) =  : don’t know if k is an induction variable

CS 412/413 Spring 2005Introduction to Compilers12 Dataflow Analysis Formulation Meet operation: if F1 and F2 are two functions, then: if F1(k)=F2(k)= , otherwise Initialization: –Detect all basic induction variables –At loop header: F(i) = for each basic variable i Transfer function: –consider F is information before instruction I –Compute information F’ after I (F1  F2)(k) =

CS 412/413 Spring 2005Introduction to Compilers13 Dataflow Analysis Formulation For a definition k = j+c, where k is not basic induction variable F’(v) =, if v=k and F(j)= F’(v) = F(v), otherwise For a definition k = j*c, where k is not basic induction variable F’(v) =, if v=k and F(j)= F’(v) = F(v), otherwise For any other instruction and any variable k in def[I] : F’(v) = , if F(v) = F’(v) = F(v), otherwise

CS 412/413 Spring 2005Introduction to Compilers14 Strength Reduction Basic idea: replace expensive operations (multiplications) with cheaper ones (additions) in definitions of induction variables while (i<10) { j = …; // a[j] = a[j] –2; i = i+2; } Benefit: cheaper to compute s = s+6 than j = 3*i –s = s+6 requires an addition –j = 3*i requires a multiplication s = 3*i+1; while (i<10) { j = s; a[j] = a[j] –2; i = i+2; s= s+6; }

CS 412/413 Spring 2005Introduction to Compilers15 General Algorithm Algorithm: For each induction variable j with triple whose definition involves multiplication: 1. create a new variable s 2. replace definition of j with j=s 3. immediately after i=i+c, insert s = s+a*c (here a*c is constant) 4. insert s = a*i+b into preheader Correctness: this transformation maintains the invariant that s = a*i+b

CS 412/413 Spring 2005Introduction to Compilers16 Strength Reduction Gives opportunities for copy propagation, dead code elimination s = 3*i+1; while (i<10) { a[s] = a[s] –2; i = i+2; s= s+6; } s = 3*i+1; while (i<10) { j = s; a[j] = a[j] –2; i = i+2; s= s+6; }

CS 412/413 Spring 2005Introduction to Compilers17 Induction Variable Elimination Idea: eliminate each basic induction variable whose only uses are in loop test conditions and in their own definitions i = i+c - rewrite loop test to eliminate induction variable When are induction variables used only in loop tests? –Usually, after strength reduction –Use algorithm from strength reduction even if definitions of induction variables don’t involve multiplications s = 3*i+1; while (i<10) { a[s] = a[s] –2; i = i+2; s= s+6; }

CS 412/413 Spring 2005Introduction to Compilers18 Induction Variable Elimination Rewrite test condition using derived induction variables Remove definition of basic induction variables (if not used after the loop) s = 3*i+1; while (i<10) { a[s] = a[s] –2; i = i+2; s= s+6; } s = 3*i+1; while (s<31) { a[s] = a[s] –2; s= s+6; }

CS 412/413 Spring 2005Introduction to Compilers19 Induction Variable Elimination For each basic induction variable i whose only uses are –The test condition i < u –The definition of i: i = i + c Take a derived induction variable k in its family, with triple Replace test condition i < u with k < c*u+d Remove definition i = i+c if i is not live on loop exit

CS 412/413 Spring 2005Introduction to Compilers20 Where We Are Defined dataflow analysis framework Used it for several analyses –Live variables –Available expressions –Reaching definitions –Constant folding Loop transformations –Loop invariant code motion –Induction variables Next: –Pointer alias analysis

CS 412/413 Spring 2005Introduction to Compilers21 Pointer Alias Analysis Most languages use variables containing addresses –E.g. pointers (C,C++), references (Java), call-by- reference parameters (Pascal, C++, Fortran) Pointer aliases: multiple names for the same memory location, which occur when dereferencing variables that hold memory addresses Problem: –Don’t know what variables read and written by accesses via pointer aliases (e.g. *p=y, x=*p, p.f=y, x=p.f, etc.) –Need to know accessed variables to compute dataflow information after each instruction

CS 412/413 Spring 2005Introduction to Compilers22 Pointer Alias Analysis Worst case scenarios –*p = y may write any memory location –x = *p may read any memory location Such assumptions may affect the precision of other analyses Example1: Live variables before any instruction x = *p, all the variables may be live Example 2: Constant folding a = 1; b = 2;*p = 0; c = a+b; c = 3 at the end of code only if *p is not an alias for a or b! Conclusion: precision of result for all other analyses depends on the amount of alias information available - hence, it is a fundamental analysis

CS 412/413 Spring 2005Introduction to Compilers23 Alias Analysis Problem Goal: for each variable v that may hold an address, compute the set Ptr(v) of possible targets of v –Ptr(v) is a set of variables (or objects) –Ptr(v) includes stack- and heap-allocated variables (objects) Is a “may” analysis: if x  Ptr(v), then v may hold the address of x in some execution of the program No alias information: for each variable v, Ptr(v) = V, where V is the set of all variables in the program

CS 412/413 Spring 2005Introduction to Compilers24 Simple Alias Analyses Address-taken analysis: –Consider AT = set of variables whose addresses are taken –Then, Ptr(v) = AT, for each pointer variable v –Addresses of heap variables are always taken at allocation sites (e.g., x = new int[2], x=malloc(8)) –Hence AT includes all heap variables Type-based alias analysis: –If v is a pointer (or reference) to type T, then Ptr(v) is the set of all variables of type T –Example: p.f and q.f can be aliases only if p and q are references to objects of the same type –Works only for strongly-typed languages

CS 412/413 Spring 2005Introduction to Compilers25 Dataflow Alias Analysis Dataflow analysis: for each variable v, compute points- to set Ptr(v) at each program point Dataflow information: set Ptr(v) for each variable v –Can be represented as a graph G  2 V x V –Nodes = V (program variables) –There is an edge v  u if u  Ptr(v) x y z t Ptr(x) = {y} Ptr(y) = {z,t}

CS 412/413 Spring 2005Introduction to Compilers26 Dataflow Alias Analysis Dataflow Lattice: (2 V x V,  ) –V x V represents “every variable may point to every var.” –“may” analysis: top element is , meet operation is  Transfer functions: use standard dataflow transfer functions: out[I] = (in[I]-kill[I]) U gen[I] p = addr qkill[I]={p} x V gen[I]={(p,q)} p = q kill[I]={p} x V gen[I]={p} x Ptr(q) p = *q kill[I]={p} x V gen[I]={p} x Ptr(Ptr(q)) *p = q kill[I]= … gen[I]=Ptr(p) x Ptr(q) For all other instruction, kill[I] = {}, gen[I] = {} Transfer functions are monotonic, but not distributive!

CS 412/413 Spring 2005Introduction to Compilers27 Alias Analysis Example x=&a; y=&b; c=&i; if(i) x=y; *x=c; x=&a; y=&b; c=&i; if(i) x=y; *x=c; Program *x=c x=&a y=&b c=&i if(i) x=y CFG Points-to Graph (at the end of program) xa by i c

CS 412/413 Spring 2005Introduction to Compilers28 Alias Analysis Uses Once alias information is available, use it in other dataflow analyses Example: Live variable analysis Use alias information to compute use[I] and def[I] for load and store statements: x = *yuse[I] = {y}  Ptr(y) def[I]={x} *x = y use[I] = {x,y} def[I]=Ptr(x)