COMPILERS Liveness Analysis hussein suleman uct csc3003s 2009.

Slides:



Advertisements
Similar presentations
Register Allocation COS 320 David Walker (with thanks to Andrew Myers for many of these slides)
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.
Data-Flow Analysis II CS 671 March 13, CS 671 – Spring Data-Flow Analysis Gather conservative, approximate information about what a program.
Register Usage Keep as many values in registers as possible Register assignment Register allocation Popular techniques – Local vs. global – Graph coloring.
P3 / 2004 Register Allocation. Kostis Sagonas 2 Spring 2004 Outline What is register allocation Webs Interference Graphs Graph coloring Spilling Live-Range.
Lecture 11: Code Optimization CS 540 George Mason University.
Register Allocation CS 320 David Walker (with thanks to Andrew Myers for most of the content of these slides)
COMPILERS Register Allocation hussein suleman uct csc305w 2004.
COMPILERS Register Allocation hussein suleman uct csc3005h 2006.
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis – today’s class –Classic analyses.
Data-Flow Analysis Framework Domain – What kind of solution is the analysis looking for? Ex. Variables have not yet been defined – Algorithm assigns a.
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.
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 Data flow analysis Goal : collect information about how a procedure manipulates its data This information is used in various optimizations For example,
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:
Representing programs Goals. Representing programs Primary goals –analysis is easy and effective just a few cases to handle directly link related things.
Cse322, Programming Languages and Compilers 1 6/14/2015 Lecture #13, May 15, 2007 Control flow graphs, Liveness using data flow, dataflow equations, Using.
More Dataflow Analysis CS153: Compilers Greg Morrisett.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
Register Allocation (Slides from Andrew Myers). Main idea Want to replace temporary variables with some fixed set of registers First: need to know which.
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
Liveness Analysis Mooly Sagiv Schrierber Wed 10:00-12:00 html://
Code Generation Professor Yihjia Tsai Tamkang University.
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.
CS 412/413 Spring 2007Introduction to Compilers1 Lecture 29: Control Flow Analysis 9 Apr 07 CS412/413 Introduction to Compilers Tim Teitelbaum.
Data Flow Analysis Compiler Design Nov. 8, 2005.
Prof. Fateman CS 164 Lecture 221 Global Optimization Lecture 22.
1 Liveness analysis and Register Allocation Cheng-Chia Chen.
Improving Code Generation Honors Compilers April 16 th 2002.
Recap from last time: live variables x := 5 y := x + 2 x := x + 1 y := x y...
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
Data Flow Analysis Compiler Design Nov. 8, 2005.
Direction of analysis Although constraints are not directional, flow functions are All flow functions we have seen so far are in the forward direction.
Precision Going back to constant prop, in what cases would we lose precision?
Data-Flow Analysis. Approaches Static Analysis Inspections Dependence analysis Symbolic execution Software Verification Data flow analysis Concurrency.
Code Optimization, Part III Global Methods Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412.
Data Flow Analysis Compiler Baojian Hua
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.
Register Allocation Harry Xu CS 142 (b) 02/11/2013.
Register Usage Keep as many values in registers as possible Keep as many values in registers as possible Register assignment Register assignment Register.
2/22/2016© Hal Perkins & UW CSEP-1 CSE P 501 – Compilers Register Allocation Hal Perkins Winter 2008.
1 Liveness analysis and Register Allocation Cheng-Chia Chen.
1 Code Optimization Chapter 9 (1 st ed. Ch.10) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
Optimization Simone Campanoni
Dataflow Analysis CS What I s Dataflow Analysis? Static analysis reasoning about flow of data in program Different kinds of data: constants, variables,
DFA foundations Simone Campanoni
Single Static Assignment Intermediate Representation (or SSA IR) Many examples and pictures taken from Wikipedia.
Code Optimization.
Data Flow Analysis Suman Jana
Iterative Dataflow Problems
Optimizing Compilers Background
Register Allocation Noam Rinetzky Text book:
Topic 10: Dataflow Analysis
University Of Virginia
Liveness Analysis and Register Allocation
Data Flow Analysis Compiler Design
Static Single Assignment
Compiler Construction
Taken largely from University of Delaware Compiler Notes
Lecture 17: Register Allocation via Graph Colouring
Code Generation Part II
Live variables and copy propagation
COMPILERS Liveness Analysis
CSE P 501 – Compilers SSA Hal Perkins Autumn /31/2019
CS 201 Compiler Construction
Presentation transcript:

COMPILERS Liveness Analysis hussein suleman uct csc3003s 2009

Register Allocation  IR trees are tiled to determine instructions, but registers are not assigned.  Can we assign registers arbitrarily?  What if: mov d0,24 mov d1,36 add d0,d1  was translated to: mov eax,24 mov eax,36 add eax,eax

Allocation Issues  Issues: Registers already have previous values when they are used. But there are a limited number of registers so we have to reuse! Use of particular registers affects which instructions to choose. Register vs. memory use affects the number and nature of LOAD/STORE instructions.  Optimal allocation of registers is difficult. NP-complete for k >1 registers

Liveness Analysis  Problem: IR contains an unbounded number of temporaries. Actual machine has bounded number of registers.  Approach: Temporaries with disjoint live ranges (where their values are needed) can map to same register. If not enough registers then spill some temporaries.  [i.e., keep them in memory]  Liveness Analysis = determining when variables/registers hold values that may still be needed.

Control Flow Analysis  Before performing liveness analysis, we need to understand the control flow by building a control flow graph [CFG]: Nodes may be individual program statements or basic blocks. Edges represent potential flow of control.  Out-edges from node n lead to successor nodes, succ[n].  In-edges to node n come from predecessor nodes, pred[n].

Control Flow Example 1/2  Sample Program a = 0 L1: b = a + 1 c = c + b a = b * 2 if a < N goto L1 return c

Control Flow Example 2/2 a = 0 b = a + 1 c = c + b a = b * 2 a < N return c a = 0 b = a + 1 c = c + b a = b * 2 a < N return c a = 0 b = a + 1 c = c + b a = b * 2 a < N return c abc

def and use  Gathering liveness information is a form of data flow analysis operating over the CFG.  Liveness of variables “flows” around the edges of the graph. assignments define a variable, v:  def[v] set of graph nodes that define v  def[n] set of variables defined by node n occurrences of v in expressions use it:  use[v] = set of nodes that use v  use[n] = set of variables used in node n

Calculating Liveness 1/3  v is live on edge e if there is a directed path from e to a use of v that does not pass through any def[v].  v is live-in at node n if live on any of n’s in-edges.  v is live-out at n if live on any of n’s out- edges.  v use[n] => v live-in at n  v live-in at n => v live-out at all m  pred[n]  v live-out at n,v  def[n] => v live-in at n

Calculating Liveness 2/3  Define: in[n]: variables live-in at n out[n]: variables live-out at n  Then: out[n] =  in[s] ssucc[n] for a single node successor,  succ[n] = {} => out[n] = {}

Calculating Liveness 3/3  Note: in[n]  use[n] in[n]  out[n] - def[n]  use[n] and def[n] are constant [independent of control flow]  Now,  v  in[n] iff v  use[n] or v  out[n] - def[n]  Thus, in[n] = use[n]  [out[n] - def[n]]

Iterative Liveness Calculation foreach n {in[n] = {}; out[n] = {}} repeat foreach n in’[n] = in[n]; out’[n] = out[n]; in[n] = use[n]  (out[n] - def[n]) out[n] =  in[s] s  succ[n] until  n (in’[n] = in[n]) & (out’[n] = out[n])

Liveness Algorithm Notes  Should order computation of inner loop to follow the “flow”. Liveness flows backward along control-flow arcs, from out to in.  Nodes can just as easily be basic blocks to reduce CFG size.  Could do one variable at a time, from uses back to defs, noting liveness along the way.

Liveness Algorithm Complexity 1/2 Complexity: for input program of size N ≤ N nodes in CFG => < N variables => N elements per in/out => O(N) time per set-union for loop performs constant number of set operations per node => O(N 2 ) time for for loop

Liveness Algorithm Complexity 2/2 Each iteration of repeat loop can only add to each set. Sets can contain at most every variable => sizes of all in and out sets sum to 2N 2, bounding the number of iterations of the repeat loop => worst-case complexity of O(N 4 ) ordering can cut repeat loop down to 2-3 iterations => O(N) or O(N 2 ) in practice

Optimality 1/2  Least fixed points There is often more than one solution for a given dataflow problem (see example in text). Any solution to dataflow equations is a conservative approximation:  v has some later use downstream from n,  => v  out(n)  but not the converse  What is the implication of a non-least- fixed-point?

Optimality 2/2  Conservatively assuming a variable is live does not break the program; just means more registers may be needed.  Assuming a variable is dead when it is really live will break things.  May be many possible solutions but want the “smallest”: the least fixed point.  The iterative liveness computation computes this least fixed point.