Common Sub-expression Elim Want to compute when an expression is available in a var Domain:

Slides:



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

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.
19 Classic Examples of Local and Global Code Optimizations Local Constant folding Constant combining Strength reduction.
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.
Static Single Assignment CS 540. Spring Efficient Representations for Reachability Efficiency is measured in terms of the size of the representation.
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-
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 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,
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 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.
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.
Recap from last time Saw several examples of optimizations –Constant folding –Constant Prop –Copy Prop –Common Sub-expression Elim –Partial Redundancy.
1 Data flow analysis Goal : –collect information about how a procedure manipulates its data This information is used in various optimizations –For example,
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.
1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning.
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.
Another example: constant prop Set D = 2 { x ! N | x 2 Vars Æ N 2 Z } x := N in out F x := N (in) = in – { x ! * } [ { x ! N } x := y op z in out F x :=
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:
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.
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.
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.
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.
Precision Going back to constant prop, in what cases would we lose precision?
What’s in an optimizing compiler?
Computer Science 313 – Advanced Programming Topics.
Program Representations. Representing programs Goals.
EECS 583 – Class 8 Classic Optimization University of Michigan October 3, 2011.
CS412/413 Introduction to Compilers and Translators April 2, 1999 Lecture 24: Introduction to Optimization.
CS 412/413 Spring 2005Introduction to Compilers1 CS412/CS413 Introduction to Compilers Tim Teitelbaum Lecture 30: Loop Optimizations and Pointer Analysis.
Code Optimization Overview and Examples
Dataflow analysis.
Program Representations
Basic Block Optimizations
Common Subexpression Elimination
Princeton University Spring 2016
Fall Compiler Principles Lecture 8: Loop Optimizations
Machine-Independent Optimization
Topic 10: Dataflow Analysis
Code Generation Part III
Dataflow analysis.
Code Generation Part III
Static Single Assignment Form (SSA)
EECS 583 – Class 8 Classic Optimization
Fall Compiler Principles Lecture 10: Loop Optimizations
EECS 583 – Class 9 Classic and ILP Optimization
Tour of common optimizations
Tour of common optimizations
Tour of common optimizations
Live Variables – Basic Block
Basic Block Optimizations
Presentation transcript:

Common Sub-expression Elim Want to compute when an expression is available in a var Domain:

Flow functions X := Y op Z in out F X := Y op Z (in) = X := Y in out F X := Y (in) =

Flow functions X := Y op Z in out F X := Y op Z (in) = in – { X ! * } – { * !... X... } [ { X ! Y op Z | X  Y Æ X  Z} X := Y in out F X := Y (in) = in – { X ! * } – { * !... X... } [ { X ! E | Y ! E 2 in }

Example

Problems z := j * 4 is not optimized to z := x, even though x contains the value j * 4 m := b + a is not optimized, even though a + b was already computed w := 4 * m it not optimized to w := x, even though x contains the value 4 *m

Problems: more abstractly Available expressions overly sensitive to name choices, operand orderings, renamings, assignments Use SSA: distinct values have distinct names Do copy prop before running available exprs Adopt canonical form for commutative ops

Example in SSA X := Y op Z in out F X := Y op Z (in) = X :=  (Y,Z) in 0 out F X := Y (in 0, in 1 ) = in 1

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 := Y (in 0, in 1 ) = (in 0 Å in 1 ) [ { X ! E | Y ! E 2 in 0 Æ Z ! E 2 in 1 } in 1

Example in SSA

What about pointers? Pointers complicate SSA. Several options. Option 1: don’t use SSA for pointed to variables Option 2: adapt SSA to account for pointers Option 3: define src language so that variables cannot be pointed to (eg: Java)

SSA helps us with CSE Let’s see what else SSA can help us with 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

Example

Detecting loop invariants An expression is invariant in a loop L iff: (base cases) –it’s a constant –it’s a variable use, all of whose defs are outside of L (inductive cases) –it’s a pure computation all of whose args are loop- invariant –it’s a variable use with only one reaching def, and the rhs of that def is loop-invariant

Computing loop invariants Option 1: iterative dataflow analysis –optimistically assume all expressions loop-invariant, and propagate Option 2: build def/use chains –follow chains to identify and propagate invariant expressions Option 3: SSA –like option 2, but using SSA instead of ef/use chains

Example using def/use chains An expression is invariant in a loop L iff: (base cases) –it’s a constant –it’s a variable use, all of whose defs are outside of L (inductive cases) –it’s a pure computation all of whose args are loop-invariant –it’s a variable use with only one reaching def, and the rhs of that def is loop-invariant

Example using def/use chains An expression is invariant in a loop L iff: (base cases) –it’s a constant –it’s a variable use, all of whose defs are outside of L (inductive cases) –it’s a pure computation all of whose args are loop-invariant –it’s a variable use with only one reaching def, and the rhs of that def is loop-invariant

Example using def/use chains An expression is invariant in a loop L iff: (base cases) –it’s a constant –it’s a variable use, all of whose defs are outside of L (inductive cases) –it’s a pure computation all of whose args are loop-invariant –it’s a variable use with only one reaching def, and the rhs of that def is loop-invariant

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