blob: b36e8e44437d430476c39dc58717398ecfe8e85d [file] [log] [blame]
Ruchira Sasanka8e604792001-09-14 21:18:34 +00001#include "llvm/CodeGen/PhyRegAlloc.h"
2
Chris Lattner045e7c82001-09-19 16:26:23 +00003cl::Enum<RegAllocDebugLevel_t> DEBUG_RA("dregalloc", cl::NoFlags,
4 "enable register allocation debugging information",
5 clEnumValN(RA_DEBUG_None , "n", "disable debug output"),
6 clEnumValN(RA_DEBUG_Normal , "y", "enable debug output"),
7 clEnumValN(RA_DEBUG_Verbose, "v", "enable extra debug output"), 0);
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00008
9
10//----------------------------------------------------------------------------
11// Constructor: Init local composite objects and create register classes.
12//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000013PhyRegAlloc::PhyRegAlloc(const Method *const M,
14 const TargetMachine& tm,
15 MethodLiveVarInfo *const Lvi)
16 : RegClassList(),
17 Meth(M), TM(tm), LVI(Lvi), LRI(M, tm, RegClassList),
18 MRI( tm.getRegInfo() ),
19 NumOfRegClasses(MRI.getNumOfRegClasses()),
Ruchira Sasanka8e604792001-09-14 21:18:34 +000020 AddedInstrMap()
21
22{
23 // **TODO: use an actual reserved color list
24 ReservedColorListType *RCL = new ReservedColorListType();
25
26 // create each RegisterClass and put in RegClassList
27 for( unsigned int rc=0; rc < NumOfRegClasses; rc++)
28 RegClassList.push_back( new RegClass(M, MRI.getMachineRegClass(rc), RCL) );
29
30}
31
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000032//----------------------------------------------------------------------------
33// This method initally creates interference graphs (one in each reg class)
34// and IGNodeList (one in each IG). The actual nodes will be pushed later.
35//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000036
37void PhyRegAlloc::createIGNodeListsAndIGs()
38{
Ruchira Sasankac4d4b762001-10-16 01:23:19 +000039 if(DEBUG_RA ) cout << "Creating LR lists ..." << endl;
Ruchira Sasanka8e604792001-09-14 21:18:34 +000040
41 // hash map iterator
42 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
43
44 // hash map end
45 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
46
47 for( ; HMI != HMIEnd ; ++HMI ) {
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +000048
49 if( (*HMI).first ) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +000050
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +000051 LiveRange *L = (*HMI).second; // get the LiveRange
Ruchira Sasanka8e604792001-09-14 21:18:34 +000052
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +000053 if( !L) {
54 if( DEBUG_RA) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +000055 cout << "\n*?!?Warning: Null liver range found for: ";
56 printValue( (*HMI).first) ; cout << endl;
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +000057 }
58 continue;
59 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +000060 // if the Value * is not null, and LR
61 // is not yet written to the IGNodeList
62 if( !(L->getUserIGNode()) ) {
63
64 RegClass *const RC = // RegClass of first value in the LR
65 //RegClassList [MRI.getRegClassIDOfValue(*(L->begin()))];
66 RegClassList[ L->getRegClass()->getID() ];
67
68 RC-> addLRToIG( L ); // add this LR to an IG
69 }
70 }
71 }
72
73 // init RegClassList
74 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
75 RegClassList[ rc ]->createInterferenceGraph();
76
77 if( DEBUG_RA)
Ruchira Sasankac4d4b762001-10-16 01:23:19 +000078 cout << "LRLists Created!" << endl;
Ruchira Sasanka8e604792001-09-14 21:18:34 +000079}
80
81
82
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000083//----------------------------------------------------------------------------
84// This method will add all interferences at for a given instruction.
Ruchira Sasanka8e604792001-09-14 21:18:34 +000085// Interence occurs only if the LR of Def (Inst or Arg) is of the same reg
86// class as that of live var. The live var passed to this function is the
87// LVset AFTER the instruction
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000088//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000089
90void PhyRegAlloc::addInterference(const Value *const Def,
91 const LiveVarSet *const LVSet,
92 const bool isCallInst) {
93
94 LiveVarSet::const_iterator LIt = LVSet->begin();
95
96 // get the live range of instruction
97 const LiveRange *const LROfDef = LRI.getLiveRangeForValue( Def );
98
99 IGNode *const IGNodeOfDef = LROfDef->getUserIGNode();
100 assert( IGNodeOfDef );
101
102 RegClass *const RCOfDef = LROfDef->getRegClass();
103
104 // for each live var in live variable set
105 for( ; LIt != LVSet->end(); ++LIt) {
106
107 if( DEBUG_RA > 1) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000108 cout << "< Def="; printValue(Def);
109 cout << ", Lvar="; printValue( *LIt); cout << "> ";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000110 }
111
112 // get the live range corresponding to live var
113 LiveRange *const LROfVar = LRI.getLiveRangeForValue(*LIt );
114
115 // LROfVar can be null if it is a const since a const
116 // doesn't have a dominating def - see Assumptions above
117 if( LROfVar) {
118
119 if(LROfDef == LROfVar) // do not set interf for same LR
120 continue;
121
122 // if 2 reg classes are the same set interference
123 if( RCOfDef == LROfVar->getRegClass() ){
124 RCOfDef->setInterference( LROfDef, LROfVar);
125
126 }
127
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000128 else if(DEBUG_RA > 1) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000129 // we will not have LRs for values not explicitly allocated in the
130 // instruction stream (e.g., constants)
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000131 cout << " warning: no live range for " ;
132 printValue( *LIt); cout << endl; }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000133
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000134 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000135
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000136 }
137
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000138}
139
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000140
141//----------------------------------------------------------------------------
142// For a call instruction, this method sets the CallInterference flag in
143// the LR of each variable live int the Live Variable Set live after the
144// call instruction (except the return value of the call instruction - since
145// the return value does not interfere with that call itself).
146//----------------------------------------------------------------------------
147
148void PhyRegAlloc::setCallInterferences(const MachineInstr *MInst,
149 const LiveVarSet *const LVSetAft )
150{
151 // Now find the LR of the return value of the call
152 // The last *implicit operand* is the return value of a call
153
154 // We do this because, we look at the LV set *after* the instruction
155 // to determine, which LRs must be saved across calls. The return value
156 // of the call is live in this set - but it does not interfere with call
157 // (i.e., we can allocate a volatile register to the return value)
158
159 LiveRange *RetValLR = NULL;
160
161 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
162 if( NumOfImpRefs > 0 ) {
163
164 if( MInst->implicitRefIsDefined(NumOfImpRefs-1) ) {
165
166 const Value *RetVal = MInst->getImplicitRef(NumOfImpRefs-1);
167 RetValLR = LRI.getLiveRangeForValue( RetVal );
168 assert( RetValLR && "No LR for RetValue of call");
169 }
170
171 }
172
173
174 if( DEBUG_RA)
175 cout << "\n For call inst: " << *MInst;
176
177 LiveVarSet::const_iterator LIt = LVSetAft->begin();
178
179 // for each live var in live variable set after machine inst
180 for( ; LIt != LVSetAft->end(); ++LIt) {
181
182 // get the live range corresponding to live var
183 LiveRange *const LR = LRI.getLiveRangeForValue(*LIt );
184
185 if( LR && DEBUG_RA) {
186 cout << "\n\tLR Aft Call: ";
187 LR->printSet();
188 }
189
190
191 // LR can be null if it is a const since a const
192 // doesn't have a dominating def - see Assumptions above
193 if( LR && (LR != RetValLR) ) {
194 LR->setCallInterference();
195 if( DEBUG_RA) {
196 cout << "\n ++Added call interf for LR: " ;
197 LR->printSet();
198 }
199 }
200
201 }
202
203}
204
205
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000206//----------------------------------------------------------------------------
207// This method will walk thru code and create interferences in the IG of
208// each RegClass.
209//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000210
211void PhyRegAlloc::buildInterferenceGraphs()
212{
213
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000214 if(DEBUG_RA) cout << "Creating interference graphs ..." << endl;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000215
216 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
217
218 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
219
220 // get the iterator for machine instructions
221 const MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
222 MachineCodeForBasicBlock::const_iterator
223 MInstIterator = MIVec.begin();
224
225 // iterate over all the machine instructions in BB
226 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000227
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000228 const MachineInstr *const MInst = *MInstIterator;
229
230 // get the LV set after the instruction
231 const LiveVarSet *const LVSetAI =
232 LVI->getLiveVarSetAfterMInst(MInst, *BBI);
233
234 const bool isCallInst = TM.getInstrInfo().isCall(MInst->getOpCode());
235
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000236 if( isCallInst ) {
237 //cout << "\nFor call inst: " << *MInst;
238
239 // set the isCallInterference flag of each live range wich extends
240 // accross this call instruction. This information is used by graph
241 // coloring algo to avoid allocating volatile colors to live ranges
242 // that span across calls (since they have to be saved/restored)
243 setCallInterferences( MInst, LVSetAI);
244 }
245
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000246
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000247 // iterate over MI operands to find defs
248 for( MachineInstr::val_op_const_iterator OpI(MInst);!OpI.done(); ++OpI) {
249
250 if( OpI.isDef() ) {
251 // create a new LR iff this operand is a def
252 addInterference(*OpI, LVSetAI, isCallInst );
253
254 } //if this is a def
255
256 } // for all operands
257
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000258
259 // Also add interference for any implicit definitions in a machine
260 // instr (currently, only calls have this).
261
262 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
263 if( NumOfImpRefs > 0 ) {
264 for(unsigned z=0; z < NumOfImpRefs; z++)
265 if( MInst->implicitRefIsDefined(z) )
266 addInterference( MInst->getImplicitRef(z), LVSetAI, isCallInst );
267 }
268
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000269 } // for all machine instructions in BB
270
271
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000272#if 0
273
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000274 // go thru LLVM instructions in the basic block and record all CALL
Ruchira Sasankae727f852001-09-18 22:43:57 +0000275 // instructions and Return instructions in the CallInstrList
276 // This is done because since there are no reverse pointers in machine
277 // instructions to find the llvm instruction, when we encounter a call
278 // or a return whose args must be specailly colored (e.g., %o's for args)
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000279 BasicBlock::const_iterator InstIt = (*BBI)->begin();
280
281 for( ; InstIt != (*BBI)->end() ; ++ InstIt) {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000282 unsigned OpCode = (*InstIt)->getOpcode();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000283
Ruchira Sasankae727f852001-09-18 22:43:57 +0000284 if( OpCode == Instruction::Call )
285 CallInstrList.push_back( *InstIt );
286
287 else if( OpCode == Instruction::Ret )
288 RetInstrList.push_back( *InstIt );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000289 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000290
291#endif
292
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000293
294 } // for all BBs in method
295
296
297 // add interferences for method arguments. Since there are no explict
298 // defs in method for args, we have to add them manually
299
300 addInterferencesForArgs(); // add interference for method args
301
302 if( DEBUG_RA)
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000303 cout << "Interference graphs calculted!" << endl;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000304
305}
306
307
308
309
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000310//----------------------------------------------------------------------------
311// This method will add interferences for incoming arguments to a method.
312//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000313void PhyRegAlloc::addInterferencesForArgs()
314{
315 // get the InSet of root BB
316 const LiveVarSet *const InSet = LVI->getInSetOfBB( Meth->front() );
317
318 // get the argument list
319 const Method::ArgumentListType& ArgList = Meth->getArgumentList();
320
321 // get an iterator to arg list
322 Method::ArgumentListType::const_iterator ArgIt = ArgList.begin();
323
324
325 for( ; ArgIt != ArgList.end() ; ++ArgIt) { // for each argument
326 addInterference( *ArgIt, InSet, false ); // add interferences between
327 // args and LVars at start
328 if( DEBUG_RA > 1) {
Ruchira Sasanka97b8b442001-10-18 22:36:26 +0000329 cout << " - %% adding interference for argument ";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000330 printValue( (const Value *) *ArgIt); cout << endl;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000331 }
332 }
333}
334
335
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000336
337//----------------------------------------------------------------------------
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000338// This method inserts caller saving/restoring instructons before/after
339// a call machine instruction.
340//----------------------------------------------------------------------------
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000341
342
343void PhyRegAlloc::insertCallerSavingCode(const MachineInstr *MInst,
344 const BasicBlock *BB )
345{
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000346 // assert( (TM.getInstrInfo()).isCall( MInst->getOpCode() ) );
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000347
Ruchira Sasanka47c13722001-10-16 01:33:55 +0000348 int StackOff = -8; // ****TODO : Change
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000349 hash_set<unsigned> PushedRegSet;
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000350
351 // Now find the LR of the return value of the call
352 // The last *implicit operand* is the return value of a call
353 // Insert it to to he PushedRegSet since we must not save that register
354 // and restore it after the call.
355 // We do this because, we look at the LV set *after* the instruction
356 // to determine, which LRs must be saved across calls. The return value
357 // of the call is live in this set - but we must not save/restore it.
358
359 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
360 if( NumOfImpRefs > 0 ) {
361
362 if( MInst->implicitRefIsDefined(NumOfImpRefs-1) ) {
363
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000364 const Value *RetVal = MInst->getImplicitRef(NumOfImpRefs-1);
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000365 LiveRange *RetValLR = LRI.getLiveRangeForValue( RetVal );
366 assert( RetValLR && "No LR for RetValue of call");
367
368 PushedRegSet.insert(
369 MRI.getUnifiedRegNum((RetValLR->getRegClass())->getID(),
370 RetValLR->getColor() ) );
371 }
372
373 }
374
375
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000376 const LiveVarSet *LVSetAft = LVI->getLiveVarSetAfterMInst(MInst, BB);
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000377
378 LiveVarSet::const_iterator LIt = LVSetAft->begin();
379
380 // for each live var in live variable set after machine inst
381 for( ; LIt != LVSetAft->end(); ++LIt) {
382
383 // get the live range corresponding to live var
384 LiveRange *const LR = LRI.getLiveRangeForValue(*LIt );
385
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000386 // LR can be null if it is a const since a const
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000387 // doesn't have a dominating def - see Assumptions above
388 if( LR ) {
389
390 if( LR->hasColor() ) {
391
392 unsigned RCID = (LR->getRegClass())->getID();
393 unsigned Color = LR->getColor();
394
395 if ( MRI.isRegVolatile(RCID, Color) ) {
396
397 // if the value is in both LV sets (i.e., live before and after
398 // the call machine instruction)
Ruchira Sasanka97b8b442001-10-18 22:36:26 +0000399
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000400 unsigned Reg = MRI.getUnifiedRegNum(RCID, Color);
401
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000402 if( PushedRegSet.find(Reg) == PushedRegSet.end() ) {
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000403
404 // if we haven't already pushed that register
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000405
406 unsigned RegType = MRI.getRegType( LR );
407
408 // Now get two instructions - to push on stack and pop from stack
409 // and add them to InstrnsBefore and InstrnsAfter of the
410 // call instruction
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000411
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000412 MachineInstr *AdIBef =
413 MRI.cpReg2MemMI(Reg, MRI.getFramePointer(), StackOff, RegType );
414
415 MachineInstr *AdIAft =
416 MRI.cpMem2RegMI(MRI.getFramePointer(), StackOff, Reg, RegType );
417
418 ((AddedInstrMap[MInst])->InstrnsBefore).push_front(AdIBef);
419 ((AddedInstrMap[MInst])->InstrnsAfter).push_back(AdIAft);
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000420
421 PushedRegSet.insert( Reg );
Ruchira Sasanka47c13722001-10-16 01:33:55 +0000422 StackOff -= 8; // ****TODO: Correct ??????
Ruchira Sasanka97b8b442001-10-18 22:36:26 +0000423
424 if(DEBUG_RA) {
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000425 cerr << "\nFor callee save call inst:" << *MInst;
Ruchira Sasanka97b8b442001-10-18 22:36:26 +0000426 cerr << "\n -inserted caller saving instrs:\n\t ";
427 cerr << *AdIBef << "\n\t" << *AdIAft ;
428 }
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000429 } // if not already pushed
430
431 } // if LR has a volatile color
432
433 } // if LR has color
434
435 } // if there is a LR for Var
436
437 } // for each value in the LV set after instruction
438
439}
440
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000441
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000442//----------------------------------------------------------------------------
443// This method is called after register allocation is complete to set the
444// allocated reisters in the machine code. This code will add register numbers
445// to MachineOperands that contain a Value.
446//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000447
448void PhyRegAlloc::updateMachineCode()
449{
450
451 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
452
453 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
454
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000455 // get the iterator for machine instructions
456 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
457 MachineCodeForBasicBlock::iterator MInstIterator = MIVec.begin();
458
459 // iterate over all the machine instructions in BB
460 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
461
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000462 MachineInstr *MInst = *MInstIterator;
463
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000464 // if this machine instr is call, insert caller saving code
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000465
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000466 if( (TM.getInstrInfo()).isCall( MInst->getOpCode()) )
467 insertCallerSavingCode(MInst, *BBI );
468
469 // If there are instructions to be added, *before* this machine
470 // instruction, add them now.
471
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000472 if( AddedInstrMap[ MInst ] ) {
473
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000474 deque<MachineInstr *> &IBef = (AddedInstrMap[MInst])->InstrnsBefore;
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000475
476 if( ! IBef.empty() ) {
477
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000478 deque<MachineInstr *>::iterator AdIt;
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000479
480 for( AdIt = IBef.begin(); AdIt != IBef.end() ; ++AdIt ) {
481
Ruchira Sasanka97b8b442001-10-18 22:36:26 +0000482 if( DEBUG_RA)
483 cerr << " *$* PREPENDed instr " << *AdIt << endl;
484
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000485 MInstIterator = MIVec.insert( MInstIterator, *AdIt );
486 ++MInstIterator;
487 }
488
489 }
490
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000491 }
492
493
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000494
495 //for(MachineInstr::val_op_const_iterator OpI(MInst);!OpI.done();++OpI) {
496
497 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
498
499 MachineOperand& Op = MInst->getOperand(OpNum);
500
501 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
502 Op.getOperandType() == MachineOperand::MO_CCRegister) {
503
504 const Value *const Val = Op.getVRegValue();
505
506 // delete this condition checking later (must assert if Val is null)
Chris Lattner045e7c82001-09-19 16:26:23 +0000507 if( !Val) {
508 if (DEBUG_RA)
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000509 cout << "Warning: NULL Value found for operand" << endl;
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000510 continue;
511 }
512 assert( Val && "Value is NULL");
513
514 const LiveRange *const LR = LRI.getLiveRangeForValue(Val);
515
516 if ( !LR ) {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000517
518 // nothing to worry if it's a const or a label
519
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000520 if (DEBUG_RA) {
Ruchira Sasanka1b732fd2001-10-16 16:34:44 +0000521 cout << "*NO LR for operand : " << Op ;
522 cout << " [reg:" << Op.getAllocatedRegNum() << "]";
523 cout << " in inst:\t" << *MInst << endl;
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000524 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000525
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000526 // if register is not allocated, mark register as invalid
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000527 if( Op.getAllocatedRegNum() == -1)
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000528 Op.setRegForValue( MRI.getInvalidRegNum());
Ruchira Sasankae727f852001-09-18 22:43:57 +0000529
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000530#if 0
Ruchira Sasankae727f852001-09-18 22:43:57 +0000531 if( ((Val->getType())->isLabelType()) ||
532 (Val->getValueType() == Value::ConstantVal) )
533 ; // do nothing
534
535 // The return address is not explicitly defined within a
536 // method. So, it is not colored by usual algorithm. In that case
537 // color it here.
538
539 //else if (TM.getInstrInfo().isCall(MInst->getOpCode()))
540 //Op.setRegForValue( MRI.getCallAddressReg() );
541
542 //TM.getInstrInfo().isReturn(MInst->getOpCode())
543 else if(TM.getInstrInfo().isReturn(MInst->getOpCode()) ) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000544 if (DEBUG_RA) cout << endl << "RETURN found" << endl;
Ruchira Sasankae727f852001-09-18 22:43:57 +0000545 Op.setRegForValue( MRI.getReturnAddressReg() );
546
547 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000548
549 if (Val->getValueType() == Value::InstructionVal)
Ruchira Sasankae727f852001-09-18 22:43:57 +0000550 {
Ruchira Sasanka1b732fd2001-10-16 16:34:44 +0000551 if( DEBUG_RA ) {
552 cout << "!Warning: No LiveRange for: ";
553 printValue( Val); cout << " Type: " << Val->getValueType();
554 cout << " RegVal=" << Op.getAllocatedRegNum() << endl;
555 }
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000556 }
557
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000558#endif
559
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000560 continue;
561 }
562
563 unsigned RCID = (LR->getRegClass())->getID();
564
565 Op.setRegForValue( MRI.getUnifiedRegNum(RCID, LR->getColor()) );
566
567 int RegNum = MRI.getUnifiedRegNum(RCID, LR->getColor());
568
Ruchira Sasankae727f852001-09-18 22:43:57 +0000569 }
570
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000571 } // for each operand
572
573
574 // If there are instructions to be added *after* this machine
575 // instruction, add them now
576
577 if( AddedInstrMap[ MInst ] ) {
578
579 deque<MachineInstr *> &IAft = (AddedInstrMap[MInst])->InstrnsAfter;
580
581 if( ! IAft.empty() ) {
582
583 deque<MachineInstr *>::iterator AdIt;
584
585 ++MInstIterator; // advance to the next instruction
586
587 for( AdIt = IAft.begin(); AdIt != IAft.end() ; ++AdIt ) {
588
Ruchira Sasanka97b8b442001-10-18 22:36:26 +0000589 if(DEBUG_RA)
590 cerr << " *#* APPENDed instr opcode: " << *AdIt << endl;
591
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000592 MInstIterator = MIVec.insert( MInstIterator, *AdIt );
593 ++MInstIterator;
594 }
595
596 // MInsterator already points to the next instr. Since the
597 // for loop also increments it, decrement it to point to the
598 // instruction added last
599 --MInstIterator;
600
601 }
602
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000603 }
604
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000605 } // for each machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000606 }
607}
608
609
610
611
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000612//----------------------------------------------------------------------------
613// This method prints the code with registers after register allocation is
614// complete.
615//----------------------------------------------------------------------------
616void PhyRegAlloc::printMachineCode()
617{
618
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000619 cout << endl << ";************** Method ";
620 cout << Meth->getName() << " *****************" << endl;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000621
622 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
623
624 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
625
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000626 cout << endl ; printLabel( *BBI); cout << ": ";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000627
628 // get the iterator for machine instructions
629 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
630 MachineCodeForBasicBlock::iterator MInstIterator = MIVec.begin();
631
632 // iterate over all the machine instructions in BB
633 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
634
635 MachineInstr *const MInst = *MInstIterator;
636
637
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000638 cout << endl << "\t";
639 cout << TargetInstrDescriptors[MInst->getOpCode()].opCodeString;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000640
641
642 //for(MachineInstr::val_op_const_iterator OpI(MInst);!OpI.done();++OpI) {
643
644 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
645
646 MachineOperand& Op = MInst->getOperand(OpNum);
647
648 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasanka97b8b442001-10-18 22:36:26 +0000649 Op.getOperandType() == MachineOperand::MO_CCRegister /*||
650 Op.getOperandType() == MachineOperand::MO_PCRelativeDisp*/ ) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000651
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000652 const Value *const Val = Op.getVRegValue () ;
Ruchira Sasankae727f852001-09-18 22:43:57 +0000653 // ****this code is temporary till NULL Values are fixed
654 if( ! Val ) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000655 cout << "\t<*NULL*>";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000656 continue;
657 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000658
659 // if a label or a constant
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000660 if( (Val->getValueType() == Value::BasicBlockVal) ) {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000661
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000662 cout << "\t"; printLabel( Op.getVRegValue () );
Ruchira Sasankae727f852001-09-18 22:43:57 +0000663 }
664 else {
665 // else it must be a register value
666 const int RegNum = Op.getAllocatedRegNum();
667
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +0000668 cout << "\t" << "%" << MRI.getUnifiedRegName( RegNum );
Ruchira Sasankae727f852001-09-18 22:43:57 +0000669 }
670
671 }
672 else if(Op.getOperandType() == MachineOperand::MO_MachineRegister) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000673 cout << "\t" << "%" << MRI.getUnifiedRegName(Op.getMachineRegNum());
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000674 }
675
676 else
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000677 cout << "\t" << Op; // use dump field
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000678 }
679
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000680
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000681
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000682 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
683 if( NumOfImpRefs > 0 ) {
684
685 cout << "\tImplicit:";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000686
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000687 for(unsigned z=0; z < NumOfImpRefs; z++) {
688 printValue( MInst->getImplicitRef(z) );
689 cout << "\t";
690 }
691
692 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000693
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000694 } // for all machine instructions
695
696
697 cout << endl;
698
699 } // for all BBs
700
701 cout << endl;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000702}
703
Ruchira Sasankae727f852001-09-18 22:43:57 +0000704
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000705//----------------------------------------------------------------------------
706//
707//----------------------------------------------------------------------------
708
709void PhyRegAlloc::colorCallRetArgs()
710{
711
712 CallRetInstrListType &CallRetInstList = LRI.getCallRetInstrList();
713 CallRetInstrListType::const_iterator It = CallRetInstList.begin();
714
715 for( ; It != CallRetInstList.end(); ++It ) {
716
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000717 const MachineInstr *const CRMI = *It;
718 unsigned OpCode = CRMI->getOpCode();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000719
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000720 // get the added instructions for this Call/Ret instruciton
721 AddedInstrns *AI = AddedInstrMap[ CRMI ];
722 if ( !AI ) {
723 AI = new AddedInstrns();
724 AddedInstrMap[ CRMI ] = AI;
725 }
726
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000727 if( (TM.getInstrInfo()).isCall( OpCode ) )
728 MRI.colorCallArgs( CRMI, LRI, AI );
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000729
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000730 else if ( (TM.getInstrInfo()).isReturn(OpCode) )
731 MRI.colorRetValue( CRMI, LRI, AI );
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000732
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000733 else assert( 0 && "Non Call/Ret instrn in CallRetInstrList\n" );
734
735 }
736
737}
738
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +0000739
740
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000741//----------------------------------------------------------------------------
742
743//----------------------------------------------------------------------------
744void PhyRegAlloc::colorIncomingArgs()
745{
746 const BasicBlock *const FirstBB = Meth->front();
747 const MachineInstr *FirstMI = *((FirstBB->getMachineInstrVec()).begin());
748 assert( FirstMI && "No machine instruction in entry BB");
749
750 AddedInstrns *AI = AddedInstrMap[ FirstMI ];
751 if ( !AI ) {
752 AI = new AddedInstrns();
753 AddedInstrMap[ FirstMI ] = AI;
754 }
755
756 MRI.colorMethodArgs(Meth, LRI, AI );
757}
758
Ruchira Sasankae727f852001-09-18 22:43:57 +0000759
760//----------------------------------------------------------------------------
761// Used to generate a label for a basic block
762//----------------------------------------------------------------------------
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000763void PhyRegAlloc::printLabel(const Value *const Val)
764{
765 if( Val->hasName() )
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000766 cout << Val->getName();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000767 else
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000768 cout << "Label" << Val;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000769}
770
771
Ruchira Sasankae727f852001-09-18 22:43:57 +0000772//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +0000773// This method calls setSugColorUsable method of each live range. This
774// will determine whether the suggested color of LR is really usable.
775// A suggested color is not usable when the suggested color is volatile
776// AND when there are call interferences
777//----------------------------------------------------------------------------
778
779void PhyRegAlloc::markUnusableSugColors()
780{
781 if(DEBUG_RA ) cout << "Creating LR lists ..." << endl;
782
783 // hash map iterator
784 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
785 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
786
787 for( ; HMI != HMIEnd ; ++HMI ) {
788
789 if( (*HMI).first ) {
790
791 LiveRange *L = (*HMI).second; // get the LiveRange
792
793 if(L) {
794 if( L->hasSuggestedColor() ) {
795
796 int RCID = (L->getRegClass())->getID();
797 if( MRI.isRegVolatile( RCID, L->getSuggestedColor()) &&
798 L->isCallInterference() )
799 L->setSuggestedColorUsable( false );
800 else
801 L->setSuggestedColorUsable( true );
802 }
803 } // if L->hasSuggestedColor()
804 }
805 } // for all LR's in hash map
806}
807
808
809
810
811
812
813
814
815
816
817
818//----------------------------------------------------------------------------
Ruchira Sasankae727f852001-09-18 22:43:57 +0000819// The entry pont to Register Allocation
820//----------------------------------------------------------------------------
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000821
822void PhyRegAlloc::allocateRegisters()
823{
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000824
825 // make sure that we put all register classes into the RegClassList
826 // before we call constructLiveRanges (now done in the constructor of
827 // PhyRegAlloc class).
828
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000829 constructLiveRanges(); // create LR info
830
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000831 if( DEBUG_RA )
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000832 LRI.printLiveRanges();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000833
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000834 createIGNodeListsAndIGs(); // create IGNode list and IGs
835
836 buildInterferenceGraphs(); // build IGs in all reg classes
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000837
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000838
839 if( DEBUG_RA ) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000840 // print all LRs in all reg classes
841 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
842 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000843
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000844 // print IGs in all register classes
845 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
846 RegClassList[ rc ]->printIG();
847 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000848
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000849 LRI.coalesceLRs(); // coalesce all live ranges
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000850
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000851 if( DEBUG_RA) {
852 // print all LRs in all reg classes
853 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
854 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000855
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000856 // print IGs in all register classes
857 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
858 RegClassList[ rc ]->printIG();
859 }
860
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +0000861
862 // mark un-usable suggested color before graph coloring algorithm.
863 // When this is done, the graph coloring algo will not reserve
864 // suggested color unnecessarily - they can be used by another LR
865 markUnusableSugColors();
866
867 // color all register classes using the graph coloring algo
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000868 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
869 RegClassList[ rc ]->colorAllRegs();
870
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000871
872 // color incoming args and call args
873 colorIncomingArgs();
874 colorCallRetArgs();
875
Ruchira Sasanka97b8b442001-10-18 22:36:26 +0000876
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000877 updateMachineCode();
Chris Lattner045e7c82001-09-19 16:26:23 +0000878 if (DEBUG_RA) {
Ruchira Sasanka1b732fd2001-10-16 16:34:44 +0000879 PrintMachineInstructions(Meth);
Chris Lattner045e7c82001-09-19 16:26:23 +0000880 printMachineCode(); // only for DEBUGGING
881 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000882}
883
Ruchira Sasankae727f852001-09-18 22:43:57 +0000884
885
886