blob: bd87ec91aad6f4e67a17b52141fe41dfcd80aaf3 [file] [log] [blame]
Chris Lattner179cdfb2002-08-09 20:08:03 +00001//===-- PhyRegAlloc.cpp ---------------------------------------------------===//
Vikram S. Adve12af1642001-11-08 04:48:50 +00002//
Chris Lattner179cdfb2002-08-09 20:08:03 +00003// Register allocation for LLVM.
4//
5//===----------------------------------------------------------------------===//
Ruchira Sasanka8e604792001-09-14 21:18:34 +00006
Chris Lattner6dd98a62002-02-04 00:33:08 +00007#include "llvm/CodeGen/RegisterAllocation.h"
Vikram S. Adve39c94e12002-09-14 23:05:33 +00008#include "llvm/CodeGen/RegAllocCommon.h"
Vikram S. Adve12af1642001-11-08 04:48:50 +00009#include "llvm/CodeGen/PhyRegAlloc.h"
10#include "llvm/CodeGen/MachineInstr.h"
Vikram S. Advedabb41d2002-05-19 15:29:31 +000011#include "llvm/CodeGen/MachineInstrAnnot.h"
Vikram S. Advef5af6362002-07-08 23:15:32 +000012#include "llvm/CodeGen/MachineCodeForBasicBlock.h"
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000013#include "llvm/CodeGen/MachineCodeForMethod.h"
Chris Lattner483e14e2002-04-27 07:27:19 +000014#include "llvm/Analysis/LiveVar/FunctionLiveVarInfo.h"
Chris Lattner14ab1ce2002-02-04 17:48:00 +000015#include "llvm/Analysis/LoopInfo.h"
Vikram S. Adve12af1642001-11-08 04:48:50 +000016#include "llvm/Target/TargetMachine.h"
17#include "llvm/Target/MachineFrameInfo.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000018#include "llvm/Function.h"
Chris Lattner37730942002-02-05 03:52:29 +000019#include "llvm/Type.h"
Vikram S. Advedabb41d2002-05-19 15:29:31 +000020#include "llvm/iOther.h"
Vikram S. Advef5af6362002-07-08 23:15:32 +000021#include "Support/STLExtras.h"
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),
81 mcInfo(MachineCodeForMethod::get(F)),
82 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. Advedabb41d2002-05-19 15:29:31 +0000422PrependInstructions(vector<MachineInstr *> &IBef,
Vikram S. Adve48762092002-04-25 04:34:15 +0000423 MachineCodeForBasicBlock& MIVec,
424 MachineCodeForBasicBlock::iterator& MII,
425 const std::string& msg)
426{
427 if (!IBef.empty())
428 {
429 MachineInstr* OrigMI = *MII;
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000430 std::vector<MachineInstr *>::iterator AdIt;
Vikram S. Adve48762092002-04-25 04:34:15 +0000431 for (AdIt = IBef.begin(); AdIt != IBef.end() ; ++AdIt)
432 {
433 if (DEBUG_RA) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000434 if (OrigMI) cerr << "For MInst:\n " << *OrigMI;
435 cerr << msg << "PREPENDed instr:\n " << **AdIt << "\n";
Vikram S. Adve48762092002-04-25 04:34:15 +0000436 }
437 MII = MIVec.insert(MII, *AdIt);
438 ++MII;
439 }
440 }
441}
442
443inline void
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000444AppendInstructions(std::vector<MachineInstr *> &IAft,
Vikram S. Adve48762092002-04-25 04:34:15 +0000445 MachineCodeForBasicBlock& MIVec,
446 MachineCodeForBasicBlock::iterator& MII,
447 const std::string& msg)
448{
449 if (!IAft.empty())
450 {
451 MachineInstr* OrigMI = *MII;
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000452 std::vector<MachineInstr *>::iterator AdIt;
Chris Lattner7e708292002-06-25 16:13:24 +0000453 for ( AdIt = IAft.begin(); AdIt != IAft.end() ; ++AdIt )
Vikram S. Adve48762092002-04-25 04:34:15 +0000454 {
Chris Lattner7e708292002-06-25 16:13:24 +0000455 if (DEBUG_RA) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000456 if (OrigMI) cerr << "For MInst:\n " << *OrigMI;
457 cerr << msg << "APPENDed instr:\n " << **AdIt << "\n";
Vikram S. Adve48762092002-04-25 04:34:15 +0000458 }
459 ++MII; // insert before the next instruction
460 MII = MIVec.insert(MII, *AdIt);
461 }
462 }
463}
464
465
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000466void PhyRegAlloc::updateMachineCode()
467{
Vikram S. Advef5af6362002-07-08 23:15:32 +0000468 MachineCodeForBasicBlock& MIVec = MachineCodeForBasicBlock::get(&Meth->getEntryNode());
Vikram S. Adve48762092002-04-25 04:34:15 +0000469
Chris Lattner7e708292002-06-25 16:13:24 +0000470 // Insert any instructions needed at method entry
471 MachineCodeForBasicBlock::iterator MII = MIVec.begin();
472 PrependInstructions(AddedInstrAtEntry.InstrnsBefore, MIVec, MII,
473 "At function entry: \n");
474 assert(AddedInstrAtEntry.InstrnsAfter.empty() &&
475 "InstrsAfter should be unnecessary since we are just inserting at "
476 "the function entry point here.");
Vikram S. Adve48762092002-04-25 04:34:15 +0000477
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000478 for (Function::const_iterator BBI = Meth->begin(), BBE = Meth->end();
479 BBI != BBE; ++BBI) {
Vikram S. Adve48762092002-04-25 04:34:15 +0000480
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000481 // iterate over all the machine instructions in BB
Vikram S. Advef5af6362002-07-08 23:15:32 +0000482 MachineCodeForBasicBlock &MIVec = MachineCodeForBasicBlock::get(BBI);
Chris Lattner7e708292002-06-25 16:13:24 +0000483 for (MachineCodeForBasicBlock::iterator MII = MIVec.begin();
Vikram S. Adve48762092002-04-25 04:34:15 +0000484 MII != MIVec.end(); ++MII) {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000485
Vikram S. Adve48762092002-04-25 04:34:15 +0000486 MachineInstr *MInst = *MII;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000487
488 unsigned Opcode = MInst->getOpCode();
489
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000490 // do not process Phis
Vikram S. Adve23a4c8f2002-03-18 03:37:19 +0000491 if (TM.getInstrInfo().isDummyPhiInstr(Opcode))
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000492 continue;
493
Vikram S. Advef5af6362002-07-08 23:15:32 +0000494 // Reset tmp stack positions so they can be reused for each machine instr.
495 mcInfo.popAllTempValues(TM);
496
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000497 // Now insert speical instructions (if necessary) for call/return
498 // instructions.
499 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000500 if (TM.getInstrInfo().isCall(Opcode) ||
501 TM.getInstrInfo().isReturn(Opcode)) {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000502
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000503 AddedInstrns &AI = AddedInstrMap[MInst];
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000504
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000505 if (TM.getInstrInfo().isCall(Opcode))
Chris Lattner7e708292002-06-25 16:13:24 +0000506 MRI.colorCallArgs(MInst, LRI, &AI, *this, BBI);
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000507 else if (TM.getInstrInfo().isReturn(Opcode))
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000508 MRI.colorRetValue(MInst, LRI, &AI);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000509 }
510
Vikram S. Advef5af6362002-07-08 23:15:32 +0000511 // Set the registers for operands in the machine instruction
512 // if a register was successfully allocated. If not, insert
513 // code to spill the register value.
514 //
515 for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum)
516 {
517 MachineOperand& Op = MInst->getOperand(OpNum);
518 if (Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
519 Op.getOperandType() == MachineOperand::MO_CCRegister)
520 {
521 const Value *const Val = Op.getVRegValue();
522
523 LiveRange *const LR = LRI.getLiveRangeForValue(Val);
524 if (!LR) // consts or labels will have no live range
525 {
526 // if register is not allocated, mark register as invalid
527 if (Op.getAllocatedRegNum() == -1)
528 MInst->SetRegForOperand(OpNum, MRI.getInvalidRegNum());
529 continue;
530 }
531
532 if (LR->hasColor() )
533 MInst->SetRegForOperand(OpNum,
534 MRI.getUnifiedRegNum(LR->getRegClass()->getID(),
535 LR->getColor()));
536 else
537 // LR did NOT receive a color (register). Insert spill code.
538 insertCode4SpilledLR(LR, MInst, BBI, OpNum );
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000539 }
Vikram S. Advef5af6362002-07-08 23:15:32 +0000540 } // for each operand
541
542
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000543 // Now add instructions that the register allocator inserts before/after
544 // this machine instructions (done only for calls/rets/incoming args)
545 // We do this here, to ensure that spill for an instruction is inserted
546 // closest as possible to an instruction (see above insertCode4Spill...)
547 //
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000548 // If there are instructions to be added, *before* this machine
549 // instruction, add them now.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000550 //
Chris Lattner7e708292002-06-25 16:13:24 +0000551 if (AddedInstrMap.count(MInst)) {
Vikram S. Adve48762092002-04-25 04:34:15 +0000552 PrependInstructions(AddedInstrMap[MInst].InstrnsBefore, MIVec, MII,"");
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000553 }
Vikram S. Adve48762092002-04-25 04:34:15 +0000554
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000555 // If there are instructions to be added *after* this machine
556 // instruction, add them now
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000557 //
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000558 if (!AddedInstrMap[MInst].InstrnsAfter.empty()) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000559
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000560 // if there are delay slots for this instruction, the instructions
561 // added after it must really go after the delayed instruction(s)
562 // So, we move the InstrAfter of the current instruction to the
563 // corresponding delayed instruction
564
565 unsigned delay;
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000566 if ((delay=TM.getInstrInfo().getNumDelaySlots(MInst->getOpCode())) >0){
Vikram S. Adve48762092002-04-25 04:34:15 +0000567 move2DelayedInstr(MInst, *(MII+delay) );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000568 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000569 else {
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000570 // Here we can add the "instructions after" to the current
571 // instruction since there are no delay slots for this instruction
Vikram S. Adve48762092002-04-25 04:34:15 +0000572 AppendInstructions(AddedInstrMap[MInst].InstrnsAfter, MIVec, MII,"");
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000573 } // if not delay
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000574 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000575
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000576 } // for each machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000577 }
578}
579
580
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000581
582//----------------------------------------------------------------------------
583// This method inserts spill code for AN operand whose LR was spilled.
584// This method may be called several times for a single machine instruction
585// if it contains many spilled operands. Each time it is called, it finds
586// a register which is not live at that instruction and also which is not
587// used by other spilled operands of the same instruction. Then it uses
588// this register temporarily to accomodate the spilled value.
589//----------------------------------------------------------------------------
590void PhyRegAlloc::insertCode4SpilledLR(const LiveRange *LR,
591 MachineInstr *MInst,
592 const BasicBlock *BB,
593 const unsigned OpNum) {
594
Vikram S. Advead9c9782002-09-28 17:02:40 +0000595 assert((! TM.getInstrInfo().isCall(MInst->getOpCode()) || OpNum == 0) &&
596 "Outgoing arg of a call must be handled elsewhere (func arg ok)");
597 assert(! TM.getInstrInfo().isReturn(MInst->getOpCode()) &&
598 "Return value of a ret must be handled elsewhere");
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000599
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000600 MachineOperand& Op = MInst->getOperand(OpNum);
601 bool isDef = MInst->operandIsDefined(OpNum);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000602 bool isDefAndUse = MInst->operandIsDefinedAndUsed(OpNum);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000603 unsigned RegType = MRI.getRegType( LR );
604 int SpillOff = LR->getSpillOffFromFP();
605 RegClass *RC = LR->getRegClass();
Chris Lattner748697d2002-02-05 04:20:12 +0000606 const ValueSet &LVSetBef = LVI->getLiveVarSetBeforeMInst(MInst, BB);
Vikram S. Adve00521d72001-11-12 23:26:35 +0000607
Chris Lattner697954c2002-01-20 22:54:45 +0000608 mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000609
Vikram S. Advef5af6362002-07-08 23:15:32 +0000610 vector<MachineInstr*> MIBef, MIAft;
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000611 vector<MachineInstr*> AdIMid;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000612
Vikram S. Advef5af6362002-07-08 23:15:32 +0000613 // Choose a register to hold the spilled value. This may insert code
614 // before and after MInst to free up the value. If so, this code should
615 // be first and last in the spill sequence before/after MInst.
616 int TmpRegU = getUsableUniRegAtMI(RegType, &LVSetBef, MInst, MIBef, MIAft);
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000617
Vikram S. Advef5af6362002-07-08 23:15:32 +0000618 // Set the operand first so that it this register does not get used
619 // as a scratch register for later calls to getUsableUniRegAtMI below
620 MInst->SetRegForOperand(OpNum, TmpRegU);
621
622 // get the added instructions for this instruction
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000623 AddedInstrns &AI = AddedInstrMap[MInst];
Vikram S. Advef5af6362002-07-08 23:15:32 +0000624
625 // We may need a scratch register to copy the spilled value to/from memory.
626 // This may itself have to insert code to free up a scratch register.
627 // Any such code should go before (after) the spill code for a load (store).
628 int scratchRegType = -1;
629 int scratchReg = -1;
630 if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType))
631 {
632 scratchReg = this->getUsableUniRegAtMI(scratchRegType, &LVSetBef,
633 MInst, MIBef, MIAft);
634 assert(scratchReg != MRI.getInvalidRegNum());
635 MInst->getRegsUsed().insert(scratchReg);
636 }
637
638 if (!isDef || isDefAndUse) {
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000639 // for a USE, we have to load the value of LR from stack to a TmpReg
640 // and use the TmpReg as one operand of instruction
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000641
Vikram S. Advef5af6362002-07-08 23:15:32 +0000642 // actual loading instruction(s)
643 MRI.cpMem2RegMI(AdIMid, MRI.getFramePointer(), SpillOff, TmpRegU, RegType,
644 scratchReg);
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000645
Vikram S. Advef5af6362002-07-08 23:15:32 +0000646 // the actual load should be after the instructions to free up TmpRegU
647 MIBef.insert(MIBef.end(), AdIMid.begin(), AdIMid.end());
648 AdIMid.clear();
649 }
650
651 if (isDef) { // if this is a Def
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000652 // for a DEF, we have to store the value produced by this instruction
653 // on the stack position allocated for this LR
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000654
Vikram S. Advef5af6362002-07-08 23:15:32 +0000655 // actual storing instruction(s)
656 MRI.cpReg2MemMI(AdIMid, TmpRegU, MRI.getFramePointer(), SpillOff, RegType,
657 scratchReg);
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000658
Vikram S. Advef5af6362002-07-08 23:15:32 +0000659 MIAft.insert(MIAft.begin(), AdIMid.begin(), AdIMid.end());
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000660 } // if !DEF
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000661
Vikram S. Advef5af6362002-07-08 23:15:32 +0000662 // Finally, insert the entire spill code sequences before/after MInst
663 AI.InstrnsBefore.insert(AI.InstrnsBefore.end(), MIBef.begin(), MIBef.end());
664 AI.InstrnsAfter.insert(AI.InstrnsAfter.begin(), MIAft.begin(), MIAft.end());
665
Chris Lattner7e708292002-06-25 16:13:24 +0000666 if (DEBUG_RA) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000667 cerr << "\nFor Inst:\n " << *MInst;
668 cerr << "SPILLED LR# " << LR->getUserIGNode()->getIndex();
669 cerr << "; added Instructions:";
Anand Shuklad58290e2002-07-09 19:18:56 +0000670 for_each(MIBef.begin(), MIBef.end(), std::mem_fun(&MachineInstr::dump));
671 for_each(MIAft.begin(), MIAft.end(), std::mem_fun(&MachineInstr::dump));
Chris Lattner7e708292002-06-25 16:13:24 +0000672 }
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000673}
674
675
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000676//----------------------------------------------------------------------------
677// We can use the following method to get a temporary register to be used
678// BEFORE any given machine instruction. If there is a register available,
679// this method will simply return that register and set MIBef = MIAft = NULL.
680// Otherwise, it will return a register and MIAft and MIBef will contain
681// two instructions used to free up this returned register.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000682// Returned register number is the UNIFIED register number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000683//----------------------------------------------------------------------------
684
Vikram S. Advef5af6362002-07-08 23:15:32 +0000685int PhyRegAlloc::getUsableUniRegAtMI(const int RegType,
686 const ValueSet *LVSetBef,
687 MachineInstr *MInst,
688 std::vector<MachineInstr*>& MIBef,
689 std::vector<MachineInstr*>& MIAft) {
690
691 RegClass* RC = this->getRegClassByID(MRI.getRegClassIDOfRegType(RegType));
692
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000693 int RegU = getUnusedUniRegAtMI(RC, MInst, LVSetBef);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000694
695 if (RegU == -1) {
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000696 // we couldn't find an unused register. Generate code to free up a reg by
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000697 // saving it on stack and restoring after the instruction
Vikram S. Advef5af6362002-07-08 23:15:32 +0000698
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000699 int TmpOff = mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
Vikram S. Adve12af1642001-11-08 04:48:50 +0000700
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000701 RegU = getUniRegNotUsedByThisInst(RC, MInst);
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000702
Vikram S. Advef5af6362002-07-08 23:15:32 +0000703 // Check if we need a scratch register to copy this register to memory.
704 int scratchRegType = -1;
705 if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType))
706 {
707 int scratchReg = this->getUsableUniRegAtMI(scratchRegType, LVSetBef,
708 MInst, MIBef, MIAft);
709 assert(scratchReg != MRI.getInvalidRegNum());
710
711 // We may as well hold the value in the scratch register instead
712 // of copying it to memory and back. But we have to mark the
713 // register as used by this instruction, so it does not get used
714 // as a scratch reg. by another operand or anyone else.
715 MInst->getRegsUsed().insert(scratchReg);
716 MRI.cpReg2RegMI(MIBef, RegU, scratchReg, RegType);
717 MRI.cpReg2RegMI(MIAft, scratchReg, RegU, RegType);
718 }
719 else
720 { // the register can be copied directly to/from memory so do it.
721 MRI.cpReg2MemMI(MIBef, RegU, MRI.getFramePointer(), TmpOff, RegType);
722 MRI.cpMem2RegMI(MIAft, MRI.getFramePointer(), TmpOff, RegU, RegType);
723 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000724 }
Vikram S. Advef5af6362002-07-08 23:15:32 +0000725
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000726 return RegU;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000727}
728
729//----------------------------------------------------------------------------
730// This method is called to get a new unused register that can be used to
731// accomodate a spilled value.
732// This method may be called several times for a single machine instruction
733// if it contains many spilled operands. Each time it is called, it finds
734// a register which is not live at that instruction and also which is not
735// used by other spilled operands of the same instruction.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000736// Return register number is relative to the register class. NOT
737// unified number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000738//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000739int PhyRegAlloc::getUnusedUniRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000740 const MachineInstr *MInst,
Chris Lattner296b7732002-02-05 02:52:05 +0000741 const ValueSet *LVSetBef) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000742
743 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
744
Chris Lattner85c54652002-05-23 15:50:03 +0000745 std::vector<bool> &IsColorUsedArr = RC->getIsColorUsedArr();
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000746
Chris Lattner7e708292002-06-25 16:13:24 +0000747 for (unsigned i=0; i < NumAvailRegs; i++) // Reset array
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000748 IsColorUsedArr[i] = false;
749
Chris Lattner296b7732002-02-05 02:52:05 +0000750 ValueSet::const_iterator LIt = LVSetBef->begin();
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000751
752 // for each live var in live variable set after machine inst
Chris Lattner7e708292002-06-25 16:13:24 +0000753 for ( ; LIt != LVSetBef->end(); ++LIt) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000754
755 // get the live range corresponding to live var
756 LiveRange *const LRofLV = LRI.getLiveRangeForValue(*LIt );
757
758 // LR can be null if it is a const since a const
759 // doesn't have a dominating def - see Assumptions above
Chris Lattner7e708292002-06-25 16:13:24 +0000760 if (LRofLV && LRofLV->getRegClass() == RC && LRofLV->hasColor() )
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000761 IsColorUsedArr[ LRofLV->getColor() ] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000762 }
763
764 // It is possible that one operand of this MInst was already spilled
765 // and it received some register temporarily. If that's the case,
766 // it is recorded in machine operand. We must skip such registers.
767
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000768 setRelRegsUsedByThisInst(RC, MInst);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000769
Chris Lattner7e708292002-06-25 16:13:24 +0000770 for (unsigned c=0; c < NumAvailRegs; c++) // find first unused color
Chris Lattner85c54652002-05-23 15:50:03 +0000771 if (!IsColorUsedArr[c])
772 return MRI.getUnifiedRegNum(RC->getID(), c);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000773
Chris Lattner85c54652002-05-23 15:50:03 +0000774 return -1;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000775}
776
777
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000778//----------------------------------------------------------------------------
779// Get any other register in a register class, other than what is used
780// by operands of a machine instruction. Returns the unified reg number.
781//----------------------------------------------------------------------------
782int PhyRegAlloc::getUniRegNotUsedByThisInst(RegClass *RC,
Chris Lattner85c54652002-05-23 15:50:03 +0000783 const MachineInstr *MInst) {
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000784
Chris Lattner85c54652002-05-23 15:50:03 +0000785 vector<bool> &IsColorUsedArr = RC->getIsColorUsedArr();
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000786 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
787
Chris Lattner7e708292002-06-25 16:13:24 +0000788 for (unsigned i=0; i < NumAvailRegs ; i++) // Reset array
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000789 IsColorUsedArr[i] = false;
790
791 setRelRegsUsedByThisInst(RC, MInst);
792
Chris Lattner7e708292002-06-25 16:13:24 +0000793 for (unsigned c=0; c < RC->getNumOfAvailRegs(); c++)// find first unused color
Chris Lattner85c54652002-05-23 15:50:03 +0000794 if (!IsColorUsedArr[c])
795 return MRI.getUnifiedRegNum(RC->getID(), c);
796
797 assert(0 && "FATAL: No free register could be found in reg class!!");
Chris Lattner697954c2002-01-20 22:54:45 +0000798 return 0;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000799}
800
801
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000802//----------------------------------------------------------------------------
803// This method modifies the IsColorUsedArr of the register class passed to it.
804// It sets the bits corresponding to the registers used by this machine
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000805// instructions. Both explicit and implicit operands are set.
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000806//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000807void PhyRegAlloc::setRelRegsUsedByThisInst(RegClass *RC,
Vikram S. Advef5af6362002-07-08 23:15:32 +0000808 const MachineInstr *MInst ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000809
Vikram S. Advef5af6362002-07-08 23:15:32 +0000810 vector<bool> &IsColorUsedArr = RC->getIsColorUsedArr();
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000811
Vikram S. Advef5af6362002-07-08 23:15:32 +0000812 // Add the registers already marked as used by the instruction.
813 // This should include any scratch registers that are used to save
814 // values across the instruction (e.g., for saving state register values).
815 const hash_set<int>& regsUsed = MInst->getRegsUsed();
816 for (hash_set<int>::const_iterator SI=regsUsed.begin(), SE=regsUsed.end();
817 SI != SE; ++SI)
818 {
819 unsigned classId = 0;
820 int classRegNum = MRI.getClassRegNum(*SI, classId);
821 if (RC->getID() == classId)
822 {
823 assert(classRegNum < (int) IsColorUsedArr.size() &&
824 "Illegal register number for this reg class?");
825 IsColorUsedArr[classRegNum] = true;
826 }
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000827 }
Vikram S. Advef5af6362002-07-08 23:15:32 +0000828
829 // Now add registers allocated to the live ranges of values used in
830 // the instruction. These are not yet recorded in the instruction.
831 for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum)
832 {
833 const MachineOperand& Op = MInst->getOperand(OpNum);
834
835 if (Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
836 Op.getOperandType() == MachineOperand::MO_CCRegister)
837 if (const Value* Val = Op.getVRegValue())
838 if (MRI.getRegClassIDOfValue(Val) == RC->getID())
839 if (Op.getAllocatedRegNum() == -1)
840 if (LiveRange *LROfVal = LRI.getLiveRangeForValue(Val))
841 if (LROfVal->hasColor() )
842 // this operand is in a LR that received a color
843 IsColorUsedArr[LROfVal->getColor()] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000844 }
Vikram S. Advef5af6362002-07-08 23:15:32 +0000845
846 // If there are implicit references, mark their allocated regs as well
847 //
848 for (unsigned z=0; z < MInst->getNumImplicitRefs(); z++)
849 if (const LiveRange*
850 LRofImpRef = LRI.getLiveRangeForValue(MInst->getImplicitRef(z)))
851 if (LRofImpRef->hasColor())
852 // this implicit reference is in a LR that received a color
853 IsColorUsedArr[LRofImpRef->getColor()] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000854}
855
856
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000857//----------------------------------------------------------------------------
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000858// If there are delay slots for an instruction, the instructions
859// added after it must really go after the delayed instruction(s).
860// So, we move the InstrAfter of that instruction to the
861// corresponding delayed instruction using the following method.
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000862
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000863//----------------------------------------------------------------------------
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000864void PhyRegAlloc::move2DelayedInstr(const MachineInstr *OrigMI,
865 const MachineInstr *DelayedMI) {
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000866
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000867 // "added after" instructions of the original instr
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000868 std::vector<MachineInstr *> &OrigAft = AddedInstrMap[OrigMI].InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000869
870 // "added instructions" of the delayed instr
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000871 AddedInstrns &DelayAdI = AddedInstrMap[DelayedMI];
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000872
873 // "added after" instructions of the delayed instr
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000874 std::vector<MachineInstr *> &DelayedAft = DelayAdI.InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000875
876 // go thru all the "added after instructions" of the original instruction
877 // and append them to the "addded after instructions" of the delayed
878 // instructions
Chris Lattner697954c2002-01-20 22:54:45 +0000879 DelayedAft.insert(DelayedAft.end(), OrigAft.begin(), OrigAft.end());
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000880
881 // empty the "added after instructions" of the original instruction
882 OrigAft.clear();
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000883}
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000884
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000885//----------------------------------------------------------------------------
886// This method prints the code with registers after register allocation is
887// complete.
888//----------------------------------------------------------------------------
889void PhyRegAlloc::printMachineCode()
890{
891
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000892 cerr << "\n;************** Function " << Meth->getName()
Chris Lattner697954c2002-01-20 22:54:45 +0000893 << " *****************\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000894
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000895 for (Function::const_iterator BBI = Meth->begin(), BBE = Meth->end();
896 BBI != BBE; ++BBI) {
Chris Lattner7e708292002-06-25 16:13:24 +0000897 cerr << "\n"; printLabel(BBI); cerr << ": ";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000898
899 // get the iterator for machine instructions
Vikram S. Advef5af6362002-07-08 23:15:32 +0000900 MachineCodeForBasicBlock& MIVec = MachineCodeForBasicBlock::get(BBI);
Vikram S. Adve48762092002-04-25 04:34:15 +0000901 MachineCodeForBasicBlock::iterator MII = MIVec.begin();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000902
903 // iterate over all the machine instructions in BB
Chris Lattner7e708292002-06-25 16:13:24 +0000904 for ( ; MII != MIVec.end(); ++MII) {
Vikram S. Adve48762092002-04-25 04:34:15 +0000905 MachineInstr *const MInst = *MII;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000906
Chris Lattner697954c2002-01-20 22:54:45 +0000907 cerr << "\n\t";
908 cerr << TargetInstrDescriptors[MInst->getOpCode()].opCodeString;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000909
Chris Lattner7e708292002-06-25 16:13:24 +0000910 for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000911 MachineOperand& Op = MInst->getOperand(OpNum);
912
Chris Lattner7e708292002-06-25 16:13:24 +0000913 if (Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasanka97b8b442001-10-18 22:36:26 +0000914 Op.getOperandType() == MachineOperand::MO_CCRegister /*||
915 Op.getOperandType() == MachineOperand::MO_PCRelativeDisp*/ ) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000916
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000917 const Value *const Val = Op.getVRegValue () ;
Ruchira Sasankae727f852001-09-18 22:43:57 +0000918 // ****this code is temporary till NULL Values are fixed
Chris Lattner7e708292002-06-25 16:13:24 +0000919 if (! Val ) {
Chris Lattner697954c2002-01-20 22:54:45 +0000920 cerr << "\t<*NULL*>";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000921 continue;
922 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000923
924 // if a label or a constant
Chris Lattner7e708292002-06-25 16:13:24 +0000925 if (isa<BasicBlock>(Val)) {
Chris Lattner697954c2002-01-20 22:54:45 +0000926 cerr << "\t"; printLabel( Op.getVRegValue () );
927 } else {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000928 // else it must be a register value
929 const int RegNum = Op.getAllocatedRegNum();
930
Chris Lattner697954c2002-01-20 22:54:45 +0000931 cerr << "\t" << "%" << MRI.getUnifiedRegName( RegNum );
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000932 if (Val->hasName() )
Chris Lattner697954c2002-01-20 22:54:45 +0000933 cerr << "(" << Val->getName() << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000934 else
Chris Lattner697954c2002-01-20 22:54:45 +0000935 cerr << "(" << Val << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000936
Chris Lattner7e708292002-06-25 16:13:24 +0000937 if (Op.opIsDef() )
Chris Lattner697954c2002-01-20 22:54:45 +0000938 cerr << "*";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000939
940 const LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
Chris Lattner7e708292002-06-25 16:13:24 +0000941 if (LROfVal )
942 if (LROfVal->hasSpillOffset() )
Chris Lattner697954c2002-01-20 22:54:45 +0000943 cerr << "$";
Ruchira Sasankae727f852001-09-18 22:43:57 +0000944 }
945
946 }
Chris Lattner7e708292002-06-25 16:13:24 +0000947 else if (Op.getOperandType() == MachineOperand::MO_MachineRegister) {
Chris Lattner697954c2002-01-20 22:54:45 +0000948 cerr << "\t" << "%" << MRI.getUnifiedRegName(Op.getMachineRegNum());
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000949 }
950
951 else
Chris Lattner697954c2002-01-20 22:54:45 +0000952 cerr << "\t" << Op; // use dump field
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000953 }
954
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000955
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000956
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000957 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
Chris Lattner7e708292002-06-25 16:13:24 +0000958 if (NumOfImpRefs > 0) {
Chris Lattner697954c2002-01-20 22:54:45 +0000959 cerr << "\tImplicit:";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000960
Chris Lattner7e708292002-06-25 16:13:24 +0000961 for (unsigned z=0; z < NumOfImpRefs; z++)
Chris Lattner0665a5f2002-02-05 01:43:49 +0000962 cerr << RAV(MInst->getImplicitRef(z)) << "\t";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000963 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000964
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000965 } // for all machine instructions
966
Chris Lattner697954c2002-01-20 22:54:45 +0000967 cerr << "\n";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000968
969 } // for all BBs
970
Chris Lattner697954c2002-01-20 22:54:45 +0000971 cerr << "\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000972}
973
Ruchira Sasankae727f852001-09-18 22:43:57 +0000974
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000975//----------------------------------------------------------------------------
976
977//----------------------------------------------------------------------------
978void PhyRegAlloc::colorIncomingArgs()
979{
Chris Lattner7e708292002-06-25 16:13:24 +0000980 const BasicBlock &FirstBB = Meth->front();
Vikram S. Advef5af6362002-07-08 23:15:32 +0000981 const MachineInstr *FirstMI = MachineCodeForBasicBlock::get(&FirstBB).front();
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000982 assert(FirstMI && "No machine instruction in entry BB");
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000983
Vikram S. Adve48762092002-04-25 04:34:15 +0000984 MRI.colorMethodArgs(Meth, LRI, &AddedInstrAtEntry);
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000985}
986
Ruchira Sasankae727f852001-09-18 22:43:57 +0000987
988//----------------------------------------------------------------------------
989// Used to generate a label for a basic block
990//----------------------------------------------------------------------------
Chris Lattner697954c2002-01-20 22:54:45 +0000991void PhyRegAlloc::printLabel(const Value *const Val) {
992 if (Val->hasName())
993 cerr << Val->getName();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000994 else
Chris Lattner697954c2002-01-20 22:54:45 +0000995 cerr << "Label" << Val;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000996}
997
998
Ruchira Sasankae727f852001-09-18 22:43:57 +0000999//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001000// This method calls setSugColorUsable method of each live range. This
1001// will determine whether the suggested color of LR is really usable.
1002// A suggested color is not usable when the suggested color is volatile
1003// AND when there are call interferences
1004//----------------------------------------------------------------------------
1005
1006void PhyRegAlloc::markUnusableSugColors()
1007{
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001008 // hash map iterator
1009 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
1010 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
1011
Chris Lattner7e708292002-06-25 16:13:24 +00001012 for (; HMI != HMIEnd ; ++HMI ) {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001013 if (HMI->first) {
1014 LiveRange *L = HMI->second; // get the LiveRange
1015 if (L) {
Chris Lattner7e708292002-06-25 16:13:24 +00001016 if (L->hasSuggestedColor()) {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001017 int RCID = L->getRegClass()->getID();
Chris Lattner7e708292002-06-25 16:13:24 +00001018 if (MRI.isRegVolatile( RCID, L->getSuggestedColor()) &&
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001019 L->isCallInterference() )
1020 L->setSuggestedColorUsable( false );
1021 else
1022 L->setSuggestedColorUsable( true );
1023 }
1024 } // if L->hasSuggestedColor()
1025 }
1026 } // for all LR's in hash map
1027}
1028
1029
1030
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001031//----------------------------------------------------------------------------
1032// The following method will set the stack offsets of the live ranges that
1033// are decided to be spillled. This must be called just after coloring the
1034// LRs using the graph coloring algo. For each live range that is spilled,
1035// this method allocate a new spill position on the stack.
1036//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001037
Chris Lattner37730942002-02-05 03:52:29 +00001038void PhyRegAlloc::allocateStackSpace4SpilledLRs() {
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001039 if (DEBUG_RA) cerr << "\nSetting LR stack offsets for spills...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001040
Chris Lattner37730942002-02-05 03:52:29 +00001041 LiveRangeMapType::const_iterator HMI = LRI.getLiveRangeMap()->begin();
1042 LiveRangeMapType::const_iterator HMIEnd = LRI.getLiveRangeMap()->end();
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001043
Chris Lattner7e708292002-06-25 16:13:24 +00001044 for ( ; HMI != HMIEnd ; ++HMI) {
Chris Lattner37730942002-02-05 03:52:29 +00001045 if (HMI->first && HMI->second) {
1046 LiveRange *L = HMI->second; // get the LiveRange
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001047 if (!L->hasColor()) { // NOTE: ** allocating the size of long Type **
1048 int stackOffset = mcInfo.allocateSpilledValue(TM, Type::LongTy);
1049 L->setSpillOffFromFP(stackOffset);
1050 if (DEBUG_RA)
1051 cerr << " LR# " << L->getUserIGNode()->getIndex()
1052 << ": stack-offset = " << stackOffset << "\n";
1053 }
Chris Lattner37730942002-02-05 03:52:29 +00001054 }
1055 } // for all LR's in hash map
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001056}
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001057
1058
1059
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001060//----------------------------------------------------------------------------
Ruchira Sasankae727f852001-09-18 22:43:57 +00001061// The entry pont to Register Allocation
1062//----------------------------------------------------------------------------
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001063
1064void PhyRegAlloc::allocateRegisters()
1065{
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001066
1067 // make sure that we put all register classes into the RegClassList
1068 // before we call constructLiveRanges (now done in the constructor of
1069 // PhyRegAlloc class).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001070 //
1071 LRI.constructLiveRanges(); // create LR info
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001072
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001073 if (DEBUG_RA >= RA_DEBUG_LiveRanges)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001074 LRI.printLiveRanges();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001075
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001076 createIGNodeListsAndIGs(); // create IGNode list and IGs
1077
1078 buildInterferenceGraphs(); // build IGs in all reg classes
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001079
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001080
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001081 if (DEBUG_RA >= RA_DEBUG_LiveRanges) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001082 // print all LRs in all reg classes
Chris Lattner7e708292002-06-25 16:13:24 +00001083 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
1084 RegClassList[rc]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001085
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001086 // print IGs in all register classes
Chris Lattner7e708292002-06-25 16:13:24 +00001087 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
1088 RegClassList[rc]->printIG();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001089 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001090
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001091
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001092 LRI.coalesceLRs(); // coalesce all live ranges
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001093
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +00001094
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001095 if (DEBUG_RA >= RA_DEBUG_LiveRanges) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001096 // print all LRs in all reg classes
Chris Lattner7e708292002-06-25 16:13:24 +00001097 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001098 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001099
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001100 // print IGs in all register classes
Chris Lattner7e708292002-06-25 16:13:24 +00001101 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001102 RegClassList[ rc ]->printIG();
1103 }
1104
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001105
1106 // mark un-usable suggested color before graph coloring algorithm.
1107 // When this is done, the graph coloring algo will not reserve
1108 // suggested color unnecessarily - they can be used by another LR
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001109 //
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001110 markUnusableSugColors();
1111
1112 // color all register classes using the graph coloring algo
Chris Lattner7e708292002-06-25 16:13:24 +00001113 for (unsigned rc=0; rc < NumOfRegClasses ; rc++)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001114 RegClassList[ rc ]->colorAllRegs();
1115
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001116 // Atter grpah coloring, if some LRs did not receive a color (i.e, spilled)
1117 // a poistion for such spilled LRs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001118 //
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001119 allocateStackSpace4SpilledLRs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001120
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001121 mcInfo.popAllTempValues(TM); // TODO **Check
1122
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001123 // color incoming args - if the correct color was not received
1124 // insert code to copy to the correct register
1125 //
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001126 colorIncomingArgs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001127
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001128 // Now update the machine code with register names and add any
1129 // additional code inserted by the register allocator to the instruction
1130 // stream
1131 //
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001132 updateMachineCode();
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001133
Chris Lattner045e7c82001-09-19 16:26:23 +00001134 if (DEBUG_RA) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001135 cerr << "\n**** Machine Code After Register Allocation:\n\n";
Vikram S. Adve12af1642001-11-08 04:48:50 +00001136 MachineCodeForMethod::get(Meth).dump();
Chris Lattner045e7c82001-09-19 16:26:23 +00001137 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001138}
1139
Ruchira Sasankae727f852001-09-18 22:43:57 +00001140
1141