Compiler Design Chapter 10 - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Compiler Design Chapter 10

Description:

Two temporaries can share the same register if. they are never 'in ... may erroneously believe a variable is live, but will never erroneously believe it is dead ... – PowerPoint PPT presentation

Number of Views:55
Avg rating:3.0/5.0
Slides: 27
Provided by: jian98
Category:

less

Transcript and Presenter's Notes

Title: Compiler Design Chapter 10


1
Compiler Design - Chapter 10
Liveness Analysis
2
Liveness Analysis
  • Problem
  • IR contains an unbounded number of temporaries.
  • Actual machine has bounded number of registers.
  • Map temporaries to registers
  • Approach
  • Two temporaries can share the same register
    ifthey are never in use at the same time
  • In use gt alive or the value will still be used
    in the future
  • If not enough registers then spill some
    temporaries.
  • i.e., keep them in memory
  • Liveness Analysis determining when (the range)
    variables/registers hold values that may still be
    needed.

3
Control Flow Graph
  • Liveness analysis needs a control flow graph
    CFG
  • Each statement (or basic block) is a node
  • There is a edge from x to y if a statement x can
    be followed by statement y

4
Liveness of Variables
  • A variable is live if its current value will be
    used in the future
  • Work backward from future to past

5
Solution of Dataflow Equations
  • Gathering liveness information is a form of data
    flow analysis operating over the CFG.
  • Liveness of variables flows around the edges of
    the graph.
  • Flow-graph terminology
  • Out-edges leading to successor nodes
  • In-edges coming from predecessor nodes
  • Given node n predn, succn are two sets of
    nodes
  • Graph 10.1
  • node 5 out-edges 5?6, 5?2, succ5 2, 6,
  • node 2 in-edges 5?2, 1?2, pred2 1, 5

6
uses and defs
  • defs Variable on the left of assignment
  • Assignments define a variable, v
  • defv set of graph nodes that define v
  • defn set of variables defined by node n
  • uses Variable on the right of assignment
  • occurrences of v in expressions use it
  • usev set of nodes that use v
  • usen set of variables used in node n
  • Examples
  • Def(3) c
  • Use(3) b, c

7
Calculation of Liveness - I
  • Liveness A variable v is live on an edge e if
    there is a directed path from e to a use of v
    that does not pass through any defv.
  • v is live-in at node n if live on any of ns
    in-edges.
  • v is live-out at node n if live on any of ns
    out-edges.
  • Calculation of liveness
  • v? usen gt v live-in at n
  • v live-in at n gt v live-out at all m ? predn
  • v live-out at n and v ? defn gt v live-in at n

8
Calculation of Liveness - II
  • Define
  • inn variables live-in at n
  • outn variables live-out at n
  • Then
  • outn ? ins
  • s?succn
  • Simplest case only one successor
  • succ(n) s gt outn ins
  • Special case no successor
  • succn gt outn

9
Calculation of Liveness - III
  • Note
  • inn ? usen
  • inn ? outn - defn
  • usen and defn are constant independent of
    control flow
  • Now,
  • v ? inn iff v ? usen or v ? outn -
    defn
  • Thus,
  • inn usen ? outn - defn

10
Iterative Liveness Calculation Algorithm
  • for each n
  • inn outn
  • repeat
  • for each n
  • inn inn
  • outn outn
  • inn usen ? (outn - defn)
  • outn ? ins
  • s?succn
  • until ?n (inn inn) and
  • (outn outn)

11
Example
12
Example speed the convergence
  • The out sets are computed before the in sets
  • Following the reversed direction of the
    flow-graph arrows

13
Liveness Algorithm Notes
  • Should order computation of inner loop to follow
    the flow.
  • Liveness flows backward along control-flow edges,
    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.
  • starts at each use node
  • performs backward (following predecessor)
    depth-first search
  • stops at any definition

14
Liveness Algorithm Complexity - I
  • Complexity for input program of size N
  • N nodes in CFG
  • N variables
  • gt N elements per in/out
  • gt O(N) time per set-union
  • for loop performs constant number of set
    operations per node
  • gt O(N2) time for for loop

15
Liveness Algorithm Complexity - II
  • Each iteration of repeat loop can only add to
    each set.
  • at most every set can contain at most every
    variable
  • gt sizes of all in and out sets sum to 2N2 ,
  • bounding the number of iterations of the repeat
    loop
  • gt worst-case complexity of O(N4)
  • ordering the nodes using depth-first search can
    cut repeat loop down to 2-3 iterations
  • gt O(N) or O(N2) in practice

16
Optimality - I
  • Least fixed points
  • There is often more than one solution for a given
    dataflow problem
  • Any solution to dataflow equations is a
    conservative approximation
  • v has some later use downstream from n, gt
    v ? out(n), but not the converse

17
Optimality - II
  • A conservatively approximation of liveness is one
    that may erroneously believe a variable is live,
    but will never erroneously believe it is dead
  • just means more registers may be needed and will
    compute the right answer
  • assuming a variable is dead when it is really
    live will compute the wrong answer.
  • May be many possible solutions but want the
    smallest (least) solution the least fixed
    point.
  • Algorithm 10.4, the iterative liveness
    computation, always computes this least fixed
    point.

18
Interference Graphs
  • Liveness analysis is for register allocation
  • A condition that prevents a and b from being
    allocated to the same register is called an
    interference
  • The most common kind of interference is cause by
    overlapping live ranges when a and b are both
    live at the same program point, then they cannot
    be put in the same register.
  • Another example when a must be generated by an
    instruction that cannot address register r1, then
    a and r1 interfere

19
Interference Graphs
20
Special Treatment of MOVE
  • Consider a program with moves
  • t ? s (copy)
  • x ? s (use of s)
  • y ? t (use of t)
  • After the copy instruction, both s and t are
    alive, and they (artificially) interfere with
    each other
  • since t is defined at a point where s is live
  • However, they contain the same value!
  • so we actually do not add the interference edge

21
Interference Edges
  • At any nonmove instruction that defines a
    variable a, where the live-out variables are b1,
    , bj, add interference edges (a, b1), , (a, bj)
  • At a move instruction a ? c, where variables b1,
    , bj are live-out, add interference edges (a,
    b1), .., (a, bj) for any bj that is not the same
    as c

22
Liveness in the MiniJava Compiler
  • Analyze Assem program
  • Produce a control-flow graph
  • Liveness analysis in control-flow graph
  • Produce an interference graph
  • Two kinds of graphs

23
Graphs
24
FlowGraph class
25
AssemFlowGraph class
26
InterferenceGraph class
Write a Comment
User Comments (0)
About PowerShow.com