blob: 46f045c5e276222a45c2d81c9d1e1c4e70dbd056 [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
Chris Lattner6dd98a62002-02-04 00:33:08 +000013#include "llvm/CodeGen/RegisterAllocation.h"
Vikram S. Adve12af1642001-11-08 04:48:50 +000014#include "llvm/CodeGen/PhyRegAlloc.h"
15#include "llvm/CodeGen/MachineInstr.h"
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000016#include "llvm/CodeGen/MachineCodeForMethod.h"
Chris Lattner0a8ed942002-02-04 05:56:09 +000017#include "llvm/Analysis/LiveVar/MethodLiveVarInfo.h"
Vikram S. Adve12af1642001-11-08 04:48:50 +000018#include "llvm/Target/TargetMachine.h"
19#include "llvm/Target/MachineFrameInfo.h"
Chris Lattner697954c2002-01-20 22:54:45 +000020#include <iostream>
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000021#include <math.h>
Chris Lattner697954c2002-01-20 22:54:45 +000022using std::cerr;
Vikram S. Adve12af1642001-11-08 04:48:50 +000023
24
25// ***TODO: There are several places we add instructions. Validate the order
26// of adding these instructions.
Ruchira Sasanka174bded2001-10-28 18:12:02 +000027
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
Chris Lattner2f9b28e2002-02-04 15:54:09 +000035//----------------------------------------------------------------------------
36// RegisterAllocation pass front end...
37//----------------------------------------------------------------------------
38namespace {
39 class RegisterAllocator : public MethodPass {
40 TargetMachine &Target;
41 public:
42 inline RegisterAllocator(TargetMachine &T) : Target(T) {}
Chris Lattner6dd98a62002-02-04 00:33:08 +000043
Chris Lattner2f9b28e2002-02-04 15:54:09 +000044 bool runOnMethod(Method *M) {
45 if (DEBUG_RA)
46 cerr << "\n******************** Method "<< M->getName()
47 << " ********************\n";
48
49 MethodLiveVarInfo LVI(M); // Analyze live varaibles
50 LVI.analyze();
51
52 PhyRegAlloc PRA(M, Target, &LVI); // allocate registers
53 PRA.allocateRegisters();
54
55 if (DEBUG_RA) cerr << "\nRegister allocation complete!\n";
56 return false;
57 }
58 };
Chris Lattner6dd98a62002-02-04 00:33:08 +000059}
60
Chris Lattner2f9b28e2002-02-04 15:54:09 +000061MethodPass *getRegisterAllocator(TargetMachine &T) {
62 return new RegisterAllocator(T);
63}
Chris Lattner6dd98a62002-02-04 00:33:08 +000064
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000065//----------------------------------------------------------------------------
66// Constructor: Init local composite objects and create register classes.
67//----------------------------------------------------------------------------
Vikram S. Adve12af1642001-11-08 04:48:50 +000068PhyRegAlloc::PhyRegAlloc(Method *M,
Ruchira Sasanka8e604792001-09-14 21:18:34 +000069 const TargetMachine& tm,
70 MethodLiveVarInfo *const Lvi)
Chris Lattner697954c2002-01-20 22:54:45 +000071 : TM(tm), Meth(M),
Vikram S. Adve12af1642001-11-08 04:48:50 +000072 mcInfo(MachineCodeForMethod::get(M)),
73 LVI(Lvi), LRI(M, tm, RegClassList),
Ruchira Sasanka8e604792001-09-14 21:18:34 +000074 MRI( tm.getRegInfo() ),
75 NumOfRegClasses(MRI.getNumOfRegClasses()),
Chris Lattner697954c2002-01-20 22:54:45 +000076 LoopDepthCalc(M) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +000077
78 // create each RegisterClass and put in RegClassList
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000079 //
Chris Lattner697954c2002-01-20 22:54:45 +000080 for(unsigned int rc=0; rc < NumOfRegClasses; rc++)
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000081 RegClassList.push_back( new RegClass(M, MRI.getMachineRegClass(rc),
82 &ResColList) );
Ruchira Sasanka8e604792001-09-14 21:18:34 +000083}
84
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000085
86//----------------------------------------------------------------------------
87// Destructor: Deletes register classes
88//----------------------------------------------------------------------------
89PhyRegAlloc::~PhyRegAlloc() {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000090 for( unsigned int rc=0; rc < NumOfRegClasses; rc++)
91 delete RegClassList[rc];
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000092}
93
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000094//----------------------------------------------------------------------------
95// This method initally creates interference graphs (one in each reg class)
96// and IGNodeList (one in each IG). The actual nodes will be pushed later.
97//----------------------------------------------------------------------------
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000098void PhyRegAlloc::createIGNodeListsAndIGs() {
99 if (DEBUG_RA) cerr << "Creating LR lists ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000100
101 // hash map iterator
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000102 LiveRangeMapType::const_iterator HMI = LRI.getLiveRangeMap()->begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000103
104 // hash map end
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000105 LiveRangeMapType::const_iterator HMIEnd = LRI.getLiveRangeMap()->end();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000106
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000107 for (; HMI != HMIEnd ; ++HMI ) {
108 if (HMI->first) {
109 LiveRange *L = HMI->second; // get the LiveRange
110 if (!L) {
111 if( DEBUG_RA) {
112 cerr << "\n*?!?Warning: Null liver range found for: ";
113 printValue(HMI->first); cerr << "\n";
114 }
115 continue;
116 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000117 // if the Value * is not null, and LR
118 // is not yet written to the IGNodeList
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000119 if( !(L->getUserIGNode()) ) {
120 RegClass *const RC = // RegClass of first value in the LR
121 RegClassList[ L->getRegClass()->getID() ];
122
123 RC->addLRToIG(L); // add this LR to an IG
124 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000125 }
126 }
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000127
128 // init RegClassList
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000129 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000130 RegClassList[rc]->createInterferenceGraph();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000131
132 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000133 cerr << "LRLists Created!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000134}
135
136
137
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000138
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000139//----------------------------------------------------------------------------
140// This method will add all interferences at for a given instruction.
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000141// Interence occurs only if the LR of Def (Inst or Arg) is of the same reg
142// class as that of live var. The live var passed to this function is the
143// LVset AFTER the instruction
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000144//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000145void PhyRegAlloc::addInterference(const Value *const Def,
146 const LiveVarSet *const LVSet,
147 const bool isCallInst) {
148
149 LiveVarSet::const_iterator LIt = LVSet->begin();
150
151 // get the live range of instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000152 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000153 const LiveRange *const LROfDef = LRI.getLiveRangeForValue( Def );
154
155 IGNode *const IGNodeOfDef = LROfDef->getUserIGNode();
156 assert( IGNodeOfDef );
157
158 RegClass *const RCOfDef = LROfDef->getRegClass();
159
160 // for each live var in live variable set
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000161 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000162 for( ; LIt != LVSet->end(); ++LIt) {
163
164 if( DEBUG_RA > 1) {
Chris Lattner697954c2002-01-20 22:54:45 +0000165 cerr << "< Def="; printValue(Def);
166 cerr << ", Lvar="; printValue( *LIt); cerr << "> ";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000167 }
168
169 // get the live range corresponding to live var
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000170 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000171 LiveRange *const LROfVar = LRI.getLiveRangeForValue(*LIt );
172
173 // LROfVar can be null if it is a const since a const
174 // doesn't have a dominating def - see Assumptions above
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000175 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000176 if (LROfVar) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000177 if(LROfDef == LROfVar) // do not set interf for same LR
178 continue;
179
180 // if 2 reg classes are the same set interference
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000181 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000182 if(RCOfDef == LROfVar->getRegClass()) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000183 RCOfDef->setInterference( LROfDef, LROfVar);
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000184 } else if(DEBUG_RA > 1) {
185 // we will not have LRs for values not explicitly allocated in the
186 // instruction stream (e.g., constants)
187 cerr << " warning: no live range for " ;
188 printValue(*LIt); cerr << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000189 }
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000190 }
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000191 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000192}
193
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000194
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000195
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000196//----------------------------------------------------------------------------
197// For a call instruction, this method sets the CallInterference flag in
198// the LR of each variable live int the Live Variable Set live after the
199// call instruction (except the return value of the call instruction - since
200// the return value does not interfere with that call itself).
201//----------------------------------------------------------------------------
202
203void PhyRegAlloc::setCallInterferences(const MachineInstr *MInst,
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000204 const LiveVarSet *const LVSetAft ) {
205
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000206 // Now find the LR of the return value of the call
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000207 // We do this because, we look at the LV set *after* the instruction
208 // to determine, which LRs must be saved across calls. The return value
209 // of the call is live in this set - but it does not interfere with call
210 // (i.e., we can allocate a volatile register to the return value)
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000211 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000212 LiveRange *RetValLR = NULL;
Ruchira Sasankab3b6f532001-10-21 16:43:41 +0000213 const Value *RetVal = MRI.getCallInstRetVal( MInst );
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000214
Ruchira Sasankab3b6f532001-10-21 16:43:41 +0000215 if( RetVal ) {
216 RetValLR = LRI.getLiveRangeForValue( RetVal );
217 assert( RetValLR && "No LR for RetValue of call");
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000218 }
219
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000220 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000221 cerr << "\n For call inst: " << *MInst;
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000222
223 LiveVarSet::const_iterator LIt = LVSetAft->begin();
224
225 // for each live var in live variable set after machine inst
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000226 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000227 for( ; LIt != LVSetAft->end(); ++LIt) {
228
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000229 // get the live range corresponding to live var
230 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000231 LiveRange *const LR = LRI.getLiveRangeForValue(*LIt );
232
233 if( LR && DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000234 cerr << "\n\tLR Aft Call: ";
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000235 LR->printSet();
236 }
237
238
239 // LR can be null if it is a const since a const
240 // doesn't have a dominating def - see Assumptions above
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000241 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000242 if( LR && (LR != RetValLR) ) {
243 LR->setCallInterference();
244 if( DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000245 cerr << "\n ++Added call interf for LR: " ;
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000246 LR->printSet();
247 }
248 }
249
250 }
251
252}
253
254
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000255
256
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000257//----------------------------------------------------------------------------
258// This method will walk thru code and create interferences in the IG of
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000259// each RegClass. Also, this method calculates the spill cost of each
260// Live Range (it is done in this method to save another pass over the code).
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000261//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000262void PhyRegAlloc::buildInterferenceGraphs()
263{
264
Chris Lattner697954c2002-01-20 22:54:45 +0000265 if(DEBUG_RA) cerr << "Creating interference graphs ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000266
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000267 unsigned BBLoopDepthCost;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000268 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
269
270 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
271
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000272 // find the 10^(loop_depth) of this BB
273 //
274 BBLoopDepthCost = (unsigned) pow( 10.0, LoopDepthCalc.getLoopDepth(*BBI));
275
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000276 // get the iterator for machine instructions
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000277 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000278 const MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
279 MachineCodeForBasicBlock::const_iterator
280 MInstIterator = MIVec.begin();
281
282 // iterate over all the machine instructions in BB
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000283 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000284 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000285
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +0000286 const MachineInstr * MInst = *MInstIterator;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000287
288 // get the LV set after the instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000289 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000290 const LiveVarSet *const LVSetAI =
291 LVI->getLiveVarSetAfterMInst(MInst, *BBI);
292
293 const bool isCallInst = TM.getInstrInfo().isCall(MInst->getOpCode());
294
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000295 if( isCallInst ) {
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000296 // set the isCallInterference flag of each live range wich extends
297 // accross this call instruction. This information is used by graph
298 // coloring algo to avoid allocating volatile colors to live ranges
299 // that span across calls (since they have to be saved/restored)
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000300 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000301 setCallInterferences( MInst, LVSetAI);
302 }
303
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000304
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000305 // iterate over all MI operands to find defs
306 //
Chris Lattner7a176752001-12-04 00:03:30 +0000307 for( MachineInstr::val_const_op_iterator OpI(MInst);!OpI.done(); ++OpI) {
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000308
309 if( OpI.isDef() ) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000310 // create a new LR iff this operand is a def
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000311 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000312 addInterference(*OpI, LVSetAI, isCallInst );
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000313 }
314
315 // Calculate the spill cost of each live range
316 //
317 LiveRange *LR = LRI.getLiveRangeForValue( *OpI );
318 if( LR )
319 LR->addSpillCost(BBLoopDepthCost);
320 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000321
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000322
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000323 // if there are multiple defs in this instruction e.g. in SETX
324 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000325 if (TM.getInstrInfo().isPseudoInstr(MInst->getOpCode()))
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000326 addInterf4PseudoInstr(MInst);
327
328
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000329 // Also add interference for any implicit definitions in a machine
330 // instr (currently, only calls have this).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000331 //
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000332 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
333 if( NumOfImpRefs > 0 ) {
334 for(unsigned z=0; z < NumOfImpRefs; z++)
335 if( MInst->implicitRefIsDefined(z) )
336 addInterference( MInst->getImplicitRef(z), LVSetAI, isCallInst );
337 }
338
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +0000339
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000340 } // for all machine instructions in BB
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000341
342 } // for all BBs in method
343
344
345 // add interferences for method arguments. Since there are no explict
346 // defs in method for args, we have to add them manually
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000347 //
348 addInterferencesForArgs();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000349
350 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000351 cerr << "Interference graphs calculted!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000352
353}
354
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000355
356
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000357//--------------------------------------------------------------------------
358// Pseudo instructions will be exapnded to multiple instructions by the
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000359// assembler. Consequently, all the opernds must get distinct registers.
360// Therefore, we mark all operands of a pseudo instruction as they interfere
361// with one another.
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000362//--------------------------------------------------------------------------
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000363void PhyRegAlloc::addInterf4PseudoInstr(const MachineInstr *MInst) {
364
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000365 bool setInterf = false;
366
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000367 // iterate over MI operands to find defs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000368 //
Chris Lattner7a176752001-12-04 00:03:30 +0000369 for( MachineInstr::val_const_op_iterator It1(MInst);!It1.done(); ++It1) {
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000370
371 const LiveRange *const LROfOp1 = LRI.getLiveRangeForValue( *It1 );
372
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000373 if( !LROfOp1 && It1.isDef() )
374 assert( 0 && "No LR for Def in PSEUDO insruction");
375
Chris Lattner7a176752001-12-04 00:03:30 +0000376 MachineInstr::val_const_op_iterator It2 = It1;
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000377 ++It2;
378
379 for( ; !It2.done(); ++It2) {
380
381 const LiveRange *const LROfOp2 = LRI.getLiveRangeForValue( *It2 );
382
383 if( LROfOp2) {
384
385 RegClass *const RCOfOp1 = LROfOp1->getRegClass();
386 RegClass *const RCOfOp2 = LROfOp2->getRegClass();
387
388 if( RCOfOp1 == RCOfOp2 ){
389 RCOfOp1->setInterference( LROfOp1, LROfOp2 );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000390 setInterf = true;
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000391 }
392
393 } // if Op2 has a LR
394
395 } // for all other defs in machine instr
396
397 } // for all operands in an instruction
398
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000399 if( !setInterf && (MInst->getNumOperands() > 2) ) {
400 cerr << "\nInterf not set for any operand in pseudo instr:\n";
401 cerr << *MInst;
402 assert(0 && "Interf not set for pseudo instr with > 2 operands" );
403
404 }
405
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000406}
407
408
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000409
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000410//----------------------------------------------------------------------------
411// This method will add interferences for incoming arguments to a method.
412//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000413void PhyRegAlloc::addInterferencesForArgs()
414{
415 // get the InSet of root BB
416 const LiveVarSet *const InSet = LVI->getInSetOfBB( Meth->front() );
417
418 // get the argument list
419 const Method::ArgumentListType& ArgList = Meth->getArgumentList();
420
421 // get an iterator to arg list
422 Method::ArgumentListType::const_iterator ArgIt = ArgList.begin();
423
424
425 for( ; ArgIt != ArgList.end() ; ++ArgIt) { // for each argument
426 addInterference( *ArgIt, InSet, false ); // add interferences between
427 // args and LVars at start
428 if( DEBUG_RA > 1) {
Chris Lattner697954c2002-01-20 22:54:45 +0000429 cerr << " - %% adding interference for argument ";
430 printValue((const Value *)*ArgIt); cerr << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000431 }
432 }
433}
434
435
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000436
437
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000438//----------------------------------------------------------------------------
439// This method is called after register allocation is complete to set the
440// allocated reisters in the machine code. This code will add register numbers
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000441// to MachineOperands that contain a Value. Also it calls target specific
442// methods to produce caller saving instructions. At the end, it adds all
443// additional instructions produced by the register allocator to the
444// instruction stream.
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000445//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000446void PhyRegAlloc::updateMachineCode()
447{
448
449 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
450
451 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
452
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000453 // get the iterator for machine instructions
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000454 //
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000455 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
456 MachineCodeForBasicBlock::iterator MInstIterator = MIVec.begin();
457
458 // iterate over all the machine instructions in BB
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000459 //
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000460 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
461
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000462 MachineInstr *MInst = *MInstIterator;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000463
464 unsigned Opcode = MInst->getOpCode();
465
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000466 // do not process Phis
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000467 if (TM.getInstrInfo().isPhi(Opcode))
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000468 continue;
469
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000470 // Now insert speical instructions (if necessary) for call/return
471 // instructions.
472 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000473 if (TM.getInstrInfo().isCall(Opcode) ||
474 TM.getInstrInfo().isReturn(Opcode)) {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000475
476 AddedInstrns *AI = AddedInstrMap[ MInst];
477 if ( !AI ) {
478 AI = new AddedInstrns();
479 AddedInstrMap[ MInst ] = AI;
480 }
481
482 // Tmp stack poistions are needed by some calls that have spilled args
483 // So reset it before we call each such method
Ruchira Sasanka6a3db8c2002-01-07 21:09:06 +0000484 //
485 mcInfo.popAllTempValues(TM);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000486
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000487 if (TM.getInstrInfo().isCall(Opcode))
488 MRI.colorCallArgs(MInst, LRI, AI, *this, *BBI);
489 else if (TM.getInstrInfo().isReturn(Opcode))
490 MRI.colorRetValue(MInst, LRI, AI);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000491 }
492
493
494 /* -- Using above code instead of this
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000495
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000496 // if this machine instr is call, insert caller saving code
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000497
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000498 if( (TM.getInstrInfo()).isCall( MInst->getOpCode()) )
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000499 MRI.insertCallerSavingCode(MInst, *BBI, *this );
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000500
501 */
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000502
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000503
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000504 // reset the stack offset for temporary variables since we may
505 // need that to spill
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000506 // mcInfo.popAllTempValues(TM);
Ruchira Sasankaf90870f2001-11-15 22:02:06 +0000507 // TODO ** : do later
Vikram S. Adve12af1642001-11-08 04:48:50 +0000508
Chris Lattner7a176752001-12-04 00:03:30 +0000509 //for(MachineInstr::val_const_op_iterator OpI(MInst);!OpI.done();++OpI) {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000510
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000511
512 // Now replace set the registers for operands in the machine instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000513 //
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000514 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
515
516 MachineOperand& Op = MInst->getOperand(OpNum);
517
518 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
519 Op.getOperandType() == MachineOperand::MO_CCRegister) {
520
521 const Value *const Val = Op.getVRegValue();
522
523 // delete this condition checking later (must assert if Val is null)
Chris Lattner045e7c82001-09-19 16:26:23 +0000524 if( !Val) {
525 if (DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000526 cerr << "Warning: NULL Value found for operand\n";
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000527 continue;
528 }
529 assert( Val && "Value is NULL");
530
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000531 LiveRange *const LR = LRI.getLiveRangeForValue(Val);
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000532
533 if ( !LR ) {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000534
535 // nothing to worry if it's a const or a label
536
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000537 if (DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000538 cerr << "*NO LR for operand : " << Op ;
539 cerr << " [reg:" << Op.getAllocatedRegNum() << "]";
540 cerr << " in inst:\t" << *MInst << "\n";
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000541 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000542
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000543 // if register is not allocated, mark register as invalid
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000544 if( Op.getAllocatedRegNum() == -1)
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000545 Op.setRegForValue( MRI.getInvalidRegNum());
Ruchira Sasankae727f852001-09-18 22:43:57 +0000546
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000547
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000548 continue;
549 }
550
551 unsigned RCID = (LR->getRegClass())->getID();
552
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000553 if( LR->hasColor() ) {
554 Op.setRegForValue( MRI.getUnifiedRegNum(RCID, LR->getColor()) );
555 }
556 else {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000557
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000558 // LR did NOT receive a color (register). Now, insert spill code
559 // for spilled opeands in this machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000560
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000561 //assert(0 && "LR must be spilled");
562 insertCode4SpilledLR(LR, MInst, *BBI, OpNum );
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000563
564 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000565 }
566
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000567 } // for each operand
568
569
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000570 // Now add instructions that the register allocator inserts before/after
571 // this machine instructions (done only for calls/rets/incoming args)
572 // We do this here, to ensure that spill for an instruction is inserted
573 // closest as possible to an instruction (see above insertCode4Spill...)
574 //
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000575 // If there are instructions to be added, *before* this machine
576 // instruction, add them now.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000577 //
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000578 if( AddedInstrMap[ MInst ] ) {
Chris Lattner697954c2002-01-20 22:54:45 +0000579 std::deque<MachineInstr *> &IBef = AddedInstrMap[MInst]->InstrnsBefore;
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000580
581 if( ! IBef.empty() ) {
Chris Lattner697954c2002-01-20 22:54:45 +0000582 std::deque<MachineInstr *>::iterator AdIt;
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000583
584 for( AdIt = IBef.begin(); AdIt != IBef.end() ; ++AdIt ) {
585
586 if( DEBUG_RA) {
587 cerr << "For inst " << *MInst;
Chris Lattner697954c2002-01-20 22:54:45 +0000588 cerr << " PREPENDed instr: " << **AdIt << "\n";
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000589 }
590
591 MInstIterator = MIVec.insert( MInstIterator, *AdIt );
592 ++MInstIterator;
593 }
594
595 }
596
597 }
598
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000599 // If there are instructions to be added *after* this machine
600 // instruction, add them now
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000601 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000602 if(AddedInstrMap[MInst] &&
603 !AddedInstrMap[MInst]->InstrnsAfter.empty() ) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000604
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000605 // if there are delay slots for this instruction, the instructions
606 // added after it must really go after the delayed instruction(s)
607 // So, we move the InstrAfter of the current instruction to the
608 // corresponding delayed instruction
609
610 unsigned delay;
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000611 if ((delay=TM.getInstrInfo().getNumDelaySlots(MInst->getOpCode())) >0){
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000612 move2DelayedInstr(MInst, *(MInstIterator+delay) );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000613
Chris Lattner697954c2002-01-20 22:54:45 +0000614 if(DEBUG_RA) cerr<< "\nMoved an added instr after the delay slot";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000615 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000616
617 else {
618
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000619
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000620 // Here we can add the "instructions after" to the current
621 // instruction since there are no delay slots for this instruction
622
Chris Lattner697954c2002-01-20 22:54:45 +0000623 std::deque<MachineInstr *> &IAft = AddedInstrMap[MInst]->InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000624
625 if( ! IAft.empty() ) {
626
Chris Lattner697954c2002-01-20 22:54:45 +0000627 std::deque<MachineInstr *>::iterator AdIt;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000628
629 ++MInstIterator; // advance to the next instruction
630
631 for( AdIt = IAft.begin(); AdIt != IAft.end() ; ++AdIt ) {
632
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000633 if(DEBUG_RA) {
634 cerr << "For inst " << *MInst;
Chris Lattner697954c2002-01-20 22:54:45 +0000635 cerr << " APPENDed instr: " << **AdIt << "\n";
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000636 }
637
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000638 MInstIterator = MIVec.insert( MInstIterator, *AdIt );
639 ++MInstIterator;
640 }
641
642 // MInsterator already points to the next instr. Since the
643 // for loop also increments it, decrement it to point to the
644 // instruction added last
645 --MInstIterator;
646
647 }
648
649 } // if not delay
650
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000651 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000652
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000653 } // for each machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000654 }
655}
656
657
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000658
659//----------------------------------------------------------------------------
660// This method inserts spill code for AN operand whose LR was spilled.
661// This method may be called several times for a single machine instruction
662// if it contains many spilled operands. Each time it is called, it finds
663// a register which is not live at that instruction and also which is not
664// used by other spilled operands of the same instruction. Then it uses
665// this register temporarily to accomodate the spilled value.
666//----------------------------------------------------------------------------
667void PhyRegAlloc::insertCode4SpilledLR(const LiveRange *LR,
668 MachineInstr *MInst,
669 const BasicBlock *BB,
670 const unsigned OpNum) {
671
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000672 assert(! TM.getInstrInfo().isCall(MInst->getOpCode()) &&
673 (! TM.getInstrInfo().isReturn(MInst->getOpCode())) &&
674 "Arg of a call/ret must be handled elsewhere");
675
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000676 MachineOperand& Op = MInst->getOperand(OpNum);
677 bool isDef = MInst->operandIsDefined(OpNum);
678 unsigned RegType = MRI.getRegType( LR );
679 int SpillOff = LR->getSpillOffFromFP();
680 RegClass *RC = LR->getRegClass();
681 const LiveVarSet *LVSetBef = LVI->getLiveVarSetBeforeMInst(MInst, BB);
Vikram S. Adve00521d72001-11-12 23:26:35 +0000682
Chris Lattner697954c2002-01-20 22:54:45 +0000683 mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000684
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000685 MachineInstr *MIBef=NULL, *AdIMid=NULL, *MIAft=NULL;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000686
687 int TmpRegU = getUsableUniRegAtMI(RC, RegType, MInst,LVSetBef, MIBef, MIAft);
688
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000689 // get the added instructions for this instruciton
690 AddedInstrns *AI = AddedInstrMap[ MInst ];
691 if ( !AI ) {
692 AI = new AddedInstrns();
693 AddedInstrMap[ MInst ] = AI;
694 }
695
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000696
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000697 if( !isDef ) {
698
699 // for a USE, we have to load the value of LR from stack to a TmpReg
700 // and use the TmpReg as one operand of instruction
701
702 // actual loading instruction
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000703 AdIMid = MRI.cpMem2RegMI(MRI.getFramePointer(), SpillOff, TmpRegU,RegType);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000704
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000705 if(MIBef)
706 AI->InstrnsBefore.push_back(MIBef);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000707
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000708 AI->InstrnsBefore.push_back(AdIMid);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000709
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000710 if(MIAft)
711 AI->InstrnsAfter.push_front(MIAft);
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000712
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000713
714 }
715 else { // if this is a Def
716
717 // for a DEF, we have to store the value produced by this instruction
718 // on the stack position allocated for this LR
719
720 // actual storing instruction
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000721 AdIMid = MRI.cpReg2MemMI(TmpRegU, MRI.getFramePointer(), SpillOff,RegType);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000722
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000723 if (MIBef)
724 AI->InstrnsBefore.push_back(MIBef);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000725
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000726 AI->InstrnsAfter.push_front(AdIMid);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000727
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000728 if (MIAft)
729 AI->InstrnsAfter.push_front(MIAft);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000730
731 } // if !DEF
732
733 cerr << "\nFor Inst " << *MInst;
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000734 cerr << " - SPILLED LR: "; LR->printSet();
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000735 cerr << "\n - Added Instructions:";
736 if( MIBef ) cerr << *MIBef;
737 cerr << *AdIMid;
738 if( MIAft ) cerr << *MIAft;
739
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000740 Op.setRegForValue( TmpRegU ); // set the opearnd
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000741
742
743}
744
745
746
747
748
749
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000750//----------------------------------------------------------------------------
751// We can use the following method to get a temporary register to be used
752// BEFORE any given machine instruction. If there is a register available,
753// this method will simply return that register and set MIBef = MIAft = NULL.
754// Otherwise, it will return a register and MIAft and MIBef will contain
755// two instructions used to free up this returned register.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000756// Returned register number is the UNIFIED register number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000757//----------------------------------------------------------------------------
758
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000759int PhyRegAlloc::getUsableUniRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000760 const int RegType,
761 const MachineInstr *MInst,
762 const LiveVarSet *LVSetBef,
763 MachineInstr *MIBef,
764 MachineInstr *MIAft) {
765
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000766 int RegU = getUnusedUniRegAtMI(RC, MInst, LVSetBef);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000767
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000768
769 if( RegU != -1) {
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000770 // we found an unused register, so we can simply use it
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000771 MIBef = MIAft = NULL;
772 }
773 else {
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000774 // we couldn't find an unused register. Generate code to free up a reg by
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000775 // saving it on stack and restoring after the instruction
776
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000777 int TmpOff = mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
Vikram S. Adve12af1642001-11-08 04:48:50 +0000778
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000779 RegU = getUniRegNotUsedByThisInst(RC, MInst);
780 MIBef = MRI.cpReg2MemMI(RegU, MRI.getFramePointer(), TmpOff, RegType );
781 MIAft = MRI.cpMem2RegMI(MRI.getFramePointer(), TmpOff, RegU, RegType );
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000782 }
783
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000784 return RegU;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000785}
786
787//----------------------------------------------------------------------------
788// This method is called to get a new unused register that can be used to
789// accomodate a spilled value.
790// This method may be called several times for a single machine instruction
791// if it contains many spilled operands. Each time it is called, it finds
792// a register which is not live at that instruction and also which is not
793// used by other spilled operands of the same instruction.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000794// Return register number is relative to the register class. NOT
795// unified number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000796//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000797int PhyRegAlloc::getUnusedUniRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000798 const MachineInstr *MInst,
799 const LiveVarSet *LVSetBef) {
800
801 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
802
803 bool *IsColorUsedArr = RC->getIsColorUsedArr();
804
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000805 for(unsigned i=0; i < NumAvailRegs; i++) // Reset array
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000806 IsColorUsedArr[i] = false;
807
808 LiveVarSet::const_iterator LIt = LVSetBef->begin();
809
810 // for each live var in live variable set after machine inst
811 for( ; LIt != LVSetBef->end(); ++LIt) {
812
813 // get the live range corresponding to live var
814 LiveRange *const LRofLV = LRI.getLiveRangeForValue(*LIt );
815
816 // LR can be null if it is a const since a const
817 // doesn't have a dominating def - see Assumptions above
818 if( LRofLV )
819 if( LRofLV->hasColor() )
820 IsColorUsedArr[ LRofLV->getColor() ] = true;
821 }
822
823 // It is possible that one operand of this MInst was already spilled
824 // and it received some register temporarily. If that's the case,
825 // it is recorded in machine operand. We must skip such registers.
826
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000827 setRelRegsUsedByThisInst(RC, MInst);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000828
829 unsigned c; // find first unused color
830 for( c=0; c < NumAvailRegs; c++)
831 if( ! IsColorUsedArr[ c ] ) break;
832
833 if(c < NumAvailRegs)
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000834 return MRI.getUnifiedRegNum(RC->getID(), c);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000835 else
836 return -1;
837
838
839}
840
841
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000842//----------------------------------------------------------------------------
843// Get any other register in a register class, other than what is used
844// by operands of a machine instruction. Returns the unified reg number.
845//----------------------------------------------------------------------------
846int PhyRegAlloc::getUniRegNotUsedByThisInst(RegClass *RC,
847 const MachineInstr *MInst) {
848
849 bool *IsColorUsedArr = RC->getIsColorUsedArr();
850 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
851
852
853 for(unsigned i=0; i < NumAvailRegs ; i++) // Reset array
854 IsColorUsedArr[i] = false;
855
856 setRelRegsUsedByThisInst(RC, MInst);
857
858 unsigned c; // find first unused color
859 for( c=0; c < RC->getNumOfAvailRegs(); c++)
860 if( ! IsColorUsedArr[ c ] ) break;
861
862 if(c < NumAvailRegs)
863 return MRI.getUnifiedRegNum(RC->getID(), c);
864 else
865 assert( 0 && "FATAL: No free register could be found in reg class!!");
Chris Lattner697954c2002-01-20 22:54:45 +0000866 return 0;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000867}
868
869
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000870//----------------------------------------------------------------------------
871// This method modifies the IsColorUsedArr of the register class passed to it.
872// It sets the bits corresponding to the registers used by this machine
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000873// instructions. Both explicit and implicit operands are set.
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000874//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000875void PhyRegAlloc::setRelRegsUsedByThisInst(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000876 const MachineInstr *MInst ) {
877
878 bool *IsColorUsedArr = RC->getIsColorUsedArr();
879
880 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
881
882 const MachineOperand& Op = MInst->getOperand(OpNum);
883
884 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000885 Op.getOperandType() == MachineOperand::MO_CCRegister ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000886
887 const Value *const Val = Op.getVRegValue();
888
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000889 if( Val )
890 if( MRI.getRegClassIDOfValue(Val) == RC->getID() ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000891 int Reg;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000892 if( (Reg=Op.getAllocatedRegNum()) != -1) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000893 IsColorUsedArr[ Reg ] = true;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000894 }
895 else {
896 // it is possilbe that this operand still is not marked with
897 // a register but it has a LR and that received a color
898
899 LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
900 if( LROfVal)
901 if( LROfVal->hasColor() )
902 IsColorUsedArr[ LROfVal->getColor() ] = true;
903 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000904
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000905 } // if reg classes are the same
906 }
907 else if (Op.getOperandType() == MachineOperand::MO_MachineRegister) {
908 IsColorUsedArr[ Op.getMachineRegNum() ] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000909 }
910 }
911
912 // If there are implicit references, mark them as well
913
914 for(unsigned z=0; z < MInst->getNumImplicitRefs(); z++) {
915
916 LiveRange *const LRofImpRef =
917 LRI.getLiveRangeForValue( MInst->getImplicitRef(z) );
Chris Lattner697954c2002-01-20 22:54:45 +0000918
919 if(LRofImpRef && LRofImpRef->hasColor())
920 IsColorUsedArr[LRofImpRef->getColor()] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000921 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000922}
923
924
925
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000926
927
928
929
930
931//----------------------------------------------------------------------------
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000932// If there are delay slots for an instruction, the instructions
933// added after it must really go after the delayed instruction(s).
934// So, we move the InstrAfter of that instruction to the
935// corresponding delayed instruction using the following method.
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000936
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000937//----------------------------------------------------------------------------
938void PhyRegAlloc:: move2DelayedInstr(const MachineInstr *OrigMI,
939 const MachineInstr *DelayedMI) {
940
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000941 // "added after" instructions of the original instr
Chris Lattner697954c2002-01-20 22:54:45 +0000942 std::deque<MachineInstr *> &OrigAft = AddedInstrMap[OrigMI]->InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000943
944 // "added instructions" of the delayed instr
945 AddedInstrns *DelayAdI = AddedInstrMap[DelayedMI];
946
947 if(! DelayAdI ) { // create a new "added after" if necessary
948 DelayAdI = new AddedInstrns();
949 AddedInstrMap[DelayedMI] = DelayAdI;
950 }
951
952 // "added after" instructions of the delayed instr
Chris Lattner697954c2002-01-20 22:54:45 +0000953 std::deque<MachineInstr *> &DelayedAft = DelayAdI->InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000954
955 // go thru all the "added after instructions" of the original instruction
956 // and append them to the "addded after instructions" of the delayed
957 // instructions
Chris Lattner697954c2002-01-20 22:54:45 +0000958 DelayedAft.insert(DelayedAft.end(), OrigAft.begin(), OrigAft.end());
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000959
960 // empty the "added after instructions" of the original instruction
961 OrigAft.clear();
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000962}
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000963
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000964//----------------------------------------------------------------------------
965// This method prints the code with registers after register allocation is
966// complete.
967//----------------------------------------------------------------------------
968void PhyRegAlloc::printMachineCode()
969{
970
Chris Lattner697954c2002-01-20 22:54:45 +0000971 cerr << "\n;************** Method " << Meth->getName()
972 << " *****************\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000973
974 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
975
976 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
977
Chris Lattner697954c2002-01-20 22:54:45 +0000978 cerr << "\n"; printLabel( *BBI); cerr << ": ";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000979
980 // get the iterator for machine instructions
981 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
982 MachineCodeForBasicBlock::iterator MInstIterator = MIVec.begin();
983
984 // iterate over all the machine instructions in BB
985 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
986
987 MachineInstr *const MInst = *MInstIterator;
988
989
Chris Lattner697954c2002-01-20 22:54:45 +0000990 cerr << "\n\t";
991 cerr << TargetInstrDescriptors[MInst->getOpCode()].opCodeString;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000992
993
Chris Lattner7a176752001-12-04 00:03:30 +0000994 //for(MachineInstr::val_const_op_iterator OpI(MInst);!OpI.done();++OpI) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000995
996 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
997
998 MachineOperand& Op = MInst->getOperand(OpNum);
999
1000 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasanka97b8b442001-10-18 22:36:26 +00001001 Op.getOperandType() == MachineOperand::MO_CCRegister /*||
1002 Op.getOperandType() == MachineOperand::MO_PCRelativeDisp*/ ) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001003
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001004 const Value *const Val = Op.getVRegValue () ;
Ruchira Sasankae727f852001-09-18 22:43:57 +00001005 // ****this code is temporary till NULL Values are fixed
1006 if( ! Val ) {
Chris Lattner697954c2002-01-20 22:54:45 +00001007 cerr << "\t<*NULL*>";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001008 continue;
1009 }
Ruchira Sasankae727f852001-09-18 22:43:57 +00001010
1011 // if a label or a constant
Chris Lattnerdbe53042002-01-21 01:33:12 +00001012 if(isa<BasicBlock>(Val)) {
Chris Lattner697954c2002-01-20 22:54:45 +00001013 cerr << "\t"; printLabel( Op.getVRegValue () );
1014 } else {
Ruchira Sasankae727f852001-09-18 22:43:57 +00001015 // else it must be a register value
1016 const int RegNum = Op.getAllocatedRegNum();
1017
Chris Lattner697954c2002-01-20 22:54:45 +00001018 cerr << "\t" << "%" << MRI.getUnifiedRegName( RegNum );
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001019 if (Val->hasName() )
Chris Lattner697954c2002-01-20 22:54:45 +00001020 cerr << "(" << Val->getName() << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001021 else
Chris Lattner697954c2002-01-20 22:54:45 +00001022 cerr << "(" << Val << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001023
1024 if( Op.opIsDef() )
Chris Lattner697954c2002-01-20 22:54:45 +00001025 cerr << "*";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001026
1027 const LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
1028 if( LROfVal )
1029 if( LROfVal->hasSpillOffset() )
Chris Lattner697954c2002-01-20 22:54:45 +00001030 cerr << "$";
Ruchira Sasankae727f852001-09-18 22:43:57 +00001031 }
1032
1033 }
1034 else if(Op.getOperandType() == MachineOperand::MO_MachineRegister) {
Chris Lattner697954c2002-01-20 22:54:45 +00001035 cerr << "\t" << "%" << MRI.getUnifiedRegName(Op.getMachineRegNum());
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001036 }
1037
1038 else
Chris Lattner697954c2002-01-20 22:54:45 +00001039 cerr << "\t" << Op; // use dump field
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001040 }
1041
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001042
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001043
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001044 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
1045 if( NumOfImpRefs > 0 ) {
1046
Chris Lattner697954c2002-01-20 22:54:45 +00001047 cerr << "\tImplicit:";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001048
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001049 for(unsigned z=0; z < NumOfImpRefs; z++) {
1050 printValue( MInst->getImplicitRef(z) );
Chris Lattner697954c2002-01-20 22:54:45 +00001051 cerr << "\t";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001052 }
1053
1054 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001055
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001056 } // for all machine instructions
1057
Chris Lattner697954c2002-01-20 22:54:45 +00001058 cerr << "\n";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001059
1060 } // for all BBs
1061
Chris Lattner697954c2002-01-20 22:54:45 +00001062 cerr << "\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001063}
1064
Ruchira Sasankae727f852001-09-18 22:43:57 +00001065
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001066#if 0
1067
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001068//----------------------------------------------------------------------------
1069//
1070//----------------------------------------------------------------------------
1071
1072void PhyRegAlloc::colorCallRetArgs()
1073{
1074
1075 CallRetInstrListType &CallRetInstList = LRI.getCallRetInstrList();
1076 CallRetInstrListType::const_iterator It = CallRetInstList.begin();
1077
1078 for( ; It != CallRetInstList.end(); ++It ) {
1079
Ruchira Sasankaa90e7702001-10-15 16:26:38 +00001080 const MachineInstr *const CRMI = *It;
1081 unsigned OpCode = CRMI->getOpCode();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001082
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001083 // get the added instructions for this Call/Ret instruciton
1084 AddedInstrns *AI = AddedInstrMap[ CRMI ];
1085 if ( !AI ) {
1086 AI = new AddedInstrns();
1087 AddedInstrMap[ CRMI ] = AI;
1088 }
1089
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001090 // Tmp stack poistions are needed by some calls that have spilled args
1091 // So reset it before we call each such method
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001092 //mcInfo.popAllTempValues(TM);
1093
1094
Vikram S. Adve12af1642001-11-08 04:48:50 +00001095
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001096 if (TM.getInstrInfo().isCall(OpCode))
1097 MRI.colorCallArgs(CRMI, LRI, AI, *this);
1098 else if (TM.getInstrInfo().isReturn(OpCode))
Ruchira Sasankaa90e7702001-10-15 16:26:38 +00001099 MRI.colorRetValue( CRMI, LRI, AI );
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001100 else
1101 assert(0 && "Non Call/Ret instrn in CallRetInstrList\n");
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001102 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001103}
1104
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001105#endif
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001106
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001107//----------------------------------------------------------------------------
1108
1109//----------------------------------------------------------------------------
1110void PhyRegAlloc::colorIncomingArgs()
1111{
1112 const BasicBlock *const FirstBB = Meth->front();
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001113 const MachineInstr *FirstMI = FirstBB->getMachineInstrVec().front();
1114 assert(FirstMI && "No machine instruction in entry BB");
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001115
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001116 AddedInstrns *AI = AddedInstrMap[FirstMI];
1117 if (!AI)
1118 AddedInstrMap[FirstMI] = AI = new AddedInstrns();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001119
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001120 MRI.colorMethodArgs(Meth, LRI, AI);
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001121}
1122
Ruchira Sasankae727f852001-09-18 22:43:57 +00001123
1124//----------------------------------------------------------------------------
1125// Used to generate a label for a basic block
1126//----------------------------------------------------------------------------
Chris Lattner697954c2002-01-20 22:54:45 +00001127void PhyRegAlloc::printLabel(const Value *const Val) {
1128 if (Val->hasName())
1129 cerr << Val->getName();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001130 else
Chris Lattner697954c2002-01-20 22:54:45 +00001131 cerr << "Label" << Val;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001132}
1133
1134
Ruchira Sasankae727f852001-09-18 22:43:57 +00001135//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001136// This method calls setSugColorUsable method of each live range. This
1137// will determine whether the suggested color of LR is really usable.
1138// A suggested color is not usable when the suggested color is volatile
1139// AND when there are call interferences
1140//----------------------------------------------------------------------------
1141
1142void PhyRegAlloc::markUnusableSugColors()
1143{
Chris Lattner697954c2002-01-20 22:54:45 +00001144 if(DEBUG_RA ) cerr << "\nmarking unusable suggested colors ...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001145
1146 // hash map iterator
1147 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
1148 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
1149
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001150 for(; HMI != HMIEnd ; ++HMI ) {
1151 if (HMI->first) {
1152 LiveRange *L = HMI->second; // get the LiveRange
1153 if (L) {
1154 if(L->hasSuggestedColor()) {
1155 int RCID = L->getRegClass()->getID();
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001156 if( MRI.isRegVolatile( RCID, L->getSuggestedColor()) &&
1157 L->isCallInterference() )
1158 L->setSuggestedColorUsable( false );
1159 else
1160 L->setSuggestedColorUsable( true );
1161 }
1162 } // if L->hasSuggestedColor()
1163 }
1164 } // for all LR's in hash map
1165}
1166
1167
1168
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001169//----------------------------------------------------------------------------
1170// The following method will set the stack offsets of the live ranges that
1171// are decided to be spillled. This must be called just after coloring the
1172// LRs using the graph coloring algo. For each live range that is spilled,
1173// this method allocate a new spill position on the stack.
1174//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001175
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001176void PhyRegAlloc::allocateStackSpace4SpilledLRs()
1177{
Chris Lattner697954c2002-01-20 22:54:45 +00001178 if(DEBUG_RA ) cerr << "\nsetting LR stack offsets ...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001179
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001180 // hash map iterator
1181 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
1182 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
1183
1184 for( ; HMI != HMIEnd ; ++HMI ) {
Chris Lattner697954c2002-01-20 22:54:45 +00001185 if(HMI->first && HMI->second) {
1186 LiveRange *L = HMI->second; // get the LiveRange
1187 if( ! L->hasColor() )
1188 // NOTE: ** allocating the size of long Type **
1189 L->setSpillOffFromFP(mcInfo.allocateSpilledValue(TM, Type::LongTy));
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001190 }
1191 } // for all LR's in hash map
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001192}
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001193
1194
1195
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001196//----------------------------------------------------------------------------
Ruchira Sasankae727f852001-09-18 22:43:57 +00001197// The entry pont to Register Allocation
1198//----------------------------------------------------------------------------
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001199
1200void PhyRegAlloc::allocateRegisters()
1201{
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001202
1203 // make sure that we put all register classes into the RegClassList
1204 // before we call constructLiveRanges (now done in the constructor of
1205 // PhyRegAlloc class).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001206 //
1207 LRI.constructLiveRanges(); // create LR info
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001208
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001209 if (DEBUG_RA)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001210 LRI.printLiveRanges();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001211
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001212 createIGNodeListsAndIGs(); // create IGNode list and IGs
1213
1214 buildInterferenceGraphs(); // build IGs in all reg classes
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001215
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001216
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001217 if (DEBUG_RA) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001218 // print all LRs in all reg classes
1219 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1220 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001221
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001222 // print IGs in all register classes
1223 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1224 RegClassList[ rc ]->printIG();
1225 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001226
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001227
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001228 LRI.coalesceLRs(); // coalesce all live ranges
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001229
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +00001230
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001231 if( DEBUG_RA) {
1232 // print all LRs in all reg classes
1233 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1234 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001235
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001236 // print IGs in all register classes
1237 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1238 RegClassList[ rc ]->printIG();
1239 }
1240
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001241
1242 // mark un-usable suggested color before graph coloring algorithm.
1243 // When this is done, the graph coloring algo will not reserve
1244 // suggested color unnecessarily - they can be used by another LR
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001245 //
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001246 markUnusableSugColors();
1247
1248 // color all register classes using the graph coloring algo
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001249 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1250 RegClassList[ rc ]->colorAllRegs();
1251
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001252 // Atter grpah coloring, if some LRs did not receive a color (i.e, spilled)
1253 // a poistion for such spilled LRs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001254 //
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001255 allocateStackSpace4SpilledLRs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001256
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001257 mcInfo.popAllTempValues(TM); // TODO **Check
1258
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001259 // color incoming args - if the correct color was not received
1260 // insert code to copy to the correct register
1261 //
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001262 colorIncomingArgs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001263
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001264 // Now update the machine code with register names and add any
1265 // additional code inserted by the register allocator to the instruction
1266 // stream
1267 //
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001268 updateMachineCode();
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001269
Chris Lattner045e7c82001-09-19 16:26:23 +00001270 if (DEBUG_RA) {
Vikram S. Adve12af1642001-11-08 04:48:50 +00001271 MachineCodeForMethod::get(Meth).dump();
Chris Lattner045e7c82001-09-19 16:26:23 +00001272 printMachineCode(); // only for DEBUGGING
1273 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001274}
1275
Ruchira Sasankae727f852001-09-18 22:43:57 +00001276
1277