blob: d901337535d29f043679f555603471f9be867424 [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 Lattner49960842002-02-05 00:35:14 +000018#include "llvm/Analysis/LiveVar/LiveVarSet.h"
Chris Lattner14ab1ce2002-02-04 17:48:00 +000019#include "llvm/Analysis/LoopInfo.h"
Vikram S. Adve12af1642001-11-08 04:48:50 +000020#include "llvm/Target/TargetMachine.h"
21#include "llvm/Target/MachineFrameInfo.h"
Chris Lattner30adeb62002-02-04 16:36:59 +000022#include "llvm/Method.h"
Chris Lattner697954c2002-01-20 22:54:45 +000023#include <iostream>
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000024#include <math.h>
Chris Lattner697954c2002-01-20 22:54:45 +000025using std::cerr;
Vikram S. Adve12af1642001-11-08 04:48:50 +000026
27
28// ***TODO: There are several places we add instructions. Validate the order
29// of adding these instructions.
Ruchira Sasanka174bded2001-10-28 18:12:02 +000030
Chris Lattner045e7c82001-09-19 16:26:23 +000031cl::Enum<RegAllocDebugLevel_t> DEBUG_RA("dregalloc", cl::NoFlags,
32 "enable register allocation debugging information",
33 clEnumValN(RA_DEBUG_None , "n", "disable debug output"),
34 clEnumValN(RA_DEBUG_Normal , "y", "enable debug output"),
35 clEnumValN(RA_DEBUG_Verbose, "v", "enable extra debug output"), 0);
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000036
37
Chris Lattner2f9b28e2002-02-04 15:54:09 +000038//----------------------------------------------------------------------------
39// RegisterAllocation pass front end...
40//----------------------------------------------------------------------------
41namespace {
42 class RegisterAllocator : public MethodPass {
43 TargetMachine &Target;
44 public:
45 inline RegisterAllocator(TargetMachine &T) : Target(T) {}
Chris Lattner6dd98a62002-02-04 00:33:08 +000046
Chris Lattner2f9b28e2002-02-04 15:54:09 +000047 bool runOnMethod(Method *M) {
48 if (DEBUG_RA)
49 cerr << "\n******************** Method "<< M->getName()
50 << " ********************\n";
51
Chris Lattner4d7fc112002-02-04 20:02:38 +000052 PhyRegAlloc PRA(M, Target, &getAnalysis<MethodLiveVarInfo>(),
Chris Lattner14ab1ce2002-02-04 17:48:00 +000053 &getAnalysis<cfg::LoopInfo>());
Chris Lattner2f9b28e2002-02-04 15:54:09 +000054 PRA.allocateRegisters();
55
56 if (DEBUG_RA) cerr << "\nRegister allocation complete!\n";
57 return false;
58 }
Chris Lattner4911c352002-02-04 17:39:42 +000059
60 virtual void getAnalysisUsageInfo(Pass::AnalysisSet &Requires,
61 Pass::AnalysisSet &Destroyed,
62 Pass::AnalysisSet &Provided) {
Chris Lattner14ab1ce2002-02-04 17:48:00 +000063 Requires.push_back(cfg::LoopInfo::ID);
Chris Lattner4d7fc112002-02-04 20:02:38 +000064 Requires.push_back(MethodLiveVarInfo::ID);
Chris Lattner4911c352002-02-04 17:39:42 +000065 }
Chris Lattner2f9b28e2002-02-04 15:54:09 +000066 };
Chris Lattner6dd98a62002-02-04 00:33:08 +000067}
68
Chris Lattner2f9b28e2002-02-04 15:54:09 +000069MethodPass *getRegisterAllocator(TargetMachine &T) {
70 return new RegisterAllocator(T);
71}
Chris Lattner6dd98a62002-02-04 00:33:08 +000072
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000073//----------------------------------------------------------------------------
74// Constructor: Init local composite objects and create register classes.
75//----------------------------------------------------------------------------
Vikram S. Adve12af1642001-11-08 04:48:50 +000076PhyRegAlloc::PhyRegAlloc(Method *M,
Ruchira Sasanka8e604792001-09-14 21:18:34 +000077 const TargetMachine& tm,
Chris Lattner4911c352002-02-04 17:39:42 +000078 MethodLiveVarInfo *Lvi,
Chris Lattner14ab1ce2002-02-04 17:48:00 +000079 cfg::LoopInfo *LDC)
Chris Lattner697954c2002-01-20 22:54:45 +000080 : TM(tm), Meth(M),
Vikram S. Adve12af1642001-11-08 04:48:50 +000081 mcInfo(MachineCodeForMethod::get(M)),
82 LVI(Lvi), LRI(M, tm, RegClassList),
Ruchira Sasanka8e604792001-09-14 21:18:34 +000083 MRI( tm.getRegInfo() ),
84 NumOfRegClasses(MRI.getNumOfRegClasses()),
Chris Lattner4911c352002-02-04 17:39:42 +000085 LoopDepthCalc(LDC) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +000086
87 // create each RegisterClass and put in RegClassList
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000088 //
Chris Lattner697954c2002-01-20 22:54:45 +000089 for(unsigned int rc=0; rc < NumOfRegClasses; rc++)
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000090 RegClassList.push_back( new RegClass(M, MRI.getMachineRegClass(rc),
91 &ResColList) );
Ruchira Sasanka8e604792001-09-14 21:18:34 +000092}
93
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000094
95//----------------------------------------------------------------------------
96// Destructor: Deletes register classes
97//----------------------------------------------------------------------------
98PhyRegAlloc::~PhyRegAlloc() {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000099 for( unsigned int rc=0; rc < NumOfRegClasses; rc++)
100 delete RegClassList[rc];
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000101}
102
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000103//----------------------------------------------------------------------------
104// This method initally creates interference graphs (one in each reg class)
105// and IGNodeList (one in each IG). The actual nodes will be pushed later.
106//----------------------------------------------------------------------------
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000107void PhyRegAlloc::createIGNodeListsAndIGs() {
108 if (DEBUG_RA) cerr << "Creating LR lists ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000109
110 // hash map iterator
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000111 LiveRangeMapType::const_iterator HMI = LRI.getLiveRangeMap()->begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000112
113 // hash map end
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000114 LiveRangeMapType::const_iterator HMIEnd = LRI.getLiveRangeMap()->end();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000115
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000116 for (; HMI != HMIEnd ; ++HMI ) {
117 if (HMI->first) {
118 LiveRange *L = HMI->second; // get the LiveRange
119 if (!L) {
120 if( DEBUG_RA) {
121 cerr << "\n*?!?Warning: Null liver range found for: ";
122 printValue(HMI->first); cerr << "\n";
123 }
124 continue;
125 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000126 // if the Value * is not null, and LR
127 // is not yet written to the IGNodeList
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000128 if( !(L->getUserIGNode()) ) {
129 RegClass *const RC = // RegClass of first value in the LR
130 RegClassList[ L->getRegClass()->getID() ];
131
132 RC->addLRToIG(L); // add this LR to an IG
133 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000134 }
135 }
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000136
137 // init RegClassList
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000138 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000139 RegClassList[rc]->createInterferenceGraph();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000140
141 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000142 cerr << "LRLists Created!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000143}
144
145
146
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000147
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000148//----------------------------------------------------------------------------
149// This method will add all interferences at for a given instruction.
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000150// Interence occurs only if the LR of Def (Inst or Arg) is of the same reg
151// class as that of live var. The live var passed to this function is the
152// LVset AFTER the instruction
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000153//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000154void PhyRegAlloc::addInterference(const Value *const Def,
155 const LiveVarSet *const LVSet,
156 const bool isCallInst) {
157
158 LiveVarSet::const_iterator LIt = LVSet->begin();
159
160 // get the live range of instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000161 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000162 const LiveRange *const LROfDef = LRI.getLiveRangeForValue( Def );
163
164 IGNode *const IGNodeOfDef = LROfDef->getUserIGNode();
165 assert( IGNodeOfDef );
166
167 RegClass *const RCOfDef = LROfDef->getRegClass();
168
169 // for each live var in live variable set
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000170 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000171 for( ; LIt != LVSet->end(); ++LIt) {
172
173 if( DEBUG_RA > 1) {
Chris Lattner697954c2002-01-20 22:54:45 +0000174 cerr << "< Def="; printValue(Def);
175 cerr << ", Lvar="; printValue( *LIt); cerr << "> ";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000176 }
177
178 // get the live range corresponding to live var
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000179 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000180 LiveRange *const LROfVar = LRI.getLiveRangeForValue(*LIt );
181
182 // LROfVar can be null if it is a const since a const
183 // doesn't have a dominating def - see Assumptions above
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000184 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000185 if (LROfVar) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000186 if(LROfDef == LROfVar) // do not set interf for same LR
187 continue;
188
189 // if 2 reg classes are the same set interference
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000190 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000191 if(RCOfDef == LROfVar->getRegClass()) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000192 RCOfDef->setInterference( LROfDef, LROfVar);
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000193 } else if(DEBUG_RA > 1) {
194 // we will not have LRs for values not explicitly allocated in the
195 // instruction stream (e.g., constants)
196 cerr << " warning: no live range for " ;
197 printValue(*LIt); cerr << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000198 }
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000199 }
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000200 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000201}
202
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000203
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000204
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000205//----------------------------------------------------------------------------
206// For a call instruction, this method sets the CallInterference flag in
207// the LR of each variable live int the Live Variable Set live after the
208// call instruction (except the return value of the call instruction - since
209// the return value does not interfere with that call itself).
210//----------------------------------------------------------------------------
211
212void PhyRegAlloc::setCallInterferences(const MachineInstr *MInst,
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000213 const LiveVarSet *const LVSetAft ) {
214
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000215 // Now find the LR of the return value of the call
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000216 // We do this because, we look at the LV set *after* the instruction
217 // to determine, which LRs must be saved across calls. The return value
218 // of the call is live in this set - but it does not interfere with call
219 // (i.e., we can allocate a volatile register to the return value)
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000220 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000221 LiveRange *RetValLR = NULL;
Ruchira Sasankab3b6f532001-10-21 16:43:41 +0000222 const Value *RetVal = MRI.getCallInstRetVal( MInst );
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000223
Ruchira Sasankab3b6f532001-10-21 16:43:41 +0000224 if( RetVal ) {
225 RetValLR = LRI.getLiveRangeForValue( RetVal );
226 assert( RetValLR && "No LR for RetValue of call");
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000227 }
228
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000229 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000230 cerr << "\n For call inst: " << *MInst;
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000231
232 LiveVarSet::const_iterator LIt = LVSetAft->begin();
233
234 // for each live var in live variable set after machine inst
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000235 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000236 for( ; LIt != LVSetAft->end(); ++LIt) {
237
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000238 // get the live range corresponding to live var
239 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000240 LiveRange *const LR = LRI.getLiveRangeForValue(*LIt );
241
242 if( LR && DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000243 cerr << "\n\tLR Aft Call: ";
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000244 LR->printSet();
245 }
246
247
248 // LR can be null if it is a const since a const
249 // doesn't have a dominating def - see Assumptions above
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000250 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000251 if( LR && (LR != RetValLR) ) {
252 LR->setCallInterference();
253 if( DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000254 cerr << "\n ++Added call interf for LR: " ;
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000255 LR->printSet();
256 }
257 }
258
259 }
260
261}
262
263
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000264
265
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000266//----------------------------------------------------------------------------
267// This method will walk thru code and create interferences in the IG of
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000268// each RegClass. Also, this method calculates the spill cost of each
269// Live Range (it is done in this method to save another pass over the code).
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000270//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000271void PhyRegAlloc::buildInterferenceGraphs()
272{
273
Chris Lattner697954c2002-01-20 22:54:45 +0000274 if(DEBUG_RA) cerr << "Creating interference graphs ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000275
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000276 unsigned BBLoopDepthCost;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000277 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
278
279 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
280
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000281 // find the 10^(loop_depth) of this BB
282 //
Chris Lattner4911c352002-02-04 17:39:42 +0000283 BBLoopDepthCost = (unsigned) pow( 10.0, LoopDepthCalc->getLoopDepth(*BBI));
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000284
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000285 // get the iterator for machine instructions
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000286 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000287 const MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
288 MachineCodeForBasicBlock::const_iterator
289 MInstIterator = MIVec.begin();
290
291 // iterate over all the machine instructions in BB
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000292 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000293 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000294
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +0000295 const MachineInstr * MInst = *MInstIterator;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000296
297 // get the LV set after the instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000298 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000299 const LiveVarSet *const LVSetAI =
300 LVI->getLiveVarSetAfterMInst(MInst, *BBI);
301
302 const bool isCallInst = TM.getInstrInfo().isCall(MInst->getOpCode());
303
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000304 if( isCallInst ) {
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000305 // set the isCallInterference flag of each live range wich extends
306 // accross this call instruction. This information is used by graph
307 // coloring algo to avoid allocating volatile colors to live ranges
308 // that span across calls (since they have to be saved/restored)
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000309 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000310 setCallInterferences( MInst, LVSetAI);
311 }
312
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000313
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000314 // iterate over all MI operands to find defs
315 //
Chris Lattner7a176752001-12-04 00:03:30 +0000316 for( MachineInstr::val_const_op_iterator OpI(MInst);!OpI.done(); ++OpI) {
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000317
318 if( OpI.isDef() ) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000319 // create a new LR iff this operand is a def
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000320 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000321 addInterference(*OpI, LVSetAI, isCallInst );
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000322 }
323
324 // Calculate the spill cost of each live range
325 //
326 LiveRange *LR = LRI.getLiveRangeForValue( *OpI );
327 if( LR )
328 LR->addSpillCost(BBLoopDepthCost);
329 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000330
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000331
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000332 // if there are multiple defs in this instruction e.g. in SETX
333 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000334 if (TM.getInstrInfo().isPseudoInstr(MInst->getOpCode()))
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000335 addInterf4PseudoInstr(MInst);
336
337
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000338 // Also add interference for any implicit definitions in a machine
339 // instr (currently, only calls have this).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000340 //
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000341 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
342 if( NumOfImpRefs > 0 ) {
343 for(unsigned z=0; z < NumOfImpRefs; z++)
344 if( MInst->implicitRefIsDefined(z) )
345 addInterference( MInst->getImplicitRef(z), LVSetAI, isCallInst );
346 }
347
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +0000348
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000349 } // for all machine instructions in BB
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000350
351 } // for all BBs in method
352
353
354 // add interferences for method arguments. Since there are no explict
355 // defs in method for args, we have to add them manually
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000356 //
357 addInterferencesForArgs();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000358
359 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000360 cerr << "Interference graphs calculted!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000361
362}
363
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000364
365
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000366//--------------------------------------------------------------------------
367// Pseudo instructions will be exapnded to multiple instructions by the
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000368// assembler. Consequently, all the opernds must get distinct registers.
369// Therefore, we mark all operands of a pseudo instruction as they interfere
370// with one another.
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000371//--------------------------------------------------------------------------
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000372void PhyRegAlloc::addInterf4PseudoInstr(const MachineInstr *MInst) {
373
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000374 bool setInterf = false;
375
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000376 // iterate over MI operands to find defs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000377 //
Chris Lattner7a176752001-12-04 00:03:30 +0000378 for( MachineInstr::val_const_op_iterator It1(MInst);!It1.done(); ++It1) {
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000379
380 const LiveRange *const LROfOp1 = LRI.getLiveRangeForValue( *It1 );
381
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000382 if( !LROfOp1 && It1.isDef() )
383 assert( 0 && "No LR for Def in PSEUDO insruction");
384
Chris Lattner7a176752001-12-04 00:03:30 +0000385 MachineInstr::val_const_op_iterator It2 = It1;
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000386 ++It2;
387
388 for( ; !It2.done(); ++It2) {
389
390 const LiveRange *const LROfOp2 = LRI.getLiveRangeForValue( *It2 );
391
392 if( LROfOp2) {
393
394 RegClass *const RCOfOp1 = LROfOp1->getRegClass();
395 RegClass *const RCOfOp2 = LROfOp2->getRegClass();
396
397 if( RCOfOp1 == RCOfOp2 ){
398 RCOfOp1->setInterference( LROfOp1, LROfOp2 );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000399 setInterf = true;
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000400 }
401
402 } // if Op2 has a LR
403
404 } // for all other defs in machine instr
405
406 } // for all operands in an instruction
407
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000408 if( !setInterf && (MInst->getNumOperands() > 2) ) {
409 cerr << "\nInterf not set for any operand in pseudo instr:\n";
410 cerr << *MInst;
411 assert(0 && "Interf not set for pseudo instr with > 2 operands" );
412
413 }
414
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000415}
416
417
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000418
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000419//----------------------------------------------------------------------------
420// This method will add interferences for incoming arguments to a method.
421//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000422void PhyRegAlloc::addInterferencesForArgs()
423{
424 // get the InSet of root BB
425 const LiveVarSet *const InSet = LVI->getInSetOfBB( Meth->front() );
426
427 // get the argument list
428 const Method::ArgumentListType& ArgList = Meth->getArgumentList();
429
430 // get an iterator to arg list
431 Method::ArgumentListType::const_iterator ArgIt = ArgList.begin();
432
433
434 for( ; ArgIt != ArgList.end() ; ++ArgIt) { // for each argument
Chris Lattner30adeb62002-02-04 16:36:59 +0000435 addInterference((Value*)*ArgIt, InSet, false); // add interferences between
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000436 // args and LVars at start
437 if( DEBUG_RA > 1) {
Chris Lattner697954c2002-01-20 22:54:45 +0000438 cerr << " - %% adding interference for argument ";
439 printValue((const Value *)*ArgIt); cerr << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000440 }
441 }
442}
443
444
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000445
446
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000447//----------------------------------------------------------------------------
448// This method is called after register allocation is complete to set the
449// allocated reisters in the machine code. This code will add register numbers
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000450// to MachineOperands that contain a Value. Also it calls target specific
451// methods to produce caller saving instructions. At the end, it adds all
452// additional instructions produced by the register allocator to the
453// instruction stream.
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000454//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000455void PhyRegAlloc::updateMachineCode()
456{
457
458 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
459
460 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
461
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000462 // get the iterator for machine instructions
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000463 //
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000464 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
465 MachineCodeForBasicBlock::iterator MInstIterator = MIVec.begin();
466
467 // iterate over all the machine instructions in BB
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000468 //
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000469 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
470
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000471 MachineInstr *MInst = *MInstIterator;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000472
473 unsigned Opcode = MInst->getOpCode();
474
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000475 // do not process Phis
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000476 if (TM.getInstrInfo().isPhi(Opcode))
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000477 continue;
478
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000479 // Now insert speical instructions (if necessary) for call/return
480 // instructions.
481 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000482 if (TM.getInstrInfo().isCall(Opcode) ||
483 TM.getInstrInfo().isReturn(Opcode)) {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000484
485 AddedInstrns *AI = AddedInstrMap[ MInst];
486 if ( !AI ) {
487 AI = new AddedInstrns();
488 AddedInstrMap[ MInst ] = AI;
489 }
490
491 // Tmp stack poistions are needed by some calls that have spilled args
492 // So reset it before we call each such method
Ruchira Sasanka6a3db8c2002-01-07 21:09:06 +0000493 //
494 mcInfo.popAllTempValues(TM);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000495
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000496 if (TM.getInstrInfo().isCall(Opcode))
497 MRI.colorCallArgs(MInst, LRI, AI, *this, *BBI);
498 else if (TM.getInstrInfo().isReturn(Opcode))
499 MRI.colorRetValue(MInst, LRI, AI);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000500 }
501
502
503 /* -- Using above code instead of this
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000504
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000505 // if this machine instr is call, insert caller saving code
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000506
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000507 if( (TM.getInstrInfo()).isCall( MInst->getOpCode()) )
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000508 MRI.insertCallerSavingCode(MInst, *BBI, *this );
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000509
510 */
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000511
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000512
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000513 // reset the stack offset for temporary variables since we may
514 // need that to spill
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000515 // mcInfo.popAllTempValues(TM);
Ruchira Sasankaf90870f2001-11-15 22:02:06 +0000516 // TODO ** : do later
Vikram S. Adve12af1642001-11-08 04:48:50 +0000517
Chris Lattner7a176752001-12-04 00:03:30 +0000518 //for(MachineInstr::val_const_op_iterator OpI(MInst);!OpI.done();++OpI) {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000519
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000520
521 // Now replace set the registers for operands in the machine instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000522 //
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000523 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
524
525 MachineOperand& Op = MInst->getOperand(OpNum);
526
527 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
528 Op.getOperandType() == MachineOperand::MO_CCRegister) {
529
530 const Value *const Val = Op.getVRegValue();
531
532 // delete this condition checking later (must assert if Val is null)
Chris Lattner045e7c82001-09-19 16:26:23 +0000533 if( !Val) {
534 if (DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000535 cerr << "Warning: NULL Value found for operand\n";
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000536 continue;
537 }
538 assert( Val && "Value is NULL");
539
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000540 LiveRange *const LR = LRI.getLiveRangeForValue(Val);
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000541
542 if ( !LR ) {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000543
544 // nothing to worry if it's a const or a label
545
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000546 if (DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000547 cerr << "*NO LR for operand : " << Op ;
548 cerr << " [reg:" << Op.getAllocatedRegNum() << "]";
549 cerr << " in inst:\t" << *MInst << "\n";
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000550 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000551
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000552 // if register is not allocated, mark register as invalid
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000553 if( Op.getAllocatedRegNum() == -1)
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000554 Op.setRegForValue( MRI.getInvalidRegNum());
Ruchira Sasankae727f852001-09-18 22:43:57 +0000555
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000556
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000557 continue;
558 }
559
560 unsigned RCID = (LR->getRegClass())->getID();
561
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000562 if( LR->hasColor() ) {
563 Op.setRegForValue( MRI.getUnifiedRegNum(RCID, LR->getColor()) );
564 }
565 else {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000566
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000567 // LR did NOT receive a color (register). Now, insert spill code
568 // for spilled opeands in this machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000569
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000570 //assert(0 && "LR must be spilled");
571 insertCode4SpilledLR(LR, MInst, *BBI, OpNum );
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000572
573 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000574 }
575
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000576 } // for each operand
577
578
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000579 // Now add instructions that the register allocator inserts before/after
580 // this machine instructions (done only for calls/rets/incoming args)
581 // We do this here, to ensure that spill for an instruction is inserted
582 // closest as possible to an instruction (see above insertCode4Spill...)
583 //
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000584 // If there are instructions to be added, *before* this machine
585 // instruction, add them now.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000586 //
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000587 if( AddedInstrMap[ MInst ] ) {
Chris Lattner697954c2002-01-20 22:54:45 +0000588 std::deque<MachineInstr *> &IBef = AddedInstrMap[MInst]->InstrnsBefore;
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000589
590 if( ! IBef.empty() ) {
Chris Lattner697954c2002-01-20 22:54:45 +0000591 std::deque<MachineInstr *>::iterator AdIt;
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000592
593 for( AdIt = IBef.begin(); AdIt != IBef.end() ; ++AdIt ) {
594
595 if( DEBUG_RA) {
596 cerr << "For inst " << *MInst;
Chris Lattner697954c2002-01-20 22:54:45 +0000597 cerr << " PREPENDed instr: " << **AdIt << "\n";
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000598 }
599
600 MInstIterator = MIVec.insert( MInstIterator, *AdIt );
601 ++MInstIterator;
602 }
603
604 }
605
606 }
607
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000608 // If there are instructions to be added *after* this machine
609 // instruction, add them now
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000610 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000611 if(AddedInstrMap[MInst] &&
612 !AddedInstrMap[MInst]->InstrnsAfter.empty() ) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000613
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000614 // if there are delay slots for this instruction, the instructions
615 // added after it must really go after the delayed instruction(s)
616 // So, we move the InstrAfter of the current instruction to the
617 // corresponding delayed instruction
618
619 unsigned delay;
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000620 if ((delay=TM.getInstrInfo().getNumDelaySlots(MInst->getOpCode())) >0){
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000621 move2DelayedInstr(MInst, *(MInstIterator+delay) );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000622
Chris Lattner697954c2002-01-20 22:54:45 +0000623 if(DEBUG_RA) cerr<< "\nMoved an added instr after the delay slot";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000624 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000625
626 else {
627
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000628
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000629 // Here we can add the "instructions after" to the current
630 // instruction since there are no delay slots for this instruction
631
Chris Lattner697954c2002-01-20 22:54:45 +0000632 std::deque<MachineInstr *> &IAft = AddedInstrMap[MInst]->InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000633
634 if( ! IAft.empty() ) {
635
Chris Lattner697954c2002-01-20 22:54:45 +0000636 std::deque<MachineInstr *>::iterator AdIt;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000637
638 ++MInstIterator; // advance to the next instruction
639
640 for( AdIt = IAft.begin(); AdIt != IAft.end() ; ++AdIt ) {
641
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000642 if(DEBUG_RA) {
643 cerr << "For inst " << *MInst;
Chris Lattner697954c2002-01-20 22:54:45 +0000644 cerr << " APPENDed instr: " << **AdIt << "\n";
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000645 }
646
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000647 MInstIterator = MIVec.insert( MInstIterator, *AdIt );
648 ++MInstIterator;
649 }
650
651 // MInsterator already points to the next instr. Since the
652 // for loop also increments it, decrement it to point to the
653 // instruction added last
654 --MInstIterator;
655
656 }
657
658 } // if not delay
659
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000660 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000661
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000662 } // for each machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000663 }
664}
665
666
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000667
668//----------------------------------------------------------------------------
669// This method inserts spill code for AN operand whose LR was spilled.
670// This method may be called several times for a single machine instruction
671// if it contains many spilled operands. Each time it is called, it finds
672// a register which is not live at that instruction and also which is not
673// used by other spilled operands of the same instruction. Then it uses
674// this register temporarily to accomodate the spilled value.
675//----------------------------------------------------------------------------
676void PhyRegAlloc::insertCode4SpilledLR(const LiveRange *LR,
677 MachineInstr *MInst,
678 const BasicBlock *BB,
679 const unsigned OpNum) {
680
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000681 assert(! TM.getInstrInfo().isCall(MInst->getOpCode()) &&
682 (! TM.getInstrInfo().isReturn(MInst->getOpCode())) &&
683 "Arg of a call/ret must be handled elsewhere");
684
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000685 MachineOperand& Op = MInst->getOperand(OpNum);
686 bool isDef = MInst->operandIsDefined(OpNum);
687 unsigned RegType = MRI.getRegType( LR );
688 int SpillOff = LR->getSpillOffFromFP();
689 RegClass *RC = LR->getRegClass();
690 const LiveVarSet *LVSetBef = LVI->getLiveVarSetBeforeMInst(MInst, BB);
Vikram S. Adve00521d72001-11-12 23:26:35 +0000691
Chris Lattner697954c2002-01-20 22:54:45 +0000692 mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000693
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000694 MachineInstr *MIBef=NULL, *AdIMid=NULL, *MIAft=NULL;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000695
696 int TmpRegU = getUsableUniRegAtMI(RC, RegType, MInst,LVSetBef, MIBef, MIAft);
697
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000698 // get the added instructions for this instruciton
699 AddedInstrns *AI = AddedInstrMap[ MInst ];
700 if ( !AI ) {
701 AI = new AddedInstrns();
702 AddedInstrMap[ MInst ] = AI;
703 }
704
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000705
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000706 if( !isDef ) {
707
708 // for a USE, we have to load the value of LR from stack to a TmpReg
709 // and use the TmpReg as one operand of instruction
710
711 // actual loading instruction
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000712 AdIMid = MRI.cpMem2RegMI(MRI.getFramePointer(), SpillOff, TmpRegU,RegType);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000713
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000714 if(MIBef)
715 AI->InstrnsBefore.push_back(MIBef);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000716
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000717 AI->InstrnsBefore.push_back(AdIMid);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000718
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000719 if(MIAft)
720 AI->InstrnsAfter.push_front(MIAft);
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000721
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000722
723 }
724 else { // if this is a Def
725
726 // for a DEF, we have to store the value produced by this instruction
727 // on the stack position allocated for this LR
728
729 // actual storing instruction
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000730 AdIMid = MRI.cpReg2MemMI(TmpRegU, MRI.getFramePointer(), SpillOff,RegType);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000731
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000732 if (MIBef)
733 AI->InstrnsBefore.push_back(MIBef);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000734
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000735 AI->InstrnsAfter.push_front(AdIMid);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000736
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000737 if (MIAft)
738 AI->InstrnsAfter.push_front(MIAft);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000739
740 } // if !DEF
741
742 cerr << "\nFor Inst " << *MInst;
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000743 cerr << " - SPILLED LR: "; LR->printSet();
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000744 cerr << "\n - Added Instructions:";
745 if( MIBef ) cerr << *MIBef;
746 cerr << *AdIMid;
747 if( MIAft ) cerr << *MIAft;
748
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000749 Op.setRegForValue( TmpRegU ); // set the opearnd
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000750
751
752}
753
754
755
756
757
758
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000759//----------------------------------------------------------------------------
760// We can use the following method to get a temporary register to be used
761// BEFORE any given machine instruction. If there is a register available,
762// this method will simply return that register and set MIBef = MIAft = NULL.
763// Otherwise, it will return a register and MIAft and MIBef will contain
764// two instructions used to free up this returned register.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000765// Returned register number is the UNIFIED register number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000766//----------------------------------------------------------------------------
767
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000768int PhyRegAlloc::getUsableUniRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000769 const int RegType,
770 const MachineInstr *MInst,
771 const LiveVarSet *LVSetBef,
772 MachineInstr *MIBef,
773 MachineInstr *MIAft) {
774
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000775 int RegU = getUnusedUniRegAtMI(RC, MInst, LVSetBef);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000776
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000777
778 if( RegU != -1) {
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000779 // we found an unused register, so we can simply use it
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000780 MIBef = MIAft = NULL;
781 }
782 else {
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000783 // we couldn't find an unused register. Generate code to free up a reg by
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000784 // saving it on stack and restoring after the instruction
785
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000786 int TmpOff = mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
Vikram S. Adve12af1642001-11-08 04:48:50 +0000787
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000788 RegU = getUniRegNotUsedByThisInst(RC, MInst);
789 MIBef = MRI.cpReg2MemMI(RegU, MRI.getFramePointer(), TmpOff, RegType );
790 MIAft = MRI.cpMem2RegMI(MRI.getFramePointer(), TmpOff, RegU, RegType );
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000791 }
792
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000793 return RegU;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000794}
795
796//----------------------------------------------------------------------------
797// This method is called to get a new unused register that can be used to
798// accomodate a spilled value.
799// This method may be called several times for a single machine instruction
800// if it contains many spilled operands. Each time it is called, it finds
801// a register which is not live at that instruction and also which is not
802// used by other spilled operands of the same instruction.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000803// Return register number is relative to the register class. NOT
804// unified number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000805//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000806int PhyRegAlloc::getUnusedUniRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000807 const MachineInstr *MInst,
808 const LiveVarSet *LVSetBef) {
809
810 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
811
812 bool *IsColorUsedArr = RC->getIsColorUsedArr();
813
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000814 for(unsigned i=0; i < NumAvailRegs; i++) // Reset array
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000815 IsColorUsedArr[i] = false;
816
817 LiveVarSet::const_iterator LIt = LVSetBef->begin();
818
819 // for each live var in live variable set after machine inst
820 for( ; LIt != LVSetBef->end(); ++LIt) {
821
822 // get the live range corresponding to live var
823 LiveRange *const LRofLV = LRI.getLiveRangeForValue(*LIt );
824
825 // LR can be null if it is a const since a const
826 // doesn't have a dominating def - see Assumptions above
827 if( LRofLV )
828 if( LRofLV->hasColor() )
829 IsColorUsedArr[ LRofLV->getColor() ] = true;
830 }
831
832 // It is possible that one operand of this MInst was already spilled
833 // and it received some register temporarily. If that's the case,
834 // it is recorded in machine operand. We must skip such registers.
835
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000836 setRelRegsUsedByThisInst(RC, MInst);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000837
838 unsigned c; // find first unused color
839 for( c=0; c < NumAvailRegs; c++)
840 if( ! IsColorUsedArr[ c ] ) break;
841
842 if(c < NumAvailRegs)
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000843 return MRI.getUnifiedRegNum(RC->getID(), c);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000844 else
845 return -1;
846
847
848}
849
850
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000851//----------------------------------------------------------------------------
852// Get any other register in a register class, other than what is used
853// by operands of a machine instruction. Returns the unified reg number.
854//----------------------------------------------------------------------------
855int PhyRegAlloc::getUniRegNotUsedByThisInst(RegClass *RC,
856 const MachineInstr *MInst) {
857
858 bool *IsColorUsedArr = RC->getIsColorUsedArr();
859 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
860
861
862 for(unsigned i=0; i < NumAvailRegs ; i++) // Reset array
863 IsColorUsedArr[i] = false;
864
865 setRelRegsUsedByThisInst(RC, MInst);
866
867 unsigned c; // find first unused color
868 for( c=0; c < RC->getNumOfAvailRegs(); c++)
869 if( ! IsColorUsedArr[ c ] ) break;
870
871 if(c < NumAvailRegs)
872 return MRI.getUnifiedRegNum(RC->getID(), c);
873 else
874 assert( 0 && "FATAL: No free register could be found in reg class!!");
Chris Lattner697954c2002-01-20 22:54:45 +0000875 return 0;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000876}
877
878
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000879//----------------------------------------------------------------------------
880// This method modifies the IsColorUsedArr of the register class passed to it.
881// It sets the bits corresponding to the registers used by this machine
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000882// instructions. Both explicit and implicit operands are set.
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000883//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000884void PhyRegAlloc::setRelRegsUsedByThisInst(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000885 const MachineInstr *MInst ) {
886
887 bool *IsColorUsedArr = RC->getIsColorUsedArr();
888
889 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
890
891 const MachineOperand& Op = MInst->getOperand(OpNum);
892
893 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000894 Op.getOperandType() == MachineOperand::MO_CCRegister ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000895
896 const Value *const Val = Op.getVRegValue();
897
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000898 if( Val )
899 if( MRI.getRegClassIDOfValue(Val) == RC->getID() ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000900 int Reg;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000901 if( (Reg=Op.getAllocatedRegNum()) != -1) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000902 IsColorUsedArr[ Reg ] = true;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000903 }
904 else {
905 // it is possilbe that this operand still is not marked with
906 // a register but it has a LR and that received a color
907
908 LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
909 if( LROfVal)
910 if( LROfVal->hasColor() )
911 IsColorUsedArr[ LROfVal->getColor() ] = true;
912 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000913
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000914 } // if reg classes are the same
915 }
916 else if (Op.getOperandType() == MachineOperand::MO_MachineRegister) {
917 IsColorUsedArr[ Op.getMachineRegNum() ] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000918 }
919 }
920
921 // If there are implicit references, mark them as well
922
923 for(unsigned z=0; z < MInst->getNumImplicitRefs(); z++) {
924
925 LiveRange *const LRofImpRef =
926 LRI.getLiveRangeForValue( MInst->getImplicitRef(z) );
Chris Lattner697954c2002-01-20 22:54:45 +0000927
928 if(LRofImpRef && LRofImpRef->hasColor())
929 IsColorUsedArr[LRofImpRef->getColor()] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000930 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000931}
932
933
934
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000935
936
937
938
939
940//----------------------------------------------------------------------------
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000941// If there are delay slots for an instruction, the instructions
942// added after it must really go after the delayed instruction(s).
943// So, we move the InstrAfter of that instruction to the
944// corresponding delayed instruction using the following method.
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000945
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000946//----------------------------------------------------------------------------
947void PhyRegAlloc:: move2DelayedInstr(const MachineInstr *OrigMI,
948 const MachineInstr *DelayedMI) {
949
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000950 // "added after" instructions of the original instr
Chris Lattner697954c2002-01-20 22:54:45 +0000951 std::deque<MachineInstr *> &OrigAft = AddedInstrMap[OrigMI]->InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000952
953 // "added instructions" of the delayed instr
954 AddedInstrns *DelayAdI = AddedInstrMap[DelayedMI];
955
956 if(! DelayAdI ) { // create a new "added after" if necessary
957 DelayAdI = new AddedInstrns();
958 AddedInstrMap[DelayedMI] = DelayAdI;
959 }
960
961 // "added after" instructions of the delayed instr
Chris Lattner697954c2002-01-20 22:54:45 +0000962 std::deque<MachineInstr *> &DelayedAft = DelayAdI->InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000963
964 // go thru all the "added after instructions" of the original instruction
965 // and append them to the "addded after instructions" of the delayed
966 // instructions
Chris Lattner697954c2002-01-20 22:54:45 +0000967 DelayedAft.insert(DelayedAft.end(), OrigAft.begin(), OrigAft.end());
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000968
969 // empty the "added after instructions" of the original instruction
970 OrigAft.clear();
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000971}
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000972
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000973//----------------------------------------------------------------------------
974// This method prints the code with registers after register allocation is
975// complete.
976//----------------------------------------------------------------------------
977void PhyRegAlloc::printMachineCode()
978{
979
Chris Lattner697954c2002-01-20 22:54:45 +0000980 cerr << "\n;************** Method " << Meth->getName()
981 << " *****************\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000982
983 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
984
985 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
986
Chris Lattner697954c2002-01-20 22:54:45 +0000987 cerr << "\n"; printLabel( *BBI); cerr << ": ";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000988
989 // get the iterator for machine instructions
990 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
991 MachineCodeForBasicBlock::iterator MInstIterator = MIVec.begin();
992
993 // iterate over all the machine instructions in BB
994 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
995
996 MachineInstr *const MInst = *MInstIterator;
997
998
Chris Lattner697954c2002-01-20 22:54:45 +0000999 cerr << "\n\t";
1000 cerr << TargetInstrDescriptors[MInst->getOpCode()].opCodeString;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001001
1002
Chris Lattner7a176752001-12-04 00:03:30 +00001003 //for(MachineInstr::val_const_op_iterator OpI(MInst);!OpI.done();++OpI) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001004
1005 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
1006
1007 MachineOperand& Op = MInst->getOperand(OpNum);
1008
1009 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasanka97b8b442001-10-18 22:36:26 +00001010 Op.getOperandType() == MachineOperand::MO_CCRegister /*||
1011 Op.getOperandType() == MachineOperand::MO_PCRelativeDisp*/ ) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001012
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001013 const Value *const Val = Op.getVRegValue () ;
Ruchira Sasankae727f852001-09-18 22:43:57 +00001014 // ****this code is temporary till NULL Values are fixed
1015 if( ! Val ) {
Chris Lattner697954c2002-01-20 22:54:45 +00001016 cerr << "\t<*NULL*>";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001017 continue;
1018 }
Ruchira Sasankae727f852001-09-18 22:43:57 +00001019
1020 // if a label or a constant
Chris Lattnerdbe53042002-01-21 01:33:12 +00001021 if(isa<BasicBlock>(Val)) {
Chris Lattner697954c2002-01-20 22:54:45 +00001022 cerr << "\t"; printLabel( Op.getVRegValue () );
1023 } else {
Ruchira Sasankae727f852001-09-18 22:43:57 +00001024 // else it must be a register value
1025 const int RegNum = Op.getAllocatedRegNum();
1026
Chris Lattner697954c2002-01-20 22:54:45 +00001027 cerr << "\t" << "%" << MRI.getUnifiedRegName( RegNum );
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001028 if (Val->hasName() )
Chris Lattner697954c2002-01-20 22:54:45 +00001029 cerr << "(" << Val->getName() << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001030 else
Chris Lattner697954c2002-01-20 22:54:45 +00001031 cerr << "(" << Val << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001032
1033 if( Op.opIsDef() )
Chris Lattner697954c2002-01-20 22:54:45 +00001034 cerr << "*";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001035
1036 const LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
1037 if( LROfVal )
1038 if( LROfVal->hasSpillOffset() )
Chris Lattner697954c2002-01-20 22:54:45 +00001039 cerr << "$";
Ruchira Sasankae727f852001-09-18 22:43:57 +00001040 }
1041
1042 }
1043 else if(Op.getOperandType() == MachineOperand::MO_MachineRegister) {
Chris Lattner697954c2002-01-20 22:54:45 +00001044 cerr << "\t" << "%" << MRI.getUnifiedRegName(Op.getMachineRegNum());
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001045 }
1046
1047 else
Chris Lattner697954c2002-01-20 22:54:45 +00001048 cerr << "\t" << Op; // use dump field
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001049 }
1050
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001051
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001052
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001053 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
1054 if( NumOfImpRefs > 0 ) {
1055
Chris Lattner697954c2002-01-20 22:54:45 +00001056 cerr << "\tImplicit:";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001057
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001058 for(unsigned z=0; z < NumOfImpRefs; z++) {
1059 printValue( MInst->getImplicitRef(z) );
Chris Lattner697954c2002-01-20 22:54:45 +00001060 cerr << "\t";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001061 }
1062
1063 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001064
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001065 } // for all machine instructions
1066
Chris Lattner697954c2002-01-20 22:54:45 +00001067 cerr << "\n";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001068
1069 } // for all BBs
1070
Chris Lattner697954c2002-01-20 22:54:45 +00001071 cerr << "\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001072}
1073
Ruchira Sasankae727f852001-09-18 22:43:57 +00001074
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001075#if 0
1076
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001077//----------------------------------------------------------------------------
1078//
1079//----------------------------------------------------------------------------
1080
1081void PhyRegAlloc::colorCallRetArgs()
1082{
1083
1084 CallRetInstrListType &CallRetInstList = LRI.getCallRetInstrList();
1085 CallRetInstrListType::const_iterator It = CallRetInstList.begin();
1086
1087 for( ; It != CallRetInstList.end(); ++It ) {
1088
Ruchira Sasankaa90e7702001-10-15 16:26:38 +00001089 const MachineInstr *const CRMI = *It;
1090 unsigned OpCode = CRMI->getOpCode();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001091
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001092 // get the added instructions for this Call/Ret instruciton
1093 AddedInstrns *AI = AddedInstrMap[ CRMI ];
1094 if ( !AI ) {
1095 AI = new AddedInstrns();
1096 AddedInstrMap[ CRMI ] = AI;
1097 }
1098
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001099 // Tmp stack poistions are needed by some calls that have spilled args
1100 // So reset it before we call each such method
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001101 //mcInfo.popAllTempValues(TM);
1102
1103
Vikram S. Adve12af1642001-11-08 04:48:50 +00001104
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001105 if (TM.getInstrInfo().isCall(OpCode))
1106 MRI.colorCallArgs(CRMI, LRI, AI, *this);
1107 else if (TM.getInstrInfo().isReturn(OpCode))
Ruchira Sasankaa90e7702001-10-15 16:26:38 +00001108 MRI.colorRetValue( CRMI, LRI, AI );
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001109 else
1110 assert(0 && "Non Call/Ret instrn in CallRetInstrList\n");
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001111 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001112}
1113
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001114#endif
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001115
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001116//----------------------------------------------------------------------------
1117
1118//----------------------------------------------------------------------------
1119void PhyRegAlloc::colorIncomingArgs()
1120{
1121 const BasicBlock *const FirstBB = Meth->front();
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001122 const MachineInstr *FirstMI = FirstBB->getMachineInstrVec().front();
1123 assert(FirstMI && "No machine instruction in entry BB");
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001124
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001125 AddedInstrns *AI = AddedInstrMap[FirstMI];
1126 if (!AI)
1127 AddedInstrMap[FirstMI] = AI = new AddedInstrns();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001128
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001129 MRI.colorMethodArgs(Meth, LRI, AI);
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001130}
1131
Ruchira Sasankae727f852001-09-18 22:43:57 +00001132
1133//----------------------------------------------------------------------------
1134// Used to generate a label for a basic block
1135//----------------------------------------------------------------------------
Chris Lattner697954c2002-01-20 22:54:45 +00001136void PhyRegAlloc::printLabel(const Value *const Val) {
1137 if (Val->hasName())
1138 cerr << Val->getName();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001139 else
Chris Lattner697954c2002-01-20 22:54:45 +00001140 cerr << "Label" << Val;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001141}
1142
1143
Ruchira Sasankae727f852001-09-18 22:43:57 +00001144//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001145// This method calls setSugColorUsable method of each live range. This
1146// will determine whether the suggested color of LR is really usable.
1147// A suggested color is not usable when the suggested color is volatile
1148// AND when there are call interferences
1149//----------------------------------------------------------------------------
1150
1151void PhyRegAlloc::markUnusableSugColors()
1152{
Chris Lattner697954c2002-01-20 22:54:45 +00001153 if(DEBUG_RA ) cerr << "\nmarking unusable suggested colors ...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001154
1155 // hash map iterator
1156 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
1157 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
1158
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001159 for(; HMI != HMIEnd ; ++HMI ) {
1160 if (HMI->first) {
1161 LiveRange *L = HMI->second; // get the LiveRange
1162 if (L) {
1163 if(L->hasSuggestedColor()) {
1164 int RCID = L->getRegClass()->getID();
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001165 if( MRI.isRegVolatile( RCID, L->getSuggestedColor()) &&
1166 L->isCallInterference() )
1167 L->setSuggestedColorUsable( false );
1168 else
1169 L->setSuggestedColorUsable( true );
1170 }
1171 } // if L->hasSuggestedColor()
1172 }
1173 } // for all LR's in hash map
1174}
1175
1176
1177
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001178//----------------------------------------------------------------------------
1179// The following method will set the stack offsets of the live ranges that
1180// are decided to be spillled. This must be called just after coloring the
1181// LRs using the graph coloring algo. For each live range that is spilled,
1182// this method allocate a new spill position on the stack.
1183//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001184
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001185void PhyRegAlloc::allocateStackSpace4SpilledLRs()
1186{
Chris Lattner697954c2002-01-20 22:54:45 +00001187 if(DEBUG_RA ) cerr << "\nsetting LR stack offsets ...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001188
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001189 // hash map iterator
1190 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
1191 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
1192
1193 for( ; HMI != HMIEnd ; ++HMI ) {
Chris Lattner697954c2002-01-20 22:54:45 +00001194 if(HMI->first && HMI->second) {
1195 LiveRange *L = HMI->second; // get the LiveRange
1196 if( ! L->hasColor() )
1197 // NOTE: ** allocating the size of long Type **
1198 L->setSpillOffFromFP(mcInfo.allocateSpilledValue(TM, Type::LongTy));
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001199 }
1200 } // for all LR's in hash map
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001201}
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001202
1203
1204
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001205//----------------------------------------------------------------------------
Ruchira Sasankae727f852001-09-18 22:43:57 +00001206// The entry pont to Register Allocation
1207//----------------------------------------------------------------------------
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001208
1209void PhyRegAlloc::allocateRegisters()
1210{
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001211
1212 // make sure that we put all register classes into the RegClassList
1213 // before we call constructLiveRanges (now done in the constructor of
1214 // PhyRegAlloc class).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001215 //
1216 LRI.constructLiveRanges(); // create LR info
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001217
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001218 if (DEBUG_RA)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001219 LRI.printLiveRanges();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001220
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001221 createIGNodeListsAndIGs(); // create IGNode list and IGs
1222
1223 buildInterferenceGraphs(); // build IGs in all reg classes
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001224
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001225
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001226 if (DEBUG_RA) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001227 // print all LRs in all reg classes
1228 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1229 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001230
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001231 // print IGs in all register classes
1232 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1233 RegClassList[ rc ]->printIG();
1234 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001235
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001236
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001237 LRI.coalesceLRs(); // coalesce all live ranges
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001238
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +00001239
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001240 if( DEBUG_RA) {
1241 // print all LRs in all reg classes
1242 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1243 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001244
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001245 // print IGs in all register classes
1246 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1247 RegClassList[ rc ]->printIG();
1248 }
1249
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001250
1251 // mark un-usable suggested color before graph coloring algorithm.
1252 // When this is done, the graph coloring algo will not reserve
1253 // suggested color unnecessarily - they can be used by another LR
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001254 //
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001255 markUnusableSugColors();
1256
1257 // color all register classes using the graph coloring algo
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001258 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1259 RegClassList[ rc ]->colorAllRegs();
1260
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001261 // Atter grpah coloring, if some LRs did not receive a color (i.e, spilled)
1262 // a poistion for such spilled LRs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001263 //
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001264 allocateStackSpace4SpilledLRs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001265
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001266 mcInfo.popAllTempValues(TM); // TODO **Check
1267
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001268 // color incoming args - if the correct color was not received
1269 // insert code to copy to the correct register
1270 //
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001271 colorIncomingArgs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001272
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001273 // Now update the machine code with register names and add any
1274 // additional code inserted by the register allocator to the instruction
1275 // stream
1276 //
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001277 updateMachineCode();
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001278
Chris Lattner045e7c82001-09-19 16:26:23 +00001279 if (DEBUG_RA) {
Vikram S. Adve12af1642001-11-08 04:48:50 +00001280 MachineCodeForMethod::get(Meth).dump();
Chris Lattner045e7c82001-09-19 16:26:23 +00001281 printMachineCode(); // only for DEBUGGING
1282 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001283}
1284
Ruchira Sasankae727f852001-09-18 22:43:57 +00001285
1286