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.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Data-Flow Analysis II CS 671 March 13, CS 671 – Spring Data-Flow Analysis Gather conservative, approximate information about what a program.
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.
Compilation 2011 Static Analysis Johnni Winther Michael I. Schwartzbach Aarhus 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.
Data-Flow Analysis Framework Domain – What kind of solution is the analysis looking for? Ex. Variables have not yet been defined – Algorithm assigns a.
CS412/413 Introduction to Compilers Radu Rugina Lecture 37: DU Chains and SSA Form 29 Apr 02.
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.
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 |
6/9/2015© Hal Perkins & UW CSEU-1 CSE P 501 – Compilers SSA Hal Perkins Winter 2008.
Common Sub-expression Elim Want to compute when an expression is available in a var Domain:
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
Recap from last time Saw several examples of optimizations –Constant folding –Constant Prop –Copy Prop –Common Sub-expression Elim –Partial Redundancy.
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.
From last time: live variables Set D = 2 Vars Lattice: (D, v, ?, >, t, u ) = (2 Vars, µ, ;,Vars, [, Å ) x := y op z in out F x := y op z (out) = out –
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.
4/25/08Prof. Hilfinger CS164 Lecture 371 Global Optimization Lecture 37 (From notes by R. Bodik & G. Necula)
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.
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.
Prof. Fateman CS 164 Lecture 221 Global Optimization Lecture 22.
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:
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...
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.
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)
From last class. The above is Click’s solution (PLDI 95)
Prof. Bodik CS 164 Lecture 16, Fall Global Optimization Lecture 16.
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.
Example x := read() v := a + b x := x + 1 w := x + 1 a := w v := a + b z := x + 1 t := a + b.
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.
Code Optimization Overview and Examples
Code Optimization.
Dataflow analysis.
Program Representations
Princeton University Spring 2016
Another example: constant prop
Dataflow analysis.
Code Optimization Overview and Examples Control Flow Graph
Fall Compiler Principles Lecture 10: Loop Optimizations
Data Flow Analysis Compiler Design
Advanced Compiler Design
Tour of common optimizations
Tour of common optimizations
Tour of common optimizations
Presentation transcript:

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 things We will learn more about each one of the topics we touched upon last time

Project schedule Find groups a week from today –groups of 2-3 –groups of 1 possible if you ask me Project proposal due the following Tuesday –but... how are we supposed to come up with ideas!!!!

Ideas for generating project ideas If you are currently doing research –that is related to programming languages/compilers –carve out some part of your current research, and do it for the project Think about what annoys you the most –in programming –and fix it using program analysis techniques

Ideas for generating project ideas Think about technology trends –iphone, mobile devices –multicore, parallelism –web browsers, java scripts, etc. –bluetooth, ad-hoc networks –how do these change the way we program/compile? Try to find bugs in real programs using PL techniques –search for “Dawson Engler” and look at some of his papers. Try to use similar techniques to find real bugs.

Ideas for generating project ideas How can I use information that is out there? –ask yourself: what information is out there? And how can I record it/analyze it/use it to improve programmer productivity? –For example: cvs logs, bugzilla, keystrokes, mouse movements, etc. Talk to your neighbor –in the 231 class, that is –have a discussion

Tour of common optimizations

Simple example foo(z) { x := 3 + 6; y := x – 5 return z * y }

Simple example foo(z) { x := 3 + 6; y := x – 5 return z * y }

Another example x := a + b;... y := a + b;

Another example x := a + b;... y := a + b;

Another example if (...) { x := a + b; }... y := a + b;

Another example if (...) { x := a + b; }... y := a + b;

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

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

Another example x := y... z := z + y What if we run CSE now?

Another example x := y... z := z + y What if we run CSE now?

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 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 is geared towards computing information at each program point (edge) in the CFG –So generalize the reaching definitions 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

Reaching definitions generalized Computed information at a program point is a set of var ! stmt bindings –eg: { x ! s 1, x ! s 2, y ! s 3 } How do we get the previous info we wanted? –if a var x is used in a stmt whose incoming info is in, then:

Reaching definitions generalized Computed information at a program point is a set of var ! stmt bindings –eg: { x ! s 1, x ! s 2, y ! s 3 } How do we get the previous info we wanted? –if a var x is used in a stmt whose incoming info is in, then: { s | (x ! s) 2 in } This is a common pattern –generalize the problem to define what information should be computed at each program point –use the computed information at the program points to get the original info we wanted

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 :=...

Using constraints to formalize DFA Now that we’ve gone through some examples, let’s try to precisely express the algorithms for computing dataflow information We’ll model DFA as solving a system of constraints Each node in the CFG will impose constraints relating information at predecessor and successor points Solution to constraints is result of analysis