blob: b296cae139f61334654681194a7cfdf4f00ba4ee [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 Lattner296b7732002-02-05 02:52:05 +000018#include "llvm/Analysis/LiveVar/ValueSet.h"
Chris Lattner14ab1ce2002-02-04 17:48:00 +000019#include "llvm/Analysis/LoopInfo.h"
Vikram S. Adve12af1642001-11-08 04:48:50 +000020#include "llvm/Target/TargetMachine.h"
21#include "llvm/Target/MachineFrameInfo.h"
Chris Lattner30adeb62002-02-04 16:36:59 +000022#include "llvm/Method.h"
Chris 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 Lattner7a176752001-12-04 00:03:30 +0000313 for( MachineInstr::val_const_op_iterator OpI(MInst);!OpI.done(); ++OpI) {
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000314
315 if( OpI.isDef() ) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000316 // create a new LR iff this operand is a def
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000317 //
Chris Lattner748697d2002-02-05 04:20:12 +0000318 addInterference(*OpI, &LVSetAI, isCallInst);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000319 }
320
321 // Calculate the spill cost of each live range
322 //
323 LiveRange *LR = LRI.getLiveRangeForValue( *OpI );
324 if( LR )
325 LR->addSpillCost(BBLoopDepthCost);
326 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000327
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000328
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000329 // if there are multiple defs in this instruction e.g. in SETX
330 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000331 if (TM.getInstrInfo().isPseudoInstr(MInst->getOpCode()))
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000332 addInterf4PseudoInstr(MInst);
333
334
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000335 // Also add interference for any implicit definitions in a machine
336 // instr (currently, only calls have this).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000337 //
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000338 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
339 if( NumOfImpRefs > 0 ) {
340 for(unsigned z=0; z < NumOfImpRefs; z++)
341 if( MInst->implicitRefIsDefined(z) )
Chris Lattner748697d2002-02-05 04:20:12 +0000342 addInterference( MInst->getImplicitRef(z), &LVSetAI, isCallInst );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000343 }
344
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +0000345
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000346 } // for all machine instructions in BB
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000347
348 } // for all BBs in method
349
350
351 // add interferences for method arguments. Since there are no explict
352 // defs in method for args, we have to add them manually
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000353 //
354 addInterferencesForArgs();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000355
356 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000357 cerr << "Interference graphs calculted!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000358
359}
360
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000361
362
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000363//--------------------------------------------------------------------------
364// Pseudo instructions will be exapnded to multiple instructions by the
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000365// assembler. Consequently, all the opernds must get distinct registers.
366// Therefore, we mark all operands of a pseudo instruction as they interfere
367// with one another.
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000368//--------------------------------------------------------------------------
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000369void PhyRegAlloc::addInterf4PseudoInstr(const MachineInstr *MInst) {
370
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000371 bool setInterf = false;
372
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000373 // iterate over MI operands to find defs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000374 //
Chris Lattner7a176752001-12-04 00:03:30 +0000375 for( MachineInstr::val_const_op_iterator It1(MInst);!It1.done(); ++It1) {
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000376
377 const LiveRange *const LROfOp1 = LRI.getLiveRangeForValue( *It1 );
378
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000379 if( !LROfOp1 && It1.isDef() )
380 assert( 0 && "No LR for Def in PSEUDO insruction");
381
Chris Lattner7a176752001-12-04 00:03:30 +0000382 MachineInstr::val_const_op_iterator It2 = It1;
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000383 ++It2;
384
385 for( ; !It2.done(); ++It2) {
386
387 const LiveRange *const LROfOp2 = LRI.getLiveRangeForValue( *It2 );
388
389 if( LROfOp2) {
390
391 RegClass *const RCOfOp1 = LROfOp1->getRegClass();
392 RegClass *const RCOfOp2 = LROfOp2->getRegClass();
393
394 if( RCOfOp1 == RCOfOp2 ){
395 RCOfOp1->setInterference( LROfOp1, LROfOp2 );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000396 setInterf = true;
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000397 }
398
399 } // if Op2 has a LR
400
401 } // for all other defs in machine instr
402
403 } // for all operands in an instruction
404
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000405 if( !setInterf && (MInst->getNumOperands() > 2) ) {
406 cerr << "\nInterf not set for any operand in pseudo instr:\n";
407 cerr << *MInst;
408 assert(0 && "Interf not set for pseudo instr with > 2 operands" );
409
410 }
411
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000412}
413
414
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000415
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000416//----------------------------------------------------------------------------
417// This method will add interferences for incoming arguments to a method.
418//----------------------------------------------------------------------------
Chris Lattner296b7732002-02-05 02:52:05 +0000419void PhyRegAlloc::addInterferencesForArgs() {
420 // get the InSet of root BB
Chris Lattner748697d2002-02-05 04:20:12 +0000421 const ValueSet &InSet = LVI->getInSetOfBB(Meth->front());
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000422
Chris Lattner296b7732002-02-05 02:52:05 +0000423 // get the argument list
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000424 const Method::ArgumentListType& ArgList = Meth->getArgumentList();
425
Chris Lattner296b7732002-02-05 02:52:05 +0000426 // get an iterator to arg list
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000427 Method::ArgumentListType::const_iterator ArgIt = ArgList.begin();
428
429
430 for( ; ArgIt != ArgList.end() ; ++ArgIt) { // for each argument
Chris Lattner748697d2002-02-05 04:20:12 +0000431 addInterference((Value*)*ArgIt, &InSet, false);// add interferences between
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000432 // args and LVars at start
Chris Lattner0665a5f2002-02-05 01:43:49 +0000433 if( DEBUG_RA > 1)
434 cerr << " - %% adding interference for argument "
435 << RAV((const Value *)*ArgIt) << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000436 }
437}
438
439
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000440
441
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000442//----------------------------------------------------------------------------
443// This method is called after register allocation is complete to set the
444// allocated reisters in the machine code. This code will add register numbers
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000445// to MachineOperands that contain a Value. Also it calls target specific
446// methods to produce caller saving instructions. At the end, it adds all
447// additional instructions produced by the register allocator to the
448// instruction stream.
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000449//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000450void PhyRegAlloc::updateMachineCode()
451{
452
453 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
454
455 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
456
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000457 // get the iterator for machine instructions
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000458 //
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000459 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
460 MachineCodeForBasicBlock::iterator MInstIterator = MIVec.begin();
461
462 // iterate over all the machine instructions in BB
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000463 //
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000464 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
465
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000466 MachineInstr *MInst = *MInstIterator;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000467
468 unsigned Opcode = MInst->getOpCode();
469
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000470 // do not process Phis
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000471 if (TM.getInstrInfo().isPhi(Opcode))
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000472 continue;
473
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000474 // Now insert speical instructions (if necessary) for call/return
475 // instructions.
476 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000477 if (TM.getInstrInfo().isCall(Opcode) ||
478 TM.getInstrInfo().isReturn(Opcode)) {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000479
480 AddedInstrns *AI = AddedInstrMap[ MInst];
481 if ( !AI ) {
482 AI = new AddedInstrns();
483 AddedInstrMap[ MInst ] = AI;
484 }
485
486 // Tmp stack poistions are needed by some calls that have spilled args
487 // So reset it before we call each such method
Ruchira Sasanka6a3db8c2002-01-07 21:09:06 +0000488 //
489 mcInfo.popAllTempValues(TM);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000490
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000491 if (TM.getInstrInfo().isCall(Opcode))
492 MRI.colorCallArgs(MInst, LRI, AI, *this, *BBI);
493 else if (TM.getInstrInfo().isReturn(Opcode))
494 MRI.colorRetValue(MInst, LRI, AI);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000495 }
496
497
498 /* -- Using above code instead of this
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000499
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000500 // if this machine instr is call, insert caller saving code
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000501
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000502 if( (TM.getInstrInfo()).isCall( MInst->getOpCode()) )
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000503 MRI.insertCallerSavingCode(MInst, *BBI, *this );
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000504
505 */
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000506
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000507
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000508 // reset the stack offset for temporary variables since we may
509 // need that to spill
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000510 // mcInfo.popAllTempValues(TM);
Ruchira Sasankaf90870f2001-11-15 22:02:06 +0000511 // TODO ** : do later
Vikram S. Adve12af1642001-11-08 04:48:50 +0000512
Chris Lattner7a176752001-12-04 00:03:30 +0000513 //for(MachineInstr::val_const_op_iterator OpI(MInst);!OpI.done();++OpI) {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000514
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000515
516 // Now replace set the registers for operands in the machine instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000517 //
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000518 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
519
520 MachineOperand& Op = MInst->getOperand(OpNum);
521
522 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
523 Op.getOperandType() == MachineOperand::MO_CCRegister) {
524
525 const Value *const Val = Op.getVRegValue();
526
527 // delete this condition checking later (must assert if Val is null)
Chris Lattner045e7c82001-09-19 16:26:23 +0000528 if( !Val) {
529 if (DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000530 cerr << "Warning: NULL Value found for operand\n";
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000531 continue;
532 }
533 assert( Val && "Value is NULL");
534
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000535 LiveRange *const LR = LRI.getLiveRangeForValue(Val);
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000536
537 if ( !LR ) {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000538
539 // nothing to worry if it's a const or a label
540
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000541 if (DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000542 cerr << "*NO LR for operand : " << Op ;
543 cerr << " [reg:" << Op.getAllocatedRegNum() << "]";
544 cerr << " in inst:\t" << *MInst << "\n";
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000545 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000546
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000547 // if register is not allocated, mark register as invalid
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000548 if( Op.getAllocatedRegNum() == -1)
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000549 Op.setRegForValue( MRI.getInvalidRegNum());
Ruchira Sasankae727f852001-09-18 22:43:57 +0000550
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000551
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000552 continue;
553 }
554
555 unsigned RCID = (LR->getRegClass())->getID();
556
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000557 if( LR->hasColor() ) {
558 Op.setRegForValue( MRI.getUnifiedRegNum(RCID, LR->getColor()) );
559 }
560 else {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000561
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000562 // LR did NOT receive a color (register). Now, insert spill code
563 // for spilled opeands in this machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000564
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000565 //assert(0 && "LR must be spilled");
566 insertCode4SpilledLR(LR, MInst, *BBI, OpNum );
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000567
568 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000569 }
570
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000571 } // for each operand
572
573
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000574 // Now add instructions that the register allocator inserts before/after
575 // this machine instructions (done only for calls/rets/incoming args)
576 // We do this here, to ensure that spill for an instruction is inserted
577 // closest as possible to an instruction (see above insertCode4Spill...)
578 //
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000579 // If there are instructions to be added, *before* this machine
580 // instruction, add them now.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000581 //
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000582 if( AddedInstrMap[ MInst ] ) {
Chris Lattner697954c2002-01-20 22:54:45 +0000583 std::deque<MachineInstr *> &IBef = AddedInstrMap[MInst]->InstrnsBefore;
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000584
585 if( ! IBef.empty() ) {
Chris Lattner697954c2002-01-20 22:54:45 +0000586 std::deque<MachineInstr *>::iterator AdIt;
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000587
588 for( AdIt = IBef.begin(); AdIt != IBef.end() ; ++AdIt ) {
589
590 if( DEBUG_RA) {
591 cerr << "For inst " << *MInst;
Chris Lattner697954c2002-01-20 22:54:45 +0000592 cerr << " PREPENDed instr: " << **AdIt << "\n";
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000593 }
594
595 MInstIterator = MIVec.insert( MInstIterator, *AdIt );
596 ++MInstIterator;
597 }
598
599 }
600
601 }
602
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000603 // If there are instructions to be added *after* this machine
604 // instruction, add them now
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000605 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000606 if(AddedInstrMap[MInst] &&
607 !AddedInstrMap[MInst]->InstrnsAfter.empty() ) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000608
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000609 // if there are delay slots for this instruction, the instructions
610 // added after it must really go after the delayed instruction(s)
611 // So, we move the InstrAfter of the current instruction to the
612 // corresponding delayed instruction
613
614 unsigned delay;
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000615 if ((delay=TM.getInstrInfo().getNumDelaySlots(MInst->getOpCode())) >0){
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000616 move2DelayedInstr(MInst, *(MInstIterator+delay) );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000617
Chris Lattner697954c2002-01-20 22:54:45 +0000618 if(DEBUG_RA) cerr<< "\nMoved an added instr after the delay slot";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000619 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000620
621 else {
622
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000623
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000624 // Here we can add the "instructions after" to the current
625 // instruction since there are no delay slots for this instruction
626
Chris Lattner697954c2002-01-20 22:54:45 +0000627 std::deque<MachineInstr *> &IAft = AddedInstrMap[MInst]->InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000628
629 if( ! IAft.empty() ) {
630
Chris Lattner697954c2002-01-20 22:54:45 +0000631 std::deque<MachineInstr *>::iterator AdIt;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000632
633 ++MInstIterator; // advance to the next instruction
634
635 for( AdIt = IAft.begin(); AdIt != IAft.end() ; ++AdIt ) {
636
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000637 if(DEBUG_RA) {
638 cerr << "For inst " << *MInst;
Chris Lattner697954c2002-01-20 22:54:45 +0000639 cerr << " APPENDed instr: " << **AdIt << "\n";
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000640 }
641
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000642 MInstIterator = MIVec.insert( MInstIterator, *AdIt );
643 ++MInstIterator;
644 }
645
646 // MInsterator already points to the next instr. Since the
647 // for loop also increments it, decrement it to point to the
648 // instruction added last
649 --MInstIterator;
650
651 }
652
653 } // if not delay
654
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000655 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000656
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000657 } // for each machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000658 }
659}
660
661
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000662
663//----------------------------------------------------------------------------
664// This method inserts spill code for AN operand whose LR was spilled.
665// This method may be called several times for a single machine instruction
666// if it contains many spilled operands. Each time it is called, it finds
667// a register which is not live at that instruction and also which is not
668// used by other spilled operands of the same instruction. Then it uses
669// this register temporarily to accomodate the spilled value.
670//----------------------------------------------------------------------------
671void PhyRegAlloc::insertCode4SpilledLR(const LiveRange *LR,
672 MachineInstr *MInst,
673 const BasicBlock *BB,
674 const unsigned OpNum) {
675
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000676 assert(! TM.getInstrInfo().isCall(MInst->getOpCode()) &&
677 (! TM.getInstrInfo().isReturn(MInst->getOpCode())) &&
678 "Arg of a call/ret must be handled elsewhere");
679
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000680 MachineOperand& Op = MInst->getOperand(OpNum);
681 bool isDef = MInst->operandIsDefined(OpNum);
682 unsigned RegType = MRI.getRegType( LR );
683 int SpillOff = LR->getSpillOffFromFP();
684 RegClass *RC = LR->getRegClass();
Chris Lattner748697d2002-02-05 04:20:12 +0000685 const ValueSet &LVSetBef = LVI->getLiveVarSetBeforeMInst(MInst, BB);
Vikram S. Adve00521d72001-11-12 23:26:35 +0000686
Chris Lattner697954c2002-01-20 22:54:45 +0000687 mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000688
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000689 MachineInstr *MIBef=NULL, *AdIMid=NULL, *MIAft=NULL;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000690
Chris Lattner748697d2002-02-05 04:20:12 +0000691 int TmpRegU = getUsableUniRegAtMI(RC, RegType, MInst,&LVSetBef, MIBef, MIAft);
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000692
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000693 // get the added instructions for this instruciton
694 AddedInstrns *AI = AddedInstrMap[ MInst ];
695 if ( !AI ) {
696 AI = new AddedInstrns();
697 AddedInstrMap[ MInst ] = AI;
698 }
699
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000700
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000701 if( !isDef ) {
702
703 // for a USE, we have to load the value of LR from stack to a TmpReg
704 // and use the TmpReg as one operand of instruction
705
706 // actual loading instruction
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000707 AdIMid = MRI.cpMem2RegMI(MRI.getFramePointer(), SpillOff, TmpRegU,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->InstrnsBefore.push_back(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 Sasanka226f1f02001-11-08 19:11:30 +0000716
Chris Lattner296b7732002-02-05 02:52:05 +0000717 } else { // if this is a Def
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000718 // for a DEF, we have to store the value produced by this instruction
719 // on the stack position allocated for this LR
720
721 // actual storing instruction
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000722 AdIMid = MRI.cpReg2MemMI(TmpRegU, MRI.getFramePointer(), SpillOff,RegType);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000723
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000724 if (MIBef)
725 AI->InstrnsBefore.push_back(MIBef);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000726
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000727 AI->InstrnsAfter.push_front(AdIMid);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000728
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000729 if (MIAft)
730 AI->InstrnsAfter.push_front(MIAft);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000731
732 } // if !DEF
733
734 cerr << "\nFor Inst " << *MInst;
Chris Lattner296b7732002-02-05 02:52:05 +0000735 cerr << " - SPILLED LR: "; printSet(*LR);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000736 cerr << "\n - Added Instructions:";
Chris Lattner296b7732002-02-05 02:52:05 +0000737 if (MIBef) cerr << *MIBef;
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000738 cerr << *AdIMid;
Chris Lattner296b7732002-02-05 02:52:05 +0000739 if (MIAft) cerr << *MIAft;
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000740
Chris Lattner296b7732002-02-05 02:52:05 +0000741 Op.setRegForValue(TmpRegU); // set the opearnd
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000742}
743
744
745
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000746//----------------------------------------------------------------------------
747// We can use the following method to get a temporary register to be used
748// BEFORE any given machine instruction. If there is a register available,
749// this method will simply return that register and set MIBef = MIAft = NULL.
750// Otherwise, it will return a register and MIAft and MIBef will contain
751// two instructions used to free up this returned register.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000752// Returned register number is the UNIFIED register number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000753//----------------------------------------------------------------------------
754
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000755int PhyRegAlloc::getUsableUniRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000756 const int RegType,
757 const MachineInstr *MInst,
Chris Lattner296b7732002-02-05 02:52:05 +0000758 const ValueSet *LVSetBef,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000759 MachineInstr *MIBef,
760 MachineInstr *MIAft) {
761
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000762 int RegU = getUnusedUniRegAtMI(RC, MInst, LVSetBef);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000763
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000764
765 if( RegU != -1) {
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000766 // we found an unused register, so we can simply use it
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000767 MIBef = MIAft = NULL;
768 }
769 else {
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000770 // we couldn't find an unused register. Generate code to free up a reg by
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000771 // saving it on stack and restoring after the instruction
772
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000773 int TmpOff = mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
Vikram S. Adve12af1642001-11-08 04:48:50 +0000774
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000775 RegU = getUniRegNotUsedByThisInst(RC, MInst);
776 MIBef = MRI.cpReg2MemMI(RegU, MRI.getFramePointer(), TmpOff, RegType );
777 MIAft = MRI.cpMem2RegMI(MRI.getFramePointer(), TmpOff, RegU, RegType );
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000778 }
779
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000780 return RegU;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000781}
782
783//----------------------------------------------------------------------------
784// This method is called to get a new unused register that can be used to
785// accomodate a spilled value.
786// This method may be called several times for a single machine instruction
787// if it contains many spilled operands. Each time it is called, it finds
788// a register which is not live at that instruction and also which is not
789// used by other spilled operands of the same instruction.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000790// Return register number is relative to the register class. NOT
791// unified number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000792//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000793int PhyRegAlloc::getUnusedUniRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000794 const MachineInstr *MInst,
Chris Lattner296b7732002-02-05 02:52:05 +0000795 const ValueSet *LVSetBef) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000796
797 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
798
799 bool *IsColorUsedArr = RC->getIsColorUsedArr();
800
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000801 for(unsigned i=0; i < NumAvailRegs; i++) // Reset array
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000802 IsColorUsedArr[i] = false;
803
Chris Lattner296b7732002-02-05 02:52:05 +0000804 ValueSet::const_iterator LIt = LVSetBef->begin();
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000805
806 // for each live var in live variable set after machine inst
807 for( ; LIt != LVSetBef->end(); ++LIt) {
808
809 // get the live range corresponding to live var
810 LiveRange *const LRofLV = LRI.getLiveRangeForValue(*LIt );
811
812 // LR can be null if it is a const since a const
813 // doesn't have a dominating def - see Assumptions above
814 if( LRofLV )
815 if( LRofLV->hasColor() )
816 IsColorUsedArr[ LRofLV->getColor() ] = true;
817 }
818
819 // It is possible that one operand of this MInst was already spilled
820 // and it received some register temporarily. If that's the case,
821 // it is recorded in machine operand. We must skip such registers.
822
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000823 setRelRegsUsedByThisInst(RC, MInst);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000824
825 unsigned c; // find first unused color
826 for( c=0; c < NumAvailRegs; c++)
827 if( ! IsColorUsedArr[ c ] ) break;
828
829 if(c < NumAvailRegs)
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000830 return MRI.getUnifiedRegNum(RC->getID(), c);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000831 else
832 return -1;
833
834
835}
836
837
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000838//----------------------------------------------------------------------------
839// Get any other register in a register class, other than what is used
840// by operands of a machine instruction. Returns the unified reg number.
841//----------------------------------------------------------------------------
842int PhyRegAlloc::getUniRegNotUsedByThisInst(RegClass *RC,
843 const MachineInstr *MInst) {
844
845 bool *IsColorUsedArr = RC->getIsColorUsedArr();
846 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
847
848
849 for(unsigned i=0; i < NumAvailRegs ; i++) // Reset array
850 IsColorUsedArr[i] = false;
851
852 setRelRegsUsedByThisInst(RC, MInst);
853
854 unsigned c; // find first unused color
855 for( c=0; c < RC->getNumOfAvailRegs(); c++)
856 if( ! IsColorUsedArr[ c ] ) break;
857
858 if(c < NumAvailRegs)
859 return MRI.getUnifiedRegNum(RC->getID(), c);
860 else
861 assert( 0 && "FATAL: No free register could be found in reg class!!");
Chris Lattner697954c2002-01-20 22:54:45 +0000862 return 0;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000863}
864
865
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000866//----------------------------------------------------------------------------
867// This method modifies the IsColorUsedArr of the register class passed to it.
868// It sets the bits corresponding to the registers used by this machine
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000869// instructions. Both explicit and implicit operands are set.
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000870//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000871void PhyRegAlloc::setRelRegsUsedByThisInst(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000872 const MachineInstr *MInst ) {
873
874 bool *IsColorUsedArr = RC->getIsColorUsedArr();
875
876 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
877
878 const MachineOperand& Op = MInst->getOperand(OpNum);
879
880 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000881 Op.getOperandType() == MachineOperand::MO_CCRegister ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000882
883 const Value *const Val = Op.getVRegValue();
884
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000885 if( Val )
886 if( MRI.getRegClassIDOfValue(Val) == RC->getID() ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000887 int Reg;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000888 if( (Reg=Op.getAllocatedRegNum()) != -1) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000889 IsColorUsedArr[ Reg ] = true;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000890 }
891 else {
892 // it is possilbe that this operand still is not marked with
893 // a register but it has a LR and that received a color
894
895 LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
896 if( LROfVal)
897 if( LROfVal->hasColor() )
898 IsColorUsedArr[ LROfVal->getColor() ] = true;
899 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000900
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000901 } // if reg classes are the same
902 }
903 else if (Op.getOperandType() == MachineOperand::MO_MachineRegister) {
904 IsColorUsedArr[ Op.getMachineRegNum() ] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000905 }
906 }
907
908 // If there are implicit references, mark them as well
909
910 for(unsigned z=0; z < MInst->getNumImplicitRefs(); z++) {
911
912 LiveRange *const LRofImpRef =
913 LRI.getLiveRangeForValue( MInst->getImplicitRef(z) );
Chris Lattner697954c2002-01-20 22:54:45 +0000914
915 if(LRofImpRef && LRofImpRef->hasColor())
916 IsColorUsedArr[LRofImpRef->getColor()] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000917 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000918}
919
920
921
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000922
923
924
925
926
927//----------------------------------------------------------------------------
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000928// If there are delay slots for an instruction, the instructions
929// added after it must really go after the delayed instruction(s).
930// So, we move the InstrAfter of that instruction to the
931// corresponding delayed instruction using the following method.
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000932
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000933//----------------------------------------------------------------------------
934void PhyRegAlloc:: move2DelayedInstr(const MachineInstr *OrigMI,
935 const MachineInstr *DelayedMI) {
936
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000937 // "added after" instructions of the original instr
Chris Lattner697954c2002-01-20 22:54:45 +0000938 std::deque<MachineInstr *> &OrigAft = AddedInstrMap[OrigMI]->InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000939
940 // "added instructions" of the delayed instr
941 AddedInstrns *DelayAdI = AddedInstrMap[DelayedMI];
942
943 if(! DelayAdI ) { // create a new "added after" if necessary
944 DelayAdI = new AddedInstrns();
945 AddedInstrMap[DelayedMI] = DelayAdI;
946 }
947
948 // "added after" instructions of the delayed instr
Chris Lattner697954c2002-01-20 22:54:45 +0000949 std::deque<MachineInstr *> &DelayedAft = DelayAdI->InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000950
951 // go thru all the "added after instructions" of the original instruction
952 // and append them to the "addded after instructions" of the delayed
953 // instructions
Chris Lattner697954c2002-01-20 22:54:45 +0000954 DelayedAft.insert(DelayedAft.end(), OrigAft.begin(), OrigAft.end());
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000955
956 // empty the "added after instructions" of the original instruction
957 OrigAft.clear();
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000958}
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000959
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000960//----------------------------------------------------------------------------
961// This method prints the code with registers after register allocation is
962// complete.
963//----------------------------------------------------------------------------
964void PhyRegAlloc::printMachineCode()
965{
966
Chris Lattner697954c2002-01-20 22:54:45 +0000967 cerr << "\n;************** Method " << Meth->getName()
968 << " *****************\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000969
970 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
971
972 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
973
Chris Lattner697954c2002-01-20 22:54:45 +0000974 cerr << "\n"; printLabel( *BBI); cerr << ": ";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000975
976 // get the iterator for machine instructions
977 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
978 MachineCodeForBasicBlock::iterator MInstIterator = MIVec.begin();
979
980 // iterate over all the machine instructions in BB
981 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
982
983 MachineInstr *const MInst = *MInstIterator;
984
985
Chris Lattner697954c2002-01-20 22:54:45 +0000986 cerr << "\n\t";
987 cerr << TargetInstrDescriptors[MInst->getOpCode()].opCodeString;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000988
989
Chris Lattner7a176752001-12-04 00:03:30 +0000990 //for(MachineInstr::val_const_op_iterator OpI(MInst);!OpI.done();++OpI) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000991
992 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
993
994 MachineOperand& Op = MInst->getOperand(OpNum);
995
996 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasanka97b8b442001-10-18 22:36:26 +0000997 Op.getOperandType() == MachineOperand::MO_CCRegister /*||
998 Op.getOperandType() == MachineOperand::MO_PCRelativeDisp*/ ) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000999
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001000 const Value *const Val = Op.getVRegValue () ;
Ruchira Sasankae727f852001-09-18 22:43:57 +00001001 // ****this code is temporary till NULL Values are fixed
1002 if( ! Val ) {
Chris Lattner697954c2002-01-20 22:54:45 +00001003 cerr << "\t<*NULL*>";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001004 continue;
1005 }
Ruchira Sasankae727f852001-09-18 22:43:57 +00001006
1007 // if a label or a constant
Chris Lattnerdbe53042002-01-21 01:33:12 +00001008 if(isa<BasicBlock>(Val)) {
Chris Lattner697954c2002-01-20 22:54:45 +00001009 cerr << "\t"; printLabel( Op.getVRegValue () );
1010 } else {
Ruchira Sasankae727f852001-09-18 22:43:57 +00001011 // else it must be a register value
1012 const int RegNum = Op.getAllocatedRegNum();
1013
Chris Lattner697954c2002-01-20 22:54:45 +00001014 cerr << "\t" << "%" << MRI.getUnifiedRegName( RegNum );
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001015 if (Val->hasName() )
Chris Lattner697954c2002-01-20 22:54:45 +00001016 cerr << "(" << Val->getName() << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001017 else
Chris Lattner697954c2002-01-20 22:54:45 +00001018 cerr << "(" << Val << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001019
1020 if( Op.opIsDef() )
Chris Lattner697954c2002-01-20 22:54:45 +00001021 cerr << "*";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001022
1023 const LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
1024 if( LROfVal )
1025 if( LROfVal->hasSpillOffset() )
Chris Lattner697954c2002-01-20 22:54:45 +00001026 cerr << "$";
Ruchira Sasankae727f852001-09-18 22:43:57 +00001027 }
1028
1029 }
1030 else if(Op.getOperandType() == MachineOperand::MO_MachineRegister) {
Chris Lattner697954c2002-01-20 22:54:45 +00001031 cerr << "\t" << "%" << MRI.getUnifiedRegName(Op.getMachineRegNum());
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001032 }
1033
1034 else
Chris Lattner697954c2002-01-20 22:54:45 +00001035 cerr << "\t" << Op; // use dump field
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001036 }
1037
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001038
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001039
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001040 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
Chris Lattner0665a5f2002-02-05 01:43:49 +00001041 if( NumOfImpRefs > 0) {
Chris Lattner697954c2002-01-20 22:54:45 +00001042 cerr << "\tImplicit:";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001043
Chris Lattner0665a5f2002-02-05 01:43:49 +00001044 for(unsigned z=0; z < NumOfImpRefs; z++)
1045 cerr << RAV(MInst->getImplicitRef(z)) << "\t";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001046 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001047
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001048 } // for all machine instructions
1049
Chris Lattner697954c2002-01-20 22:54:45 +00001050 cerr << "\n";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001051
1052 } // for all BBs
1053
Chris Lattner697954c2002-01-20 22:54:45 +00001054 cerr << "\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001055}
1056
Ruchira Sasankae727f852001-09-18 22:43:57 +00001057
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001058#if 0
1059
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001060//----------------------------------------------------------------------------
1061//
1062//----------------------------------------------------------------------------
1063
1064void PhyRegAlloc::colorCallRetArgs()
1065{
1066
1067 CallRetInstrListType &CallRetInstList = LRI.getCallRetInstrList();
1068 CallRetInstrListType::const_iterator It = CallRetInstList.begin();
1069
1070 for( ; It != CallRetInstList.end(); ++It ) {
1071
Ruchira Sasankaa90e7702001-10-15 16:26:38 +00001072 const MachineInstr *const CRMI = *It;
1073 unsigned OpCode = CRMI->getOpCode();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001074
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001075 // get the added instructions for this Call/Ret instruciton
1076 AddedInstrns *AI = AddedInstrMap[ CRMI ];
1077 if ( !AI ) {
1078 AI = new AddedInstrns();
1079 AddedInstrMap[ CRMI ] = AI;
1080 }
1081
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001082 // Tmp stack poistions are needed by some calls that have spilled args
1083 // So reset it before we call each such method
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001084 //mcInfo.popAllTempValues(TM);
1085
1086
Vikram S. Adve12af1642001-11-08 04:48:50 +00001087
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001088 if (TM.getInstrInfo().isCall(OpCode))
1089 MRI.colorCallArgs(CRMI, LRI, AI, *this);
1090 else if (TM.getInstrInfo().isReturn(OpCode))
Ruchira Sasankaa90e7702001-10-15 16:26:38 +00001091 MRI.colorRetValue( CRMI, LRI, AI );
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001092 else
1093 assert(0 && "Non Call/Ret instrn in CallRetInstrList\n");
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001094 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001095}
1096
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001097#endif
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001098
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001099//----------------------------------------------------------------------------
1100
1101//----------------------------------------------------------------------------
1102void PhyRegAlloc::colorIncomingArgs()
1103{
1104 const BasicBlock *const FirstBB = Meth->front();
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001105 const MachineInstr *FirstMI = FirstBB->getMachineInstrVec().front();
1106 assert(FirstMI && "No machine instruction in entry BB");
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001107
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001108 AddedInstrns *AI = AddedInstrMap[FirstMI];
1109 if (!AI)
1110 AddedInstrMap[FirstMI] = AI = new AddedInstrns();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001111
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001112 MRI.colorMethodArgs(Meth, LRI, AI);
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001113}
1114
Ruchira Sasankae727f852001-09-18 22:43:57 +00001115
1116//----------------------------------------------------------------------------
1117// Used to generate a label for a basic block
1118//----------------------------------------------------------------------------
Chris Lattner697954c2002-01-20 22:54:45 +00001119void PhyRegAlloc::printLabel(const Value *const Val) {
1120 if (Val->hasName())
1121 cerr << Val->getName();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001122 else
Chris Lattner697954c2002-01-20 22:54:45 +00001123 cerr << "Label" << Val;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001124}
1125
1126
Ruchira Sasankae727f852001-09-18 22:43:57 +00001127//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001128// This method calls setSugColorUsable method of each live range. This
1129// will determine whether the suggested color of LR is really usable.
1130// A suggested color is not usable when the suggested color is volatile
1131// AND when there are call interferences
1132//----------------------------------------------------------------------------
1133
1134void PhyRegAlloc::markUnusableSugColors()
1135{
Chris Lattner697954c2002-01-20 22:54:45 +00001136 if(DEBUG_RA ) cerr << "\nmarking unusable suggested colors ...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001137
1138 // hash map iterator
1139 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
1140 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
1141
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001142 for(; HMI != HMIEnd ; ++HMI ) {
1143 if (HMI->first) {
1144 LiveRange *L = HMI->second; // get the LiveRange
1145 if (L) {
1146 if(L->hasSuggestedColor()) {
1147 int RCID = L->getRegClass()->getID();
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001148 if( MRI.isRegVolatile( RCID, L->getSuggestedColor()) &&
1149 L->isCallInterference() )
1150 L->setSuggestedColorUsable( false );
1151 else
1152 L->setSuggestedColorUsable( true );
1153 }
1154 } // if L->hasSuggestedColor()
1155 }
1156 } // for all LR's in hash map
1157}
1158
1159
1160
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001161//----------------------------------------------------------------------------
1162// The following method will set the stack offsets of the live ranges that
1163// are decided to be spillled. This must be called just after coloring the
1164// LRs using the graph coloring algo. For each live range that is spilled,
1165// this method allocate a new spill position on the stack.
1166//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001167
Chris Lattner37730942002-02-05 03:52:29 +00001168void PhyRegAlloc::allocateStackSpace4SpilledLRs() {
1169 if (DEBUG_RA) cerr << "\nsetting LR stack offsets ...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001170
Chris Lattner37730942002-02-05 03:52:29 +00001171 LiveRangeMapType::const_iterator HMI = LRI.getLiveRangeMap()->begin();
1172 LiveRangeMapType::const_iterator HMIEnd = LRI.getLiveRangeMap()->end();
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001173
Chris Lattner37730942002-02-05 03:52:29 +00001174 for( ; HMI != HMIEnd ; ++HMI) {
1175 if (HMI->first && HMI->second) {
1176 LiveRange *L = HMI->second; // get the LiveRange
1177 if (!L->hasColor()) // NOTE: ** allocating the size of long Type **
1178 L->setSpillOffFromFP(mcInfo.allocateSpilledValue(TM, Type::LongTy));
1179 }
1180 } // for all LR's in hash map
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001181}
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001182
1183
1184
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001185//----------------------------------------------------------------------------
Ruchira Sasankae727f852001-09-18 22:43:57 +00001186// The entry pont to Register Allocation
1187//----------------------------------------------------------------------------
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001188
1189void PhyRegAlloc::allocateRegisters()
1190{
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001191
1192 // make sure that we put all register classes into the RegClassList
1193 // before we call constructLiveRanges (now done in the constructor of
1194 // PhyRegAlloc class).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001195 //
1196 LRI.constructLiveRanges(); // create LR info
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001197
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001198 if (DEBUG_RA)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001199 LRI.printLiveRanges();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001200
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001201 createIGNodeListsAndIGs(); // create IGNode list and IGs
1202
1203 buildInterferenceGraphs(); // build IGs in all reg classes
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001204
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001205
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001206 if (DEBUG_RA) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001207 // print all LRs in all reg classes
1208 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1209 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001210
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001211 // print IGs in all register classes
1212 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1213 RegClassList[ rc ]->printIG();
1214 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001215
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001216
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001217 LRI.coalesceLRs(); // coalesce all live ranges
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001218
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +00001219
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001220 if( DEBUG_RA) {
1221 // print all LRs in all reg classes
1222 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1223 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001224
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001225 // print IGs in all register classes
1226 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1227 RegClassList[ rc ]->printIG();
1228 }
1229
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001230
1231 // mark un-usable suggested color before graph coloring algorithm.
1232 // When this is done, the graph coloring algo will not reserve
1233 // suggested color unnecessarily - they can be used by another LR
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001234 //
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001235 markUnusableSugColors();
1236
1237 // color all register classes using the graph coloring algo
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001238 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1239 RegClassList[ rc ]->colorAllRegs();
1240
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001241 // Atter grpah coloring, if some LRs did not receive a color (i.e, spilled)
1242 // a poistion for such spilled LRs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001243 //
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001244 allocateStackSpace4SpilledLRs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001245
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001246 mcInfo.popAllTempValues(TM); // TODO **Check
1247
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001248 // color incoming args - if the correct color was not received
1249 // insert code to copy to the correct register
1250 //
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001251 colorIncomingArgs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001252
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001253 // Now update the machine code with register names and add any
1254 // additional code inserted by the register allocator to the instruction
1255 // stream
1256 //
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001257 updateMachineCode();
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001258
Chris Lattner045e7c82001-09-19 16:26:23 +00001259 if (DEBUG_RA) {
Vikram S. Adve12af1642001-11-08 04:48:50 +00001260 MachineCodeForMethod::get(Meth).dump();
Chris Lattner045e7c82001-09-19 16:26:23 +00001261 printMachineCode(); // only for DEBUGGING
1262 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001263}
1264
Ruchira Sasankae727f852001-09-18 22:43:57 +00001265
1266