CS 201 Compiler Construction

Slides:



Advertisements
Similar presentations
Continuing Abstract Interpretation We have seen: 1.How to compile abstract syntax trees into control-flow graphs 2.Lattices, as structures that describe.
Advertisements

School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) SSA Guo, Yao.
Data Flow Analysis. Goal: make assertions about the data usage in a program Use these assertions to determine if and when optimizations are legal Local:
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.
SSA.
Static Single Assignment CS 540. Spring Efficient Representations for Reachability Efficiency is measured in terms of the size of the representation.
CS412/413 Introduction to Compilers Radu Rugina Lecture 37: DU Chains and SSA Form 29 Apr 02.
Some Properties of SSA Mooly Sagiv. Outline Why is it called Static Single Assignment form What does it buy us? How much does it cost us? Open questions.
Components of representation Control dependencies: sequencing of operations –evaluation of if & then –side-effects of statements occur in right order Data.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Dataflow Analysis Introduction Guo, Yao Part of the slides are adapted from.
Program Representations. Representing programs Goals.
1 CS 201 Compiler Construction Lecture 7 Code Optimizations: Partial Redundancy Elimination.
Common Sub-expression Elim Want to compute when an expression is available in a var Domain:
1 CS 201 Compiler Construction Lecture 10 SSA-Based Sparse Conditional Constant Propagation.
1 CS 201 Compiler Construction Lecture 5 Code Optimizations: Copy Propagation & Elimination.
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
1 CS 201 Compiler Construction Lecture 1 Introduction.
1 CS 201 Compiler Construction Lecture 8 Code Optimizations: Partial Dead Code Elimination.
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; ???
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.
1 CS 201 Compiler Construction Lecture 6 Code Optimizations: Constant Propagation & Folding.
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.
Direction of analysis Although constraints are not directional, flow functions are All flow functions we have seen so far are in the forward direction.
Recap from last time: live variables x := 5 y := x + 2 x := x + 1 y := x y...
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.
1 CS 201 Compiler Construction Lecture 10 SSA-Based Sparse Conditional Constant Propagation.
Constant Propagation. The constant propagation framework is different from all the data-flow problems discussed so far, in that It has an unbounded set.
Precision Going back to constant prop, in what cases would we lose precision?
1 CS 201 Compiler Construction Data Flow Analysis.
Example x := read() v := a + b x := x + 1 w := x + 1 a := w v := a + b z := x + 1 t := a + b.
Machine-Independent Optimizations Ⅱ CS308 Compiler Theory1.
U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Emery Berger University of Massachusetts, Amherst Advanced Compilers CMPSCI 710.
Dataflow Analysis Topic today Data flow analysis: Section 3 of Representation and Analysis Paper (Section 3) NOTE we finished through slide 30 on Friday.
MIT Introduction to Program Analysis and Optimization Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology.
1 CS 201 Compiler Construction Introduction. 2 Instructor Information Rajiv Gupta Office: WCH Room Tel: (951) Office.
1 Data Flow Analysis Data flow analysis is used to collect information about the flow of data values across basic blocks. Dominator analysis collected.
1 Control Flow Analysis Topic today Representation and Analysis Paper (Sections 1, 2) For next class: Read Representation and Analysis Paper (Section 3)
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.
1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis.
U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT Optimizing Compilers CISC 673 Spring 2011 Yet More Data flow analysis John Cavazos.
Code Optimization More Optimization Techniques. More Optimization Techniques  Loop optimization  Code motion  Strength reduction for induction variables.
COMPILERS Liveness Analysis hussein suleman uct csc3003s 2009.
Data Flow Analysis Suman Jana
Control Flow Testing Handouts
Handouts Software Testing and Quality Assurance Theory and Practice Chapter 4 Control Flow Testing
Outline of the Chapter Basic Idea Outline of Control Flow Testing
White-Box Testing.
Efficiently Computing SSA
Dataflow Testing G. Rothermel.
Topic 10: Dataflow Analysis
University Of Virginia
White-Box Testing.
1. Reaching Definitions Definition d of variable v: a statement d that assigns a value to v. Use of variable v: reference to value of v in an expression.
Dataflow analysis.
CS 201 Compiler Construction
CS 201 Compiler Construction
Optimizations using SSA
Final Code Generation and Code Optimization
Static Single Assignment
EECS 583 – Class 4 If-conversion
SSA-based Optimizations
COMPILERS Liveness Analysis
CSE P 501 – Compilers SSA Hal Perkins Autumn /31/2019
Objectives Identify advantages (and disadvantages ?) of optimizing in SSA form Given a CFG in SSA form, perform Global Constant Propagation Dead code elimination.
Objectives Identify advantages (and disadvantages ?) of optimizing in SSA form Given a CFG in SSA form, perform Global Constant Propagation Dead code elimination.
Presentation transcript:

CS 201 Compiler Construction Code Optimizations: Constant Propagation & Folding

Constant Propagation & Folding 1. Use of var replaced by a constant value - propagation. 2. Exp. is evaluated or folded if all of its operands are constants.

Constant Propagation & Folding Evaluation of branch outcomes may be enabled. 2. Can take advantage of non-executable paths.

Problem Characteristics Not Partitionable Values of variables are interdependent. if Y > 0 then X = 2 endif Z = X + 1 Z depends upon X and Y. Not a bit-vector problem The solution is not true/false  rather can be any constant value.

Data Flow Analysis Constant Prop. Lattice for a Single variable.

Contd.. Meet Operator for a Single variable.

Contd.. Lattice for all variables (L,∧‘) VAR – set of variables VAL – set of values (all constants, Τ, ) L = {β: β is a total function β: VARVAL} i.e., set of all states of variables. forall v ε VAR β1 ∧’ β2(v) = β1 (v) ∧ β2 (v)

Contd.. Transfer Function S: A = B op C fS(β) – transfer function of S. fS(β) =

Contd.. Transfer Function S: A = c, constant assignment S: A = B, copy statement S: A < B, conditional

Example

Worklist Algorithm Each node is a single statement IN and OUT contain the lattice values at node entry and exit Algorithm will take advantage of resolved branches. How ? Mark edges as executable or non-executable and propagate information only along executable edges Branch (true) (false) (true or false) – mark edges executable accordingly

Algorithm: Initialization Mark all edges as unexecutable Worklist  { n0 } (start node in the CFG) IN[n0] = β (none of the variables is a constant) ∨ n ε N – {n0}, IN[n] = OUT[n] = β (all undefined) Τ

Algorithm: Analysis p ε Pred(n) While Worklist ≠ Φ do get n from Worklist IN[n] = Λ’ OUT[p] st p  n is executable if n is an assignment statement then n: A = B op C or A = c or A = B OUT[n] = fn ( IN[n] ) mark outgoing edges as executable else – n is a predicate evaluate condition: value is true or false or not-a-constant mark appropriate edges executable true edge or false edge or both endif if OUT[n] changed then add Succ(n) to Worklist endwhile p ε Pred(n)