blob: 4ad98d917fd592f85cf17df0d1c67d4dc571ca02 [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 Lattner483e14e2002-04-27 07:27:19 +000017#include "llvm/Analysis/LiveVar/FunctionLiveVarInfo.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 {
Chris Lattnerf57b8452002-04-27 06:56:12 +000043 class RegisterAllocator : public FunctionPass {
Chris Lattner2f9b28e2002-02-04 15:54:09 +000044 TargetMachine &Target;
45 public:
46 inline RegisterAllocator(TargetMachine &T) : Target(T) {}
Chris Lattner6dd98a62002-02-04 00:33:08 +000047
Chris Lattnerf57b8452002-04-27 06:56:12 +000048 bool runOnFunction(Function *F) {
Chris Lattner2f9b28e2002-02-04 15:54:09 +000049 if (DEBUG_RA)
Chris Lattnerf57b8452002-04-27 06:56:12 +000050 cerr << "\n******************** Function "<< F->getName()
Chris Lattner2f9b28e2002-02-04 15:54:09 +000051 << " ********************\n";
52
Chris Lattner483e14e2002-04-27 07:27:19 +000053 PhyRegAlloc PRA(F, Target, &getAnalysis<FunctionLiveVarInfo>(),
Chris Lattner1b7f7dc2002-04-28 16:21:30 +000054 &getAnalysis<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
Chris Lattnerf57b8452002-04-27 06:56:12 +000061 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner1b7f7dc2002-04-28 16:21:30 +000062 AU.addRequired(LoopInfo::ID);
Chris Lattner483e14e2002-04-27 07:27:19 +000063 AU.addRequired(FunctionLiveVarInfo::ID);
Chris Lattner4911c352002-02-04 17:39:42 +000064 }
Chris Lattner2f9b28e2002-02-04 15:54:09 +000065 };
Chris Lattner6dd98a62002-02-04 00:33:08 +000066}
67
Chris Lattnerf57b8452002-04-27 06:56:12 +000068Pass *getRegisterAllocator(TargetMachine &T) {
Chris Lattner2f9b28e2002-02-04 15:54:09 +000069 return new RegisterAllocator(T);
70}
Chris Lattner6dd98a62002-02-04 00:33:08 +000071
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000072//----------------------------------------------------------------------------
73// Constructor: Init local composite objects and create register classes.
74//----------------------------------------------------------------------------
Chris Lattner1b7f7dc2002-04-28 16:21:30 +000075PhyRegAlloc::PhyRegAlloc(Function *F, const TargetMachine& tm,
76 FunctionLiveVarInfo *Lvi, LoopInfo *LDC)
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000077 : TM(tm), Meth(F),
78 mcInfo(MachineCodeForMethod::get(F)),
79 LVI(Lvi), LRI(F, tm, RegClassList),
80 MRI(tm.getRegInfo()),
Ruchira Sasanka8e604792001-09-14 21:18:34 +000081 NumOfRegClasses(MRI.getNumOfRegClasses()),
Chris Lattner4911c352002-02-04 17:39:42 +000082 LoopDepthCalc(LDC) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +000083
84 // create each RegisterClass and put in RegClassList
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000085 //
Chris Lattner697954c2002-01-20 22:54:45 +000086 for(unsigned int rc=0; rc < NumOfRegClasses; rc++)
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000087 RegClassList.push_back(new RegClass(F, MRI.getMachineRegClass(rc),
88 &ResColList));
Ruchira Sasanka8e604792001-09-14 21:18:34 +000089}
90
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000091
92//----------------------------------------------------------------------------
93// Destructor: Deletes register classes
94//----------------------------------------------------------------------------
95PhyRegAlloc::~PhyRegAlloc() {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000096 for( unsigned int rc=0; rc < NumOfRegClasses; rc++)
97 delete RegClassList[rc];
Chris Lattner0b0ffa02002-04-09 05:13:04 +000098
99 AddedInstrMap.clear();
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000100}
101
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000102//----------------------------------------------------------------------------
103// This method initally creates interference graphs (one in each reg class)
104// and IGNodeList (one in each IG). The actual nodes will be pushed later.
105//----------------------------------------------------------------------------
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000106void PhyRegAlloc::createIGNodeListsAndIGs() {
107 if (DEBUG_RA) cerr << "Creating LR lists ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000108
109 // hash map iterator
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000110 LiveRangeMapType::const_iterator HMI = LRI.getLiveRangeMap()->begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000111
112 // hash map end
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000113 LiveRangeMapType::const_iterator HMIEnd = LRI.getLiveRangeMap()->end();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000114
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000115 for (; HMI != HMIEnd ; ++HMI ) {
116 if (HMI->first) {
117 LiveRange *L = HMI->second; // get the LiveRange
118 if (!L) {
119 if( DEBUG_RA) {
Chris Lattner0665a5f2002-02-05 01:43:49 +0000120 cerr << "\n*?!?Warning: Null liver range found for: "
121 << RAV(HMI->first) << "\n";
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000122 }
123 continue;
124 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000125 // if the Value * is not null, and LR
126 // is not yet written to the IGNodeList
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000127 if( !(L->getUserIGNode()) ) {
128 RegClass *const RC = // RegClass of first value in the LR
129 RegClassList[ L->getRegClass()->getID() ];
130
131 RC->addLRToIG(L); // add this LR to an IG
132 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000133 }
134 }
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000135
136 // init RegClassList
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000137 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000138 RegClassList[rc]->createInterferenceGraph();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000139
140 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000141 cerr << "LRLists Created!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000142}
143
144
145
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000146
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000147//----------------------------------------------------------------------------
148// This method will add all interferences at for a given instruction.
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000149// Interence occurs only if the LR of Def (Inst or Arg) is of the same reg
150// class as that of live var. The live var passed to this function is the
151// LVset AFTER the instruction
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000152//----------------------------------------------------------------------------
Chris Lattner296b7732002-02-05 02:52:05 +0000153void PhyRegAlloc::addInterference(const Value *Def,
154 const ValueSet *LVSet,
155 bool isCallInst) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000156
Chris Lattner296b7732002-02-05 02:52:05 +0000157 ValueSet::const_iterator LIt = LVSet->begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000158
159 // get the live range of instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000160 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000161 const LiveRange *const LROfDef = LRI.getLiveRangeForValue( Def );
162
163 IGNode *const IGNodeOfDef = LROfDef->getUserIGNode();
164 assert( IGNodeOfDef );
165
166 RegClass *const RCOfDef = LROfDef->getRegClass();
167
168 // for each live var in live variable set
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000169 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000170 for( ; LIt != LVSet->end(); ++LIt) {
171
Chris Lattner0665a5f2002-02-05 01:43:49 +0000172 if (DEBUG_RA > 1)
173 cerr << "< Def=" << RAV(Def) << ", Lvar=" << RAV(*LIt) << "> ";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000174
175 // get the live range corresponding to live var
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000176 //
Chris Lattner0665a5f2002-02-05 01:43:49 +0000177 LiveRange *LROfVar = LRI.getLiveRangeForValue(*LIt);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000178
179 // LROfVar can be null if it is a const since a const
180 // doesn't have a dominating def - see Assumptions above
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000181 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000182 if (LROfVar) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000183 if(LROfDef == LROfVar) // do not set interf for same LR
184 continue;
185
186 // if 2 reg classes are the same set interference
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000187 //
Chris Lattner0665a5f2002-02-05 01:43:49 +0000188 if (RCOfDef == LROfVar->getRegClass()) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000189 RCOfDef->setInterference( LROfDef, LROfVar);
Chris Lattner0665a5f2002-02-05 01:43:49 +0000190 } else if (DEBUG_RA > 1) {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000191 // we will not have LRs for values not explicitly allocated in the
192 // instruction stream (e.g., constants)
Chris Lattner0665a5f2002-02-05 01:43:49 +0000193 cerr << " warning: no live range for " << RAV(*LIt) << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000194 }
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000195 }
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000196 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000197}
198
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000199
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000200
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000201//----------------------------------------------------------------------------
202// For a call instruction, this method sets the CallInterference flag in
203// the LR of each variable live int the Live Variable Set live after the
204// call instruction (except the return value of the call instruction - since
205// the return value does not interfere with that call itself).
206//----------------------------------------------------------------------------
207
208void PhyRegAlloc::setCallInterferences(const MachineInstr *MInst,
Chris Lattner296b7732002-02-05 02:52:05 +0000209 const ValueSet *LVSetAft) {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000210
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000211 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000212 cerr << "\n For call inst: " << *MInst;
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000213
Chris Lattner296b7732002-02-05 02:52:05 +0000214 ValueSet::const_iterator LIt = LVSetAft->begin();
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000215
216 // for each live var in live variable set after machine inst
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000217 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000218 for( ; LIt != LVSetAft->end(); ++LIt) {
219
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000220 // get the live range corresponding to live var
221 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000222 LiveRange *const LR = LRI.getLiveRangeForValue(*LIt );
223
224 if( LR && DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000225 cerr << "\n\tLR Aft Call: ";
Chris Lattner296b7732002-02-05 02:52:05 +0000226 printSet(*LR);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000227 }
228
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000229 // LR can be null if it is a const since a const
230 // doesn't have a dominating def - see Assumptions above
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000231 //
Vikram S. Adve1a53f032002-03-31 18:54:37 +0000232 if( LR ) {
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000233 LR->setCallInterference();
234 if( DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000235 cerr << "\n ++Added call interf for LR: " ;
Chris Lattner296b7732002-02-05 02:52:05 +0000236 printSet(*LR);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000237 }
238 }
239
240 }
241
Vikram S. Adve1a53f032002-03-31 18:54:37 +0000242 // Now find the LR of the return value of the call
243 // We do this because, we look at the LV set *after* the instruction
244 // to determine, which LRs must be saved across calls. The return value
245 // of the call is live in this set - but it does not interfere with call
246 // (i.e., we can allocate a volatile register to the return value)
247 //
248 if( const Value *RetVal = MRI.getCallInstRetVal( MInst )) {
249 LiveRange *RetValLR = LRI.getLiveRangeForValue( RetVal );
250 assert( RetValLR && "No LR for RetValue of call");
251 RetValLR->clearCallInterference();
252 }
253
254 // If the CALL is an indirect call, find the LR of the function pointer.
255 // That has a call interference because it conflicts with outgoing args.
256 if( const Value *AddrVal = MRI.getCallInstIndirectAddrVal( MInst )) {
257 LiveRange *AddrValLR = LRI.getLiveRangeForValue( AddrVal );
258 assert( AddrValLR && "No LR for indirect addr val of call");
259 AddrValLR->setCallInterference();
260 }
261
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000262}
263
264
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000265
266
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000267//----------------------------------------------------------------------------
268// This method will walk thru code and create interferences in the IG of
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000269// each RegClass. Also, this method calculates the spill cost of each
270// Live Range (it is done in this method to save another pass over the code).
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000271//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000272void PhyRegAlloc::buildInterferenceGraphs()
273{
274
Chris Lattner697954c2002-01-20 22:54:45 +0000275 if(DEBUG_RA) cerr << "Creating interference graphs ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000276
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000277 unsigned BBLoopDepthCost;
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000278 for (Function::const_iterator BBI = Meth->begin(), BBE = Meth->end();
279 BBI != BBE; ++BBI) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000280
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000281 // find the 10^(loop_depth) of this BB
282 //
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000283 BBLoopDepthCost = (unsigned) pow(10.0, LoopDepthCalc->getLoopDepth(*BBI));
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000284
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000285 // get the iterator for machine instructions
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000286 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000287 const MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
Vikram S. Adve48762092002-04-25 04:34:15 +0000288 MachineCodeForBasicBlock::const_iterator MII = MIVec.begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000289
290 // iterate over all the machine instructions in BB
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000291 //
Vikram S. Adve48762092002-04-25 04:34:15 +0000292 for( ; MII != MIVec.end(); ++MII) {
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000293
Vikram S. Adve48762092002-04-25 04:34:15 +0000294 const MachineInstr *MInst = *MII;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000295
296 // get the LV set after the instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000297 //
Chris Lattner748697d2002-02-05 04:20:12 +0000298 const ValueSet &LVSetAI = LVI->getLiveVarSetAfterMInst(MInst, *BBI);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000299
300 const bool isCallInst = TM.getInstrInfo().isCall(MInst->getOpCode());
301
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000302 if( isCallInst ) {
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000303 // set the isCallInterference flag of each live range wich extends
304 // accross this call instruction. This information is used by graph
305 // coloring algo to avoid allocating volatile colors to live ranges
306 // that span across calls (since they have to be saved/restored)
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000307 //
Chris Lattner748697d2002-02-05 04:20:12 +0000308 setCallInterferences(MInst, &LVSetAI);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000309 }
310
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000311
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000312 // iterate over all MI operands to find defs
313 //
Chris Lattner2f898d22002-02-05 06:02:59 +0000314 for (MachineInstr::const_val_op_iterator OpI = MInst->begin(),
315 OpE = MInst->end(); OpI != OpE; ++OpI) {
316 if (OpI.isDef()) // create a new LR iff this operand is a def
Chris Lattner748697d2002-02-05 04:20:12 +0000317 addInterference(*OpI, &LVSetAI, isCallInst);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000318
319 // Calculate the spill cost of each live range
320 //
Chris Lattner2f898d22002-02-05 06:02:59 +0000321 LiveRange *LR = LRI.getLiveRangeForValue(*OpI);
322 if (LR) LR->addSpillCost(BBLoopDepthCost);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000323 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000324
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000325
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000326 // if there are multiple defs in this instruction e.g. in SETX
327 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000328 if (TM.getInstrInfo().isPseudoInstr(MInst->getOpCode()))
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000329 addInterf4PseudoInstr(MInst);
330
331
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000332 // Also add interference for any implicit definitions in a machine
333 // instr (currently, only calls have this).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000334 //
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000335 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
336 if( NumOfImpRefs > 0 ) {
337 for(unsigned z=0; z < NumOfImpRefs; z++)
338 if( MInst->implicitRefIsDefined(z) )
Chris Lattner748697d2002-02-05 04:20:12 +0000339 addInterference( MInst->getImplicitRef(z), &LVSetAI, isCallInst );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000340 }
341
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +0000342
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000343 } // for all machine instructions in BB
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000344 } // for all BBs in function
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000345
346
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000347 // add interferences for function arguments. Since there are no explict
348 // defs in the function for args, we have to add them manually
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000349 //
350 addInterferencesForArgs();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000351
352 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000353 cerr << "Interference graphs calculted!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000354
355}
356
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000357
358
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000359//--------------------------------------------------------------------------
360// Pseudo instructions will be exapnded to multiple instructions by the
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000361// assembler. Consequently, all the opernds must get distinct registers.
362// Therefore, we mark all operands of a pseudo instruction as they interfere
363// with one another.
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000364//--------------------------------------------------------------------------
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000365void PhyRegAlloc::addInterf4PseudoInstr(const MachineInstr *MInst) {
366
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000367 bool setInterf = false;
368
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000369 // iterate over MI operands to find defs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000370 //
Chris Lattner2f898d22002-02-05 06:02:59 +0000371 for (MachineInstr::const_val_op_iterator It1 = MInst->begin(),
372 ItE = MInst->end(); It1 != ItE; ++It1) {
373 const LiveRange *LROfOp1 = LRI.getLiveRangeForValue(*It1);
374 assert((LROfOp1 || !It1.isDef()) && "No LR for Def in PSEUDO insruction");
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000375
Chris Lattner2f898d22002-02-05 06:02:59 +0000376 MachineInstr::const_val_op_iterator It2 = It1;
377 for(++It2; It2 != ItE; ++It2) {
378 const LiveRange *LROfOp2 = LRI.getLiveRangeForValue(*It2);
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000379
Chris Lattner2f898d22002-02-05 06:02:59 +0000380 if (LROfOp2) {
381 RegClass *RCOfOp1 = LROfOp1->getRegClass();
382 RegClass *RCOfOp2 = LROfOp2->getRegClass();
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000383
384 if( RCOfOp1 == RCOfOp2 ){
385 RCOfOp1->setInterference( LROfOp1, LROfOp2 );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000386 setInterf = true;
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000387 }
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000388 } // if Op2 has a LR
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000389 } // for all other defs in machine instr
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000390 } // for all operands in an instruction
391
Chris Lattner2f898d22002-02-05 06:02:59 +0000392 if (!setInterf && MInst->getNumOperands() > 2) {
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000393 cerr << "\nInterf not set for any operand in pseudo instr:\n";
394 cerr << *MInst;
395 assert(0 && "Interf not set for pseudo instr with > 2 operands" );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000396 }
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000397}
398
399
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000400
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000401//----------------------------------------------------------------------------
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000402// This method will add interferences for incoming arguments to a function.
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000403//----------------------------------------------------------------------------
Chris Lattner296b7732002-02-05 02:52:05 +0000404void PhyRegAlloc::addInterferencesForArgs() {
405 // get the InSet of root BB
Chris Lattner748697d2002-02-05 04:20:12 +0000406 const ValueSet &InSet = LVI->getInSetOfBB(Meth->front());
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000407
Chris Lattner296b7732002-02-05 02:52:05 +0000408 // get the argument list
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000409 const Function::ArgumentListType &ArgList = Meth->getArgumentList();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000410
Chris Lattner296b7732002-02-05 02:52:05 +0000411 // get an iterator to arg list
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000412 Function::ArgumentListType::const_iterator ArgIt = ArgList.begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000413
414
415 for( ; ArgIt != ArgList.end() ; ++ArgIt) { // for each argument
Chris Lattner748697d2002-02-05 04:20:12 +0000416 addInterference((Value*)*ArgIt, &InSet, false);// add interferences between
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000417 // args and LVars at start
Chris Lattner0665a5f2002-02-05 01:43:49 +0000418 if( DEBUG_RA > 1)
419 cerr << " - %% adding interference for argument "
420 << RAV((const Value *)*ArgIt) << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000421 }
422}
423
424
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000425//----------------------------------------------------------------------------
426// This method is called after register allocation is complete to set the
427// allocated reisters in the machine code. This code will add register numbers
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000428// to MachineOperands that contain a Value. Also it calls target specific
429// methods to produce caller saving instructions. At the end, it adds all
430// additional instructions produced by the register allocator to the
431// instruction stream.
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000432//----------------------------------------------------------------------------
Vikram S. Adve48762092002-04-25 04:34:15 +0000433
434//-----------------------------
435// Utility functions used below
436//-----------------------------
437inline void
438PrependInstructions(std::deque<MachineInstr *> &IBef,
439 MachineCodeForBasicBlock& MIVec,
440 MachineCodeForBasicBlock::iterator& MII,
441 const std::string& msg)
442{
443 if (!IBef.empty())
444 {
445 MachineInstr* OrigMI = *MII;
446 std::deque<MachineInstr *>::iterator AdIt;
447 for (AdIt = IBef.begin(); AdIt != IBef.end() ; ++AdIt)
448 {
449 if (DEBUG_RA) {
450 if (OrigMI) cerr << "For MInst: " << *OrigMI;
451 cerr << msg << " PREPENDed instr: " << **AdIt << "\n";
452 }
453 MII = MIVec.insert(MII, *AdIt);
454 ++MII;
455 }
456 }
457}
458
459inline void
460AppendInstructions(std::deque<MachineInstr *> &IAft,
461 MachineCodeForBasicBlock& MIVec,
462 MachineCodeForBasicBlock::iterator& MII,
463 const std::string& msg)
464{
465 if (!IAft.empty())
466 {
467 MachineInstr* OrigMI = *MII;
468 std::deque<MachineInstr *>::iterator AdIt;
469 for( AdIt = IAft.begin(); AdIt != IAft.end() ; ++AdIt )
470 {
471 if(DEBUG_RA) {
472 if (OrigMI) cerr << "For MInst: " << *OrigMI;
473 cerr << msg << " APPENDed instr: " << **AdIt << "\n";
474 }
475 ++MII; // insert before the next instruction
476 MII = MIVec.insert(MII, *AdIt);
477 }
478 }
479}
480
481
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000482void PhyRegAlloc::updateMachineCode()
483{
Vikram S. Adve48762092002-04-25 04:34:15 +0000484 const BasicBlock* entryBB = Meth->getEntryNode();
485 if (entryBB) {
486 MachineCodeForBasicBlock& MIVec = entryBB->getMachineInstrVec();
487 MachineCodeForBasicBlock::iterator MII = MIVec.begin();
488
489 // Insert any instructions needed at method entry
490 PrependInstructions(AddedInstrAtEntry.InstrnsBefore, MIVec, MII,
491 "At function entry: \n");
492 assert(AddedInstrAtEntry.InstrnsAfter.empty() &&
493 "InstrsAfter should be unnecessary since we are just inserting at "
494 "the function entry point here.");
495 }
496
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000497 for (Function::const_iterator BBI = Meth->begin(), BBE = Meth->end();
498 BBI != BBE; ++BBI) {
Vikram S. Adve48762092002-04-25 04:34:15 +0000499
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000500 // iterate over all the machine instructions in BB
Vikram S. Adve48762092002-04-25 04:34:15 +0000501 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
502 for(MachineCodeForBasicBlock::iterator MII = MIVec.begin();
503 MII != MIVec.end(); ++MII) {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000504
Vikram S. Adve48762092002-04-25 04:34:15 +0000505 MachineInstr *MInst = *MII;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000506
507 unsigned Opcode = MInst->getOpCode();
508
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000509 // do not process Phis
Vikram S. Adve23a4c8f2002-03-18 03:37:19 +0000510 if (TM.getInstrInfo().isDummyPhiInstr(Opcode))
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000511 continue;
512
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000513 // Now insert speical instructions (if necessary) for call/return
514 // instructions.
515 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000516 if (TM.getInstrInfo().isCall(Opcode) ||
517 TM.getInstrInfo().isReturn(Opcode)) {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000518
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000519 AddedInstrns &AI = AddedInstrMap[MInst];
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000520
521 // Tmp stack poistions are needed by some calls that have spilled args
522 // So reset it before we call each such method
Ruchira Sasanka6a3db8c2002-01-07 21:09:06 +0000523 //
524 mcInfo.popAllTempValues(TM);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000525
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000526 if (TM.getInstrInfo().isCall(Opcode))
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000527 MRI.colorCallArgs(MInst, LRI, &AI, *this, *BBI);
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000528 else if (TM.getInstrInfo().isReturn(Opcode))
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000529 MRI.colorRetValue(MInst, LRI, &AI);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000530 }
531
532
533 /* -- Using above code instead of this
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000534
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000535 // if this machine instr is call, insert caller saving code
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000536
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000537 if( (TM.getInstrInfo()).isCall( MInst->getOpCode()) )
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000538 MRI.insertCallerSavingCode(MInst, *BBI, *this );
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000539
540 */
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000541
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000542
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000543 // reset the stack offset for temporary variables since we may
544 // need that to spill
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000545 // mcInfo.popAllTempValues(TM);
Ruchira Sasankaf90870f2001-11-15 22:02:06 +0000546 // TODO ** : do later
Vikram S. Adve12af1642001-11-08 04:48:50 +0000547
Chris Lattner7a176752001-12-04 00:03:30 +0000548 //for(MachineInstr::val_const_op_iterator OpI(MInst);!OpI.done();++OpI) {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000549
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000550
551 // Now replace set the registers for operands in the machine instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000552 //
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000553 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
554
555 MachineOperand& Op = MInst->getOperand(OpNum);
556
557 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
558 Op.getOperandType() == MachineOperand::MO_CCRegister) {
559
560 const Value *const Val = Op.getVRegValue();
561
562 // delete this condition checking later (must assert if Val is null)
Chris Lattner045e7c82001-09-19 16:26:23 +0000563 if( !Val) {
564 if (DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000565 cerr << "Warning: NULL Value found for operand\n";
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000566 continue;
567 }
568 assert( Val && "Value is NULL");
569
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000570 LiveRange *const LR = LRI.getLiveRangeForValue(Val);
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000571
572 if ( !LR ) {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000573
574 // nothing to worry if it's a const or a label
575
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000576 if (DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000577 cerr << "*NO LR for operand : " << Op ;
578 cerr << " [reg:" << Op.getAllocatedRegNum() << "]";
579 cerr << " in inst:\t" << *MInst << "\n";
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000580 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000581
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000582 // if register is not allocated, mark register as invalid
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000583 if( Op.getAllocatedRegNum() == -1)
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000584 Op.setRegForValue( MRI.getInvalidRegNum());
Ruchira Sasankae727f852001-09-18 22:43:57 +0000585
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000586
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000587 continue;
588 }
589
590 unsigned RCID = (LR->getRegClass())->getID();
591
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000592 if( LR->hasColor() ) {
593 Op.setRegForValue( MRI.getUnifiedRegNum(RCID, LR->getColor()) );
594 }
595 else {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000596
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000597 // LR did NOT receive a color (register). Now, insert spill code
598 // for spilled opeands in this machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000599
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000600 //assert(0 && "LR must be spilled");
601 insertCode4SpilledLR(LR, MInst, *BBI, OpNum );
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000602
603 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000604 }
605
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000606 } // for each operand
607
608
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000609 // Now add instructions that the register allocator inserts before/after
610 // this machine instructions (done only for calls/rets/incoming args)
611 // We do this here, to ensure that spill for an instruction is inserted
612 // closest as possible to an instruction (see above insertCode4Spill...)
613 //
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000614 // If there are instructions to be added, *before* this machine
615 // instruction, add them now.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000616 //
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000617 if(AddedInstrMap.count(MInst)) {
Vikram S. Adve48762092002-04-25 04:34:15 +0000618 PrependInstructions(AddedInstrMap[MInst].InstrnsBefore, MIVec, MII,"");
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000619 }
Vikram S. Adve48762092002-04-25 04:34:15 +0000620
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000621 // If there are instructions to be added *after* 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[MInst].InstrnsAfter.empty()) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000625
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000626 // if there are delay slots for this instruction, the instructions
627 // added after it must really go after the delayed instruction(s)
628 // So, we move the InstrAfter of the current instruction to the
629 // corresponding delayed instruction
630
631 unsigned delay;
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000632 if ((delay=TM.getInstrInfo().getNumDelaySlots(MInst->getOpCode())) >0){
Vikram S. Adve48762092002-04-25 04:34:15 +0000633 move2DelayedInstr(MInst, *(MII+delay) );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000634
Chris Lattner697954c2002-01-20 22:54:45 +0000635 if(DEBUG_RA) cerr<< "\nMoved an added instr after the delay slot";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000636 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000637
638 else {
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000639 // Here we can add the "instructions after" to the current
640 // instruction since there are no delay slots for this instruction
Vikram S. Adve48762092002-04-25 04:34:15 +0000641 AppendInstructions(AddedInstrMap[MInst].InstrnsAfter, MIVec, MII,"");
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000642 } // if not delay
643
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000644 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000645
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000646 } // for each machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000647 }
648}
649
650
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000651
652//----------------------------------------------------------------------------
653// This method inserts spill code for AN operand whose LR was spilled.
654// This method may be called several times for a single machine instruction
655// if it contains many spilled operands. Each time it is called, it finds
656// a register which is not live at that instruction and also which is not
657// used by other spilled operands of the same instruction. Then it uses
658// this register temporarily to accomodate the spilled value.
659//----------------------------------------------------------------------------
660void PhyRegAlloc::insertCode4SpilledLR(const LiveRange *LR,
661 MachineInstr *MInst,
662 const BasicBlock *BB,
663 const unsigned OpNum) {
664
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000665 assert(! TM.getInstrInfo().isCall(MInst->getOpCode()) &&
666 (! TM.getInstrInfo().isReturn(MInst->getOpCode())) &&
667 "Arg of a call/ret must be handled elsewhere");
668
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000669 MachineOperand& Op = MInst->getOperand(OpNum);
670 bool isDef = MInst->operandIsDefined(OpNum);
671 unsigned RegType = MRI.getRegType( LR );
672 int SpillOff = LR->getSpillOffFromFP();
673 RegClass *RC = LR->getRegClass();
Chris Lattner748697d2002-02-05 04:20:12 +0000674 const ValueSet &LVSetBef = LVI->getLiveVarSetBeforeMInst(MInst, BB);
Vikram S. Adve00521d72001-11-12 23:26:35 +0000675
Chris Lattner697954c2002-01-20 22:54:45 +0000676 mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000677
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000678 MachineInstr *MIBef=NULL, *AdIMid=NULL, *MIAft=NULL;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000679
Chris Lattner748697d2002-02-05 04:20:12 +0000680 int TmpRegU = getUsableUniRegAtMI(RC, RegType, MInst,&LVSetBef, MIBef, MIAft);
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000681
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000682 // get the added instructions for this instruciton
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000683 AddedInstrns &AI = AddedInstrMap[MInst];
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000684
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000685 if (!isDef) {
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000686 // for a USE, we have to load the value of LR from stack to a TmpReg
687 // and use the TmpReg as one operand of instruction
688
689 // actual loading instruction
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000690 AdIMid = MRI.cpMem2RegMI(MRI.getFramePointer(), SpillOff, TmpRegU,RegType);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000691
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000692 if(MIBef)
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000693 AI.InstrnsBefore.push_back(MIBef);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000694
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000695 AI.InstrnsBefore.push_back(AdIMid);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000696
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000697 if(MIAft)
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000698 AI.InstrnsAfter.push_front(MIAft);
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000699
Chris Lattner296b7732002-02-05 02:52:05 +0000700 } else { // if this is a Def
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000701 // for a DEF, we have to store the value produced by this instruction
702 // on the stack position allocated for this LR
703
704 // actual storing instruction
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000705 AdIMid = MRI.cpReg2MemMI(TmpRegU, MRI.getFramePointer(), SpillOff,RegType);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000706
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000707 if (MIBef)
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000708 AI.InstrnsBefore.push_back(MIBef);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000709
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000710 AI.InstrnsAfter.push_front(AdIMid);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000711
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000712 if (MIAft)
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000713 AI.InstrnsAfter.push_front(MIAft);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000714
715 } // if !DEF
716
717 cerr << "\nFor Inst " << *MInst;
Chris Lattner296b7732002-02-05 02:52:05 +0000718 cerr << " - SPILLED LR: "; printSet(*LR);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000719 cerr << "\n - Added Instructions:";
Chris Lattner296b7732002-02-05 02:52:05 +0000720 if (MIBef) cerr << *MIBef;
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000721 cerr << *AdIMid;
Chris Lattner296b7732002-02-05 02:52:05 +0000722 if (MIAft) cerr << *MIAft;
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000723
Chris Lattner296b7732002-02-05 02:52:05 +0000724 Op.setRegForValue(TmpRegU); // set the opearnd
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000725}
726
727
728
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000729//----------------------------------------------------------------------------
730// We can use the following method to get a temporary register to be used
731// BEFORE any given machine instruction. If there is a register available,
732// this method will simply return that register and set MIBef = MIAft = NULL.
733// Otherwise, it will return a register and MIAft and MIBef will contain
734// two instructions used to free up this returned register.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000735// Returned register number is the UNIFIED register number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000736//----------------------------------------------------------------------------
737
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000738int PhyRegAlloc::getUsableUniRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000739 const int RegType,
740 const MachineInstr *MInst,
Chris Lattner296b7732002-02-05 02:52:05 +0000741 const ValueSet *LVSetBef,
Vikram S. Adve23a4c8f2002-03-18 03:37:19 +0000742 MachineInstr *&MIBef,
743 MachineInstr *&MIAft) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000744
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000745 int RegU = getUnusedUniRegAtMI(RC, MInst, LVSetBef);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000746
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000747
748 if( RegU != -1) {
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000749 // we found an unused register, so we can simply use it
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000750 MIBef = MIAft = NULL;
751 }
752 else {
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000753 // we couldn't find an unused register. Generate code to free up a reg by
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000754 // saving it on stack and restoring after the instruction
755
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000756 int TmpOff = mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
Vikram S. Adve12af1642001-11-08 04:48:50 +0000757
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000758 RegU = getUniRegNotUsedByThisInst(RC, MInst);
759 MIBef = MRI.cpReg2MemMI(RegU, MRI.getFramePointer(), TmpOff, RegType );
760 MIAft = MRI.cpMem2RegMI(MRI.getFramePointer(), TmpOff, RegU, RegType );
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000761 }
762
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000763 return RegU;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000764}
765
766//----------------------------------------------------------------------------
767// This method is called to get a new unused register that can be used to
768// accomodate a spilled value.
769// This method may be called several times for a single machine instruction
770// if it contains many spilled operands. Each time it is called, it finds
771// a register which is not live at that instruction and also which is not
772// used by other spilled operands of the same instruction.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000773// Return register number is relative to the register class. NOT
774// unified number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000775//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000776int PhyRegAlloc::getUnusedUniRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000777 const MachineInstr *MInst,
Chris Lattner296b7732002-02-05 02:52:05 +0000778 const ValueSet *LVSetBef) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000779
780 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
781
782 bool *IsColorUsedArr = RC->getIsColorUsedArr();
783
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000784 for(unsigned i=0; i < NumAvailRegs; i++) // Reset array
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000785 IsColorUsedArr[i] = false;
786
Chris Lattner296b7732002-02-05 02:52:05 +0000787 ValueSet::const_iterator LIt = LVSetBef->begin();
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000788
789 // for each live var in live variable set after machine inst
790 for( ; LIt != LVSetBef->end(); ++LIt) {
791
792 // get the live range corresponding to live var
793 LiveRange *const LRofLV = LRI.getLiveRangeForValue(*LIt );
794
795 // LR can be null if it is a const since a const
796 // doesn't have a dominating def - see Assumptions above
797 if( LRofLV )
798 if( LRofLV->hasColor() )
799 IsColorUsedArr[ LRofLV->getColor() ] = true;
800 }
801
802 // It is possible that one operand of this MInst was already spilled
803 // and it received some register temporarily. If that's the case,
804 // it is recorded in machine operand. We must skip such registers.
805
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000806 setRelRegsUsedByThisInst(RC, MInst);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000807
808 unsigned c; // find first unused color
809 for( c=0; c < NumAvailRegs; c++)
810 if( ! IsColorUsedArr[ c ] ) break;
811
812 if(c < NumAvailRegs)
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000813 return MRI.getUnifiedRegNum(RC->getID(), c);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000814 else
815 return -1;
816
817
818}
819
820
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000821//----------------------------------------------------------------------------
822// Get any other register in a register class, other than what is used
823// by operands of a machine instruction. Returns the unified reg number.
824//----------------------------------------------------------------------------
825int PhyRegAlloc::getUniRegNotUsedByThisInst(RegClass *RC,
826 const MachineInstr *MInst) {
827
828 bool *IsColorUsedArr = RC->getIsColorUsedArr();
829 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
830
831
832 for(unsigned i=0; i < NumAvailRegs ; i++) // Reset array
833 IsColorUsedArr[i] = false;
834
835 setRelRegsUsedByThisInst(RC, MInst);
836
837 unsigned c; // find first unused color
838 for( c=0; c < RC->getNumOfAvailRegs(); c++)
839 if( ! IsColorUsedArr[ c ] ) break;
840
841 if(c < NumAvailRegs)
842 return MRI.getUnifiedRegNum(RC->getID(), c);
843 else
844 assert( 0 && "FATAL: No free register could be found in reg class!!");
Chris Lattner697954c2002-01-20 22:54:45 +0000845 return 0;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000846}
847
848
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000849//----------------------------------------------------------------------------
850// This method modifies the IsColorUsedArr of the register class passed to it.
851// It sets the bits corresponding to the registers used by this machine
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000852// instructions. Both explicit and implicit operands are set.
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000853//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000854void PhyRegAlloc::setRelRegsUsedByThisInst(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000855 const MachineInstr *MInst ) {
856
857 bool *IsColorUsedArr = RC->getIsColorUsedArr();
858
859 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
860
861 const MachineOperand& Op = MInst->getOperand(OpNum);
862
863 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000864 Op.getOperandType() == MachineOperand::MO_CCRegister ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000865
866 const Value *const Val = Op.getVRegValue();
867
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000868 if( Val )
869 if( MRI.getRegClassIDOfValue(Val) == RC->getID() ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000870 int Reg;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000871 if( (Reg=Op.getAllocatedRegNum()) != -1) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000872 IsColorUsedArr[ Reg ] = true;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000873 }
874 else {
875 // it is possilbe that this operand still is not marked with
876 // a register but it has a LR and that received a color
877
878 LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
879 if( LROfVal)
880 if( LROfVal->hasColor() )
881 IsColorUsedArr[ LROfVal->getColor() ] = true;
882 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000883
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000884 } // if reg classes are the same
885 }
886 else if (Op.getOperandType() == MachineOperand::MO_MachineRegister) {
887 IsColorUsedArr[ Op.getMachineRegNum() ] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000888 }
889 }
890
891 // If there are implicit references, mark them as well
892
893 for(unsigned z=0; z < MInst->getNumImplicitRefs(); z++) {
894
895 LiveRange *const LRofImpRef =
896 LRI.getLiveRangeForValue( MInst->getImplicitRef(z) );
Chris Lattner697954c2002-01-20 22:54:45 +0000897
898 if(LRofImpRef && LRofImpRef->hasColor())
899 IsColorUsedArr[LRofImpRef->getColor()] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000900 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000901}
902
903
904
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000905
906
907
908
909
910//----------------------------------------------------------------------------
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000911// If there are delay slots for an instruction, the instructions
912// added after it must really go after the delayed instruction(s).
913// So, we move the InstrAfter of that instruction to the
914// corresponding delayed instruction using the following method.
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000915
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000916//----------------------------------------------------------------------------
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000917void PhyRegAlloc::move2DelayedInstr(const MachineInstr *OrigMI,
918 const MachineInstr *DelayedMI) {
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000919
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000920 // "added after" instructions of the original instr
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000921 std::deque<MachineInstr *> &OrigAft = AddedInstrMap[OrigMI].InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000922
923 // "added instructions" of the delayed instr
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000924 AddedInstrns &DelayAdI = AddedInstrMap[DelayedMI];
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000925
926 // "added after" instructions of the delayed instr
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000927 std::deque<MachineInstr *> &DelayedAft = DelayAdI.InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000928
929 // go thru all the "added after instructions" of the original instruction
930 // and append them to the "addded after instructions" of the delayed
931 // instructions
Chris Lattner697954c2002-01-20 22:54:45 +0000932 DelayedAft.insert(DelayedAft.end(), OrigAft.begin(), OrigAft.end());
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000933
934 // empty the "added after instructions" of the original instruction
935 OrigAft.clear();
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000936}
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000937
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000938//----------------------------------------------------------------------------
939// This method prints the code with registers after register allocation is
940// complete.
941//----------------------------------------------------------------------------
942void PhyRegAlloc::printMachineCode()
943{
944
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000945 cerr << "\n;************** Function " << Meth->getName()
Chris Lattner697954c2002-01-20 22:54:45 +0000946 << " *****************\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000947
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000948 for (Function::const_iterator BBI = Meth->begin(), BBE = Meth->end();
949 BBI != BBE; ++BBI) {
950 cerr << "\n"; printLabel(*BBI); cerr << ": ";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000951
952 // get the iterator for machine instructions
953 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
Vikram S. Adve48762092002-04-25 04:34:15 +0000954 MachineCodeForBasicBlock::iterator MII = MIVec.begin();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000955
956 // iterate over all the machine instructions in BB
Vikram S. Adve48762092002-04-25 04:34:15 +0000957 for( ; MII != MIVec.end(); ++MII) {
958 MachineInstr *const MInst = *MII;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000959
Chris Lattner697954c2002-01-20 22:54:45 +0000960 cerr << "\n\t";
961 cerr << TargetInstrDescriptors[MInst->getOpCode()].opCodeString;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000962
963 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000964 MachineOperand& Op = MInst->getOperand(OpNum);
965
966 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasanka97b8b442001-10-18 22:36:26 +0000967 Op.getOperandType() == MachineOperand::MO_CCRegister /*||
968 Op.getOperandType() == MachineOperand::MO_PCRelativeDisp*/ ) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000969
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000970 const Value *const Val = Op.getVRegValue () ;
Ruchira Sasankae727f852001-09-18 22:43:57 +0000971 // ****this code is temporary till NULL Values are fixed
972 if( ! Val ) {
Chris Lattner697954c2002-01-20 22:54:45 +0000973 cerr << "\t<*NULL*>";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000974 continue;
975 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000976
977 // if a label or a constant
Chris Lattnerdbe53042002-01-21 01:33:12 +0000978 if(isa<BasicBlock>(Val)) {
Chris Lattner697954c2002-01-20 22:54:45 +0000979 cerr << "\t"; printLabel( Op.getVRegValue () );
980 } else {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000981 // else it must be a register value
982 const int RegNum = Op.getAllocatedRegNum();
983
Chris Lattner697954c2002-01-20 22:54:45 +0000984 cerr << "\t" << "%" << MRI.getUnifiedRegName( RegNum );
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000985 if (Val->hasName() )
Chris Lattner697954c2002-01-20 22:54:45 +0000986 cerr << "(" << Val->getName() << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000987 else
Chris Lattner697954c2002-01-20 22:54:45 +0000988 cerr << "(" << Val << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000989
990 if( Op.opIsDef() )
Chris Lattner697954c2002-01-20 22:54:45 +0000991 cerr << "*";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000992
993 const LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
994 if( LROfVal )
995 if( LROfVal->hasSpillOffset() )
Chris Lattner697954c2002-01-20 22:54:45 +0000996 cerr << "$";
Ruchira Sasankae727f852001-09-18 22:43:57 +0000997 }
998
999 }
1000 else if(Op.getOperandType() == MachineOperand::MO_MachineRegister) {
Chris Lattner697954c2002-01-20 22:54:45 +00001001 cerr << "\t" << "%" << MRI.getUnifiedRegName(Op.getMachineRegNum());
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001002 }
1003
1004 else
Chris Lattner697954c2002-01-20 22:54:45 +00001005 cerr << "\t" << Op; // use dump field
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001006 }
1007
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001008
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001009
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001010 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
Chris Lattner0665a5f2002-02-05 01:43:49 +00001011 if( NumOfImpRefs > 0) {
Chris Lattner697954c2002-01-20 22:54:45 +00001012 cerr << "\tImplicit:";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001013
Chris Lattner0665a5f2002-02-05 01:43:49 +00001014 for(unsigned z=0; z < NumOfImpRefs; z++)
1015 cerr << RAV(MInst->getImplicitRef(z)) << "\t";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001016 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001017
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001018 } // for all machine instructions
1019
Chris Lattner697954c2002-01-20 22:54:45 +00001020 cerr << "\n";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001021
1022 } // for all BBs
1023
Chris Lattner697954c2002-01-20 22:54:45 +00001024 cerr << "\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001025}
1026
Ruchira Sasankae727f852001-09-18 22:43:57 +00001027
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001028#if 0
1029
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001030//----------------------------------------------------------------------------
1031//
1032//----------------------------------------------------------------------------
1033
1034void PhyRegAlloc::colorCallRetArgs()
1035{
1036
1037 CallRetInstrListType &CallRetInstList = LRI.getCallRetInstrList();
1038 CallRetInstrListType::const_iterator It = CallRetInstList.begin();
1039
1040 for( ; It != CallRetInstList.end(); ++It ) {
1041
Ruchira Sasankaa90e7702001-10-15 16:26:38 +00001042 const MachineInstr *const CRMI = *It;
1043 unsigned OpCode = CRMI->getOpCode();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001044
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001045 // get the added instructions for this Call/Ret instruciton
Chris Lattner0b0ffa02002-04-09 05:13:04 +00001046 AddedInstrns &AI = AddedInstrMap[CRMI];
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001047
Chris Lattner0b0ffa02002-04-09 05:13:04 +00001048 // Tmp stack positions are needed by some calls that have spilled args
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001049 // So reset it before we call each such method
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001050 //mcInfo.popAllTempValues(TM);
1051
Vikram S. Adve12af1642001-11-08 04:48:50 +00001052
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001053 if (TM.getInstrInfo().isCall(OpCode))
Chris Lattner0b0ffa02002-04-09 05:13:04 +00001054 MRI.colorCallArgs(CRMI, LRI, &AI, *this);
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001055 else if (TM.getInstrInfo().isReturn(OpCode))
Chris Lattner0b0ffa02002-04-09 05:13:04 +00001056 MRI.colorRetValue(CRMI, LRI, &AI);
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001057 else
1058 assert(0 && "Non Call/Ret instrn in CallRetInstrList\n");
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001059 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001060}
1061
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001062#endif
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001063
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001064//----------------------------------------------------------------------------
1065
1066//----------------------------------------------------------------------------
1067void PhyRegAlloc::colorIncomingArgs()
1068{
1069 const BasicBlock *const FirstBB = Meth->front();
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001070 const MachineInstr *FirstMI = FirstBB->getMachineInstrVec().front();
1071 assert(FirstMI && "No machine instruction in entry BB");
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001072
Vikram S. Adve48762092002-04-25 04:34:15 +00001073 MRI.colorMethodArgs(Meth, LRI, &AddedInstrAtEntry);
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001074}
1075
Ruchira Sasankae727f852001-09-18 22:43:57 +00001076
1077//----------------------------------------------------------------------------
1078// Used to generate a label for a basic block
1079//----------------------------------------------------------------------------
Chris Lattner697954c2002-01-20 22:54:45 +00001080void PhyRegAlloc::printLabel(const Value *const Val) {
1081 if (Val->hasName())
1082 cerr << Val->getName();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001083 else
Chris Lattner697954c2002-01-20 22:54:45 +00001084 cerr << "Label" << Val;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001085}
1086
1087
Ruchira Sasankae727f852001-09-18 22:43:57 +00001088//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001089// This method calls setSugColorUsable method of each live range. This
1090// will determine whether the suggested color of LR is really usable.
1091// A suggested color is not usable when the suggested color is volatile
1092// AND when there are call interferences
1093//----------------------------------------------------------------------------
1094
1095void PhyRegAlloc::markUnusableSugColors()
1096{
Chris Lattner697954c2002-01-20 22:54:45 +00001097 if(DEBUG_RA ) cerr << "\nmarking unusable suggested colors ...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001098
1099 // hash map iterator
1100 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
1101 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
1102
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001103 for(; HMI != HMIEnd ; ++HMI ) {
1104 if (HMI->first) {
1105 LiveRange *L = HMI->second; // get the LiveRange
1106 if (L) {
1107 if(L->hasSuggestedColor()) {
1108 int RCID = L->getRegClass()->getID();
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001109 if( MRI.isRegVolatile( RCID, L->getSuggestedColor()) &&
1110 L->isCallInterference() )
1111 L->setSuggestedColorUsable( false );
1112 else
1113 L->setSuggestedColorUsable( true );
1114 }
1115 } // if L->hasSuggestedColor()
1116 }
1117 } // for all LR's in hash map
1118}
1119
1120
1121
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001122//----------------------------------------------------------------------------
1123// The following method will set the stack offsets of the live ranges that
1124// are decided to be spillled. This must be called just after coloring the
1125// LRs using the graph coloring algo. For each live range that is spilled,
1126// this method allocate a new spill position on the stack.
1127//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001128
Chris Lattner37730942002-02-05 03:52:29 +00001129void PhyRegAlloc::allocateStackSpace4SpilledLRs() {
1130 if (DEBUG_RA) cerr << "\nsetting LR stack offsets ...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001131
Chris Lattner37730942002-02-05 03:52:29 +00001132 LiveRangeMapType::const_iterator HMI = LRI.getLiveRangeMap()->begin();
1133 LiveRangeMapType::const_iterator HMIEnd = LRI.getLiveRangeMap()->end();
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001134
Chris Lattner37730942002-02-05 03:52:29 +00001135 for( ; HMI != HMIEnd ; ++HMI) {
1136 if (HMI->first && HMI->second) {
1137 LiveRange *L = HMI->second; // get the LiveRange
1138 if (!L->hasColor()) // NOTE: ** allocating the size of long Type **
1139 L->setSpillOffFromFP(mcInfo.allocateSpilledValue(TM, Type::LongTy));
1140 }
1141 } // for all LR's in hash map
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001142}
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001143
1144
1145
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001146//----------------------------------------------------------------------------
Ruchira Sasankae727f852001-09-18 22:43:57 +00001147// The entry pont to Register Allocation
1148//----------------------------------------------------------------------------
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001149
1150void PhyRegAlloc::allocateRegisters()
1151{
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001152
1153 // make sure that we put all register classes into the RegClassList
1154 // before we call constructLiveRanges (now done in the constructor of
1155 // PhyRegAlloc class).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001156 //
1157 LRI.constructLiveRanges(); // create LR info
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001158
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001159 if (DEBUG_RA)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001160 LRI.printLiveRanges();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001161
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001162 createIGNodeListsAndIGs(); // create IGNode list and IGs
1163
1164 buildInterferenceGraphs(); // build IGs in all reg classes
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001165
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001166
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001167 if (DEBUG_RA) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001168 // print all LRs in all reg classes
1169 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1170 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001171
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001172 // print IGs in all register classes
1173 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1174 RegClassList[ rc ]->printIG();
1175 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001176
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001177
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001178 LRI.coalesceLRs(); // coalesce all live ranges
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001179
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +00001180
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001181 if( DEBUG_RA) {
1182 // print all LRs in all reg classes
1183 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1184 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001185
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001186 // print IGs in all register classes
1187 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1188 RegClassList[ rc ]->printIG();
1189 }
1190
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001191
1192 // mark un-usable suggested color before graph coloring algorithm.
1193 // When this is done, the graph coloring algo will not reserve
1194 // suggested color unnecessarily - they can be used by another LR
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001195 //
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001196 markUnusableSugColors();
1197
1198 // color all register classes using the graph coloring algo
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001199 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1200 RegClassList[ rc ]->colorAllRegs();
1201
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001202 // Atter grpah coloring, if some LRs did not receive a color (i.e, spilled)
1203 // a poistion for such spilled LRs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001204 //
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001205 allocateStackSpace4SpilledLRs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001206
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001207 mcInfo.popAllTempValues(TM); // TODO **Check
1208
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001209 // color incoming args - if the correct color was not received
1210 // insert code to copy to the correct register
1211 //
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001212 colorIncomingArgs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001213
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001214 // Now update the machine code with register names and add any
1215 // additional code inserted by the register allocator to the instruction
1216 // stream
1217 //
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001218 updateMachineCode();
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001219
Chris Lattner045e7c82001-09-19 16:26:23 +00001220 if (DEBUG_RA) {
Vikram S. Adve12af1642001-11-08 04:48:50 +00001221 MachineCodeForMethod::get(Meth).dump();
Chris Lattner045e7c82001-09-19 16:26:23 +00001222 printMachineCode(); // only for DEBUGGING
1223 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001224}
1225
Ruchira Sasankae727f852001-09-18 22:43:57 +00001226
1227