Recap from last time Saw several examples of optimizations –Constant folding –Constant Prop –Copy Prop –Common Sub-expression Elim –Partial Redundancy.

Slides:



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

SSA and CPS CS153: Compilers Greg Morrisett. Monadic Form vs CFGs Consider CFG available exp. analysis: statement gen's kill's x:=v 1 p v 2 x:=v 1 p v.
Lecture 11: Code Optimization CS 540 George Mason University.
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.
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-
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.
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 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 We saw various different issues related to program analysis and program transformations You were not expected to know all of these.
Feedback: Keep, Quit, Start
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.
Previous finals up on the web page use them as practice problems look at them early.
X := 11; if (x == 11) { DoSomething(); } else { DoSomethingElse(); x := x + 1; } y := x; // value of y? Phase ordering problem Optimizations can interact.
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:
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.
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.
Common Sub-expression Elim Want to compute when an expression is available in a var Domain:
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.
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.
Program Analysis Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
Composing Dataflow Analyses and Transformations Sorin Lerner (University of Washington) David Grove (IBM T.J. Watson) Craig Chambers (University of Washington)
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?
Topic #10: Optimization EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
1 Code optimization “Code optimization refers to the techniques used by the compiler to improve the execution efficiency of the generated object code”
Compiler Principles Fall Compiler Principles Lecture 0: Local Optimizations Roman Manevich Ben-Gurion University.
Compiler Optimizations ECE 454 Computer Systems Programming Topics: The Role of the Compiler Common Compiler (Automatic) Code Optimizations Cristiana Amza.
Program Representations. Representing programs Goals.
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.
CS412/413 Introduction to Compilers and Translators April 2, 1999 Lecture 24: Introduction to Optimization.
Optimization Simone Campanoni
Code Optimization Overview and Examples
Code Optimization.
Dataflow analysis.
Program Representations
Optimization Code Optimization ©SoftMoore Consulting.
Fall Compiler Principles Lecture 8: Loop Optimizations
Machine-Independent Optimization
Topic 10: Dataflow Analysis
Dataflow analysis.
Code Optimization Overview and Examples Control Flow Graph
Fall Compiler Principles Lecture 10: Loop Optimizations
Data Flow Analysis Compiler Design
Tour of common optimizations
Tour of common optimizations
Tour of common optimizations
Presentation transcript:

Recap from last time Saw several examples of optimizations –Constant folding –Constant Prop –Copy Prop –Common Sub-expression Elim –Partial Redundancy Elim Saw that a naïve CSE can undo Copy Prop

Another example x := y**z... x :=...

Another example Often used as a clean-up pass x := y**z... x :=... x := y z := z + x x := y z := z + y Copy propDAE x := y z := z + y

Another example if (false) {... }

Another example if (false) {... }

Another example In Java: a = new int [10]; for (index = 0; index < 10; index ++) { a[index] = 100; }

Another example In “lowered” Java: a = new int [10]; for (index = 0; index < 10; index ++) { if (index = a.length()) { throw OutOfBoundsException; } a[index] = 0; }

Another example In “lowered” Java: a = new int [10]; for (index = 0; index < 10; index ++) { if (index = a.length()) { throw OutOfBoundsException; } a[index] = 0; }

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; ???

Another example for j := 1 to N for i := 1 to M a[i] := a[i] + b[j]

Another example for j := 1 to N for i := 1 to M a[i] := a[i] + b[j]

Another example area(h,w) { return h * w } h :=...; w := 4; a := area(h,w)

Another example area(h,w) { return h * w } h :=...; w := 4; a := area(h,w)

Optimization themes Don’t compute if you don’t have to –unused assignment elimination Compute at compile-time if possible –constant folding, loop unrolling, inlining Compute it as few times as possible –CSE, PRE, PDE, loop invariant code motion Compute it as cheaply as possible –strength reduction Enable other optimizations –constant and copy prop, pointer analysis Compute it with as little code space as possible –unreachable code elimination

Dataflow analysis

Dataflow analysis: what is it? A common framework for expressing algorithms that compute information about a program Why is such a framework useful?

Dataflow analysis: what is it? A common framework for expressing algorithms that compute information about a program Why is such a framework useful? Provides a common language, which makes it easier to: –communicate your analysis to others –compare analyses –adapt techniques from one analysis to another –reuse implementations (eg: dataflow analysis frameworks)

Control Flow Graphs For now, we will use a Control Flow Graph representation of programs –each statement becomes a node –edges between nodes represent control flow Later we will see other program representations –variations on the CFG (eg CFG with basic blocks) –other graph based representations

x :=... y :=... p :=... if (...) {... x... x := y... } else {... x... x :=... *p :=... }... x y... y :=... p := x... x := y x... x :=... *p := x... y :=... if (...) Example CFG

An example DFA: reaching definitions For each use of a variable, determine what assignments could have set the value being read from the variable Information useful for: –performing constant and copy prop –detecting references to undefined variables –presenting “def/use chains” to the programmer –building other representations, like the DFG Let’s try this out on an example

1: x :=... 2: y :=... 3: y :=... 4: p := x... 5: x := y x... 6: x :=... 7: *p := x y... 8: y :=... x :=... y :=... p := x... x := y x... x :=... *p := x... y :=... if (...) Visual sugar

1: x :=... 2: y :=... 3: y :=... 4: p := x... 5: x := y x... 6: x :=... 7: *p := x y... 8: y :=...

1: x :=... 2: y :=... 3: y :=... 4: p := x... 5: x := y x... 6: x :=... 7: *p := x y... 8: y :=...

Safety When is computed info safe? Recall intended use of this info: –performing constant and copy prop –detecting references to undefined variables –presenting “def/use chains” to the programmer –building other representations, like the DFG Safety: –can have more bindings than the “true” answer, but can’t miss any

Reaching definitions generalized DFA framework geared to computing information at each program point (edge) in the CFG –So generalize problem by stating what should be computed at each program point For each program point in the CFG, compute the set of definitions (statements) that may reach that point Notion of safety remains the same