blob: 9faa9cbcad7c9ee8a38f7f9a9d36d1d483968c2e [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. Adve39c94e12002-09-14 23:05:33 +00008#include "llvm/CodeGen/RegAllocCommon.h"
Vikram S. Adve12af1642001-11-08 04:48:50 +00009#include "llvm/CodeGen/PhyRegAlloc.h"
10#include "llvm/CodeGen/MachineInstr.h"
Vikram S. Advedabb41d2002-05-19 15:29:31 +000011#include "llvm/CodeGen/MachineInstrAnnot.h"
Vikram S. Advef5af6362002-07-08 23:15:32 +000012#include "llvm/CodeGen/MachineCodeForBasicBlock.h"
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000013#include "llvm/CodeGen/MachineCodeForMethod.h"
Chris Lattner483e14e2002-04-27 07:27:19 +000014#include "llvm/Analysis/LiveVar/FunctionLiveVarInfo.h"
Chris Lattner14ab1ce2002-02-04 17:48:00 +000015#include "llvm/Analysis/LoopInfo.h"
Vikram S. Adve12af1642001-11-08 04:48:50 +000016#include "llvm/Target/TargetMachine.h"
17#include "llvm/Target/MachineFrameInfo.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000018#include "llvm/Function.h"
Chris Lattner37730942002-02-05 03:52:29 +000019#include "llvm/Type.h"
Vikram S. Advedabb41d2002-05-19 15:29:31 +000020#include "llvm/iOther.h"
Vikram S. Advef5af6362002-07-08 23:15:32 +000021#include "Support/STLExtras.h"
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000022#include <math.h>
Chris Lattner697954c2002-01-20 22:54:45 +000023using std::cerr;
Anand Shuklacfb22d32002-06-25 20:55:50 +000024using std::vector;
Vikram S. Adve12af1642001-11-08 04:48:50 +000025
Chris Lattner70e60cb2002-05-22 17:08:27 +000026RegAllocDebugLevel_t DEBUG_RA;
Vikram S. Adve39c94e12002-09-14 23:05:33 +000027
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(
Vikram S. Adve39c94e12002-09-14 23:05:33 +000032 clEnumValN(RA_DEBUG_None , "n", "disable debug output"),
33 clEnumValN(RA_DEBUG_Results, "y", "debug output for allocation results"),
34 clEnumValN(RA_DEBUG_Coloring, "c", "debug output for graph coloring step"),
35 clEnumValN(RA_DEBUG_Interference,"ig","debug output for interference graphs"),
36 clEnumValN(RA_DEBUG_LiveRanges , "lr","debug output for live ranges"),
37 clEnumValN(RA_DEBUG_Verbose, "v", "extra debug output"),
Chris Lattner5ff62e92002-07-22 02:10:13 +000038 0));
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000039
Chris Lattner2f9b28e2002-02-04 15:54:09 +000040//----------------------------------------------------------------------------
41// RegisterAllocation pass front end...
42//----------------------------------------------------------------------------
43namespace {
Chris Lattnerf57b8452002-04-27 06:56:12 +000044 class RegisterAllocator : public FunctionPass {
Chris Lattner2f9b28e2002-02-04 15:54:09 +000045 TargetMachine &Target;
46 public:
47 inline RegisterAllocator(TargetMachine &T) : Target(T) {}
Chris Lattner96c466b2002-04-29 14:57:45 +000048
49 const char *getPassName() const { return "Register Allocation"; }
Chris Lattner6dd98a62002-02-04 00:33:08 +000050
Chris Lattner7e708292002-06-25 16:13:24 +000051 bool runOnFunction(Function &F) {
Chris Lattner2f9b28e2002-02-04 15:54:09 +000052 if (DEBUG_RA)
Chris Lattner7e708292002-06-25 16:13:24 +000053 cerr << "\n********* Function "<< F.getName() << " ***********\n";
Chris Lattner2f9b28e2002-02-04 15:54:09 +000054
Chris Lattner7e708292002-06-25 16:13:24 +000055 PhyRegAlloc PRA(&F, Target, &getAnalysis<FunctionLiveVarInfo>(),
Chris Lattner1b7f7dc2002-04-28 16:21:30 +000056 &getAnalysis<LoopInfo>());
Chris Lattner2f9b28e2002-02-04 15:54:09 +000057 PRA.allocateRegisters();
58
59 if (DEBUG_RA) cerr << "\nRegister allocation complete!\n";
60 return false;
61 }
Chris Lattner4911c352002-02-04 17:39:42 +000062
Chris Lattnerf57b8452002-04-27 06:56:12 +000063 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerdd5b4952002-08-08 19:01:28 +000064 AU.addRequired<LoopInfo>();
65 AU.addRequired<FunctionLiveVarInfo>();
Chris Lattner4911c352002-02-04 17:39:42 +000066 }
Chris Lattner2f9b28e2002-02-04 15:54:09 +000067 };
Chris Lattner6dd98a62002-02-04 00:33:08 +000068}
69
Chris Lattnerf57b8452002-04-27 06:56:12 +000070Pass *getRegisterAllocator(TargetMachine &T) {
Chris Lattner2f9b28e2002-02-04 15:54:09 +000071 return new RegisterAllocator(T);
72}
Chris Lattner6dd98a62002-02-04 00:33:08 +000073
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000074//----------------------------------------------------------------------------
75// Constructor: Init local composite objects and create register classes.
76//----------------------------------------------------------------------------
Chris Lattner1b7f7dc2002-04-28 16:21:30 +000077PhyRegAlloc::PhyRegAlloc(Function *F, const TargetMachine& tm,
78 FunctionLiveVarInfo *Lvi, LoopInfo *LDC)
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000079 : TM(tm), Meth(F),
80 mcInfo(MachineCodeForMethod::get(F)),
81 LVI(Lvi), LRI(F, tm, RegClassList),
82 MRI(tm.getRegInfo()),
Ruchira Sasanka8e604792001-09-14 21:18:34 +000083 NumOfRegClasses(MRI.getNumOfRegClasses()),
Chris Lattner4911c352002-02-04 17:39:42 +000084 LoopDepthCalc(LDC) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +000085
86 // create each RegisterClass and put in RegClassList
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000087 //
Chris Lattner7e708292002-06-25 16:13:24 +000088 for (unsigned rc=0; rc < NumOfRegClasses; rc++)
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000089 RegClassList.push_back(new RegClass(F, MRI.getMachineRegClass(rc),
90 &ResColList));
Ruchira Sasanka8e604792001-09-14 21:18:34 +000091}
92
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000093
94//----------------------------------------------------------------------------
95// Destructor: Deletes register classes
96//----------------------------------------------------------------------------
97PhyRegAlloc::~PhyRegAlloc() {
Chris Lattner7e708292002-06-25 16:13:24 +000098 for ( unsigned rc=0; rc < NumOfRegClasses; rc++)
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000099 delete RegClassList[rc];
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000100
101 AddedInstrMap.clear();
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000102}
103
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000104//----------------------------------------------------------------------------
105// This method initally creates interference graphs (one in each reg class)
106// and IGNodeList (one in each IG). The actual nodes will be pushed later.
107//----------------------------------------------------------------------------
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000108void PhyRegAlloc::createIGNodeListsAndIGs() {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000109 if (DEBUG_RA >= RA_DEBUG_LiveRanges) cerr << "Creating LR lists ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000110
111 // hash map iterator
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000112 LiveRangeMapType::const_iterator HMI = LRI.getLiveRangeMap()->begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000113
114 // hash map end
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000115 LiveRangeMapType::const_iterator HMIEnd = LRI.getLiveRangeMap()->end();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000116
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000117 for (; HMI != HMIEnd ; ++HMI ) {
118 if (HMI->first) {
119 LiveRange *L = HMI->second; // get the LiveRange
120 if (!L) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000121 if (DEBUG_RA)
122 cerr << "\n**** ?!?WARNING: NULL LIVE RANGE FOUND FOR: "
123 << RAV(HMI->first) << "****\n";
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000124 continue;
125 }
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000126
127 // if the Value * is not null, and LR is not yet written to the IGNodeList
Chris Lattner7e708292002-06-25 16:13:24 +0000128 if (!(L->getUserIGNode()) ) {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000129 RegClass *const RC = // RegClass of first value in the LR
130 RegClassList[ L->getRegClass()->getID() ];
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000131 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
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000140 if (DEBUG_RA >= RA_DEBUG_LiveRanges) cerr << "LRLists Created!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000141}
142
143
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000144//----------------------------------------------------------------------------
145// This method will add all interferences at for a given instruction.
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000146// Interence occurs only if the LR of Def (Inst or Arg) is of the same reg
147// class as that of live var. The live var passed to this function is the
148// LVset AFTER the instruction
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000149//----------------------------------------------------------------------------
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000150
Chris Lattner296b7732002-02-05 02:52:05 +0000151void PhyRegAlloc::addInterference(const Value *Def,
152 const ValueSet *LVSet,
153 bool isCallInst) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000154
Chris Lattner296b7732002-02-05 02:52:05 +0000155 ValueSet::const_iterator LIt = LVSet->begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000156
157 // get the live range of instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000158 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000159 const LiveRange *const LROfDef = LRI.getLiveRangeForValue( Def );
160
161 IGNode *const IGNodeOfDef = LROfDef->getUserIGNode();
162 assert( IGNodeOfDef );
163
164 RegClass *const RCOfDef = LROfDef->getRegClass();
165
166 // for each live var in live variable set
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000167 //
Chris Lattner7e708292002-06-25 16:13:24 +0000168 for ( ; LIt != LVSet->end(); ++LIt) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000169
Vikram S. Advef5af6362002-07-08 23:15:32 +0000170 if (DEBUG_RA >= RA_DEBUG_Verbose)
Chris Lattner0665a5f2002-02-05 01:43:49 +0000171 cerr << "< Def=" << RAV(Def) << ", Lvar=" << RAV(*LIt) << "> ";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000172
173 // get the live range corresponding to live var
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000174 //
Chris Lattner0665a5f2002-02-05 01:43:49 +0000175 LiveRange *LROfVar = LRI.getLiveRangeForValue(*LIt);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000176
177 // LROfVar can be null if it is a const since a const
178 // doesn't have a dominating def - see Assumptions above
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000179 //
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000180 if (LROfVar)
181 if (LROfDef != LROfVar) // do not set interf for same LR
182 if (RCOfDef == LROfVar->getRegClass()) // 2 reg classes are the same
183 RCOfDef->setInterference( LROfDef, LROfVar);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000184 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000185}
186
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000187
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000188
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000189//----------------------------------------------------------------------------
190// For a call instruction, this method sets the CallInterference flag in
191// the LR of each variable live int the Live Variable Set live after the
192// call instruction (except the return value of the call instruction - since
193// the return value does not interfere with that call itself).
194//----------------------------------------------------------------------------
195
196void PhyRegAlloc::setCallInterferences(const MachineInstr *MInst,
Chris Lattner296b7732002-02-05 02:52:05 +0000197 const ValueSet *LVSetAft) {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000198
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000199 if (DEBUG_RA >= RA_DEBUG_Interference)
Chris Lattner697954c2002-01-20 22:54:45 +0000200 cerr << "\n For call inst: " << *MInst;
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000201
Chris Lattner296b7732002-02-05 02:52:05 +0000202 ValueSet::const_iterator LIt = LVSetAft->begin();
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000203
204 // for each live var in live variable set after machine inst
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000205 //
Chris Lattner7e708292002-06-25 16:13:24 +0000206 for ( ; LIt != LVSetAft->end(); ++LIt) {
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000207
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000208 // get the live range corresponding to live var
209 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000210 LiveRange *const LR = LRI.getLiveRangeForValue(*LIt );
211
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000212 // LR can be null if it is a const since a const
213 // doesn't have a dominating def - see Assumptions above
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000214 //
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000215 if (LR ) {
216 if (DEBUG_RA >= RA_DEBUG_Interference) {
217 cerr << "\n\tLR after Call: ";
218 printSet(*LR);
219 }
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000220 LR->setCallInterference();
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000221 if (DEBUG_RA >= RA_DEBUG_Interference) {
222 cerr << "\n ++After adding call interference for LR: " ;
Chris Lattner296b7732002-02-05 02:52:05 +0000223 printSet(*LR);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000224 }
225 }
226
227 }
228
Vikram S. Adve1a53f032002-03-31 18:54:37 +0000229 // Now find the LR of the return value of the call
230 // We do this because, we look at the LV set *after* the instruction
231 // to determine, which LRs must be saved across calls. The return value
232 // of the call is live in this set - but it does not interfere with call
233 // (i.e., we can allocate a volatile register to the return value)
234 //
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000235 CallArgsDescriptor* argDesc = CallArgsDescriptor::get(MInst);
236
237 if (const Value *RetVal = argDesc->getReturnValue()) {
Vikram S. Adve1a53f032002-03-31 18:54:37 +0000238 LiveRange *RetValLR = LRI.getLiveRangeForValue( RetVal );
239 assert( RetValLR && "No LR for RetValue of call");
240 RetValLR->clearCallInterference();
241 }
242
243 // If the CALL is an indirect call, find the LR of the function pointer.
244 // That has a call interference because it conflicts with outgoing args.
Chris Lattner7e708292002-06-25 16:13:24 +0000245 if (const Value *AddrVal = argDesc->getIndirectFuncPtr()) {
Vikram S. Adve1a53f032002-03-31 18:54:37 +0000246 LiveRange *AddrValLR = LRI.getLiveRangeForValue( AddrVal );
247 assert( AddrValLR && "No LR for indirect addr val of call");
248 AddrValLR->setCallInterference();
249 }
250
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000251}
252
253
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000254
255
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000256//----------------------------------------------------------------------------
257// This method will walk thru code and create interferences in the IG of
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000258// each RegClass. Also, this method calculates the spill cost of each
259// Live Range (it is done in this method to save another pass over the code).
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000260//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000261void PhyRegAlloc::buildInterferenceGraphs()
262{
263
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000264 if (DEBUG_RA >= RA_DEBUG_Interference)
265 cerr << "Creating interference graphs ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000266
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000267 unsigned BBLoopDepthCost;
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000268 for (Function::const_iterator BBI = Meth->begin(), BBE = Meth->end();
269 BBI != BBE; ++BBI) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000270
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000271 // find the 10^(loop_depth) of this BB
272 //
Chris Lattner7e708292002-06-25 16:13:24 +0000273 BBLoopDepthCost = (unsigned)pow(10.0, LoopDepthCalc->getLoopDepth(BBI));
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000274
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000275 // get the iterator for machine instructions
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000276 //
Vikram S. Advef5af6362002-07-08 23:15:32 +0000277 const MachineCodeForBasicBlock& MIVec = MachineCodeForBasicBlock::get(BBI);
Vikram S. Adve48762092002-04-25 04:34:15 +0000278 MachineCodeForBasicBlock::const_iterator MII = MIVec.begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000279
280 // iterate over all the machine instructions in BB
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000281 //
Chris Lattner7e708292002-06-25 16:13:24 +0000282 for ( ; MII != MIVec.end(); ++MII) {
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000283
Vikram S. Adve48762092002-04-25 04:34:15 +0000284 const MachineInstr *MInst = *MII;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000285
286 // get the LV set after the instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000287 //
Chris Lattner7e708292002-06-25 16:13:24 +0000288 const ValueSet &LVSetAI = LVI->getLiveVarSetAfterMInst(MInst, BBI);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000289
290 const bool isCallInst = TM.getInstrInfo().isCall(MInst->getOpCode());
291
Chris Lattner7e708292002-06-25 16:13:24 +0000292 if (isCallInst ) {
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000293 // set the isCallInterference flag of each live range wich extends
294 // accross this call instruction. This information is used by graph
295 // coloring algo to avoid allocating volatile colors to live ranges
296 // that span across calls (since they have to be saved/restored)
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000297 //
Chris Lattner748697d2002-02-05 04:20:12 +0000298 setCallInterferences(MInst, &LVSetAI);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000299 }
300
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000301
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000302 // iterate over all MI operands to find defs
303 //
Chris Lattner2f898d22002-02-05 06:02:59 +0000304 for (MachineInstr::const_val_op_iterator OpI = MInst->begin(),
305 OpE = MInst->end(); OpI != OpE; ++OpI) {
306 if (OpI.isDef()) // create a new LR iff this operand is a def
Chris Lattner748697d2002-02-05 04:20:12 +0000307 addInterference(*OpI, &LVSetAI, isCallInst);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000308
309 // Calculate the spill cost of each live range
310 //
Chris Lattner2f898d22002-02-05 06:02:59 +0000311 LiveRange *LR = LRI.getLiveRangeForValue(*OpI);
312 if (LR) LR->addSpillCost(BBLoopDepthCost);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000313 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000314
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000315
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000316 // if there are multiple defs in this instruction e.g. in SETX
317 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000318 if (TM.getInstrInfo().isPseudoInstr(MInst->getOpCode()))
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000319 addInterf4PseudoInstr(MInst);
320
321
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000322 // Also add interference for any implicit definitions in a machine
323 // instr (currently, only calls have this).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000324 //
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000325 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
Chris Lattner7e708292002-06-25 16:13:24 +0000326 if ( NumOfImpRefs > 0 ) {
327 for (unsigned z=0; z < NumOfImpRefs; z++)
328 if (MInst->implicitRefIsDefined(z) )
Chris Lattner748697d2002-02-05 04:20:12 +0000329 addInterference( MInst->getImplicitRef(z), &LVSetAI, isCallInst );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000330 }
331
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +0000332
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000333 } // for all machine instructions in BB
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000334 } // for all BBs in function
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000335
336
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000337 // add interferences for function arguments. Since there are no explict
338 // defs in the function for args, we have to add them manually
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000339 //
340 addInterferencesForArgs();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000341
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000342 if (DEBUG_RA >= RA_DEBUG_Interference)
343 cerr << "Interference graphs calculated!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000344}
345
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000346
347
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000348//--------------------------------------------------------------------------
349// Pseudo instructions will be exapnded to multiple instructions by the
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000350// assembler. Consequently, all the opernds must get distinct registers.
351// Therefore, we mark all operands of a pseudo instruction as they interfere
352// with one another.
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000353//--------------------------------------------------------------------------
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000354void PhyRegAlloc::addInterf4PseudoInstr(const MachineInstr *MInst) {
355
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000356 bool setInterf = false;
357
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000358 // iterate over MI operands to find defs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000359 //
Chris Lattner2f898d22002-02-05 06:02:59 +0000360 for (MachineInstr::const_val_op_iterator It1 = MInst->begin(),
361 ItE = MInst->end(); It1 != ItE; ++It1) {
362 const LiveRange *LROfOp1 = LRI.getLiveRangeForValue(*It1);
363 assert((LROfOp1 || !It1.isDef()) && "No LR for Def in PSEUDO insruction");
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000364
Chris Lattner2f898d22002-02-05 06:02:59 +0000365 MachineInstr::const_val_op_iterator It2 = It1;
Chris Lattner7e708292002-06-25 16:13:24 +0000366 for (++It2; It2 != ItE; ++It2) {
Chris Lattner2f898d22002-02-05 06:02:59 +0000367 const LiveRange *LROfOp2 = LRI.getLiveRangeForValue(*It2);
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000368
Chris Lattner2f898d22002-02-05 06:02:59 +0000369 if (LROfOp2) {
370 RegClass *RCOfOp1 = LROfOp1->getRegClass();
371 RegClass *RCOfOp2 = LROfOp2->getRegClass();
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000372
Chris Lattner7e708292002-06-25 16:13:24 +0000373 if (RCOfOp1 == RCOfOp2 ){
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000374 RCOfOp1->setInterference( LROfOp1, LROfOp2 );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000375 setInterf = true;
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000376 }
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000377 } // if Op2 has a LR
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000378 } // for all other defs in machine instr
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000379 } // for all operands in an instruction
380
Chris Lattner2f898d22002-02-05 06:02:59 +0000381 if (!setInterf && MInst->getNumOperands() > 2) {
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000382 cerr << "\nInterf not set for any operand in pseudo instr:\n";
383 cerr << *MInst;
384 assert(0 && "Interf not set for pseudo instr with > 2 operands" );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000385 }
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000386}
387
388
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000389
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000390//----------------------------------------------------------------------------
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000391// This method will add interferences for incoming arguments to a function.
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000392//----------------------------------------------------------------------------
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000393
Chris Lattner296b7732002-02-05 02:52:05 +0000394void PhyRegAlloc::addInterferencesForArgs() {
395 // get the InSet of root BB
Chris Lattner7e708292002-06-25 16:13:24 +0000396 const ValueSet &InSet = LVI->getInSetOfBB(&Meth->front());
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000397
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000398 for (Function::const_aiterator AI=Meth->abegin(); AI != Meth->aend(); ++AI) {
Chris Lattner7e708292002-06-25 16:13:24 +0000399 // add interferences between args and LVars at start
400 addInterference(AI, &InSet, false);
401
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000402 if (DEBUG_RA >= RA_DEBUG_Interference)
Chris Lattner7e708292002-06-25 16:13:24 +0000403 cerr << " - %% adding interference for argument " << RAV(AI) << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000404 }
405}
406
407
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000408//----------------------------------------------------------------------------
409// This method is called after register allocation is complete to set the
410// allocated reisters in the machine code. This code will add register numbers
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000411// to MachineOperands that contain a Value. Also it calls target specific
412// methods to produce caller saving instructions. At the end, it adds all
413// additional instructions produced by the register allocator to the
414// instruction stream.
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000415//----------------------------------------------------------------------------
Vikram S. Adve48762092002-04-25 04:34:15 +0000416
417//-----------------------------
418// Utility functions used below
419//-----------------------------
420inline void
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000421PrependInstructions(vector<MachineInstr *> &IBef,
Vikram S. Adve48762092002-04-25 04:34:15 +0000422 MachineCodeForBasicBlock& MIVec,
423 MachineCodeForBasicBlock::iterator& MII,
424 const std::string& msg)
425{
426 if (!IBef.empty())
427 {
428 MachineInstr* OrigMI = *MII;
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000429 std::vector<MachineInstr *>::iterator AdIt;
Vikram S. Adve48762092002-04-25 04:34:15 +0000430 for (AdIt = IBef.begin(); AdIt != IBef.end() ; ++AdIt)
431 {
432 if (DEBUG_RA) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000433 if (OrigMI) cerr << "For MInst:\n " << *OrigMI;
434 cerr << msg << "PREPENDed instr:\n " << **AdIt << "\n";
Vikram S. Adve48762092002-04-25 04:34:15 +0000435 }
436 MII = MIVec.insert(MII, *AdIt);
437 ++MII;
438 }
439 }
440}
441
442inline void
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000443AppendInstructions(std::vector<MachineInstr *> &IAft,
Vikram S. Adve48762092002-04-25 04:34:15 +0000444 MachineCodeForBasicBlock& MIVec,
445 MachineCodeForBasicBlock::iterator& MII,
446 const std::string& msg)
447{
448 if (!IAft.empty())
449 {
450 MachineInstr* OrigMI = *MII;
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000451 std::vector<MachineInstr *>::iterator AdIt;
Chris Lattner7e708292002-06-25 16:13:24 +0000452 for ( AdIt = IAft.begin(); AdIt != IAft.end() ; ++AdIt )
Vikram S. Adve48762092002-04-25 04:34:15 +0000453 {
Chris Lattner7e708292002-06-25 16:13:24 +0000454 if (DEBUG_RA) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000455 if (OrigMI) cerr << "For MInst:\n " << *OrigMI;
456 cerr << msg << "APPENDed instr:\n " << **AdIt << "\n";
Vikram S. Adve48762092002-04-25 04:34:15 +0000457 }
458 ++MII; // insert before the next instruction
459 MII = MIVec.insert(MII, *AdIt);
460 }
461 }
462}
463
464
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000465void PhyRegAlloc::updateMachineCode()
466{
Vikram S. Advef5af6362002-07-08 23:15:32 +0000467 MachineCodeForBasicBlock& MIVec = MachineCodeForBasicBlock::get(&Meth->getEntryNode());
Vikram S. Adve48762092002-04-25 04:34:15 +0000468
Chris Lattner7e708292002-06-25 16:13:24 +0000469 // Insert any instructions needed at method entry
470 MachineCodeForBasicBlock::iterator MII = MIVec.begin();
471 PrependInstructions(AddedInstrAtEntry.InstrnsBefore, MIVec, MII,
472 "At function entry: \n");
473 assert(AddedInstrAtEntry.InstrnsAfter.empty() &&
474 "InstrsAfter should be unnecessary since we are just inserting at "
475 "the function entry point here.");
Vikram S. Adve48762092002-04-25 04:34:15 +0000476
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000477 for (Function::const_iterator BBI = Meth->begin(), BBE = Meth->end();
478 BBI != BBE; ++BBI) {
Vikram S. Adve48762092002-04-25 04:34:15 +0000479
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000480 // iterate over all the machine instructions in BB
Vikram S. Advef5af6362002-07-08 23:15:32 +0000481 MachineCodeForBasicBlock &MIVec = MachineCodeForBasicBlock::get(BBI);
Chris Lattner7e708292002-06-25 16:13:24 +0000482 for (MachineCodeForBasicBlock::iterator MII = MIVec.begin();
Vikram S. Adve48762092002-04-25 04:34:15 +0000483 MII != MIVec.end(); ++MII) {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000484
Vikram S. Adve48762092002-04-25 04:34:15 +0000485 MachineInstr *MInst = *MII;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000486
487 unsigned Opcode = MInst->getOpCode();
488
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000489 // do not process Phis
Vikram S. Adve23a4c8f2002-03-18 03:37:19 +0000490 if (TM.getInstrInfo().isDummyPhiInstr(Opcode))
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000491 continue;
492
Vikram S. Advef5af6362002-07-08 23:15:32 +0000493 // Reset tmp stack positions so they can be reused for each machine instr.
494 mcInfo.popAllTempValues(TM);
495
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000496 // Now insert speical instructions (if necessary) for call/return
497 // instructions.
498 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000499 if (TM.getInstrInfo().isCall(Opcode) ||
500 TM.getInstrInfo().isReturn(Opcode)) {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000501
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000502 AddedInstrns &AI = AddedInstrMap[MInst];
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000503
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000504 if (TM.getInstrInfo().isCall(Opcode))
Chris Lattner7e708292002-06-25 16:13:24 +0000505 MRI.colorCallArgs(MInst, LRI, &AI, *this, BBI);
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000506 else if (TM.getInstrInfo().isReturn(Opcode))
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000507 MRI.colorRetValue(MInst, LRI, &AI);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000508 }
509
Vikram S. Advef5af6362002-07-08 23:15:32 +0000510 // Set the registers for operands in the machine instruction
511 // if a register was successfully allocated. If not, insert
512 // code to spill the register value.
513 //
514 for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum)
515 {
516 MachineOperand& Op = MInst->getOperand(OpNum);
517 if (Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
518 Op.getOperandType() == MachineOperand::MO_CCRegister)
519 {
520 const Value *const Val = Op.getVRegValue();
521
522 LiveRange *const LR = LRI.getLiveRangeForValue(Val);
523 if (!LR) // consts or labels will have no live range
524 {
525 // if register is not allocated, mark register as invalid
526 if (Op.getAllocatedRegNum() == -1)
527 MInst->SetRegForOperand(OpNum, MRI.getInvalidRegNum());
528 continue;
529 }
530
531 if (LR->hasColor() )
532 MInst->SetRegForOperand(OpNum,
533 MRI.getUnifiedRegNum(LR->getRegClass()->getID(),
534 LR->getColor()));
535 else
536 // LR did NOT receive a color (register). Insert spill code.
537 insertCode4SpilledLR(LR, MInst, BBI, OpNum );
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000538 }
Vikram S. Advef5af6362002-07-08 23:15:32 +0000539 } // for each operand
540
541
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000542 // Now add instructions that the register allocator inserts before/after
543 // this machine instructions (done only for calls/rets/incoming args)
544 // We do this here, to ensure that spill for an instruction is inserted
545 // closest as possible to an instruction (see above insertCode4Spill...)
546 //
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000547 // If there are instructions to be added, *before* this machine
548 // instruction, add them now.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000549 //
Chris Lattner7e708292002-06-25 16:13:24 +0000550 if (AddedInstrMap.count(MInst)) {
Vikram S. Adve48762092002-04-25 04:34:15 +0000551 PrependInstructions(AddedInstrMap[MInst].InstrnsBefore, MIVec, MII,"");
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000552 }
Vikram S. Adve48762092002-04-25 04:34:15 +0000553
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000554 // If there are instructions to be added *after* this machine
555 // instruction, add them now
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000556 //
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000557 if (!AddedInstrMap[MInst].InstrnsAfter.empty()) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000558
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000559 // if there are delay slots for this instruction, the instructions
560 // added after it must really go after the delayed instruction(s)
561 // So, we move the InstrAfter of the current instruction to the
562 // corresponding delayed instruction
563
564 unsigned delay;
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000565 if ((delay=TM.getInstrInfo().getNumDelaySlots(MInst->getOpCode())) >0){
Vikram S. Adve48762092002-04-25 04:34:15 +0000566 move2DelayedInstr(MInst, *(MII+delay) );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000567 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000568 else {
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000569 // Here we can add the "instructions after" to the current
570 // instruction since there are no delay slots for this instruction
Vikram S. Adve48762092002-04-25 04:34:15 +0000571 AppendInstructions(AddedInstrMap[MInst].InstrnsAfter, MIVec, MII,"");
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000572 } // if not delay
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000573 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000574
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000575 } // for each machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000576 }
577}
578
579
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000580
581//----------------------------------------------------------------------------
582// This method inserts spill code for AN operand whose LR was spilled.
583// This method may be called several times for a single machine instruction
584// if it contains many spilled operands. Each time it is called, it finds
585// a register which is not live at that instruction and also which is not
586// used by other spilled operands of the same instruction. Then it uses
587// this register temporarily to accomodate the spilled value.
588//----------------------------------------------------------------------------
589void PhyRegAlloc::insertCode4SpilledLR(const LiveRange *LR,
590 MachineInstr *MInst,
591 const BasicBlock *BB,
592 const unsigned OpNum) {
593
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000594 assert(! TM.getInstrInfo().isCall(MInst->getOpCode()) &&
595 (! TM.getInstrInfo().isReturn(MInst->getOpCode())) &&
596 "Arg of a call/ret must be handled elsewhere");
597
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000598 MachineOperand& Op = MInst->getOperand(OpNum);
599 bool isDef = MInst->operandIsDefined(OpNum);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000600 bool isDefAndUse = MInst->operandIsDefinedAndUsed(OpNum);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000601 unsigned RegType = MRI.getRegType( LR );
602 int SpillOff = LR->getSpillOffFromFP();
603 RegClass *RC = LR->getRegClass();
Chris Lattner748697d2002-02-05 04:20:12 +0000604 const ValueSet &LVSetBef = LVI->getLiveVarSetBeforeMInst(MInst, BB);
Vikram S. Adve00521d72001-11-12 23:26:35 +0000605
Chris Lattner697954c2002-01-20 22:54:45 +0000606 mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000607
Vikram S. Advef5af6362002-07-08 23:15:32 +0000608 vector<MachineInstr*> MIBef, MIAft;
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000609 vector<MachineInstr*> AdIMid;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000610
Vikram S. Advef5af6362002-07-08 23:15:32 +0000611 // Choose a register to hold the spilled value. This may insert code
612 // before and after MInst to free up the value. If so, this code should
613 // be first and last in the spill sequence before/after MInst.
614 int TmpRegU = getUsableUniRegAtMI(RegType, &LVSetBef, MInst, MIBef, MIAft);
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000615
Vikram S. Advef5af6362002-07-08 23:15:32 +0000616 // Set the operand first so that it this register does not get used
617 // as a scratch register for later calls to getUsableUniRegAtMI below
618 MInst->SetRegForOperand(OpNum, TmpRegU);
619
620 // get the added instructions for this instruction
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000621 AddedInstrns &AI = AddedInstrMap[MInst];
Vikram S. Advef5af6362002-07-08 23:15:32 +0000622
623 // We may need a scratch register to copy the spilled value to/from memory.
624 // This may itself have to insert code to free up a scratch register.
625 // Any such code should go before (after) the spill code for a load (store).
626 int scratchRegType = -1;
627 int scratchReg = -1;
628 if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType))
629 {
630 scratchReg = this->getUsableUniRegAtMI(scratchRegType, &LVSetBef,
631 MInst, MIBef, MIAft);
632 assert(scratchReg != MRI.getInvalidRegNum());
633 MInst->getRegsUsed().insert(scratchReg);
634 }
635
636 if (!isDef || isDefAndUse) {
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000637 // for a USE, we have to load the value of LR from stack to a TmpReg
638 // and use the TmpReg as one operand of instruction
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000639
Vikram S. Advef5af6362002-07-08 23:15:32 +0000640 // actual loading instruction(s)
641 MRI.cpMem2RegMI(AdIMid, MRI.getFramePointer(), SpillOff, TmpRegU, RegType,
642 scratchReg);
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000643
Vikram S. Advef5af6362002-07-08 23:15:32 +0000644 // the actual load should be after the instructions to free up TmpRegU
645 MIBef.insert(MIBef.end(), AdIMid.begin(), AdIMid.end());
646 AdIMid.clear();
647 }
648
649 if (isDef) { // if this is a Def
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000650 // for a DEF, we have to store the value produced by this instruction
651 // on the stack position allocated for this LR
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000652
Vikram S. Advef5af6362002-07-08 23:15:32 +0000653 // actual storing instruction(s)
654 MRI.cpReg2MemMI(AdIMid, TmpRegU, MRI.getFramePointer(), SpillOff, RegType,
655 scratchReg);
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000656
Vikram S. Advef5af6362002-07-08 23:15:32 +0000657 MIAft.insert(MIAft.begin(), AdIMid.begin(), AdIMid.end());
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000658 } // if !DEF
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000659
Vikram S. Advef5af6362002-07-08 23:15:32 +0000660 // Finally, insert the entire spill code sequences before/after MInst
661 AI.InstrnsBefore.insert(AI.InstrnsBefore.end(), MIBef.begin(), MIBef.end());
662 AI.InstrnsAfter.insert(AI.InstrnsAfter.begin(), MIAft.begin(), MIAft.end());
663
Chris Lattner7e708292002-06-25 16:13:24 +0000664 if (DEBUG_RA) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000665 cerr << "\nFor Inst:\n " << *MInst;
666 cerr << "SPILLED LR# " << LR->getUserIGNode()->getIndex();
667 cerr << "; added Instructions:";
Anand Shuklad58290e2002-07-09 19:18:56 +0000668 for_each(MIBef.begin(), MIBef.end(), std::mem_fun(&MachineInstr::dump));
669 for_each(MIAft.begin(), MIAft.end(), std::mem_fun(&MachineInstr::dump));
Chris Lattner7e708292002-06-25 16:13:24 +0000670 }
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000671}
672
673
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000674//----------------------------------------------------------------------------
675// We can use the following method to get a temporary register to be used
676// BEFORE any given machine instruction. If there is a register available,
677// this method will simply return that register and set MIBef = MIAft = NULL.
678// Otherwise, it will return a register and MIAft and MIBef will contain
679// two instructions used to free up this returned register.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000680// Returned register number is the UNIFIED register number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000681//----------------------------------------------------------------------------
682
Vikram S. Advef5af6362002-07-08 23:15:32 +0000683int PhyRegAlloc::getUsableUniRegAtMI(const int RegType,
684 const ValueSet *LVSetBef,
685 MachineInstr *MInst,
686 std::vector<MachineInstr*>& MIBef,
687 std::vector<MachineInstr*>& MIAft) {
688
689 RegClass* RC = this->getRegClassByID(MRI.getRegClassIDOfRegType(RegType));
690
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000691 int RegU = getUnusedUniRegAtMI(RC, MInst, LVSetBef);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000692
693 if (RegU == -1) {
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000694 // we couldn't find an unused register. Generate code to free up a reg by
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000695 // saving it on stack and restoring after the instruction
Vikram S. Advef5af6362002-07-08 23:15:32 +0000696
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000697 int TmpOff = mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
Vikram S. Adve12af1642001-11-08 04:48:50 +0000698
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000699 RegU = getUniRegNotUsedByThisInst(RC, MInst);
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000700
Vikram S. Advef5af6362002-07-08 23:15:32 +0000701 // Check if we need a scratch register to copy this register to memory.
702 int scratchRegType = -1;
703 if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType))
704 {
705 int scratchReg = this->getUsableUniRegAtMI(scratchRegType, LVSetBef,
706 MInst, MIBef, MIAft);
707 assert(scratchReg != MRI.getInvalidRegNum());
708
709 // We may as well hold the value in the scratch register instead
710 // of copying it to memory and back. But we have to mark the
711 // register as used by this instruction, so it does not get used
712 // as a scratch reg. by another operand or anyone else.
713 MInst->getRegsUsed().insert(scratchReg);
714 MRI.cpReg2RegMI(MIBef, RegU, scratchReg, RegType);
715 MRI.cpReg2RegMI(MIAft, scratchReg, RegU, RegType);
716 }
717 else
718 { // the register can be copied directly to/from memory so do it.
719 MRI.cpReg2MemMI(MIBef, RegU, MRI.getFramePointer(), TmpOff, RegType);
720 MRI.cpMem2RegMI(MIAft, MRI.getFramePointer(), TmpOff, RegU, RegType);
721 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000722 }
Vikram S. Advef5af6362002-07-08 23:15:32 +0000723
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000724 return RegU;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000725}
726
727//----------------------------------------------------------------------------
728// This method is called to get a new unused register that can be used to
729// accomodate a spilled value.
730// This method may be called several times for a single machine instruction
731// if it contains many spilled operands. Each time it is called, it finds
732// a register which is not live at that instruction and also which is not
733// used by other spilled operands of the same instruction.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000734// Return register number is relative to the register class. NOT
735// unified number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000736//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000737int PhyRegAlloc::getUnusedUniRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000738 const MachineInstr *MInst,
Chris Lattner296b7732002-02-05 02:52:05 +0000739 const ValueSet *LVSetBef) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000740
741 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
742
Chris Lattner85c54652002-05-23 15:50:03 +0000743 std::vector<bool> &IsColorUsedArr = RC->getIsColorUsedArr();
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000744
Chris Lattner7e708292002-06-25 16:13:24 +0000745 for (unsigned i=0; i < NumAvailRegs; i++) // Reset array
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000746 IsColorUsedArr[i] = false;
747
Chris Lattner296b7732002-02-05 02:52:05 +0000748 ValueSet::const_iterator LIt = LVSetBef->begin();
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000749
750 // for each live var in live variable set after machine inst
Chris Lattner7e708292002-06-25 16:13:24 +0000751 for ( ; LIt != LVSetBef->end(); ++LIt) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000752
753 // get the live range corresponding to live var
754 LiveRange *const LRofLV = LRI.getLiveRangeForValue(*LIt );
755
756 // LR can be null if it is a const since a const
757 // doesn't have a dominating def - see Assumptions above
Chris Lattner7e708292002-06-25 16:13:24 +0000758 if (LRofLV && LRofLV->getRegClass() == RC && LRofLV->hasColor() )
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000759 IsColorUsedArr[ LRofLV->getColor() ] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000760 }
761
762 // It is possible that one operand of this MInst was already spilled
763 // and it received some register temporarily. If that's the case,
764 // it is recorded in machine operand. We must skip such registers.
765
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000766 setRelRegsUsedByThisInst(RC, MInst);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000767
Chris Lattner7e708292002-06-25 16:13:24 +0000768 for (unsigned c=0; c < NumAvailRegs; c++) // find first unused color
Chris Lattner85c54652002-05-23 15:50:03 +0000769 if (!IsColorUsedArr[c])
770 return MRI.getUnifiedRegNum(RC->getID(), c);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000771
Chris Lattner85c54652002-05-23 15:50:03 +0000772 return -1;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000773}
774
775
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000776//----------------------------------------------------------------------------
777// Get any other register in a register class, other than what is used
778// by operands of a machine instruction. Returns the unified reg number.
779//----------------------------------------------------------------------------
780int PhyRegAlloc::getUniRegNotUsedByThisInst(RegClass *RC,
Chris Lattner85c54652002-05-23 15:50:03 +0000781 const MachineInstr *MInst) {
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000782
Chris Lattner85c54652002-05-23 15:50:03 +0000783 vector<bool> &IsColorUsedArr = RC->getIsColorUsedArr();
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000784 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
785
Chris Lattner7e708292002-06-25 16:13:24 +0000786 for (unsigned i=0; i < NumAvailRegs ; i++) // Reset array
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000787 IsColorUsedArr[i] = false;
788
789 setRelRegsUsedByThisInst(RC, MInst);
790
Chris Lattner7e708292002-06-25 16:13:24 +0000791 for (unsigned c=0; c < RC->getNumOfAvailRegs(); c++)// find first unused color
Chris Lattner85c54652002-05-23 15:50:03 +0000792 if (!IsColorUsedArr[c])
793 return MRI.getUnifiedRegNum(RC->getID(), c);
794
795 assert(0 && "FATAL: No free register could be found in reg class!!");
Chris Lattner697954c2002-01-20 22:54:45 +0000796 return 0;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000797}
798
799
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000800//----------------------------------------------------------------------------
801// This method modifies the IsColorUsedArr of the register class passed to it.
802// It sets the bits corresponding to the registers used by this machine
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000803// instructions. Both explicit and implicit operands are set.
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000804//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000805void PhyRegAlloc::setRelRegsUsedByThisInst(RegClass *RC,
Vikram S. Advef5af6362002-07-08 23:15:32 +0000806 const MachineInstr *MInst ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000807
Vikram S. Advef5af6362002-07-08 23:15:32 +0000808 vector<bool> &IsColorUsedArr = RC->getIsColorUsedArr();
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000809
Vikram S. Advef5af6362002-07-08 23:15:32 +0000810 // Add the registers already marked as used by the instruction.
811 // This should include any scratch registers that are used to save
812 // values across the instruction (e.g., for saving state register values).
813 const hash_set<int>& regsUsed = MInst->getRegsUsed();
814 for (hash_set<int>::const_iterator SI=regsUsed.begin(), SE=regsUsed.end();
815 SI != SE; ++SI)
816 {
817 unsigned classId = 0;
818 int classRegNum = MRI.getClassRegNum(*SI, classId);
819 if (RC->getID() == classId)
820 {
821 assert(classRegNum < (int) IsColorUsedArr.size() &&
822 "Illegal register number for this reg class?");
823 IsColorUsedArr[classRegNum] = true;
824 }
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000825 }
Vikram S. Advef5af6362002-07-08 23:15:32 +0000826
827 // Now add registers allocated to the live ranges of values used in
828 // the instruction. These are not yet recorded in the instruction.
829 for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum)
830 {
831 const MachineOperand& Op = MInst->getOperand(OpNum);
832
833 if (Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
834 Op.getOperandType() == MachineOperand::MO_CCRegister)
835 if (const Value* Val = Op.getVRegValue())
836 if (MRI.getRegClassIDOfValue(Val) == RC->getID())
837 if (Op.getAllocatedRegNum() == -1)
838 if (LiveRange *LROfVal = LRI.getLiveRangeForValue(Val))
839 if (LROfVal->hasColor() )
840 // this operand is in a LR that received a color
841 IsColorUsedArr[LROfVal->getColor()] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000842 }
Vikram S. Advef5af6362002-07-08 23:15:32 +0000843
844 // If there are implicit references, mark their allocated regs as well
845 //
846 for (unsigned z=0; z < MInst->getNumImplicitRefs(); z++)
847 if (const LiveRange*
848 LRofImpRef = LRI.getLiveRangeForValue(MInst->getImplicitRef(z)))
849 if (LRofImpRef->hasColor())
850 // this implicit reference is in a LR that received a color
851 IsColorUsedArr[LRofImpRef->getColor()] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000852}
853
854
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000855//----------------------------------------------------------------------------
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000856// If there are delay slots for an instruction, the instructions
857// added after it must really go after the delayed instruction(s).
858// So, we move the InstrAfter of that instruction to the
859// corresponding delayed instruction using the following method.
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000860
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000861//----------------------------------------------------------------------------
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000862void PhyRegAlloc::move2DelayedInstr(const MachineInstr *OrigMI,
863 const MachineInstr *DelayedMI) {
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000864
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000865 // "added after" instructions of the original instr
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000866 std::vector<MachineInstr *> &OrigAft = AddedInstrMap[OrigMI].InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000867
868 // "added instructions" of the delayed instr
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000869 AddedInstrns &DelayAdI = AddedInstrMap[DelayedMI];
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000870
871 // "added after" instructions of the delayed instr
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000872 std::vector<MachineInstr *> &DelayedAft = DelayAdI.InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000873
874 // go thru all the "added after instructions" of the original instruction
875 // and append them to the "addded after instructions" of the delayed
876 // instructions
Chris Lattner697954c2002-01-20 22:54:45 +0000877 DelayedAft.insert(DelayedAft.end(), OrigAft.begin(), OrigAft.end());
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000878
879 // empty the "added after instructions" of the original instruction
880 OrigAft.clear();
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000881}
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000882
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000883//----------------------------------------------------------------------------
884// This method prints the code with registers after register allocation is
885// complete.
886//----------------------------------------------------------------------------
887void PhyRegAlloc::printMachineCode()
888{
889
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000890 cerr << "\n;************** Function " << Meth->getName()
Chris Lattner697954c2002-01-20 22:54:45 +0000891 << " *****************\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000892
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000893 for (Function::const_iterator BBI = Meth->begin(), BBE = Meth->end();
894 BBI != BBE; ++BBI) {
Chris Lattner7e708292002-06-25 16:13:24 +0000895 cerr << "\n"; printLabel(BBI); cerr << ": ";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000896
897 // get the iterator for machine instructions
Vikram S. Advef5af6362002-07-08 23:15:32 +0000898 MachineCodeForBasicBlock& MIVec = MachineCodeForBasicBlock::get(BBI);
Vikram S. Adve48762092002-04-25 04:34:15 +0000899 MachineCodeForBasicBlock::iterator MII = MIVec.begin();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000900
901 // iterate over all the machine instructions in BB
Chris Lattner7e708292002-06-25 16:13:24 +0000902 for ( ; MII != MIVec.end(); ++MII) {
Vikram S. Adve48762092002-04-25 04:34:15 +0000903 MachineInstr *const MInst = *MII;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000904
Chris Lattner697954c2002-01-20 22:54:45 +0000905 cerr << "\n\t";
906 cerr << TargetInstrDescriptors[MInst->getOpCode()].opCodeString;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000907
Chris Lattner7e708292002-06-25 16:13:24 +0000908 for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000909 MachineOperand& Op = MInst->getOperand(OpNum);
910
Chris Lattner7e708292002-06-25 16:13:24 +0000911 if (Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasanka97b8b442001-10-18 22:36:26 +0000912 Op.getOperandType() == MachineOperand::MO_CCRegister /*||
913 Op.getOperandType() == MachineOperand::MO_PCRelativeDisp*/ ) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000914
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000915 const Value *const Val = Op.getVRegValue () ;
Ruchira Sasankae727f852001-09-18 22:43:57 +0000916 // ****this code is temporary till NULL Values are fixed
Chris Lattner7e708292002-06-25 16:13:24 +0000917 if (! Val ) {
Chris Lattner697954c2002-01-20 22:54:45 +0000918 cerr << "\t<*NULL*>";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000919 continue;
920 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000921
922 // if a label or a constant
Chris Lattner7e708292002-06-25 16:13:24 +0000923 if (isa<BasicBlock>(Val)) {
Chris Lattner697954c2002-01-20 22:54:45 +0000924 cerr << "\t"; printLabel( Op.getVRegValue () );
925 } else {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000926 // else it must be a register value
927 const int RegNum = Op.getAllocatedRegNum();
928
Chris Lattner697954c2002-01-20 22:54:45 +0000929 cerr << "\t" << "%" << MRI.getUnifiedRegName( RegNum );
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000930 if (Val->hasName() )
Chris Lattner697954c2002-01-20 22:54:45 +0000931 cerr << "(" << Val->getName() << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000932 else
Chris Lattner697954c2002-01-20 22:54:45 +0000933 cerr << "(" << Val << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000934
Chris Lattner7e708292002-06-25 16:13:24 +0000935 if (Op.opIsDef() )
Chris Lattner697954c2002-01-20 22:54:45 +0000936 cerr << "*";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000937
938 const LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
Chris Lattner7e708292002-06-25 16:13:24 +0000939 if (LROfVal )
940 if (LROfVal->hasSpillOffset() )
Chris Lattner697954c2002-01-20 22:54:45 +0000941 cerr << "$";
Ruchira Sasankae727f852001-09-18 22:43:57 +0000942 }
943
944 }
Chris Lattner7e708292002-06-25 16:13:24 +0000945 else if (Op.getOperandType() == MachineOperand::MO_MachineRegister) {
Chris Lattner697954c2002-01-20 22:54:45 +0000946 cerr << "\t" << "%" << MRI.getUnifiedRegName(Op.getMachineRegNum());
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000947 }
948
949 else
Chris Lattner697954c2002-01-20 22:54:45 +0000950 cerr << "\t" << Op; // use dump field
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000951 }
952
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000953
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000954
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000955 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
Chris Lattner7e708292002-06-25 16:13:24 +0000956 if (NumOfImpRefs > 0) {
Chris Lattner697954c2002-01-20 22:54:45 +0000957 cerr << "\tImplicit:";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000958
Chris Lattner7e708292002-06-25 16:13:24 +0000959 for (unsigned z=0; z < NumOfImpRefs; z++)
Chris Lattner0665a5f2002-02-05 01:43:49 +0000960 cerr << RAV(MInst->getImplicitRef(z)) << "\t";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000961 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000962
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000963 } // for all machine instructions
964
Chris Lattner697954c2002-01-20 22:54:45 +0000965 cerr << "\n";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000966
967 } // for all BBs
968
Chris Lattner697954c2002-01-20 22:54:45 +0000969 cerr << "\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000970}
971
Ruchira Sasankae727f852001-09-18 22:43:57 +0000972
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000973//----------------------------------------------------------------------------
974
975//----------------------------------------------------------------------------
976void PhyRegAlloc::colorIncomingArgs()
977{
Chris Lattner7e708292002-06-25 16:13:24 +0000978 const BasicBlock &FirstBB = Meth->front();
Vikram S. Advef5af6362002-07-08 23:15:32 +0000979 const MachineInstr *FirstMI = MachineCodeForBasicBlock::get(&FirstBB).front();
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000980 assert(FirstMI && "No machine instruction in entry BB");
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000981
Vikram S. Adve48762092002-04-25 04:34:15 +0000982 MRI.colorMethodArgs(Meth, LRI, &AddedInstrAtEntry);
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000983}
984
Ruchira Sasankae727f852001-09-18 22:43:57 +0000985
986//----------------------------------------------------------------------------
987// Used to generate a label for a basic block
988//----------------------------------------------------------------------------
Chris Lattner697954c2002-01-20 22:54:45 +0000989void PhyRegAlloc::printLabel(const Value *const Val) {
990 if (Val->hasName())
991 cerr << Val->getName();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000992 else
Chris Lattner697954c2002-01-20 22:54:45 +0000993 cerr << "Label" << Val;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000994}
995
996
Ruchira Sasankae727f852001-09-18 22:43:57 +0000997//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +0000998// This method calls setSugColorUsable method of each live range. This
999// will determine whether the suggested color of LR is really usable.
1000// A suggested color is not usable when the suggested color is volatile
1001// AND when there are call interferences
1002//----------------------------------------------------------------------------
1003
1004void PhyRegAlloc::markUnusableSugColors()
1005{
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001006 // hash map iterator
1007 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
1008 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
1009
Chris Lattner7e708292002-06-25 16:13:24 +00001010 for (; HMI != HMIEnd ; ++HMI ) {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001011 if (HMI->first) {
1012 LiveRange *L = HMI->second; // get the LiveRange
1013 if (L) {
Chris Lattner7e708292002-06-25 16:13:24 +00001014 if (L->hasSuggestedColor()) {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001015 int RCID = L->getRegClass()->getID();
Chris Lattner7e708292002-06-25 16:13:24 +00001016 if (MRI.isRegVolatile( RCID, L->getSuggestedColor()) &&
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001017 L->isCallInterference() )
1018 L->setSuggestedColorUsable( false );
1019 else
1020 L->setSuggestedColorUsable( true );
1021 }
1022 } // if L->hasSuggestedColor()
1023 }
1024 } // for all LR's in hash map
1025}
1026
1027
1028
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001029//----------------------------------------------------------------------------
1030// The following method will set the stack offsets of the live ranges that
1031// are decided to be spillled. This must be called just after coloring the
1032// LRs using the graph coloring algo. For each live range that is spilled,
1033// this method allocate a new spill position on the stack.
1034//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001035
Chris Lattner37730942002-02-05 03:52:29 +00001036void PhyRegAlloc::allocateStackSpace4SpilledLRs() {
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001037 if (DEBUG_RA) cerr << "\nSetting LR stack offsets for spills...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001038
Chris Lattner37730942002-02-05 03:52:29 +00001039 LiveRangeMapType::const_iterator HMI = LRI.getLiveRangeMap()->begin();
1040 LiveRangeMapType::const_iterator HMIEnd = LRI.getLiveRangeMap()->end();
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001041
Chris Lattner7e708292002-06-25 16:13:24 +00001042 for ( ; HMI != HMIEnd ; ++HMI) {
Chris Lattner37730942002-02-05 03:52:29 +00001043 if (HMI->first && HMI->second) {
1044 LiveRange *L = HMI->second; // get the LiveRange
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001045 if (!L->hasColor()) { // NOTE: ** allocating the size of long Type **
1046 int stackOffset = mcInfo.allocateSpilledValue(TM, Type::LongTy);
1047 L->setSpillOffFromFP(stackOffset);
1048 if (DEBUG_RA)
1049 cerr << " LR# " << L->getUserIGNode()->getIndex()
1050 << ": stack-offset = " << stackOffset << "\n";
1051 }
Chris Lattner37730942002-02-05 03:52:29 +00001052 }
1053 } // for all LR's in hash map
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001054}
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001055
1056
1057
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001058//----------------------------------------------------------------------------
Ruchira Sasankae727f852001-09-18 22:43:57 +00001059// The entry pont to Register Allocation
1060//----------------------------------------------------------------------------
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001061
1062void PhyRegAlloc::allocateRegisters()
1063{
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001064
1065 // make sure that we put all register classes into the RegClassList
1066 // before we call constructLiveRanges (now done in the constructor of
1067 // PhyRegAlloc class).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001068 //
1069 LRI.constructLiveRanges(); // create LR info
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001070
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001071 if (DEBUG_RA >= RA_DEBUG_LiveRanges)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001072 LRI.printLiveRanges();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001073
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001074 createIGNodeListsAndIGs(); // create IGNode list and IGs
1075
1076 buildInterferenceGraphs(); // build IGs in all reg classes
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001077
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001078
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001079 if (DEBUG_RA >= RA_DEBUG_LiveRanges) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001080 // print all LRs in all reg classes
Chris Lattner7e708292002-06-25 16:13:24 +00001081 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
1082 RegClassList[rc]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001083
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001084 // print IGs in all register classes
Chris Lattner7e708292002-06-25 16:13:24 +00001085 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
1086 RegClassList[rc]->printIG();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001087 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001088
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001089
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001090 LRI.coalesceLRs(); // coalesce all live ranges
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001091
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +00001092
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001093 if (DEBUG_RA >= RA_DEBUG_LiveRanges) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001094 // print all LRs in all reg classes
Chris Lattner7e708292002-06-25 16:13:24 +00001095 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001096 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001097
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001098 // print IGs in all register classes
Chris Lattner7e708292002-06-25 16:13:24 +00001099 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001100 RegClassList[ rc ]->printIG();
1101 }
1102
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001103
1104 // mark un-usable suggested color before graph coloring algorithm.
1105 // When this is done, the graph coloring algo will not reserve
1106 // suggested color unnecessarily - they can be used by another LR
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001107 //
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001108 markUnusableSugColors();
1109
1110 // color all register classes using the graph coloring algo
Chris Lattner7e708292002-06-25 16:13:24 +00001111 for (unsigned rc=0; rc < NumOfRegClasses ; rc++)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001112 RegClassList[ rc ]->colorAllRegs();
1113
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001114 // Atter grpah coloring, if some LRs did not receive a color (i.e, spilled)
1115 // a poistion for such spilled LRs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001116 //
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001117 allocateStackSpace4SpilledLRs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001118
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001119 mcInfo.popAllTempValues(TM); // TODO **Check
1120
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001121 // color incoming args - if the correct color was not received
1122 // insert code to copy to the correct register
1123 //
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001124 colorIncomingArgs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001125
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001126 // Now update the machine code with register names and add any
1127 // additional code inserted by the register allocator to the instruction
1128 // stream
1129 //
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001130 updateMachineCode();
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001131
Chris Lattner045e7c82001-09-19 16:26:23 +00001132 if (DEBUG_RA) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001133 cerr << "\n**** Machine Code After Register Allocation:\n\n";
Vikram S. Adve12af1642001-11-08 04:48:50 +00001134 MachineCodeForMethod::get(Meth).dump();
Chris Lattner045e7c82001-09-19 16:26:23 +00001135 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001136}
1137
Ruchira Sasankae727f852001-09-18 22:43:57 +00001138
1139