blob: 90de8b969f1e386e65fbb8d7cb0c37485c823e83 [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) {
Chris Lattner0665a5f2002-02-05 01:43:49 +0000121 cerr << "\n*?!?Warning: Null liver range found for: "
122 << RAV(HMI->first) << "\n";
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000123 }
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
Chris Lattner0665a5f2002-02-05 01:43:49 +0000173 if (DEBUG_RA > 1)
174 cerr << "< Def=" << RAV(Def) << ", Lvar=" << RAV(*LIt) << "> ";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000175
176 // get the live range corresponding to live var
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000177 //
Chris Lattner0665a5f2002-02-05 01:43:49 +0000178 LiveRange *LROfVar = LRI.getLiveRangeForValue(*LIt);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000179
180 // LROfVar can be null if it is a const since a const
181 // doesn't have a dominating def - see Assumptions above
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000182 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000183 if (LROfVar) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000184 if(LROfDef == LROfVar) // do not set interf for same LR
185 continue;
186
187 // if 2 reg classes are the same set interference
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000188 //
Chris Lattner0665a5f2002-02-05 01:43:49 +0000189 if (RCOfDef == LROfVar->getRegClass()) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000190 RCOfDef->setInterference( LROfDef, LROfVar);
Chris Lattner0665a5f2002-02-05 01:43:49 +0000191 } else if (DEBUG_RA > 1) {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000192 // we will not have LRs for values not explicitly allocated in the
193 // instruction stream (e.g., constants)
Chris Lattner0665a5f2002-02-05 01:43:49 +0000194 cerr << " warning: no live range for " << RAV(*LIt) << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000195 }
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000196 }
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000197 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000198}
199
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000200
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000201
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000202//----------------------------------------------------------------------------
203// For a call instruction, this method sets the CallInterference flag in
204// the LR of each variable live int the Live Variable Set live after the
205// call instruction (except the return value of the call instruction - since
206// the return value does not interfere with that call itself).
207//----------------------------------------------------------------------------
208
209void PhyRegAlloc::setCallInterferences(const MachineInstr *MInst,
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000210 const LiveVarSet *const LVSetAft ) {
211
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000212 // Now find the LR of the return value of the call
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000213 // We do this because, we look at the LV set *after* the instruction
214 // to determine, which LRs must be saved across calls. The return value
215 // of the call is live in this set - but it does not interfere with call
216 // (i.e., we can allocate a volatile register to the return value)
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000217 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000218 LiveRange *RetValLR = NULL;
Ruchira Sasankab3b6f532001-10-21 16:43:41 +0000219 const Value *RetVal = MRI.getCallInstRetVal( MInst );
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000220
Ruchira Sasankab3b6f532001-10-21 16:43:41 +0000221 if( RetVal ) {
222 RetValLR = LRI.getLiveRangeForValue( RetVal );
223 assert( RetValLR && "No LR for RetValue of call");
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000224 }
225
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000226 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000227 cerr << "\n For call inst: " << *MInst;
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000228
229 LiveVarSet::const_iterator LIt = LVSetAft->begin();
230
231 // for each live var in live variable set after machine inst
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000232 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000233 for( ; LIt != LVSetAft->end(); ++LIt) {
234
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000235 // get the live range corresponding to live var
236 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000237 LiveRange *const LR = LRI.getLiveRangeForValue(*LIt );
238
239 if( LR && DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000240 cerr << "\n\tLR Aft Call: ";
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000241 LR->printSet();
242 }
243
244
245 // LR can be null if it is a const since a const
246 // doesn't have a dominating def - see Assumptions above
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000247 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000248 if( LR && (LR != RetValLR) ) {
249 LR->setCallInterference();
250 if( DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000251 cerr << "\n ++Added call interf for LR: " ;
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000252 LR->printSet();
253 }
254 }
255
256 }
257
258}
259
260
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000261
262
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000263//----------------------------------------------------------------------------
264// This method will walk thru code and create interferences in the IG of
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000265// each RegClass. Also, this method calculates the spill cost of each
266// Live Range (it is done in this method to save another pass over the code).
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000267//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000268void PhyRegAlloc::buildInterferenceGraphs()
269{
270
Chris Lattner697954c2002-01-20 22:54:45 +0000271 if(DEBUG_RA) cerr << "Creating interference graphs ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000272
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000273 unsigned BBLoopDepthCost;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000274 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
275
276 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
277
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000278 // find the 10^(loop_depth) of this BB
279 //
Chris Lattner4911c352002-02-04 17:39:42 +0000280 BBLoopDepthCost = (unsigned) pow( 10.0, LoopDepthCalc->getLoopDepth(*BBI));
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000281
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000282 // get the iterator for machine instructions
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000283 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000284 const MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
285 MachineCodeForBasicBlock::const_iterator
286 MInstIterator = MIVec.begin();
287
288 // iterate over all the machine instructions in BB
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000289 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000290 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000291
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +0000292 const MachineInstr * MInst = *MInstIterator;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000293
294 // get the LV set after the instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000295 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000296 const LiveVarSet *const LVSetAI =
297 LVI->getLiveVarSetAfterMInst(MInst, *BBI);
298
299 const bool isCallInst = TM.getInstrInfo().isCall(MInst->getOpCode());
300
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000301 if( isCallInst ) {
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000302 // set the isCallInterference flag of each live range wich extends
303 // accross this call instruction. This information is used by graph
304 // coloring algo to avoid allocating volatile colors to live ranges
305 // that span across calls (since they have to be saved/restored)
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000306 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000307 setCallInterferences( MInst, LVSetAI);
308 }
309
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000310
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000311 // iterate over all MI operands to find defs
312 //
Chris Lattner7a176752001-12-04 00:03:30 +0000313 for( MachineInstr::val_const_op_iterator OpI(MInst);!OpI.done(); ++OpI) {
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000314
315 if( OpI.isDef() ) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000316 // create a new LR iff this operand is a def
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000317 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000318 addInterference(*OpI, LVSetAI, isCallInst );
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000319 }
320
321 // Calculate the spill cost of each live range
322 //
323 LiveRange *LR = LRI.getLiveRangeForValue( *OpI );
324 if( LR )
325 LR->addSpillCost(BBLoopDepthCost);
326 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000327
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000328
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000329 // if there are multiple defs in this instruction e.g. in SETX
330 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000331 if (TM.getInstrInfo().isPseudoInstr(MInst->getOpCode()))
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000332 addInterf4PseudoInstr(MInst);
333
334
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000335 // Also add interference for any implicit definitions in a machine
336 // instr (currently, only calls have this).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000337 //
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000338 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
339 if( NumOfImpRefs > 0 ) {
340 for(unsigned z=0; z < NumOfImpRefs; z++)
341 if( MInst->implicitRefIsDefined(z) )
342 addInterference( MInst->getImplicitRef(z), LVSetAI, isCallInst );
343 }
344
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +0000345
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000346 } // for all machine instructions in BB
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000347
348 } // for all BBs in method
349
350
351 // add interferences for method arguments. Since there are no explict
352 // defs in method for args, we have to add them manually
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000353 //
354 addInterferencesForArgs();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000355
356 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000357 cerr << "Interference graphs calculted!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000358
359}
360
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000361
362
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000363//--------------------------------------------------------------------------
364// Pseudo instructions will be exapnded to multiple instructions by the
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000365// assembler. Consequently, all the opernds must get distinct registers.
366// Therefore, we mark all operands of a pseudo instruction as they interfere
367// with one another.
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000368//--------------------------------------------------------------------------
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000369void PhyRegAlloc::addInterf4PseudoInstr(const MachineInstr *MInst) {
370
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000371 bool setInterf = false;
372
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000373 // iterate over MI operands to find defs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000374 //
Chris Lattner7a176752001-12-04 00:03:30 +0000375 for( MachineInstr::val_const_op_iterator It1(MInst);!It1.done(); ++It1) {
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000376
377 const LiveRange *const LROfOp1 = LRI.getLiveRangeForValue( *It1 );
378
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000379 if( !LROfOp1 && It1.isDef() )
380 assert( 0 && "No LR for Def in PSEUDO insruction");
381
Chris Lattner7a176752001-12-04 00:03:30 +0000382 MachineInstr::val_const_op_iterator It2 = It1;
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000383 ++It2;
384
385 for( ; !It2.done(); ++It2) {
386
387 const LiveRange *const LROfOp2 = LRI.getLiveRangeForValue( *It2 );
388
389 if( LROfOp2) {
390
391 RegClass *const RCOfOp1 = LROfOp1->getRegClass();
392 RegClass *const RCOfOp2 = LROfOp2->getRegClass();
393
394 if( RCOfOp1 == RCOfOp2 ){
395 RCOfOp1->setInterference( LROfOp1, LROfOp2 );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000396 setInterf = true;
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000397 }
398
399 } // if Op2 has a LR
400
401 } // for all other defs in machine instr
402
403 } // for all operands in an instruction
404
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000405 if( !setInterf && (MInst->getNumOperands() > 2) ) {
406 cerr << "\nInterf not set for any operand in pseudo instr:\n";
407 cerr << *MInst;
408 assert(0 && "Interf not set for pseudo instr with > 2 operands" );
409
410 }
411
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000412}
413
414
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000415
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000416//----------------------------------------------------------------------------
417// This method will add interferences for incoming arguments to a method.
418//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000419void PhyRegAlloc::addInterferencesForArgs()
420{
421 // get the InSet of root BB
422 const LiveVarSet *const InSet = LVI->getInSetOfBB( Meth->front() );
423
424 // get the argument list
425 const Method::ArgumentListType& ArgList = Meth->getArgumentList();
426
427 // get an iterator to arg list
428 Method::ArgumentListType::const_iterator ArgIt = ArgList.begin();
429
430
431 for( ; ArgIt != ArgList.end() ; ++ArgIt) { // for each argument
Chris Lattner30adeb62002-02-04 16:36:59 +0000432 addInterference((Value*)*ArgIt, InSet, false); // add interferences between
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000433 // args and LVars at start
Chris Lattner0665a5f2002-02-05 01:43:49 +0000434 if( DEBUG_RA > 1)
435 cerr << " - %% adding interference for argument "
436 << RAV((const Value *)*ArgIt) << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000437 }
438}
439
440
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000441
442
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000443//----------------------------------------------------------------------------
444// This method is called after register allocation is complete to set the
445// allocated reisters in the machine code. This code will add register numbers
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000446// to MachineOperands that contain a Value. Also it calls target specific
447// methods to produce caller saving instructions. At the end, it adds all
448// additional instructions produced by the register allocator to the
449// instruction stream.
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000450//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000451void PhyRegAlloc::updateMachineCode()
452{
453
454 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
455
456 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
457
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000458 // get the iterator for machine instructions
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000459 //
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000460 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
461 MachineCodeForBasicBlock::iterator MInstIterator = MIVec.begin();
462
463 // iterate over all the machine instructions in BB
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000464 //
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000465 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
466
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000467 MachineInstr *MInst = *MInstIterator;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000468
469 unsigned Opcode = MInst->getOpCode();
470
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000471 // do not process Phis
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000472 if (TM.getInstrInfo().isPhi(Opcode))
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000473 continue;
474
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000475 // Now insert speical instructions (if necessary) for call/return
476 // instructions.
477 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000478 if (TM.getInstrInfo().isCall(Opcode) ||
479 TM.getInstrInfo().isReturn(Opcode)) {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000480
481 AddedInstrns *AI = AddedInstrMap[ MInst];
482 if ( !AI ) {
483 AI = new AddedInstrns();
484 AddedInstrMap[ MInst ] = AI;
485 }
486
487 // Tmp stack poistions are needed by some calls that have spilled args
488 // So reset it before we call each such method
Ruchira Sasanka6a3db8c2002-01-07 21:09:06 +0000489 //
490 mcInfo.popAllTempValues(TM);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000491
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000492 if (TM.getInstrInfo().isCall(Opcode))
493 MRI.colorCallArgs(MInst, LRI, AI, *this, *BBI);
494 else if (TM.getInstrInfo().isReturn(Opcode))
495 MRI.colorRetValue(MInst, LRI, AI);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000496 }
497
498
499 /* -- Using above code instead of this
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000500
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000501 // if this machine instr is call, insert caller saving code
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000502
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000503 if( (TM.getInstrInfo()).isCall( MInst->getOpCode()) )
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000504 MRI.insertCallerSavingCode(MInst, *BBI, *this );
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000505
506 */
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000507
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000508
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000509 // reset the stack offset for temporary variables since we may
510 // need that to spill
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000511 // mcInfo.popAllTempValues(TM);
Ruchira Sasankaf90870f2001-11-15 22:02:06 +0000512 // TODO ** : do later
Vikram S. Adve12af1642001-11-08 04:48:50 +0000513
Chris Lattner7a176752001-12-04 00:03:30 +0000514 //for(MachineInstr::val_const_op_iterator OpI(MInst);!OpI.done();++OpI) {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000515
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000516
517 // Now replace set the registers for operands in the machine instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000518 //
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000519 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
520
521 MachineOperand& Op = MInst->getOperand(OpNum);
522
523 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
524 Op.getOperandType() == MachineOperand::MO_CCRegister) {
525
526 const Value *const Val = Op.getVRegValue();
527
528 // delete this condition checking later (must assert if Val is null)
Chris Lattner045e7c82001-09-19 16:26:23 +0000529 if( !Val) {
530 if (DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000531 cerr << "Warning: NULL Value found for operand\n";
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000532 continue;
533 }
534 assert( Val && "Value is NULL");
535
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000536 LiveRange *const LR = LRI.getLiveRangeForValue(Val);
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000537
538 if ( !LR ) {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000539
540 // nothing to worry if it's a const or a label
541
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000542 if (DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000543 cerr << "*NO LR for operand : " << Op ;
544 cerr << " [reg:" << Op.getAllocatedRegNum() << "]";
545 cerr << " in inst:\t" << *MInst << "\n";
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000546 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000547
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000548 // if register is not allocated, mark register as invalid
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000549 if( Op.getAllocatedRegNum() == -1)
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000550 Op.setRegForValue( MRI.getInvalidRegNum());
Ruchira Sasankae727f852001-09-18 22:43:57 +0000551
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000552
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000553 continue;
554 }
555
556 unsigned RCID = (LR->getRegClass())->getID();
557
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000558 if( LR->hasColor() ) {
559 Op.setRegForValue( MRI.getUnifiedRegNum(RCID, LR->getColor()) );
560 }
561 else {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000562
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000563 // LR did NOT receive a color (register). Now, insert spill code
564 // for spilled opeands in this machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000565
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000566 //assert(0 && "LR must be spilled");
567 insertCode4SpilledLR(LR, MInst, *BBI, OpNum );
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000568
569 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000570 }
571
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000572 } // for each operand
573
574
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000575 // Now add instructions that the register allocator inserts before/after
576 // this machine instructions (done only for calls/rets/incoming args)
577 // We do this here, to ensure that spill for an instruction is inserted
578 // closest as possible to an instruction (see above insertCode4Spill...)
579 //
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000580 // If there are instructions to be added, *before* this machine
581 // instruction, add them now.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000582 //
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000583 if( AddedInstrMap[ MInst ] ) {
Chris Lattner697954c2002-01-20 22:54:45 +0000584 std::deque<MachineInstr *> &IBef = AddedInstrMap[MInst]->InstrnsBefore;
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000585
586 if( ! IBef.empty() ) {
Chris Lattner697954c2002-01-20 22:54:45 +0000587 std::deque<MachineInstr *>::iterator AdIt;
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000588
589 for( AdIt = IBef.begin(); AdIt != IBef.end() ; ++AdIt ) {
590
591 if( DEBUG_RA) {
592 cerr << "For inst " << *MInst;
Chris Lattner697954c2002-01-20 22:54:45 +0000593 cerr << " PREPENDed instr: " << **AdIt << "\n";
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000594 }
595
596 MInstIterator = MIVec.insert( MInstIterator, *AdIt );
597 ++MInstIterator;
598 }
599
600 }
601
602 }
603
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000604 // If there are instructions to be added *after* this machine
605 // instruction, add them now
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000606 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000607 if(AddedInstrMap[MInst] &&
608 !AddedInstrMap[MInst]->InstrnsAfter.empty() ) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000609
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000610 // if there are delay slots for this instruction, the instructions
611 // added after it must really go after the delayed instruction(s)
612 // So, we move the InstrAfter of the current instruction to the
613 // corresponding delayed instruction
614
615 unsigned delay;
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000616 if ((delay=TM.getInstrInfo().getNumDelaySlots(MInst->getOpCode())) >0){
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000617 move2DelayedInstr(MInst, *(MInstIterator+delay) );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000618
Chris Lattner697954c2002-01-20 22:54:45 +0000619 if(DEBUG_RA) cerr<< "\nMoved an added instr after the delay slot";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000620 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000621
622 else {
623
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000624
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000625 // Here we can add the "instructions after" to the current
626 // instruction since there are no delay slots for this instruction
627
Chris Lattner697954c2002-01-20 22:54:45 +0000628 std::deque<MachineInstr *> &IAft = AddedInstrMap[MInst]->InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000629
630 if( ! IAft.empty() ) {
631
Chris Lattner697954c2002-01-20 22:54:45 +0000632 std::deque<MachineInstr *>::iterator AdIt;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000633
634 ++MInstIterator; // advance to the next instruction
635
636 for( AdIt = IAft.begin(); AdIt != IAft.end() ; ++AdIt ) {
637
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000638 if(DEBUG_RA) {
639 cerr << "For inst " << *MInst;
Chris Lattner697954c2002-01-20 22:54:45 +0000640 cerr << " APPENDed instr: " << **AdIt << "\n";
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000641 }
642
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000643 MInstIterator = MIVec.insert( MInstIterator, *AdIt );
644 ++MInstIterator;
645 }
646
647 // MInsterator already points to the next instr. Since the
648 // for loop also increments it, decrement it to point to the
649 // instruction added last
650 --MInstIterator;
651
652 }
653
654 } // if not delay
655
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000656 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000657
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000658 } // for each machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000659 }
660}
661
662
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000663
664//----------------------------------------------------------------------------
665// This method inserts spill code for AN operand whose LR was spilled.
666// This method may be called several times for a single machine instruction
667// if it contains many spilled operands. Each time it is called, it finds
668// a register which is not live at that instruction and also which is not
669// used by other spilled operands of the same instruction. Then it uses
670// this register temporarily to accomodate the spilled value.
671//----------------------------------------------------------------------------
672void PhyRegAlloc::insertCode4SpilledLR(const LiveRange *LR,
673 MachineInstr *MInst,
674 const BasicBlock *BB,
675 const unsigned OpNum) {
676
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000677 assert(! TM.getInstrInfo().isCall(MInst->getOpCode()) &&
678 (! TM.getInstrInfo().isReturn(MInst->getOpCode())) &&
679 "Arg of a call/ret must be handled elsewhere");
680
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000681 MachineOperand& Op = MInst->getOperand(OpNum);
682 bool isDef = MInst->operandIsDefined(OpNum);
683 unsigned RegType = MRI.getRegType( LR );
684 int SpillOff = LR->getSpillOffFromFP();
685 RegClass *RC = LR->getRegClass();
686 const LiveVarSet *LVSetBef = LVI->getLiveVarSetBeforeMInst(MInst, BB);
Vikram S. Adve00521d72001-11-12 23:26:35 +0000687
Chris Lattner697954c2002-01-20 22:54:45 +0000688 mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000689
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000690 MachineInstr *MIBef=NULL, *AdIMid=NULL, *MIAft=NULL;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000691
692 int TmpRegU = getUsableUniRegAtMI(RC, RegType, MInst,LVSetBef, MIBef, MIAft);
693
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000694 // get the added instructions for this instruciton
695 AddedInstrns *AI = AddedInstrMap[ MInst ];
696 if ( !AI ) {
697 AI = new AddedInstrns();
698 AddedInstrMap[ MInst ] = AI;
699 }
700
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000701
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000702 if( !isDef ) {
703
704 // for a USE, we have to load the value of LR from stack to a TmpReg
705 // and use the TmpReg as one operand of instruction
706
707 // actual loading instruction
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000708 AdIMid = MRI.cpMem2RegMI(MRI.getFramePointer(), SpillOff, TmpRegU,RegType);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000709
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000710 if(MIBef)
711 AI->InstrnsBefore.push_back(MIBef);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000712
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000713 AI->InstrnsBefore.push_back(AdIMid);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000714
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000715 if(MIAft)
716 AI->InstrnsAfter.push_front(MIAft);
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000717
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000718
719 }
720 else { // if this is a Def
721
722 // for a DEF, we have to store the value produced by this instruction
723 // on the stack position allocated for this LR
724
725 // actual storing instruction
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000726 AdIMid = MRI.cpReg2MemMI(TmpRegU, MRI.getFramePointer(), SpillOff,RegType);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000727
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000728 if (MIBef)
729 AI->InstrnsBefore.push_back(MIBef);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000730
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000731 AI->InstrnsAfter.push_front(AdIMid);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000732
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000733 if (MIAft)
734 AI->InstrnsAfter.push_front(MIAft);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000735
736 } // if !DEF
737
738 cerr << "\nFor Inst " << *MInst;
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000739 cerr << " - SPILLED LR: "; LR->printSet();
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000740 cerr << "\n - Added Instructions:";
741 if( MIBef ) cerr << *MIBef;
742 cerr << *AdIMid;
743 if( MIAft ) cerr << *MIAft;
744
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000745 Op.setRegForValue( TmpRegU ); // set the opearnd
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000746
747
748}
749
750
751
752
753
754
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000755//----------------------------------------------------------------------------
756// We can use the following method to get a temporary register to be used
757// BEFORE any given machine instruction. If there is a register available,
758// this method will simply return that register and set MIBef = MIAft = NULL.
759// Otherwise, it will return a register and MIAft and MIBef will contain
760// two instructions used to free up this returned register.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000761// Returned register number is the UNIFIED register number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000762//----------------------------------------------------------------------------
763
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000764int PhyRegAlloc::getUsableUniRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000765 const int RegType,
766 const MachineInstr *MInst,
767 const LiveVarSet *LVSetBef,
768 MachineInstr *MIBef,
769 MachineInstr *MIAft) {
770
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000771 int RegU = getUnusedUniRegAtMI(RC, MInst, LVSetBef);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000772
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000773
774 if( RegU != -1) {
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000775 // we found an unused register, so we can simply use it
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000776 MIBef = MIAft = NULL;
777 }
778 else {
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000779 // we couldn't find an unused register. Generate code to free up a reg by
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000780 // saving it on stack and restoring after the instruction
781
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000782 int TmpOff = mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
Vikram S. Adve12af1642001-11-08 04:48:50 +0000783
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000784 RegU = getUniRegNotUsedByThisInst(RC, MInst);
785 MIBef = MRI.cpReg2MemMI(RegU, MRI.getFramePointer(), TmpOff, RegType );
786 MIAft = MRI.cpMem2RegMI(MRI.getFramePointer(), TmpOff, RegU, RegType );
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000787 }
788
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000789 return RegU;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000790}
791
792//----------------------------------------------------------------------------
793// This method is called to get a new unused register that can be used to
794// accomodate a spilled value.
795// This method may be called several times for a single machine instruction
796// if it contains many spilled operands. Each time it is called, it finds
797// a register which is not live at that instruction and also which is not
798// used by other spilled operands of the same instruction.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000799// Return register number is relative to the register class. NOT
800// unified number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000801//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000802int PhyRegAlloc::getUnusedUniRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000803 const MachineInstr *MInst,
804 const LiveVarSet *LVSetBef) {
805
806 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
807
808 bool *IsColorUsedArr = RC->getIsColorUsedArr();
809
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000810 for(unsigned i=0; i < NumAvailRegs; i++) // Reset array
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000811 IsColorUsedArr[i] = false;
812
813 LiveVarSet::const_iterator LIt = LVSetBef->begin();
814
815 // for each live var in live variable set after machine inst
816 for( ; LIt != LVSetBef->end(); ++LIt) {
817
818 // get the live range corresponding to live var
819 LiveRange *const LRofLV = LRI.getLiveRangeForValue(*LIt );
820
821 // LR can be null if it is a const since a const
822 // doesn't have a dominating def - see Assumptions above
823 if( LRofLV )
824 if( LRofLV->hasColor() )
825 IsColorUsedArr[ LRofLV->getColor() ] = true;
826 }
827
828 // It is possible that one operand of this MInst was already spilled
829 // and it received some register temporarily. If that's the case,
830 // it is recorded in machine operand. We must skip such registers.
831
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000832 setRelRegsUsedByThisInst(RC, MInst);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000833
834 unsigned c; // find first unused color
835 for( c=0; c < NumAvailRegs; c++)
836 if( ! IsColorUsedArr[ c ] ) break;
837
838 if(c < NumAvailRegs)
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000839 return MRI.getUnifiedRegNum(RC->getID(), c);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000840 else
841 return -1;
842
843
844}
845
846
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000847//----------------------------------------------------------------------------
848// Get any other register in a register class, other than what is used
849// by operands of a machine instruction. Returns the unified reg number.
850//----------------------------------------------------------------------------
851int PhyRegAlloc::getUniRegNotUsedByThisInst(RegClass *RC,
852 const MachineInstr *MInst) {
853
854 bool *IsColorUsedArr = RC->getIsColorUsedArr();
855 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
856
857
858 for(unsigned i=0; i < NumAvailRegs ; i++) // Reset array
859 IsColorUsedArr[i] = false;
860
861 setRelRegsUsedByThisInst(RC, MInst);
862
863 unsigned c; // find first unused color
864 for( c=0; c < RC->getNumOfAvailRegs(); c++)
865 if( ! IsColorUsedArr[ c ] ) break;
866
867 if(c < NumAvailRegs)
868 return MRI.getUnifiedRegNum(RC->getID(), c);
869 else
870 assert( 0 && "FATAL: No free register could be found in reg class!!");
Chris Lattner697954c2002-01-20 22:54:45 +0000871 return 0;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000872}
873
874
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000875//----------------------------------------------------------------------------
876// This method modifies the IsColorUsedArr of the register class passed to it.
877// It sets the bits corresponding to the registers used by this machine
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000878// instructions. Both explicit and implicit operands are set.
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000879//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000880void PhyRegAlloc::setRelRegsUsedByThisInst(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000881 const MachineInstr *MInst ) {
882
883 bool *IsColorUsedArr = RC->getIsColorUsedArr();
884
885 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
886
887 const MachineOperand& Op = MInst->getOperand(OpNum);
888
889 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000890 Op.getOperandType() == MachineOperand::MO_CCRegister ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000891
892 const Value *const Val = Op.getVRegValue();
893
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000894 if( Val )
895 if( MRI.getRegClassIDOfValue(Val) == RC->getID() ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000896 int Reg;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000897 if( (Reg=Op.getAllocatedRegNum()) != -1) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000898 IsColorUsedArr[ Reg ] = true;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000899 }
900 else {
901 // it is possilbe that this operand still is not marked with
902 // a register but it has a LR and that received a color
903
904 LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
905 if( LROfVal)
906 if( LROfVal->hasColor() )
907 IsColorUsedArr[ LROfVal->getColor() ] = true;
908 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000909
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000910 } // if reg classes are the same
911 }
912 else if (Op.getOperandType() == MachineOperand::MO_MachineRegister) {
913 IsColorUsedArr[ Op.getMachineRegNum() ] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000914 }
915 }
916
917 // If there are implicit references, mark them as well
918
919 for(unsigned z=0; z < MInst->getNumImplicitRefs(); z++) {
920
921 LiveRange *const LRofImpRef =
922 LRI.getLiveRangeForValue( MInst->getImplicitRef(z) );
Chris Lattner697954c2002-01-20 22:54:45 +0000923
924 if(LRofImpRef && LRofImpRef->hasColor())
925 IsColorUsedArr[LRofImpRef->getColor()] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000926 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000927}
928
929
930
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000931
932
933
934
935
936//----------------------------------------------------------------------------
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000937// If there are delay slots for an instruction, the instructions
938// added after it must really go after the delayed instruction(s).
939// So, we move the InstrAfter of that instruction to the
940// corresponding delayed instruction using the following method.
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000941
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000942//----------------------------------------------------------------------------
943void PhyRegAlloc:: move2DelayedInstr(const MachineInstr *OrigMI,
944 const MachineInstr *DelayedMI) {
945
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000946 // "added after" instructions of the original instr
Chris Lattner697954c2002-01-20 22:54:45 +0000947 std::deque<MachineInstr *> &OrigAft = AddedInstrMap[OrigMI]->InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000948
949 // "added instructions" of the delayed instr
950 AddedInstrns *DelayAdI = AddedInstrMap[DelayedMI];
951
952 if(! DelayAdI ) { // create a new "added after" if necessary
953 DelayAdI = new AddedInstrns();
954 AddedInstrMap[DelayedMI] = DelayAdI;
955 }
956
957 // "added after" instructions of the delayed instr
Chris Lattner697954c2002-01-20 22:54:45 +0000958 std::deque<MachineInstr *> &DelayedAft = DelayAdI->InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000959
960 // go thru all the "added after instructions" of the original instruction
961 // and append them to the "addded after instructions" of the delayed
962 // instructions
Chris Lattner697954c2002-01-20 22:54:45 +0000963 DelayedAft.insert(DelayedAft.end(), OrigAft.begin(), OrigAft.end());
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000964
965 // empty the "added after instructions" of the original instruction
966 OrigAft.clear();
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000967}
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000968
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000969//----------------------------------------------------------------------------
970// This method prints the code with registers after register allocation is
971// complete.
972//----------------------------------------------------------------------------
973void PhyRegAlloc::printMachineCode()
974{
975
Chris Lattner697954c2002-01-20 22:54:45 +0000976 cerr << "\n;************** Method " << Meth->getName()
977 << " *****************\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000978
979 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
980
981 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
982
Chris Lattner697954c2002-01-20 22:54:45 +0000983 cerr << "\n"; printLabel( *BBI); cerr << ": ";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000984
985 // get the iterator for machine instructions
986 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
987 MachineCodeForBasicBlock::iterator MInstIterator = MIVec.begin();
988
989 // iterate over all the machine instructions in BB
990 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
991
992 MachineInstr *const MInst = *MInstIterator;
993
994
Chris Lattner697954c2002-01-20 22:54:45 +0000995 cerr << "\n\t";
996 cerr << TargetInstrDescriptors[MInst->getOpCode()].opCodeString;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000997
998
Chris Lattner7a176752001-12-04 00:03:30 +0000999 //for(MachineInstr::val_const_op_iterator OpI(MInst);!OpI.done();++OpI) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001000
1001 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
1002
1003 MachineOperand& Op = MInst->getOperand(OpNum);
1004
1005 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasanka97b8b442001-10-18 22:36:26 +00001006 Op.getOperandType() == MachineOperand::MO_CCRegister /*||
1007 Op.getOperandType() == MachineOperand::MO_PCRelativeDisp*/ ) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001008
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001009 const Value *const Val = Op.getVRegValue () ;
Ruchira Sasankae727f852001-09-18 22:43:57 +00001010 // ****this code is temporary till NULL Values are fixed
1011 if( ! Val ) {
Chris Lattner697954c2002-01-20 22:54:45 +00001012 cerr << "\t<*NULL*>";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001013 continue;
1014 }
Ruchira Sasankae727f852001-09-18 22:43:57 +00001015
1016 // if a label or a constant
Chris Lattnerdbe53042002-01-21 01:33:12 +00001017 if(isa<BasicBlock>(Val)) {
Chris Lattner697954c2002-01-20 22:54:45 +00001018 cerr << "\t"; printLabel( Op.getVRegValue () );
1019 } else {
Ruchira Sasankae727f852001-09-18 22:43:57 +00001020 // else it must be a register value
1021 const int RegNum = Op.getAllocatedRegNum();
1022
Chris Lattner697954c2002-01-20 22:54:45 +00001023 cerr << "\t" << "%" << MRI.getUnifiedRegName( RegNum );
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001024 if (Val->hasName() )
Chris Lattner697954c2002-01-20 22:54:45 +00001025 cerr << "(" << Val->getName() << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001026 else
Chris Lattner697954c2002-01-20 22:54:45 +00001027 cerr << "(" << Val << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001028
1029 if( Op.opIsDef() )
Chris Lattner697954c2002-01-20 22:54:45 +00001030 cerr << "*";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001031
1032 const LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
1033 if( LROfVal )
1034 if( LROfVal->hasSpillOffset() )
Chris Lattner697954c2002-01-20 22:54:45 +00001035 cerr << "$";
Ruchira Sasankae727f852001-09-18 22:43:57 +00001036 }
1037
1038 }
1039 else if(Op.getOperandType() == MachineOperand::MO_MachineRegister) {
Chris Lattner697954c2002-01-20 22:54:45 +00001040 cerr << "\t" << "%" << MRI.getUnifiedRegName(Op.getMachineRegNum());
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001041 }
1042
1043 else
Chris Lattner697954c2002-01-20 22:54:45 +00001044 cerr << "\t" << Op; // use dump field
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001045 }
1046
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001047
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001048
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001049 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
Chris Lattner0665a5f2002-02-05 01:43:49 +00001050 if( NumOfImpRefs > 0) {
Chris Lattner697954c2002-01-20 22:54:45 +00001051 cerr << "\tImplicit:";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001052
Chris Lattner0665a5f2002-02-05 01:43:49 +00001053 for(unsigned z=0; z < NumOfImpRefs; z++)
1054 cerr << RAV(MInst->getImplicitRef(z)) << "\t";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001055 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001056
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001057 } // for all machine instructions
1058
Chris Lattner697954c2002-01-20 22:54:45 +00001059 cerr << "\n";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001060
1061 } // for all BBs
1062
Chris Lattner697954c2002-01-20 22:54:45 +00001063 cerr << "\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001064}
1065
Ruchira Sasankae727f852001-09-18 22:43:57 +00001066
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001067#if 0
1068
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001069//----------------------------------------------------------------------------
1070//
1071//----------------------------------------------------------------------------
1072
1073void PhyRegAlloc::colorCallRetArgs()
1074{
1075
1076 CallRetInstrListType &CallRetInstList = LRI.getCallRetInstrList();
1077 CallRetInstrListType::const_iterator It = CallRetInstList.begin();
1078
1079 for( ; It != CallRetInstList.end(); ++It ) {
1080
Ruchira Sasankaa90e7702001-10-15 16:26:38 +00001081 const MachineInstr *const CRMI = *It;
1082 unsigned OpCode = CRMI->getOpCode();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001083
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001084 // get the added instructions for this Call/Ret instruciton
1085 AddedInstrns *AI = AddedInstrMap[ CRMI ];
1086 if ( !AI ) {
1087 AI = new AddedInstrns();
1088 AddedInstrMap[ CRMI ] = AI;
1089 }
1090
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001091 // Tmp stack poistions are needed by some calls that have spilled args
1092 // So reset it before we call each such method
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001093 //mcInfo.popAllTempValues(TM);
1094
1095
Vikram S. Adve12af1642001-11-08 04:48:50 +00001096
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001097 if (TM.getInstrInfo().isCall(OpCode))
1098 MRI.colorCallArgs(CRMI, LRI, AI, *this);
1099 else if (TM.getInstrInfo().isReturn(OpCode))
Ruchira Sasankaa90e7702001-10-15 16:26:38 +00001100 MRI.colorRetValue( CRMI, LRI, AI );
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001101 else
1102 assert(0 && "Non Call/Ret instrn in CallRetInstrList\n");
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001103 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001104}
1105
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001106#endif
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001107
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001108//----------------------------------------------------------------------------
1109
1110//----------------------------------------------------------------------------
1111void PhyRegAlloc::colorIncomingArgs()
1112{
1113 const BasicBlock *const FirstBB = Meth->front();
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001114 const MachineInstr *FirstMI = FirstBB->getMachineInstrVec().front();
1115 assert(FirstMI && "No machine instruction in entry BB");
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001116
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001117 AddedInstrns *AI = AddedInstrMap[FirstMI];
1118 if (!AI)
1119 AddedInstrMap[FirstMI] = AI = new AddedInstrns();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001120
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001121 MRI.colorMethodArgs(Meth, LRI, AI);
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001122}
1123
Ruchira Sasankae727f852001-09-18 22:43:57 +00001124
1125//----------------------------------------------------------------------------
1126// Used to generate a label for a basic block
1127//----------------------------------------------------------------------------
Chris Lattner697954c2002-01-20 22:54:45 +00001128void PhyRegAlloc::printLabel(const Value *const Val) {
1129 if (Val->hasName())
1130 cerr << Val->getName();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001131 else
Chris Lattner697954c2002-01-20 22:54:45 +00001132 cerr << "Label" << Val;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001133}
1134
1135
Ruchira Sasankae727f852001-09-18 22:43:57 +00001136//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001137// This method calls setSugColorUsable method of each live range. This
1138// will determine whether the suggested color of LR is really usable.
1139// A suggested color is not usable when the suggested color is volatile
1140// AND when there are call interferences
1141//----------------------------------------------------------------------------
1142
1143void PhyRegAlloc::markUnusableSugColors()
1144{
Chris Lattner697954c2002-01-20 22:54:45 +00001145 if(DEBUG_RA ) cerr << "\nmarking unusable suggested colors ...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001146
1147 // hash map iterator
1148 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
1149 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
1150
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001151 for(; HMI != HMIEnd ; ++HMI ) {
1152 if (HMI->first) {
1153 LiveRange *L = HMI->second; // get the LiveRange
1154 if (L) {
1155 if(L->hasSuggestedColor()) {
1156 int RCID = L->getRegClass()->getID();
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001157 if( MRI.isRegVolatile( RCID, L->getSuggestedColor()) &&
1158 L->isCallInterference() )
1159 L->setSuggestedColorUsable( false );
1160 else
1161 L->setSuggestedColorUsable( true );
1162 }
1163 } // if L->hasSuggestedColor()
1164 }
1165 } // for all LR's in hash map
1166}
1167
1168
1169
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001170//----------------------------------------------------------------------------
1171// The following method will set the stack offsets of the live ranges that
1172// are decided to be spillled. This must be called just after coloring the
1173// LRs using the graph coloring algo. For each live range that is spilled,
1174// this method allocate a new spill position on the stack.
1175//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001176
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001177void PhyRegAlloc::allocateStackSpace4SpilledLRs()
1178{
Chris Lattner697954c2002-01-20 22:54:45 +00001179 if(DEBUG_RA ) cerr << "\nsetting LR stack offsets ...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001180
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001181 // hash map iterator
1182 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
1183 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
1184
1185 for( ; HMI != HMIEnd ; ++HMI ) {
Chris Lattner697954c2002-01-20 22:54:45 +00001186 if(HMI->first && HMI->second) {
1187 LiveRange *L = HMI->second; // get the LiveRange
1188 if( ! L->hasColor() )
1189 // NOTE: ** allocating the size of long Type **
1190 L->setSpillOffFromFP(mcInfo.allocateSpilledValue(TM, Type::LongTy));
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001191 }
1192 } // for all LR's in hash map
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001193}
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001194
1195
1196
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001197//----------------------------------------------------------------------------
Ruchira Sasankae727f852001-09-18 22:43:57 +00001198// The entry pont to Register Allocation
1199//----------------------------------------------------------------------------
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001200
1201void PhyRegAlloc::allocateRegisters()
1202{
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001203
1204 // make sure that we put all register classes into the RegClassList
1205 // before we call constructLiveRanges (now done in the constructor of
1206 // PhyRegAlloc class).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001207 //
1208 LRI.constructLiveRanges(); // create LR info
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001209
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001210 if (DEBUG_RA)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001211 LRI.printLiveRanges();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001212
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001213 createIGNodeListsAndIGs(); // create IGNode list and IGs
1214
1215 buildInterferenceGraphs(); // build IGs in all reg classes
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001216
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001217
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001218 if (DEBUG_RA) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001219 // print all LRs in all reg classes
1220 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1221 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001222
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001223 // print IGs in all register classes
1224 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1225 RegClassList[ rc ]->printIG();
1226 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001227
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001228
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001229 LRI.coalesceLRs(); // coalesce all live ranges
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001230
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +00001231
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001232 if( DEBUG_RA) {
1233 // print all LRs in all reg classes
1234 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1235 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001236
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001237 // print IGs in all register classes
1238 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1239 RegClassList[ rc ]->printIG();
1240 }
1241
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001242
1243 // mark un-usable suggested color before graph coloring algorithm.
1244 // When this is done, the graph coloring algo will not reserve
1245 // suggested color unnecessarily - they can be used by another LR
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001246 //
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001247 markUnusableSugColors();
1248
1249 // color all register classes using the graph coloring algo
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001250 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1251 RegClassList[ rc ]->colorAllRegs();
1252
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001253 // Atter grpah coloring, if some LRs did not receive a color (i.e, spilled)
1254 // a poistion for such spilled LRs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001255 //
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001256 allocateStackSpace4SpilledLRs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001257
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001258 mcInfo.popAllTempValues(TM); // TODO **Check
1259
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001260 // color incoming args - if the correct color was not received
1261 // insert code to copy to the correct register
1262 //
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001263 colorIncomingArgs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001264
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001265 // Now update the machine code with register names and add any
1266 // additional code inserted by the register allocator to the instruction
1267 // stream
1268 //
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001269 updateMachineCode();
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001270
Chris Lattner045e7c82001-09-19 16:26:23 +00001271 if (DEBUG_RA) {
Vikram S. Adve12af1642001-11-08 04:48:50 +00001272 MachineCodeForMethod::get(Meth).dump();
Chris Lattner045e7c82001-09-19 16:26:23 +00001273 printMachineCode(); // only for DEBUGGING
1274 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001275}
1276
Ruchira Sasankae727f852001-09-18 22:43:57 +00001277
1278