blob: c7c9756ce77600ae22af53e129aaa5055f9c481c [file] [log] [blame]
Vikram S. Adve12af1642001-11-08 04:48:50 +00001// $Id$
2//***************************************************************************
3// File:
4// PhyRegAlloc.cpp
5//
6// Purpose:
7// Register allocation for LLVM.
8//
9// History:
10// 9/10/01 - Ruchira Sasanka - created.
11//**************************************************************************/
Ruchira Sasanka8e604792001-09-14 21:18:34 +000012
Chris Lattner6dd98a62002-02-04 00:33:08 +000013#include "llvm/CodeGen/RegisterAllocation.h"
Vikram S. Adve12af1642001-11-08 04:48:50 +000014#include "llvm/CodeGen/PhyRegAlloc.h"
15#include "llvm/CodeGen/MachineInstr.h"
Vikram S. Advedabb41d2002-05-19 15:29:31 +000016#include "llvm/CodeGen/MachineInstrAnnot.h"
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000017#include "llvm/CodeGen/MachineCodeForMethod.h"
Chris Lattner483e14e2002-04-27 07:27:19 +000018#include "llvm/Analysis/LiveVar/FunctionLiveVarInfo.h"
Chris Lattner14ab1ce2002-02-04 17:48:00 +000019#include "llvm/Analysis/LoopInfo.h"
Vikram S. Adve12af1642001-11-08 04:48:50 +000020#include "llvm/Target/TargetMachine.h"
21#include "llvm/Target/MachineFrameInfo.h"
Chris Lattner221d6882002-02-12 21:07:25 +000022#include "llvm/BasicBlock.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000023#include "llvm/Function.h"
Chris Lattner37730942002-02-05 03:52:29 +000024#include "llvm/Type.h"
Vikram S. Advedabb41d2002-05-19 15:29:31 +000025#include "llvm/iOther.h"
Chris Lattnerc6f3ae52002-04-29 17:42:12 +000026#include "llvm/CodeGen/RegAllocCommon.h"
Chris Lattner70e60cb2002-05-22 17:08:27 +000027#include "Support/CommandLine.h"
Chris Lattner697954c2002-01-20 22:54:45 +000028#include <iostream>
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000029#include <math.h>
Chris Lattner697954c2002-01-20 22:54:45 +000030using std::cerr;
Vikram S. Adve12af1642001-11-08 04:48:50 +000031
Chris Lattner70e60cb2002-05-22 17:08:27 +000032RegAllocDebugLevel_t DEBUG_RA;
33static cl::Enum<RegAllocDebugLevel_t> DEBUG_RA_c(DEBUG_RA, "dregalloc",
34 cl::Hidden,
Chris Lattner045e7c82001-09-19 16:26:23 +000035 "enable register allocation debugging information",
36 clEnumValN(RA_DEBUG_None , "n", "disable debug output"),
37 clEnumValN(RA_DEBUG_Normal , "y", "enable debug output"),
38 clEnumValN(RA_DEBUG_Verbose, "v", "enable extra debug output"), 0);
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000039
40
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 Lattner1b7f7dc2002-04-28 16:21:30 +000065 AU.addRequired(LoopInfo::ID);
Chris Lattner483e14e2002-04-27 07:27:19 +000066 AU.addRequired(FunctionLiveVarInfo::ID);
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() {
110 if (DEBUG_RA) 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) {
Chris Lattner7e708292002-06-25 16:13:24 +0000122 if (DEBUG_RA) {
Chris Lattner0665a5f2002-02-05 01:43:49 +0000123 cerr << "\n*?!?Warning: Null liver range found for: "
124 << RAV(HMI->first) << "\n";
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000125 }
126 continue;
127 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000128 // if the Value * is not null, and LR
129 // is not yet written to the IGNodeList
Chris Lattner7e708292002-06-25 16:13:24 +0000130 if (!(L->getUserIGNode()) ) {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000131 RegClass *const RC = // RegClass of first value in the LR
132 RegClassList[ L->getRegClass()->getID() ];
133
134 RC->addLRToIG(L); // add this LR to an IG
135 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000136 }
137 }
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000138
139 // init RegClassList
Chris Lattner7e708292002-06-25 16:13:24 +0000140 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000141 RegClassList[rc]->createInterferenceGraph();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000142
Chris Lattner7e708292002-06-25 16:13:24 +0000143 if (DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000144 cerr << "LRLists Created!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000145}
146
147
148
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000149
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000150//----------------------------------------------------------------------------
151// This method will add all interferences at for a given instruction.
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000152// Interence occurs only if the LR of Def (Inst or Arg) is of the same reg
153// class as that of live var. The live var passed to this function is the
154// LVset AFTER the instruction
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000155//----------------------------------------------------------------------------
Chris Lattner296b7732002-02-05 02:52:05 +0000156void PhyRegAlloc::addInterference(const Value *Def,
157 const ValueSet *LVSet,
158 bool isCallInst) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000159
Chris Lattner296b7732002-02-05 02:52:05 +0000160 ValueSet::const_iterator LIt = LVSet->begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000161
162 // get the live range of instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000163 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000164 const LiveRange *const LROfDef = LRI.getLiveRangeForValue( Def );
165
166 IGNode *const IGNodeOfDef = LROfDef->getUserIGNode();
167 assert( IGNodeOfDef );
168
169 RegClass *const RCOfDef = LROfDef->getRegClass();
170
171 // for each live var in live variable set
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000172 //
Chris Lattner7e708292002-06-25 16:13:24 +0000173 for ( ; LIt != LVSet->end(); ++LIt) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000174
Chris Lattner0665a5f2002-02-05 01:43:49 +0000175 if (DEBUG_RA > 1)
176 cerr << "< Def=" << RAV(Def) << ", Lvar=" << RAV(*LIt) << "> ";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000177
178 // get the live range corresponding to live var
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000179 //
Chris Lattner0665a5f2002-02-05 01:43:49 +0000180 LiveRange *LROfVar = LRI.getLiveRangeForValue(*LIt);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000181
182 // LROfVar can be null if it is a const since a const
183 // doesn't have a dominating def - see Assumptions above
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000184 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000185 if (LROfVar) {
Chris Lattner7e708292002-06-25 16:13:24 +0000186 if (LROfDef == LROfVar) // do not set interf for same LR
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000187 continue;
188
189 // if 2 reg classes are the same set interference
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000190 //
Chris Lattner0665a5f2002-02-05 01:43:49 +0000191 if (RCOfDef == LROfVar->getRegClass()) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000192 RCOfDef->setInterference( LROfDef, LROfVar);
Chris Lattner0665a5f2002-02-05 01:43:49 +0000193 } else if (DEBUG_RA > 1) {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000194 // we will not have LRs for values not explicitly allocated in the
195 // instruction stream (e.g., constants)
Chris Lattner0665a5f2002-02-05 01:43:49 +0000196 cerr << " warning: no live range for " << RAV(*LIt) << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000197 }
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000198 }
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000199 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000200}
201
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000202
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000203
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000204//----------------------------------------------------------------------------
205// For a call instruction, this method sets the CallInterference flag in
206// the LR of each variable live int the Live Variable Set live after the
207// call instruction (except the return value of the call instruction - since
208// the return value does not interfere with that call itself).
209//----------------------------------------------------------------------------
210
211void PhyRegAlloc::setCallInterferences(const MachineInstr *MInst,
Chris Lattner296b7732002-02-05 02:52:05 +0000212 const ValueSet *LVSetAft) {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000213
Chris Lattner7e708292002-06-25 16:13:24 +0000214 if (DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000215 cerr << "\n For call inst: " << *MInst;
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000216
Chris Lattner296b7732002-02-05 02:52:05 +0000217 ValueSet::const_iterator LIt = LVSetAft->begin();
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000218
219 // for each live var in live variable set after machine inst
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000220 //
Chris Lattner7e708292002-06-25 16:13:24 +0000221 for ( ; LIt != LVSetAft->end(); ++LIt) {
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000222
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000223 // get the live range corresponding to live var
224 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000225 LiveRange *const LR = LRI.getLiveRangeForValue(*LIt );
226
Chris Lattner7e708292002-06-25 16:13:24 +0000227 if (LR && DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000228 cerr << "\n\tLR Aft Call: ";
Chris Lattner296b7732002-02-05 02:52:05 +0000229 printSet(*LR);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000230 }
231
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000232 // LR can be null if it is a const since a const
233 // doesn't have a dominating def - see Assumptions above
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000234 //
Chris Lattner7e708292002-06-25 16:13:24 +0000235 if (LR ) {
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000236 LR->setCallInterference();
Chris Lattner7e708292002-06-25 16:13:24 +0000237 if (DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000238 cerr << "\n ++Added call interf for LR: " ;
Chris Lattner296b7732002-02-05 02:52:05 +0000239 printSet(*LR);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000240 }
241 }
242
243 }
244
Vikram S. Adve1a53f032002-03-31 18:54:37 +0000245 // Now find the LR of the return value of the call
246 // We do this because, we look at the LV set *after* the instruction
247 // to determine, which LRs must be saved across calls. The return value
248 // of the call is live in this set - but it does not interfere with call
249 // (i.e., we can allocate a volatile register to the return value)
250 //
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000251 CallArgsDescriptor* argDesc = CallArgsDescriptor::get(MInst);
252
253 if (const Value *RetVal = argDesc->getReturnValue()) {
Vikram S. Adve1a53f032002-03-31 18:54:37 +0000254 LiveRange *RetValLR = LRI.getLiveRangeForValue( RetVal );
255 assert( RetValLR && "No LR for RetValue of call");
256 RetValLR->clearCallInterference();
257 }
258
259 // If the CALL is an indirect call, find the LR of the function pointer.
260 // That has a call interference because it conflicts with outgoing args.
Chris Lattner7e708292002-06-25 16:13:24 +0000261 if (const Value *AddrVal = argDesc->getIndirectFuncPtr()) {
Vikram S. Adve1a53f032002-03-31 18:54:37 +0000262 LiveRange *AddrValLR = LRI.getLiveRangeForValue( AddrVal );
263 assert( AddrValLR && "No LR for indirect addr val of call");
264 AddrValLR->setCallInterference();
265 }
266
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000267}
268
269
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000270
271
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000272//----------------------------------------------------------------------------
273// This method will walk thru code and create interferences in the IG of
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000274// each RegClass. Also, this method calculates the spill cost of each
275// Live Range (it is done in this method to save another pass over the code).
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000276//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000277void PhyRegAlloc::buildInterferenceGraphs()
278{
279
Chris Lattner7e708292002-06-25 16:13:24 +0000280 if (DEBUG_RA) cerr << "Creating interference graphs ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000281
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000282 unsigned BBLoopDepthCost;
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000283 for (Function::const_iterator BBI = Meth->begin(), BBE = Meth->end();
284 BBI != BBE; ++BBI) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000285
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000286 // find the 10^(loop_depth) of this BB
287 //
Chris Lattner7e708292002-06-25 16:13:24 +0000288 BBLoopDepthCost = (unsigned)pow(10.0, LoopDepthCalc->getLoopDepth(BBI));
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000289
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000290 // get the iterator for machine instructions
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000291 //
Chris Lattner7e708292002-06-25 16:13:24 +0000292 const MachineCodeForBasicBlock& MIVec = BBI->getMachineInstrVec();
Vikram S. Adve48762092002-04-25 04:34:15 +0000293 MachineCodeForBasicBlock::const_iterator MII = MIVec.begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000294
295 // iterate over all the machine instructions in BB
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000296 //
Chris Lattner7e708292002-06-25 16:13:24 +0000297 for ( ; MII != MIVec.end(); ++MII) {
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000298
Vikram S. Adve48762092002-04-25 04:34:15 +0000299 const MachineInstr *MInst = *MII;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000300
301 // get the LV set after the instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000302 //
Chris Lattner7e708292002-06-25 16:13:24 +0000303 const ValueSet &LVSetAI = LVI->getLiveVarSetAfterMInst(MInst, BBI);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000304
305 const bool isCallInst = TM.getInstrInfo().isCall(MInst->getOpCode());
306
Chris Lattner7e708292002-06-25 16:13:24 +0000307 if (isCallInst ) {
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000308 // set the isCallInterference flag of each live range wich extends
309 // accross this call instruction. This information is used by graph
310 // coloring algo to avoid allocating volatile colors to live ranges
311 // that span across calls (since they have to be saved/restored)
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000312 //
Chris Lattner748697d2002-02-05 04:20:12 +0000313 setCallInterferences(MInst, &LVSetAI);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000314 }
315
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000316
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000317 // iterate over all MI operands to find defs
318 //
Chris Lattner2f898d22002-02-05 06:02:59 +0000319 for (MachineInstr::const_val_op_iterator OpI = MInst->begin(),
320 OpE = MInst->end(); OpI != OpE; ++OpI) {
321 if (OpI.isDef()) // create a new LR iff this operand is a def
Chris Lattner748697d2002-02-05 04:20:12 +0000322 addInterference(*OpI, &LVSetAI, isCallInst);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000323
324 // Calculate the spill cost of each live range
325 //
Chris Lattner2f898d22002-02-05 06:02:59 +0000326 LiveRange *LR = LRI.getLiveRangeForValue(*OpI);
327 if (LR) LR->addSpillCost(BBLoopDepthCost);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000328 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000329
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000330
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000331 // if there are multiple defs in this instruction e.g. in SETX
332 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000333 if (TM.getInstrInfo().isPseudoInstr(MInst->getOpCode()))
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000334 addInterf4PseudoInstr(MInst);
335
336
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000337 // Also add interference for any implicit definitions in a machine
338 // instr (currently, only calls have this).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000339 //
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000340 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
Chris Lattner7e708292002-06-25 16:13:24 +0000341 if ( NumOfImpRefs > 0 ) {
342 for (unsigned z=0; z < NumOfImpRefs; z++)
343 if (MInst->implicitRefIsDefined(z) )
Chris Lattner748697d2002-02-05 04:20:12 +0000344 addInterference( MInst->getImplicitRef(z), &LVSetAI, isCallInst );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000345 }
346
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +0000347
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000348 } // for all machine instructions in BB
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000349 } // for all BBs in function
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000350
351
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000352 // add interferences for function arguments. Since there are no explict
353 // defs in the function for args, we have to add them manually
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000354 //
355 addInterferencesForArgs();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000356
Chris Lattner7e708292002-06-25 16:13:24 +0000357 if (DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000358 cerr << "Interference graphs calculted!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000359
360}
361
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000362
363
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000364//--------------------------------------------------------------------------
365// Pseudo instructions will be exapnded to multiple instructions by the
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000366// assembler. Consequently, all the opernds must get distinct registers.
367// Therefore, we mark all operands of a pseudo instruction as they interfere
368// with one another.
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000369//--------------------------------------------------------------------------
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000370void PhyRegAlloc::addInterf4PseudoInstr(const MachineInstr *MInst) {
371
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000372 bool setInterf = false;
373
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000374 // iterate over MI operands to find defs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000375 //
Chris Lattner2f898d22002-02-05 06:02:59 +0000376 for (MachineInstr::const_val_op_iterator It1 = MInst->begin(),
377 ItE = MInst->end(); It1 != ItE; ++It1) {
378 const LiveRange *LROfOp1 = LRI.getLiveRangeForValue(*It1);
379 assert((LROfOp1 || !It1.isDef()) && "No LR for Def in PSEUDO insruction");
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000380
Chris Lattner2f898d22002-02-05 06:02:59 +0000381 MachineInstr::const_val_op_iterator It2 = It1;
Chris Lattner7e708292002-06-25 16:13:24 +0000382 for (++It2; It2 != ItE; ++It2) {
Chris Lattner2f898d22002-02-05 06:02:59 +0000383 const LiveRange *LROfOp2 = LRI.getLiveRangeForValue(*It2);
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000384
Chris Lattner2f898d22002-02-05 06:02:59 +0000385 if (LROfOp2) {
386 RegClass *RCOfOp1 = LROfOp1->getRegClass();
387 RegClass *RCOfOp2 = LROfOp2->getRegClass();
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000388
Chris Lattner7e708292002-06-25 16:13:24 +0000389 if (RCOfOp1 == RCOfOp2 ){
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000390 RCOfOp1->setInterference( LROfOp1, LROfOp2 );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000391 setInterf = true;
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000392 }
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000393 } // if Op2 has a LR
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000394 } // for all other defs in machine instr
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000395 } // for all operands in an instruction
396
Chris Lattner2f898d22002-02-05 06:02:59 +0000397 if (!setInterf && MInst->getNumOperands() > 2) {
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000398 cerr << "\nInterf not set for any operand in pseudo instr:\n";
399 cerr << *MInst;
400 assert(0 && "Interf not set for pseudo instr with > 2 operands" );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000401 }
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000402}
403
404
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000405
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000406//----------------------------------------------------------------------------
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000407// This method will add interferences for incoming arguments to a function.
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000408//----------------------------------------------------------------------------
Chris Lattner296b7732002-02-05 02:52:05 +0000409void PhyRegAlloc::addInterferencesForArgs() {
410 // get the InSet of root BB
Chris Lattner7e708292002-06-25 16:13:24 +0000411 const ValueSet &InSet = LVI->getInSetOfBB(&Meth->front());
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000412
Chris Lattner7e708292002-06-25 16:13:24 +0000413 for (Function::const_aiterator AI = Meth->abegin(); AI != Meth->aend(); ++AI) {
414 // add interferences between args and LVars at start
415 addInterference(AI, &InSet, false);
416
417 if (DEBUG_RA > 1)
418 cerr << " - %% adding interference for argument " << RAV(AI) << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000419 }
420}
421
422
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000423//----------------------------------------------------------------------------
424// This method is called after register allocation is complete to set the
425// allocated reisters in the machine code. This code will add register numbers
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000426// to MachineOperands that contain a Value. Also it calls target specific
427// methods to produce caller saving instructions. At the end, it adds all
428// additional instructions produced by the register allocator to the
429// instruction stream.
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000430//----------------------------------------------------------------------------
Vikram S. Adve48762092002-04-25 04:34:15 +0000431
432//-----------------------------
433// Utility functions used below
434//-----------------------------
435inline void
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000436PrependInstructions(vector<MachineInstr *> &IBef,
Vikram S. Adve48762092002-04-25 04:34:15 +0000437 MachineCodeForBasicBlock& MIVec,
438 MachineCodeForBasicBlock::iterator& MII,
439 const std::string& msg)
440{
441 if (!IBef.empty())
442 {
443 MachineInstr* OrigMI = *MII;
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000444 std::vector<MachineInstr *>::iterator AdIt;
Vikram S. Adve48762092002-04-25 04:34:15 +0000445 for (AdIt = IBef.begin(); AdIt != IBef.end() ; ++AdIt)
446 {
447 if (DEBUG_RA) {
448 if (OrigMI) cerr << "For MInst: " << *OrigMI;
449 cerr << msg << " PREPENDed instr: " << **AdIt << "\n";
450 }
451 MII = MIVec.insert(MII, *AdIt);
452 ++MII;
453 }
454 }
455}
456
457inline void
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000458AppendInstructions(std::vector<MachineInstr *> &IAft,
Vikram S. Adve48762092002-04-25 04:34:15 +0000459 MachineCodeForBasicBlock& MIVec,
460 MachineCodeForBasicBlock::iterator& MII,
461 const std::string& msg)
462{
463 if (!IAft.empty())
464 {
465 MachineInstr* OrigMI = *MII;
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000466 std::vector<MachineInstr *>::iterator AdIt;
Chris Lattner7e708292002-06-25 16:13:24 +0000467 for ( AdIt = IAft.begin(); AdIt != IAft.end() ; ++AdIt )
Vikram S. Adve48762092002-04-25 04:34:15 +0000468 {
Chris Lattner7e708292002-06-25 16:13:24 +0000469 if (DEBUG_RA) {
Vikram S. Adve48762092002-04-25 04:34:15 +0000470 if (OrigMI) cerr << "For MInst: " << *OrigMI;
471 cerr << msg << " APPENDed instr: " << **AdIt << "\n";
472 }
473 ++MII; // insert before the next instruction
474 MII = MIVec.insert(MII, *AdIt);
475 }
476 }
477}
478
479
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000480void PhyRegAlloc::updateMachineCode()
481{
Chris Lattner7e708292002-06-25 16:13:24 +0000482 MachineCodeForBasicBlock& MIVec = Meth->getEntryNode().getMachineInstrVec();
Vikram S. Adve48762092002-04-25 04:34:15 +0000483
Chris Lattner7e708292002-06-25 16:13:24 +0000484 // Insert any instructions needed at method entry
485 MachineCodeForBasicBlock::iterator MII = MIVec.begin();
486 PrependInstructions(AddedInstrAtEntry.InstrnsBefore, MIVec, MII,
487 "At function entry: \n");
488 assert(AddedInstrAtEntry.InstrnsAfter.empty() &&
489 "InstrsAfter should be unnecessary since we are just inserting at "
490 "the function entry point here.");
Vikram S. Adve48762092002-04-25 04:34:15 +0000491
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000492 for (Function::const_iterator BBI = Meth->begin(), BBE = Meth->end();
493 BBI != BBE; ++BBI) {
Vikram S. Adve48762092002-04-25 04:34:15 +0000494
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000495 // iterate over all the machine instructions in BB
Chris Lattner7e708292002-06-25 16:13:24 +0000496 MachineCodeForBasicBlock &MIVec = BBI->getMachineInstrVec();
497 for (MachineCodeForBasicBlock::iterator MII = MIVec.begin();
Vikram S. Adve48762092002-04-25 04:34:15 +0000498 MII != MIVec.end(); ++MII) {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000499
Vikram S. Adve48762092002-04-25 04:34:15 +0000500 MachineInstr *MInst = *MII;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000501
502 unsigned Opcode = MInst->getOpCode();
503
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000504 // do not process Phis
Vikram S. Adve23a4c8f2002-03-18 03:37:19 +0000505 if (TM.getInstrInfo().isDummyPhiInstr(Opcode))
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000506 continue;
507
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000508 // Now insert speical instructions (if necessary) for call/return
509 // instructions.
510 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000511 if (TM.getInstrInfo().isCall(Opcode) ||
512 TM.getInstrInfo().isReturn(Opcode)) {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000513
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000514 AddedInstrns &AI = AddedInstrMap[MInst];
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000515
516 // Tmp stack poistions are needed by some calls that have spilled args
517 // So reset it before we call each such method
Ruchira Sasanka6a3db8c2002-01-07 21:09:06 +0000518 //
519 mcInfo.popAllTempValues(TM);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000520
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000521 if (TM.getInstrInfo().isCall(Opcode))
Chris Lattner7e708292002-06-25 16:13:24 +0000522 MRI.colorCallArgs(MInst, LRI, &AI, *this, BBI);
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000523 else if (TM.getInstrInfo().isReturn(Opcode))
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000524 MRI.colorRetValue(MInst, LRI, &AI);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000525 }
526
527
528 /* -- Using above code instead of this
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000529
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000530 // if this machine instr is call, insert caller saving code
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000531
Chris Lattner7e708292002-06-25 16:13:24 +0000532 if ((TM.getInstrInfo()).isCall( MInst->getOpCode()) )
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000533 MRI.insertCallerSavingCode(MInst, *BBI, *this );
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000534
535 */
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000536
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000537
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000538 // reset the stack offset for temporary variables since we may
539 // need that to spill
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000540 // mcInfo.popAllTempValues(TM);
Ruchira Sasankaf90870f2001-11-15 22:02:06 +0000541 // TODO ** : do later
Vikram S. Adve12af1642001-11-08 04:48:50 +0000542
Chris Lattner7e708292002-06-25 16:13:24 +0000543 //for (MachineInstr::val_const_op_iterator OpI(MInst);!OpI.done();++OpI) {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000544
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000545
546 // Now replace set the registers for operands in the machine instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000547 //
Chris Lattner7e708292002-06-25 16:13:24 +0000548 for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000549
550 MachineOperand& Op = MInst->getOperand(OpNum);
551
Chris Lattner7e708292002-06-25 16:13:24 +0000552 if (Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000553 Op.getOperandType() == MachineOperand::MO_CCRegister) {
554
555 const Value *const Val = Op.getVRegValue();
556
557 // delete this condition checking later (must assert if Val is null)
Chris Lattner7e708292002-06-25 16:13:24 +0000558 if (!Val) {
Chris Lattner045e7c82001-09-19 16:26:23 +0000559 if (DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000560 cerr << "Warning: NULL Value found for operand\n";
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000561 continue;
562 }
563 assert( Val && "Value is NULL");
564
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000565 LiveRange *const LR = LRI.getLiveRangeForValue(Val);
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000566
Chris Lattner7e708292002-06-25 16:13:24 +0000567 if (!LR ) {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000568
569 // nothing to worry if it's a const or a label
570
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000571 if (DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000572 cerr << "*NO LR for operand : " << Op ;
573 cerr << " [reg:" << Op.getAllocatedRegNum() << "]";
574 cerr << " in inst:\t" << *MInst << "\n";
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000575 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000576
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000577 // if register is not allocated, mark register as invalid
Chris Lattner7e708292002-06-25 16:13:24 +0000578 if (Op.getAllocatedRegNum() == -1)
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000579 Op.setRegForValue( MRI.getInvalidRegNum());
Ruchira Sasankae727f852001-09-18 22:43:57 +0000580
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000581
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000582 continue;
583 }
584
585 unsigned RCID = (LR->getRegClass())->getID();
586
Chris Lattner7e708292002-06-25 16:13:24 +0000587 if (LR->hasColor() ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000588 Op.setRegForValue( MRI.getUnifiedRegNum(RCID, LR->getColor()) );
589 }
590 else {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000591
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000592 // LR did NOT receive a color (register). Now, insert spill code
593 // for spilled opeands in this machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000594
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000595 //assert(0 && "LR must be spilled");
Chris Lattner7e708292002-06-25 16:13:24 +0000596 insertCode4SpilledLR(LR, MInst, BBI, OpNum );
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000597
598 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000599 }
600
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000601 } // for each operand
602
603
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000604 // Now add instructions that the register allocator inserts before/after
605 // this machine instructions (done only for calls/rets/incoming args)
606 // We do this here, to ensure that spill for an instruction is inserted
607 // closest as possible to an instruction (see above insertCode4Spill...)
608 //
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000609 // If there are instructions to be added, *before* this machine
610 // instruction, add them now.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000611 //
Chris Lattner7e708292002-06-25 16:13:24 +0000612 if (AddedInstrMap.count(MInst)) {
Vikram S. Adve48762092002-04-25 04:34:15 +0000613 PrependInstructions(AddedInstrMap[MInst].InstrnsBefore, MIVec, MII,"");
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000614 }
Vikram S. Adve48762092002-04-25 04:34:15 +0000615
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000616 // If there are instructions to be added *after* this machine
617 // instruction, add them now
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000618 //
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000619 if (!AddedInstrMap[MInst].InstrnsAfter.empty()) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000620
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000621 // if there are delay slots for this instruction, the instructions
622 // added after it must really go after the delayed instruction(s)
623 // So, we move the InstrAfter of the current instruction to the
624 // corresponding delayed instruction
625
626 unsigned delay;
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000627 if ((delay=TM.getInstrInfo().getNumDelaySlots(MInst->getOpCode())) >0){
Vikram S. Adve48762092002-04-25 04:34:15 +0000628 move2DelayedInstr(MInst, *(MII+delay) );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000629
Chris Lattner7e708292002-06-25 16:13:24 +0000630 if (DEBUG_RA) cerr<< "\nMoved an added instr after the delay slot";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000631 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000632
633 else {
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000634 // Here we can add the "instructions after" to the current
635 // instruction since there are no delay slots for this instruction
Vikram S. Adve48762092002-04-25 04:34:15 +0000636 AppendInstructions(AddedInstrMap[MInst].InstrnsAfter, MIVec, MII,"");
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000637 } // if not delay
638
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000639 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000640
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000641 } // for each machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000642 }
643}
644
645
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000646
647//----------------------------------------------------------------------------
648// This method inserts spill code for AN operand whose LR was spilled.
649// This method may be called several times for a single machine instruction
650// if it contains many spilled operands. Each time it is called, it finds
651// a register which is not live at that instruction and also which is not
652// used by other spilled operands of the same instruction. Then it uses
653// this register temporarily to accomodate the spilled value.
654//----------------------------------------------------------------------------
655void PhyRegAlloc::insertCode4SpilledLR(const LiveRange *LR,
656 MachineInstr *MInst,
657 const BasicBlock *BB,
658 const unsigned OpNum) {
659
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000660 assert(! TM.getInstrInfo().isCall(MInst->getOpCode()) &&
661 (! TM.getInstrInfo().isReturn(MInst->getOpCode())) &&
662 "Arg of a call/ret must be handled elsewhere");
663
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000664 MachineOperand& Op = MInst->getOperand(OpNum);
665 bool isDef = MInst->operandIsDefined(OpNum);
666 unsigned RegType = MRI.getRegType( LR );
667 int SpillOff = LR->getSpillOffFromFP();
668 RegClass *RC = LR->getRegClass();
Chris Lattner748697d2002-02-05 04:20:12 +0000669 const ValueSet &LVSetBef = LVI->getLiveVarSetBeforeMInst(MInst, BB);
Vikram S. Adve00521d72001-11-12 23:26:35 +0000670
Chris Lattner697954c2002-01-20 22:54:45 +0000671 mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000672
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000673 MachineInstr *MIBef=NULL, *MIAft=NULL;
674 vector<MachineInstr*> AdIMid;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000675
Chris Lattner748697d2002-02-05 04:20:12 +0000676 int TmpRegU = getUsableUniRegAtMI(RC, RegType, MInst,&LVSetBef, MIBef, MIAft);
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000677
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000678 // get the added instructions for this instruciton
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000679 AddedInstrns &AI = AddedInstrMap[MInst];
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000680
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000681 if (!isDef) {
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000682 // for a USE, we have to load the value of LR from stack to a TmpReg
683 // and use the TmpReg as one operand of instruction
684
685 // actual loading instruction
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000686 MRI.cpMem2RegMI(MRI.getFramePointer(), SpillOff, TmpRegU,RegType, AdIMid);
687 AI.InstrnsBefore.insert(AI.InstrnsBefore.end(),
688 AdIMid.begin(), AdIMid.end());
689
Chris Lattner7e708292002-06-25 16:13:24 +0000690 if (MIBef)
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000691 AI.InstrnsBefore.push_back(MIBef);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000692
Chris Lattner7e708292002-06-25 16:13:24 +0000693 if (MIAft)
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000694 AI.InstrnsAfter.insert(AI.InstrnsAfter.begin(), MIAft);
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000695
Chris Lattner296b7732002-02-05 02:52:05 +0000696 } else { // if this is a Def
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000697 // for a DEF, we have to store the value produced by this instruction
698 // on the stack position allocated for this LR
699
700 // actual storing instruction
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000701 MRI.cpReg2MemMI(TmpRegU, MRI.getFramePointer(), SpillOff,RegType, AdIMid);
702
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000703 if (MIBef)
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000704 AI.InstrnsBefore.push_back(MIBef);
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000705
706 AI.InstrnsAfter.insert(AI.InstrnsAfter.begin(),
707 AdIMid.begin(), AdIMid.end());
708
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000709 if (MIAft)
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000710 AI.InstrnsAfter.insert(AI.InstrnsAfter.begin(), MIAft);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000711
712 } // if !DEF
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000713
Chris Lattner7e708292002-06-25 16:13:24 +0000714 if (DEBUG_RA) {
715 cerr << "\nFor Inst " << *MInst;
716 cerr << " - SPILLED LR: "; printSet(*LR);
717 cerr << "\n - Added Instructions:";
718 if (MIBef) cerr << *MIBef;
719 for (vector<MachineInstr*>::const_iterator II=AdIMid.begin();
720 II != AdIMid.end(); ++II)
721 cerr << **II;
722 if (MIAft) cerr << *MIAft;
723 }
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000724
Chris Lattner296b7732002-02-05 02:52:05 +0000725 Op.setRegForValue(TmpRegU); // set the opearnd
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000726}
727
728
729
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000730//----------------------------------------------------------------------------
731// We can use the following method to get a temporary register to be used
732// BEFORE any given machine instruction. If there is a register available,
733// this method will simply return that register and set MIBef = MIAft = NULL.
734// Otherwise, it will return a register and MIAft and MIBef will contain
735// two instructions used to free up this returned register.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000736// Returned register number is the UNIFIED register number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000737//----------------------------------------------------------------------------
738
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000739int PhyRegAlloc::getUsableUniRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000740 const int RegType,
741 const MachineInstr *MInst,
Chris Lattner296b7732002-02-05 02:52:05 +0000742 const ValueSet *LVSetBef,
Vikram S. Adve23a4c8f2002-03-18 03:37:19 +0000743 MachineInstr *&MIBef,
744 MachineInstr *&MIAft) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000745
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000746 int RegU = getUnusedUniRegAtMI(RC, MInst, LVSetBef);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000747
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000748
Chris Lattner7e708292002-06-25 16:13:24 +0000749 if (RegU != -1) {
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000750 // we found an unused register, so we can simply use it
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000751 MIBef = MIAft = NULL;
752 }
753 else {
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000754 // we couldn't find an unused register. Generate code to free up a reg by
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000755 // saving it on stack and restoring after the instruction
756
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000757 int TmpOff = mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
Vikram S. Adve12af1642001-11-08 04:48:50 +0000758
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000759 RegU = getUniRegNotUsedByThisInst(RC, MInst);
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000760
761 vector<MachineInstr*> mvec;
762
763 MRI.cpReg2MemMI(RegU, MRI.getFramePointer(), TmpOff, RegType, mvec);
764 assert(mvec.size() == 1 && "Need to return a vector here too");
765 MIBef = * mvec.begin();
766
767 MRI.cpMem2RegMI(MRI.getFramePointer(), TmpOff, RegU, RegType, mvec);
768 assert(mvec.size() == 1 && "Need to return a vector here too");
769 MIAft = * mvec.begin();
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000770 }
771
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000772 return RegU;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000773}
774
775//----------------------------------------------------------------------------
776// This method is called to get a new unused register that can be used to
777// accomodate a spilled value.
778// This method may be called several times for a single machine instruction
779// if it contains many spilled operands. Each time it is called, it finds
780// a register which is not live at that instruction and also which is not
781// used by other spilled operands of the same instruction.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000782// Return register number is relative to the register class. NOT
783// unified number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000784//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000785int PhyRegAlloc::getUnusedUniRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000786 const MachineInstr *MInst,
Chris Lattner296b7732002-02-05 02:52:05 +0000787 const ValueSet *LVSetBef) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000788
789 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
790
Chris Lattner85c54652002-05-23 15:50:03 +0000791 std::vector<bool> &IsColorUsedArr = RC->getIsColorUsedArr();
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000792
Chris Lattner7e708292002-06-25 16:13:24 +0000793 for (unsigned i=0; i < NumAvailRegs; i++) // Reset array
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000794 IsColorUsedArr[i] = false;
795
Chris Lattner296b7732002-02-05 02:52:05 +0000796 ValueSet::const_iterator LIt = LVSetBef->begin();
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000797
798 // for each live var in live variable set after machine inst
Chris Lattner7e708292002-06-25 16:13:24 +0000799 for ( ; LIt != LVSetBef->end(); ++LIt) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000800
801 // get the live range corresponding to live var
802 LiveRange *const LRofLV = LRI.getLiveRangeForValue(*LIt );
803
804 // LR can be null if it is a const since a const
805 // doesn't have a dominating def - see Assumptions above
Chris Lattner7e708292002-06-25 16:13:24 +0000806 if (LRofLV && LRofLV->getRegClass() == RC && LRofLV->hasColor() )
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000807 IsColorUsedArr[ LRofLV->getColor() ] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000808 }
809
810 // It is possible that one operand of this MInst was already spilled
811 // and it received some register temporarily. If that's the case,
812 // it is recorded in machine operand. We must skip such registers.
813
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000814 setRelRegsUsedByThisInst(RC, MInst);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000815
Chris Lattner7e708292002-06-25 16:13:24 +0000816 for (unsigned c=0; c < NumAvailRegs; c++) // find first unused color
Chris Lattner85c54652002-05-23 15:50:03 +0000817 if (!IsColorUsedArr[c])
818 return MRI.getUnifiedRegNum(RC->getID(), c);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000819
Chris Lattner85c54652002-05-23 15:50:03 +0000820 return -1;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000821}
822
823
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000824//----------------------------------------------------------------------------
825// Get any other register in a register class, other than what is used
826// by operands of a machine instruction. Returns the unified reg number.
827//----------------------------------------------------------------------------
828int PhyRegAlloc::getUniRegNotUsedByThisInst(RegClass *RC,
Chris Lattner85c54652002-05-23 15:50:03 +0000829 const MachineInstr *MInst) {
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000830
Chris Lattner85c54652002-05-23 15:50:03 +0000831 vector<bool> &IsColorUsedArr = RC->getIsColorUsedArr();
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000832 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
833
834
Chris Lattner7e708292002-06-25 16:13:24 +0000835 for (unsigned i=0; i < NumAvailRegs ; i++) // Reset array
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000836 IsColorUsedArr[i] = false;
837
838 setRelRegsUsedByThisInst(RC, MInst);
839
Chris Lattner7e708292002-06-25 16:13:24 +0000840 for (unsigned c=0; c < RC->getNumOfAvailRegs(); c++)// find first unused color
Chris Lattner85c54652002-05-23 15:50:03 +0000841 if (!IsColorUsedArr[c])
842 return MRI.getUnifiedRegNum(RC->getID(), c);
843
844 assert(0 && "FATAL: No free register could be found in reg class!!");
Chris Lattner697954c2002-01-20 22:54:45 +0000845 return 0;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000846}
847
848
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000849//----------------------------------------------------------------------------
850// This method modifies the IsColorUsedArr of the register class passed to it.
851// It sets the bits corresponding to the registers used by this machine
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000852// instructions. Both explicit and implicit operands are set.
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000853//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000854void PhyRegAlloc::setRelRegsUsedByThisInst(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000855 const MachineInstr *MInst ) {
856
Chris Lattner85c54652002-05-23 15:50:03 +0000857 vector<bool> &IsColorUsedArr = RC->getIsColorUsedArr();
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000858
Chris Lattner7e708292002-06-25 16:13:24 +0000859 for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000860
861 const MachineOperand& Op = MInst->getOperand(OpNum);
862
Chris Lattner7e708292002-06-25 16:13:24 +0000863 if (Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000864 Op.getOperandType() == MachineOperand::MO_CCRegister ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000865
866 const Value *const Val = Op.getVRegValue();
867
Chris Lattner7e708292002-06-25 16:13:24 +0000868 if (Val )
869 if (MRI.getRegClassIDOfValue(Val) == RC->getID() ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000870 int Reg;
Chris Lattner7e708292002-06-25 16:13:24 +0000871 if ((Reg=Op.getAllocatedRegNum()) != -1) {
Chris Lattner85c54652002-05-23 15:50:03 +0000872 IsColorUsedArr[Reg] = true;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000873 }
874 else {
875 // it is possilbe that this operand still is not marked with
876 // a register but it has a LR and that received a color
877
878 LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
Chris Lattner7e708292002-06-25 16:13:24 +0000879 if (LROfVal)
880 if (LROfVal->hasColor() )
Chris Lattner85c54652002-05-23 15:50:03 +0000881 IsColorUsedArr[LROfVal->getColor()] = true;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000882 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000883
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000884 } // if reg classes are the same
885 }
886 else if (Op.getOperandType() == MachineOperand::MO_MachineRegister) {
Chris Lattner85c54652002-05-23 15:50:03 +0000887 assert((unsigned)Op.getMachineRegNum() < IsColorUsedArr.size());
888 IsColorUsedArr[Op.getMachineRegNum()] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000889 }
890 }
891
892 // If there are implicit references, mark them as well
893
Chris Lattner7e708292002-06-25 16:13:24 +0000894 for (unsigned z=0; z < MInst->getNumImplicitRefs(); z++) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000895
896 LiveRange *const LRofImpRef =
897 LRI.getLiveRangeForValue( MInst->getImplicitRef(z) );
Chris Lattner697954c2002-01-20 22:54:45 +0000898
Chris Lattner7e708292002-06-25 16:13:24 +0000899 if (LRofImpRef && LRofImpRef->hasColor())
Chris Lattner697954c2002-01-20 22:54:45 +0000900 IsColorUsedArr[LRofImpRef->getColor()] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000901 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000902}
903
904
905
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000906
907
908
909
910
911//----------------------------------------------------------------------------
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000912// If there are delay slots for an instruction, the instructions
913// added after it must really go after the delayed instruction(s).
914// So, we move the InstrAfter of that instruction to the
915// corresponding delayed instruction using the following method.
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000916
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000917//----------------------------------------------------------------------------
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000918void PhyRegAlloc::move2DelayedInstr(const MachineInstr *OrigMI,
919 const MachineInstr *DelayedMI) {
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000920
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000921 // "added after" instructions of the original instr
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000922 std::vector<MachineInstr *> &OrigAft = AddedInstrMap[OrigMI].InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000923
924 // "added instructions" of the delayed instr
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000925 AddedInstrns &DelayAdI = AddedInstrMap[DelayedMI];
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000926
927 // "added after" instructions of the delayed instr
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000928 std::vector<MachineInstr *> &DelayedAft = DelayAdI.InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000929
930 // go thru all the "added after instructions" of the original instruction
931 // and append them to the "addded after instructions" of the delayed
932 // instructions
Chris Lattner697954c2002-01-20 22:54:45 +0000933 DelayedAft.insert(DelayedAft.end(), OrigAft.begin(), OrigAft.end());
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000934
935 // empty the "added after instructions" of the original instruction
936 OrigAft.clear();
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000937}
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000938
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000939//----------------------------------------------------------------------------
940// This method prints the code with registers after register allocation is
941// complete.
942//----------------------------------------------------------------------------
943void PhyRegAlloc::printMachineCode()
944{
945
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000946 cerr << "\n;************** Function " << Meth->getName()
Chris Lattner697954c2002-01-20 22:54:45 +0000947 << " *****************\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000948
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000949 for (Function::const_iterator BBI = Meth->begin(), BBE = Meth->end();
950 BBI != BBE; ++BBI) {
Chris Lattner7e708292002-06-25 16:13:24 +0000951 cerr << "\n"; printLabel(BBI); cerr << ": ";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000952
953 // get the iterator for machine instructions
Chris Lattner7e708292002-06-25 16:13:24 +0000954 MachineCodeForBasicBlock& MIVec = BBI->getMachineInstrVec();
Vikram S. Adve48762092002-04-25 04:34:15 +0000955 MachineCodeForBasicBlock::iterator MII = MIVec.begin();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000956
957 // iterate over all the machine instructions in BB
Chris Lattner7e708292002-06-25 16:13:24 +0000958 for ( ; MII != MIVec.end(); ++MII) {
Vikram S. Adve48762092002-04-25 04:34:15 +0000959 MachineInstr *const MInst = *MII;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000960
Chris Lattner697954c2002-01-20 22:54:45 +0000961 cerr << "\n\t";
962 cerr << TargetInstrDescriptors[MInst->getOpCode()].opCodeString;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000963
Chris Lattner7e708292002-06-25 16:13:24 +0000964 for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000965 MachineOperand& Op = MInst->getOperand(OpNum);
966
Chris Lattner7e708292002-06-25 16:13:24 +0000967 if (Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasanka97b8b442001-10-18 22:36:26 +0000968 Op.getOperandType() == MachineOperand::MO_CCRegister /*||
969 Op.getOperandType() == MachineOperand::MO_PCRelativeDisp*/ ) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000970
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000971 const Value *const Val = Op.getVRegValue () ;
Ruchira Sasankae727f852001-09-18 22:43:57 +0000972 // ****this code is temporary till NULL Values are fixed
Chris Lattner7e708292002-06-25 16:13:24 +0000973 if (! Val ) {
Chris Lattner697954c2002-01-20 22:54:45 +0000974 cerr << "\t<*NULL*>";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000975 continue;
976 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000977
978 // if a label or a constant
Chris Lattner7e708292002-06-25 16:13:24 +0000979 if (isa<BasicBlock>(Val)) {
Chris Lattner697954c2002-01-20 22:54:45 +0000980 cerr << "\t"; printLabel( Op.getVRegValue () );
981 } else {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000982 // else it must be a register value
983 const int RegNum = Op.getAllocatedRegNum();
984
Chris Lattner697954c2002-01-20 22:54:45 +0000985 cerr << "\t" << "%" << MRI.getUnifiedRegName( RegNum );
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000986 if (Val->hasName() )
Chris Lattner697954c2002-01-20 22:54:45 +0000987 cerr << "(" << Val->getName() << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000988 else
Chris Lattner697954c2002-01-20 22:54:45 +0000989 cerr << "(" << Val << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000990
Chris Lattner7e708292002-06-25 16:13:24 +0000991 if (Op.opIsDef() )
Chris Lattner697954c2002-01-20 22:54:45 +0000992 cerr << "*";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000993
994 const LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
Chris Lattner7e708292002-06-25 16:13:24 +0000995 if (LROfVal )
996 if (LROfVal->hasSpillOffset() )
Chris Lattner697954c2002-01-20 22:54:45 +0000997 cerr << "$";
Ruchira Sasankae727f852001-09-18 22:43:57 +0000998 }
999
1000 }
Chris Lattner7e708292002-06-25 16:13:24 +00001001 else if (Op.getOperandType() == MachineOperand::MO_MachineRegister) {
Chris Lattner697954c2002-01-20 22:54:45 +00001002 cerr << "\t" << "%" << MRI.getUnifiedRegName(Op.getMachineRegNum());
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001003 }
1004
1005 else
Chris Lattner697954c2002-01-20 22:54:45 +00001006 cerr << "\t" << Op; // use dump field
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001007 }
1008
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001009
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001010
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001011 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
Chris Lattner7e708292002-06-25 16:13:24 +00001012 if (NumOfImpRefs > 0) {
Chris Lattner697954c2002-01-20 22:54:45 +00001013 cerr << "\tImplicit:";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001014
Chris Lattner7e708292002-06-25 16:13:24 +00001015 for (unsigned z=0; z < NumOfImpRefs; z++)
Chris Lattner0665a5f2002-02-05 01:43:49 +00001016 cerr << RAV(MInst->getImplicitRef(z)) << "\t";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001017 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001018
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001019 } // for all machine instructions
1020
Chris Lattner697954c2002-01-20 22:54:45 +00001021 cerr << "\n";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001022
1023 } // for all BBs
1024
Chris Lattner697954c2002-01-20 22:54:45 +00001025 cerr << "\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001026}
1027
Ruchira Sasankae727f852001-09-18 22:43:57 +00001028
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001029#if 0
1030
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001031//----------------------------------------------------------------------------
1032//
1033//----------------------------------------------------------------------------
1034
1035void PhyRegAlloc::colorCallRetArgs()
1036{
1037
1038 CallRetInstrListType &CallRetInstList = LRI.getCallRetInstrList();
1039 CallRetInstrListType::const_iterator It = CallRetInstList.begin();
1040
Chris Lattner7e708292002-06-25 16:13:24 +00001041 for ( ; It != CallRetInstList.end(); ++It ) {
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001042
Ruchira Sasankaa90e7702001-10-15 16:26:38 +00001043 const MachineInstr *const CRMI = *It;
1044 unsigned OpCode = CRMI->getOpCode();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001045
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001046 // get the added instructions for this Call/Ret instruciton
Chris Lattner0b0ffa02002-04-09 05:13:04 +00001047 AddedInstrns &AI = AddedInstrMap[CRMI];
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001048
Chris Lattner0b0ffa02002-04-09 05:13:04 +00001049 // Tmp stack positions are needed by some calls that have spilled args
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001050 // So reset it before we call each such method
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001051 //mcInfo.popAllTempValues(TM);
1052
Vikram S. Adve12af1642001-11-08 04:48:50 +00001053
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001054 if (TM.getInstrInfo().isCall(OpCode))
Chris Lattner0b0ffa02002-04-09 05:13:04 +00001055 MRI.colorCallArgs(CRMI, LRI, &AI, *this);
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001056 else if (TM.getInstrInfo().isReturn(OpCode))
Chris Lattner0b0ffa02002-04-09 05:13:04 +00001057 MRI.colorRetValue(CRMI, LRI, &AI);
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001058 else
1059 assert(0 && "Non Call/Ret instrn in CallRetInstrList\n");
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001060 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001061}
1062
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001063#endif
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001064
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001065//----------------------------------------------------------------------------
1066
1067//----------------------------------------------------------------------------
1068void PhyRegAlloc::colorIncomingArgs()
1069{
Chris Lattner7e708292002-06-25 16:13:24 +00001070 const BasicBlock &FirstBB = Meth->front();
1071 const MachineInstr *FirstMI = FirstBB.getMachineInstrVec().front();
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001072 assert(FirstMI && "No machine instruction in entry BB");
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001073
Vikram S. Adve48762092002-04-25 04:34:15 +00001074 MRI.colorMethodArgs(Meth, LRI, &AddedInstrAtEntry);
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001075}
1076
Ruchira Sasankae727f852001-09-18 22:43:57 +00001077
1078//----------------------------------------------------------------------------
1079// Used to generate a label for a basic block
1080//----------------------------------------------------------------------------
Chris Lattner697954c2002-01-20 22:54:45 +00001081void PhyRegAlloc::printLabel(const Value *const Val) {
1082 if (Val->hasName())
1083 cerr << Val->getName();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001084 else
Chris Lattner697954c2002-01-20 22:54:45 +00001085 cerr << "Label" << Val;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001086}
1087
1088
Ruchira Sasankae727f852001-09-18 22:43:57 +00001089//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001090// This method calls setSugColorUsable method of each live range. This
1091// will determine whether the suggested color of LR is really usable.
1092// A suggested color is not usable when the suggested color is volatile
1093// AND when there are call interferences
1094//----------------------------------------------------------------------------
1095
1096void PhyRegAlloc::markUnusableSugColors()
1097{
Chris Lattner7e708292002-06-25 16:13:24 +00001098 if (DEBUG_RA ) cerr << "\nmarking unusable suggested colors ...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001099
1100 // hash map iterator
1101 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
1102 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
1103
Chris Lattner7e708292002-06-25 16:13:24 +00001104 for (; HMI != HMIEnd ; ++HMI ) {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001105 if (HMI->first) {
1106 LiveRange *L = HMI->second; // get the LiveRange
1107 if (L) {
Chris Lattner7e708292002-06-25 16:13:24 +00001108 if (L->hasSuggestedColor()) {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001109 int RCID = L->getRegClass()->getID();
Chris Lattner7e708292002-06-25 16:13:24 +00001110 if (MRI.isRegVolatile( RCID, L->getSuggestedColor()) &&
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001111 L->isCallInterference() )
1112 L->setSuggestedColorUsable( false );
1113 else
1114 L->setSuggestedColorUsable( true );
1115 }
1116 } // if L->hasSuggestedColor()
1117 }
1118 } // for all LR's in hash map
1119}
1120
1121
1122
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001123//----------------------------------------------------------------------------
1124// The following method will set the stack offsets of the live ranges that
1125// are decided to be spillled. This must be called just after coloring the
1126// LRs using the graph coloring algo. For each live range that is spilled,
1127// this method allocate a new spill position on the stack.
1128//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001129
Chris Lattner37730942002-02-05 03:52:29 +00001130void PhyRegAlloc::allocateStackSpace4SpilledLRs() {
1131 if (DEBUG_RA) cerr << "\nsetting LR stack offsets ...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001132
Chris Lattner37730942002-02-05 03:52:29 +00001133 LiveRangeMapType::const_iterator HMI = LRI.getLiveRangeMap()->begin();
1134 LiveRangeMapType::const_iterator HMIEnd = LRI.getLiveRangeMap()->end();
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001135
Chris Lattner7e708292002-06-25 16:13:24 +00001136 for ( ; HMI != HMIEnd ; ++HMI) {
Chris Lattner37730942002-02-05 03:52:29 +00001137 if (HMI->first && HMI->second) {
1138 LiveRange *L = HMI->second; // get the LiveRange
1139 if (!L->hasColor()) // NOTE: ** allocating the size of long Type **
1140 L->setSpillOffFromFP(mcInfo.allocateSpilledValue(TM, Type::LongTy));
1141 }
1142 } // for all LR's in hash map
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001143}
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001144
1145
1146
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001147//----------------------------------------------------------------------------
Ruchira Sasankae727f852001-09-18 22:43:57 +00001148// The entry pont to Register Allocation
1149//----------------------------------------------------------------------------
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001150
1151void PhyRegAlloc::allocateRegisters()
1152{
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001153
1154 // make sure that we put all register classes into the RegClassList
1155 // before we call constructLiveRanges (now done in the constructor of
1156 // PhyRegAlloc class).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001157 //
1158 LRI.constructLiveRanges(); // create LR info
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001159
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001160 if (DEBUG_RA)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001161 LRI.printLiveRanges();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001162
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001163 createIGNodeListsAndIGs(); // create IGNode list and IGs
1164
1165 buildInterferenceGraphs(); // build IGs in all reg classes
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001166
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001167
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001168 if (DEBUG_RA) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001169 // print all LRs in all reg classes
Chris Lattner7e708292002-06-25 16:13:24 +00001170 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
1171 RegClassList[rc]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001172
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001173 // print IGs in all register classes
Chris Lattner7e708292002-06-25 16:13:24 +00001174 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
1175 RegClassList[rc]->printIG();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001176 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001177
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001178
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001179 LRI.coalesceLRs(); // coalesce all live ranges
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001180
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +00001181
Chris Lattner7e708292002-06-25 16:13:24 +00001182 if (DEBUG_RA) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001183 // print all LRs in all reg classes
Chris Lattner7e708292002-06-25 16:13:24 +00001184 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001185 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001186
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001187 // print IGs in all register classes
Chris Lattner7e708292002-06-25 16:13:24 +00001188 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001189 RegClassList[ rc ]->printIG();
1190 }
1191
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001192
1193 // mark un-usable suggested color before graph coloring algorithm.
1194 // When this is done, the graph coloring algo will not reserve
1195 // suggested color unnecessarily - they can be used by another LR
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001196 //
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001197 markUnusableSugColors();
1198
1199 // color all register classes using the graph coloring algo
Chris Lattner7e708292002-06-25 16:13:24 +00001200 for (unsigned rc=0; rc < NumOfRegClasses ; rc++)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001201 RegClassList[ rc ]->colorAllRegs();
1202
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001203 // Atter grpah coloring, if some LRs did not receive a color (i.e, spilled)
1204 // a poistion for such spilled LRs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001205 //
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001206 allocateStackSpace4SpilledLRs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001207
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001208 mcInfo.popAllTempValues(TM); // TODO **Check
1209
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001210 // color incoming args - if the correct color was not received
1211 // insert code to copy to the correct register
1212 //
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001213 colorIncomingArgs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001214
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001215 // Now update the machine code with register names and add any
1216 // additional code inserted by the register allocator to the instruction
1217 // stream
1218 //
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001219 updateMachineCode();
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001220
Chris Lattner045e7c82001-09-19 16:26:23 +00001221 if (DEBUG_RA) {
Vikram S. Adve12af1642001-11-08 04:48:50 +00001222 MachineCodeForMethod::get(Meth).dump();
Chris Lattner045e7c82001-09-19 16:26:23 +00001223 printMachineCode(); // only for DEBUGGING
1224 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001225}
1226
Ruchira Sasankae727f852001-09-18 22:43:57 +00001227
1228