blob: c39919a162b71f35522ca9805085651700b0fdc1 [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
128 //the live range of this var interferes with this call
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000129 if( isCallInst ) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000130 LROfVar->addCallInterference( (const Instruction *const) Def );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000131 // cout << "\n ++Added Call Interf to set:";
Ruchira Sasanka47c13722001-10-16 01:33:55 +0000132 //LROfVar->printSet();
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000133 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000134 }
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000135 else if(DEBUG_RA > 1) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000136 // we will not have LRs for values not explicitly allocated in the
137 // instruction stream (e.g., constants)
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000138 cout << " warning: no live range for " ;
139 printValue( *LIt); cout << endl; }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000140
141 }
142
143}
144
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000145//----------------------------------------------------------------------------
146// This method will walk thru code and create interferences in the IG of
147// each RegClass.
148//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000149
150void PhyRegAlloc::buildInterferenceGraphs()
151{
152
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000153 if(DEBUG_RA) cout << "Creating interference graphs ..." << endl;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000154
155 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
156
157 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
158
159 // get the iterator for machine instructions
160 const MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
161 MachineCodeForBasicBlock::const_iterator
162 MInstIterator = MIVec.begin();
163
164 // iterate over all the machine instructions in BB
165 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000166
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000167 const MachineInstr *const MInst = *MInstIterator;
168
169 // get the LV set after the instruction
170 const LiveVarSet *const LVSetAI =
171 LVI->getLiveVarSetAfterMInst(MInst, *BBI);
172
173 const bool isCallInst = TM.getInstrInfo().isCall(MInst->getOpCode());
174
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000175 // if( isCallInst) cout << "\n%%% Found call Inst:\n";
176
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000177 // iterate over MI operands to find defs
178 for( MachineInstr::val_op_const_iterator OpI(MInst);!OpI.done(); ++OpI) {
179
180 if( OpI.isDef() ) {
181 // create a new LR iff this operand is a def
182 addInterference(*OpI, LVSetAI, isCallInst );
183
184 } //if this is a def
185
186 } // for all operands
187
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000188
189 // Also add interference for any implicit definitions in a machine
190 // instr (currently, only calls have this).
191
192 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
193 if( NumOfImpRefs > 0 ) {
194 for(unsigned z=0; z < NumOfImpRefs; z++)
195 if( MInst->implicitRefIsDefined(z) )
196 addInterference( MInst->getImplicitRef(z), LVSetAI, isCallInst );
197 }
198
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000199 } // for all machine instructions in BB
200
201
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000202#if 0
203
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000204 // go thru LLVM instructions in the basic block and record all CALL
Ruchira Sasankae727f852001-09-18 22:43:57 +0000205 // instructions and Return instructions in the CallInstrList
206 // This is done because since there are no reverse pointers in machine
207 // instructions to find the llvm instruction, when we encounter a call
208 // or a return whose args must be specailly colored (e.g., %o's for args)
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000209 BasicBlock::const_iterator InstIt = (*BBI)->begin();
210
211 for( ; InstIt != (*BBI)->end() ; ++ InstIt) {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000212 unsigned OpCode = (*InstIt)->getOpcode();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000213
Ruchira Sasankae727f852001-09-18 22:43:57 +0000214 if( OpCode == Instruction::Call )
215 CallInstrList.push_back( *InstIt );
216
217 else if( OpCode == Instruction::Ret )
218 RetInstrList.push_back( *InstIt );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000219 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000220
221#endif
222
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000223
224 } // for all BBs in method
225
226
227 // add interferences for method arguments. Since there are no explict
228 // defs in method for args, we have to add them manually
229
230 addInterferencesForArgs(); // add interference for method args
231
232 if( DEBUG_RA)
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000233 cout << "Interference graphs calculted!" << endl;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000234
235}
236
237
238
239
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000240//----------------------------------------------------------------------------
241// This method will add interferences for incoming arguments to a method.
242//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000243void PhyRegAlloc::addInterferencesForArgs()
244{
245 // get the InSet of root BB
246 const LiveVarSet *const InSet = LVI->getInSetOfBB( Meth->front() );
247
248 // get the argument list
249 const Method::ArgumentListType& ArgList = Meth->getArgumentList();
250
251 // get an iterator to arg list
252 Method::ArgumentListType::const_iterator ArgIt = ArgList.begin();
253
254
255 for( ; ArgIt != ArgList.end() ; ++ArgIt) { // for each argument
256 addInterference( *ArgIt, InSet, false ); // add interferences between
257 // args and LVars at start
258 if( DEBUG_RA > 1) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000259 cout << " - %% adding interference for argument ";
260 printValue( (const Value *) *ArgIt); cout << endl;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000261 }
262 }
263}
264
265
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000266
267//----------------------------------------------------------------------------
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000268// This method inserts caller saving/restoring instructons before/after
269// a call machine instruction.
270//----------------------------------------------------------------------------
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000271
272
273void PhyRegAlloc::insertCallerSavingCode(const MachineInstr *MInst,
274 const BasicBlock *BB )
275{
276 assert( (TM.getInstrInfo()).isCall( MInst->getOpCode() ) );
277
Ruchira Sasanka47c13722001-10-16 01:33:55 +0000278 int StackOff = -8; // ****TODO : Change
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000279 hash_set<unsigned> PushedRegSet;
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000280
281 // Now find the LR of the return value of the call
282 // The last *implicit operand* is the return value of a call
283 // Insert it to to he PushedRegSet since we must not save that register
284 // and restore it after the call.
285 // We do this because, we look at the LV set *after* the instruction
286 // to determine, which LRs must be saved across calls. The return value
287 // of the call is live in this set - but we must not save/restore it.
288
289 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
290 if( NumOfImpRefs > 0 ) {
291
292 if( MInst->implicitRefIsDefined(NumOfImpRefs-1) ) {
293
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000294 const Value *RetVal = MInst->getImplicitRef(NumOfImpRefs-1);
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000295 LiveRange *RetValLR = LRI.getLiveRangeForValue( RetVal );
296 assert( RetValLR && "No LR for RetValue of call");
297
298 PushedRegSet.insert(
299 MRI.getUnifiedRegNum((RetValLR->getRegClass())->getID(),
300 RetValLR->getColor() ) );
301 }
302
303 }
304
305
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000306 const LiveVarSet *LVSetAft = LVI->getLiveVarSetAfterMInst(MInst, BB);
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000307
308 LiveVarSet::const_iterator LIt = LVSetAft->begin();
309
310 // for each live var in live variable set after machine inst
311 for( ; LIt != LVSetAft->end(); ++LIt) {
312
313 // get the live range corresponding to live var
314 LiveRange *const LR = LRI.getLiveRangeForValue(*LIt );
315
316 // LROfVar can be null if it is a const since a const
317 // doesn't have a dominating def - see Assumptions above
318 if( LR ) {
319
320 if( LR->hasColor() ) {
321
322 unsigned RCID = (LR->getRegClass())->getID();
323 unsigned Color = LR->getColor();
324
325 if ( MRI.isRegVolatile(RCID, Color) ) {
326
327 // if the value is in both LV sets (i.e., live before and after
328 // the call machine instruction)
329
330 unsigned Reg = MRI.getUnifiedRegNum(RCID, Color);
331
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000332 if( PushedRegSet.find(Reg) == PushedRegSet.end() ) {
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000333
334 // if we haven't already pushed that register
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000335
336 unsigned RegType = MRI.getRegType( LR );
337
338 // Now get two instructions - to push on stack and pop from stack
339 // and add them to InstrnsBefore and InstrnsAfter of the
340 // call instruction
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000341
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000342 MachineInstr *AdIBef =
343 MRI.cpReg2MemMI(Reg, MRI.getFramePointer(), StackOff, RegType );
344
345 MachineInstr *AdIAft =
346 MRI.cpMem2RegMI(MRI.getFramePointer(), StackOff, Reg, RegType );
347
348 ((AddedInstrMap[MInst])->InstrnsBefore).push_front(AdIBef);
349 ((AddedInstrMap[MInst])->InstrnsAfter).push_back(AdIAft);
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000350
351 PushedRegSet.insert( Reg );
Ruchira Sasanka47c13722001-10-16 01:33:55 +0000352 StackOff -= 8; // ****TODO: Correct ??????
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000353 cout << "\n $$$ Inserted caller saving instr";
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000354
355 } // if not already pushed
356
357 } // if LR has a volatile color
358
359 } // if LR has color
360
361 } // if there is a LR for Var
362
363 } // for each value in the LV set after instruction
364
365}
366
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000367
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000368//----------------------------------------------------------------------------
369// This method is called after register allocation is complete to set the
370// allocated reisters in the machine code. This code will add register numbers
371// to MachineOperands that contain a Value.
372//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000373
374void PhyRegAlloc::updateMachineCode()
375{
376
377 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
378
379 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
380
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000381 // get the iterator for machine instructions
382 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
383 MachineCodeForBasicBlock::iterator MInstIterator = MIVec.begin();
384
385 // iterate over all the machine instructions in BB
386 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
387
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000388 MachineInstr *MInst = *MInstIterator;
389
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000390 // if this machine instr is call, insert caller saving code
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000391
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000392 if( (TM.getInstrInfo()).isCall( MInst->getOpCode()) )
393 insertCallerSavingCode(MInst, *BBI );
394
395 // If there are instructions to be added, *before* this machine
396 // instruction, add them now.
397
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000398 if( AddedInstrMap[ MInst ] ) {
399
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000400 deque<MachineInstr *> &IBef = (AddedInstrMap[MInst])->InstrnsBefore;
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000401
402 if( ! IBef.empty() ) {
403
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000404 deque<MachineInstr *>::iterator AdIt;
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000405
406 for( AdIt = IBef.begin(); AdIt != IBef.end() ; ++AdIt ) {
407
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000408 cout << " *$* PREPENDed instr opcode: ";
409 cout << TargetInstrDescriptors[(*AdIt)->getOpCode()].opCodeString;
410 cout << endl;
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000411
412 MInstIterator = MIVec.insert( MInstIterator, *AdIt );
413 ++MInstIterator;
414 }
415
416 }
417
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000418 }
419
420
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000421
422 //for(MachineInstr::val_op_const_iterator OpI(MInst);!OpI.done();++OpI) {
423
424 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
425
426 MachineOperand& Op = MInst->getOperand(OpNum);
427
428 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
429 Op.getOperandType() == MachineOperand::MO_CCRegister) {
430
431 const Value *const Val = Op.getVRegValue();
432
433 // delete this condition checking later (must assert if Val is null)
Chris Lattner045e7c82001-09-19 16:26:23 +0000434 if( !Val) {
435 if (DEBUG_RA)
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000436 cout << "Warning: NULL Value found for operand" << endl;
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000437 continue;
438 }
439 assert( Val && "Value is NULL");
440
441 const LiveRange *const LR = LRI.getLiveRangeForValue(Val);
442
443 if ( !LR ) {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000444
445 // nothing to worry if it's a const or a label
446
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000447 if (DEBUG_RA) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000448 cout << "*NO LR for inst opcode: ";
449 cout << TargetInstrDescriptors[MInst->getOpCode()].opCodeString;
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000450 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000451
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000452 // if register is not allocated, mark register as invalid
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000453 if( Op.getAllocatedRegNum() == -1)
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000454 Op.setRegForValue( MRI.getInvalidRegNum());
Ruchira Sasankae727f852001-09-18 22:43:57 +0000455
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000456#if 0
Ruchira Sasankae727f852001-09-18 22:43:57 +0000457 if( ((Val->getType())->isLabelType()) ||
458 (Val->getValueType() == Value::ConstantVal) )
459 ; // do nothing
460
461 // The return address is not explicitly defined within a
462 // method. So, it is not colored by usual algorithm. In that case
463 // color it here.
464
465 //else if (TM.getInstrInfo().isCall(MInst->getOpCode()))
466 //Op.setRegForValue( MRI.getCallAddressReg() );
467
468 //TM.getInstrInfo().isReturn(MInst->getOpCode())
469 else if(TM.getInstrInfo().isReturn(MInst->getOpCode()) ) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000470 if (DEBUG_RA) cout << endl << "RETURN found" << endl;
Ruchira Sasankae727f852001-09-18 22:43:57 +0000471 Op.setRegForValue( MRI.getReturnAddressReg() );
472
473 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000474
475 if (Val->getValueType() == Value::InstructionVal)
Ruchira Sasankae727f852001-09-18 22:43:57 +0000476 {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000477 cout << "!Warning: No LiveRange for: ";
478 printValue( Val); cout << " Type: " << Val->getValueType();
479 cout << " RegVal=" << Op.getAllocatedRegNum() << endl;
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000480 }
481
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000482#endif
483
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000484 continue;
485 }
486
487 unsigned RCID = (LR->getRegClass())->getID();
488
489 Op.setRegForValue( MRI.getUnifiedRegNum(RCID, LR->getColor()) );
490
491 int RegNum = MRI.getUnifiedRegNum(RCID, LR->getColor());
492
Ruchira Sasankae727f852001-09-18 22:43:57 +0000493 }
494
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000495 } // for each operand
496
497
498 // If there are instructions to be added *after* this machine
499 // instruction, add them now
500
501 if( AddedInstrMap[ MInst ] ) {
502
503 deque<MachineInstr *> &IAft = (AddedInstrMap[MInst])->InstrnsAfter;
504
505 if( ! IAft.empty() ) {
506
507 deque<MachineInstr *>::iterator AdIt;
508
509 ++MInstIterator; // advance to the next instruction
510
511 for( AdIt = IAft.begin(); AdIt != IAft.end() ; ++AdIt ) {
512
513 cout << " *#* APPENDed instr opcode: ";
514 cout << TargetInstrDescriptors[(*AdIt)->getOpCode()].opCodeString;
515 cout << endl;
516
517 MInstIterator = MIVec.insert( MInstIterator, *AdIt );
518 ++MInstIterator;
519 }
520
521 // MInsterator already points to the next instr. Since the
522 // for loop also increments it, decrement it to point to the
523 // instruction added last
524 --MInstIterator;
525
526 }
527
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000528 }
529
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000530 } // for each machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000531 }
532}
533
534
535
536
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000537//----------------------------------------------------------------------------
538// This method prints the code with registers after register allocation is
539// complete.
540//----------------------------------------------------------------------------
541void PhyRegAlloc::printMachineCode()
542{
543
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000544 cout << endl << ";************** Method ";
545 cout << Meth->getName() << " *****************" << endl;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000546
547 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
548
549 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
550
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000551 cout << endl ; printLabel( *BBI); cout << ": ";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000552
553 // get the iterator for machine instructions
554 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
555 MachineCodeForBasicBlock::iterator MInstIterator = MIVec.begin();
556
557 // iterate over all the machine instructions in BB
558 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
559
560 MachineInstr *const MInst = *MInstIterator;
561
562
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000563 cout << endl << "\t";
564 cout << TargetInstrDescriptors[MInst->getOpCode()].opCodeString;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000565
566
567 //for(MachineInstr::val_op_const_iterator OpI(MInst);!OpI.done();++OpI) {
568
569 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
570
571 MachineOperand& Op = MInst->getOperand(OpNum);
572
573 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasankae727f852001-09-18 22:43:57 +0000574 Op.getOperandType() == MachineOperand::MO_CCRegister ||
575 Op.getOperandType() == MachineOperand::MO_PCRelativeDisp ) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000576
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000577 const Value *const Val = Op.getVRegValue () ;
Ruchira Sasankae727f852001-09-18 22:43:57 +0000578 // ****this code is temporary till NULL Values are fixed
579 if( ! Val ) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000580 cout << "\t<*NULL*>";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000581 continue;
582 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000583
584 // if a label or a constant
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000585 if( (Val->getValueType() == Value::BasicBlockVal) ) {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000586
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000587 cout << "\t"; printLabel( Op.getVRegValue () );
Ruchira Sasankae727f852001-09-18 22:43:57 +0000588 }
589 else {
590 // else it must be a register value
591 const int RegNum = Op.getAllocatedRegNum();
592
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000593 //if( RegNum != 1000)
594
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000595 cout << "\t" << "%" << MRI.getUnifiedRegName( RegNum );
596 // else cout << "\t<*NoReg*>";
Ruchira Sasankae727f852001-09-18 22:43:57 +0000597
598 }
599
600 }
601 else if(Op.getOperandType() == MachineOperand::MO_MachineRegister) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000602 cout << "\t" << "%" << MRI.getUnifiedRegName(Op.getMachineRegNum());
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000603 }
604
605 else
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000606 cout << "\t" << Op; // use dump field
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000607 }
608
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000609
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000610
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000611 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
612 if( NumOfImpRefs > 0 ) {
613
614 cout << "\tImplicit:";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000615
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000616 for(unsigned z=0; z < NumOfImpRefs; z++) {
617 printValue( MInst->getImplicitRef(z) );
618 cout << "\t";
619 }
620
621 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000622
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000623 } // for all machine instructions
624
625
626 cout << endl;
627
628 } // for all BBs
629
630 cout << endl;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000631}
632
Ruchira Sasankae727f852001-09-18 22:43:57 +0000633
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000634//----------------------------------------------------------------------------
635//
636//----------------------------------------------------------------------------
637
638void PhyRegAlloc::colorCallRetArgs()
639{
640
641 CallRetInstrListType &CallRetInstList = LRI.getCallRetInstrList();
642 CallRetInstrListType::const_iterator It = CallRetInstList.begin();
643
644 for( ; It != CallRetInstList.end(); ++It ) {
645
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000646 const MachineInstr *const CRMI = *It;
647 unsigned OpCode = CRMI->getOpCode();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000648
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000649 // get the added instructions for this Call/Ret instruciton
650 AddedInstrns *AI = AddedInstrMap[ CRMI ];
651 if ( !AI ) {
652 AI = new AddedInstrns();
653 AddedInstrMap[ CRMI ] = AI;
654 }
655
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000656 if( (TM.getInstrInfo()).isCall( OpCode ) )
657 MRI.colorCallArgs( CRMI, LRI, AI );
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000658
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000659 else if ( (TM.getInstrInfo()).isReturn(OpCode) )
660 MRI.colorRetValue( CRMI, LRI, AI );
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000661
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000662 else assert( 0 && "Non Call/Ret instrn in CallRetInstrList\n" );
663
664 }
665
666}
667
668//----------------------------------------------------------------------------
669
670//----------------------------------------------------------------------------
671void PhyRegAlloc::colorIncomingArgs()
672{
673 const BasicBlock *const FirstBB = Meth->front();
674 const MachineInstr *FirstMI = *((FirstBB->getMachineInstrVec()).begin());
675 assert( FirstMI && "No machine instruction in entry BB");
676
677 AddedInstrns *AI = AddedInstrMap[ FirstMI ];
678 if ( !AI ) {
679 AI = new AddedInstrns();
680 AddedInstrMap[ FirstMI ] = AI;
681 }
682
683 MRI.colorMethodArgs(Meth, LRI, AI );
684}
685
Ruchira Sasankae727f852001-09-18 22:43:57 +0000686
687//----------------------------------------------------------------------------
688// Used to generate a label for a basic block
689//----------------------------------------------------------------------------
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000690void PhyRegAlloc::printLabel(const Value *const Val)
691{
692 if( Val->hasName() )
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000693 cout << Val->getName();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000694 else
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000695 cout << "Label" << Val;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000696}
697
698
Ruchira Sasankae727f852001-09-18 22:43:57 +0000699//----------------------------------------------------------------------------
700// The entry pont to Register Allocation
701//----------------------------------------------------------------------------
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000702
703void PhyRegAlloc::allocateRegisters()
704{
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000705
706 // make sure that we put all register classes into the RegClassList
707 // before we call constructLiveRanges (now done in the constructor of
708 // PhyRegAlloc class).
709
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000710 constructLiveRanges(); // create LR info
711
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000712 if( DEBUG_RA )
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000713 LRI.printLiveRanges();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000714
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000715 createIGNodeListsAndIGs(); // create IGNode list and IGs
716
717 buildInterferenceGraphs(); // build IGs in all reg classes
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000718
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000719
720 if( DEBUG_RA ) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000721 // print all LRs in all reg classes
722 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
723 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000724
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000725 // print IGs in all register classes
726 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
727 RegClassList[ rc ]->printIG();
728 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000729
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000730 LRI.coalesceLRs(); // coalesce all live ranges
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000731
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000732 if( DEBUG_RA) {
733 // print all LRs in all reg classes
734 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
735 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000736
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000737 // print IGs in all register classes
738 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
739 RegClassList[ rc ]->printIG();
740 }
741
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000742 // color all register classes
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000743 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
744 RegClassList[ rc ]->colorAllRegs();
745
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000746
747 // color incoming args and call args
748 colorIncomingArgs();
749 colorCallRetArgs();
750
751
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000752 updateMachineCode();
Chris Lattner045e7c82001-09-19 16:26:23 +0000753 if (DEBUG_RA) {
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000754 // PrintMachineInstructions(Meth);
Chris Lattner045e7c82001-09-19 16:26:23 +0000755 printMachineCode(); // only for DEBUGGING
756 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000757}
758
Ruchira Sasankae727f852001-09-18 22:43:57 +0000759
760
761