blob: 85c55d6fbc7676e0e247d94ce4e2e56ce2d4e7d1 [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"
Misha Brukmanfce11432002-10-28 00:28:31 +000012#include "llvm/CodeGen/MachineFunction.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"
Vikram S. Advef5af6362002-07-08 23:15:32 +000020#include "Support/STLExtras.h"
Chris Lattner4bc23482002-09-15 07:07:55 +000021#include "Support/CommandLine.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),
Misha Brukmanfce11432002-10-28 00:28:31 +000080 mcInfo(MachineFunction::get(F)),
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000081 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 //
Chris Lattner32be9f62002-10-28 01:41:27 +0000277 const MachineBasicBlock& MIVec = MachineBasicBlock::get(BBI);
278 MachineBasicBlock::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. Advecb202e32002-10-11 16:12:40 +0000421InsertBefore(MachineInstr* newMI,
Chris Lattner32be9f62002-10-28 01:41:27 +0000422 MachineBasicBlock& MIVec,
423 MachineBasicBlock::iterator& MII)
Vikram S. Advecb202e32002-10-11 16:12:40 +0000424{
425 MII = MIVec.insert(MII, newMI);
426 ++MII;
427}
428
429inline void
430InsertAfter(MachineInstr* newMI,
Chris Lattner32be9f62002-10-28 01:41:27 +0000431 MachineBasicBlock& MIVec,
432 MachineBasicBlock::iterator& MII)
Vikram S. Advecb202e32002-10-11 16:12:40 +0000433{
434 ++MII; // insert before the next instruction
435 MII = MIVec.insert(MII, newMI);
436}
437
438inline void
439SubstituteInPlace(MachineInstr* newMI,
Chris Lattner32be9f62002-10-28 01:41:27 +0000440 MachineBasicBlock& MIVec,
441 MachineBasicBlock::iterator MII)
Vikram S. Advecb202e32002-10-11 16:12:40 +0000442{
443 *MII = newMI;
444}
445
446inline void
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000447PrependInstructions(vector<MachineInstr *> &IBef,
Chris Lattner32be9f62002-10-28 01:41:27 +0000448 MachineBasicBlock& MIVec,
449 MachineBasicBlock::iterator& MII,
Vikram S. Adve48762092002-04-25 04:34:15 +0000450 const std::string& msg)
451{
452 if (!IBef.empty())
453 {
454 MachineInstr* OrigMI = *MII;
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000455 std::vector<MachineInstr *>::iterator AdIt;
Vikram S. Adve48762092002-04-25 04:34:15 +0000456 for (AdIt = IBef.begin(); AdIt != IBef.end() ; ++AdIt)
457 {
458 if (DEBUG_RA) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000459 if (OrigMI) cerr << "For MInst:\n " << *OrigMI;
460 cerr << msg << "PREPENDed instr:\n " << **AdIt << "\n";
Vikram S. Adve48762092002-04-25 04:34:15 +0000461 }
Vikram S. Advecb202e32002-10-11 16:12:40 +0000462 InsertBefore(*AdIt, MIVec, MII);
Vikram S. Adve48762092002-04-25 04:34:15 +0000463 }
464 }
465}
466
467inline void
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000468AppendInstructions(std::vector<MachineInstr *> &IAft,
Chris Lattner32be9f62002-10-28 01:41:27 +0000469 MachineBasicBlock& MIVec,
470 MachineBasicBlock::iterator& MII,
Vikram S. Adve48762092002-04-25 04:34:15 +0000471 const std::string& msg)
472{
473 if (!IAft.empty())
474 {
475 MachineInstr* OrigMI = *MII;
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000476 std::vector<MachineInstr *>::iterator AdIt;
Chris Lattner7e708292002-06-25 16:13:24 +0000477 for ( AdIt = IAft.begin(); AdIt != IAft.end() ; ++AdIt )
Vikram S. Adve48762092002-04-25 04:34:15 +0000478 {
Chris Lattner7e708292002-06-25 16:13:24 +0000479 if (DEBUG_RA) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000480 if (OrigMI) cerr << "For MInst:\n " << *OrigMI;
481 cerr << msg << "APPENDed instr:\n " << **AdIt << "\n";
Vikram S. Adve48762092002-04-25 04:34:15 +0000482 }
Vikram S. Advecb202e32002-10-11 16:12:40 +0000483 InsertAfter(*AdIt, MIVec, MII);
Vikram S. Adve48762092002-04-25 04:34:15 +0000484 }
485 }
486}
487
488
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000489void PhyRegAlloc::updateMachineCode()
490{
Chris Lattner32be9f62002-10-28 01:41:27 +0000491 MachineBasicBlock& MIVec = MachineBasicBlock::get(&Meth->getEntryNode());
Vikram S. Adve48762092002-04-25 04:34:15 +0000492
Chris Lattner7e708292002-06-25 16:13:24 +0000493 // Insert any instructions needed at method entry
Chris Lattner32be9f62002-10-28 01:41:27 +0000494 MachineBasicBlock::iterator MII = MIVec.begin();
Chris Lattner7e708292002-06-25 16:13:24 +0000495 PrependInstructions(AddedInstrAtEntry.InstrnsBefore, MIVec, MII,
496 "At function entry: \n");
497 assert(AddedInstrAtEntry.InstrnsAfter.empty() &&
498 "InstrsAfter should be unnecessary since we are just inserting at "
499 "the function entry point here.");
Vikram S. Adve48762092002-04-25 04:34:15 +0000500
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000501 for (Function::const_iterator BBI = Meth->begin(), BBE = Meth->end();
502 BBI != BBE; ++BBI) {
Vikram S. Advecb202e32002-10-11 16:12:40 +0000503
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000504 // iterate over all the machine instructions in BB
Chris Lattner32be9f62002-10-28 01:41:27 +0000505 MachineBasicBlock &MIVec = MachineBasicBlock::get(BBI);
506 for (MachineBasicBlock::iterator MII = MIVec.begin();
Vikram S. Adve48762092002-04-25 04:34:15 +0000507 MII != MIVec.end(); ++MII) {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000508
Vikram S. Adve48762092002-04-25 04:34:15 +0000509 MachineInstr *MInst = *MII;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000510
511 unsigned Opcode = MInst->getOpCode();
512
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000513 // do not process Phis
Vikram S. Adve23a4c8f2002-03-18 03:37:19 +0000514 if (TM.getInstrInfo().isDummyPhiInstr(Opcode))
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000515 continue;
516
Vikram S. Advef5af6362002-07-08 23:15:32 +0000517 // Reset tmp stack positions so they can be reused for each machine instr.
518 mcInfo.popAllTempValues(TM);
519
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000520 // Now insert speical instructions (if necessary) for call/return
521 // instructions.
522 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000523 if (TM.getInstrInfo().isCall(Opcode) ||
524 TM.getInstrInfo().isReturn(Opcode)) {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000525
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000526 AddedInstrns &AI = AddedInstrMap[MInst];
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000527
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000528 if (TM.getInstrInfo().isCall(Opcode))
Chris Lattner7e708292002-06-25 16:13:24 +0000529 MRI.colorCallArgs(MInst, LRI, &AI, *this, BBI);
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000530 else if (TM.getInstrInfo().isReturn(Opcode))
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000531 MRI.colorRetValue(MInst, LRI, &AI);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000532 }
533
Vikram S. Advef5af6362002-07-08 23:15:32 +0000534 // Set the registers for operands in the machine instruction
535 // if a register was successfully allocated. If not, insert
536 // code to spill the register value.
537 //
538 for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum)
539 {
540 MachineOperand& Op = MInst->getOperand(OpNum);
541 if (Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
542 Op.getOperandType() == MachineOperand::MO_CCRegister)
543 {
544 const Value *const Val = Op.getVRegValue();
545
546 LiveRange *const LR = LRI.getLiveRangeForValue(Val);
547 if (!LR) // consts or labels will have no live range
548 {
549 // if register is not allocated, mark register as invalid
550 if (Op.getAllocatedRegNum() == -1)
551 MInst->SetRegForOperand(OpNum, MRI.getInvalidRegNum());
552 continue;
553 }
554
555 if (LR->hasColor() )
556 MInst->SetRegForOperand(OpNum,
557 MRI.getUnifiedRegNum(LR->getRegClass()->getID(),
558 LR->getColor()));
559 else
560 // LR did NOT receive a color (register). Insert spill code.
561 insertCode4SpilledLR(LR, MInst, BBI, OpNum );
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000562 }
Vikram S. Advef5af6362002-07-08 23:15:32 +0000563 } // for each operand
Vikram S. Advecb202e32002-10-11 16:12:40 +0000564
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000565 // Now add instructions that the register allocator inserts before/after
566 // this machine instructions (done only for calls/rets/incoming args)
567 // We do this here, to ensure that spill for an instruction is inserted
568 // closest as possible to an instruction (see above insertCode4Spill...)
569 //
Vikram S. Advecb202e32002-10-11 16:12:40 +0000570 // First, if the instruction in the delay slot of a branch needs
571 // instructions inserted, move it out of the delay slot and before the
572 // branch because putting code before or after it would be VERY BAD!
573 //
574 unsigned bumpIteratorBy = 0;
575 if (MII != MIVec.begin())
576 if (unsigned predDelaySlots =
577 TM.getInstrInfo().getNumDelaySlots((*(MII-1))->getOpCode()))
578 {
579 assert(predDelaySlots==1 && "Not handling multiple delay slots!");
580 if (TM.getInstrInfo().isBranch((*(MII-1))->getOpCode())
581 && (AddedInstrMap.count(MInst) ||
582 AddedInstrMap[MInst].InstrnsAfter.size() > 0))
583 {
584 // Current instruction is in the delay slot of a branch and it
585 // needs spill code inserted before or after it.
586 // Move it before the preceding branch.
587 InsertBefore(MInst, MIVec, --MII);
588 MachineInstr* nopI =
589 new MachineInstr(TM.getInstrInfo().getNOPOpCode());
590 SubstituteInPlace(nopI, MIVec, MII+1); // replace orig with NOP
591 --MII; // point to MInst in new location
592 bumpIteratorBy = 2; // later skip the branch and the NOP!
593 }
594 }
595
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000596 // If there are instructions to be added, *before* this machine
597 // instruction, add them now.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000598 //
Chris Lattner7e708292002-06-25 16:13:24 +0000599 if (AddedInstrMap.count(MInst)) {
Vikram S. Adve48762092002-04-25 04:34:15 +0000600 PrependInstructions(AddedInstrMap[MInst].InstrnsBefore, MIVec, MII,"");
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000601 }
Vikram S. Adve48762092002-04-25 04:34:15 +0000602
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000603 // If there are instructions to be added *after* this machine
604 // instruction, add them now
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000605 //
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000606 if (!AddedInstrMap[MInst].InstrnsAfter.empty()) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000607
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000608 // if there are delay slots for this instruction, the instructions
609 // added after it must really go after the delayed instruction(s)
610 // So, we move the InstrAfter of the current instruction to the
611 // corresponding delayed instruction
Vikram S. Advecb202e32002-10-11 16:12:40 +0000612 if (unsigned delay =
613 TM.getInstrInfo().getNumDelaySlots(MInst->getOpCode())) {
614
615 // Delayed instructions are typically branches or calls. Let's make
616 // sure this is not a branch, otherwise "insert-after" is meaningless,
617 // and should never happen for any reason (spill code, register
618 // restores, etc.).
619 assert(! TM.getInstrInfo().isBranch(MInst->getOpCode()) &&
620 ! TM.getInstrInfo().isReturn(MInst->getOpCode()) &&
621 "INTERNAL ERROR: Register allocator should not be inserting "
622 "any code after a branch or return!");
623
Vikram S. Adve48762092002-04-25 04:34:15 +0000624 move2DelayedInstr(MInst, *(MII+delay) );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000625 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000626 else {
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000627 // Here we can add the "instructions after" to the current
628 // instruction since there are no delay slots for this instruction
Vikram S. Adve48762092002-04-25 04:34:15 +0000629 AppendInstructions(AddedInstrMap[MInst].InstrnsAfter, MIVec, MII,"");
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000630 } // if not delay
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000631 }
Vikram S. Advecb202e32002-10-11 16:12:40 +0000632
633 // If we mucked with the instruction order above, adjust the loop iterator
634 if (bumpIteratorBy)
635 MII = MII + bumpIteratorBy;
636
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000637 } // for each machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000638 }
639}
640
641
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000642
643//----------------------------------------------------------------------------
644// This method inserts spill code for AN operand whose LR was spilled.
645// This method may be called several times for a single machine instruction
646// if it contains many spilled operands. Each time it is called, it finds
647// a register which is not live at that instruction and also which is not
648// used by other spilled operands of the same instruction. Then it uses
649// this register temporarily to accomodate the spilled value.
650//----------------------------------------------------------------------------
651void PhyRegAlloc::insertCode4SpilledLR(const LiveRange *LR,
652 MachineInstr *MInst,
653 const BasicBlock *BB,
654 const unsigned OpNum) {
655
Vikram S. Advead9c9782002-09-28 17:02:40 +0000656 assert((! TM.getInstrInfo().isCall(MInst->getOpCode()) || OpNum == 0) &&
657 "Outgoing arg of a call must be handled elsewhere (func arg ok)");
658 assert(! TM.getInstrInfo().isReturn(MInst->getOpCode()) &&
659 "Return value of a ret must be handled elsewhere");
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000660
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000661 MachineOperand& Op = MInst->getOperand(OpNum);
662 bool isDef = MInst->operandIsDefined(OpNum);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000663 bool isDefAndUse = MInst->operandIsDefinedAndUsed(OpNum);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000664 unsigned RegType = MRI.getRegType( LR );
665 int SpillOff = LR->getSpillOffFromFP();
666 RegClass *RC = LR->getRegClass();
Chris Lattner748697d2002-02-05 04:20:12 +0000667 const ValueSet &LVSetBef = LVI->getLiveVarSetBeforeMInst(MInst, BB);
Vikram S. Adve00521d72001-11-12 23:26:35 +0000668
Chris Lattner697954c2002-01-20 22:54:45 +0000669 mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000670
Vikram S. Advef5af6362002-07-08 23:15:32 +0000671 vector<MachineInstr*> MIBef, MIAft;
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000672 vector<MachineInstr*> AdIMid;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000673
Vikram S. Advef5af6362002-07-08 23:15:32 +0000674 // Choose a register to hold the spilled value. This may insert code
675 // before and after MInst to free up the value. If so, this code should
676 // be first and last in the spill sequence before/after MInst.
677 int TmpRegU = getUsableUniRegAtMI(RegType, &LVSetBef, MInst, MIBef, MIAft);
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000678
Vikram S. Advef5af6362002-07-08 23:15:32 +0000679 // Set the operand first so that it this register does not get used
680 // as a scratch register for later calls to getUsableUniRegAtMI below
681 MInst->SetRegForOperand(OpNum, TmpRegU);
682
683 // get the added instructions for this instruction
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000684 AddedInstrns &AI = AddedInstrMap[MInst];
Vikram S. Advef5af6362002-07-08 23:15:32 +0000685
686 // We may need a scratch register to copy the spilled value to/from memory.
687 // This may itself have to insert code to free up a scratch register.
688 // Any such code should go before (after) the spill code for a load (store).
689 int scratchRegType = -1;
690 int scratchReg = -1;
691 if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType))
692 {
Chris Lattner27a08932002-10-22 23:16:21 +0000693 scratchReg = getUsableUniRegAtMI(scratchRegType, &LVSetBef,
694 MInst, MIBef, MIAft);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000695 assert(scratchReg != MRI.getInvalidRegNum());
Chris Lattner27a08932002-10-22 23:16:21 +0000696 MInst->insertUsedReg(scratchReg);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000697 }
698
699 if (!isDef || isDefAndUse) {
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000700 // for a USE, we have to load the value of LR from stack to a TmpReg
701 // and use the TmpReg as one operand of instruction
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000702
Vikram S. Advef5af6362002-07-08 23:15:32 +0000703 // actual loading instruction(s)
704 MRI.cpMem2RegMI(AdIMid, MRI.getFramePointer(), SpillOff, TmpRegU, RegType,
705 scratchReg);
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000706
Vikram S. Advef5af6362002-07-08 23:15:32 +0000707 // the actual load should be after the instructions to free up TmpRegU
708 MIBef.insert(MIBef.end(), AdIMid.begin(), AdIMid.end());
709 AdIMid.clear();
710 }
711
712 if (isDef) { // if this is a Def
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000713 // for a DEF, we have to store the value produced by this instruction
714 // on the stack position allocated for this LR
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000715
Vikram S. Advef5af6362002-07-08 23:15:32 +0000716 // actual storing instruction(s)
717 MRI.cpReg2MemMI(AdIMid, TmpRegU, MRI.getFramePointer(), SpillOff, RegType,
718 scratchReg);
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000719
Vikram S. Advef5af6362002-07-08 23:15:32 +0000720 MIAft.insert(MIAft.begin(), AdIMid.begin(), AdIMid.end());
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000721 } // if !DEF
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000722
Vikram S. Advef5af6362002-07-08 23:15:32 +0000723 // Finally, insert the entire spill code sequences before/after MInst
724 AI.InstrnsBefore.insert(AI.InstrnsBefore.end(), MIBef.begin(), MIBef.end());
725 AI.InstrnsAfter.insert(AI.InstrnsAfter.begin(), MIAft.begin(), MIAft.end());
726
Chris Lattner7e708292002-06-25 16:13:24 +0000727 if (DEBUG_RA) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000728 cerr << "\nFor Inst:\n " << *MInst;
729 cerr << "SPILLED LR# " << LR->getUserIGNode()->getIndex();
730 cerr << "; added Instructions:";
Anand Shuklad58290e2002-07-09 19:18:56 +0000731 for_each(MIBef.begin(), MIBef.end(), std::mem_fun(&MachineInstr::dump));
732 for_each(MIAft.begin(), MIAft.end(), std::mem_fun(&MachineInstr::dump));
Chris Lattner7e708292002-06-25 16:13:24 +0000733 }
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000734}
735
736
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000737//----------------------------------------------------------------------------
738// We can use the following method to get a temporary register to be used
739// BEFORE any given machine instruction. If there is a register available,
740// this method will simply return that register and set MIBef = MIAft = NULL.
741// Otherwise, it will return a register and MIAft and MIBef will contain
742// two instructions used to free up this returned register.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000743// Returned register number is the UNIFIED register number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000744//----------------------------------------------------------------------------
745
Vikram S. Advef5af6362002-07-08 23:15:32 +0000746int PhyRegAlloc::getUsableUniRegAtMI(const int RegType,
747 const ValueSet *LVSetBef,
748 MachineInstr *MInst,
749 std::vector<MachineInstr*>& MIBef,
750 std::vector<MachineInstr*>& MIAft) {
751
752 RegClass* RC = this->getRegClassByID(MRI.getRegClassIDOfRegType(RegType));
753
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000754 int RegU = getUnusedUniRegAtMI(RC, MInst, LVSetBef);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000755
756 if (RegU == -1) {
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000757 // we couldn't find an unused register. Generate code to free up a reg by
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000758 // saving it on stack and restoring after the instruction
Vikram S. Advef5af6362002-07-08 23:15:32 +0000759
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000760 int TmpOff = mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
Vikram S. Adve12af1642001-11-08 04:48:50 +0000761
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000762 RegU = getUniRegNotUsedByThisInst(RC, MInst);
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000763
Vikram S. Advef5af6362002-07-08 23:15:32 +0000764 // Check if we need a scratch register to copy this register to memory.
765 int scratchRegType = -1;
766 if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType))
767 {
768 int scratchReg = this->getUsableUniRegAtMI(scratchRegType, LVSetBef,
769 MInst, MIBef, MIAft);
770 assert(scratchReg != MRI.getInvalidRegNum());
771
772 // We may as well hold the value in the scratch register instead
773 // of copying it to memory and back. But we have to mark the
774 // register as used by this instruction, so it does not get used
775 // as a scratch reg. by another operand or anyone else.
Chris Lattner27a08932002-10-22 23:16:21 +0000776 MInst->insertUsedReg(scratchReg);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000777 MRI.cpReg2RegMI(MIBef, RegU, scratchReg, RegType);
778 MRI.cpReg2RegMI(MIAft, scratchReg, RegU, RegType);
779 }
780 else
781 { // the register can be copied directly to/from memory so do it.
782 MRI.cpReg2MemMI(MIBef, RegU, MRI.getFramePointer(), TmpOff, RegType);
783 MRI.cpMem2RegMI(MIAft, MRI.getFramePointer(), TmpOff, RegU, RegType);
784 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000785 }
Vikram S. Advef5af6362002-07-08 23:15:32 +0000786
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000787 return RegU;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000788}
789
790//----------------------------------------------------------------------------
791// This method is called to get a new unused register that can be used to
792// accomodate a spilled value.
793// This method may be called several times for a single machine instruction
794// if it contains many spilled operands. Each time it is called, it finds
795// a register which is not live at that instruction and also which is not
796// used by other spilled operands of the same instruction.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000797// Return register number is relative to the register class. NOT
798// unified number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000799//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000800int PhyRegAlloc::getUnusedUniRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000801 const MachineInstr *MInst,
Chris Lattner296b7732002-02-05 02:52:05 +0000802 const ValueSet *LVSetBef) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000803
804 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
805
Chris Lattner85c54652002-05-23 15:50:03 +0000806 std::vector<bool> &IsColorUsedArr = RC->getIsColorUsedArr();
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000807
Chris Lattner7e708292002-06-25 16:13:24 +0000808 for (unsigned i=0; i < NumAvailRegs; i++) // Reset array
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000809 IsColorUsedArr[i] = false;
810
Chris Lattner296b7732002-02-05 02:52:05 +0000811 ValueSet::const_iterator LIt = LVSetBef->begin();
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000812
813 // for each live var in live variable set after machine inst
Chris Lattner7e708292002-06-25 16:13:24 +0000814 for ( ; LIt != LVSetBef->end(); ++LIt) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000815
816 // get the live range corresponding to live var
817 LiveRange *const LRofLV = LRI.getLiveRangeForValue(*LIt );
818
819 // LR can be null if it is a const since a const
820 // doesn't have a dominating def - see Assumptions above
Chris Lattner7e708292002-06-25 16:13:24 +0000821 if (LRofLV && LRofLV->getRegClass() == RC && LRofLV->hasColor() )
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000822 IsColorUsedArr[ LRofLV->getColor() ] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000823 }
824
825 // It is possible that one operand of this MInst was already spilled
826 // and it received some register temporarily. If that's the case,
827 // it is recorded in machine operand. We must skip such registers.
828
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000829 setRelRegsUsedByThisInst(RC, MInst);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000830
Chris Lattner7e708292002-06-25 16:13:24 +0000831 for (unsigned c=0; c < NumAvailRegs; c++) // find first unused color
Chris Lattner85c54652002-05-23 15:50:03 +0000832 if (!IsColorUsedArr[c])
833 return MRI.getUnifiedRegNum(RC->getID(), c);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000834
Chris Lattner85c54652002-05-23 15:50:03 +0000835 return -1;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000836}
837
838
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000839//----------------------------------------------------------------------------
840// Get any other register in a register class, other than what is used
841// by operands of a machine instruction. Returns the unified reg number.
842//----------------------------------------------------------------------------
843int PhyRegAlloc::getUniRegNotUsedByThisInst(RegClass *RC,
Chris Lattner85c54652002-05-23 15:50:03 +0000844 const MachineInstr *MInst) {
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000845
Chris Lattner85c54652002-05-23 15:50:03 +0000846 vector<bool> &IsColorUsedArr = RC->getIsColorUsedArr();
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000847 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
848
Chris Lattner7e708292002-06-25 16:13:24 +0000849 for (unsigned i=0; i < NumAvailRegs ; i++) // Reset array
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000850 IsColorUsedArr[i] = false;
851
852 setRelRegsUsedByThisInst(RC, MInst);
853
Chris Lattner7e708292002-06-25 16:13:24 +0000854 for (unsigned c=0; c < RC->getNumOfAvailRegs(); c++)// find first unused color
Chris Lattner85c54652002-05-23 15:50:03 +0000855 if (!IsColorUsedArr[c])
856 return MRI.getUnifiedRegNum(RC->getID(), c);
857
858 assert(0 && "FATAL: No free register could be found in reg class!!");
Chris Lattner697954c2002-01-20 22:54:45 +0000859 return 0;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000860}
861
862
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000863//----------------------------------------------------------------------------
864// This method modifies the IsColorUsedArr of the register class passed to it.
865// It sets the bits corresponding to the registers used by this machine
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000866// instructions. Both explicit and implicit operands are set.
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000867//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000868void PhyRegAlloc::setRelRegsUsedByThisInst(RegClass *RC,
Vikram S. Advef5af6362002-07-08 23:15:32 +0000869 const MachineInstr *MInst ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000870
Vikram S. Advef5af6362002-07-08 23:15:32 +0000871 vector<bool> &IsColorUsedArr = RC->getIsColorUsedArr();
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000872
Vikram S. Advef5af6362002-07-08 23:15:32 +0000873 // Add the registers already marked as used by the instruction.
874 // This should include any scratch registers that are used to save
875 // values across the instruction (e.g., for saving state register values).
Chris Lattner27a08932002-10-22 23:16:21 +0000876 const vector<bool> &regsUsed = MInst->getRegsUsed();
877 for (unsigned i = 0, e = regsUsed.size(); i != e; ++i)
878 if (regsUsed[i]) {
Vikram S. Advef5af6362002-07-08 23:15:32 +0000879 unsigned classId = 0;
Chris Lattner27a08932002-10-22 23:16:21 +0000880 int classRegNum = MRI.getClassRegNum(i, classId);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000881 if (RC->getID() == classId)
882 {
883 assert(classRegNum < (int) IsColorUsedArr.size() &&
884 "Illegal register number for this reg class?");
885 IsColorUsedArr[classRegNum] = true;
886 }
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000887 }
Vikram S. Advef5af6362002-07-08 23:15:32 +0000888
889 // Now add registers allocated to the live ranges of values used in
890 // the instruction. These are not yet recorded in the instruction.
891 for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum)
892 {
893 const MachineOperand& Op = MInst->getOperand(OpNum);
894
895 if (Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
896 Op.getOperandType() == MachineOperand::MO_CCRegister)
897 if (const Value* Val = Op.getVRegValue())
898 if (MRI.getRegClassIDOfValue(Val) == RC->getID())
899 if (Op.getAllocatedRegNum() == -1)
900 if (LiveRange *LROfVal = LRI.getLiveRangeForValue(Val))
901 if (LROfVal->hasColor() )
902 // this operand is in a LR that received a color
903 IsColorUsedArr[LROfVal->getColor()] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000904 }
Vikram S. Advef5af6362002-07-08 23:15:32 +0000905
906 // If there are implicit references, mark their allocated regs as well
907 //
908 for (unsigned z=0; z < MInst->getNumImplicitRefs(); z++)
909 if (const LiveRange*
910 LRofImpRef = LRI.getLiveRangeForValue(MInst->getImplicitRef(z)))
911 if (LRofImpRef->hasColor())
912 // this implicit reference is in a LR that received a color
913 IsColorUsedArr[LRofImpRef->getColor()] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000914}
915
916
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000917//----------------------------------------------------------------------------
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000918// If there are delay slots for an instruction, the instructions
919// added after it must really go after the delayed instruction(s).
920// So, we move the InstrAfter of that instruction to the
921// corresponding delayed instruction using the following method.
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000922
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000923//----------------------------------------------------------------------------
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000924void PhyRegAlloc::move2DelayedInstr(const MachineInstr *OrigMI,
925 const MachineInstr *DelayedMI) {
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000926
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000927 // "added after" instructions of the original instr
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000928 std::vector<MachineInstr *> &OrigAft = AddedInstrMap[OrigMI].InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000929
930 // "added instructions" of the delayed instr
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000931 AddedInstrns &DelayAdI = AddedInstrMap[DelayedMI];
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000932
933 // "added after" instructions of the delayed instr
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000934 std::vector<MachineInstr *> &DelayedAft = DelayAdI.InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000935
936 // go thru all the "added after instructions" of the original instruction
937 // and append them to the "addded after instructions" of the delayed
938 // instructions
Chris Lattner697954c2002-01-20 22:54:45 +0000939 DelayedAft.insert(DelayedAft.end(), OrigAft.begin(), OrigAft.end());
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000940
941 // empty the "added after instructions" of the original instruction
942 OrigAft.clear();
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000943}
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000944
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000945//----------------------------------------------------------------------------
946// This method prints the code with registers after register allocation is
947// complete.
948//----------------------------------------------------------------------------
949void PhyRegAlloc::printMachineCode()
950{
951
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000952 cerr << "\n;************** Function " << Meth->getName()
Chris Lattner697954c2002-01-20 22:54:45 +0000953 << " *****************\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000954
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000955 for (Function::const_iterator BBI = Meth->begin(), BBE = Meth->end();
956 BBI != BBE; ++BBI) {
Chris Lattner7e708292002-06-25 16:13:24 +0000957 cerr << "\n"; printLabel(BBI); cerr << ": ";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000958
959 // get the iterator for machine instructions
Chris Lattner32be9f62002-10-28 01:41:27 +0000960 MachineBasicBlock& MIVec = MachineBasicBlock::get(BBI);
961 MachineBasicBlock::iterator MII = MIVec.begin();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000962
963 // iterate over all the machine instructions in BB
Chris Lattner7e708292002-06-25 16:13:24 +0000964 for ( ; MII != MIVec.end(); ++MII) {
Vikram S. Adve48762092002-04-25 04:34:15 +0000965 MachineInstr *const MInst = *MII;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000966
Chris Lattner697954c2002-01-20 22:54:45 +0000967 cerr << "\n\t";
968 cerr << TargetInstrDescriptors[MInst->getOpCode()].opCodeString;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000969
Chris Lattner7e708292002-06-25 16:13:24 +0000970 for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000971 MachineOperand& Op = MInst->getOperand(OpNum);
972
Chris Lattner7e708292002-06-25 16:13:24 +0000973 if (Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasanka97b8b442001-10-18 22:36:26 +0000974 Op.getOperandType() == MachineOperand::MO_CCRegister /*||
975 Op.getOperandType() == MachineOperand::MO_PCRelativeDisp*/ ) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000976
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000977 const Value *const Val = Op.getVRegValue () ;
Ruchira Sasankae727f852001-09-18 22:43:57 +0000978 // ****this code is temporary till NULL Values are fixed
Chris Lattner7e708292002-06-25 16:13:24 +0000979 if (! Val ) {
Chris Lattner697954c2002-01-20 22:54:45 +0000980 cerr << "\t<*NULL*>";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000981 continue;
982 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000983
984 // if a label or a constant
Chris Lattner7e708292002-06-25 16:13:24 +0000985 if (isa<BasicBlock>(Val)) {
Chris Lattner697954c2002-01-20 22:54:45 +0000986 cerr << "\t"; printLabel( Op.getVRegValue () );
987 } else {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000988 // else it must be a register value
989 const int RegNum = Op.getAllocatedRegNum();
990
Chris Lattner697954c2002-01-20 22:54:45 +0000991 cerr << "\t" << "%" << MRI.getUnifiedRegName( RegNum );
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000992 if (Val->hasName() )
Chris Lattner697954c2002-01-20 22:54:45 +0000993 cerr << "(" << Val->getName() << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000994 else
Chris Lattner697954c2002-01-20 22:54:45 +0000995 cerr << "(" << Val << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000996
Chris Lattner7e708292002-06-25 16:13:24 +0000997 if (Op.opIsDef() )
Chris Lattner697954c2002-01-20 22:54:45 +0000998 cerr << "*";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000999
1000 const LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
Chris Lattner7e708292002-06-25 16:13:24 +00001001 if (LROfVal )
1002 if (LROfVal->hasSpillOffset() )
Chris Lattner697954c2002-01-20 22:54:45 +00001003 cerr << "$";
Ruchira Sasankae727f852001-09-18 22:43:57 +00001004 }
1005
1006 }
Chris Lattner7e708292002-06-25 16:13:24 +00001007 else if (Op.getOperandType() == MachineOperand::MO_MachineRegister) {
Chris Lattner697954c2002-01-20 22:54:45 +00001008 cerr << "\t" << "%" << MRI.getUnifiedRegName(Op.getMachineRegNum());
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001009 }
1010
1011 else
Chris Lattner697954c2002-01-20 22:54:45 +00001012 cerr << "\t" << Op; // use dump field
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001013 }
1014
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001015
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001016
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001017 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
Chris Lattner7e708292002-06-25 16:13:24 +00001018 if (NumOfImpRefs > 0) {
Chris Lattner697954c2002-01-20 22:54:45 +00001019 cerr << "\tImplicit:";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001020
Chris Lattner7e708292002-06-25 16:13:24 +00001021 for (unsigned z=0; z < NumOfImpRefs; z++)
Chris Lattner0665a5f2002-02-05 01:43:49 +00001022 cerr << RAV(MInst->getImplicitRef(z)) << "\t";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001023 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001024
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001025 } // for all machine instructions
1026
Chris Lattner697954c2002-01-20 22:54:45 +00001027 cerr << "\n";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001028
1029 } // for all BBs
1030
Chris Lattner697954c2002-01-20 22:54:45 +00001031 cerr << "\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001032}
1033
Ruchira Sasankae727f852001-09-18 22:43:57 +00001034
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001035//----------------------------------------------------------------------------
1036
1037//----------------------------------------------------------------------------
1038void PhyRegAlloc::colorIncomingArgs()
1039{
Chris Lattner7e708292002-06-25 16:13:24 +00001040 const BasicBlock &FirstBB = Meth->front();
Chris Lattner32be9f62002-10-28 01:41:27 +00001041 const MachineInstr *FirstMI = MachineBasicBlock::get(&FirstBB).front();
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001042 assert(FirstMI && "No machine instruction in entry BB");
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001043
Vikram S. Adve48762092002-04-25 04:34:15 +00001044 MRI.colorMethodArgs(Meth, LRI, &AddedInstrAtEntry);
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001045}
1046
Ruchira Sasankae727f852001-09-18 22:43:57 +00001047
1048//----------------------------------------------------------------------------
1049// Used to generate a label for a basic block
1050//----------------------------------------------------------------------------
Chris Lattner697954c2002-01-20 22:54:45 +00001051void PhyRegAlloc::printLabel(const Value *const Val) {
1052 if (Val->hasName())
1053 cerr << Val->getName();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001054 else
Chris Lattner697954c2002-01-20 22:54:45 +00001055 cerr << "Label" << Val;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001056}
1057
1058
Ruchira Sasankae727f852001-09-18 22:43:57 +00001059//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001060// This method calls setSugColorUsable method of each live range. This
1061// will determine whether the suggested color of LR is really usable.
1062// A suggested color is not usable when the suggested color is volatile
1063// AND when there are call interferences
1064//----------------------------------------------------------------------------
1065
1066void PhyRegAlloc::markUnusableSugColors()
1067{
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001068 // hash map iterator
1069 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
1070 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
1071
Chris Lattner7e708292002-06-25 16:13:24 +00001072 for (; HMI != HMIEnd ; ++HMI ) {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001073 if (HMI->first) {
1074 LiveRange *L = HMI->second; // get the LiveRange
1075 if (L) {
Chris Lattner7e708292002-06-25 16:13:24 +00001076 if (L->hasSuggestedColor()) {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001077 int RCID = L->getRegClass()->getID();
Chris Lattner7e708292002-06-25 16:13:24 +00001078 if (MRI.isRegVolatile( RCID, L->getSuggestedColor()) &&
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001079 L->isCallInterference() )
1080 L->setSuggestedColorUsable( false );
1081 else
1082 L->setSuggestedColorUsable( true );
1083 }
1084 } // if L->hasSuggestedColor()
1085 }
1086 } // for all LR's in hash map
1087}
1088
1089
1090
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001091//----------------------------------------------------------------------------
1092// The following method will set the stack offsets of the live ranges that
1093// are decided to be spillled. This must be called just after coloring the
1094// LRs using the graph coloring algo. For each live range that is spilled,
1095// this method allocate a new spill position on the stack.
1096//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001097
Chris Lattner37730942002-02-05 03:52:29 +00001098void PhyRegAlloc::allocateStackSpace4SpilledLRs() {
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001099 if (DEBUG_RA) cerr << "\nSetting LR stack offsets for spills...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001100
Chris Lattner37730942002-02-05 03:52:29 +00001101 LiveRangeMapType::const_iterator HMI = LRI.getLiveRangeMap()->begin();
1102 LiveRangeMapType::const_iterator HMIEnd = LRI.getLiveRangeMap()->end();
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001103
Chris Lattner7e708292002-06-25 16:13:24 +00001104 for ( ; HMI != HMIEnd ; ++HMI) {
Chris Lattner37730942002-02-05 03:52:29 +00001105 if (HMI->first && HMI->second) {
1106 LiveRange *L = HMI->second; // get the LiveRange
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001107 if (!L->hasColor()) { // NOTE: ** allocating the size of long Type **
1108 int stackOffset = mcInfo.allocateSpilledValue(TM, Type::LongTy);
1109 L->setSpillOffFromFP(stackOffset);
1110 if (DEBUG_RA)
1111 cerr << " LR# " << L->getUserIGNode()->getIndex()
1112 << ": stack-offset = " << stackOffset << "\n";
1113 }
Chris Lattner37730942002-02-05 03:52:29 +00001114 }
1115 } // for all LR's in hash map
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001116}
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001117
1118
1119
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001120//----------------------------------------------------------------------------
Ruchira Sasankae727f852001-09-18 22:43:57 +00001121// The entry pont to Register Allocation
1122//----------------------------------------------------------------------------
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001123
1124void PhyRegAlloc::allocateRegisters()
1125{
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001126
1127 // make sure that we put all register classes into the RegClassList
1128 // before we call constructLiveRanges (now done in the constructor of
1129 // PhyRegAlloc class).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001130 //
1131 LRI.constructLiveRanges(); // create LR info
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001132
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001133 if (DEBUG_RA >= RA_DEBUG_LiveRanges)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001134 LRI.printLiveRanges();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001135
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001136 createIGNodeListsAndIGs(); // create IGNode list and IGs
1137
1138 buildInterferenceGraphs(); // build IGs in all reg classes
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001139
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001140
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001141 if (DEBUG_RA >= RA_DEBUG_LiveRanges) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001142 // print all LRs in all reg classes
Chris Lattner7e708292002-06-25 16:13:24 +00001143 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
1144 RegClassList[rc]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001145
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001146 // print IGs in all register classes
Chris Lattner7e708292002-06-25 16:13:24 +00001147 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
1148 RegClassList[rc]->printIG();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001149 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001150
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001151
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001152 LRI.coalesceLRs(); // coalesce all live ranges
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001153
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +00001154
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001155 if (DEBUG_RA >= RA_DEBUG_LiveRanges) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001156 // print all LRs in all reg classes
Chris Lattner7e708292002-06-25 16:13:24 +00001157 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001158 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001159
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001160 // print IGs in all register classes
Chris Lattner7e708292002-06-25 16:13:24 +00001161 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001162 RegClassList[ rc ]->printIG();
1163 }
1164
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001165
1166 // mark un-usable suggested color before graph coloring algorithm.
1167 // When this is done, the graph coloring algo will not reserve
1168 // suggested color unnecessarily - they can be used by another LR
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001169 //
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001170 markUnusableSugColors();
1171
1172 // color all register classes using the graph coloring algo
Chris Lattner7e708292002-06-25 16:13:24 +00001173 for (unsigned rc=0; rc < NumOfRegClasses ; rc++)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001174 RegClassList[ rc ]->colorAllRegs();
1175
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001176 // Atter grpah coloring, if some LRs did not receive a color (i.e, spilled)
1177 // a poistion for such spilled LRs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001178 //
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001179 allocateStackSpace4SpilledLRs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001180
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001181 mcInfo.popAllTempValues(TM); // TODO **Check
1182
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001183 // color incoming args - if the correct color was not received
1184 // insert code to copy to the correct register
1185 //
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001186 colorIncomingArgs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001187
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001188 // Now update the machine code with register names and add any
1189 // additional code inserted by the register allocator to the instruction
1190 // stream
1191 //
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001192 updateMachineCode();
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001193
Chris Lattner045e7c82001-09-19 16:26:23 +00001194 if (DEBUG_RA) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001195 cerr << "\n**** Machine Code After Register Allocation:\n\n";
Misha Brukmanfce11432002-10-28 00:28:31 +00001196 MachineFunction::get(Meth).dump();
Chris Lattner045e7c82001-09-19 16:26:23 +00001197 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001198}
1199
Ruchira Sasankae727f852001-09-18 22:43:57 +00001200
1201