blob: 49667e00795d678b85edbb1de48eab7fdbb86f5d [file] [log] [blame]
Vikram S. Adve12af1642001-11-08 04:48:50 +00001// $Id$
2//***************************************************************************
3// File:
4// PhyRegAlloc.cpp
5//
6// Purpose:
7// Register allocation for LLVM.
8//
9// History:
10// 9/10/01 - Ruchira Sasanka - created.
11//**************************************************************************/
Ruchira Sasanka8e604792001-09-14 21:18:34 +000012
Chris Lattner6dd98a62002-02-04 00:33:08 +000013#include "llvm/CodeGen/RegisterAllocation.h"
Vikram S. Adve12af1642001-11-08 04:48:50 +000014#include "llvm/CodeGen/PhyRegAlloc.h"
15#include "llvm/CodeGen/MachineInstr.h"
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000016#include "llvm/CodeGen/MachineCodeForMethod.h"
Chris Lattner0a8ed942002-02-04 05:56:09 +000017#include "llvm/Analysis/LiveVar/MethodLiveVarInfo.h"
Chris Lattner14ab1ce2002-02-04 17:48:00 +000018#include "llvm/Analysis/LoopInfo.h"
Vikram S. Adve12af1642001-11-08 04:48:50 +000019#include "llvm/Target/TargetMachine.h"
20#include "llvm/Target/MachineFrameInfo.h"
Chris Lattner221d6882002-02-12 21:07:25 +000021#include "llvm/BasicBlock.h"
Chris Lattner30adeb62002-02-04 16:36:59 +000022#include "llvm/Method.h"
Chris Lattner37730942002-02-05 03:52:29 +000023#include "llvm/Type.h"
Chris Lattner697954c2002-01-20 22:54:45 +000024#include <iostream>
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000025#include <math.h>
Chris Lattner697954c2002-01-20 22:54:45 +000026using std::cerr;
Vikram S. Adve12af1642001-11-08 04:48:50 +000027
28
29// ***TODO: There are several places we add instructions. Validate the order
30// of adding these instructions.
Ruchira Sasanka174bded2001-10-28 18:12:02 +000031
Chris Lattner045e7c82001-09-19 16:26:23 +000032cl::Enum<RegAllocDebugLevel_t> DEBUG_RA("dregalloc", cl::NoFlags,
33 "enable register allocation debugging information",
34 clEnumValN(RA_DEBUG_None , "n", "disable debug output"),
35 clEnumValN(RA_DEBUG_Normal , "y", "enable debug output"),
36 clEnumValN(RA_DEBUG_Verbose, "v", "enable extra debug output"), 0);
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000037
38
Chris Lattner2f9b28e2002-02-04 15:54:09 +000039//----------------------------------------------------------------------------
40// RegisterAllocation pass front end...
41//----------------------------------------------------------------------------
42namespace {
43 class RegisterAllocator : public MethodPass {
44 TargetMachine &Target;
45 public:
46 inline RegisterAllocator(TargetMachine &T) : Target(T) {}
Chris Lattner6dd98a62002-02-04 00:33:08 +000047
Chris Lattner2f9b28e2002-02-04 15:54:09 +000048 bool runOnMethod(Method *M) {
49 if (DEBUG_RA)
50 cerr << "\n******************** Method "<< M->getName()
51 << " ********************\n";
52
Chris Lattner4d7fc112002-02-04 20:02:38 +000053 PhyRegAlloc PRA(M, Target, &getAnalysis<MethodLiveVarInfo>(),
Chris Lattner14ab1ce2002-02-04 17:48:00 +000054 &getAnalysis<cfg::LoopInfo>());
Chris Lattner2f9b28e2002-02-04 15:54:09 +000055 PRA.allocateRegisters();
56
57 if (DEBUG_RA) cerr << "\nRegister allocation complete!\n";
58 return false;
59 }
Chris Lattner4911c352002-02-04 17:39:42 +000060
61 virtual void getAnalysisUsageInfo(Pass::AnalysisSet &Requires,
62 Pass::AnalysisSet &Destroyed,
63 Pass::AnalysisSet &Provided) {
Chris Lattner14ab1ce2002-02-04 17:48:00 +000064 Requires.push_back(cfg::LoopInfo::ID);
Chris Lattner4d7fc112002-02-04 20:02:38 +000065 Requires.push_back(MethodLiveVarInfo::ID);
Vikram S. Adve9c4f7262002-03-24 03:54:03 +000066 Destroyed.push_back(MethodLiveVarInfo::ID);
Chris Lattner4911c352002-02-04 17:39:42 +000067 }
Chris Lattner2f9b28e2002-02-04 15:54:09 +000068 };
Chris Lattner6dd98a62002-02-04 00:33:08 +000069}
70
Chris Lattner2f9b28e2002-02-04 15:54:09 +000071MethodPass *getRegisterAllocator(TargetMachine &T) {
72 return new RegisterAllocator(T);
73}
Chris Lattner6dd98a62002-02-04 00:33:08 +000074
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000075//----------------------------------------------------------------------------
76// Constructor: Init local composite objects and create register classes.
77//----------------------------------------------------------------------------
Vikram S. Adve12af1642001-11-08 04:48:50 +000078PhyRegAlloc::PhyRegAlloc(Method *M,
Ruchira Sasanka8e604792001-09-14 21:18:34 +000079 const TargetMachine& tm,
Chris Lattner4911c352002-02-04 17:39:42 +000080 MethodLiveVarInfo *Lvi,
Chris Lattner14ab1ce2002-02-04 17:48:00 +000081 cfg::LoopInfo *LDC)
Chris Lattner697954c2002-01-20 22:54:45 +000082 : TM(tm), Meth(M),
Vikram S. Adve12af1642001-11-08 04:48:50 +000083 mcInfo(MachineCodeForMethod::get(M)),
84 LVI(Lvi), LRI(M, tm, RegClassList),
Ruchira Sasanka8e604792001-09-14 21:18:34 +000085 MRI( tm.getRegInfo() ),
86 NumOfRegClasses(MRI.getNumOfRegClasses()),
Chris Lattner4911c352002-02-04 17:39:42 +000087 LoopDepthCalc(LDC) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +000088
89 // create each RegisterClass and put in RegClassList
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000090 //
Chris Lattner697954c2002-01-20 22:54:45 +000091 for(unsigned int rc=0; rc < NumOfRegClasses; rc++)
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000092 RegClassList.push_back( new RegClass(M, MRI.getMachineRegClass(rc),
93 &ResColList) );
Ruchira Sasanka8e604792001-09-14 21:18:34 +000094}
95
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000096
97//----------------------------------------------------------------------------
98// Destructor: Deletes register classes
99//----------------------------------------------------------------------------
100PhyRegAlloc::~PhyRegAlloc() {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000101 for( unsigned int rc=0; rc < NumOfRegClasses; rc++)
102 delete RegClassList[rc];
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000103}
104
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000105//----------------------------------------------------------------------------
106// This method initally creates interference graphs (one in each reg class)
107// and IGNodeList (one in each IG). The actual nodes will be pushed later.
108//----------------------------------------------------------------------------
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000109void PhyRegAlloc::createIGNodeListsAndIGs() {
110 if (DEBUG_RA) cerr << "Creating LR lists ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000111
112 // hash map iterator
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000113 LiveRangeMapType::const_iterator HMI = LRI.getLiveRangeMap()->begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000114
115 // hash map end
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000116 LiveRangeMapType::const_iterator HMIEnd = LRI.getLiveRangeMap()->end();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000117
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000118 for (; HMI != HMIEnd ; ++HMI ) {
119 if (HMI->first) {
120 LiveRange *L = HMI->second; // get the LiveRange
121 if (!L) {
122 if( DEBUG_RA) {
Chris Lattner0665a5f2002-02-05 01:43:49 +0000123 cerr << "\n*?!?Warning: Null liver range found for: "
124 << RAV(HMI->first) << "\n";
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000125 }
126 continue;
127 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000128 // if the Value * is not null, and LR
129 // is not yet written to the IGNodeList
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000130 if( !(L->getUserIGNode()) ) {
131 RegClass *const RC = // RegClass of first value in the LR
132 RegClassList[ L->getRegClass()->getID() ];
133
134 RC->addLRToIG(L); // add this LR to an IG
135 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000136 }
137 }
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000138
139 // init RegClassList
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000140 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000141 RegClassList[rc]->createInterferenceGraph();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000142
143 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000144 cerr << "LRLists Created!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000145}
146
147
148
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000149
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000150//----------------------------------------------------------------------------
151// This method will add all interferences at for a given instruction.
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000152// Interence occurs only if the LR of Def (Inst or Arg) is of the same reg
153// class as that of live var. The live var passed to this function is the
154// LVset AFTER the instruction
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000155//----------------------------------------------------------------------------
Chris Lattner296b7732002-02-05 02:52:05 +0000156void PhyRegAlloc::addInterference(const Value *Def,
157 const ValueSet *LVSet,
158 bool isCallInst) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000159
Chris Lattner296b7732002-02-05 02:52:05 +0000160 ValueSet::const_iterator LIt = LVSet->begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000161
162 // get the live range of instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000163 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000164 const LiveRange *const LROfDef = LRI.getLiveRangeForValue( Def );
165
166 IGNode *const IGNodeOfDef = LROfDef->getUserIGNode();
167 assert( IGNodeOfDef );
168
169 RegClass *const RCOfDef = LROfDef->getRegClass();
170
171 // for each live var in live variable set
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000172 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000173 for( ; LIt != LVSet->end(); ++LIt) {
174
Chris Lattner0665a5f2002-02-05 01:43:49 +0000175 if (DEBUG_RA > 1)
176 cerr << "< Def=" << RAV(Def) << ", Lvar=" << RAV(*LIt) << "> ";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000177
178 // get the live range corresponding to live var
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000179 //
Chris Lattner0665a5f2002-02-05 01:43:49 +0000180 LiveRange *LROfVar = LRI.getLiveRangeForValue(*LIt);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000181
182 // LROfVar can be null if it is a const since a const
183 // doesn't have a dominating def - see Assumptions above
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000184 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000185 if (LROfVar) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000186 if(LROfDef == LROfVar) // do not set interf for same LR
187 continue;
188
189 // if 2 reg classes are the same set interference
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000190 //
Chris Lattner0665a5f2002-02-05 01:43:49 +0000191 if (RCOfDef == LROfVar->getRegClass()) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000192 RCOfDef->setInterference( LROfDef, LROfVar);
Chris Lattner0665a5f2002-02-05 01:43:49 +0000193 } else if (DEBUG_RA > 1) {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000194 // we will not have LRs for values not explicitly allocated in the
195 // instruction stream (e.g., constants)
Chris Lattner0665a5f2002-02-05 01:43:49 +0000196 cerr << " warning: no live range for " << RAV(*LIt) << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000197 }
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000198 }
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000199 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000200}
201
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000202
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000203
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000204//----------------------------------------------------------------------------
205// For a call instruction, this method sets the CallInterference flag in
206// the LR of each variable live int the Live Variable Set live after the
207// call instruction (except the return value of the call instruction - since
208// the return value does not interfere with that call itself).
209//----------------------------------------------------------------------------
210
211void PhyRegAlloc::setCallInterferences(const MachineInstr *MInst,
Chris Lattner296b7732002-02-05 02:52:05 +0000212 const ValueSet *LVSetAft) {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000213
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000214 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000215 cerr << "\n For call inst: " << *MInst;
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000216
Chris Lattner296b7732002-02-05 02:52:05 +0000217 ValueSet::const_iterator LIt = LVSetAft->begin();
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000218
219 // for each live var in live variable set after machine inst
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000220 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000221 for( ; LIt != LVSetAft->end(); ++LIt) {
222
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000223 // get the live range corresponding to live var
224 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000225 LiveRange *const LR = LRI.getLiveRangeForValue(*LIt );
226
227 if( LR && DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000228 cerr << "\n\tLR Aft Call: ";
Chris Lattner296b7732002-02-05 02:52:05 +0000229 printSet(*LR);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000230 }
231
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000232 // LR can be null if it is a const since a const
233 // doesn't have a dominating def - see Assumptions above
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000234 //
Vikram S. Adve1a53f032002-03-31 18:54:37 +0000235 if( LR ) {
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000236 LR->setCallInterference();
237 if( DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000238 cerr << "\n ++Added call interf for LR: " ;
Chris Lattner296b7732002-02-05 02:52:05 +0000239 printSet(*LR);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000240 }
241 }
242
243 }
244
Vikram S. Adve1a53f032002-03-31 18:54:37 +0000245 // Now find the LR of the return value of the call
246 // We do this because, we look at the LV set *after* the instruction
247 // to determine, which LRs must be saved across calls. The return value
248 // of the call is live in this set - but it does not interfere with call
249 // (i.e., we can allocate a volatile register to the return value)
250 //
251 if( const Value *RetVal = MRI.getCallInstRetVal( MInst )) {
252 LiveRange *RetValLR = LRI.getLiveRangeForValue( RetVal );
253 assert( RetValLR && "No LR for RetValue of call");
254 RetValLR->clearCallInterference();
255 }
256
257 // If the CALL is an indirect call, find the LR of the function pointer.
258 // That has a call interference because it conflicts with outgoing args.
259 if( const Value *AddrVal = MRI.getCallInstIndirectAddrVal( MInst )) {
260 LiveRange *AddrValLR = LRI.getLiveRangeForValue( AddrVal );
261 assert( AddrValLR && "No LR for indirect addr val of call");
262 AddrValLR->setCallInterference();
263 }
264
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000265}
266
267
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000268
269
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000270//----------------------------------------------------------------------------
271// This method will walk thru code and create interferences in the IG of
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000272// each RegClass. Also, this method calculates the spill cost of each
273// Live Range (it is done in this method to save another pass over the code).
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000274//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000275void PhyRegAlloc::buildInterferenceGraphs()
276{
277
Chris Lattner697954c2002-01-20 22:54:45 +0000278 if(DEBUG_RA) cerr << "Creating interference graphs ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000279
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000280 unsigned BBLoopDepthCost;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000281 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
282
283 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
284
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000285 // find the 10^(loop_depth) of this BB
286 //
Chris Lattner4911c352002-02-04 17:39:42 +0000287 BBLoopDepthCost = (unsigned) pow( 10.0, LoopDepthCalc->getLoopDepth(*BBI));
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000288
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000289 // get the iterator for machine instructions
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000290 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000291 const MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
292 MachineCodeForBasicBlock::const_iterator
293 MInstIterator = MIVec.begin();
294
295 // iterate over all the machine instructions in BB
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000296 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000297 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000298
Chris Lattner748697d2002-02-05 04:20:12 +0000299 const MachineInstr *MInst = *MInstIterator;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000300
301 // get the LV set after the instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000302 //
Chris Lattner748697d2002-02-05 04:20:12 +0000303 const ValueSet &LVSetAI = LVI->getLiveVarSetAfterMInst(MInst, *BBI);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000304
305 const bool isCallInst = TM.getInstrInfo().isCall(MInst->getOpCode());
306
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000307 if( isCallInst ) {
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000308 // set the isCallInterference flag of each live range wich extends
309 // accross this call instruction. This information is used by graph
310 // coloring algo to avoid allocating volatile colors to live ranges
311 // that span across calls (since they have to be saved/restored)
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000312 //
Chris Lattner748697d2002-02-05 04:20:12 +0000313 setCallInterferences(MInst, &LVSetAI);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000314 }
315
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000316
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000317 // iterate over all MI operands to find defs
318 //
Chris Lattner2f898d22002-02-05 06:02:59 +0000319 for (MachineInstr::const_val_op_iterator OpI = MInst->begin(),
320 OpE = MInst->end(); OpI != OpE; ++OpI) {
321 if (OpI.isDef()) // create a new LR iff this operand is a def
Chris Lattner748697d2002-02-05 04:20:12 +0000322 addInterference(*OpI, &LVSetAI, isCallInst);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000323
324 // Calculate the spill cost of each live range
325 //
Chris Lattner2f898d22002-02-05 06:02:59 +0000326 LiveRange *LR = LRI.getLiveRangeForValue(*OpI);
327 if (LR) LR->addSpillCost(BBLoopDepthCost);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000328 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000329
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000330
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000331 // if there are multiple defs in this instruction e.g. in SETX
332 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000333 if (TM.getInstrInfo().isPseudoInstr(MInst->getOpCode()))
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000334 addInterf4PseudoInstr(MInst);
335
336
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000337 // Also add interference for any implicit definitions in a machine
338 // instr (currently, only calls have this).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000339 //
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000340 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
341 if( NumOfImpRefs > 0 ) {
342 for(unsigned z=0; z < NumOfImpRefs; z++)
343 if( MInst->implicitRefIsDefined(z) )
Chris Lattner748697d2002-02-05 04:20:12 +0000344 addInterference( MInst->getImplicitRef(z), &LVSetAI, isCallInst );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000345 }
346
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +0000347
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000348 } // for all machine instructions in BB
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000349
350 } // for all BBs in method
351
352
353 // add interferences for method arguments. Since there are no explict
354 // defs in method for args, we have to add them manually
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000355 //
356 addInterferencesForArgs();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000357
358 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000359 cerr << "Interference graphs calculted!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000360
361}
362
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000363
364
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000365//--------------------------------------------------------------------------
366// Pseudo instructions will be exapnded to multiple instructions by the
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000367// assembler. Consequently, all the opernds must get distinct registers.
368// Therefore, we mark all operands of a pseudo instruction as they interfere
369// with one another.
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000370//--------------------------------------------------------------------------
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000371void PhyRegAlloc::addInterf4PseudoInstr(const MachineInstr *MInst) {
372
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000373 bool setInterf = false;
374
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000375 // iterate over MI operands to find defs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000376 //
Chris Lattner2f898d22002-02-05 06:02:59 +0000377 for (MachineInstr::const_val_op_iterator It1 = MInst->begin(),
378 ItE = MInst->end(); It1 != ItE; ++It1) {
379 const LiveRange *LROfOp1 = LRI.getLiveRangeForValue(*It1);
380 assert((LROfOp1 || !It1.isDef()) && "No LR for Def in PSEUDO insruction");
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000381
Chris Lattner2f898d22002-02-05 06:02:59 +0000382 MachineInstr::const_val_op_iterator It2 = It1;
383 for(++It2; It2 != ItE; ++It2) {
384 const LiveRange *LROfOp2 = LRI.getLiveRangeForValue(*It2);
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000385
Chris Lattner2f898d22002-02-05 06:02:59 +0000386 if (LROfOp2) {
387 RegClass *RCOfOp1 = LROfOp1->getRegClass();
388 RegClass *RCOfOp2 = LROfOp2->getRegClass();
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000389
390 if( RCOfOp1 == RCOfOp2 ){
391 RCOfOp1->setInterference( LROfOp1, LROfOp2 );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000392 setInterf = true;
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000393 }
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000394 } // if Op2 has a LR
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000395 } // for all other defs in machine instr
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000396 } // for all operands in an instruction
397
Chris Lattner2f898d22002-02-05 06:02:59 +0000398 if (!setInterf && MInst->getNumOperands() > 2) {
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000399 cerr << "\nInterf not set for any operand in pseudo instr:\n";
400 cerr << *MInst;
401 assert(0 && "Interf not set for pseudo instr with > 2 operands" );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000402 }
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000403}
404
405
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000406
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000407//----------------------------------------------------------------------------
408// This method will add interferences for incoming arguments to a method.
409//----------------------------------------------------------------------------
Chris Lattner296b7732002-02-05 02:52:05 +0000410void PhyRegAlloc::addInterferencesForArgs() {
411 // get the InSet of root BB
Chris Lattner748697d2002-02-05 04:20:12 +0000412 const ValueSet &InSet = LVI->getInSetOfBB(Meth->front());
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000413
Chris Lattner296b7732002-02-05 02:52:05 +0000414 // get the argument list
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000415 const Method::ArgumentListType& ArgList = Meth->getArgumentList();
416
Chris Lattner296b7732002-02-05 02:52:05 +0000417 // get an iterator to arg list
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000418 Method::ArgumentListType::const_iterator ArgIt = ArgList.begin();
419
420
421 for( ; ArgIt != ArgList.end() ; ++ArgIt) { // for each argument
Chris Lattner748697d2002-02-05 04:20:12 +0000422 addInterference((Value*)*ArgIt, &InSet, false);// add interferences between
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000423 // args and LVars at start
Chris Lattner0665a5f2002-02-05 01:43:49 +0000424 if( DEBUG_RA > 1)
425 cerr << " - %% adding interference for argument "
426 << RAV((const Value *)*ArgIt) << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000427 }
428}
429
430
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000431
432
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000433//----------------------------------------------------------------------------
434// This method is called after register allocation is complete to set the
435// allocated reisters in the machine code. This code will add register numbers
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000436// to MachineOperands that contain a Value. Also it calls target specific
437// methods to produce caller saving instructions. At the end, it adds all
438// additional instructions produced by the register allocator to the
439// instruction stream.
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000440//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000441void PhyRegAlloc::updateMachineCode()
442{
443
444 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
445
446 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
447
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000448 // get the iterator for machine instructions
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000449 //
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000450 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
451 MachineCodeForBasicBlock::iterator MInstIterator = MIVec.begin();
452
453 // iterate over all the machine instructions in BB
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000454 //
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000455 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
456
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000457 MachineInstr *MInst = *MInstIterator;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000458
459 unsigned Opcode = MInst->getOpCode();
460
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000461 // do not process Phis
Vikram S. Adve23a4c8f2002-03-18 03:37:19 +0000462 if (TM.getInstrInfo().isDummyPhiInstr(Opcode))
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000463 continue;
464
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000465 // Now insert speical instructions (if necessary) for call/return
466 // instructions.
467 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000468 if (TM.getInstrInfo().isCall(Opcode) ||
469 TM.getInstrInfo().isReturn(Opcode)) {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000470
471 AddedInstrns *AI = AddedInstrMap[ MInst];
472 if ( !AI ) {
473 AI = new AddedInstrns();
474 AddedInstrMap[ MInst ] = AI;
475 }
476
477 // Tmp stack poistions are needed by some calls that have spilled args
478 // So reset it before we call each such method
Ruchira Sasanka6a3db8c2002-01-07 21:09:06 +0000479 //
480 mcInfo.popAllTempValues(TM);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000481
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000482 if (TM.getInstrInfo().isCall(Opcode))
483 MRI.colorCallArgs(MInst, LRI, AI, *this, *BBI);
484 else if (TM.getInstrInfo().isReturn(Opcode))
485 MRI.colorRetValue(MInst, LRI, AI);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000486 }
487
488
489 /* -- Using above code instead of this
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000490
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000491 // if this machine instr is call, insert caller saving code
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000492
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000493 if( (TM.getInstrInfo()).isCall( MInst->getOpCode()) )
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000494 MRI.insertCallerSavingCode(MInst, *BBI, *this );
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000495
496 */
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000497
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000498
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000499 // reset the stack offset for temporary variables since we may
500 // need that to spill
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000501 // mcInfo.popAllTempValues(TM);
Ruchira Sasankaf90870f2001-11-15 22:02:06 +0000502 // TODO ** : do later
Vikram S. Adve12af1642001-11-08 04:48:50 +0000503
Chris Lattner7a176752001-12-04 00:03:30 +0000504 //for(MachineInstr::val_const_op_iterator OpI(MInst);!OpI.done();++OpI) {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000505
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000506
507 // Now replace set the registers for operands in the machine instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000508 //
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000509 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
510
511 MachineOperand& Op = MInst->getOperand(OpNum);
512
513 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
514 Op.getOperandType() == MachineOperand::MO_CCRegister) {
515
516 const Value *const Val = Op.getVRegValue();
517
518 // delete this condition checking later (must assert if Val is null)
Chris Lattner045e7c82001-09-19 16:26:23 +0000519 if( !Val) {
520 if (DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000521 cerr << "Warning: NULL Value found for operand\n";
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000522 continue;
523 }
524 assert( Val && "Value is NULL");
525
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000526 LiveRange *const LR = LRI.getLiveRangeForValue(Val);
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000527
528 if ( !LR ) {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000529
530 // nothing to worry if it's a const or a label
531
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000532 if (DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000533 cerr << "*NO LR for operand : " << Op ;
534 cerr << " [reg:" << Op.getAllocatedRegNum() << "]";
535 cerr << " in inst:\t" << *MInst << "\n";
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000536 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000537
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000538 // if register is not allocated, mark register as invalid
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000539 if( Op.getAllocatedRegNum() == -1)
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000540 Op.setRegForValue( MRI.getInvalidRegNum());
Ruchira Sasankae727f852001-09-18 22:43:57 +0000541
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000542
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000543 continue;
544 }
545
546 unsigned RCID = (LR->getRegClass())->getID();
547
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000548 if( LR->hasColor() ) {
549 Op.setRegForValue( MRI.getUnifiedRegNum(RCID, LR->getColor()) );
550 }
551 else {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000552
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000553 // LR did NOT receive a color (register). Now, insert spill code
554 // for spilled opeands in this machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000555
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000556 //assert(0 && "LR must be spilled");
557 insertCode4SpilledLR(LR, MInst, *BBI, OpNum );
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000558
559 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000560 }
561
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000562 } // for each operand
563
564
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000565 // Now add instructions that the register allocator inserts before/after
566 // this machine instructions (done only for calls/rets/incoming args)
567 // We do this here, to ensure that spill for an instruction is inserted
568 // closest as possible to an instruction (see above insertCode4Spill...)
569 //
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000570 // If there are instructions to be added, *before* this machine
571 // instruction, add them now.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000572 //
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000573 if( AddedInstrMap[ MInst ] ) {
Chris Lattner697954c2002-01-20 22:54:45 +0000574 std::deque<MachineInstr *> &IBef = AddedInstrMap[MInst]->InstrnsBefore;
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000575
576 if( ! IBef.empty() ) {
Chris Lattner697954c2002-01-20 22:54:45 +0000577 std::deque<MachineInstr *>::iterator AdIt;
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000578
579 for( AdIt = IBef.begin(); AdIt != IBef.end() ; ++AdIt ) {
580
581 if( DEBUG_RA) {
582 cerr << "For inst " << *MInst;
Chris Lattner697954c2002-01-20 22:54:45 +0000583 cerr << " PREPENDed instr: " << **AdIt << "\n";
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000584 }
585
586 MInstIterator = MIVec.insert( MInstIterator, *AdIt );
587 ++MInstIterator;
588 }
589
590 }
591
592 }
593
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000594 // If there are instructions to be added *after* this machine
595 // instruction, add them now
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000596 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000597 if(AddedInstrMap[MInst] &&
598 !AddedInstrMap[MInst]->InstrnsAfter.empty() ) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000599
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000600 // if there are delay slots for this instruction, the instructions
601 // added after it must really go after the delayed instruction(s)
602 // So, we move the InstrAfter of the current instruction to the
603 // corresponding delayed instruction
604
605 unsigned delay;
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000606 if ((delay=TM.getInstrInfo().getNumDelaySlots(MInst->getOpCode())) >0){
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000607 move2DelayedInstr(MInst, *(MInstIterator+delay) );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000608
Chris Lattner697954c2002-01-20 22:54:45 +0000609 if(DEBUG_RA) cerr<< "\nMoved an added instr after the delay slot";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000610 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000611
612 else {
613
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000614
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000615 // Here we can add the "instructions after" to the current
616 // instruction since there are no delay slots for this instruction
617
Chris Lattner697954c2002-01-20 22:54:45 +0000618 std::deque<MachineInstr *> &IAft = AddedInstrMap[MInst]->InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000619
620 if( ! IAft.empty() ) {
621
Chris Lattner697954c2002-01-20 22:54:45 +0000622 std::deque<MachineInstr *>::iterator AdIt;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000623
624 ++MInstIterator; // advance to the next instruction
625
626 for( AdIt = IAft.begin(); AdIt != IAft.end() ; ++AdIt ) {
627
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000628 if(DEBUG_RA) {
629 cerr << "For inst " << *MInst;
Chris Lattner697954c2002-01-20 22:54:45 +0000630 cerr << " APPENDed instr: " << **AdIt << "\n";
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000631 }
632
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000633 MInstIterator = MIVec.insert( MInstIterator, *AdIt );
634 ++MInstIterator;
635 }
636
637 // MInsterator already points to the next instr. Since the
638 // for loop also increments it, decrement it to point to the
639 // instruction added last
640 --MInstIterator;
641
642 }
643
644 } // if not delay
645
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000646 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000647
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000648 } // for each machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000649 }
650}
651
652
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000653
654//----------------------------------------------------------------------------
655// This method inserts spill code for AN operand whose LR was spilled.
656// This method may be called several times for a single machine instruction
657// if it contains many spilled operands. Each time it is called, it finds
658// a register which is not live at that instruction and also which is not
659// used by other spilled operands of the same instruction. Then it uses
660// this register temporarily to accomodate the spilled value.
661//----------------------------------------------------------------------------
662void PhyRegAlloc::insertCode4SpilledLR(const LiveRange *LR,
663 MachineInstr *MInst,
664 const BasicBlock *BB,
665 const unsigned OpNum) {
666
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000667 assert(! TM.getInstrInfo().isCall(MInst->getOpCode()) &&
668 (! TM.getInstrInfo().isReturn(MInst->getOpCode())) &&
669 "Arg of a call/ret must be handled elsewhere");
670
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000671 MachineOperand& Op = MInst->getOperand(OpNum);
672 bool isDef = MInst->operandIsDefined(OpNum);
673 unsigned RegType = MRI.getRegType( LR );
674 int SpillOff = LR->getSpillOffFromFP();
675 RegClass *RC = LR->getRegClass();
Chris Lattner748697d2002-02-05 04:20:12 +0000676 const ValueSet &LVSetBef = LVI->getLiveVarSetBeforeMInst(MInst, BB);
Vikram S. Adve00521d72001-11-12 23:26:35 +0000677
Chris Lattner697954c2002-01-20 22:54:45 +0000678 mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000679
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000680 MachineInstr *MIBef=NULL, *AdIMid=NULL, *MIAft=NULL;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000681
Chris Lattner748697d2002-02-05 04:20:12 +0000682 int TmpRegU = getUsableUniRegAtMI(RC, RegType, MInst,&LVSetBef, MIBef, MIAft);
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000683
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000684 // get the added instructions for this instruciton
685 AddedInstrns *AI = AddedInstrMap[ MInst ];
686 if ( !AI ) {
687 AI = new AddedInstrns();
688 AddedInstrMap[ MInst ] = AI;
689 }
690
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000691
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000692 if( !isDef ) {
693
694 // for a USE, we have to load the value of LR from stack to a TmpReg
695 // and use the TmpReg as one operand of instruction
696
697 // actual loading instruction
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000698 AdIMid = MRI.cpMem2RegMI(MRI.getFramePointer(), SpillOff, TmpRegU,RegType);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000699
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000700 if(MIBef)
701 AI->InstrnsBefore.push_back(MIBef);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000702
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000703 AI->InstrnsBefore.push_back(AdIMid);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000704
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000705 if(MIAft)
706 AI->InstrnsAfter.push_front(MIAft);
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000707
Chris Lattner296b7732002-02-05 02:52:05 +0000708 } else { // if this is a Def
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000709 // for a DEF, we have to store the value produced by this instruction
710 // on the stack position allocated for this LR
711
712 // actual storing instruction
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000713 AdIMid = MRI.cpReg2MemMI(TmpRegU, MRI.getFramePointer(), SpillOff,RegType);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000714
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000715 if (MIBef)
716 AI->InstrnsBefore.push_back(MIBef);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000717
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000718 AI->InstrnsAfter.push_front(AdIMid);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000719
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000720 if (MIAft)
721 AI->InstrnsAfter.push_front(MIAft);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000722
723 } // if !DEF
724
725 cerr << "\nFor Inst " << *MInst;
Chris Lattner296b7732002-02-05 02:52:05 +0000726 cerr << " - SPILLED LR: "; printSet(*LR);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000727 cerr << "\n - Added Instructions:";
Chris Lattner296b7732002-02-05 02:52:05 +0000728 if (MIBef) cerr << *MIBef;
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000729 cerr << *AdIMid;
Chris Lattner296b7732002-02-05 02:52:05 +0000730 if (MIAft) cerr << *MIAft;
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000731
Chris Lattner296b7732002-02-05 02:52:05 +0000732 Op.setRegForValue(TmpRegU); // set the opearnd
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000733}
734
735
736
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000737//----------------------------------------------------------------------------
738// We can use the following method to get a temporary register to be used
739// BEFORE any given machine instruction. If there is a register available,
740// this method will simply return that register and set MIBef = MIAft = NULL.
741// Otherwise, it will return a register and MIAft and MIBef will contain
742// two instructions used to free up this returned register.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000743// Returned register number is the UNIFIED register number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000744//----------------------------------------------------------------------------
745
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000746int PhyRegAlloc::getUsableUniRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000747 const int RegType,
748 const MachineInstr *MInst,
Chris Lattner296b7732002-02-05 02:52:05 +0000749 const ValueSet *LVSetBef,
Vikram S. Adve23a4c8f2002-03-18 03:37:19 +0000750 MachineInstr *&MIBef,
751 MachineInstr *&MIAft) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000752
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000753 int RegU = getUnusedUniRegAtMI(RC, MInst, LVSetBef);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000754
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000755
756 if( RegU != -1) {
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000757 // we found an unused register, so we can simply use it
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000758 MIBef = MIAft = NULL;
759 }
760 else {
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000761 // we couldn't find an unused register. Generate code to free up a reg by
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000762 // saving it on stack and restoring after the instruction
763
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000764 int TmpOff = mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
Vikram S. Adve12af1642001-11-08 04:48:50 +0000765
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000766 RegU = getUniRegNotUsedByThisInst(RC, MInst);
767 MIBef = MRI.cpReg2MemMI(RegU, MRI.getFramePointer(), TmpOff, RegType );
768 MIAft = MRI.cpMem2RegMI(MRI.getFramePointer(), TmpOff, RegU, RegType );
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000769 }
770
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000771 return RegU;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000772}
773
774//----------------------------------------------------------------------------
775// This method is called to get a new unused register that can be used to
776// accomodate a spilled value.
777// This method may be called several times for a single machine instruction
778// if it contains many spilled operands. Each time it is called, it finds
779// a register which is not live at that instruction and also which is not
780// used by other spilled operands of the same instruction.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000781// Return register number is relative to the register class. NOT
782// unified number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000783//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000784int PhyRegAlloc::getUnusedUniRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000785 const MachineInstr *MInst,
Chris Lattner296b7732002-02-05 02:52:05 +0000786 const ValueSet *LVSetBef) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000787
788 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
789
790 bool *IsColorUsedArr = RC->getIsColorUsedArr();
791
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000792 for(unsigned i=0; i < NumAvailRegs; i++) // Reset array
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000793 IsColorUsedArr[i] = false;
794
Chris Lattner296b7732002-02-05 02:52:05 +0000795 ValueSet::const_iterator LIt = LVSetBef->begin();
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000796
797 // for each live var in live variable set after machine inst
798 for( ; LIt != LVSetBef->end(); ++LIt) {
799
800 // get the live range corresponding to live var
801 LiveRange *const LRofLV = LRI.getLiveRangeForValue(*LIt );
802
803 // LR can be null if it is a const since a const
804 // doesn't have a dominating def - see Assumptions above
805 if( LRofLV )
806 if( LRofLV->hasColor() )
807 IsColorUsedArr[ LRofLV->getColor() ] = true;
808 }
809
810 // It is possible that one operand of this MInst was already spilled
811 // and it received some register temporarily. If that's the case,
812 // it is recorded in machine operand. We must skip such registers.
813
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000814 setRelRegsUsedByThisInst(RC, MInst);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000815
816 unsigned c; // find first unused color
817 for( c=0; c < NumAvailRegs; c++)
818 if( ! IsColorUsedArr[ c ] ) break;
819
820 if(c < NumAvailRegs)
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000821 return MRI.getUnifiedRegNum(RC->getID(), c);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000822 else
823 return -1;
824
825
826}
827
828
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000829//----------------------------------------------------------------------------
830// Get any other register in a register class, other than what is used
831// by operands of a machine instruction. Returns the unified reg number.
832//----------------------------------------------------------------------------
833int PhyRegAlloc::getUniRegNotUsedByThisInst(RegClass *RC,
834 const MachineInstr *MInst) {
835
836 bool *IsColorUsedArr = RC->getIsColorUsedArr();
837 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
838
839
840 for(unsigned i=0; i < NumAvailRegs ; i++) // Reset array
841 IsColorUsedArr[i] = false;
842
843 setRelRegsUsedByThisInst(RC, MInst);
844
845 unsigned c; // find first unused color
846 for( c=0; c < RC->getNumOfAvailRegs(); c++)
847 if( ! IsColorUsedArr[ c ] ) break;
848
849 if(c < NumAvailRegs)
850 return MRI.getUnifiedRegNum(RC->getID(), c);
851 else
852 assert( 0 && "FATAL: No free register could be found in reg class!!");
Chris Lattner697954c2002-01-20 22:54:45 +0000853 return 0;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000854}
855
856
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000857//----------------------------------------------------------------------------
858// This method modifies the IsColorUsedArr of the register class passed to it.
859// It sets the bits corresponding to the registers used by this machine
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000860// instructions. Both explicit and implicit operands are set.
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000861//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000862void PhyRegAlloc::setRelRegsUsedByThisInst(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000863 const MachineInstr *MInst ) {
864
865 bool *IsColorUsedArr = RC->getIsColorUsedArr();
866
867 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
868
869 const MachineOperand& Op = MInst->getOperand(OpNum);
870
871 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000872 Op.getOperandType() == MachineOperand::MO_CCRegister ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000873
874 const Value *const Val = Op.getVRegValue();
875
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000876 if( Val )
877 if( MRI.getRegClassIDOfValue(Val) == RC->getID() ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000878 int Reg;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000879 if( (Reg=Op.getAllocatedRegNum()) != -1) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000880 IsColorUsedArr[ Reg ] = true;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000881 }
882 else {
883 // it is possilbe that this operand still is not marked with
884 // a register but it has a LR and that received a color
885
886 LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
887 if( LROfVal)
888 if( LROfVal->hasColor() )
889 IsColorUsedArr[ LROfVal->getColor() ] = true;
890 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000891
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000892 } // if reg classes are the same
893 }
894 else if (Op.getOperandType() == MachineOperand::MO_MachineRegister) {
895 IsColorUsedArr[ Op.getMachineRegNum() ] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000896 }
897 }
898
899 // If there are implicit references, mark them as well
900
901 for(unsigned z=0; z < MInst->getNumImplicitRefs(); z++) {
902
903 LiveRange *const LRofImpRef =
904 LRI.getLiveRangeForValue( MInst->getImplicitRef(z) );
Chris Lattner697954c2002-01-20 22:54:45 +0000905
906 if(LRofImpRef && LRofImpRef->hasColor())
907 IsColorUsedArr[LRofImpRef->getColor()] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000908 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000909}
910
911
912
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000913
914
915
916
917
918//----------------------------------------------------------------------------
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000919// If there are delay slots for an instruction, the instructions
920// added after it must really go after the delayed instruction(s).
921// So, we move the InstrAfter of that instruction to the
922// corresponding delayed instruction using the following method.
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000923
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000924//----------------------------------------------------------------------------
925void PhyRegAlloc:: move2DelayedInstr(const MachineInstr *OrigMI,
926 const MachineInstr *DelayedMI) {
927
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000928 // "added after" instructions of the original instr
Chris Lattner697954c2002-01-20 22:54:45 +0000929 std::deque<MachineInstr *> &OrigAft = AddedInstrMap[OrigMI]->InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000930
931 // "added instructions" of the delayed instr
932 AddedInstrns *DelayAdI = AddedInstrMap[DelayedMI];
933
934 if(! DelayAdI ) { // create a new "added after" if necessary
935 DelayAdI = new AddedInstrns();
936 AddedInstrMap[DelayedMI] = DelayAdI;
937 }
938
939 // "added after" instructions of the delayed instr
Chris Lattner697954c2002-01-20 22:54:45 +0000940 std::deque<MachineInstr *> &DelayedAft = DelayAdI->InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000941
942 // go thru all the "added after instructions" of the original instruction
943 // and append them to the "addded after instructions" of the delayed
944 // instructions
Chris Lattner697954c2002-01-20 22:54:45 +0000945 DelayedAft.insert(DelayedAft.end(), OrigAft.begin(), OrigAft.end());
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000946
947 // empty the "added after instructions" of the original instruction
948 OrigAft.clear();
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000949}
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000950
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000951//----------------------------------------------------------------------------
952// This method prints the code with registers after register allocation is
953// complete.
954//----------------------------------------------------------------------------
955void PhyRegAlloc::printMachineCode()
956{
957
Chris Lattner697954c2002-01-20 22:54:45 +0000958 cerr << "\n;************** Method " << Meth->getName()
959 << " *****************\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000960
961 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
962
963 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
964
Chris Lattner697954c2002-01-20 22:54:45 +0000965 cerr << "\n"; printLabel( *BBI); cerr << ": ";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000966
967 // get the iterator for machine instructions
968 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
969 MachineCodeForBasicBlock::iterator MInstIterator = MIVec.begin();
970
971 // iterate over all the machine instructions in BB
972 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
973
974 MachineInstr *const MInst = *MInstIterator;
975
976
Chris Lattner697954c2002-01-20 22:54:45 +0000977 cerr << "\n\t";
978 cerr << TargetInstrDescriptors[MInst->getOpCode()].opCodeString;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000979
980
Chris Lattner7a176752001-12-04 00:03:30 +0000981 //for(MachineInstr::val_const_op_iterator OpI(MInst);!OpI.done();++OpI) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000982
983 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
984
985 MachineOperand& Op = MInst->getOperand(OpNum);
986
987 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasanka97b8b442001-10-18 22:36:26 +0000988 Op.getOperandType() == MachineOperand::MO_CCRegister /*||
989 Op.getOperandType() == MachineOperand::MO_PCRelativeDisp*/ ) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000990
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000991 const Value *const Val = Op.getVRegValue () ;
Ruchira Sasankae727f852001-09-18 22:43:57 +0000992 // ****this code is temporary till NULL Values are fixed
993 if( ! Val ) {
Chris Lattner697954c2002-01-20 22:54:45 +0000994 cerr << "\t<*NULL*>";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000995 continue;
996 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000997
998 // if a label or a constant
Chris Lattnerdbe53042002-01-21 01:33:12 +0000999 if(isa<BasicBlock>(Val)) {
Chris Lattner697954c2002-01-20 22:54:45 +00001000 cerr << "\t"; printLabel( Op.getVRegValue () );
1001 } else {
Ruchira Sasankae727f852001-09-18 22:43:57 +00001002 // else it must be a register value
1003 const int RegNum = Op.getAllocatedRegNum();
1004
Chris Lattner697954c2002-01-20 22:54:45 +00001005 cerr << "\t" << "%" << MRI.getUnifiedRegName( RegNum );
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001006 if (Val->hasName() )
Chris Lattner697954c2002-01-20 22:54:45 +00001007 cerr << "(" << Val->getName() << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001008 else
Chris Lattner697954c2002-01-20 22:54:45 +00001009 cerr << "(" << Val << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001010
1011 if( Op.opIsDef() )
Chris Lattner697954c2002-01-20 22:54:45 +00001012 cerr << "*";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001013
1014 const LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
1015 if( LROfVal )
1016 if( LROfVal->hasSpillOffset() )
Chris Lattner697954c2002-01-20 22:54:45 +00001017 cerr << "$";
Ruchira Sasankae727f852001-09-18 22:43:57 +00001018 }
1019
1020 }
1021 else if(Op.getOperandType() == MachineOperand::MO_MachineRegister) {
Chris Lattner697954c2002-01-20 22:54:45 +00001022 cerr << "\t" << "%" << MRI.getUnifiedRegName(Op.getMachineRegNum());
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001023 }
1024
1025 else
Chris Lattner697954c2002-01-20 22:54:45 +00001026 cerr << "\t" << Op; // use dump field
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001027 }
1028
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001029
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001030
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001031 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
Chris Lattner0665a5f2002-02-05 01:43:49 +00001032 if( NumOfImpRefs > 0) {
Chris Lattner697954c2002-01-20 22:54:45 +00001033 cerr << "\tImplicit:";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001034
Chris Lattner0665a5f2002-02-05 01:43:49 +00001035 for(unsigned z=0; z < NumOfImpRefs; z++)
1036 cerr << RAV(MInst->getImplicitRef(z)) << "\t";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001037 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001038
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001039 } // for all machine instructions
1040
Chris Lattner697954c2002-01-20 22:54:45 +00001041 cerr << "\n";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001042
1043 } // for all BBs
1044
Chris Lattner697954c2002-01-20 22:54:45 +00001045 cerr << "\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001046}
1047
Ruchira Sasankae727f852001-09-18 22:43:57 +00001048
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001049#if 0
1050
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001051//----------------------------------------------------------------------------
1052//
1053//----------------------------------------------------------------------------
1054
1055void PhyRegAlloc::colorCallRetArgs()
1056{
1057
1058 CallRetInstrListType &CallRetInstList = LRI.getCallRetInstrList();
1059 CallRetInstrListType::const_iterator It = CallRetInstList.begin();
1060
1061 for( ; It != CallRetInstList.end(); ++It ) {
1062
Ruchira Sasankaa90e7702001-10-15 16:26:38 +00001063 const MachineInstr *const CRMI = *It;
1064 unsigned OpCode = CRMI->getOpCode();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001065
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001066 // get the added instructions for this Call/Ret instruciton
1067 AddedInstrns *AI = AddedInstrMap[ CRMI ];
1068 if ( !AI ) {
1069 AI = new AddedInstrns();
1070 AddedInstrMap[ CRMI ] = AI;
1071 }
1072
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001073 // Tmp stack poistions are needed by some calls that have spilled args
1074 // So reset it before we call each such method
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001075 //mcInfo.popAllTempValues(TM);
1076
1077
Vikram S. Adve12af1642001-11-08 04:48:50 +00001078
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001079 if (TM.getInstrInfo().isCall(OpCode))
1080 MRI.colorCallArgs(CRMI, LRI, AI, *this);
1081 else if (TM.getInstrInfo().isReturn(OpCode))
Ruchira Sasankaa90e7702001-10-15 16:26:38 +00001082 MRI.colorRetValue( CRMI, LRI, AI );
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001083 else
1084 assert(0 && "Non Call/Ret instrn in CallRetInstrList\n");
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001085 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001086}
1087
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001088#endif
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001089
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001090//----------------------------------------------------------------------------
1091
1092//----------------------------------------------------------------------------
1093void PhyRegAlloc::colorIncomingArgs()
1094{
1095 const BasicBlock *const FirstBB = Meth->front();
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001096 const MachineInstr *FirstMI = FirstBB->getMachineInstrVec().front();
1097 assert(FirstMI && "No machine instruction in entry BB");
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001098
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001099 AddedInstrns *AI = AddedInstrMap[FirstMI];
1100 if (!AI)
1101 AddedInstrMap[FirstMI] = AI = new AddedInstrns();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001102
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001103 MRI.colorMethodArgs(Meth, LRI, AI);
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001104}
1105
Ruchira Sasankae727f852001-09-18 22:43:57 +00001106
1107//----------------------------------------------------------------------------
1108// Used to generate a label for a basic block
1109//----------------------------------------------------------------------------
Chris Lattner697954c2002-01-20 22:54:45 +00001110void PhyRegAlloc::printLabel(const Value *const Val) {
1111 if (Val->hasName())
1112 cerr << Val->getName();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001113 else
Chris Lattner697954c2002-01-20 22:54:45 +00001114 cerr << "Label" << Val;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001115}
1116
1117
Ruchira Sasankae727f852001-09-18 22:43:57 +00001118//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001119// This method calls setSugColorUsable method of each live range. This
1120// will determine whether the suggested color of LR is really usable.
1121// A suggested color is not usable when the suggested color is volatile
1122// AND when there are call interferences
1123//----------------------------------------------------------------------------
1124
1125void PhyRegAlloc::markUnusableSugColors()
1126{
Chris Lattner697954c2002-01-20 22:54:45 +00001127 if(DEBUG_RA ) cerr << "\nmarking unusable suggested colors ...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001128
1129 // hash map iterator
1130 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
1131 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
1132
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001133 for(; HMI != HMIEnd ; ++HMI ) {
1134 if (HMI->first) {
1135 LiveRange *L = HMI->second; // get the LiveRange
1136 if (L) {
1137 if(L->hasSuggestedColor()) {
1138 int RCID = L->getRegClass()->getID();
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001139 if( MRI.isRegVolatile( RCID, L->getSuggestedColor()) &&
1140 L->isCallInterference() )
1141 L->setSuggestedColorUsable( false );
1142 else
1143 L->setSuggestedColorUsable( true );
1144 }
1145 } // if L->hasSuggestedColor()
1146 }
1147 } // for all LR's in hash map
1148}
1149
1150
1151
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001152//----------------------------------------------------------------------------
1153// The following method will set the stack offsets of the live ranges that
1154// are decided to be spillled. This must be called just after coloring the
1155// LRs using the graph coloring algo. For each live range that is spilled,
1156// this method allocate a new spill position on the stack.
1157//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001158
Chris Lattner37730942002-02-05 03:52:29 +00001159void PhyRegAlloc::allocateStackSpace4SpilledLRs() {
1160 if (DEBUG_RA) cerr << "\nsetting LR stack offsets ...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001161
Chris Lattner37730942002-02-05 03:52:29 +00001162 LiveRangeMapType::const_iterator HMI = LRI.getLiveRangeMap()->begin();
1163 LiveRangeMapType::const_iterator HMIEnd = LRI.getLiveRangeMap()->end();
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001164
Chris Lattner37730942002-02-05 03:52:29 +00001165 for( ; HMI != HMIEnd ; ++HMI) {
1166 if (HMI->first && HMI->second) {
1167 LiveRange *L = HMI->second; // get the LiveRange
1168 if (!L->hasColor()) // NOTE: ** allocating the size of long Type **
1169 L->setSpillOffFromFP(mcInfo.allocateSpilledValue(TM, Type::LongTy));
1170 }
1171 } // for all LR's in hash map
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001172}
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001173
1174
1175
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001176//----------------------------------------------------------------------------
Ruchira Sasankae727f852001-09-18 22:43:57 +00001177// The entry pont to Register Allocation
1178//----------------------------------------------------------------------------
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001179
1180void PhyRegAlloc::allocateRegisters()
1181{
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001182
1183 // make sure that we put all register classes into the RegClassList
1184 // before we call constructLiveRanges (now done in the constructor of
1185 // PhyRegAlloc class).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001186 //
1187 LRI.constructLiveRanges(); // create LR info
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001188
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001189 if (DEBUG_RA)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001190 LRI.printLiveRanges();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001191
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001192 createIGNodeListsAndIGs(); // create IGNode list and IGs
1193
1194 buildInterferenceGraphs(); // build IGs in all reg classes
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001195
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001196
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001197 if (DEBUG_RA) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001198 // print all LRs in all reg classes
1199 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1200 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001201
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001202 // print IGs in all register classes
1203 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1204 RegClassList[ rc ]->printIG();
1205 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001206
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001207
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001208 LRI.coalesceLRs(); // coalesce all live ranges
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001209
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +00001210
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001211 if( DEBUG_RA) {
1212 // print all LRs in all reg classes
1213 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1214 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001215
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001216 // print IGs in all register classes
1217 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1218 RegClassList[ rc ]->printIG();
1219 }
1220
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001221
1222 // mark un-usable suggested color before graph coloring algorithm.
1223 // When this is done, the graph coloring algo will not reserve
1224 // suggested color unnecessarily - they can be used by another LR
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001225 //
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001226 markUnusableSugColors();
1227
1228 // color all register classes using the graph coloring algo
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001229 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1230 RegClassList[ rc ]->colorAllRegs();
1231
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001232 // Atter grpah coloring, if some LRs did not receive a color (i.e, spilled)
1233 // a poistion for such spilled LRs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001234 //
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001235 allocateStackSpace4SpilledLRs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001236
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001237 mcInfo.popAllTempValues(TM); // TODO **Check
1238
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001239 // color incoming args - if the correct color was not received
1240 // insert code to copy to the correct register
1241 //
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001242 colorIncomingArgs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001243
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001244 // Now update the machine code with register names and add any
1245 // additional code inserted by the register allocator to the instruction
1246 // stream
1247 //
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001248 updateMachineCode();
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001249
Chris Lattner045e7c82001-09-19 16:26:23 +00001250 if (DEBUG_RA) {
Vikram S. Adve12af1642001-11-08 04:48:50 +00001251 MachineCodeForMethod::get(Meth).dump();
Chris Lattner045e7c82001-09-19 16:26:23 +00001252 printMachineCode(); // only for DEBUGGING
1253 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001254}
1255
Ruchira Sasankae727f852001-09-18 22:43:57 +00001256
1257