blob: e2117bec723b790a8fd7bac319f70d9896179f4a [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()
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +000045
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) {
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000266
267 if( OpI.isDef() ) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000268 // create a new LR iff this operand is a def
269 addInterference(*OpI, LVSetAI, isCallInst );
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000270 } //if this is a def
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000271 } // for all operands
272
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000273
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000274 // if there are multiple defs in this instruction e.g. in SETX
275 //
276 if( (TM.getInstrInfo()).isPseudoInstr( MInst->getOpCode()) )
277 addInterf4PseudoInstr(MInst);
278
279
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000280 // Also add interference for any implicit definitions in a machine
281 // instr (currently, only calls have this).
282
283 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
284 if( NumOfImpRefs > 0 ) {
285 for(unsigned z=0; z < NumOfImpRefs; z++)
286 if( MInst->implicitRefIsDefined(z) )
287 addInterference( MInst->getImplicitRef(z), LVSetAI, isCallInst );
288 }
289
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000290 /*
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +0000291 // record phi instrns in PhiInstList
292 if( TM.getInstrInfo().isDummyPhiInstr(MInst->getOpCode()) )
293 PhiInstList.push_back( MInst );
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000294 */
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +0000295
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000296 } // for all machine instructions in BB
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000297
298 } // for all BBs in method
299
300
301 // add interferences for method arguments. Since there are no explict
302 // defs in method for args, we have to add them manually
303
304 addInterferencesForArgs(); // add interference for method args
305
306 if( DEBUG_RA)
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000307 cout << "Interference graphs calculted!" << endl;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000308
309}
310
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000311//--------------------------------------------------------------------------
312// Pseudo instructions will be exapnded to multiple instructions by the
313// assembler. Consequently, all the opernds must get distinct registers
314//--------------------------------------------------------------------------
315
316void PhyRegAlloc::addInterf4PseudoInstr(const MachineInstr *MInst) {
317
318 // iterate over MI operands to find defs
319 for( MachineInstr::val_op_const_iterator It1(MInst);!It1.done(); ++It1) {
320
321 const LiveRange *const LROfOp1 = LRI.getLiveRangeForValue( *It1 );
322
323 if( !LROfOp1 ) continue;
324
325 MachineInstr::val_op_const_iterator It2 = It1;
326 ++It2;
327
328 for( ; !It2.done(); ++It2) {
329
330 const LiveRange *const LROfOp2 = LRI.getLiveRangeForValue( *It2 );
331
332 if( LROfOp2) {
333
334 RegClass *const RCOfOp1 = LROfOp1->getRegClass();
335 RegClass *const RCOfOp2 = LROfOp2->getRegClass();
336
337 if( RCOfOp1 == RCOfOp2 ){
338 RCOfOp1->setInterference( LROfOp1, LROfOp2 );
339 //cerr << "\nSet interfs for PSEUDO inst: " << *MInst;
340 }
341
342 } // if Op2 has a LR
343
344 } // for all other defs in machine instr
345
346 } // for all operands in an instruction
347
348}
349
350
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000351
352
353
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000354//----------------------------------------------------------------------------
355// This method will add interferences for incoming arguments to a method.
356//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000357void PhyRegAlloc::addInterferencesForArgs()
358{
359 // get the InSet of root BB
360 const LiveVarSet *const InSet = LVI->getInSetOfBB( Meth->front() );
361
362 // get the argument list
363 const Method::ArgumentListType& ArgList = Meth->getArgumentList();
364
365 // get an iterator to arg list
366 Method::ArgumentListType::const_iterator ArgIt = ArgList.begin();
367
368
369 for( ; ArgIt != ArgList.end() ; ++ArgIt) { // for each argument
370 addInterference( *ArgIt, InSet, false ); // add interferences between
371 // args and LVars at start
372 if( DEBUG_RA > 1) {
Ruchira Sasanka97b8b442001-10-18 22:36:26 +0000373 cout << " - %% adding interference for argument ";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000374 printValue( (const Value *) *ArgIt); cout << endl;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000375 }
376 }
377}
378
379
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000380//----------------------------------------------------------------------------
381// This method is called after register allocation is complete to set the
382// allocated reisters in the machine code. This code will add register numbers
383// to MachineOperands that contain a Value.
384//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000385
386void PhyRegAlloc::updateMachineCode()
387{
388
389 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
390
391 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
392
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000393 // get the iterator for machine instructions
394 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
395 MachineCodeForBasicBlock::iterator MInstIterator = MIVec.begin();
396
397 // iterate over all the machine instructions in BB
398 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
399
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000400 MachineInstr *MInst = *MInstIterator;
401
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000402 // do not process Phis
403 if( (TM.getInstrInfo()).isPhi( MInst->getOpCode()) )
404 continue;
405
406
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000407 // if this machine instr is call, insert caller saving code
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000408
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000409 if( (TM.getInstrInfo()).isCall( MInst->getOpCode()) )
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000410 MRI.insertCallerSavingCode(MInst, *BBI, *this );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000411
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000412
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000413 // reset the stack offset for temporary variables since we may
414 // need that to spill
Vikram S. Adve12af1642001-11-08 04:48:50 +0000415 mcInfo.popAllTempValues(TM);
416
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000417 //for(MachineInstr::val_op_const_iterator OpI(MInst);!OpI.done();++OpI) {
418
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000419
420 // Now replace set the registers for operands in the machine instruction
421
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000422 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
423
424 MachineOperand& Op = MInst->getOperand(OpNum);
425
426 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
427 Op.getOperandType() == MachineOperand::MO_CCRegister) {
428
429 const Value *const Val = Op.getVRegValue();
430
431 // delete this condition checking later (must assert if Val is null)
Chris Lattner045e7c82001-09-19 16:26:23 +0000432 if( !Val) {
433 if (DEBUG_RA)
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000434 cout << "Warning: NULL Value found for operand" << endl;
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000435 continue;
436 }
437 assert( Val && "Value is NULL");
438
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000439 LiveRange *const LR = LRI.getLiveRangeForValue(Val);
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000440
441 if ( !LR ) {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000442
443 // nothing to worry if it's a const or a label
444
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000445 if (DEBUG_RA) {
Ruchira Sasanka1b732fd2001-10-16 16:34:44 +0000446 cout << "*NO LR for operand : " << Op ;
447 cout << " [reg:" << Op.getAllocatedRegNum() << "]";
448 cout << " in inst:\t" << *MInst << endl;
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000449 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000450
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000451 // if register is not allocated, mark register as invalid
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000452 if( Op.getAllocatedRegNum() == -1)
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000453 Op.setRegForValue( MRI.getInvalidRegNum());
Ruchira Sasankae727f852001-09-18 22:43:57 +0000454
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000455
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000456 continue;
457 }
458
459 unsigned RCID = (LR->getRegClass())->getID();
460
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000461 if( LR->hasColor() ) {
462 Op.setRegForValue( MRI.getUnifiedRegNum(RCID, LR->getColor()) );
463 }
464 else {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000465
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000466 // LR did NOT receive a color (register). Now, insert spill code
467 // for spilled opeands in this machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000468
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000469 //assert(0 && "LR must be spilled");
470 insertCode4SpilledLR(LR, MInst, *BBI, OpNum );
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000471
472 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000473 }
474
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000475 } // for each operand
476
477
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000478 // If there are instructions to be added, *before* this machine
479 // instruction, add them now.
480
481 if( AddedInstrMap[ MInst ] ) {
482
483 deque<MachineInstr *> &IBef = (AddedInstrMap[MInst])->InstrnsBefore;
484
485 if( ! IBef.empty() ) {
486
487 deque<MachineInstr *>::iterator AdIt;
488
489 for( AdIt = IBef.begin(); AdIt != IBef.end() ; ++AdIt ) {
490
491 if( DEBUG_RA) {
492 cerr << "For inst " << *MInst;
493 cerr << " PREPENDed instr: " << **AdIt << endl;
494 }
495
496 MInstIterator = MIVec.insert( MInstIterator, *AdIt );
497 ++MInstIterator;
498 }
499
500 }
501
502 }
503
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000504 // If there are instructions to be added *after* this machine
505 // instruction, add them now
506
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000507 if( AddedInstrMap[ MInst ] &&
508 ! (AddedInstrMap[ MInst ]->InstrnsAfter).empty() ) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000509
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000510 // if there are delay slots for this instruction, the instructions
511 // added after it must really go after the delayed instruction(s)
512 // So, we move the InstrAfter of the current instruction to the
513 // corresponding delayed instruction
514
515 unsigned delay;
516 if((delay=TM.getInstrInfo().getNumDelaySlots(MInst->getOpCode())) >0){
517 move2DelayedInstr(MInst, *(MInstIterator+delay) );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000518
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000519 if(DEBUG_RA) cout<< "\nMoved an added instr after the delay slot";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000520 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000521
522 else {
523
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000524
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000525 // Here we can add the "instructions after" to the current
526 // instruction since there are no delay slots for this instruction
527
528 deque<MachineInstr *> &IAft = (AddedInstrMap[MInst])->InstrnsAfter;
529
530 if( ! IAft.empty() ) {
531
532 deque<MachineInstr *>::iterator AdIt;
533
534 ++MInstIterator; // advance to the next instruction
535
536 for( AdIt = IAft.begin(); AdIt != IAft.end() ; ++AdIt ) {
537
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000538 if(DEBUG_RA) {
539 cerr << "For inst " << *MInst;
Ruchira Sasankaad140092001-11-09 23:49:42 +0000540 cerr << " APPENDed instr: " << **AdIt << endl;
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000541 }
542
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000543 MInstIterator = MIVec.insert( MInstIterator, *AdIt );
544 ++MInstIterator;
545 }
546
547 // MInsterator already points to the next instr. Since the
548 // for loop also increments it, decrement it to point to the
549 // instruction added last
550 --MInstIterator;
551
552 }
553
554 } // if not delay
555
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000556 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000557
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000558 } // for each machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000559 }
560}
561
562
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000563
564//----------------------------------------------------------------------------
565// This method inserts spill code for AN operand whose LR was spilled.
566// This method may be called several times for a single machine instruction
567// if it contains many spilled operands. Each time it is called, it finds
568// a register which is not live at that instruction and also which is not
569// used by other spilled operands of the same instruction. Then it uses
570// this register temporarily to accomodate the spilled value.
571//----------------------------------------------------------------------------
572void PhyRegAlloc::insertCode4SpilledLR(const LiveRange *LR,
573 MachineInstr *MInst,
574 const BasicBlock *BB,
575 const unsigned OpNum) {
576
577 MachineOperand& Op = MInst->getOperand(OpNum);
578 bool isDef = MInst->operandIsDefined(OpNum);
579 unsigned RegType = MRI.getRegType( LR );
580 int SpillOff = LR->getSpillOffFromFP();
581 RegClass *RC = LR->getRegClass();
582 const LiveVarSet *LVSetBef = LVI->getLiveVarSetBeforeMInst(MInst, BB);
Vikram S. Adve00521d72001-11-12 23:26:35 +0000583
584 /**** NOTE: THIS SHOULD USE THE RIGHT SIZE FOR THE REG BEING PUSHED ****/
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000585 int TmpOff =
Vikram S. Adve00521d72001-11-12 23:26:35 +0000586 mcInfo.pushTempValue(TM, 8 /* TM.findOptimalStorageSize(LR->getType()) */);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000587
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000588 MachineInstr *MIBef=NULL, *AdIMid=NULL, *MIAft=NULL;
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000589 int TmpReg;
590
591 TmpReg = getUsableRegAtMI(RC, RegType, MInst,LVSetBef, MIBef, MIAft);
592 TmpReg = MRI.getUnifiedRegNum( RC->getID(), TmpReg );
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000593
594
595 // get the added instructions for this instruciton
596 AddedInstrns *AI = AddedInstrMap[ MInst ];
597 if ( !AI ) {
598 AI = new AddedInstrns();
599 AddedInstrMap[ MInst ] = AI;
600 }
601
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000602
603
604 if( !isDef ) {
605
606 // for a USE, we have to load the value of LR from stack to a TmpReg
607 // and use the TmpReg as one operand of instruction
608
609 // actual loading instruction
610 AdIMid = MRI.cpMem2RegMI(MRI.getFramePointer(), SpillOff, TmpReg, RegType);
611
612 if( MIBef )
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000613 (AI->InstrnsBefore).push_back(MIBef);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000614
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000615 (AI->InstrnsBefore).push_back(AdIMid);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000616
617 if( MIAft)
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000618 (AI->InstrnsAfter).push_front(MIAft);
619
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000620
621 }
622 else { // if this is a Def
623
624 // for a DEF, we have to store the value produced by this instruction
625 // on the stack position allocated for this LR
626
627 // actual storing instruction
628 AdIMid = MRI.cpReg2MemMI(TmpReg, MRI.getFramePointer(), SpillOff, RegType);
629
630 if( MIBef )
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000631 (AI->InstrnsBefore).push_back(MIBef);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000632
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000633 (AI->InstrnsAfter).push_front(AdIMid);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000634
635 if( MIAft)
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000636 (AI->InstrnsAfter).push_front(MIAft);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000637
638 } // if !DEF
639
640 cerr << "\nFor Inst " << *MInst;
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000641 cerr << " - SPILLED LR: "; LR->printSet();
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000642 cerr << "\n - Added Instructions:";
643 if( MIBef ) cerr << *MIBef;
644 cerr << *AdIMid;
645 if( MIAft ) cerr << *MIAft;
646
647 Op.setRegForValue( TmpReg ); // set the opearnd
648
649
650}
651
652
653
654
655
656
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000657//----------------------------------------------------------------------------
658// We can use the following method to get a temporary register to be used
659// BEFORE any given machine instruction. If there is a register available,
660// this method will simply return that register and set MIBef = MIAft = NULL.
661// Otherwise, it will return a register and MIAft and MIBef will contain
662// two instructions used to free up this returned register.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000663// Returned register number is the UNIFIED register number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000664//----------------------------------------------------------------------------
665
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000666int PhyRegAlloc::getUsableRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000667 const int RegType,
668 const MachineInstr *MInst,
669 const LiveVarSet *LVSetBef,
670 MachineInstr *MIBef,
671 MachineInstr *MIAft) {
672
673 int Reg = getUnusedRegAtMI(RC, MInst, LVSetBef);
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000674 Reg = MRI.getUnifiedRegNum(RC->getID(), Reg);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000675
676 if( Reg != -1) {
677 // we found an unused register, so we can simply used
678 MIBef = MIAft = NULL;
679 }
680 else {
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000681 // we couldn't find an unused register. Generate code to free up a reg by
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000682 // saving it on stack and restoring after the instruction
683
Vikram S. Adve12af1642001-11-08 04:48:50 +0000684 /**** NOTE: THIS SHOULD USE THE RIGHT SIZE FOR THE REG BEING PUSHED ****/
685 int TmpOff = mcInfo.pushTempValue(TM, /*size*/ 8);
686
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000687 Reg = getRegNotUsedByThisInst(RC, MInst);
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000688 MIBef = MRI.cpReg2MemMI(Reg, MRI.getFramePointer(), TmpOff, RegType );
689 MIAft = MRI.cpMem2RegMI(MRI.getFramePointer(), TmpOff, Reg, RegType );
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000690 }
691
692 return Reg;
693}
694
695//----------------------------------------------------------------------------
696// This method is called to get a new unused register that can be used to
697// accomodate a spilled value.
698// This method may be called several times for a single machine instruction
699// if it contains many spilled operands. Each time it is called, it finds
700// a register which is not live at that instruction and also which is not
701// used by other spilled operands of the same instruction.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000702// Return register number is relative to the register class. NOT
703// unified number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000704//----------------------------------------------------------------------------
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000705int PhyRegAlloc::getUnusedRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000706 const MachineInstr *MInst,
707 const LiveVarSet *LVSetBef) {
708
709 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
710
711 bool *IsColorUsedArr = RC->getIsColorUsedArr();
712
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000713 for(unsigned i=0; i < NumAvailRegs; i++)
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000714 IsColorUsedArr[i] = false;
715
716 LiveVarSet::const_iterator LIt = LVSetBef->begin();
717
718 // for each live var in live variable set after machine inst
719 for( ; LIt != LVSetBef->end(); ++LIt) {
720
721 // get the live range corresponding to live var
722 LiveRange *const LRofLV = LRI.getLiveRangeForValue(*LIt );
723
724 // LR can be null if it is a const since a const
725 // doesn't have a dominating def - see Assumptions above
726 if( LRofLV )
727 if( LRofLV->hasColor() )
728 IsColorUsedArr[ LRofLV->getColor() ] = true;
729 }
730
731 // It is possible that one operand of this MInst was already spilled
732 // and it received some register temporarily. If that's the case,
733 // it is recorded in machine operand. We must skip such registers.
734
735 setRegsUsedByThisInst(RC, MInst);
736
737 unsigned c; // find first unused color
738 for( c=0; c < NumAvailRegs; c++)
739 if( ! IsColorUsedArr[ c ] ) break;
740
741 if(c < NumAvailRegs)
742 return c;
743 else
744 return -1;
745
746
747}
748
749
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000750
751//----------------------------------------------------------------------------
752// This method modifies the IsColorUsedArr of the register class passed to it.
753// It sets the bits corresponding to the registers used by this machine
754// instructions. Explicit operands are set.
755//----------------------------------------------------------------------------
756void PhyRegAlloc::setRegsUsedByThisInst(RegClass *RC,
757 const MachineInstr *MInst ) {
758
759 bool *IsColorUsedArr = RC->getIsColorUsedArr();
760
761 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
762
763 const MachineOperand& Op = MInst->getOperand(OpNum);
764
765 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
766 Op.getOperandType() == MachineOperand::MO_CCRegister) {
767
768 const Value *const Val = Op.getVRegValue();
769
770 if( !Val )
771 if( MRI.getRegClassIDOfValue( Val )== RC->getID() ) {
772 int Reg;
773 if( (Reg=Op.getAllocatedRegNum()) != -1)
774 IsColorUsedArr[ Reg ] = true;
775
776 }
777 }
778 }
779
780 // If there are implicit references, mark them as well
781
782 for(unsigned z=0; z < MInst->getNumImplicitRefs(); z++) {
783
784 LiveRange *const LRofImpRef =
785 LRI.getLiveRangeForValue( MInst->getImplicitRef(z) );
786
787 if( LRofImpRef )
788 if( LRofImpRef->hasColor() )
789 IsColorUsedArr[ LRofImpRef->getColor() ] = true;
790 }
791
792
793
794}
795
796
797
798//----------------------------------------------------------------------------
799// Get any other register in a register class, other than what is used
800// by operands of a machine instruction.
801//----------------------------------------------------------------------------
802int PhyRegAlloc::getRegNotUsedByThisInst(RegClass *RC,
803 const MachineInstr *MInst) {
804
805 bool *IsColorUsedArr = RC->getIsColorUsedArr();
806 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
807
808
809 for(unsigned i=0; i < NumAvailRegs ; i++)
810 IsColorUsedArr[i] = false;
811
812 setRegsUsedByThisInst(RC, MInst);
813
814 unsigned c; // find first unused color
815 for( c=0; c < RC->getNumOfAvailRegs(); c++)
816 if( ! IsColorUsedArr[ c ] ) break;
817
818 if(c < NumAvailRegs)
819 return c;
820 else
821 assert( 0 && "FATAL: No free register could be found in reg class!!");
822
823}
824
825
826
827
828
829//----------------------------------------------------------------------------
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000830// If there are delay slots for an instruction, the instructions
831// added after it must really go after the delayed instruction(s).
832// So, we move the InstrAfter of that instruction to the
833// corresponding delayed instruction using the following method.
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000834
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000835//----------------------------------------------------------------------------
836void PhyRegAlloc:: move2DelayedInstr(const MachineInstr *OrigMI,
837 const MachineInstr *DelayedMI) {
838
839
840 // "added after" instructions of the original instr
841 deque<MachineInstr *> &OrigAft = (AddedInstrMap[OrigMI])->InstrnsAfter;
842
843 // "added instructions" of the delayed instr
844 AddedInstrns *DelayAdI = AddedInstrMap[DelayedMI];
845
846 if(! DelayAdI ) { // create a new "added after" if necessary
847 DelayAdI = new AddedInstrns();
848 AddedInstrMap[DelayedMI] = DelayAdI;
849 }
850
851 // "added after" instructions of the delayed instr
852 deque<MachineInstr *> &DelayedAft = DelayAdI->InstrnsAfter;
853
854 // go thru all the "added after instructions" of the original instruction
855 // and append them to the "addded after instructions" of the delayed
856 // instructions
857
858 deque<MachineInstr *>::iterator OrigAdIt;
859
860 for( OrigAdIt = OrigAft.begin(); OrigAdIt != OrigAft.end() ; ++OrigAdIt ) {
861 DelayedAft.push_back( *OrigAdIt );
862 }
863
864 // empty the "added after instructions" of the original instruction
865 OrigAft.clear();
866
867}
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000868
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000869//----------------------------------------------------------------------------
870// This method prints the code with registers after register allocation is
871// complete.
872//----------------------------------------------------------------------------
873void PhyRegAlloc::printMachineCode()
874{
875
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000876 cout << endl << ";************** Method ";
877 cout << Meth->getName() << " *****************" << endl;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000878
879 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
880
881 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
882
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000883 cout << endl ; printLabel( *BBI); cout << ": ";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000884
885 // get the iterator for machine instructions
886 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
887 MachineCodeForBasicBlock::iterator MInstIterator = MIVec.begin();
888
889 // iterate over all the machine instructions in BB
890 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
891
892 MachineInstr *const MInst = *MInstIterator;
893
894
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000895 cout << endl << "\t";
896 cout << TargetInstrDescriptors[MInst->getOpCode()].opCodeString;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000897
898
899 //for(MachineInstr::val_op_const_iterator OpI(MInst);!OpI.done();++OpI) {
900
901 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
902
903 MachineOperand& Op = MInst->getOperand(OpNum);
904
905 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasanka97b8b442001-10-18 22:36:26 +0000906 Op.getOperandType() == MachineOperand::MO_CCRegister /*||
907 Op.getOperandType() == MachineOperand::MO_PCRelativeDisp*/ ) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000908
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000909 const Value *const Val = Op.getVRegValue () ;
Ruchira Sasankae727f852001-09-18 22:43:57 +0000910 // ****this code is temporary till NULL Values are fixed
911 if( ! Val ) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000912 cout << "\t<*NULL*>";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000913 continue;
914 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000915
916 // if a label or a constant
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000917 if( (Val->getValueType() == Value::BasicBlockVal) ) {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000918
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000919 cout << "\t"; printLabel( Op.getVRegValue () );
Ruchira Sasankae727f852001-09-18 22:43:57 +0000920 }
921 else {
922 // else it must be a register value
923 const int RegNum = Op.getAllocatedRegNum();
924
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +0000925 cout << "\t" << "%" << MRI.getUnifiedRegName( RegNum );
Ruchira Sasankae727f852001-09-18 22:43:57 +0000926 }
927
928 }
929 else if(Op.getOperandType() == MachineOperand::MO_MachineRegister) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000930 cout << "\t" << "%" << MRI.getUnifiedRegName(Op.getMachineRegNum());
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000931 }
932
933 else
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000934 cout << "\t" << Op; // use dump field
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000935 }
936
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000937
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000938
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000939 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
940 if( NumOfImpRefs > 0 ) {
941
942 cout << "\tImplicit:";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000943
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000944 for(unsigned z=0; z < NumOfImpRefs; z++) {
945 printValue( MInst->getImplicitRef(z) );
946 cout << "\t";
947 }
948
949 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000950
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000951 } // for all machine instructions
952
953
954 cout << endl;
955
956 } // for all BBs
957
958 cout << endl;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000959}
960
Ruchira Sasankae727f852001-09-18 22:43:57 +0000961
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000962//----------------------------------------------------------------------------
963//
964//----------------------------------------------------------------------------
965
966void PhyRegAlloc::colorCallRetArgs()
967{
968
969 CallRetInstrListType &CallRetInstList = LRI.getCallRetInstrList();
970 CallRetInstrListType::const_iterator It = CallRetInstList.begin();
971
972 for( ; It != CallRetInstList.end(); ++It ) {
973
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000974 const MachineInstr *const CRMI = *It;
975 unsigned OpCode = CRMI->getOpCode();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000976
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000977 // get the added instructions for this Call/Ret instruciton
978 AddedInstrns *AI = AddedInstrMap[ CRMI ];
979 if ( !AI ) {
980 AI = new AddedInstrns();
981 AddedInstrMap[ CRMI ] = AI;
982 }
983
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000984 // Tmp stack poistions are needed by some calls that have spilled args
985 // So reset it before we call each such method
Vikram S. Adve12af1642001-11-08 04:48:50 +0000986 mcInfo.popAllTempValues(TM);
987
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000988 if( (TM.getInstrInfo()).isCall( OpCode ) )
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000989 MRI.colorCallArgs( CRMI, LRI, AI, *this );
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000990
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000991 else if ( (TM.getInstrInfo()).isReturn(OpCode) )
992 MRI.colorRetValue( CRMI, LRI, AI );
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000993
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000994 else assert( 0 && "Non Call/Ret instrn in CallRetInstrList\n" );
995
996 }
997
998}
999
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001000
1001
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001002//----------------------------------------------------------------------------
1003
1004//----------------------------------------------------------------------------
1005void PhyRegAlloc::colorIncomingArgs()
1006{
1007 const BasicBlock *const FirstBB = Meth->front();
1008 const MachineInstr *FirstMI = *((FirstBB->getMachineInstrVec()).begin());
1009 assert( FirstMI && "No machine instruction in entry BB");
1010
1011 AddedInstrns *AI = AddedInstrMap[ FirstMI ];
1012 if ( !AI ) {
1013 AI = new AddedInstrns();
1014 AddedInstrMap[ FirstMI ] = AI;
1015 }
1016
1017 MRI.colorMethodArgs(Meth, LRI, AI );
1018}
1019
Ruchira Sasankae727f852001-09-18 22:43:57 +00001020
1021//----------------------------------------------------------------------------
1022// Used to generate a label for a basic block
1023//----------------------------------------------------------------------------
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001024void PhyRegAlloc::printLabel(const Value *const Val)
1025{
1026 if( Val->hasName() )
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001027 cout << Val->getName();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001028 else
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001029 cout << "Label" << Val;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001030}
1031
1032
Ruchira Sasankae727f852001-09-18 22:43:57 +00001033//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001034// This method calls setSugColorUsable method of each live range. This
1035// will determine whether the suggested color of LR is really usable.
1036// A suggested color is not usable when the suggested color is volatile
1037// AND when there are call interferences
1038//----------------------------------------------------------------------------
1039
1040void PhyRegAlloc::markUnusableSugColors()
1041{
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001042 if(DEBUG_RA ) cout << "\nmarking unusable suggested colors ..." << endl;
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001043
1044 // hash map iterator
1045 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
1046 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
1047
1048 for( ; HMI != HMIEnd ; ++HMI ) {
1049
1050 if( (*HMI).first ) {
1051
1052 LiveRange *L = (*HMI).second; // get the LiveRange
1053
1054 if(L) {
1055 if( L->hasSuggestedColor() ) {
1056
1057 int RCID = (L->getRegClass())->getID();
1058 if( MRI.isRegVolatile( RCID, L->getSuggestedColor()) &&
1059 L->isCallInterference() )
1060 L->setSuggestedColorUsable( false );
1061 else
1062 L->setSuggestedColorUsable( true );
1063 }
1064 } // if L->hasSuggestedColor()
1065 }
1066 } // for all LR's in hash map
1067}
1068
1069
1070
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001071//----------------------------------------------------------------------------
1072// The following method will set the stack offsets of the live ranges that
1073// are decided to be spillled. This must be called just after coloring the
1074// LRs using the graph coloring algo. For each live range that is spilled,
1075// this method allocate a new spill position on the stack.
1076//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001077
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001078void PhyRegAlloc::allocateStackSpace4SpilledLRs()
1079{
1080 if(DEBUG_RA ) cout << "\nsetting LR stack offsets ..." << endl;
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001081
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001082 // hash map iterator
1083 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
1084 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
1085
1086 for( ; HMI != HMIEnd ; ++HMI ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001087 if( (*HMI).first ) {
1088 LiveRange *L = (*HMI).second; // get the LiveRange
1089 if(L)
1090 if( ! L->hasColor() )
Vikram S. Advee85f2332001-11-12 23:40:22 +00001091 /**** NOTE: THIS SHOULD USE THE RIGHT SIZE FOR THE REG BEING PUSHED ****/
1092 L->setSpillOffFromFP(mcInfo.allocateSpilledValue(TM, Type::LongTy /*L->getType()*/ ));
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001093 }
1094 } // for all LR's in hash map
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001095}
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001096
1097
1098
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001099//----------------------------------------------------------------------------
Ruchira Sasankae727f852001-09-18 22:43:57 +00001100// The entry pont to Register Allocation
1101//----------------------------------------------------------------------------
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001102
1103void PhyRegAlloc::allocateRegisters()
1104{
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001105
1106 // make sure that we put all register classes into the RegClassList
1107 // before we call constructLiveRanges (now done in the constructor of
1108 // PhyRegAlloc class).
1109
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001110 constructLiveRanges(); // create LR info
1111
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001112 if( DEBUG_RA )
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001113 LRI.printLiveRanges();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001114
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001115 createIGNodeListsAndIGs(); // create IGNode list and IGs
1116
1117 buildInterferenceGraphs(); // build IGs in all reg classes
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001118
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001119
1120 if( DEBUG_RA ) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001121 // print all LRs in all reg classes
1122 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1123 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001124
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001125 // print IGs in all register classes
1126 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1127 RegClassList[ rc ]->printIG();
1128 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001129
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001130 LRI.coalesceLRs(); // coalesce all live ranges
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001131
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +00001132 // coalscing could not get rid of all phi's, add phi elimination
1133 // instructions
1134 // insertPhiEleminateInstrns();
1135
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001136 if( DEBUG_RA) {
1137 // print all LRs in all reg classes
1138 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1139 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001140
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001141 // print IGs in all register classes
1142 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1143 RegClassList[ rc ]->printIG();
1144 }
1145
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001146
1147 // mark un-usable suggested color before graph coloring algorithm.
1148 // When this is done, the graph coloring algo will not reserve
1149 // suggested color unnecessarily - they can be used by another LR
1150 markUnusableSugColors();
1151
1152 // color all register classes using the graph coloring algo
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001153 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1154 RegClassList[ rc ]->colorAllRegs();
1155
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001156 // Atter grpah coloring, if some LRs did not receive a color (i.e, spilled)
1157 // a poistion for such spilled LRs
1158 allocateStackSpace4SpilledLRs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001159
1160 // color incoming args and call args
1161 colorIncomingArgs();
1162 colorCallRetArgs();
1163
Ruchira Sasanka97b8b442001-10-18 22:36:26 +00001164
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001165 updateMachineCode();
Chris Lattner045e7c82001-09-19 16:26:23 +00001166 if (DEBUG_RA) {
Vikram S. Adve12af1642001-11-08 04:48:50 +00001167 MachineCodeForMethod::get(Meth).dump();
Chris Lattner045e7c82001-09-19 16:26:23 +00001168 printMachineCode(); // only for DEBUGGING
1169 }
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +00001170
1171 // char ch;
1172 //cin >> ch;
1173
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001174}
1175
Ruchira Sasankae727f852001-09-18 22:43:57 +00001176
1177