blob: 0b680ab9e49ac71ba8c973a632e008b9108d2b39 [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"
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000016#include "llvm/CodeGen/MachineCodeForMethod.h"
Chris Lattner0a8ed942002-02-04 05:56:09 +000017#include "llvm/Analysis/LiveVar/MethodLiveVarInfo.h"
Chris Lattner14ab1ce2002-02-04 17:48:00 +000018#include "llvm/Analysis/LoopInfo.h"
Vikram S. Adve12af1642001-11-08 04:48:50 +000019#include "llvm/Target/TargetMachine.h"
20#include "llvm/Target/MachineFrameInfo.h"
Chris Lattner221d6882002-02-12 21:07:25 +000021#include "llvm/BasicBlock.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000022#include "llvm/Function.h"
Chris Lattner37730942002-02-05 03:52:29 +000023#include "llvm/Type.h"
Chris Lattner697954c2002-01-20 22:54:45 +000024#include <iostream>
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000025#include <math.h>
Chris Lattner697954c2002-01-20 22:54:45 +000026using std::cerr;
Vikram S. Adve12af1642001-11-08 04:48:50 +000027
28
29// ***TODO: There are several places we add instructions. Validate the order
30// of adding these instructions.
Ruchira Sasanka174bded2001-10-28 18:12:02 +000031
Chris Lattner045e7c82001-09-19 16:26:23 +000032cl::Enum<RegAllocDebugLevel_t> DEBUG_RA("dregalloc", cl::NoFlags,
33 "enable register allocation debugging information",
34 clEnumValN(RA_DEBUG_None , "n", "disable debug output"),
35 clEnumValN(RA_DEBUG_Normal , "y", "enable debug output"),
36 clEnumValN(RA_DEBUG_Verbose, "v", "enable extra debug output"), 0);
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000037
38
Chris Lattner2f9b28e2002-02-04 15:54:09 +000039//----------------------------------------------------------------------------
40// RegisterAllocation pass front end...
41//----------------------------------------------------------------------------
42namespace {
43 class RegisterAllocator : public MethodPass {
44 TargetMachine &Target;
45 public:
46 inline RegisterAllocator(TargetMachine &T) : Target(T) {}
Chris Lattner6dd98a62002-02-04 00:33:08 +000047
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000048 bool runOnMethod(Function *F) {
Chris Lattner2f9b28e2002-02-04 15:54:09 +000049 if (DEBUG_RA)
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000050 cerr << "\n******************** Method "<< F->getName()
Chris Lattner2f9b28e2002-02-04 15:54:09 +000051 << " ********************\n";
52
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000053 PhyRegAlloc PRA(F, Target, &getAnalysis<MethodLiveVarInfo>(),
Chris Lattner14ab1ce2002-02-04 17:48:00 +000054 &getAnalysis<cfg::LoopInfo>());
Chris Lattner2f9b28e2002-02-04 15:54:09 +000055 PRA.allocateRegisters();
56
57 if (DEBUG_RA) cerr << "\nRegister allocation complete!\n";
58 return false;
59 }
Chris Lattner4911c352002-02-04 17:39:42 +000060
61 virtual void getAnalysisUsageInfo(Pass::AnalysisSet &Requires,
62 Pass::AnalysisSet &Destroyed,
63 Pass::AnalysisSet &Provided) {
Chris Lattner14ab1ce2002-02-04 17:48:00 +000064 Requires.push_back(cfg::LoopInfo::ID);
Chris Lattner4d7fc112002-02-04 20:02:38 +000065 Requires.push_back(MethodLiveVarInfo::ID);
Vikram S. Adve9c4f7262002-03-24 03:54:03 +000066 Destroyed.push_back(MethodLiveVarInfo::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 Lattner2f9b28e2002-02-04 15:54:09 +000071MethodPass *getRegisterAllocator(TargetMachine &T) {
72 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 Lattner2fbfdcf2002-04-07 20:49:59 +000078PhyRegAlloc::PhyRegAlloc(Function *F,
Ruchira Sasanka8e604792001-09-14 21:18:34 +000079 const TargetMachine& tm,
Chris Lattner4911c352002-02-04 17:39:42 +000080 MethodLiveVarInfo *Lvi,
Chris Lattner14ab1ce2002-02-04 17:48:00 +000081 cfg::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 //
253 if( const Value *RetVal = MRI.getCallInstRetVal( MInst )) {
254 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.
261 if( const Value *AddrVal = MRI.getCallInstIndirectAddrVal( MInst )) {
262 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 Lattner697954c2002-01-20 22:54:45 +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 Lattner2fbfdcf2002-04-07 20:49:59 +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 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +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 //
Vikram S. Adve48762092002-04-25 04:34:15 +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 Lattner748697d2002-02-05 04:20:12 +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
Ruchira Sasanka958faf32001-10-19 17:21:03 +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();
341 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
357 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;
382 for(++It2; It2 != ItE; ++It2) {
383 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
389 if( RCOfOp1 == RCOfOp2 ){
390 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 Lattner748697d2002-02-05 04:20:12 +0000411 const ValueSet &InSet = LVI->getInSetOfBB(Meth->front());
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000412
Chris Lattner296b7732002-02-05 02:52:05 +0000413 // get the argument list
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000414 const Function::ArgumentListType &ArgList = Meth->getArgumentList();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000415
Chris Lattner296b7732002-02-05 02:52:05 +0000416 // get an iterator to arg list
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000417 Function::ArgumentListType::const_iterator ArgIt = ArgList.begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000418
419
420 for( ; ArgIt != ArgList.end() ; ++ArgIt) { // for each argument
Chris Lattner748697d2002-02-05 04:20:12 +0000421 addInterference((Value*)*ArgIt, &InSet, false);// add interferences between
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000422 // args and LVars at start
Chris Lattner0665a5f2002-02-05 01:43:49 +0000423 if( DEBUG_RA > 1)
424 cerr << " - %% adding interference for argument "
425 << RAV((const Value *)*ArgIt) << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000426 }
427}
428
429
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000430//----------------------------------------------------------------------------
431// This method is called after register allocation is complete to set the
432// allocated reisters in the machine code. This code will add register numbers
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000433// to MachineOperands that contain a Value. Also it calls target specific
434// methods to produce caller saving instructions. At the end, it adds all
435// additional instructions produced by the register allocator to the
436// instruction stream.
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000437//----------------------------------------------------------------------------
Vikram S. Adve48762092002-04-25 04:34:15 +0000438
439//-----------------------------
440// Utility functions used below
441//-----------------------------
442inline void
443PrependInstructions(std::deque<MachineInstr *> &IBef,
444 MachineCodeForBasicBlock& MIVec,
445 MachineCodeForBasicBlock::iterator& MII,
446 const std::string& msg)
447{
448 if (!IBef.empty())
449 {
450 MachineInstr* OrigMI = *MII;
451 std::deque<MachineInstr *>::iterator AdIt;
452 for (AdIt = IBef.begin(); AdIt != IBef.end() ; ++AdIt)
453 {
454 if (DEBUG_RA) {
455 if (OrigMI) cerr << "For MInst: " << *OrigMI;
456 cerr << msg << " PREPENDed instr: " << **AdIt << "\n";
457 }
458 MII = MIVec.insert(MII, *AdIt);
459 ++MII;
460 }
461 }
462}
463
464inline void
465AppendInstructions(std::deque<MachineInstr *> &IAft,
466 MachineCodeForBasicBlock& MIVec,
467 MachineCodeForBasicBlock::iterator& MII,
468 const std::string& msg)
469{
470 if (!IAft.empty())
471 {
472 MachineInstr* OrigMI = *MII;
473 std::deque<MachineInstr *>::iterator AdIt;
474 for( AdIt = IAft.begin(); AdIt != IAft.end() ; ++AdIt )
475 {
476 if(DEBUG_RA) {
477 if (OrigMI) cerr << "For MInst: " << *OrigMI;
478 cerr << msg << " APPENDed instr: " << **AdIt << "\n";
479 }
480 ++MII; // insert before the next instruction
481 MII = MIVec.insert(MII, *AdIt);
482 }
483 }
484}
485
486
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000487void PhyRegAlloc::updateMachineCode()
488{
Vikram S. Adve48762092002-04-25 04:34:15 +0000489 const BasicBlock* entryBB = Meth->getEntryNode();
490 if (entryBB) {
491 MachineCodeForBasicBlock& MIVec = entryBB->getMachineInstrVec();
492 MachineCodeForBasicBlock::iterator MII = MIVec.begin();
493
494 // Insert any instructions needed at method entry
495 PrependInstructions(AddedInstrAtEntry.InstrnsBefore, MIVec, MII,
496 "At function entry: \n");
497 assert(AddedInstrAtEntry.InstrnsAfter.empty() &&
498 "InstrsAfter should be unnecessary since we are just inserting at "
499 "the function entry point here.");
500 }
501
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000502 for (Function::const_iterator BBI = Meth->begin(), BBE = Meth->end();
503 BBI != BBE; ++BBI) {
Vikram S. Adve48762092002-04-25 04:34:15 +0000504
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000505 // iterate over all the machine instructions in BB
Vikram S. Adve48762092002-04-25 04:34:15 +0000506 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
507 for(MachineCodeForBasicBlock::iterator MII = MIVec.begin();
508 MII != MIVec.end(); ++MII) {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000509
Vikram S. Adve48762092002-04-25 04:34:15 +0000510 MachineInstr *MInst = *MII;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000511
512 unsigned Opcode = MInst->getOpCode();
513
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000514 // do not process Phis
Vikram S. Adve23a4c8f2002-03-18 03:37:19 +0000515 if (TM.getInstrInfo().isDummyPhiInstr(Opcode))
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000516 continue;
517
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000518 // Now insert speical instructions (if necessary) for call/return
519 // instructions.
520 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000521 if (TM.getInstrInfo().isCall(Opcode) ||
522 TM.getInstrInfo().isReturn(Opcode)) {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000523
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000524 AddedInstrns &AI = AddedInstrMap[MInst];
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000525
526 // Tmp stack poistions are needed by some calls that have spilled args
527 // So reset it before we call each such method
Ruchira Sasanka6a3db8c2002-01-07 21:09:06 +0000528 //
529 mcInfo.popAllTempValues(TM);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000530
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000531 if (TM.getInstrInfo().isCall(Opcode))
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000532 MRI.colorCallArgs(MInst, LRI, &AI, *this, *BBI);
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000533 else if (TM.getInstrInfo().isReturn(Opcode))
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000534 MRI.colorRetValue(MInst, LRI, &AI);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000535 }
536
537
538 /* -- Using above code instead of this
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000539
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000540 // if this machine instr is call, insert caller saving code
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000541
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000542 if( (TM.getInstrInfo()).isCall( MInst->getOpCode()) )
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000543 MRI.insertCallerSavingCode(MInst, *BBI, *this );
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000544
545 */
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000546
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000547
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000548 // reset the stack offset for temporary variables since we may
549 // need that to spill
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000550 // mcInfo.popAllTempValues(TM);
Ruchira Sasankaf90870f2001-11-15 22:02:06 +0000551 // TODO ** : do later
Vikram S. Adve12af1642001-11-08 04:48:50 +0000552
Chris Lattner7a176752001-12-04 00:03:30 +0000553 //for(MachineInstr::val_const_op_iterator OpI(MInst);!OpI.done();++OpI) {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000554
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000555
556 // Now replace set the registers for operands in the machine instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000557 //
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000558 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
559
560 MachineOperand& Op = MInst->getOperand(OpNum);
561
562 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
563 Op.getOperandType() == MachineOperand::MO_CCRegister) {
564
565 const Value *const Val = Op.getVRegValue();
566
567 // delete this condition checking later (must assert if Val is null)
Chris Lattner045e7c82001-09-19 16:26:23 +0000568 if( !Val) {
569 if (DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000570 cerr << "Warning: NULL Value found for operand\n";
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000571 continue;
572 }
573 assert( Val && "Value is NULL");
574
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000575 LiveRange *const LR = LRI.getLiveRangeForValue(Val);
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000576
577 if ( !LR ) {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000578
579 // nothing to worry if it's a const or a label
580
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000581 if (DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000582 cerr << "*NO LR for operand : " << Op ;
583 cerr << " [reg:" << Op.getAllocatedRegNum() << "]";
584 cerr << " in inst:\t" << *MInst << "\n";
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000585 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000586
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000587 // if register is not allocated, mark register as invalid
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000588 if( Op.getAllocatedRegNum() == -1)
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000589 Op.setRegForValue( MRI.getInvalidRegNum());
Ruchira Sasankae727f852001-09-18 22:43:57 +0000590
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000591
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000592 continue;
593 }
594
595 unsigned RCID = (LR->getRegClass())->getID();
596
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000597 if( LR->hasColor() ) {
598 Op.setRegForValue( MRI.getUnifiedRegNum(RCID, LR->getColor()) );
599 }
600 else {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000601
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000602 // LR did NOT receive a color (register). Now, insert spill code
603 // for spilled opeands in this machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000604
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000605 //assert(0 && "LR must be spilled");
606 insertCode4SpilledLR(LR, MInst, *BBI, OpNum );
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000607
608 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000609 }
610
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000611 } // for each operand
612
613
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000614 // Now add instructions that the register allocator inserts before/after
615 // this machine instructions (done only for calls/rets/incoming args)
616 // We do this here, to ensure that spill for an instruction is inserted
617 // closest as possible to an instruction (see above insertCode4Spill...)
618 //
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000619 // If there are instructions to be added, *before* this machine
620 // instruction, add them now.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000621 //
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000622 if(AddedInstrMap.count(MInst)) {
Vikram S. Adve48762092002-04-25 04:34:15 +0000623 PrependInstructions(AddedInstrMap[MInst].InstrnsBefore, MIVec, MII,"");
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000624 }
Vikram S. Adve48762092002-04-25 04:34:15 +0000625
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000626 // If there are instructions to be added *after* this machine
627 // instruction, add them now
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000628 //
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000629 if (!AddedInstrMap[MInst].InstrnsAfter.empty()) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000630
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000631 // if there are delay slots for this instruction, the instructions
632 // added after it must really go after the delayed instruction(s)
633 // So, we move the InstrAfter of the current instruction to the
634 // corresponding delayed instruction
635
636 unsigned delay;
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000637 if ((delay=TM.getInstrInfo().getNumDelaySlots(MInst->getOpCode())) >0){
Vikram S. Adve48762092002-04-25 04:34:15 +0000638 move2DelayedInstr(MInst, *(MII+delay) );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000639
Chris Lattner697954c2002-01-20 22:54:45 +0000640 if(DEBUG_RA) cerr<< "\nMoved an added instr after the delay slot";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000641 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000642
643 else {
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000644 // Here we can add the "instructions after" to the current
645 // instruction since there are no delay slots for this instruction
Vikram S. Adve48762092002-04-25 04:34:15 +0000646 AppendInstructions(AddedInstrMap[MInst].InstrnsAfter, MIVec, MII,"");
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000647 } // if not delay
648
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000649 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000650
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000651 } // for each machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000652 }
653}
654
655
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000656
657//----------------------------------------------------------------------------
658// This method inserts spill code for AN operand whose LR was spilled.
659// This method may be called several times for a single machine instruction
660// if it contains many spilled operands. Each time it is called, it finds
661// a register which is not live at that instruction and also which is not
662// used by other spilled operands of the same instruction. Then it uses
663// this register temporarily to accomodate the spilled value.
664//----------------------------------------------------------------------------
665void PhyRegAlloc::insertCode4SpilledLR(const LiveRange *LR,
666 MachineInstr *MInst,
667 const BasicBlock *BB,
668 const unsigned OpNum) {
669
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000670 assert(! TM.getInstrInfo().isCall(MInst->getOpCode()) &&
671 (! TM.getInstrInfo().isReturn(MInst->getOpCode())) &&
672 "Arg of a call/ret must be handled elsewhere");
673
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000674 MachineOperand& Op = MInst->getOperand(OpNum);
675 bool isDef = MInst->operandIsDefined(OpNum);
676 unsigned RegType = MRI.getRegType( LR );
677 int SpillOff = LR->getSpillOffFromFP();
678 RegClass *RC = LR->getRegClass();
Chris Lattner748697d2002-02-05 04:20:12 +0000679 const ValueSet &LVSetBef = LVI->getLiveVarSetBeforeMInst(MInst, BB);
Vikram S. Adve00521d72001-11-12 23:26:35 +0000680
Chris Lattner697954c2002-01-20 22:54:45 +0000681 mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000682
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000683 MachineInstr *MIBef=NULL, *AdIMid=NULL, *MIAft=NULL;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000684
Chris Lattner748697d2002-02-05 04:20:12 +0000685 int TmpRegU = getUsableUniRegAtMI(RC, RegType, MInst,&LVSetBef, MIBef, MIAft);
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000686
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000687 // get the added instructions for this instruciton
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000688 AddedInstrns &AI = AddedInstrMap[MInst];
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000689
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000690 if (!isDef) {
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000691 // for a USE, we have to load the value of LR from stack to a TmpReg
692 // and use the TmpReg as one operand of instruction
693
694 // actual loading instruction
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000695 AdIMid = MRI.cpMem2RegMI(MRI.getFramePointer(), SpillOff, TmpRegU,RegType);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000696
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000697 if(MIBef)
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000698 AI.InstrnsBefore.push_back(MIBef);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000699
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000700 AI.InstrnsBefore.push_back(AdIMid);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000701
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000702 if(MIAft)
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000703 AI.InstrnsAfter.push_front(MIAft);
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000704
Chris Lattner296b7732002-02-05 02:52:05 +0000705 } else { // if this is a Def
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000706 // for a DEF, we have to store the value produced by this instruction
707 // on the stack position allocated for this LR
708
709 // actual storing instruction
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000710 AdIMid = MRI.cpReg2MemMI(TmpRegU, MRI.getFramePointer(), SpillOff,RegType);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000711
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000712 if (MIBef)
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000713 AI.InstrnsBefore.push_back(MIBef);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000714
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000715 AI.InstrnsAfter.push_front(AdIMid);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000716
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000717 if (MIAft)
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000718 AI.InstrnsAfter.push_front(MIAft);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000719
720 } // if !DEF
721
722 cerr << "\nFor Inst " << *MInst;
Chris Lattner296b7732002-02-05 02:52:05 +0000723 cerr << " - SPILLED LR: "; printSet(*LR);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000724 cerr << "\n - Added Instructions:";
Chris Lattner296b7732002-02-05 02:52:05 +0000725 if (MIBef) cerr << *MIBef;
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000726 cerr << *AdIMid;
Chris Lattner296b7732002-02-05 02:52:05 +0000727 if (MIAft) cerr << *MIAft;
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000728
Chris Lattner296b7732002-02-05 02:52:05 +0000729 Op.setRegForValue(TmpRegU); // set the opearnd
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000730}
731
732
733
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000734//----------------------------------------------------------------------------
735// We can use the following method to get a temporary register to be used
736// BEFORE any given machine instruction. If there is a register available,
737// this method will simply return that register and set MIBef = MIAft = NULL.
738// Otherwise, it will return a register and MIAft and MIBef will contain
739// two instructions used to free up this returned register.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000740// Returned register number is the UNIFIED register number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000741//----------------------------------------------------------------------------
742
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000743int PhyRegAlloc::getUsableUniRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000744 const int RegType,
745 const MachineInstr *MInst,
Chris Lattner296b7732002-02-05 02:52:05 +0000746 const ValueSet *LVSetBef,
Vikram S. Adve23a4c8f2002-03-18 03:37:19 +0000747 MachineInstr *&MIBef,
748 MachineInstr *&MIAft) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000749
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000750 int RegU = getUnusedUniRegAtMI(RC, MInst, LVSetBef);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000751
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000752
753 if( RegU != -1) {
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000754 // we found an unused register, so we can simply use it
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000755 MIBef = MIAft = NULL;
756 }
757 else {
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000758 // we couldn't find an unused register. Generate code to free up a reg by
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000759 // saving it on stack and restoring after the instruction
760
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000761 int TmpOff = mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
Vikram S. Adve12af1642001-11-08 04:48:50 +0000762
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000763 RegU = getUniRegNotUsedByThisInst(RC, MInst);
764 MIBef = MRI.cpReg2MemMI(RegU, MRI.getFramePointer(), TmpOff, RegType );
765 MIAft = MRI.cpMem2RegMI(MRI.getFramePointer(), TmpOff, RegU, RegType );
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000766 }
767
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000768 return RegU;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000769}
770
771//----------------------------------------------------------------------------
772// This method is called to get a new unused register that can be used to
773// accomodate a spilled value.
774// This method may be called several times for a single machine instruction
775// if it contains many spilled operands. Each time it is called, it finds
776// a register which is not live at that instruction and also which is not
777// used by other spilled operands of the same instruction.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000778// Return register number is relative to the register class. NOT
779// unified number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000780//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000781int PhyRegAlloc::getUnusedUniRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000782 const MachineInstr *MInst,
Chris Lattner296b7732002-02-05 02:52:05 +0000783 const ValueSet *LVSetBef) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000784
785 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
786
787 bool *IsColorUsedArr = RC->getIsColorUsedArr();
788
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000789 for(unsigned i=0; i < NumAvailRegs; i++) // Reset array
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000790 IsColorUsedArr[i] = false;
791
Chris Lattner296b7732002-02-05 02:52:05 +0000792 ValueSet::const_iterator LIt = LVSetBef->begin();
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000793
794 // for each live var in live variable set after machine inst
795 for( ; LIt != LVSetBef->end(); ++LIt) {
796
797 // get the live range corresponding to live var
798 LiveRange *const LRofLV = LRI.getLiveRangeForValue(*LIt );
799
800 // LR can be null if it is a const since a const
801 // doesn't have a dominating def - see Assumptions above
802 if( LRofLV )
803 if( LRofLV->hasColor() )
804 IsColorUsedArr[ LRofLV->getColor() ] = true;
805 }
806
807 // It is possible that one operand of this MInst was already spilled
808 // and it received some register temporarily. If that's the case,
809 // it is recorded in machine operand. We must skip such registers.
810
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000811 setRelRegsUsedByThisInst(RC, MInst);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000812
813 unsigned c; // find first unused color
814 for( c=0; c < NumAvailRegs; c++)
815 if( ! IsColorUsedArr[ c ] ) break;
816
817 if(c < NumAvailRegs)
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000818 return MRI.getUnifiedRegNum(RC->getID(), c);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000819 else
820 return -1;
821
822
823}
824
825
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000826//----------------------------------------------------------------------------
827// Get any other register in a register class, other than what is used
828// by operands of a machine instruction. Returns the unified reg number.
829//----------------------------------------------------------------------------
830int PhyRegAlloc::getUniRegNotUsedByThisInst(RegClass *RC,
831 const MachineInstr *MInst) {
832
833 bool *IsColorUsedArr = RC->getIsColorUsedArr();
834 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
835
836
837 for(unsigned i=0; i < NumAvailRegs ; i++) // Reset array
838 IsColorUsedArr[i] = false;
839
840 setRelRegsUsedByThisInst(RC, MInst);
841
842 unsigned c; // find first unused color
843 for( c=0; c < RC->getNumOfAvailRegs(); c++)
844 if( ! IsColorUsedArr[ c ] ) break;
845
846 if(c < NumAvailRegs)
847 return MRI.getUnifiedRegNum(RC->getID(), c);
848 else
849 assert( 0 && "FATAL: No free register could be found in reg class!!");
Chris Lattner697954c2002-01-20 22:54:45 +0000850 return 0;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000851}
852
853
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000854//----------------------------------------------------------------------------
855// This method modifies the IsColorUsedArr of the register class passed to it.
856// It sets the bits corresponding to the registers used by this machine
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000857// instructions. Both explicit and implicit operands are set.
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000858//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000859void PhyRegAlloc::setRelRegsUsedByThisInst(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000860 const MachineInstr *MInst ) {
861
862 bool *IsColorUsedArr = RC->getIsColorUsedArr();
863
864 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
865
866 const MachineOperand& Op = MInst->getOperand(OpNum);
867
868 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000869 Op.getOperandType() == MachineOperand::MO_CCRegister ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000870
871 const Value *const Val = Op.getVRegValue();
872
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000873 if( Val )
874 if( MRI.getRegClassIDOfValue(Val) == RC->getID() ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000875 int Reg;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000876 if( (Reg=Op.getAllocatedRegNum()) != -1) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000877 IsColorUsedArr[ Reg ] = true;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000878 }
879 else {
880 // it is possilbe that this operand still is not marked with
881 // a register but it has a LR and that received a color
882
883 LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
884 if( LROfVal)
885 if( LROfVal->hasColor() )
886 IsColorUsedArr[ LROfVal->getColor() ] = true;
887 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000888
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000889 } // if reg classes are the same
890 }
891 else if (Op.getOperandType() == MachineOperand::MO_MachineRegister) {
892 IsColorUsedArr[ Op.getMachineRegNum() ] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000893 }
894 }
895
896 // If there are implicit references, mark them as well
897
898 for(unsigned z=0; z < MInst->getNumImplicitRefs(); z++) {
899
900 LiveRange *const LRofImpRef =
901 LRI.getLiveRangeForValue( MInst->getImplicitRef(z) );
Chris Lattner697954c2002-01-20 22:54:45 +0000902
903 if(LRofImpRef && LRofImpRef->hasColor())
904 IsColorUsedArr[LRofImpRef->getColor()] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000905 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000906}
907
908
909
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000910
911
912
913
914
915//----------------------------------------------------------------------------
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000916// If there are delay slots for an instruction, the instructions
917// added after it must really go after the delayed instruction(s).
918// So, we move the InstrAfter of that instruction to the
919// corresponding delayed instruction using the following method.
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000920
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000921//----------------------------------------------------------------------------
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000922void PhyRegAlloc::move2DelayedInstr(const MachineInstr *OrigMI,
923 const MachineInstr *DelayedMI) {
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000924
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000925 // "added after" instructions of the original instr
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000926 std::deque<MachineInstr *> &OrigAft = AddedInstrMap[OrigMI].InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000927
928 // "added instructions" of the delayed instr
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000929 AddedInstrns &DelayAdI = AddedInstrMap[DelayedMI];
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000930
931 // "added after" instructions of the delayed instr
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000932 std::deque<MachineInstr *> &DelayedAft = DelayAdI.InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000933
934 // go thru all the "added after instructions" of the original instruction
935 // and append them to the "addded after instructions" of the delayed
936 // instructions
Chris Lattner697954c2002-01-20 22:54:45 +0000937 DelayedAft.insert(DelayedAft.end(), OrigAft.begin(), OrigAft.end());
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000938
939 // empty the "added after instructions" of the original instruction
940 OrigAft.clear();
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000941}
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000942
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000943//----------------------------------------------------------------------------
944// This method prints the code with registers after register allocation is
945// complete.
946//----------------------------------------------------------------------------
947void PhyRegAlloc::printMachineCode()
948{
949
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000950 cerr << "\n;************** Function " << Meth->getName()
Chris Lattner697954c2002-01-20 22:54:45 +0000951 << " *****************\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000952
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000953 for (Function::const_iterator BBI = Meth->begin(), BBE = Meth->end();
954 BBI != BBE; ++BBI) {
955 cerr << "\n"; printLabel(*BBI); cerr << ": ";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000956
957 // get the iterator for machine instructions
958 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
Vikram S. Adve48762092002-04-25 04:34:15 +0000959 MachineCodeForBasicBlock::iterator MII = MIVec.begin();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000960
961 // iterate over all the machine instructions in BB
Vikram S. Adve48762092002-04-25 04:34:15 +0000962 for( ; MII != MIVec.end(); ++MII) {
963 MachineInstr *const MInst = *MII;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000964
Chris Lattner697954c2002-01-20 22:54:45 +0000965 cerr << "\n\t";
966 cerr << TargetInstrDescriptors[MInst->getOpCode()].opCodeString;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000967
968 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000969 MachineOperand& Op = MInst->getOperand(OpNum);
970
971 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasanka97b8b442001-10-18 22:36:26 +0000972 Op.getOperandType() == MachineOperand::MO_CCRegister /*||
973 Op.getOperandType() == MachineOperand::MO_PCRelativeDisp*/ ) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000974
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000975 const Value *const Val = Op.getVRegValue () ;
Ruchira Sasankae727f852001-09-18 22:43:57 +0000976 // ****this code is temporary till NULL Values are fixed
977 if( ! Val ) {
Chris Lattner697954c2002-01-20 22:54:45 +0000978 cerr << "\t<*NULL*>";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000979 continue;
980 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000981
982 // if a label or a constant
Chris Lattnerdbe53042002-01-21 01:33:12 +0000983 if(isa<BasicBlock>(Val)) {
Chris Lattner697954c2002-01-20 22:54:45 +0000984 cerr << "\t"; printLabel( Op.getVRegValue () );
985 } else {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000986 // else it must be a register value
987 const int RegNum = Op.getAllocatedRegNum();
988
Chris Lattner697954c2002-01-20 22:54:45 +0000989 cerr << "\t" << "%" << MRI.getUnifiedRegName( RegNum );
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000990 if (Val->hasName() )
Chris Lattner697954c2002-01-20 22:54:45 +0000991 cerr << "(" << Val->getName() << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000992 else
Chris Lattner697954c2002-01-20 22:54:45 +0000993 cerr << "(" << Val << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000994
995 if( Op.opIsDef() )
Chris Lattner697954c2002-01-20 22:54:45 +0000996 cerr << "*";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000997
998 const LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
999 if( LROfVal )
1000 if( LROfVal->hasSpillOffset() )
Chris Lattner697954c2002-01-20 22:54:45 +00001001 cerr << "$";
Ruchira Sasankae727f852001-09-18 22:43:57 +00001002 }
1003
1004 }
1005 else if(Op.getOperandType() == MachineOperand::MO_MachineRegister) {
Chris Lattner697954c2002-01-20 22:54:45 +00001006 cerr << "\t" << "%" << MRI.getUnifiedRegName(Op.getMachineRegNum());
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001007 }
1008
1009 else
Chris Lattner697954c2002-01-20 22:54:45 +00001010 cerr << "\t" << Op; // use dump field
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001011 }
1012
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001013
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001014
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001015 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
Chris Lattner0665a5f2002-02-05 01:43:49 +00001016 if( NumOfImpRefs > 0) {
Chris Lattner697954c2002-01-20 22:54:45 +00001017 cerr << "\tImplicit:";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001018
Chris Lattner0665a5f2002-02-05 01:43:49 +00001019 for(unsigned z=0; z < NumOfImpRefs; z++)
1020 cerr << RAV(MInst->getImplicitRef(z)) << "\t";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001021 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001022
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001023 } // for all machine instructions
1024
Chris Lattner697954c2002-01-20 22:54:45 +00001025 cerr << "\n";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001026
1027 } // for all BBs
1028
Chris Lattner697954c2002-01-20 22:54:45 +00001029 cerr << "\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001030}
1031
Ruchira Sasankae727f852001-09-18 22:43:57 +00001032
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001033#if 0
1034
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001035//----------------------------------------------------------------------------
1036//
1037//----------------------------------------------------------------------------
1038
1039void PhyRegAlloc::colorCallRetArgs()
1040{
1041
1042 CallRetInstrListType &CallRetInstList = LRI.getCallRetInstrList();
1043 CallRetInstrListType::const_iterator It = CallRetInstList.begin();
1044
1045 for( ; It != CallRetInstList.end(); ++It ) {
1046
Ruchira Sasankaa90e7702001-10-15 16:26:38 +00001047 const MachineInstr *const CRMI = *It;
1048 unsigned OpCode = CRMI->getOpCode();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001049
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001050 // get the added instructions for this Call/Ret instruciton
Chris Lattner0b0ffa02002-04-09 05:13:04 +00001051 AddedInstrns &AI = AddedInstrMap[CRMI];
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001052
Chris Lattner0b0ffa02002-04-09 05:13:04 +00001053 // Tmp stack positions are needed by some calls that have spilled args
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001054 // So reset it before we call each such method
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001055 //mcInfo.popAllTempValues(TM);
1056
Vikram S. Adve12af1642001-11-08 04:48:50 +00001057
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001058 if (TM.getInstrInfo().isCall(OpCode))
Chris Lattner0b0ffa02002-04-09 05:13:04 +00001059 MRI.colorCallArgs(CRMI, LRI, &AI, *this);
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001060 else if (TM.getInstrInfo().isReturn(OpCode))
Chris Lattner0b0ffa02002-04-09 05:13:04 +00001061 MRI.colorRetValue(CRMI, LRI, &AI);
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001062 else
1063 assert(0 && "Non Call/Ret instrn in CallRetInstrList\n");
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001064 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001065}
1066
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001067#endif
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001068
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001069//----------------------------------------------------------------------------
1070
1071//----------------------------------------------------------------------------
1072void PhyRegAlloc::colorIncomingArgs()
1073{
1074 const BasicBlock *const FirstBB = Meth->front();
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001075 const MachineInstr *FirstMI = FirstBB->getMachineInstrVec().front();
1076 assert(FirstMI && "No machine instruction in entry BB");
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001077
Vikram S. Adve48762092002-04-25 04:34:15 +00001078 MRI.colorMethodArgs(Meth, LRI, &AddedInstrAtEntry);
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001079}
1080
Ruchira Sasankae727f852001-09-18 22:43:57 +00001081
1082//----------------------------------------------------------------------------
1083// Used to generate a label for a basic block
1084//----------------------------------------------------------------------------
Chris Lattner697954c2002-01-20 22:54:45 +00001085void PhyRegAlloc::printLabel(const Value *const Val) {
1086 if (Val->hasName())
1087 cerr << Val->getName();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001088 else
Chris Lattner697954c2002-01-20 22:54:45 +00001089 cerr << "Label" << Val;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001090}
1091
1092
Ruchira Sasankae727f852001-09-18 22:43:57 +00001093//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001094// This method calls setSugColorUsable method of each live range. This
1095// will determine whether the suggested color of LR is really usable.
1096// A suggested color is not usable when the suggested color is volatile
1097// AND when there are call interferences
1098//----------------------------------------------------------------------------
1099
1100void PhyRegAlloc::markUnusableSugColors()
1101{
Chris Lattner697954c2002-01-20 22:54:45 +00001102 if(DEBUG_RA ) cerr << "\nmarking unusable suggested colors ...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001103
1104 // hash map iterator
1105 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
1106 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
1107
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001108 for(; HMI != HMIEnd ; ++HMI ) {
1109 if (HMI->first) {
1110 LiveRange *L = HMI->second; // get the LiveRange
1111 if (L) {
1112 if(L->hasSuggestedColor()) {
1113 int RCID = L->getRegClass()->getID();
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001114 if( MRI.isRegVolatile( RCID, L->getSuggestedColor()) &&
1115 L->isCallInterference() )
1116 L->setSuggestedColorUsable( false );
1117 else
1118 L->setSuggestedColorUsable( true );
1119 }
1120 } // if L->hasSuggestedColor()
1121 }
1122 } // for all LR's in hash map
1123}
1124
1125
1126
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001127//----------------------------------------------------------------------------
1128// The following method will set the stack offsets of the live ranges that
1129// are decided to be spillled. This must be called just after coloring the
1130// LRs using the graph coloring algo. For each live range that is spilled,
1131// this method allocate a new spill position on the stack.
1132//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001133
Chris Lattner37730942002-02-05 03:52:29 +00001134void PhyRegAlloc::allocateStackSpace4SpilledLRs() {
1135 if (DEBUG_RA) cerr << "\nsetting LR stack offsets ...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001136
Chris Lattner37730942002-02-05 03:52:29 +00001137 LiveRangeMapType::const_iterator HMI = LRI.getLiveRangeMap()->begin();
1138 LiveRangeMapType::const_iterator HMIEnd = LRI.getLiveRangeMap()->end();
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001139
Chris Lattner37730942002-02-05 03:52:29 +00001140 for( ; HMI != HMIEnd ; ++HMI) {
1141 if (HMI->first && HMI->second) {
1142 LiveRange *L = HMI->second; // get the LiveRange
1143 if (!L->hasColor()) // NOTE: ** allocating the size of long Type **
1144 L->setSpillOffFromFP(mcInfo.allocateSpilledValue(TM, Type::LongTy));
1145 }
1146 } // for all LR's in hash map
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001147}
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001148
1149
1150
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001151//----------------------------------------------------------------------------
Ruchira Sasankae727f852001-09-18 22:43:57 +00001152// The entry pont to Register Allocation
1153//----------------------------------------------------------------------------
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001154
1155void PhyRegAlloc::allocateRegisters()
1156{
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001157
1158 // make sure that we put all register classes into the RegClassList
1159 // before we call constructLiveRanges (now done in the constructor of
1160 // PhyRegAlloc class).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001161 //
1162 LRI.constructLiveRanges(); // create LR info
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001163
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001164 if (DEBUG_RA)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001165 LRI.printLiveRanges();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001166
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001167 createIGNodeListsAndIGs(); // create IGNode list and IGs
1168
1169 buildInterferenceGraphs(); // build IGs in all reg classes
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001170
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001171
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001172 if (DEBUG_RA) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001173 // print all LRs in all reg classes
1174 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1175 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001176
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001177 // print IGs in all register classes
1178 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1179 RegClassList[ rc ]->printIG();
1180 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001181
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001182
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001183 LRI.coalesceLRs(); // coalesce all live ranges
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001184
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +00001185
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001186 if( DEBUG_RA) {
1187 // print all LRs in all reg classes
1188 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1189 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001190
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001191 // print IGs in all register classes
1192 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1193 RegClassList[ rc ]->printIG();
1194 }
1195
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001196
1197 // mark un-usable suggested color before graph coloring algorithm.
1198 // When this is done, the graph coloring algo will not reserve
1199 // suggested color unnecessarily - they can be used by another LR
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001200 //
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001201 markUnusableSugColors();
1202
1203 // color all register classes using the graph coloring algo
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001204 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1205 RegClassList[ rc ]->colorAllRegs();
1206
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001207 // Atter grpah coloring, if some LRs did not receive a color (i.e, spilled)
1208 // a poistion for such spilled LRs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001209 //
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001210 allocateStackSpace4SpilledLRs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001211
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001212 mcInfo.popAllTempValues(TM); // TODO **Check
1213
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001214 // color incoming args - if the correct color was not received
1215 // insert code to copy to the correct register
1216 //
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001217 colorIncomingArgs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001218
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001219 // Now update the machine code with register names and add any
1220 // additional code inserted by the register allocator to the instruction
1221 // stream
1222 //
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001223 updateMachineCode();
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001224
Chris Lattner045e7c82001-09-19 16:26:23 +00001225 if (DEBUG_RA) {
Vikram S. Adve12af1642001-11-08 04:48:50 +00001226 MachineCodeForMethod::get(Meth).dump();
Chris Lattner045e7c82001-09-19 16:26:23 +00001227 printMachineCode(); // only for DEBUGGING
1228 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001229}
1230
Ruchira Sasankae727f852001-09-18 22:43:57 +00001231
1232