Compiler Design Chapter 11 - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

Compiler Design Chapter 11

Description:

Suppose a and b are candidates for coalescing into node ab. ... coalescing to eliminate most moves while without extra spills (Figure 11.5, 11.6) ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 31
Provided by: jian9
Category:
Tags: ab | bloc | chapter | compiler | design | extra

less

Transcript and Presenter's Notes

Title: Compiler Design Chapter 11


1
Compiler Design - Chapter 11
Register Allocation
2
Register Allocation
  • Given interference graph, we need to allocate
    registers to all TEMPs
  • Interference graph
  • Nodes TEMPs
  • Edges TEMPS cant be allocated to the same
    register
  • Allocation is to assign each TEMP to a register
    so that no two connected TEMPs are assigned to
    the same register.
  • Then K-color the graph by assigning a maximum of
    K colors such that interfering variables always
    have different colors.

3
Build Inference Graph
  • Start with final live-in and live-out sets from
    liveness analysis. Add an edge for any
    simultaneous pairs of nodes.

out in A AB AB D D ACD ACD AC
B
A
D
C
steady state from liveness analysis
inference graph
4
Simplify
  • Given K colors and the interference graph G
  • Simplify Color graph using a simple heuristic
  • Remove a node m if it has fewer than K neighbors
    (degree lt K)
  • If G G - m can be colored then so can G,
    since nodes adjacent to m have at most K - 1
    colors
  • Push the node into a stack (This leads to
    recursive coloring algorithm)
  • Each such simplification will reduce degree of
    remaining nodes leading to more opportunity for
    simplification
  • until the graph is empty (good, no spilling!)

5
No Spilling Case
  • Every node is in the stack now.
  • Select assign colors to nodes
  • starting with the empty graph ? rebuild the
    original graph
  • Pop each node from the stack
  • Assign a color that its neighbors (that are
    already popped out) have not used.
  • Example
  • Figure 11.1 (try 4 colors, i.e. 4 registers)
  • Figure 11.2, 11.3

6
Handling Spill
  • Spill at some point during simplification the
    graph G has nodes only of significant degree
  • i.e., nodes of degree gt K
  • cant simplify anymore
  • Select a node, and mark it as potential spill,
    and push on the stack
  • there is no guarantee that it will be colorable
  • During the Select step, basically, a spill node
    is assigned to memory (and instructions will be
    inserted to handle this.)

7
Repeat
  • Start over if select has no actual spills then
    finished, otherwise
  • A potential spills neighbors may be colored with
    K different colors already ? actual spill
  • Optimistic Coloring Perhaps some of the
    neighbors are the same color, so that among them
    there are fewer than K colors ? then we can color
    the node.
  • Usually we have to rewrite program to fetch
    actual spills before each use and store after
    each definition i.e., convert a spill from a
    register temporary to a memory temporary
  • Recalculate liveness and repeat

8
Coalescing
  • Can delete a move instruction when source s and
    destination d do not interfere
  • coalesce them into a new node whose edges are the
    union of those of s and d
  • In principle, any pair of non-interfering nodes
    can be coalesced
  • unfortunately, the union is more constrained and
    new graph may no longer be K-colorable
  • overly aggressive

9
Aggressive Coalescing
10
Safe (Conservative) Coalescing
  • Apply tests for coalescing that preserve the
    colorable property.
  • Suppose a and b are candidates for coalescing
    into node ab.
  • Briggs Nodes a and b can be coalesced if the
    resulting node ab is such that among all its
    neighbors, fewer than K of them have a
    significant degree (gt K edges)
  • simplify will first remove all insignificant-degre
    e neighbors
  • ab will then be adjacent to lt K
    (significant-degree) neighbors
  • simplify can then remove ab
  • This guarantees colorable remains colorable

11
Safe (Conservative) Coalescing
  • George Nodes a and b can be coalesced if, for
    every neighbor t of a, either t already
    interferes with b, or t is of insignificant
    degree (lt K edges)
  • simplify can remove all insignificant-degree
    neighbors of a
  • remaining significant-degree neighbors of a
    already interfere with b so coalescing does not
    increase the degree of any node
  • Both Briggs and George are conservative methods
    meaning there are situations where its safe to
    coalesce but they fail to do

12
Iterated Register Coalescing - I
  • Interleave simplification with coalescing to
    eliminate most moves while without extra spills
    (Figure 11.5, 11.6)
  • Build interference graph G distinguish
    move-related from non-move-related nodes
  • Simplify remove non-move-related nodes of low
    degree one at a time
  • Coalesce conservatively coalesce move-related
    nodes
  • remove associated move instruction
  • if resulting node is non-move-related it can now
    be simplified
  • repeat simplify and coalesce until only
    significant-degree nodes or uncoalesced
    move-related nodes (which might have
    insignificant-degree)

13
Iterated Register Coalescing - II
  • Freeze if unable to simplify or coalesce
  • look for a move-related node of low-degree
    (insignificant degree) and freeze its associated
    moves (no hope of coalescing them)
  • now treat it as a non-move-related and resume
    iteration of simplify and coalesce
  • Spill if no low-degree nodes
  • select a significant-degree node for potential
    spilling
  • remove to stack and continue simplifying
  • Select pop stack assigning colors
  • Start over if select has no actual spills then
    finished, else
  • rewrite code to fetch actual spills before each
    use and store after each definition
  • recalculate liveness and repeat

14
Spilling
  • Spills require repeating build and simplify on
    the whole program.
  • To avoid increasing number of spills in future
    rounds of build can simply discard coalescences.
  • Alternatively, preserve coalescences from before
    first potential spill, discard those after that
    point.
  • if a and b are both spilled, a ? b requires a
    fetch-store sequence, t ? Mbloc Maloc ? t.
  • Spilled temporaries can be graph-colored to reuse
    activation record slots.
  • Coalescing (of spills) can be aggressive, since
    (unlike registers) there is no limit on the
    number of stack-frame locations

15
Precolored Nodes
  • Precolored nodes correspond to machine registers
    (e.g., stack pointer, argument register)
  • select and coalesce can give an ordinary
    temporary the same color as a precolored
    register, if they dont interfere
  • e.g., argument registers can be reused inside
    procedures for a temporary
  • simplify, freeze and spill cannot be performed on
    them
  • precolored nodes interfere with other precolored
    nodes
  • Treat precolored nodes as having infinite degree
    - this also avoids large adjacency lists for
    precolored nodes

16
Temporary Copies
  • Since precolored nodes dont spill, their live
    ranges must be kept short
  • use move instructions
  • move callee-save registers to fresh temporaries
    on procedure entry, and back on exit, spilling
    between as necessary, Figure 11.7
  • register pressure will spill the fresh
    temporaries as necessary, otherwise they can be
    coalesced with their precolored counterpart and
    the moves deleted

17
Caller and callee save registers
  • Variables whose live ranges span calls should go
    to callee-save registers, otherwise to
    caller-save
  • This is easy for graph coloring allocation with
    spilling
  • calls define/interfere with caller-save registers
  • a cross-call variable interferes with all
    precolored caller-save registers, as well as with
    the fresh temporaries created for callee-save
    copies, forcing a spill
  • choose nodes with high degree but few uses, to
    spill the fresh callee-save temporary instead of
    the cross-call variable
  • this makes the original callee-save register
    available for coloring the cross-call variable

18
Example
  • int f(int a, int b)
  • int d 0
  • int e a
  • do (d d b
  • e e - 1
  • ) while (e gt 0)
  • return d
  • enter
  • c r3
  • a r1
  • b r2
  • d 0
  • e a
  • loop
  • d d b
  • e e - 1
  • if e gt 0 goto loop
  • r1 d
  • r3 c
  • return r1, r3 live out

19
Example
  • Temporaries are a,b,c,d,e
  • Assume target machine with K 3 registers
  • r1, r2 (caller-save/argument/result)
  • r3 (callee-save)
  • The code generator has already made arrangements
    to save r3 explicitly by copying into temporary c
    and back again

20
Example
  • Inference graph
  • No opportunity for simplify or freeze (all
    non-precolored nodes have significant degree gt K)
  • Any attempt to coalesce will produce a new node
    adjacent to gt K significant-degree nodes

21
Example
  • Must spill based on priorities
  • Node uses defs uses defs degree
    priority
  • outside loop inside loop
  • a ( 2 10 0 ) /
    4 0.50
  • b ( 1 10 1 )
    / 4 2.75
  • c ( 2 10 0 ) /
    6 0.33
  • d ( 2 10 2 ) /
    4 5.50
  • e ( 1 10 3 )
    / 3 10.30
  • Node c has lowest priority (as it interferences
    with many other temporaries but is rarely used)
    ? so spill it

22
Example
  • Inference graph with c removed
  • Only possibility is to coalesce a and e ae will
    have fewer than K significant degree neighbors
    (after coalescing d will be low-degree, though
    high-degree before)

23
Example
  • Can now coalesce b with r2 (or coalesce ae with
    r1)
  • Coalescing ae with r1 (could coalesce d with r1)

r3
d
24
Example
  • Cannot coalesce r1ae with d because the move is
    constrained (p225) the nodes interfere. Must
    simplify d
  • Graph now has only precolored nodes, so pop nodes
    from stack coloring along the way
  • d r3
  • a,b, and e already have colors by coalescing
  • c must spill since no color can be found for it
  • Introduce new temporaries c1 and c2 for each
    use/def, add loads before each use and stores
    after each def

25
Example
  • enter
  • c1 r3
  • Mc_loc c1
  • a r1
  • b r2
  • d 0
  • e a
  • loop
  • d d b
  • e e - 1
  • if e gt 0 goto loop
  • r1 d
  • c2 Mc_loc
  • r3 c2
  • return r1, r3 live out

26
Example
  • New inference graph
  • Coalesce c1 with r3 then c2 with r3

27
Example
  • Coalesce a with e then b with r2
  • Coalesce ae with r1 and simplify d

r3c1c2
r2b
r1ae
28
Example
  • Pop d from stack select r3. All other nodes were
    coalesced or precolored. So, the coloring is
  • a r1
  • b r2
  • c r3
  • d r3
  • e r1

29
Example
  • Rewrite the program with this assignment
  • enter
  • r3 r3
  • Mc_loc r3
  • r1 r1
  • r2 r2
  • r3 0
  • r1 r1
  • loop
  • r2 r3 r2
  • r1 r1 - 1
  • if r1 gt 0 goto loop
  • r1 r3
  • r3 Mc_loc
  • r3 r3
  • return r1, r3 live out

30
Example
  • Delete moves with source and destination the same
    (coalesced)
  • enter
  • Mc_loc r3
  • r3 0
  • loop
  • r2 r3 r2
  • r1 r1 - 1
  • if r1 gt 0 goto loop
  • r1 r3
  • r3 Mc_loc
  • return r1, r3 live
    out
  • Only one uncoalesced move remains
Write a Comment
User Comments (0)
About PowerShow.com