blob: ab9b1a7b1765ea5728da80dc0124c9db6c6727e4 [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 Lattner697954c2002-01-20 22:54:45 +000027#include <iostream>
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000028#include <math.h>
Chris Lattner697954c2002-01-20 22:54:45 +000029using std::cerr;
Vikram S. Adve12af1642001-11-08 04:48:50 +000030
31
32// ***TODO: There are several places we add instructions. Validate the order
33// of adding these instructions.
Ruchira Sasanka174bded2001-10-28 18:12:02 +000034
Chris Lattnerad86b742002-05-20 21:39:10 +000035cl::Enum<RegAllocDebugLevel_t> DEBUG_RA("dregalloc", cl::Hidden,
Chris Lattner045e7c82001-09-19 16:26:23 +000036 "enable register allocation debugging information",
37 clEnumValN(RA_DEBUG_None , "n", "disable debug output"),
38 clEnumValN(RA_DEBUG_Normal , "y", "enable debug output"),
39 clEnumValN(RA_DEBUG_Verbose, "v", "enable extra debug output"), 0);
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000040
41
Chris Lattner2f9b28e2002-02-04 15:54:09 +000042//----------------------------------------------------------------------------
43// RegisterAllocation pass front end...
44//----------------------------------------------------------------------------
45namespace {
Chris Lattnerf57b8452002-04-27 06:56:12 +000046 class RegisterAllocator : public FunctionPass {
Chris Lattner2f9b28e2002-02-04 15:54:09 +000047 TargetMachine &Target;
48 public:
49 inline RegisterAllocator(TargetMachine &T) : Target(T) {}
Chris Lattner96c466b2002-04-29 14:57:45 +000050
51 const char *getPassName() const { return "Register Allocation"; }
Chris Lattner6dd98a62002-02-04 00:33:08 +000052
Chris Lattnerf57b8452002-04-27 06:56:12 +000053 bool runOnFunction(Function *F) {
Chris Lattner2f9b28e2002-02-04 15:54:09 +000054 if (DEBUG_RA)
Chris Lattnerf57b8452002-04-27 06:56:12 +000055 cerr << "\n******************** Function "<< F->getName()
Chris Lattner2f9b28e2002-02-04 15:54:09 +000056 << " ********************\n";
57
Chris Lattner483e14e2002-04-27 07:27:19 +000058 PhyRegAlloc PRA(F, Target, &getAnalysis<FunctionLiveVarInfo>(),
Chris Lattner1b7f7dc2002-04-28 16:21:30 +000059 &getAnalysis<LoopInfo>());
Chris Lattner2f9b28e2002-02-04 15:54:09 +000060 PRA.allocateRegisters();
61
62 if (DEBUG_RA) cerr << "\nRegister allocation complete!\n";
63 return false;
64 }
Chris Lattner4911c352002-02-04 17:39:42 +000065
Chris Lattnerf57b8452002-04-27 06:56:12 +000066 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner1b7f7dc2002-04-28 16:21:30 +000067 AU.addRequired(LoopInfo::ID);
Chris Lattner483e14e2002-04-27 07:27:19 +000068 AU.addRequired(FunctionLiveVarInfo::ID);
Chris Lattner4911c352002-02-04 17:39:42 +000069 }
Chris Lattner2f9b28e2002-02-04 15:54:09 +000070 };
Chris Lattner6dd98a62002-02-04 00:33:08 +000071}
72
Chris Lattnerf57b8452002-04-27 06:56:12 +000073Pass *getRegisterAllocator(TargetMachine &T) {
Chris Lattner2f9b28e2002-02-04 15:54:09 +000074 return new RegisterAllocator(T);
75}
Chris Lattner6dd98a62002-02-04 00:33:08 +000076
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000077//----------------------------------------------------------------------------
78// Constructor: Init local composite objects and create register classes.
79//----------------------------------------------------------------------------
Chris Lattner1b7f7dc2002-04-28 16:21:30 +000080PhyRegAlloc::PhyRegAlloc(Function *F, const TargetMachine& tm,
81 FunctionLiveVarInfo *Lvi, LoopInfo *LDC)
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000082 : TM(tm), Meth(F),
83 mcInfo(MachineCodeForMethod::get(F)),
84 LVI(Lvi), LRI(F, tm, RegClassList),
85 MRI(tm.getRegInfo()),
Ruchira Sasanka8e604792001-09-14 21:18:34 +000086 NumOfRegClasses(MRI.getNumOfRegClasses()),
Chris Lattner4911c352002-02-04 17:39:42 +000087 LoopDepthCalc(LDC) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +000088
89 // create each RegisterClass and put in RegClassList
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000090 //
Chris Lattner697954c2002-01-20 22:54:45 +000091 for(unsigned int rc=0; rc < NumOfRegClasses; rc++)
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000092 RegClassList.push_back(new RegClass(F, MRI.getMachineRegClass(rc),
93 &ResColList));
Ruchira Sasanka8e604792001-09-14 21:18:34 +000094}
95
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000096
97//----------------------------------------------------------------------------
98// Destructor: Deletes register classes
99//----------------------------------------------------------------------------
100PhyRegAlloc::~PhyRegAlloc() {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000101 for( unsigned int rc=0; rc < NumOfRegClasses; rc++)
102 delete RegClassList[rc];
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000103
104 AddedInstrMap.clear();
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000105}
106
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000107//----------------------------------------------------------------------------
108// This method initally creates interference graphs (one in each reg class)
109// and IGNodeList (one in each IG). The actual nodes will be pushed later.
110//----------------------------------------------------------------------------
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000111void PhyRegAlloc::createIGNodeListsAndIGs() {
112 if (DEBUG_RA) cerr << "Creating LR lists ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000113
114 // hash map iterator
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000115 LiveRangeMapType::const_iterator HMI = LRI.getLiveRangeMap()->begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000116
117 // hash map end
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000118 LiveRangeMapType::const_iterator HMIEnd = LRI.getLiveRangeMap()->end();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000119
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000120 for (; HMI != HMIEnd ; ++HMI ) {
121 if (HMI->first) {
122 LiveRange *L = HMI->second; // get the LiveRange
123 if (!L) {
124 if( DEBUG_RA) {
Chris Lattner0665a5f2002-02-05 01:43:49 +0000125 cerr << "\n*?!?Warning: Null liver range found for: "
126 << RAV(HMI->first) << "\n";
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000127 }
128 continue;
129 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000130 // if the Value * is not null, and LR
131 // is not yet written to the IGNodeList
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000132 if( !(L->getUserIGNode()) ) {
133 RegClass *const RC = // RegClass of first value in the LR
134 RegClassList[ L->getRegClass()->getID() ];
135
136 RC->addLRToIG(L); // add this LR to an IG
137 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000138 }
139 }
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000140
141 // init RegClassList
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000142 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000143 RegClassList[rc]->createInterferenceGraph();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000144
145 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000146 cerr << "LRLists Created!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000147}
148
149
150
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000151
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000152//----------------------------------------------------------------------------
153// This method will add all interferences at for a given instruction.
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000154// Interence occurs only if the LR of Def (Inst or Arg) is of the same reg
155// class as that of live var. The live var passed to this function is the
156// LVset AFTER the instruction
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000157//----------------------------------------------------------------------------
Chris Lattner296b7732002-02-05 02:52:05 +0000158void PhyRegAlloc::addInterference(const Value *Def,
159 const ValueSet *LVSet,
160 bool isCallInst) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000161
Chris Lattner296b7732002-02-05 02:52:05 +0000162 ValueSet::const_iterator LIt = LVSet->begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000163
164 // get the live range of instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000165 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000166 const LiveRange *const LROfDef = LRI.getLiveRangeForValue( Def );
167
168 IGNode *const IGNodeOfDef = LROfDef->getUserIGNode();
169 assert( IGNodeOfDef );
170
171 RegClass *const RCOfDef = LROfDef->getRegClass();
172
173 // for each live var in live variable set
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000174 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000175 for( ; LIt != LVSet->end(); ++LIt) {
176
Chris Lattner0665a5f2002-02-05 01:43:49 +0000177 if (DEBUG_RA > 1)
178 cerr << "< Def=" << RAV(Def) << ", Lvar=" << RAV(*LIt) << "> ";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000179
180 // get the live range corresponding to live var
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000181 //
Chris Lattner0665a5f2002-02-05 01:43:49 +0000182 LiveRange *LROfVar = LRI.getLiveRangeForValue(*LIt);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000183
184 // LROfVar can be null if it is a const since a const
185 // doesn't have a dominating def - see Assumptions above
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000186 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000187 if (LROfVar) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000188 if(LROfDef == LROfVar) // do not set interf for same LR
189 continue;
190
191 // if 2 reg classes are the same set interference
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000192 //
Chris Lattner0665a5f2002-02-05 01:43:49 +0000193 if (RCOfDef == LROfVar->getRegClass()) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000194 RCOfDef->setInterference( LROfDef, LROfVar);
Chris Lattner0665a5f2002-02-05 01:43:49 +0000195 } else if (DEBUG_RA > 1) {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000196 // we will not have LRs for values not explicitly allocated in the
197 // instruction stream (e.g., constants)
Chris Lattner0665a5f2002-02-05 01:43:49 +0000198 cerr << " warning: no live range for " << RAV(*LIt) << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000199 }
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000200 }
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000201 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000202}
203
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000204
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000205
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000206//----------------------------------------------------------------------------
207// For a call instruction, this method sets the CallInterference flag in
208// the LR of each variable live int the Live Variable Set live after the
209// call instruction (except the return value of the call instruction - since
210// the return value does not interfere with that call itself).
211//----------------------------------------------------------------------------
212
213void PhyRegAlloc::setCallInterferences(const MachineInstr *MInst,
Chris Lattner296b7732002-02-05 02:52:05 +0000214 const ValueSet *LVSetAft) {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000215
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000216 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000217 cerr << "\n For call inst: " << *MInst;
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000218
Chris Lattner296b7732002-02-05 02:52:05 +0000219 ValueSet::const_iterator LIt = LVSetAft->begin();
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000220
221 // for each live var in live variable set after machine inst
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000222 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000223 for( ; LIt != LVSetAft->end(); ++LIt) {
224
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000225 // get the live range corresponding to live var
226 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000227 LiveRange *const LR = LRI.getLiveRangeForValue(*LIt );
228
229 if( LR && DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000230 cerr << "\n\tLR Aft Call: ";
Chris Lattner296b7732002-02-05 02:52:05 +0000231 printSet(*LR);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000232 }
233
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000234 // LR can be null if it is a const since a const
235 // doesn't have a dominating def - see Assumptions above
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000236 //
Vikram S. Adve1a53f032002-03-31 18:54:37 +0000237 if( LR ) {
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000238 LR->setCallInterference();
239 if( DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000240 cerr << "\n ++Added call interf for LR: " ;
Chris Lattner296b7732002-02-05 02:52:05 +0000241 printSet(*LR);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000242 }
243 }
244
245 }
246
Vikram S. Adve1a53f032002-03-31 18:54:37 +0000247 // Now find the LR of the return value of the call
248 // We do this because, we look at the LV set *after* the instruction
249 // to determine, which LRs must be saved across calls. The return value
250 // of the call is live in this set - but it does not interfere with call
251 // (i.e., we can allocate a volatile register to the return value)
252 //
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000253 CallArgsDescriptor* argDesc = CallArgsDescriptor::get(MInst);
254
255 if (const Value *RetVal = argDesc->getReturnValue()) {
Vikram S. Adve1a53f032002-03-31 18:54:37 +0000256 LiveRange *RetValLR = LRI.getLiveRangeForValue( RetVal );
257 assert( RetValLR && "No LR for RetValue of call");
258 RetValLR->clearCallInterference();
259 }
260
261 // If the CALL is an indirect call, find the LR of the function pointer.
262 // That has a call interference because it conflicts with outgoing args.
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000263 if( const Value *AddrVal = argDesc->getIndirectFuncPtr()) {
Vikram S. Adve1a53f032002-03-31 18:54:37 +0000264 LiveRange *AddrValLR = LRI.getLiveRangeForValue( AddrVal );
265 assert( AddrValLR && "No LR for indirect addr val of call");
266 AddrValLR->setCallInterference();
267 }
268
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000269}
270
271
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000272
273
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000274//----------------------------------------------------------------------------
275// This method will walk thru code and create interferences in the IG of
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000276// each RegClass. Also, this method calculates the spill cost of each
277// Live Range (it is done in this method to save another pass over the code).
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000278//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000279void PhyRegAlloc::buildInterferenceGraphs()
280{
281
Chris Lattner697954c2002-01-20 22:54:45 +0000282 if(DEBUG_RA) cerr << "Creating interference graphs ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000283
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000284 unsigned BBLoopDepthCost;
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000285 for (Function::const_iterator BBI = Meth->begin(), BBE = Meth->end();
286 BBI != BBE; ++BBI) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000287
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000288 // find the 10^(loop_depth) of this BB
289 //
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000290 BBLoopDepthCost = (unsigned) pow(10.0, LoopDepthCalc->getLoopDepth(*BBI));
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000291
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000292 // get the iterator for machine instructions
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000293 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000294 const MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
Vikram S. Adve48762092002-04-25 04:34:15 +0000295 MachineCodeForBasicBlock::const_iterator MII = MIVec.begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000296
297 // iterate over all the machine instructions in BB
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000298 //
Vikram S. Adve48762092002-04-25 04:34:15 +0000299 for( ; MII != MIVec.end(); ++MII) {
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000300
Vikram S. Adve48762092002-04-25 04:34:15 +0000301 const MachineInstr *MInst = *MII;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000302
303 // get the LV set after the instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000304 //
Chris Lattner748697d2002-02-05 04:20:12 +0000305 const ValueSet &LVSetAI = LVI->getLiveVarSetAfterMInst(MInst, *BBI);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000306
307 const bool isCallInst = TM.getInstrInfo().isCall(MInst->getOpCode());
308
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000309 if( isCallInst ) {
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000310 // set the isCallInterference flag of each live range wich extends
311 // accross this call instruction. This information is used by graph
312 // coloring algo to avoid allocating volatile colors to live ranges
313 // that span across calls (since they have to be saved/restored)
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000314 //
Chris Lattner748697d2002-02-05 04:20:12 +0000315 setCallInterferences(MInst, &LVSetAI);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000316 }
317
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000318
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000319 // iterate over all MI operands to find defs
320 //
Chris Lattner2f898d22002-02-05 06:02:59 +0000321 for (MachineInstr::const_val_op_iterator OpI = MInst->begin(),
322 OpE = MInst->end(); OpI != OpE; ++OpI) {
323 if (OpI.isDef()) // create a new LR iff this operand is a def
Chris Lattner748697d2002-02-05 04:20:12 +0000324 addInterference(*OpI, &LVSetAI, isCallInst);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000325
326 // Calculate the spill cost of each live range
327 //
Chris Lattner2f898d22002-02-05 06:02:59 +0000328 LiveRange *LR = LRI.getLiveRangeForValue(*OpI);
329 if (LR) LR->addSpillCost(BBLoopDepthCost);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000330 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000331
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000332
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000333 // if there are multiple defs in this instruction e.g. in SETX
334 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000335 if (TM.getInstrInfo().isPseudoInstr(MInst->getOpCode()))
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000336 addInterf4PseudoInstr(MInst);
337
338
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000339 // Also add interference for any implicit definitions in a machine
340 // instr (currently, only calls have this).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000341 //
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000342 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
343 if( NumOfImpRefs > 0 ) {
344 for(unsigned z=0; z < NumOfImpRefs; z++)
345 if( MInst->implicitRefIsDefined(z) )
Chris Lattner748697d2002-02-05 04:20:12 +0000346 addInterference( MInst->getImplicitRef(z), &LVSetAI, isCallInst );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000347 }
348
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +0000349
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000350 } // for all machine instructions in BB
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000351 } // for all BBs in function
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000352
353
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000354 // add interferences for function arguments. Since there are no explict
355 // defs in the function for args, we have to add them manually
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000356 //
357 addInterferencesForArgs();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000358
359 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000360 cerr << "Interference graphs calculted!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000361
362}
363
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000364
365
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000366//--------------------------------------------------------------------------
367// Pseudo instructions will be exapnded to multiple instructions by the
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000368// assembler. Consequently, all the opernds must get distinct registers.
369// Therefore, we mark all operands of a pseudo instruction as they interfere
370// with one another.
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000371//--------------------------------------------------------------------------
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000372void PhyRegAlloc::addInterf4PseudoInstr(const MachineInstr *MInst) {
373
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000374 bool setInterf = false;
375
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000376 // iterate over MI operands to find defs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000377 //
Chris Lattner2f898d22002-02-05 06:02:59 +0000378 for (MachineInstr::const_val_op_iterator It1 = MInst->begin(),
379 ItE = MInst->end(); It1 != ItE; ++It1) {
380 const LiveRange *LROfOp1 = LRI.getLiveRangeForValue(*It1);
381 assert((LROfOp1 || !It1.isDef()) && "No LR for Def in PSEUDO insruction");
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000382
Chris Lattner2f898d22002-02-05 06:02:59 +0000383 MachineInstr::const_val_op_iterator It2 = It1;
384 for(++It2; It2 != ItE; ++It2) {
385 const LiveRange *LROfOp2 = LRI.getLiveRangeForValue(*It2);
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000386
Chris Lattner2f898d22002-02-05 06:02:59 +0000387 if (LROfOp2) {
388 RegClass *RCOfOp1 = LROfOp1->getRegClass();
389 RegClass *RCOfOp2 = LROfOp2->getRegClass();
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000390
391 if( RCOfOp1 == RCOfOp2 ){
392 RCOfOp1->setInterference( LROfOp1, LROfOp2 );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000393 setInterf = true;
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000394 }
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000395 } // if Op2 has a LR
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000396 } // for all other defs in machine instr
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000397 } // for all operands in an instruction
398
Chris Lattner2f898d22002-02-05 06:02:59 +0000399 if (!setInterf && MInst->getNumOperands() > 2) {
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000400 cerr << "\nInterf not set for any operand in pseudo instr:\n";
401 cerr << *MInst;
402 assert(0 && "Interf not set for pseudo instr with > 2 operands" );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000403 }
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000404}
405
406
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000407
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000408//----------------------------------------------------------------------------
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000409// This method will add interferences for incoming arguments to a function.
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000410//----------------------------------------------------------------------------
Chris Lattner296b7732002-02-05 02:52:05 +0000411void PhyRegAlloc::addInterferencesForArgs() {
412 // get the InSet of root BB
Chris Lattner748697d2002-02-05 04:20:12 +0000413 const ValueSet &InSet = LVI->getInSetOfBB(Meth->front());
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000414
Chris Lattner296b7732002-02-05 02:52:05 +0000415 // get the argument list
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000416 const Function::ArgumentListType &ArgList = Meth->getArgumentList();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000417
Chris Lattner296b7732002-02-05 02:52:05 +0000418 // get an iterator to arg list
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000419 Function::ArgumentListType::const_iterator ArgIt = ArgList.begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000420
421
422 for( ; ArgIt != ArgList.end() ; ++ArgIt) { // for each argument
Chris Lattner748697d2002-02-05 04:20:12 +0000423 addInterference((Value*)*ArgIt, &InSet, false);// add interferences between
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000424 // args and LVars at start
Chris Lattner0665a5f2002-02-05 01:43:49 +0000425 if( DEBUG_RA > 1)
426 cerr << " - %% adding interference for argument "
427 << RAV((const Value *)*ArgIt) << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000428 }
429}
430
431
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000432//----------------------------------------------------------------------------
433// This method is called after register allocation is complete to set the
434// allocated reisters in the machine code. This code will add register numbers
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000435// to MachineOperands that contain a Value. Also it calls target specific
436// methods to produce caller saving instructions. At the end, it adds all
437// additional instructions produced by the register allocator to the
438// instruction stream.
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000439//----------------------------------------------------------------------------
Vikram S. Adve48762092002-04-25 04:34:15 +0000440
441//-----------------------------
442// Utility functions used below
443//-----------------------------
444inline void
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000445PrependInstructions(vector<MachineInstr *> &IBef,
Vikram S. Adve48762092002-04-25 04:34:15 +0000446 MachineCodeForBasicBlock& MIVec,
447 MachineCodeForBasicBlock::iterator& MII,
448 const std::string& msg)
449{
450 if (!IBef.empty())
451 {
452 MachineInstr* OrigMI = *MII;
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000453 std::vector<MachineInstr *>::iterator AdIt;
Vikram S. Adve48762092002-04-25 04:34:15 +0000454 for (AdIt = IBef.begin(); AdIt != IBef.end() ; ++AdIt)
455 {
456 if (DEBUG_RA) {
457 if (OrigMI) cerr << "For MInst: " << *OrigMI;
458 cerr << msg << " PREPENDed instr: " << **AdIt << "\n";
459 }
460 MII = MIVec.insert(MII, *AdIt);
461 ++MII;
462 }
463 }
464}
465
466inline void
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000467AppendInstructions(std::vector<MachineInstr *> &IAft,
Vikram S. Adve48762092002-04-25 04:34:15 +0000468 MachineCodeForBasicBlock& MIVec,
469 MachineCodeForBasicBlock::iterator& MII,
470 const std::string& msg)
471{
472 if (!IAft.empty())
473 {
474 MachineInstr* OrigMI = *MII;
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000475 std::vector<MachineInstr *>::iterator AdIt;
Vikram S. Adve48762092002-04-25 04:34:15 +0000476 for( AdIt = IAft.begin(); AdIt != IAft.end() ; ++AdIt )
477 {
478 if(DEBUG_RA) {
479 if (OrigMI) cerr << "For MInst: " << *OrigMI;
480 cerr << msg << " APPENDed instr: " << **AdIt << "\n";
481 }
482 ++MII; // insert before the next instruction
483 MII = MIVec.insert(MII, *AdIt);
484 }
485 }
486}
487
488
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000489void PhyRegAlloc::updateMachineCode()
490{
Vikram S. Adve48762092002-04-25 04:34:15 +0000491 const BasicBlock* entryBB = Meth->getEntryNode();
492 if (entryBB) {
493 MachineCodeForBasicBlock& MIVec = entryBB->getMachineInstrVec();
494 MachineCodeForBasicBlock::iterator MII = MIVec.begin();
495
496 // Insert any instructions needed at method entry
497 PrependInstructions(AddedInstrAtEntry.InstrnsBefore, MIVec, MII,
498 "At function entry: \n");
499 assert(AddedInstrAtEntry.InstrnsAfter.empty() &&
500 "InstrsAfter should be unnecessary since we are just inserting at "
501 "the function entry point here.");
502 }
503
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000504 for (Function::const_iterator BBI = Meth->begin(), BBE = Meth->end();
505 BBI != BBE; ++BBI) {
Vikram S. Adve48762092002-04-25 04:34:15 +0000506
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000507 // iterate over all the machine instructions in BB
Vikram S. Adve48762092002-04-25 04:34:15 +0000508 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
509 for(MachineCodeForBasicBlock::iterator MII = MIVec.begin();
510 MII != MIVec.end(); ++MII) {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000511
Vikram S. Adve48762092002-04-25 04:34:15 +0000512 MachineInstr *MInst = *MII;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000513
514 unsigned Opcode = MInst->getOpCode();
515
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000516 // do not process Phis
Vikram S. Adve23a4c8f2002-03-18 03:37:19 +0000517 if (TM.getInstrInfo().isDummyPhiInstr(Opcode))
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000518 continue;
519
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000520 // Now insert speical instructions (if necessary) for call/return
521 // instructions.
522 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000523 if (TM.getInstrInfo().isCall(Opcode) ||
524 TM.getInstrInfo().isReturn(Opcode)) {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000525
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000526 AddedInstrns &AI = AddedInstrMap[MInst];
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000527
528 // Tmp stack poistions are needed by some calls that have spilled args
529 // So reset it before we call each such method
Ruchira Sasanka6a3db8c2002-01-07 21:09:06 +0000530 //
531 mcInfo.popAllTempValues(TM);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000532
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000533 if (TM.getInstrInfo().isCall(Opcode))
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000534 MRI.colorCallArgs(MInst, LRI, &AI, *this, *BBI);
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000535 else if (TM.getInstrInfo().isReturn(Opcode))
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000536 MRI.colorRetValue(MInst, LRI, &AI);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000537 }
538
539
540 /* -- Using above code instead of this
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000541
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000542 // if this machine instr is call, insert caller saving code
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000543
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000544 if( (TM.getInstrInfo()).isCall( MInst->getOpCode()) )
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000545 MRI.insertCallerSavingCode(MInst, *BBI, *this );
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000546
547 */
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000548
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000549
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000550 // reset the stack offset for temporary variables since we may
551 // need that to spill
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000552 // mcInfo.popAllTempValues(TM);
Ruchira Sasankaf90870f2001-11-15 22:02:06 +0000553 // TODO ** : do later
Vikram S. Adve12af1642001-11-08 04:48:50 +0000554
Chris Lattner7a176752001-12-04 00:03:30 +0000555 //for(MachineInstr::val_const_op_iterator OpI(MInst);!OpI.done();++OpI) {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000556
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000557
558 // Now replace set the registers for operands in the machine instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000559 //
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000560 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
561
562 MachineOperand& Op = MInst->getOperand(OpNum);
563
564 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
565 Op.getOperandType() == MachineOperand::MO_CCRegister) {
566
567 const Value *const Val = Op.getVRegValue();
568
569 // delete this condition checking later (must assert if Val is null)
Chris Lattner045e7c82001-09-19 16:26:23 +0000570 if( !Val) {
571 if (DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000572 cerr << "Warning: NULL Value found for operand\n";
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000573 continue;
574 }
575 assert( Val && "Value is NULL");
576
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000577 LiveRange *const LR = LRI.getLiveRangeForValue(Val);
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000578
579 if ( !LR ) {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000580
581 // nothing to worry if it's a const or a label
582
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000583 if (DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000584 cerr << "*NO LR for operand : " << Op ;
585 cerr << " [reg:" << Op.getAllocatedRegNum() << "]";
586 cerr << " in inst:\t" << *MInst << "\n";
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000587 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000588
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000589 // if register is not allocated, mark register as invalid
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000590 if( Op.getAllocatedRegNum() == -1)
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000591 Op.setRegForValue( MRI.getInvalidRegNum());
Ruchira Sasankae727f852001-09-18 22:43:57 +0000592
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000593
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000594 continue;
595 }
596
597 unsigned RCID = (LR->getRegClass())->getID();
598
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000599 if( LR->hasColor() ) {
600 Op.setRegForValue( MRI.getUnifiedRegNum(RCID, LR->getColor()) );
601 }
602 else {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000603
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000604 // LR did NOT receive a color (register). Now, insert spill code
605 // for spilled opeands in this machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000606
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000607 //assert(0 && "LR must be spilled");
608 insertCode4SpilledLR(LR, MInst, *BBI, OpNum );
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000609
610 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000611 }
612
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000613 } // for each operand
614
615
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000616 // Now add instructions that the register allocator inserts before/after
617 // this machine instructions (done only for calls/rets/incoming args)
618 // We do this here, to ensure that spill for an instruction is inserted
619 // closest as possible to an instruction (see above insertCode4Spill...)
620 //
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000621 // If there are instructions to be added, *before* this machine
622 // instruction, add them now.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000623 //
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000624 if(AddedInstrMap.count(MInst)) {
Vikram S. Adve48762092002-04-25 04:34:15 +0000625 PrependInstructions(AddedInstrMap[MInst].InstrnsBefore, MIVec, MII,"");
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000626 }
Vikram S. Adve48762092002-04-25 04:34:15 +0000627
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000628 // If there are instructions to be added *after* this machine
629 // instruction, add them now
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000630 //
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000631 if (!AddedInstrMap[MInst].InstrnsAfter.empty()) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000632
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000633 // if there are delay slots for this instruction, the instructions
634 // added after it must really go after the delayed instruction(s)
635 // So, we move the InstrAfter of the current instruction to the
636 // corresponding delayed instruction
637
638 unsigned delay;
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000639 if ((delay=TM.getInstrInfo().getNumDelaySlots(MInst->getOpCode())) >0){
Vikram S. Adve48762092002-04-25 04:34:15 +0000640 move2DelayedInstr(MInst, *(MII+delay) );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000641
Chris Lattner697954c2002-01-20 22:54:45 +0000642 if(DEBUG_RA) cerr<< "\nMoved an added instr after the delay slot";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000643 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000644
645 else {
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000646 // Here we can add the "instructions after" to the current
647 // instruction since there are no delay slots for this instruction
Vikram S. Adve48762092002-04-25 04:34:15 +0000648 AppendInstructions(AddedInstrMap[MInst].InstrnsAfter, MIVec, MII,"");
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000649 } // if not delay
650
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000651 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000652
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000653 } // for each machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000654 }
655}
656
657
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000658
659//----------------------------------------------------------------------------
660// This method inserts spill code for AN operand whose LR was spilled.
661// This method may be called several times for a single machine instruction
662// if it contains many spilled operands. Each time it is called, it finds
663// a register which is not live at that instruction and also which is not
664// used by other spilled operands of the same instruction. Then it uses
665// this register temporarily to accomodate the spilled value.
666//----------------------------------------------------------------------------
667void PhyRegAlloc::insertCode4SpilledLR(const LiveRange *LR,
668 MachineInstr *MInst,
669 const BasicBlock *BB,
670 const unsigned OpNum) {
671
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000672 assert(! TM.getInstrInfo().isCall(MInst->getOpCode()) &&
673 (! TM.getInstrInfo().isReturn(MInst->getOpCode())) &&
674 "Arg of a call/ret must be handled elsewhere");
675
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000676 MachineOperand& Op = MInst->getOperand(OpNum);
677 bool isDef = MInst->operandIsDefined(OpNum);
678 unsigned RegType = MRI.getRegType( LR );
679 int SpillOff = LR->getSpillOffFromFP();
680 RegClass *RC = LR->getRegClass();
Chris Lattner748697d2002-02-05 04:20:12 +0000681 const ValueSet &LVSetBef = LVI->getLiveVarSetBeforeMInst(MInst, BB);
Vikram S. Adve00521d72001-11-12 23:26:35 +0000682
Chris Lattner697954c2002-01-20 22:54:45 +0000683 mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000684
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000685 MachineInstr *MIBef=NULL, *MIAft=NULL;
686 vector<MachineInstr*> AdIMid;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000687
Chris Lattner748697d2002-02-05 04:20:12 +0000688 int TmpRegU = getUsableUniRegAtMI(RC, RegType, MInst,&LVSetBef, MIBef, MIAft);
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000689
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000690 // get the added instructions for this instruciton
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000691 AddedInstrns &AI = AddedInstrMap[MInst];
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000692
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000693 if (!isDef) {
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000694 // for a USE, we have to load the value of LR from stack to a TmpReg
695 // and use the TmpReg as one operand of instruction
696
697 // actual loading instruction
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000698 MRI.cpMem2RegMI(MRI.getFramePointer(), SpillOff, TmpRegU,RegType, AdIMid);
699 AI.InstrnsBefore.insert(AI.InstrnsBefore.end(),
700 AdIMid.begin(), AdIMid.end());
701
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000702 if(MIBef)
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000703 AI.InstrnsBefore.push_back(MIBef);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000704
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000705 if(MIAft)
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000706 AI.InstrnsAfter.insert(AI.InstrnsAfter.begin(), MIAft);
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000707
Chris Lattner296b7732002-02-05 02:52:05 +0000708 } else { // if this is a Def
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000709 // for a DEF, we have to store the value produced by this instruction
710 // on the stack position allocated for this LR
711
712 // actual storing instruction
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000713 MRI.cpReg2MemMI(TmpRegU, MRI.getFramePointer(), SpillOff,RegType, AdIMid);
714
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000715 if (MIBef)
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000716 AI.InstrnsBefore.push_back(MIBef);
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000717
718 AI.InstrnsAfter.insert(AI.InstrnsAfter.begin(),
719 AdIMid.begin(), AdIMid.end());
720
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000721 if (MIAft)
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000722 AI.InstrnsAfter.insert(AI.InstrnsAfter.begin(), MIAft);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000723
724 } // if !DEF
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000725
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000726 cerr << "\nFor Inst " << *MInst;
Chris Lattner296b7732002-02-05 02:52:05 +0000727 cerr << " - SPILLED LR: "; printSet(*LR);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000728 cerr << "\n - Added Instructions:";
Chris Lattner296b7732002-02-05 02:52:05 +0000729 if (MIBef) cerr << *MIBef;
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000730 for (vector<MachineInstr*>::const_iterator II=AdIMid.begin();
731 II != AdIMid.end(); ++II)
732 cerr << **II;
Chris Lattner296b7732002-02-05 02:52:05 +0000733 if (MIAft) cerr << *MIAft;
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000734
Chris Lattner296b7732002-02-05 02:52:05 +0000735 Op.setRegForValue(TmpRegU); // set the opearnd
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000736}
737
738
739
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000740//----------------------------------------------------------------------------
741// We can use the following method to get a temporary register to be used
742// BEFORE any given machine instruction. If there is a register available,
743// this method will simply return that register and set MIBef = MIAft = NULL.
744// Otherwise, it will return a register and MIAft and MIBef will contain
745// two instructions used to free up this returned register.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000746// Returned register number is the UNIFIED register number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000747//----------------------------------------------------------------------------
748
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000749int PhyRegAlloc::getUsableUniRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000750 const int RegType,
751 const MachineInstr *MInst,
Chris Lattner296b7732002-02-05 02:52:05 +0000752 const ValueSet *LVSetBef,
Vikram S. Adve23a4c8f2002-03-18 03:37:19 +0000753 MachineInstr *&MIBef,
754 MachineInstr *&MIAft) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000755
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000756 int RegU = getUnusedUniRegAtMI(RC, MInst, LVSetBef);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000757
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000758
759 if( RegU != -1) {
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000760 // we found an unused register, so we can simply use it
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000761 MIBef = MIAft = NULL;
762 }
763 else {
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000764 // we couldn't find an unused register. Generate code to free up a reg by
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000765 // saving it on stack and restoring after the instruction
766
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000767 int TmpOff = mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
Vikram S. Adve12af1642001-11-08 04:48:50 +0000768
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000769 RegU = getUniRegNotUsedByThisInst(RC, MInst);
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000770
771 vector<MachineInstr*> mvec;
772
773 MRI.cpReg2MemMI(RegU, MRI.getFramePointer(), TmpOff, RegType, mvec);
774 assert(mvec.size() == 1 && "Need to return a vector here too");
775 MIBef = * mvec.begin();
776
777 MRI.cpMem2RegMI(MRI.getFramePointer(), TmpOff, RegU, RegType, mvec);
778 assert(mvec.size() == 1 && "Need to return a vector here too");
779 MIAft = * mvec.begin();
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000780 }
781
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000782 return RegU;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000783}
784
785//----------------------------------------------------------------------------
786// This method is called to get a new unused register that can be used to
787// accomodate a spilled value.
788// This method may be called several times for a single machine instruction
789// if it contains many spilled operands. Each time it is called, it finds
790// a register which is not live at that instruction and also which is not
791// used by other spilled operands of the same instruction.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000792// Return register number is relative to the register class. NOT
793// unified number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000794//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000795int PhyRegAlloc::getUnusedUniRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000796 const MachineInstr *MInst,
Chris Lattner296b7732002-02-05 02:52:05 +0000797 const ValueSet *LVSetBef) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000798
799 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
800
801 bool *IsColorUsedArr = RC->getIsColorUsedArr();
802
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000803 for(unsigned i=0; i < NumAvailRegs; i++) // Reset array
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000804 IsColorUsedArr[i] = false;
805
Chris Lattner296b7732002-02-05 02:52:05 +0000806 ValueSet::const_iterator LIt = LVSetBef->begin();
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000807
808 // for each live var in live variable set after machine inst
809 for( ; LIt != LVSetBef->end(); ++LIt) {
810
811 // get the live range corresponding to live var
812 LiveRange *const LRofLV = LRI.getLiveRangeForValue(*LIt );
813
814 // LR can be null if it is a const since a const
815 // doesn't have a dominating def - see Assumptions above
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000816 if( LRofLV && LRofLV->getRegClass() == RC && LRofLV->hasColor() )
817 IsColorUsedArr[ LRofLV->getColor() ] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000818 }
819
820 // It is possible that one operand of this MInst was already spilled
821 // and it received some register temporarily. If that's the case,
822 // it is recorded in machine operand. We must skip such registers.
823
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000824 setRelRegsUsedByThisInst(RC, MInst);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000825
826 unsigned c; // find first unused color
827 for( c=0; c < NumAvailRegs; c++)
828 if( ! IsColorUsedArr[ c ] ) break;
829
830 if(c < NumAvailRegs)
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000831 return MRI.getUnifiedRegNum(RC->getID(), c);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000832 else
833 return -1;
834
835
836}
837
838
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000839//----------------------------------------------------------------------------
840// Get any other register in a register class, other than what is used
841// by operands of a machine instruction. Returns the unified reg number.
842//----------------------------------------------------------------------------
843int PhyRegAlloc::getUniRegNotUsedByThisInst(RegClass *RC,
844 const MachineInstr *MInst) {
845
846 bool *IsColorUsedArr = RC->getIsColorUsedArr();
847 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
848
849
850 for(unsigned i=0; i < NumAvailRegs ; i++) // Reset array
851 IsColorUsedArr[i] = false;
852
853 setRelRegsUsedByThisInst(RC, MInst);
854
855 unsigned c; // find first unused color
856 for( c=0; c < RC->getNumOfAvailRegs(); c++)
857 if( ! IsColorUsedArr[ c ] ) break;
858
859 if(c < NumAvailRegs)
860 return MRI.getUnifiedRegNum(RC->getID(), c);
861 else
862 assert( 0 && "FATAL: No free register could be found in reg class!!");
Chris Lattner697954c2002-01-20 22:54:45 +0000863 return 0;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000864}
865
866
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000867//----------------------------------------------------------------------------
868// This method modifies the IsColorUsedArr of the register class passed to it.
869// It sets the bits corresponding to the registers used by this machine
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000870// instructions. Both explicit and implicit operands are set.
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000871//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000872void PhyRegAlloc::setRelRegsUsedByThisInst(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000873 const MachineInstr *MInst ) {
874
875 bool *IsColorUsedArr = RC->getIsColorUsedArr();
876
877 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
878
879 const MachineOperand& Op = MInst->getOperand(OpNum);
880
881 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000882 Op.getOperandType() == MachineOperand::MO_CCRegister ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000883
884 const Value *const Val = Op.getVRegValue();
885
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000886 if( Val )
887 if( MRI.getRegClassIDOfValue(Val) == RC->getID() ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000888 int Reg;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000889 if( (Reg=Op.getAllocatedRegNum()) != -1) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000890 IsColorUsedArr[ Reg ] = true;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000891 }
892 else {
893 // it is possilbe that this operand still is not marked with
894 // a register but it has a LR and that received a color
895
896 LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
897 if( LROfVal)
898 if( LROfVal->hasColor() )
899 IsColorUsedArr[ LROfVal->getColor() ] = true;
900 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000901
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000902 } // if reg classes are the same
903 }
904 else if (Op.getOperandType() == MachineOperand::MO_MachineRegister) {
905 IsColorUsedArr[ Op.getMachineRegNum() ] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000906 }
907 }
908
909 // If there are implicit references, mark them as well
910
911 for(unsigned z=0; z < MInst->getNumImplicitRefs(); z++) {
912
913 LiveRange *const LRofImpRef =
914 LRI.getLiveRangeForValue( MInst->getImplicitRef(z) );
Chris Lattner697954c2002-01-20 22:54:45 +0000915
916 if(LRofImpRef && LRofImpRef->hasColor())
917 IsColorUsedArr[LRofImpRef->getColor()] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000918 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000919}
920
921
922
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000923
924
925
926
927
928//----------------------------------------------------------------------------
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000929// If there are delay slots for an instruction, the instructions
930// added after it must really go after the delayed instruction(s).
931// So, we move the InstrAfter of that instruction to the
932// corresponding delayed instruction using the following method.
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000933
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000934//----------------------------------------------------------------------------
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000935void PhyRegAlloc::move2DelayedInstr(const MachineInstr *OrigMI,
936 const MachineInstr *DelayedMI) {
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000937
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000938 // "added after" instructions of the original instr
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000939 std::vector<MachineInstr *> &OrigAft = AddedInstrMap[OrigMI].InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000940
941 // "added instructions" of the delayed instr
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000942 AddedInstrns &DelayAdI = AddedInstrMap[DelayedMI];
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000943
944 // "added after" instructions of the delayed instr
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000945 std::vector<MachineInstr *> &DelayedAft = DelayAdI.InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000946
947 // go thru all the "added after instructions" of the original instruction
948 // and append them to the "addded after instructions" of the delayed
949 // instructions
Chris Lattner697954c2002-01-20 22:54:45 +0000950 DelayedAft.insert(DelayedAft.end(), OrigAft.begin(), OrigAft.end());
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000951
952 // empty the "added after instructions" of the original instruction
953 OrigAft.clear();
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000954}
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000955
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000956//----------------------------------------------------------------------------
957// This method prints the code with registers after register allocation is
958// complete.
959//----------------------------------------------------------------------------
960void PhyRegAlloc::printMachineCode()
961{
962
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000963 cerr << "\n;************** Function " << Meth->getName()
Chris Lattner697954c2002-01-20 22:54:45 +0000964 << " *****************\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000965
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000966 for (Function::const_iterator BBI = Meth->begin(), BBE = Meth->end();
967 BBI != BBE; ++BBI) {
968 cerr << "\n"; printLabel(*BBI); cerr << ": ";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000969
970 // get the iterator for machine instructions
971 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
Vikram S. Adve48762092002-04-25 04:34:15 +0000972 MachineCodeForBasicBlock::iterator MII = MIVec.begin();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000973
974 // iterate over all the machine instructions in BB
Vikram S. Adve48762092002-04-25 04:34:15 +0000975 for( ; MII != MIVec.end(); ++MII) {
976 MachineInstr *const MInst = *MII;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000977
Chris Lattner697954c2002-01-20 22:54:45 +0000978 cerr << "\n\t";
979 cerr << TargetInstrDescriptors[MInst->getOpCode()].opCodeString;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000980
981 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000982 MachineOperand& Op = MInst->getOperand(OpNum);
983
984 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasanka97b8b442001-10-18 22:36:26 +0000985 Op.getOperandType() == MachineOperand::MO_CCRegister /*||
986 Op.getOperandType() == MachineOperand::MO_PCRelativeDisp*/ ) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000987
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000988 const Value *const Val = Op.getVRegValue () ;
Ruchira Sasankae727f852001-09-18 22:43:57 +0000989 // ****this code is temporary till NULL Values are fixed
990 if( ! Val ) {
Chris Lattner697954c2002-01-20 22:54:45 +0000991 cerr << "\t<*NULL*>";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000992 continue;
993 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000994
995 // if a label or a constant
Chris Lattnerdbe53042002-01-21 01:33:12 +0000996 if(isa<BasicBlock>(Val)) {
Chris Lattner697954c2002-01-20 22:54:45 +0000997 cerr << "\t"; printLabel( Op.getVRegValue () );
998 } else {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000999 // else it must be a register value
1000 const int RegNum = Op.getAllocatedRegNum();
1001
Chris Lattner697954c2002-01-20 22:54:45 +00001002 cerr << "\t" << "%" << MRI.getUnifiedRegName( RegNum );
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001003 if (Val->hasName() )
Chris Lattner697954c2002-01-20 22:54:45 +00001004 cerr << "(" << Val->getName() << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001005 else
Chris Lattner697954c2002-01-20 22:54:45 +00001006 cerr << "(" << Val << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001007
1008 if( Op.opIsDef() )
Chris Lattner697954c2002-01-20 22:54:45 +00001009 cerr << "*";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001010
1011 const LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
1012 if( LROfVal )
1013 if( LROfVal->hasSpillOffset() )
Chris Lattner697954c2002-01-20 22:54:45 +00001014 cerr << "$";
Ruchira Sasankae727f852001-09-18 22:43:57 +00001015 }
1016
1017 }
1018 else if(Op.getOperandType() == MachineOperand::MO_MachineRegister) {
Chris Lattner697954c2002-01-20 22:54:45 +00001019 cerr << "\t" << "%" << MRI.getUnifiedRegName(Op.getMachineRegNum());
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001020 }
1021
1022 else
Chris Lattner697954c2002-01-20 22:54:45 +00001023 cerr << "\t" << Op; // use dump field
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001024 }
1025
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001026
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001027
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001028 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
Chris Lattner0665a5f2002-02-05 01:43:49 +00001029 if( NumOfImpRefs > 0) {
Chris Lattner697954c2002-01-20 22:54:45 +00001030 cerr << "\tImplicit:";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001031
Chris Lattner0665a5f2002-02-05 01:43:49 +00001032 for(unsigned z=0; z < NumOfImpRefs; z++)
1033 cerr << RAV(MInst->getImplicitRef(z)) << "\t";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001034 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001035
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001036 } // for all machine instructions
1037
Chris Lattner697954c2002-01-20 22:54:45 +00001038 cerr << "\n";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001039
1040 } // for all BBs
1041
Chris Lattner697954c2002-01-20 22:54:45 +00001042 cerr << "\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001043}
1044
Ruchira Sasankae727f852001-09-18 22:43:57 +00001045
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001046#if 0
1047
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001048//----------------------------------------------------------------------------
1049//
1050//----------------------------------------------------------------------------
1051
1052void PhyRegAlloc::colorCallRetArgs()
1053{
1054
1055 CallRetInstrListType &CallRetInstList = LRI.getCallRetInstrList();
1056 CallRetInstrListType::const_iterator It = CallRetInstList.begin();
1057
1058 for( ; It != CallRetInstList.end(); ++It ) {
1059
Ruchira Sasankaa90e7702001-10-15 16:26:38 +00001060 const MachineInstr *const CRMI = *It;
1061 unsigned OpCode = CRMI->getOpCode();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001062
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001063 // get the added instructions for this Call/Ret instruciton
Chris Lattner0b0ffa02002-04-09 05:13:04 +00001064 AddedInstrns &AI = AddedInstrMap[CRMI];
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001065
Chris Lattner0b0ffa02002-04-09 05:13:04 +00001066 // Tmp stack positions are needed by some calls that have spilled args
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001067 // So reset it before we call each such method
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001068 //mcInfo.popAllTempValues(TM);
1069
Vikram S. Adve12af1642001-11-08 04:48:50 +00001070
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001071 if (TM.getInstrInfo().isCall(OpCode))
Chris Lattner0b0ffa02002-04-09 05:13:04 +00001072 MRI.colorCallArgs(CRMI, LRI, &AI, *this);
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001073 else if (TM.getInstrInfo().isReturn(OpCode))
Chris Lattner0b0ffa02002-04-09 05:13:04 +00001074 MRI.colorRetValue(CRMI, LRI, &AI);
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001075 else
1076 assert(0 && "Non Call/Ret instrn in CallRetInstrList\n");
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001077 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001078}
1079
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001080#endif
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001081
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001082//----------------------------------------------------------------------------
1083
1084//----------------------------------------------------------------------------
1085void PhyRegAlloc::colorIncomingArgs()
1086{
1087 const BasicBlock *const FirstBB = Meth->front();
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001088 const MachineInstr *FirstMI = FirstBB->getMachineInstrVec().front();
1089 assert(FirstMI && "No machine instruction in entry BB");
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001090
Vikram S. Adve48762092002-04-25 04:34:15 +00001091 MRI.colorMethodArgs(Meth, LRI, &AddedInstrAtEntry);
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001092}
1093
Ruchira Sasankae727f852001-09-18 22:43:57 +00001094
1095//----------------------------------------------------------------------------
1096// Used to generate a label for a basic block
1097//----------------------------------------------------------------------------
Chris Lattner697954c2002-01-20 22:54:45 +00001098void PhyRegAlloc::printLabel(const Value *const Val) {
1099 if (Val->hasName())
1100 cerr << Val->getName();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001101 else
Chris Lattner697954c2002-01-20 22:54:45 +00001102 cerr << "Label" << Val;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001103}
1104
1105
Ruchira Sasankae727f852001-09-18 22:43:57 +00001106//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001107// This method calls setSugColorUsable method of each live range. This
1108// will determine whether the suggested color of LR is really usable.
1109// A suggested color is not usable when the suggested color is volatile
1110// AND when there are call interferences
1111//----------------------------------------------------------------------------
1112
1113void PhyRegAlloc::markUnusableSugColors()
1114{
Chris Lattner697954c2002-01-20 22:54:45 +00001115 if(DEBUG_RA ) cerr << "\nmarking unusable suggested colors ...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001116
1117 // hash map iterator
1118 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
1119 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
1120
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001121 for(; HMI != HMIEnd ; ++HMI ) {
1122 if (HMI->first) {
1123 LiveRange *L = HMI->second; // get the LiveRange
1124 if (L) {
1125 if(L->hasSuggestedColor()) {
1126 int RCID = L->getRegClass()->getID();
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001127 if( MRI.isRegVolatile( RCID, L->getSuggestedColor()) &&
1128 L->isCallInterference() )
1129 L->setSuggestedColorUsable( false );
1130 else
1131 L->setSuggestedColorUsable( true );
1132 }
1133 } // if L->hasSuggestedColor()
1134 }
1135 } // for all LR's in hash map
1136}
1137
1138
1139
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001140//----------------------------------------------------------------------------
1141// The following method will set the stack offsets of the live ranges that
1142// are decided to be spillled. This must be called just after coloring the
1143// LRs using the graph coloring algo. For each live range that is spilled,
1144// this method allocate a new spill position on the stack.
1145//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001146
Chris Lattner37730942002-02-05 03:52:29 +00001147void PhyRegAlloc::allocateStackSpace4SpilledLRs() {
1148 if (DEBUG_RA) cerr << "\nsetting LR stack offsets ...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001149
Chris Lattner37730942002-02-05 03:52:29 +00001150 LiveRangeMapType::const_iterator HMI = LRI.getLiveRangeMap()->begin();
1151 LiveRangeMapType::const_iterator HMIEnd = LRI.getLiveRangeMap()->end();
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001152
Chris Lattner37730942002-02-05 03:52:29 +00001153 for( ; HMI != HMIEnd ; ++HMI) {
1154 if (HMI->first && HMI->second) {
1155 LiveRange *L = HMI->second; // get the LiveRange
1156 if (!L->hasColor()) // NOTE: ** allocating the size of long Type **
1157 L->setSpillOffFromFP(mcInfo.allocateSpilledValue(TM, Type::LongTy));
1158 }
1159 } // for all LR's in hash map
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001160}
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001161
1162
1163
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001164//----------------------------------------------------------------------------
Ruchira Sasankae727f852001-09-18 22:43:57 +00001165// The entry pont to Register Allocation
1166//----------------------------------------------------------------------------
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001167
1168void PhyRegAlloc::allocateRegisters()
1169{
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001170
1171 // make sure that we put all register classes into the RegClassList
1172 // before we call constructLiveRanges (now done in the constructor of
1173 // PhyRegAlloc class).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001174 //
1175 LRI.constructLiveRanges(); // create LR info
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001176
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001177 if (DEBUG_RA)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001178 LRI.printLiveRanges();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001179
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001180 createIGNodeListsAndIGs(); // create IGNode list and IGs
1181
1182 buildInterferenceGraphs(); // build IGs in all reg classes
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001183
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001184
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001185 if (DEBUG_RA) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001186 // print all LRs in all reg classes
1187 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1188 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001189
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001190 // print IGs in all register classes
1191 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1192 RegClassList[ rc ]->printIG();
1193 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001194
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001195
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001196 LRI.coalesceLRs(); // coalesce all live ranges
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001197
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +00001198
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001199 if( DEBUG_RA) {
1200 // print all LRs in all reg classes
1201 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1202 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001203
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001204 // print IGs in all register classes
1205 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1206 RegClassList[ rc ]->printIG();
1207 }
1208
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001209
1210 // mark un-usable suggested color before graph coloring algorithm.
1211 // When this is done, the graph coloring algo will not reserve
1212 // suggested color unnecessarily - they can be used by another LR
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001213 //
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001214 markUnusableSugColors();
1215
1216 // color all register classes using the graph coloring algo
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001217 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1218 RegClassList[ rc ]->colorAllRegs();
1219
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001220 // Atter grpah coloring, if some LRs did not receive a color (i.e, spilled)
1221 // a poistion for such spilled LRs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001222 //
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001223 allocateStackSpace4SpilledLRs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001224
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001225 mcInfo.popAllTempValues(TM); // TODO **Check
1226
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001227 // color incoming args - if the correct color was not received
1228 // insert code to copy to the correct register
1229 //
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001230 colorIncomingArgs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001231
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001232 // Now update the machine code with register names and add any
1233 // additional code inserted by the register allocator to the instruction
1234 // stream
1235 //
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001236 updateMachineCode();
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001237
Chris Lattner045e7c82001-09-19 16:26:23 +00001238 if (DEBUG_RA) {
Vikram S. Adve12af1642001-11-08 04:48:50 +00001239 MachineCodeForMethod::get(Meth).dump();
Chris Lattner045e7c82001-09-19 16:26:23 +00001240 printMachineCode(); // only for DEBUGGING
1241 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001242}
1243
Ruchira Sasankae727f852001-09-18 22:43:57 +00001244
1245