blob: 8e4530c15d4a5cc1dd61094da44596a2b3274554 [file] [log] [blame]
Vikram S. Adve12af1642001-11-08 04:48:50 +00001// $Id$
2//***************************************************************************
3// File:
4// PhyRegAlloc.cpp
5//
6// Purpose:
7// Register allocation for LLVM.
8//
9// History:
10// 9/10/01 - Ruchira Sasanka - created.
11//**************************************************************************/
Ruchira Sasanka8e604792001-09-14 21:18:34 +000012
Vikram S. Adve12af1642001-11-08 04:48:50 +000013#include "llvm/CodeGen/PhyRegAlloc.h"
14#include "llvm/CodeGen/MachineInstr.h"
15#include "llvm/Target/TargetMachine.h"
16#include "llvm/Target/MachineFrameInfo.h"
17
18
19// ***TODO: There are several places we add instructions. Validate the order
20// of adding these instructions.
Ruchira Sasanka174bded2001-10-28 18:12:02 +000021
22
23
Chris Lattner045e7c82001-09-19 16:26:23 +000024cl::Enum<RegAllocDebugLevel_t> DEBUG_RA("dregalloc", cl::NoFlags,
25 "enable register allocation debugging information",
26 clEnumValN(RA_DEBUG_None , "n", "disable debug output"),
27 clEnumValN(RA_DEBUG_Normal , "y", "enable debug output"),
28 clEnumValN(RA_DEBUG_Verbose, "v", "enable extra debug output"), 0);
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000029
30
31//----------------------------------------------------------------------------
32// Constructor: Init local composite objects and create register classes.
33//----------------------------------------------------------------------------
Vikram S. Adve12af1642001-11-08 04:48:50 +000034PhyRegAlloc::PhyRegAlloc(Method *M,
Ruchira Sasanka8e604792001-09-14 21:18:34 +000035 const TargetMachine& tm,
36 MethodLiveVarInfo *const Lvi)
37 : RegClassList(),
Vikram S. Adve12af1642001-11-08 04:48:50 +000038 TM(tm),
39 Meth(M),
40 mcInfo(MachineCodeForMethod::get(M)),
41 LVI(Lvi), LRI(M, tm, RegClassList),
Ruchira Sasanka8e604792001-09-14 21:18:34 +000042 MRI( tm.getRegInfo() ),
43 NumOfRegClasses(MRI.getNumOfRegClasses()),
Vikram S. Adve12af1642001-11-08 04:48:50 +000044 AddedInstrMap()
45 /*, PhiInstList()*/
Ruchira Sasanka8e604792001-09-14 21:18:34 +000046{
47 // **TODO: use an actual reserved color list
48 ReservedColorListType *RCL = new ReservedColorListType();
49
50 // create each RegisterClass and put in RegClassList
51 for( unsigned int rc=0; rc < NumOfRegClasses; rc++)
52 RegClassList.push_back( new RegClass(M, MRI.getMachineRegClass(rc), RCL) );
Ruchira Sasanka8e604792001-09-14 21:18:34 +000053}
54
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000055//----------------------------------------------------------------------------
56// This method initally creates interference graphs (one in each reg class)
57// and IGNodeList (one in each IG). The actual nodes will be pushed later.
58//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000059
60void PhyRegAlloc::createIGNodeListsAndIGs()
61{
Ruchira Sasankac4d4b762001-10-16 01:23:19 +000062 if(DEBUG_RA ) cout << "Creating LR lists ..." << endl;
Ruchira Sasanka8e604792001-09-14 21:18:34 +000063
64 // hash map iterator
65 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
66
67 // hash map end
68 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
69
70 for( ; HMI != HMIEnd ; ++HMI ) {
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +000071
72 if( (*HMI).first ) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +000073
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +000074 LiveRange *L = (*HMI).second; // get the LiveRange
Ruchira Sasanka8e604792001-09-14 21:18:34 +000075
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +000076 if( !L) {
77 if( DEBUG_RA) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +000078 cout << "\n*?!?Warning: Null liver range found for: ";
79 printValue( (*HMI).first) ; cout << endl;
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +000080 }
81 continue;
82 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +000083 // if the Value * is not null, and LR
84 // is not yet written to the IGNodeList
85 if( !(L->getUserIGNode()) ) {
86
87 RegClass *const RC = // RegClass of first value in the LR
88 //RegClassList [MRI.getRegClassIDOfValue(*(L->begin()))];
89 RegClassList[ L->getRegClass()->getID() ];
90
91 RC-> addLRToIG( L ); // add this LR to an IG
92 }
93 }
94 }
95
96 // init RegClassList
97 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
98 RegClassList[ rc ]->createInterferenceGraph();
99
100 if( DEBUG_RA)
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000101 cout << "LRLists Created!" << endl;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000102}
103
104
105
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000106//----------------------------------------------------------------------------
107// This method will add all interferences at for a given instruction.
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000108// Interence occurs only if the LR of Def (Inst or Arg) is of the same reg
109// class as that of live var. The live var passed to this function is the
110// LVset AFTER the instruction
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000111//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000112
113void PhyRegAlloc::addInterference(const Value *const Def,
114 const LiveVarSet *const LVSet,
115 const bool isCallInst) {
116
117 LiveVarSet::const_iterator LIt = LVSet->begin();
118
119 // get the live range of instruction
120 const LiveRange *const LROfDef = LRI.getLiveRangeForValue( Def );
121
122 IGNode *const IGNodeOfDef = LROfDef->getUserIGNode();
123 assert( IGNodeOfDef );
124
125 RegClass *const RCOfDef = LROfDef->getRegClass();
126
127 // for each live var in live variable set
128 for( ; LIt != LVSet->end(); ++LIt) {
129
130 if( DEBUG_RA > 1) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000131 cout << "< Def="; printValue(Def);
132 cout << ", Lvar="; printValue( *LIt); cout << "> ";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000133 }
134
135 // get the live range corresponding to live var
136 LiveRange *const LROfVar = LRI.getLiveRangeForValue(*LIt );
137
138 // LROfVar can be null if it is a const since a const
139 // doesn't have a dominating def - see Assumptions above
140 if( LROfVar) {
141
142 if(LROfDef == LROfVar) // do not set interf for same LR
143 continue;
144
145 // if 2 reg classes are the same set interference
146 if( RCOfDef == LROfVar->getRegClass() ){
147 RCOfDef->setInterference( LROfDef, LROfVar);
148
149 }
150
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000151 else if(DEBUG_RA > 1) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000152 // we will not have LRs for values not explicitly allocated in the
153 // instruction stream (e.g., constants)
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000154 cout << " warning: no live range for " ;
155 printValue( *LIt); cout << endl; }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000156
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000157 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000158
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000159 }
160
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000161}
162
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000163
164//----------------------------------------------------------------------------
165// For a call instruction, this method sets the CallInterference flag in
166// the LR of each variable live int the Live Variable Set live after the
167// call instruction (except the return value of the call instruction - since
168// the return value does not interfere with that call itself).
169//----------------------------------------------------------------------------
170
171void PhyRegAlloc::setCallInterferences(const MachineInstr *MInst,
172 const LiveVarSet *const LVSetAft )
173{
174 // Now find the LR of the return value of the call
Ruchira Sasankab3b6f532001-10-21 16:43:41 +0000175
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000176
177 // We do this because, we look at the LV set *after* the instruction
178 // to determine, which LRs must be saved across calls. The return value
179 // of the call is live in this set - but it does not interfere with call
180 // (i.e., we can allocate a volatile register to the return value)
181
182 LiveRange *RetValLR = NULL;
183
Ruchira Sasankab3b6f532001-10-21 16:43:41 +0000184 const Value *RetVal = MRI.getCallInstRetVal( MInst );
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000185
Ruchira Sasankab3b6f532001-10-21 16:43:41 +0000186 if( RetVal ) {
187 RetValLR = LRI.getLiveRangeForValue( RetVal );
188 assert( RetValLR && "No LR for RetValue of call");
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000189 }
190
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000191 if( DEBUG_RA)
192 cout << "\n For call inst: " << *MInst;
193
194 LiveVarSet::const_iterator LIt = LVSetAft->begin();
195
196 // for each live var in live variable set after machine inst
197 for( ; LIt != LVSetAft->end(); ++LIt) {
198
199 // get the live range corresponding to live var
200 LiveRange *const LR = LRI.getLiveRangeForValue(*LIt );
201
202 if( LR && DEBUG_RA) {
203 cout << "\n\tLR Aft Call: ";
204 LR->printSet();
205 }
206
207
208 // LR can be null if it is a const since a const
209 // doesn't have a dominating def - see Assumptions above
210 if( LR && (LR != RetValLR) ) {
211 LR->setCallInterference();
212 if( DEBUG_RA) {
213 cout << "\n ++Added call interf for LR: " ;
214 LR->printSet();
215 }
216 }
217
218 }
219
220}
221
222
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000223//----------------------------------------------------------------------------
224// This method will walk thru code and create interferences in the IG of
225// each RegClass.
226//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000227
228void PhyRegAlloc::buildInterferenceGraphs()
229{
230
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000231 if(DEBUG_RA) cout << "Creating interference graphs ..." << endl;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000232
233 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
234
235 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
236
237 // get the iterator for machine instructions
238 const MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
239 MachineCodeForBasicBlock::const_iterator
240 MInstIterator = MIVec.begin();
241
242 // iterate over all the machine instructions in BB
243 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000244
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +0000245 const MachineInstr * MInst = *MInstIterator;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000246
247 // get the LV set after the instruction
248 const LiveVarSet *const LVSetAI =
249 LVI->getLiveVarSetAfterMInst(MInst, *BBI);
250
251 const bool isCallInst = TM.getInstrInfo().isCall(MInst->getOpCode());
252
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000253 if( isCallInst ) {
254 //cout << "\nFor call inst: " << *MInst;
255
256 // set the isCallInterference flag of each live range wich extends
257 // accross this call instruction. This information is used by graph
258 // coloring algo to avoid allocating volatile colors to live ranges
259 // that span across calls (since they have to be saved/restored)
260 setCallInterferences( MInst, LVSetAI);
261 }
262
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000263
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000264 // iterate over MI operands to find defs
265 for( MachineInstr::val_op_const_iterator OpI(MInst);!OpI.done(); ++OpI) {
266
267 if( OpI.isDef() ) {
268 // create a new LR iff this operand is a def
269 addInterference(*OpI, LVSetAI, isCallInst );
270
271 } //if this is a def
272
273 } // for all operands
274
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000275
276 // Also add interference for any implicit definitions in a machine
277 // instr (currently, only calls have this).
278
279 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
280 if( NumOfImpRefs > 0 ) {
281 for(unsigned z=0; z < NumOfImpRefs; z++)
282 if( MInst->implicitRefIsDefined(z) )
283 addInterference( MInst->getImplicitRef(z), LVSetAI, isCallInst );
284 }
285
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000286 /*
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +0000287 // record phi instrns in PhiInstList
288 if( TM.getInstrInfo().isDummyPhiInstr(MInst->getOpCode()) )
289 PhiInstList.push_back( MInst );
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000290 */
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +0000291
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000292 } // for all machine instructions in BB
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 Sasanka6b0a8b52001-09-15 21:11:11 +0000336//----------------------------------------------------------------------------
337// This method is called after register allocation is complete to set the
338// allocated reisters in the machine code. This code will add register numbers
339// to MachineOperands that contain a Value.
340//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000341
342void PhyRegAlloc::updateMachineCode()
343{
344
345 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
346
347 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
348
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000349 // get the iterator for machine instructions
350 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
351 MachineCodeForBasicBlock::iterator MInstIterator = MIVec.begin();
352
353 // iterate over all the machine instructions in BB
354 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
355
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000356 MachineInstr *MInst = *MInstIterator;
357
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000358 // if this machine instr is call, insert caller saving code
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000359
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000360 if( (TM.getInstrInfo()).isCall( MInst->getOpCode()) )
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000361 MRI.insertCallerSavingCode(MInst, *BBI, *this );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000362
363 // If there are instructions to be added, *before* this machine
364 // instruction, add them now.
365
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000366 if( AddedInstrMap[ MInst ] ) {
367
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000368 deque<MachineInstr *> &IBef = (AddedInstrMap[MInst])->InstrnsBefore;
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000369
370 if( ! IBef.empty() ) {
371
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000372 deque<MachineInstr *>::iterator AdIt;
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000373
374 for( AdIt = IBef.begin(); AdIt != IBef.end() ; ++AdIt ) {
375
Ruchira Sasankaad140092001-11-09 23:49:42 +0000376 if( DEBUG_RA )
377 cerr << " PREPENDed instr: " << **AdIt << endl;
Ruchira Sasanka97b8b442001-10-18 22:36:26 +0000378
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000379 MInstIterator = MIVec.insert( MInstIterator, *AdIt );
380 ++MInstIterator;
381 }
382
383 }
384
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000385 }
386
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000387 // reset the stack offset for temporary variables since we may
388 // need that to spill
Vikram S. Adve12af1642001-11-08 04:48:50 +0000389 mcInfo.popAllTempValues(TM);
390
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000391 //for(MachineInstr::val_op_const_iterator OpI(MInst);!OpI.done();++OpI) {
392
393 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
394
395 MachineOperand& Op = MInst->getOperand(OpNum);
396
397 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
398 Op.getOperandType() == MachineOperand::MO_CCRegister) {
399
400 const Value *const Val = Op.getVRegValue();
401
402 // delete this condition checking later (must assert if Val is null)
Chris Lattner045e7c82001-09-19 16:26:23 +0000403 if( !Val) {
404 if (DEBUG_RA)
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000405 cout << "Warning: NULL Value found for operand" << endl;
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000406 continue;
407 }
408 assert( Val && "Value is NULL");
409
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000410 LiveRange *const LR = LRI.getLiveRangeForValue(Val);
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000411
412 if ( !LR ) {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000413
414 // nothing to worry if it's a const or a label
415
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000416 if (DEBUG_RA) {
Ruchira Sasanka1b732fd2001-10-16 16:34:44 +0000417 cout << "*NO LR for operand : " << Op ;
418 cout << " [reg:" << Op.getAllocatedRegNum() << "]";
419 cout << " in inst:\t" << *MInst << endl;
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000420 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000421
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000422 // if register is not allocated, mark register as invalid
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000423 if( Op.getAllocatedRegNum() == -1)
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000424 Op.setRegForValue( MRI.getInvalidRegNum());
Ruchira Sasankae727f852001-09-18 22:43:57 +0000425
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000426
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000427 continue;
428 }
429
430 unsigned RCID = (LR->getRegClass())->getID();
431
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000432 if( LR->hasColor() ) {
433 Op.setRegForValue( MRI.getUnifiedRegNum(RCID, LR->getColor()) );
434 }
435 else {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000436
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000437 // LR did NOT receive a color (register). Now, insert spill code
438 // for spilled opeands in this machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000439
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000440 //assert(0 && "LR must be spilled");
441 insertCode4SpilledLR(LR, MInst, *BBI, OpNum );
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000442
443 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000444 }
445
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000446 } // for each operand
447
448
449 // If there are instructions to be added *after* this machine
450 // instruction, add them now
451
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000452 if( AddedInstrMap[ MInst ] &&
453 ! (AddedInstrMap[ MInst ]->InstrnsAfter).empty() ) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000454
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000455 // if there are delay slots for this instruction, the instructions
456 // added after it must really go after the delayed instruction(s)
457 // So, we move the InstrAfter of the current instruction to the
458 // corresponding delayed instruction
459
460 unsigned delay;
461 if((delay=TM.getInstrInfo().getNumDelaySlots(MInst->getOpCode())) >0){
462 move2DelayedInstr(MInst, *(MInstIterator+delay) );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000463
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000464 if(DEBUG_RA) cout<< "\nMoved an added instr after the delay slot";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000465 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000466
467 else {
468
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000469
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000470 // Here we can add the "instructions after" to the current
471 // instruction since there are no delay slots for this instruction
472
473 deque<MachineInstr *> &IAft = (AddedInstrMap[MInst])->InstrnsAfter;
474
475 if( ! IAft.empty() ) {
476
477 deque<MachineInstr *>::iterator AdIt;
478
479 ++MInstIterator; // advance to the next instruction
480
481 for( AdIt = IAft.begin(); AdIt != IAft.end() ; ++AdIt ) {
482
483 if(DEBUG_RA)
Ruchira Sasankaad140092001-11-09 23:49:42 +0000484 cerr << " APPENDed instr: " << **AdIt << endl;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000485
486 MInstIterator = MIVec.insert( MInstIterator, *AdIt );
487 ++MInstIterator;
488 }
489
490 // MInsterator already points to the next instr. Since the
491 // for loop also increments it, decrement it to point to the
492 // instruction added last
493 --MInstIterator;
494
495 }
496
497 } // if not delay
498
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000499 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000500
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000501 } // for each machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000502 }
503}
504
505
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000506
507//----------------------------------------------------------------------------
508// This method inserts spill code for AN operand whose LR was spilled.
509// This method may be called several times for a single machine instruction
510// if it contains many spilled operands. Each time it is called, it finds
511// a register which is not live at that instruction and also which is not
512// used by other spilled operands of the same instruction. Then it uses
513// this register temporarily to accomodate the spilled value.
514//----------------------------------------------------------------------------
515void PhyRegAlloc::insertCode4SpilledLR(const LiveRange *LR,
516 MachineInstr *MInst,
517 const BasicBlock *BB,
518 const unsigned OpNum) {
519
520 MachineOperand& Op = MInst->getOperand(OpNum);
521 bool isDef = MInst->operandIsDefined(OpNum);
522 unsigned RegType = MRI.getRegType( LR );
523 int SpillOff = LR->getSpillOffFromFP();
524 RegClass *RC = LR->getRegClass();
525 const LiveVarSet *LVSetBef = LVI->getLiveVarSetBeforeMInst(MInst, BB);
526 int TmpOff =
527 mcInfo.pushTempValue(TM, TM.findOptimalStorageSize(LR->getType()));
528
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000529 MachineInstr *MIBef=NULL, *AdIMid=NULL, *MIAft=NULL;
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000530 int TmpReg;
531
532 TmpReg = getUsableRegAtMI(RC, RegType, MInst,LVSetBef, MIBef, MIAft);
533 TmpReg = MRI.getUnifiedRegNum( RC->getID(), TmpReg );
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000534
535
536 // get the added instructions for this instruciton
537 AddedInstrns *AI = AddedInstrMap[ MInst ];
538 if ( !AI ) {
539 AI = new AddedInstrns();
540 AddedInstrMap[ MInst ] = AI;
541 }
542
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000543
544
545 if( !isDef ) {
546
547 // for a USE, we have to load the value of LR from stack to a TmpReg
548 // and use the TmpReg as one operand of instruction
549
550 // actual loading instruction
551 AdIMid = MRI.cpMem2RegMI(MRI.getFramePointer(), SpillOff, TmpReg, RegType);
552
553 if( MIBef )
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000554 (AI->InstrnsBefore).push_back(MIBef);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000555
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000556 (AI->InstrnsBefore).push_back(AdIMid);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000557
558 if( MIAft)
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000559 (AI->InstrnsAfter).push_front(MIAft);
560
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000561
562 }
563 else { // if this is a Def
564
565 // for a DEF, we have to store the value produced by this instruction
566 // on the stack position allocated for this LR
567
568 // actual storing instruction
569 AdIMid = MRI.cpReg2MemMI(TmpReg, MRI.getFramePointer(), SpillOff, RegType);
570
571 if( MIBef )
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000572 (AI->InstrnsBefore).push_back(MIBef);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000573
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000574 (AI->InstrnsBefore).push_back(AdIMid);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000575
576 if( MIAft)
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000577 (AI->InstrnsAfter).push_front(MIAft);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000578
579 } // if !DEF
580
581 cerr << "\nFor Inst " << *MInst;
582 cerr << "\n - SPILLED LR:"; LR->printSet();
583 cerr << "\n - Added Instructions:";
584 if( MIBef ) cerr << *MIBef;
585 cerr << *AdIMid;
586 if( MIAft ) cerr << *MIAft;
587
588 Op.setRegForValue( TmpReg ); // set the opearnd
589
590
591}
592
593
594
595
596
597
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000598//----------------------------------------------------------------------------
599// We can use the following method to get a temporary register to be used
600// BEFORE any given machine instruction. If there is a register available,
601// this method will simply return that register and set MIBef = MIAft = NULL.
602// Otherwise, it will return a register and MIAft and MIBef will contain
603// two instructions used to free up this returned register.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000604// Returned register number is the UNIFIED register number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000605//----------------------------------------------------------------------------
606
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000607int PhyRegAlloc::getUsableRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000608 const int RegType,
609 const MachineInstr *MInst,
610 const LiveVarSet *LVSetBef,
611 MachineInstr *MIBef,
612 MachineInstr *MIAft) {
613
614 int Reg = getUnusedRegAtMI(RC, MInst, LVSetBef);
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000615 Reg = MRI.getUnifiedRegNum(RC->getID(), Reg);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000616
617 if( Reg != -1) {
618 // we found an unused register, so we can simply used
619 MIBef = MIAft = NULL;
620 }
621 else {
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000622 // we couldn't find an unused register. Generate code to free up a reg by
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000623 // saving it on stack and restoring after the instruction
624
Vikram S. Adve12af1642001-11-08 04:48:50 +0000625 /**** NOTE: THIS SHOULD USE THE RIGHT SIZE FOR THE REG BEING PUSHED ****/
626 int TmpOff = mcInfo.pushTempValue(TM, /*size*/ 8);
627
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000628 Reg = getRegNotUsedByThisInst(RC, MInst);
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000629 MIBef = MRI.cpReg2MemMI(Reg, MRI.getFramePointer(), TmpOff, RegType );
630 MIAft = MRI.cpMem2RegMI(MRI.getFramePointer(), TmpOff, Reg, RegType );
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000631 }
632
633 return Reg;
634}
635
636//----------------------------------------------------------------------------
637// This method is called to get a new unused register that can be used to
638// accomodate a spilled value.
639// This method may be called several times for a single machine instruction
640// if it contains many spilled operands. Each time it is called, it finds
641// a register which is not live at that instruction and also which is not
642// used by other spilled operands of the same instruction.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000643// Return register number is relative to the register class. NOT
644// unified number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000645//----------------------------------------------------------------------------
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000646int PhyRegAlloc::getUnusedRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000647 const MachineInstr *MInst,
648 const LiveVarSet *LVSetBef) {
649
650 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
651
652 bool *IsColorUsedArr = RC->getIsColorUsedArr();
653
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000654 for(unsigned i=0; i < NumAvailRegs; i++)
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000655 IsColorUsedArr[i] = false;
656
657 LiveVarSet::const_iterator LIt = LVSetBef->begin();
658
659 // for each live var in live variable set after machine inst
660 for( ; LIt != LVSetBef->end(); ++LIt) {
661
662 // get the live range corresponding to live var
663 LiveRange *const LRofLV = LRI.getLiveRangeForValue(*LIt );
664
665 // LR can be null if it is a const since a const
666 // doesn't have a dominating def - see Assumptions above
667 if( LRofLV )
668 if( LRofLV->hasColor() )
669 IsColorUsedArr[ LRofLV->getColor() ] = true;
670 }
671
672 // It is possible that one operand of this MInst was already spilled
673 // and it received some register temporarily. If that's the case,
674 // it is recorded in machine operand. We must skip such registers.
675
676 setRegsUsedByThisInst(RC, MInst);
677
678 unsigned c; // find first unused color
679 for( c=0; c < NumAvailRegs; c++)
680 if( ! IsColorUsedArr[ c ] ) break;
681
682 if(c < NumAvailRegs)
683 return c;
684 else
685 return -1;
686
687
688}
689
690
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000691
692//----------------------------------------------------------------------------
693// This method modifies the IsColorUsedArr of the register class passed to it.
694// It sets the bits corresponding to the registers used by this machine
695// instructions. Explicit operands are set.
696//----------------------------------------------------------------------------
697void PhyRegAlloc::setRegsUsedByThisInst(RegClass *RC,
698 const MachineInstr *MInst ) {
699
700 bool *IsColorUsedArr = RC->getIsColorUsedArr();
701
702 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
703
704 const MachineOperand& Op = MInst->getOperand(OpNum);
705
706 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
707 Op.getOperandType() == MachineOperand::MO_CCRegister) {
708
709 const Value *const Val = Op.getVRegValue();
710
711 if( !Val )
712 if( MRI.getRegClassIDOfValue( Val )== RC->getID() ) {
713 int Reg;
714 if( (Reg=Op.getAllocatedRegNum()) != -1)
715 IsColorUsedArr[ Reg ] = true;
716
717 }
718 }
719 }
720
721 // If there are implicit references, mark them as well
722
723 for(unsigned z=0; z < MInst->getNumImplicitRefs(); z++) {
724
725 LiveRange *const LRofImpRef =
726 LRI.getLiveRangeForValue( MInst->getImplicitRef(z) );
727
728 if( LRofImpRef )
729 if( LRofImpRef->hasColor() )
730 IsColorUsedArr[ LRofImpRef->getColor() ] = true;
731 }
732
733
734
735}
736
737
738
739//----------------------------------------------------------------------------
740// Get any other register in a register class, other than what is used
741// by operands of a machine instruction.
742//----------------------------------------------------------------------------
743int PhyRegAlloc::getRegNotUsedByThisInst(RegClass *RC,
744 const MachineInstr *MInst) {
745
746 bool *IsColorUsedArr = RC->getIsColorUsedArr();
747 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
748
749
750 for(unsigned i=0; i < NumAvailRegs ; i++)
751 IsColorUsedArr[i] = false;
752
753 setRegsUsedByThisInst(RC, MInst);
754
755 unsigned c; // find first unused color
756 for( c=0; c < RC->getNumOfAvailRegs(); c++)
757 if( ! IsColorUsedArr[ c ] ) break;
758
759 if(c < NumAvailRegs)
760 return c;
761 else
762 assert( 0 && "FATAL: No free register could be found in reg class!!");
763
764}
765
766
767
768
769
770//----------------------------------------------------------------------------
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000771// If there are delay slots for an instruction, the instructions
772// added after it must really go after the delayed instruction(s).
773// So, we move the InstrAfter of that instruction to the
774// corresponding delayed instruction using the following method.
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000775
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000776//----------------------------------------------------------------------------
777void PhyRegAlloc:: move2DelayedInstr(const MachineInstr *OrigMI,
778 const MachineInstr *DelayedMI) {
779
780
781 // "added after" instructions of the original instr
782 deque<MachineInstr *> &OrigAft = (AddedInstrMap[OrigMI])->InstrnsAfter;
783
784 // "added instructions" of the delayed instr
785 AddedInstrns *DelayAdI = AddedInstrMap[DelayedMI];
786
787 if(! DelayAdI ) { // create a new "added after" if necessary
788 DelayAdI = new AddedInstrns();
789 AddedInstrMap[DelayedMI] = DelayAdI;
790 }
791
792 // "added after" instructions of the delayed instr
793 deque<MachineInstr *> &DelayedAft = DelayAdI->InstrnsAfter;
794
795 // go thru all the "added after instructions" of the original instruction
796 // and append them to the "addded after instructions" of the delayed
797 // instructions
798
799 deque<MachineInstr *>::iterator OrigAdIt;
800
801 for( OrigAdIt = OrigAft.begin(); OrigAdIt != OrigAft.end() ; ++OrigAdIt ) {
802 DelayedAft.push_back( *OrigAdIt );
803 }
804
805 // empty the "added after instructions" of the original instruction
806 OrigAft.clear();
807
808}
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000809
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000810//----------------------------------------------------------------------------
811// This method prints the code with registers after register allocation is
812// complete.
813//----------------------------------------------------------------------------
814void PhyRegAlloc::printMachineCode()
815{
816
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000817 cout << endl << ";************** Method ";
818 cout << Meth->getName() << " *****************" << endl;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000819
820 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
821
822 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
823
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000824 cout << endl ; printLabel( *BBI); cout << ": ";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000825
826 // get the iterator for machine instructions
827 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
828 MachineCodeForBasicBlock::iterator MInstIterator = MIVec.begin();
829
830 // iterate over all the machine instructions in BB
831 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
832
833 MachineInstr *const MInst = *MInstIterator;
834
835
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000836 cout << endl << "\t";
837 cout << TargetInstrDescriptors[MInst->getOpCode()].opCodeString;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000838
839
840 //for(MachineInstr::val_op_const_iterator OpI(MInst);!OpI.done();++OpI) {
841
842 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
843
844 MachineOperand& Op = MInst->getOperand(OpNum);
845
846 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasanka97b8b442001-10-18 22:36:26 +0000847 Op.getOperandType() == MachineOperand::MO_CCRegister /*||
848 Op.getOperandType() == MachineOperand::MO_PCRelativeDisp*/ ) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000849
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000850 const Value *const Val = Op.getVRegValue () ;
Ruchira Sasankae727f852001-09-18 22:43:57 +0000851 // ****this code is temporary till NULL Values are fixed
852 if( ! Val ) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000853 cout << "\t<*NULL*>";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000854 continue;
855 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000856
857 // if a label or a constant
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000858 if( (Val->getValueType() == Value::BasicBlockVal) ) {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000859
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000860 cout << "\t"; printLabel( Op.getVRegValue () );
Ruchira Sasankae727f852001-09-18 22:43:57 +0000861 }
862 else {
863 // else it must be a register value
864 const int RegNum = Op.getAllocatedRegNum();
865
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +0000866 cout << "\t" << "%" << MRI.getUnifiedRegName( RegNum );
Ruchira Sasankae727f852001-09-18 22:43:57 +0000867 }
868
869 }
870 else if(Op.getOperandType() == MachineOperand::MO_MachineRegister) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000871 cout << "\t" << "%" << MRI.getUnifiedRegName(Op.getMachineRegNum());
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000872 }
873
874 else
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000875 cout << "\t" << Op; // use dump field
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000876 }
877
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000878
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000879
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000880 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
881 if( NumOfImpRefs > 0 ) {
882
883 cout << "\tImplicit:";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000884
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000885 for(unsigned z=0; z < NumOfImpRefs; z++) {
886 printValue( MInst->getImplicitRef(z) );
887 cout << "\t";
888 }
889
890 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000891
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000892 } // for all machine instructions
893
894
895 cout << endl;
896
897 } // for all BBs
898
899 cout << endl;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000900}
901
Ruchira Sasankae727f852001-09-18 22:43:57 +0000902
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000903//----------------------------------------------------------------------------
904//
905//----------------------------------------------------------------------------
906
907void PhyRegAlloc::colorCallRetArgs()
908{
909
910 CallRetInstrListType &CallRetInstList = LRI.getCallRetInstrList();
911 CallRetInstrListType::const_iterator It = CallRetInstList.begin();
912
913 for( ; It != CallRetInstList.end(); ++It ) {
914
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000915 const MachineInstr *const CRMI = *It;
916 unsigned OpCode = CRMI->getOpCode();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000917
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000918 // get the added instructions for this Call/Ret instruciton
919 AddedInstrns *AI = AddedInstrMap[ CRMI ];
920 if ( !AI ) {
921 AI = new AddedInstrns();
922 AddedInstrMap[ CRMI ] = AI;
923 }
924
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000925 // Tmp stack poistions are needed by some calls that have spilled args
926 // So reset it before we call each such method
Vikram S. Adve12af1642001-11-08 04:48:50 +0000927 mcInfo.popAllTempValues(TM);
928
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000929 if( (TM.getInstrInfo()).isCall( OpCode ) )
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000930 MRI.colorCallArgs( CRMI, LRI, AI, *this );
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000931
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000932 else if ( (TM.getInstrInfo()).isReturn(OpCode) )
933 MRI.colorRetValue( CRMI, LRI, AI );
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000934
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000935 else assert( 0 && "Non Call/Ret instrn in CallRetInstrList\n" );
936
937 }
938
939}
940
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +0000941
942
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000943//----------------------------------------------------------------------------
944
945//----------------------------------------------------------------------------
946void PhyRegAlloc::colorIncomingArgs()
947{
948 const BasicBlock *const FirstBB = Meth->front();
949 const MachineInstr *FirstMI = *((FirstBB->getMachineInstrVec()).begin());
950 assert( FirstMI && "No machine instruction in entry BB");
951
952 AddedInstrns *AI = AddedInstrMap[ FirstMI ];
953 if ( !AI ) {
954 AI = new AddedInstrns();
955 AddedInstrMap[ FirstMI ] = AI;
956 }
957
958 MRI.colorMethodArgs(Meth, LRI, AI );
959}
960
Ruchira Sasankae727f852001-09-18 22:43:57 +0000961
962//----------------------------------------------------------------------------
963// Used to generate a label for a basic block
964//----------------------------------------------------------------------------
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000965void PhyRegAlloc::printLabel(const Value *const Val)
966{
967 if( Val->hasName() )
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000968 cout << Val->getName();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000969 else
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000970 cout << "Label" << Val;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000971}
972
973
Ruchira Sasankae727f852001-09-18 22:43:57 +0000974//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +0000975// This method calls setSugColorUsable method of each live range. This
976// will determine whether the suggested color of LR is really usable.
977// A suggested color is not usable when the suggested color is volatile
978// AND when there are call interferences
979//----------------------------------------------------------------------------
980
981void PhyRegAlloc::markUnusableSugColors()
982{
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000983 if(DEBUG_RA ) cout << "\nmarking unusable suggested colors ..." << endl;
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +0000984
985 // hash map iterator
986 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
987 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
988
989 for( ; HMI != HMIEnd ; ++HMI ) {
990
991 if( (*HMI).first ) {
992
993 LiveRange *L = (*HMI).second; // get the LiveRange
994
995 if(L) {
996 if( L->hasSuggestedColor() ) {
997
998 int RCID = (L->getRegClass())->getID();
999 if( MRI.isRegVolatile( RCID, L->getSuggestedColor()) &&
1000 L->isCallInterference() )
1001 L->setSuggestedColorUsable( false );
1002 else
1003 L->setSuggestedColorUsable( true );
1004 }
1005 } // if L->hasSuggestedColor()
1006 }
1007 } // for all LR's in hash map
1008}
1009
1010
1011
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001012//----------------------------------------------------------------------------
1013// The following method will set the stack offsets of the live ranges that
1014// are decided to be spillled. This must be called just after coloring the
1015// LRs using the graph coloring algo. For each live range that is spilled,
1016// this method allocate a new spill position on the stack.
1017//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001018
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001019void PhyRegAlloc::allocateStackSpace4SpilledLRs()
1020{
1021 if(DEBUG_RA ) cout << "\nsetting LR stack offsets ..." << endl;
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001022
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001023 // hash map iterator
1024 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
1025 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
1026
1027 for( ; HMI != HMIEnd ; ++HMI ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001028 if( (*HMI).first ) {
1029 LiveRange *L = (*HMI).second; // get the LiveRange
1030 if(L)
1031 if( ! L->hasColor() )
Vikram S. Adve12af1642001-11-08 04:48:50 +00001032 L->setSpillOffFromFP(mcInfo.allocateSpilledValue(TM,L->getType()));
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001033 }
1034 } // for all LR's in hash map
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001035}
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001036
1037
1038
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001039//----------------------------------------------------------------------------
Ruchira Sasankae727f852001-09-18 22:43:57 +00001040// The entry pont to Register Allocation
1041//----------------------------------------------------------------------------
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001042
1043void PhyRegAlloc::allocateRegisters()
1044{
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001045
1046 // make sure that we put all register classes into the RegClassList
1047 // before we call constructLiveRanges (now done in the constructor of
1048 // PhyRegAlloc class).
1049
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001050 constructLiveRanges(); // create LR info
1051
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001052 if( DEBUG_RA )
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001053 LRI.printLiveRanges();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001054
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001055 createIGNodeListsAndIGs(); // create IGNode list and IGs
1056
1057 buildInterferenceGraphs(); // build IGs in all reg classes
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001058
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001059
1060 if( DEBUG_RA ) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001061 // print all LRs in all reg classes
1062 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1063 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001064
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001065 // print IGs in all register classes
1066 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1067 RegClassList[ rc ]->printIG();
1068 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001069
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001070 LRI.coalesceLRs(); // coalesce all live ranges
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001071
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +00001072 // coalscing could not get rid of all phi's, add phi elimination
1073 // instructions
1074 // insertPhiEleminateInstrns();
1075
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001076 if( DEBUG_RA) {
1077 // print all LRs in all reg classes
1078 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1079 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001080
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001081 // print IGs in all register classes
1082 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1083 RegClassList[ rc ]->printIG();
1084 }
1085
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001086
1087 // mark un-usable suggested color before graph coloring algorithm.
1088 // When this is done, the graph coloring algo will not reserve
1089 // suggested color unnecessarily - they can be used by another LR
1090 markUnusableSugColors();
1091
1092 // color all register classes using the graph coloring algo
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001093 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1094 RegClassList[ rc ]->colorAllRegs();
1095
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001096 // Atter grpah coloring, if some LRs did not receive a color (i.e, spilled)
1097 // a poistion for such spilled LRs
1098 allocateStackSpace4SpilledLRs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001099
1100 // color incoming args and call args
1101 colorIncomingArgs();
1102 colorCallRetArgs();
1103
Ruchira Sasanka97b8b442001-10-18 22:36:26 +00001104
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001105 updateMachineCode();
Chris Lattner045e7c82001-09-19 16:26:23 +00001106 if (DEBUG_RA) {
Vikram S. Adve12af1642001-11-08 04:48:50 +00001107 MachineCodeForMethod::get(Meth).dump();
Chris Lattner045e7c82001-09-19 16:26:23 +00001108 printMachineCode(); // only for DEBUGGING
1109 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001110}
1111
Ruchira Sasankae727f852001-09-18 22:43:57 +00001112
1113