blob: d54229e838fdaf7ee7149acef00a8282f758a6f6 [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 Lattnerc6f3ae52002-04-29 17:42:12 +000024#include "llvm/CodeGen/RegAllocCommon.h"
Chris Lattner697954c2002-01-20 22:54:45 +000025#include <iostream>
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000026#include <math.h>
Chris Lattner697954c2002-01-20 22:54:45 +000027using std::cerr;
Vikram S. Adve12af1642001-11-08 04:48:50 +000028
29
30// ***TODO: There are several places we add instructions. Validate the order
31// of adding these instructions.
Ruchira Sasanka174bded2001-10-28 18:12:02 +000032
Chris Lattner045e7c82001-09-19 16:26:23 +000033cl::Enum<RegAllocDebugLevel_t> DEBUG_RA("dregalloc", cl::NoFlags,
34 "enable register allocation debugging information",
35 clEnumValN(RA_DEBUG_None , "n", "disable debug output"),
36 clEnumValN(RA_DEBUG_Normal , "y", "enable debug output"),
37 clEnumValN(RA_DEBUG_Verbose, "v", "enable extra debug output"), 0);
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000038
39
Chris Lattner2f9b28e2002-02-04 15:54:09 +000040//----------------------------------------------------------------------------
41// RegisterAllocation pass front end...
42//----------------------------------------------------------------------------
43namespace {
Chris Lattnerf57b8452002-04-27 06:56:12 +000044 class RegisterAllocator : public FunctionPass {
Chris Lattner2f9b28e2002-02-04 15:54:09 +000045 TargetMachine &Target;
46 public:
47 inline RegisterAllocator(TargetMachine &T) : Target(T) {}
Chris Lattner96c466b2002-04-29 14:57:45 +000048
49 const char *getPassName() const { return "Register Allocation"; }
Chris Lattner6dd98a62002-02-04 00:33:08 +000050
Chris Lattnerf57b8452002-04-27 06:56:12 +000051 bool runOnFunction(Function *F) {
Chris Lattner2f9b28e2002-02-04 15:54:09 +000052 if (DEBUG_RA)
Chris Lattnerf57b8452002-04-27 06:56:12 +000053 cerr << "\n******************** Function "<< F->getName()
Chris Lattner2f9b28e2002-02-04 15:54:09 +000054 << " ********************\n";
55
Chris Lattner483e14e2002-04-27 07:27:19 +000056 PhyRegAlloc PRA(F, Target, &getAnalysis<FunctionLiveVarInfo>(),
Chris Lattner1b7f7dc2002-04-28 16:21:30 +000057 &getAnalysis<LoopInfo>());
Chris Lattner2f9b28e2002-02-04 15:54:09 +000058 PRA.allocateRegisters();
59
60 if (DEBUG_RA) cerr << "\nRegister allocation complete!\n";
61 return false;
62 }
Chris Lattner4911c352002-02-04 17:39:42 +000063
Chris Lattnerf57b8452002-04-27 06:56:12 +000064 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner1b7f7dc2002-04-28 16:21:30 +000065 AU.addRequired(LoopInfo::ID);
Chris Lattner483e14e2002-04-27 07:27:19 +000066 AU.addRequired(FunctionLiveVarInfo::ID);
Chris Lattner4911c352002-02-04 17:39:42 +000067 }
Chris Lattner2f9b28e2002-02-04 15:54:09 +000068 };
Chris Lattner6dd98a62002-02-04 00:33:08 +000069}
70
Chris Lattnerf57b8452002-04-27 06:56:12 +000071Pass *getRegisterAllocator(TargetMachine &T) {
Chris Lattner2f9b28e2002-02-04 15:54:09 +000072 return new RegisterAllocator(T);
73}
Chris Lattner6dd98a62002-02-04 00:33:08 +000074
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000075//----------------------------------------------------------------------------
76// Constructor: Init local composite objects and create register classes.
77//----------------------------------------------------------------------------
Chris Lattner1b7f7dc2002-04-28 16:21:30 +000078PhyRegAlloc::PhyRegAlloc(Function *F, const TargetMachine& tm,
79 FunctionLiveVarInfo *Lvi, LoopInfo *LDC)
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000080 : TM(tm), Meth(F),
81 mcInfo(MachineCodeForMethod::get(F)),
82 LVI(Lvi), LRI(F, tm, RegClassList),
83 MRI(tm.getRegInfo()),
Ruchira Sasanka8e604792001-09-14 21:18:34 +000084 NumOfRegClasses(MRI.getNumOfRegClasses()),
Chris Lattner4911c352002-02-04 17:39:42 +000085 LoopDepthCalc(LDC) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +000086
87 // create each RegisterClass and put in RegClassList
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000088 //
Chris Lattner697954c2002-01-20 22:54:45 +000089 for(unsigned int rc=0; rc < NumOfRegClasses; rc++)
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000090 RegClassList.push_back(new RegClass(F, MRI.getMachineRegClass(rc),
91 &ResColList));
Ruchira Sasanka8e604792001-09-14 21:18:34 +000092}
93
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000094
95//----------------------------------------------------------------------------
96// Destructor: Deletes register classes
97//----------------------------------------------------------------------------
98PhyRegAlloc::~PhyRegAlloc() {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000099 for( unsigned int rc=0; rc < NumOfRegClasses; rc++)
100 delete RegClassList[rc];
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000101
102 AddedInstrMap.clear();
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000103}
104
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000105//----------------------------------------------------------------------------
106// This method initally creates interference graphs (one in each reg class)
107// and IGNodeList (one in each IG). The actual nodes will be pushed later.
108//----------------------------------------------------------------------------
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000109void PhyRegAlloc::createIGNodeListsAndIGs() {
110 if (DEBUG_RA) cerr << "Creating LR lists ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000111
112 // hash map iterator
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000113 LiveRangeMapType::const_iterator HMI = LRI.getLiveRangeMap()->begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000114
115 // hash map end
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000116 LiveRangeMapType::const_iterator HMIEnd = LRI.getLiveRangeMap()->end();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000117
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000118 for (; HMI != HMIEnd ; ++HMI ) {
119 if (HMI->first) {
120 LiveRange *L = HMI->second; // get the LiveRange
121 if (!L) {
122 if( DEBUG_RA) {
Chris Lattner0665a5f2002-02-05 01:43:49 +0000123 cerr << "\n*?!?Warning: Null liver range found for: "
124 << RAV(HMI->first) << "\n";
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000125 }
126 continue;
127 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000128 // if the Value * is not null, and LR
129 // is not yet written to the IGNodeList
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000130 if( !(L->getUserIGNode()) ) {
131 RegClass *const RC = // RegClass of first value in the LR
132 RegClassList[ L->getRegClass()->getID() ];
133
134 RC->addLRToIG(L); // add this LR to an IG
135 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000136 }
137 }
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000138
139 // init RegClassList
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000140 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000141 RegClassList[rc]->createInterferenceGraph();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000142
143 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000144 cerr << "LRLists Created!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000145}
146
147
148
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000149
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000150//----------------------------------------------------------------------------
151// This method will add all interferences at for a given instruction.
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000152// Interence occurs only if the LR of Def (Inst or Arg) is of the same reg
153// class as that of live var. The live var passed to this function is the
154// LVset AFTER the instruction
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000155//----------------------------------------------------------------------------
Chris Lattner296b7732002-02-05 02:52:05 +0000156void PhyRegAlloc::addInterference(const Value *Def,
157 const ValueSet *LVSet,
158 bool isCallInst) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000159
Chris Lattner296b7732002-02-05 02:52:05 +0000160 ValueSet::const_iterator LIt = LVSet->begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000161
162 // get the live range of instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000163 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000164 const LiveRange *const LROfDef = LRI.getLiveRangeForValue( Def );
165
166 IGNode *const IGNodeOfDef = LROfDef->getUserIGNode();
167 assert( IGNodeOfDef );
168
169 RegClass *const RCOfDef = LROfDef->getRegClass();
170
171 // for each live var in live variable set
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000172 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000173 for( ; LIt != LVSet->end(); ++LIt) {
174
Chris Lattner0665a5f2002-02-05 01:43:49 +0000175 if (DEBUG_RA > 1)
176 cerr << "< Def=" << RAV(Def) << ", Lvar=" << RAV(*LIt) << "> ";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000177
178 // get the live range corresponding to live var
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000179 //
Chris Lattner0665a5f2002-02-05 01:43:49 +0000180 LiveRange *LROfVar = LRI.getLiveRangeForValue(*LIt);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000181
182 // LROfVar can be null if it is a const since a const
183 // doesn't have a dominating def - see Assumptions above
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000184 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000185 if (LROfVar) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000186 if(LROfDef == LROfVar) // do not set interf for same LR
187 continue;
188
189 // if 2 reg classes are the same set interference
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000190 //
Chris Lattner0665a5f2002-02-05 01:43:49 +0000191 if (RCOfDef == LROfVar->getRegClass()) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000192 RCOfDef->setInterference( LROfDef, LROfVar);
Chris Lattner0665a5f2002-02-05 01:43:49 +0000193 } else if (DEBUG_RA > 1) {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000194 // we will not have LRs for values not explicitly allocated in the
195 // instruction stream (e.g., constants)
Chris Lattner0665a5f2002-02-05 01:43:49 +0000196 cerr << " warning: no live range for " << RAV(*LIt) << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000197 }
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000198 }
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000199 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000200}
201
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000202
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000203
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000204//----------------------------------------------------------------------------
205// For a call instruction, this method sets the CallInterference flag in
206// the LR of each variable live int the Live Variable Set live after the
207// call instruction (except the return value of the call instruction - since
208// the return value does not interfere with that call itself).
209//----------------------------------------------------------------------------
210
211void PhyRegAlloc::setCallInterferences(const MachineInstr *MInst,
Chris Lattner296b7732002-02-05 02:52:05 +0000212 const ValueSet *LVSetAft) {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000213
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000214 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000215 cerr << "\n For call inst: " << *MInst;
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000216
Chris Lattner296b7732002-02-05 02:52:05 +0000217 ValueSet::const_iterator LIt = LVSetAft->begin();
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000218
219 // for each live var in live variable set after machine inst
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000220 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000221 for( ; LIt != LVSetAft->end(); ++LIt) {
222
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000223 // get the live range corresponding to live var
224 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000225 LiveRange *const LR = LRI.getLiveRangeForValue(*LIt );
226
227 if( LR && DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000228 cerr << "\n\tLR Aft Call: ";
Chris Lattner296b7732002-02-05 02:52:05 +0000229 printSet(*LR);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000230 }
231
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000232 // LR can be null if it is a const since a const
233 // doesn't have a dominating def - see Assumptions above
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000234 //
Vikram S. Adve1a53f032002-03-31 18:54:37 +0000235 if( LR ) {
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000236 LR->setCallInterference();
237 if( DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000238 cerr << "\n ++Added call interf for LR: " ;
Chris Lattner296b7732002-02-05 02:52:05 +0000239 printSet(*LR);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000240 }
241 }
242
243 }
244
Vikram S. Adve1a53f032002-03-31 18:54:37 +0000245 // Now find the LR of the return value of the call
246 // We do this because, we look at the LV set *after* the instruction
247 // to determine, which LRs must be saved across calls. The return value
248 // of the call is live in this set - but it does not interfere with call
249 // (i.e., we can allocate a volatile register to the return value)
250 //
251 if( const Value *RetVal = MRI.getCallInstRetVal( MInst )) {
252 LiveRange *RetValLR = LRI.getLiveRangeForValue( RetVal );
253 assert( RetValLR && "No LR for RetValue of call");
254 RetValLR->clearCallInterference();
255 }
256
257 // If the CALL is an indirect call, find the LR of the function pointer.
258 // That has a call interference because it conflicts with outgoing args.
259 if( const Value *AddrVal = MRI.getCallInstIndirectAddrVal( MInst )) {
260 LiveRange *AddrValLR = LRI.getLiveRangeForValue( AddrVal );
261 assert( AddrValLR && "No LR for indirect addr val of call");
262 AddrValLR->setCallInterference();
263 }
264
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000265}
266
267
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000268
269
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000270//----------------------------------------------------------------------------
271// This method will walk thru code and create interferences in the IG of
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000272// each RegClass. Also, this method calculates the spill cost of each
273// Live Range (it is done in this method to save another pass over the code).
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000274//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000275void PhyRegAlloc::buildInterferenceGraphs()
276{
277
Chris Lattner697954c2002-01-20 22:54:45 +0000278 if(DEBUG_RA) cerr << "Creating interference graphs ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000279
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000280 unsigned BBLoopDepthCost;
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000281 for (Function::const_iterator BBI = Meth->begin(), BBE = Meth->end();
282 BBI != BBE; ++BBI) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000283
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000284 // find the 10^(loop_depth) of this BB
285 //
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000286 BBLoopDepthCost = (unsigned) pow(10.0, LoopDepthCalc->getLoopDepth(*BBI));
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000287
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000288 // get the iterator for machine instructions
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000289 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000290 const MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
Vikram S. Adve48762092002-04-25 04:34:15 +0000291 MachineCodeForBasicBlock::const_iterator MII = MIVec.begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000292
293 // iterate over all the machine instructions in BB
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000294 //
Vikram S. Adve48762092002-04-25 04:34:15 +0000295 for( ; MII != MIVec.end(); ++MII) {
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000296
Vikram S. Adve48762092002-04-25 04:34:15 +0000297 const MachineInstr *MInst = *MII;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000298
299 // get the LV set after the instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000300 //
Chris Lattner748697d2002-02-05 04:20:12 +0000301 const ValueSet &LVSetAI = LVI->getLiveVarSetAfterMInst(MInst, *BBI);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000302
303 const bool isCallInst = TM.getInstrInfo().isCall(MInst->getOpCode());
304
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000305 if( isCallInst ) {
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000306 // set the isCallInterference flag of each live range wich extends
307 // accross this call instruction. This information is used by graph
308 // coloring algo to avoid allocating volatile colors to live ranges
309 // that span across calls (since they have to be saved/restored)
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000310 //
Chris Lattner748697d2002-02-05 04:20:12 +0000311 setCallInterferences(MInst, &LVSetAI);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000312 }
313
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000314
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000315 // iterate over all MI operands to find defs
316 //
Chris Lattner2f898d22002-02-05 06:02:59 +0000317 for (MachineInstr::const_val_op_iterator OpI = MInst->begin(),
318 OpE = MInst->end(); OpI != OpE; ++OpI) {
319 if (OpI.isDef()) // create a new LR iff this operand is a def
Chris Lattner748697d2002-02-05 04:20:12 +0000320 addInterference(*OpI, &LVSetAI, isCallInst);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000321
322 // Calculate the spill cost of each live range
323 //
Chris Lattner2f898d22002-02-05 06:02:59 +0000324 LiveRange *LR = LRI.getLiveRangeForValue(*OpI);
325 if (LR) LR->addSpillCost(BBLoopDepthCost);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000326 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000327
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000328
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000329 // if there are multiple defs in this instruction e.g. in SETX
330 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000331 if (TM.getInstrInfo().isPseudoInstr(MInst->getOpCode()))
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000332 addInterf4PseudoInstr(MInst);
333
334
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000335 // Also add interference for any implicit definitions in a machine
336 // instr (currently, only calls have this).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000337 //
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000338 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
339 if( NumOfImpRefs > 0 ) {
340 for(unsigned z=0; z < NumOfImpRefs; z++)
341 if( MInst->implicitRefIsDefined(z) )
Chris Lattner748697d2002-02-05 04:20:12 +0000342 addInterference( MInst->getImplicitRef(z), &LVSetAI, isCallInst );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000343 }
344
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +0000345
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000346 } // for all machine instructions in BB
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000347 } // for all BBs in function
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000348
349
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000350 // add interferences for function arguments. Since there are no explict
351 // defs in the function for args, we have to add them manually
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000352 //
353 addInterferencesForArgs();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000354
355 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000356 cerr << "Interference graphs calculted!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000357
358}
359
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000360
361
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000362//--------------------------------------------------------------------------
363// Pseudo instructions will be exapnded to multiple instructions by the
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000364// assembler. Consequently, all the opernds must get distinct registers.
365// Therefore, we mark all operands of a pseudo instruction as they interfere
366// with one another.
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000367//--------------------------------------------------------------------------
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000368void PhyRegAlloc::addInterf4PseudoInstr(const MachineInstr *MInst) {
369
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000370 bool setInterf = false;
371
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000372 // iterate over MI operands to find defs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000373 //
Chris Lattner2f898d22002-02-05 06:02:59 +0000374 for (MachineInstr::const_val_op_iterator It1 = MInst->begin(),
375 ItE = MInst->end(); It1 != ItE; ++It1) {
376 const LiveRange *LROfOp1 = LRI.getLiveRangeForValue(*It1);
377 assert((LROfOp1 || !It1.isDef()) && "No LR for Def in PSEUDO insruction");
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000378
Chris Lattner2f898d22002-02-05 06:02:59 +0000379 MachineInstr::const_val_op_iterator It2 = It1;
380 for(++It2; It2 != ItE; ++It2) {
381 const LiveRange *LROfOp2 = LRI.getLiveRangeForValue(*It2);
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000382
Chris Lattner2f898d22002-02-05 06:02:59 +0000383 if (LROfOp2) {
384 RegClass *RCOfOp1 = LROfOp1->getRegClass();
385 RegClass *RCOfOp2 = LROfOp2->getRegClass();
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000386
387 if( RCOfOp1 == RCOfOp2 ){
388 RCOfOp1->setInterference( LROfOp1, LROfOp2 );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000389 setInterf = true;
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000390 }
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000391 } // if Op2 has a LR
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000392 } // for all other defs in machine instr
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000393 } // for all operands in an instruction
394
Chris Lattner2f898d22002-02-05 06:02:59 +0000395 if (!setInterf && MInst->getNumOperands() > 2) {
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000396 cerr << "\nInterf not set for any operand in pseudo instr:\n";
397 cerr << *MInst;
398 assert(0 && "Interf not set for pseudo instr with > 2 operands" );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000399 }
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000400}
401
402
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000403
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000404//----------------------------------------------------------------------------
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000405// This method will add interferences for incoming arguments to a function.
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000406//----------------------------------------------------------------------------
Chris Lattner296b7732002-02-05 02:52:05 +0000407void PhyRegAlloc::addInterferencesForArgs() {
408 // get the InSet of root BB
Chris Lattner748697d2002-02-05 04:20:12 +0000409 const ValueSet &InSet = LVI->getInSetOfBB(Meth->front());
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000410
Chris Lattner296b7732002-02-05 02:52:05 +0000411 // get the argument list
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000412 const Function::ArgumentListType &ArgList = Meth->getArgumentList();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000413
Chris Lattner296b7732002-02-05 02:52:05 +0000414 // get an iterator to arg list
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000415 Function::ArgumentListType::const_iterator ArgIt = ArgList.begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000416
417
418 for( ; ArgIt != ArgList.end() ; ++ArgIt) { // for each argument
Chris Lattner748697d2002-02-05 04:20:12 +0000419 addInterference((Value*)*ArgIt, &InSet, false);// add interferences between
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000420 // args and LVars at start
Chris Lattner0665a5f2002-02-05 01:43:49 +0000421 if( DEBUG_RA > 1)
422 cerr << " - %% adding interference for argument "
423 << RAV((const Value *)*ArgIt) << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000424 }
425}
426
427
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000428//----------------------------------------------------------------------------
429// This method is called after register allocation is complete to set the
430// allocated reisters in the machine code. This code will add register numbers
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000431// to MachineOperands that contain a Value. Also it calls target specific
432// methods to produce caller saving instructions. At the end, it adds all
433// additional instructions produced by the register allocator to the
434// instruction stream.
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000435//----------------------------------------------------------------------------
Vikram S. Adve48762092002-04-25 04:34:15 +0000436
437//-----------------------------
438// Utility functions used below
439//-----------------------------
440inline void
441PrependInstructions(std::deque<MachineInstr *> &IBef,
442 MachineCodeForBasicBlock& MIVec,
443 MachineCodeForBasicBlock::iterator& MII,
444 const std::string& msg)
445{
446 if (!IBef.empty())
447 {
448 MachineInstr* OrigMI = *MII;
449 std::deque<MachineInstr *>::iterator AdIt;
450 for (AdIt = IBef.begin(); AdIt != IBef.end() ; ++AdIt)
451 {
452 if (DEBUG_RA) {
453 if (OrigMI) cerr << "For MInst: " << *OrigMI;
454 cerr << msg << " PREPENDed instr: " << **AdIt << "\n";
455 }
456 MII = MIVec.insert(MII, *AdIt);
457 ++MII;
458 }
459 }
460}
461
462inline void
463AppendInstructions(std::deque<MachineInstr *> &IAft,
464 MachineCodeForBasicBlock& MIVec,
465 MachineCodeForBasicBlock::iterator& MII,
466 const std::string& msg)
467{
468 if (!IAft.empty())
469 {
470 MachineInstr* OrigMI = *MII;
471 std::deque<MachineInstr *>::iterator AdIt;
472 for( AdIt = IAft.begin(); AdIt != IAft.end() ; ++AdIt )
473 {
474 if(DEBUG_RA) {
475 if (OrigMI) cerr << "For MInst: " << *OrigMI;
476 cerr << msg << " APPENDed instr: " << **AdIt << "\n";
477 }
478 ++MII; // insert before the next instruction
479 MII = MIVec.insert(MII, *AdIt);
480 }
481 }
482}
483
484
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000485void PhyRegAlloc::updateMachineCode()
486{
Vikram S. Adve48762092002-04-25 04:34:15 +0000487 const BasicBlock* entryBB = Meth->getEntryNode();
488 if (entryBB) {
489 MachineCodeForBasicBlock& MIVec = entryBB->getMachineInstrVec();
490 MachineCodeForBasicBlock::iterator MII = MIVec.begin();
491
492 // Insert any instructions needed at method entry
493 PrependInstructions(AddedInstrAtEntry.InstrnsBefore, MIVec, MII,
494 "At function entry: \n");
495 assert(AddedInstrAtEntry.InstrnsAfter.empty() &&
496 "InstrsAfter should be unnecessary since we are just inserting at "
497 "the function entry point here.");
498 }
499
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000500 for (Function::const_iterator BBI = Meth->begin(), BBE = Meth->end();
501 BBI != BBE; ++BBI) {
Vikram S. Adve48762092002-04-25 04:34:15 +0000502
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000503 // iterate over all the machine instructions in BB
Vikram S. Adve48762092002-04-25 04:34:15 +0000504 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
505 for(MachineCodeForBasicBlock::iterator MII = MIVec.begin();
506 MII != MIVec.end(); ++MII) {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000507
Vikram S. Adve48762092002-04-25 04:34:15 +0000508 MachineInstr *MInst = *MII;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000509
510 unsigned Opcode = MInst->getOpCode();
511
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000512 // do not process Phis
Vikram S. Adve23a4c8f2002-03-18 03:37:19 +0000513 if (TM.getInstrInfo().isDummyPhiInstr(Opcode))
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000514 continue;
515
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000516 // Now insert speical instructions (if necessary) for call/return
517 // instructions.
518 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000519 if (TM.getInstrInfo().isCall(Opcode) ||
520 TM.getInstrInfo().isReturn(Opcode)) {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000521
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000522 AddedInstrns &AI = AddedInstrMap[MInst];
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000523
524 // Tmp stack poistions are needed by some calls that have spilled args
525 // So reset it before we call each such method
Ruchira Sasanka6a3db8c2002-01-07 21:09:06 +0000526 //
527 mcInfo.popAllTempValues(TM);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000528
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000529 if (TM.getInstrInfo().isCall(Opcode))
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000530 MRI.colorCallArgs(MInst, LRI, &AI, *this, *BBI);
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000531 else if (TM.getInstrInfo().isReturn(Opcode))
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000532 MRI.colorRetValue(MInst, LRI, &AI);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000533 }
534
535
536 /* -- Using above code instead of this
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000537
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000538 // if this machine instr is call, insert caller saving code
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000539
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000540 if( (TM.getInstrInfo()).isCall( MInst->getOpCode()) )
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000541 MRI.insertCallerSavingCode(MInst, *BBI, *this );
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000542
543 */
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000544
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000545
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000546 // reset the stack offset for temporary variables since we may
547 // need that to spill
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000548 // mcInfo.popAllTempValues(TM);
Ruchira Sasankaf90870f2001-11-15 22:02:06 +0000549 // TODO ** : do later
Vikram S. Adve12af1642001-11-08 04:48:50 +0000550
Chris Lattner7a176752001-12-04 00:03:30 +0000551 //for(MachineInstr::val_const_op_iterator OpI(MInst);!OpI.done();++OpI) {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000552
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000553
554 // Now replace set the registers for operands in the machine instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000555 //
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000556 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
557
558 MachineOperand& Op = MInst->getOperand(OpNum);
559
560 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
561 Op.getOperandType() == MachineOperand::MO_CCRegister) {
562
563 const Value *const Val = Op.getVRegValue();
564
565 // delete this condition checking later (must assert if Val is null)
Chris Lattner045e7c82001-09-19 16:26:23 +0000566 if( !Val) {
567 if (DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000568 cerr << "Warning: NULL Value found for operand\n";
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000569 continue;
570 }
571 assert( Val && "Value is NULL");
572
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000573 LiveRange *const LR = LRI.getLiveRangeForValue(Val);
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000574
575 if ( !LR ) {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000576
577 // nothing to worry if it's a const or a label
578
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000579 if (DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000580 cerr << "*NO LR for operand : " << Op ;
581 cerr << " [reg:" << Op.getAllocatedRegNum() << "]";
582 cerr << " in inst:\t" << *MInst << "\n";
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000583 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000584
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000585 // if register is not allocated, mark register as invalid
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000586 if( Op.getAllocatedRegNum() == -1)
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000587 Op.setRegForValue( MRI.getInvalidRegNum());
Ruchira Sasankae727f852001-09-18 22:43:57 +0000588
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000589
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000590 continue;
591 }
592
593 unsigned RCID = (LR->getRegClass())->getID();
594
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000595 if( LR->hasColor() ) {
596 Op.setRegForValue( MRI.getUnifiedRegNum(RCID, LR->getColor()) );
597 }
598 else {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000599
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000600 // LR did NOT receive a color (register). Now, insert spill code
601 // for spilled opeands in this machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000602
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000603 //assert(0 && "LR must be spilled");
604 insertCode4SpilledLR(LR, MInst, *BBI, OpNum );
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000605
606 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000607 }
608
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000609 } // for each operand
610
611
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000612 // Now add instructions that the register allocator inserts before/after
613 // this machine instructions (done only for calls/rets/incoming args)
614 // We do this here, to ensure that spill for an instruction is inserted
615 // closest as possible to an instruction (see above insertCode4Spill...)
616 //
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000617 // If there are instructions to be added, *before* this machine
618 // instruction, add them now.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000619 //
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000620 if(AddedInstrMap.count(MInst)) {
Vikram S. Adve48762092002-04-25 04:34:15 +0000621 PrependInstructions(AddedInstrMap[MInst].InstrnsBefore, MIVec, MII,"");
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000622 }
Vikram S. Adve48762092002-04-25 04:34:15 +0000623
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000624 // If there are instructions to be added *after* this machine
625 // instruction, add them now
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000626 //
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000627 if (!AddedInstrMap[MInst].InstrnsAfter.empty()) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000628
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000629 // if there are delay slots for this instruction, the instructions
630 // added after it must really go after the delayed instruction(s)
631 // So, we move the InstrAfter of the current instruction to the
632 // corresponding delayed instruction
633
634 unsigned delay;
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000635 if ((delay=TM.getInstrInfo().getNumDelaySlots(MInst->getOpCode())) >0){
Vikram S. Adve48762092002-04-25 04:34:15 +0000636 move2DelayedInstr(MInst, *(MII+delay) );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000637
Chris Lattner697954c2002-01-20 22:54:45 +0000638 if(DEBUG_RA) cerr<< "\nMoved an added instr after the delay slot";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000639 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000640
641 else {
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000642 // Here we can add the "instructions after" to the current
643 // instruction since there are no delay slots for this instruction
Vikram S. Adve48762092002-04-25 04:34:15 +0000644 AppendInstructions(AddedInstrMap[MInst].InstrnsAfter, MIVec, MII,"");
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000645 } // if not delay
646
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000647 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000648
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000649 } // for each machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000650 }
651}
652
653
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000654
655//----------------------------------------------------------------------------
656// This method inserts spill code for AN operand whose LR was spilled.
657// This method may be called several times for a single machine instruction
658// if it contains many spilled operands. Each time it is called, it finds
659// a register which is not live at that instruction and also which is not
660// used by other spilled operands of the same instruction. Then it uses
661// this register temporarily to accomodate the spilled value.
662//----------------------------------------------------------------------------
663void PhyRegAlloc::insertCode4SpilledLR(const LiveRange *LR,
664 MachineInstr *MInst,
665 const BasicBlock *BB,
666 const unsigned OpNum) {
667
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000668 assert(! TM.getInstrInfo().isCall(MInst->getOpCode()) &&
669 (! TM.getInstrInfo().isReturn(MInst->getOpCode())) &&
670 "Arg of a call/ret must be handled elsewhere");
671
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000672 MachineOperand& Op = MInst->getOperand(OpNum);
673 bool isDef = MInst->operandIsDefined(OpNum);
674 unsigned RegType = MRI.getRegType( LR );
675 int SpillOff = LR->getSpillOffFromFP();
676 RegClass *RC = LR->getRegClass();
Chris Lattner748697d2002-02-05 04:20:12 +0000677 const ValueSet &LVSetBef = LVI->getLiveVarSetBeforeMInst(MInst, BB);
Vikram S. Adve00521d72001-11-12 23:26:35 +0000678
Chris Lattner697954c2002-01-20 22:54:45 +0000679 mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000680
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000681 MachineInstr *MIBef=NULL, *AdIMid=NULL, *MIAft=NULL;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000682
Chris Lattner748697d2002-02-05 04:20:12 +0000683 int TmpRegU = getUsableUniRegAtMI(RC, RegType, MInst,&LVSetBef, MIBef, MIAft);
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000684
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000685 // get the added instructions for this instruciton
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000686 AddedInstrns &AI = AddedInstrMap[MInst];
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000687
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000688 if (!isDef) {
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000689 // for a USE, we have to load the value of LR from stack to a TmpReg
690 // and use the TmpReg as one operand of instruction
691
692 // actual loading instruction
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000693 AdIMid = MRI.cpMem2RegMI(MRI.getFramePointer(), SpillOff, TmpRegU,RegType);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000694
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000695 if(MIBef)
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000696 AI.InstrnsBefore.push_back(MIBef);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000697
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000698 AI.InstrnsBefore.push_back(AdIMid);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000699
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000700 if(MIAft)
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000701 AI.InstrnsAfter.push_front(MIAft);
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000702
Chris Lattner296b7732002-02-05 02:52:05 +0000703 } else { // if this is a Def
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000704 // for a DEF, we have to store the value produced by this instruction
705 // on the stack position allocated for this LR
706
707 // actual storing instruction
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000708 AdIMid = MRI.cpReg2MemMI(TmpRegU, MRI.getFramePointer(), SpillOff,RegType);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000709
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000710 if (MIBef)
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000711 AI.InstrnsBefore.push_back(MIBef);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000712
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000713 AI.InstrnsAfter.push_front(AdIMid);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000714
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000715 if (MIAft)
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000716 AI.InstrnsAfter.push_front(MIAft);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000717
718 } // if !DEF
719
720 cerr << "\nFor Inst " << *MInst;
Chris Lattner296b7732002-02-05 02:52:05 +0000721 cerr << " - SPILLED LR: "; printSet(*LR);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000722 cerr << "\n - Added Instructions:";
Chris Lattner296b7732002-02-05 02:52:05 +0000723 if (MIBef) cerr << *MIBef;
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000724 cerr << *AdIMid;
Chris Lattner296b7732002-02-05 02:52:05 +0000725 if (MIAft) cerr << *MIAft;
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000726
Chris Lattner296b7732002-02-05 02:52:05 +0000727 Op.setRegForValue(TmpRegU); // set the opearnd
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000728}
729
730
731
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000732//----------------------------------------------------------------------------
733// We can use the following method to get a temporary register to be used
734// BEFORE any given machine instruction. If there is a register available,
735// this method will simply return that register and set MIBef = MIAft = NULL.
736// Otherwise, it will return a register and MIAft and MIBef will contain
737// two instructions used to free up this returned register.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000738// Returned register number is the UNIFIED register number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000739//----------------------------------------------------------------------------
740
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000741int PhyRegAlloc::getUsableUniRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000742 const int RegType,
743 const MachineInstr *MInst,
Chris Lattner296b7732002-02-05 02:52:05 +0000744 const ValueSet *LVSetBef,
Vikram S. Adve23a4c8f2002-03-18 03:37:19 +0000745 MachineInstr *&MIBef,
746 MachineInstr *&MIAft) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000747
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000748 int RegU = getUnusedUniRegAtMI(RC, MInst, LVSetBef);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000749
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000750
751 if( RegU != -1) {
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000752 // we found an unused register, so we can simply use it
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000753 MIBef = MIAft = NULL;
754 }
755 else {
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000756 // we couldn't find an unused register. Generate code to free up a reg by
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000757 // saving it on stack and restoring after the instruction
758
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000759 int TmpOff = mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
Vikram S. Adve12af1642001-11-08 04:48:50 +0000760
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000761 RegU = getUniRegNotUsedByThisInst(RC, MInst);
762 MIBef = MRI.cpReg2MemMI(RegU, MRI.getFramePointer(), TmpOff, RegType );
763 MIAft = MRI.cpMem2RegMI(MRI.getFramePointer(), TmpOff, RegU, RegType );
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000764 }
765
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000766 return RegU;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000767}
768
769//----------------------------------------------------------------------------
770// This method is called to get a new unused register that can be used to
771// accomodate a spilled value.
772// This method may be called several times for a single machine instruction
773// if it contains many spilled operands. Each time it is called, it finds
774// a register which is not live at that instruction and also which is not
775// used by other spilled operands of the same instruction.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000776// Return register number is relative to the register class. NOT
777// unified number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000778//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000779int PhyRegAlloc::getUnusedUniRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000780 const MachineInstr *MInst,
Chris Lattner296b7732002-02-05 02:52:05 +0000781 const ValueSet *LVSetBef) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000782
783 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
784
785 bool *IsColorUsedArr = RC->getIsColorUsedArr();
786
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000787 for(unsigned i=0; i < NumAvailRegs; i++) // Reset array
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000788 IsColorUsedArr[i] = false;
789
Chris Lattner296b7732002-02-05 02:52:05 +0000790 ValueSet::const_iterator LIt = LVSetBef->begin();
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000791
792 // for each live var in live variable set after machine inst
793 for( ; LIt != LVSetBef->end(); ++LIt) {
794
795 // get the live range corresponding to live var
796 LiveRange *const LRofLV = LRI.getLiveRangeForValue(*LIt );
797
798 // LR can be null if it is a const since a const
799 // doesn't have a dominating def - see Assumptions above
800 if( LRofLV )
801 if( LRofLV->hasColor() )
802 IsColorUsedArr[ LRofLV->getColor() ] = true;
803 }
804
805 // It is possible that one operand of this MInst was already spilled
806 // and it received some register temporarily. If that's the case,
807 // it is recorded in machine operand. We must skip such registers.
808
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000809 setRelRegsUsedByThisInst(RC, MInst);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000810
811 unsigned c; // find first unused color
812 for( c=0; c < NumAvailRegs; c++)
813 if( ! IsColorUsedArr[ c ] ) break;
814
815 if(c < NumAvailRegs)
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000816 return MRI.getUnifiedRegNum(RC->getID(), c);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000817 else
818 return -1;
819
820
821}
822
823
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000824//----------------------------------------------------------------------------
825// Get any other register in a register class, other than what is used
826// by operands of a machine instruction. Returns the unified reg number.
827//----------------------------------------------------------------------------
828int PhyRegAlloc::getUniRegNotUsedByThisInst(RegClass *RC,
829 const MachineInstr *MInst) {
830
831 bool *IsColorUsedArr = RC->getIsColorUsedArr();
832 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
833
834
835 for(unsigned i=0; i < NumAvailRegs ; i++) // Reset array
836 IsColorUsedArr[i] = false;
837
838 setRelRegsUsedByThisInst(RC, MInst);
839
840 unsigned c; // find first unused color
841 for( c=0; c < RC->getNumOfAvailRegs(); c++)
842 if( ! IsColorUsedArr[ c ] ) break;
843
844 if(c < NumAvailRegs)
845 return MRI.getUnifiedRegNum(RC->getID(), c);
846 else
847 assert( 0 && "FATAL: No free register could be found in reg class!!");
Chris Lattner697954c2002-01-20 22:54:45 +0000848 return 0;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000849}
850
851
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000852//----------------------------------------------------------------------------
853// This method modifies the IsColorUsedArr of the register class passed to it.
854// It sets the bits corresponding to the registers used by this machine
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000855// instructions. Both explicit and implicit operands are set.
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000856//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000857void PhyRegAlloc::setRelRegsUsedByThisInst(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000858 const MachineInstr *MInst ) {
859
860 bool *IsColorUsedArr = RC->getIsColorUsedArr();
861
862 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
863
864 const MachineOperand& Op = MInst->getOperand(OpNum);
865
866 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000867 Op.getOperandType() == MachineOperand::MO_CCRegister ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000868
869 const Value *const Val = Op.getVRegValue();
870
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000871 if( Val )
872 if( MRI.getRegClassIDOfValue(Val) == RC->getID() ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000873 int Reg;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000874 if( (Reg=Op.getAllocatedRegNum()) != -1) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000875 IsColorUsedArr[ Reg ] = true;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000876 }
877 else {
878 // it is possilbe that this operand still is not marked with
879 // a register but it has a LR and that received a color
880
881 LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
882 if( LROfVal)
883 if( LROfVal->hasColor() )
884 IsColorUsedArr[ LROfVal->getColor() ] = true;
885 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000886
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000887 } // if reg classes are the same
888 }
889 else if (Op.getOperandType() == MachineOperand::MO_MachineRegister) {
890 IsColorUsedArr[ Op.getMachineRegNum() ] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000891 }
892 }
893
894 // If there are implicit references, mark them as well
895
896 for(unsigned z=0; z < MInst->getNumImplicitRefs(); z++) {
897
898 LiveRange *const LRofImpRef =
899 LRI.getLiveRangeForValue( MInst->getImplicitRef(z) );
Chris Lattner697954c2002-01-20 22:54:45 +0000900
901 if(LRofImpRef && LRofImpRef->hasColor())
902 IsColorUsedArr[LRofImpRef->getColor()] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000903 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000904}
905
906
907
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000908
909
910
911
912
913//----------------------------------------------------------------------------
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000914// If there are delay slots for an instruction, the instructions
915// added after it must really go after the delayed instruction(s).
916// So, we move the InstrAfter of that instruction to the
917// corresponding delayed instruction using the following method.
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000918
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000919//----------------------------------------------------------------------------
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000920void PhyRegAlloc::move2DelayedInstr(const MachineInstr *OrigMI,
921 const MachineInstr *DelayedMI) {
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000922
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000923 // "added after" instructions of the original instr
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000924 std::deque<MachineInstr *> &OrigAft = AddedInstrMap[OrigMI].InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000925
926 // "added instructions" of the delayed instr
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000927 AddedInstrns &DelayAdI = AddedInstrMap[DelayedMI];
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000928
929 // "added after" instructions of the delayed instr
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000930 std::deque<MachineInstr *> &DelayedAft = DelayAdI.InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000931
932 // go thru all the "added after instructions" of the original instruction
933 // and append them to the "addded after instructions" of the delayed
934 // instructions
Chris Lattner697954c2002-01-20 22:54:45 +0000935 DelayedAft.insert(DelayedAft.end(), OrigAft.begin(), OrigAft.end());
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000936
937 // empty the "added after instructions" of the original instruction
938 OrigAft.clear();
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000939}
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000940
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000941//----------------------------------------------------------------------------
942// This method prints the code with registers after register allocation is
943// complete.
944//----------------------------------------------------------------------------
945void PhyRegAlloc::printMachineCode()
946{
947
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000948 cerr << "\n;************** Function " << Meth->getName()
Chris Lattner697954c2002-01-20 22:54:45 +0000949 << " *****************\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000950
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000951 for (Function::const_iterator BBI = Meth->begin(), BBE = Meth->end();
952 BBI != BBE; ++BBI) {
953 cerr << "\n"; printLabel(*BBI); cerr << ": ";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000954
955 // get the iterator for machine instructions
956 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
Vikram S. Adve48762092002-04-25 04:34:15 +0000957 MachineCodeForBasicBlock::iterator MII = MIVec.begin();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000958
959 // iterate over all the machine instructions in BB
Vikram S. Adve48762092002-04-25 04:34:15 +0000960 for( ; MII != MIVec.end(); ++MII) {
961 MachineInstr *const MInst = *MII;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000962
Chris Lattner697954c2002-01-20 22:54:45 +0000963 cerr << "\n\t";
964 cerr << TargetInstrDescriptors[MInst->getOpCode()].opCodeString;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000965
966 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000967 MachineOperand& Op = MInst->getOperand(OpNum);
968
969 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasanka97b8b442001-10-18 22:36:26 +0000970 Op.getOperandType() == MachineOperand::MO_CCRegister /*||
971 Op.getOperandType() == MachineOperand::MO_PCRelativeDisp*/ ) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000972
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000973 const Value *const Val = Op.getVRegValue () ;
Ruchira Sasankae727f852001-09-18 22:43:57 +0000974 // ****this code is temporary till NULL Values are fixed
975 if( ! Val ) {
Chris Lattner697954c2002-01-20 22:54:45 +0000976 cerr << "\t<*NULL*>";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000977 continue;
978 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000979
980 // if a label or a constant
Chris Lattnerdbe53042002-01-21 01:33:12 +0000981 if(isa<BasicBlock>(Val)) {
Chris Lattner697954c2002-01-20 22:54:45 +0000982 cerr << "\t"; printLabel( Op.getVRegValue () );
983 } else {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000984 // else it must be a register value
985 const int RegNum = Op.getAllocatedRegNum();
986
Chris Lattner697954c2002-01-20 22:54:45 +0000987 cerr << "\t" << "%" << MRI.getUnifiedRegName( RegNum );
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000988 if (Val->hasName() )
Chris Lattner697954c2002-01-20 22:54:45 +0000989 cerr << "(" << Val->getName() << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000990 else
Chris Lattner697954c2002-01-20 22:54:45 +0000991 cerr << "(" << Val << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000992
993 if( Op.opIsDef() )
Chris Lattner697954c2002-01-20 22:54:45 +0000994 cerr << "*";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000995
996 const LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
997 if( LROfVal )
998 if( LROfVal->hasSpillOffset() )
Chris Lattner697954c2002-01-20 22:54:45 +0000999 cerr << "$";
Ruchira Sasankae727f852001-09-18 22:43:57 +00001000 }
1001
1002 }
1003 else if(Op.getOperandType() == MachineOperand::MO_MachineRegister) {
Chris Lattner697954c2002-01-20 22:54:45 +00001004 cerr << "\t" << "%" << MRI.getUnifiedRegName(Op.getMachineRegNum());
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001005 }
1006
1007 else
Chris Lattner697954c2002-01-20 22:54:45 +00001008 cerr << "\t" << Op; // use dump field
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001009 }
1010
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001011
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001012
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001013 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
Chris Lattner0665a5f2002-02-05 01:43:49 +00001014 if( NumOfImpRefs > 0) {
Chris Lattner697954c2002-01-20 22:54:45 +00001015 cerr << "\tImplicit:";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001016
Chris Lattner0665a5f2002-02-05 01:43:49 +00001017 for(unsigned z=0; z < NumOfImpRefs; z++)
1018 cerr << RAV(MInst->getImplicitRef(z)) << "\t";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001019 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001020
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001021 } // for all machine instructions
1022
Chris Lattner697954c2002-01-20 22:54:45 +00001023 cerr << "\n";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001024
1025 } // for all BBs
1026
Chris Lattner697954c2002-01-20 22:54:45 +00001027 cerr << "\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001028}
1029
Ruchira Sasankae727f852001-09-18 22:43:57 +00001030
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001031#if 0
1032
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001033//----------------------------------------------------------------------------
1034//
1035//----------------------------------------------------------------------------
1036
1037void PhyRegAlloc::colorCallRetArgs()
1038{
1039
1040 CallRetInstrListType &CallRetInstList = LRI.getCallRetInstrList();
1041 CallRetInstrListType::const_iterator It = CallRetInstList.begin();
1042
1043 for( ; It != CallRetInstList.end(); ++It ) {
1044
Ruchira Sasankaa90e7702001-10-15 16:26:38 +00001045 const MachineInstr *const CRMI = *It;
1046 unsigned OpCode = CRMI->getOpCode();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001047
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001048 // get the added instructions for this Call/Ret instruciton
Chris Lattner0b0ffa02002-04-09 05:13:04 +00001049 AddedInstrns &AI = AddedInstrMap[CRMI];
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001050
Chris Lattner0b0ffa02002-04-09 05:13:04 +00001051 // Tmp stack positions are needed by some calls that have spilled args
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001052 // So reset it before we call each such method
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001053 //mcInfo.popAllTempValues(TM);
1054
Vikram S. Adve12af1642001-11-08 04:48:50 +00001055
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001056 if (TM.getInstrInfo().isCall(OpCode))
Chris Lattner0b0ffa02002-04-09 05:13:04 +00001057 MRI.colorCallArgs(CRMI, LRI, &AI, *this);
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001058 else if (TM.getInstrInfo().isReturn(OpCode))
Chris Lattner0b0ffa02002-04-09 05:13:04 +00001059 MRI.colorRetValue(CRMI, LRI, &AI);
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001060 else
1061 assert(0 && "Non Call/Ret instrn in CallRetInstrList\n");
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001062 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001063}
1064
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001065#endif
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001066
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001067//----------------------------------------------------------------------------
1068
1069//----------------------------------------------------------------------------
1070void PhyRegAlloc::colorIncomingArgs()
1071{
1072 const BasicBlock *const FirstBB = Meth->front();
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001073 const MachineInstr *FirstMI = FirstBB->getMachineInstrVec().front();
1074 assert(FirstMI && "No machine instruction in entry BB");
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001075
Vikram S. Adve48762092002-04-25 04:34:15 +00001076 MRI.colorMethodArgs(Meth, LRI, &AddedInstrAtEntry);
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001077}
1078
Ruchira Sasankae727f852001-09-18 22:43:57 +00001079
1080//----------------------------------------------------------------------------
1081// Used to generate a label for a basic block
1082//----------------------------------------------------------------------------
Chris Lattner697954c2002-01-20 22:54:45 +00001083void PhyRegAlloc::printLabel(const Value *const Val) {
1084 if (Val->hasName())
1085 cerr << Val->getName();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001086 else
Chris Lattner697954c2002-01-20 22:54:45 +00001087 cerr << "Label" << Val;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001088}
1089
1090
Ruchira Sasankae727f852001-09-18 22:43:57 +00001091//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001092// This method calls setSugColorUsable method of each live range. This
1093// will determine whether the suggested color of LR is really usable.
1094// A suggested color is not usable when the suggested color is volatile
1095// AND when there are call interferences
1096//----------------------------------------------------------------------------
1097
1098void PhyRegAlloc::markUnusableSugColors()
1099{
Chris Lattner697954c2002-01-20 22:54:45 +00001100 if(DEBUG_RA ) cerr << "\nmarking unusable suggested colors ...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001101
1102 // hash map iterator
1103 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
1104 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
1105
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001106 for(; HMI != HMIEnd ; ++HMI ) {
1107 if (HMI->first) {
1108 LiveRange *L = HMI->second; // get the LiveRange
1109 if (L) {
1110 if(L->hasSuggestedColor()) {
1111 int RCID = L->getRegClass()->getID();
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001112 if( MRI.isRegVolatile( RCID, L->getSuggestedColor()) &&
1113 L->isCallInterference() )
1114 L->setSuggestedColorUsable( false );
1115 else
1116 L->setSuggestedColorUsable( true );
1117 }
1118 } // if L->hasSuggestedColor()
1119 }
1120 } // for all LR's in hash map
1121}
1122
1123
1124
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001125//----------------------------------------------------------------------------
1126// The following method will set the stack offsets of the live ranges that
1127// are decided to be spillled. This must be called just after coloring the
1128// LRs using the graph coloring algo. For each live range that is spilled,
1129// this method allocate a new spill position on the stack.
1130//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001131
Chris Lattner37730942002-02-05 03:52:29 +00001132void PhyRegAlloc::allocateStackSpace4SpilledLRs() {
1133 if (DEBUG_RA) cerr << "\nsetting LR stack offsets ...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001134
Chris Lattner37730942002-02-05 03:52:29 +00001135 LiveRangeMapType::const_iterator HMI = LRI.getLiveRangeMap()->begin();
1136 LiveRangeMapType::const_iterator HMIEnd = LRI.getLiveRangeMap()->end();
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001137
Chris Lattner37730942002-02-05 03:52:29 +00001138 for( ; HMI != HMIEnd ; ++HMI) {
1139 if (HMI->first && HMI->second) {
1140 LiveRange *L = HMI->second; // get the LiveRange
1141 if (!L->hasColor()) // NOTE: ** allocating the size of long Type **
1142 L->setSpillOffFromFP(mcInfo.allocateSpilledValue(TM, Type::LongTy));
1143 }
1144 } // for all LR's in hash map
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001145}
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001146
1147
1148
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001149//----------------------------------------------------------------------------
Ruchira Sasankae727f852001-09-18 22:43:57 +00001150// The entry pont to Register Allocation
1151//----------------------------------------------------------------------------
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001152
1153void PhyRegAlloc::allocateRegisters()
1154{
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001155
1156 // make sure that we put all register classes into the RegClassList
1157 // before we call constructLiveRanges (now done in the constructor of
1158 // PhyRegAlloc class).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001159 //
1160 LRI.constructLiveRanges(); // create LR info
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001161
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001162 if (DEBUG_RA)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001163 LRI.printLiveRanges();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001164
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001165 createIGNodeListsAndIGs(); // create IGNode list and IGs
1166
1167 buildInterferenceGraphs(); // build IGs in all reg classes
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001168
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001169
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001170 if (DEBUG_RA) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001171 // print all LRs in all reg classes
1172 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1173 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001174
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001175 // print IGs in all register classes
1176 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1177 RegClassList[ rc ]->printIG();
1178 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001179
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001180
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001181 LRI.coalesceLRs(); // coalesce all live ranges
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001182
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +00001183
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001184 if( DEBUG_RA) {
1185 // print all LRs in all reg classes
1186 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1187 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001188
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001189 // print IGs in all register classes
1190 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1191 RegClassList[ rc ]->printIG();
1192 }
1193
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001194
1195 // mark un-usable suggested color before graph coloring algorithm.
1196 // When this is done, the graph coloring algo will not reserve
1197 // suggested color unnecessarily - they can be used by another LR
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001198 //
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001199 markUnusableSugColors();
1200
1201 // color all register classes using the graph coloring algo
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001202 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1203 RegClassList[ rc ]->colorAllRegs();
1204
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001205 // Atter grpah coloring, if some LRs did not receive a color (i.e, spilled)
1206 // a poistion for such spilled LRs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001207 //
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001208 allocateStackSpace4SpilledLRs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001209
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001210 mcInfo.popAllTempValues(TM); // TODO **Check
1211
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001212 // color incoming args - if the correct color was not received
1213 // insert code to copy to the correct register
1214 //
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001215 colorIncomingArgs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001216
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001217 // Now update the machine code with register names and add any
1218 // additional code inserted by the register allocator to the instruction
1219 // stream
1220 //
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001221 updateMachineCode();
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001222
Chris Lattner045e7c82001-09-19 16:26:23 +00001223 if (DEBUG_RA) {
Vikram S. Adve12af1642001-11-08 04:48:50 +00001224 MachineCodeForMethod::get(Meth).dump();
Chris Lattner045e7c82001-09-19 16:26:23 +00001225 printMachineCode(); // only for DEBUGGING
1226 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001227}
1228
Ruchira Sasankae727f852001-09-18 22:43:57 +00001229
1230