blob: 415c7989989b45bb970127deda4ad26e9726fd50 [file] [log] [blame]
Chris Lattner179cdfb2002-08-09 20:08:03 +00001//===-- PhyRegAlloc.cpp ---------------------------------------------------===//
Vikram S. Adve12af1642001-11-08 04:48:50 +00002//
Chris Lattner179cdfb2002-08-09 20:08:03 +00003// Register allocation for LLVM.
4//
5//===----------------------------------------------------------------------===//
Ruchira Sasanka8e604792001-09-14 21:18:34 +00006
Chris Lattner6dd98a62002-02-04 00:33:08 +00007#include "llvm/CodeGen/RegisterAllocation.h"
Vikram S. Adve12af1642001-11-08 04:48:50 +00008#include "llvm/CodeGen/PhyRegAlloc.h"
9#include "llvm/CodeGen/MachineInstr.h"
Vikram S. Advedabb41d2002-05-19 15:29:31 +000010#include "llvm/CodeGen/MachineInstrAnnot.h"
Vikram S. Advef5af6362002-07-08 23:15:32 +000011#include "llvm/CodeGen/MachineCodeForBasicBlock.h"
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000012#include "llvm/CodeGen/MachineCodeForMethod.h"
Chris Lattner483e14e2002-04-27 07:27:19 +000013#include "llvm/Analysis/LiveVar/FunctionLiveVarInfo.h"
Chris Lattner14ab1ce2002-02-04 17:48:00 +000014#include "llvm/Analysis/LoopInfo.h"
Vikram S. Adve12af1642001-11-08 04:48:50 +000015#include "llvm/Target/TargetMachine.h"
16#include "llvm/Target/MachineFrameInfo.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000017#include "llvm/Function.h"
Chris Lattner37730942002-02-05 03:52:29 +000018#include "llvm/Type.h"
Vikram S. Advedabb41d2002-05-19 15:29:31 +000019#include "llvm/iOther.h"
Chris Lattnerc6f3ae52002-04-29 17:42:12 +000020#include "llvm/CodeGen/RegAllocCommon.h"
Chris Lattner70e60cb2002-05-22 17:08:27 +000021#include "Support/CommandLine.h"
Vikram S. Advef5af6362002-07-08 23:15:32 +000022#include "Support/STLExtras.h"
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000023#include <math.h>
Chris Lattner697954c2002-01-20 22:54:45 +000024using std::cerr;
Anand Shuklacfb22d32002-06-25 20:55:50 +000025using std::vector;
Vikram S. Adve12af1642001-11-08 04:48:50 +000026
Chris Lattner70e60cb2002-05-22 17:08:27 +000027RegAllocDebugLevel_t DEBUG_RA;
Chris Lattner5ff62e92002-07-22 02:10:13 +000028static cl::opt<RegAllocDebugLevel_t, true>
29DRA_opt("dregalloc", cl::Hidden, cl::location(DEBUG_RA),
30 cl::desc("enable register allocation debugging information"),
31 cl::values(
Chris Lattner045e7c82001-09-19 16:26:23 +000032 clEnumValN(RA_DEBUG_None , "n", "disable debug output"),
33 clEnumValN(RA_DEBUG_Normal , "y", "enable debug output"),
Chris Lattner5ff62e92002-07-22 02:10:13 +000034 clEnumValN(RA_DEBUG_Verbose, "v", "enable extra debug output"),
35 0));
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000036
37
Chris Lattner2f9b28e2002-02-04 15:54:09 +000038//----------------------------------------------------------------------------
39// RegisterAllocation pass front end...
40//----------------------------------------------------------------------------
41namespace {
Chris Lattnerf57b8452002-04-27 06:56:12 +000042 class RegisterAllocator : public FunctionPass {
Chris Lattner2f9b28e2002-02-04 15:54:09 +000043 TargetMachine &Target;
44 public:
45 inline RegisterAllocator(TargetMachine &T) : Target(T) {}
Chris Lattner96c466b2002-04-29 14:57:45 +000046
47 const char *getPassName() const { return "Register Allocation"; }
Chris Lattner6dd98a62002-02-04 00:33:08 +000048
Chris Lattner7e708292002-06-25 16:13:24 +000049 bool runOnFunction(Function &F) {
Chris Lattner2f9b28e2002-02-04 15:54:09 +000050 if (DEBUG_RA)
Chris Lattner7e708292002-06-25 16:13:24 +000051 cerr << "\n********* Function "<< F.getName() << " ***********\n";
Chris Lattner2f9b28e2002-02-04 15:54:09 +000052
Chris Lattner7e708292002-06-25 16:13:24 +000053 PhyRegAlloc PRA(&F, Target, &getAnalysis<FunctionLiveVarInfo>(),
Chris Lattner1b7f7dc2002-04-28 16:21:30 +000054 &getAnalysis<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
Chris Lattnerf57b8452002-04-27 06:56:12 +000061 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerdd5b4952002-08-08 19:01:28 +000062 AU.addRequired<LoopInfo>();
63 AU.addRequired<FunctionLiveVarInfo>();
Chris Lattner4911c352002-02-04 17:39:42 +000064 }
Chris Lattner2f9b28e2002-02-04 15:54:09 +000065 };
Chris Lattner6dd98a62002-02-04 00:33:08 +000066}
67
Chris Lattnerf57b8452002-04-27 06:56:12 +000068Pass *getRegisterAllocator(TargetMachine &T) {
Chris Lattner2f9b28e2002-02-04 15:54:09 +000069 return new RegisterAllocator(T);
70}
Chris Lattner6dd98a62002-02-04 00:33:08 +000071
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000072//----------------------------------------------------------------------------
73// Constructor: Init local composite objects and create register classes.
74//----------------------------------------------------------------------------
Chris Lattner1b7f7dc2002-04-28 16:21:30 +000075PhyRegAlloc::PhyRegAlloc(Function *F, const TargetMachine& tm,
76 FunctionLiveVarInfo *Lvi, LoopInfo *LDC)
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000077 : TM(tm), Meth(F),
78 mcInfo(MachineCodeForMethod::get(F)),
79 LVI(Lvi), LRI(F, tm, RegClassList),
80 MRI(tm.getRegInfo()),
Ruchira Sasanka8e604792001-09-14 21:18:34 +000081 NumOfRegClasses(MRI.getNumOfRegClasses()),
Chris Lattner4911c352002-02-04 17:39:42 +000082 LoopDepthCalc(LDC) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +000083
84 // create each RegisterClass and put in RegClassList
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000085 //
Chris Lattner7e708292002-06-25 16:13:24 +000086 for (unsigned rc=0; rc < NumOfRegClasses; rc++)
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000087 RegClassList.push_back(new RegClass(F, MRI.getMachineRegClass(rc),
88 &ResColList));
Ruchira Sasanka8e604792001-09-14 21:18:34 +000089}
90
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000091
92//----------------------------------------------------------------------------
93// Destructor: Deletes register classes
94//----------------------------------------------------------------------------
95PhyRegAlloc::~PhyRegAlloc() {
Chris Lattner7e708292002-06-25 16:13:24 +000096 for ( unsigned rc=0; rc < NumOfRegClasses; rc++)
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000097 delete RegClassList[rc];
Chris Lattner0b0ffa02002-04-09 05:13:04 +000098
99 AddedInstrMap.clear();
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000100}
101
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000102//----------------------------------------------------------------------------
103// This method initally creates interference graphs (one in each reg class)
104// and IGNodeList (one in each IG). The actual nodes will be pushed later.
105//----------------------------------------------------------------------------
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000106void PhyRegAlloc::createIGNodeListsAndIGs() {
107 if (DEBUG_RA) cerr << "Creating LR lists ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000108
109 // hash map iterator
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000110 LiveRangeMapType::const_iterator HMI = LRI.getLiveRangeMap()->begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000111
112 // hash map end
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000113 LiveRangeMapType::const_iterator HMIEnd = LRI.getLiveRangeMap()->end();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000114
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000115 for (; HMI != HMIEnd ; ++HMI ) {
116 if (HMI->first) {
117 LiveRange *L = HMI->second; // get the LiveRange
118 if (!L) {
Chris Lattner7e708292002-06-25 16:13:24 +0000119 if (DEBUG_RA) {
Chris Lattner0665a5f2002-02-05 01:43:49 +0000120 cerr << "\n*?!?Warning: Null liver range found for: "
121 << RAV(HMI->first) << "\n";
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000122 }
123 continue;
124 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000125 // if the Value * is not null, and LR
126 // is not yet written to the IGNodeList
Chris Lattner7e708292002-06-25 16:13:24 +0000127 if (!(L->getUserIGNode()) ) {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000128 RegClass *const RC = // RegClass of first value in the LR
129 RegClassList[ L->getRegClass()->getID() ];
130
131 RC->addLRToIG(L); // add this LR to an IG
132 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000133 }
134 }
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000135
136 // init RegClassList
Chris Lattner7e708292002-06-25 16:13:24 +0000137 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000138 RegClassList[rc]->createInterferenceGraph();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000139
Chris Lattner7e708292002-06-25 16:13:24 +0000140 if (DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000141 cerr << "LRLists Created!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000142}
143
144
145
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000146
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000147//----------------------------------------------------------------------------
148// This method will add all interferences at for a given instruction.
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000149// Interence occurs only if the LR of Def (Inst or Arg) is of the same reg
150// class as that of live var. The live var passed to this function is the
151// LVset AFTER the instruction
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000152//----------------------------------------------------------------------------
Chris Lattner296b7732002-02-05 02:52:05 +0000153void PhyRegAlloc::addInterference(const Value *Def,
154 const ValueSet *LVSet,
155 bool isCallInst) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000156
Chris Lattner296b7732002-02-05 02:52:05 +0000157 ValueSet::const_iterator LIt = LVSet->begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000158
159 // get the live range of instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000160 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000161 const LiveRange *const LROfDef = LRI.getLiveRangeForValue( Def );
162
163 IGNode *const IGNodeOfDef = LROfDef->getUserIGNode();
164 assert( IGNodeOfDef );
165
166 RegClass *const RCOfDef = LROfDef->getRegClass();
167
168 // for each live var in live variable set
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000169 //
Chris Lattner7e708292002-06-25 16:13:24 +0000170 for ( ; LIt != LVSet->end(); ++LIt) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000171
Vikram S. Advef5af6362002-07-08 23:15:32 +0000172 if (DEBUG_RA >= RA_DEBUG_Verbose)
Chris Lattner0665a5f2002-02-05 01:43:49 +0000173 cerr << "< Def=" << RAV(Def) << ", Lvar=" << RAV(*LIt) << "> ";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000174
175 // get the live range corresponding to live var
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000176 //
Chris Lattner0665a5f2002-02-05 01:43:49 +0000177 LiveRange *LROfVar = LRI.getLiveRangeForValue(*LIt);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000178
179 // LROfVar can be null if it is a const since a const
180 // doesn't have a dominating def - see Assumptions above
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000181 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000182 if (LROfVar) {
Chris Lattner7e708292002-06-25 16:13:24 +0000183 if (LROfDef == LROfVar) // do not set interf for same LR
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000184 continue;
185
186 // if 2 reg classes are the same set interference
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000187 //
Chris Lattner0665a5f2002-02-05 01:43:49 +0000188 if (RCOfDef == LROfVar->getRegClass()) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000189 RCOfDef->setInterference( LROfDef, LROfVar);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000190 } else if (DEBUG_RA >= RA_DEBUG_Verbose) {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000191 // we will not have LRs for values not explicitly allocated in the
192 // instruction stream (e.g., constants)
Chris Lattner0665a5f2002-02-05 01:43:49 +0000193 cerr << " warning: no live range for " << RAV(*LIt) << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000194 }
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000195 }
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000196 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000197}
198
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000199
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000200
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000201//----------------------------------------------------------------------------
202// For a call instruction, this method sets the CallInterference flag in
203// the LR of each variable live int the Live Variable Set live after the
204// call instruction (except the return value of the call instruction - since
205// the return value does not interfere with that call itself).
206//----------------------------------------------------------------------------
207
208void PhyRegAlloc::setCallInterferences(const MachineInstr *MInst,
Chris Lattner296b7732002-02-05 02:52:05 +0000209 const ValueSet *LVSetAft) {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000210
Chris Lattner7e708292002-06-25 16:13:24 +0000211 if (DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000212 cerr << "\n For call inst: " << *MInst;
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000213
Chris Lattner296b7732002-02-05 02:52:05 +0000214 ValueSet::const_iterator LIt = LVSetAft->begin();
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000215
216 // for each live var in live variable set after machine inst
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000217 //
Chris Lattner7e708292002-06-25 16:13:24 +0000218 for ( ; LIt != LVSetAft->end(); ++LIt) {
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000219
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000220 // get the live range corresponding to live var
221 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000222 LiveRange *const LR = LRI.getLiveRangeForValue(*LIt );
223
Chris Lattner7e708292002-06-25 16:13:24 +0000224 if (LR && DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000225 cerr << "\n\tLR Aft Call: ";
Chris Lattner296b7732002-02-05 02:52:05 +0000226 printSet(*LR);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000227 }
228
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000229 // LR can be null if it is a const since a const
230 // doesn't have a dominating def - see Assumptions above
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000231 //
Chris Lattner7e708292002-06-25 16:13:24 +0000232 if (LR ) {
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000233 LR->setCallInterference();
Chris Lattner7e708292002-06-25 16:13:24 +0000234 if (DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000235 cerr << "\n ++Added call interf for LR: " ;
Chris Lattner296b7732002-02-05 02:52:05 +0000236 printSet(*LR);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000237 }
238 }
239
240 }
241
Vikram S. Adve1a53f032002-03-31 18:54:37 +0000242 // Now find the LR of the return value of the call
243 // We do this because, we look at the LV set *after* the instruction
244 // to determine, which LRs must be saved across calls. The return value
245 // of the call is live in this set - but it does not interfere with call
246 // (i.e., we can allocate a volatile register to the return value)
247 //
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000248 CallArgsDescriptor* argDesc = CallArgsDescriptor::get(MInst);
249
250 if (const Value *RetVal = argDesc->getReturnValue()) {
Vikram S. Adve1a53f032002-03-31 18:54:37 +0000251 LiveRange *RetValLR = LRI.getLiveRangeForValue( RetVal );
252 assert( RetValLR && "No LR for RetValue of call");
253 RetValLR->clearCallInterference();
254 }
255
256 // If the CALL is an indirect call, find the LR of the function pointer.
257 // That has a call interference because it conflicts with outgoing args.
Chris Lattner7e708292002-06-25 16:13:24 +0000258 if (const Value *AddrVal = argDesc->getIndirectFuncPtr()) {
Vikram S. Adve1a53f032002-03-31 18:54:37 +0000259 LiveRange *AddrValLR = LRI.getLiveRangeForValue( AddrVal );
260 assert( AddrValLR && "No LR for indirect addr val of call");
261 AddrValLR->setCallInterference();
262 }
263
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000264}
265
266
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000267
268
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000269//----------------------------------------------------------------------------
270// This method will walk thru code and create interferences in the IG of
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000271// each RegClass. Also, this method calculates the spill cost of each
272// Live Range (it is done in this method to save another pass over the code).
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000273//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000274void PhyRegAlloc::buildInterferenceGraphs()
275{
276
Chris Lattner7e708292002-06-25 16:13:24 +0000277 if (DEBUG_RA) cerr << "Creating interference graphs ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000278
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000279 unsigned BBLoopDepthCost;
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000280 for (Function::const_iterator BBI = Meth->begin(), BBE = Meth->end();
281 BBI != BBE; ++BBI) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000282
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000283 // find the 10^(loop_depth) of this BB
284 //
Chris Lattner7e708292002-06-25 16:13:24 +0000285 BBLoopDepthCost = (unsigned)pow(10.0, LoopDepthCalc->getLoopDepth(BBI));
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000286
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000287 // get the iterator for machine instructions
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000288 //
Vikram S. Advef5af6362002-07-08 23:15:32 +0000289 const MachineCodeForBasicBlock& MIVec = MachineCodeForBasicBlock::get(BBI);
Vikram S. Adve48762092002-04-25 04:34:15 +0000290 MachineCodeForBasicBlock::const_iterator MII = MIVec.begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000291
292 // iterate over all the machine instructions in BB
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000293 //
Chris Lattner7e708292002-06-25 16:13:24 +0000294 for ( ; MII != MIVec.end(); ++MII) {
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000295
Vikram S. Adve48762092002-04-25 04:34:15 +0000296 const MachineInstr *MInst = *MII;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000297
298 // get the LV set after the instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000299 //
Chris Lattner7e708292002-06-25 16:13:24 +0000300 const ValueSet &LVSetAI = LVI->getLiveVarSetAfterMInst(MInst, BBI);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000301
302 const bool isCallInst = TM.getInstrInfo().isCall(MInst->getOpCode());
303
Chris Lattner7e708292002-06-25 16:13:24 +0000304 if (isCallInst ) {
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000305 // set the isCallInterference flag of each live range wich extends
306 // accross this call instruction. This information is used by graph
307 // coloring algo to avoid allocating volatile colors to live ranges
308 // that span across calls (since they have to be saved/restored)
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000309 //
Chris Lattner748697d2002-02-05 04:20:12 +0000310 setCallInterferences(MInst, &LVSetAI);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000311 }
312
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000313
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000314 // iterate over all MI operands to find defs
315 //
Chris Lattner2f898d22002-02-05 06:02:59 +0000316 for (MachineInstr::const_val_op_iterator OpI = MInst->begin(),
317 OpE = MInst->end(); OpI != OpE; ++OpI) {
318 if (OpI.isDef()) // create a new LR iff this operand is a def
Chris Lattner748697d2002-02-05 04:20:12 +0000319 addInterference(*OpI, &LVSetAI, isCallInst);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000320
321 // Calculate the spill cost of each live range
322 //
Chris Lattner2f898d22002-02-05 06:02:59 +0000323 LiveRange *LR = LRI.getLiveRangeForValue(*OpI);
324 if (LR) LR->addSpillCost(BBLoopDepthCost);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000325 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000326
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000327
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000328 // if there are multiple defs in this instruction e.g. in SETX
329 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000330 if (TM.getInstrInfo().isPseudoInstr(MInst->getOpCode()))
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000331 addInterf4PseudoInstr(MInst);
332
333
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000334 // Also add interference for any implicit definitions in a machine
335 // instr (currently, only calls have this).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000336 //
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000337 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
Chris Lattner7e708292002-06-25 16:13:24 +0000338 if ( NumOfImpRefs > 0 ) {
339 for (unsigned z=0; z < NumOfImpRefs; z++)
340 if (MInst->implicitRefIsDefined(z) )
Chris Lattner748697d2002-02-05 04:20:12 +0000341 addInterference( MInst->getImplicitRef(z), &LVSetAI, isCallInst );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000342 }
343
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +0000344
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000345 } // for all machine instructions in BB
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000346 } // for all BBs in function
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000347
348
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000349 // add interferences for function arguments. Since there are no explict
350 // defs in the function for args, we have to add them manually
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000351 //
352 addInterferencesForArgs();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000353
Chris Lattner7e708292002-06-25 16:13:24 +0000354 if (DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000355 cerr << "Interference graphs calculted!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000356
357}
358
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000359
360
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000361//--------------------------------------------------------------------------
362// Pseudo instructions will be exapnded to multiple instructions by the
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000363// assembler. Consequently, all the opernds must get distinct registers.
364// Therefore, we mark all operands of a pseudo instruction as they interfere
365// with one another.
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000366//--------------------------------------------------------------------------
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000367void PhyRegAlloc::addInterf4PseudoInstr(const MachineInstr *MInst) {
368
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000369 bool setInterf = false;
370
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000371 // iterate over MI operands to find defs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000372 //
Chris Lattner2f898d22002-02-05 06:02:59 +0000373 for (MachineInstr::const_val_op_iterator It1 = MInst->begin(),
374 ItE = MInst->end(); It1 != ItE; ++It1) {
375 const LiveRange *LROfOp1 = LRI.getLiveRangeForValue(*It1);
376 assert((LROfOp1 || !It1.isDef()) && "No LR for Def in PSEUDO insruction");
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000377
Chris Lattner2f898d22002-02-05 06:02:59 +0000378 MachineInstr::const_val_op_iterator It2 = It1;
Chris Lattner7e708292002-06-25 16:13:24 +0000379 for (++It2; It2 != ItE; ++It2) {
Chris Lattner2f898d22002-02-05 06:02:59 +0000380 const LiveRange *LROfOp2 = LRI.getLiveRangeForValue(*It2);
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000381
Chris Lattner2f898d22002-02-05 06:02:59 +0000382 if (LROfOp2) {
383 RegClass *RCOfOp1 = LROfOp1->getRegClass();
384 RegClass *RCOfOp2 = LROfOp2->getRegClass();
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000385
Chris Lattner7e708292002-06-25 16:13:24 +0000386 if (RCOfOp1 == RCOfOp2 ){
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000387 RCOfOp1->setInterference( LROfOp1, LROfOp2 );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000388 setInterf = true;
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000389 }
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000390 } // if Op2 has a LR
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000391 } // for all other defs in machine instr
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000392 } // for all operands in an instruction
393
Chris Lattner2f898d22002-02-05 06:02:59 +0000394 if (!setInterf && MInst->getNumOperands() > 2) {
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000395 cerr << "\nInterf not set for any operand in pseudo instr:\n";
396 cerr << *MInst;
397 assert(0 && "Interf not set for pseudo instr with > 2 operands" );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000398 }
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000399}
400
401
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000402
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000403//----------------------------------------------------------------------------
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000404// This method will add interferences for incoming arguments to a function.
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000405//----------------------------------------------------------------------------
Chris Lattner296b7732002-02-05 02:52:05 +0000406void PhyRegAlloc::addInterferencesForArgs() {
407 // get the InSet of root BB
Chris Lattner7e708292002-06-25 16:13:24 +0000408 const ValueSet &InSet = LVI->getInSetOfBB(&Meth->front());
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000409
Chris Lattner7e708292002-06-25 16:13:24 +0000410 for (Function::const_aiterator AI = Meth->abegin(); AI != Meth->aend(); ++AI) {
411 // add interferences between args and LVars at start
412 addInterference(AI, &InSet, false);
413
Vikram S. Advef5af6362002-07-08 23:15:32 +0000414 if (DEBUG_RA >= RA_DEBUG_Verbose)
Chris Lattner7e708292002-06-25 16:13:24 +0000415 cerr << " - %% adding interference for argument " << RAV(AI) << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000416 }
417}
418
419
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000420//----------------------------------------------------------------------------
421// This method is called after register allocation is complete to set the
422// allocated reisters in the machine code. This code will add register numbers
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000423// to MachineOperands that contain a Value. Also it calls target specific
424// methods to produce caller saving instructions. At the end, it adds all
425// additional instructions produced by the register allocator to the
426// instruction stream.
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000427//----------------------------------------------------------------------------
Vikram S. Adve48762092002-04-25 04:34:15 +0000428
429//-----------------------------
430// Utility functions used below
431//-----------------------------
432inline void
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000433PrependInstructions(vector<MachineInstr *> &IBef,
Vikram S. Adve48762092002-04-25 04:34:15 +0000434 MachineCodeForBasicBlock& MIVec,
435 MachineCodeForBasicBlock::iterator& MII,
436 const std::string& msg)
437{
438 if (!IBef.empty())
439 {
440 MachineInstr* OrigMI = *MII;
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000441 std::vector<MachineInstr *>::iterator AdIt;
Vikram S. Adve48762092002-04-25 04:34:15 +0000442 for (AdIt = IBef.begin(); AdIt != IBef.end() ; ++AdIt)
443 {
444 if (DEBUG_RA) {
445 if (OrigMI) cerr << "For MInst: " << *OrigMI;
446 cerr << msg << " PREPENDed instr: " << **AdIt << "\n";
447 }
448 MII = MIVec.insert(MII, *AdIt);
449 ++MII;
450 }
451 }
452}
453
454inline void
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000455AppendInstructions(std::vector<MachineInstr *> &IAft,
Vikram S. Adve48762092002-04-25 04:34:15 +0000456 MachineCodeForBasicBlock& MIVec,
457 MachineCodeForBasicBlock::iterator& MII,
458 const std::string& msg)
459{
460 if (!IAft.empty())
461 {
462 MachineInstr* OrigMI = *MII;
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000463 std::vector<MachineInstr *>::iterator AdIt;
Chris Lattner7e708292002-06-25 16:13:24 +0000464 for ( AdIt = IAft.begin(); AdIt != IAft.end() ; ++AdIt )
Vikram S. Adve48762092002-04-25 04:34:15 +0000465 {
Chris Lattner7e708292002-06-25 16:13:24 +0000466 if (DEBUG_RA) {
Vikram S. Adve48762092002-04-25 04:34:15 +0000467 if (OrigMI) cerr << "For MInst: " << *OrigMI;
468 cerr << msg << " APPENDed instr: " << **AdIt << "\n";
469 }
470 ++MII; // insert before the next instruction
471 MII = MIVec.insert(MII, *AdIt);
472 }
473 }
474}
475
476
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000477void PhyRegAlloc::updateMachineCode()
478{
Vikram S. Advef5af6362002-07-08 23:15:32 +0000479 MachineCodeForBasicBlock& MIVec = MachineCodeForBasicBlock::get(&Meth->getEntryNode());
Vikram S. Adve48762092002-04-25 04:34:15 +0000480
Chris Lattner7e708292002-06-25 16:13:24 +0000481 // Insert any instructions needed at method entry
482 MachineCodeForBasicBlock::iterator MII = MIVec.begin();
483 PrependInstructions(AddedInstrAtEntry.InstrnsBefore, MIVec, MII,
484 "At function entry: \n");
485 assert(AddedInstrAtEntry.InstrnsAfter.empty() &&
486 "InstrsAfter should be unnecessary since we are just inserting at "
487 "the function entry point here.");
Vikram S. Adve48762092002-04-25 04:34:15 +0000488
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000489 for (Function::const_iterator BBI = Meth->begin(), BBE = Meth->end();
490 BBI != BBE; ++BBI) {
Vikram S. Adve48762092002-04-25 04:34:15 +0000491
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000492 // iterate over all the machine instructions in BB
Vikram S. Advef5af6362002-07-08 23:15:32 +0000493 MachineCodeForBasicBlock &MIVec = MachineCodeForBasicBlock::get(BBI);
Chris Lattner7e708292002-06-25 16:13:24 +0000494 for (MachineCodeForBasicBlock::iterator MII = MIVec.begin();
Vikram S. Adve48762092002-04-25 04:34:15 +0000495 MII != MIVec.end(); ++MII) {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000496
Vikram S. Adve48762092002-04-25 04:34:15 +0000497 MachineInstr *MInst = *MII;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000498
499 unsigned Opcode = MInst->getOpCode();
500
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000501 // do not process Phis
Vikram S. Adve23a4c8f2002-03-18 03:37:19 +0000502 if (TM.getInstrInfo().isDummyPhiInstr(Opcode))
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000503 continue;
504
Vikram S. Advef5af6362002-07-08 23:15:32 +0000505 // Reset tmp stack positions so they can be reused for each machine instr.
506 mcInfo.popAllTempValues(TM);
507
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000508 // Now insert speical instructions (if necessary) for call/return
509 // instructions.
510 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000511 if (TM.getInstrInfo().isCall(Opcode) ||
512 TM.getInstrInfo().isReturn(Opcode)) {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000513
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000514 AddedInstrns &AI = AddedInstrMap[MInst];
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000515
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000516 if (TM.getInstrInfo().isCall(Opcode))
Chris Lattner7e708292002-06-25 16:13:24 +0000517 MRI.colorCallArgs(MInst, LRI, &AI, *this, BBI);
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000518 else if (TM.getInstrInfo().isReturn(Opcode))
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000519 MRI.colorRetValue(MInst, LRI, &AI);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000520 }
521
Vikram S. Advef5af6362002-07-08 23:15:32 +0000522 // Set the registers for operands in the machine instruction
523 // if a register was successfully allocated. If not, insert
524 // code to spill the register value.
525 //
526 for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum)
527 {
528 MachineOperand& Op = MInst->getOperand(OpNum);
529 if (Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
530 Op.getOperandType() == MachineOperand::MO_CCRegister)
531 {
532 const Value *const Val = Op.getVRegValue();
533
534 LiveRange *const LR = LRI.getLiveRangeForValue(Val);
535 if (!LR) // consts or labels will have no live range
536 {
537 // if register is not allocated, mark register as invalid
538 if (Op.getAllocatedRegNum() == -1)
539 MInst->SetRegForOperand(OpNum, MRI.getInvalidRegNum());
540 continue;
541 }
542
543 if (LR->hasColor() )
544 MInst->SetRegForOperand(OpNum,
545 MRI.getUnifiedRegNum(LR->getRegClass()->getID(),
546 LR->getColor()));
547 else
548 // LR did NOT receive a color (register). Insert spill code.
549 insertCode4SpilledLR(LR, MInst, BBI, OpNum );
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000550 }
Vikram S. Advef5af6362002-07-08 23:15:32 +0000551 } // for each operand
552
553
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000554 // Now add instructions that the register allocator inserts before/after
555 // this machine instructions (done only for calls/rets/incoming args)
556 // We do this here, to ensure that spill for an instruction is inserted
557 // closest as possible to an instruction (see above insertCode4Spill...)
558 //
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000559 // If there are instructions to be added, *before* this machine
560 // instruction, add them now.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000561 //
Chris Lattner7e708292002-06-25 16:13:24 +0000562 if (AddedInstrMap.count(MInst)) {
Vikram S. Adve48762092002-04-25 04:34:15 +0000563 PrependInstructions(AddedInstrMap[MInst].InstrnsBefore, MIVec, MII,"");
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000564 }
Vikram S. Adve48762092002-04-25 04:34:15 +0000565
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000566 // If there are instructions to be added *after* this machine
567 // instruction, add them now
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000568 //
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000569 if (!AddedInstrMap[MInst].InstrnsAfter.empty()) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000570
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000571 // if there are delay slots for this instruction, the instructions
572 // added after it must really go after the delayed instruction(s)
573 // So, we move the InstrAfter of the current instruction to the
574 // corresponding delayed instruction
575
576 unsigned delay;
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000577 if ((delay=TM.getInstrInfo().getNumDelaySlots(MInst->getOpCode())) >0){
Vikram S. Adve48762092002-04-25 04:34:15 +0000578 move2DelayedInstr(MInst, *(MII+delay) );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000579 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000580 else {
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000581 // Here we can add the "instructions after" to the current
582 // instruction since there are no delay slots for this instruction
Vikram S. Adve48762092002-04-25 04:34:15 +0000583 AppendInstructions(AddedInstrMap[MInst].InstrnsAfter, MIVec, MII,"");
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000584 } // if not delay
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000585 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000586
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000587 } // for each machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000588 }
589}
590
591
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000592
593//----------------------------------------------------------------------------
594// This method inserts spill code for AN operand whose LR was spilled.
595// This method may be called several times for a single machine instruction
596// if it contains many spilled operands. Each time it is called, it finds
597// a register which is not live at that instruction and also which is not
598// used by other spilled operands of the same instruction. Then it uses
599// this register temporarily to accomodate the spilled value.
600//----------------------------------------------------------------------------
601void PhyRegAlloc::insertCode4SpilledLR(const LiveRange *LR,
602 MachineInstr *MInst,
603 const BasicBlock *BB,
604 const unsigned OpNum) {
605
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000606 assert(! TM.getInstrInfo().isCall(MInst->getOpCode()) &&
607 (! TM.getInstrInfo().isReturn(MInst->getOpCode())) &&
608 "Arg of a call/ret must be handled elsewhere");
609
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000610 MachineOperand& Op = MInst->getOperand(OpNum);
611 bool isDef = MInst->operandIsDefined(OpNum);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000612 bool isDefAndUse = MInst->operandIsDefinedAndUsed(OpNum);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000613 unsigned RegType = MRI.getRegType( LR );
614 int SpillOff = LR->getSpillOffFromFP();
615 RegClass *RC = LR->getRegClass();
Chris Lattner748697d2002-02-05 04:20:12 +0000616 const ValueSet &LVSetBef = LVI->getLiveVarSetBeforeMInst(MInst, BB);
Vikram S. Adve00521d72001-11-12 23:26:35 +0000617
Chris Lattner697954c2002-01-20 22:54:45 +0000618 mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000619
Vikram S. Advef5af6362002-07-08 23:15:32 +0000620 vector<MachineInstr*> MIBef, MIAft;
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000621 vector<MachineInstr*> AdIMid;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000622
Vikram S. Advef5af6362002-07-08 23:15:32 +0000623 // Choose a register to hold the spilled value. This may insert code
624 // before and after MInst to free up the value. If so, this code should
625 // be first and last in the spill sequence before/after MInst.
626 int TmpRegU = getUsableUniRegAtMI(RegType, &LVSetBef, MInst, MIBef, MIAft);
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000627
Vikram S. Advef5af6362002-07-08 23:15:32 +0000628 // Set the operand first so that it this register does not get used
629 // as a scratch register for later calls to getUsableUniRegAtMI below
630 MInst->SetRegForOperand(OpNum, TmpRegU);
631
632 // get the added instructions for this instruction
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000633 AddedInstrns &AI = AddedInstrMap[MInst];
Vikram S. Advef5af6362002-07-08 23:15:32 +0000634
635 // We may need a scratch register to copy the spilled value to/from memory.
636 // This may itself have to insert code to free up a scratch register.
637 // Any such code should go before (after) the spill code for a load (store).
638 int scratchRegType = -1;
639 int scratchReg = -1;
640 if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType))
641 {
642 scratchReg = this->getUsableUniRegAtMI(scratchRegType, &LVSetBef,
643 MInst, MIBef, MIAft);
644 assert(scratchReg != MRI.getInvalidRegNum());
645 MInst->getRegsUsed().insert(scratchReg);
646 }
647
648 if (!isDef || isDefAndUse) {
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000649 // for a USE, we have to load the value of LR from stack to a TmpReg
650 // and use the TmpReg as one operand of instruction
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000651
Vikram S. Advef5af6362002-07-08 23:15:32 +0000652 // actual loading instruction(s)
653 MRI.cpMem2RegMI(AdIMid, MRI.getFramePointer(), SpillOff, TmpRegU, RegType,
654 scratchReg);
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000655
Vikram S. Advef5af6362002-07-08 23:15:32 +0000656 // the actual load should be after the instructions to free up TmpRegU
657 MIBef.insert(MIBef.end(), AdIMid.begin(), AdIMid.end());
658 AdIMid.clear();
659 }
660
661 if (isDef) { // if this is a Def
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000662 // for a DEF, we have to store the value produced by this instruction
663 // on the stack position allocated for this LR
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000664
Vikram S. Advef5af6362002-07-08 23:15:32 +0000665 // actual storing instruction(s)
666 MRI.cpReg2MemMI(AdIMid, TmpRegU, MRI.getFramePointer(), SpillOff, RegType,
667 scratchReg);
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000668
Vikram S. Advef5af6362002-07-08 23:15:32 +0000669 MIAft.insert(MIAft.begin(), AdIMid.begin(), AdIMid.end());
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000670 } // if !DEF
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000671
Vikram S. Advef5af6362002-07-08 23:15:32 +0000672 // Finally, insert the entire spill code sequences before/after MInst
673 AI.InstrnsBefore.insert(AI.InstrnsBefore.end(), MIBef.begin(), MIBef.end());
674 AI.InstrnsAfter.insert(AI.InstrnsAfter.begin(), MIAft.begin(), MIAft.end());
675
Chris Lattner7e708292002-06-25 16:13:24 +0000676 if (DEBUG_RA) {
677 cerr << "\nFor Inst " << *MInst;
678 cerr << " - SPILLED LR: "; printSet(*LR);
679 cerr << "\n - Added Instructions:";
Anand Shuklad58290e2002-07-09 19:18:56 +0000680 for_each(MIBef.begin(), MIBef.end(), std::mem_fun(&MachineInstr::dump));
681 for_each(MIAft.begin(), MIAft.end(), std::mem_fun(&MachineInstr::dump));
Chris Lattner7e708292002-06-25 16:13:24 +0000682 }
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000683}
684
685
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000686//----------------------------------------------------------------------------
687// We can use the following method to get a temporary register to be used
688// BEFORE any given machine instruction. If there is a register available,
689// this method will simply return that register and set MIBef = MIAft = NULL.
690// Otherwise, it will return a register and MIAft and MIBef will contain
691// two instructions used to free up this returned register.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000692// Returned register number is the UNIFIED register number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000693//----------------------------------------------------------------------------
694
Vikram S. Advef5af6362002-07-08 23:15:32 +0000695int PhyRegAlloc::getUsableUniRegAtMI(const int RegType,
696 const ValueSet *LVSetBef,
697 MachineInstr *MInst,
698 std::vector<MachineInstr*>& MIBef,
699 std::vector<MachineInstr*>& MIAft) {
700
701 RegClass* RC = this->getRegClassByID(MRI.getRegClassIDOfRegType(RegType));
702
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000703 int RegU = getUnusedUniRegAtMI(RC, MInst, LVSetBef);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000704
705 if (RegU == -1) {
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000706 // we couldn't find an unused register. Generate code to free up a reg by
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000707 // saving it on stack and restoring after the instruction
Vikram S. Advef5af6362002-07-08 23:15:32 +0000708
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000709 int TmpOff = mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
Vikram S. Adve12af1642001-11-08 04:48:50 +0000710
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000711 RegU = getUniRegNotUsedByThisInst(RC, MInst);
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000712
Vikram S. Advef5af6362002-07-08 23:15:32 +0000713 // Check if we need a scratch register to copy this register to memory.
714 int scratchRegType = -1;
715 if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType))
716 {
717 int scratchReg = this->getUsableUniRegAtMI(scratchRegType, LVSetBef,
718 MInst, MIBef, MIAft);
719 assert(scratchReg != MRI.getInvalidRegNum());
720
721 // We may as well hold the value in the scratch register instead
722 // of copying it to memory and back. But we have to mark the
723 // register as used by this instruction, so it does not get used
724 // as a scratch reg. by another operand or anyone else.
725 MInst->getRegsUsed().insert(scratchReg);
726 MRI.cpReg2RegMI(MIBef, RegU, scratchReg, RegType);
727 MRI.cpReg2RegMI(MIAft, scratchReg, RegU, RegType);
728 }
729 else
730 { // the register can be copied directly to/from memory so do it.
731 MRI.cpReg2MemMI(MIBef, RegU, MRI.getFramePointer(), TmpOff, RegType);
732 MRI.cpMem2RegMI(MIAft, MRI.getFramePointer(), TmpOff, RegU, RegType);
733 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000734 }
Vikram S. Advef5af6362002-07-08 23:15:32 +0000735
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000736 return RegU;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000737}
738
739//----------------------------------------------------------------------------
740// This method is called to get a new unused register that can be used to
741// accomodate a spilled value.
742// This method may be called several times for a single machine instruction
743// if it contains many spilled operands. Each time it is called, it finds
744// a register which is not live at that instruction and also which is not
745// used by other spilled operands of the same instruction.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000746// Return register number is relative to the register class. NOT
747// unified number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000748//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000749int PhyRegAlloc::getUnusedUniRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000750 const MachineInstr *MInst,
Chris Lattner296b7732002-02-05 02:52:05 +0000751 const ValueSet *LVSetBef) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000752
753 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
754
Chris Lattner85c54652002-05-23 15:50:03 +0000755 std::vector<bool> &IsColorUsedArr = RC->getIsColorUsedArr();
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000756
Chris Lattner7e708292002-06-25 16:13:24 +0000757 for (unsigned i=0; i < NumAvailRegs; i++) // Reset array
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000758 IsColorUsedArr[i] = false;
759
Chris Lattner296b7732002-02-05 02:52:05 +0000760 ValueSet::const_iterator LIt = LVSetBef->begin();
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000761
762 // for each live var in live variable set after machine inst
Chris Lattner7e708292002-06-25 16:13:24 +0000763 for ( ; LIt != LVSetBef->end(); ++LIt) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000764
765 // get the live range corresponding to live var
766 LiveRange *const LRofLV = LRI.getLiveRangeForValue(*LIt );
767
768 // LR can be null if it is a const since a const
769 // doesn't have a dominating def - see Assumptions above
Chris Lattner7e708292002-06-25 16:13:24 +0000770 if (LRofLV && LRofLV->getRegClass() == RC && LRofLV->hasColor() )
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000771 IsColorUsedArr[ LRofLV->getColor() ] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000772 }
773
774 // It is possible that one operand of this MInst was already spilled
775 // and it received some register temporarily. If that's the case,
776 // it is recorded in machine operand. We must skip such registers.
777
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000778 setRelRegsUsedByThisInst(RC, MInst);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000779
Chris Lattner7e708292002-06-25 16:13:24 +0000780 for (unsigned c=0; c < NumAvailRegs; c++) // find first unused color
Chris Lattner85c54652002-05-23 15:50:03 +0000781 if (!IsColorUsedArr[c])
782 return MRI.getUnifiedRegNum(RC->getID(), c);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000783
Chris Lattner85c54652002-05-23 15:50:03 +0000784 return -1;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000785}
786
787
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000788//----------------------------------------------------------------------------
789// Get any other register in a register class, other than what is used
790// by operands of a machine instruction. Returns the unified reg number.
791//----------------------------------------------------------------------------
792int PhyRegAlloc::getUniRegNotUsedByThisInst(RegClass *RC,
Chris Lattner85c54652002-05-23 15:50:03 +0000793 const MachineInstr *MInst) {
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000794
Chris Lattner85c54652002-05-23 15:50:03 +0000795 vector<bool> &IsColorUsedArr = RC->getIsColorUsedArr();
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000796 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
797
Chris Lattner7e708292002-06-25 16:13:24 +0000798 for (unsigned i=0; i < NumAvailRegs ; i++) // Reset array
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000799 IsColorUsedArr[i] = false;
800
801 setRelRegsUsedByThisInst(RC, MInst);
802
Chris Lattner7e708292002-06-25 16:13:24 +0000803 for (unsigned c=0; c < RC->getNumOfAvailRegs(); c++)// find first unused color
Chris Lattner85c54652002-05-23 15:50:03 +0000804 if (!IsColorUsedArr[c])
805 return MRI.getUnifiedRegNum(RC->getID(), c);
806
807 assert(0 && "FATAL: No free register could be found in reg class!!");
Chris Lattner697954c2002-01-20 22:54:45 +0000808 return 0;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000809}
810
811
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000812//----------------------------------------------------------------------------
813// This method modifies the IsColorUsedArr of the register class passed to it.
814// It sets the bits corresponding to the registers used by this machine
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000815// instructions. Both explicit and implicit operands are set.
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000816//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000817void PhyRegAlloc::setRelRegsUsedByThisInst(RegClass *RC,
Vikram S. Advef5af6362002-07-08 23:15:32 +0000818 const MachineInstr *MInst ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000819
Vikram S. Advef5af6362002-07-08 23:15:32 +0000820 vector<bool> &IsColorUsedArr = RC->getIsColorUsedArr();
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000821
Vikram S. Advef5af6362002-07-08 23:15:32 +0000822 // Add the registers already marked as used by the instruction.
823 // This should include any scratch registers that are used to save
824 // values across the instruction (e.g., for saving state register values).
825 const hash_set<int>& regsUsed = MInst->getRegsUsed();
826 for (hash_set<int>::const_iterator SI=regsUsed.begin(), SE=regsUsed.end();
827 SI != SE; ++SI)
828 {
829 unsigned classId = 0;
830 int classRegNum = MRI.getClassRegNum(*SI, classId);
831 if (RC->getID() == classId)
832 {
833 assert(classRegNum < (int) IsColorUsedArr.size() &&
834 "Illegal register number for this reg class?");
835 IsColorUsedArr[classRegNum] = true;
836 }
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000837 }
Vikram S. Advef5af6362002-07-08 23:15:32 +0000838
839 // Now add registers allocated to the live ranges of values used in
840 // the instruction. These are not yet recorded in the instruction.
841 for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum)
842 {
843 const MachineOperand& Op = MInst->getOperand(OpNum);
844
845 if (Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
846 Op.getOperandType() == MachineOperand::MO_CCRegister)
847 if (const Value* Val = Op.getVRegValue())
848 if (MRI.getRegClassIDOfValue(Val) == RC->getID())
849 if (Op.getAllocatedRegNum() == -1)
850 if (LiveRange *LROfVal = LRI.getLiveRangeForValue(Val))
851 if (LROfVal->hasColor() )
852 // this operand is in a LR that received a color
853 IsColorUsedArr[LROfVal->getColor()] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000854 }
Vikram S. Advef5af6362002-07-08 23:15:32 +0000855
856 // If there are implicit references, mark their allocated regs as well
857 //
858 for (unsigned z=0; z < MInst->getNumImplicitRefs(); z++)
859 if (const LiveRange*
860 LRofImpRef = LRI.getLiveRangeForValue(MInst->getImplicitRef(z)))
861 if (LRofImpRef->hasColor())
862 // this implicit reference is in a LR that received a color
863 IsColorUsedArr[LRofImpRef->getColor()] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000864}
865
866
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000867//----------------------------------------------------------------------------
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000868// If there are delay slots for an instruction, the instructions
869// added after it must really go after the delayed instruction(s).
870// So, we move the InstrAfter of that instruction to the
871// corresponding delayed instruction using the following method.
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000872
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000873//----------------------------------------------------------------------------
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000874void PhyRegAlloc::move2DelayedInstr(const MachineInstr *OrigMI,
875 const MachineInstr *DelayedMI) {
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000876
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000877 // "added after" instructions of the original instr
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000878 std::vector<MachineInstr *> &OrigAft = AddedInstrMap[OrigMI].InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000879
880 // "added instructions" of the delayed instr
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000881 AddedInstrns &DelayAdI = AddedInstrMap[DelayedMI];
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000882
883 // "added after" instructions of the delayed instr
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000884 std::vector<MachineInstr *> &DelayedAft = DelayAdI.InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000885
886 // go thru all the "added after instructions" of the original instruction
887 // and append them to the "addded after instructions" of the delayed
888 // instructions
Chris Lattner697954c2002-01-20 22:54:45 +0000889 DelayedAft.insert(DelayedAft.end(), OrigAft.begin(), OrigAft.end());
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000890
891 // empty the "added after instructions" of the original instruction
892 OrigAft.clear();
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000893}
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000894
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000895//----------------------------------------------------------------------------
896// This method prints the code with registers after register allocation is
897// complete.
898//----------------------------------------------------------------------------
899void PhyRegAlloc::printMachineCode()
900{
901
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000902 cerr << "\n;************** Function " << Meth->getName()
Chris Lattner697954c2002-01-20 22:54:45 +0000903 << " *****************\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000904
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000905 for (Function::const_iterator BBI = Meth->begin(), BBE = Meth->end();
906 BBI != BBE; ++BBI) {
Chris Lattner7e708292002-06-25 16:13:24 +0000907 cerr << "\n"; printLabel(BBI); cerr << ": ";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000908
909 // get the iterator for machine instructions
Vikram S. Advef5af6362002-07-08 23:15:32 +0000910 MachineCodeForBasicBlock& MIVec = MachineCodeForBasicBlock::get(BBI);
Vikram S. Adve48762092002-04-25 04:34:15 +0000911 MachineCodeForBasicBlock::iterator MII = MIVec.begin();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000912
913 // iterate over all the machine instructions in BB
Chris Lattner7e708292002-06-25 16:13:24 +0000914 for ( ; MII != MIVec.end(); ++MII) {
Vikram S. Adve48762092002-04-25 04:34:15 +0000915 MachineInstr *const MInst = *MII;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000916
Chris Lattner697954c2002-01-20 22:54:45 +0000917 cerr << "\n\t";
918 cerr << TargetInstrDescriptors[MInst->getOpCode()].opCodeString;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000919
Chris Lattner7e708292002-06-25 16:13:24 +0000920 for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000921 MachineOperand& Op = MInst->getOperand(OpNum);
922
Chris Lattner7e708292002-06-25 16:13:24 +0000923 if (Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasanka97b8b442001-10-18 22:36:26 +0000924 Op.getOperandType() == MachineOperand::MO_CCRegister /*||
925 Op.getOperandType() == MachineOperand::MO_PCRelativeDisp*/ ) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000926
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000927 const Value *const Val = Op.getVRegValue () ;
Ruchira Sasankae727f852001-09-18 22:43:57 +0000928 // ****this code is temporary till NULL Values are fixed
Chris Lattner7e708292002-06-25 16:13:24 +0000929 if (! Val ) {
Chris Lattner697954c2002-01-20 22:54:45 +0000930 cerr << "\t<*NULL*>";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000931 continue;
932 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000933
934 // if a label or a constant
Chris Lattner7e708292002-06-25 16:13:24 +0000935 if (isa<BasicBlock>(Val)) {
Chris Lattner697954c2002-01-20 22:54:45 +0000936 cerr << "\t"; printLabel( Op.getVRegValue () );
937 } else {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000938 // else it must be a register value
939 const int RegNum = Op.getAllocatedRegNum();
940
Chris Lattner697954c2002-01-20 22:54:45 +0000941 cerr << "\t" << "%" << MRI.getUnifiedRegName( RegNum );
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000942 if (Val->hasName() )
Chris Lattner697954c2002-01-20 22:54:45 +0000943 cerr << "(" << Val->getName() << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000944 else
Chris Lattner697954c2002-01-20 22:54:45 +0000945 cerr << "(" << Val << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000946
Chris Lattner7e708292002-06-25 16:13:24 +0000947 if (Op.opIsDef() )
Chris Lattner697954c2002-01-20 22:54:45 +0000948 cerr << "*";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000949
950 const LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
Chris Lattner7e708292002-06-25 16:13:24 +0000951 if (LROfVal )
952 if (LROfVal->hasSpillOffset() )
Chris Lattner697954c2002-01-20 22:54:45 +0000953 cerr << "$";
Ruchira Sasankae727f852001-09-18 22:43:57 +0000954 }
955
956 }
Chris Lattner7e708292002-06-25 16:13:24 +0000957 else if (Op.getOperandType() == MachineOperand::MO_MachineRegister) {
Chris Lattner697954c2002-01-20 22:54:45 +0000958 cerr << "\t" << "%" << MRI.getUnifiedRegName(Op.getMachineRegNum());
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000959 }
960
961 else
Chris Lattner697954c2002-01-20 22:54:45 +0000962 cerr << "\t" << Op; // use dump field
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000963 }
964
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000965
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000966
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000967 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
Chris Lattner7e708292002-06-25 16:13:24 +0000968 if (NumOfImpRefs > 0) {
Chris Lattner697954c2002-01-20 22:54:45 +0000969 cerr << "\tImplicit:";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000970
Chris Lattner7e708292002-06-25 16:13:24 +0000971 for (unsigned z=0; z < NumOfImpRefs; z++)
Chris Lattner0665a5f2002-02-05 01:43:49 +0000972 cerr << RAV(MInst->getImplicitRef(z)) << "\t";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000973 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000974
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000975 } // for all machine instructions
976
Chris Lattner697954c2002-01-20 22:54:45 +0000977 cerr << "\n";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000978
979 } // for all BBs
980
Chris Lattner697954c2002-01-20 22:54:45 +0000981 cerr << "\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000982}
983
Ruchira Sasankae727f852001-09-18 22:43:57 +0000984
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000985//----------------------------------------------------------------------------
986
987//----------------------------------------------------------------------------
988void PhyRegAlloc::colorIncomingArgs()
989{
Chris Lattner7e708292002-06-25 16:13:24 +0000990 const BasicBlock &FirstBB = Meth->front();
Vikram S. Advef5af6362002-07-08 23:15:32 +0000991 const MachineInstr *FirstMI = MachineCodeForBasicBlock::get(&FirstBB).front();
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000992 assert(FirstMI && "No machine instruction in entry BB");
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000993
Vikram S. Adve48762092002-04-25 04:34:15 +0000994 MRI.colorMethodArgs(Meth, LRI, &AddedInstrAtEntry);
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000995}
996
Ruchira Sasankae727f852001-09-18 22:43:57 +0000997
998//----------------------------------------------------------------------------
999// Used to generate a label for a basic block
1000//----------------------------------------------------------------------------
Chris Lattner697954c2002-01-20 22:54:45 +00001001void PhyRegAlloc::printLabel(const Value *const Val) {
1002 if (Val->hasName())
1003 cerr << Val->getName();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001004 else
Chris Lattner697954c2002-01-20 22:54:45 +00001005 cerr << "Label" << Val;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001006}
1007
1008
Ruchira Sasankae727f852001-09-18 22:43:57 +00001009//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001010// This method calls setSugColorUsable method of each live range. This
1011// will determine whether the suggested color of LR is really usable.
1012// A suggested color is not usable when the suggested color is volatile
1013// AND when there are call interferences
1014//----------------------------------------------------------------------------
1015
1016void PhyRegAlloc::markUnusableSugColors()
1017{
Chris Lattner7e708292002-06-25 16:13:24 +00001018 if (DEBUG_RA ) cerr << "\nmarking unusable suggested colors ...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001019
1020 // hash map iterator
1021 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
1022 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
1023
Chris Lattner7e708292002-06-25 16:13:24 +00001024 for (; HMI != HMIEnd ; ++HMI ) {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001025 if (HMI->first) {
1026 LiveRange *L = HMI->second; // get the LiveRange
1027 if (L) {
Chris Lattner7e708292002-06-25 16:13:24 +00001028 if (L->hasSuggestedColor()) {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001029 int RCID = L->getRegClass()->getID();
Chris Lattner7e708292002-06-25 16:13:24 +00001030 if (MRI.isRegVolatile( RCID, L->getSuggestedColor()) &&
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001031 L->isCallInterference() )
1032 L->setSuggestedColorUsable( false );
1033 else
1034 L->setSuggestedColorUsable( true );
1035 }
1036 } // if L->hasSuggestedColor()
1037 }
1038 } // for all LR's in hash map
1039}
1040
1041
1042
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001043//----------------------------------------------------------------------------
1044// The following method will set the stack offsets of the live ranges that
1045// are decided to be spillled. This must be called just after coloring the
1046// LRs using the graph coloring algo. For each live range that is spilled,
1047// this method allocate a new spill position on the stack.
1048//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001049
Chris Lattner37730942002-02-05 03:52:29 +00001050void PhyRegAlloc::allocateStackSpace4SpilledLRs() {
1051 if (DEBUG_RA) cerr << "\nsetting LR stack offsets ...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001052
Chris Lattner37730942002-02-05 03:52:29 +00001053 LiveRangeMapType::const_iterator HMI = LRI.getLiveRangeMap()->begin();
1054 LiveRangeMapType::const_iterator HMIEnd = LRI.getLiveRangeMap()->end();
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001055
Chris Lattner7e708292002-06-25 16:13:24 +00001056 for ( ; HMI != HMIEnd ; ++HMI) {
Chris Lattner37730942002-02-05 03:52:29 +00001057 if (HMI->first && HMI->second) {
1058 LiveRange *L = HMI->second; // get the LiveRange
1059 if (!L->hasColor()) // NOTE: ** allocating the size of long Type **
1060 L->setSpillOffFromFP(mcInfo.allocateSpilledValue(TM, Type::LongTy));
1061 }
1062 } // for all LR's in hash map
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001063}
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001064
1065
1066
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001067//----------------------------------------------------------------------------
Ruchira Sasankae727f852001-09-18 22:43:57 +00001068// The entry pont to Register Allocation
1069//----------------------------------------------------------------------------
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001070
1071void PhyRegAlloc::allocateRegisters()
1072{
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001073
1074 // make sure that we put all register classes into the RegClassList
1075 // before we call constructLiveRanges (now done in the constructor of
1076 // PhyRegAlloc class).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001077 //
1078 LRI.constructLiveRanges(); // create LR info
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001079
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001080 if (DEBUG_RA)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001081 LRI.printLiveRanges();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001082
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001083 createIGNodeListsAndIGs(); // create IGNode list and IGs
1084
1085 buildInterferenceGraphs(); // build IGs in all reg classes
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001086
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001087
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001088 if (DEBUG_RA) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001089 // print all LRs in all reg classes
Chris Lattner7e708292002-06-25 16:13:24 +00001090 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
1091 RegClassList[rc]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001092
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001093 // print IGs in all register classes
Chris Lattner7e708292002-06-25 16:13:24 +00001094 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
1095 RegClassList[rc]->printIG();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001096 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001097
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001098
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001099 LRI.coalesceLRs(); // coalesce all live ranges
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001100
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +00001101
Chris Lattner7e708292002-06-25 16:13:24 +00001102 if (DEBUG_RA) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001103 // print all LRs in all reg classes
Chris Lattner7e708292002-06-25 16:13:24 +00001104 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001105 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001106
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001107 // print IGs in all register classes
Chris Lattner7e708292002-06-25 16:13:24 +00001108 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001109 RegClassList[ rc ]->printIG();
1110 }
1111
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001112
1113 // mark un-usable suggested color before graph coloring algorithm.
1114 // When this is done, the graph coloring algo will not reserve
1115 // suggested color unnecessarily - they can be used by another LR
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001116 //
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001117 markUnusableSugColors();
1118
1119 // color all register classes using the graph coloring algo
Chris Lattner7e708292002-06-25 16:13:24 +00001120 for (unsigned rc=0; rc < NumOfRegClasses ; rc++)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001121 RegClassList[ rc ]->colorAllRegs();
1122
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001123 // Atter grpah coloring, if some LRs did not receive a color (i.e, spilled)
1124 // a poistion for such spilled LRs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001125 //
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001126 allocateStackSpace4SpilledLRs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001127
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001128 mcInfo.popAllTempValues(TM); // TODO **Check
1129
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001130 // color incoming args - if the correct color was not received
1131 // insert code to copy to the correct register
1132 //
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001133 colorIncomingArgs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001134
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001135 // Now update the machine code with register names and add any
1136 // additional code inserted by the register allocator to the instruction
1137 // stream
1138 //
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001139 updateMachineCode();
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001140
Chris Lattner045e7c82001-09-19 16:26:23 +00001141 if (DEBUG_RA) {
Vikram S. Adve12af1642001-11-08 04:48:50 +00001142 MachineCodeForMethod::get(Meth).dump();
Chris Lattner045e7c82001-09-19 16:26:23 +00001143 printMachineCode(); // only for DEBUGGING
1144 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001145}
1146
Ruchira Sasankae727f852001-09-18 22:43:57 +00001147
1148