blob: 18d019e8f4cda16e462c86fc4265340cc0b0a4f9 [file] [log] [blame]
Vikram S. Adve12af1642001-11-08 04:48:50 +00001// $Id$
2//***************************************************************************
3// File:
4// PhyRegAlloc.cpp
5//
6// Purpose:
7// Register allocation for LLVM.
8//
9// History:
10// 9/10/01 - Ruchira Sasanka - created.
11//**************************************************************************/
Ruchira Sasanka8e604792001-09-14 21:18:34 +000012
Chris Lattner6dd98a62002-02-04 00:33:08 +000013#include "llvm/CodeGen/RegisterAllocation.h"
Vikram S. Adve12af1642001-11-08 04:48:50 +000014#include "llvm/CodeGen/PhyRegAlloc.h"
15#include "llvm/CodeGen/MachineInstr.h"
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000016#include "llvm/CodeGen/MachineCodeForMethod.h"
Chris Lattner0a8ed942002-02-04 05:56:09 +000017#include "llvm/Analysis/LiveVar/MethodLiveVarInfo.h"
Chris Lattner14ab1ce2002-02-04 17:48:00 +000018#include "llvm/Analysis/LoopInfo.h"
Vikram S. Adve12af1642001-11-08 04:48:50 +000019#include "llvm/Target/TargetMachine.h"
20#include "llvm/Target/MachineFrameInfo.h"
Chris Lattner30adeb62002-02-04 16:36:59 +000021#include "llvm/Method.h"
Chris Lattner37730942002-02-05 03:52:29 +000022#include "llvm/Type.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//----------------------------------------------------------------------------
Chris Lattner296b7732002-02-05 02:52:05 +0000154void PhyRegAlloc::addInterference(const Value *Def,
155 const ValueSet *LVSet,
156 bool isCallInst) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000157
Chris Lattner296b7732002-02-05 02:52:05 +0000158 ValueSet::const_iterator LIt = LVSet->begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000159
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,
Chris Lattner296b7732002-02-05 02:52:05 +0000210 const ValueSet *LVSetAft) {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000211
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
Chris Lattner296b7732002-02-05 02:52:05 +0000229 ValueSet::const_iterator LIt = LVSetAft->begin();
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000230
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: ";
Chris Lattner296b7732002-02-05 02:52:05 +0000241 printSet(*LR);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000242 }
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: " ;
Chris Lattner296b7732002-02-05 02:52:05 +0000252 printSet(*LR);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000253 }
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
Chris Lattner748697d2002-02-05 04:20:12 +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 //
Chris Lattner748697d2002-02-05 04:20:12 +0000296 const ValueSet &LVSetAI = LVI->getLiveVarSetAfterMInst(MInst, *BBI);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000297
298 const bool isCallInst = TM.getInstrInfo().isCall(MInst->getOpCode());
299
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000300 if( isCallInst ) {
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000301 // set the isCallInterference flag of each live range wich extends
302 // accross this call instruction. This information is used by graph
303 // coloring algo to avoid allocating volatile colors to live ranges
304 // that span across calls (since they have to be saved/restored)
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000305 //
Chris Lattner748697d2002-02-05 04:20:12 +0000306 setCallInterferences(MInst, &LVSetAI);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000307 }
308
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000309
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000310 // iterate over all MI operands to find defs
311 //
Chris Lattner2f898d22002-02-05 06:02:59 +0000312 for (MachineInstr::const_val_op_iterator OpI = MInst->begin(),
313 OpE = MInst->end(); OpI != OpE; ++OpI) {
314 if (OpI.isDef()) // create a new LR iff this operand is a def
Chris Lattner748697d2002-02-05 04:20:12 +0000315 addInterference(*OpI, &LVSetAI, isCallInst);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000316
317 // Calculate the spill cost of each live range
318 //
Chris Lattner2f898d22002-02-05 06:02:59 +0000319 LiveRange *LR = LRI.getLiveRangeForValue(*OpI);
320 if (LR) LR->addSpillCost(BBLoopDepthCost);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000321 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000322
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000323
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000324 // if there are multiple defs in this instruction e.g. in SETX
325 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000326 if (TM.getInstrInfo().isPseudoInstr(MInst->getOpCode()))
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000327 addInterf4PseudoInstr(MInst);
328
329
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000330 // Also add interference for any implicit definitions in a machine
331 // instr (currently, only calls have this).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000332 //
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000333 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
334 if( NumOfImpRefs > 0 ) {
335 for(unsigned z=0; z < NumOfImpRefs; z++)
336 if( MInst->implicitRefIsDefined(z) )
Chris Lattner748697d2002-02-05 04:20:12 +0000337 addInterference( MInst->getImplicitRef(z), &LVSetAI, isCallInst );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000338 }
339
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +0000340
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000341 } // for all machine instructions in BB
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000342
343 } // for all BBs in method
344
345
346 // add interferences for method arguments. Since there are no explict
347 // defs in method for args, we have to add them manually
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000348 //
349 addInterferencesForArgs();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000350
351 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000352 cerr << "Interference graphs calculted!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000353
354}
355
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000356
357
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000358//--------------------------------------------------------------------------
359// Pseudo instructions will be exapnded to multiple instructions by the
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000360// assembler. Consequently, all the opernds must get distinct registers.
361// Therefore, we mark all operands of a pseudo instruction as they interfere
362// with one another.
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000363//--------------------------------------------------------------------------
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000364void PhyRegAlloc::addInterf4PseudoInstr(const MachineInstr *MInst) {
365
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000366 bool setInterf = false;
367
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000368 // iterate over MI operands to find defs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000369 //
Chris Lattner2f898d22002-02-05 06:02:59 +0000370 for (MachineInstr::const_val_op_iterator It1 = MInst->begin(),
371 ItE = MInst->end(); It1 != ItE; ++It1) {
372 const LiveRange *LROfOp1 = LRI.getLiveRangeForValue(*It1);
373 assert((LROfOp1 || !It1.isDef()) && "No LR for Def in PSEUDO insruction");
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000374
Chris Lattner2f898d22002-02-05 06:02:59 +0000375 MachineInstr::const_val_op_iterator It2 = It1;
376 for(++It2; It2 != ItE; ++It2) {
377 const LiveRange *LROfOp2 = LRI.getLiveRangeForValue(*It2);
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000378
Chris Lattner2f898d22002-02-05 06:02:59 +0000379 if (LROfOp2) {
380 RegClass *RCOfOp1 = LROfOp1->getRegClass();
381 RegClass *RCOfOp2 = LROfOp2->getRegClass();
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000382
383 if( RCOfOp1 == RCOfOp2 ){
384 RCOfOp1->setInterference( LROfOp1, LROfOp2 );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000385 setInterf = true;
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000386 }
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000387 } // if Op2 has a LR
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000388 } // for all other defs in machine instr
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000389 } // for all operands in an instruction
390
Chris Lattner2f898d22002-02-05 06:02:59 +0000391 if (!setInterf && MInst->getNumOperands() > 2) {
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000392 cerr << "\nInterf not set for any operand in pseudo instr:\n";
393 cerr << *MInst;
394 assert(0 && "Interf not set for pseudo instr with > 2 operands" );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000395 }
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000396}
397
398
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000399
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000400//----------------------------------------------------------------------------
401// This method will add interferences for incoming arguments to a method.
402//----------------------------------------------------------------------------
Chris Lattner296b7732002-02-05 02:52:05 +0000403void PhyRegAlloc::addInterferencesForArgs() {
404 // get the InSet of root BB
Chris Lattner748697d2002-02-05 04:20:12 +0000405 const ValueSet &InSet = LVI->getInSetOfBB(Meth->front());
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000406
Chris Lattner296b7732002-02-05 02:52:05 +0000407 // get the argument list
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000408 const Method::ArgumentListType& ArgList = Meth->getArgumentList();
409
Chris Lattner296b7732002-02-05 02:52:05 +0000410 // get an iterator to arg list
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000411 Method::ArgumentListType::const_iterator ArgIt = ArgList.begin();
412
413
414 for( ; ArgIt != ArgList.end() ; ++ArgIt) { // for each argument
Chris Lattner748697d2002-02-05 04:20:12 +0000415 addInterference((Value*)*ArgIt, &InSet, false);// add interferences between
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000416 // args and LVars at start
Chris Lattner0665a5f2002-02-05 01:43:49 +0000417 if( DEBUG_RA > 1)
418 cerr << " - %% adding interference for argument "
419 << RAV((const Value *)*ArgIt) << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000420 }
421}
422
423
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000424
425
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000426//----------------------------------------------------------------------------
427// This method is called after register allocation is complete to set the
428// allocated reisters in the machine code. This code will add register numbers
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000429// to MachineOperands that contain a Value. Also it calls target specific
430// methods to produce caller saving instructions. At the end, it adds all
431// additional instructions produced by the register allocator to the
432// instruction stream.
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000433//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000434void PhyRegAlloc::updateMachineCode()
435{
436
437 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
438
439 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
440
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000441 // get the iterator for machine instructions
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000442 //
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000443 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
444 MachineCodeForBasicBlock::iterator MInstIterator = MIVec.begin();
445
446 // iterate over all the machine instructions in BB
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000447 //
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000448 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
449
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000450 MachineInstr *MInst = *MInstIterator;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000451
452 unsigned Opcode = MInst->getOpCode();
453
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000454 // do not process Phis
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000455 if (TM.getInstrInfo().isPhi(Opcode))
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000456 continue;
457
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000458 // Now insert speical instructions (if necessary) for call/return
459 // instructions.
460 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000461 if (TM.getInstrInfo().isCall(Opcode) ||
462 TM.getInstrInfo().isReturn(Opcode)) {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000463
464 AddedInstrns *AI = AddedInstrMap[ MInst];
465 if ( !AI ) {
466 AI = new AddedInstrns();
467 AddedInstrMap[ MInst ] = AI;
468 }
469
470 // Tmp stack poistions are needed by some calls that have spilled args
471 // So reset it before we call each such method
Ruchira Sasanka6a3db8c2002-01-07 21:09:06 +0000472 //
473 mcInfo.popAllTempValues(TM);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000474
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000475 if (TM.getInstrInfo().isCall(Opcode))
476 MRI.colorCallArgs(MInst, LRI, AI, *this, *BBI);
477 else if (TM.getInstrInfo().isReturn(Opcode))
478 MRI.colorRetValue(MInst, LRI, AI);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000479 }
480
481
482 /* -- Using above code instead of this
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000483
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000484 // if this machine instr is call, insert caller saving code
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000485
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000486 if( (TM.getInstrInfo()).isCall( MInst->getOpCode()) )
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000487 MRI.insertCallerSavingCode(MInst, *BBI, *this );
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000488
489 */
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000490
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000491
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000492 // reset the stack offset for temporary variables since we may
493 // need that to spill
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000494 // mcInfo.popAllTempValues(TM);
Ruchira Sasankaf90870f2001-11-15 22:02:06 +0000495 // TODO ** : do later
Vikram S. Adve12af1642001-11-08 04:48:50 +0000496
Chris Lattner7a176752001-12-04 00:03:30 +0000497 //for(MachineInstr::val_const_op_iterator OpI(MInst);!OpI.done();++OpI) {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000498
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000499
500 // Now replace set the registers for operands in the machine instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000501 //
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000502 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
503
504 MachineOperand& Op = MInst->getOperand(OpNum);
505
506 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
507 Op.getOperandType() == MachineOperand::MO_CCRegister) {
508
509 const Value *const Val = Op.getVRegValue();
510
511 // delete this condition checking later (must assert if Val is null)
Chris Lattner045e7c82001-09-19 16:26:23 +0000512 if( !Val) {
513 if (DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000514 cerr << "Warning: NULL Value found for operand\n";
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000515 continue;
516 }
517 assert( Val && "Value is NULL");
518
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000519 LiveRange *const LR = LRI.getLiveRangeForValue(Val);
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000520
521 if ( !LR ) {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000522
523 // nothing to worry if it's a const or a label
524
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000525 if (DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000526 cerr << "*NO LR for operand : " << Op ;
527 cerr << " [reg:" << Op.getAllocatedRegNum() << "]";
528 cerr << " in inst:\t" << *MInst << "\n";
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000529 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000530
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000531 // if register is not allocated, mark register as invalid
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000532 if( Op.getAllocatedRegNum() == -1)
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000533 Op.setRegForValue( MRI.getInvalidRegNum());
Ruchira Sasankae727f852001-09-18 22:43:57 +0000534
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000535
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000536 continue;
537 }
538
539 unsigned RCID = (LR->getRegClass())->getID();
540
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000541 if( LR->hasColor() ) {
542 Op.setRegForValue( MRI.getUnifiedRegNum(RCID, LR->getColor()) );
543 }
544 else {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000545
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000546 // LR did NOT receive a color (register). Now, insert spill code
547 // for spilled opeands in this machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000548
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000549 //assert(0 && "LR must be spilled");
550 insertCode4SpilledLR(LR, MInst, *BBI, OpNum );
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000551
552 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000553 }
554
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000555 } // for each operand
556
557
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000558 // Now add instructions that the register allocator inserts before/after
559 // this machine instructions (done only for calls/rets/incoming args)
560 // We do this here, to ensure that spill for an instruction is inserted
561 // closest as possible to an instruction (see above insertCode4Spill...)
562 //
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000563 // If there are instructions to be added, *before* this machine
564 // instruction, add them now.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000565 //
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000566 if( AddedInstrMap[ MInst ] ) {
Chris Lattner697954c2002-01-20 22:54:45 +0000567 std::deque<MachineInstr *> &IBef = AddedInstrMap[MInst]->InstrnsBefore;
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000568
569 if( ! IBef.empty() ) {
Chris Lattner697954c2002-01-20 22:54:45 +0000570 std::deque<MachineInstr *>::iterator AdIt;
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000571
572 for( AdIt = IBef.begin(); AdIt != IBef.end() ; ++AdIt ) {
573
574 if( DEBUG_RA) {
575 cerr << "For inst " << *MInst;
Chris Lattner697954c2002-01-20 22:54:45 +0000576 cerr << " PREPENDed instr: " << **AdIt << "\n";
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000577 }
578
579 MInstIterator = MIVec.insert( MInstIterator, *AdIt );
580 ++MInstIterator;
581 }
582
583 }
584
585 }
586
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000587 // If there are instructions to be added *after* this machine
588 // instruction, add them now
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000589 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000590 if(AddedInstrMap[MInst] &&
591 !AddedInstrMap[MInst]->InstrnsAfter.empty() ) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000592
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000593 // if there are delay slots for this instruction, the instructions
594 // added after it must really go after the delayed instruction(s)
595 // So, we move the InstrAfter of the current instruction to the
596 // corresponding delayed instruction
597
598 unsigned delay;
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000599 if ((delay=TM.getInstrInfo().getNumDelaySlots(MInst->getOpCode())) >0){
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000600 move2DelayedInstr(MInst, *(MInstIterator+delay) );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000601
Chris Lattner697954c2002-01-20 22:54:45 +0000602 if(DEBUG_RA) cerr<< "\nMoved an added instr after the delay slot";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000603 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000604
605 else {
606
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000607
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000608 // Here we can add the "instructions after" to the current
609 // instruction since there are no delay slots for this instruction
610
Chris Lattner697954c2002-01-20 22:54:45 +0000611 std::deque<MachineInstr *> &IAft = AddedInstrMap[MInst]->InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000612
613 if( ! IAft.empty() ) {
614
Chris Lattner697954c2002-01-20 22:54:45 +0000615 std::deque<MachineInstr *>::iterator AdIt;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000616
617 ++MInstIterator; // advance to the next instruction
618
619 for( AdIt = IAft.begin(); AdIt != IAft.end() ; ++AdIt ) {
620
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000621 if(DEBUG_RA) {
622 cerr << "For inst " << *MInst;
Chris Lattner697954c2002-01-20 22:54:45 +0000623 cerr << " APPENDed instr: " << **AdIt << "\n";
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000624 }
625
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000626 MInstIterator = MIVec.insert( MInstIterator, *AdIt );
627 ++MInstIterator;
628 }
629
630 // MInsterator already points to the next instr. Since the
631 // for loop also increments it, decrement it to point to the
632 // instruction added last
633 --MInstIterator;
634
635 }
636
637 } // if not delay
638
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000639 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000640
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000641 } // for each machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000642 }
643}
644
645
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000646
647//----------------------------------------------------------------------------
648// This method inserts spill code for AN operand whose LR was spilled.
649// This method may be called several times for a single machine instruction
650// if it contains many spilled operands. Each time it is called, it finds
651// a register which is not live at that instruction and also which is not
652// used by other spilled operands of the same instruction. Then it uses
653// this register temporarily to accomodate the spilled value.
654//----------------------------------------------------------------------------
655void PhyRegAlloc::insertCode4SpilledLR(const LiveRange *LR,
656 MachineInstr *MInst,
657 const BasicBlock *BB,
658 const unsigned OpNum) {
659
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000660 assert(! TM.getInstrInfo().isCall(MInst->getOpCode()) &&
661 (! TM.getInstrInfo().isReturn(MInst->getOpCode())) &&
662 "Arg of a call/ret must be handled elsewhere");
663
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000664 MachineOperand& Op = MInst->getOperand(OpNum);
665 bool isDef = MInst->operandIsDefined(OpNum);
666 unsigned RegType = MRI.getRegType( LR );
667 int SpillOff = LR->getSpillOffFromFP();
668 RegClass *RC = LR->getRegClass();
Chris Lattner748697d2002-02-05 04:20:12 +0000669 const ValueSet &LVSetBef = LVI->getLiveVarSetBeforeMInst(MInst, BB);
Vikram S. Adve00521d72001-11-12 23:26:35 +0000670
Chris Lattner697954c2002-01-20 22:54:45 +0000671 mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000672
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000673 MachineInstr *MIBef=NULL, *AdIMid=NULL, *MIAft=NULL;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000674
Chris Lattner748697d2002-02-05 04:20:12 +0000675 int TmpRegU = getUsableUniRegAtMI(RC, RegType, MInst,&LVSetBef, MIBef, MIAft);
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000676
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000677 // get the added instructions for this instruciton
678 AddedInstrns *AI = AddedInstrMap[ MInst ];
679 if ( !AI ) {
680 AI = new AddedInstrns();
681 AddedInstrMap[ MInst ] = AI;
682 }
683
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000684
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000685 if( !isDef ) {
686
687 // for a USE, we have to load the value of LR from stack to a TmpReg
688 // and use the TmpReg as one operand of instruction
689
690 // actual loading instruction
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000691 AdIMid = MRI.cpMem2RegMI(MRI.getFramePointer(), SpillOff, TmpRegU,RegType);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000692
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000693 if(MIBef)
694 AI->InstrnsBefore.push_back(MIBef);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000695
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000696 AI->InstrnsBefore.push_back(AdIMid);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000697
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000698 if(MIAft)
699 AI->InstrnsAfter.push_front(MIAft);
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000700
Chris Lattner296b7732002-02-05 02:52:05 +0000701 } else { // if this is a Def
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000702 // for a DEF, we have to store the value produced by this instruction
703 // on the stack position allocated for this LR
704
705 // actual storing instruction
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000706 AdIMid = MRI.cpReg2MemMI(TmpRegU, MRI.getFramePointer(), SpillOff,RegType);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000707
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000708 if (MIBef)
709 AI->InstrnsBefore.push_back(MIBef);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000710
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000711 AI->InstrnsAfter.push_front(AdIMid);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000712
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000713 if (MIAft)
714 AI->InstrnsAfter.push_front(MIAft);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000715
716 } // if !DEF
717
718 cerr << "\nFor Inst " << *MInst;
Chris Lattner296b7732002-02-05 02:52:05 +0000719 cerr << " - SPILLED LR: "; printSet(*LR);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000720 cerr << "\n - Added Instructions:";
Chris Lattner296b7732002-02-05 02:52:05 +0000721 if (MIBef) cerr << *MIBef;
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000722 cerr << *AdIMid;
Chris Lattner296b7732002-02-05 02:52:05 +0000723 if (MIAft) cerr << *MIAft;
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000724
Chris Lattner296b7732002-02-05 02:52:05 +0000725 Op.setRegForValue(TmpRegU); // set the opearnd
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000726}
727
728
729
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000730//----------------------------------------------------------------------------
731// We can use the following method to get a temporary register to be used
732// BEFORE any given machine instruction. If there is a register available,
733// this method will simply return that register and set MIBef = MIAft = NULL.
734// Otherwise, it will return a register and MIAft and MIBef will contain
735// two instructions used to free up this returned register.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000736// Returned register number is the UNIFIED register number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000737//----------------------------------------------------------------------------
738
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000739int PhyRegAlloc::getUsableUniRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000740 const int RegType,
741 const MachineInstr *MInst,
Chris Lattner296b7732002-02-05 02:52:05 +0000742 const ValueSet *LVSetBef,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000743 MachineInstr *MIBef,
744 MachineInstr *MIAft) {
745
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000746 int RegU = getUnusedUniRegAtMI(RC, MInst, LVSetBef);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000747
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000748
749 if( RegU != -1) {
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000750 // we found an unused register, so we can simply use it
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000751 MIBef = MIAft = NULL;
752 }
753 else {
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000754 // we couldn't find an unused register. Generate code to free up a reg by
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000755 // saving it on stack and restoring after the instruction
756
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000757 int TmpOff = mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
Vikram S. Adve12af1642001-11-08 04:48:50 +0000758
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000759 RegU = getUniRegNotUsedByThisInst(RC, MInst);
760 MIBef = MRI.cpReg2MemMI(RegU, MRI.getFramePointer(), TmpOff, RegType );
761 MIAft = MRI.cpMem2RegMI(MRI.getFramePointer(), TmpOff, RegU, RegType );
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000762 }
763
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000764 return RegU;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000765}
766
767//----------------------------------------------------------------------------
768// This method is called to get a new unused register that can be used to
769// accomodate a spilled value.
770// This method may be called several times for a single machine instruction
771// if it contains many spilled operands. Each time it is called, it finds
772// a register which is not live at that instruction and also which is not
773// used by other spilled operands of the same instruction.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000774// Return register number is relative to the register class. NOT
775// unified number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000776//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000777int PhyRegAlloc::getUnusedUniRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000778 const MachineInstr *MInst,
Chris Lattner296b7732002-02-05 02:52:05 +0000779 const ValueSet *LVSetBef) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000780
781 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
782
783 bool *IsColorUsedArr = RC->getIsColorUsedArr();
784
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000785 for(unsigned i=0; i < NumAvailRegs; i++) // Reset array
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000786 IsColorUsedArr[i] = false;
787
Chris Lattner296b7732002-02-05 02:52:05 +0000788 ValueSet::const_iterator LIt = LVSetBef->begin();
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000789
790 // for each live var in live variable set after machine inst
791 for( ; LIt != LVSetBef->end(); ++LIt) {
792
793 // get the live range corresponding to live var
794 LiveRange *const LRofLV = LRI.getLiveRangeForValue(*LIt );
795
796 // LR can be null if it is a const since a const
797 // doesn't have a dominating def - see Assumptions above
798 if( LRofLV )
799 if( LRofLV->hasColor() )
800 IsColorUsedArr[ LRofLV->getColor() ] = true;
801 }
802
803 // It is possible that one operand of this MInst was already spilled
804 // and it received some register temporarily. If that's the case,
805 // it is recorded in machine operand. We must skip such registers.
806
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000807 setRelRegsUsedByThisInst(RC, MInst);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000808
809 unsigned c; // find first unused color
810 for( c=0; c < NumAvailRegs; c++)
811 if( ! IsColorUsedArr[ c ] ) break;
812
813 if(c < NumAvailRegs)
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000814 return MRI.getUnifiedRegNum(RC->getID(), c);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000815 else
816 return -1;
817
818
819}
820
821
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000822//----------------------------------------------------------------------------
823// Get any other register in a register class, other than what is used
824// by operands of a machine instruction. Returns the unified reg number.
825//----------------------------------------------------------------------------
826int PhyRegAlloc::getUniRegNotUsedByThisInst(RegClass *RC,
827 const MachineInstr *MInst) {
828
829 bool *IsColorUsedArr = RC->getIsColorUsedArr();
830 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
831
832
833 for(unsigned i=0; i < NumAvailRegs ; i++) // Reset array
834 IsColorUsedArr[i] = false;
835
836 setRelRegsUsedByThisInst(RC, MInst);
837
838 unsigned c; // find first unused color
839 for( c=0; c < RC->getNumOfAvailRegs(); c++)
840 if( ! IsColorUsedArr[ c ] ) break;
841
842 if(c < NumAvailRegs)
843 return MRI.getUnifiedRegNum(RC->getID(), c);
844 else
845 assert( 0 && "FATAL: No free register could be found in reg class!!");
Chris Lattner697954c2002-01-20 22:54:45 +0000846 return 0;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000847}
848
849
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000850//----------------------------------------------------------------------------
851// This method modifies the IsColorUsedArr of the register class passed to it.
852// It sets the bits corresponding to the registers used by this machine
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000853// instructions. Both explicit and implicit operands are set.
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000854//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000855void PhyRegAlloc::setRelRegsUsedByThisInst(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000856 const MachineInstr *MInst ) {
857
858 bool *IsColorUsedArr = RC->getIsColorUsedArr();
859
860 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
861
862 const MachineOperand& Op = MInst->getOperand(OpNum);
863
864 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000865 Op.getOperandType() == MachineOperand::MO_CCRegister ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000866
867 const Value *const Val = Op.getVRegValue();
868
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000869 if( Val )
870 if( MRI.getRegClassIDOfValue(Val) == RC->getID() ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000871 int Reg;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000872 if( (Reg=Op.getAllocatedRegNum()) != -1) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000873 IsColorUsedArr[ Reg ] = true;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000874 }
875 else {
876 // it is possilbe that this operand still is not marked with
877 // a register but it has a LR and that received a color
878
879 LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
880 if( LROfVal)
881 if( LROfVal->hasColor() )
882 IsColorUsedArr[ LROfVal->getColor() ] = true;
883 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000884
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000885 } // if reg classes are the same
886 }
887 else if (Op.getOperandType() == MachineOperand::MO_MachineRegister) {
888 IsColorUsedArr[ Op.getMachineRegNum() ] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000889 }
890 }
891
892 // If there are implicit references, mark them as well
893
894 for(unsigned z=0; z < MInst->getNumImplicitRefs(); z++) {
895
896 LiveRange *const LRofImpRef =
897 LRI.getLiveRangeForValue( MInst->getImplicitRef(z) );
Chris Lattner697954c2002-01-20 22:54:45 +0000898
899 if(LRofImpRef && LRofImpRef->hasColor())
900 IsColorUsedArr[LRofImpRef->getColor()] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000901 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000902}
903
904
905
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000906
907
908
909
910
911//----------------------------------------------------------------------------
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000912// If there are delay slots for an instruction, the instructions
913// added after it must really go after the delayed instruction(s).
914// So, we move the InstrAfter of that instruction to the
915// corresponding delayed instruction using the following method.
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000916
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000917//----------------------------------------------------------------------------
918void PhyRegAlloc:: move2DelayedInstr(const MachineInstr *OrigMI,
919 const MachineInstr *DelayedMI) {
920
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000921 // "added after" instructions of the original instr
Chris Lattner697954c2002-01-20 22:54:45 +0000922 std::deque<MachineInstr *> &OrigAft = AddedInstrMap[OrigMI]->InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000923
924 // "added instructions" of the delayed instr
925 AddedInstrns *DelayAdI = AddedInstrMap[DelayedMI];
926
927 if(! DelayAdI ) { // create a new "added after" if necessary
928 DelayAdI = new AddedInstrns();
929 AddedInstrMap[DelayedMI] = DelayAdI;
930 }
931
932 // "added after" instructions of the delayed instr
Chris Lattner697954c2002-01-20 22:54:45 +0000933 std::deque<MachineInstr *> &DelayedAft = DelayAdI->InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000934
935 // go thru all the "added after instructions" of the original instruction
936 // and append them to the "addded after instructions" of the delayed
937 // instructions
Chris Lattner697954c2002-01-20 22:54:45 +0000938 DelayedAft.insert(DelayedAft.end(), OrigAft.begin(), OrigAft.end());
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000939
940 // empty the "added after instructions" of the original instruction
941 OrigAft.clear();
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000942}
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000943
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000944//----------------------------------------------------------------------------
945// This method prints the code with registers after register allocation is
946// complete.
947//----------------------------------------------------------------------------
948void PhyRegAlloc::printMachineCode()
949{
950
Chris Lattner697954c2002-01-20 22:54:45 +0000951 cerr << "\n;************** Method " << Meth->getName()
952 << " *****************\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000953
954 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
955
956 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
957
Chris Lattner697954c2002-01-20 22:54:45 +0000958 cerr << "\n"; printLabel( *BBI); cerr << ": ";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000959
960 // get the iterator for machine instructions
961 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
962 MachineCodeForBasicBlock::iterator MInstIterator = MIVec.begin();
963
964 // iterate over all the machine instructions in BB
965 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
966
967 MachineInstr *const MInst = *MInstIterator;
968
969
Chris Lattner697954c2002-01-20 22:54:45 +0000970 cerr << "\n\t";
971 cerr << TargetInstrDescriptors[MInst->getOpCode()].opCodeString;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000972
973
Chris Lattner7a176752001-12-04 00:03:30 +0000974 //for(MachineInstr::val_const_op_iterator OpI(MInst);!OpI.done();++OpI) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000975
976 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
977
978 MachineOperand& Op = MInst->getOperand(OpNum);
979
980 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasanka97b8b442001-10-18 22:36:26 +0000981 Op.getOperandType() == MachineOperand::MO_CCRegister /*||
982 Op.getOperandType() == MachineOperand::MO_PCRelativeDisp*/ ) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000983
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000984 const Value *const Val = Op.getVRegValue () ;
Ruchira Sasankae727f852001-09-18 22:43:57 +0000985 // ****this code is temporary till NULL Values are fixed
986 if( ! Val ) {
Chris Lattner697954c2002-01-20 22:54:45 +0000987 cerr << "\t<*NULL*>";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000988 continue;
989 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000990
991 // if a label or a constant
Chris Lattnerdbe53042002-01-21 01:33:12 +0000992 if(isa<BasicBlock>(Val)) {
Chris Lattner697954c2002-01-20 22:54:45 +0000993 cerr << "\t"; printLabel( Op.getVRegValue () );
994 } else {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000995 // else it must be a register value
996 const int RegNum = Op.getAllocatedRegNum();
997
Chris Lattner697954c2002-01-20 22:54:45 +0000998 cerr << "\t" << "%" << MRI.getUnifiedRegName( RegNum );
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000999 if (Val->hasName() )
Chris Lattner697954c2002-01-20 22:54:45 +00001000 cerr << "(" << Val->getName() << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001001 else
Chris Lattner697954c2002-01-20 22:54:45 +00001002 cerr << "(" << Val << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001003
1004 if( Op.opIsDef() )
Chris Lattner697954c2002-01-20 22:54:45 +00001005 cerr << "*";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001006
1007 const LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
1008 if( LROfVal )
1009 if( LROfVal->hasSpillOffset() )
Chris Lattner697954c2002-01-20 22:54:45 +00001010 cerr << "$";
Ruchira Sasankae727f852001-09-18 22:43:57 +00001011 }
1012
1013 }
1014 else if(Op.getOperandType() == MachineOperand::MO_MachineRegister) {
Chris Lattner697954c2002-01-20 22:54:45 +00001015 cerr << "\t" << "%" << MRI.getUnifiedRegName(Op.getMachineRegNum());
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001016 }
1017
1018 else
Chris Lattner697954c2002-01-20 22:54:45 +00001019 cerr << "\t" << Op; // use dump field
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001020 }
1021
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001022
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001023
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001024 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
Chris Lattner0665a5f2002-02-05 01:43:49 +00001025 if( NumOfImpRefs > 0) {
Chris Lattner697954c2002-01-20 22:54:45 +00001026 cerr << "\tImplicit:";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001027
Chris Lattner0665a5f2002-02-05 01:43:49 +00001028 for(unsigned z=0; z < NumOfImpRefs; z++)
1029 cerr << RAV(MInst->getImplicitRef(z)) << "\t";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001030 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001031
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001032 } // for all machine instructions
1033
Chris Lattner697954c2002-01-20 22:54:45 +00001034 cerr << "\n";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001035
1036 } // for all BBs
1037
Chris Lattner697954c2002-01-20 22:54:45 +00001038 cerr << "\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001039}
1040
Ruchira Sasankae727f852001-09-18 22:43:57 +00001041
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001042#if 0
1043
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001044//----------------------------------------------------------------------------
1045//
1046//----------------------------------------------------------------------------
1047
1048void PhyRegAlloc::colorCallRetArgs()
1049{
1050
1051 CallRetInstrListType &CallRetInstList = LRI.getCallRetInstrList();
1052 CallRetInstrListType::const_iterator It = CallRetInstList.begin();
1053
1054 for( ; It != CallRetInstList.end(); ++It ) {
1055
Ruchira Sasankaa90e7702001-10-15 16:26:38 +00001056 const MachineInstr *const CRMI = *It;
1057 unsigned OpCode = CRMI->getOpCode();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001058
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001059 // get the added instructions for this Call/Ret instruciton
1060 AddedInstrns *AI = AddedInstrMap[ CRMI ];
1061 if ( !AI ) {
1062 AI = new AddedInstrns();
1063 AddedInstrMap[ CRMI ] = AI;
1064 }
1065
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001066 // Tmp stack poistions are needed by some calls that have spilled args
1067 // So reset it before we call each such method
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001068 //mcInfo.popAllTempValues(TM);
1069
1070
Vikram S. Adve12af1642001-11-08 04:48:50 +00001071
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001072 if (TM.getInstrInfo().isCall(OpCode))
1073 MRI.colorCallArgs(CRMI, LRI, AI, *this);
1074 else if (TM.getInstrInfo().isReturn(OpCode))
Ruchira Sasankaa90e7702001-10-15 16:26:38 +00001075 MRI.colorRetValue( CRMI, LRI, AI );
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001076 else
1077 assert(0 && "Non Call/Ret instrn in CallRetInstrList\n");
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001078 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001079}
1080
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001081#endif
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001082
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001083//----------------------------------------------------------------------------
1084
1085//----------------------------------------------------------------------------
1086void PhyRegAlloc::colorIncomingArgs()
1087{
1088 const BasicBlock *const FirstBB = Meth->front();
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001089 const MachineInstr *FirstMI = FirstBB->getMachineInstrVec().front();
1090 assert(FirstMI && "No machine instruction in entry BB");
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001091
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001092 AddedInstrns *AI = AddedInstrMap[FirstMI];
1093 if (!AI)
1094 AddedInstrMap[FirstMI] = AI = new AddedInstrns();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001095
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001096 MRI.colorMethodArgs(Meth, LRI, AI);
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001097}
1098
Ruchira Sasankae727f852001-09-18 22:43:57 +00001099
1100//----------------------------------------------------------------------------
1101// Used to generate a label for a basic block
1102//----------------------------------------------------------------------------
Chris Lattner697954c2002-01-20 22:54:45 +00001103void PhyRegAlloc::printLabel(const Value *const Val) {
1104 if (Val->hasName())
1105 cerr << Val->getName();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001106 else
Chris Lattner697954c2002-01-20 22:54:45 +00001107 cerr << "Label" << Val;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001108}
1109
1110
Ruchira Sasankae727f852001-09-18 22:43:57 +00001111//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001112// This method calls setSugColorUsable method of each live range. This
1113// will determine whether the suggested color of LR is really usable.
1114// A suggested color is not usable when the suggested color is volatile
1115// AND when there are call interferences
1116//----------------------------------------------------------------------------
1117
1118void PhyRegAlloc::markUnusableSugColors()
1119{
Chris Lattner697954c2002-01-20 22:54:45 +00001120 if(DEBUG_RA ) cerr << "\nmarking unusable suggested colors ...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001121
1122 // hash map iterator
1123 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
1124 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
1125
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001126 for(; HMI != HMIEnd ; ++HMI ) {
1127 if (HMI->first) {
1128 LiveRange *L = HMI->second; // get the LiveRange
1129 if (L) {
1130 if(L->hasSuggestedColor()) {
1131 int RCID = L->getRegClass()->getID();
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001132 if( MRI.isRegVolatile( RCID, L->getSuggestedColor()) &&
1133 L->isCallInterference() )
1134 L->setSuggestedColorUsable( false );
1135 else
1136 L->setSuggestedColorUsable( true );
1137 }
1138 } // if L->hasSuggestedColor()
1139 }
1140 } // for all LR's in hash map
1141}
1142
1143
1144
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001145//----------------------------------------------------------------------------
1146// The following method will set the stack offsets of the live ranges that
1147// are decided to be spillled. This must be called just after coloring the
1148// LRs using the graph coloring algo. For each live range that is spilled,
1149// this method allocate a new spill position on the stack.
1150//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001151
Chris Lattner37730942002-02-05 03:52:29 +00001152void PhyRegAlloc::allocateStackSpace4SpilledLRs() {
1153 if (DEBUG_RA) cerr << "\nsetting LR stack offsets ...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001154
Chris Lattner37730942002-02-05 03:52:29 +00001155 LiveRangeMapType::const_iterator HMI = LRI.getLiveRangeMap()->begin();
1156 LiveRangeMapType::const_iterator HMIEnd = LRI.getLiveRangeMap()->end();
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001157
Chris Lattner37730942002-02-05 03:52:29 +00001158 for( ; HMI != HMIEnd ; ++HMI) {
1159 if (HMI->first && HMI->second) {
1160 LiveRange *L = HMI->second; // get the LiveRange
1161 if (!L->hasColor()) // NOTE: ** allocating the size of long Type **
1162 L->setSpillOffFromFP(mcInfo.allocateSpilledValue(TM, Type::LongTy));
1163 }
1164 } // for all LR's in hash map
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001165}
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001166
1167
1168
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001169//----------------------------------------------------------------------------
Ruchira Sasankae727f852001-09-18 22:43:57 +00001170// The entry pont to Register Allocation
1171//----------------------------------------------------------------------------
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001172
1173void PhyRegAlloc::allocateRegisters()
1174{
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001175
1176 // make sure that we put all register classes into the RegClassList
1177 // before we call constructLiveRanges (now done in the constructor of
1178 // PhyRegAlloc class).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001179 //
1180 LRI.constructLiveRanges(); // create LR info
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001181
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001182 if (DEBUG_RA)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001183 LRI.printLiveRanges();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001184
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001185 createIGNodeListsAndIGs(); // create IGNode list and IGs
1186
1187 buildInterferenceGraphs(); // build IGs in all reg classes
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001188
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001189
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001190 if (DEBUG_RA) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001191 // print all LRs in all reg classes
1192 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1193 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001194
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001195 // print IGs in all register classes
1196 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1197 RegClassList[ rc ]->printIG();
1198 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001199
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001200
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001201 LRI.coalesceLRs(); // coalesce all live ranges
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001202
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +00001203
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001204 if( DEBUG_RA) {
1205 // print all LRs in all reg classes
1206 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1207 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001208
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001209 // print IGs in all register classes
1210 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1211 RegClassList[ rc ]->printIG();
1212 }
1213
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001214
1215 // mark un-usable suggested color before graph coloring algorithm.
1216 // When this is done, the graph coloring algo will not reserve
1217 // suggested color unnecessarily - they can be used by another LR
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001218 //
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001219 markUnusableSugColors();
1220
1221 // color all register classes using the graph coloring algo
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001222 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1223 RegClassList[ rc ]->colorAllRegs();
1224
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001225 // Atter grpah coloring, if some LRs did not receive a color (i.e, spilled)
1226 // a poistion for such spilled LRs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001227 //
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001228 allocateStackSpace4SpilledLRs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001229
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001230 mcInfo.popAllTempValues(TM); // TODO **Check
1231
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001232 // color incoming args - if the correct color was not received
1233 // insert code to copy to the correct register
1234 //
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001235 colorIncomingArgs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001236
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001237 // Now update the machine code with register names and add any
1238 // additional code inserted by the register allocator to the instruction
1239 // stream
1240 //
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001241 updateMachineCode();
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001242
Chris Lattner045e7c82001-09-19 16:26:23 +00001243 if (DEBUG_RA) {
Vikram S. Adve12af1642001-11-08 04:48:50 +00001244 MachineCodeForMethod::get(Meth).dump();
Chris Lattner045e7c82001-09-19 16:26:23 +00001245 printMachineCode(); // only for DEBUGGING
1246 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001247}
1248
Ruchira Sasankae727f852001-09-18 22:43:57 +00001249
1250