blob: b872f553960fec87210d7ca08df5839c5b748895 [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
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000318 bool setInterf = false;
319
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000320 // iterate over MI operands to find defs
321 for( MachineInstr::val_op_const_iterator It1(MInst);!It1.done(); ++It1) {
322
323 const LiveRange *const LROfOp1 = LRI.getLiveRangeForValue( *It1 );
324
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000325 if( !LROfOp1 && It1.isDef() )
326 assert( 0 && "No LR for Def in PSEUDO insruction");
327
328 //if( !LROfOp1 ) continue;
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000329
330 MachineInstr::val_op_const_iterator It2 = It1;
331 ++It2;
332
333 for( ; !It2.done(); ++It2) {
334
335 const LiveRange *const LROfOp2 = LRI.getLiveRangeForValue( *It2 );
336
337 if( LROfOp2) {
338
339 RegClass *const RCOfOp1 = LROfOp1->getRegClass();
340 RegClass *const RCOfOp2 = LROfOp2->getRegClass();
341
342 if( RCOfOp1 == RCOfOp2 ){
343 RCOfOp1->setInterference( LROfOp1, LROfOp2 );
344 //cerr << "\nSet interfs for PSEUDO inst: " << *MInst;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000345 setInterf = true;
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000346 }
347
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000348
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000349 } // if Op2 has a LR
350
351 } // for all other defs in machine instr
352
353 } // for all operands in an instruction
354
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000355 if( !setInterf && (MInst->getNumOperands() > 2) ) {
356 cerr << "\nInterf not set for any operand in pseudo instr:\n";
357 cerr << *MInst;
358 assert(0 && "Interf not set for pseudo instr with > 2 operands" );
359
360 }
361
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000362}
363
364
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000365
366
367
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000368//----------------------------------------------------------------------------
369// This method will add interferences for incoming arguments to a method.
370//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000371void PhyRegAlloc::addInterferencesForArgs()
372{
373 // get the InSet of root BB
374 const LiveVarSet *const InSet = LVI->getInSetOfBB( Meth->front() );
375
376 // get the argument list
377 const Method::ArgumentListType& ArgList = Meth->getArgumentList();
378
379 // get an iterator to arg list
380 Method::ArgumentListType::const_iterator ArgIt = ArgList.begin();
381
382
383 for( ; ArgIt != ArgList.end() ; ++ArgIt) { // for each argument
384 addInterference( *ArgIt, InSet, false ); // add interferences between
385 // args and LVars at start
386 if( DEBUG_RA > 1) {
Ruchira Sasanka97b8b442001-10-18 22:36:26 +0000387 cout << " - %% adding interference for argument ";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000388 printValue( (const Value *) *ArgIt); cout << endl;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000389 }
390 }
391}
392
393
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000394//----------------------------------------------------------------------------
395// This method is called after register allocation is complete to set the
396// allocated reisters in the machine code. This code will add register numbers
397// to MachineOperands that contain a Value.
398//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000399
400void PhyRegAlloc::updateMachineCode()
401{
402
403 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
404
405 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
406
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000407 // get the iterator for machine instructions
408 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
409 MachineCodeForBasicBlock::iterator MInstIterator = MIVec.begin();
410
411 // iterate over all the machine instructions in BB
412 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
413
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000414 MachineInstr *MInst = *MInstIterator;
415
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000416 // do not process Phis
417 if( (TM.getInstrInfo()).isPhi( MInst->getOpCode()) )
418 continue;
419
420
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000421 // if this machine instr is call, insert caller saving code
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000422
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000423 if( (TM.getInstrInfo()).isCall( MInst->getOpCode()) )
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000424 MRI.insertCallerSavingCode(MInst, *BBI, *this );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000425
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000426
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000427 // reset the stack offset for temporary variables since we may
428 // need that to spill
Vikram S. Adve12af1642001-11-08 04:48:50 +0000429 mcInfo.popAllTempValues(TM);
430
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000431 //for(MachineInstr::val_op_const_iterator OpI(MInst);!OpI.done();++OpI) {
432
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000433
434 // Now replace set the registers for operands in the machine instruction
435
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000436 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
437
438 MachineOperand& Op = MInst->getOperand(OpNum);
439
440 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
441 Op.getOperandType() == MachineOperand::MO_CCRegister) {
442
443 const Value *const Val = Op.getVRegValue();
444
445 // delete this condition checking later (must assert if Val is null)
Chris Lattner045e7c82001-09-19 16:26:23 +0000446 if( !Val) {
447 if (DEBUG_RA)
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000448 cout << "Warning: NULL Value found for operand" << endl;
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000449 continue;
450 }
451 assert( Val && "Value is NULL");
452
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000453 LiveRange *const LR = LRI.getLiveRangeForValue(Val);
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000454
455 if ( !LR ) {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000456
457 // nothing to worry if it's a const or a label
458
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000459 if (DEBUG_RA) {
Ruchira Sasanka1b732fd2001-10-16 16:34:44 +0000460 cout << "*NO LR for operand : " << Op ;
461 cout << " [reg:" << Op.getAllocatedRegNum() << "]";
462 cout << " in inst:\t" << *MInst << endl;
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000463 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000464
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000465 // if register is not allocated, mark register as invalid
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000466 if( Op.getAllocatedRegNum() == -1)
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000467 Op.setRegForValue( MRI.getInvalidRegNum());
Ruchira Sasankae727f852001-09-18 22:43:57 +0000468
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000469
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000470 continue;
471 }
472
473 unsigned RCID = (LR->getRegClass())->getID();
474
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000475 if( LR->hasColor() ) {
476 Op.setRegForValue( MRI.getUnifiedRegNum(RCID, LR->getColor()) );
477 }
478 else {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000479
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000480 // LR did NOT receive a color (register). Now, insert spill code
481 // for spilled opeands in this machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000482
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000483 //assert(0 && "LR must be spilled");
484 insertCode4SpilledLR(LR, MInst, *BBI, OpNum );
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000485
486 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000487 }
488
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000489 } // for each operand
490
491
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000492 // If there are instructions to be added, *before* this machine
493 // instruction, add them now.
494
495 if( AddedInstrMap[ MInst ] ) {
496
497 deque<MachineInstr *> &IBef = (AddedInstrMap[MInst])->InstrnsBefore;
498
499 if( ! IBef.empty() ) {
500
501 deque<MachineInstr *>::iterator AdIt;
502
503 for( AdIt = IBef.begin(); AdIt != IBef.end() ; ++AdIt ) {
504
505 if( DEBUG_RA) {
506 cerr << "For inst " << *MInst;
507 cerr << " PREPENDed instr: " << **AdIt << endl;
508 }
509
510 MInstIterator = MIVec.insert( MInstIterator, *AdIt );
511 ++MInstIterator;
512 }
513
514 }
515
516 }
517
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000518 // If there are instructions to be added *after* this machine
519 // instruction, add them now
520
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000521 if( AddedInstrMap[ MInst ] &&
522 ! (AddedInstrMap[ MInst ]->InstrnsAfter).empty() ) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000523
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000524 // if there are delay slots for this instruction, the instructions
525 // added after it must really go after the delayed instruction(s)
526 // So, we move the InstrAfter of the current instruction to the
527 // corresponding delayed instruction
528
529 unsigned delay;
530 if((delay=TM.getInstrInfo().getNumDelaySlots(MInst->getOpCode())) >0){
531 move2DelayedInstr(MInst, *(MInstIterator+delay) );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000532
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000533 if(DEBUG_RA) cout<< "\nMoved an added instr after the delay slot";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000534 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000535
536 else {
537
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000538
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000539 // Here we can add the "instructions after" to the current
540 // instruction since there are no delay slots for this instruction
541
542 deque<MachineInstr *> &IAft = (AddedInstrMap[MInst])->InstrnsAfter;
543
544 if( ! IAft.empty() ) {
545
546 deque<MachineInstr *>::iterator AdIt;
547
548 ++MInstIterator; // advance to the next instruction
549
550 for( AdIt = IAft.begin(); AdIt != IAft.end() ; ++AdIt ) {
551
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000552 if(DEBUG_RA) {
553 cerr << "For inst " << *MInst;
Ruchira Sasankaad140092001-11-09 23:49:42 +0000554 cerr << " APPENDed instr: " << **AdIt << endl;
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000555 }
556
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000557 MInstIterator = MIVec.insert( MInstIterator, *AdIt );
558 ++MInstIterator;
559 }
560
561 // MInsterator already points to the next instr. Since the
562 // for loop also increments it, decrement it to point to the
563 // instruction added last
564 --MInstIterator;
565
566 }
567
568 } // if not delay
569
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000570 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000571
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000572 } // for each machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000573 }
574}
575
576
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000577
578//----------------------------------------------------------------------------
579// This method inserts spill code for AN operand whose LR was spilled.
580// This method may be called several times for a single machine instruction
581// if it contains many spilled operands. Each time it is called, it finds
582// a register which is not live at that instruction and also which is not
583// used by other spilled operands of the same instruction. Then it uses
584// this register temporarily to accomodate the spilled value.
585//----------------------------------------------------------------------------
586void PhyRegAlloc::insertCode4SpilledLR(const LiveRange *LR,
587 MachineInstr *MInst,
588 const BasicBlock *BB,
589 const unsigned OpNum) {
590
591 MachineOperand& Op = MInst->getOperand(OpNum);
592 bool isDef = MInst->operandIsDefined(OpNum);
593 unsigned RegType = MRI.getRegType( LR );
594 int SpillOff = LR->getSpillOffFromFP();
595 RegClass *RC = LR->getRegClass();
596 const LiveVarSet *LVSetBef = LVI->getLiveVarSetBeforeMInst(MInst, BB);
Vikram S. Adve00521d72001-11-12 23:26:35 +0000597
598 /**** NOTE: THIS SHOULD USE THE RIGHT SIZE FOR THE REG BEING PUSHED ****/
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000599 int TmpOff =
Vikram S. Adve00521d72001-11-12 23:26:35 +0000600 mcInfo.pushTempValue(TM, 8 /* TM.findOptimalStorageSize(LR->getType()) */);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000601
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000602 MachineInstr *MIBef=NULL, *AdIMid=NULL, *MIAft=NULL;
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000603 int TmpReg;
604
605 TmpReg = getUsableRegAtMI(RC, RegType, MInst,LVSetBef, MIBef, MIAft);
606 TmpReg = MRI.getUnifiedRegNum( RC->getID(), TmpReg );
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000607
608
609 // get the added instructions for this instruciton
610 AddedInstrns *AI = AddedInstrMap[ MInst ];
611 if ( !AI ) {
612 AI = new AddedInstrns();
613 AddedInstrMap[ MInst ] = AI;
614 }
615
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000616
617
618 if( !isDef ) {
619
620 // for a USE, we have to load the value of LR from stack to a TmpReg
621 // and use the TmpReg as one operand of instruction
622
623 // actual loading instruction
624 AdIMid = MRI.cpMem2RegMI(MRI.getFramePointer(), SpillOff, TmpReg, RegType);
625
626 if( MIBef )
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000627 (AI->InstrnsBefore).push_back(MIBef);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000628
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000629 (AI->InstrnsBefore).push_back(AdIMid);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000630
631 if( MIAft)
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000632 (AI->InstrnsAfter).push_front(MIAft);
633
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000634
635 }
636 else { // if this is a Def
637
638 // for a DEF, we have to store the value produced by this instruction
639 // on the stack position allocated for this LR
640
641 // actual storing instruction
642 AdIMid = MRI.cpReg2MemMI(TmpReg, MRI.getFramePointer(), SpillOff, RegType);
643
644 if( MIBef )
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000645 (AI->InstrnsBefore).push_back(MIBef);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000646
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000647 (AI->InstrnsAfter).push_front(AdIMid);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000648
649 if( MIAft)
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000650 (AI->InstrnsAfter).push_front(MIAft);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000651
652 } // if !DEF
653
654 cerr << "\nFor Inst " << *MInst;
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000655 cerr << " - SPILLED LR: "; LR->printSet();
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000656 cerr << "\n - Added Instructions:";
657 if( MIBef ) cerr << *MIBef;
658 cerr << *AdIMid;
659 if( MIAft ) cerr << *MIAft;
660
661 Op.setRegForValue( TmpReg ); // set the opearnd
662
663
664}
665
666
667
668
669
670
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000671//----------------------------------------------------------------------------
672// We can use the following method to get a temporary register to be used
673// BEFORE any given machine instruction. If there is a register available,
674// this method will simply return that register and set MIBef = MIAft = NULL.
675// Otherwise, it will return a register and MIAft and MIBef will contain
676// two instructions used to free up this returned register.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000677// Returned register number is the UNIFIED register number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000678//----------------------------------------------------------------------------
679
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000680int PhyRegAlloc::getUsableRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000681 const int RegType,
682 const MachineInstr *MInst,
683 const LiveVarSet *LVSetBef,
684 MachineInstr *MIBef,
685 MachineInstr *MIAft) {
686
687 int Reg = getUnusedRegAtMI(RC, MInst, LVSetBef);
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000688 Reg = MRI.getUnifiedRegNum(RC->getID(), Reg);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000689
690 if( Reg != -1) {
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000691 // we found an unused register, so we can simply use it
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000692 MIBef = MIAft = NULL;
693 }
694 else {
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000695 // we couldn't find an unused register. Generate code to free up a reg by
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000696 // saving it on stack and restoring after the instruction
697
Vikram S. Adve12af1642001-11-08 04:48:50 +0000698 /**** NOTE: THIS SHOULD USE THE RIGHT SIZE FOR THE REG BEING PUSHED ****/
699 int TmpOff = mcInfo.pushTempValue(TM, /*size*/ 8);
700
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000701 Reg = getRegNotUsedByThisInst(RC, MInst);
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000702 MIBef = MRI.cpReg2MemMI(Reg, MRI.getFramePointer(), TmpOff, RegType );
703 MIAft = MRI.cpMem2RegMI(MRI.getFramePointer(), TmpOff, Reg, RegType );
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000704 }
705
706 return Reg;
707}
708
709//----------------------------------------------------------------------------
710// This method is called to get a new unused register that can be used to
711// accomodate a spilled value.
712// This method may be called several times for a single machine instruction
713// if it contains many spilled operands. Each time it is called, it finds
714// a register which is not live at that instruction and also which is not
715// used by other spilled operands of the same instruction.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000716// Return register number is relative to the register class. NOT
717// unified number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000718//----------------------------------------------------------------------------
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000719int PhyRegAlloc::getUnusedRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000720 const MachineInstr *MInst,
721 const LiveVarSet *LVSetBef) {
722
723 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
724
725 bool *IsColorUsedArr = RC->getIsColorUsedArr();
726
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000727 for(unsigned i=0; i < NumAvailRegs; i++)
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000728 IsColorUsedArr[i] = false;
729
730 LiveVarSet::const_iterator LIt = LVSetBef->begin();
731
732 // for each live var in live variable set after machine inst
733 for( ; LIt != LVSetBef->end(); ++LIt) {
734
735 // get the live range corresponding to live var
736 LiveRange *const LRofLV = LRI.getLiveRangeForValue(*LIt );
737
738 // LR can be null if it is a const since a const
739 // doesn't have a dominating def - see Assumptions above
740 if( LRofLV )
741 if( LRofLV->hasColor() )
742 IsColorUsedArr[ LRofLV->getColor() ] = true;
743 }
744
745 // It is possible that one operand of this MInst was already spilled
746 // and it received some register temporarily. If that's the case,
747 // it is recorded in machine operand. We must skip such registers.
748
749 setRegsUsedByThisInst(RC, MInst);
750
751 unsigned c; // find first unused color
752 for( c=0; c < NumAvailRegs; c++)
753 if( ! IsColorUsedArr[ c ] ) break;
754
755 if(c < NumAvailRegs)
756 return c;
757 else
758 return -1;
759
760
761}
762
763
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000764
765//----------------------------------------------------------------------------
766// This method modifies the IsColorUsedArr of the register class passed to it.
767// It sets the bits corresponding to the registers used by this machine
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000768// instructions. Both explicit and implicit operands are set.
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000769//----------------------------------------------------------------------------
770void PhyRegAlloc::setRegsUsedByThisInst(RegClass *RC,
771 const MachineInstr *MInst ) {
772
773 bool *IsColorUsedArr = RC->getIsColorUsedArr();
774
775 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
776
777 const MachineOperand& Op = MInst->getOperand(OpNum);
778
779 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000780 Op.getOperandType() == MachineOperand::MO_CCRegister ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000781
782 const Value *const Val = Op.getVRegValue();
783
784 if( !Val )
785 if( MRI.getRegClassIDOfValue( Val )== RC->getID() ) {
786 int Reg;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000787 if( (Reg=Op.getAllocatedRegNum()) != -1) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000788 IsColorUsedArr[ Reg ] = true;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000789 }
790 else {
791 // it is possilbe that this operand still is not marked with
792 // a register but it has a LR and that received a color
793
794 LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
795 if( LROfVal)
796 if( LROfVal->hasColor() )
797 IsColorUsedArr[ LROfVal->getColor() ] = true;
798 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000799
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000800 } // if reg classes are the same
801 }
802 else if (Op.getOperandType() == MachineOperand::MO_MachineRegister) {
803 IsColorUsedArr[ Op.getMachineRegNum() ] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000804 }
805 }
806
807 // If there are implicit references, mark them as well
808
809 for(unsigned z=0; z < MInst->getNumImplicitRefs(); z++) {
810
811 LiveRange *const LRofImpRef =
812 LRI.getLiveRangeForValue( MInst->getImplicitRef(z) );
813
814 if( LRofImpRef )
815 if( LRofImpRef->hasColor() )
816 IsColorUsedArr[ LRofImpRef->getColor() ] = true;
817 }
818
819
820
821}
822
823
824
825//----------------------------------------------------------------------------
826// Get any other register in a register class, other than what is used
827// by operands of a machine instruction.
828//----------------------------------------------------------------------------
829int PhyRegAlloc::getRegNotUsedByThisInst(RegClass *RC,
830 const MachineInstr *MInst) {
831
832 bool *IsColorUsedArr = RC->getIsColorUsedArr();
833 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
834
835
836 for(unsigned i=0; i < NumAvailRegs ; i++)
837 IsColorUsedArr[i] = false;
838
839 setRegsUsedByThisInst(RC, MInst);
840
841 unsigned c; // find first unused color
842 for( c=0; c < RC->getNumOfAvailRegs(); c++)
843 if( ! IsColorUsedArr[ c ] ) break;
844
845 if(c < NumAvailRegs)
846 return c;
847 else
848 assert( 0 && "FATAL: No free register could be found in reg class!!");
849
850}
851
852
853
854
855
856//----------------------------------------------------------------------------
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000857// If there are delay slots for an instruction, the instructions
858// added after it must really go after the delayed instruction(s).
859// So, we move the InstrAfter of that instruction to the
860// corresponding delayed instruction using the following method.
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000861
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000862//----------------------------------------------------------------------------
863void PhyRegAlloc:: move2DelayedInstr(const MachineInstr *OrigMI,
864 const MachineInstr *DelayedMI) {
865
866
867 // "added after" instructions of the original instr
868 deque<MachineInstr *> &OrigAft = (AddedInstrMap[OrigMI])->InstrnsAfter;
869
870 // "added instructions" of the delayed instr
871 AddedInstrns *DelayAdI = AddedInstrMap[DelayedMI];
872
873 if(! DelayAdI ) { // create a new "added after" if necessary
874 DelayAdI = new AddedInstrns();
875 AddedInstrMap[DelayedMI] = DelayAdI;
876 }
877
878 // "added after" instructions of the delayed instr
879 deque<MachineInstr *> &DelayedAft = DelayAdI->InstrnsAfter;
880
881 // go thru all the "added after instructions" of the original instruction
882 // and append them to the "addded after instructions" of the delayed
883 // instructions
884
885 deque<MachineInstr *>::iterator OrigAdIt;
886
887 for( OrigAdIt = OrigAft.begin(); OrigAdIt != OrigAft.end() ; ++OrigAdIt ) {
888 DelayedAft.push_back( *OrigAdIt );
889 }
890
891 // empty the "added after instructions" of the original instruction
892 OrigAft.clear();
893
894}
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000895
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000896//----------------------------------------------------------------------------
897// This method prints the code with registers after register allocation is
898// complete.
899//----------------------------------------------------------------------------
900void PhyRegAlloc::printMachineCode()
901{
902
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000903 cout << endl << ";************** Method ";
904 cout << Meth->getName() << " *****************" << endl;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000905
906 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
907
908 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
909
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000910 cout << endl ; printLabel( *BBI); cout << ": ";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000911
912 // get the iterator for machine instructions
913 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
914 MachineCodeForBasicBlock::iterator MInstIterator = MIVec.begin();
915
916 // iterate over all the machine instructions in BB
917 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
918
919 MachineInstr *const MInst = *MInstIterator;
920
921
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000922 cout << endl << "\t";
923 cout << TargetInstrDescriptors[MInst->getOpCode()].opCodeString;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000924
925
926 //for(MachineInstr::val_op_const_iterator OpI(MInst);!OpI.done();++OpI) {
927
928 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
929
930 MachineOperand& Op = MInst->getOperand(OpNum);
931
932 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasanka97b8b442001-10-18 22:36:26 +0000933 Op.getOperandType() == MachineOperand::MO_CCRegister /*||
934 Op.getOperandType() == MachineOperand::MO_PCRelativeDisp*/ ) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000935
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000936 const Value *const Val = Op.getVRegValue () ;
Ruchira Sasankae727f852001-09-18 22:43:57 +0000937 // ****this code is temporary till NULL Values are fixed
938 if( ! Val ) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000939 cout << "\t<*NULL*>";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000940 continue;
941 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000942
943 // if a label or a constant
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000944 if( (Val->getValueType() == Value::BasicBlockVal) ) {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000945
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000946 cout << "\t"; printLabel( Op.getVRegValue () );
Ruchira Sasankae727f852001-09-18 22:43:57 +0000947 }
948 else {
949 // else it must be a register value
950 const int RegNum = Op.getAllocatedRegNum();
951
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +0000952 cout << "\t" << "%" << MRI.getUnifiedRegName( RegNum );
Ruchira Sasankae727f852001-09-18 22:43:57 +0000953 }
954
955 }
956 else if(Op.getOperandType() == MachineOperand::MO_MachineRegister) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000957 cout << "\t" << "%" << MRI.getUnifiedRegName(Op.getMachineRegNum());
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000958 }
959
960 else
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000961 cout << "\t" << Op; // use dump field
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000962 }
963
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000964
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000965
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000966 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
967 if( NumOfImpRefs > 0 ) {
968
969 cout << "\tImplicit:";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000970
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000971 for(unsigned z=0; z < NumOfImpRefs; z++) {
972 printValue( MInst->getImplicitRef(z) );
973 cout << "\t";
974 }
975
976 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000977
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000978 } // for all machine instructions
979
980
981 cout << endl;
982
983 } // for all BBs
984
985 cout << endl;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000986}
987
Ruchira Sasankae727f852001-09-18 22:43:57 +0000988
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000989//----------------------------------------------------------------------------
990//
991//----------------------------------------------------------------------------
992
993void PhyRegAlloc::colorCallRetArgs()
994{
995
996 CallRetInstrListType &CallRetInstList = LRI.getCallRetInstrList();
997 CallRetInstrListType::const_iterator It = CallRetInstList.begin();
998
999 for( ; It != CallRetInstList.end(); ++It ) {
1000
Ruchira Sasankaa90e7702001-10-15 16:26:38 +00001001 const MachineInstr *const CRMI = *It;
1002 unsigned OpCode = CRMI->getOpCode();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001003
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001004 // get the added instructions for this Call/Ret instruciton
1005 AddedInstrns *AI = AddedInstrMap[ CRMI ];
1006 if ( !AI ) {
1007 AI = new AddedInstrns();
1008 AddedInstrMap[ CRMI ] = AI;
1009 }
1010
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001011 // Tmp stack poistions are needed by some calls that have spilled args
1012 // So reset it before we call each such method
Vikram S. Adve12af1642001-11-08 04:48:50 +00001013 mcInfo.popAllTempValues(TM);
1014
Ruchira Sasankaa90e7702001-10-15 16:26:38 +00001015 if( (TM.getInstrInfo()).isCall( OpCode ) )
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001016 MRI.colorCallArgs( CRMI, LRI, AI, *this );
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001017
Ruchira Sasankaa90e7702001-10-15 16:26:38 +00001018 else if ( (TM.getInstrInfo()).isReturn(OpCode) )
1019 MRI.colorRetValue( CRMI, LRI, AI );
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001020
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001021 else assert( 0 && "Non Call/Ret instrn in CallRetInstrList\n" );
1022
1023 }
1024
1025}
1026
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001027
1028
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001029//----------------------------------------------------------------------------
1030
1031//----------------------------------------------------------------------------
1032void PhyRegAlloc::colorIncomingArgs()
1033{
1034 const BasicBlock *const FirstBB = Meth->front();
1035 const MachineInstr *FirstMI = *((FirstBB->getMachineInstrVec()).begin());
1036 assert( FirstMI && "No machine instruction in entry BB");
1037
1038 AddedInstrns *AI = AddedInstrMap[ FirstMI ];
1039 if ( !AI ) {
1040 AI = new AddedInstrns();
1041 AddedInstrMap[ FirstMI ] = AI;
1042 }
1043
1044 MRI.colorMethodArgs(Meth, LRI, AI );
1045}
1046
Ruchira Sasankae727f852001-09-18 22:43:57 +00001047
1048//----------------------------------------------------------------------------
1049// Used to generate a label for a basic block
1050//----------------------------------------------------------------------------
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001051void PhyRegAlloc::printLabel(const Value *const Val)
1052{
1053 if( Val->hasName() )
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001054 cout << Val->getName();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001055 else
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001056 cout << "Label" << Val;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001057}
1058
1059
Ruchira Sasankae727f852001-09-18 22:43:57 +00001060//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001061// This method calls setSugColorUsable method of each live range. This
1062// will determine whether the suggested color of LR is really usable.
1063// A suggested color is not usable when the suggested color is volatile
1064// AND when there are call interferences
1065//----------------------------------------------------------------------------
1066
1067void PhyRegAlloc::markUnusableSugColors()
1068{
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001069 if(DEBUG_RA ) cout << "\nmarking unusable suggested colors ..." << endl;
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001070
1071 // hash map iterator
1072 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
1073 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
1074
1075 for( ; HMI != HMIEnd ; ++HMI ) {
1076
1077 if( (*HMI).first ) {
1078
1079 LiveRange *L = (*HMI).second; // get the LiveRange
1080
1081 if(L) {
1082 if( L->hasSuggestedColor() ) {
1083
1084 int RCID = (L->getRegClass())->getID();
1085 if( MRI.isRegVolatile( RCID, L->getSuggestedColor()) &&
1086 L->isCallInterference() )
1087 L->setSuggestedColorUsable( false );
1088 else
1089 L->setSuggestedColorUsable( true );
1090 }
1091 } // if L->hasSuggestedColor()
1092 }
1093 } // for all LR's in hash map
1094}
1095
1096
1097
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001098//----------------------------------------------------------------------------
1099// The following method will set the stack offsets of the live ranges that
1100// are decided to be spillled. This must be called just after coloring the
1101// LRs using the graph coloring algo. For each live range that is spilled,
1102// this method allocate a new spill position on the stack.
1103//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001104
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001105void PhyRegAlloc::allocateStackSpace4SpilledLRs()
1106{
1107 if(DEBUG_RA ) cout << "\nsetting LR stack offsets ..." << endl;
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001108
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001109 // hash map iterator
1110 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
1111 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
1112
1113 for( ; HMI != HMIEnd ; ++HMI ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001114 if( (*HMI).first ) {
1115 LiveRange *L = (*HMI).second; // get the LiveRange
1116 if(L)
1117 if( ! L->hasColor() )
Vikram S. Advee85f2332001-11-12 23:40:22 +00001118 /**** NOTE: THIS SHOULD USE THE RIGHT SIZE FOR THE REG BEING PUSHED ****/
1119 L->setSpillOffFromFP(mcInfo.allocateSpilledValue(TM, Type::LongTy /*L->getType()*/ ));
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001120 }
1121 } // for all LR's in hash map
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001122}
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001123
1124
1125
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001126//----------------------------------------------------------------------------
Ruchira Sasankae727f852001-09-18 22:43:57 +00001127// The entry pont to Register Allocation
1128//----------------------------------------------------------------------------
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001129
1130void PhyRegAlloc::allocateRegisters()
1131{
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001132
1133 // make sure that we put all register classes into the RegClassList
1134 // before we call constructLiveRanges (now done in the constructor of
1135 // PhyRegAlloc class).
1136
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001137 constructLiveRanges(); // create LR info
1138
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001139 if( DEBUG_RA )
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001140 LRI.printLiveRanges();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001141
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001142 createIGNodeListsAndIGs(); // create IGNode list and IGs
1143
1144 buildInterferenceGraphs(); // build IGs in all reg classes
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001145
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001146
1147 if( DEBUG_RA ) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001148 // print all LRs in all reg classes
1149 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1150 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001151
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001152 // print IGs in all register classes
1153 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1154 RegClassList[ rc ]->printIG();
1155 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001156
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001157 LRI.coalesceLRs(); // coalesce all live ranges
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001158
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +00001159 // coalscing could not get rid of all phi's, add phi elimination
1160 // instructions
1161 // insertPhiEleminateInstrns();
1162
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001163 if( DEBUG_RA) {
1164 // print all LRs in all reg classes
1165 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1166 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001167
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001168 // print IGs in all register classes
1169 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1170 RegClassList[ rc ]->printIG();
1171 }
1172
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001173
1174 // mark un-usable suggested color before graph coloring algorithm.
1175 // When this is done, the graph coloring algo will not reserve
1176 // suggested color unnecessarily - they can be used by another LR
1177 markUnusableSugColors();
1178
1179 // color all register classes using the graph coloring algo
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001180 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1181 RegClassList[ rc ]->colorAllRegs();
1182
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001183 // Atter grpah coloring, if some LRs did not receive a color (i.e, spilled)
1184 // a poistion for such spilled LRs
1185 allocateStackSpace4SpilledLRs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001186
1187 // color incoming args and call args
1188 colorIncomingArgs();
1189 colorCallRetArgs();
1190
Ruchira Sasanka97b8b442001-10-18 22:36:26 +00001191
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001192 updateMachineCode();
Chris Lattner045e7c82001-09-19 16:26:23 +00001193 if (DEBUG_RA) {
Vikram S. Adve12af1642001-11-08 04:48:50 +00001194 MachineCodeForMethod::get(Meth).dump();
Chris Lattner045e7c82001-09-19 16:26:23 +00001195 printMachineCode(); // only for DEBUGGING
1196 }
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +00001197
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +00001198 //char ch;
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +00001199 //cin >> ch;
1200
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001201}
1202
Ruchira Sasankae727f852001-09-18 22:43:57 +00001203
1204