blob: 61a20198118d658e69d4ea3a5a21d8742f789c78 [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"
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000015#include "llvm/CodeGen/MachineCodeForMethod.h"
Vikram S. Adve12af1642001-11-08 04:48:50 +000016#include "llvm/Target/TargetMachine.h"
17#include "llvm/Target/MachineFrameInfo.h"
Chris Lattner697954c2002-01-20 22:54:45 +000018#include <iostream>
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000019#include <math.h>
Chris Lattner697954c2002-01-20 22:54:45 +000020using std::cerr;
Vikram S. Adve12af1642001-11-08 04:48:50 +000021
22
23// ***TODO: There are several places we add instructions. Validate the order
24// of adding these instructions.
Ruchira Sasanka174bded2001-10-28 18:12:02 +000025
26
27
Chris Lattner045e7c82001-09-19 16:26:23 +000028cl::Enum<RegAllocDebugLevel_t> DEBUG_RA("dregalloc", cl::NoFlags,
29 "enable register allocation debugging information",
30 clEnumValN(RA_DEBUG_None , "n", "disable debug output"),
31 clEnumValN(RA_DEBUG_Normal , "y", "enable debug output"),
32 clEnumValN(RA_DEBUG_Verbose, "v", "enable extra debug output"), 0);
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000033
34
35//----------------------------------------------------------------------------
36// Constructor: Init local composite objects and create register classes.
37//----------------------------------------------------------------------------
Vikram S. Adve12af1642001-11-08 04:48:50 +000038PhyRegAlloc::PhyRegAlloc(Method *M,
Ruchira Sasanka8e604792001-09-14 21:18:34 +000039 const TargetMachine& tm,
40 MethodLiveVarInfo *const Lvi)
Chris Lattner697954c2002-01-20 22:54:45 +000041 : TM(tm), Meth(M),
Vikram S. Adve12af1642001-11-08 04:48:50 +000042 mcInfo(MachineCodeForMethod::get(M)),
43 LVI(Lvi), LRI(M, tm, RegClassList),
Ruchira Sasanka8e604792001-09-14 21:18:34 +000044 MRI( tm.getRegInfo() ),
45 NumOfRegClasses(MRI.getNumOfRegClasses()),
Chris Lattner697954c2002-01-20 22:54:45 +000046 LoopDepthCalc(M) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +000047
48 // create each RegisterClass and put in RegClassList
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000049 //
Chris Lattner697954c2002-01-20 22:54:45 +000050 for(unsigned int rc=0; rc < NumOfRegClasses; rc++)
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000051 RegClassList.push_back( new RegClass(M, MRI.getMachineRegClass(rc),
52 &ResColList) );
Ruchira Sasanka8e604792001-09-14 21:18:34 +000053}
54
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000055
56//----------------------------------------------------------------------------
57// Destructor: Deletes register classes
58//----------------------------------------------------------------------------
59PhyRegAlloc::~PhyRegAlloc() {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000060 for( unsigned int rc=0; rc < NumOfRegClasses; rc++)
61 delete RegClassList[rc];
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000062}
63
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000064//----------------------------------------------------------------------------
65// This method initally creates interference graphs (one in each reg class)
66// and IGNodeList (one in each IG). The actual nodes will be pushed later.
67//----------------------------------------------------------------------------
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000068void PhyRegAlloc::createIGNodeListsAndIGs() {
69 if (DEBUG_RA) cerr << "Creating LR lists ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +000070
71 // hash map iterator
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000072 LiveRangeMapType::const_iterator HMI = LRI.getLiveRangeMap()->begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +000073
74 // hash map end
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000075 LiveRangeMapType::const_iterator HMIEnd = LRI.getLiveRangeMap()->end();
Ruchira Sasanka8e604792001-09-14 21:18:34 +000076
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000077 for (; HMI != HMIEnd ; ++HMI ) {
78 if (HMI->first) {
79 LiveRange *L = HMI->second; // get the LiveRange
80 if (!L) {
81 if( DEBUG_RA) {
82 cerr << "\n*?!?Warning: Null liver range found for: ";
83 printValue(HMI->first); cerr << "\n";
84 }
85 continue;
86 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +000087 // if the Value * is not null, and LR
88 // is not yet written to the IGNodeList
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000089 if( !(L->getUserIGNode()) ) {
90 RegClass *const RC = // RegClass of first value in the LR
91 RegClassList[ L->getRegClass()->getID() ];
92
93 RC->addLRToIG(L); // add this LR to an IG
94 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +000095 }
96 }
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000097
98 // init RegClassList
Ruchira Sasanka8e604792001-09-14 21:18:34 +000099 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000100 RegClassList[rc]->createInterferenceGraph();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000101
102 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000103 cerr << "LRLists Created!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000104}
105
106
107
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000108
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000109//----------------------------------------------------------------------------
110// This method will add all interferences at for a given instruction.
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000111// Interence occurs only if the LR of Def (Inst or Arg) is of the same reg
112// class as that of live var. The live var passed to this function is the
113// LVset AFTER the instruction
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000114//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000115void PhyRegAlloc::addInterference(const Value *const Def,
116 const LiveVarSet *const LVSet,
117 const bool isCallInst) {
118
119 LiveVarSet::const_iterator LIt = LVSet->begin();
120
121 // get the live range of instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000122 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000123 const LiveRange *const LROfDef = LRI.getLiveRangeForValue( Def );
124
125 IGNode *const IGNodeOfDef = LROfDef->getUserIGNode();
126 assert( IGNodeOfDef );
127
128 RegClass *const RCOfDef = LROfDef->getRegClass();
129
130 // for each live var in live variable set
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000131 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000132 for( ; LIt != LVSet->end(); ++LIt) {
133
134 if( DEBUG_RA > 1) {
Chris Lattner697954c2002-01-20 22:54:45 +0000135 cerr << "< Def="; printValue(Def);
136 cerr << ", Lvar="; printValue( *LIt); cerr << "> ";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000137 }
138
139 // get the live range corresponding to live var
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000140 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000141 LiveRange *const LROfVar = LRI.getLiveRangeForValue(*LIt );
142
143 // LROfVar can be null if it is a const since a const
144 // doesn't have a dominating def - see Assumptions above
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000145 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000146 if (LROfVar) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000147 if(LROfDef == LROfVar) // do not set interf for same LR
148 continue;
149
150 // if 2 reg classes are the same set interference
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000151 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000152 if(RCOfDef == LROfVar->getRegClass()) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000153 RCOfDef->setInterference( LROfDef, LROfVar);
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000154 } else if(DEBUG_RA > 1) {
155 // we will not have LRs for values not explicitly allocated in the
156 // instruction stream (e.g., constants)
157 cerr << " warning: no live range for " ;
158 printValue(*LIt); cerr << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000159 }
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000160 }
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000161 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000162}
163
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000164
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000165
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000166//----------------------------------------------------------------------------
167// For a call instruction, this method sets the CallInterference flag in
168// the LR of each variable live int the Live Variable Set live after the
169// call instruction (except the return value of the call instruction - since
170// the return value does not interfere with that call itself).
171//----------------------------------------------------------------------------
172
173void PhyRegAlloc::setCallInterferences(const MachineInstr *MInst,
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000174 const LiveVarSet *const LVSetAft ) {
175
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000176 // Now find the LR of the return value of the call
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000177 // 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)
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000181 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000182 LiveRange *RetValLR = NULL;
Ruchira Sasankab3b6f532001-10-21 16:43:41 +0000183 const Value *RetVal = MRI.getCallInstRetVal( MInst );
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000184
Ruchira Sasankab3b6f532001-10-21 16:43:41 +0000185 if( RetVal ) {
186 RetValLR = LRI.getLiveRangeForValue( RetVal );
187 assert( RetValLR && "No LR for RetValue of call");
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000188 }
189
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000190 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000191 cerr << "\n For call inst: " << *MInst;
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000192
193 LiveVarSet::const_iterator LIt = LVSetAft->begin();
194
195 // for each live var in live variable set after machine inst
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000196 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000197 for( ; LIt != LVSetAft->end(); ++LIt) {
198
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000199 // get the live range corresponding to live var
200 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000201 LiveRange *const LR = LRI.getLiveRangeForValue(*LIt );
202
203 if( LR && DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000204 cerr << "\n\tLR Aft Call: ";
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000205 LR->printSet();
206 }
207
208
209 // LR can be null if it is a const since a const
210 // doesn't have a dominating def - see Assumptions above
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000211 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000212 if( LR && (LR != RetValLR) ) {
213 LR->setCallInterference();
214 if( DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000215 cerr << "\n ++Added call interf for LR: " ;
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000216 LR->printSet();
217 }
218 }
219
220 }
221
222}
223
224
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000225
226
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000227//----------------------------------------------------------------------------
228// This method will walk thru code and create interferences in the IG of
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000229// each RegClass. Also, this method calculates the spill cost of each
230// Live Range (it is done in this method to save another pass over the code).
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000231//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000232void PhyRegAlloc::buildInterferenceGraphs()
233{
234
Chris Lattner697954c2002-01-20 22:54:45 +0000235 if(DEBUG_RA) cerr << "Creating interference graphs ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000236
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000237 unsigned BBLoopDepthCost;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000238 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
239
240 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
241
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000242 // find the 10^(loop_depth) of this BB
243 //
244 BBLoopDepthCost = (unsigned) pow( 10.0, LoopDepthCalc.getLoopDepth(*BBI));
245
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000246 // get the iterator for machine instructions
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000247 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000248 const MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
249 MachineCodeForBasicBlock::const_iterator
250 MInstIterator = MIVec.begin();
251
252 // iterate over all the machine instructions in BB
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000253 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000254 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000255
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +0000256 const MachineInstr * MInst = *MInstIterator;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000257
258 // get the LV set after the instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000259 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000260 const LiveVarSet *const LVSetAI =
261 LVI->getLiveVarSetAfterMInst(MInst, *BBI);
262
263 const bool isCallInst = TM.getInstrInfo().isCall(MInst->getOpCode());
264
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000265 if( isCallInst ) {
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000266 // set the isCallInterference flag of each live range wich extends
267 // accross this call instruction. This information is used by graph
268 // coloring algo to avoid allocating volatile colors to live ranges
269 // that span across calls (since they have to be saved/restored)
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000270 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000271 setCallInterferences( MInst, LVSetAI);
272 }
273
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000274
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000275 // iterate over all MI operands to find defs
276 //
Chris Lattner7a176752001-12-04 00:03:30 +0000277 for( MachineInstr::val_const_op_iterator OpI(MInst);!OpI.done(); ++OpI) {
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000278
279 if( OpI.isDef() ) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000280 // create a new LR iff this operand is a def
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000281 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000282 addInterference(*OpI, LVSetAI, isCallInst );
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000283 }
284
285 // Calculate the spill cost of each live range
286 //
287 LiveRange *LR = LRI.getLiveRangeForValue( *OpI );
288 if( LR )
289 LR->addSpillCost(BBLoopDepthCost);
290 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000291
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000292
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000293 // if there are multiple defs in this instruction e.g. in SETX
294 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000295 if (TM.getInstrInfo().isPseudoInstr(MInst->getOpCode()))
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000296 addInterf4PseudoInstr(MInst);
297
298
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000299 // Also add interference for any implicit definitions in a machine
300 // instr (currently, only calls have this).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000301 //
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000302 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
303 if( NumOfImpRefs > 0 ) {
304 for(unsigned z=0; z < NumOfImpRefs; z++)
305 if( MInst->implicitRefIsDefined(z) )
306 addInterference( MInst->getImplicitRef(z), LVSetAI, isCallInst );
307 }
308
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +0000309
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000310 } // for all machine instructions in BB
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000311
312 } // for all BBs in method
313
314
315 // add interferences for method arguments. Since there are no explict
316 // defs in method for args, we have to add them manually
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000317 //
318 addInterferencesForArgs();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000319
320 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000321 cerr << "Interference graphs calculted!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000322
323}
324
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000325
326
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000327//--------------------------------------------------------------------------
328// Pseudo instructions will be exapnded to multiple instructions by the
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000329// assembler. Consequently, all the opernds must get distinct registers.
330// Therefore, we mark all operands of a pseudo instruction as they interfere
331// with one another.
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000332//--------------------------------------------------------------------------
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000333void PhyRegAlloc::addInterf4PseudoInstr(const MachineInstr *MInst) {
334
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000335 bool setInterf = false;
336
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000337 // iterate over MI operands to find defs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000338 //
Chris Lattner7a176752001-12-04 00:03:30 +0000339 for( MachineInstr::val_const_op_iterator It1(MInst);!It1.done(); ++It1) {
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000340
341 const LiveRange *const LROfOp1 = LRI.getLiveRangeForValue( *It1 );
342
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000343 if( !LROfOp1 && It1.isDef() )
344 assert( 0 && "No LR for Def in PSEUDO insruction");
345
Chris Lattner7a176752001-12-04 00:03:30 +0000346 MachineInstr::val_const_op_iterator It2 = It1;
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000347 ++It2;
348
349 for( ; !It2.done(); ++It2) {
350
351 const LiveRange *const LROfOp2 = LRI.getLiveRangeForValue( *It2 );
352
353 if( LROfOp2) {
354
355 RegClass *const RCOfOp1 = LROfOp1->getRegClass();
356 RegClass *const RCOfOp2 = LROfOp2->getRegClass();
357
358 if( RCOfOp1 == RCOfOp2 ){
359 RCOfOp1->setInterference( LROfOp1, LROfOp2 );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000360 setInterf = true;
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000361 }
362
363 } // if Op2 has a LR
364
365 } // for all other defs in machine instr
366
367 } // for all operands in an instruction
368
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000369 if( !setInterf && (MInst->getNumOperands() > 2) ) {
370 cerr << "\nInterf not set for any operand in pseudo instr:\n";
371 cerr << *MInst;
372 assert(0 && "Interf not set for pseudo instr with > 2 operands" );
373
374 }
375
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000376}
377
378
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000379
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000380//----------------------------------------------------------------------------
381// This method will add interferences for incoming arguments to a method.
382//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000383void PhyRegAlloc::addInterferencesForArgs()
384{
385 // get the InSet of root BB
386 const LiveVarSet *const InSet = LVI->getInSetOfBB( Meth->front() );
387
388 // get the argument list
389 const Method::ArgumentListType& ArgList = Meth->getArgumentList();
390
391 // get an iterator to arg list
392 Method::ArgumentListType::const_iterator ArgIt = ArgList.begin();
393
394
395 for( ; ArgIt != ArgList.end() ; ++ArgIt) { // for each argument
396 addInterference( *ArgIt, InSet, false ); // add interferences between
397 // args and LVars at start
398 if( DEBUG_RA > 1) {
Chris Lattner697954c2002-01-20 22:54:45 +0000399 cerr << " - %% adding interference for argument ";
400 printValue((const Value *)*ArgIt); cerr << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000401 }
402 }
403}
404
405
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000406
407
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000408//----------------------------------------------------------------------------
409// This method is called after register allocation is complete to set the
410// allocated reisters in the machine code. This code will add register numbers
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000411// to MachineOperands that contain a Value. Also it calls target specific
412// methods to produce caller saving instructions. At the end, it adds all
413// additional instructions produced by the register allocator to the
414// instruction stream.
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000415//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000416void PhyRegAlloc::updateMachineCode()
417{
418
419 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
420
421 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
422
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000423 // get the iterator for machine instructions
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000424 //
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000425 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
426 MachineCodeForBasicBlock::iterator MInstIterator = MIVec.begin();
427
428 // iterate over all the machine instructions in BB
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000429 //
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000430 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
431
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000432 MachineInstr *MInst = *MInstIterator;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000433
434 unsigned Opcode = MInst->getOpCode();
435
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000436 // do not process Phis
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000437 if (TM.getInstrInfo().isPhi(Opcode))
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000438 continue;
439
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000440 // Now insert speical instructions (if necessary) for call/return
441 // instructions.
442 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000443 if (TM.getInstrInfo().isCall(Opcode) ||
444 TM.getInstrInfo().isReturn(Opcode)) {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000445
446 AddedInstrns *AI = AddedInstrMap[ MInst];
447 if ( !AI ) {
448 AI = new AddedInstrns();
449 AddedInstrMap[ MInst ] = AI;
450 }
451
452 // Tmp stack poistions are needed by some calls that have spilled args
453 // So reset it before we call each such method
Ruchira Sasanka6a3db8c2002-01-07 21:09:06 +0000454 //
455 mcInfo.popAllTempValues(TM);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000456
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000457 if (TM.getInstrInfo().isCall(Opcode))
458 MRI.colorCallArgs(MInst, LRI, AI, *this, *BBI);
459 else if (TM.getInstrInfo().isReturn(Opcode))
460 MRI.colorRetValue(MInst, LRI, AI);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000461 }
462
463
464 /* -- Using above code instead of this
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000465
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000466 // if this machine instr is call, insert caller saving code
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000467
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000468 if( (TM.getInstrInfo()).isCall( MInst->getOpCode()) )
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000469 MRI.insertCallerSavingCode(MInst, *BBI, *this );
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000470
471 */
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000472
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000473
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000474 // reset the stack offset for temporary variables since we may
475 // need that to spill
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000476 // mcInfo.popAllTempValues(TM);
Ruchira Sasankaf90870f2001-11-15 22:02:06 +0000477 // TODO ** : do later
Vikram S. Adve12af1642001-11-08 04:48:50 +0000478
Chris Lattner7a176752001-12-04 00:03:30 +0000479 //for(MachineInstr::val_const_op_iterator OpI(MInst);!OpI.done();++OpI) {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000480
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000481
482 // Now replace set the registers for operands in the machine instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000483 //
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000484 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
485
486 MachineOperand& Op = MInst->getOperand(OpNum);
487
488 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
489 Op.getOperandType() == MachineOperand::MO_CCRegister) {
490
491 const Value *const Val = Op.getVRegValue();
492
493 // delete this condition checking later (must assert if Val is null)
Chris Lattner045e7c82001-09-19 16:26:23 +0000494 if( !Val) {
495 if (DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000496 cerr << "Warning: NULL Value found for operand\n";
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000497 continue;
498 }
499 assert( Val && "Value is NULL");
500
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000501 LiveRange *const LR = LRI.getLiveRangeForValue(Val);
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000502
503 if ( !LR ) {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000504
505 // nothing to worry if it's a const or a label
506
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000507 if (DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000508 cerr << "*NO LR for operand : " << Op ;
509 cerr << " [reg:" << Op.getAllocatedRegNum() << "]";
510 cerr << " in inst:\t" << *MInst << "\n";
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000511 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000512
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000513 // if register is not allocated, mark register as invalid
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000514 if( Op.getAllocatedRegNum() == -1)
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000515 Op.setRegForValue( MRI.getInvalidRegNum());
Ruchira Sasankae727f852001-09-18 22:43:57 +0000516
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000517
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000518 continue;
519 }
520
521 unsigned RCID = (LR->getRegClass())->getID();
522
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000523 if( LR->hasColor() ) {
524 Op.setRegForValue( MRI.getUnifiedRegNum(RCID, LR->getColor()) );
525 }
526 else {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000527
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000528 // LR did NOT receive a color (register). Now, insert spill code
529 // for spilled opeands in this machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000530
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000531 //assert(0 && "LR must be spilled");
532 insertCode4SpilledLR(LR, MInst, *BBI, OpNum );
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000533
534 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000535 }
536
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000537 } // for each operand
538
539
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000540 // Now add instructions that the register allocator inserts before/after
541 // this machine instructions (done only for calls/rets/incoming args)
542 // We do this here, to ensure that spill for an instruction is inserted
543 // closest as possible to an instruction (see above insertCode4Spill...)
544 //
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000545 // If there are instructions to be added, *before* this machine
546 // instruction, add them now.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000547 //
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000548 if( AddedInstrMap[ MInst ] ) {
Chris Lattner697954c2002-01-20 22:54:45 +0000549 std::deque<MachineInstr *> &IBef = AddedInstrMap[MInst]->InstrnsBefore;
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000550
551 if( ! IBef.empty() ) {
Chris Lattner697954c2002-01-20 22:54:45 +0000552 std::deque<MachineInstr *>::iterator AdIt;
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000553
554 for( AdIt = IBef.begin(); AdIt != IBef.end() ; ++AdIt ) {
555
556 if( DEBUG_RA) {
557 cerr << "For inst " << *MInst;
Chris Lattner697954c2002-01-20 22:54:45 +0000558 cerr << " PREPENDed instr: " << **AdIt << "\n";
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000559 }
560
561 MInstIterator = MIVec.insert( MInstIterator, *AdIt );
562 ++MInstIterator;
563 }
564
565 }
566
567 }
568
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000569 // If there are instructions to be added *after* this machine
570 // instruction, add them now
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000571 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000572 if(AddedInstrMap[MInst] &&
573 !AddedInstrMap[MInst]->InstrnsAfter.empty() ) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000574
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000575 // if there are delay slots for this instruction, the instructions
576 // added after it must really go after the delayed instruction(s)
577 // So, we move the InstrAfter of the current instruction to the
578 // corresponding delayed instruction
579
580 unsigned delay;
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000581 if ((delay=TM.getInstrInfo().getNumDelaySlots(MInst->getOpCode())) >0){
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000582 move2DelayedInstr(MInst, *(MInstIterator+delay) );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000583
Chris Lattner697954c2002-01-20 22:54:45 +0000584 if(DEBUG_RA) cerr<< "\nMoved an added instr after the delay slot";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000585 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000586
587 else {
588
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000589
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000590 // Here we can add the "instructions after" to the current
591 // instruction since there are no delay slots for this instruction
592
Chris Lattner697954c2002-01-20 22:54:45 +0000593 std::deque<MachineInstr *> &IAft = AddedInstrMap[MInst]->InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000594
595 if( ! IAft.empty() ) {
596
Chris Lattner697954c2002-01-20 22:54:45 +0000597 std::deque<MachineInstr *>::iterator AdIt;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000598
599 ++MInstIterator; // advance to the next instruction
600
601 for( AdIt = IAft.begin(); AdIt != IAft.end() ; ++AdIt ) {
602
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000603 if(DEBUG_RA) {
604 cerr << "For inst " << *MInst;
Chris Lattner697954c2002-01-20 22:54:45 +0000605 cerr << " APPENDed instr: " << **AdIt << "\n";
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000606 }
607
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000608 MInstIterator = MIVec.insert( MInstIterator, *AdIt );
609 ++MInstIterator;
610 }
611
612 // MInsterator already points to the next instr. Since the
613 // for loop also increments it, decrement it to point to the
614 // instruction added last
615 --MInstIterator;
616
617 }
618
619 } // if not delay
620
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000621 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000622
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000623 } // for each machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000624 }
625}
626
627
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000628
629//----------------------------------------------------------------------------
630// This method inserts spill code for AN operand whose LR was spilled.
631// This method may be called several times for a single machine instruction
632// if it contains many spilled operands. Each time it is called, it finds
633// a register which is not live at that instruction and also which is not
634// used by other spilled operands of the same instruction. Then it uses
635// this register temporarily to accomodate the spilled value.
636//----------------------------------------------------------------------------
637void PhyRegAlloc::insertCode4SpilledLR(const LiveRange *LR,
638 MachineInstr *MInst,
639 const BasicBlock *BB,
640 const unsigned OpNum) {
641
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000642 assert(! TM.getInstrInfo().isCall(MInst->getOpCode()) &&
643 (! TM.getInstrInfo().isReturn(MInst->getOpCode())) &&
644 "Arg of a call/ret must be handled elsewhere");
645
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000646 MachineOperand& Op = MInst->getOperand(OpNum);
647 bool isDef = MInst->operandIsDefined(OpNum);
648 unsigned RegType = MRI.getRegType( LR );
649 int SpillOff = LR->getSpillOffFromFP();
650 RegClass *RC = LR->getRegClass();
651 const LiveVarSet *LVSetBef = LVI->getLiveVarSetBeforeMInst(MInst, BB);
Vikram S. Adve00521d72001-11-12 23:26:35 +0000652
Chris Lattner697954c2002-01-20 22:54:45 +0000653 mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000654
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000655 MachineInstr *MIBef=NULL, *AdIMid=NULL, *MIAft=NULL;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000656
657 int TmpRegU = getUsableUniRegAtMI(RC, RegType, MInst,LVSetBef, MIBef, MIAft);
658
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000659 // get the added instructions for this instruciton
660 AddedInstrns *AI = AddedInstrMap[ MInst ];
661 if ( !AI ) {
662 AI = new AddedInstrns();
663 AddedInstrMap[ MInst ] = AI;
664 }
665
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000666
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000667 if( !isDef ) {
668
669 // for a USE, we have to load the value of LR from stack to a TmpReg
670 // and use the TmpReg as one operand of instruction
671
672 // actual loading instruction
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000673 AdIMid = MRI.cpMem2RegMI(MRI.getFramePointer(), SpillOff, TmpRegU,RegType);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000674
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000675 if(MIBef)
676 AI->InstrnsBefore.push_back(MIBef);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000677
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000678 AI->InstrnsBefore.push_back(AdIMid);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000679
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000680 if(MIAft)
681 AI->InstrnsAfter.push_front(MIAft);
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000682
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000683
684 }
685 else { // if this is a Def
686
687 // for a DEF, we have to store the value produced by this instruction
688 // on the stack position allocated for this LR
689
690 // actual storing instruction
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000691 AdIMid = MRI.cpReg2MemMI(TmpRegU, MRI.getFramePointer(), SpillOff,RegType);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000692
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000693 if (MIBef)
694 AI->InstrnsBefore.push_back(MIBef);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000695
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000696 AI->InstrnsAfter.push_front(AdIMid);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000697
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000698 if (MIAft)
699 AI->InstrnsAfter.push_front(MIAft);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000700
701 } // if !DEF
702
703 cerr << "\nFor Inst " << *MInst;
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000704 cerr << " - SPILLED LR: "; LR->printSet();
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000705 cerr << "\n - Added Instructions:";
706 if( MIBef ) cerr << *MIBef;
707 cerr << *AdIMid;
708 if( MIAft ) cerr << *MIAft;
709
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000710 Op.setRegForValue( TmpRegU ); // set the opearnd
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000711
712
713}
714
715
716
717
718
719
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000720//----------------------------------------------------------------------------
721// We can use the following method to get a temporary register to be used
722// BEFORE any given machine instruction. If there is a register available,
723// this method will simply return that register and set MIBef = MIAft = NULL.
724// Otherwise, it will return a register and MIAft and MIBef will contain
725// two instructions used to free up this returned register.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000726// Returned register number is the UNIFIED register number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000727//----------------------------------------------------------------------------
728
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000729int PhyRegAlloc::getUsableUniRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000730 const int RegType,
731 const MachineInstr *MInst,
732 const LiveVarSet *LVSetBef,
733 MachineInstr *MIBef,
734 MachineInstr *MIAft) {
735
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000736 int RegU = getUnusedUniRegAtMI(RC, MInst, LVSetBef);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000737
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000738
739 if( RegU != -1) {
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000740 // we found an unused register, so we can simply use it
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000741 MIBef = MIAft = NULL;
742 }
743 else {
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000744 // we couldn't find an unused register. Generate code to free up a reg by
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000745 // saving it on stack and restoring after the instruction
746
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000747 int TmpOff = mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
Vikram S. Adve12af1642001-11-08 04:48:50 +0000748
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000749 RegU = getUniRegNotUsedByThisInst(RC, MInst);
750 MIBef = MRI.cpReg2MemMI(RegU, MRI.getFramePointer(), TmpOff, RegType );
751 MIAft = MRI.cpMem2RegMI(MRI.getFramePointer(), TmpOff, RegU, RegType );
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000752 }
753
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000754 return RegU;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000755}
756
757//----------------------------------------------------------------------------
758// This method is called to get a new unused register that can be used to
759// accomodate a spilled value.
760// This method may be called several times for a single machine instruction
761// if it contains many spilled operands. Each time it is called, it finds
762// a register which is not live at that instruction and also which is not
763// used by other spilled operands of the same instruction.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000764// Return register number is relative to the register class. NOT
765// unified number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000766//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000767int PhyRegAlloc::getUnusedUniRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000768 const MachineInstr *MInst,
769 const LiveVarSet *LVSetBef) {
770
771 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
772
773 bool *IsColorUsedArr = RC->getIsColorUsedArr();
774
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000775 for(unsigned i=0; i < NumAvailRegs; i++) // Reset array
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000776 IsColorUsedArr[i] = false;
777
778 LiveVarSet::const_iterator LIt = LVSetBef->begin();
779
780 // for each live var in live variable set after machine inst
781 for( ; LIt != LVSetBef->end(); ++LIt) {
782
783 // get the live range corresponding to live var
784 LiveRange *const LRofLV = LRI.getLiveRangeForValue(*LIt );
785
786 // LR can be null if it is a const since a const
787 // doesn't have a dominating def - see Assumptions above
788 if( LRofLV )
789 if( LRofLV->hasColor() )
790 IsColorUsedArr[ LRofLV->getColor() ] = true;
791 }
792
793 // It is possible that one operand of this MInst was already spilled
794 // and it received some register temporarily. If that's the case,
795 // it is recorded in machine operand. We must skip such registers.
796
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000797 setRelRegsUsedByThisInst(RC, MInst);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000798
799 unsigned c; // find first unused color
800 for( c=0; c < NumAvailRegs; c++)
801 if( ! IsColorUsedArr[ c ] ) break;
802
803 if(c < NumAvailRegs)
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000804 return MRI.getUnifiedRegNum(RC->getID(), c);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000805 else
806 return -1;
807
808
809}
810
811
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000812//----------------------------------------------------------------------------
813// Get any other register in a register class, other than what is used
814// by operands of a machine instruction. Returns the unified reg number.
815//----------------------------------------------------------------------------
816int PhyRegAlloc::getUniRegNotUsedByThisInst(RegClass *RC,
817 const MachineInstr *MInst) {
818
819 bool *IsColorUsedArr = RC->getIsColorUsedArr();
820 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
821
822
823 for(unsigned i=0; i < NumAvailRegs ; i++) // Reset array
824 IsColorUsedArr[i] = false;
825
826 setRelRegsUsedByThisInst(RC, MInst);
827
828 unsigned c; // find first unused color
829 for( c=0; c < RC->getNumOfAvailRegs(); c++)
830 if( ! IsColorUsedArr[ c ] ) break;
831
832 if(c < NumAvailRegs)
833 return MRI.getUnifiedRegNum(RC->getID(), c);
834 else
835 assert( 0 && "FATAL: No free register could be found in reg class!!");
Chris Lattner697954c2002-01-20 22:54:45 +0000836 return 0;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000837}
838
839
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000840//----------------------------------------------------------------------------
841// This method modifies the IsColorUsedArr of the register class passed to it.
842// It sets the bits corresponding to the registers used by this machine
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000843// instructions. Both explicit and implicit operands are set.
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000844//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000845void PhyRegAlloc::setRelRegsUsedByThisInst(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000846 const MachineInstr *MInst ) {
847
848 bool *IsColorUsedArr = RC->getIsColorUsedArr();
849
850 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
851
852 const MachineOperand& Op = MInst->getOperand(OpNum);
853
854 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000855 Op.getOperandType() == MachineOperand::MO_CCRegister ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000856
857 const Value *const Val = Op.getVRegValue();
858
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000859 if( Val )
860 if( MRI.getRegClassIDOfValue(Val) == RC->getID() ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000861 int Reg;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000862 if( (Reg=Op.getAllocatedRegNum()) != -1) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000863 IsColorUsedArr[ Reg ] = true;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000864 }
865 else {
866 // it is possilbe that this operand still is not marked with
867 // a register but it has a LR and that received a color
868
869 LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
870 if( LROfVal)
871 if( LROfVal->hasColor() )
872 IsColorUsedArr[ LROfVal->getColor() ] = true;
873 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000874
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000875 } // if reg classes are the same
876 }
877 else if (Op.getOperandType() == MachineOperand::MO_MachineRegister) {
878 IsColorUsedArr[ Op.getMachineRegNum() ] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000879 }
880 }
881
882 // If there are implicit references, mark them as well
883
884 for(unsigned z=0; z < MInst->getNumImplicitRefs(); z++) {
885
886 LiveRange *const LRofImpRef =
887 LRI.getLiveRangeForValue( MInst->getImplicitRef(z) );
Chris Lattner697954c2002-01-20 22:54:45 +0000888
889 if(LRofImpRef && LRofImpRef->hasColor())
890 IsColorUsedArr[LRofImpRef->getColor()] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000891 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000892}
893
894
895
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000896
897
898
899
900
901//----------------------------------------------------------------------------
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000902// If there are delay slots for an instruction, the instructions
903// added after it must really go after the delayed instruction(s).
904// So, we move the InstrAfter of that instruction to the
905// corresponding delayed instruction using the following method.
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000906
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000907//----------------------------------------------------------------------------
908void PhyRegAlloc:: move2DelayedInstr(const MachineInstr *OrigMI,
909 const MachineInstr *DelayedMI) {
910
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000911 // "added after" instructions of the original instr
Chris Lattner697954c2002-01-20 22:54:45 +0000912 std::deque<MachineInstr *> &OrigAft = AddedInstrMap[OrigMI]->InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000913
914 // "added instructions" of the delayed instr
915 AddedInstrns *DelayAdI = AddedInstrMap[DelayedMI];
916
917 if(! DelayAdI ) { // create a new "added after" if necessary
918 DelayAdI = new AddedInstrns();
919 AddedInstrMap[DelayedMI] = DelayAdI;
920 }
921
922 // "added after" instructions of the delayed instr
Chris Lattner697954c2002-01-20 22:54:45 +0000923 std::deque<MachineInstr *> &DelayedAft = DelayAdI->InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000924
925 // go thru all the "added after instructions" of the original instruction
926 // and append them to the "addded after instructions" of the delayed
927 // instructions
Chris Lattner697954c2002-01-20 22:54:45 +0000928 DelayedAft.insert(DelayedAft.end(), OrigAft.begin(), OrigAft.end());
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000929
930 // empty the "added after instructions" of the original instruction
931 OrigAft.clear();
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000932}
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000933
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000934//----------------------------------------------------------------------------
935// This method prints the code with registers after register allocation is
936// complete.
937//----------------------------------------------------------------------------
938void PhyRegAlloc::printMachineCode()
939{
940
Chris Lattner697954c2002-01-20 22:54:45 +0000941 cerr << "\n;************** Method " << Meth->getName()
942 << " *****************\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000943
944 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
945
946 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
947
Chris Lattner697954c2002-01-20 22:54:45 +0000948 cerr << "\n"; printLabel( *BBI); cerr << ": ";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000949
950 // get the iterator for machine instructions
951 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
952 MachineCodeForBasicBlock::iterator MInstIterator = MIVec.begin();
953
954 // iterate over all the machine instructions in BB
955 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
956
957 MachineInstr *const MInst = *MInstIterator;
958
959
Chris Lattner697954c2002-01-20 22:54:45 +0000960 cerr << "\n\t";
961 cerr << TargetInstrDescriptors[MInst->getOpCode()].opCodeString;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000962
963
Chris Lattner7a176752001-12-04 00:03:30 +0000964 //for(MachineInstr::val_const_op_iterator OpI(MInst);!OpI.done();++OpI) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000965
966 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
967
968 MachineOperand& Op = MInst->getOperand(OpNum);
969
970 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasanka97b8b442001-10-18 22:36:26 +0000971 Op.getOperandType() == MachineOperand::MO_CCRegister /*||
972 Op.getOperandType() == MachineOperand::MO_PCRelativeDisp*/ ) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000973
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000974 const Value *const Val = Op.getVRegValue () ;
Ruchira Sasankae727f852001-09-18 22:43:57 +0000975 // ****this code is temporary till NULL Values are fixed
976 if( ! Val ) {
Chris Lattner697954c2002-01-20 22:54:45 +0000977 cerr << "\t<*NULL*>";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000978 continue;
979 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000980
981 // if a label or a constant
Chris Lattnerdbe53042002-01-21 01:33:12 +0000982 if(isa<BasicBlock>(Val)) {
Chris Lattner697954c2002-01-20 22:54:45 +0000983 cerr << "\t"; printLabel( Op.getVRegValue () );
984 } else {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000985 // else it must be a register value
986 const int RegNum = Op.getAllocatedRegNum();
987
Chris Lattner697954c2002-01-20 22:54:45 +0000988 cerr << "\t" << "%" << MRI.getUnifiedRegName( RegNum );
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000989 if (Val->hasName() )
Chris Lattner697954c2002-01-20 22:54:45 +0000990 cerr << "(" << Val->getName() << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000991 else
Chris Lattner697954c2002-01-20 22:54:45 +0000992 cerr << "(" << Val << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000993
994 if( Op.opIsDef() )
Chris Lattner697954c2002-01-20 22:54:45 +0000995 cerr << "*";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000996
997 const LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
998 if( LROfVal )
999 if( LROfVal->hasSpillOffset() )
Chris Lattner697954c2002-01-20 22:54:45 +00001000 cerr << "$";
Ruchira Sasankae727f852001-09-18 22:43:57 +00001001 }
1002
1003 }
1004 else if(Op.getOperandType() == MachineOperand::MO_MachineRegister) {
Chris Lattner697954c2002-01-20 22:54:45 +00001005 cerr << "\t" << "%" << MRI.getUnifiedRegName(Op.getMachineRegNum());
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001006 }
1007
1008 else
Chris Lattner697954c2002-01-20 22:54:45 +00001009 cerr << "\t" << Op; // use dump field
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001010 }
1011
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001012
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001013
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001014 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
1015 if( NumOfImpRefs > 0 ) {
1016
Chris Lattner697954c2002-01-20 22:54:45 +00001017 cerr << "\tImplicit:";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001018
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001019 for(unsigned z=0; z < NumOfImpRefs; z++) {
1020 printValue( MInst->getImplicitRef(z) );
Chris Lattner697954c2002-01-20 22:54:45 +00001021 cerr << "\t";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001022 }
1023
1024 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001025
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001026 } // for all machine instructions
1027
Chris Lattner697954c2002-01-20 22:54:45 +00001028 cerr << "\n";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001029
1030 } // for all BBs
1031
Chris Lattner697954c2002-01-20 22:54:45 +00001032 cerr << "\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001033}
1034
Ruchira Sasankae727f852001-09-18 22:43:57 +00001035
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001036#if 0
1037
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001038//----------------------------------------------------------------------------
1039//
1040//----------------------------------------------------------------------------
1041
1042void PhyRegAlloc::colorCallRetArgs()
1043{
1044
1045 CallRetInstrListType &CallRetInstList = LRI.getCallRetInstrList();
1046 CallRetInstrListType::const_iterator It = CallRetInstList.begin();
1047
1048 for( ; It != CallRetInstList.end(); ++It ) {
1049
Ruchira Sasankaa90e7702001-10-15 16:26:38 +00001050 const MachineInstr *const CRMI = *It;
1051 unsigned OpCode = CRMI->getOpCode();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001052
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001053 // get the added instructions for this Call/Ret instruciton
1054 AddedInstrns *AI = AddedInstrMap[ CRMI ];
1055 if ( !AI ) {
1056 AI = new AddedInstrns();
1057 AddedInstrMap[ CRMI ] = AI;
1058 }
1059
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001060 // Tmp stack poistions are needed by some calls that have spilled args
1061 // So reset it before we call each such method
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001062 //mcInfo.popAllTempValues(TM);
1063
1064
Vikram S. Adve12af1642001-11-08 04:48:50 +00001065
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001066 if (TM.getInstrInfo().isCall(OpCode))
1067 MRI.colorCallArgs(CRMI, LRI, AI, *this);
1068 else if (TM.getInstrInfo().isReturn(OpCode))
Ruchira Sasankaa90e7702001-10-15 16:26:38 +00001069 MRI.colorRetValue( CRMI, LRI, AI );
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001070 else
1071 assert(0 && "Non Call/Ret instrn in CallRetInstrList\n");
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001072 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001073}
1074
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001075#endif
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001076
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001077//----------------------------------------------------------------------------
1078
1079//----------------------------------------------------------------------------
1080void PhyRegAlloc::colorIncomingArgs()
1081{
1082 const BasicBlock *const FirstBB = Meth->front();
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001083 const MachineInstr *FirstMI = FirstBB->getMachineInstrVec().front();
1084 assert(FirstMI && "No machine instruction in entry BB");
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001085
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001086 AddedInstrns *AI = AddedInstrMap[FirstMI];
1087 if (!AI)
1088 AddedInstrMap[FirstMI] = AI = new AddedInstrns();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001089
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001090 MRI.colorMethodArgs(Meth, LRI, AI);
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001091}
1092
Ruchira Sasankae727f852001-09-18 22:43:57 +00001093
1094//----------------------------------------------------------------------------
1095// Used to generate a label for a basic block
1096//----------------------------------------------------------------------------
Chris Lattner697954c2002-01-20 22:54:45 +00001097void PhyRegAlloc::printLabel(const Value *const Val) {
1098 if (Val->hasName())
1099 cerr << Val->getName();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001100 else
Chris Lattner697954c2002-01-20 22:54:45 +00001101 cerr << "Label" << Val;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001102}
1103
1104
Ruchira Sasankae727f852001-09-18 22:43:57 +00001105//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001106// This method calls setSugColorUsable method of each live range. This
1107// will determine whether the suggested color of LR is really usable.
1108// A suggested color is not usable when the suggested color is volatile
1109// AND when there are call interferences
1110//----------------------------------------------------------------------------
1111
1112void PhyRegAlloc::markUnusableSugColors()
1113{
Chris Lattner697954c2002-01-20 22:54:45 +00001114 if(DEBUG_RA ) cerr << "\nmarking unusable suggested colors ...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001115
1116 // hash map iterator
1117 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
1118 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
1119
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001120 for(; HMI != HMIEnd ; ++HMI ) {
1121 if (HMI->first) {
1122 LiveRange *L = HMI->second; // get the LiveRange
1123 if (L) {
1124 if(L->hasSuggestedColor()) {
1125 int RCID = L->getRegClass()->getID();
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001126 if( MRI.isRegVolatile( RCID, L->getSuggestedColor()) &&
1127 L->isCallInterference() )
1128 L->setSuggestedColorUsable( false );
1129 else
1130 L->setSuggestedColorUsable( true );
1131 }
1132 } // if L->hasSuggestedColor()
1133 }
1134 } // for all LR's in hash map
1135}
1136
1137
1138
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001139//----------------------------------------------------------------------------
1140// The following method will set the stack offsets of the live ranges that
1141// are decided to be spillled. This must be called just after coloring the
1142// LRs using the graph coloring algo. For each live range that is spilled,
1143// this method allocate a new spill position on the stack.
1144//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001145
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001146void PhyRegAlloc::allocateStackSpace4SpilledLRs()
1147{
Chris Lattner697954c2002-01-20 22:54:45 +00001148 if(DEBUG_RA ) cerr << "\nsetting LR stack offsets ...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001149
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001150 // hash map iterator
1151 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
1152 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
1153
1154 for( ; HMI != HMIEnd ; ++HMI ) {
Chris Lattner697954c2002-01-20 22:54:45 +00001155 if(HMI->first && HMI->second) {
1156 LiveRange *L = HMI->second; // get the LiveRange
1157 if( ! L->hasColor() )
1158 // NOTE: ** allocating the size of long Type **
1159 L->setSpillOffFromFP(mcInfo.allocateSpilledValue(TM, Type::LongTy));
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001160 }
1161 } // for all LR's in hash map
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001162}
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001163
1164
1165
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001166//----------------------------------------------------------------------------
Ruchira Sasankae727f852001-09-18 22:43:57 +00001167// The entry pont to Register Allocation
1168//----------------------------------------------------------------------------
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001169
1170void PhyRegAlloc::allocateRegisters()
1171{
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001172
1173 // make sure that we put all register classes into the RegClassList
1174 // before we call constructLiveRanges (now done in the constructor of
1175 // PhyRegAlloc class).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001176 //
1177 LRI.constructLiveRanges(); // create LR info
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001178
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001179 if (DEBUG_RA)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001180 LRI.printLiveRanges();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001181
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001182 createIGNodeListsAndIGs(); // create IGNode list and IGs
1183
1184 buildInterferenceGraphs(); // build IGs in all reg classes
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001185
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001186
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001187 if (DEBUG_RA) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001188 // print all LRs in all reg classes
1189 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1190 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001191
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001192 // print IGs in all register classes
1193 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1194 RegClassList[ rc ]->printIG();
1195 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001196
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001197
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001198 LRI.coalesceLRs(); // coalesce all live ranges
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001199
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +00001200
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001201 if( DEBUG_RA) {
1202 // print all LRs in all reg classes
1203 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1204 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001205
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001206 // print IGs in all register classes
1207 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1208 RegClassList[ rc ]->printIG();
1209 }
1210
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001211
1212 // mark un-usable suggested color before graph coloring algorithm.
1213 // When this is done, the graph coloring algo will not reserve
1214 // suggested color unnecessarily - they can be used by another LR
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001215 //
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001216 markUnusableSugColors();
1217
1218 // color all register classes using the graph coloring algo
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001219 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1220 RegClassList[ rc ]->colorAllRegs();
1221
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001222 // Atter grpah coloring, if some LRs did not receive a color (i.e, spilled)
1223 // a poistion for such spilled LRs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001224 //
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001225 allocateStackSpace4SpilledLRs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001226
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001227 mcInfo.popAllTempValues(TM); // TODO **Check
1228
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001229 // color incoming args - if the correct color was not received
1230 // insert code to copy to the correct register
1231 //
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001232 colorIncomingArgs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001233
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001234 // Now update the machine code with register names and add any
1235 // additional code inserted by the register allocator to the instruction
1236 // stream
1237 //
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001238 updateMachineCode();
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001239
Chris Lattner045e7c82001-09-19 16:26:23 +00001240 if (DEBUG_RA) {
Vikram S. Adve12af1642001-11-08 04:48:50 +00001241 MachineCodeForMethod::get(Meth).dump();
Chris Lattner045e7c82001-09-19 16:26:23 +00001242 printMachineCode(); // only for DEBUGGING
1243 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001244}
1245
Ruchira Sasankae727f852001-09-18 22:43:57 +00001246
1247