blob: 2fd2c91a6fd6b5e382ffd45704b34b42a4b72f71 [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
Chris Lattner4d7fc112002-02-04 20:02:38 +000051 PhyRegAlloc PRA(M, Target, &getAnalysis<MethodLiveVarInfo>(),
Chris Lattner14ab1ce2002-02-04 17:48:00 +000052 &getAnalysis<cfg::LoopInfo>());
Chris Lattner2f9b28e2002-02-04 15:54:09 +000053 PRA.allocateRegisters();
54
55 if (DEBUG_RA) cerr << "\nRegister allocation complete!\n";
56 return false;
57 }
Chris Lattner4911c352002-02-04 17:39:42 +000058
59 virtual void getAnalysisUsageInfo(Pass::AnalysisSet &Requires,
60 Pass::AnalysisSet &Destroyed,
61 Pass::AnalysisSet &Provided) {
Chris Lattner14ab1ce2002-02-04 17:48:00 +000062 Requires.push_back(cfg::LoopInfo::ID);
Chris Lattner4d7fc112002-02-04 20:02:38 +000063 Requires.push_back(MethodLiveVarInfo::ID);
Chris Lattner4911c352002-02-04 17:39:42 +000064 }
Chris Lattner2f9b28e2002-02-04 15:54:09 +000065 };
Chris Lattner6dd98a62002-02-04 00:33:08 +000066}
67
Chris Lattner2f9b28e2002-02-04 15:54:09 +000068MethodPass *getRegisterAllocator(TargetMachine &T) {
69 return new RegisterAllocator(T);
70}
Chris Lattner6dd98a62002-02-04 00:33:08 +000071
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000072//----------------------------------------------------------------------------
73// Constructor: Init local composite objects and create register classes.
74//----------------------------------------------------------------------------
Vikram S. Adve12af1642001-11-08 04:48:50 +000075PhyRegAlloc::PhyRegAlloc(Method *M,
Ruchira Sasanka8e604792001-09-14 21:18:34 +000076 const TargetMachine& tm,
Chris Lattner4911c352002-02-04 17:39:42 +000077 MethodLiveVarInfo *Lvi,
Chris Lattner14ab1ce2002-02-04 17:48:00 +000078 cfg::LoopInfo *LDC)
Chris Lattner697954c2002-01-20 22:54:45 +000079 : TM(tm), Meth(M),
Vikram S. Adve12af1642001-11-08 04:48:50 +000080 mcInfo(MachineCodeForMethod::get(M)),
81 LVI(Lvi), LRI(M, tm, RegClassList),
Ruchira Sasanka8e604792001-09-14 21:18:34 +000082 MRI( tm.getRegInfo() ),
83 NumOfRegClasses(MRI.getNumOfRegClasses()),
Chris Lattner4911c352002-02-04 17:39:42 +000084 LoopDepthCalc(LDC) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +000085
86 // create each RegisterClass and put in RegClassList
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000087 //
Chris Lattner697954c2002-01-20 22:54:45 +000088 for(unsigned int rc=0; rc < NumOfRegClasses; rc++)
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000089 RegClassList.push_back( new RegClass(M, MRI.getMachineRegClass(rc),
90 &ResColList) );
Ruchira Sasanka8e604792001-09-14 21:18:34 +000091}
92
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000093
94//----------------------------------------------------------------------------
95// Destructor: Deletes register classes
96//----------------------------------------------------------------------------
97PhyRegAlloc::~PhyRegAlloc() {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000098 for( unsigned int rc=0; rc < NumOfRegClasses; rc++)
99 delete RegClassList[rc];
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000100}
101
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000102//----------------------------------------------------------------------------
103// This method initally creates interference graphs (one in each reg class)
104// and IGNodeList (one in each IG). The actual nodes will be pushed later.
105//----------------------------------------------------------------------------
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000106void PhyRegAlloc::createIGNodeListsAndIGs() {
107 if (DEBUG_RA) cerr << "Creating LR lists ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000108
109 // hash map iterator
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000110 LiveRangeMapType::const_iterator HMI = LRI.getLiveRangeMap()->begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000111
112 // hash map end
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000113 LiveRangeMapType::const_iterator HMIEnd = LRI.getLiveRangeMap()->end();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000114
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000115 for (; HMI != HMIEnd ; ++HMI ) {
116 if (HMI->first) {
117 LiveRange *L = HMI->second; // get the LiveRange
118 if (!L) {
119 if( DEBUG_RA) {
120 cerr << "\n*?!?Warning: Null liver range found for: ";
121 printValue(HMI->first); cerr << "\n";
122 }
123 continue;
124 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000125 // if the Value * is not null, and LR
126 // is not yet written to the IGNodeList
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000127 if( !(L->getUserIGNode()) ) {
128 RegClass *const RC = // RegClass of first value in the LR
129 RegClassList[ L->getRegClass()->getID() ];
130
131 RC->addLRToIG(L); // add this LR to an IG
132 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000133 }
134 }
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000135
136 // init RegClassList
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000137 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000138 RegClassList[rc]->createInterferenceGraph();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000139
140 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000141 cerr << "LRLists Created!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000142}
143
144
145
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000146
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000147//----------------------------------------------------------------------------
148// This method will add all interferences at for a given instruction.
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000149// Interence occurs only if the LR of Def (Inst or Arg) is of the same reg
150// class as that of live var. The live var passed to this function is the
151// LVset AFTER the instruction
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000152//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000153void PhyRegAlloc::addInterference(const Value *const Def,
154 const LiveVarSet *const LVSet,
155 const bool isCallInst) {
156
157 LiveVarSet::const_iterator LIt = LVSet->begin();
158
159 // get the live range of instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000160 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000161 const LiveRange *const LROfDef = LRI.getLiveRangeForValue( Def );
162
163 IGNode *const IGNodeOfDef = LROfDef->getUserIGNode();
164 assert( IGNodeOfDef );
165
166 RegClass *const RCOfDef = LROfDef->getRegClass();
167
168 // for each live var in live variable set
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000169 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000170 for( ; LIt != LVSet->end(); ++LIt) {
171
172 if( DEBUG_RA > 1) {
Chris Lattner697954c2002-01-20 22:54:45 +0000173 cerr << "< Def="; printValue(Def);
174 cerr << ", Lvar="; printValue( *LIt); cerr << "> ";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000175 }
176
177 // get the live range corresponding to live var
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000178 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000179 LiveRange *const LROfVar = LRI.getLiveRangeForValue(*LIt );
180
181 // LROfVar can be null if it is a const since a const
182 // doesn't have a dominating def - see Assumptions above
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000183 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000184 if (LROfVar) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000185 if(LROfDef == LROfVar) // do not set interf for same LR
186 continue;
187
188 // if 2 reg classes are the same set interference
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000189 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000190 if(RCOfDef == LROfVar->getRegClass()) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000191 RCOfDef->setInterference( LROfDef, LROfVar);
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000192 } else if(DEBUG_RA > 1) {
193 // we will not have LRs for values not explicitly allocated in the
194 // instruction stream (e.g., constants)
195 cerr << " warning: no live range for " ;
196 printValue(*LIt); cerr << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000197 }
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000198 }
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000199 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000200}
201
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000202
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000203
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000204//----------------------------------------------------------------------------
205// For a call instruction, this method sets the CallInterference flag in
206// the LR of each variable live int the Live Variable Set live after the
207// call instruction (except the return value of the call instruction - since
208// the return value does not interfere with that call itself).
209//----------------------------------------------------------------------------
210
211void PhyRegAlloc::setCallInterferences(const MachineInstr *MInst,
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000212 const LiveVarSet *const LVSetAft ) {
213
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000214 // Now find the LR of the return value of the call
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000215 // We do this because, we look at the LV set *after* the instruction
216 // to determine, which LRs must be saved across calls. The return value
217 // of the call is live in this set - but it does not interfere with call
218 // (i.e., we can allocate a volatile register to the return value)
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000219 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000220 LiveRange *RetValLR = NULL;
Ruchira Sasankab3b6f532001-10-21 16:43:41 +0000221 const Value *RetVal = MRI.getCallInstRetVal( MInst );
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000222
Ruchira Sasankab3b6f532001-10-21 16:43:41 +0000223 if( RetVal ) {
224 RetValLR = LRI.getLiveRangeForValue( RetVal );
225 assert( RetValLR && "No LR for RetValue of call");
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000226 }
227
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000228 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000229 cerr << "\n For call inst: " << *MInst;
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000230
231 LiveVarSet::const_iterator LIt = LVSetAft->begin();
232
233 // for each live var in live variable set after machine inst
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000234 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000235 for( ; LIt != LVSetAft->end(); ++LIt) {
236
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000237 // get the live range corresponding to live var
238 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000239 LiveRange *const LR = LRI.getLiveRangeForValue(*LIt );
240
241 if( LR && DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000242 cerr << "\n\tLR Aft Call: ";
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000243 LR->printSet();
244 }
245
246
247 // LR can be null if it is a const since a const
248 // doesn't have a dominating def - see Assumptions above
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000249 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000250 if( LR && (LR != RetValLR) ) {
251 LR->setCallInterference();
252 if( DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000253 cerr << "\n ++Added call interf for LR: " ;
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000254 LR->printSet();
255 }
256 }
257
258 }
259
260}
261
262
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000263
264
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000265//----------------------------------------------------------------------------
266// This method will walk thru code and create interferences in the IG of
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000267// each RegClass. Also, this method calculates the spill cost of each
268// Live Range (it is done in this method to save another pass over the code).
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000269//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000270void PhyRegAlloc::buildInterferenceGraphs()
271{
272
Chris Lattner697954c2002-01-20 22:54:45 +0000273 if(DEBUG_RA) cerr << "Creating interference graphs ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000274
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000275 unsigned BBLoopDepthCost;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000276 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
277
278 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
279
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000280 // find the 10^(loop_depth) of this BB
281 //
Chris Lattner4911c352002-02-04 17:39:42 +0000282 BBLoopDepthCost = (unsigned) pow( 10.0, LoopDepthCalc->getLoopDepth(*BBI));
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000283
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000284 // get the iterator for machine instructions
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000285 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000286 const MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
287 MachineCodeForBasicBlock::const_iterator
288 MInstIterator = MIVec.begin();
289
290 // iterate over all the machine instructions in BB
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000291 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000292 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000293
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +0000294 const MachineInstr * MInst = *MInstIterator;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000295
296 // get the LV set after the instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000297 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000298 const LiveVarSet *const LVSetAI =
299 LVI->getLiveVarSetAfterMInst(MInst, *BBI);
300
301 const bool isCallInst = TM.getInstrInfo().isCall(MInst->getOpCode());
302
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000303 if( isCallInst ) {
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000304 // set the isCallInterference flag of each live range wich extends
305 // accross this call instruction. This information is used by graph
306 // coloring algo to avoid allocating volatile colors to live ranges
307 // that span across calls (since they have to be saved/restored)
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000308 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000309 setCallInterferences( MInst, LVSetAI);
310 }
311
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000312
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000313 // iterate over all MI operands to find defs
314 //
Chris Lattner7a176752001-12-04 00:03:30 +0000315 for( MachineInstr::val_const_op_iterator OpI(MInst);!OpI.done(); ++OpI) {
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000316
317 if( OpI.isDef() ) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000318 // create a new LR iff this operand is a def
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000319 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000320 addInterference(*OpI, LVSetAI, isCallInst );
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000321 }
322
323 // Calculate the spill cost of each live range
324 //
325 LiveRange *LR = LRI.getLiveRangeForValue( *OpI );
326 if( LR )
327 LR->addSpillCost(BBLoopDepthCost);
328 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000329
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000330
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000331 // if there are multiple defs in this instruction e.g. in SETX
332 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000333 if (TM.getInstrInfo().isPseudoInstr(MInst->getOpCode()))
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000334 addInterf4PseudoInstr(MInst);
335
336
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000337 // Also add interference for any implicit definitions in a machine
338 // instr (currently, only calls have this).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000339 //
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000340 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
341 if( NumOfImpRefs > 0 ) {
342 for(unsigned z=0; z < NumOfImpRefs; z++)
343 if( MInst->implicitRefIsDefined(z) )
344 addInterference( MInst->getImplicitRef(z), LVSetAI, isCallInst );
345 }
346
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +0000347
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000348 } // for all machine instructions in BB
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000349
350 } // for all BBs in method
351
352
353 // add interferences for method arguments. Since there are no explict
354 // defs in method for args, we have to add them manually
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000355 //
356 addInterferencesForArgs();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000357
358 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000359 cerr << "Interference graphs calculted!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000360
361}
362
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000363
364
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000365//--------------------------------------------------------------------------
366// Pseudo instructions will be exapnded to multiple instructions by the
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000367// assembler. Consequently, all the opernds must get distinct registers.
368// Therefore, we mark all operands of a pseudo instruction as they interfere
369// with one another.
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000370//--------------------------------------------------------------------------
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000371void PhyRegAlloc::addInterf4PseudoInstr(const MachineInstr *MInst) {
372
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000373 bool setInterf = false;
374
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000375 // iterate over MI operands to find defs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000376 //
Chris Lattner7a176752001-12-04 00:03:30 +0000377 for( MachineInstr::val_const_op_iterator It1(MInst);!It1.done(); ++It1) {
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000378
379 const LiveRange *const LROfOp1 = LRI.getLiveRangeForValue( *It1 );
380
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000381 if( !LROfOp1 && It1.isDef() )
382 assert( 0 && "No LR for Def in PSEUDO insruction");
383
Chris Lattner7a176752001-12-04 00:03:30 +0000384 MachineInstr::val_const_op_iterator It2 = It1;
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000385 ++It2;
386
387 for( ; !It2.done(); ++It2) {
388
389 const LiveRange *const LROfOp2 = LRI.getLiveRangeForValue( *It2 );
390
391 if( LROfOp2) {
392
393 RegClass *const RCOfOp1 = LROfOp1->getRegClass();
394 RegClass *const RCOfOp2 = LROfOp2->getRegClass();
395
396 if( RCOfOp1 == RCOfOp2 ){
397 RCOfOp1->setInterference( LROfOp1, LROfOp2 );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000398 setInterf = true;
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000399 }
400
401 } // if Op2 has a LR
402
403 } // for all other defs in machine instr
404
405 } // for all operands in an instruction
406
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000407 if( !setInterf && (MInst->getNumOperands() > 2) ) {
408 cerr << "\nInterf not set for any operand in pseudo instr:\n";
409 cerr << *MInst;
410 assert(0 && "Interf not set for pseudo instr with > 2 operands" );
411
412 }
413
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000414}
415
416
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000417
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000418//----------------------------------------------------------------------------
419// This method will add interferences for incoming arguments to a method.
420//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000421void PhyRegAlloc::addInterferencesForArgs()
422{
423 // get the InSet of root BB
424 const LiveVarSet *const InSet = LVI->getInSetOfBB( Meth->front() );
425
426 // get the argument list
427 const Method::ArgumentListType& ArgList = Meth->getArgumentList();
428
429 // get an iterator to arg list
430 Method::ArgumentListType::const_iterator ArgIt = ArgList.begin();
431
432
433 for( ; ArgIt != ArgList.end() ; ++ArgIt) { // for each argument
Chris Lattner30adeb62002-02-04 16:36:59 +0000434 addInterference((Value*)*ArgIt, InSet, false); // add interferences between
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000435 // args and LVars at start
436 if( DEBUG_RA > 1) {
Chris Lattner697954c2002-01-20 22:54:45 +0000437 cerr << " - %% adding interference for argument ";
438 printValue((const Value *)*ArgIt); cerr << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000439 }
440 }
441}
442
443
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000444
445
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000446//----------------------------------------------------------------------------
447// This method is called after register allocation is complete to set the
448// allocated reisters in the machine code. This code will add register numbers
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000449// to MachineOperands that contain a Value. Also it calls target specific
450// methods to produce caller saving instructions. At the end, it adds all
451// additional instructions produced by the register allocator to the
452// instruction stream.
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000453//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000454void PhyRegAlloc::updateMachineCode()
455{
456
457 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
458
459 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
460
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000461 // get the iterator for machine instructions
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000462 //
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000463 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
464 MachineCodeForBasicBlock::iterator MInstIterator = MIVec.begin();
465
466 // iterate over all the machine instructions in BB
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000467 //
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000468 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
469
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000470 MachineInstr *MInst = *MInstIterator;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000471
472 unsigned Opcode = MInst->getOpCode();
473
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000474 // do not process Phis
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000475 if (TM.getInstrInfo().isPhi(Opcode))
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000476 continue;
477
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000478 // Now insert speical instructions (if necessary) for call/return
479 // instructions.
480 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000481 if (TM.getInstrInfo().isCall(Opcode) ||
482 TM.getInstrInfo().isReturn(Opcode)) {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000483
484 AddedInstrns *AI = AddedInstrMap[ MInst];
485 if ( !AI ) {
486 AI = new AddedInstrns();
487 AddedInstrMap[ MInst ] = AI;
488 }
489
490 // Tmp stack poistions are needed by some calls that have spilled args
491 // So reset it before we call each such method
Ruchira Sasanka6a3db8c2002-01-07 21:09:06 +0000492 //
493 mcInfo.popAllTempValues(TM);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000494
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000495 if (TM.getInstrInfo().isCall(Opcode))
496 MRI.colorCallArgs(MInst, LRI, AI, *this, *BBI);
497 else if (TM.getInstrInfo().isReturn(Opcode))
498 MRI.colorRetValue(MInst, LRI, AI);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000499 }
500
501
502 /* -- Using above code instead of this
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000503
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000504 // if this machine instr is call, insert caller saving code
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000505
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000506 if( (TM.getInstrInfo()).isCall( MInst->getOpCode()) )
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000507 MRI.insertCallerSavingCode(MInst, *BBI, *this );
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000508
509 */
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000510
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000511
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000512 // reset the stack offset for temporary variables since we may
513 // need that to spill
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000514 // mcInfo.popAllTempValues(TM);
Ruchira Sasankaf90870f2001-11-15 22:02:06 +0000515 // TODO ** : do later
Vikram S. Adve12af1642001-11-08 04:48:50 +0000516
Chris Lattner7a176752001-12-04 00:03:30 +0000517 //for(MachineInstr::val_const_op_iterator OpI(MInst);!OpI.done();++OpI) {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000518
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000519
520 // Now replace set the registers for operands in the machine instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000521 //
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000522 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
523
524 MachineOperand& Op = MInst->getOperand(OpNum);
525
526 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
527 Op.getOperandType() == MachineOperand::MO_CCRegister) {
528
529 const Value *const Val = Op.getVRegValue();
530
531 // delete this condition checking later (must assert if Val is null)
Chris Lattner045e7c82001-09-19 16:26:23 +0000532 if( !Val) {
533 if (DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000534 cerr << "Warning: NULL Value found for operand\n";
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000535 continue;
536 }
537 assert( Val && "Value is NULL");
538
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000539 LiveRange *const LR = LRI.getLiveRangeForValue(Val);
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000540
541 if ( !LR ) {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000542
543 // nothing to worry if it's a const or a label
544
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000545 if (DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000546 cerr << "*NO LR for operand : " << Op ;
547 cerr << " [reg:" << Op.getAllocatedRegNum() << "]";
548 cerr << " in inst:\t" << *MInst << "\n";
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000549 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000550
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000551 // if register is not allocated, mark register as invalid
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000552 if( Op.getAllocatedRegNum() == -1)
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000553 Op.setRegForValue( MRI.getInvalidRegNum());
Ruchira Sasankae727f852001-09-18 22:43:57 +0000554
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000555
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000556 continue;
557 }
558
559 unsigned RCID = (LR->getRegClass())->getID();
560
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000561 if( LR->hasColor() ) {
562 Op.setRegForValue( MRI.getUnifiedRegNum(RCID, LR->getColor()) );
563 }
564 else {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000565
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000566 // LR did NOT receive a color (register). Now, insert spill code
567 // for spilled opeands in this machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000568
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000569 //assert(0 && "LR must be spilled");
570 insertCode4SpilledLR(LR, MInst, *BBI, OpNum );
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000571
572 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000573 }
574
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000575 } // for each operand
576
577
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000578 // Now add instructions that the register allocator inserts before/after
579 // this machine instructions (done only for calls/rets/incoming args)
580 // We do this here, to ensure that spill for an instruction is inserted
581 // closest as possible to an instruction (see above insertCode4Spill...)
582 //
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000583 // If there are instructions to be added, *before* this machine
584 // instruction, add them now.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000585 //
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000586 if( AddedInstrMap[ MInst ] ) {
Chris Lattner697954c2002-01-20 22:54:45 +0000587 std::deque<MachineInstr *> &IBef = AddedInstrMap[MInst]->InstrnsBefore;
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000588
589 if( ! IBef.empty() ) {
Chris Lattner697954c2002-01-20 22:54:45 +0000590 std::deque<MachineInstr *>::iterator AdIt;
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000591
592 for( AdIt = IBef.begin(); AdIt != IBef.end() ; ++AdIt ) {
593
594 if( DEBUG_RA) {
595 cerr << "For inst " << *MInst;
Chris Lattner697954c2002-01-20 22:54:45 +0000596 cerr << " PREPENDed instr: " << **AdIt << "\n";
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000597 }
598
599 MInstIterator = MIVec.insert( MInstIterator, *AdIt );
600 ++MInstIterator;
601 }
602
603 }
604
605 }
606
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000607 // If there are instructions to be added *after* this machine
608 // instruction, add them now
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000609 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000610 if(AddedInstrMap[MInst] &&
611 !AddedInstrMap[MInst]->InstrnsAfter.empty() ) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000612
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000613 // if there are delay slots for this instruction, the instructions
614 // added after it must really go after the delayed instruction(s)
615 // So, we move the InstrAfter of the current instruction to the
616 // corresponding delayed instruction
617
618 unsigned delay;
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000619 if ((delay=TM.getInstrInfo().getNumDelaySlots(MInst->getOpCode())) >0){
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000620 move2DelayedInstr(MInst, *(MInstIterator+delay) );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000621
Chris Lattner697954c2002-01-20 22:54:45 +0000622 if(DEBUG_RA) cerr<< "\nMoved an added instr after the delay slot";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000623 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000624
625 else {
626
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000627
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000628 // Here we can add the "instructions after" to the current
629 // instruction since there are no delay slots for this instruction
630
Chris Lattner697954c2002-01-20 22:54:45 +0000631 std::deque<MachineInstr *> &IAft = AddedInstrMap[MInst]->InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000632
633 if( ! IAft.empty() ) {
634
Chris Lattner697954c2002-01-20 22:54:45 +0000635 std::deque<MachineInstr *>::iterator AdIt;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000636
637 ++MInstIterator; // advance to the next instruction
638
639 for( AdIt = IAft.begin(); AdIt != IAft.end() ; ++AdIt ) {
640
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000641 if(DEBUG_RA) {
642 cerr << "For inst " << *MInst;
Chris Lattner697954c2002-01-20 22:54:45 +0000643 cerr << " APPENDed instr: " << **AdIt << "\n";
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000644 }
645
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000646 MInstIterator = MIVec.insert( MInstIterator, *AdIt );
647 ++MInstIterator;
648 }
649
650 // MInsterator already points to the next instr. Since the
651 // for loop also increments it, decrement it to point to the
652 // instruction added last
653 --MInstIterator;
654
655 }
656
657 } // if not delay
658
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000659 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000660
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000661 } // for each machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000662 }
663}
664
665
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000666
667//----------------------------------------------------------------------------
668// This method inserts spill code for AN operand whose LR was spilled.
669// This method may be called several times for a single machine instruction
670// if it contains many spilled operands. Each time it is called, it finds
671// a register which is not live at that instruction and also which is not
672// used by other spilled operands of the same instruction. Then it uses
673// this register temporarily to accomodate the spilled value.
674//----------------------------------------------------------------------------
675void PhyRegAlloc::insertCode4SpilledLR(const LiveRange *LR,
676 MachineInstr *MInst,
677 const BasicBlock *BB,
678 const unsigned OpNum) {
679
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000680 assert(! TM.getInstrInfo().isCall(MInst->getOpCode()) &&
681 (! TM.getInstrInfo().isReturn(MInst->getOpCode())) &&
682 "Arg of a call/ret must be handled elsewhere");
683
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000684 MachineOperand& Op = MInst->getOperand(OpNum);
685 bool isDef = MInst->operandIsDefined(OpNum);
686 unsigned RegType = MRI.getRegType( LR );
687 int SpillOff = LR->getSpillOffFromFP();
688 RegClass *RC = LR->getRegClass();
689 const LiveVarSet *LVSetBef = LVI->getLiveVarSetBeforeMInst(MInst, BB);
Vikram S. Adve00521d72001-11-12 23:26:35 +0000690
Chris Lattner697954c2002-01-20 22:54:45 +0000691 mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000692
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000693 MachineInstr *MIBef=NULL, *AdIMid=NULL, *MIAft=NULL;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000694
695 int TmpRegU = getUsableUniRegAtMI(RC, RegType, MInst,LVSetBef, MIBef, MIAft);
696
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000697 // get the added instructions for this instruciton
698 AddedInstrns *AI = AddedInstrMap[ MInst ];
699 if ( !AI ) {
700 AI = new AddedInstrns();
701 AddedInstrMap[ MInst ] = AI;
702 }
703
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000704
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000705 if( !isDef ) {
706
707 // for a USE, we have to load the value of LR from stack to a TmpReg
708 // and use the TmpReg as one operand of instruction
709
710 // actual loading instruction
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000711 AdIMid = MRI.cpMem2RegMI(MRI.getFramePointer(), SpillOff, TmpRegU,RegType);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000712
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000713 if(MIBef)
714 AI->InstrnsBefore.push_back(MIBef);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000715
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000716 AI->InstrnsBefore.push_back(AdIMid);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000717
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000718 if(MIAft)
719 AI->InstrnsAfter.push_front(MIAft);
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000720
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000721
722 }
723 else { // if this is a Def
724
725 // for a DEF, we have to store the value produced by this instruction
726 // on the stack position allocated for this LR
727
728 // actual storing instruction
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000729 AdIMid = MRI.cpReg2MemMI(TmpRegU, MRI.getFramePointer(), SpillOff,RegType);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000730
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000731 if (MIBef)
732 AI->InstrnsBefore.push_back(MIBef);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000733
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000734 AI->InstrnsAfter.push_front(AdIMid);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000735
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000736 if (MIAft)
737 AI->InstrnsAfter.push_front(MIAft);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000738
739 } // if !DEF
740
741 cerr << "\nFor Inst " << *MInst;
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000742 cerr << " - SPILLED LR: "; LR->printSet();
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000743 cerr << "\n - Added Instructions:";
744 if( MIBef ) cerr << *MIBef;
745 cerr << *AdIMid;
746 if( MIAft ) cerr << *MIAft;
747
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000748 Op.setRegForValue( TmpRegU ); // set the opearnd
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000749
750
751}
752
753
754
755
756
757
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000758//----------------------------------------------------------------------------
759// We can use the following method to get a temporary register to be used
760// BEFORE any given machine instruction. If there is a register available,
761// this method will simply return that register and set MIBef = MIAft = NULL.
762// Otherwise, it will return a register and MIAft and MIBef will contain
763// two instructions used to free up this returned register.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000764// Returned register number is the UNIFIED register number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000765//----------------------------------------------------------------------------
766
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000767int PhyRegAlloc::getUsableUniRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000768 const int RegType,
769 const MachineInstr *MInst,
770 const LiveVarSet *LVSetBef,
771 MachineInstr *MIBef,
772 MachineInstr *MIAft) {
773
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000774 int RegU = getUnusedUniRegAtMI(RC, MInst, LVSetBef);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000775
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000776
777 if( RegU != -1) {
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000778 // we found an unused register, so we can simply use it
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000779 MIBef = MIAft = NULL;
780 }
781 else {
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000782 // we couldn't find an unused register. Generate code to free up a reg by
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000783 // saving it on stack and restoring after the instruction
784
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000785 int TmpOff = mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
Vikram S. Adve12af1642001-11-08 04:48:50 +0000786
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000787 RegU = getUniRegNotUsedByThisInst(RC, MInst);
788 MIBef = MRI.cpReg2MemMI(RegU, MRI.getFramePointer(), TmpOff, RegType );
789 MIAft = MRI.cpMem2RegMI(MRI.getFramePointer(), TmpOff, RegU, RegType );
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000790 }
791
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000792 return RegU;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000793}
794
795//----------------------------------------------------------------------------
796// This method is called to get a new unused register that can be used to
797// accomodate a spilled value.
798// This method may be called several times for a single machine instruction
799// if it contains many spilled operands. Each time it is called, it finds
800// a register which is not live at that instruction and also which is not
801// used by other spilled operands of the same instruction.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000802// Return register number is relative to the register class. NOT
803// unified number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000804//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000805int PhyRegAlloc::getUnusedUniRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000806 const MachineInstr *MInst,
807 const LiveVarSet *LVSetBef) {
808
809 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
810
811 bool *IsColorUsedArr = RC->getIsColorUsedArr();
812
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000813 for(unsigned i=0; i < NumAvailRegs; i++) // Reset array
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000814 IsColorUsedArr[i] = false;
815
816 LiveVarSet::const_iterator LIt = LVSetBef->begin();
817
818 // for each live var in live variable set after machine inst
819 for( ; LIt != LVSetBef->end(); ++LIt) {
820
821 // get the live range corresponding to live var
822 LiveRange *const LRofLV = LRI.getLiveRangeForValue(*LIt );
823
824 // LR can be null if it is a const since a const
825 // doesn't have a dominating def - see Assumptions above
826 if( LRofLV )
827 if( LRofLV->hasColor() )
828 IsColorUsedArr[ LRofLV->getColor() ] = true;
829 }
830
831 // It is possible that one operand of this MInst was already spilled
832 // and it received some register temporarily. If that's the case,
833 // it is recorded in machine operand. We must skip such registers.
834
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000835 setRelRegsUsedByThisInst(RC, MInst);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000836
837 unsigned c; // find first unused color
838 for( c=0; c < NumAvailRegs; c++)
839 if( ! IsColorUsedArr[ c ] ) break;
840
841 if(c < NumAvailRegs)
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000842 return MRI.getUnifiedRegNum(RC->getID(), c);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000843 else
844 return -1;
845
846
847}
848
849
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000850//----------------------------------------------------------------------------
851// Get any other register in a register class, other than what is used
852// by operands of a machine instruction. Returns the unified reg number.
853//----------------------------------------------------------------------------
854int PhyRegAlloc::getUniRegNotUsedByThisInst(RegClass *RC,
855 const MachineInstr *MInst) {
856
857 bool *IsColorUsedArr = RC->getIsColorUsedArr();
858 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
859
860
861 for(unsigned i=0; i < NumAvailRegs ; i++) // Reset array
862 IsColorUsedArr[i] = false;
863
864 setRelRegsUsedByThisInst(RC, MInst);
865
866 unsigned c; // find first unused color
867 for( c=0; c < RC->getNumOfAvailRegs(); c++)
868 if( ! IsColorUsedArr[ c ] ) break;
869
870 if(c < NumAvailRegs)
871 return MRI.getUnifiedRegNum(RC->getID(), c);
872 else
873 assert( 0 && "FATAL: No free register could be found in reg class!!");
Chris Lattner697954c2002-01-20 22:54:45 +0000874 return 0;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000875}
876
877
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000878//----------------------------------------------------------------------------
879// This method modifies the IsColorUsedArr of the register class passed to it.
880// It sets the bits corresponding to the registers used by this machine
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000881// instructions. Both explicit and implicit operands are set.
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000882//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000883void PhyRegAlloc::setRelRegsUsedByThisInst(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000884 const MachineInstr *MInst ) {
885
886 bool *IsColorUsedArr = RC->getIsColorUsedArr();
887
888 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
889
890 const MachineOperand& Op = MInst->getOperand(OpNum);
891
892 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000893 Op.getOperandType() == MachineOperand::MO_CCRegister ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000894
895 const Value *const Val = Op.getVRegValue();
896
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000897 if( Val )
898 if( MRI.getRegClassIDOfValue(Val) == RC->getID() ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000899 int Reg;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000900 if( (Reg=Op.getAllocatedRegNum()) != -1) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000901 IsColorUsedArr[ Reg ] = true;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000902 }
903 else {
904 // it is possilbe that this operand still is not marked with
905 // a register but it has a LR and that received a color
906
907 LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
908 if( LROfVal)
909 if( LROfVal->hasColor() )
910 IsColorUsedArr[ LROfVal->getColor() ] = true;
911 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000912
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000913 } // if reg classes are the same
914 }
915 else if (Op.getOperandType() == MachineOperand::MO_MachineRegister) {
916 IsColorUsedArr[ Op.getMachineRegNum() ] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000917 }
918 }
919
920 // If there are implicit references, mark them as well
921
922 for(unsigned z=0; z < MInst->getNumImplicitRefs(); z++) {
923
924 LiveRange *const LRofImpRef =
925 LRI.getLiveRangeForValue( MInst->getImplicitRef(z) );
Chris Lattner697954c2002-01-20 22:54:45 +0000926
927 if(LRofImpRef && LRofImpRef->hasColor())
928 IsColorUsedArr[LRofImpRef->getColor()] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000929 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000930}
931
932
933
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000934
935
936
937
938
939//----------------------------------------------------------------------------
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000940// If there are delay slots for an instruction, the instructions
941// added after it must really go after the delayed instruction(s).
942// So, we move the InstrAfter of that instruction to the
943// corresponding delayed instruction using the following method.
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000944
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000945//----------------------------------------------------------------------------
946void PhyRegAlloc:: move2DelayedInstr(const MachineInstr *OrigMI,
947 const MachineInstr *DelayedMI) {
948
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000949 // "added after" instructions of the original instr
Chris Lattner697954c2002-01-20 22:54:45 +0000950 std::deque<MachineInstr *> &OrigAft = AddedInstrMap[OrigMI]->InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000951
952 // "added instructions" of the delayed instr
953 AddedInstrns *DelayAdI = AddedInstrMap[DelayedMI];
954
955 if(! DelayAdI ) { // create a new "added after" if necessary
956 DelayAdI = new AddedInstrns();
957 AddedInstrMap[DelayedMI] = DelayAdI;
958 }
959
960 // "added after" instructions of the delayed instr
Chris Lattner697954c2002-01-20 22:54:45 +0000961 std::deque<MachineInstr *> &DelayedAft = DelayAdI->InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000962
963 // go thru all the "added after instructions" of the original instruction
964 // and append them to the "addded after instructions" of the delayed
965 // instructions
Chris Lattner697954c2002-01-20 22:54:45 +0000966 DelayedAft.insert(DelayedAft.end(), OrigAft.begin(), OrigAft.end());
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000967
968 // empty the "added after instructions" of the original instruction
969 OrigAft.clear();
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000970}
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000971
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000972//----------------------------------------------------------------------------
973// This method prints the code with registers after register allocation is
974// complete.
975//----------------------------------------------------------------------------
976void PhyRegAlloc::printMachineCode()
977{
978
Chris Lattner697954c2002-01-20 22:54:45 +0000979 cerr << "\n;************** Method " << Meth->getName()
980 << " *****************\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000981
982 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
983
984 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
985
Chris Lattner697954c2002-01-20 22:54:45 +0000986 cerr << "\n"; printLabel( *BBI); cerr << ": ";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000987
988 // get the iterator for machine instructions
989 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
990 MachineCodeForBasicBlock::iterator MInstIterator = MIVec.begin();
991
992 // iterate over all the machine instructions in BB
993 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
994
995 MachineInstr *const MInst = *MInstIterator;
996
997
Chris Lattner697954c2002-01-20 22:54:45 +0000998 cerr << "\n\t";
999 cerr << TargetInstrDescriptors[MInst->getOpCode()].opCodeString;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001000
1001
Chris Lattner7a176752001-12-04 00:03:30 +00001002 //for(MachineInstr::val_const_op_iterator OpI(MInst);!OpI.done();++OpI) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001003
1004 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
1005
1006 MachineOperand& Op = MInst->getOperand(OpNum);
1007
1008 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasanka97b8b442001-10-18 22:36:26 +00001009 Op.getOperandType() == MachineOperand::MO_CCRegister /*||
1010 Op.getOperandType() == MachineOperand::MO_PCRelativeDisp*/ ) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001011
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001012 const Value *const Val = Op.getVRegValue () ;
Ruchira Sasankae727f852001-09-18 22:43:57 +00001013 // ****this code is temporary till NULL Values are fixed
1014 if( ! Val ) {
Chris Lattner697954c2002-01-20 22:54:45 +00001015 cerr << "\t<*NULL*>";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001016 continue;
1017 }
Ruchira Sasankae727f852001-09-18 22:43:57 +00001018
1019 // if a label or a constant
Chris Lattnerdbe53042002-01-21 01:33:12 +00001020 if(isa<BasicBlock>(Val)) {
Chris Lattner697954c2002-01-20 22:54:45 +00001021 cerr << "\t"; printLabel( Op.getVRegValue () );
1022 } else {
Ruchira Sasankae727f852001-09-18 22:43:57 +00001023 // else it must be a register value
1024 const int RegNum = Op.getAllocatedRegNum();
1025
Chris Lattner697954c2002-01-20 22:54:45 +00001026 cerr << "\t" << "%" << MRI.getUnifiedRegName( RegNum );
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001027 if (Val->hasName() )
Chris Lattner697954c2002-01-20 22:54:45 +00001028 cerr << "(" << Val->getName() << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001029 else
Chris Lattner697954c2002-01-20 22:54:45 +00001030 cerr << "(" << Val << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001031
1032 if( Op.opIsDef() )
Chris Lattner697954c2002-01-20 22:54:45 +00001033 cerr << "*";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001034
1035 const LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
1036 if( LROfVal )
1037 if( LROfVal->hasSpillOffset() )
Chris Lattner697954c2002-01-20 22:54:45 +00001038 cerr << "$";
Ruchira Sasankae727f852001-09-18 22:43:57 +00001039 }
1040
1041 }
1042 else if(Op.getOperandType() == MachineOperand::MO_MachineRegister) {
Chris Lattner697954c2002-01-20 22:54:45 +00001043 cerr << "\t" << "%" << MRI.getUnifiedRegName(Op.getMachineRegNum());
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001044 }
1045
1046 else
Chris Lattner697954c2002-01-20 22:54:45 +00001047 cerr << "\t" << Op; // use dump field
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001048 }
1049
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001050
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001051
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001052 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
1053 if( NumOfImpRefs > 0 ) {
1054
Chris Lattner697954c2002-01-20 22:54:45 +00001055 cerr << "\tImplicit:";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001056
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001057 for(unsigned z=0; z < NumOfImpRefs; z++) {
1058 printValue( MInst->getImplicitRef(z) );
Chris Lattner697954c2002-01-20 22:54:45 +00001059 cerr << "\t";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001060 }
1061
1062 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001063
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001064 } // for all machine instructions
1065
Chris Lattner697954c2002-01-20 22:54:45 +00001066 cerr << "\n";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001067
1068 } // for all BBs
1069
Chris Lattner697954c2002-01-20 22:54:45 +00001070 cerr << "\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001071}
1072
Ruchira Sasankae727f852001-09-18 22:43:57 +00001073
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001074#if 0
1075
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001076//----------------------------------------------------------------------------
1077//
1078//----------------------------------------------------------------------------
1079
1080void PhyRegAlloc::colorCallRetArgs()
1081{
1082
1083 CallRetInstrListType &CallRetInstList = LRI.getCallRetInstrList();
1084 CallRetInstrListType::const_iterator It = CallRetInstList.begin();
1085
1086 for( ; It != CallRetInstList.end(); ++It ) {
1087
Ruchira Sasankaa90e7702001-10-15 16:26:38 +00001088 const MachineInstr *const CRMI = *It;
1089 unsigned OpCode = CRMI->getOpCode();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001090
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001091 // get the added instructions for this Call/Ret instruciton
1092 AddedInstrns *AI = AddedInstrMap[ CRMI ];
1093 if ( !AI ) {
1094 AI = new AddedInstrns();
1095 AddedInstrMap[ CRMI ] = AI;
1096 }
1097
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001098 // Tmp stack poistions are needed by some calls that have spilled args
1099 // So reset it before we call each such method
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001100 //mcInfo.popAllTempValues(TM);
1101
1102
Vikram S. Adve12af1642001-11-08 04:48:50 +00001103
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001104 if (TM.getInstrInfo().isCall(OpCode))
1105 MRI.colorCallArgs(CRMI, LRI, AI, *this);
1106 else if (TM.getInstrInfo().isReturn(OpCode))
Ruchira Sasankaa90e7702001-10-15 16:26:38 +00001107 MRI.colorRetValue( CRMI, LRI, AI );
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001108 else
1109 assert(0 && "Non Call/Ret instrn in CallRetInstrList\n");
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001110 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001111}
1112
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001113#endif
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001114
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001115//----------------------------------------------------------------------------
1116
1117//----------------------------------------------------------------------------
1118void PhyRegAlloc::colorIncomingArgs()
1119{
1120 const BasicBlock *const FirstBB = Meth->front();
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001121 const MachineInstr *FirstMI = FirstBB->getMachineInstrVec().front();
1122 assert(FirstMI && "No machine instruction in entry BB");
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001123
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001124 AddedInstrns *AI = AddedInstrMap[FirstMI];
1125 if (!AI)
1126 AddedInstrMap[FirstMI] = AI = new AddedInstrns();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001127
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001128 MRI.colorMethodArgs(Meth, LRI, AI);
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001129}
1130
Ruchira Sasankae727f852001-09-18 22:43:57 +00001131
1132//----------------------------------------------------------------------------
1133// Used to generate a label for a basic block
1134//----------------------------------------------------------------------------
Chris Lattner697954c2002-01-20 22:54:45 +00001135void PhyRegAlloc::printLabel(const Value *const Val) {
1136 if (Val->hasName())
1137 cerr << Val->getName();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001138 else
Chris Lattner697954c2002-01-20 22:54:45 +00001139 cerr << "Label" << Val;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001140}
1141
1142
Ruchira Sasankae727f852001-09-18 22:43:57 +00001143//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001144// This method calls setSugColorUsable method of each live range. This
1145// will determine whether the suggested color of LR is really usable.
1146// A suggested color is not usable when the suggested color is volatile
1147// AND when there are call interferences
1148//----------------------------------------------------------------------------
1149
1150void PhyRegAlloc::markUnusableSugColors()
1151{
Chris Lattner697954c2002-01-20 22:54:45 +00001152 if(DEBUG_RA ) cerr << "\nmarking unusable suggested colors ...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001153
1154 // hash map iterator
1155 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
1156 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
1157
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001158 for(; HMI != HMIEnd ; ++HMI ) {
1159 if (HMI->first) {
1160 LiveRange *L = HMI->second; // get the LiveRange
1161 if (L) {
1162 if(L->hasSuggestedColor()) {
1163 int RCID = L->getRegClass()->getID();
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001164 if( MRI.isRegVolatile( RCID, L->getSuggestedColor()) &&
1165 L->isCallInterference() )
1166 L->setSuggestedColorUsable( false );
1167 else
1168 L->setSuggestedColorUsable( true );
1169 }
1170 } // if L->hasSuggestedColor()
1171 }
1172 } // for all LR's in hash map
1173}
1174
1175
1176
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001177//----------------------------------------------------------------------------
1178// The following method will set the stack offsets of the live ranges that
1179// are decided to be spillled. This must be called just after coloring the
1180// LRs using the graph coloring algo. For each live range that is spilled,
1181// this method allocate a new spill position on the stack.
1182//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001183
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001184void PhyRegAlloc::allocateStackSpace4SpilledLRs()
1185{
Chris Lattner697954c2002-01-20 22:54:45 +00001186 if(DEBUG_RA ) cerr << "\nsetting LR stack offsets ...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001187
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001188 // hash map iterator
1189 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
1190 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
1191
1192 for( ; HMI != HMIEnd ; ++HMI ) {
Chris Lattner697954c2002-01-20 22:54:45 +00001193 if(HMI->first && HMI->second) {
1194 LiveRange *L = HMI->second; // get the LiveRange
1195 if( ! L->hasColor() )
1196 // NOTE: ** allocating the size of long Type **
1197 L->setSpillOffFromFP(mcInfo.allocateSpilledValue(TM, Type::LongTy));
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001198 }
1199 } // for all LR's in hash map
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001200}
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001201
1202
1203
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001204//----------------------------------------------------------------------------
Ruchira Sasankae727f852001-09-18 22:43:57 +00001205// The entry pont to Register Allocation
1206//----------------------------------------------------------------------------
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001207
1208void PhyRegAlloc::allocateRegisters()
1209{
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001210
1211 // make sure that we put all register classes into the RegClassList
1212 // before we call constructLiveRanges (now done in the constructor of
1213 // PhyRegAlloc class).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001214 //
1215 LRI.constructLiveRanges(); // create LR info
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001216
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001217 if (DEBUG_RA)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001218 LRI.printLiveRanges();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001219
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001220 createIGNodeListsAndIGs(); // create IGNode list and IGs
1221
1222 buildInterferenceGraphs(); // build IGs in all reg classes
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001223
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001224
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001225 if (DEBUG_RA) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001226 // print all LRs in all reg classes
1227 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1228 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001229
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001230 // print IGs in all register classes
1231 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1232 RegClassList[ rc ]->printIG();
1233 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001234
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001235
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001236 LRI.coalesceLRs(); // coalesce all live ranges
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001237
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +00001238
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001239 if( DEBUG_RA) {
1240 // print all LRs in all reg classes
1241 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1242 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001243
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001244 // print IGs in all register classes
1245 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1246 RegClassList[ rc ]->printIG();
1247 }
1248
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001249
1250 // mark un-usable suggested color before graph coloring algorithm.
1251 // When this is done, the graph coloring algo will not reserve
1252 // suggested color unnecessarily - they can be used by another LR
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001253 //
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001254 markUnusableSugColors();
1255
1256 // color all register classes using the graph coloring algo
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001257 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1258 RegClassList[ rc ]->colorAllRegs();
1259
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001260 // Atter grpah coloring, if some LRs did not receive a color (i.e, spilled)
1261 // a poistion for such spilled LRs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001262 //
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001263 allocateStackSpace4SpilledLRs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001264
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001265 mcInfo.popAllTempValues(TM); // TODO **Check
1266
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001267 // color incoming args - if the correct color was not received
1268 // insert code to copy to the correct register
1269 //
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001270 colorIncomingArgs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001271
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001272 // Now update the machine code with register names and add any
1273 // additional code inserted by the register allocator to the instruction
1274 // stream
1275 //
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001276 updateMachineCode();
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001277
Chris Lattner045e7c82001-09-19 16:26:23 +00001278 if (DEBUG_RA) {
Vikram S. Adve12af1642001-11-08 04:48:50 +00001279 MachineCodeForMethod::get(Meth).dump();
Chris Lattner045e7c82001-09-19 16:26:23 +00001280 printMachineCode(); // only for DEBUGGING
1281 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001282}
1283
Ruchira Sasankae727f852001-09-18 22:43:57 +00001284
1285