blob: b6a8ae1f25b1b7017bf27ce49e48e0167e0982cf [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 Lattner221d6882002-02-12 21:07:25 +000021#include "llvm/BasicBlock.h"
Chris Lattner30adeb62002-02-04 16:36:59 +000022#include "llvm/Method.h"
Chris Lattner37730942002-02-05 03:52:29 +000023#include "llvm/Type.h"
Chris Lattner697954c2002-01-20 22:54:45 +000024#include <iostream>
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000025#include <math.h>
Chris Lattner697954c2002-01-20 22:54:45 +000026using std::cerr;
Vikram S. Adve12af1642001-11-08 04:48:50 +000027
28
29// ***TODO: There are several places we add instructions. Validate the order
30// of adding these instructions.
Ruchira Sasanka174bded2001-10-28 18:12:02 +000031
Chris Lattner045e7c82001-09-19 16:26:23 +000032cl::Enum<RegAllocDebugLevel_t> DEBUG_RA("dregalloc", cl::NoFlags,
33 "enable register allocation debugging information",
34 clEnumValN(RA_DEBUG_None , "n", "disable debug output"),
35 clEnumValN(RA_DEBUG_Normal , "y", "enable debug output"),
36 clEnumValN(RA_DEBUG_Verbose, "v", "enable extra debug output"), 0);
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000037
38
Chris Lattner2f9b28e2002-02-04 15:54:09 +000039//----------------------------------------------------------------------------
40// RegisterAllocation pass front end...
41//----------------------------------------------------------------------------
42namespace {
43 class RegisterAllocator : public MethodPass {
44 TargetMachine &Target;
45 public:
46 inline RegisterAllocator(TargetMachine &T) : Target(T) {}
Chris Lattner6dd98a62002-02-04 00:33:08 +000047
Chris Lattner2f9b28e2002-02-04 15:54:09 +000048 bool runOnMethod(Method *M) {
49 if (DEBUG_RA)
50 cerr << "\n******************** Method "<< M->getName()
51 << " ********************\n";
52
Chris Lattner4d7fc112002-02-04 20:02:38 +000053 PhyRegAlloc PRA(M, Target, &getAnalysis<MethodLiveVarInfo>(),
Chris Lattner14ab1ce2002-02-04 17:48:00 +000054 &getAnalysis<cfg::LoopInfo>());
Chris Lattner2f9b28e2002-02-04 15:54:09 +000055 PRA.allocateRegisters();
56
57 if (DEBUG_RA) cerr << "\nRegister allocation complete!\n";
58 return false;
59 }
Chris Lattner4911c352002-02-04 17:39:42 +000060
61 virtual void getAnalysisUsageInfo(Pass::AnalysisSet &Requires,
62 Pass::AnalysisSet &Destroyed,
63 Pass::AnalysisSet &Provided) {
Chris Lattner14ab1ce2002-02-04 17:48:00 +000064 Requires.push_back(cfg::LoopInfo::ID);
Chris Lattner4d7fc112002-02-04 20:02:38 +000065 Requires.push_back(MethodLiveVarInfo::ID);
Vikram S. Adve9c4f7262002-03-24 03:54:03 +000066 Destroyed.push_back(MethodLiveVarInfo::ID);
Chris Lattner4911c352002-02-04 17:39:42 +000067 }
Chris Lattner2f9b28e2002-02-04 15:54:09 +000068 };
Chris Lattner6dd98a62002-02-04 00:33:08 +000069}
70
Chris Lattner2f9b28e2002-02-04 15:54:09 +000071MethodPass *getRegisterAllocator(TargetMachine &T) {
72 return new RegisterAllocator(T);
73}
Chris Lattner6dd98a62002-02-04 00:33:08 +000074
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000075//----------------------------------------------------------------------------
76// Constructor: Init local composite objects and create register classes.
77//----------------------------------------------------------------------------
Vikram S. Adve12af1642001-11-08 04:48:50 +000078PhyRegAlloc::PhyRegAlloc(Method *M,
Ruchira Sasanka8e604792001-09-14 21:18:34 +000079 const TargetMachine& tm,
Chris Lattner4911c352002-02-04 17:39:42 +000080 MethodLiveVarInfo *Lvi,
Chris Lattner14ab1ce2002-02-04 17:48:00 +000081 cfg::LoopInfo *LDC)
Chris Lattner697954c2002-01-20 22:54:45 +000082 : TM(tm), Meth(M),
Vikram S. Adve12af1642001-11-08 04:48:50 +000083 mcInfo(MachineCodeForMethod::get(M)),
84 LVI(Lvi), LRI(M, tm, RegClassList),
Ruchira Sasanka8e604792001-09-14 21:18:34 +000085 MRI( tm.getRegInfo() ),
86 NumOfRegClasses(MRI.getNumOfRegClasses()),
Chris Lattner4911c352002-02-04 17:39:42 +000087 LoopDepthCalc(LDC) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +000088
89 // create each RegisterClass and put in RegClassList
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000090 //
Chris Lattner697954c2002-01-20 22:54:45 +000091 for(unsigned int rc=0; rc < NumOfRegClasses; rc++)
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000092 RegClassList.push_back( new RegClass(M, MRI.getMachineRegClass(rc),
93 &ResColList) );
Ruchira Sasanka8e604792001-09-14 21:18:34 +000094}
95
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000096
97//----------------------------------------------------------------------------
98// Destructor: Deletes register classes
99//----------------------------------------------------------------------------
100PhyRegAlloc::~PhyRegAlloc() {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000101 for( unsigned int rc=0; rc < NumOfRegClasses; rc++)
102 delete RegClassList[rc];
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000103}
104
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000105//----------------------------------------------------------------------------
106// This method initally creates interference graphs (one in each reg class)
107// and IGNodeList (one in each IG). The actual nodes will be pushed later.
108//----------------------------------------------------------------------------
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000109void PhyRegAlloc::createIGNodeListsAndIGs() {
110 if (DEBUG_RA) cerr << "Creating LR lists ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000111
112 // hash map iterator
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000113 LiveRangeMapType::const_iterator HMI = LRI.getLiveRangeMap()->begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000114
115 // hash map end
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000116 LiveRangeMapType::const_iterator HMIEnd = LRI.getLiveRangeMap()->end();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000117
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000118 for (; HMI != HMIEnd ; ++HMI ) {
119 if (HMI->first) {
120 LiveRange *L = HMI->second; // get the LiveRange
121 if (!L) {
122 if( DEBUG_RA) {
Chris Lattner0665a5f2002-02-05 01:43:49 +0000123 cerr << "\n*?!?Warning: Null liver range found for: "
124 << RAV(HMI->first) << "\n";
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000125 }
126 continue;
127 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000128 // if the Value * is not null, and LR
129 // is not yet written to the IGNodeList
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000130 if( !(L->getUserIGNode()) ) {
131 RegClass *const RC = // RegClass of first value in the LR
132 RegClassList[ L->getRegClass()->getID() ];
133
134 RC->addLRToIG(L); // add this LR to an IG
135 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000136 }
137 }
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000138
139 // init RegClassList
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000140 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000141 RegClassList[rc]->createInterferenceGraph();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000142
143 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000144 cerr << "LRLists Created!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000145}
146
147
148
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000149
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000150//----------------------------------------------------------------------------
151// This method will add all interferences at for a given instruction.
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000152// Interence occurs only if the LR of Def (Inst or Arg) is of the same reg
153// class as that of live var. The live var passed to this function is the
154// LVset AFTER the instruction
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000155//----------------------------------------------------------------------------
Chris Lattner296b7732002-02-05 02:52:05 +0000156void PhyRegAlloc::addInterference(const Value *Def,
157 const ValueSet *LVSet,
158 bool isCallInst) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000159
Chris Lattner296b7732002-02-05 02:52:05 +0000160 ValueSet::const_iterator LIt = LVSet->begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000161
162 // get the live range of instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000163 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000164 const LiveRange *const LROfDef = LRI.getLiveRangeForValue( Def );
165
166 IGNode *const IGNodeOfDef = LROfDef->getUserIGNode();
167 assert( IGNodeOfDef );
168
169 RegClass *const RCOfDef = LROfDef->getRegClass();
170
171 // for each live var in live variable set
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000172 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000173 for( ; LIt != LVSet->end(); ++LIt) {
174
Chris Lattner0665a5f2002-02-05 01:43:49 +0000175 if (DEBUG_RA > 1)
176 cerr << "< Def=" << RAV(Def) << ", Lvar=" << RAV(*LIt) << "> ";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000177
178 // get the live range corresponding to live var
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000179 //
Chris Lattner0665a5f2002-02-05 01:43:49 +0000180 LiveRange *LROfVar = LRI.getLiveRangeForValue(*LIt);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000181
182 // LROfVar can be null if it is a const since a const
183 // doesn't have a dominating def - see Assumptions above
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000184 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000185 if (LROfVar) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000186 if(LROfDef == LROfVar) // do not set interf for same LR
187 continue;
188
189 // if 2 reg classes are the same set interference
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000190 //
Chris Lattner0665a5f2002-02-05 01:43:49 +0000191 if (RCOfDef == LROfVar->getRegClass()) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000192 RCOfDef->setInterference( LROfDef, LROfVar);
Chris Lattner0665a5f2002-02-05 01:43:49 +0000193 } else if (DEBUG_RA > 1) {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000194 // we will not have LRs for values not explicitly allocated in the
195 // instruction stream (e.g., constants)
Chris Lattner0665a5f2002-02-05 01:43:49 +0000196 cerr << " warning: no live range for " << RAV(*LIt) << "\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,
Chris Lattner296b7732002-02-05 02:52:05 +0000212 const ValueSet *LVSetAft) {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000213
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
Chris Lattner296b7732002-02-05 02:52:05 +0000231 ValueSet::const_iterator LIt = LVSetAft->begin();
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000232
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: ";
Chris Lattner296b7732002-02-05 02:52:05 +0000243 printSet(*LR);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000244 }
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: " ;
Chris Lattner296b7732002-02-05 02:52:05 +0000254 printSet(*LR);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000255 }
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
Chris Lattner748697d2002-02-05 04:20:12 +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 //
Chris Lattner748697d2002-02-05 04:20:12 +0000298 const ValueSet &LVSetAI = LVI->getLiveVarSetAfterMInst(MInst, *BBI);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000299
300 const bool isCallInst = TM.getInstrInfo().isCall(MInst->getOpCode());
301
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000302 if( isCallInst ) {
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000303 // set the isCallInterference flag of each live range wich extends
304 // accross this call instruction. This information is used by graph
305 // coloring algo to avoid allocating volatile colors to live ranges
306 // that span across calls (since they have to be saved/restored)
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000307 //
Chris Lattner748697d2002-02-05 04:20:12 +0000308 setCallInterferences(MInst, &LVSetAI);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000309 }
310
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000311
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000312 // iterate over all MI operands to find defs
313 //
Chris Lattner2f898d22002-02-05 06:02:59 +0000314 for (MachineInstr::const_val_op_iterator OpI = MInst->begin(),
315 OpE = MInst->end(); OpI != OpE; ++OpI) {
316 if (OpI.isDef()) // create a new LR iff this operand is a def
Chris Lattner748697d2002-02-05 04:20:12 +0000317 addInterference(*OpI, &LVSetAI, isCallInst);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000318
319 // Calculate the spill cost of each live range
320 //
Chris Lattner2f898d22002-02-05 06:02:59 +0000321 LiveRange *LR = LRI.getLiveRangeForValue(*OpI);
322 if (LR) LR->addSpillCost(BBLoopDepthCost);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000323 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000324
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000325
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000326 // if there are multiple defs in this instruction e.g. in SETX
327 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000328 if (TM.getInstrInfo().isPseudoInstr(MInst->getOpCode()))
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000329 addInterf4PseudoInstr(MInst);
330
331
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000332 // Also add interference for any implicit definitions in a machine
333 // instr (currently, only calls have this).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000334 //
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000335 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
336 if( NumOfImpRefs > 0 ) {
337 for(unsigned z=0; z < NumOfImpRefs; z++)
338 if( MInst->implicitRefIsDefined(z) )
Chris Lattner748697d2002-02-05 04:20:12 +0000339 addInterference( MInst->getImplicitRef(z), &LVSetAI, isCallInst );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000340 }
341
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +0000342
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000343 } // for all machine instructions in BB
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000344
345 } // for all BBs in method
346
347
348 // add interferences for method arguments. Since there are no explict
349 // defs in method for args, we have to add them manually
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000350 //
351 addInterferencesForArgs();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000352
353 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000354 cerr << "Interference graphs calculted!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000355
356}
357
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000358
359
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000360//--------------------------------------------------------------------------
361// Pseudo instructions will be exapnded to multiple instructions by the
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000362// assembler. Consequently, all the opernds must get distinct registers.
363// Therefore, we mark all operands of a pseudo instruction as they interfere
364// with one another.
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000365//--------------------------------------------------------------------------
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000366void PhyRegAlloc::addInterf4PseudoInstr(const MachineInstr *MInst) {
367
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000368 bool setInterf = false;
369
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000370 // iterate over MI operands to find defs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000371 //
Chris Lattner2f898d22002-02-05 06:02:59 +0000372 for (MachineInstr::const_val_op_iterator It1 = MInst->begin(),
373 ItE = MInst->end(); It1 != ItE; ++It1) {
374 const LiveRange *LROfOp1 = LRI.getLiveRangeForValue(*It1);
375 assert((LROfOp1 || !It1.isDef()) && "No LR for Def in PSEUDO insruction");
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000376
Chris Lattner2f898d22002-02-05 06:02:59 +0000377 MachineInstr::const_val_op_iterator It2 = It1;
378 for(++It2; It2 != ItE; ++It2) {
379 const LiveRange *LROfOp2 = LRI.getLiveRangeForValue(*It2);
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000380
Chris Lattner2f898d22002-02-05 06:02:59 +0000381 if (LROfOp2) {
382 RegClass *RCOfOp1 = LROfOp1->getRegClass();
383 RegClass *RCOfOp2 = LROfOp2->getRegClass();
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000384
385 if( RCOfOp1 == RCOfOp2 ){
386 RCOfOp1->setInterference( LROfOp1, LROfOp2 );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000387 setInterf = true;
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000388 }
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000389 } // if Op2 has a LR
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000390 } // for all other defs in machine instr
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000391 } // for all operands in an instruction
392
Chris Lattner2f898d22002-02-05 06:02:59 +0000393 if (!setInterf && MInst->getNumOperands() > 2) {
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000394 cerr << "\nInterf not set for any operand in pseudo instr:\n";
395 cerr << *MInst;
396 assert(0 && "Interf not set for pseudo instr with > 2 operands" );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000397 }
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000398}
399
400
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000401
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000402//----------------------------------------------------------------------------
403// This method will add interferences for incoming arguments to a method.
404//----------------------------------------------------------------------------
Chris Lattner296b7732002-02-05 02:52:05 +0000405void PhyRegAlloc::addInterferencesForArgs() {
406 // get the InSet of root BB
Chris Lattner748697d2002-02-05 04:20:12 +0000407 const ValueSet &InSet = LVI->getInSetOfBB(Meth->front());
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000408
Chris Lattner296b7732002-02-05 02:52:05 +0000409 // get the argument list
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000410 const Method::ArgumentListType& ArgList = Meth->getArgumentList();
411
Chris Lattner296b7732002-02-05 02:52:05 +0000412 // get an iterator to arg list
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000413 Method::ArgumentListType::const_iterator ArgIt = ArgList.begin();
414
415
416 for( ; ArgIt != ArgList.end() ; ++ArgIt) { // for each argument
Chris Lattner748697d2002-02-05 04:20:12 +0000417 addInterference((Value*)*ArgIt, &InSet, false);// add interferences between
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000418 // args and LVars at start
Chris Lattner0665a5f2002-02-05 01:43:49 +0000419 if( DEBUG_RA > 1)
420 cerr << " - %% adding interference for argument "
421 << RAV((const Value *)*ArgIt) << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000422 }
423}
424
425
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000426
427
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000428//----------------------------------------------------------------------------
429// This method is called after register allocation is complete to set the
430// allocated reisters in the machine code. This code will add register numbers
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000431// to MachineOperands that contain a Value. Also it calls target specific
432// methods to produce caller saving instructions. At the end, it adds all
433// additional instructions produced by the register allocator to the
434// instruction stream.
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000435//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000436void PhyRegAlloc::updateMachineCode()
437{
438
439 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
440
441 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
442
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000443 // get the iterator for machine instructions
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000444 //
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000445 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
446 MachineCodeForBasicBlock::iterator MInstIterator = MIVec.begin();
447
448 // iterate over all the machine instructions in BB
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000449 //
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000450 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
451
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000452 MachineInstr *MInst = *MInstIterator;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000453
454 unsigned Opcode = MInst->getOpCode();
455
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000456 // do not process Phis
Vikram S. Adve23a4c8f2002-03-18 03:37:19 +0000457 if (TM.getInstrInfo().isDummyPhiInstr(Opcode))
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000458 continue;
459
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000460 // Now insert speical instructions (if necessary) for call/return
461 // instructions.
462 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000463 if (TM.getInstrInfo().isCall(Opcode) ||
464 TM.getInstrInfo().isReturn(Opcode)) {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000465
466 AddedInstrns *AI = AddedInstrMap[ MInst];
467 if ( !AI ) {
468 AI = new AddedInstrns();
469 AddedInstrMap[ MInst ] = AI;
470 }
471
472 // Tmp stack poistions are needed by some calls that have spilled args
473 // So reset it before we call each such method
Ruchira Sasanka6a3db8c2002-01-07 21:09:06 +0000474 //
475 mcInfo.popAllTempValues(TM);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000476
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000477 if (TM.getInstrInfo().isCall(Opcode))
478 MRI.colorCallArgs(MInst, LRI, AI, *this, *BBI);
479 else if (TM.getInstrInfo().isReturn(Opcode))
480 MRI.colorRetValue(MInst, LRI, AI);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000481 }
482
483
484 /* -- Using above code instead of this
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000485
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000486 // if this machine instr is call, insert caller saving code
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000487
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000488 if( (TM.getInstrInfo()).isCall( MInst->getOpCode()) )
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000489 MRI.insertCallerSavingCode(MInst, *BBI, *this );
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000490
491 */
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000492
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000493
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000494 // reset the stack offset for temporary variables since we may
495 // need that to spill
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000496 // mcInfo.popAllTempValues(TM);
Ruchira Sasankaf90870f2001-11-15 22:02:06 +0000497 // TODO ** : do later
Vikram S. Adve12af1642001-11-08 04:48:50 +0000498
Chris Lattner7a176752001-12-04 00:03:30 +0000499 //for(MachineInstr::val_const_op_iterator OpI(MInst);!OpI.done();++OpI) {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000500
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000501
502 // Now replace set the registers for operands in the machine instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000503 //
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000504 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
505
506 MachineOperand& Op = MInst->getOperand(OpNum);
507
508 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
509 Op.getOperandType() == MachineOperand::MO_CCRegister) {
510
511 const Value *const Val = Op.getVRegValue();
512
513 // delete this condition checking later (must assert if Val is null)
Chris Lattner045e7c82001-09-19 16:26:23 +0000514 if( !Val) {
515 if (DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000516 cerr << "Warning: NULL Value found for operand\n";
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000517 continue;
518 }
519 assert( Val && "Value is NULL");
520
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000521 LiveRange *const LR = LRI.getLiveRangeForValue(Val);
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000522
523 if ( !LR ) {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000524
525 // nothing to worry if it's a const or a label
526
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000527 if (DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000528 cerr << "*NO LR for operand : " << Op ;
529 cerr << " [reg:" << Op.getAllocatedRegNum() << "]";
530 cerr << " in inst:\t" << *MInst << "\n";
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000531 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000532
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000533 // if register is not allocated, mark register as invalid
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000534 if( Op.getAllocatedRegNum() == -1)
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000535 Op.setRegForValue( MRI.getInvalidRegNum());
Ruchira Sasankae727f852001-09-18 22:43:57 +0000536
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000537
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000538 continue;
539 }
540
541 unsigned RCID = (LR->getRegClass())->getID();
542
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000543 if( LR->hasColor() ) {
544 Op.setRegForValue( MRI.getUnifiedRegNum(RCID, LR->getColor()) );
545 }
546 else {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000547
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000548 // LR did NOT receive a color (register). Now, insert spill code
549 // for spilled opeands in this machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000550
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000551 //assert(0 && "LR must be spilled");
552 insertCode4SpilledLR(LR, MInst, *BBI, OpNum );
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000553
554 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000555 }
556
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000557 } // for each operand
558
559
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000560 // Now add instructions that the register allocator inserts before/after
561 // this machine instructions (done only for calls/rets/incoming args)
562 // We do this here, to ensure that spill for an instruction is inserted
563 // closest as possible to an instruction (see above insertCode4Spill...)
564 //
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000565 // If there are instructions to be added, *before* this machine
566 // instruction, add them now.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000567 //
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000568 if( AddedInstrMap[ MInst ] ) {
Chris Lattner697954c2002-01-20 22:54:45 +0000569 std::deque<MachineInstr *> &IBef = AddedInstrMap[MInst]->InstrnsBefore;
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000570
571 if( ! IBef.empty() ) {
Chris Lattner697954c2002-01-20 22:54:45 +0000572 std::deque<MachineInstr *>::iterator AdIt;
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000573
574 for( AdIt = IBef.begin(); AdIt != IBef.end() ; ++AdIt ) {
575
576 if( DEBUG_RA) {
577 cerr << "For inst " << *MInst;
Chris Lattner697954c2002-01-20 22:54:45 +0000578 cerr << " PREPENDed instr: " << **AdIt << "\n";
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000579 }
580
581 MInstIterator = MIVec.insert( MInstIterator, *AdIt );
582 ++MInstIterator;
583 }
584
585 }
586
587 }
588
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000589 // If there are instructions to be added *after* this machine
590 // instruction, add them now
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000591 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000592 if(AddedInstrMap[MInst] &&
593 !AddedInstrMap[MInst]->InstrnsAfter.empty() ) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000594
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000595 // if there are delay slots for this instruction, the instructions
596 // added after it must really go after the delayed instruction(s)
597 // So, we move the InstrAfter of the current instruction to the
598 // corresponding delayed instruction
599
600 unsigned delay;
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000601 if ((delay=TM.getInstrInfo().getNumDelaySlots(MInst->getOpCode())) >0){
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000602 move2DelayedInstr(MInst, *(MInstIterator+delay) );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000603
Chris Lattner697954c2002-01-20 22:54:45 +0000604 if(DEBUG_RA) cerr<< "\nMoved an added instr after the delay slot";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000605 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000606
607 else {
608
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000609
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000610 // Here we can add the "instructions after" to the current
611 // instruction since there are no delay slots for this instruction
612
Chris Lattner697954c2002-01-20 22:54:45 +0000613 std::deque<MachineInstr *> &IAft = AddedInstrMap[MInst]->InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000614
615 if( ! IAft.empty() ) {
616
Chris Lattner697954c2002-01-20 22:54:45 +0000617 std::deque<MachineInstr *>::iterator AdIt;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000618
619 ++MInstIterator; // advance to the next instruction
620
621 for( AdIt = IAft.begin(); AdIt != IAft.end() ; ++AdIt ) {
622
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000623 if(DEBUG_RA) {
624 cerr << "For inst " << *MInst;
Chris Lattner697954c2002-01-20 22:54:45 +0000625 cerr << " APPENDed instr: " << **AdIt << "\n";
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000626 }
627
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000628 MInstIterator = MIVec.insert( MInstIterator, *AdIt );
629 ++MInstIterator;
630 }
631
632 // MInsterator already points to the next instr. Since the
633 // for loop also increments it, decrement it to point to the
634 // instruction added last
635 --MInstIterator;
636
637 }
638
639 } // if not delay
640
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000641 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000642
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000643 } // for each machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000644 }
645}
646
647
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000648
649//----------------------------------------------------------------------------
650// This method inserts spill code for AN operand whose LR was spilled.
651// This method may be called several times for a single machine instruction
652// if it contains many spilled operands. Each time it is called, it finds
653// a register which is not live at that instruction and also which is not
654// used by other spilled operands of the same instruction. Then it uses
655// this register temporarily to accomodate the spilled value.
656//----------------------------------------------------------------------------
657void PhyRegAlloc::insertCode4SpilledLR(const LiveRange *LR,
658 MachineInstr *MInst,
659 const BasicBlock *BB,
660 const unsigned OpNum) {
661
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000662 assert(! TM.getInstrInfo().isCall(MInst->getOpCode()) &&
663 (! TM.getInstrInfo().isReturn(MInst->getOpCode())) &&
664 "Arg of a call/ret must be handled elsewhere");
665
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000666 MachineOperand& Op = MInst->getOperand(OpNum);
667 bool isDef = MInst->operandIsDefined(OpNum);
668 unsigned RegType = MRI.getRegType( LR );
669 int SpillOff = LR->getSpillOffFromFP();
670 RegClass *RC = LR->getRegClass();
Chris Lattner748697d2002-02-05 04:20:12 +0000671 const ValueSet &LVSetBef = LVI->getLiveVarSetBeforeMInst(MInst, BB);
Vikram S. Adve00521d72001-11-12 23:26:35 +0000672
Chris Lattner697954c2002-01-20 22:54:45 +0000673 mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000674
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000675 MachineInstr *MIBef=NULL, *AdIMid=NULL, *MIAft=NULL;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000676
Chris Lattner748697d2002-02-05 04:20:12 +0000677 int TmpRegU = getUsableUniRegAtMI(RC, RegType, MInst,&LVSetBef, MIBef, MIAft);
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000678
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000679 // get the added instructions for this instruciton
680 AddedInstrns *AI = AddedInstrMap[ MInst ];
681 if ( !AI ) {
682 AI = new AddedInstrns();
683 AddedInstrMap[ MInst ] = AI;
684 }
685
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000686
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000687 if( !isDef ) {
688
689 // for a USE, we have to load the value of LR from stack to a TmpReg
690 // and use the TmpReg as one operand of instruction
691
692 // actual loading instruction
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000693 AdIMid = MRI.cpMem2RegMI(MRI.getFramePointer(), SpillOff, TmpRegU,RegType);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000694
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000695 if(MIBef)
696 AI->InstrnsBefore.push_back(MIBef);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000697
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000698 AI->InstrnsBefore.push_back(AdIMid);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000699
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000700 if(MIAft)
701 AI->InstrnsAfter.push_front(MIAft);
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000702
Chris Lattner296b7732002-02-05 02:52:05 +0000703 } else { // if this is a Def
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000704 // for a DEF, we have to store the value produced by this instruction
705 // on the stack position allocated for this LR
706
707 // actual storing instruction
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000708 AdIMid = MRI.cpReg2MemMI(TmpRegU, MRI.getFramePointer(), SpillOff,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->InstrnsAfter.push_front(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 Sasanka5a61d852001-11-08 16:43:25 +0000717
718 } // if !DEF
719
720 cerr << "\nFor Inst " << *MInst;
Chris Lattner296b7732002-02-05 02:52:05 +0000721 cerr << " - SPILLED LR: "; printSet(*LR);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000722 cerr << "\n - Added Instructions:";
Chris Lattner296b7732002-02-05 02:52:05 +0000723 if (MIBef) cerr << *MIBef;
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000724 cerr << *AdIMid;
Chris Lattner296b7732002-02-05 02:52:05 +0000725 if (MIAft) cerr << *MIAft;
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000726
Chris Lattner296b7732002-02-05 02:52:05 +0000727 Op.setRegForValue(TmpRegU); // set the opearnd
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000728}
729
730
731
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000732//----------------------------------------------------------------------------
733// We can use the following method to get a temporary register to be used
734// BEFORE any given machine instruction. If there is a register available,
735// this method will simply return that register and set MIBef = MIAft = NULL.
736// Otherwise, it will return a register and MIAft and MIBef will contain
737// two instructions used to free up this returned register.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000738// Returned register number is the UNIFIED register number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000739//----------------------------------------------------------------------------
740
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000741int PhyRegAlloc::getUsableUniRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000742 const int RegType,
743 const MachineInstr *MInst,
Chris Lattner296b7732002-02-05 02:52:05 +0000744 const ValueSet *LVSetBef,
Vikram S. Adve23a4c8f2002-03-18 03:37:19 +0000745 MachineInstr *&MIBef,
746 MachineInstr *&MIAft) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000747
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000748 int RegU = getUnusedUniRegAtMI(RC, MInst, LVSetBef);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000749
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000750
751 if( RegU != -1) {
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000752 // we found an unused register, so we can simply use it
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000753 MIBef = MIAft = NULL;
754 }
755 else {
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000756 // we couldn't find an unused register. Generate code to free up a reg by
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000757 // saving it on stack and restoring after the instruction
758
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000759 int TmpOff = mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
Vikram S. Adve12af1642001-11-08 04:48:50 +0000760
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000761 RegU = getUniRegNotUsedByThisInst(RC, MInst);
762 MIBef = MRI.cpReg2MemMI(RegU, MRI.getFramePointer(), TmpOff, RegType );
763 MIAft = MRI.cpMem2RegMI(MRI.getFramePointer(), TmpOff, RegU, RegType );
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000764 }
765
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000766 return RegU;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000767}
768
769//----------------------------------------------------------------------------
770// This method is called to get a new unused register that can be used to
771// accomodate a spilled value.
772// This method may be called several times for a single machine instruction
773// if it contains many spilled operands. Each time it is called, it finds
774// a register which is not live at that instruction and also which is not
775// used by other spilled operands of the same instruction.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000776// Return register number is relative to the register class. NOT
777// unified number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000778//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000779int PhyRegAlloc::getUnusedUniRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000780 const MachineInstr *MInst,
Chris Lattner296b7732002-02-05 02:52:05 +0000781 const ValueSet *LVSetBef) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000782
783 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
784
785 bool *IsColorUsedArr = RC->getIsColorUsedArr();
786
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000787 for(unsigned i=0; i < NumAvailRegs; i++) // Reset array
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000788 IsColorUsedArr[i] = false;
789
Chris Lattner296b7732002-02-05 02:52:05 +0000790 ValueSet::const_iterator LIt = LVSetBef->begin();
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000791
792 // for each live var in live variable set after machine inst
793 for( ; LIt != LVSetBef->end(); ++LIt) {
794
795 // get the live range corresponding to live var
796 LiveRange *const LRofLV = LRI.getLiveRangeForValue(*LIt );
797
798 // LR can be null if it is a const since a const
799 // doesn't have a dominating def - see Assumptions above
800 if( LRofLV )
801 if( LRofLV->hasColor() )
802 IsColorUsedArr[ LRofLV->getColor() ] = true;
803 }
804
805 // It is possible that one operand of this MInst was already spilled
806 // and it received some register temporarily. If that's the case,
807 // it is recorded in machine operand. We must skip such registers.
808
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000809 setRelRegsUsedByThisInst(RC, MInst);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000810
811 unsigned c; // find first unused color
812 for( c=0; c < NumAvailRegs; c++)
813 if( ! IsColorUsedArr[ c ] ) break;
814
815 if(c < NumAvailRegs)
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000816 return MRI.getUnifiedRegNum(RC->getID(), c);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000817 else
818 return -1;
819
820
821}
822
823
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000824//----------------------------------------------------------------------------
825// Get any other register in a register class, other than what is used
826// by operands of a machine instruction. Returns the unified reg number.
827//----------------------------------------------------------------------------
828int PhyRegAlloc::getUniRegNotUsedByThisInst(RegClass *RC,
829 const MachineInstr *MInst) {
830
831 bool *IsColorUsedArr = RC->getIsColorUsedArr();
832 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
833
834
835 for(unsigned i=0; i < NumAvailRegs ; i++) // Reset array
836 IsColorUsedArr[i] = false;
837
838 setRelRegsUsedByThisInst(RC, MInst);
839
840 unsigned c; // find first unused color
841 for( c=0; c < RC->getNumOfAvailRegs(); c++)
842 if( ! IsColorUsedArr[ c ] ) break;
843
844 if(c < NumAvailRegs)
845 return MRI.getUnifiedRegNum(RC->getID(), c);
846 else
847 assert( 0 && "FATAL: No free register could be found in reg class!!");
Chris Lattner697954c2002-01-20 22:54:45 +0000848 return 0;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000849}
850
851
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000852//----------------------------------------------------------------------------
853// This method modifies the IsColorUsedArr of the register class passed to it.
854// It sets the bits corresponding to the registers used by this machine
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000855// instructions. Both explicit and implicit operands are set.
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000856//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000857void PhyRegAlloc::setRelRegsUsedByThisInst(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000858 const MachineInstr *MInst ) {
859
860 bool *IsColorUsedArr = RC->getIsColorUsedArr();
861
862 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
863
864 const MachineOperand& Op = MInst->getOperand(OpNum);
865
866 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000867 Op.getOperandType() == MachineOperand::MO_CCRegister ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000868
869 const Value *const Val = Op.getVRegValue();
870
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000871 if( Val )
872 if( MRI.getRegClassIDOfValue(Val) == RC->getID() ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000873 int Reg;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000874 if( (Reg=Op.getAllocatedRegNum()) != -1) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000875 IsColorUsedArr[ Reg ] = true;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000876 }
877 else {
878 // it is possilbe that this operand still is not marked with
879 // a register but it has a LR and that received a color
880
881 LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
882 if( LROfVal)
883 if( LROfVal->hasColor() )
884 IsColorUsedArr[ LROfVal->getColor() ] = true;
885 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000886
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000887 } // if reg classes are the same
888 }
889 else if (Op.getOperandType() == MachineOperand::MO_MachineRegister) {
890 IsColorUsedArr[ Op.getMachineRegNum() ] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000891 }
892 }
893
894 // If there are implicit references, mark them as well
895
896 for(unsigned z=0; z < MInst->getNumImplicitRefs(); z++) {
897
898 LiveRange *const LRofImpRef =
899 LRI.getLiveRangeForValue( MInst->getImplicitRef(z) );
Chris Lattner697954c2002-01-20 22:54:45 +0000900
901 if(LRofImpRef && LRofImpRef->hasColor())
902 IsColorUsedArr[LRofImpRef->getColor()] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000903 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000904}
905
906
907
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000908
909
910
911
912
913//----------------------------------------------------------------------------
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000914// If there are delay slots for an instruction, the instructions
915// added after it must really go after the delayed instruction(s).
916// So, we move the InstrAfter of that instruction to the
917// corresponding delayed instruction using the following method.
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000918
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000919//----------------------------------------------------------------------------
920void PhyRegAlloc:: move2DelayedInstr(const MachineInstr *OrigMI,
921 const MachineInstr *DelayedMI) {
922
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000923 // "added after" instructions of the original instr
Chris Lattner697954c2002-01-20 22:54:45 +0000924 std::deque<MachineInstr *> &OrigAft = AddedInstrMap[OrigMI]->InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000925
926 // "added instructions" of the delayed instr
927 AddedInstrns *DelayAdI = AddedInstrMap[DelayedMI];
928
929 if(! DelayAdI ) { // create a new "added after" if necessary
930 DelayAdI = new AddedInstrns();
931 AddedInstrMap[DelayedMI] = DelayAdI;
932 }
933
934 // "added after" instructions of the delayed instr
Chris Lattner697954c2002-01-20 22:54:45 +0000935 std::deque<MachineInstr *> &DelayedAft = DelayAdI->InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000936
937 // go thru all the "added after instructions" of the original instruction
938 // and append them to the "addded after instructions" of the delayed
939 // instructions
Chris Lattner697954c2002-01-20 22:54:45 +0000940 DelayedAft.insert(DelayedAft.end(), OrigAft.begin(), OrigAft.end());
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000941
942 // empty the "added after instructions" of the original instruction
943 OrigAft.clear();
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000944}
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000945
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000946//----------------------------------------------------------------------------
947// This method prints the code with registers after register allocation is
948// complete.
949//----------------------------------------------------------------------------
950void PhyRegAlloc::printMachineCode()
951{
952
Chris Lattner697954c2002-01-20 22:54:45 +0000953 cerr << "\n;************** Method " << Meth->getName()
954 << " *****************\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000955
956 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
957
958 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
959
Chris Lattner697954c2002-01-20 22:54:45 +0000960 cerr << "\n"; printLabel( *BBI); cerr << ": ";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000961
962 // get the iterator for machine instructions
963 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
964 MachineCodeForBasicBlock::iterator MInstIterator = MIVec.begin();
965
966 // iterate over all the machine instructions in BB
967 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
968
969 MachineInstr *const MInst = *MInstIterator;
970
971
Chris Lattner697954c2002-01-20 22:54:45 +0000972 cerr << "\n\t";
973 cerr << TargetInstrDescriptors[MInst->getOpCode()].opCodeString;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000974
975
Chris Lattner7a176752001-12-04 00:03:30 +0000976 //for(MachineInstr::val_const_op_iterator OpI(MInst);!OpI.done();++OpI) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000977
978 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
979
980 MachineOperand& Op = MInst->getOperand(OpNum);
981
982 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasanka97b8b442001-10-18 22:36:26 +0000983 Op.getOperandType() == MachineOperand::MO_CCRegister /*||
984 Op.getOperandType() == MachineOperand::MO_PCRelativeDisp*/ ) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000985
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000986 const Value *const Val = Op.getVRegValue () ;
Ruchira Sasankae727f852001-09-18 22:43:57 +0000987 // ****this code is temporary till NULL Values are fixed
988 if( ! Val ) {
Chris Lattner697954c2002-01-20 22:54:45 +0000989 cerr << "\t<*NULL*>";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000990 continue;
991 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000992
993 // if a label or a constant
Chris Lattnerdbe53042002-01-21 01:33:12 +0000994 if(isa<BasicBlock>(Val)) {
Chris Lattner697954c2002-01-20 22:54:45 +0000995 cerr << "\t"; printLabel( Op.getVRegValue () );
996 } else {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000997 // else it must be a register value
998 const int RegNum = Op.getAllocatedRegNum();
999
Chris Lattner697954c2002-01-20 22:54:45 +00001000 cerr << "\t" << "%" << MRI.getUnifiedRegName( RegNum );
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001001 if (Val->hasName() )
Chris Lattner697954c2002-01-20 22:54:45 +00001002 cerr << "(" << Val->getName() << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001003 else
Chris Lattner697954c2002-01-20 22:54:45 +00001004 cerr << "(" << Val << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001005
1006 if( Op.opIsDef() )
Chris Lattner697954c2002-01-20 22:54:45 +00001007 cerr << "*";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001008
1009 const LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
1010 if( LROfVal )
1011 if( LROfVal->hasSpillOffset() )
Chris Lattner697954c2002-01-20 22:54:45 +00001012 cerr << "$";
Ruchira Sasankae727f852001-09-18 22:43:57 +00001013 }
1014
1015 }
1016 else if(Op.getOperandType() == MachineOperand::MO_MachineRegister) {
Chris Lattner697954c2002-01-20 22:54:45 +00001017 cerr << "\t" << "%" << MRI.getUnifiedRegName(Op.getMachineRegNum());
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001018 }
1019
1020 else
Chris Lattner697954c2002-01-20 22:54:45 +00001021 cerr << "\t" << Op; // use dump field
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001022 }
1023
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001024
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001025
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001026 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
Chris Lattner0665a5f2002-02-05 01:43:49 +00001027 if( NumOfImpRefs > 0) {
Chris Lattner697954c2002-01-20 22:54:45 +00001028 cerr << "\tImplicit:";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001029
Chris Lattner0665a5f2002-02-05 01:43:49 +00001030 for(unsigned z=0; z < NumOfImpRefs; z++)
1031 cerr << RAV(MInst->getImplicitRef(z)) << "\t";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001032 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001033
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001034 } // for all machine instructions
1035
Chris Lattner697954c2002-01-20 22:54:45 +00001036 cerr << "\n";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001037
1038 } // for all BBs
1039
Chris Lattner697954c2002-01-20 22:54:45 +00001040 cerr << "\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001041}
1042
Ruchira Sasankae727f852001-09-18 22:43:57 +00001043
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001044#if 0
1045
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001046//----------------------------------------------------------------------------
1047//
1048//----------------------------------------------------------------------------
1049
1050void PhyRegAlloc::colorCallRetArgs()
1051{
1052
1053 CallRetInstrListType &CallRetInstList = LRI.getCallRetInstrList();
1054 CallRetInstrListType::const_iterator It = CallRetInstList.begin();
1055
1056 for( ; It != CallRetInstList.end(); ++It ) {
1057
Ruchira Sasankaa90e7702001-10-15 16:26:38 +00001058 const MachineInstr *const CRMI = *It;
1059 unsigned OpCode = CRMI->getOpCode();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001060
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001061 // get the added instructions for this Call/Ret instruciton
1062 AddedInstrns *AI = AddedInstrMap[ CRMI ];
1063 if ( !AI ) {
1064 AI = new AddedInstrns();
1065 AddedInstrMap[ CRMI ] = AI;
1066 }
1067
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001068 // Tmp stack poistions are needed by some calls that have spilled args
1069 // So reset it before we call each such method
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001070 //mcInfo.popAllTempValues(TM);
1071
1072
Vikram S. Adve12af1642001-11-08 04:48:50 +00001073
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001074 if (TM.getInstrInfo().isCall(OpCode))
1075 MRI.colorCallArgs(CRMI, LRI, AI, *this);
1076 else if (TM.getInstrInfo().isReturn(OpCode))
Ruchira Sasankaa90e7702001-10-15 16:26:38 +00001077 MRI.colorRetValue( CRMI, LRI, AI );
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001078 else
1079 assert(0 && "Non Call/Ret instrn in CallRetInstrList\n");
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001080 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001081}
1082
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001083#endif
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001084
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001085//----------------------------------------------------------------------------
1086
1087//----------------------------------------------------------------------------
1088void PhyRegAlloc::colorIncomingArgs()
1089{
1090 const BasicBlock *const FirstBB = Meth->front();
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001091 const MachineInstr *FirstMI = FirstBB->getMachineInstrVec().front();
1092 assert(FirstMI && "No machine instruction in entry BB");
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001093
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001094 AddedInstrns *AI = AddedInstrMap[FirstMI];
1095 if (!AI)
1096 AddedInstrMap[FirstMI] = AI = new AddedInstrns();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001097
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001098 MRI.colorMethodArgs(Meth, LRI, AI);
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001099}
1100
Ruchira Sasankae727f852001-09-18 22:43:57 +00001101
1102//----------------------------------------------------------------------------
1103// Used to generate a label for a basic block
1104//----------------------------------------------------------------------------
Chris Lattner697954c2002-01-20 22:54:45 +00001105void PhyRegAlloc::printLabel(const Value *const Val) {
1106 if (Val->hasName())
1107 cerr << Val->getName();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001108 else
Chris Lattner697954c2002-01-20 22:54:45 +00001109 cerr << "Label" << Val;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001110}
1111
1112
Ruchira Sasankae727f852001-09-18 22:43:57 +00001113//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001114// This method calls setSugColorUsable method of each live range. This
1115// will determine whether the suggested color of LR is really usable.
1116// A suggested color is not usable when the suggested color is volatile
1117// AND when there are call interferences
1118//----------------------------------------------------------------------------
1119
1120void PhyRegAlloc::markUnusableSugColors()
1121{
Chris Lattner697954c2002-01-20 22:54:45 +00001122 if(DEBUG_RA ) cerr << "\nmarking unusable suggested colors ...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001123
1124 // hash map iterator
1125 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
1126 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
1127
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001128 for(; HMI != HMIEnd ; ++HMI ) {
1129 if (HMI->first) {
1130 LiveRange *L = HMI->second; // get the LiveRange
1131 if (L) {
1132 if(L->hasSuggestedColor()) {
1133 int RCID = L->getRegClass()->getID();
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001134 if( MRI.isRegVolatile( RCID, L->getSuggestedColor()) &&
1135 L->isCallInterference() )
1136 L->setSuggestedColorUsable( false );
1137 else
1138 L->setSuggestedColorUsable( true );
1139 }
1140 } // if L->hasSuggestedColor()
1141 }
1142 } // for all LR's in hash map
1143}
1144
1145
1146
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001147//----------------------------------------------------------------------------
1148// The following method will set the stack offsets of the live ranges that
1149// are decided to be spillled. This must be called just after coloring the
1150// LRs using the graph coloring algo. For each live range that is spilled,
1151// this method allocate a new spill position on the stack.
1152//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001153
Chris Lattner37730942002-02-05 03:52:29 +00001154void PhyRegAlloc::allocateStackSpace4SpilledLRs() {
1155 if (DEBUG_RA) cerr << "\nsetting LR stack offsets ...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001156
Chris Lattner37730942002-02-05 03:52:29 +00001157 LiveRangeMapType::const_iterator HMI = LRI.getLiveRangeMap()->begin();
1158 LiveRangeMapType::const_iterator HMIEnd = LRI.getLiveRangeMap()->end();
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001159
Chris Lattner37730942002-02-05 03:52:29 +00001160 for( ; HMI != HMIEnd ; ++HMI) {
1161 if (HMI->first && HMI->second) {
1162 LiveRange *L = HMI->second; // get the LiveRange
1163 if (!L->hasColor()) // NOTE: ** allocating the size of long Type **
1164 L->setSpillOffFromFP(mcInfo.allocateSpilledValue(TM, Type::LongTy));
1165 }
1166 } // for all LR's in hash map
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001167}
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001168
1169
1170
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001171//----------------------------------------------------------------------------
Ruchira Sasankae727f852001-09-18 22:43:57 +00001172// The entry pont to Register Allocation
1173//----------------------------------------------------------------------------
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001174
1175void PhyRegAlloc::allocateRegisters()
1176{
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001177
1178 // make sure that we put all register classes into the RegClassList
1179 // before we call constructLiveRanges (now done in the constructor of
1180 // PhyRegAlloc class).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001181 //
1182 LRI.constructLiveRanges(); // create LR info
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001183
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001184 if (DEBUG_RA)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001185 LRI.printLiveRanges();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001186
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001187 createIGNodeListsAndIGs(); // create IGNode list and IGs
1188
1189 buildInterferenceGraphs(); // build IGs in all reg classes
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001190
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001191
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001192 if (DEBUG_RA) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001193 // print all LRs in all reg classes
1194 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1195 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001196
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001197 // print IGs in all register classes
1198 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1199 RegClassList[ rc ]->printIG();
1200 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001201
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001202
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001203 LRI.coalesceLRs(); // coalesce all live ranges
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001204
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +00001205
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001206 if( DEBUG_RA) {
1207 // print all LRs in all reg classes
1208 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1209 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001210
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001211 // print IGs in all register classes
1212 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1213 RegClassList[ rc ]->printIG();
1214 }
1215
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001216
1217 // mark un-usable suggested color before graph coloring algorithm.
1218 // When this is done, the graph coloring algo will not reserve
1219 // suggested color unnecessarily - they can be used by another LR
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001220 //
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001221 markUnusableSugColors();
1222
1223 // color all register classes using the graph coloring algo
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001224 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1225 RegClassList[ rc ]->colorAllRegs();
1226
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001227 // Atter grpah coloring, if some LRs did not receive a color (i.e, spilled)
1228 // a poistion for such spilled LRs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001229 //
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001230 allocateStackSpace4SpilledLRs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001231
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001232 mcInfo.popAllTempValues(TM); // TODO **Check
1233
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001234 // color incoming args - if the correct color was not received
1235 // insert code to copy to the correct register
1236 //
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001237 colorIncomingArgs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001238
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001239 // Now update the machine code with register names and add any
1240 // additional code inserted by the register allocator to the instruction
1241 // stream
1242 //
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001243 updateMachineCode();
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001244
Chris Lattner045e7c82001-09-19 16:26:23 +00001245 if (DEBUG_RA) {
Vikram S. Adve12af1642001-11-08 04:48:50 +00001246 MachineCodeForMethod::get(Meth).dump();
Chris Lattner045e7c82001-09-19 16:26:23 +00001247 printMachineCode(); // only for DEBUGGING
1248 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001249}
1250
Ruchira Sasankae727f852001-09-18 22:43:57 +00001251
1252