blob: 6a7031bab59211ff92ea71e40e2b5c776965c322 [file] [log] [blame]
Vikram S. Adve12af1642001-11-08 04:48:50 +00001// $Id$
2//***************************************************************************
3// File:
4// PhyRegAlloc.cpp
5//
6// Purpose:
7// Register allocation for LLVM.
8//
9// History:
10// 9/10/01 - Ruchira Sasanka - created.
11//**************************************************************************/
Ruchira Sasanka8e604792001-09-14 21:18:34 +000012
Chris Lattner6dd98a62002-02-04 00:33:08 +000013#include "llvm/CodeGen/RegisterAllocation.h"
Vikram S. Adve12af1642001-11-08 04:48:50 +000014#include "llvm/CodeGen/PhyRegAlloc.h"
15#include "llvm/CodeGen/MachineInstr.h"
Vikram S. Advedabb41d2002-05-19 15:29:31 +000016#include "llvm/CodeGen/MachineInstrAnnot.h"
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000017#include "llvm/CodeGen/MachineCodeForMethod.h"
Chris Lattner483e14e2002-04-27 07:27:19 +000018#include "llvm/Analysis/LiveVar/FunctionLiveVarInfo.h"
Chris Lattner14ab1ce2002-02-04 17:48:00 +000019#include "llvm/Analysis/LoopInfo.h"
Vikram S. Adve12af1642001-11-08 04:48:50 +000020#include "llvm/Target/TargetMachine.h"
21#include "llvm/Target/MachineFrameInfo.h"
Chris Lattner221d6882002-02-12 21:07:25 +000022#include "llvm/BasicBlock.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000023#include "llvm/Function.h"
Chris Lattner37730942002-02-05 03:52:29 +000024#include "llvm/Type.h"
Vikram S. Advedabb41d2002-05-19 15:29:31 +000025#include "llvm/iOther.h"
Chris Lattnerc6f3ae52002-04-29 17:42:12 +000026#include "llvm/CodeGen/RegAllocCommon.h"
Chris Lattner70e60cb2002-05-22 17:08:27 +000027#include "Support/CommandLine.h"
Chris Lattner697954c2002-01-20 22:54:45 +000028#include <iostream>
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000029#include <math.h>
Chris Lattner697954c2002-01-20 22:54:45 +000030using std::cerr;
Vikram S. Adve12af1642001-11-08 04:48:50 +000031
Chris Lattner70e60cb2002-05-22 17:08:27 +000032RegAllocDebugLevel_t DEBUG_RA;
33static cl::Enum<RegAllocDebugLevel_t> DEBUG_RA_c(DEBUG_RA, "dregalloc",
34 cl::Hidden,
Chris Lattner045e7c82001-09-19 16:26:23 +000035 "enable register allocation debugging information",
36 clEnumValN(RA_DEBUG_None , "n", "disable debug output"),
37 clEnumValN(RA_DEBUG_Normal , "y", "enable debug output"),
38 clEnumValN(RA_DEBUG_Verbose, "v", "enable extra debug output"), 0);
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000039
40
Chris Lattner2f9b28e2002-02-04 15:54:09 +000041//----------------------------------------------------------------------------
42// RegisterAllocation pass front end...
43//----------------------------------------------------------------------------
44namespace {
Chris Lattnerf57b8452002-04-27 06:56:12 +000045 class RegisterAllocator : public FunctionPass {
Chris Lattner2f9b28e2002-02-04 15:54:09 +000046 TargetMachine &Target;
47 public:
48 inline RegisterAllocator(TargetMachine &T) : Target(T) {}
Chris Lattner96c466b2002-04-29 14:57:45 +000049
50 const char *getPassName() const { return "Register Allocation"; }
Chris Lattner6dd98a62002-02-04 00:33:08 +000051
Chris Lattnerf57b8452002-04-27 06:56:12 +000052 bool runOnFunction(Function *F) {
Chris Lattner2f9b28e2002-02-04 15:54:09 +000053 if (DEBUG_RA)
Chris Lattnerf57b8452002-04-27 06:56:12 +000054 cerr << "\n******************** Function "<< F->getName()
Chris Lattner2f9b28e2002-02-04 15:54:09 +000055 << " ********************\n";
56
Chris Lattner483e14e2002-04-27 07:27:19 +000057 PhyRegAlloc PRA(F, Target, &getAnalysis<FunctionLiveVarInfo>(),
Chris Lattner1b7f7dc2002-04-28 16:21:30 +000058 &getAnalysis<LoopInfo>());
Chris Lattner2f9b28e2002-02-04 15:54:09 +000059 PRA.allocateRegisters();
60
61 if (DEBUG_RA) cerr << "\nRegister allocation complete!\n";
62 return false;
63 }
Chris Lattner4911c352002-02-04 17:39:42 +000064
Chris Lattnerf57b8452002-04-27 06:56:12 +000065 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner1b7f7dc2002-04-28 16:21:30 +000066 AU.addRequired(LoopInfo::ID);
Chris Lattner483e14e2002-04-27 07:27:19 +000067 AU.addRequired(FunctionLiveVarInfo::ID);
Chris Lattner4911c352002-02-04 17:39:42 +000068 }
Chris Lattner2f9b28e2002-02-04 15:54:09 +000069 };
Chris Lattner6dd98a62002-02-04 00:33:08 +000070}
71
Chris Lattnerf57b8452002-04-27 06:56:12 +000072Pass *getRegisterAllocator(TargetMachine &T) {
Chris Lattner2f9b28e2002-02-04 15:54:09 +000073 return new RegisterAllocator(T);
74}
Chris Lattner6dd98a62002-02-04 00:33:08 +000075
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000076//----------------------------------------------------------------------------
77// Constructor: Init local composite objects and create register classes.
78//----------------------------------------------------------------------------
Chris Lattner1b7f7dc2002-04-28 16:21:30 +000079PhyRegAlloc::PhyRegAlloc(Function *F, const TargetMachine& tm,
80 FunctionLiveVarInfo *Lvi, LoopInfo *LDC)
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000081 : TM(tm), Meth(F),
82 mcInfo(MachineCodeForMethod::get(F)),
83 LVI(Lvi), LRI(F, tm, RegClassList),
84 MRI(tm.getRegInfo()),
Ruchira Sasanka8e604792001-09-14 21:18:34 +000085 NumOfRegClasses(MRI.getNumOfRegClasses()),
Chris Lattner4911c352002-02-04 17:39:42 +000086 LoopDepthCalc(LDC) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +000087
88 // create each RegisterClass and put in RegClassList
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000089 //
Chris Lattner697954c2002-01-20 22:54:45 +000090 for(unsigned int rc=0; rc < NumOfRegClasses; rc++)
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000091 RegClassList.push_back(new RegClass(F, MRI.getMachineRegClass(rc),
92 &ResColList));
Ruchira Sasanka8e604792001-09-14 21:18:34 +000093}
94
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000095
96//----------------------------------------------------------------------------
97// Destructor: Deletes register classes
98//----------------------------------------------------------------------------
99PhyRegAlloc::~PhyRegAlloc() {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000100 for( unsigned int rc=0; rc < NumOfRegClasses; rc++)
101 delete RegClassList[rc];
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000102
103 AddedInstrMap.clear();
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000104}
105
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000106//----------------------------------------------------------------------------
107// This method initally creates interference graphs (one in each reg class)
108// and IGNodeList (one in each IG). The actual nodes will be pushed later.
109//----------------------------------------------------------------------------
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000110void PhyRegAlloc::createIGNodeListsAndIGs() {
111 if (DEBUG_RA) cerr << "Creating LR lists ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000112
113 // hash map iterator
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000114 LiveRangeMapType::const_iterator HMI = LRI.getLiveRangeMap()->begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000115
116 // hash map end
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000117 LiveRangeMapType::const_iterator HMIEnd = LRI.getLiveRangeMap()->end();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000118
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000119 for (; HMI != HMIEnd ; ++HMI ) {
120 if (HMI->first) {
121 LiveRange *L = HMI->second; // get the LiveRange
122 if (!L) {
123 if( DEBUG_RA) {
Chris Lattner0665a5f2002-02-05 01:43:49 +0000124 cerr << "\n*?!?Warning: Null liver range found for: "
125 << RAV(HMI->first) << "\n";
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000126 }
127 continue;
128 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000129 // if the Value * is not null, and LR
130 // is not yet written to the IGNodeList
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000131 if( !(L->getUserIGNode()) ) {
132 RegClass *const RC = // RegClass of first value in the LR
133 RegClassList[ L->getRegClass()->getID() ];
134
135 RC->addLRToIG(L); // add this LR to an IG
136 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000137 }
138 }
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000139
140 // init RegClassList
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000141 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000142 RegClassList[rc]->createInterferenceGraph();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000143
144 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000145 cerr << "LRLists Created!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000146}
147
148
149
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000150
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000151//----------------------------------------------------------------------------
152// This method will add all interferences at for a given instruction.
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000153// Interence occurs only if the LR of Def (Inst or Arg) is of the same reg
154// class as that of live var. The live var passed to this function is the
155// LVset AFTER the instruction
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000156//----------------------------------------------------------------------------
Chris Lattner296b7732002-02-05 02:52:05 +0000157void PhyRegAlloc::addInterference(const Value *Def,
158 const ValueSet *LVSet,
159 bool isCallInst) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000160
Chris Lattner296b7732002-02-05 02:52:05 +0000161 ValueSet::const_iterator LIt = LVSet->begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000162
163 // get the live range of instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000164 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000165 const LiveRange *const LROfDef = LRI.getLiveRangeForValue( Def );
166
167 IGNode *const IGNodeOfDef = LROfDef->getUserIGNode();
168 assert( IGNodeOfDef );
169
170 RegClass *const RCOfDef = LROfDef->getRegClass();
171
172 // for each live var in live variable set
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000173 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000174 for( ; LIt != LVSet->end(); ++LIt) {
175
Chris Lattner0665a5f2002-02-05 01:43:49 +0000176 if (DEBUG_RA > 1)
177 cerr << "< Def=" << RAV(Def) << ", Lvar=" << RAV(*LIt) << "> ";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000178
179 // get the live range corresponding to live var
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000180 //
Chris Lattner0665a5f2002-02-05 01:43:49 +0000181 LiveRange *LROfVar = LRI.getLiveRangeForValue(*LIt);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000182
183 // LROfVar can be null if it is a const since a const
184 // doesn't have a dominating def - see Assumptions above
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000185 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000186 if (LROfVar) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000187 if(LROfDef == LROfVar) // do not set interf for same LR
188 continue;
189
190 // if 2 reg classes are the same set interference
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000191 //
Chris Lattner0665a5f2002-02-05 01:43:49 +0000192 if (RCOfDef == LROfVar->getRegClass()) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000193 RCOfDef->setInterference( LROfDef, LROfVar);
Chris Lattner0665a5f2002-02-05 01:43:49 +0000194 } else if (DEBUG_RA > 1) {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000195 // we will not have LRs for values not explicitly allocated in the
196 // instruction stream (e.g., constants)
Chris Lattner0665a5f2002-02-05 01:43:49 +0000197 cerr << " warning: no live range for " << RAV(*LIt) << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000198 }
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000199 }
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000200 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000201}
202
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000203
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000204
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000205//----------------------------------------------------------------------------
206// For a call instruction, this method sets the CallInterference flag in
207// the LR of each variable live int the Live Variable Set live after the
208// call instruction (except the return value of the call instruction - since
209// the return value does not interfere with that call itself).
210//----------------------------------------------------------------------------
211
212void PhyRegAlloc::setCallInterferences(const MachineInstr *MInst,
Chris Lattner296b7732002-02-05 02:52:05 +0000213 const ValueSet *LVSetAft) {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000214
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000215 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000216 cerr << "\n For call inst: " << *MInst;
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000217
Chris Lattner296b7732002-02-05 02:52:05 +0000218 ValueSet::const_iterator LIt = LVSetAft->begin();
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000219
220 // for each live var in live variable set after machine inst
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000221 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000222 for( ; LIt != LVSetAft->end(); ++LIt) {
223
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000224 // get the live range corresponding to live var
225 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000226 LiveRange *const LR = LRI.getLiveRangeForValue(*LIt );
227
228 if( LR && DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000229 cerr << "\n\tLR Aft Call: ";
Chris Lattner296b7732002-02-05 02:52:05 +0000230 printSet(*LR);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000231 }
232
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000233 // LR can be null if it is a const since a const
234 // doesn't have a dominating def - see Assumptions above
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000235 //
Vikram S. Adve1a53f032002-03-31 18:54:37 +0000236 if( LR ) {
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000237 LR->setCallInterference();
238 if( DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000239 cerr << "\n ++Added call interf for LR: " ;
Chris Lattner296b7732002-02-05 02:52:05 +0000240 printSet(*LR);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000241 }
242 }
243
244 }
245
Vikram S. Adve1a53f032002-03-31 18:54:37 +0000246 // Now find the LR of the return value of the call
247 // We do this because, we look at the LV set *after* the instruction
248 // to determine, which LRs must be saved across calls. The return value
249 // of the call is live in this set - but it does not interfere with call
250 // (i.e., we can allocate a volatile register to the return value)
251 //
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000252 CallArgsDescriptor* argDesc = CallArgsDescriptor::get(MInst);
253
254 if (const Value *RetVal = argDesc->getReturnValue()) {
Vikram S. Adve1a53f032002-03-31 18:54:37 +0000255 LiveRange *RetValLR = LRI.getLiveRangeForValue( RetVal );
256 assert( RetValLR && "No LR for RetValue of call");
257 RetValLR->clearCallInterference();
258 }
259
260 // If the CALL is an indirect call, find the LR of the function pointer.
261 // That has a call interference because it conflicts with outgoing args.
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000262 if( const Value *AddrVal = argDesc->getIndirectFuncPtr()) {
Vikram S. Adve1a53f032002-03-31 18:54:37 +0000263 LiveRange *AddrValLR = LRI.getLiveRangeForValue( AddrVal );
264 assert( AddrValLR && "No LR for indirect addr val of call");
265 AddrValLR->setCallInterference();
266 }
267
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000268}
269
270
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000271
272
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000273//----------------------------------------------------------------------------
274// This method will walk thru code and create interferences in the IG of
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000275// each RegClass. Also, this method calculates the spill cost of each
276// Live Range (it is done in this method to save another pass over the code).
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000277//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000278void PhyRegAlloc::buildInterferenceGraphs()
279{
280
Chris Lattner697954c2002-01-20 22:54:45 +0000281 if(DEBUG_RA) cerr << "Creating interference graphs ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000282
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000283 unsigned BBLoopDepthCost;
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000284 for (Function::const_iterator BBI = Meth->begin(), BBE = Meth->end();
285 BBI != BBE; ++BBI) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000286
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000287 // find the 10^(loop_depth) of this BB
288 //
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000289 BBLoopDepthCost = (unsigned) pow(10.0, LoopDepthCalc->getLoopDepth(*BBI));
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000290
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000291 // get the iterator for machine instructions
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000292 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000293 const MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
Vikram S. Adve48762092002-04-25 04:34:15 +0000294 MachineCodeForBasicBlock::const_iterator MII = MIVec.begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000295
296 // iterate over all the machine instructions in BB
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000297 //
Vikram S. Adve48762092002-04-25 04:34:15 +0000298 for( ; MII != MIVec.end(); ++MII) {
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000299
Vikram S. Adve48762092002-04-25 04:34:15 +0000300 const MachineInstr *MInst = *MII;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000301
302 // get the LV set after the instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000303 //
Chris Lattner748697d2002-02-05 04:20:12 +0000304 const ValueSet &LVSetAI = LVI->getLiveVarSetAfterMInst(MInst, *BBI);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000305
306 const bool isCallInst = TM.getInstrInfo().isCall(MInst->getOpCode());
307
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000308 if( isCallInst ) {
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000309 // set the isCallInterference flag of each live range wich extends
310 // accross this call instruction. This information is used by graph
311 // coloring algo to avoid allocating volatile colors to live ranges
312 // that span across calls (since they have to be saved/restored)
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000313 //
Chris Lattner748697d2002-02-05 04:20:12 +0000314 setCallInterferences(MInst, &LVSetAI);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000315 }
316
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000317
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000318 // iterate over all MI operands to find defs
319 //
Chris Lattner2f898d22002-02-05 06:02:59 +0000320 for (MachineInstr::const_val_op_iterator OpI = MInst->begin(),
321 OpE = MInst->end(); OpI != OpE; ++OpI) {
322 if (OpI.isDef()) // create a new LR iff this operand is a def
Chris Lattner748697d2002-02-05 04:20:12 +0000323 addInterference(*OpI, &LVSetAI, isCallInst);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000324
325 // Calculate the spill cost of each live range
326 //
Chris Lattner2f898d22002-02-05 06:02:59 +0000327 LiveRange *LR = LRI.getLiveRangeForValue(*OpI);
328 if (LR) LR->addSpillCost(BBLoopDepthCost);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000329 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000330
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000331
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000332 // if there are multiple defs in this instruction e.g. in SETX
333 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000334 if (TM.getInstrInfo().isPseudoInstr(MInst->getOpCode()))
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000335 addInterf4PseudoInstr(MInst);
336
337
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000338 // Also add interference for any implicit definitions in a machine
339 // instr (currently, only calls have this).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000340 //
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000341 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
342 if( NumOfImpRefs > 0 ) {
343 for(unsigned z=0; z < NumOfImpRefs; z++)
344 if( MInst->implicitRefIsDefined(z) )
Chris Lattner748697d2002-02-05 04:20:12 +0000345 addInterference( MInst->getImplicitRef(z), &LVSetAI, isCallInst );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000346 }
347
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +0000348
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000349 } // for all machine instructions in BB
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000350 } // for all BBs in function
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000351
352
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000353 // add interferences for function arguments. Since there are no explict
354 // defs in the function for args, we have to add them manually
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000355 //
356 addInterferencesForArgs();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000357
358 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000359 cerr << "Interference graphs calculted!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000360
361}
362
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000363
364
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000365//--------------------------------------------------------------------------
366// Pseudo instructions will be exapnded to multiple instructions by the
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000367// assembler. Consequently, all the opernds must get distinct registers.
368// Therefore, we mark all operands of a pseudo instruction as they interfere
369// with one another.
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000370//--------------------------------------------------------------------------
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000371void PhyRegAlloc::addInterf4PseudoInstr(const MachineInstr *MInst) {
372
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000373 bool setInterf = false;
374
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000375 // iterate over MI operands to find defs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000376 //
Chris Lattner2f898d22002-02-05 06:02:59 +0000377 for (MachineInstr::const_val_op_iterator It1 = MInst->begin(),
378 ItE = MInst->end(); It1 != ItE; ++It1) {
379 const LiveRange *LROfOp1 = LRI.getLiveRangeForValue(*It1);
380 assert((LROfOp1 || !It1.isDef()) && "No LR for Def in PSEUDO insruction");
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000381
Chris Lattner2f898d22002-02-05 06:02:59 +0000382 MachineInstr::const_val_op_iterator It2 = It1;
383 for(++It2; It2 != ItE; ++It2) {
384 const LiveRange *LROfOp2 = LRI.getLiveRangeForValue(*It2);
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000385
Chris Lattner2f898d22002-02-05 06:02:59 +0000386 if (LROfOp2) {
387 RegClass *RCOfOp1 = LROfOp1->getRegClass();
388 RegClass *RCOfOp2 = LROfOp2->getRegClass();
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000389
390 if( RCOfOp1 == RCOfOp2 ){
391 RCOfOp1->setInterference( LROfOp1, LROfOp2 );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000392 setInterf = true;
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000393 }
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000394 } // if Op2 has a LR
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000395 } // for all other defs in machine instr
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000396 } // for all operands in an instruction
397
Chris Lattner2f898d22002-02-05 06:02:59 +0000398 if (!setInterf && MInst->getNumOperands() > 2) {
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000399 cerr << "\nInterf not set for any operand in pseudo instr:\n";
400 cerr << *MInst;
401 assert(0 && "Interf not set for pseudo instr with > 2 operands" );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000402 }
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000403}
404
405
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000406
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000407//----------------------------------------------------------------------------
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000408// This method will add interferences for incoming arguments to a function.
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000409//----------------------------------------------------------------------------
Chris Lattner296b7732002-02-05 02:52:05 +0000410void PhyRegAlloc::addInterferencesForArgs() {
411 // get the InSet of root BB
Chris Lattner748697d2002-02-05 04:20:12 +0000412 const ValueSet &InSet = LVI->getInSetOfBB(Meth->front());
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000413
Chris Lattner296b7732002-02-05 02:52:05 +0000414 // get the argument list
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000415 const Function::ArgumentListType &ArgList = Meth->getArgumentList();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000416
Chris Lattner296b7732002-02-05 02:52:05 +0000417 // get an iterator to arg list
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000418 Function::ArgumentListType::const_iterator ArgIt = ArgList.begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000419
420
421 for( ; ArgIt != ArgList.end() ; ++ArgIt) { // for each argument
Chris Lattner748697d2002-02-05 04:20:12 +0000422 addInterference((Value*)*ArgIt, &InSet, false);// add interferences between
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000423 // args and LVars at start
Chris Lattner0665a5f2002-02-05 01:43:49 +0000424 if( DEBUG_RA > 1)
425 cerr << " - %% adding interference for argument "
426 << RAV((const Value *)*ArgIt) << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000427 }
428}
429
430
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000431//----------------------------------------------------------------------------
432// This method is called after register allocation is complete to set the
433// allocated reisters in the machine code. This code will add register numbers
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000434// to MachineOperands that contain a Value. Also it calls target specific
435// methods to produce caller saving instructions. At the end, it adds all
436// additional instructions produced by the register allocator to the
437// instruction stream.
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000438//----------------------------------------------------------------------------
Vikram S. Adve48762092002-04-25 04:34:15 +0000439
440//-----------------------------
441// Utility functions used below
442//-----------------------------
443inline void
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000444PrependInstructions(vector<MachineInstr *> &IBef,
Vikram S. Adve48762092002-04-25 04:34:15 +0000445 MachineCodeForBasicBlock& MIVec,
446 MachineCodeForBasicBlock::iterator& MII,
447 const std::string& msg)
448{
449 if (!IBef.empty())
450 {
451 MachineInstr* OrigMI = *MII;
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000452 std::vector<MachineInstr *>::iterator AdIt;
Vikram S. Adve48762092002-04-25 04:34:15 +0000453 for (AdIt = IBef.begin(); AdIt != IBef.end() ; ++AdIt)
454 {
455 if (DEBUG_RA) {
456 if (OrigMI) cerr << "For MInst: " << *OrigMI;
457 cerr << msg << " PREPENDed instr: " << **AdIt << "\n";
458 }
459 MII = MIVec.insert(MII, *AdIt);
460 ++MII;
461 }
462 }
463}
464
465inline void
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000466AppendInstructions(std::vector<MachineInstr *> &IAft,
Vikram S. Adve48762092002-04-25 04:34:15 +0000467 MachineCodeForBasicBlock& MIVec,
468 MachineCodeForBasicBlock::iterator& MII,
469 const std::string& msg)
470{
471 if (!IAft.empty())
472 {
473 MachineInstr* OrigMI = *MII;
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000474 std::vector<MachineInstr *>::iterator AdIt;
Vikram S. Adve48762092002-04-25 04:34:15 +0000475 for( AdIt = IAft.begin(); AdIt != IAft.end() ; ++AdIt )
476 {
477 if(DEBUG_RA) {
478 if (OrigMI) cerr << "For MInst: " << *OrigMI;
479 cerr << msg << " APPENDed instr: " << **AdIt << "\n";
480 }
481 ++MII; // insert before the next instruction
482 MII = MIVec.insert(MII, *AdIt);
483 }
484 }
485}
486
487
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000488void PhyRegAlloc::updateMachineCode()
489{
Vikram S. Adve48762092002-04-25 04:34:15 +0000490 const BasicBlock* entryBB = Meth->getEntryNode();
491 if (entryBB) {
492 MachineCodeForBasicBlock& MIVec = entryBB->getMachineInstrVec();
493 MachineCodeForBasicBlock::iterator MII = MIVec.begin();
494
495 // Insert any instructions needed at method entry
496 PrependInstructions(AddedInstrAtEntry.InstrnsBefore, MIVec, MII,
497 "At function entry: \n");
498 assert(AddedInstrAtEntry.InstrnsAfter.empty() &&
499 "InstrsAfter should be unnecessary since we are just inserting at "
500 "the function entry point here.");
501 }
502
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000503 for (Function::const_iterator BBI = Meth->begin(), BBE = Meth->end();
504 BBI != BBE; ++BBI) {
Vikram S. Adve48762092002-04-25 04:34:15 +0000505
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000506 // iterate over all the machine instructions in BB
Vikram S. Adve48762092002-04-25 04:34:15 +0000507 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
508 for(MachineCodeForBasicBlock::iterator MII = MIVec.begin();
509 MII != MIVec.end(); ++MII) {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000510
Vikram S. Adve48762092002-04-25 04:34:15 +0000511 MachineInstr *MInst = *MII;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000512
513 unsigned Opcode = MInst->getOpCode();
514
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000515 // do not process Phis
Vikram S. Adve23a4c8f2002-03-18 03:37:19 +0000516 if (TM.getInstrInfo().isDummyPhiInstr(Opcode))
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000517 continue;
518
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000519 // Now insert speical instructions (if necessary) for call/return
520 // instructions.
521 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000522 if (TM.getInstrInfo().isCall(Opcode) ||
523 TM.getInstrInfo().isReturn(Opcode)) {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000524
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000525 AddedInstrns &AI = AddedInstrMap[MInst];
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000526
527 // Tmp stack poistions are needed by some calls that have spilled args
528 // So reset it before we call each such method
Ruchira Sasanka6a3db8c2002-01-07 21:09:06 +0000529 //
530 mcInfo.popAllTempValues(TM);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000531
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000532 if (TM.getInstrInfo().isCall(Opcode))
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000533 MRI.colorCallArgs(MInst, LRI, &AI, *this, *BBI);
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000534 else if (TM.getInstrInfo().isReturn(Opcode))
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000535 MRI.colorRetValue(MInst, LRI, &AI);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000536 }
537
538
539 /* -- Using above code instead of this
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000540
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000541 // if this machine instr is call, insert caller saving code
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000542
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000543 if( (TM.getInstrInfo()).isCall( MInst->getOpCode()) )
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000544 MRI.insertCallerSavingCode(MInst, *BBI, *this );
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000545
546 */
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000547
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000548
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000549 // reset the stack offset for temporary variables since we may
550 // need that to spill
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000551 // mcInfo.popAllTempValues(TM);
Ruchira Sasankaf90870f2001-11-15 22:02:06 +0000552 // TODO ** : do later
Vikram S. Adve12af1642001-11-08 04:48:50 +0000553
Chris Lattner7a176752001-12-04 00:03:30 +0000554 //for(MachineInstr::val_const_op_iterator OpI(MInst);!OpI.done();++OpI) {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000555
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000556
557 // Now replace set the registers for operands in the machine instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000558 //
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000559 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
560
561 MachineOperand& Op = MInst->getOperand(OpNum);
562
563 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
564 Op.getOperandType() == MachineOperand::MO_CCRegister) {
565
566 const Value *const Val = Op.getVRegValue();
567
568 // delete this condition checking later (must assert if Val is null)
Chris Lattner045e7c82001-09-19 16:26:23 +0000569 if( !Val) {
570 if (DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000571 cerr << "Warning: NULL Value found for operand\n";
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000572 continue;
573 }
574 assert( Val && "Value is NULL");
575
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000576 LiveRange *const LR = LRI.getLiveRangeForValue(Val);
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000577
578 if ( !LR ) {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000579
580 // nothing to worry if it's a const or a label
581
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000582 if (DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000583 cerr << "*NO LR for operand : " << Op ;
584 cerr << " [reg:" << Op.getAllocatedRegNum() << "]";
585 cerr << " in inst:\t" << *MInst << "\n";
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000586 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000587
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000588 // if register is not allocated, mark register as invalid
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000589 if( Op.getAllocatedRegNum() == -1)
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000590 Op.setRegForValue( MRI.getInvalidRegNum());
Ruchira Sasankae727f852001-09-18 22:43:57 +0000591
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000592
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000593 continue;
594 }
595
596 unsigned RCID = (LR->getRegClass())->getID();
597
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000598 if( LR->hasColor() ) {
599 Op.setRegForValue( MRI.getUnifiedRegNum(RCID, LR->getColor()) );
600 }
601 else {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000602
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000603 // LR did NOT receive a color (register). Now, insert spill code
604 // for spilled opeands in this machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000605
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000606 //assert(0 && "LR must be spilled");
607 insertCode4SpilledLR(LR, MInst, *BBI, OpNum );
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000608
609 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000610 }
611
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000612 } // for each operand
613
614
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000615 // Now add instructions that the register allocator inserts before/after
616 // this machine instructions (done only for calls/rets/incoming args)
617 // We do this here, to ensure that spill for an instruction is inserted
618 // closest as possible to an instruction (see above insertCode4Spill...)
619 //
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000620 // If there are instructions to be added, *before* this machine
621 // instruction, add them now.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000622 //
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000623 if(AddedInstrMap.count(MInst)) {
Vikram S. Adve48762092002-04-25 04:34:15 +0000624 PrependInstructions(AddedInstrMap[MInst].InstrnsBefore, MIVec, MII,"");
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000625 }
Vikram S. Adve48762092002-04-25 04:34:15 +0000626
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000627 // If there are instructions to be added *after* this machine
628 // instruction, add them now
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000629 //
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000630 if (!AddedInstrMap[MInst].InstrnsAfter.empty()) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000631
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000632 // if there are delay slots for this instruction, the instructions
633 // added after it must really go after the delayed instruction(s)
634 // So, we move the InstrAfter of the current instruction to the
635 // corresponding delayed instruction
636
637 unsigned delay;
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000638 if ((delay=TM.getInstrInfo().getNumDelaySlots(MInst->getOpCode())) >0){
Vikram S. Adve48762092002-04-25 04:34:15 +0000639 move2DelayedInstr(MInst, *(MII+delay) );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000640
Chris Lattner697954c2002-01-20 22:54:45 +0000641 if(DEBUG_RA) cerr<< "\nMoved an added instr after the delay slot";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000642 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000643
644 else {
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000645 // Here we can add the "instructions after" to the current
646 // instruction since there are no delay slots for this instruction
Vikram S. Adve48762092002-04-25 04:34:15 +0000647 AppendInstructions(AddedInstrMap[MInst].InstrnsAfter, MIVec, MII,"");
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000648 } // if not delay
649
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000650 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000651
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000652 } // for each machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000653 }
654}
655
656
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000657
658//----------------------------------------------------------------------------
659// This method inserts spill code for AN operand whose LR was spilled.
660// This method may be called several times for a single machine instruction
661// if it contains many spilled operands. Each time it is called, it finds
662// a register which is not live at that instruction and also which is not
663// used by other spilled operands of the same instruction. Then it uses
664// this register temporarily to accomodate the spilled value.
665//----------------------------------------------------------------------------
666void PhyRegAlloc::insertCode4SpilledLR(const LiveRange *LR,
667 MachineInstr *MInst,
668 const BasicBlock *BB,
669 const unsigned OpNum) {
670
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000671 assert(! TM.getInstrInfo().isCall(MInst->getOpCode()) &&
672 (! TM.getInstrInfo().isReturn(MInst->getOpCode())) &&
673 "Arg of a call/ret must be handled elsewhere");
674
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000675 MachineOperand& Op = MInst->getOperand(OpNum);
676 bool isDef = MInst->operandIsDefined(OpNum);
677 unsigned RegType = MRI.getRegType( LR );
678 int SpillOff = LR->getSpillOffFromFP();
679 RegClass *RC = LR->getRegClass();
Chris Lattner748697d2002-02-05 04:20:12 +0000680 const ValueSet &LVSetBef = LVI->getLiveVarSetBeforeMInst(MInst, BB);
Vikram S. Adve00521d72001-11-12 23:26:35 +0000681
Chris Lattner697954c2002-01-20 22:54:45 +0000682 mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000683
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000684 MachineInstr *MIBef=NULL, *MIAft=NULL;
685 vector<MachineInstr*> AdIMid;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000686
Chris Lattner748697d2002-02-05 04:20:12 +0000687 int TmpRegU = getUsableUniRegAtMI(RC, RegType, MInst,&LVSetBef, MIBef, MIAft);
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000688
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000689 // get the added instructions for this instruciton
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000690 AddedInstrns &AI = AddedInstrMap[MInst];
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000691
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000692 if (!isDef) {
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000693 // for a USE, we have to load the value of LR from stack to a TmpReg
694 // and use the TmpReg as one operand of instruction
695
696 // actual loading instruction
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000697 MRI.cpMem2RegMI(MRI.getFramePointer(), SpillOff, TmpRegU,RegType, AdIMid);
698 AI.InstrnsBefore.insert(AI.InstrnsBefore.end(),
699 AdIMid.begin(), AdIMid.end());
700
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000701 if(MIBef)
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000702 AI.InstrnsBefore.push_back(MIBef);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000703
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000704 if(MIAft)
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000705 AI.InstrnsAfter.insert(AI.InstrnsAfter.begin(), MIAft);
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000706
Chris Lattner296b7732002-02-05 02:52:05 +0000707 } else { // if this is a Def
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000708 // for a DEF, we have to store the value produced by this instruction
709 // on the stack position allocated for this LR
710
711 // actual storing instruction
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000712 MRI.cpReg2MemMI(TmpRegU, MRI.getFramePointer(), SpillOff,RegType, AdIMid);
713
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000714 if (MIBef)
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000715 AI.InstrnsBefore.push_back(MIBef);
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000716
717 AI.InstrnsAfter.insert(AI.InstrnsAfter.begin(),
718 AdIMid.begin(), AdIMid.end());
719
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000720 if (MIAft)
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000721 AI.InstrnsAfter.insert(AI.InstrnsAfter.begin(), MIAft);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000722
723 } // if !DEF
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000724
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000725 cerr << "\nFor Inst " << *MInst;
Chris Lattner296b7732002-02-05 02:52:05 +0000726 cerr << " - SPILLED LR: "; printSet(*LR);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000727 cerr << "\n - Added Instructions:";
Chris Lattner296b7732002-02-05 02:52:05 +0000728 if (MIBef) cerr << *MIBef;
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000729 for (vector<MachineInstr*>::const_iterator II=AdIMid.begin();
730 II != AdIMid.end(); ++II)
731 cerr << **II;
Chris Lattner296b7732002-02-05 02:52:05 +0000732 if (MIAft) cerr << *MIAft;
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000733
Chris Lattner296b7732002-02-05 02:52:05 +0000734 Op.setRegForValue(TmpRegU); // set the opearnd
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000735}
736
737
738
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000739//----------------------------------------------------------------------------
740// We can use the following method to get a temporary register to be used
741// BEFORE any given machine instruction. If there is a register available,
742// this method will simply return that register and set MIBef = MIAft = NULL.
743// Otherwise, it will return a register and MIAft and MIBef will contain
744// two instructions used to free up this returned register.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000745// Returned register number is the UNIFIED register number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000746//----------------------------------------------------------------------------
747
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000748int PhyRegAlloc::getUsableUniRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000749 const int RegType,
750 const MachineInstr *MInst,
Chris Lattner296b7732002-02-05 02:52:05 +0000751 const ValueSet *LVSetBef,
Vikram S. Adve23a4c8f2002-03-18 03:37:19 +0000752 MachineInstr *&MIBef,
753 MachineInstr *&MIAft) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000754
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000755 int RegU = getUnusedUniRegAtMI(RC, MInst, LVSetBef);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000756
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000757
758 if( RegU != -1) {
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000759 // we found an unused register, so we can simply use it
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000760 MIBef = MIAft = NULL;
761 }
762 else {
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000763 // we couldn't find an unused register. Generate code to free up a reg by
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000764 // saving it on stack and restoring after the instruction
765
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000766 int TmpOff = mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
Vikram S. Adve12af1642001-11-08 04:48:50 +0000767
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000768 RegU = getUniRegNotUsedByThisInst(RC, MInst);
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000769
770 vector<MachineInstr*> mvec;
771
772 MRI.cpReg2MemMI(RegU, MRI.getFramePointer(), TmpOff, RegType, mvec);
773 assert(mvec.size() == 1 && "Need to return a vector here too");
774 MIBef = * mvec.begin();
775
776 MRI.cpMem2RegMI(MRI.getFramePointer(), TmpOff, RegU, RegType, mvec);
777 assert(mvec.size() == 1 && "Need to return a vector here too");
778 MIAft = * mvec.begin();
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000779 }
780
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000781 return RegU;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000782}
783
784//----------------------------------------------------------------------------
785// This method is called to get a new unused register that can be used to
786// accomodate a spilled value.
787// This method may be called several times for a single machine instruction
788// if it contains many spilled operands. Each time it is called, it finds
789// a register which is not live at that instruction and also which is not
790// used by other spilled operands of the same instruction.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000791// Return register number is relative to the register class. NOT
792// unified number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000793//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000794int PhyRegAlloc::getUnusedUniRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000795 const MachineInstr *MInst,
Chris Lattner296b7732002-02-05 02:52:05 +0000796 const ValueSet *LVSetBef) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000797
798 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
799
Chris Lattner85c54652002-05-23 15:50:03 +0000800 std::vector<bool> &IsColorUsedArr = RC->getIsColorUsedArr();
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000801
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000802 for(unsigned i=0; i < NumAvailRegs; i++) // Reset array
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000803 IsColorUsedArr[i] = false;
804
Chris Lattner296b7732002-02-05 02:52:05 +0000805 ValueSet::const_iterator LIt = LVSetBef->begin();
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000806
807 // for each live var in live variable set after machine inst
808 for( ; LIt != LVSetBef->end(); ++LIt) {
809
810 // get the live range corresponding to live var
811 LiveRange *const LRofLV = LRI.getLiveRangeForValue(*LIt );
812
813 // LR can be null if it is a const since a const
814 // doesn't have a dominating def - see Assumptions above
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000815 if( LRofLV && LRofLV->getRegClass() == RC && LRofLV->hasColor() )
816 IsColorUsedArr[ LRofLV->getColor() ] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000817 }
818
819 // It is possible that one operand of this MInst was already spilled
820 // and it received some register temporarily. If that's the case,
821 // it is recorded in machine operand. We must skip such registers.
822
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000823 setRelRegsUsedByThisInst(RC, MInst);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000824
Chris Lattner85c54652002-05-23 15:50:03 +0000825 for(unsigned c=0; c < NumAvailRegs; c++) // find first unused color
826 if (!IsColorUsedArr[c])
827 return MRI.getUnifiedRegNum(RC->getID(), c);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000828
Chris Lattner85c54652002-05-23 15:50:03 +0000829 return -1;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000830}
831
832
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000833//----------------------------------------------------------------------------
834// Get any other register in a register class, other than what is used
835// by operands of a machine instruction. Returns the unified reg number.
836//----------------------------------------------------------------------------
837int PhyRegAlloc::getUniRegNotUsedByThisInst(RegClass *RC,
Chris Lattner85c54652002-05-23 15:50:03 +0000838 const MachineInstr *MInst) {
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000839
Chris Lattner85c54652002-05-23 15:50:03 +0000840 vector<bool> &IsColorUsedArr = RC->getIsColorUsedArr();
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000841 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
842
843
844 for(unsigned i=0; i < NumAvailRegs ; i++) // Reset array
845 IsColorUsedArr[i] = false;
846
847 setRelRegsUsedByThisInst(RC, MInst);
848
Chris Lattner85c54652002-05-23 15:50:03 +0000849 for(unsigned c=0; c < RC->getNumOfAvailRegs(); c++)// find first unused color
850 if (!IsColorUsedArr[c])
851 return MRI.getUnifiedRegNum(RC->getID(), c);
852
853 assert(0 && "FATAL: No free register could be found in reg class!!");
Chris Lattner697954c2002-01-20 22:54:45 +0000854 return 0;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000855}
856
857
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000858//----------------------------------------------------------------------------
859// This method modifies the IsColorUsedArr of the register class passed to it.
860// It sets the bits corresponding to the registers used by this machine
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000861// instructions. Both explicit and implicit operands are set.
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000862//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000863void PhyRegAlloc::setRelRegsUsedByThisInst(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000864 const MachineInstr *MInst ) {
865
Chris Lattner85c54652002-05-23 15:50:03 +0000866 vector<bool> &IsColorUsedArr = RC->getIsColorUsedArr();
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000867
868 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
869
870 const MachineOperand& Op = MInst->getOperand(OpNum);
871
872 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000873 Op.getOperandType() == MachineOperand::MO_CCRegister ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000874
875 const Value *const Val = Op.getVRegValue();
876
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000877 if( Val )
878 if( MRI.getRegClassIDOfValue(Val) == RC->getID() ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000879 int Reg;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000880 if( (Reg=Op.getAllocatedRegNum()) != -1) {
Chris Lattner85c54652002-05-23 15:50:03 +0000881 IsColorUsedArr[Reg] = true;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000882 }
883 else {
884 // it is possilbe that this operand still is not marked with
885 // a register but it has a LR and that received a color
886
887 LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
888 if( LROfVal)
889 if( LROfVal->hasColor() )
Chris Lattner85c54652002-05-23 15:50:03 +0000890 IsColorUsedArr[LROfVal->getColor()] = true;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000891 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000892
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000893 } // if reg classes are the same
894 }
895 else if (Op.getOperandType() == MachineOperand::MO_MachineRegister) {
Chris Lattner85c54652002-05-23 15:50:03 +0000896 assert((unsigned)Op.getMachineRegNum() < IsColorUsedArr.size());
897 IsColorUsedArr[Op.getMachineRegNum()] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000898 }
899 }
900
901 // If there are implicit references, mark them as well
902
903 for(unsigned z=0; z < MInst->getNumImplicitRefs(); z++) {
904
905 LiveRange *const LRofImpRef =
906 LRI.getLiveRangeForValue( MInst->getImplicitRef(z) );
Chris Lattner697954c2002-01-20 22:54:45 +0000907
908 if(LRofImpRef && LRofImpRef->hasColor())
909 IsColorUsedArr[LRofImpRef->getColor()] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000910 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000911}
912
913
914
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000915
916
917
918
919
920//----------------------------------------------------------------------------
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000921// If there are delay slots for an instruction, the instructions
922// added after it must really go after the delayed instruction(s).
923// So, we move the InstrAfter of that instruction to the
924// corresponding delayed instruction using the following method.
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000925
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000926//----------------------------------------------------------------------------
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000927void PhyRegAlloc::move2DelayedInstr(const MachineInstr *OrigMI,
928 const MachineInstr *DelayedMI) {
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000929
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000930 // "added after" instructions of the original instr
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000931 std::vector<MachineInstr *> &OrigAft = AddedInstrMap[OrigMI].InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000932
933 // "added instructions" of the delayed instr
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000934 AddedInstrns &DelayAdI = AddedInstrMap[DelayedMI];
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000935
936 // "added after" instructions of the delayed instr
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000937 std::vector<MachineInstr *> &DelayedAft = DelayAdI.InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000938
939 // go thru all the "added after instructions" of the original instruction
940 // and append them to the "addded after instructions" of the delayed
941 // instructions
Chris Lattner697954c2002-01-20 22:54:45 +0000942 DelayedAft.insert(DelayedAft.end(), OrigAft.begin(), OrigAft.end());
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000943
944 // empty the "added after instructions" of the original instruction
945 OrigAft.clear();
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000946}
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000947
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000948//----------------------------------------------------------------------------
949// This method prints the code with registers after register allocation is
950// complete.
951//----------------------------------------------------------------------------
952void PhyRegAlloc::printMachineCode()
953{
954
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000955 cerr << "\n;************** Function " << Meth->getName()
Chris Lattner697954c2002-01-20 22:54:45 +0000956 << " *****************\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000957
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000958 for (Function::const_iterator BBI = Meth->begin(), BBE = Meth->end();
959 BBI != BBE; ++BBI) {
960 cerr << "\n"; printLabel(*BBI); cerr << ": ";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000961
962 // get the iterator for machine instructions
963 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
Vikram S. Adve48762092002-04-25 04:34:15 +0000964 MachineCodeForBasicBlock::iterator MII = MIVec.begin();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000965
966 // iterate over all the machine instructions in BB
Vikram S. Adve48762092002-04-25 04:34:15 +0000967 for( ; MII != MIVec.end(); ++MII) {
968 MachineInstr *const MInst = *MII;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000969
Chris Lattner697954c2002-01-20 22:54:45 +0000970 cerr << "\n\t";
971 cerr << TargetInstrDescriptors[MInst->getOpCode()].opCodeString;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000972
973 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000974 MachineOperand& Op = MInst->getOperand(OpNum);
975
976 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasanka97b8b442001-10-18 22:36:26 +0000977 Op.getOperandType() == MachineOperand::MO_CCRegister /*||
978 Op.getOperandType() == MachineOperand::MO_PCRelativeDisp*/ ) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000979
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000980 const Value *const Val = Op.getVRegValue () ;
Ruchira Sasankae727f852001-09-18 22:43:57 +0000981 // ****this code is temporary till NULL Values are fixed
982 if( ! Val ) {
Chris Lattner697954c2002-01-20 22:54:45 +0000983 cerr << "\t<*NULL*>";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000984 continue;
985 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000986
987 // if a label or a constant
Chris Lattnerdbe53042002-01-21 01:33:12 +0000988 if(isa<BasicBlock>(Val)) {
Chris Lattner697954c2002-01-20 22:54:45 +0000989 cerr << "\t"; printLabel( Op.getVRegValue () );
990 } else {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000991 // else it must be a register value
992 const int RegNum = Op.getAllocatedRegNum();
993
Chris Lattner697954c2002-01-20 22:54:45 +0000994 cerr << "\t" << "%" << MRI.getUnifiedRegName( RegNum );
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000995 if (Val->hasName() )
Chris Lattner697954c2002-01-20 22:54:45 +0000996 cerr << "(" << Val->getName() << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000997 else
Chris Lattner697954c2002-01-20 22:54:45 +0000998 cerr << "(" << Val << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000999
1000 if( Op.opIsDef() )
Chris Lattner697954c2002-01-20 22:54:45 +00001001 cerr << "*";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001002
1003 const LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
1004 if( LROfVal )
1005 if( LROfVal->hasSpillOffset() )
Chris Lattner697954c2002-01-20 22:54:45 +00001006 cerr << "$";
Ruchira Sasankae727f852001-09-18 22:43:57 +00001007 }
1008
1009 }
1010 else if(Op.getOperandType() == MachineOperand::MO_MachineRegister) {
Chris Lattner697954c2002-01-20 22:54:45 +00001011 cerr << "\t" << "%" << MRI.getUnifiedRegName(Op.getMachineRegNum());
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001012 }
1013
1014 else
Chris Lattner697954c2002-01-20 22:54:45 +00001015 cerr << "\t" << Op; // use dump field
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001016 }
1017
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001018
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001019
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001020 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
Chris Lattner0665a5f2002-02-05 01:43:49 +00001021 if( NumOfImpRefs > 0) {
Chris Lattner697954c2002-01-20 22:54:45 +00001022 cerr << "\tImplicit:";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001023
Chris Lattner0665a5f2002-02-05 01:43:49 +00001024 for(unsigned z=0; z < NumOfImpRefs; z++)
1025 cerr << RAV(MInst->getImplicitRef(z)) << "\t";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001026 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001027
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001028 } // for all machine instructions
1029
Chris Lattner697954c2002-01-20 22:54:45 +00001030 cerr << "\n";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001031
1032 } // for all BBs
1033
Chris Lattner697954c2002-01-20 22:54:45 +00001034 cerr << "\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001035}
1036
Ruchira Sasankae727f852001-09-18 22:43:57 +00001037
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001038#if 0
1039
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001040//----------------------------------------------------------------------------
1041//
1042//----------------------------------------------------------------------------
1043
1044void PhyRegAlloc::colorCallRetArgs()
1045{
1046
1047 CallRetInstrListType &CallRetInstList = LRI.getCallRetInstrList();
1048 CallRetInstrListType::const_iterator It = CallRetInstList.begin();
1049
1050 for( ; It != CallRetInstList.end(); ++It ) {
1051
Ruchira Sasankaa90e7702001-10-15 16:26:38 +00001052 const MachineInstr *const CRMI = *It;
1053 unsigned OpCode = CRMI->getOpCode();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001054
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001055 // get the added instructions for this Call/Ret instruciton
Chris Lattner0b0ffa02002-04-09 05:13:04 +00001056 AddedInstrns &AI = AddedInstrMap[CRMI];
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001057
Chris Lattner0b0ffa02002-04-09 05:13:04 +00001058 // Tmp stack positions are needed by some calls that have spilled args
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001059 // So reset it before we call each such method
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001060 //mcInfo.popAllTempValues(TM);
1061
Vikram S. Adve12af1642001-11-08 04:48:50 +00001062
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001063 if (TM.getInstrInfo().isCall(OpCode))
Chris Lattner0b0ffa02002-04-09 05:13:04 +00001064 MRI.colorCallArgs(CRMI, LRI, &AI, *this);
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001065 else if (TM.getInstrInfo().isReturn(OpCode))
Chris Lattner0b0ffa02002-04-09 05:13:04 +00001066 MRI.colorRetValue(CRMI, LRI, &AI);
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001067 else
1068 assert(0 && "Non Call/Ret instrn in CallRetInstrList\n");
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001069 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001070}
1071
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001072#endif
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001073
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001074//----------------------------------------------------------------------------
1075
1076//----------------------------------------------------------------------------
1077void PhyRegAlloc::colorIncomingArgs()
1078{
1079 const BasicBlock *const FirstBB = Meth->front();
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001080 const MachineInstr *FirstMI = FirstBB->getMachineInstrVec().front();
1081 assert(FirstMI && "No machine instruction in entry BB");
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001082
Vikram S. Adve48762092002-04-25 04:34:15 +00001083 MRI.colorMethodArgs(Meth, LRI, &AddedInstrAtEntry);
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001084}
1085
Ruchira Sasankae727f852001-09-18 22:43:57 +00001086
1087//----------------------------------------------------------------------------
1088// Used to generate a label for a basic block
1089//----------------------------------------------------------------------------
Chris Lattner697954c2002-01-20 22:54:45 +00001090void PhyRegAlloc::printLabel(const Value *const Val) {
1091 if (Val->hasName())
1092 cerr << Val->getName();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001093 else
Chris Lattner697954c2002-01-20 22:54:45 +00001094 cerr << "Label" << Val;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001095}
1096
1097
Ruchira Sasankae727f852001-09-18 22:43:57 +00001098//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001099// This method calls setSugColorUsable method of each live range. This
1100// will determine whether the suggested color of LR is really usable.
1101// A suggested color is not usable when the suggested color is volatile
1102// AND when there are call interferences
1103//----------------------------------------------------------------------------
1104
1105void PhyRegAlloc::markUnusableSugColors()
1106{
Chris Lattner697954c2002-01-20 22:54:45 +00001107 if(DEBUG_RA ) cerr << "\nmarking unusable suggested colors ...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001108
1109 // hash map iterator
1110 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
1111 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
1112
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001113 for(; HMI != HMIEnd ; ++HMI ) {
1114 if (HMI->first) {
1115 LiveRange *L = HMI->second; // get the LiveRange
1116 if (L) {
1117 if(L->hasSuggestedColor()) {
1118 int RCID = L->getRegClass()->getID();
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001119 if( MRI.isRegVolatile( RCID, L->getSuggestedColor()) &&
1120 L->isCallInterference() )
1121 L->setSuggestedColorUsable( false );
1122 else
1123 L->setSuggestedColorUsable( true );
1124 }
1125 } // if L->hasSuggestedColor()
1126 }
1127 } // for all LR's in hash map
1128}
1129
1130
1131
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001132//----------------------------------------------------------------------------
1133// The following method will set the stack offsets of the live ranges that
1134// are decided to be spillled. This must be called just after coloring the
1135// LRs using the graph coloring algo. For each live range that is spilled,
1136// this method allocate a new spill position on the stack.
1137//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001138
Chris Lattner37730942002-02-05 03:52:29 +00001139void PhyRegAlloc::allocateStackSpace4SpilledLRs() {
1140 if (DEBUG_RA) cerr << "\nsetting LR stack offsets ...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001141
Chris Lattner37730942002-02-05 03:52:29 +00001142 LiveRangeMapType::const_iterator HMI = LRI.getLiveRangeMap()->begin();
1143 LiveRangeMapType::const_iterator HMIEnd = LRI.getLiveRangeMap()->end();
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001144
Chris Lattner37730942002-02-05 03:52:29 +00001145 for( ; HMI != HMIEnd ; ++HMI) {
1146 if (HMI->first && HMI->second) {
1147 LiveRange *L = HMI->second; // get the LiveRange
1148 if (!L->hasColor()) // NOTE: ** allocating the size of long Type **
1149 L->setSpillOffFromFP(mcInfo.allocateSpilledValue(TM, Type::LongTy));
1150 }
1151 } // for all LR's in hash map
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001152}
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001153
1154
1155
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001156//----------------------------------------------------------------------------
Ruchira Sasankae727f852001-09-18 22:43:57 +00001157// The entry pont to Register Allocation
1158//----------------------------------------------------------------------------
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001159
1160void PhyRegAlloc::allocateRegisters()
1161{
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001162
1163 // make sure that we put all register classes into the RegClassList
1164 // before we call constructLiveRanges (now done in the constructor of
1165 // PhyRegAlloc class).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001166 //
1167 LRI.constructLiveRanges(); // create LR info
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001168
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001169 if (DEBUG_RA)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001170 LRI.printLiveRanges();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001171
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001172 createIGNodeListsAndIGs(); // create IGNode list and IGs
1173
1174 buildInterferenceGraphs(); // build IGs in all reg classes
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001175
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001176
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001177 if (DEBUG_RA) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001178 // print all LRs in all reg classes
1179 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1180 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001181
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001182 // print IGs in all register classes
1183 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1184 RegClassList[ rc ]->printIG();
1185 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001186
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001187
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001188 LRI.coalesceLRs(); // coalesce all live ranges
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001189
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +00001190
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001191 if( DEBUG_RA) {
1192 // print all LRs in all reg classes
1193 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1194 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001195
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001196 // print IGs in all register classes
1197 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1198 RegClassList[ rc ]->printIG();
1199 }
1200
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001201
1202 // mark un-usable suggested color before graph coloring algorithm.
1203 // When this is done, the graph coloring algo will not reserve
1204 // suggested color unnecessarily - they can be used by another LR
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001205 //
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001206 markUnusableSugColors();
1207
1208 // color all register classes using the graph coloring algo
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001209 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1210 RegClassList[ rc ]->colorAllRegs();
1211
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001212 // Atter grpah coloring, if some LRs did not receive a color (i.e, spilled)
1213 // a poistion for such spilled LRs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001214 //
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001215 allocateStackSpace4SpilledLRs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001216
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001217 mcInfo.popAllTempValues(TM); // TODO **Check
1218
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001219 // color incoming args - if the correct color was not received
1220 // insert code to copy to the correct register
1221 //
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001222 colorIncomingArgs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001223
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001224 // Now update the machine code with register names and add any
1225 // additional code inserted by the register allocator to the instruction
1226 // stream
1227 //
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001228 updateMachineCode();
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001229
Chris Lattner045e7c82001-09-19 16:26:23 +00001230 if (DEBUG_RA) {
Vikram S. Adve12af1642001-11-08 04:48:50 +00001231 MachineCodeForMethod::get(Meth).dump();
Chris Lattner045e7c82001-09-19 16:26:23 +00001232 printMachineCode(); // only for DEBUGGING
1233 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001234}
1235
Ruchira Sasankae727f852001-09-18 22:43:57 +00001236
1237