blob: bc825657344bde7af85a915bb8a89a5f9895d1e0 [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 Sasanka65480b72001-11-10 21:21:36 +0000358 // do not process Phis
359 if( (TM.getInstrInfo()).isPhi( MInst->getOpCode()) )
360 continue;
361
362
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000363 // if this machine instr is call, insert caller saving code
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000364
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000365 if( (TM.getInstrInfo()).isCall( MInst->getOpCode()) )
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000366 MRI.insertCallerSavingCode(MInst, *BBI, *this );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000367
368 // If there are instructions to be added, *before* this machine
369 // instruction, add them now.
370
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000371 if( AddedInstrMap[ MInst ] ) {
372
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000373 deque<MachineInstr *> &IBef = (AddedInstrMap[MInst])->InstrnsBefore;
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000374
375 if( ! IBef.empty() ) {
376
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000377 deque<MachineInstr *>::iterator AdIt;
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000378
379 for( AdIt = IBef.begin(); AdIt != IBef.end() ; ++AdIt ) {
380
Ruchira Sasankaad140092001-11-09 23:49:42 +0000381 if( DEBUG_RA )
382 cerr << " PREPENDed instr: " << **AdIt << endl;
Ruchira Sasanka97b8b442001-10-18 22:36:26 +0000383
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000384 MInstIterator = MIVec.insert( MInstIterator, *AdIt );
385 ++MInstIterator;
386 }
387
388 }
389
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000390 }
391
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000392 // reset the stack offset for temporary variables since we may
393 // need that to spill
Vikram S. Adve12af1642001-11-08 04:48:50 +0000394 mcInfo.popAllTempValues(TM);
395
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000396 //for(MachineInstr::val_op_const_iterator OpI(MInst);!OpI.done();++OpI) {
397
398 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
399
400 MachineOperand& Op = MInst->getOperand(OpNum);
401
402 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
403 Op.getOperandType() == MachineOperand::MO_CCRegister) {
404
405 const Value *const Val = Op.getVRegValue();
406
407 // delete this condition checking later (must assert if Val is null)
Chris Lattner045e7c82001-09-19 16:26:23 +0000408 if( !Val) {
409 if (DEBUG_RA)
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000410 cout << "Warning: NULL Value found for operand" << endl;
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000411 continue;
412 }
413 assert( Val && "Value is NULL");
414
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000415 LiveRange *const LR = LRI.getLiveRangeForValue(Val);
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000416
417 if ( !LR ) {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000418
419 // nothing to worry if it's a const or a label
420
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000421 if (DEBUG_RA) {
Ruchira Sasanka1b732fd2001-10-16 16:34:44 +0000422 cout << "*NO LR for operand : " << Op ;
423 cout << " [reg:" << Op.getAllocatedRegNum() << "]";
424 cout << " in inst:\t" << *MInst << endl;
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000425 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000426
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000427 // if register is not allocated, mark register as invalid
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000428 if( Op.getAllocatedRegNum() == -1)
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000429 Op.setRegForValue( MRI.getInvalidRegNum());
Ruchira Sasankae727f852001-09-18 22:43:57 +0000430
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000431
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000432 continue;
433 }
434
435 unsigned RCID = (LR->getRegClass())->getID();
436
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000437 if( LR->hasColor() ) {
438 Op.setRegForValue( MRI.getUnifiedRegNum(RCID, LR->getColor()) );
439 }
440 else {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000441
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000442 // LR did NOT receive a color (register). Now, insert spill code
443 // for spilled opeands in this machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000444
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000445 //assert(0 && "LR must be spilled");
446 insertCode4SpilledLR(LR, MInst, *BBI, OpNum );
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000447
448 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000449 }
450
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000451 } // for each operand
452
453
454 // If there are instructions to be added *after* this machine
455 // instruction, add them now
456
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000457 if( AddedInstrMap[ MInst ] &&
458 ! (AddedInstrMap[ MInst ]->InstrnsAfter).empty() ) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000459
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000460 // if there are delay slots for this instruction, the instructions
461 // added after it must really go after the delayed instruction(s)
462 // So, we move the InstrAfter of the current instruction to the
463 // corresponding delayed instruction
464
465 unsigned delay;
466 if((delay=TM.getInstrInfo().getNumDelaySlots(MInst->getOpCode())) >0){
467 move2DelayedInstr(MInst, *(MInstIterator+delay) );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000468
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000469 if(DEBUG_RA) cout<< "\nMoved an added instr after the delay slot";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000470 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000471
472 else {
473
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000474
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000475 // Here we can add the "instructions after" to the current
476 // instruction since there are no delay slots for this instruction
477
478 deque<MachineInstr *> &IAft = (AddedInstrMap[MInst])->InstrnsAfter;
479
480 if( ! IAft.empty() ) {
481
482 deque<MachineInstr *>::iterator AdIt;
483
484 ++MInstIterator; // advance to the next instruction
485
486 for( AdIt = IAft.begin(); AdIt != IAft.end() ; ++AdIt ) {
487
488 if(DEBUG_RA)
Ruchira Sasankaad140092001-11-09 23:49:42 +0000489 cerr << " APPENDed instr: " << **AdIt << endl;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000490
491 MInstIterator = MIVec.insert( MInstIterator, *AdIt );
492 ++MInstIterator;
493 }
494
495 // MInsterator already points to the next instr. Since the
496 // for loop also increments it, decrement it to point to the
497 // instruction added last
498 --MInstIterator;
499
500 }
501
502 } // if not delay
503
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000504 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000505
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000506 } // for each machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000507 }
508}
509
510
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000511
512//----------------------------------------------------------------------------
513// This method inserts spill code for AN operand whose LR was spilled.
514// This method may be called several times for a single machine instruction
515// if it contains many spilled operands. Each time it is called, it finds
516// a register which is not live at that instruction and also which is not
517// used by other spilled operands of the same instruction. Then it uses
518// this register temporarily to accomodate the spilled value.
519//----------------------------------------------------------------------------
520void PhyRegAlloc::insertCode4SpilledLR(const LiveRange *LR,
521 MachineInstr *MInst,
522 const BasicBlock *BB,
523 const unsigned OpNum) {
524
525 MachineOperand& Op = MInst->getOperand(OpNum);
526 bool isDef = MInst->operandIsDefined(OpNum);
527 unsigned RegType = MRI.getRegType( LR );
528 int SpillOff = LR->getSpillOffFromFP();
529 RegClass *RC = LR->getRegClass();
530 const LiveVarSet *LVSetBef = LVI->getLiveVarSetBeforeMInst(MInst, BB);
531 int TmpOff =
532 mcInfo.pushTempValue(TM, TM.findOptimalStorageSize(LR->getType()));
533
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000534 MachineInstr *MIBef=NULL, *AdIMid=NULL, *MIAft=NULL;
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000535 int TmpReg;
536
537 TmpReg = getUsableRegAtMI(RC, RegType, MInst,LVSetBef, MIBef, MIAft);
538 TmpReg = MRI.getUnifiedRegNum( RC->getID(), TmpReg );
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000539
540
541 // get the added instructions for this instruciton
542 AddedInstrns *AI = AddedInstrMap[ MInst ];
543 if ( !AI ) {
544 AI = new AddedInstrns();
545 AddedInstrMap[ MInst ] = AI;
546 }
547
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000548
549
550 if( !isDef ) {
551
552 // for a USE, we have to load the value of LR from stack to a TmpReg
553 // and use the TmpReg as one operand of instruction
554
555 // actual loading instruction
556 AdIMid = MRI.cpMem2RegMI(MRI.getFramePointer(), SpillOff, TmpReg, RegType);
557
558 if( MIBef )
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000559 (AI->InstrnsBefore).push_back(MIBef);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000560
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000561 (AI->InstrnsBefore).push_back(AdIMid);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000562
563 if( MIAft)
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000564 (AI->InstrnsAfter).push_front(MIAft);
565
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000566
567 }
568 else { // if this is a Def
569
570 // for a DEF, we have to store the value produced by this instruction
571 // on the stack position allocated for this LR
572
573 // actual storing instruction
574 AdIMid = MRI.cpReg2MemMI(TmpReg, MRI.getFramePointer(), SpillOff, RegType);
575
576 if( MIBef )
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000577 (AI->InstrnsBefore).push_back(MIBef);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000578
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000579 (AI->InstrnsBefore).push_back(AdIMid);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000580
581 if( MIAft)
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000582 (AI->InstrnsAfter).push_front(MIAft);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000583
584 } // if !DEF
585
586 cerr << "\nFor Inst " << *MInst;
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000587 cerr << " - SPILLED LR: "; LR->printSet();
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000588 cerr << "\n - Added Instructions:";
589 if( MIBef ) cerr << *MIBef;
590 cerr << *AdIMid;
591 if( MIAft ) cerr << *MIAft;
592
593 Op.setRegForValue( TmpReg ); // set the opearnd
594
595
596}
597
598
599
600
601
602
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000603//----------------------------------------------------------------------------
604// We can use the following method to get a temporary register to be used
605// BEFORE any given machine instruction. If there is a register available,
606// this method will simply return that register and set MIBef = MIAft = NULL.
607// Otherwise, it will return a register and MIAft and MIBef will contain
608// two instructions used to free up this returned register.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000609// Returned register number is the UNIFIED register number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000610//----------------------------------------------------------------------------
611
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000612int PhyRegAlloc::getUsableRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000613 const int RegType,
614 const MachineInstr *MInst,
615 const LiveVarSet *LVSetBef,
616 MachineInstr *MIBef,
617 MachineInstr *MIAft) {
618
619 int Reg = getUnusedRegAtMI(RC, MInst, LVSetBef);
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000620 Reg = MRI.getUnifiedRegNum(RC->getID(), Reg);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000621
622 if( Reg != -1) {
623 // we found an unused register, so we can simply used
624 MIBef = MIAft = NULL;
625 }
626 else {
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000627 // we couldn't find an unused register. Generate code to free up a reg by
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000628 // saving it on stack and restoring after the instruction
629
Vikram S. Adve12af1642001-11-08 04:48:50 +0000630 /**** NOTE: THIS SHOULD USE THE RIGHT SIZE FOR THE REG BEING PUSHED ****/
631 int TmpOff = mcInfo.pushTempValue(TM, /*size*/ 8);
632
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000633 Reg = getRegNotUsedByThisInst(RC, MInst);
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000634 MIBef = MRI.cpReg2MemMI(Reg, MRI.getFramePointer(), TmpOff, RegType );
635 MIAft = MRI.cpMem2RegMI(MRI.getFramePointer(), TmpOff, Reg, RegType );
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000636 }
637
638 return Reg;
639}
640
641//----------------------------------------------------------------------------
642// This method is called to get a new unused register that can be used to
643// accomodate a spilled value.
644// This method may be called several times for a single machine instruction
645// if it contains many spilled operands. Each time it is called, it finds
646// a register which is not live at that instruction and also which is not
647// used by other spilled operands of the same instruction.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000648// Return register number is relative to the register class. NOT
649// unified number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000650//----------------------------------------------------------------------------
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000651int PhyRegAlloc::getUnusedRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000652 const MachineInstr *MInst,
653 const LiveVarSet *LVSetBef) {
654
655 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
656
657 bool *IsColorUsedArr = RC->getIsColorUsedArr();
658
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000659 for(unsigned i=0; i < NumAvailRegs; i++)
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000660 IsColorUsedArr[i] = false;
661
662 LiveVarSet::const_iterator LIt = LVSetBef->begin();
663
664 // for each live var in live variable set after machine inst
665 for( ; LIt != LVSetBef->end(); ++LIt) {
666
667 // get the live range corresponding to live var
668 LiveRange *const LRofLV = LRI.getLiveRangeForValue(*LIt );
669
670 // LR can be null if it is a const since a const
671 // doesn't have a dominating def - see Assumptions above
672 if( LRofLV )
673 if( LRofLV->hasColor() )
674 IsColorUsedArr[ LRofLV->getColor() ] = true;
675 }
676
677 // It is possible that one operand of this MInst was already spilled
678 // and it received some register temporarily. If that's the case,
679 // it is recorded in machine operand. We must skip such registers.
680
681 setRegsUsedByThisInst(RC, MInst);
682
683 unsigned c; // find first unused color
684 for( c=0; c < NumAvailRegs; c++)
685 if( ! IsColorUsedArr[ c ] ) break;
686
687 if(c < NumAvailRegs)
688 return c;
689 else
690 return -1;
691
692
693}
694
695
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000696
697//----------------------------------------------------------------------------
698// This method modifies the IsColorUsedArr of the register class passed to it.
699// It sets the bits corresponding to the registers used by this machine
700// instructions. Explicit operands are set.
701//----------------------------------------------------------------------------
702void PhyRegAlloc::setRegsUsedByThisInst(RegClass *RC,
703 const MachineInstr *MInst ) {
704
705 bool *IsColorUsedArr = RC->getIsColorUsedArr();
706
707 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
708
709 const MachineOperand& Op = MInst->getOperand(OpNum);
710
711 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
712 Op.getOperandType() == MachineOperand::MO_CCRegister) {
713
714 const Value *const Val = Op.getVRegValue();
715
716 if( !Val )
717 if( MRI.getRegClassIDOfValue( Val )== RC->getID() ) {
718 int Reg;
719 if( (Reg=Op.getAllocatedRegNum()) != -1)
720 IsColorUsedArr[ Reg ] = true;
721
722 }
723 }
724 }
725
726 // If there are implicit references, mark them as well
727
728 for(unsigned z=0; z < MInst->getNumImplicitRefs(); z++) {
729
730 LiveRange *const LRofImpRef =
731 LRI.getLiveRangeForValue( MInst->getImplicitRef(z) );
732
733 if( LRofImpRef )
734 if( LRofImpRef->hasColor() )
735 IsColorUsedArr[ LRofImpRef->getColor() ] = true;
736 }
737
738
739
740}
741
742
743
744//----------------------------------------------------------------------------
745// Get any other register in a register class, other than what is used
746// by operands of a machine instruction.
747//----------------------------------------------------------------------------
748int PhyRegAlloc::getRegNotUsedByThisInst(RegClass *RC,
749 const MachineInstr *MInst) {
750
751 bool *IsColorUsedArr = RC->getIsColorUsedArr();
752 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
753
754
755 for(unsigned i=0; i < NumAvailRegs ; i++)
756 IsColorUsedArr[i] = false;
757
758 setRegsUsedByThisInst(RC, MInst);
759
760 unsigned c; // find first unused color
761 for( c=0; c < RC->getNumOfAvailRegs(); c++)
762 if( ! IsColorUsedArr[ c ] ) break;
763
764 if(c < NumAvailRegs)
765 return c;
766 else
767 assert( 0 && "FATAL: No free register could be found in reg class!!");
768
769}
770
771
772
773
774
775//----------------------------------------------------------------------------
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000776// If there are delay slots for an instruction, the instructions
777// added after it must really go after the delayed instruction(s).
778// So, we move the InstrAfter of that instruction to the
779// corresponding delayed instruction using the following method.
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000780
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000781//----------------------------------------------------------------------------
782void PhyRegAlloc:: move2DelayedInstr(const MachineInstr *OrigMI,
783 const MachineInstr *DelayedMI) {
784
785
786 // "added after" instructions of the original instr
787 deque<MachineInstr *> &OrigAft = (AddedInstrMap[OrigMI])->InstrnsAfter;
788
789 // "added instructions" of the delayed instr
790 AddedInstrns *DelayAdI = AddedInstrMap[DelayedMI];
791
792 if(! DelayAdI ) { // create a new "added after" if necessary
793 DelayAdI = new AddedInstrns();
794 AddedInstrMap[DelayedMI] = DelayAdI;
795 }
796
797 // "added after" instructions of the delayed instr
798 deque<MachineInstr *> &DelayedAft = DelayAdI->InstrnsAfter;
799
800 // go thru all the "added after instructions" of the original instruction
801 // and append them to the "addded after instructions" of the delayed
802 // instructions
803
804 deque<MachineInstr *>::iterator OrigAdIt;
805
806 for( OrigAdIt = OrigAft.begin(); OrigAdIt != OrigAft.end() ; ++OrigAdIt ) {
807 DelayedAft.push_back( *OrigAdIt );
808 }
809
810 // empty the "added after instructions" of the original instruction
811 OrigAft.clear();
812
813}
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000814
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000815//----------------------------------------------------------------------------
816// This method prints the code with registers after register allocation is
817// complete.
818//----------------------------------------------------------------------------
819void PhyRegAlloc::printMachineCode()
820{
821
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000822 cout << endl << ";************** Method ";
823 cout << Meth->getName() << " *****************" << endl;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000824
825 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
826
827 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
828
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000829 cout << endl ; printLabel( *BBI); cout << ": ";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000830
831 // get the iterator for machine instructions
832 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
833 MachineCodeForBasicBlock::iterator MInstIterator = MIVec.begin();
834
835 // iterate over all the machine instructions in BB
836 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
837
838 MachineInstr *const MInst = *MInstIterator;
839
840
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000841 cout << endl << "\t";
842 cout << TargetInstrDescriptors[MInst->getOpCode()].opCodeString;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000843
844
845 //for(MachineInstr::val_op_const_iterator OpI(MInst);!OpI.done();++OpI) {
846
847 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
848
849 MachineOperand& Op = MInst->getOperand(OpNum);
850
851 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasanka97b8b442001-10-18 22:36:26 +0000852 Op.getOperandType() == MachineOperand::MO_CCRegister /*||
853 Op.getOperandType() == MachineOperand::MO_PCRelativeDisp*/ ) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000854
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000855 const Value *const Val = Op.getVRegValue () ;
Ruchira Sasankae727f852001-09-18 22:43:57 +0000856 // ****this code is temporary till NULL Values are fixed
857 if( ! Val ) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000858 cout << "\t<*NULL*>";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000859 continue;
860 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000861
862 // if a label or a constant
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000863 if( (Val->getValueType() == Value::BasicBlockVal) ) {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000864
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000865 cout << "\t"; printLabel( Op.getVRegValue () );
Ruchira Sasankae727f852001-09-18 22:43:57 +0000866 }
867 else {
868 // else it must be a register value
869 const int RegNum = Op.getAllocatedRegNum();
870
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +0000871 cout << "\t" << "%" << MRI.getUnifiedRegName( RegNum );
Ruchira Sasankae727f852001-09-18 22:43:57 +0000872 }
873
874 }
875 else if(Op.getOperandType() == MachineOperand::MO_MachineRegister) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000876 cout << "\t" << "%" << MRI.getUnifiedRegName(Op.getMachineRegNum());
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000877 }
878
879 else
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000880 cout << "\t" << Op; // use dump field
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000881 }
882
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000883
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000884
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000885 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
886 if( NumOfImpRefs > 0 ) {
887
888 cout << "\tImplicit:";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000889
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000890 for(unsigned z=0; z < NumOfImpRefs; z++) {
891 printValue( MInst->getImplicitRef(z) );
892 cout << "\t";
893 }
894
895 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000896
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000897 } // for all machine instructions
898
899
900 cout << endl;
901
902 } // for all BBs
903
904 cout << endl;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000905}
906
Ruchira Sasankae727f852001-09-18 22:43:57 +0000907
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000908//----------------------------------------------------------------------------
909//
910//----------------------------------------------------------------------------
911
912void PhyRegAlloc::colorCallRetArgs()
913{
914
915 CallRetInstrListType &CallRetInstList = LRI.getCallRetInstrList();
916 CallRetInstrListType::const_iterator It = CallRetInstList.begin();
917
918 for( ; It != CallRetInstList.end(); ++It ) {
919
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000920 const MachineInstr *const CRMI = *It;
921 unsigned OpCode = CRMI->getOpCode();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000922
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000923 // get the added instructions for this Call/Ret instruciton
924 AddedInstrns *AI = AddedInstrMap[ CRMI ];
925 if ( !AI ) {
926 AI = new AddedInstrns();
927 AddedInstrMap[ CRMI ] = AI;
928 }
929
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000930 // Tmp stack poistions are needed by some calls that have spilled args
931 // So reset it before we call each such method
Vikram S. Adve12af1642001-11-08 04:48:50 +0000932 mcInfo.popAllTempValues(TM);
933
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000934 if( (TM.getInstrInfo()).isCall( OpCode ) )
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000935 MRI.colorCallArgs( CRMI, LRI, AI, *this );
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000936
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000937 else if ( (TM.getInstrInfo()).isReturn(OpCode) )
938 MRI.colorRetValue( CRMI, LRI, AI );
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000939
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000940 else assert( 0 && "Non Call/Ret instrn in CallRetInstrList\n" );
941
942 }
943
944}
945
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +0000946
947
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000948//----------------------------------------------------------------------------
949
950//----------------------------------------------------------------------------
951void PhyRegAlloc::colorIncomingArgs()
952{
953 const BasicBlock *const FirstBB = Meth->front();
954 const MachineInstr *FirstMI = *((FirstBB->getMachineInstrVec()).begin());
955 assert( FirstMI && "No machine instruction in entry BB");
956
957 AddedInstrns *AI = AddedInstrMap[ FirstMI ];
958 if ( !AI ) {
959 AI = new AddedInstrns();
960 AddedInstrMap[ FirstMI ] = AI;
961 }
962
963 MRI.colorMethodArgs(Meth, LRI, AI );
964}
965
Ruchira Sasankae727f852001-09-18 22:43:57 +0000966
967//----------------------------------------------------------------------------
968// Used to generate a label for a basic block
969//----------------------------------------------------------------------------
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000970void PhyRegAlloc::printLabel(const Value *const Val)
971{
972 if( Val->hasName() )
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000973 cout << Val->getName();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000974 else
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000975 cout << "Label" << Val;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000976}
977
978
Ruchira Sasankae727f852001-09-18 22:43:57 +0000979//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +0000980// This method calls setSugColorUsable method of each live range. This
981// will determine whether the suggested color of LR is really usable.
982// A suggested color is not usable when the suggested color is volatile
983// AND when there are call interferences
984//----------------------------------------------------------------------------
985
986void PhyRegAlloc::markUnusableSugColors()
987{
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000988 if(DEBUG_RA ) cout << "\nmarking unusable suggested colors ..." << endl;
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +0000989
990 // hash map iterator
991 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
992 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
993
994 for( ; HMI != HMIEnd ; ++HMI ) {
995
996 if( (*HMI).first ) {
997
998 LiveRange *L = (*HMI).second; // get the LiveRange
999
1000 if(L) {
1001 if( L->hasSuggestedColor() ) {
1002
1003 int RCID = (L->getRegClass())->getID();
1004 if( MRI.isRegVolatile( RCID, L->getSuggestedColor()) &&
1005 L->isCallInterference() )
1006 L->setSuggestedColorUsable( false );
1007 else
1008 L->setSuggestedColorUsable( true );
1009 }
1010 } // if L->hasSuggestedColor()
1011 }
1012 } // for all LR's in hash map
1013}
1014
1015
1016
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001017//----------------------------------------------------------------------------
1018// The following method will set the stack offsets of the live ranges that
1019// are decided to be spillled. This must be called just after coloring the
1020// LRs using the graph coloring algo. For each live range that is spilled,
1021// this method allocate a new spill position on the stack.
1022//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001023
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001024void PhyRegAlloc::allocateStackSpace4SpilledLRs()
1025{
1026 if(DEBUG_RA ) cout << "\nsetting LR stack offsets ..." << endl;
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001027
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001028 // hash map iterator
1029 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
1030 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
1031
1032 for( ; HMI != HMIEnd ; ++HMI ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001033 if( (*HMI).first ) {
1034 LiveRange *L = (*HMI).second; // get the LiveRange
1035 if(L)
1036 if( ! L->hasColor() )
Vikram S. Adve12af1642001-11-08 04:48:50 +00001037 L->setSpillOffFromFP(mcInfo.allocateSpilledValue(TM,L->getType()));
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001038 }
1039 } // for all LR's in hash map
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001040}
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001041
1042
1043
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001044//----------------------------------------------------------------------------
Ruchira Sasankae727f852001-09-18 22:43:57 +00001045// The entry pont to Register Allocation
1046//----------------------------------------------------------------------------
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001047
1048void PhyRegAlloc::allocateRegisters()
1049{
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001050
1051 // make sure that we put all register classes into the RegClassList
1052 // before we call constructLiveRanges (now done in the constructor of
1053 // PhyRegAlloc class).
1054
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001055 constructLiveRanges(); // create LR info
1056
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001057 if( DEBUG_RA )
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001058 LRI.printLiveRanges();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001059
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001060 createIGNodeListsAndIGs(); // create IGNode list and IGs
1061
1062 buildInterferenceGraphs(); // build IGs in all reg classes
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001063
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001064
1065 if( DEBUG_RA ) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001066 // print all LRs in all reg classes
1067 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1068 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001069
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001070 // print IGs in all register classes
1071 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1072 RegClassList[ rc ]->printIG();
1073 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001074
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001075 LRI.coalesceLRs(); // coalesce all live ranges
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001076
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +00001077 // coalscing could not get rid of all phi's, add phi elimination
1078 // instructions
1079 // insertPhiEleminateInstrns();
1080
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001081 if( DEBUG_RA) {
1082 // print all LRs in all reg classes
1083 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1084 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001085
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001086 // print IGs in all register classes
1087 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1088 RegClassList[ rc ]->printIG();
1089 }
1090
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001091
1092 // mark un-usable suggested color before graph coloring algorithm.
1093 // When this is done, the graph coloring algo will not reserve
1094 // suggested color unnecessarily - they can be used by another LR
1095 markUnusableSugColors();
1096
1097 // color all register classes using the graph coloring algo
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001098 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1099 RegClassList[ rc ]->colorAllRegs();
1100
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001101 // Atter grpah coloring, if some LRs did not receive a color (i.e, spilled)
1102 // a poistion for such spilled LRs
1103 allocateStackSpace4SpilledLRs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001104
1105 // color incoming args and call args
1106 colorIncomingArgs();
1107 colorCallRetArgs();
1108
Ruchira Sasanka97b8b442001-10-18 22:36:26 +00001109
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001110 updateMachineCode();
Chris Lattner045e7c82001-09-19 16:26:23 +00001111 if (DEBUG_RA) {
Vikram S. Adve12af1642001-11-08 04:48:50 +00001112 MachineCodeForMethod::get(Meth).dump();
Chris Lattner045e7c82001-09-19 16:26:23 +00001113 printMachineCode(); // only for DEBUGGING
1114 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001115}
1116
Ruchira Sasankae727f852001-09-18 22:43:57 +00001117
1118