blob: 9899dbc71cc070db9eeebd8f22363db31ec8d318 [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"
Misha Brukmanfce11432002-10-28 00:28:31 +000013#include "llvm/CodeGen/MachineFunction.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"
Chris Lattner4bc23482002-09-15 07:07:55 +000022#include "Support/CommandLine.h"
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000023#include <math.h>
Chris Lattner697954c2002-01-20 22:54:45 +000024using std::cerr;
Anand Shuklacfb22d32002-06-25 20:55:50 +000025using std::vector;
Vikram S. Adve12af1642001-11-08 04:48:50 +000026
Chris Lattner70e60cb2002-05-22 17:08:27 +000027RegAllocDebugLevel_t DEBUG_RA;
Vikram S. Adve39c94e12002-09-14 23:05:33 +000028
Chris Lattner5ff62e92002-07-22 02:10:13 +000029static cl::opt<RegAllocDebugLevel_t, true>
30DRA_opt("dregalloc", cl::Hidden, cl::location(DEBUG_RA),
31 cl::desc("enable register allocation debugging information"),
32 cl::values(
Vikram S. Adve39c94e12002-09-14 23:05:33 +000033 clEnumValN(RA_DEBUG_None , "n", "disable debug output"),
34 clEnumValN(RA_DEBUG_Results, "y", "debug output for allocation results"),
35 clEnumValN(RA_DEBUG_Coloring, "c", "debug output for graph coloring step"),
36 clEnumValN(RA_DEBUG_Interference,"ig","debug output for interference graphs"),
37 clEnumValN(RA_DEBUG_LiveRanges , "lr","debug output for live ranges"),
38 clEnumValN(RA_DEBUG_Verbose, "v", "extra debug output"),
Chris Lattner5ff62e92002-07-22 02:10:13 +000039 0));
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000040
Chris Lattner2f9b28e2002-02-04 15:54:09 +000041//----------------------------------------------------------------------------
42// RegisterAllocation pass front end...
43//----------------------------------------------------------------------------
44namespace {
Chris Lattnerf57b8452002-04-27 06:56:12 +000045 class RegisterAllocator : public FunctionPass {
Chris Lattner2f9b28e2002-02-04 15:54:09 +000046 TargetMachine &Target;
47 public:
48 inline RegisterAllocator(TargetMachine &T) : Target(T) {}
Chris Lattner96c466b2002-04-29 14:57:45 +000049
50 const char *getPassName() const { return "Register Allocation"; }
Chris Lattner6dd98a62002-02-04 00:33:08 +000051
Chris Lattner7e708292002-06-25 16:13:24 +000052 bool runOnFunction(Function &F) {
Chris Lattner2f9b28e2002-02-04 15:54:09 +000053 if (DEBUG_RA)
Chris Lattner7e708292002-06-25 16:13:24 +000054 cerr << "\n********* Function "<< F.getName() << " ***********\n";
Chris Lattner2f9b28e2002-02-04 15:54:09 +000055
Chris Lattner7e708292002-06-25 16:13:24 +000056 PhyRegAlloc PRA(&F, Target, &getAnalysis<FunctionLiveVarInfo>(),
Chris Lattner1b7f7dc2002-04-28 16:21:30 +000057 &getAnalysis<LoopInfo>());
Chris Lattner2f9b28e2002-02-04 15:54:09 +000058 PRA.allocateRegisters();
59
60 if (DEBUG_RA) cerr << "\nRegister allocation complete!\n";
61 return false;
62 }
Chris Lattner4911c352002-02-04 17:39:42 +000063
Chris Lattnerf57b8452002-04-27 06:56:12 +000064 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerdd5b4952002-08-08 19:01:28 +000065 AU.addRequired<LoopInfo>();
66 AU.addRequired<FunctionLiveVarInfo>();
Chris Lattner4911c352002-02-04 17:39:42 +000067 }
Chris Lattner2f9b28e2002-02-04 15:54:09 +000068 };
Chris Lattner6dd98a62002-02-04 00:33:08 +000069}
70
Chris Lattnerf57b8452002-04-27 06:56:12 +000071Pass *getRegisterAllocator(TargetMachine &T) {
Chris Lattner2f9b28e2002-02-04 15:54:09 +000072 return new RegisterAllocator(T);
73}
Chris Lattner6dd98a62002-02-04 00:33:08 +000074
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000075//----------------------------------------------------------------------------
76// Constructor: Init local composite objects and create register classes.
77//----------------------------------------------------------------------------
Chris Lattner1b7f7dc2002-04-28 16:21:30 +000078PhyRegAlloc::PhyRegAlloc(Function *F, const TargetMachine& tm,
79 FunctionLiveVarInfo *Lvi, LoopInfo *LDC)
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000080 : TM(tm), Meth(F),
Misha Brukmanfce11432002-10-28 00:28:31 +000081 mcInfo(MachineFunction::get(F)),
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000082 LVI(Lvi), LRI(F, tm, RegClassList),
83 MRI(tm.getRegInfo()),
Ruchira Sasanka8e604792001-09-14 21:18:34 +000084 NumOfRegClasses(MRI.getNumOfRegClasses()),
Chris Lattner4911c352002-02-04 17:39:42 +000085 LoopDepthCalc(LDC) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +000086
87 // create each RegisterClass and put in RegClassList
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000088 //
Chris Lattner7e708292002-06-25 16:13:24 +000089 for (unsigned rc=0; rc < NumOfRegClasses; rc++)
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000090 RegClassList.push_back(new RegClass(F, MRI.getMachineRegClass(rc),
91 &ResColList));
Ruchira Sasanka8e604792001-09-14 21:18:34 +000092}
93
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000094
95//----------------------------------------------------------------------------
96// Destructor: Deletes register classes
97//----------------------------------------------------------------------------
98PhyRegAlloc::~PhyRegAlloc() {
Chris Lattner7e708292002-06-25 16:13:24 +000099 for ( unsigned rc=0; rc < NumOfRegClasses; rc++)
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000100 delete RegClassList[rc];
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000101
102 AddedInstrMap.clear();
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000103}
104
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000105//----------------------------------------------------------------------------
106// This method initally creates interference graphs (one in each reg class)
107// and IGNodeList (one in each IG). The actual nodes will be pushed later.
108//----------------------------------------------------------------------------
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000109void PhyRegAlloc::createIGNodeListsAndIGs() {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000110 if (DEBUG_RA >= RA_DEBUG_LiveRanges) cerr << "Creating LR lists ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000111
112 // hash map iterator
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000113 LiveRangeMapType::const_iterator HMI = LRI.getLiveRangeMap()->begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000114
115 // hash map end
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000116 LiveRangeMapType::const_iterator HMIEnd = LRI.getLiveRangeMap()->end();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000117
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000118 for (; HMI != HMIEnd ; ++HMI ) {
119 if (HMI->first) {
120 LiveRange *L = HMI->second; // get the LiveRange
121 if (!L) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000122 if (DEBUG_RA)
123 cerr << "\n**** ?!?WARNING: NULL LIVE RANGE FOUND FOR: "
124 << RAV(HMI->first) << "****\n";
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000125 continue;
126 }
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000127
128 // if the Value * is not null, and LR is not yet written to the IGNodeList
Chris Lattner7e708292002-06-25 16:13:24 +0000129 if (!(L->getUserIGNode()) ) {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000130 RegClass *const RC = // RegClass of first value in the LR
131 RegClassList[ L->getRegClass()->getID() ];
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000132 RC->addLRToIG(L); // add this LR to an IG
133 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000134 }
135 }
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000136
137 // init RegClassList
Chris Lattner7e708292002-06-25 16:13:24 +0000138 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000139 RegClassList[rc]->createInterferenceGraph();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000140
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000141 if (DEBUG_RA >= RA_DEBUG_LiveRanges) cerr << "LRLists Created!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000142}
143
144
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000145//----------------------------------------------------------------------------
146// This method will add all interferences at for a given instruction.
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000147// Interence occurs only if the LR of Def (Inst or Arg) is of the same reg
148// class as that of live var. The live var passed to this function is the
149// LVset AFTER the instruction
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000150//----------------------------------------------------------------------------
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000151
Chris Lattner296b7732002-02-05 02:52:05 +0000152void PhyRegAlloc::addInterference(const Value *Def,
153 const ValueSet *LVSet,
154 bool isCallInst) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000155
Chris Lattner296b7732002-02-05 02:52:05 +0000156 ValueSet::const_iterator LIt = LVSet->begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000157
158 // get the live range of instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000159 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000160 const LiveRange *const LROfDef = LRI.getLiveRangeForValue( Def );
161
162 IGNode *const IGNodeOfDef = LROfDef->getUserIGNode();
163 assert( IGNodeOfDef );
164
165 RegClass *const RCOfDef = LROfDef->getRegClass();
166
167 // for each live var in live variable set
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000168 //
Chris Lattner7e708292002-06-25 16:13:24 +0000169 for ( ; LIt != LVSet->end(); ++LIt) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000170
Vikram S. Advef5af6362002-07-08 23:15:32 +0000171 if (DEBUG_RA >= RA_DEBUG_Verbose)
Chris Lattner0665a5f2002-02-05 01:43:49 +0000172 cerr << "< Def=" << RAV(Def) << ", Lvar=" << RAV(*LIt) << "> ";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000173
174 // get the live range corresponding to live var
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000175 //
Chris Lattner0665a5f2002-02-05 01:43:49 +0000176 LiveRange *LROfVar = LRI.getLiveRangeForValue(*LIt);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000177
178 // LROfVar can be null if it is a const since a const
179 // doesn't have a dominating def - see Assumptions above
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000180 //
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000181 if (LROfVar)
182 if (LROfDef != LROfVar) // do not set interf for same LR
183 if (RCOfDef == LROfVar->getRegClass()) // 2 reg classes are the same
184 RCOfDef->setInterference( LROfDef, LROfVar);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000185 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000186}
187
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000188
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000189
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000190//----------------------------------------------------------------------------
191// For a call instruction, this method sets the CallInterference flag in
192// the LR of each variable live int the Live Variable Set live after the
193// call instruction (except the return value of the call instruction - since
194// the return value does not interfere with that call itself).
195//----------------------------------------------------------------------------
196
197void PhyRegAlloc::setCallInterferences(const MachineInstr *MInst,
Chris Lattner296b7732002-02-05 02:52:05 +0000198 const ValueSet *LVSetAft) {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000199
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000200 if (DEBUG_RA >= RA_DEBUG_Interference)
Chris Lattner697954c2002-01-20 22:54:45 +0000201 cerr << "\n For call inst: " << *MInst;
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000202
Chris Lattner296b7732002-02-05 02:52:05 +0000203 ValueSet::const_iterator LIt = LVSetAft->begin();
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000204
205 // for each live var in live variable set after machine inst
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000206 //
Chris Lattner7e708292002-06-25 16:13:24 +0000207 for ( ; LIt != LVSetAft->end(); ++LIt) {
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000208
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000209 // get the live range corresponding to live var
210 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000211 LiveRange *const LR = LRI.getLiveRangeForValue(*LIt );
212
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000213 // LR can be null if it is a const since a const
214 // doesn't have a dominating def - see Assumptions above
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000215 //
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000216 if (LR ) {
217 if (DEBUG_RA >= RA_DEBUG_Interference) {
218 cerr << "\n\tLR after Call: ";
219 printSet(*LR);
220 }
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000221 LR->setCallInterference();
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000222 if (DEBUG_RA >= RA_DEBUG_Interference) {
223 cerr << "\n ++After adding call interference for LR: " ;
Chris Lattner296b7732002-02-05 02:52:05 +0000224 printSet(*LR);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000225 }
226 }
227
228 }
229
Vikram S. Adve1a53f032002-03-31 18:54:37 +0000230 // Now find the LR of the return value of the call
231 // We do this because, we look at the LV set *after* the instruction
232 // to determine, which LRs must be saved across calls. The return value
233 // of the call is live in this set - but it does not interfere with call
234 // (i.e., we can allocate a volatile register to the return value)
235 //
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000236 CallArgsDescriptor* argDesc = CallArgsDescriptor::get(MInst);
237
238 if (const Value *RetVal = argDesc->getReturnValue()) {
Vikram S. Adve1a53f032002-03-31 18:54:37 +0000239 LiveRange *RetValLR = LRI.getLiveRangeForValue( RetVal );
240 assert( RetValLR && "No LR for RetValue of call");
241 RetValLR->clearCallInterference();
242 }
243
244 // If the CALL is an indirect call, find the LR of the function pointer.
245 // That has a call interference because it conflicts with outgoing args.
Chris Lattner7e708292002-06-25 16:13:24 +0000246 if (const Value *AddrVal = argDesc->getIndirectFuncPtr()) {
Vikram S. Adve1a53f032002-03-31 18:54:37 +0000247 LiveRange *AddrValLR = LRI.getLiveRangeForValue( AddrVal );
248 assert( AddrValLR && "No LR for indirect addr val of call");
249 AddrValLR->setCallInterference();
250 }
251
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000252}
253
254
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000255
256
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000257//----------------------------------------------------------------------------
258// This method will walk thru code and create interferences in the IG of
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000259// each RegClass. Also, this method calculates the spill cost of each
260// Live Range (it is done in this method to save another pass over the code).
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000261//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000262void PhyRegAlloc::buildInterferenceGraphs()
263{
264
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000265 if (DEBUG_RA >= RA_DEBUG_Interference)
266 cerr << "Creating interference graphs ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000267
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000268 unsigned BBLoopDepthCost;
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000269 for (Function::const_iterator BBI = Meth->begin(), BBE = Meth->end();
270 BBI != BBE; ++BBI) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000271
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000272 // find the 10^(loop_depth) of this BB
273 //
Chris Lattner7e708292002-06-25 16:13:24 +0000274 BBLoopDepthCost = (unsigned)pow(10.0, LoopDepthCalc->getLoopDepth(BBI));
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000275
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000276 // get the iterator for machine instructions
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000277 //
Vikram S. Advef5af6362002-07-08 23:15:32 +0000278 const MachineCodeForBasicBlock& MIVec = MachineCodeForBasicBlock::get(BBI);
Vikram S. Adve48762092002-04-25 04:34:15 +0000279 MachineCodeForBasicBlock::const_iterator MII = MIVec.begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000280
281 // iterate over all the machine instructions in BB
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000282 //
Chris Lattner7e708292002-06-25 16:13:24 +0000283 for ( ; MII != MIVec.end(); ++MII) {
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000284
Vikram S. Adve48762092002-04-25 04:34:15 +0000285 const MachineInstr *MInst = *MII;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000286
287 // get the LV set after the instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000288 //
Chris Lattner7e708292002-06-25 16:13:24 +0000289 const ValueSet &LVSetAI = LVI->getLiveVarSetAfterMInst(MInst, BBI);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000290
291 const bool isCallInst = TM.getInstrInfo().isCall(MInst->getOpCode());
292
Chris Lattner7e708292002-06-25 16:13:24 +0000293 if (isCallInst ) {
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000294 // set the isCallInterference flag of each live range wich extends
295 // accross this call instruction. This information is used by graph
296 // coloring algo to avoid allocating volatile colors to live ranges
297 // that span across calls (since they have to be saved/restored)
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000298 //
Chris Lattner748697d2002-02-05 04:20:12 +0000299 setCallInterferences(MInst, &LVSetAI);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000300 }
301
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000302
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000303 // iterate over all MI operands to find defs
304 //
Chris Lattner2f898d22002-02-05 06:02:59 +0000305 for (MachineInstr::const_val_op_iterator OpI = MInst->begin(),
306 OpE = MInst->end(); OpI != OpE; ++OpI) {
307 if (OpI.isDef()) // create a new LR iff this operand is a def
Chris Lattner748697d2002-02-05 04:20:12 +0000308 addInterference(*OpI, &LVSetAI, isCallInst);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000309
310 // Calculate the spill cost of each live range
311 //
Chris Lattner2f898d22002-02-05 06:02:59 +0000312 LiveRange *LR = LRI.getLiveRangeForValue(*OpI);
313 if (LR) LR->addSpillCost(BBLoopDepthCost);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000314 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000315
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000316
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000317 // if there are multiple defs in this instruction e.g. in SETX
318 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000319 if (TM.getInstrInfo().isPseudoInstr(MInst->getOpCode()))
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000320 addInterf4PseudoInstr(MInst);
321
322
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000323 // Also add interference for any implicit definitions in a machine
324 // instr (currently, only calls have this).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000325 //
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000326 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
Chris Lattner7e708292002-06-25 16:13:24 +0000327 if ( NumOfImpRefs > 0 ) {
328 for (unsigned z=0; z < NumOfImpRefs; z++)
329 if (MInst->implicitRefIsDefined(z) )
Chris Lattner748697d2002-02-05 04:20:12 +0000330 addInterference( MInst->getImplicitRef(z), &LVSetAI, isCallInst );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000331 }
332
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +0000333
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000334 } // for all machine instructions in BB
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000335 } // for all BBs in function
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000336
337
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000338 // add interferences for function arguments. Since there are no explict
339 // defs in the function for args, we have to add them manually
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000340 //
341 addInterferencesForArgs();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000342
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000343 if (DEBUG_RA >= RA_DEBUG_Interference)
344 cerr << "Interference graphs calculated!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000345}
346
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000347
348
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000349//--------------------------------------------------------------------------
350// Pseudo instructions will be exapnded to multiple instructions by the
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000351// assembler. Consequently, all the opernds must get distinct registers.
352// Therefore, we mark all operands of a pseudo instruction as they interfere
353// with one another.
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000354//--------------------------------------------------------------------------
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000355void PhyRegAlloc::addInterf4PseudoInstr(const MachineInstr *MInst) {
356
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000357 bool setInterf = false;
358
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000359 // iterate over MI operands to find defs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000360 //
Chris Lattner2f898d22002-02-05 06:02:59 +0000361 for (MachineInstr::const_val_op_iterator It1 = MInst->begin(),
362 ItE = MInst->end(); It1 != ItE; ++It1) {
363 const LiveRange *LROfOp1 = LRI.getLiveRangeForValue(*It1);
364 assert((LROfOp1 || !It1.isDef()) && "No LR for Def in PSEUDO insruction");
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000365
Chris Lattner2f898d22002-02-05 06:02:59 +0000366 MachineInstr::const_val_op_iterator It2 = It1;
Chris Lattner7e708292002-06-25 16:13:24 +0000367 for (++It2; It2 != ItE; ++It2) {
Chris Lattner2f898d22002-02-05 06:02:59 +0000368 const LiveRange *LROfOp2 = LRI.getLiveRangeForValue(*It2);
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000369
Chris Lattner2f898d22002-02-05 06:02:59 +0000370 if (LROfOp2) {
371 RegClass *RCOfOp1 = LROfOp1->getRegClass();
372 RegClass *RCOfOp2 = LROfOp2->getRegClass();
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000373
Chris Lattner7e708292002-06-25 16:13:24 +0000374 if (RCOfOp1 == RCOfOp2 ){
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000375 RCOfOp1->setInterference( LROfOp1, LROfOp2 );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000376 setInterf = true;
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000377 }
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000378 } // if Op2 has a LR
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000379 } // for all other defs in machine instr
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000380 } // for all operands in an instruction
381
Chris Lattner2f898d22002-02-05 06:02:59 +0000382 if (!setInterf && MInst->getNumOperands() > 2) {
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000383 cerr << "\nInterf not set for any operand in pseudo instr:\n";
384 cerr << *MInst;
385 assert(0 && "Interf not set for pseudo instr with > 2 operands" );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000386 }
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000387}
388
389
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000390
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000391//----------------------------------------------------------------------------
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000392// This method will add interferences for incoming arguments to a function.
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000393//----------------------------------------------------------------------------
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000394
Chris Lattner296b7732002-02-05 02:52:05 +0000395void PhyRegAlloc::addInterferencesForArgs() {
396 // get the InSet of root BB
Chris Lattner7e708292002-06-25 16:13:24 +0000397 const ValueSet &InSet = LVI->getInSetOfBB(&Meth->front());
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000398
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000399 for (Function::const_aiterator AI=Meth->abegin(); AI != Meth->aend(); ++AI) {
Chris Lattner7e708292002-06-25 16:13:24 +0000400 // add interferences between args and LVars at start
401 addInterference(AI, &InSet, false);
402
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000403 if (DEBUG_RA >= RA_DEBUG_Interference)
Chris Lattner7e708292002-06-25 16:13:24 +0000404 cerr << " - %% adding interference for argument " << RAV(AI) << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000405 }
406}
407
408
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000409//----------------------------------------------------------------------------
410// This method is called after register allocation is complete to set the
411// allocated reisters in the machine code. This code will add register numbers
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000412// to MachineOperands that contain a Value. Also it calls target specific
413// methods to produce caller saving instructions. At the end, it adds all
414// additional instructions produced by the register allocator to the
415// instruction stream.
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000416//----------------------------------------------------------------------------
Vikram S. Adve48762092002-04-25 04:34:15 +0000417
418//-----------------------------
419// Utility functions used below
420//-----------------------------
421inline void
Vikram S. Advecb202e32002-10-11 16:12:40 +0000422InsertBefore(MachineInstr* newMI,
423 MachineCodeForBasicBlock& MIVec,
424 MachineCodeForBasicBlock::iterator& MII)
425{
426 MII = MIVec.insert(MII, newMI);
427 ++MII;
428}
429
430inline void
431InsertAfter(MachineInstr* newMI,
432 MachineCodeForBasicBlock& MIVec,
433 MachineCodeForBasicBlock::iterator& MII)
434{
435 ++MII; // insert before the next instruction
436 MII = MIVec.insert(MII, newMI);
437}
438
439inline void
440SubstituteInPlace(MachineInstr* newMI,
441 MachineCodeForBasicBlock& MIVec,
442 MachineCodeForBasicBlock::iterator MII)
443{
444 *MII = newMI;
445}
446
447inline void
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000448PrependInstructions(vector<MachineInstr *> &IBef,
Vikram S. Adve48762092002-04-25 04:34:15 +0000449 MachineCodeForBasicBlock& MIVec,
450 MachineCodeForBasicBlock::iterator& MII,
451 const std::string& msg)
452{
453 if (!IBef.empty())
454 {
455 MachineInstr* OrigMI = *MII;
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000456 std::vector<MachineInstr *>::iterator AdIt;
Vikram S. Adve48762092002-04-25 04:34:15 +0000457 for (AdIt = IBef.begin(); AdIt != IBef.end() ; ++AdIt)
458 {
459 if (DEBUG_RA) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000460 if (OrigMI) cerr << "For MInst:\n " << *OrigMI;
461 cerr << msg << "PREPENDed instr:\n " << **AdIt << "\n";
Vikram S. Adve48762092002-04-25 04:34:15 +0000462 }
Vikram S. Advecb202e32002-10-11 16:12:40 +0000463 InsertBefore(*AdIt, MIVec, MII);
Vikram S. Adve48762092002-04-25 04:34:15 +0000464 }
465 }
466}
467
468inline void
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000469AppendInstructions(std::vector<MachineInstr *> &IAft,
Vikram S. Adve48762092002-04-25 04:34:15 +0000470 MachineCodeForBasicBlock& MIVec,
471 MachineCodeForBasicBlock::iterator& MII,
472 const std::string& msg)
473{
474 if (!IAft.empty())
475 {
476 MachineInstr* OrigMI = *MII;
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000477 std::vector<MachineInstr *>::iterator AdIt;
Chris Lattner7e708292002-06-25 16:13:24 +0000478 for ( AdIt = IAft.begin(); AdIt != IAft.end() ; ++AdIt )
Vikram S. Adve48762092002-04-25 04:34:15 +0000479 {
Chris Lattner7e708292002-06-25 16:13:24 +0000480 if (DEBUG_RA) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000481 if (OrigMI) cerr << "For MInst:\n " << *OrigMI;
482 cerr << msg << "APPENDed instr:\n " << **AdIt << "\n";
Vikram S. Adve48762092002-04-25 04:34:15 +0000483 }
Vikram S. Advecb202e32002-10-11 16:12:40 +0000484 InsertAfter(*AdIt, MIVec, MII);
Vikram S. Adve48762092002-04-25 04:34:15 +0000485 }
486 }
487}
488
489
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000490void PhyRegAlloc::updateMachineCode()
491{
Vikram S. Advef5af6362002-07-08 23:15:32 +0000492 MachineCodeForBasicBlock& MIVec = MachineCodeForBasicBlock::get(&Meth->getEntryNode());
Vikram S. Adve48762092002-04-25 04:34:15 +0000493
Chris Lattner7e708292002-06-25 16:13:24 +0000494 // Insert any instructions needed at method entry
495 MachineCodeForBasicBlock::iterator MII = MIVec.begin();
496 PrependInstructions(AddedInstrAtEntry.InstrnsBefore, MIVec, MII,
497 "At function entry: \n");
498 assert(AddedInstrAtEntry.InstrnsAfter.empty() &&
499 "InstrsAfter should be unnecessary since we are just inserting at "
500 "the function entry point here.");
Vikram S. Adve48762092002-04-25 04:34:15 +0000501
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000502 for (Function::const_iterator BBI = Meth->begin(), BBE = Meth->end();
503 BBI != BBE; ++BBI) {
Vikram S. Advecb202e32002-10-11 16:12:40 +0000504
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000505 // iterate over all the machine instructions in BB
Vikram S. Advef5af6362002-07-08 23:15:32 +0000506 MachineCodeForBasicBlock &MIVec = MachineCodeForBasicBlock::get(BBI);
Chris Lattner7e708292002-06-25 16:13:24 +0000507 for (MachineCodeForBasicBlock::iterator MII = MIVec.begin();
Vikram S. Adve48762092002-04-25 04:34:15 +0000508 MII != MIVec.end(); ++MII) {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000509
Vikram S. Adve48762092002-04-25 04:34:15 +0000510 MachineInstr *MInst = *MII;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000511
512 unsigned Opcode = MInst->getOpCode();
513
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000514 // do not process Phis
Vikram S. Adve23a4c8f2002-03-18 03:37:19 +0000515 if (TM.getInstrInfo().isDummyPhiInstr(Opcode))
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000516 continue;
517
Vikram S. Advef5af6362002-07-08 23:15:32 +0000518 // Reset tmp stack positions so they can be reused for each machine instr.
519 mcInfo.popAllTempValues(TM);
520
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000521 // Now insert speical instructions (if necessary) for call/return
522 // instructions.
523 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000524 if (TM.getInstrInfo().isCall(Opcode) ||
525 TM.getInstrInfo().isReturn(Opcode)) {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000526
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000527 AddedInstrns &AI = AddedInstrMap[MInst];
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000528
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000529 if (TM.getInstrInfo().isCall(Opcode))
Chris Lattner7e708292002-06-25 16:13:24 +0000530 MRI.colorCallArgs(MInst, LRI, &AI, *this, BBI);
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000531 else if (TM.getInstrInfo().isReturn(Opcode))
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000532 MRI.colorRetValue(MInst, LRI, &AI);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000533 }
534
Vikram S. Advef5af6362002-07-08 23:15:32 +0000535 // Set the registers for operands in the machine instruction
536 // if a register was successfully allocated. If not, insert
537 // code to spill the register value.
538 //
539 for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum)
540 {
541 MachineOperand& Op = MInst->getOperand(OpNum);
542 if (Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
543 Op.getOperandType() == MachineOperand::MO_CCRegister)
544 {
545 const Value *const Val = Op.getVRegValue();
546
547 LiveRange *const LR = LRI.getLiveRangeForValue(Val);
548 if (!LR) // consts or labels will have no live range
549 {
550 // if register is not allocated, mark register as invalid
551 if (Op.getAllocatedRegNum() == -1)
552 MInst->SetRegForOperand(OpNum, MRI.getInvalidRegNum());
553 continue;
554 }
555
556 if (LR->hasColor() )
557 MInst->SetRegForOperand(OpNum,
558 MRI.getUnifiedRegNum(LR->getRegClass()->getID(),
559 LR->getColor()));
560 else
561 // LR did NOT receive a color (register). Insert spill code.
562 insertCode4SpilledLR(LR, MInst, BBI, OpNum );
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000563 }
Vikram S. Advef5af6362002-07-08 23:15:32 +0000564 } // for each operand
Vikram S. Advecb202e32002-10-11 16:12:40 +0000565
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000566 // Now add instructions that the register allocator inserts before/after
567 // this machine instructions (done only for calls/rets/incoming args)
568 // We do this here, to ensure that spill for an instruction is inserted
569 // closest as possible to an instruction (see above insertCode4Spill...)
570 //
Vikram S. Advecb202e32002-10-11 16:12:40 +0000571 // First, if the instruction in the delay slot of a branch needs
572 // instructions inserted, move it out of the delay slot and before the
573 // branch because putting code before or after it would be VERY BAD!
574 //
575 unsigned bumpIteratorBy = 0;
576 if (MII != MIVec.begin())
577 if (unsigned predDelaySlots =
578 TM.getInstrInfo().getNumDelaySlots((*(MII-1))->getOpCode()))
579 {
580 assert(predDelaySlots==1 && "Not handling multiple delay slots!");
581 if (TM.getInstrInfo().isBranch((*(MII-1))->getOpCode())
582 && (AddedInstrMap.count(MInst) ||
583 AddedInstrMap[MInst].InstrnsAfter.size() > 0))
584 {
585 // Current instruction is in the delay slot of a branch and it
586 // needs spill code inserted before or after it.
587 // Move it before the preceding branch.
588 InsertBefore(MInst, MIVec, --MII);
589 MachineInstr* nopI =
590 new MachineInstr(TM.getInstrInfo().getNOPOpCode());
591 SubstituteInPlace(nopI, MIVec, MII+1); // replace orig with NOP
592 --MII; // point to MInst in new location
593 bumpIteratorBy = 2; // later skip the branch and the NOP!
594 }
595 }
596
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000597 // If there are instructions to be added, *before* this machine
598 // instruction, add them now.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000599 //
Chris Lattner7e708292002-06-25 16:13:24 +0000600 if (AddedInstrMap.count(MInst)) {
Vikram S. Adve48762092002-04-25 04:34:15 +0000601 PrependInstructions(AddedInstrMap[MInst].InstrnsBefore, MIVec, MII,"");
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000602 }
Vikram S. Adve48762092002-04-25 04:34:15 +0000603
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000604 // If there are instructions to be added *after* this machine
605 // instruction, add them now
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000606 //
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000607 if (!AddedInstrMap[MInst].InstrnsAfter.empty()) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000608
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000609 // if there are delay slots for this instruction, the instructions
610 // added after it must really go after the delayed instruction(s)
611 // So, we move the InstrAfter of the current instruction to the
612 // corresponding delayed instruction
Vikram S. Advecb202e32002-10-11 16:12:40 +0000613 if (unsigned delay =
614 TM.getInstrInfo().getNumDelaySlots(MInst->getOpCode())) {
615
616 // Delayed instructions are typically branches or calls. Let's make
617 // sure this is not a branch, otherwise "insert-after" is meaningless,
618 // and should never happen for any reason (spill code, register
619 // restores, etc.).
620 assert(! TM.getInstrInfo().isBranch(MInst->getOpCode()) &&
621 ! TM.getInstrInfo().isReturn(MInst->getOpCode()) &&
622 "INTERNAL ERROR: Register allocator should not be inserting "
623 "any code after a branch or return!");
624
Vikram S. Adve48762092002-04-25 04:34:15 +0000625 move2DelayedInstr(MInst, *(MII+delay) );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000626 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000627 else {
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000628 // Here we can add the "instructions after" to the current
629 // instruction since there are no delay slots for this instruction
Vikram S. Adve48762092002-04-25 04:34:15 +0000630 AppendInstructions(AddedInstrMap[MInst].InstrnsAfter, MIVec, MII,"");
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000631 } // if not delay
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000632 }
Vikram S. Advecb202e32002-10-11 16:12:40 +0000633
634 // If we mucked with the instruction order above, adjust the loop iterator
635 if (bumpIteratorBy)
636 MII = MII + bumpIteratorBy;
637
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000638 } // for each machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000639 }
640}
641
642
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000643
644//----------------------------------------------------------------------------
645// This method inserts spill code for AN operand whose LR was spilled.
646// This method may be called several times for a single machine instruction
647// if it contains many spilled operands. Each time it is called, it finds
648// a register which is not live at that instruction and also which is not
649// used by other spilled operands of the same instruction. Then it uses
650// this register temporarily to accomodate the spilled value.
651//----------------------------------------------------------------------------
652void PhyRegAlloc::insertCode4SpilledLR(const LiveRange *LR,
653 MachineInstr *MInst,
654 const BasicBlock *BB,
655 const unsigned OpNum) {
656
Vikram S. Advead9c9782002-09-28 17:02:40 +0000657 assert((! TM.getInstrInfo().isCall(MInst->getOpCode()) || OpNum == 0) &&
658 "Outgoing arg of a call must be handled elsewhere (func arg ok)");
659 assert(! TM.getInstrInfo().isReturn(MInst->getOpCode()) &&
660 "Return value of a ret must be handled elsewhere");
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000661
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000662 MachineOperand& Op = MInst->getOperand(OpNum);
663 bool isDef = MInst->operandIsDefined(OpNum);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000664 bool isDefAndUse = MInst->operandIsDefinedAndUsed(OpNum);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000665 unsigned RegType = MRI.getRegType( LR );
666 int SpillOff = LR->getSpillOffFromFP();
667 RegClass *RC = LR->getRegClass();
Chris Lattner748697d2002-02-05 04:20:12 +0000668 const ValueSet &LVSetBef = LVI->getLiveVarSetBeforeMInst(MInst, BB);
Vikram S. Adve00521d72001-11-12 23:26:35 +0000669
Chris Lattner697954c2002-01-20 22:54:45 +0000670 mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000671
Vikram S. Advef5af6362002-07-08 23:15:32 +0000672 vector<MachineInstr*> MIBef, MIAft;
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000673 vector<MachineInstr*> AdIMid;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000674
Vikram S. Advef5af6362002-07-08 23:15:32 +0000675 // Choose a register to hold the spilled value. This may insert code
676 // before and after MInst to free up the value. If so, this code should
677 // be first and last in the spill sequence before/after MInst.
678 int TmpRegU = getUsableUniRegAtMI(RegType, &LVSetBef, MInst, MIBef, MIAft);
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000679
Vikram S. Advef5af6362002-07-08 23:15:32 +0000680 // Set the operand first so that it this register does not get used
681 // as a scratch register for later calls to getUsableUniRegAtMI below
682 MInst->SetRegForOperand(OpNum, TmpRegU);
683
684 // get the added instructions for this instruction
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000685 AddedInstrns &AI = AddedInstrMap[MInst];
Vikram S. Advef5af6362002-07-08 23:15:32 +0000686
687 // We may need a scratch register to copy the spilled value to/from memory.
688 // This may itself have to insert code to free up a scratch register.
689 // Any such code should go before (after) the spill code for a load (store).
690 int scratchRegType = -1;
691 int scratchReg = -1;
692 if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType))
693 {
Chris Lattner27a08932002-10-22 23:16:21 +0000694 scratchReg = getUsableUniRegAtMI(scratchRegType, &LVSetBef,
695 MInst, MIBef, MIAft);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000696 assert(scratchReg != MRI.getInvalidRegNum());
Chris Lattner27a08932002-10-22 23:16:21 +0000697 MInst->insertUsedReg(scratchReg);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000698 }
699
700 if (!isDef || isDefAndUse) {
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000701 // for a USE, we have to load the value of LR from stack to a TmpReg
702 // and use the TmpReg as one operand of instruction
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000703
Vikram S. Advef5af6362002-07-08 23:15:32 +0000704 // actual loading instruction(s)
705 MRI.cpMem2RegMI(AdIMid, MRI.getFramePointer(), SpillOff, TmpRegU, RegType,
706 scratchReg);
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000707
Vikram S. Advef5af6362002-07-08 23:15:32 +0000708 // the actual load should be after the instructions to free up TmpRegU
709 MIBef.insert(MIBef.end(), AdIMid.begin(), AdIMid.end());
710 AdIMid.clear();
711 }
712
713 if (isDef) { // if this is a Def
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000714 // for a DEF, we have to store the value produced by this instruction
715 // on the stack position allocated for this LR
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000716
Vikram S. Advef5af6362002-07-08 23:15:32 +0000717 // actual storing instruction(s)
718 MRI.cpReg2MemMI(AdIMid, TmpRegU, MRI.getFramePointer(), SpillOff, RegType,
719 scratchReg);
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000720
Vikram S. Advef5af6362002-07-08 23:15:32 +0000721 MIAft.insert(MIAft.begin(), AdIMid.begin(), AdIMid.end());
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000722 } // if !DEF
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000723
Vikram S. Advef5af6362002-07-08 23:15:32 +0000724 // Finally, insert the entire spill code sequences before/after MInst
725 AI.InstrnsBefore.insert(AI.InstrnsBefore.end(), MIBef.begin(), MIBef.end());
726 AI.InstrnsAfter.insert(AI.InstrnsAfter.begin(), MIAft.begin(), MIAft.end());
727
Chris Lattner7e708292002-06-25 16:13:24 +0000728 if (DEBUG_RA) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000729 cerr << "\nFor Inst:\n " << *MInst;
730 cerr << "SPILLED LR# " << LR->getUserIGNode()->getIndex();
731 cerr << "; added Instructions:";
Anand Shuklad58290e2002-07-09 19:18:56 +0000732 for_each(MIBef.begin(), MIBef.end(), std::mem_fun(&MachineInstr::dump));
733 for_each(MIAft.begin(), MIAft.end(), std::mem_fun(&MachineInstr::dump));
Chris Lattner7e708292002-06-25 16:13:24 +0000734 }
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000735}
736
737
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000738//----------------------------------------------------------------------------
739// We can use the following method to get a temporary register to be used
740// BEFORE any given machine instruction. If there is a register available,
741// this method will simply return that register and set MIBef = MIAft = NULL.
742// Otherwise, it will return a register and MIAft and MIBef will contain
743// two instructions used to free up this returned register.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000744// Returned register number is the UNIFIED register number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000745//----------------------------------------------------------------------------
746
Vikram S. Advef5af6362002-07-08 23:15:32 +0000747int PhyRegAlloc::getUsableUniRegAtMI(const int RegType,
748 const ValueSet *LVSetBef,
749 MachineInstr *MInst,
750 std::vector<MachineInstr*>& MIBef,
751 std::vector<MachineInstr*>& MIAft) {
752
753 RegClass* RC = this->getRegClassByID(MRI.getRegClassIDOfRegType(RegType));
754
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000755 int RegU = getUnusedUniRegAtMI(RC, MInst, LVSetBef);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000756
757 if (RegU == -1) {
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000758 // we couldn't find an unused register. Generate code to free up a reg by
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000759 // saving it on stack and restoring after the instruction
Vikram S. Advef5af6362002-07-08 23:15:32 +0000760
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000761 int TmpOff = mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
Vikram S. Adve12af1642001-11-08 04:48:50 +0000762
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000763 RegU = getUniRegNotUsedByThisInst(RC, MInst);
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000764
Vikram S. Advef5af6362002-07-08 23:15:32 +0000765 // Check if we need a scratch register to copy this register to memory.
766 int scratchRegType = -1;
767 if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType))
768 {
769 int scratchReg = this->getUsableUniRegAtMI(scratchRegType, LVSetBef,
770 MInst, MIBef, MIAft);
771 assert(scratchReg != MRI.getInvalidRegNum());
772
773 // We may as well hold the value in the scratch register instead
774 // of copying it to memory and back. But we have to mark the
775 // register as used by this instruction, so it does not get used
776 // as a scratch reg. by another operand or anyone else.
Chris Lattner27a08932002-10-22 23:16:21 +0000777 MInst->insertUsedReg(scratchReg);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000778 MRI.cpReg2RegMI(MIBef, RegU, scratchReg, RegType);
779 MRI.cpReg2RegMI(MIAft, scratchReg, RegU, RegType);
780 }
781 else
782 { // the register can be copied directly to/from memory so do it.
783 MRI.cpReg2MemMI(MIBef, RegU, MRI.getFramePointer(), TmpOff, RegType);
784 MRI.cpMem2RegMI(MIAft, MRI.getFramePointer(), TmpOff, RegU, RegType);
785 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000786 }
Vikram S. Advef5af6362002-07-08 23:15:32 +0000787
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000788 return RegU;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000789}
790
791//----------------------------------------------------------------------------
792// This method is called to get a new unused register that can be used to
793// accomodate a spilled value.
794// This method may be called several times for a single machine instruction
795// if it contains many spilled operands. Each time it is called, it finds
796// a register which is not live at that instruction and also which is not
797// used by other spilled operands of the same instruction.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000798// Return register number is relative to the register class. NOT
799// unified number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000800//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000801int PhyRegAlloc::getUnusedUniRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000802 const MachineInstr *MInst,
Chris Lattner296b7732002-02-05 02:52:05 +0000803 const ValueSet *LVSetBef) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000804
805 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
806
Chris Lattner85c54652002-05-23 15:50:03 +0000807 std::vector<bool> &IsColorUsedArr = RC->getIsColorUsedArr();
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000808
Chris Lattner7e708292002-06-25 16:13:24 +0000809 for (unsigned i=0; i < NumAvailRegs; i++) // Reset array
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000810 IsColorUsedArr[i] = false;
811
Chris Lattner296b7732002-02-05 02:52:05 +0000812 ValueSet::const_iterator LIt = LVSetBef->begin();
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000813
814 // for each live var in live variable set after machine inst
Chris Lattner7e708292002-06-25 16:13:24 +0000815 for ( ; LIt != LVSetBef->end(); ++LIt) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000816
817 // get the live range corresponding to live var
818 LiveRange *const LRofLV = LRI.getLiveRangeForValue(*LIt );
819
820 // LR can be null if it is a const since a const
821 // doesn't have a dominating def - see Assumptions above
Chris Lattner7e708292002-06-25 16:13:24 +0000822 if (LRofLV && LRofLV->getRegClass() == RC && LRofLV->hasColor() )
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000823 IsColorUsedArr[ LRofLV->getColor() ] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000824 }
825
826 // It is possible that one operand of this MInst was already spilled
827 // and it received some register temporarily. If that's the case,
828 // it is recorded in machine operand. We must skip such registers.
829
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000830 setRelRegsUsedByThisInst(RC, MInst);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000831
Chris Lattner7e708292002-06-25 16:13:24 +0000832 for (unsigned c=0; c < NumAvailRegs; c++) // find first unused color
Chris Lattner85c54652002-05-23 15:50:03 +0000833 if (!IsColorUsedArr[c])
834 return MRI.getUnifiedRegNum(RC->getID(), c);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000835
Chris Lattner85c54652002-05-23 15:50:03 +0000836 return -1;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000837}
838
839
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000840//----------------------------------------------------------------------------
841// Get any other register in a register class, other than what is used
842// by operands of a machine instruction. Returns the unified reg number.
843//----------------------------------------------------------------------------
844int PhyRegAlloc::getUniRegNotUsedByThisInst(RegClass *RC,
Chris Lattner85c54652002-05-23 15:50:03 +0000845 const MachineInstr *MInst) {
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000846
Chris Lattner85c54652002-05-23 15:50:03 +0000847 vector<bool> &IsColorUsedArr = RC->getIsColorUsedArr();
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000848 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
849
Chris Lattner7e708292002-06-25 16:13:24 +0000850 for (unsigned i=0; i < NumAvailRegs ; i++) // Reset array
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000851 IsColorUsedArr[i] = false;
852
853 setRelRegsUsedByThisInst(RC, MInst);
854
Chris Lattner7e708292002-06-25 16:13:24 +0000855 for (unsigned c=0; c < RC->getNumOfAvailRegs(); c++)// find first unused color
Chris Lattner85c54652002-05-23 15:50:03 +0000856 if (!IsColorUsedArr[c])
857 return MRI.getUnifiedRegNum(RC->getID(), c);
858
859 assert(0 && "FATAL: No free register could be found in reg class!!");
Chris Lattner697954c2002-01-20 22:54:45 +0000860 return 0;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000861}
862
863
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000864//----------------------------------------------------------------------------
865// This method modifies the IsColorUsedArr of the register class passed to it.
866// It sets the bits corresponding to the registers used by this machine
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000867// instructions. Both explicit and implicit operands are set.
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000868//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000869void PhyRegAlloc::setRelRegsUsedByThisInst(RegClass *RC,
Vikram S. Advef5af6362002-07-08 23:15:32 +0000870 const MachineInstr *MInst ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000871
Vikram S. Advef5af6362002-07-08 23:15:32 +0000872 vector<bool> &IsColorUsedArr = RC->getIsColorUsedArr();
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000873
Vikram S. Advef5af6362002-07-08 23:15:32 +0000874 // Add the registers already marked as used by the instruction.
875 // This should include any scratch registers that are used to save
876 // values across the instruction (e.g., for saving state register values).
Chris Lattner27a08932002-10-22 23:16:21 +0000877 const vector<bool> &regsUsed = MInst->getRegsUsed();
878 for (unsigned i = 0, e = regsUsed.size(); i != e; ++i)
879 if (regsUsed[i]) {
Vikram S. Advef5af6362002-07-08 23:15:32 +0000880 unsigned classId = 0;
Chris Lattner27a08932002-10-22 23:16:21 +0000881 int classRegNum = MRI.getClassRegNum(i, classId);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000882 if (RC->getID() == classId)
883 {
884 assert(classRegNum < (int) IsColorUsedArr.size() &&
885 "Illegal register number for this reg class?");
886 IsColorUsedArr[classRegNum] = true;
887 }
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000888 }
Vikram S. Advef5af6362002-07-08 23:15:32 +0000889
890 // Now add registers allocated to the live ranges of values used in
891 // the instruction. These are not yet recorded in the instruction.
892 for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum)
893 {
894 const MachineOperand& Op = MInst->getOperand(OpNum);
895
896 if (Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
897 Op.getOperandType() == MachineOperand::MO_CCRegister)
898 if (const Value* Val = Op.getVRegValue())
899 if (MRI.getRegClassIDOfValue(Val) == RC->getID())
900 if (Op.getAllocatedRegNum() == -1)
901 if (LiveRange *LROfVal = LRI.getLiveRangeForValue(Val))
902 if (LROfVal->hasColor() )
903 // this operand is in a LR that received a color
904 IsColorUsedArr[LROfVal->getColor()] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000905 }
Vikram S. Advef5af6362002-07-08 23:15:32 +0000906
907 // If there are implicit references, mark their allocated regs as well
908 //
909 for (unsigned z=0; z < MInst->getNumImplicitRefs(); z++)
910 if (const LiveRange*
911 LRofImpRef = LRI.getLiveRangeForValue(MInst->getImplicitRef(z)))
912 if (LRofImpRef->hasColor())
913 // this implicit reference is in a LR that received a color
914 IsColorUsedArr[LRofImpRef->getColor()] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000915}
916
917
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000918//----------------------------------------------------------------------------
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000919// If there are delay slots for an instruction, the instructions
920// added after it must really go after the delayed instruction(s).
921// So, we move the InstrAfter of that instruction to the
922// corresponding delayed instruction using the following method.
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000923
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000924//----------------------------------------------------------------------------
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000925void PhyRegAlloc::move2DelayedInstr(const MachineInstr *OrigMI,
926 const MachineInstr *DelayedMI) {
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000927
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000928 // "added after" instructions of the original instr
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000929 std::vector<MachineInstr *> &OrigAft = AddedInstrMap[OrigMI].InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000930
931 // "added instructions" of the delayed instr
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000932 AddedInstrns &DelayAdI = AddedInstrMap[DelayedMI];
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000933
934 // "added after" instructions of the delayed instr
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000935 std::vector<MachineInstr *> &DelayedAft = DelayAdI.InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000936
937 // go thru all the "added after instructions" of the original instruction
938 // and append them to the "addded after instructions" of the delayed
939 // instructions
Chris Lattner697954c2002-01-20 22:54:45 +0000940 DelayedAft.insert(DelayedAft.end(), OrigAft.begin(), OrigAft.end());
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000941
942 // empty the "added after instructions" of the original instruction
943 OrigAft.clear();
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000944}
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000945
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000946//----------------------------------------------------------------------------
947// This method prints the code with registers after register allocation is
948// complete.
949//----------------------------------------------------------------------------
950void PhyRegAlloc::printMachineCode()
951{
952
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000953 cerr << "\n;************** Function " << Meth->getName()
Chris Lattner697954c2002-01-20 22:54:45 +0000954 << " *****************\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000955
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000956 for (Function::const_iterator BBI = Meth->begin(), BBE = Meth->end();
957 BBI != BBE; ++BBI) {
Chris Lattner7e708292002-06-25 16:13:24 +0000958 cerr << "\n"; printLabel(BBI); cerr << ": ";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000959
960 // get the iterator for machine instructions
Vikram S. Advef5af6362002-07-08 23:15:32 +0000961 MachineCodeForBasicBlock& MIVec = MachineCodeForBasicBlock::get(BBI);
Vikram S. Adve48762092002-04-25 04:34:15 +0000962 MachineCodeForBasicBlock::iterator MII = MIVec.begin();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000963
964 // iterate over all the machine instructions in BB
Chris Lattner7e708292002-06-25 16:13:24 +0000965 for ( ; MII != MIVec.end(); ++MII) {
Vikram S. Adve48762092002-04-25 04:34:15 +0000966 MachineInstr *const MInst = *MII;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000967
Chris Lattner697954c2002-01-20 22:54:45 +0000968 cerr << "\n\t";
969 cerr << TargetInstrDescriptors[MInst->getOpCode()].opCodeString;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000970
Chris Lattner7e708292002-06-25 16:13:24 +0000971 for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000972 MachineOperand& Op = MInst->getOperand(OpNum);
973
Chris Lattner7e708292002-06-25 16:13:24 +0000974 if (Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasanka97b8b442001-10-18 22:36:26 +0000975 Op.getOperandType() == MachineOperand::MO_CCRegister /*||
976 Op.getOperandType() == MachineOperand::MO_PCRelativeDisp*/ ) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000977
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000978 const Value *const Val = Op.getVRegValue () ;
Ruchira Sasankae727f852001-09-18 22:43:57 +0000979 // ****this code is temporary till NULL Values are fixed
Chris Lattner7e708292002-06-25 16:13:24 +0000980 if (! Val ) {
Chris Lattner697954c2002-01-20 22:54:45 +0000981 cerr << "\t<*NULL*>";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000982 continue;
983 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000984
985 // if a label or a constant
Chris Lattner7e708292002-06-25 16:13:24 +0000986 if (isa<BasicBlock>(Val)) {
Chris Lattner697954c2002-01-20 22:54:45 +0000987 cerr << "\t"; printLabel( Op.getVRegValue () );
988 } else {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000989 // else it must be a register value
990 const int RegNum = Op.getAllocatedRegNum();
991
Chris Lattner697954c2002-01-20 22:54:45 +0000992 cerr << "\t" << "%" << MRI.getUnifiedRegName( RegNum );
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000993 if (Val->hasName() )
Chris Lattner697954c2002-01-20 22:54:45 +0000994 cerr << "(" << Val->getName() << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000995 else
Chris Lattner697954c2002-01-20 22:54:45 +0000996 cerr << "(" << Val << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000997
Chris Lattner7e708292002-06-25 16:13:24 +0000998 if (Op.opIsDef() )
Chris Lattner697954c2002-01-20 22:54:45 +0000999 cerr << "*";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001000
1001 const LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
Chris Lattner7e708292002-06-25 16:13:24 +00001002 if (LROfVal )
1003 if (LROfVal->hasSpillOffset() )
Chris Lattner697954c2002-01-20 22:54:45 +00001004 cerr << "$";
Ruchira Sasankae727f852001-09-18 22:43:57 +00001005 }
1006
1007 }
Chris Lattner7e708292002-06-25 16:13:24 +00001008 else if (Op.getOperandType() == MachineOperand::MO_MachineRegister) {
Chris Lattner697954c2002-01-20 22:54:45 +00001009 cerr << "\t" << "%" << MRI.getUnifiedRegName(Op.getMachineRegNum());
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001010 }
1011
1012 else
Chris Lattner697954c2002-01-20 22:54:45 +00001013 cerr << "\t" << Op; // use dump field
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001014 }
1015
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001016
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001017
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001018 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
Chris Lattner7e708292002-06-25 16:13:24 +00001019 if (NumOfImpRefs > 0) {
Chris Lattner697954c2002-01-20 22:54:45 +00001020 cerr << "\tImplicit:";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001021
Chris Lattner7e708292002-06-25 16:13:24 +00001022 for (unsigned z=0; z < NumOfImpRefs; z++)
Chris Lattner0665a5f2002-02-05 01:43:49 +00001023 cerr << RAV(MInst->getImplicitRef(z)) << "\t";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001024 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001025
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001026 } // for all machine instructions
1027
Chris Lattner697954c2002-01-20 22:54:45 +00001028 cerr << "\n";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001029
1030 } // for all BBs
1031
Chris Lattner697954c2002-01-20 22:54:45 +00001032 cerr << "\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001033}
1034
Ruchira Sasankae727f852001-09-18 22:43:57 +00001035
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001036//----------------------------------------------------------------------------
1037
1038//----------------------------------------------------------------------------
1039void PhyRegAlloc::colorIncomingArgs()
1040{
Chris Lattner7e708292002-06-25 16:13:24 +00001041 const BasicBlock &FirstBB = Meth->front();
Vikram S. Advef5af6362002-07-08 23:15:32 +00001042 const MachineInstr *FirstMI = MachineCodeForBasicBlock::get(&FirstBB).front();
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001043 assert(FirstMI && "No machine instruction in entry BB");
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001044
Vikram S. Adve48762092002-04-25 04:34:15 +00001045 MRI.colorMethodArgs(Meth, LRI, &AddedInstrAtEntry);
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001046}
1047
Ruchira Sasankae727f852001-09-18 22:43:57 +00001048
1049//----------------------------------------------------------------------------
1050// Used to generate a label for a basic block
1051//----------------------------------------------------------------------------
Chris Lattner697954c2002-01-20 22:54:45 +00001052void PhyRegAlloc::printLabel(const Value *const Val) {
1053 if (Val->hasName())
1054 cerr << Val->getName();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001055 else
Chris Lattner697954c2002-01-20 22:54:45 +00001056 cerr << "Label" << Val;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001057}
1058
1059
Ruchira Sasankae727f852001-09-18 22:43:57 +00001060//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001061// This method calls setSugColorUsable method of each live range. This
1062// will determine whether the suggested color of LR is really usable.
1063// A suggested color is not usable when the suggested color is volatile
1064// AND when there are call interferences
1065//----------------------------------------------------------------------------
1066
1067void PhyRegAlloc::markUnusableSugColors()
1068{
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001069 // hash map iterator
1070 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
1071 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
1072
Chris Lattner7e708292002-06-25 16:13:24 +00001073 for (; HMI != HMIEnd ; ++HMI ) {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001074 if (HMI->first) {
1075 LiveRange *L = HMI->second; // get the LiveRange
1076 if (L) {
Chris Lattner7e708292002-06-25 16:13:24 +00001077 if (L->hasSuggestedColor()) {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001078 int RCID = L->getRegClass()->getID();
Chris Lattner7e708292002-06-25 16:13:24 +00001079 if (MRI.isRegVolatile( RCID, L->getSuggestedColor()) &&
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001080 L->isCallInterference() )
1081 L->setSuggestedColorUsable( false );
1082 else
1083 L->setSuggestedColorUsable( true );
1084 }
1085 } // if L->hasSuggestedColor()
1086 }
1087 } // for all LR's in hash map
1088}
1089
1090
1091
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001092//----------------------------------------------------------------------------
1093// The following method will set the stack offsets of the live ranges that
1094// are decided to be spillled. This must be called just after coloring the
1095// LRs using the graph coloring algo. For each live range that is spilled,
1096// this method allocate a new spill position on the stack.
1097//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001098
Chris Lattner37730942002-02-05 03:52:29 +00001099void PhyRegAlloc::allocateStackSpace4SpilledLRs() {
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001100 if (DEBUG_RA) cerr << "\nSetting LR stack offsets for spills...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001101
Chris Lattner37730942002-02-05 03:52:29 +00001102 LiveRangeMapType::const_iterator HMI = LRI.getLiveRangeMap()->begin();
1103 LiveRangeMapType::const_iterator HMIEnd = LRI.getLiveRangeMap()->end();
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001104
Chris Lattner7e708292002-06-25 16:13:24 +00001105 for ( ; HMI != HMIEnd ; ++HMI) {
Chris Lattner37730942002-02-05 03:52:29 +00001106 if (HMI->first && HMI->second) {
1107 LiveRange *L = HMI->second; // get the LiveRange
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001108 if (!L->hasColor()) { // NOTE: ** allocating the size of long Type **
1109 int stackOffset = mcInfo.allocateSpilledValue(TM, Type::LongTy);
1110 L->setSpillOffFromFP(stackOffset);
1111 if (DEBUG_RA)
1112 cerr << " LR# " << L->getUserIGNode()->getIndex()
1113 << ": stack-offset = " << stackOffset << "\n";
1114 }
Chris Lattner37730942002-02-05 03:52:29 +00001115 }
1116 } // for all LR's in hash map
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001117}
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001118
1119
1120
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001121//----------------------------------------------------------------------------
Ruchira Sasankae727f852001-09-18 22:43:57 +00001122// The entry pont to Register Allocation
1123//----------------------------------------------------------------------------
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001124
1125void PhyRegAlloc::allocateRegisters()
1126{
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001127
1128 // make sure that we put all register classes into the RegClassList
1129 // before we call constructLiveRanges (now done in the constructor of
1130 // PhyRegAlloc class).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001131 //
1132 LRI.constructLiveRanges(); // create LR info
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001133
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001134 if (DEBUG_RA >= RA_DEBUG_LiveRanges)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001135 LRI.printLiveRanges();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001136
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001137 createIGNodeListsAndIGs(); // create IGNode list and IGs
1138
1139 buildInterferenceGraphs(); // build IGs in all reg classes
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001140
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001141
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001142 if (DEBUG_RA >= RA_DEBUG_LiveRanges) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001143 // print all LRs in all reg classes
Chris Lattner7e708292002-06-25 16:13:24 +00001144 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
1145 RegClassList[rc]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001146
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001147 // print IGs in all register classes
Chris Lattner7e708292002-06-25 16:13:24 +00001148 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
1149 RegClassList[rc]->printIG();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001150 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001151
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001152
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001153 LRI.coalesceLRs(); // coalesce all live ranges
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001154
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +00001155
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001156 if (DEBUG_RA >= RA_DEBUG_LiveRanges) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001157 // print all LRs in all reg classes
Chris Lattner7e708292002-06-25 16:13:24 +00001158 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001159 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001160
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001161 // print IGs in all register classes
Chris Lattner7e708292002-06-25 16:13:24 +00001162 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001163 RegClassList[ rc ]->printIG();
1164 }
1165
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001166
1167 // mark un-usable suggested color before graph coloring algorithm.
1168 // When this is done, the graph coloring algo will not reserve
1169 // suggested color unnecessarily - they can be used by another LR
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001170 //
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001171 markUnusableSugColors();
1172
1173 // color all register classes using the graph coloring algo
Chris Lattner7e708292002-06-25 16:13:24 +00001174 for (unsigned rc=0; rc < NumOfRegClasses ; rc++)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001175 RegClassList[ rc ]->colorAllRegs();
1176
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001177 // Atter grpah coloring, if some LRs did not receive a color (i.e, spilled)
1178 // a poistion for such spilled LRs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001179 //
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001180 allocateStackSpace4SpilledLRs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001181
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001182 mcInfo.popAllTempValues(TM); // TODO **Check
1183
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001184 // color incoming args - if the correct color was not received
1185 // insert code to copy to the correct register
1186 //
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001187 colorIncomingArgs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001188
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001189 // Now update the machine code with register names and add any
1190 // additional code inserted by the register allocator to the instruction
1191 // stream
1192 //
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001193 updateMachineCode();
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001194
Chris Lattner045e7c82001-09-19 16:26:23 +00001195 if (DEBUG_RA) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001196 cerr << "\n**** Machine Code After Register Allocation:\n\n";
Misha Brukmanfce11432002-10-28 00:28:31 +00001197 MachineFunction::get(Meth).dump();
Chris Lattner045e7c82001-09-19 16:26:23 +00001198 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001199}
1200
Ruchira Sasankae727f852001-09-18 22:43:57 +00001201
1202