blob: 208d9bcbb077118976bf2be1216cab1a610dc900 [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"
Chris Lattner697954c2002-01-20 22:54:45 +000017#include <iostream>
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000018#include <math.h>
Chris Lattner697954c2002-01-20 22:54:45 +000019using std::cerr;
Vikram S. Adve12af1642001-11-08 04:48:50 +000020
21
22// ***TODO: There are several places we add instructions. Validate the order
23// of adding these instructions.
Ruchira Sasanka174bded2001-10-28 18:12:02 +000024
25
26
Chris Lattner045e7c82001-09-19 16:26:23 +000027cl::Enum<RegAllocDebugLevel_t> DEBUG_RA("dregalloc", cl::NoFlags,
28 "enable register allocation debugging information",
29 clEnumValN(RA_DEBUG_None , "n", "disable debug output"),
30 clEnumValN(RA_DEBUG_Normal , "y", "enable debug output"),
31 clEnumValN(RA_DEBUG_Verbose, "v", "enable extra debug output"), 0);
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000032
33
34//----------------------------------------------------------------------------
35// Constructor: Init local composite objects and create register classes.
36//----------------------------------------------------------------------------
Vikram S. Adve12af1642001-11-08 04:48:50 +000037PhyRegAlloc::PhyRegAlloc(Method *M,
Ruchira Sasanka8e604792001-09-14 21:18:34 +000038 const TargetMachine& tm,
39 MethodLiveVarInfo *const Lvi)
Chris Lattner697954c2002-01-20 22:54:45 +000040 : TM(tm), Meth(M),
Vikram S. Adve12af1642001-11-08 04:48:50 +000041 mcInfo(MachineCodeForMethod::get(M)),
42 LVI(Lvi), LRI(M, tm, RegClassList),
Ruchira Sasanka8e604792001-09-14 21:18:34 +000043 MRI( tm.getRegInfo() ),
44 NumOfRegClasses(MRI.getNumOfRegClasses()),
Chris Lattner697954c2002-01-20 22:54:45 +000045 LoopDepthCalc(M) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +000046
47 // create each RegisterClass and put in RegClassList
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000048 //
Chris Lattner697954c2002-01-20 22:54:45 +000049 for(unsigned int rc=0; rc < NumOfRegClasses; rc++)
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000050 RegClassList.push_back( new RegClass(M, MRI.getMachineRegClass(rc),
51 &ResColList) );
Ruchira Sasanka8e604792001-09-14 21:18:34 +000052}
53
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000054
55//----------------------------------------------------------------------------
56// Destructor: Deletes register classes
57//----------------------------------------------------------------------------
58PhyRegAlloc::~PhyRegAlloc() {
59
60 for( unsigned int rc=0; rc < NumOfRegClasses; rc++) {
61 RegClass *RC = RegClassList[rc];
62 delete RC;
63 }
64}
65
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000066//----------------------------------------------------------------------------
67// This method initally creates interference graphs (one in each reg class)
68// and IGNodeList (one in each IG). The actual nodes will be pushed later.
69//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000070void PhyRegAlloc::createIGNodeListsAndIGs()
71{
Chris Lattner697954c2002-01-20 22:54:45 +000072 if(DEBUG_RA ) cerr << "Creating LR lists ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +000073
74 // hash map iterator
75 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
76
77 // hash map end
78 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
79
80 for( ; HMI != HMIEnd ; ++HMI ) {
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +000081
82 if( (*HMI).first ) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +000083
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000084 LiveRange *L = (*HMI).second; // get the LiveRange
Ruchira Sasanka8e604792001-09-14 21:18:34 +000085
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +000086 if( !L) {
87 if( DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +000088 cerr << "\n*?!?Warning: Null liver range found for: ";
89 printValue(HMI->first); cerr << "\n";
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +000090 }
91 continue;
92 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +000093 // if the Value * is not null, and LR
94 // is not yet written to the IGNodeList
95 if( !(L->getUserIGNode()) ) {
96
97 RegClass *const RC = // RegClass of first value in the LR
98 //RegClassList [MRI.getRegClassIDOfValue(*(L->begin()))];
99 RegClassList[ L->getRegClass()->getID() ];
100
101 RC-> addLRToIG( L ); // add this LR to an IG
102 }
103 }
104 }
105
106 // init RegClassList
107 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
108 RegClassList[ rc ]->createInterferenceGraph();
109
110 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000111 cerr << "LRLists Created!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000112}
113
114
115
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000116
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000117//----------------------------------------------------------------------------
118// This method will add all interferences at for a given instruction.
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000119// Interence occurs only if the LR of Def (Inst or Arg) is of the same reg
120// class as that of live var. The live var passed to this function is the
121// LVset AFTER the instruction
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000122//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000123void PhyRegAlloc::addInterference(const Value *const Def,
124 const LiveVarSet *const LVSet,
125 const bool isCallInst) {
126
127 LiveVarSet::const_iterator LIt = LVSet->begin();
128
129 // get the live range of instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000130 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000131 const LiveRange *const LROfDef = LRI.getLiveRangeForValue( Def );
132
133 IGNode *const IGNodeOfDef = LROfDef->getUserIGNode();
134 assert( IGNodeOfDef );
135
136 RegClass *const RCOfDef = LROfDef->getRegClass();
137
138 // for each live var in live variable set
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000139 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000140 for( ; LIt != LVSet->end(); ++LIt) {
141
142 if( DEBUG_RA > 1) {
Chris Lattner697954c2002-01-20 22:54:45 +0000143 cerr << "< Def="; printValue(Def);
144 cerr << ", Lvar="; printValue( *LIt); cerr << "> ";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000145 }
146
147 // get the live range corresponding to live var
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000148 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000149 LiveRange *const LROfVar = LRI.getLiveRangeForValue(*LIt );
150
151 // LROfVar can be null if it is a const since a const
152 // doesn't have a dominating def - see Assumptions above
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000153 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000154 if( LROfVar) {
155
156 if(LROfDef == LROfVar) // do not set interf for same LR
157 continue;
158
159 // if 2 reg classes are the same set interference
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000160 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000161 if( RCOfDef == LROfVar->getRegClass() ){
162 RCOfDef->setInterference( LROfDef, LROfVar);
163
164 }
165
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000166 else if(DEBUG_RA > 1) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000167 // we will not have LRs for values not explicitly allocated in the
168 // instruction stream (e.g., constants)
Chris Lattner697954c2002-01-20 22:54:45 +0000169 cerr << " warning: no live range for " ;
170 printValue(*LIt); cerr << "\n"; }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000171
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000172 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000173
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000174 }
175
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000176}
177
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000178
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000179
180
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000181//----------------------------------------------------------------------------
182// For a call instruction, this method sets the CallInterference flag in
183// the LR of each variable live int the Live Variable Set live after the
184// call instruction (except the return value of the call instruction - since
185// the return value does not interfere with that call itself).
186//----------------------------------------------------------------------------
187
188void PhyRegAlloc::setCallInterferences(const MachineInstr *MInst,
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000189 const LiveVarSet *const LVSetAft ) {
190
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000191 // Now find the LR of the return value of the call
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000192 // We do this because, we look at the LV set *after* the instruction
193 // to determine, which LRs must be saved across calls. The return value
194 // of the call is live in this set - but it does not interfere with call
195 // (i.e., we can allocate a volatile register to the return value)
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000196 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000197 LiveRange *RetValLR = NULL;
Ruchira Sasankab3b6f532001-10-21 16:43:41 +0000198 const Value *RetVal = MRI.getCallInstRetVal( MInst );
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000199
Ruchira Sasankab3b6f532001-10-21 16:43:41 +0000200 if( RetVal ) {
201 RetValLR = LRI.getLiveRangeForValue( RetVal );
202 assert( RetValLR && "No LR for RetValue of call");
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000203 }
204
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000205 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000206 cerr << "\n For call inst: " << *MInst;
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000207
208 LiveVarSet::const_iterator LIt = LVSetAft->begin();
209
210 // for each live var in live variable set after machine inst
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000211 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000212 for( ; LIt != LVSetAft->end(); ++LIt) {
213
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000214 // get the live range corresponding to live var
215 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000216 LiveRange *const LR = LRI.getLiveRangeForValue(*LIt );
217
218 if( LR && DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000219 cerr << "\n\tLR Aft Call: ";
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000220 LR->printSet();
221 }
222
223
224 // LR can be null if it is a const since a const
225 // doesn't have a dominating def - see Assumptions above
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000226 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000227 if( LR && (LR != RetValLR) ) {
228 LR->setCallInterference();
229 if( DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000230 cerr << "\n ++Added call interf for LR: " ;
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000231 LR->printSet();
232 }
233 }
234
235 }
236
237}
238
239
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000240
241
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000242//----------------------------------------------------------------------------
243// This method will walk thru code and create interferences in the IG of
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000244// each RegClass. Also, this method calculates the spill cost of each
245// Live Range (it is done in this method to save another pass over the code).
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000246//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000247void PhyRegAlloc::buildInterferenceGraphs()
248{
249
Chris Lattner697954c2002-01-20 22:54:45 +0000250 if(DEBUG_RA) cerr << "Creating interference graphs ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000251
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000252 unsigned BBLoopDepthCost;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000253 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
254
255 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
256
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000257 // find the 10^(loop_depth) of this BB
258 //
259 BBLoopDepthCost = (unsigned) pow( 10.0, LoopDepthCalc.getLoopDepth(*BBI));
260
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000261 // get the iterator for machine instructions
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000262 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000263 const MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
264 MachineCodeForBasicBlock::const_iterator
265 MInstIterator = MIVec.begin();
266
267 // iterate over all the machine instructions in BB
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000268 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000269 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000270
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +0000271 const MachineInstr * MInst = *MInstIterator;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000272
273 // get the LV set after the instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000274 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000275 const LiveVarSet *const LVSetAI =
276 LVI->getLiveVarSetAfterMInst(MInst, *BBI);
277
278 const bool isCallInst = TM.getInstrInfo().isCall(MInst->getOpCode());
279
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000280 if( isCallInst ) {
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000281 // set the isCallInterference flag of each live range wich extends
282 // accross this call instruction. This information is used by graph
283 // coloring algo to avoid allocating volatile colors to live ranges
284 // that span across calls (since they have to be saved/restored)
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000285 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000286 setCallInterferences( MInst, LVSetAI);
287 }
288
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000289
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000290 // iterate over all MI operands to find defs
291 //
Chris Lattner7a176752001-12-04 00:03:30 +0000292 for( MachineInstr::val_const_op_iterator OpI(MInst);!OpI.done(); ++OpI) {
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000293
294 if( OpI.isDef() ) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000295 // create a new LR iff this operand is a def
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000296 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000297 addInterference(*OpI, LVSetAI, isCallInst );
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000298 }
299
300 // Calculate the spill cost of each live range
301 //
302 LiveRange *LR = LRI.getLiveRangeForValue( *OpI );
303 if( LR )
304 LR->addSpillCost(BBLoopDepthCost);
305 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000306
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000307
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000308 // if there are multiple defs in this instruction e.g. in SETX
309 //
310 if( (TM.getInstrInfo()).isPseudoInstr( MInst->getOpCode()) )
311 addInterf4PseudoInstr(MInst);
312
313
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000314 // Also add interference for any implicit definitions in a machine
315 // instr (currently, only calls have this).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000316 //
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000317 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
318 if( NumOfImpRefs > 0 ) {
319 for(unsigned z=0; z < NumOfImpRefs; z++)
320 if( MInst->implicitRefIsDefined(z) )
321 addInterference( MInst->getImplicitRef(z), LVSetAI, isCallInst );
322 }
323
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +0000324
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000325 } // for all machine instructions in BB
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000326
327 } // for all BBs in method
328
329
330 // add interferences for method arguments. Since there are no explict
331 // defs in method for args, we have to add them manually
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000332 //
333 addInterferencesForArgs();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000334
335 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000336 cerr << "Interference graphs calculted!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000337
338}
339
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000340
341
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000342//--------------------------------------------------------------------------
343// Pseudo instructions will be exapnded to multiple instructions by the
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000344// assembler. Consequently, all the opernds must get distinct registers.
345// Therefore, we mark all operands of a pseudo instruction as they interfere
346// with one another.
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000347//--------------------------------------------------------------------------
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000348void PhyRegAlloc::addInterf4PseudoInstr(const MachineInstr *MInst) {
349
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000350 bool setInterf = false;
351
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000352 // iterate over MI operands to find defs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000353 //
Chris Lattner7a176752001-12-04 00:03:30 +0000354 for( MachineInstr::val_const_op_iterator It1(MInst);!It1.done(); ++It1) {
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000355
356 const LiveRange *const LROfOp1 = LRI.getLiveRangeForValue( *It1 );
357
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000358 if( !LROfOp1 && It1.isDef() )
359 assert( 0 && "No LR for Def in PSEUDO insruction");
360
Chris Lattner7a176752001-12-04 00:03:30 +0000361 MachineInstr::val_const_op_iterator It2 = It1;
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000362 ++It2;
363
364 for( ; !It2.done(); ++It2) {
365
366 const LiveRange *const LROfOp2 = LRI.getLiveRangeForValue( *It2 );
367
368 if( LROfOp2) {
369
370 RegClass *const RCOfOp1 = LROfOp1->getRegClass();
371 RegClass *const RCOfOp2 = LROfOp2->getRegClass();
372
373 if( RCOfOp1 == RCOfOp2 ){
374 RCOfOp1->setInterference( LROfOp1, LROfOp2 );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000375 setInterf = true;
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000376 }
377
378 } // if Op2 has a LR
379
380 } // for all other defs in machine instr
381
382 } // for all operands in an instruction
383
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000384 if( !setInterf && (MInst->getNumOperands() > 2) ) {
385 cerr << "\nInterf not set for any operand in pseudo instr:\n";
386 cerr << *MInst;
387 assert(0 && "Interf not set for pseudo instr with > 2 operands" );
388
389 }
390
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000391}
392
393
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000394
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000395//----------------------------------------------------------------------------
396// This method will add interferences for incoming arguments to a method.
397//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000398void PhyRegAlloc::addInterferencesForArgs()
399{
400 // get the InSet of root BB
401 const LiveVarSet *const InSet = LVI->getInSetOfBB( Meth->front() );
402
403 // get the argument list
404 const Method::ArgumentListType& ArgList = Meth->getArgumentList();
405
406 // get an iterator to arg list
407 Method::ArgumentListType::const_iterator ArgIt = ArgList.begin();
408
409
410 for( ; ArgIt != ArgList.end() ; ++ArgIt) { // for each argument
411 addInterference( *ArgIt, InSet, false ); // add interferences between
412 // args and LVars at start
413 if( DEBUG_RA > 1) {
Chris Lattner697954c2002-01-20 22:54:45 +0000414 cerr << " - %% adding interference for argument ";
415 printValue((const Value *)*ArgIt); cerr << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000416 }
417 }
418}
419
420
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000421
422
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000423//----------------------------------------------------------------------------
424// This method is called after register allocation is complete to set the
425// allocated reisters in the machine code. This code will add register numbers
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000426// to MachineOperands that contain a Value. Also it calls target specific
427// methods to produce caller saving instructions. At the end, it adds all
428// additional instructions produced by the register allocator to the
429// instruction stream.
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000430//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000431void PhyRegAlloc::updateMachineCode()
432{
433
434 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
435
436 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
437
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000438 // get the iterator for machine instructions
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000439 //
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000440 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
441 MachineCodeForBasicBlock::iterator MInstIterator = MIVec.begin();
442
443 // iterate over all the machine instructions in BB
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000444 //
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000445 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
446
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000447 MachineInstr *MInst = *MInstIterator;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000448
449 unsigned Opcode = MInst->getOpCode();
450
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000451 // do not process Phis
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000452 if( (TM.getInstrInfo()).isPhi( Opcode ) )
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000453 continue;
454
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000455 // Now insert speical instructions (if necessary) for call/return
456 // instructions.
457 //
458 if( (TM.getInstrInfo()).isCall( Opcode) ||
459 (TM.getInstrInfo()).isReturn( Opcode) ) {
460
461 AddedInstrns *AI = AddedInstrMap[ MInst];
462 if ( !AI ) {
463 AI = new AddedInstrns();
464 AddedInstrMap[ MInst ] = AI;
465 }
466
467 // Tmp stack poistions are needed by some calls that have spilled args
468 // So reset it before we call each such method
Ruchira Sasanka6a3db8c2002-01-07 21:09:06 +0000469 //
470 mcInfo.popAllTempValues(TM);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000471
472 if( (TM.getInstrInfo()).isCall( Opcode ) )
473 MRI.colorCallArgs( MInst, LRI, AI, *this, *BBI );
474
475 else if ( (TM.getInstrInfo()).isReturn(Opcode) )
476 MRI.colorRetValue( MInst, LRI, AI );
477
478 }
479
480
481 /* -- Using above code instead of this
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000482
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000483 // if this machine instr is call, insert caller saving code
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000484
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000485 if( (TM.getInstrInfo()).isCall( MInst->getOpCode()) )
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000486 MRI.insertCallerSavingCode(MInst, *BBI, *this );
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000487
488 */
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000489
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000490
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000491 // reset the stack offset for temporary variables since we may
492 // need that to spill
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000493 // mcInfo.popAllTempValues(TM);
Ruchira Sasankaf90870f2001-11-15 22:02:06 +0000494 // TODO ** : do later
Vikram S. Adve12af1642001-11-08 04:48:50 +0000495
Chris Lattner7a176752001-12-04 00:03:30 +0000496 //for(MachineInstr::val_const_op_iterator OpI(MInst);!OpI.done();++OpI) {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000497
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000498
499 // Now replace set the registers for operands in the machine instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000500 //
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000501 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
502
503 MachineOperand& Op = MInst->getOperand(OpNum);
504
505 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
506 Op.getOperandType() == MachineOperand::MO_CCRegister) {
507
508 const Value *const Val = Op.getVRegValue();
509
510 // delete this condition checking later (must assert if Val is null)
Chris Lattner045e7c82001-09-19 16:26:23 +0000511 if( !Val) {
512 if (DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000513 cerr << "Warning: NULL Value found for operand\n";
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000514 continue;
515 }
516 assert( Val && "Value is NULL");
517
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000518 LiveRange *const LR = LRI.getLiveRangeForValue(Val);
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000519
520 if ( !LR ) {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000521
522 // nothing to worry if it's a const or a label
523
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000524 if (DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000525 cerr << "*NO LR for operand : " << Op ;
526 cerr << " [reg:" << Op.getAllocatedRegNum() << "]";
527 cerr << " in inst:\t" << *MInst << "\n";
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000528 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000529
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000530 // if register is not allocated, mark register as invalid
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000531 if( Op.getAllocatedRegNum() == -1)
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000532 Op.setRegForValue( MRI.getInvalidRegNum());
Ruchira Sasankae727f852001-09-18 22:43:57 +0000533
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000534
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000535 continue;
536 }
537
538 unsigned RCID = (LR->getRegClass())->getID();
539
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000540 if( LR->hasColor() ) {
541 Op.setRegForValue( MRI.getUnifiedRegNum(RCID, LR->getColor()) );
542 }
543 else {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000544
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000545 // LR did NOT receive a color (register). Now, insert spill code
546 // for spilled opeands in this machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000547
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000548 //assert(0 && "LR must be spilled");
549 insertCode4SpilledLR(LR, MInst, *BBI, OpNum );
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000550
551 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000552 }
553
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000554 } // for each operand
555
556
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000557 // Now add instructions that the register allocator inserts before/after
558 // this machine instructions (done only for calls/rets/incoming args)
559 // We do this here, to ensure that spill for an instruction is inserted
560 // closest as possible to an instruction (see above insertCode4Spill...)
561 //
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000562 // If there are instructions to be added, *before* this machine
563 // instruction, add them now.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000564 //
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000565 if( AddedInstrMap[ MInst ] ) {
Chris Lattner697954c2002-01-20 22:54:45 +0000566 std::deque<MachineInstr *> &IBef = AddedInstrMap[MInst]->InstrnsBefore;
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000567
568 if( ! IBef.empty() ) {
Chris Lattner697954c2002-01-20 22:54:45 +0000569 std::deque<MachineInstr *>::iterator AdIt;
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000570
571 for( AdIt = IBef.begin(); AdIt != IBef.end() ; ++AdIt ) {
572
573 if( DEBUG_RA) {
574 cerr << "For inst " << *MInst;
Chris Lattner697954c2002-01-20 22:54:45 +0000575 cerr << " PREPENDed instr: " << **AdIt << "\n";
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000576 }
577
578 MInstIterator = MIVec.insert( MInstIterator, *AdIt );
579 ++MInstIterator;
580 }
581
582 }
583
584 }
585
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000586 // If there are instructions to be added *after* this machine
587 // instruction, add them now
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000588 //
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000589 if( AddedInstrMap[ MInst ] &&
590 ! (AddedInstrMap[ MInst ]->InstrnsAfter).empty() ) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000591
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000592 // if there are delay slots for this instruction, the instructions
593 // added after it must really go after the delayed instruction(s)
594 // So, we move the InstrAfter of the current instruction to the
595 // corresponding delayed instruction
596
597 unsigned delay;
598 if((delay=TM.getInstrInfo().getNumDelaySlots(MInst->getOpCode())) >0){
599 move2DelayedInstr(MInst, *(MInstIterator+delay) );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000600
Chris Lattner697954c2002-01-20 22:54:45 +0000601 if(DEBUG_RA) cerr<< "\nMoved an added instr after the delay slot";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000602 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000603
604 else {
605
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000606
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000607 // Here we can add the "instructions after" to the current
608 // instruction since there are no delay slots for this instruction
609
Chris Lattner697954c2002-01-20 22:54:45 +0000610 std::deque<MachineInstr *> &IAft = AddedInstrMap[MInst]->InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000611
612 if( ! IAft.empty() ) {
613
Chris Lattner697954c2002-01-20 22:54:45 +0000614 std::deque<MachineInstr *>::iterator AdIt;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000615
616 ++MInstIterator; // advance to the next instruction
617
618 for( AdIt = IAft.begin(); AdIt != IAft.end() ; ++AdIt ) {
619
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000620 if(DEBUG_RA) {
621 cerr << "For inst " << *MInst;
Chris Lattner697954c2002-01-20 22:54:45 +0000622 cerr << " APPENDed instr: " << **AdIt << "\n";
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000623 }
624
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000625 MInstIterator = MIVec.insert( MInstIterator, *AdIt );
626 ++MInstIterator;
627 }
628
629 // MInsterator already points to the next instr. Since the
630 // for loop also increments it, decrement it to point to the
631 // instruction added last
632 --MInstIterator;
633
634 }
635
636 } // if not delay
637
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000638 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000639
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000640 } // for each machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000641 }
642}
643
644
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000645
646//----------------------------------------------------------------------------
647// This method inserts spill code for AN operand whose LR was spilled.
648// This method may be called several times for a single machine instruction
649// if it contains many spilled operands. Each time it is called, it finds
650// a register which is not live at that instruction and also which is not
651// used by other spilled operands of the same instruction. Then it uses
652// this register temporarily to accomodate the spilled value.
653//----------------------------------------------------------------------------
654void PhyRegAlloc::insertCode4SpilledLR(const LiveRange *LR,
655 MachineInstr *MInst,
656 const BasicBlock *BB,
657 const unsigned OpNum) {
658
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000659 assert(! TM.getInstrInfo().isCall(MInst->getOpCode()) &&
660 (! TM.getInstrInfo().isReturn(MInst->getOpCode())) &&
661 "Arg of a call/ret must be handled elsewhere");
662
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000663 MachineOperand& Op = MInst->getOperand(OpNum);
664 bool isDef = MInst->operandIsDefined(OpNum);
665 unsigned RegType = MRI.getRegType( LR );
666 int SpillOff = LR->getSpillOffFromFP();
667 RegClass *RC = LR->getRegClass();
668 const LiveVarSet *LVSetBef = LVI->getLiveVarSetBeforeMInst(MInst, BB);
Vikram S. Adve00521d72001-11-12 23:26:35 +0000669
Chris Lattner697954c2002-01-20 22:54:45 +0000670 mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000671
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000672 MachineInstr *MIBef=NULL, *AdIMid=NULL, *MIAft=NULL;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000673
674 int TmpRegU = getUsableUniRegAtMI(RC, RegType, MInst,LVSetBef, MIBef, MIAft);
675
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000676 // get the added instructions for this instruciton
677 AddedInstrns *AI = AddedInstrMap[ MInst ];
678 if ( !AI ) {
679 AI = new AddedInstrns();
680 AddedInstrMap[ MInst ] = AI;
681 }
682
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000683
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000684 if( !isDef ) {
685
686 // for a USE, we have to load the value of LR from stack to a TmpReg
687 // and use the TmpReg as one operand of instruction
688
689 // actual loading instruction
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000690 AdIMid = MRI.cpMem2RegMI(MRI.getFramePointer(), SpillOff, TmpRegU,RegType);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000691
692 if( MIBef )
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000693 (AI->InstrnsBefore).push_back(MIBef);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000694
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000695 (AI->InstrnsBefore).push_back(AdIMid);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000696
697 if( MIAft)
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000698 (AI->InstrnsAfter).push_front(MIAft);
699
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000700
701 }
702 else { // if this is a Def
703
704 // for a DEF, we have to store the value produced by this instruction
705 // on the stack position allocated for this LR
706
707 // actual storing instruction
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000708 AdIMid = MRI.cpReg2MemMI(TmpRegU, MRI.getFramePointer(), SpillOff,RegType);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000709
710 if( MIBef )
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000711 (AI->InstrnsBefore).push_back(MIBef);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000712
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000713 (AI->InstrnsAfter).push_front(AdIMid);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000714
715 if( MIAft)
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000716 (AI->InstrnsAfter).push_front(MIAft);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000717
718 } // if !DEF
719
720 cerr << "\nFor Inst " << *MInst;
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000721 cerr << " - SPILLED LR: "; LR->printSet();
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000722 cerr << "\n - Added Instructions:";
723 if( MIBef ) cerr << *MIBef;
724 cerr << *AdIMid;
725 if( MIAft ) cerr << *MIAft;
726
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000727 Op.setRegForValue( TmpRegU ); // set the opearnd
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000728
729
730}
731
732
733
734
735
736
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000737//----------------------------------------------------------------------------
738// We can use the following method to get a temporary register to be used
739// BEFORE any given machine instruction. If there is a register available,
740// this method will simply return that register and set MIBef = MIAft = NULL.
741// Otherwise, it will return a register and MIAft and MIBef will contain
742// two instructions used to free up this returned register.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000743// Returned register number is the UNIFIED register number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000744//----------------------------------------------------------------------------
745
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000746int PhyRegAlloc::getUsableUniRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000747 const int RegType,
748 const MachineInstr *MInst,
749 const LiveVarSet *LVSetBef,
750 MachineInstr *MIBef,
751 MachineInstr *MIAft) {
752
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000753 int RegU = getUnusedUniRegAtMI(RC, MInst, LVSetBef);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000754
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000755
756 if( RegU != -1) {
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000757 // we found an unused register, so we can simply use it
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000758 MIBef = MIAft = NULL;
759 }
760 else {
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000761 // we couldn't find an unused register. Generate code to free up a reg by
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000762 // saving it on stack and restoring after the instruction
763
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000764 int TmpOff = mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
Vikram S. Adve12af1642001-11-08 04:48:50 +0000765
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000766 RegU = getUniRegNotUsedByThisInst(RC, MInst);
767 MIBef = MRI.cpReg2MemMI(RegU, MRI.getFramePointer(), TmpOff, RegType );
768 MIAft = MRI.cpMem2RegMI(MRI.getFramePointer(), TmpOff, RegU, RegType );
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000769 }
770
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000771 return RegU;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000772}
773
774//----------------------------------------------------------------------------
775// This method is called to get a new unused register that can be used to
776// accomodate a spilled value.
777// This method may be called several times for a single machine instruction
778// if it contains many spilled operands. Each time it is called, it finds
779// a register which is not live at that instruction and also which is not
780// used by other spilled operands of the same instruction.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000781// Return register number is relative to the register class. NOT
782// unified number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000783//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000784int PhyRegAlloc::getUnusedUniRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000785 const MachineInstr *MInst,
786 const LiveVarSet *LVSetBef) {
787
788 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
789
790 bool *IsColorUsedArr = RC->getIsColorUsedArr();
791
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000792 for(unsigned i=0; i < NumAvailRegs; i++) // Reset array
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000793 IsColorUsedArr[i] = false;
794
795 LiveVarSet::const_iterator LIt = LVSetBef->begin();
796
797 // for each live var in live variable set after machine inst
798 for( ; LIt != LVSetBef->end(); ++LIt) {
799
800 // get the live range corresponding to live var
801 LiveRange *const LRofLV = LRI.getLiveRangeForValue(*LIt );
802
803 // LR can be null if it is a const since a const
804 // doesn't have a dominating def - see Assumptions above
805 if( LRofLV )
806 if( LRofLV->hasColor() )
807 IsColorUsedArr[ LRofLV->getColor() ] = true;
808 }
809
810 // It is possible that one operand of this MInst was already spilled
811 // and it received some register temporarily. If that's the case,
812 // it is recorded in machine operand. We must skip such registers.
813
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000814 setRelRegsUsedByThisInst(RC, MInst);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000815
816 unsigned c; // find first unused color
817 for( c=0; c < NumAvailRegs; c++)
818 if( ! IsColorUsedArr[ c ] ) break;
819
820 if(c < NumAvailRegs)
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000821 return MRI.getUnifiedRegNum(RC->getID(), c);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000822 else
823 return -1;
824
825
826}
827
828
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000829//----------------------------------------------------------------------------
830// Get any other register in a register class, other than what is used
831// by operands of a machine instruction. Returns the unified reg number.
832//----------------------------------------------------------------------------
833int PhyRegAlloc::getUniRegNotUsedByThisInst(RegClass *RC,
834 const MachineInstr *MInst) {
835
836 bool *IsColorUsedArr = RC->getIsColorUsedArr();
837 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
838
839
840 for(unsigned i=0; i < NumAvailRegs ; i++) // Reset array
841 IsColorUsedArr[i] = false;
842
843 setRelRegsUsedByThisInst(RC, MInst);
844
845 unsigned c; // find first unused color
846 for( c=0; c < RC->getNumOfAvailRegs(); c++)
847 if( ! IsColorUsedArr[ c ] ) break;
848
849 if(c < NumAvailRegs)
850 return MRI.getUnifiedRegNum(RC->getID(), c);
851 else
852 assert( 0 && "FATAL: No free register could be found in reg class!!");
Chris Lattner697954c2002-01-20 22:54:45 +0000853 return 0;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000854}
855
856
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000857//----------------------------------------------------------------------------
858// This method modifies the IsColorUsedArr of the register class passed to it.
859// It sets the bits corresponding to the registers used by this machine
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000860// instructions. Both explicit and implicit operands are set.
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000861//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000862void PhyRegAlloc::setRelRegsUsedByThisInst(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000863 const MachineInstr *MInst ) {
864
865 bool *IsColorUsedArr = RC->getIsColorUsedArr();
866
867 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
868
869 const MachineOperand& Op = MInst->getOperand(OpNum);
870
871 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000872 Op.getOperandType() == MachineOperand::MO_CCRegister ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000873
874 const Value *const Val = Op.getVRegValue();
875
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000876 if( Val )
877 if( MRI.getRegClassIDOfValue(Val) == RC->getID() ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000878 int Reg;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000879 if( (Reg=Op.getAllocatedRegNum()) != -1) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000880 IsColorUsedArr[ Reg ] = true;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000881 }
882 else {
883 // it is possilbe that this operand still is not marked with
884 // a register but it has a LR and that received a color
885
886 LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
887 if( LROfVal)
888 if( LROfVal->hasColor() )
889 IsColorUsedArr[ LROfVal->getColor() ] = true;
890 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000891
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000892 } // if reg classes are the same
893 }
894 else if (Op.getOperandType() == MachineOperand::MO_MachineRegister) {
895 IsColorUsedArr[ Op.getMachineRegNum() ] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000896 }
897 }
898
899 // If there are implicit references, mark them as well
900
901 for(unsigned z=0; z < MInst->getNumImplicitRefs(); z++) {
902
903 LiveRange *const LRofImpRef =
904 LRI.getLiveRangeForValue( MInst->getImplicitRef(z) );
Chris Lattner697954c2002-01-20 22:54:45 +0000905
906 if(LRofImpRef && LRofImpRef->hasColor())
907 IsColorUsedArr[LRofImpRef->getColor()] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000908 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000909}
910
911
912
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000913
914
915
916
917
918//----------------------------------------------------------------------------
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000919// If there are delay slots for an instruction, the instructions
920// added after it must really go after the delayed instruction(s).
921// So, we move the InstrAfter of that instruction to the
922// corresponding delayed instruction using the following method.
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000923
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000924//----------------------------------------------------------------------------
925void PhyRegAlloc:: move2DelayedInstr(const MachineInstr *OrigMI,
926 const MachineInstr *DelayedMI) {
927
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000928 // "added after" instructions of the original instr
Chris Lattner697954c2002-01-20 22:54:45 +0000929 std::deque<MachineInstr *> &OrigAft = AddedInstrMap[OrigMI]->InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000930
931 // "added instructions" of the delayed instr
932 AddedInstrns *DelayAdI = AddedInstrMap[DelayedMI];
933
934 if(! DelayAdI ) { // create a new "added after" if necessary
935 DelayAdI = new AddedInstrns();
936 AddedInstrMap[DelayedMI] = DelayAdI;
937 }
938
939 // "added after" instructions of the delayed instr
Chris Lattner697954c2002-01-20 22:54:45 +0000940 std::deque<MachineInstr *> &DelayedAft = DelayAdI->InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000941
942 // go thru all the "added after instructions" of the original instruction
943 // and append them to the "addded after instructions" of the delayed
944 // instructions
Chris Lattner697954c2002-01-20 22:54:45 +0000945 DelayedAft.insert(DelayedAft.end(), OrigAft.begin(), OrigAft.end());
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000946
947 // empty the "added after instructions" of the original instruction
948 OrigAft.clear();
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000949}
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000950
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000951//----------------------------------------------------------------------------
952// This method prints the code with registers after register allocation is
953// complete.
954//----------------------------------------------------------------------------
955void PhyRegAlloc::printMachineCode()
956{
957
Chris Lattner697954c2002-01-20 22:54:45 +0000958 cerr << "\n;************** Method " << Meth->getName()
959 << " *****************\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000960
961 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
962
963 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
964
Chris Lattner697954c2002-01-20 22:54:45 +0000965 cerr << "\n"; printLabel( *BBI); cerr << ": ";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000966
967 // get the iterator for machine instructions
968 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
969 MachineCodeForBasicBlock::iterator MInstIterator = MIVec.begin();
970
971 // iterate over all the machine instructions in BB
972 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
973
974 MachineInstr *const MInst = *MInstIterator;
975
976
Chris Lattner697954c2002-01-20 22:54:45 +0000977 cerr << "\n\t";
978 cerr << TargetInstrDescriptors[MInst->getOpCode()].opCodeString;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000979
980
Chris Lattner7a176752001-12-04 00:03:30 +0000981 //for(MachineInstr::val_const_op_iterator OpI(MInst);!OpI.done();++OpI) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000982
983 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
984
985 MachineOperand& Op = MInst->getOperand(OpNum);
986
987 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasanka97b8b442001-10-18 22:36:26 +0000988 Op.getOperandType() == MachineOperand::MO_CCRegister /*||
989 Op.getOperandType() == MachineOperand::MO_PCRelativeDisp*/ ) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000990
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000991 const Value *const Val = Op.getVRegValue () ;
Ruchira Sasankae727f852001-09-18 22:43:57 +0000992 // ****this code is temporary till NULL Values are fixed
993 if( ! Val ) {
Chris Lattner697954c2002-01-20 22:54:45 +0000994 cerr << "\t<*NULL*>";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000995 continue;
996 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000997
998 // if a label or a constant
Chris Lattnerdbe53042002-01-21 01:33:12 +0000999 if(isa<BasicBlock>(Val)) {
Chris Lattner697954c2002-01-20 22:54:45 +00001000 cerr << "\t"; printLabel( Op.getVRegValue () );
1001 } else {
Ruchira Sasankae727f852001-09-18 22:43:57 +00001002 // else it must be a register value
1003 const int RegNum = Op.getAllocatedRegNum();
1004
Chris Lattner697954c2002-01-20 22:54:45 +00001005 cerr << "\t" << "%" << MRI.getUnifiedRegName( RegNum );
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001006 if (Val->hasName() )
Chris Lattner697954c2002-01-20 22:54:45 +00001007 cerr << "(" << Val->getName() << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001008 else
Chris Lattner697954c2002-01-20 22:54:45 +00001009 cerr << "(" << Val << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001010
1011 if( Op.opIsDef() )
Chris Lattner697954c2002-01-20 22:54:45 +00001012 cerr << "*";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001013
1014 const LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
1015 if( LROfVal )
1016 if( LROfVal->hasSpillOffset() )
Chris Lattner697954c2002-01-20 22:54:45 +00001017 cerr << "$";
Ruchira Sasankae727f852001-09-18 22:43:57 +00001018 }
1019
1020 }
1021 else if(Op.getOperandType() == MachineOperand::MO_MachineRegister) {
Chris Lattner697954c2002-01-20 22:54:45 +00001022 cerr << "\t" << "%" << MRI.getUnifiedRegName(Op.getMachineRegNum());
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001023 }
1024
1025 else
Chris Lattner697954c2002-01-20 22:54:45 +00001026 cerr << "\t" << Op; // use dump field
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001027 }
1028
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001029
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001030
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001031 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
1032 if( NumOfImpRefs > 0 ) {
1033
Chris Lattner697954c2002-01-20 22:54:45 +00001034 cerr << "\tImplicit:";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001035
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001036 for(unsigned z=0; z < NumOfImpRefs; z++) {
1037 printValue( MInst->getImplicitRef(z) );
Chris Lattner697954c2002-01-20 22:54:45 +00001038 cerr << "\t";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001039 }
1040
1041 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001042
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001043 } // for all machine instructions
1044
Chris Lattner697954c2002-01-20 22:54:45 +00001045 cerr << "\n";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001046
1047 } // for all BBs
1048
Chris Lattner697954c2002-01-20 22:54:45 +00001049 cerr << "\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001050}
1051
Ruchira Sasankae727f852001-09-18 22:43:57 +00001052
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001053#if 0
1054
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001055//----------------------------------------------------------------------------
1056//
1057//----------------------------------------------------------------------------
1058
1059void PhyRegAlloc::colorCallRetArgs()
1060{
1061
1062 CallRetInstrListType &CallRetInstList = LRI.getCallRetInstrList();
1063 CallRetInstrListType::const_iterator It = CallRetInstList.begin();
1064
1065 for( ; It != CallRetInstList.end(); ++It ) {
1066
Ruchira Sasankaa90e7702001-10-15 16:26:38 +00001067 const MachineInstr *const CRMI = *It;
1068 unsigned OpCode = CRMI->getOpCode();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001069
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001070 // get the added instructions for this Call/Ret instruciton
1071 AddedInstrns *AI = AddedInstrMap[ CRMI ];
1072 if ( !AI ) {
1073 AI = new AddedInstrns();
1074 AddedInstrMap[ CRMI ] = AI;
1075 }
1076
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001077 // Tmp stack poistions are needed by some calls that have spilled args
1078 // So reset it before we call each such method
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001079 //mcInfo.popAllTempValues(TM);
1080
1081
Vikram S. Adve12af1642001-11-08 04:48:50 +00001082
Ruchira Sasankaa90e7702001-10-15 16:26:38 +00001083 if( (TM.getInstrInfo()).isCall( OpCode ) )
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001084 MRI.colorCallArgs( CRMI, LRI, AI, *this );
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001085
Ruchira Sasankaa90e7702001-10-15 16:26:38 +00001086 else if ( (TM.getInstrInfo()).isReturn(OpCode) )
1087 MRI.colorRetValue( CRMI, LRI, AI );
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001088
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001089 else assert( 0 && "Non Call/Ret instrn in CallRetInstrList\n" );
1090
1091 }
1092
1093}
1094
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001095#endif
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001096
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001097//----------------------------------------------------------------------------
1098
1099//----------------------------------------------------------------------------
1100void PhyRegAlloc::colorIncomingArgs()
1101{
1102 const BasicBlock *const FirstBB = Meth->front();
1103 const MachineInstr *FirstMI = *((FirstBB->getMachineInstrVec()).begin());
1104 assert( FirstMI && "No machine instruction in entry BB");
1105
1106 AddedInstrns *AI = AddedInstrMap[ FirstMI ];
Chris Lattner697954c2002-01-20 22:54:45 +00001107 if (!AI) {
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001108 AI = new AddedInstrns();
Chris Lattner697954c2002-01-20 22:54:45 +00001109 AddedInstrMap[FirstMI] = AI;
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001110 }
1111
1112 MRI.colorMethodArgs(Meth, LRI, AI );
1113}
1114
Ruchira Sasankae727f852001-09-18 22:43:57 +00001115
1116//----------------------------------------------------------------------------
1117// Used to generate a label for a basic block
1118//----------------------------------------------------------------------------
Chris Lattner697954c2002-01-20 22:54:45 +00001119void PhyRegAlloc::printLabel(const Value *const Val) {
1120 if (Val->hasName())
1121 cerr << Val->getName();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001122 else
Chris Lattner697954c2002-01-20 22:54:45 +00001123 cerr << "Label" << Val;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001124}
1125
1126
Ruchira Sasankae727f852001-09-18 22:43:57 +00001127//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001128// This method calls setSugColorUsable method of each live range. This
1129// will determine whether the suggested color of LR is really usable.
1130// A suggested color is not usable when the suggested color is volatile
1131// AND when there are call interferences
1132//----------------------------------------------------------------------------
1133
1134void PhyRegAlloc::markUnusableSugColors()
1135{
Chris Lattner697954c2002-01-20 22:54:45 +00001136 if(DEBUG_RA ) cerr << "\nmarking unusable suggested colors ...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001137
1138 // hash map iterator
1139 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
1140 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
1141
1142 for( ; HMI != HMIEnd ; ++HMI ) {
1143
1144 if( (*HMI).first ) {
1145
1146 LiveRange *L = (*HMI).second; // get the LiveRange
1147
1148 if(L) {
1149 if( L->hasSuggestedColor() ) {
1150
1151 int RCID = (L->getRegClass())->getID();
1152 if( MRI.isRegVolatile( RCID, L->getSuggestedColor()) &&
1153 L->isCallInterference() )
1154 L->setSuggestedColorUsable( false );
1155 else
1156 L->setSuggestedColorUsable( true );
1157 }
1158 } // if L->hasSuggestedColor()
1159 }
1160 } // for all LR's in hash map
1161}
1162
1163
1164
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001165//----------------------------------------------------------------------------
1166// The following method will set the stack offsets of the live ranges that
1167// are decided to be spillled. This must be called just after coloring the
1168// LRs using the graph coloring algo. For each live range that is spilled,
1169// this method allocate a new spill position on the stack.
1170//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001171
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001172void PhyRegAlloc::allocateStackSpace4SpilledLRs()
1173{
Chris Lattner697954c2002-01-20 22:54:45 +00001174 if(DEBUG_RA ) cerr << "\nsetting LR stack offsets ...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001175
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001176 // hash map iterator
1177 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
1178 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
1179
1180 for( ; HMI != HMIEnd ; ++HMI ) {
Chris Lattner697954c2002-01-20 22:54:45 +00001181 if(HMI->first && HMI->second) {
1182 LiveRange *L = HMI->second; // get the LiveRange
1183 if( ! L->hasColor() )
1184 // NOTE: ** allocating the size of long Type **
1185 L->setSpillOffFromFP(mcInfo.allocateSpilledValue(TM, Type::LongTy));
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001186 }
1187 } // for all LR's in hash map
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001188}
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001189
1190
1191
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001192//----------------------------------------------------------------------------
Ruchira Sasankae727f852001-09-18 22:43:57 +00001193// The entry pont to Register Allocation
1194//----------------------------------------------------------------------------
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001195
1196void PhyRegAlloc::allocateRegisters()
1197{
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001198
1199 // make sure that we put all register classes into the RegClassList
1200 // before we call constructLiveRanges (now done in the constructor of
1201 // PhyRegAlloc class).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001202 //
1203 LRI.constructLiveRanges(); // create LR info
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001204
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001205 if( DEBUG_RA )
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001206 LRI.printLiveRanges();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001207
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001208 createIGNodeListsAndIGs(); // create IGNode list and IGs
1209
1210 buildInterferenceGraphs(); // build IGs in all reg classes
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001211
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001212
1213 if( DEBUG_RA ) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001214 // print all LRs in all reg classes
1215 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1216 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001217
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001218 // print IGs in all register classes
1219 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1220 RegClassList[ rc ]->printIG();
1221 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001222
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001223
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001224 LRI.coalesceLRs(); // coalesce all live ranges
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001225
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +00001226
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001227 if( DEBUG_RA) {
1228 // print all LRs in all reg classes
1229 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1230 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001231
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001232 // print IGs in all register classes
1233 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1234 RegClassList[ rc ]->printIG();
1235 }
1236
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001237
1238 // mark un-usable suggested color before graph coloring algorithm.
1239 // When this is done, the graph coloring algo will not reserve
1240 // suggested color unnecessarily - they can be used by another LR
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001241 //
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001242 markUnusableSugColors();
1243
1244 // color all register classes using the graph coloring algo
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001245 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1246 RegClassList[ rc ]->colorAllRegs();
1247
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001248 // Atter grpah coloring, if some LRs did not receive a color (i.e, spilled)
1249 // a poistion for such spilled LRs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001250 //
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001251 allocateStackSpace4SpilledLRs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001252
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001253 mcInfo.popAllTempValues(TM); // TODO **Check
1254
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001255 // color incoming args - if the correct color was not received
1256 // insert code to copy to the correct register
1257 //
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001258 colorIncomingArgs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001259
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001260
1261 // Now update the machine code with register names and add any
1262 // additional code inserted by the register allocator to the instruction
1263 // stream
1264 //
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001265 updateMachineCode();
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001266
1267
Chris Lattner045e7c82001-09-19 16:26:23 +00001268 if (DEBUG_RA) {
Vikram S. Adve12af1642001-11-08 04:48:50 +00001269 MachineCodeForMethod::get(Meth).dump();
Chris Lattner045e7c82001-09-19 16:26:23 +00001270 printMachineCode(); // only for DEBUGGING
1271 }
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +00001272
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001273}
1274
Ruchira Sasankae727f852001-09-18 22:43:57 +00001275
1276