blob: 1ce41fda430b0f9fd4d7aa9d2e40f743f526900b [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"
Chris Lattner14ab1ce2002-02-04 17:48:00 +000018#include "llvm/Analysis/LoopInfo.h"
Vikram S. Adve12af1642001-11-08 04:48:50 +000019#include "llvm/Target/TargetMachine.h"
20#include "llvm/Target/MachineFrameInfo.h"
Chris Lattner30adeb62002-02-04 16:36:59 +000021#include "llvm/Method.h"
Chris Lattner697954c2002-01-20 22:54:45 +000022#include <iostream>
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000023#include <math.h>
Chris Lattner697954c2002-01-20 22:54:45 +000024using std::cerr;
Vikram S. Adve12af1642001-11-08 04:48:50 +000025
26
27// ***TODO: There are several places we add instructions. Validate the order
28// of adding these instructions.
Ruchira Sasanka174bded2001-10-28 18:12:02 +000029
Chris Lattner045e7c82001-09-19 16:26:23 +000030cl::Enum<RegAllocDebugLevel_t> DEBUG_RA("dregalloc", cl::NoFlags,
31 "enable register allocation debugging information",
32 clEnumValN(RA_DEBUG_None , "n", "disable debug output"),
33 clEnumValN(RA_DEBUG_Normal , "y", "enable debug output"),
34 clEnumValN(RA_DEBUG_Verbose, "v", "enable extra debug output"), 0);
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000035
36
Chris Lattner2f9b28e2002-02-04 15:54:09 +000037//----------------------------------------------------------------------------
38// RegisterAllocation pass front end...
39//----------------------------------------------------------------------------
40namespace {
41 class RegisterAllocator : public MethodPass {
42 TargetMachine &Target;
43 public:
44 inline RegisterAllocator(TargetMachine &T) : Target(T) {}
Chris Lattner6dd98a62002-02-04 00:33:08 +000045
Chris Lattner2f9b28e2002-02-04 15:54:09 +000046 bool runOnMethod(Method *M) {
47 if (DEBUG_RA)
48 cerr << "\n******************** Method "<< M->getName()
49 << " ********************\n";
50
51 MethodLiveVarInfo LVI(M); // Analyze live varaibles
52 LVI.analyze();
53
Chris Lattner4911c352002-02-04 17:39:42 +000054 PhyRegAlloc PRA(M, Target, &LVI,
Chris Lattner14ab1ce2002-02-04 17:48:00 +000055 &getAnalysis<cfg::LoopInfo>());
Chris Lattner2f9b28e2002-02-04 15:54:09 +000056 PRA.allocateRegisters();
57
58 if (DEBUG_RA) cerr << "\nRegister allocation complete!\n";
59 return false;
60 }
Chris Lattner4911c352002-02-04 17:39:42 +000061
62 virtual void getAnalysisUsageInfo(Pass::AnalysisSet &Requires,
63 Pass::AnalysisSet &Destroyed,
64 Pass::AnalysisSet &Provided) {
Chris Lattner14ab1ce2002-02-04 17:48:00 +000065 Requires.push_back(cfg::LoopInfo::ID);
Chris Lattner4911c352002-02-04 17:39:42 +000066 }
Chris Lattner2f9b28e2002-02-04 15:54:09 +000067 };
Chris Lattner6dd98a62002-02-04 00:33:08 +000068}
69
Chris Lattner2f9b28e2002-02-04 15:54:09 +000070MethodPass *getRegisterAllocator(TargetMachine &T) {
71 return new RegisterAllocator(T);
72}
Chris Lattner6dd98a62002-02-04 00:33:08 +000073
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000074//----------------------------------------------------------------------------
75// Constructor: Init local composite objects and create register classes.
76//----------------------------------------------------------------------------
Vikram S. Adve12af1642001-11-08 04:48:50 +000077PhyRegAlloc::PhyRegAlloc(Method *M,
Ruchira Sasanka8e604792001-09-14 21:18:34 +000078 const TargetMachine& tm,
Chris Lattner4911c352002-02-04 17:39:42 +000079 MethodLiveVarInfo *Lvi,
Chris Lattner14ab1ce2002-02-04 17:48:00 +000080 cfg::LoopInfo *LDC)
Chris Lattner697954c2002-01-20 22:54:45 +000081 : TM(tm), Meth(M),
Vikram S. Adve12af1642001-11-08 04:48:50 +000082 mcInfo(MachineCodeForMethod::get(M)),
83 LVI(Lvi), LRI(M, tm, RegClassList),
Ruchira Sasanka8e604792001-09-14 21:18:34 +000084 MRI( tm.getRegInfo() ),
85 NumOfRegClasses(MRI.getNumOfRegClasses()),
Chris Lattner4911c352002-02-04 17:39:42 +000086 LoopDepthCalc(LDC) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +000087
88 // create each RegisterClass and put in RegClassList
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000089 //
Chris Lattner697954c2002-01-20 22:54:45 +000090 for(unsigned int rc=0; rc < NumOfRegClasses; rc++)
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000091 RegClassList.push_back( new RegClass(M, MRI.getMachineRegClass(rc),
92 &ResColList) );
Ruchira Sasanka8e604792001-09-14 21:18:34 +000093}
94
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000095
96//----------------------------------------------------------------------------
97// Destructor: Deletes register classes
98//----------------------------------------------------------------------------
99PhyRegAlloc::~PhyRegAlloc() {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000100 for( unsigned int rc=0; rc < NumOfRegClasses; rc++)
101 delete RegClassList[rc];
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000102}
103
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000104//----------------------------------------------------------------------------
105// This method initally creates interference graphs (one in each reg class)
106// and IGNodeList (one in each IG). The actual nodes will be pushed later.
107//----------------------------------------------------------------------------
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000108void PhyRegAlloc::createIGNodeListsAndIGs() {
109 if (DEBUG_RA) cerr << "Creating LR lists ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000110
111 // hash map iterator
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000112 LiveRangeMapType::const_iterator HMI = LRI.getLiveRangeMap()->begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000113
114 // hash map end
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000115 LiveRangeMapType::const_iterator HMIEnd = LRI.getLiveRangeMap()->end();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000116
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000117 for (; HMI != HMIEnd ; ++HMI ) {
118 if (HMI->first) {
119 LiveRange *L = HMI->second; // get the LiveRange
120 if (!L) {
121 if( DEBUG_RA) {
122 cerr << "\n*?!?Warning: Null liver range found for: ";
123 printValue(HMI->first); cerr << "\n";
124 }
125 continue;
126 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000127 // if the Value * is not null, and LR
128 // is not yet written to the IGNodeList
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000129 if( !(L->getUserIGNode()) ) {
130 RegClass *const RC = // RegClass of first value in the LR
131 RegClassList[ L->getRegClass()->getID() ];
132
133 RC->addLRToIG(L); // add this LR to an IG
134 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000135 }
136 }
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000137
138 // init RegClassList
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000139 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000140 RegClassList[rc]->createInterferenceGraph();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000141
142 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000143 cerr << "LRLists Created!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000144}
145
146
147
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000148
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000149//----------------------------------------------------------------------------
150// This method will add all interferences at for a given instruction.
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000151// Interence occurs only if the LR of Def (Inst or Arg) is of the same reg
152// class as that of live var. The live var passed to this function is the
153// LVset AFTER the instruction
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000154//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000155void PhyRegAlloc::addInterference(const Value *const Def,
156 const LiveVarSet *const LVSet,
157 const bool isCallInst) {
158
159 LiveVarSet::const_iterator LIt = LVSet->begin();
160
161 // get the live range of instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000162 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000163 const LiveRange *const LROfDef = LRI.getLiveRangeForValue( Def );
164
165 IGNode *const IGNodeOfDef = LROfDef->getUserIGNode();
166 assert( IGNodeOfDef );
167
168 RegClass *const RCOfDef = LROfDef->getRegClass();
169
170 // for each live var in live variable set
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000171 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000172 for( ; LIt != LVSet->end(); ++LIt) {
173
174 if( DEBUG_RA > 1) {
Chris Lattner697954c2002-01-20 22:54:45 +0000175 cerr << "< Def="; printValue(Def);
176 cerr << ", Lvar="; printValue( *LIt); cerr << "> ";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000177 }
178
179 // get the live range corresponding to live var
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000180 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000181 LiveRange *const LROfVar = LRI.getLiveRangeForValue(*LIt );
182
183 // LROfVar can be null if it is a const since a const
184 // doesn't have a dominating def - see Assumptions above
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000185 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000186 if (LROfVar) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000187 if(LROfDef == LROfVar) // do not set interf for same LR
188 continue;
189
190 // if 2 reg classes are the same set interference
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000191 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000192 if(RCOfDef == LROfVar->getRegClass()) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000193 RCOfDef->setInterference( LROfDef, LROfVar);
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000194 } else if(DEBUG_RA > 1) {
195 // we will not have LRs for values not explicitly allocated in the
196 // instruction stream (e.g., constants)
197 cerr << " warning: no live range for " ;
198 printValue(*LIt); cerr << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000199 }
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000200 }
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000201 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000202}
203
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000204
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000205
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000206//----------------------------------------------------------------------------
207// For a call instruction, this method sets the CallInterference flag in
208// the LR of each variable live int the Live Variable Set live after the
209// call instruction (except the return value of the call instruction - since
210// the return value does not interfere with that call itself).
211//----------------------------------------------------------------------------
212
213void PhyRegAlloc::setCallInterferences(const MachineInstr *MInst,
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000214 const LiveVarSet *const LVSetAft ) {
215
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000216 // Now find the LR of the return value of the call
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000217 // We do this because, we look at the LV set *after* the instruction
218 // to determine, which LRs must be saved across calls. The return value
219 // of the call is live in this set - but it does not interfere with call
220 // (i.e., we can allocate a volatile register to the return value)
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000221 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000222 LiveRange *RetValLR = NULL;
Ruchira Sasankab3b6f532001-10-21 16:43:41 +0000223 const Value *RetVal = MRI.getCallInstRetVal( MInst );
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000224
Ruchira Sasankab3b6f532001-10-21 16:43:41 +0000225 if( RetVal ) {
226 RetValLR = LRI.getLiveRangeForValue( RetVal );
227 assert( RetValLR && "No LR for RetValue of call");
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000228 }
229
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000230 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000231 cerr << "\n For call inst: " << *MInst;
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000232
233 LiveVarSet::const_iterator LIt = LVSetAft->begin();
234
235 // for each live var in live variable set after machine inst
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000236 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000237 for( ; LIt != LVSetAft->end(); ++LIt) {
238
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000239 // get the live range corresponding to live var
240 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000241 LiveRange *const LR = LRI.getLiveRangeForValue(*LIt );
242
243 if( LR && DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000244 cerr << "\n\tLR Aft Call: ";
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000245 LR->printSet();
246 }
247
248
249 // LR can be null if it is a const since a const
250 // doesn't have a dominating def - see Assumptions above
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000251 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000252 if( LR && (LR != RetValLR) ) {
253 LR->setCallInterference();
254 if( DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000255 cerr << "\n ++Added call interf for LR: " ;
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000256 LR->printSet();
257 }
258 }
259
260 }
261
262}
263
264
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000265
266
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000267//----------------------------------------------------------------------------
268// This method will walk thru code and create interferences in the IG of
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000269// each RegClass. Also, this method calculates the spill cost of each
270// Live Range (it is done in this method to save another pass over the code).
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000271//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000272void PhyRegAlloc::buildInterferenceGraphs()
273{
274
Chris Lattner697954c2002-01-20 22:54:45 +0000275 if(DEBUG_RA) cerr << "Creating interference graphs ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000276
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000277 unsigned BBLoopDepthCost;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000278 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
279
280 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
281
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000282 // find the 10^(loop_depth) of this BB
283 //
Chris Lattner4911c352002-02-04 17:39:42 +0000284 BBLoopDepthCost = (unsigned) pow( 10.0, LoopDepthCalc->getLoopDepth(*BBI));
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000285
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000286 // get the iterator for machine instructions
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000287 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000288 const MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
289 MachineCodeForBasicBlock::const_iterator
290 MInstIterator = MIVec.begin();
291
292 // iterate over all the machine instructions in BB
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000293 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000294 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000295
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +0000296 const MachineInstr * MInst = *MInstIterator;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000297
298 // get the LV set after the instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000299 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000300 const LiveVarSet *const LVSetAI =
301 LVI->getLiveVarSetAfterMInst(MInst, *BBI);
302
303 const bool isCallInst = TM.getInstrInfo().isCall(MInst->getOpCode());
304
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000305 if( isCallInst ) {
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000306 // set the isCallInterference flag of each live range wich extends
307 // accross this call instruction. This information is used by graph
308 // coloring algo to avoid allocating volatile colors to live ranges
309 // that span across calls (since they have to be saved/restored)
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000310 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000311 setCallInterferences( MInst, LVSetAI);
312 }
313
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000314
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000315 // iterate over all MI operands to find defs
316 //
Chris Lattner7a176752001-12-04 00:03:30 +0000317 for( MachineInstr::val_const_op_iterator OpI(MInst);!OpI.done(); ++OpI) {
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000318
319 if( OpI.isDef() ) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000320 // create a new LR iff this operand is a def
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000321 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000322 addInterference(*OpI, LVSetAI, isCallInst );
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000323 }
324
325 // Calculate the spill cost of each live range
326 //
327 LiveRange *LR = LRI.getLiveRangeForValue( *OpI );
328 if( LR )
329 LR->addSpillCost(BBLoopDepthCost);
330 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000331
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000332
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000333 // if there are multiple defs in this instruction e.g. in SETX
334 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000335 if (TM.getInstrInfo().isPseudoInstr(MInst->getOpCode()))
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000336 addInterf4PseudoInstr(MInst);
337
338
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000339 // Also add interference for any implicit definitions in a machine
340 // instr (currently, only calls have this).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000341 //
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000342 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
343 if( NumOfImpRefs > 0 ) {
344 for(unsigned z=0; z < NumOfImpRefs; z++)
345 if( MInst->implicitRefIsDefined(z) )
346 addInterference( MInst->getImplicitRef(z), LVSetAI, isCallInst );
347 }
348
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +0000349
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000350 } // for all machine instructions in BB
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000351
352 } // for all BBs in method
353
354
355 // add interferences for method arguments. Since there are no explict
356 // defs in method for args, we have to add them manually
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000357 //
358 addInterferencesForArgs();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000359
360 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000361 cerr << "Interference graphs calculted!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000362
363}
364
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000365
366
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000367//--------------------------------------------------------------------------
368// Pseudo instructions will be exapnded to multiple instructions by the
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000369// assembler. Consequently, all the opernds must get distinct registers.
370// Therefore, we mark all operands of a pseudo instruction as they interfere
371// with one another.
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000372//--------------------------------------------------------------------------
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000373void PhyRegAlloc::addInterf4PseudoInstr(const MachineInstr *MInst) {
374
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000375 bool setInterf = false;
376
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000377 // iterate over MI operands to find defs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000378 //
Chris Lattner7a176752001-12-04 00:03:30 +0000379 for( MachineInstr::val_const_op_iterator It1(MInst);!It1.done(); ++It1) {
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000380
381 const LiveRange *const LROfOp1 = LRI.getLiveRangeForValue( *It1 );
382
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000383 if( !LROfOp1 && It1.isDef() )
384 assert( 0 && "No LR for Def in PSEUDO insruction");
385
Chris Lattner7a176752001-12-04 00:03:30 +0000386 MachineInstr::val_const_op_iterator It2 = It1;
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000387 ++It2;
388
389 for( ; !It2.done(); ++It2) {
390
391 const LiveRange *const LROfOp2 = LRI.getLiveRangeForValue( *It2 );
392
393 if( LROfOp2) {
394
395 RegClass *const RCOfOp1 = LROfOp1->getRegClass();
396 RegClass *const RCOfOp2 = LROfOp2->getRegClass();
397
398 if( RCOfOp1 == RCOfOp2 ){
399 RCOfOp1->setInterference( LROfOp1, LROfOp2 );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000400 setInterf = true;
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000401 }
402
403 } // if Op2 has a LR
404
405 } // for all other defs in machine instr
406
407 } // for all operands in an instruction
408
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000409 if( !setInterf && (MInst->getNumOperands() > 2) ) {
410 cerr << "\nInterf not set for any operand in pseudo instr:\n";
411 cerr << *MInst;
412 assert(0 && "Interf not set for pseudo instr with > 2 operands" );
413
414 }
415
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000416}
417
418
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000419
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000420//----------------------------------------------------------------------------
421// This method will add interferences for incoming arguments to a method.
422//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000423void PhyRegAlloc::addInterferencesForArgs()
424{
425 // get the InSet of root BB
426 const LiveVarSet *const InSet = LVI->getInSetOfBB( Meth->front() );
427
428 // get the argument list
429 const Method::ArgumentListType& ArgList = Meth->getArgumentList();
430
431 // get an iterator to arg list
432 Method::ArgumentListType::const_iterator ArgIt = ArgList.begin();
433
434
435 for( ; ArgIt != ArgList.end() ; ++ArgIt) { // for each argument
Chris Lattner30adeb62002-02-04 16:36:59 +0000436 addInterference((Value*)*ArgIt, InSet, false); // add interferences between
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000437 // args and LVars at start
438 if( DEBUG_RA > 1) {
Chris Lattner697954c2002-01-20 22:54:45 +0000439 cerr << " - %% adding interference for argument ";
440 printValue((const Value *)*ArgIt); cerr << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000441 }
442 }
443}
444
445
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000446
447
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000448//----------------------------------------------------------------------------
449// This method is called after register allocation is complete to set the
450// allocated reisters in the machine code. This code will add register numbers
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000451// to MachineOperands that contain a Value. Also it calls target specific
452// methods to produce caller saving instructions. At the end, it adds all
453// additional instructions produced by the register allocator to the
454// instruction stream.
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000455//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000456void PhyRegAlloc::updateMachineCode()
457{
458
459 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
460
461 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
462
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000463 // get the iterator for machine instructions
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000464 //
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000465 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
466 MachineCodeForBasicBlock::iterator MInstIterator = MIVec.begin();
467
468 // iterate over all the machine instructions in BB
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000469 //
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000470 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
471
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000472 MachineInstr *MInst = *MInstIterator;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000473
474 unsigned Opcode = MInst->getOpCode();
475
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000476 // do not process Phis
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000477 if (TM.getInstrInfo().isPhi(Opcode))
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000478 continue;
479
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000480 // Now insert speical instructions (if necessary) for call/return
481 // instructions.
482 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000483 if (TM.getInstrInfo().isCall(Opcode) ||
484 TM.getInstrInfo().isReturn(Opcode)) {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000485
486 AddedInstrns *AI = AddedInstrMap[ MInst];
487 if ( !AI ) {
488 AI = new AddedInstrns();
489 AddedInstrMap[ MInst ] = AI;
490 }
491
492 // Tmp stack poistions are needed by some calls that have spilled args
493 // So reset it before we call each such method
Ruchira Sasanka6a3db8c2002-01-07 21:09:06 +0000494 //
495 mcInfo.popAllTempValues(TM);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000496
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000497 if (TM.getInstrInfo().isCall(Opcode))
498 MRI.colorCallArgs(MInst, LRI, AI, *this, *BBI);
499 else if (TM.getInstrInfo().isReturn(Opcode))
500 MRI.colorRetValue(MInst, LRI, AI);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000501 }
502
503
504 /* -- Using above code instead of this
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000505
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000506 // if this machine instr is call, insert caller saving code
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000507
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000508 if( (TM.getInstrInfo()).isCall( MInst->getOpCode()) )
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000509 MRI.insertCallerSavingCode(MInst, *BBI, *this );
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000510
511 */
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000512
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000513
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000514 // reset the stack offset for temporary variables since we may
515 // need that to spill
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000516 // mcInfo.popAllTempValues(TM);
Ruchira Sasankaf90870f2001-11-15 22:02:06 +0000517 // TODO ** : do later
Vikram S. Adve12af1642001-11-08 04:48:50 +0000518
Chris Lattner7a176752001-12-04 00:03:30 +0000519 //for(MachineInstr::val_const_op_iterator OpI(MInst);!OpI.done();++OpI) {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000520
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000521
522 // Now replace set the registers for operands in the machine instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000523 //
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000524 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
525
526 MachineOperand& Op = MInst->getOperand(OpNum);
527
528 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
529 Op.getOperandType() == MachineOperand::MO_CCRegister) {
530
531 const Value *const Val = Op.getVRegValue();
532
533 // delete this condition checking later (must assert if Val is null)
Chris Lattner045e7c82001-09-19 16:26:23 +0000534 if( !Val) {
535 if (DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000536 cerr << "Warning: NULL Value found for operand\n";
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000537 continue;
538 }
539 assert( Val && "Value is NULL");
540
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000541 LiveRange *const LR = LRI.getLiveRangeForValue(Val);
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000542
543 if ( !LR ) {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000544
545 // nothing to worry if it's a const or a label
546
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000547 if (DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000548 cerr << "*NO LR for operand : " << Op ;
549 cerr << " [reg:" << Op.getAllocatedRegNum() << "]";
550 cerr << " in inst:\t" << *MInst << "\n";
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000551 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000552
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000553 // if register is not allocated, mark register as invalid
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000554 if( Op.getAllocatedRegNum() == -1)
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000555 Op.setRegForValue( MRI.getInvalidRegNum());
Ruchira Sasankae727f852001-09-18 22:43:57 +0000556
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000557
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000558 continue;
559 }
560
561 unsigned RCID = (LR->getRegClass())->getID();
562
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000563 if( LR->hasColor() ) {
564 Op.setRegForValue( MRI.getUnifiedRegNum(RCID, LR->getColor()) );
565 }
566 else {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000567
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000568 // LR did NOT receive a color (register). Now, insert spill code
569 // for spilled opeands in this machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000570
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000571 //assert(0 && "LR must be spilled");
572 insertCode4SpilledLR(LR, MInst, *BBI, OpNum );
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000573
574 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000575 }
576
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000577 } // for each operand
578
579
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000580 // Now add instructions that the register allocator inserts before/after
581 // this machine instructions (done only for calls/rets/incoming args)
582 // We do this here, to ensure that spill for an instruction is inserted
583 // closest as possible to an instruction (see above insertCode4Spill...)
584 //
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000585 // If there are instructions to be added, *before* this machine
586 // instruction, add them now.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000587 //
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000588 if( AddedInstrMap[ MInst ] ) {
Chris Lattner697954c2002-01-20 22:54:45 +0000589 std::deque<MachineInstr *> &IBef = AddedInstrMap[MInst]->InstrnsBefore;
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000590
591 if( ! IBef.empty() ) {
Chris Lattner697954c2002-01-20 22:54:45 +0000592 std::deque<MachineInstr *>::iterator AdIt;
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000593
594 for( AdIt = IBef.begin(); AdIt != IBef.end() ; ++AdIt ) {
595
596 if( DEBUG_RA) {
597 cerr << "For inst " << *MInst;
Chris Lattner697954c2002-01-20 22:54:45 +0000598 cerr << " PREPENDed instr: " << **AdIt << "\n";
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000599 }
600
601 MInstIterator = MIVec.insert( MInstIterator, *AdIt );
602 ++MInstIterator;
603 }
604
605 }
606
607 }
608
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000609 // If there are instructions to be added *after* this machine
610 // instruction, add them now
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000611 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000612 if(AddedInstrMap[MInst] &&
613 !AddedInstrMap[MInst]->InstrnsAfter.empty() ) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000614
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000615 // if there are delay slots for this instruction, the instructions
616 // added after it must really go after the delayed instruction(s)
617 // So, we move the InstrAfter of the current instruction to the
618 // corresponding delayed instruction
619
620 unsigned delay;
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000621 if ((delay=TM.getInstrInfo().getNumDelaySlots(MInst->getOpCode())) >0){
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000622 move2DelayedInstr(MInst, *(MInstIterator+delay) );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000623
Chris Lattner697954c2002-01-20 22:54:45 +0000624 if(DEBUG_RA) cerr<< "\nMoved an added instr after the delay slot";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000625 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000626
627 else {
628
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000629
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000630 // Here we can add the "instructions after" to the current
631 // instruction since there are no delay slots for this instruction
632
Chris Lattner697954c2002-01-20 22:54:45 +0000633 std::deque<MachineInstr *> &IAft = AddedInstrMap[MInst]->InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000634
635 if( ! IAft.empty() ) {
636
Chris Lattner697954c2002-01-20 22:54:45 +0000637 std::deque<MachineInstr *>::iterator AdIt;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000638
639 ++MInstIterator; // advance to the next instruction
640
641 for( AdIt = IAft.begin(); AdIt != IAft.end() ; ++AdIt ) {
642
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000643 if(DEBUG_RA) {
644 cerr << "For inst " << *MInst;
Chris Lattner697954c2002-01-20 22:54:45 +0000645 cerr << " APPENDed instr: " << **AdIt << "\n";
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000646 }
647
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000648 MInstIterator = MIVec.insert( MInstIterator, *AdIt );
649 ++MInstIterator;
650 }
651
652 // MInsterator already points to the next instr. Since the
653 // for loop also increments it, decrement it to point to the
654 // instruction added last
655 --MInstIterator;
656
657 }
658
659 } // if not delay
660
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000661 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000662
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000663 } // for each machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000664 }
665}
666
667
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000668
669//----------------------------------------------------------------------------
670// This method inserts spill code for AN operand whose LR was spilled.
671// This method may be called several times for a single machine instruction
672// if it contains many spilled operands. Each time it is called, it finds
673// a register which is not live at that instruction and also which is not
674// used by other spilled operands of the same instruction. Then it uses
675// this register temporarily to accomodate the spilled value.
676//----------------------------------------------------------------------------
677void PhyRegAlloc::insertCode4SpilledLR(const LiveRange *LR,
678 MachineInstr *MInst,
679 const BasicBlock *BB,
680 const unsigned OpNum) {
681
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000682 assert(! TM.getInstrInfo().isCall(MInst->getOpCode()) &&
683 (! TM.getInstrInfo().isReturn(MInst->getOpCode())) &&
684 "Arg of a call/ret must be handled elsewhere");
685
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000686 MachineOperand& Op = MInst->getOperand(OpNum);
687 bool isDef = MInst->operandIsDefined(OpNum);
688 unsigned RegType = MRI.getRegType( LR );
689 int SpillOff = LR->getSpillOffFromFP();
690 RegClass *RC = LR->getRegClass();
691 const LiveVarSet *LVSetBef = LVI->getLiveVarSetBeforeMInst(MInst, BB);
Vikram S. Adve00521d72001-11-12 23:26:35 +0000692
Chris Lattner697954c2002-01-20 22:54:45 +0000693 mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000694
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000695 MachineInstr *MIBef=NULL, *AdIMid=NULL, *MIAft=NULL;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000696
697 int TmpRegU = getUsableUniRegAtMI(RC, RegType, MInst,LVSetBef, MIBef, MIAft);
698
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000699 // get the added instructions for this instruciton
700 AddedInstrns *AI = AddedInstrMap[ MInst ];
701 if ( !AI ) {
702 AI = new AddedInstrns();
703 AddedInstrMap[ MInst ] = AI;
704 }
705
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000706
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000707 if( !isDef ) {
708
709 // for a USE, we have to load the value of LR from stack to a TmpReg
710 // and use the TmpReg as one operand of instruction
711
712 // actual loading instruction
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000713 AdIMid = MRI.cpMem2RegMI(MRI.getFramePointer(), SpillOff, TmpRegU,RegType);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000714
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000715 if(MIBef)
716 AI->InstrnsBefore.push_back(MIBef);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000717
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000718 AI->InstrnsBefore.push_back(AdIMid);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000719
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000720 if(MIAft)
721 AI->InstrnsAfter.push_front(MIAft);
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000722
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000723
724 }
725 else { // if this is a Def
726
727 // for a DEF, we have to store the value produced by this instruction
728 // on the stack position allocated for this LR
729
730 // actual storing instruction
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000731 AdIMid = MRI.cpReg2MemMI(TmpRegU, MRI.getFramePointer(), SpillOff,RegType);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000732
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000733 if (MIBef)
734 AI->InstrnsBefore.push_back(MIBef);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000735
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000736 AI->InstrnsAfter.push_front(AdIMid);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000737
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000738 if (MIAft)
739 AI->InstrnsAfter.push_front(MIAft);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000740
741 } // if !DEF
742
743 cerr << "\nFor Inst " << *MInst;
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000744 cerr << " - SPILLED LR: "; LR->printSet();
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000745 cerr << "\n - Added Instructions:";
746 if( MIBef ) cerr << *MIBef;
747 cerr << *AdIMid;
748 if( MIAft ) cerr << *MIAft;
749
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000750 Op.setRegForValue( TmpRegU ); // set the opearnd
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000751
752
753}
754
755
756
757
758
759
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000760//----------------------------------------------------------------------------
761// We can use the following method to get a temporary register to be used
762// BEFORE any given machine instruction. If there is a register available,
763// this method will simply return that register and set MIBef = MIAft = NULL.
764// Otherwise, it will return a register and MIAft and MIBef will contain
765// two instructions used to free up this returned register.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000766// Returned register number is the UNIFIED register number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000767//----------------------------------------------------------------------------
768
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000769int PhyRegAlloc::getUsableUniRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000770 const int RegType,
771 const MachineInstr *MInst,
772 const LiveVarSet *LVSetBef,
773 MachineInstr *MIBef,
774 MachineInstr *MIAft) {
775
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000776 int RegU = getUnusedUniRegAtMI(RC, MInst, LVSetBef);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000777
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000778
779 if( RegU != -1) {
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000780 // we found an unused register, so we can simply use it
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000781 MIBef = MIAft = NULL;
782 }
783 else {
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000784 // we couldn't find an unused register. Generate code to free up a reg by
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000785 // saving it on stack and restoring after the instruction
786
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000787 int TmpOff = mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
Vikram S. Adve12af1642001-11-08 04:48:50 +0000788
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000789 RegU = getUniRegNotUsedByThisInst(RC, MInst);
790 MIBef = MRI.cpReg2MemMI(RegU, MRI.getFramePointer(), TmpOff, RegType );
791 MIAft = MRI.cpMem2RegMI(MRI.getFramePointer(), TmpOff, RegU, RegType );
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000792 }
793
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000794 return RegU;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000795}
796
797//----------------------------------------------------------------------------
798// This method is called to get a new unused register that can be used to
799// accomodate a spilled value.
800// This method may be called several times for a single machine instruction
801// if it contains many spilled operands. Each time it is called, it finds
802// a register which is not live at that instruction and also which is not
803// used by other spilled operands of the same instruction.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000804// Return register number is relative to the register class. NOT
805// unified number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000806//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000807int PhyRegAlloc::getUnusedUniRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000808 const MachineInstr *MInst,
809 const LiveVarSet *LVSetBef) {
810
811 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
812
813 bool *IsColorUsedArr = RC->getIsColorUsedArr();
814
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000815 for(unsigned i=0; i < NumAvailRegs; i++) // Reset array
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000816 IsColorUsedArr[i] = false;
817
818 LiveVarSet::const_iterator LIt = LVSetBef->begin();
819
820 // for each live var in live variable set after machine inst
821 for( ; LIt != LVSetBef->end(); ++LIt) {
822
823 // get the live range corresponding to live var
824 LiveRange *const LRofLV = LRI.getLiveRangeForValue(*LIt );
825
826 // LR can be null if it is a const since a const
827 // doesn't have a dominating def - see Assumptions above
828 if( LRofLV )
829 if( LRofLV->hasColor() )
830 IsColorUsedArr[ LRofLV->getColor() ] = true;
831 }
832
833 // It is possible that one operand of this MInst was already spilled
834 // and it received some register temporarily. If that's the case,
835 // it is recorded in machine operand. We must skip such registers.
836
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000837 setRelRegsUsedByThisInst(RC, MInst);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000838
839 unsigned c; // find first unused color
840 for( c=0; c < NumAvailRegs; c++)
841 if( ! IsColorUsedArr[ c ] ) break;
842
843 if(c < NumAvailRegs)
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000844 return MRI.getUnifiedRegNum(RC->getID(), c);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000845 else
846 return -1;
847
848
849}
850
851
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000852//----------------------------------------------------------------------------
853// Get any other register in a register class, other than what is used
854// by operands of a machine instruction. Returns the unified reg number.
855//----------------------------------------------------------------------------
856int PhyRegAlloc::getUniRegNotUsedByThisInst(RegClass *RC,
857 const MachineInstr *MInst) {
858
859 bool *IsColorUsedArr = RC->getIsColorUsedArr();
860 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
861
862
863 for(unsigned i=0; i < NumAvailRegs ; i++) // Reset array
864 IsColorUsedArr[i] = false;
865
866 setRelRegsUsedByThisInst(RC, MInst);
867
868 unsigned c; // find first unused color
869 for( c=0; c < RC->getNumOfAvailRegs(); c++)
870 if( ! IsColorUsedArr[ c ] ) break;
871
872 if(c < NumAvailRegs)
873 return MRI.getUnifiedRegNum(RC->getID(), c);
874 else
875 assert( 0 && "FATAL: No free register could be found in reg class!!");
Chris Lattner697954c2002-01-20 22:54:45 +0000876 return 0;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000877}
878
879
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000880//----------------------------------------------------------------------------
881// This method modifies the IsColorUsedArr of the register class passed to it.
882// It sets the bits corresponding to the registers used by this machine
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000883// instructions. Both explicit and implicit operands are set.
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000884//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000885void PhyRegAlloc::setRelRegsUsedByThisInst(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000886 const MachineInstr *MInst ) {
887
888 bool *IsColorUsedArr = RC->getIsColorUsedArr();
889
890 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
891
892 const MachineOperand& Op = MInst->getOperand(OpNum);
893
894 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000895 Op.getOperandType() == MachineOperand::MO_CCRegister ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000896
897 const Value *const Val = Op.getVRegValue();
898
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000899 if( Val )
900 if( MRI.getRegClassIDOfValue(Val) == RC->getID() ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000901 int Reg;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000902 if( (Reg=Op.getAllocatedRegNum()) != -1) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000903 IsColorUsedArr[ Reg ] = true;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000904 }
905 else {
906 // it is possilbe that this operand still is not marked with
907 // a register but it has a LR and that received a color
908
909 LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
910 if( LROfVal)
911 if( LROfVal->hasColor() )
912 IsColorUsedArr[ LROfVal->getColor() ] = true;
913 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000914
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000915 } // if reg classes are the same
916 }
917 else if (Op.getOperandType() == MachineOperand::MO_MachineRegister) {
918 IsColorUsedArr[ Op.getMachineRegNum() ] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000919 }
920 }
921
922 // If there are implicit references, mark them as well
923
924 for(unsigned z=0; z < MInst->getNumImplicitRefs(); z++) {
925
926 LiveRange *const LRofImpRef =
927 LRI.getLiveRangeForValue( MInst->getImplicitRef(z) );
Chris Lattner697954c2002-01-20 22:54:45 +0000928
929 if(LRofImpRef && LRofImpRef->hasColor())
930 IsColorUsedArr[LRofImpRef->getColor()] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000931 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000932}
933
934
935
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000936
937
938
939
940
941//----------------------------------------------------------------------------
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000942// If there are delay slots for an instruction, the instructions
943// added after it must really go after the delayed instruction(s).
944// So, we move the InstrAfter of that instruction to the
945// corresponding delayed instruction using the following method.
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000946
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000947//----------------------------------------------------------------------------
948void PhyRegAlloc:: move2DelayedInstr(const MachineInstr *OrigMI,
949 const MachineInstr *DelayedMI) {
950
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000951 // "added after" instructions of the original instr
Chris Lattner697954c2002-01-20 22:54:45 +0000952 std::deque<MachineInstr *> &OrigAft = AddedInstrMap[OrigMI]->InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000953
954 // "added instructions" of the delayed instr
955 AddedInstrns *DelayAdI = AddedInstrMap[DelayedMI];
956
957 if(! DelayAdI ) { // create a new "added after" if necessary
958 DelayAdI = new AddedInstrns();
959 AddedInstrMap[DelayedMI] = DelayAdI;
960 }
961
962 // "added after" instructions of the delayed instr
Chris Lattner697954c2002-01-20 22:54:45 +0000963 std::deque<MachineInstr *> &DelayedAft = DelayAdI->InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000964
965 // go thru all the "added after instructions" of the original instruction
966 // and append them to the "addded after instructions" of the delayed
967 // instructions
Chris Lattner697954c2002-01-20 22:54:45 +0000968 DelayedAft.insert(DelayedAft.end(), OrigAft.begin(), OrigAft.end());
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000969
970 // empty the "added after instructions" of the original instruction
971 OrigAft.clear();
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000972}
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000973
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000974//----------------------------------------------------------------------------
975// This method prints the code with registers after register allocation is
976// complete.
977//----------------------------------------------------------------------------
978void PhyRegAlloc::printMachineCode()
979{
980
Chris Lattner697954c2002-01-20 22:54:45 +0000981 cerr << "\n;************** Method " << Meth->getName()
982 << " *****************\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000983
984 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
985
986 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
987
Chris Lattner697954c2002-01-20 22:54:45 +0000988 cerr << "\n"; printLabel( *BBI); cerr << ": ";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000989
990 // get the iterator for machine instructions
991 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
992 MachineCodeForBasicBlock::iterator MInstIterator = MIVec.begin();
993
994 // iterate over all the machine instructions in BB
995 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
996
997 MachineInstr *const MInst = *MInstIterator;
998
999
Chris Lattner697954c2002-01-20 22:54:45 +00001000 cerr << "\n\t";
1001 cerr << TargetInstrDescriptors[MInst->getOpCode()].opCodeString;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001002
1003
Chris Lattner7a176752001-12-04 00:03:30 +00001004 //for(MachineInstr::val_const_op_iterator OpI(MInst);!OpI.done();++OpI) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001005
1006 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
1007
1008 MachineOperand& Op = MInst->getOperand(OpNum);
1009
1010 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasanka97b8b442001-10-18 22:36:26 +00001011 Op.getOperandType() == MachineOperand::MO_CCRegister /*||
1012 Op.getOperandType() == MachineOperand::MO_PCRelativeDisp*/ ) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001013
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001014 const Value *const Val = Op.getVRegValue () ;
Ruchira Sasankae727f852001-09-18 22:43:57 +00001015 // ****this code is temporary till NULL Values are fixed
1016 if( ! Val ) {
Chris Lattner697954c2002-01-20 22:54:45 +00001017 cerr << "\t<*NULL*>";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001018 continue;
1019 }
Ruchira Sasankae727f852001-09-18 22:43:57 +00001020
1021 // if a label or a constant
Chris Lattnerdbe53042002-01-21 01:33:12 +00001022 if(isa<BasicBlock>(Val)) {
Chris Lattner697954c2002-01-20 22:54:45 +00001023 cerr << "\t"; printLabel( Op.getVRegValue () );
1024 } else {
Ruchira Sasankae727f852001-09-18 22:43:57 +00001025 // else it must be a register value
1026 const int RegNum = Op.getAllocatedRegNum();
1027
Chris Lattner697954c2002-01-20 22:54:45 +00001028 cerr << "\t" << "%" << MRI.getUnifiedRegName( RegNum );
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001029 if (Val->hasName() )
Chris Lattner697954c2002-01-20 22:54:45 +00001030 cerr << "(" << Val->getName() << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001031 else
Chris Lattner697954c2002-01-20 22:54:45 +00001032 cerr << "(" << Val << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001033
1034 if( Op.opIsDef() )
Chris Lattner697954c2002-01-20 22:54:45 +00001035 cerr << "*";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001036
1037 const LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
1038 if( LROfVal )
1039 if( LROfVal->hasSpillOffset() )
Chris Lattner697954c2002-01-20 22:54:45 +00001040 cerr << "$";
Ruchira Sasankae727f852001-09-18 22:43:57 +00001041 }
1042
1043 }
1044 else if(Op.getOperandType() == MachineOperand::MO_MachineRegister) {
Chris Lattner697954c2002-01-20 22:54:45 +00001045 cerr << "\t" << "%" << MRI.getUnifiedRegName(Op.getMachineRegNum());
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001046 }
1047
1048 else
Chris Lattner697954c2002-01-20 22:54:45 +00001049 cerr << "\t" << Op; // use dump field
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001050 }
1051
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001052
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001053
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001054 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
1055 if( NumOfImpRefs > 0 ) {
1056
Chris Lattner697954c2002-01-20 22:54:45 +00001057 cerr << "\tImplicit:";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001058
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001059 for(unsigned z=0; z < NumOfImpRefs; z++) {
1060 printValue( MInst->getImplicitRef(z) );
Chris Lattner697954c2002-01-20 22:54:45 +00001061 cerr << "\t";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001062 }
1063
1064 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001065
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001066 } // for all machine instructions
1067
Chris Lattner697954c2002-01-20 22:54:45 +00001068 cerr << "\n";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001069
1070 } // for all BBs
1071
Chris Lattner697954c2002-01-20 22:54:45 +00001072 cerr << "\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001073}
1074
Ruchira Sasankae727f852001-09-18 22:43:57 +00001075
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001076#if 0
1077
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001078//----------------------------------------------------------------------------
1079//
1080//----------------------------------------------------------------------------
1081
1082void PhyRegAlloc::colorCallRetArgs()
1083{
1084
1085 CallRetInstrListType &CallRetInstList = LRI.getCallRetInstrList();
1086 CallRetInstrListType::const_iterator It = CallRetInstList.begin();
1087
1088 for( ; It != CallRetInstList.end(); ++It ) {
1089
Ruchira Sasankaa90e7702001-10-15 16:26:38 +00001090 const MachineInstr *const CRMI = *It;
1091 unsigned OpCode = CRMI->getOpCode();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001092
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001093 // get the added instructions for this Call/Ret instruciton
1094 AddedInstrns *AI = AddedInstrMap[ CRMI ];
1095 if ( !AI ) {
1096 AI = new AddedInstrns();
1097 AddedInstrMap[ CRMI ] = AI;
1098 }
1099
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001100 // Tmp stack poistions are needed by some calls that have spilled args
1101 // So reset it before we call each such method
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001102 //mcInfo.popAllTempValues(TM);
1103
1104
Vikram S. Adve12af1642001-11-08 04:48:50 +00001105
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001106 if (TM.getInstrInfo().isCall(OpCode))
1107 MRI.colorCallArgs(CRMI, LRI, AI, *this);
1108 else if (TM.getInstrInfo().isReturn(OpCode))
Ruchira Sasankaa90e7702001-10-15 16:26:38 +00001109 MRI.colorRetValue( CRMI, LRI, AI );
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001110 else
1111 assert(0 && "Non Call/Ret instrn in CallRetInstrList\n");
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001112 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001113}
1114
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001115#endif
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001116
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001117//----------------------------------------------------------------------------
1118
1119//----------------------------------------------------------------------------
1120void PhyRegAlloc::colorIncomingArgs()
1121{
1122 const BasicBlock *const FirstBB = Meth->front();
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001123 const MachineInstr *FirstMI = FirstBB->getMachineInstrVec().front();
1124 assert(FirstMI && "No machine instruction in entry BB");
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001125
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001126 AddedInstrns *AI = AddedInstrMap[FirstMI];
1127 if (!AI)
1128 AddedInstrMap[FirstMI] = AI = new AddedInstrns();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001129
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001130 MRI.colorMethodArgs(Meth, LRI, AI);
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001131}
1132
Ruchira Sasankae727f852001-09-18 22:43:57 +00001133
1134//----------------------------------------------------------------------------
1135// Used to generate a label for a basic block
1136//----------------------------------------------------------------------------
Chris Lattner697954c2002-01-20 22:54:45 +00001137void PhyRegAlloc::printLabel(const Value *const Val) {
1138 if (Val->hasName())
1139 cerr << Val->getName();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001140 else
Chris Lattner697954c2002-01-20 22:54:45 +00001141 cerr << "Label" << Val;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001142}
1143
1144
Ruchira Sasankae727f852001-09-18 22:43:57 +00001145//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001146// This method calls setSugColorUsable method of each live range. This
1147// will determine whether the suggested color of LR is really usable.
1148// A suggested color is not usable when the suggested color is volatile
1149// AND when there are call interferences
1150//----------------------------------------------------------------------------
1151
1152void PhyRegAlloc::markUnusableSugColors()
1153{
Chris Lattner697954c2002-01-20 22:54:45 +00001154 if(DEBUG_RA ) cerr << "\nmarking unusable suggested colors ...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001155
1156 // hash map iterator
1157 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
1158 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
1159
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001160 for(; HMI != HMIEnd ; ++HMI ) {
1161 if (HMI->first) {
1162 LiveRange *L = HMI->second; // get the LiveRange
1163 if (L) {
1164 if(L->hasSuggestedColor()) {
1165 int RCID = L->getRegClass()->getID();
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001166 if( MRI.isRegVolatile( RCID, L->getSuggestedColor()) &&
1167 L->isCallInterference() )
1168 L->setSuggestedColorUsable( false );
1169 else
1170 L->setSuggestedColorUsable( true );
1171 }
1172 } // if L->hasSuggestedColor()
1173 }
1174 } // for all LR's in hash map
1175}
1176
1177
1178
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001179//----------------------------------------------------------------------------
1180// The following method will set the stack offsets of the live ranges that
1181// are decided to be spillled. This must be called just after coloring the
1182// LRs using the graph coloring algo. For each live range that is spilled,
1183// this method allocate a new spill position on the stack.
1184//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001185
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001186void PhyRegAlloc::allocateStackSpace4SpilledLRs()
1187{
Chris Lattner697954c2002-01-20 22:54:45 +00001188 if(DEBUG_RA ) cerr << "\nsetting LR stack offsets ...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001189
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001190 // hash map iterator
1191 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
1192 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
1193
1194 for( ; HMI != HMIEnd ; ++HMI ) {
Chris Lattner697954c2002-01-20 22:54:45 +00001195 if(HMI->first && HMI->second) {
1196 LiveRange *L = HMI->second; // get the LiveRange
1197 if( ! L->hasColor() )
1198 // NOTE: ** allocating the size of long Type **
1199 L->setSpillOffFromFP(mcInfo.allocateSpilledValue(TM, Type::LongTy));
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001200 }
1201 } // for all LR's in hash map
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001202}
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001203
1204
1205
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001206//----------------------------------------------------------------------------
Ruchira Sasankae727f852001-09-18 22:43:57 +00001207// The entry pont to Register Allocation
1208//----------------------------------------------------------------------------
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001209
1210void PhyRegAlloc::allocateRegisters()
1211{
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001212
1213 // make sure that we put all register classes into the RegClassList
1214 // before we call constructLiveRanges (now done in the constructor of
1215 // PhyRegAlloc class).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001216 //
1217 LRI.constructLiveRanges(); // create LR info
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001218
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001219 if (DEBUG_RA)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001220 LRI.printLiveRanges();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001221
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001222 createIGNodeListsAndIGs(); // create IGNode list and IGs
1223
1224 buildInterferenceGraphs(); // build IGs in all reg classes
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001225
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001226
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001227 if (DEBUG_RA) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001228 // print all LRs in all reg classes
1229 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1230 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001231
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001232 // print IGs in all register classes
1233 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1234 RegClassList[ rc ]->printIG();
1235 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001236
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001237
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001238 LRI.coalesceLRs(); // coalesce all live ranges
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001239
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +00001240
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001241 if( DEBUG_RA) {
1242 // print all LRs in all reg classes
1243 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1244 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001245
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001246 // print IGs in all register classes
1247 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1248 RegClassList[ rc ]->printIG();
1249 }
1250
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001251
1252 // mark un-usable suggested color before graph coloring algorithm.
1253 // When this is done, the graph coloring algo will not reserve
1254 // suggested color unnecessarily - they can be used by another LR
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001255 //
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001256 markUnusableSugColors();
1257
1258 // color all register classes using the graph coloring algo
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001259 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1260 RegClassList[ rc ]->colorAllRegs();
1261
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001262 // Atter grpah coloring, if some LRs did not receive a color (i.e, spilled)
1263 // a poistion for such spilled LRs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001264 //
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001265 allocateStackSpace4SpilledLRs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001266
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001267 mcInfo.popAllTempValues(TM); // TODO **Check
1268
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001269 // color incoming args - if the correct color was not received
1270 // insert code to copy to the correct register
1271 //
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001272 colorIncomingArgs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001273
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001274 // Now update the machine code with register names and add any
1275 // additional code inserted by the register allocator to the instruction
1276 // stream
1277 //
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001278 updateMachineCode();
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001279
Chris Lattner045e7c82001-09-19 16:26:23 +00001280 if (DEBUG_RA) {
Vikram S. Adve12af1642001-11-08 04:48:50 +00001281 MachineCodeForMethod::get(Meth).dump();
Chris Lattner045e7c82001-09-19 16:26:23 +00001282 printMachineCode(); // only for DEBUGGING
1283 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001284}
1285
Ruchira Sasankae727f852001-09-18 22:43:57 +00001286
1287