blob: 446ffa1efadf400d4cd41b313f77570dd1de141a [file] [log] [blame]
Ruchira Sasanka299f6a92001-12-09 20:21:49 +00001 ===================
2 Register Allocation
3 ===================
4
5
61. Introduction
7===============
8
9Purpose: This file contains implementation information about register
10 allocation.
11Author : Ruchira Sasanka
12Date : Dec 8, 2001
13
14
152. Entry Point
16==============
17class PhyRegAlloc (PhyRegAlloc.h) is the main class for the register
18allocation. PhyRegAlloc::allocateRegisters() starts the register allocation
19and contains the major steps for register allocation.
20
212. Usage
22========
23Register allocation must be done as:
24
25 MethodLiveVarInfo LVI(*MethodI ); // compute LV info
26 LVI.analyze();
27
28 TargetMachine &target = .... // target description
29
30
31 PhyRegAlloc PRA(*MethodI, target, &LVI); // allocate regs
32 PRA.allocateRegisters();
33
34
354. Input and Preconditions
36==========================
Ruchira Sasanka299f6a92001-12-09 20:21:49 +000037Register allocation is done using machine instructions. The constructor
38to the class takes a pointer to a method, a target machine description and
39a live variable information for the method.
40
41The preconditions are:
42
431. Instruction selection is complete (i.e., machine instructions are
44 generated) for the method before the live variable analysis
45
462. Phi elimination is complete.
47
48
495. Assumptions
50==============
51
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000052 All variables (llvm Values) are defined before they are used. However, a
53 constant may not be defined in the machine instruction stream if it can be
54 used as an immediate value within a machine instruction. However, register
55 allocation does not have to worry about immediate constants since they
56 do not require registers.
57
58 Since an llvm Value has a list of uses associated, it is sufficient to
59 record only the defs in a Live Range.
60
Ruchira Sasanka299f6a92001-12-09 20:21:49 +000061
62
63
646. Overall Design
65=================
66There are sperate reigster classes - e.g., Int, Float,
67IntConditionCode, FloatConditionCode register classes for Sparc.
68
69Registerallocation consists of the following main steps:
70
71 1. Construct Live-ranges & Suggest colors (machine specific) if required
72 2. Create Interference graphs
73 3. Coalescing live ranges
74 4. Color all live ranges in each RegClass using graph coloring algo
75 5. Insert additional (machine specific) code for calls/returns/incoming args
76 6. Update instruction stream and insert spill code
77
78All the above methods are called from PhyRegAlloc::allocateRegisters().
79
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000080All steps above except step 5 and suggesting colors in step 1 are indepenedent
81of a particular target architecture. Targer independent code is availble in
82../lib/CodeGen/RegAlloc. Target specific code for Sparc is available in
83../lib/Target/Sparc.
84
Ruchira Sasanka299f6a92001-12-09 20:21:49 +000085
866.1. Construct Live-ranges & Suggest colors (machine specific) if required
87--------------------------------------------------------------------------
88Live range construction is done using machine instructions. Since there must
89be at least one definition for each variable in the machine instruction, we
90consider only definitions in creating live ranges. After live range
91construction is complete, there is a live range for all variables defined in
92the instruction stream. Note however that, live ranges are not constructed for
93constants which are not defined in the instruction stream.
94
95A LiveRange is a set of Values (only defs) in that live range. Live range
96construction is done in combination for all register classes. All the live
97ranges for a method are entered to a LiveRangeMap which can be accessed using
98any Value in the LiveRange.
99
100After live ranges have been constructed, we call machine specific code to
101suggest colors for speical live ranges. For instance, incoming args, call args,
102return values must be placed in special registers for most architectures. By
103suggesting colors for such special live ranges before we do the actual register
104allocation using graph coloring, the graph coloring can try its best to assign
105the required color for such special live ranges. This will reduce unnecessary
106copy instructions needed to move values to special registers. However, there
107is no guarantee that a live range will receive its suggested color. If the
108live range does not receive the suggested color, we have to insert explicit
109copy instructions to move the value into requred registers and its done in
110step 5 above.
111
112See LiveRange.h, LiveRangeInfo.h (and LiveRange.cpp, LiveRangeInfo.cpp) for
113algorithms and details. See SparcRegInfo.cpp for suggesting colors for
114incoming/call arguments and return values.
115
116
1176.2. Create Interference graphs
118-------------------------------
119Once live ranges are constructed, we can build interference graphs for each
120register class. Though each register class must have a seperate interference
121graph, building all interference graphs is performed in one pass. Also, the
122adjacency list for each live range is built in this phase. Consequently, each
123register class has an interference graph (which is a bit matrix) and each
124LiveRange has an adjacency list to record its neighbors. Live variable info
125is used for finding the interferences.
126
127See IGNode.h, InterferenceGraph.h (and IGNode.h, InterferenceGraph.h) for
128data structures and PhyRegAlloc::createIGNodeListsAndIGs() for the starting
129point for interference graph construction.
130
131
1326.3. Coalescing live ranges
133---------------------------
134We coalesce live ranges to reduce the number of live ranges.
135
136See LiveRangeInfo.h (and LiveRangeInfo.cpp). The entire algorithm for
137coalesing is given in LiveRangeInfo::coalesceLRs().
138
139
1406.4. Color all live ranges in each RegClass using graph coloring algo
141---------------------------------------------------------------------
142Each register class is colored seperately using the graph coloring algo. When
143assigning colors, preference is given to live ranges with suggested colors
144so that if such a live range receives a color (i.e., not spilled), then
145we try to assign the color suggested for that live range. When this phase
146is complete it is possible that some live ranges do not have colors (i.e.,
147those that must be spilled).
148
149
1506.5. Insert additional (machine specific) code for calls/returns/incoming args
151------------------------------------------------------------------------------
152This code is machine specific. Currently, the code for Sparc is implemented
153in SparcRegInfo.cpp. This code is more complex because of the complex
154requirements of assigning some values to special registers. When a special
155value as an incoming argument receives a color through the graph coloring
156alogorithm, we have to make sure that it received the correct color (for
157instance the first incoming int argument must be colored to %i0 on Sparc). If
158it didn't receive the correct color, we have to insert instruction to to move
159the value to the required register. Also, this phase produces the caller
160saving code. All adition code produced is kept seperately until the last
161phase (see 6.6)
162
163
1646.6. Update instruction stream and insert spill code
165-----------------------------------------------------
166After we have assigned registers for all values and after we have produced
167additional code to be inserted before some instructions, we update the
168machine instruction stream. While we are updating the machine instruction
169stream, if an instruction referes to a spilled value, we insert spill
170instructions before/after that instruction. Also, we prepend/append additonal
171instructions that have been produced for that instruction by the register
172allocation (e.g., caller saving code)
173
174
Ruchira Sasanka42bd1772002-01-07 19:16:26 +00001757. Furture work
Ruchira Sasanka304e7072002-01-08 16:31:28 +0000176===============
Ruchira Sasanka42bd1772002-01-07 19:16:26 +0000177If it is necessary to port the register allocator to another architecture
178than Sparc, only the target specific code in ../lib/Target/Sparc needs to
179be rewritten. Methods defined in class MachineRegInfo must be provided for
180the new architecure.
181
Ruchira Sasanka304e7072002-01-08 16:31:28 +00001827.1 Using ReservedColorList in RegClass
183----------------------------------------
184The register allocator allows reserving a set of registers - i.e. the reserved
185registers are not used by the register allocator. Currently, there are no
186reserved registers. It it is necessary to make some registers unavailable to
187a particular method, this feature will become handy. To do that, the reserved
188register list must be passed to the register allocator. See PhyRegAlloc.cpp
189
190
1917.2 %g registers on Sparc
192-------------------------
193Currently, %g registers are not allocated on Sparc. If it is necessary to
194allocate these %g registers, the enumeration of registers in SparcIntRegClass
195in SparcRegClassInfo.h must be changed. %g registers can be easily added as
196volatile registers just by moving them above in the eneumeration - see
197SparcRegClassInfo.h