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.

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.
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis – today’s class –Classic analyses.
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.
Jeffrey D. Ullman Stanford University. 2  A set of nodes N and edges E is a region if: 1.There is a header h in N that dominates all nodes in N. 2.If.
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.
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.
Components of representation Control dependencies: sequencing of operations –evaluation of if & then –side-effects of statements occur in right order Data.
Program Representations. Representing programs Goals.
1 Constant Propagation for Loops with Factored Use-Def Chains Reporter : Lai, Yen-Chang.
1 CS 201 Compiler Construction Data Flow Framework.
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 |
Common Sub-expression Elim Want to compute when an expression is available in a var Domain:
Worklist algorithm Initialize all d i to the empty set Store all nodes onto a worklist while worklist is not empty: –remove node n from worklist –apply.
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.
Course project presentations No midterm project presentation Instead of classes, next week I’ll meet with each group individually, 30 mins each Two time.
Representing programs Goals. Representing programs Primary goals –analysis is easy and effective just a few cases to handle directly link related things.
Feedback: Keep, Quit, Start
Recap from last time Saw several examples of optimizations –Constant folding –Constant Prop –Copy Prop –Common Sub-expression Elim –Partial Redundancy.
Correctness. Until now We’ve seen how to define dataflow analyses How do we know our analyses are correct? We could reason about each individual analysis.
Administrative info Subscribe to the class mailing list –instructions are on the class web page, click on “Course Syllabus” –if you don’t subscribe by.
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.
From last time: reaching definitions For each use of a variable, determine what assignments could have set the value being read from the variable Information.
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; ???
From last class: CSE Want to compute when an expression is available in a var Domain:
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
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.
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.
From last lecture x := y op z in out F x := y op z (in) = in [ x ! in(y) op in(z) ] where a op b =
Direction of analysis Although constraints are not directional, flow functions are All flow functions we have seen so far are in the forward direction.
Common Sub-expression Elim Want to compute when an expression is available in a var Domain:
Projects. Dataflow analysis Dataflow analysis: what is it? A common framework for expressing algorithms that compute information about a program Why.
Recap from last time: live variables x := 5 y := x + 2 x := x + 1 y := x y...
Course project presentations Midterm project presentation Originally scheduled for Tuesday Nov 4 th Can move to Th Nov 6 th or Th Nov 13 th.
Machine-Independent Optimizations Ⅰ CS308 Compiler Theory1.
From last time: reaching definitions For each use of a variable, determine what assignments could have set the value being read from the variable Information.
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
Schedule Midterm out tomorrow, due by next Monday Final during finals week Project updates next week.
Direction of analysis Although constraints are not directional, flow functions are All flow functions we have seen so far are in the forward direction.
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.
Recap from last time We saw various different issues related to program analysis and program transformations You were not expected to know all of these.
From last class. The above is Click’s solution (PLDI 95)
Precision Going back to constant prop, in what cases would we lose precision?
Example x := read() v := a + b x := x + 1 w := x + 1 a := w v := a + b z := x + 1 t := a + b.
Generating SSA Form (mostly from Morgan). Why is SSA form useful? For many dataflow problems, SSA form enables sparse dataflow analysis that –yields the.
Program Representations. Representing programs Goals.
1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis.
Loops Simone Campanoni
CS 412/413 Spring 2005Introduction to Compilers1 CS412/CS413 Introduction to Compilers Tim Teitelbaum Lecture 30: Loop Optimizations and Pointer Analysis.
Dataflow analysis.
Program Representations
Fall Compiler Principles Lecture 8: Loop Optimizations
Topic 10: Dataflow Analysis
Another example: constant prop
Dataflow analysis.
CSC D70: Compiler Optimization Static Single Assignment (SSA)
Code Optimization Overview and Examples Control Flow Graph
Static Single Assignment Form (SSA)
Optimizations using SSA
Fall Compiler Principles Lecture 10: Loop Optimizations
EECS 583 – Class 7 Static Single Assignment Form
SSA-based Optimizations
CSC D70: Compiler Optimization Static Single Assignment (SSA)
Presentation transcript:

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 defs are outside of L (inductive cases) –it’s a pure computation all of whose args are loop- invariant –it’s a variable use whose single reaching def, and the rhs of that def is loop-invariant  functions are not pure

Example 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 defs are outside of L (inductive cases) –it’s a pure computation all of whose args are loop-invariant –it’s a variable use whose single reaching def, and the rhs of that def is loop-invariant  functions are not pure

Example using SSA and preheader An expression is invariant in a loop L iff: (base cases) –it’s a constant –it’s a variable use, all of whose single defs are outside of L (inductive cases) –it’s a pure computation all of whose args are loop-invariant –it’s a variable use whose single reaching def, and the rhs of that def is loop-invariant  functions are not pure

Summary: Loop-invariant code motion Two steps: analysis and transformations Step1: find invariant computations in loop –invariant: computes same result each time evaluated Step 2: move them outside loop –to top if used within loop: code hoisting –to bottom if used after loop: code sinking

Code motion

Example

Lesson from example: domination restriction

Domination restriction in for loops

Avoiding domination restriction

Another example

Data dependence restriction

Avoiding data restriction

Summary of Data dependencies We’ve seen SSA, a way to encode data dependencies better than just def/use chains –makes CSE easier –makes loop invariant detection easier –makes code motion easier Now we move on to looking at how to encode control dependencies

Control Dependencies A node (basic block) Y is control-dependent on another X iff X determines whether Y executes –there exists a path from X to Y s.t. every node in the path other than X and Y is post-dominated by Y –X is not post-dominated by Y

Control Dependencies A node (basic block) Y is control-dependent on another X iff X determines whether Y executes –there exists a path from X to Y s.t. every node in the path other than X and Y is post-dominated by Y –X is not post-dominated by Y

Example

Control Dependence Graph Control dependence graph: Y descendent of X iff Y is control dependent on X –label each child edge with required condition –group all children with same condition under region node Program dependence graph: super-impose dataflow graph (in SSA form or not) on top of the control dependence graph

Example

Another example

Summary of Control Depence Graph More flexible way of representing control- depencies than CFG (less constraining) Makes code motion a local transformation However, much harder to convert back to an executable form

Course summary so far Dataflow analysis –flow functions, lattice theoretic framework, optimistic iterative analysis, precision, MOP Advanced Program Representations –SSA, CDG, PDG Along the way, several analyses and opts –reaching defns, const prop & folding, available exprs & CSE, liveness & DAE, loop invariant code motion Next: dealing with procedures