blob: 7237effe68dec4d2c58653bc12a9e65e748e9683 [file] [log] [blame]
Ruchira Sasanka8e604792001-09-14 21:18:34 +00001#include "llvm/CodeGen/PhyRegAlloc.h"
2
3
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00004
5
6//----------------------------------------------------------------------------
7// Constructor: Init local composite objects and create register classes.
8//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +00009PhyRegAlloc::PhyRegAlloc(const Method *const M,
10 const TargetMachine& tm,
11 MethodLiveVarInfo *const Lvi)
12 : RegClassList(),
13 Meth(M), TM(tm), LVI(Lvi), LRI(M, tm, RegClassList),
14 MRI( tm.getRegInfo() ),
15 NumOfRegClasses(MRI.getNumOfRegClasses()),
16 CallInstrList(),
Ruchira Sasankae727f852001-09-18 22:43:57 +000017 RetInstrList(),
Ruchira Sasanka8e604792001-09-14 21:18:34 +000018 AddedInstrMap()
19
20{
21 // **TODO: use an actual reserved color list
22 ReservedColorListType *RCL = new ReservedColorListType();
23
24 // create each RegisterClass and put in RegClassList
25 for( unsigned int rc=0; rc < NumOfRegClasses; rc++)
26 RegClassList.push_back( new RegClass(M, MRI.getMachineRegClass(rc), RCL) );
27
28}
29
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000030//----------------------------------------------------------------------------
31// This method initally creates interference graphs (one in each reg class)
32// and IGNodeList (one in each IG). The actual nodes will be pushed later.
33//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000034
35void PhyRegAlloc::createIGNodeListsAndIGs()
36{
Ruchira Sasanka0931a012001-09-15 19:06:58 +000037 if(DEBUG_RA ) cout << "Creating LR lists ..." << endl;
Ruchira Sasanka8e604792001-09-14 21:18:34 +000038
39 // hash map iterator
40 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
41
42 // hash map end
43 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
44
45 for( ; HMI != HMIEnd ; ++HMI ) {
46
47 LiveRange *L = (*HMI).second; // get the LiveRange
48
49 if( (*HMI).first ) {
50 // if the Value * is not null, and LR
51 // is not yet written to the IGNodeList
52 if( !(L->getUserIGNode()) ) {
53
54 RegClass *const RC = // RegClass of first value in the LR
55 //RegClassList [MRI.getRegClassIDOfValue(*(L->begin()))];
56 RegClassList[ L->getRegClass()->getID() ];
57
58 RC-> addLRToIG( L ); // add this LR to an IG
59 }
60 }
61 }
62
63 // init RegClassList
64 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
65 RegClassList[ rc ]->createInterferenceGraph();
66
67 if( DEBUG_RA)
68 cout << "LRLists Created!" << endl;
69}
70
71
72
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000073//----------------------------------------------------------------------------
74// This method will add all interferences at for a given instruction.
Ruchira Sasanka8e604792001-09-14 21:18:34 +000075// Interence occurs only if the LR of Def (Inst or Arg) is of the same reg
76// class as that of live var. The live var passed to this function is the
77// LVset AFTER the instruction
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000078//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000079
80void PhyRegAlloc::addInterference(const Value *const Def,
81 const LiveVarSet *const LVSet,
82 const bool isCallInst) {
83
84 LiveVarSet::const_iterator LIt = LVSet->begin();
85
86 // get the live range of instruction
87 const LiveRange *const LROfDef = LRI.getLiveRangeForValue( Def );
88
89 IGNode *const IGNodeOfDef = LROfDef->getUserIGNode();
90 assert( IGNodeOfDef );
91
92 RegClass *const RCOfDef = LROfDef->getRegClass();
93
94 // for each live var in live variable set
95 for( ; LIt != LVSet->end(); ++LIt) {
96
97 if( DEBUG_RA > 1) {
98 cout << "< Def="; printValue(Def);
99 cout << ", Lvar="; printValue( *LIt); cout << "> ";
100 }
101
102 // get the live range corresponding to live var
103 LiveRange *const LROfVar = LRI.getLiveRangeForValue(*LIt );
104
105 // LROfVar can be null if it is a const since a const
106 // doesn't have a dominating def - see Assumptions above
107 if( LROfVar) {
108
109 if(LROfDef == LROfVar) // do not set interf for same LR
110 continue;
111
112 // if 2 reg classes are the same set interference
113 if( RCOfDef == LROfVar->getRegClass() ){
114 RCOfDef->setInterference( LROfDef, LROfVar);
115
116 }
117
118 //the live range of this var interferes with this call
119 if( isCallInst )
120 LROfVar->addCallInterference( (const Instruction *const) Def );
121
122 }
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000123 else if(DEBUG_RA > 1) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000124 // we will not have LRs for values not explicitly allocated in the
125 // instruction stream (e.g., constants)
126 cout << " warning: no live range for " ;
127 printValue( *LIt); cout << endl; }
128
129 }
130
131}
132
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000133//----------------------------------------------------------------------------
134// This method will walk thru code and create interferences in the IG of
135// each RegClass.
136//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000137
138void PhyRegAlloc::buildInterferenceGraphs()
139{
140
141 if(DEBUG_RA) cout << "Creating interference graphs ..." << endl;
142
143 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
144
145 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
146
147 // get the iterator for machine instructions
148 const MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
149 MachineCodeForBasicBlock::const_iterator
150 MInstIterator = MIVec.begin();
151
152 // iterate over all the machine instructions in BB
153 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
154
155 const MachineInstr *const MInst = *MInstIterator;
156
157 // get the LV set after the instruction
158 const LiveVarSet *const LVSetAI =
159 LVI->getLiveVarSetAfterMInst(MInst, *BBI);
160
161 const bool isCallInst = TM.getInstrInfo().isCall(MInst->getOpCode());
162
163 // iterate over MI operands to find defs
164 for( MachineInstr::val_op_const_iterator OpI(MInst);!OpI.done(); ++OpI) {
165
166 if( OpI.isDef() ) {
167 // create a new LR iff this operand is a def
168 addInterference(*OpI, LVSetAI, isCallInst );
169
170 } //if this is a def
171
172 } // for all operands
173
174 } // for all machine instructions in BB
175
176
177 // go thru LLVM instructions in the basic block and record all CALL
Ruchira Sasankae727f852001-09-18 22:43:57 +0000178 // instructions and Return instructions in the CallInstrList
179 // This is done because since there are no reverse pointers in machine
180 // instructions to find the llvm instruction, when we encounter a call
181 // or a return whose args must be specailly colored (e.g., %o's for args)
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000182 BasicBlock::const_iterator InstIt = (*BBI)->begin();
183
184 for( ; InstIt != (*BBI)->end() ; ++ InstIt) {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000185 unsigned OpCode = (*InstIt)->getOpcode();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000186
Ruchira Sasankae727f852001-09-18 22:43:57 +0000187 if( OpCode == Instruction::Call )
188 CallInstrList.push_back( *InstIt );
189
190 else if( OpCode == Instruction::Ret )
191 RetInstrList.push_back( *InstIt );
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000192 }
193
194 } // for all BBs in method
195
196
197 // add interferences for method arguments. Since there are no explict
198 // defs in method for args, we have to add them manually
199
200 addInterferencesForArgs(); // add interference for method args
201
202 if( DEBUG_RA)
203 cout << "Interference graphs calculted!" << endl;
204
205}
206
207
208
209
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000210//----------------------------------------------------------------------------
211// This method will add interferences for incoming arguments to a method.
212//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000213void PhyRegAlloc::addInterferencesForArgs()
214{
215 // get the InSet of root BB
216 const LiveVarSet *const InSet = LVI->getInSetOfBB( Meth->front() );
217
218 // get the argument list
219 const Method::ArgumentListType& ArgList = Meth->getArgumentList();
220
221 // get an iterator to arg list
222 Method::ArgumentListType::const_iterator ArgIt = ArgList.begin();
223
224
225 for( ; ArgIt != ArgList.end() ; ++ArgIt) { // for each argument
226 addInterference( *ArgIt, InSet, false ); // add interferences between
227 // args and LVars at start
228 if( DEBUG_RA > 1) {
229 cout << " - %% adding interference for argument ";
230 printValue( (const Value *) *ArgIt); cout << endl;
231 }
232 }
233}
234
235
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000236//----------------------------------------------------------------------------
237// This method is called after register allocation is complete to set the
238// allocated reisters in the machine code. This code will add register numbers
239// to MachineOperands that contain a Value.
240//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000241
242void PhyRegAlloc::updateMachineCode()
243{
244
245 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
246
247 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
248
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000249 // get the iterator for machine instructions
250 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
251 MachineCodeForBasicBlock::iterator MInstIterator = MIVec.begin();
252
253 // iterate over all the machine instructions in BB
254 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
255
256 MachineInstr *const MInst = *MInstIterator;
257
258 //for(MachineInstr::val_op_const_iterator OpI(MInst);!OpI.done();++OpI) {
259
260 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
261
262 MachineOperand& Op = MInst->getOperand(OpNum);
263
264 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
265 Op.getOperandType() == MachineOperand::MO_CCRegister) {
266
267 const Value *const Val = Op.getVRegValue();
268
269 // delete this condition checking later (must assert if Val is null)
270 if( !Val ) {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000271 cout << "Warning: NULL Value found for operand" << endl;
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000272 continue;
273 }
274 assert( Val && "Value is NULL");
275
276 const LiveRange *const LR = LRI.getLiveRangeForValue(Val);
277
278 if ( !LR ) {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000279
280 // nothing to worry if it's a const or a label
281
282 cout << "*NO LR for inst opcode: ";
283 cout << TargetInstrDescriptors[MInst->getOpCode()].opCodeString;
284
285 Op.setRegForValue( -1 ); // mark register as invalid
286
287 if( ((Val->getType())->isLabelType()) ||
288 (Val->getValueType() == Value::ConstantVal) )
289 ; // do nothing
290
291 // The return address is not explicitly defined within a
292 // method. So, it is not colored by usual algorithm. In that case
293 // color it here.
294
295 //else if (TM.getInstrInfo().isCall(MInst->getOpCode()))
296 //Op.setRegForValue( MRI.getCallAddressReg() );
297
298 //TM.getInstrInfo().isReturn(MInst->getOpCode())
299 else if(TM.getInstrInfo().isReturn(MInst->getOpCode()) ) {
300 cout << endl << "RETURN found" << endl;
301 Op.setRegForValue( MRI.getReturnAddressReg() );
302
303 }
304
305 else
306 {
307 cout << "!Warning: No LiveRange for: ";
308 printValue( Val); cout << " Type: " << Val->getValueType();
309 cout << " RegVal=" << Op.getAllocatedRegNum() << endl;
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000310 }
311
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000312 continue;
313 }
314
315 unsigned RCID = (LR->getRegClass())->getID();
316
317 Op.setRegForValue( MRI.getUnifiedRegNum(RCID, LR->getColor()) );
318
319 int RegNum = MRI.getUnifiedRegNum(RCID, LR->getColor());
320
Ruchira Sasankae727f852001-09-18 22:43:57 +0000321 }
322
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000323 }
324
325 }
326 }
327}
328
329
330
331
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000332//----------------------------------------------------------------------------
333// This method prints the code with registers after register allocation is
334// complete.
335//----------------------------------------------------------------------------
336void PhyRegAlloc::printMachineCode()
337{
338
339 cout << endl << ";************** Method ";
340 cout << Meth->getName() << " *****************" << endl;
341
342 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
343
344 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
345
346 cout << endl ; printLabel( *BBI); cout << ": ";
347
348 // get the iterator for machine instructions
349 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
350 MachineCodeForBasicBlock::iterator MInstIterator = MIVec.begin();
351
352 // iterate over all the machine instructions in BB
353 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
354
355 MachineInstr *const MInst = *MInstIterator;
356
357
358 cout << endl << "\t";
359 cout << TargetInstrDescriptors[MInst->getOpCode()].opCodeString;
360
361
362 //for(MachineInstr::val_op_const_iterator OpI(MInst);!OpI.done();++OpI) {
363
364 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
365
366 MachineOperand& Op = MInst->getOperand(OpNum);
367
368 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasankae727f852001-09-18 22:43:57 +0000369 Op.getOperandType() == MachineOperand::MO_CCRegister ||
370 Op.getOperandType() == MachineOperand::MO_PCRelativeDisp ) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000371
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000372
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000373
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000374 const Value *const Val = Op.getVRegValue () ;
Ruchira Sasankae727f852001-09-18 22:43:57 +0000375 // ****this code is temporary till NULL Values are fixed
376 if( ! Val ) {
377 cout << "\t<*NULL*>";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000378 continue;
379 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000380
381 // if a label or a constant
382 if( (Val->getValueType() == Value::BasicBlockVal) ||
383 (Val->getValueType() == Value::ConstantVal) ) {
384
385 cout << "\t"; printLabel( Op.getVRegValue () );
386 }
387 else {
388 // else it must be a register value
389 const int RegNum = Op.getAllocatedRegNum();
390
391 cout << "\t" << "%" << MRI.getUnifiedRegName( RegNum );
392
393 }
394
395 }
396 else if(Op.getOperandType() == MachineOperand::MO_MachineRegister) {
397 cout << "\t" << "%" << MRI.getUnifiedRegName(Op.getMachineRegNum());
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000398 }
399
400 else
401 cout << "\t" << Op; // use dump field
402 }
403
404 }
405
406 cout << endl;
407
408 }
409
410 cout << endl;
411}
412
Ruchira Sasankae727f852001-09-18 22:43:57 +0000413
414
415//----------------------------------------------------------------------------
416// Used to generate a label for a basic block
417//----------------------------------------------------------------------------
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000418void PhyRegAlloc::printLabel(const Value *const Val)
419{
420 if( Val->hasName() )
421 cout << Val->getName();
422 else
423 cout << "Label" << Val;
424}
425
426
Ruchira Sasankae727f852001-09-18 22:43:57 +0000427//----------------------------------------------------------------------------
428// The entry pont to Register Allocation
429//----------------------------------------------------------------------------
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000430
431void PhyRegAlloc::allocateRegisters()
432{
433 constructLiveRanges(); // create LR info
434
435 if( DEBUG_RA)
436 LRI.printLiveRanges();
437
438 createIGNodeListsAndIGs(); // create IGNode list and IGs
439
440 buildInterferenceGraphs(); // build IGs in all reg classes
441
442
443 if( DEBUG_RA) {
444 // print all LRs in all reg classes
445 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
446 RegClassList[ rc ]->printIGNodeList();
447
448 // print IGs in all register classes
449 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
450 RegClassList[ rc ]->printIG();
451 }
452
453 LRI.coalesceLRs(); // coalesce all live ranges
454
455 if( DEBUG_RA) {
456 // print all LRs in all reg classes
457 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
458 RegClassList[ rc ]->printIGNodeList();
459
460 // print IGs in all register classes
461 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
462 RegClassList[ rc ]->printIG();
463 }
464
Ruchira Sasankae727f852001-09-18 22:43:57 +0000465
466 // the following three calls must be made in that order since
467 // coloring or definitions must come before their uses
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000468 MRI.colorArgs(Meth, LRI); // color method args
469 // color call args of call instrns
470 MRI.colorCallArgs(CallInstrList, LRI, AddedInstrMap);
Ruchira Sasankae727f852001-09-18 22:43:57 +0000471 // color return args
472 MRI.colorRetArg(CallInstrList, LRI, AddedInstrMap);
473
474
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000475
476 // color all register classes
477 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
478 RegClassList[ rc ]->colorAllRegs();
479
480 updateMachineCode();
481 PrintMachineInstructions(Meth);
482 printMachineCode(); // only for DEBUGGING
483}
484
Ruchira Sasankae727f852001-09-18 22:43:57 +0000485
486
487