blob: 1fbe5258b2c48fe189154d8a22dcd5c9303656bd [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);
Chris Lattner4911c352002-02-04 17:39:42 +000066 }
Chris Lattner2f9b28e2002-02-04 15:54:09 +000067 };
Chris Lattner6dd98a62002-02-04 00:33:08 +000068}
69
Chris Lattner2f9b28e2002-02-04 15:54:09 +000070MethodPass *getRegisterAllocator(TargetMachine &T) {
71 return new RegisterAllocator(T);
72}
Chris Lattner6dd98a62002-02-04 00:33:08 +000073
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000074//----------------------------------------------------------------------------
75// Constructor: Init local composite objects and create register classes.
76//----------------------------------------------------------------------------
Vikram S. Adve12af1642001-11-08 04:48:50 +000077PhyRegAlloc::PhyRegAlloc(Method *M,
Ruchira Sasanka8e604792001-09-14 21:18:34 +000078 const TargetMachine& tm,
Chris Lattner4911c352002-02-04 17:39:42 +000079 MethodLiveVarInfo *Lvi,
Chris Lattner14ab1ce2002-02-04 17:48:00 +000080 cfg::LoopInfo *LDC)
Chris Lattner697954c2002-01-20 22:54:45 +000081 : TM(tm), Meth(M),
Vikram S. Adve12af1642001-11-08 04:48:50 +000082 mcInfo(MachineCodeForMethod::get(M)),
83 LVI(Lvi), LRI(M, tm, RegClassList),
Ruchira Sasanka8e604792001-09-14 21:18:34 +000084 MRI( tm.getRegInfo() ),
85 NumOfRegClasses(MRI.getNumOfRegClasses()),
Chris Lattner4911c352002-02-04 17:39:42 +000086 LoopDepthCalc(LDC) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +000087
88 // create each RegisterClass and put in RegClassList
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000089 //
Chris Lattner697954c2002-01-20 22:54:45 +000090 for(unsigned int rc=0; rc < NumOfRegClasses; rc++)
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000091 RegClassList.push_back( new RegClass(M, MRI.getMachineRegClass(rc),
92 &ResColList) );
Ruchira Sasanka8e604792001-09-14 21:18:34 +000093}
94
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000095
96//----------------------------------------------------------------------------
97// Destructor: Deletes register classes
98//----------------------------------------------------------------------------
99PhyRegAlloc::~PhyRegAlloc() {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000100 for( unsigned int rc=0; rc < NumOfRegClasses; rc++)
101 delete RegClassList[rc];
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000102}
103
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000104//----------------------------------------------------------------------------
105// This method initally creates interference graphs (one in each reg class)
106// and IGNodeList (one in each IG). The actual nodes will be pushed later.
107//----------------------------------------------------------------------------
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000108void PhyRegAlloc::createIGNodeListsAndIGs() {
109 if (DEBUG_RA) cerr << "Creating LR lists ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000110
111 // hash map iterator
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000112 LiveRangeMapType::const_iterator HMI = LRI.getLiveRangeMap()->begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000113
114 // hash map end
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000115 LiveRangeMapType::const_iterator HMIEnd = LRI.getLiveRangeMap()->end();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000116
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000117 for (; HMI != HMIEnd ; ++HMI ) {
118 if (HMI->first) {
119 LiveRange *L = HMI->second; // get the LiveRange
120 if (!L) {
121 if( DEBUG_RA) {
Chris Lattner0665a5f2002-02-05 01:43:49 +0000122 cerr << "\n*?!?Warning: Null liver range found for: "
123 << RAV(HMI->first) << "\n";
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000124 }
125 continue;
126 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000127 // if the Value * is not null, and LR
128 // is not yet written to the IGNodeList
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000129 if( !(L->getUserIGNode()) ) {
130 RegClass *const RC = // RegClass of first value in the LR
131 RegClassList[ L->getRegClass()->getID() ];
132
133 RC->addLRToIG(L); // add this LR to an IG
134 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000135 }
136 }
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000137
138 // init RegClassList
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000139 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000140 RegClassList[rc]->createInterferenceGraph();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000141
142 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000143 cerr << "LRLists Created!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000144}
145
146
147
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000148
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000149//----------------------------------------------------------------------------
150// This method will add all interferences at for a given instruction.
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000151// Interence occurs only if the LR of Def (Inst or Arg) is of the same reg
152// class as that of live var. The live var passed to this function is the
153// LVset AFTER the instruction
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000154//----------------------------------------------------------------------------
Chris Lattner296b7732002-02-05 02:52:05 +0000155void PhyRegAlloc::addInterference(const Value *Def,
156 const ValueSet *LVSet,
157 bool isCallInst) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000158
Chris Lattner296b7732002-02-05 02:52:05 +0000159 ValueSet::const_iterator LIt = LVSet->begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000160
161 // get the live range of instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000162 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000163 const LiveRange *const LROfDef = LRI.getLiveRangeForValue( Def );
164
165 IGNode *const IGNodeOfDef = LROfDef->getUserIGNode();
166 assert( IGNodeOfDef );
167
168 RegClass *const RCOfDef = LROfDef->getRegClass();
169
170 // for each live var in live variable set
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000171 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000172 for( ; LIt != LVSet->end(); ++LIt) {
173
Chris Lattner0665a5f2002-02-05 01:43:49 +0000174 if (DEBUG_RA > 1)
175 cerr << "< Def=" << RAV(Def) << ", Lvar=" << RAV(*LIt) << "> ";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000176
177 // get the live range corresponding to live var
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000178 //
Chris Lattner0665a5f2002-02-05 01:43:49 +0000179 LiveRange *LROfVar = LRI.getLiveRangeForValue(*LIt);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000180
181 // LROfVar can be null if it is a const since a const
182 // doesn't have a dominating def - see Assumptions above
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000183 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000184 if (LROfVar) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000185 if(LROfDef == LROfVar) // do not set interf for same LR
186 continue;
187
188 // if 2 reg classes are the same set interference
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000189 //
Chris Lattner0665a5f2002-02-05 01:43:49 +0000190 if (RCOfDef == LROfVar->getRegClass()) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000191 RCOfDef->setInterference( LROfDef, LROfVar);
Chris Lattner0665a5f2002-02-05 01:43:49 +0000192 } else if (DEBUG_RA > 1) {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000193 // we will not have LRs for values not explicitly allocated in the
194 // instruction stream (e.g., constants)
Chris Lattner0665a5f2002-02-05 01:43:49 +0000195 cerr << " warning: no live range for " << RAV(*LIt) << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000196 }
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000197 }
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000198 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000199}
200
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000201
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000202
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000203//----------------------------------------------------------------------------
204// For a call instruction, this method sets the CallInterference flag in
205// the LR of each variable live int the Live Variable Set live after the
206// call instruction (except the return value of the call instruction - since
207// the return value does not interfere with that call itself).
208//----------------------------------------------------------------------------
209
210void PhyRegAlloc::setCallInterferences(const MachineInstr *MInst,
Chris Lattner296b7732002-02-05 02:52:05 +0000211 const ValueSet *LVSetAft) {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000212
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000213 // Now find the LR of the return value of the call
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000214 // We do this because, we look at the LV set *after* the instruction
215 // to determine, which LRs must be saved across calls. The return value
216 // of the call is live in this set - but it does not interfere with call
217 // (i.e., we can allocate a volatile register to the return value)
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000218 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000219 LiveRange *RetValLR = NULL;
Ruchira Sasankab3b6f532001-10-21 16:43:41 +0000220 const Value *RetVal = MRI.getCallInstRetVal( MInst );
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000221
Ruchira Sasankab3b6f532001-10-21 16:43:41 +0000222 if( RetVal ) {
223 RetValLR = LRI.getLiveRangeForValue( RetVal );
224 assert( RetValLR && "No LR for RetValue of call");
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000225 }
226
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000227 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000228 cerr << "\n For call inst: " << *MInst;
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000229
Chris Lattner296b7732002-02-05 02:52:05 +0000230 ValueSet::const_iterator LIt = LVSetAft->begin();
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000231
232 // for each live var in live variable set after machine inst
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000233 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000234 for( ; LIt != LVSetAft->end(); ++LIt) {
235
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000236 // get the live range corresponding to live var
237 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000238 LiveRange *const LR = LRI.getLiveRangeForValue(*LIt );
239
240 if( LR && DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000241 cerr << "\n\tLR Aft Call: ";
Chris Lattner296b7732002-02-05 02:52:05 +0000242 printSet(*LR);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000243 }
244
245
246 // LR can be null if it is a const since a const
247 // doesn't have a dominating def - see Assumptions above
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000248 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000249 if( LR && (LR != RetValLR) ) {
250 LR->setCallInterference();
251 if( DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000252 cerr << "\n ++Added call interf for LR: " ;
Chris Lattner296b7732002-02-05 02:52:05 +0000253 printSet(*LR);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000254 }
255 }
256
257 }
258
259}
260
261
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000262
263
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000264//----------------------------------------------------------------------------
265// This method will walk thru code and create interferences in the IG of
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000266// each RegClass. Also, this method calculates the spill cost of each
267// Live Range (it is done in this method to save another pass over the code).
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000268//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000269void PhyRegAlloc::buildInterferenceGraphs()
270{
271
Chris Lattner697954c2002-01-20 22:54:45 +0000272 if(DEBUG_RA) cerr << "Creating interference graphs ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000273
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000274 unsigned BBLoopDepthCost;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000275 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
276
277 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
278
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000279 // find the 10^(loop_depth) of this BB
280 //
Chris Lattner4911c352002-02-04 17:39:42 +0000281 BBLoopDepthCost = (unsigned) pow( 10.0, LoopDepthCalc->getLoopDepth(*BBI));
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000282
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000283 // get the iterator for machine instructions
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000284 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000285 const MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
286 MachineCodeForBasicBlock::const_iterator
287 MInstIterator = MIVec.begin();
288
289 // iterate over all the machine instructions in BB
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000290 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000291 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000292
Chris Lattner748697d2002-02-05 04:20:12 +0000293 const MachineInstr *MInst = *MInstIterator;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000294
295 // get the LV set after the instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000296 //
Chris Lattner748697d2002-02-05 04:20:12 +0000297 const ValueSet &LVSetAI = LVI->getLiveVarSetAfterMInst(MInst, *BBI);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000298
299 const bool isCallInst = TM.getInstrInfo().isCall(MInst->getOpCode());
300
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000301 if( isCallInst ) {
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000302 // set the isCallInterference flag of each live range wich extends
303 // accross this call instruction. This information is used by graph
304 // coloring algo to avoid allocating volatile colors to live ranges
305 // that span across calls (since they have to be saved/restored)
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000306 //
Chris Lattner748697d2002-02-05 04:20:12 +0000307 setCallInterferences(MInst, &LVSetAI);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000308 }
309
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000310
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000311 // iterate over all MI operands to find defs
312 //
Chris Lattner2f898d22002-02-05 06:02:59 +0000313 for (MachineInstr::const_val_op_iterator OpI = MInst->begin(),
314 OpE = MInst->end(); OpI != OpE; ++OpI) {
315 if (OpI.isDef()) // create a new LR iff this operand is a def
Chris Lattner748697d2002-02-05 04:20:12 +0000316 addInterference(*OpI, &LVSetAI, isCallInst);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000317
318 // Calculate the spill cost of each live range
319 //
Chris Lattner2f898d22002-02-05 06:02:59 +0000320 LiveRange *LR = LRI.getLiveRangeForValue(*OpI);
321 if (LR) LR->addSpillCost(BBLoopDepthCost);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000322 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000323
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000324
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000325 // if there are multiple defs in this instruction e.g. in SETX
326 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000327 if (TM.getInstrInfo().isPseudoInstr(MInst->getOpCode()))
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000328 addInterf4PseudoInstr(MInst);
329
330
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000331 // Also add interference for any implicit definitions in a machine
332 // instr (currently, only calls have this).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000333 //
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000334 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
335 if( NumOfImpRefs > 0 ) {
336 for(unsigned z=0; z < NumOfImpRefs; z++)
337 if( MInst->implicitRefIsDefined(z) )
Chris Lattner748697d2002-02-05 04:20:12 +0000338 addInterference( MInst->getImplicitRef(z), &LVSetAI, isCallInst );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000339 }
340
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +0000341
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000342 } // for all machine instructions in BB
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000343
344 } // for all BBs in method
345
346
347 // add interferences for method arguments. Since there are no explict
348 // defs in method for args, we have to add them manually
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000349 //
350 addInterferencesForArgs();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000351
352 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000353 cerr << "Interference graphs calculted!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000354
355}
356
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000357
358
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000359//--------------------------------------------------------------------------
360// Pseudo instructions will be exapnded to multiple instructions by the
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000361// assembler. Consequently, all the opernds must get distinct registers.
362// Therefore, we mark all operands of a pseudo instruction as they interfere
363// with one another.
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000364//--------------------------------------------------------------------------
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000365void PhyRegAlloc::addInterf4PseudoInstr(const MachineInstr *MInst) {
366
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000367 bool setInterf = false;
368
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000369 // iterate over MI operands to find defs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000370 //
Chris Lattner2f898d22002-02-05 06:02:59 +0000371 for (MachineInstr::const_val_op_iterator It1 = MInst->begin(),
372 ItE = MInst->end(); It1 != ItE; ++It1) {
373 const LiveRange *LROfOp1 = LRI.getLiveRangeForValue(*It1);
374 assert((LROfOp1 || !It1.isDef()) && "No LR for Def in PSEUDO insruction");
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000375
Chris Lattner2f898d22002-02-05 06:02:59 +0000376 MachineInstr::const_val_op_iterator It2 = It1;
377 for(++It2; It2 != ItE; ++It2) {
378 const LiveRange *LROfOp2 = LRI.getLiveRangeForValue(*It2);
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000379
Chris Lattner2f898d22002-02-05 06:02:59 +0000380 if (LROfOp2) {
381 RegClass *RCOfOp1 = LROfOp1->getRegClass();
382 RegClass *RCOfOp2 = LROfOp2->getRegClass();
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000383
384 if( RCOfOp1 == RCOfOp2 ){
385 RCOfOp1->setInterference( LROfOp1, LROfOp2 );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000386 setInterf = true;
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000387 }
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000388 } // if Op2 has a LR
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000389 } // for all other defs in machine instr
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000390 } // for all operands in an instruction
391
Chris Lattner2f898d22002-02-05 06:02:59 +0000392 if (!setInterf && MInst->getNumOperands() > 2) {
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000393 cerr << "\nInterf not set for any operand in pseudo instr:\n";
394 cerr << *MInst;
395 assert(0 && "Interf not set for pseudo instr with > 2 operands" );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000396 }
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000397}
398
399
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000400
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000401//----------------------------------------------------------------------------
402// This method will add interferences for incoming arguments to a method.
403//----------------------------------------------------------------------------
Chris Lattner296b7732002-02-05 02:52:05 +0000404void PhyRegAlloc::addInterferencesForArgs() {
405 // get the InSet of root BB
Chris Lattner748697d2002-02-05 04:20:12 +0000406 const ValueSet &InSet = LVI->getInSetOfBB(Meth->front());
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000407
Chris Lattner296b7732002-02-05 02:52:05 +0000408 // get the argument list
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000409 const Method::ArgumentListType& ArgList = Meth->getArgumentList();
410
Chris Lattner296b7732002-02-05 02:52:05 +0000411 // get an iterator to arg list
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000412 Method::ArgumentListType::const_iterator ArgIt = ArgList.begin();
413
414
415 for( ; ArgIt != ArgList.end() ; ++ArgIt) { // for each argument
Chris Lattner748697d2002-02-05 04:20:12 +0000416 addInterference((Value*)*ArgIt, &InSet, false);// add interferences between
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000417 // args and LVars at start
Chris Lattner0665a5f2002-02-05 01:43:49 +0000418 if( DEBUG_RA > 1)
419 cerr << " - %% adding interference for argument "
420 << RAV((const Value *)*ArgIt) << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000421 }
422}
423
424
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000425
426
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000427//----------------------------------------------------------------------------
428// This method is called after register allocation is complete to set the
429// allocated reisters in the machine code. This code will add register numbers
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000430// to MachineOperands that contain a Value. Also it calls target specific
431// methods to produce caller saving instructions. At the end, it adds all
432// additional instructions produced by the register allocator to the
433// instruction stream.
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000434//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000435void PhyRegAlloc::updateMachineCode()
436{
437
438 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
439
440 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
441
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000442 // get the iterator for machine instructions
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000443 //
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000444 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
445 MachineCodeForBasicBlock::iterator MInstIterator = MIVec.begin();
446
447 // iterate over all the machine instructions in BB
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000448 //
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000449 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
450
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000451 MachineInstr *MInst = *MInstIterator;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000452
453 unsigned Opcode = MInst->getOpCode();
454
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000455 // do not process Phis
Vikram S. Adve23a4c8f2002-03-18 03:37:19 +0000456 if (TM.getInstrInfo().isDummyPhiInstr(Opcode))
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000457 continue;
458
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000459 // Now insert speical instructions (if necessary) for call/return
460 // instructions.
461 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000462 if (TM.getInstrInfo().isCall(Opcode) ||
463 TM.getInstrInfo().isReturn(Opcode)) {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000464
465 AddedInstrns *AI = AddedInstrMap[ MInst];
466 if ( !AI ) {
467 AI = new AddedInstrns();
468 AddedInstrMap[ MInst ] = AI;
469 }
470
471 // Tmp stack poistions are needed by some calls that have spilled args
472 // So reset it before we call each such method
Ruchira Sasanka6a3db8c2002-01-07 21:09:06 +0000473 //
474 mcInfo.popAllTempValues(TM);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000475
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000476 if (TM.getInstrInfo().isCall(Opcode))
477 MRI.colorCallArgs(MInst, LRI, AI, *this, *BBI);
478 else if (TM.getInstrInfo().isReturn(Opcode))
479 MRI.colorRetValue(MInst, LRI, AI);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000480 }
481
482
483 /* -- Using above code instead of this
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000484
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000485 // if this machine instr is call, insert caller saving code
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000486
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000487 if( (TM.getInstrInfo()).isCall( MInst->getOpCode()) )
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000488 MRI.insertCallerSavingCode(MInst, *BBI, *this );
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000489
490 */
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000491
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000492
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000493 // reset the stack offset for temporary variables since we may
494 // need that to spill
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000495 // mcInfo.popAllTempValues(TM);
Ruchira Sasankaf90870f2001-11-15 22:02:06 +0000496 // TODO ** : do later
Vikram S. Adve12af1642001-11-08 04:48:50 +0000497
Chris Lattner7a176752001-12-04 00:03:30 +0000498 //for(MachineInstr::val_const_op_iterator OpI(MInst);!OpI.done();++OpI) {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000499
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000500
501 // Now replace set the registers for operands in the machine instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000502 //
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000503 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
504
505 MachineOperand& Op = MInst->getOperand(OpNum);
506
507 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
508 Op.getOperandType() == MachineOperand::MO_CCRegister) {
509
510 const Value *const Val = Op.getVRegValue();
511
512 // delete this condition checking later (must assert if Val is null)
Chris Lattner045e7c82001-09-19 16:26:23 +0000513 if( !Val) {
514 if (DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000515 cerr << "Warning: NULL Value found for operand\n";
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000516 continue;
517 }
518 assert( Val && "Value is NULL");
519
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000520 LiveRange *const LR = LRI.getLiveRangeForValue(Val);
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000521
522 if ( !LR ) {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000523
524 // nothing to worry if it's a const or a label
525
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000526 if (DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000527 cerr << "*NO LR for operand : " << Op ;
528 cerr << " [reg:" << Op.getAllocatedRegNum() << "]";
529 cerr << " in inst:\t" << *MInst << "\n";
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000530 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000531
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000532 // if register is not allocated, mark register as invalid
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000533 if( Op.getAllocatedRegNum() == -1)
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000534 Op.setRegForValue( MRI.getInvalidRegNum());
Ruchira Sasankae727f852001-09-18 22:43:57 +0000535
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000536
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000537 continue;
538 }
539
540 unsigned RCID = (LR->getRegClass())->getID();
541
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000542 if( LR->hasColor() ) {
543 Op.setRegForValue( MRI.getUnifiedRegNum(RCID, LR->getColor()) );
544 }
545 else {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000546
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000547 // LR did NOT receive a color (register). Now, insert spill code
548 // for spilled opeands in this machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000549
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000550 //assert(0 && "LR must be spilled");
551 insertCode4SpilledLR(LR, MInst, *BBI, OpNum );
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000552
553 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000554 }
555
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000556 } // for each operand
557
558
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000559 // Now add instructions that the register allocator inserts before/after
560 // this machine instructions (done only for calls/rets/incoming args)
561 // We do this here, to ensure that spill for an instruction is inserted
562 // closest as possible to an instruction (see above insertCode4Spill...)
563 //
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000564 // If there are instructions to be added, *before* this machine
565 // instruction, add them now.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000566 //
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000567 if( AddedInstrMap[ MInst ] ) {
Chris Lattner697954c2002-01-20 22:54:45 +0000568 std::deque<MachineInstr *> &IBef = AddedInstrMap[MInst]->InstrnsBefore;
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000569
570 if( ! IBef.empty() ) {
Chris Lattner697954c2002-01-20 22:54:45 +0000571 std::deque<MachineInstr *>::iterator AdIt;
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000572
573 for( AdIt = IBef.begin(); AdIt != IBef.end() ; ++AdIt ) {
574
575 if( DEBUG_RA) {
576 cerr << "For inst " << *MInst;
Chris Lattner697954c2002-01-20 22:54:45 +0000577 cerr << " PREPENDed instr: " << **AdIt << "\n";
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000578 }
579
580 MInstIterator = MIVec.insert( MInstIterator, *AdIt );
581 ++MInstIterator;
582 }
583
584 }
585
586 }
587
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000588 // If there are instructions to be added *after* this machine
589 // instruction, add them now
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000590 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000591 if(AddedInstrMap[MInst] &&
592 !AddedInstrMap[MInst]->InstrnsAfter.empty() ) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000593
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000594 // if there are delay slots for this instruction, the instructions
595 // added after it must really go after the delayed instruction(s)
596 // So, we move the InstrAfter of the current instruction to the
597 // corresponding delayed instruction
598
599 unsigned delay;
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000600 if ((delay=TM.getInstrInfo().getNumDelaySlots(MInst->getOpCode())) >0){
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000601 move2DelayedInstr(MInst, *(MInstIterator+delay) );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000602
Chris Lattner697954c2002-01-20 22:54:45 +0000603 if(DEBUG_RA) cerr<< "\nMoved an added instr after the delay slot";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000604 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000605
606 else {
607
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000608
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000609 // Here we can add the "instructions after" to the current
610 // instruction since there are no delay slots for this instruction
611
Chris Lattner697954c2002-01-20 22:54:45 +0000612 std::deque<MachineInstr *> &IAft = AddedInstrMap[MInst]->InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000613
614 if( ! IAft.empty() ) {
615
Chris Lattner697954c2002-01-20 22:54:45 +0000616 std::deque<MachineInstr *>::iterator AdIt;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000617
618 ++MInstIterator; // advance to the next instruction
619
620 for( AdIt = IAft.begin(); AdIt != IAft.end() ; ++AdIt ) {
621
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000622 if(DEBUG_RA) {
623 cerr << "For inst " << *MInst;
Chris Lattner697954c2002-01-20 22:54:45 +0000624 cerr << " APPENDed instr: " << **AdIt << "\n";
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000625 }
626
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000627 MInstIterator = MIVec.insert( MInstIterator, *AdIt );
628 ++MInstIterator;
629 }
630
631 // MInsterator already points to the next instr. Since the
632 // for loop also increments it, decrement it to point to the
633 // instruction added last
634 --MInstIterator;
635
636 }
637
638 } // if not delay
639
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000640 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000641
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000642 } // for each machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000643 }
644}
645
646
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000647
648//----------------------------------------------------------------------------
649// This method inserts spill code for AN operand whose LR was spilled.
650// This method may be called several times for a single machine instruction
651// if it contains many spilled operands. Each time it is called, it finds
652// a register which is not live at that instruction and also which is not
653// used by other spilled operands of the same instruction. Then it uses
654// this register temporarily to accomodate the spilled value.
655//----------------------------------------------------------------------------
656void PhyRegAlloc::insertCode4SpilledLR(const LiveRange *LR,
657 MachineInstr *MInst,
658 const BasicBlock *BB,
659 const unsigned OpNum) {
660
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000661 assert(! TM.getInstrInfo().isCall(MInst->getOpCode()) &&
662 (! TM.getInstrInfo().isReturn(MInst->getOpCode())) &&
663 "Arg of a call/ret must be handled elsewhere");
664
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000665 MachineOperand& Op = MInst->getOperand(OpNum);
666 bool isDef = MInst->operandIsDefined(OpNum);
667 unsigned RegType = MRI.getRegType( LR );
668 int SpillOff = LR->getSpillOffFromFP();
669 RegClass *RC = LR->getRegClass();
Chris Lattner748697d2002-02-05 04:20:12 +0000670 const ValueSet &LVSetBef = LVI->getLiveVarSetBeforeMInst(MInst, BB);
Vikram S. Adve00521d72001-11-12 23:26:35 +0000671
Chris Lattner697954c2002-01-20 22:54:45 +0000672 mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000673
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000674 MachineInstr *MIBef=NULL, *AdIMid=NULL, *MIAft=NULL;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000675
Chris Lattner748697d2002-02-05 04:20:12 +0000676 int TmpRegU = getUsableUniRegAtMI(RC, RegType, MInst,&LVSetBef, MIBef, MIAft);
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000677
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000678 // get the added instructions for this instruciton
679 AddedInstrns *AI = AddedInstrMap[ MInst ];
680 if ( !AI ) {
681 AI = new AddedInstrns();
682 AddedInstrMap[ MInst ] = AI;
683 }
684
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000685
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000686 if( !isDef ) {
687
688 // for a USE, we have to load the value of LR from stack to a TmpReg
689 // and use the TmpReg as one operand of instruction
690
691 // actual loading instruction
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000692 AdIMid = MRI.cpMem2RegMI(MRI.getFramePointer(), SpillOff, TmpRegU,RegType);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000693
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000694 if(MIBef)
695 AI->InstrnsBefore.push_back(MIBef);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000696
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000697 AI->InstrnsBefore.push_back(AdIMid);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000698
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000699 if(MIAft)
700 AI->InstrnsAfter.push_front(MIAft);
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000701
Chris Lattner296b7732002-02-05 02:52:05 +0000702 } else { // if this is a Def
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000703 // for a DEF, we have to store the value produced by this instruction
704 // on the stack position allocated for this LR
705
706 // actual storing instruction
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000707 AdIMid = MRI.cpReg2MemMI(TmpRegU, MRI.getFramePointer(), SpillOff,RegType);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000708
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000709 if (MIBef)
710 AI->InstrnsBefore.push_back(MIBef);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000711
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000712 AI->InstrnsAfter.push_front(AdIMid);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000713
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000714 if (MIAft)
715 AI->InstrnsAfter.push_front(MIAft);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000716
717 } // if !DEF
718
719 cerr << "\nFor Inst " << *MInst;
Chris Lattner296b7732002-02-05 02:52:05 +0000720 cerr << " - SPILLED LR: "; printSet(*LR);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000721 cerr << "\n - Added Instructions:";
Chris Lattner296b7732002-02-05 02:52:05 +0000722 if (MIBef) cerr << *MIBef;
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000723 cerr << *AdIMid;
Chris Lattner296b7732002-02-05 02:52:05 +0000724 if (MIAft) cerr << *MIAft;
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000725
Chris Lattner296b7732002-02-05 02:52:05 +0000726 Op.setRegForValue(TmpRegU); // set the opearnd
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000727}
728
729
730
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000731//----------------------------------------------------------------------------
732// We can use the following method to get a temporary register to be used
733// BEFORE any given machine instruction. If there is a register available,
734// this method will simply return that register and set MIBef = MIAft = NULL.
735// Otherwise, it will return a register and MIAft and MIBef will contain
736// two instructions used to free up this returned register.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000737// Returned register number is the UNIFIED register number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000738//----------------------------------------------------------------------------
739
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000740int PhyRegAlloc::getUsableUniRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000741 const int RegType,
742 const MachineInstr *MInst,
Chris Lattner296b7732002-02-05 02:52:05 +0000743 const ValueSet *LVSetBef,
Vikram S. Adve23a4c8f2002-03-18 03:37:19 +0000744 MachineInstr *&MIBef,
745 MachineInstr *&MIAft) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000746
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000747 int RegU = getUnusedUniRegAtMI(RC, MInst, LVSetBef);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000748
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000749
750 if( RegU != -1) {
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000751 // we found an unused register, so we can simply use it
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000752 MIBef = MIAft = NULL;
753 }
754 else {
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000755 // we couldn't find an unused register. Generate code to free up a reg by
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000756 // saving it on stack and restoring after the instruction
757
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000758 int TmpOff = mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
Vikram S. Adve12af1642001-11-08 04:48:50 +0000759
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000760 RegU = getUniRegNotUsedByThisInst(RC, MInst);
761 MIBef = MRI.cpReg2MemMI(RegU, MRI.getFramePointer(), TmpOff, RegType );
762 MIAft = MRI.cpMem2RegMI(MRI.getFramePointer(), TmpOff, RegU, RegType );
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000763 }
764
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000765 return RegU;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000766}
767
768//----------------------------------------------------------------------------
769// This method is called to get a new unused register that can be used to
770// accomodate a spilled value.
771// This method may be called several times for a single machine instruction
772// if it contains many spilled operands. Each time it is called, it finds
773// a register which is not live at that instruction and also which is not
774// used by other spilled operands of the same instruction.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000775// Return register number is relative to the register class. NOT
776// unified number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000777//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000778int PhyRegAlloc::getUnusedUniRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000779 const MachineInstr *MInst,
Chris Lattner296b7732002-02-05 02:52:05 +0000780 const ValueSet *LVSetBef) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000781
782 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
783
784 bool *IsColorUsedArr = RC->getIsColorUsedArr();
785
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000786 for(unsigned i=0; i < NumAvailRegs; i++) // Reset array
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000787 IsColorUsedArr[i] = false;
788
Chris Lattner296b7732002-02-05 02:52:05 +0000789 ValueSet::const_iterator LIt = LVSetBef->begin();
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000790
791 // for each live var in live variable set after machine inst
792 for( ; LIt != LVSetBef->end(); ++LIt) {
793
794 // get the live range corresponding to live var
795 LiveRange *const LRofLV = LRI.getLiveRangeForValue(*LIt );
796
797 // LR can be null if it is a const since a const
798 // doesn't have a dominating def - see Assumptions above
799 if( LRofLV )
800 if( LRofLV->hasColor() )
801 IsColorUsedArr[ LRofLV->getColor() ] = true;
802 }
803
804 // It is possible that one operand of this MInst was already spilled
805 // and it received some register temporarily. If that's the case,
806 // it is recorded in machine operand. We must skip such registers.
807
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000808 setRelRegsUsedByThisInst(RC, MInst);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000809
810 unsigned c; // find first unused color
811 for( c=0; c < NumAvailRegs; c++)
812 if( ! IsColorUsedArr[ c ] ) break;
813
814 if(c < NumAvailRegs)
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000815 return MRI.getUnifiedRegNum(RC->getID(), c);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000816 else
817 return -1;
818
819
820}
821
822
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000823//----------------------------------------------------------------------------
824// Get any other register in a register class, other than what is used
825// by operands of a machine instruction. Returns the unified reg number.
826//----------------------------------------------------------------------------
827int PhyRegAlloc::getUniRegNotUsedByThisInst(RegClass *RC,
828 const MachineInstr *MInst) {
829
830 bool *IsColorUsedArr = RC->getIsColorUsedArr();
831 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
832
833
834 for(unsigned i=0; i < NumAvailRegs ; i++) // Reset array
835 IsColorUsedArr[i] = false;
836
837 setRelRegsUsedByThisInst(RC, MInst);
838
839 unsigned c; // find first unused color
840 for( c=0; c < RC->getNumOfAvailRegs(); c++)
841 if( ! IsColorUsedArr[ c ] ) break;
842
843 if(c < NumAvailRegs)
844 return MRI.getUnifiedRegNum(RC->getID(), c);
845 else
846 assert( 0 && "FATAL: No free register could be found in reg class!!");
Chris Lattner697954c2002-01-20 22:54:45 +0000847 return 0;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000848}
849
850
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000851//----------------------------------------------------------------------------
852// This method modifies the IsColorUsedArr of the register class passed to it.
853// It sets the bits corresponding to the registers used by this machine
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000854// instructions. Both explicit and implicit operands are set.
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000855//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000856void PhyRegAlloc::setRelRegsUsedByThisInst(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000857 const MachineInstr *MInst ) {
858
859 bool *IsColorUsedArr = RC->getIsColorUsedArr();
860
861 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
862
863 const MachineOperand& Op = MInst->getOperand(OpNum);
864
865 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000866 Op.getOperandType() == MachineOperand::MO_CCRegister ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000867
868 const Value *const Val = Op.getVRegValue();
869
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000870 if( Val )
871 if( MRI.getRegClassIDOfValue(Val) == RC->getID() ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000872 int Reg;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000873 if( (Reg=Op.getAllocatedRegNum()) != -1) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000874 IsColorUsedArr[ Reg ] = true;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000875 }
876 else {
877 // it is possilbe that this operand still is not marked with
878 // a register but it has a LR and that received a color
879
880 LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
881 if( LROfVal)
882 if( LROfVal->hasColor() )
883 IsColorUsedArr[ LROfVal->getColor() ] = true;
884 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000885
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000886 } // if reg classes are the same
887 }
888 else if (Op.getOperandType() == MachineOperand::MO_MachineRegister) {
889 IsColorUsedArr[ Op.getMachineRegNum() ] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000890 }
891 }
892
893 // If there are implicit references, mark them as well
894
895 for(unsigned z=0; z < MInst->getNumImplicitRefs(); z++) {
896
897 LiveRange *const LRofImpRef =
898 LRI.getLiveRangeForValue( MInst->getImplicitRef(z) );
Chris Lattner697954c2002-01-20 22:54:45 +0000899
900 if(LRofImpRef && LRofImpRef->hasColor())
901 IsColorUsedArr[LRofImpRef->getColor()] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000902 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000903}
904
905
906
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000907
908
909
910
911
912//----------------------------------------------------------------------------
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000913// If there are delay slots for an instruction, the instructions
914// added after it must really go after the delayed instruction(s).
915// So, we move the InstrAfter of that instruction to the
916// corresponding delayed instruction using the following method.
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000917
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000918//----------------------------------------------------------------------------
919void PhyRegAlloc:: move2DelayedInstr(const MachineInstr *OrigMI,
920 const MachineInstr *DelayedMI) {
921
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000922 // "added after" instructions of the original instr
Chris Lattner697954c2002-01-20 22:54:45 +0000923 std::deque<MachineInstr *> &OrigAft = AddedInstrMap[OrigMI]->InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000924
925 // "added instructions" of the delayed instr
926 AddedInstrns *DelayAdI = AddedInstrMap[DelayedMI];
927
928 if(! DelayAdI ) { // create a new "added after" if necessary
929 DelayAdI = new AddedInstrns();
930 AddedInstrMap[DelayedMI] = DelayAdI;
931 }
932
933 // "added after" instructions of the delayed instr
Chris Lattner697954c2002-01-20 22:54:45 +0000934 std::deque<MachineInstr *> &DelayedAft = DelayAdI->InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000935
936 // go thru all the "added after instructions" of the original instruction
937 // and append them to the "addded after instructions" of the delayed
938 // instructions
Chris Lattner697954c2002-01-20 22:54:45 +0000939 DelayedAft.insert(DelayedAft.end(), OrigAft.begin(), OrigAft.end());
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000940
941 // empty the "added after instructions" of the original instruction
942 OrigAft.clear();
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000943}
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000944
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000945//----------------------------------------------------------------------------
946// This method prints the code with registers after register allocation is
947// complete.
948//----------------------------------------------------------------------------
949void PhyRegAlloc::printMachineCode()
950{
951
Chris Lattner697954c2002-01-20 22:54:45 +0000952 cerr << "\n;************** Method " << Meth->getName()
953 << " *****************\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000954
955 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
956
957 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
958
Chris Lattner697954c2002-01-20 22:54:45 +0000959 cerr << "\n"; printLabel( *BBI); cerr << ": ";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000960
961 // get the iterator for machine instructions
962 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
963 MachineCodeForBasicBlock::iterator MInstIterator = MIVec.begin();
964
965 // iterate over all the machine instructions in BB
966 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
967
968 MachineInstr *const MInst = *MInstIterator;
969
970
Chris Lattner697954c2002-01-20 22:54:45 +0000971 cerr << "\n\t";
972 cerr << TargetInstrDescriptors[MInst->getOpCode()].opCodeString;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000973
974
Chris Lattner7a176752001-12-04 00:03:30 +0000975 //for(MachineInstr::val_const_op_iterator OpI(MInst);!OpI.done();++OpI) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000976
977 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
978
979 MachineOperand& Op = MInst->getOperand(OpNum);
980
981 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasanka97b8b442001-10-18 22:36:26 +0000982 Op.getOperandType() == MachineOperand::MO_CCRegister /*||
983 Op.getOperandType() == MachineOperand::MO_PCRelativeDisp*/ ) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000984
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000985 const Value *const Val = Op.getVRegValue () ;
Ruchira Sasankae727f852001-09-18 22:43:57 +0000986 // ****this code is temporary till NULL Values are fixed
987 if( ! Val ) {
Chris Lattner697954c2002-01-20 22:54:45 +0000988 cerr << "\t<*NULL*>";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000989 continue;
990 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000991
992 // if a label or a constant
Chris Lattnerdbe53042002-01-21 01:33:12 +0000993 if(isa<BasicBlock>(Val)) {
Chris Lattner697954c2002-01-20 22:54:45 +0000994 cerr << "\t"; printLabel( Op.getVRegValue () );
995 } else {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000996 // else it must be a register value
997 const int RegNum = Op.getAllocatedRegNum();
998
Chris Lattner697954c2002-01-20 22:54:45 +0000999 cerr << "\t" << "%" << MRI.getUnifiedRegName( RegNum );
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001000 if (Val->hasName() )
Chris Lattner697954c2002-01-20 22:54:45 +00001001 cerr << "(" << Val->getName() << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001002 else
Chris Lattner697954c2002-01-20 22:54:45 +00001003 cerr << "(" << Val << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001004
1005 if( Op.opIsDef() )
Chris Lattner697954c2002-01-20 22:54:45 +00001006 cerr << "*";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001007
1008 const LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
1009 if( LROfVal )
1010 if( LROfVal->hasSpillOffset() )
Chris Lattner697954c2002-01-20 22:54:45 +00001011 cerr << "$";
Ruchira Sasankae727f852001-09-18 22:43:57 +00001012 }
1013
1014 }
1015 else if(Op.getOperandType() == MachineOperand::MO_MachineRegister) {
Chris Lattner697954c2002-01-20 22:54:45 +00001016 cerr << "\t" << "%" << MRI.getUnifiedRegName(Op.getMachineRegNum());
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001017 }
1018
1019 else
Chris Lattner697954c2002-01-20 22:54:45 +00001020 cerr << "\t" << Op; // use dump field
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001021 }
1022
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001023
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001024
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001025 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
Chris Lattner0665a5f2002-02-05 01:43:49 +00001026 if( NumOfImpRefs > 0) {
Chris Lattner697954c2002-01-20 22:54:45 +00001027 cerr << "\tImplicit:";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001028
Chris Lattner0665a5f2002-02-05 01:43:49 +00001029 for(unsigned z=0; z < NumOfImpRefs; z++)
1030 cerr << RAV(MInst->getImplicitRef(z)) << "\t";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001031 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001032
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001033 } // for all machine instructions
1034
Chris Lattner697954c2002-01-20 22:54:45 +00001035 cerr << "\n";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001036
1037 } // for all BBs
1038
Chris Lattner697954c2002-01-20 22:54:45 +00001039 cerr << "\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001040}
1041
Ruchira Sasankae727f852001-09-18 22:43:57 +00001042
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001043#if 0
1044
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001045//----------------------------------------------------------------------------
1046//
1047//----------------------------------------------------------------------------
1048
1049void PhyRegAlloc::colorCallRetArgs()
1050{
1051
1052 CallRetInstrListType &CallRetInstList = LRI.getCallRetInstrList();
1053 CallRetInstrListType::const_iterator It = CallRetInstList.begin();
1054
1055 for( ; It != CallRetInstList.end(); ++It ) {
1056
Ruchira Sasankaa90e7702001-10-15 16:26:38 +00001057 const MachineInstr *const CRMI = *It;
1058 unsigned OpCode = CRMI->getOpCode();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001059
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001060 // get the added instructions for this Call/Ret instruciton
1061 AddedInstrns *AI = AddedInstrMap[ CRMI ];
1062 if ( !AI ) {
1063 AI = new AddedInstrns();
1064 AddedInstrMap[ CRMI ] = AI;
1065 }
1066
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001067 // Tmp stack poistions are needed by some calls that have spilled args
1068 // So reset it before we call each such method
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001069 //mcInfo.popAllTempValues(TM);
1070
1071
Vikram S. Adve12af1642001-11-08 04:48:50 +00001072
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001073 if (TM.getInstrInfo().isCall(OpCode))
1074 MRI.colorCallArgs(CRMI, LRI, AI, *this);
1075 else if (TM.getInstrInfo().isReturn(OpCode))
Ruchira Sasankaa90e7702001-10-15 16:26:38 +00001076 MRI.colorRetValue( CRMI, LRI, AI );
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001077 else
1078 assert(0 && "Non Call/Ret instrn in CallRetInstrList\n");
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001079 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001080}
1081
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001082#endif
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001083
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001084//----------------------------------------------------------------------------
1085
1086//----------------------------------------------------------------------------
1087void PhyRegAlloc::colorIncomingArgs()
1088{
1089 const BasicBlock *const FirstBB = Meth->front();
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001090 const MachineInstr *FirstMI = FirstBB->getMachineInstrVec().front();
1091 assert(FirstMI && "No machine instruction in entry BB");
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001092
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001093 AddedInstrns *AI = AddedInstrMap[FirstMI];
1094 if (!AI)
1095 AddedInstrMap[FirstMI] = AI = new AddedInstrns();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001096
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001097 MRI.colorMethodArgs(Meth, LRI, AI);
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001098}
1099
Ruchira Sasankae727f852001-09-18 22:43:57 +00001100
1101//----------------------------------------------------------------------------
1102// Used to generate a label for a basic block
1103//----------------------------------------------------------------------------
Chris Lattner697954c2002-01-20 22:54:45 +00001104void PhyRegAlloc::printLabel(const Value *const Val) {
1105 if (Val->hasName())
1106 cerr << Val->getName();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001107 else
Chris Lattner697954c2002-01-20 22:54:45 +00001108 cerr << "Label" << Val;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001109}
1110
1111
Ruchira Sasankae727f852001-09-18 22:43:57 +00001112//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001113// This method calls setSugColorUsable method of each live range. This
1114// will determine whether the suggested color of LR is really usable.
1115// A suggested color is not usable when the suggested color is volatile
1116// AND when there are call interferences
1117//----------------------------------------------------------------------------
1118
1119void PhyRegAlloc::markUnusableSugColors()
1120{
Chris Lattner697954c2002-01-20 22:54:45 +00001121 if(DEBUG_RA ) cerr << "\nmarking unusable suggested colors ...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001122
1123 // hash map iterator
1124 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
1125 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
1126
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001127 for(; HMI != HMIEnd ; ++HMI ) {
1128 if (HMI->first) {
1129 LiveRange *L = HMI->second; // get the LiveRange
1130 if (L) {
1131 if(L->hasSuggestedColor()) {
1132 int RCID = L->getRegClass()->getID();
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001133 if( MRI.isRegVolatile( RCID, L->getSuggestedColor()) &&
1134 L->isCallInterference() )
1135 L->setSuggestedColorUsable( false );
1136 else
1137 L->setSuggestedColorUsable( true );
1138 }
1139 } // if L->hasSuggestedColor()
1140 }
1141 } // for all LR's in hash map
1142}
1143
1144
1145
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001146//----------------------------------------------------------------------------
1147// The following method will set the stack offsets of the live ranges that
1148// are decided to be spillled. This must be called just after coloring the
1149// LRs using the graph coloring algo. For each live range that is spilled,
1150// this method allocate a new spill position on the stack.
1151//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001152
Chris Lattner37730942002-02-05 03:52:29 +00001153void PhyRegAlloc::allocateStackSpace4SpilledLRs() {
1154 if (DEBUG_RA) cerr << "\nsetting LR stack offsets ...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001155
Chris Lattner37730942002-02-05 03:52:29 +00001156 LiveRangeMapType::const_iterator HMI = LRI.getLiveRangeMap()->begin();
1157 LiveRangeMapType::const_iterator HMIEnd = LRI.getLiveRangeMap()->end();
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001158
Chris Lattner37730942002-02-05 03:52:29 +00001159 for( ; HMI != HMIEnd ; ++HMI) {
1160 if (HMI->first && HMI->second) {
1161 LiveRange *L = HMI->second; // get the LiveRange
1162 if (!L->hasColor()) // NOTE: ** allocating the size of long Type **
1163 L->setSpillOffFromFP(mcInfo.allocateSpilledValue(TM, Type::LongTy));
1164 }
1165 } // for all LR's in hash map
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001166}
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001167
1168
1169
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001170//----------------------------------------------------------------------------
Ruchira Sasankae727f852001-09-18 22:43:57 +00001171// The entry pont to Register Allocation
1172//----------------------------------------------------------------------------
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001173
1174void PhyRegAlloc::allocateRegisters()
1175{
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001176
1177 // make sure that we put all register classes into the RegClassList
1178 // before we call constructLiveRanges (now done in the constructor of
1179 // PhyRegAlloc class).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001180 //
1181 LRI.constructLiveRanges(); // create LR info
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001182
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001183 if (DEBUG_RA)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001184 LRI.printLiveRanges();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001185
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001186 createIGNodeListsAndIGs(); // create IGNode list and IGs
1187
1188 buildInterferenceGraphs(); // build IGs in all reg classes
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001189
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001190
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001191 if (DEBUG_RA) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001192 // print all LRs in all reg classes
1193 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1194 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001195
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001196 // print IGs in all register classes
1197 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1198 RegClassList[ rc ]->printIG();
1199 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001200
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001201
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001202 LRI.coalesceLRs(); // coalesce all live ranges
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001203
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +00001204
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001205 if( DEBUG_RA) {
1206 // print all LRs in all reg classes
1207 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1208 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001209
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001210 // print IGs in all register classes
1211 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1212 RegClassList[ rc ]->printIG();
1213 }
1214
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001215
1216 // mark un-usable suggested color before graph coloring algorithm.
1217 // When this is done, the graph coloring algo will not reserve
1218 // suggested color unnecessarily - they can be used by another LR
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001219 //
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001220 markUnusableSugColors();
1221
1222 // color all register classes using the graph coloring algo
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001223 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1224 RegClassList[ rc ]->colorAllRegs();
1225
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001226 // Atter grpah coloring, if some LRs did not receive a color (i.e, spilled)
1227 // a poistion for such spilled LRs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001228 //
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001229 allocateStackSpace4SpilledLRs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001230
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001231 mcInfo.popAllTempValues(TM); // TODO **Check
1232
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001233 // color incoming args - if the correct color was not received
1234 // insert code to copy to the correct register
1235 //
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001236 colorIncomingArgs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001237
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001238 // Now update the machine code with register names and add any
1239 // additional code inserted by the register allocator to the instruction
1240 // stream
1241 //
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001242 updateMachineCode();
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001243
Chris Lattner045e7c82001-09-19 16:26:23 +00001244 if (DEBUG_RA) {
Vikram S. Adve12af1642001-11-08 04:48:50 +00001245 MachineCodeForMethod::get(Meth).dump();
Chris Lattner045e7c82001-09-19 16:26:23 +00001246 printMachineCode(); // only for DEBUGGING
1247 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001248}
1249
Ruchira Sasankae727f852001-09-18 22:43:57 +00001250
1251