blob: 3b61e1e6b781af17fa0f8c489719cf01b7e0a11c [file] [log] [blame]
Ruchira Sasanka8e604792001-09-14 21:18:34 +00001#include "llvm/CodeGen/PhyRegAlloc.h"
2
Ruchira Sasanka174bded2001-10-28 18:12:02 +00003//***TODO: There are several places we add instructions. Validate the order
4// of adding these instructions.
5
6
7
Chris Lattner045e7c82001-09-19 16:26:23 +00008cl::Enum<RegAllocDebugLevel_t> DEBUG_RA("dregalloc", cl::NoFlags,
9 "enable register allocation debugging information",
10 clEnumValN(RA_DEBUG_None , "n", "disable debug output"),
11 clEnumValN(RA_DEBUG_Normal , "y", "enable debug output"),
12 clEnumValN(RA_DEBUG_Verbose, "v", "enable extra debug output"), 0);
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000013
14
15//----------------------------------------------------------------------------
16// Constructor: Init local composite objects and create register classes.
17//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000018PhyRegAlloc::PhyRegAlloc(const Method *const M,
19 const TargetMachine& tm,
20 MethodLiveVarInfo *const Lvi)
21 : RegClassList(),
22 Meth(M), TM(tm), LVI(Lvi), LRI(M, tm, RegClassList),
23 MRI( tm.getRegInfo() ),
24 NumOfRegClasses(MRI.getNumOfRegClasses()),
Ruchira Sasanka174bded2001-10-28 18:12:02 +000025 AddedInstrMap(), StackOffsets()
Ruchira Sasanka8e604792001-09-14 21:18:34 +000026
27{
28 // **TODO: use an actual reserved color list
29 ReservedColorListType *RCL = new ReservedColorListType();
30
31 // create each RegisterClass and put in RegClassList
32 for( unsigned int rc=0; rc < NumOfRegClasses; rc++)
33 RegClassList.push_back( new RegClass(M, MRI.getMachineRegClass(rc), RCL) );
34
Ruchira Sasanka174bded2001-10-28 18:12:02 +000035 // **TODO: Init to the correct value. Also reset this to the correct
36 // value at the start of each instruction. Need a way to track max used
37 int curOffset4TmpSpills =0 ;
Ruchira Sasanka8e604792001-09-14 21:18:34 +000038}
39
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000040//----------------------------------------------------------------------------
41// This method initally creates interference graphs (one in each reg class)
42// and IGNodeList (one in each IG). The actual nodes will be pushed later.
43//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000044
45void PhyRegAlloc::createIGNodeListsAndIGs()
46{
Ruchira Sasankac4d4b762001-10-16 01:23:19 +000047 if(DEBUG_RA ) cout << "Creating LR lists ..." << endl;
Ruchira Sasanka8e604792001-09-14 21:18:34 +000048
49 // hash map iterator
50 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
51
52 // hash map end
53 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
54
55 for( ; HMI != HMIEnd ; ++HMI ) {
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +000056
57 if( (*HMI).first ) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +000058
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +000059 LiveRange *L = (*HMI).second; // get the LiveRange
Ruchira Sasanka8e604792001-09-14 21:18:34 +000060
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +000061 if( !L) {
62 if( DEBUG_RA) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +000063 cout << "\n*?!?Warning: Null liver range found for: ";
64 printValue( (*HMI).first) ; cout << endl;
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +000065 }
66 continue;
67 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +000068 // if the Value * is not null, and LR
69 // is not yet written to the IGNodeList
70 if( !(L->getUserIGNode()) ) {
71
72 RegClass *const RC = // RegClass of first value in the LR
73 //RegClassList [MRI.getRegClassIDOfValue(*(L->begin()))];
74 RegClassList[ L->getRegClass()->getID() ];
75
76 RC-> addLRToIG( L ); // add this LR to an IG
77 }
78 }
79 }
80
81 // init RegClassList
82 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
83 RegClassList[ rc ]->createInterferenceGraph();
84
85 if( DEBUG_RA)
Ruchira Sasankac4d4b762001-10-16 01:23:19 +000086 cout << "LRLists Created!" << endl;
Ruchira Sasanka8e604792001-09-14 21:18:34 +000087}
88
89
90
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000091//----------------------------------------------------------------------------
92// This method will add all interferences at for a given instruction.
Ruchira Sasanka8e604792001-09-14 21:18:34 +000093// Interence occurs only if the LR of Def (Inst or Arg) is of the same reg
94// class as that of live var. The live var passed to this function is the
95// LVset AFTER the instruction
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000096//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000097
98void PhyRegAlloc::addInterference(const Value *const Def,
99 const LiveVarSet *const LVSet,
100 const bool isCallInst) {
101
102 LiveVarSet::const_iterator LIt = LVSet->begin();
103
104 // get the live range of instruction
105 const LiveRange *const LROfDef = LRI.getLiveRangeForValue( Def );
106
107 IGNode *const IGNodeOfDef = LROfDef->getUserIGNode();
108 assert( IGNodeOfDef );
109
110 RegClass *const RCOfDef = LROfDef->getRegClass();
111
112 // for each live var in live variable set
113 for( ; LIt != LVSet->end(); ++LIt) {
114
115 if( DEBUG_RA > 1) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000116 cout << "< Def="; printValue(Def);
117 cout << ", Lvar="; printValue( *LIt); cout << "> ";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000118 }
119
120 // get the live range corresponding to live var
121 LiveRange *const LROfVar = LRI.getLiveRangeForValue(*LIt );
122
123 // LROfVar can be null if it is a const since a const
124 // doesn't have a dominating def - see Assumptions above
125 if( LROfVar) {
126
127 if(LROfDef == LROfVar) // do not set interf for same LR
128 continue;
129
130 // if 2 reg classes are the same set interference
131 if( RCOfDef == LROfVar->getRegClass() ){
132 RCOfDef->setInterference( LROfDef, LROfVar);
133
134 }
135
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000136 else if(DEBUG_RA > 1) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000137 // we will not have LRs for values not explicitly allocated in the
138 // instruction stream (e.g., constants)
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000139 cout << " warning: no live range for " ;
140 printValue( *LIt); cout << endl; }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000141
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000142 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000143
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000144 }
145
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000146}
147
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000148
149//----------------------------------------------------------------------------
150// For a call instruction, this method sets the CallInterference flag in
151// the LR of each variable live int the Live Variable Set live after the
152// call instruction (except the return value of the call instruction - since
153// the return value does not interfere with that call itself).
154//----------------------------------------------------------------------------
155
156void PhyRegAlloc::setCallInterferences(const MachineInstr *MInst,
157 const LiveVarSet *const LVSetAft )
158{
159 // Now find the LR of the return value of the call
Ruchira Sasankab3b6f532001-10-21 16:43:41 +0000160
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000161
162 // We do this because, we look at the LV set *after* the instruction
163 // to determine, which LRs must be saved across calls. The return value
164 // of the call is live in this set - but it does not interfere with call
165 // (i.e., we can allocate a volatile register to the return value)
166
167 LiveRange *RetValLR = NULL;
168
Ruchira Sasankab3b6f532001-10-21 16:43:41 +0000169 const Value *RetVal = MRI.getCallInstRetVal( MInst );
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000170
Ruchira Sasankab3b6f532001-10-21 16:43:41 +0000171 if( RetVal ) {
172 RetValLR = LRI.getLiveRangeForValue( RetVal );
173 assert( RetValLR && "No LR for RetValue of call");
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000174 }
175
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000176 if( DEBUG_RA)
177 cout << "\n For call inst: " << *MInst;
178
179 LiveVarSet::const_iterator LIt = LVSetAft->begin();
180
181 // for each live var in live variable set after machine inst
182 for( ; LIt != LVSetAft->end(); ++LIt) {
183
184 // get the live range corresponding to live var
185 LiveRange *const LR = LRI.getLiveRangeForValue(*LIt );
186
187 if( LR && DEBUG_RA) {
188 cout << "\n\tLR Aft Call: ";
189 LR->printSet();
190 }
191
192
193 // LR can be null if it is a const since a const
194 // doesn't have a dominating def - see Assumptions above
195 if( LR && (LR != RetValLR) ) {
196 LR->setCallInterference();
197 if( DEBUG_RA) {
198 cout << "\n ++Added call interf for LR: " ;
199 LR->printSet();
200 }
201 }
202
203 }
204
205}
206
207
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000208//----------------------------------------------------------------------------
209// This method will walk thru code and create interferences in the IG of
210// each RegClass.
211//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000212
213void PhyRegAlloc::buildInterferenceGraphs()
214{
215
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000216 if(DEBUG_RA) cout << "Creating interference graphs ..." << endl;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000217
218 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
219
220 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
221
222 // get the iterator for machine instructions
223 const MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
224 MachineCodeForBasicBlock::const_iterator
225 MInstIterator = MIVec.begin();
226
227 // iterate over all the machine instructions in BB
228 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000229
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000230 const MachineInstr *const MInst = *MInstIterator;
231
232 // get the LV set after the instruction
233 const LiveVarSet *const LVSetAI =
234 LVI->getLiveVarSetAfterMInst(MInst, *BBI);
235
236 const bool isCallInst = TM.getInstrInfo().isCall(MInst->getOpCode());
237
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000238 if( isCallInst ) {
239 //cout << "\nFor call inst: " << *MInst;
240
241 // set the isCallInterference flag of each live range wich extends
242 // accross this call instruction. This information is used by graph
243 // coloring algo to avoid allocating volatile colors to live ranges
244 // that span across calls (since they have to be saved/restored)
245 setCallInterferences( MInst, LVSetAI);
246 }
247
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000248
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000249 // iterate over MI operands to find defs
250 for( MachineInstr::val_op_const_iterator OpI(MInst);!OpI.done(); ++OpI) {
251
252 if( OpI.isDef() ) {
253 // create a new LR iff this operand is a def
254 addInterference(*OpI, LVSetAI, isCallInst );
255
256 } //if this is a def
257
258 } // for all operands
259
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000260
261 // Also add interference for any implicit definitions in a machine
262 // instr (currently, only calls have this).
263
264 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
265 if( NumOfImpRefs > 0 ) {
266 for(unsigned z=0; z < NumOfImpRefs; z++)
267 if( MInst->implicitRefIsDefined(z) )
268 addInterference( MInst->getImplicitRef(z), LVSetAI, isCallInst );
269 }
270
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000271 } // for all machine instructions in BB
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000272
273 } // for all BBs in method
274
275
276 // add interferences for method arguments. Since there are no explict
277 // defs in method for args, we have to add them manually
278
279 addInterferencesForArgs(); // add interference for method args
280
281 if( DEBUG_RA)
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000282 cout << "Interference graphs calculted!" << endl;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000283
284}
285
286
287
288
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000289//----------------------------------------------------------------------------
290// This method will add interferences for incoming arguments to a method.
291//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000292void PhyRegAlloc::addInterferencesForArgs()
293{
294 // get the InSet of root BB
295 const LiveVarSet *const InSet = LVI->getInSetOfBB( Meth->front() );
296
297 // get the argument list
298 const Method::ArgumentListType& ArgList = Meth->getArgumentList();
299
300 // get an iterator to arg list
301 Method::ArgumentListType::const_iterator ArgIt = ArgList.begin();
302
303
304 for( ; ArgIt != ArgList.end() ; ++ArgIt) { // for each argument
305 addInterference( *ArgIt, InSet, false ); // add interferences between
306 // args and LVars at start
307 if( DEBUG_RA > 1) {
Ruchira Sasanka97b8b442001-10-18 22:36:26 +0000308 cout << " - %% adding interference for argument ";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000309 printValue( (const Value *) *ArgIt); cout << endl;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000310 }
311 }
312}
313
314
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000315
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000316#if 0
317
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000318//----------------------------------------------------------------------------
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000319// This method inserts caller saving/restoring instructons before/after
320// a call machine instruction.
321//----------------------------------------------------------------------------
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000322
323
324void PhyRegAlloc::insertCallerSavingCode(const MachineInstr *MInst,
325 const BasicBlock *BB )
326{
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000327 // assert( (TM.getInstrInfo()).isCall( MInst->getOpCode() ) );
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000328
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000329 StackOffsets.resetTmpPos();
330
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000331 hash_set<unsigned> PushedRegSet;
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000332
333 // Now find the LR of the return value of the call
334 // The last *implicit operand* is the return value of a call
335 // Insert it to to he PushedRegSet since we must not save that register
336 // and restore it after the call.
337 // We do this because, we look at the LV set *after* the instruction
338 // to determine, which LRs must be saved across calls. The return value
339 // of the call is live in this set - but we must not save/restore it.
340
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000341
Ruchira Sasankab3b6f532001-10-21 16:43:41 +0000342 const Value *RetVal = MRI.getCallInstRetVal( MInst );
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000343
Ruchira Sasankab3b6f532001-10-21 16:43:41 +0000344 if( RetVal ) {
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000345
Ruchira Sasankab3b6f532001-10-21 16:43:41 +0000346 LiveRange *RetValLR = LRI.getLiveRangeForValue( RetVal );
347 assert( RetValLR && "No LR for RetValue of call");
348
349 PushedRegSet.insert(
350 MRI.getUnifiedRegNum((RetValLR->getRegClass())->getID(),
351 RetValLR->getColor() ) );
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000352 }
353
354
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000355 const LiveVarSet *LVSetAft = LVI->getLiveVarSetAfterMInst(MInst, BB);
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000356
357 LiveVarSet::const_iterator LIt = LVSetAft->begin();
358
359 // for each live var in live variable set after machine inst
360 for( ; LIt != LVSetAft->end(); ++LIt) {
361
362 // get the live range corresponding to live var
363 LiveRange *const LR = LRI.getLiveRangeForValue(*LIt );
364
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000365 // LR can be null if it is a const since a const
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000366 // doesn't have a dominating def - see Assumptions above
367 if( LR ) {
368
369 if( LR->hasColor() ) {
370
371 unsigned RCID = (LR->getRegClass())->getID();
372 unsigned Color = LR->getColor();
373
374 if ( MRI.isRegVolatile(RCID, Color) ) {
375
376 // if the value is in both LV sets (i.e., live before and after
377 // the call machine instruction)
Ruchira Sasanka97b8b442001-10-18 22:36:26 +0000378
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000379 unsigned Reg = MRI.getUnifiedRegNum(RCID, Color);
380
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000381 if( PushedRegSet.find(Reg) == PushedRegSet.end() ) {
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000382
383 // if we haven't already pushed that register
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000384
385 unsigned RegType = MRI.getRegType( LR );
386
387 // Now get two instructions - to push on stack and pop from stack
388 // and add them to InstrnsBefore and InstrnsAfter of the
389 // call instruction
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000390
391 int StackOff = StackOffsets.getNewTmpPosOffFromSP();
392
393 /**** TODO
394
395 if( RegType == SaveViaIntReg) {
396
397 int FreeIntReg = getFreedIntReg(......)
398
399
400 }
401 */
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000402
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000403 MachineInstr *AdIBef =
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000404 MRI.cpReg2MemMI(Reg, MRI.getStackPointer(), StackOff, RegType );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000405
406 MachineInstr *AdIAft =
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000407 MRI.cpMem2RegMI(MRI.getStackPointer(), StackOff, Reg, RegType );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000408
409 ((AddedInstrMap[MInst])->InstrnsBefore).push_front(AdIBef);
410 ((AddedInstrMap[MInst])->InstrnsAfter).push_back(AdIAft);
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000411
412 PushedRegSet.insert( Reg );
Ruchira Sasanka97b8b442001-10-18 22:36:26 +0000413
414 if(DEBUG_RA) {
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000415 cerr << "\nFor callee save call inst:" << *MInst;
Ruchira Sasanka97b8b442001-10-18 22:36:26 +0000416 cerr << "\n -inserted caller saving instrs:\n\t ";
417 cerr << *AdIBef << "\n\t" << *AdIAft ;
418 }
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000419 } // if not already pushed
420
421 } // if LR has a volatile color
422
423 } // if LR has color
424
425 } // if there is a LR for Var
426
427 } // for each value in the LV set after instruction
428
429}
430
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000431#endif
432
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000433
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000434//----------------------------------------------------------------------------
435// This method is called after register allocation is complete to set the
436// allocated reisters in the machine code. This code will add register numbers
437// to MachineOperands that contain a Value.
438//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000439
440void PhyRegAlloc::updateMachineCode()
441{
442
443 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
444
445 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
446
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000447 // get the iterator for machine instructions
448 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
449 MachineCodeForBasicBlock::iterator MInstIterator = MIVec.begin();
450
451 // iterate over all the machine instructions in BB
452 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
453
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000454 MachineInstr *MInst = *MInstIterator;
455
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000456 // if this machine instr is call, insert caller saving code
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000457
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000458 if( (TM.getInstrInfo()).isCall( MInst->getOpCode()) )
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000459 MRI.insertCallerSavingCode(MInst, *BBI, *this );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000460
461 // If there are instructions to be added, *before* this machine
462 // instruction, add them now.
463
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000464 if( AddedInstrMap[ MInst ] ) {
465
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000466 deque<MachineInstr *> &IBef = (AddedInstrMap[MInst])->InstrnsBefore;
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000467
468 if( ! IBef.empty() ) {
469
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000470 deque<MachineInstr *>::iterator AdIt;
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000471
472 for( AdIt = IBef.begin(); AdIt != IBef.end() ; ++AdIt ) {
473
Ruchira Sasanka97b8b442001-10-18 22:36:26 +0000474 if( DEBUG_RA)
475 cerr << " *$* PREPENDed instr " << *AdIt << endl;
476
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000477 MInstIterator = MIVec.insert( MInstIterator, *AdIt );
478 ++MInstIterator;
479 }
480
481 }
482
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000483 }
484
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000485 // reset the stack offset for temporary variables since we may
486 // need that to spill
487 StackOffsets.resetTmpPos();
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000488
489 //for(MachineInstr::val_op_const_iterator OpI(MInst);!OpI.done();++OpI) {
490
491 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
492
493 MachineOperand& Op = MInst->getOperand(OpNum);
494
495 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
496 Op.getOperandType() == MachineOperand::MO_CCRegister) {
497
498 const Value *const Val = Op.getVRegValue();
499
500 // delete this condition checking later (must assert if Val is null)
Chris Lattner045e7c82001-09-19 16:26:23 +0000501 if( !Val) {
502 if (DEBUG_RA)
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000503 cout << "Warning: NULL Value found for operand" << endl;
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000504 continue;
505 }
506 assert( Val && "Value is NULL");
507
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000508 LiveRange *const LR = LRI.getLiveRangeForValue(Val);
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000509
510 if ( !LR ) {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000511
512 // nothing to worry if it's a const or a label
513
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000514 if (DEBUG_RA) {
Ruchira Sasanka1b732fd2001-10-16 16:34:44 +0000515 cout << "*NO LR for operand : " << Op ;
516 cout << " [reg:" << Op.getAllocatedRegNum() << "]";
517 cout << " in inst:\t" << *MInst << endl;
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000518 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000519
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000520 // if register is not allocated, mark register as invalid
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000521 if( Op.getAllocatedRegNum() == -1)
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000522 Op.setRegForValue( MRI.getInvalidRegNum());
Ruchira Sasankae727f852001-09-18 22:43:57 +0000523
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000524
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000525 continue;
526 }
527
528 unsigned RCID = (LR->getRegClass())->getID();
529
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000530 if( LR->hasColor() ) {
531 Op.setRegForValue( MRI.getUnifiedRegNum(RCID, LR->getColor()) );
532 }
533 else {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000534
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000535 // LR did NOT receive a color (register). Now, insert spill code
536 // for spilled opeands in this machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000537
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000538 assert(0 && "LR must be spilled");
539 // insertCode4SpilledLR(LR, MInst, *BBI, OpNum );
540
541 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000542 }
543
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000544 } // for each operand
545
546
547 // If there are instructions to be added *after* this machine
548 // instruction, add them now
549
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000550 if( AddedInstrMap[ MInst ] &&
551 ! (AddedInstrMap[ MInst ]->InstrnsAfter).empty() ) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000552
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000553 // if there are delay slots for this instruction, the instructions
554 // added after it must really go after the delayed instruction(s)
555 // So, we move the InstrAfter of the current instruction to the
556 // corresponding delayed instruction
557
558 unsigned delay;
559 if((delay=TM.getInstrInfo().getNumDelaySlots(MInst->getOpCode())) >0){
560 move2DelayedInstr(MInst, *(MInstIterator+delay) );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000561
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000562 if(DEBUG_RA) cout<< "\nMoved an added instr after the delay slot";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000563 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000564
565 else {
566
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000567
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000568 // Here we can add the "instructions after" to the current
569 // instruction since there are no delay slots for this instruction
570
571 deque<MachineInstr *> &IAft = (AddedInstrMap[MInst])->InstrnsAfter;
572
573 if( ! IAft.empty() ) {
574
575 deque<MachineInstr *>::iterator AdIt;
576
577 ++MInstIterator; // advance to the next instruction
578
579 for( AdIt = IAft.begin(); AdIt != IAft.end() ; ++AdIt ) {
580
581 if(DEBUG_RA)
582 cerr << " *#* APPENDed instr opcode: " << *AdIt << endl;
583
584 MInstIterator = MIVec.insert( MInstIterator, *AdIt );
585 ++MInstIterator;
586 }
587
588 // MInsterator already points to the next instr. Since the
589 // for loop also increments it, decrement it to point to the
590 // instruction added last
591 --MInstIterator;
592
593 }
594
595 } // if not delay
596
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000597 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000598
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000599 } // for each machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000600 }
601}
602
603
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000604
605#if 0
606
607
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000608//----------------------------------------------------------------------------
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000609// This method inserts spill code for AN operand whose LR was spilled.
610// This method may be called several times for a single machine instruction
611// if it contains many spilled operands. Each time it is called, it finds
612// a register which is not live at that instruction and also which is not
613// used by other spilled operands of the same instruction. Then it uses
614// this register temporarily to accomodate the spilled value.
615//----------------------------------------------------------------------------
616void PhyRegAlloc::insertCode4SpilledLR(const LiveRange *LR,
617 const MachineInstr *MInst,
618 const BasisBlock *BB,
619 const unsigned OpNum) {
620
621 MachineOperand& Op = MInst->getOperand(OpNum);
622 bool isDef = MInst->operandIsDefined(OpNum);
623 unsigned RegType = MRI.getRegType( LR );
624 int SpillOff = LR->getSpillOffFromFP();
625 RegClass *RC = LR->getRegClass();
626 const LiveVarSet *LVSetBef = LVI->getLiveVarSetBeforeMInst(MInst, BB);
627 int TmpOff = StackOffsets.getNewTmpPosOffFromSP();
628 MachineInstr *MIBef, *AdIMid, *MIAft;
629 int TmpReg;
630
631 TmpReg = getUsableRegAtMI(RC, RegType, MInst,LVSetBef, MIBef, MIAft);
632 TmpReg = getUnifiedRegNum( RC->getID(), TmpReg );
633
634
635 if( !isDef ) {
636
637 // for a USE, we have to load the value of LR from stack to a TmpReg
638 // and use the TmpReg as one operand of instruction
639
640 // actual loading instruction
641 AdIMid = MRI.cpMem2RegMI(MRI.getFramePointer(), SpillOff, TmpReg, RegType);
642
643 if( MIBef )
644 ((AddedInstrMap[MInst])->InstrnsBefore).push_back(MIBef);
645
646 ((AddedInstrMap[MInst])->InstrnsBefore).push_back(AdiMid);
647
648 if( MIAft)
649 ((AddedInstrMap[MInst])->InstrnsAfter).push_front(MIAft);
650
651
652 }
653 else { // if this is a Def
654
655 // for a DEF, we have to store the value produced by this instruction
656 // on the stack position allocated for this LR
657
658 // actual storing instruction
659 AdIMid = MRI.cpReg2MemMI(TmpReg, MRI.getFramePointer(), SpillOff, RegType);
660
661 if( MIBef )
662 ((AddedInstrMap[MInst])->InstrnsBefore).push_back(MIBef);
663
664 ((AddedInstrMap[MInst])->InstrnsBefore).push_back(AdiMid);
665
666 if( MIAft)
667 ((AddedInstrMap[MInst])->InstrnsAfter).push_front(MIAft);
668
669 } // if !DEF
670
671
672 Op.setRegForValue( TmpReg ); // set the opearnd
673
674
675}
676
677
678//----------------------------------------------------------------------------
679// We can use the following method to get a temporary register to be used
680// BEFORE any given machine instruction. If there is a register available,
681// this method will simply return that register and set MIBef = MIAft = NULL.
682// Otherwise, it will return a register and MIAft and MIBef will contain
683// two instructions used to free up this returned register.
684//----------------------------------------------------------------------------
685
686int PhyRegAlloc::getUsableRegAtMI(const RegClass *RC,
687 const int RegType,
688 const MachineInstr *MInst,
689 const LiveVarSet *LVSetBef,
690 MachineInstr *MIBef,
691 MachineInstr *MIAft) {
692
693 int Reg = getUnusedRegAtMI(RC, MInst, LVSetBef);
694
695 if( Reg != -1) {
696 // we found an unused register, so we can simply used
697 MIBef = MIAft = NULL;
698 }
699 else {
700 // we couldn't find an unused register. Generate code to ree up a reg by
701 // saving it on stack and restoring after the instruction
702
703 Reg = getRegNotUsedByThisInst(RC, MInst);
704 MIBef = cpReg2MemMI(Reg, MRI.getFramePointer(), TmpOff, RegType );
705 MIAft = cpMem2RegMI(MEI.getFramePointer(), TmpOff, Reg, RegType );
706 }
707
708 return Reg;
709}
710
711//----------------------------------------------------------------------------
712// This method is called to get a new unused register that can be used to
713// accomodate a spilled value.
714// This method may be called several times for a single machine instruction
715// if it contains many spilled operands. Each time it is called, it finds
716// a register which is not live at that instruction and also which is not
717// used by other spilled operands of the same instruction.
718//----------------------------------------------------------------------------
719int PhyRegAlloc::getUnusedRegAtMI(const RegClass *RC,
720 const MachineInstr *MInst,
721 const LiveVarSet *LVSetBef) {
722
723 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
724
725 bool *IsColorUsedArr = RC->getIsColorUsedArr();
726
727 for(unsigned i=0; i < NumAvailRegs; i++);
728 IsColorUsedArr[i] = false;
729
730 LiveVarSet::const_iterator LIt = LVSetBef->begin();
731
732 // for each live var in live variable set after machine inst
733 for( ; LIt != LVSetBef->end(); ++LIt) {
734
735 // get the live range corresponding to live var
736 LiveRange *const LRofLV = LRI.getLiveRangeForValue(*LIt );
737
738 // LR can be null if it is a const since a const
739 // doesn't have a dominating def - see Assumptions above
740 if( LRofLV )
741 if( LRofLV->hasColor() )
742 IsColorUsedArr[ LRofLV->getColor() ] = true;
743 }
744
745 // It is possible that one operand of this MInst was already spilled
746 // and it received some register temporarily. If that's the case,
747 // it is recorded in machine operand. We must skip such registers.
748
749 setRegsUsedByThisInst(RC, MInst);
750
751 unsigned c; // find first unused color
752 for( c=0; c < NumAvailRegs; c++)
753 if( ! IsColorUsedArr[ c ] ) break;
754
755 if(c < NumAvailRegs)
756 return c;
757 else
758 return -1;
759
760
761}
762
763
764#endif
765
766//----------------------------------------------------------------------------
767// This method modifies the IsColorUsedArr of the register class passed to it.
768// It sets the bits corresponding to the registers used by this machine
769// instructions. Explicit operands are set.
770//----------------------------------------------------------------------------
771void PhyRegAlloc::setRegsUsedByThisInst(RegClass *RC,
772 const MachineInstr *MInst ) {
773
774 bool *IsColorUsedArr = RC->getIsColorUsedArr();
775
776 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
777
778 const MachineOperand& Op = MInst->getOperand(OpNum);
779
780 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
781 Op.getOperandType() == MachineOperand::MO_CCRegister) {
782
783 const Value *const Val = Op.getVRegValue();
784
785 if( !Val )
786 if( MRI.getRegClassIDOfValue( Val )== RC->getID() ) {
787 int Reg;
788 if( (Reg=Op.getAllocatedRegNum()) != -1)
789 IsColorUsedArr[ Reg ] = true;
790
791 }
792 }
793 }
794
795 // If there are implicit references, mark them as well
796
797 for(unsigned z=0; z < MInst->getNumImplicitRefs(); z++) {
798
799 LiveRange *const LRofImpRef =
800 LRI.getLiveRangeForValue( MInst->getImplicitRef(z) );
801
802 if( LRofImpRef )
803 if( LRofImpRef->hasColor() )
804 IsColorUsedArr[ LRofImpRef->getColor() ] = true;
805 }
806
807
808
809}
810
811
812
813//----------------------------------------------------------------------------
814// Get any other register in a register class, other than what is used
815// by operands of a machine instruction.
816//----------------------------------------------------------------------------
817int PhyRegAlloc::getRegNotUsedByThisInst(RegClass *RC,
818 const MachineInstr *MInst) {
819
820 bool *IsColorUsedArr = RC->getIsColorUsedArr();
821 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
822
823
824 for(unsigned i=0; i < NumAvailRegs ; i++)
825 IsColorUsedArr[i] = false;
826
827 setRegsUsedByThisInst(RC, MInst);
828
829 unsigned c; // find first unused color
830 for( c=0; c < RC->getNumOfAvailRegs(); c++)
831 if( ! IsColorUsedArr[ c ] ) break;
832
833 if(c < NumAvailRegs)
834 return c;
835 else
836 assert( 0 && "FATAL: No free register could be found in reg class!!");
837
838}
839
840
841
842
843
844//----------------------------------------------------------------------------
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000845// If there are delay slots for an instruction, the instructions
846// added after it must really go after the delayed instruction(s).
847// So, we move the InstrAfter of that instruction to the
848// corresponding delayed instruction using the following method.
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000849
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000850//----------------------------------------------------------------------------
851void PhyRegAlloc:: move2DelayedInstr(const MachineInstr *OrigMI,
852 const MachineInstr *DelayedMI) {
853
854
855 // "added after" instructions of the original instr
856 deque<MachineInstr *> &OrigAft = (AddedInstrMap[OrigMI])->InstrnsAfter;
857
858 // "added instructions" of the delayed instr
859 AddedInstrns *DelayAdI = AddedInstrMap[DelayedMI];
860
861 if(! DelayAdI ) { // create a new "added after" if necessary
862 DelayAdI = new AddedInstrns();
863 AddedInstrMap[DelayedMI] = DelayAdI;
864 }
865
866 // "added after" instructions of the delayed instr
867 deque<MachineInstr *> &DelayedAft = DelayAdI->InstrnsAfter;
868
869 // go thru all the "added after instructions" of the original instruction
870 // and append them to the "addded after instructions" of the delayed
871 // instructions
872
873 deque<MachineInstr *>::iterator OrigAdIt;
874
875 for( OrigAdIt = OrigAft.begin(); OrigAdIt != OrigAft.end() ; ++OrigAdIt ) {
876 DelayedAft.push_back( *OrigAdIt );
877 }
878
879 // empty the "added after instructions" of the original instruction
880 OrigAft.clear();
881
882}
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000883
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000884//----------------------------------------------------------------------------
885// This method prints the code with registers after register allocation is
886// complete.
887//----------------------------------------------------------------------------
888void PhyRegAlloc::printMachineCode()
889{
890
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000891 cout << endl << ";************** Method ";
892 cout << Meth->getName() << " *****************" << endl;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000893
894 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
895
896 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
897
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000898 cout << endl ; printLabel( *BBI); cout << ": ";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000899
900 // get the iterator for machine instructions
901 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
902 MachineCodeForBasicBlock::iterator MInstIterator = MIVec.begin();
903
904 // iterate over all the machine instructions in BB
905 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
906
907 MachineInstr *const MInst = *MInstIterator;
908
909
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000910 cout << endl << "\t";
911 cout << TargetInstrDescriptors[MInst->getOpCode()].opCodeString;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000912
913
914 //for(MachineInstr::val_op_const_iterator OpI(MInst);!OpI.done();++OpI) {
915
916 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
917
918 MachineOperand& Op = MInst->getOperand(OpNum);
919
920 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasanka97b8b442001-10-18 22:36:26 +0000921 Op.getOperandType() == MachineOperand::MO_CCRegister /*||
922 Op.getOperandType() == MachineOperand::MO_PCRelativeDisp*/ ) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000923
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000924 const Value *const Val = Op.getVRegValue () ;
Ruchira Sasankae727f852001-09-18 22:43:57 +0000925 // ****this code is temporary till NULL Values are fixed
926 if( ! Val ) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000927 cout << "\t<*NULL*>";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000928 continue;
929 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000930
931 // if a label or a constant
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000932 if( (Val->getValueType() == Value::BasicBlockVal) ) {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000933
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000934 cout << "\t"; printLabel( Op.getVRegValue () );
Ruchira Sasankae727f852001-09-18 22:43:57 +0000935 }
936 else {
937 // else it must be a register value
938 const int RegNum = Op.getAllocatedRegNum();
939
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +0000940 cout << "\t" << "%" << MRI.getUnifiedRegName( RegNum );
Ruchira Sasankae727f852001-09-18 22:43:57 +0000941 }
942
943 }
944 else if(Op.getOperandType() == MachineOperand::MO_MachineRegister) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000945 cout << "\t" << "%" << MRI.getUnifiedRegName(Op.getMachineRegNum());
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000946 }
947
948 else
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000949 cout << "\t" << Op; // use dump field
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000950 }
951
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000952
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000953
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000954 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
955 if( NumOfImpRefs > 0 ) {
956
957 cout << "\tImplicit:";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000958
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000959 for(unsigned z=0; z < NumOfImpRefs; z++) {
960 printValue( MInst->getImplicitRef(z) );
961 cout << "\t";
962 }
963
964 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000965
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000966 } // for all machine instructions
967
968
969 cout << endl;
970
971 } // for all BBs
972
973 cout << endl;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000974}
975
Ruchira Sasankae727f852001-09-18 22:43:57 +0000976
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000977//----------------------------------------------------------------------------
978//
979//----------------------------------------------------------------------------
980
981void PhyRegAlloc::colorCallRetArgs()
982{
983
984 CallRetInstrListType &CallRetInstList = LRI.getCallRetInstrList();
985 CallRetInstrListType::const_iterator It = CallRetInstList.begin();
986
987 for( ; It != CallRetInstList.end(); ++It ) {
988
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000989 const MachineInstr *const CRMI = *It;
990 unsigned OpCode = CRMI->getOpCode();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000991
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000992 // get the added instructions for this Call/Ret instruciton
993 AddedInstrns *AI = AddedInstrMap[ CRMI ];
994 if ( !AI ) {
995 AI = new AddedInstrns();
996 AddedInstrMap[ CRMI ] = AI;
997 }
998
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000999 // Tmp stack poistions are needed by some calls that have spilled args
1000 // So reset it before we call each such method
1001 StackOffsets.resetTmpPos();
1002
Ruchira Sasankaa90e7702001-10-15 16:26:38 +00001003 if( (TM.getInstrInfo()).isCall( OpCode ) )
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001004 MRI.colorCallArgs( CRMI, LRI, AI, *this );
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001005
Ruchira Sasankaa90e7702001-10-15 16:26:38 +00001006 else if ( (TM.getInstrInfo()).isReturn(OpCode) )
1007 MRI.colorRetValue( CRMI, LRI, AI );
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001008
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001009 else assert( 0 && "Non Call/Ret instrn in CallRetInstrList\n" );
1010
1011 }
1012
1013}
1014
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001015
1016
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001017//----------------------------------------------------------------------------
1018
1019//----------------------------------------------------------------------------
1020void PhyRegAlloc::colorIncomingArgs()
1021{
1022 const BasicBlock *const FirstBB = Meth->front();
1023 const MachineInstr *FirstMI = *((FirstBB->getMachineInstrVec()).begin());
1024 assert( FirstMI && "No machine instruction in entry BB");
1025
1026 AddedInstrns *AI = AddedInstrMap[ FirstMI ];
1027 if ( !AI ) {
1028 AI = new AddedInstrns();
1029 AddedInstrMap[ FirstMI ] = AI;
1030 }
1031
1032 MRI.colorMethodArgs(Meth, LRI, AI );
1033}
1034
Ruchira Sasankae727f852001-09-18 22:43:57 +00001035
1036//----------------------------------------------------------------------------
1037// Used to generate a label for a basic block
1038//----------------------------------------------------------------------------
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001039void PhyRegAlloc::printLabel(const Value *const Val)
1040{
1041 if( Val->hasName() )
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001042 cout << Val->getName();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001043 else
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001044 cout << "Label" << Val;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001045}
1046
1047
Ruchira Sasankae727f852001-09-18 22:43:57 +00001048//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001049// This method calls setSugColorUsable method of each live range. This
1050// will determine whether the suggested color of LR is really usable.
1051// A suggested color is not usable when the suggested color is volatile
1052// AND when there are call interferences
1053//----------------------------------------------------------------------------
1054
1055void PhyRegAlloc::markUnusableSugColors()
1056{
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001057 if(DEBUG_RA ) cout << "\nmarking unusable suggested colors ..." << endl;
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001058
1059 // hash map iterator
1060 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
1061 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
1062
1063 for( ; HMI != HMIEnd ; ++HMI ) {
1064
1065 if( (*HMI).first ) {
1066
1067 LiveRange *L = (*HMI).second; // get the LiveRange
1068
1069 if(L) {
1070 if( L->hasSuggestedColor() ) {
1071
1072 int RCID = (L->getRegClass())->getID();
1073 if( MRI.isRegVolatile( RCID, L->getSuggestedColor()) &&
1074 L->isCallInterference() )
1075 L->setSuggestedColorUsable( false );
1076 else
1077 L->setSuggestedColorUsable( true );
1078 }
1079 } // if L->hasSuggestedColor()
1080 }
1081 } // for all LR's in hash map
1082}
1083
1084
1085
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001086//----------------------------------------------------------------------------
1087// The following method will set the stack offsets of the live ranges that
1088// are decided to be spillled. This must be called just after coloring the
1089// LRs using the graph coloring algo. For each live range that is spilled,
1090// this method allocate a new spill position on the stack.
1091//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001092
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001093void PhyRegAlloc::allocateStackSpace4SpilledLRs()
1094{
1095 if(DEBUG_RA ) cout << "\nsetting LR stack offsets ..." << endl;
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001096
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001097 // hash map iterator
1098 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
1099 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
1100
1101 for( ; HMI != HMIEnd ; ++HMI ) {
1102
1103 if( (*HMI).first ) {
1104 LiveRange *L = (*HMI).second; // get the LiveRange
1105 if(L)
1106 if( ! L->hasColor() )
1107 L->setSpillOffFromFP( StackOffsets.getNewSpillOffFromFP() );
1108 }
1109 } // for all LR's in hash map
1110
1111 StackOffsets.setEndOfSpillRegion();
1112
1113}
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001114
1115
1116
1117
1118
1119
1120//----------------------------------------------------------------------------
Ruchira Sasankae727f852001-09-18 22:43:57 +00001121// The entry pont to Register Allocation
1122//----------------------------------------------------------------------------
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001123
1124void PhyRegAlloc::allocateRegisters()
1125{
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001126
1127 // make sure that we put all register classes into the RegClassList
1128 // before we call constructLiveRanges (now done in the constructor of
1129 // PhyRegAlloc class).
1130
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001131 constructLiveRanges(); // create LR info
1132
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001133 if( DEBUG_RA )
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001134 LRI.printLiveRanges();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001135
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001136 createIGNodeListsAndIGs(); // create IGNode list and IGs
1137
1138 buildInterferenceGraphs(); // build IGs in all reg classes
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001139
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001140
1141 if( DEBUG_RA ) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001142 // print all LRs in all reg classes
1143 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1144 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001145
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001146 // print IGs in all register classes
1147 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1148 RegClassList[ rc ]->printIG();
1149 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001150
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001151 LRI.coalesceLRs(); // coalesce all live ranges
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001152
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001153 if( DEBUG_RA) {
1154 // print all LRs in all reg classes
1155 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1156 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001157
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001158 // print IGs in all register classes
1159 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1160 RegClassList[ rc ]->printIG();
1161 }
1162
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001163
1164 // mark un-usable suggested color before graph coloring algorithm.
1165 // When this is done, the graph coloring algo will not reserve
1166 // suggested color unnecessarily - they can be used by another LR
1167 markUnusableSugColors();
1168
1169 // color all register classes using the graph coloring algo
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001170 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1171 RegClassList[ rc ]->colorAllRegs();
1172
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001173 // Atter grpah coloring, if some LRs did not receive a color (i.e, spilled)
1174 // a poistion for such spilled LRs
1175 allocateStackSpace4SpilledLRs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001176
1177 // color incoming args and call args
1178 colorIncomingArgs();
1179 colorCallRetArgs();
1180
Ruchira Sasanka97b8b442001-10-18 22:36:26 +00001181
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001182 updateMachineCode();
Chris Lattner045e7c82001-09-19 16:26:23 +00001183 if (DEBUG_RA) {
Vikram S. Advec023be22001-10-22 13:52:03 +00001184 Meth->getMachineCode().dump();
Chris Lattner045e7c82001-09-19 16:26:23 +00001185 printMachineCode(); // only for DEBUGGING
1186 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001187}
1188
Ruchira Sasankae727f852001-09-18 22:43:57 +00001189
1190