blob: c2c7c64ec90d31b6969532ce06638fd5a1d57066 [file] [log] [blame]
Ruchira Sasanka8e604792001-09-14 21:18:34 +00001#include "llvm/CodeGen/PhyRegAlloc.h"
2
3
4PhyRegAlloc::PhyRegAlloc(const Method *const M,
5 const TargetMachine& tm,
6 MethodLiveVarInfo *const Lvi)
7 : RegClassList(),
8 Meth(M), TM(tm), LVI(Lvi), LRI(M, tm, RegClassList),
9 MRI( tm.getRegInfo() ),
10 NumOfRegClasses(MRI.getNumOfRegClasses()),
11 CallInstrList(),
12 AddedInstrMap()
13
14{
15 // **TODO: use an actual reserved color list
16 ReservedColorListType *RCL = new ReservedColorListType();
17
18 // create each RegisterClass and put in RegClassList
19 for( unsigned int rc=0; rc < NumOfRegClasses; rc++)
20 RegClassList.push_back( new RegClass(M, MRI.getMachineRegClass(rc), RCL) );
21
22}
23
24
25
26
27
28
29void PhyRegAlloc::createIGNodeListsAndIGs()
30{
31 cout << "Creating LR lists ..." << endl;
32
33 // hash map iterator
34 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
35
36 // hash map end
37 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
38
39 for( ; HMI != HMIEnd ; ++HMI ) {
40
41 LiveRange *L = (*HMI).second; // get the LiveRange
42
43 if( (*HMI).first ) {
44 // if the Value * is not null, and LR
45 // is not yet written to the IGNodeList
46 if( !(L->getUserIGNode()) ) {
47
48 RegClass *const RC = // RegClass of first value in the LR
49 //RegClassList [MRI.getRegClassIDOfValue(*(L->begin()))];
50 RegClassList[ L->getRegClass()->getID() ];
51
52 RC-> addLRToIG( L ); // add this LR to an IG
53 }
54 }
55 }
56
57 // init RegClassList
58 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
59 RegClassList[ rc ]->createInterferenceGraph();
60
61 if( DEBUG_RA)
62 cout << "LRLists Created!" << endl;
63}
64
65
66
67
68// Interence occurs only if the LR of Def (Inst or Arg) is of the same reg
69// class as that of live var. The live var passed to this function is the
70// LVset AFTER the instruction
71
72
73void PhyRegAlloc::addInterference(const Value *const Def,
74 const LiveVarSet *const LVSet,
75 const bool isCallInst) {
76
77 LiveVarSet::const_iterator LIt = LVSet->begin();
78
79 // get the live range of instruction
80 const LiveRange *const LROfDef = LRI.getLiveRangeForValue( Def );
81
82 IGNode *const IGNodeOfDef = LROfDef->getUserIGNode();
83 assert( IGNodeOfDef );
84
85 RegClass *const RCOfDef = LROfDef->getRegClass();
86
87 // for each live var in live variable set
88 for( ; LIt != LVSet->end(); ++LIt) {
89
90 if( DEBUG_RA > 1) {
91 cout << "< Def="; printValue(Def);
92 cout << ", Lvar="; printValue( *LIt); cout << "> ";
93 }
94
95 // get the live range corresponding to live var
96 LiveRange *const LROfVar = LRI.getLiveRangeForValue(*LIt );
97
98 // LROfVar can be null if it is a const since a const
99 // doesn't have a dominating def - see Assumptions above
100 if( LROfVar) {
101
102 if(LROfDef == LROfVar) // do not set interf for same LR
103 continue;
104
105 // if 2 reg classes are the same set interference
106 if( RCOfDef == LROfVar->getRegClass() ){
107 RCOfDef->setInterference( LROfDef, LROfVar);
108
109 }
110
111 //the live range of this var interferes with this call
112 if( isCallInst )
113 LROfVar->addCallInterference( (const Instruction *const) Def );
114
115 }
116 else if(DEBUG_RA) {
117 // we will not have LRs for values not explicitly allocated in the
118 // instruction stream (e.g., constants)
119 cout << " warning: no live range for " ;
120 printValue( *LIt); cout << endl; }
121
122 }
123
124}
125
126
127
128void PhyRegAlloc::buildInterferenceGraphs()
129{
130
131 if(DEBUG_RA) cout << "Creating interference graphs ..." << endl;
132
133 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
134
135 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
136
137 // get the iterator for machine instructions
138 const MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
139 MachineCodeForBasicBlock::const_iterator
140 MInstIterator = MIVec.begin();
141
142 // iterate over all the machine instructions in BB
143 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
144
145 const MachineInstr *const MInst = *MInstIterator;
146
147 // get the LV set after the instruction
148 const LiveVarSet *const LVSetAI =
149 LVI->getLiveVarSetAfterMInst(MInst, *BBI);
150
151 const bool isCallInst = TM.getInstrInfo().isCall(MInst->getOpCode());
152
153 // iterate over MI operands to find defs
154 for( MachineInstr::val_op_const_iterator OpI(MInst);!OpI.done(); ++OpI) {
155
156 if( OpI.isDef() ) {
157 // create a new LR iff this operand is a def
158 addInterference(*OpI, LVSetAI, isCallInst );
159
160 } //if this is a def
161
162 } // for all operands
163
164 } // for all machine instructions in BB
165
166
167 // go thru LLVM instructions in the basic block and record all CALL
168 // instructions in the CallInstrList
169 BasicBlock::const_iterator InstIt = (*BBI)->begin();
170
171 for( ; InstIt != (*BBI)->end() ; ++ InstIt) {
172
173 if( (*InstIt)->getOpcode() == Instruction::Call )
174 CallInstrList.push_back( *InstIt );
175 }
176
177 } // for all BBs in method
178
179
180 // add interferences for method arguments. Since there are no explict
181 // defs in method for args, we have to add them manually
182
183 addInterferencesForArgs(); // add interference for method args
184
185 if( DEBUG_RA)
186 cout << "Interference graphs calculted!" << endl;
187
188}
189
190
191
192
193void PhyRegAlloc::addInterferencesForArgs()
194{
195 // get the InSet of root BB
196 const LiveVarSet *const InSet = LVI->getInSetOfBB( Meth->front() );
197
198 // get the argument list
199 const Method::ArgumentListType& ArgList = Meth->getArgumentList();
200
201 // get an iterator to arg list
202 Method::ArgumentListType::const_iterator ArgIt = ArgList.begin();
203
204
205 for( ; ArgIt != ArgList.end() ; ++ArgIt) { // for each argument
206 addInterference( *ArgIt, InSet, false ); // add interferences between
207 // args and LVars at start
208 if( DEBUG_RA > 1) {
209 cout << " - %% adding interference for argument ";
210 printValue( (const Value *) *ArgIt); cout << endl;
211 }
212 }
213}
214
215
216
217void PhyRegAlloc::updateMachineCode()
218{
219
220 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
221
222 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
223
224 cout << endl << "BB "; printValue( *BBI); cout << ": ";
225
226 // get the iterator for machine instructions
227 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
228 MachineCodeForBasicBlock::iterator MInstIterator = MIVec.begin();
229
230 // iterate over all the machine instructions in BB
231 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
232
233 MachineInstr *const MInst = *MInstIterator;
234
235 cout << endl << "\t";
236 cout << TargetInstrDescriptors[MInst->getOpCode()].opCodeString;
237
238 //for(MachineInstr::val_op_const_iterator OpI(MInst);!OpI.done();++OpI) {
239
240 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
241
242 MachineOperand& Op = MInst->getOperand(OpNum);
243
244 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
245 Op.getOperandType() == MachineOperand::MO_CCRegister) {
246
247 const Value *const Val = Op.getVRegValue();
248
249 if( !Val ) {
250 cout << "\t<** Value is NULL!!!**>";
251 continue;
252 }
253 assert( Val && "Value is NULL");
254
255 const LiveRange *const LR = LRI.getLiveRangeForValue(Val);
256
257 if ( !LR ) {
258 if( ! ( (Val->getType())->isLabelType() ||
259 (Val->getValueType() == Value::ConstantVal) ) ) {
260 cout << "\t" << "<*No LiveRange for: ";
261 printValue( Val); cout << "*>";
262 }
263
264
265 //assert( LR && "No LR found for Value");
266 continue;
267 }
268
269 unsigned RCID = (LR->getRegClass())->getID();
270
271 //cout << "Setting reg for value: "; printValue( Val );
272 //cout << endl;
273
274 //Op.setRegForValue( MRI.getUnifiedRegNum(RCID, LR->getColor()) );
275
276 int RegNum = MRI.getUnifiedRegNum(RCID, LR->getColor());
277
278 cout << "\t" << "%" << MRI.getUnifiedRegName( RegNum );
279
280 }
281 else
282 cout << "\t" << Op; // use dump field
283
284 }
285
286 }
287 }
288}
289
290
291
292
293
294
295
296void PhyRegAlloc::allocateRegisters()
297{
298 constructLiveRanges(); // create LR info
299
300 if( DEBUG_RA)
301 LRI.printLiveRanges();
302
303 createIGNodeListsAndIGs(); // create IGNode list and IGs
304
305 buildInterferenceGraphs(); // build IGs in all reg classes
306
307
308 if( DEBUG_RA) {
309 // print all LRs in all reg classes
310 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
311 RegClassList[ rc ]->printIGNodeList();
312
313 // print IGs in all register classes
314 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
315 RegClassList[ rc ]->printIG();
316 }
317
318 LRI.coalesceLRs(); // coalesce all live ranges
319
320 if( DEBUG_RA) {
321 // print all LRs in all reg classes
322 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
323 RegClassList[ rc ]->printIGNodeList();
324
325 // print IGs in all register classes
326 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
327 RegClassList[ rc ]->printIG();
328 }
329
330 MRI.colorArgs(Meth, LRI); // color method args
331 MRI.colorCallArgs(CallInstrList, LRI, AddedInstrMap); // color call args of call instrns
332
333 // color all register classes
334 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
335 RegClassList[ rc ]->colorAllRegs();
336
337 updateMachineCode();
338 //PrintMachineInstructions(Meth);
339}
340
341
342
343