Download presentation
Presentation is loading. Please wait.
1
Global Register Allocation Based on
Graph Fusion Lueh G., Gross T., Tabatabai A.
2
Introduction Register Allocation – which of the values that might profitably be in a machine's registers should be in registers. Candidates for register allocation – user variables constants compiler generated constants
3
Introduction Technique used –
Assign a virtual register to each register allocation candidate. Determine the live range for each virtual register. (live range : the set of basic blocks a variable is live in.) Map virtual registers to physical registers. Insert spill and/or shuffle code, as necessary.
4
The Register Allocation Problem
can be modeled as graph coloring problem. graph coloring problem : color the nodes of an arbitrary graph using r-colors such that no two adjacent nodes are assigned the same color. Interference Graph : nodes – live ranges of virtual registers. edges – two live ranges are connected by an edge if they are live simultaneously.
5
Graph coloring - Finding whether a graph is N-colorable is NP complete. Assign colors to live ranges in some heuristic order. If coloring blocks, modify the interference graph to allow coloring to proceed. spilling : a live range for virtual register x is assigned a location in memory. splitting : a long live range is segmented into smaller live ranges.
6
Overview of the fusion based technique
Region-based approach, region may be – basic block loop nest super block any other combination of basic blocks Regions are connected via control flow edges, that are prioritized. Key Idea – Incrementally build up the interference graph. Fusion operator – interference graph of regions are fused to build up the complete interference graph.
7
Some Notations and Terms-
lr(x) – live range of a virtual register x. lri(x) – segment of lr(x) that spans region i. Unconstrained node – a node lr with degree less than N (the number of physical registers). ReachIn(B) – the set of live ranges lr(x) for region B s.t. x is not defined prior to use in B and so that definition of x reaches the entry of B. LiveOut(B) – the set of live ranges that are live at the exit of B.
8
The Fusion based approach -
Four phases – region formation graph simplification graph merging register assignment Region Formation – regions are formed & interference graph GR is built for each region R. control flow edges that connect regions are ordered.
9
Graph Simplification –
objective is to determine the number of live ranges that must be spilled for each region. if GR can be simplified, no spill code is necessary within region R. otherwise, some live ranges have to be spilled. which live ranges to spill ? those that are transparent within R – beneficial from R’s perspective from global perspective, can’t be determined at this point the decision as to which live ranges to spill is delayed – delayed spilling
10
live range splitting decisions are made by the fusion operator.
Graph Merging – fuses interference graphs along each edge in the order determined earlier. based on fusion operator that maintains the invariant that resulting graph is simplifiable. live range splitting decisions are made by the fusion operator. At the end of merging phase – we have one simplifiable graph. we know how many live ranges to spill for each region. we know where to place the shuffle code. no physical registers are committed to any live range.
11
As more graphs are fused, delayed spilling gradually spills live ranges.
splitting is combined with delayed spilling. Register Assignment – physical registers are assigned to live ranges. shuffle code is generated. shuffle code inserted at an edge E = <B1,B2> for those live ranges that extend across E but have different storage locations in B1 and B2. Shuffle code is either a load, store or register to register move.
12
Properties of Graph Fusion
GR is sparser than the function wide graph G. As fusion proceeds, denser graphs are formed. The earlier an edge is considered, the less likely it is that live ranges are spilt along that edge. shuffle code ends up on less frequently executed edges. simplifiability invariant is maintained. No register binding decisions are taken
13
An Example -
14
Fusion Operation fuses two interference graphs into one and maintains the simplifiability invariant. steps of fusion operation – finding live ranges that span edge E unioning non-splitable live ranges merging cliques taking snap-shot of interference graph unioning splitable live ranges maintaining simplifiability
15
Fusion Operation fusion operation combines live ranges.
A live range can be in one of three states : it has been spilled it is represented in GRi and transparent it is represented in GRi and non-transparent If both segments, “from” and “to”, are spilled, no shuffle code is needed.
16
Fusion Operation
17
Fusion Operation Finding Spanning-E Live Ranges –
ReachIn(B2) and LiveOut(B1) indicate which live ranges span edge E = <B1, B2> if lr1(x) Є LiveOut(B1) and lr2(x) Є ReachIn(B2), one definition of lr2(x) comes from lr1(x) coalesce lr1(x) and lr2(x), so that no shuffle code is needed for x along E.
18
Fusion Operation Unioning Non-splitable live ranges –
coalesce the live ranges whose combined live range is not splitable. such live ranges are not considered further combining two live ranges “from” and “to” consists of three steps : unioning two live ranges merging interference edges propagating attributes
19
Fusion Operation Merging Cliques –
If a region R needs M physical registers to be colored & M>N, some live ranges must be spilled. Let, T = transparent live ranges in R, K = no. of live ranges to be spilled. Then if : K < T : delay choosing the live ranges to spill (delayed spilling). K = T : spill all transparent live ranges K > T : all transparent live ranges as well as K – T non-transparent live ranges are spilled. spilling transparent live ranges is not always optimal
20
Example
21
The delayed spilling technique
used to handle the case where K < T transparent live ranges form a clique in the interference graph transparent live ranges are collected into a single clique summary node C. node C contains an edge to all other nodes in the graph. transparent live ranges interfere with all other live ranges in a region. C is annotated with T(C)
22
Delayed spilling record that ψ(C) = K of the T transparent live ranges must be spilled. but do not specify which ones ! actual size of clique is thus T(C) – ψ(C) clique is dealt with as a single unit by keeping a summary node, we can keep more live ranges than there are registers. this makes possible to delay the decision as to which live ranges to spill Affect of clique representation on graph coloring – computing degree fusing graphs
23
Delayed Spilling computing degree :
Consider a clique node C and a regular node lr, connected by E. Degree contributed by E to – lr = T(C) – ψ(C), as lr interferes with every node inside C clique C = 1 degree of clique node = interference edges of C + (T(C) – ψ(C) – 1), as within C each node interferes with T(C) – ψ(C) – 1 other nodes.
24
Delayed Spilling merging live ranges :
live ranges that span across a given edge E=<B1,B2> are merged. when lr1 is merged with lr2 and at least one is transparent, there are three cases – lr1 is spilled & lr2 is in clique : lr2 removed from clique & spilled decrement T(C) and ψ(C) by one lr1 non-transparent & lr2 in clique : make non-spilling decision for lr2 & remove lr2 from C decrement T(C)
25
Delayed Spilling lr1 in clique C1 & lr2 in clique C2:
coalesce lr1 and lr2 and add to a new clique C = C1 ∩ C2 . after processing all live ranges across E, we have three cliques : C, C1’ = C1 – C and C2’ = C2 – C. ψ(C) determines the no. of ranges of C to spill & is a function of ψ(C1) and ψ(C2). compute ψ(C1’) = ψ(C1) - ψ(C) & ψ(C2’) = ψ(C2) - ψ(C) ensure that ψ(C) <= T(C) always holds, so there are four ways to set ψ(C)
26
Delayed Spilling If T(C) = ψ(C) at any time, then all live ranges in C must be spilled. At that point, C is an empty clique and is removed from the graph
27
Delayed Spilling new generated interference edge is added as well
a copy of interference edges of C is generated new generated interference edge is added as well
28
Example of Delayed Spilling
29
Fusion Operation Taking a snapshot of the interference graph –
combined live ranges may have to be split to undo combining, original graph must be retained take a snap-shot of current graph before combining splitable live ranges. Unioning splitable live ranges – combine two live ranges into one as before merge interference edges without suppressing duplicate edges defer deletion of duplicate edges and propagation of attributes until simplifiability invariant is satisfied.
30
Fusion Operation Maintaining the simplifiability invariant –
compute degree remove nodes with degree < N lower register pressure find a transparent live range to spill instead of splitting live ranges if there is no transparent live range, split a coalesced (splitable) live range commit live range unioning decision all necessary spilling and splitting decisions have been made attributes of live ranges are propagated duplicate edges eliminated
31
Placement of Shuffle Code
after color assignment shuffle code is inserted for x that has been split along E = <B1, B2> if lr1(x) and lr2(x) are not assigned same location. three types of shuffle code shuffle move shuffle store shuffle load simple rule for inserting shuffle store store x when lr1(x) is in a register & lr2(x) is spilled. but x’s memory location may already contain up-to-date value.
32
Placement of Shuffle Code
fusion operation propagates an attribute (has_def), indicating if value of a live range has been defined within the live range if has_def is set, then shuffle stores needed at edges that exit to a spilled live range if value of lr(x) comes from a shuffle move & value in memory is not up-to-date, shuffle stores required.
33
Placement of Shuffle Code
34
Ordering of Shuffle Code
ordering in which to insert shuffle code : shuffle stores shuffle moves ordering among shuffle moves ? prevent source value of a move being targeted by another move. shuffle loads optimization of placement : insertion of shuffle code may lead to partial redundancies
35
Evaluation
36
Evaluation
37
Evaluation
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.