Docsity

Liveness Analysis - Compilers - Code Generation - Lecture Slides, Slides of Compilers

Partial preview of the text

Download Liveness Analysis - Compilers - Code Generation - Lecture Slides and more Slides Compilers in PDF only on Docsity!

COMPILERS

Liveness Analysis

Register Allocation

 IR trees are tiled to determine instructions,

but registers are not assigned.

 Can we assign registers arbitrarily?

 What if:

mov d0, mov d1, add d0,d

 was translated to:

mov eax, mov eax, add eax,eax

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 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 a b c Docsity.com

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 vdef[n] set of variables defined by node n  (^) occurrences of v in expressions use it:   use[v] = set of nodes that use vuse[n] = set of variables used in node n

Calculating Liveness 2/

 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/

 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]]

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/

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

Optimality 1/

 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/

 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.