blob: bd0db117f0fed82e7b8324363461e5c02e4c4a63 [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
800 bool *IsColorUsedArr = RC->getIsColorUsedArr();
801
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
825 unsigned c; // find first unused color
826 for( c=0; c < NumAvailRegs; c++)
827 if( ! IsColorUsedArr[ c ] ) break;
828
829 if(c < NumAvailRegs)
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000830 return MRI.getUnifiedRegNum(RC->getID(), c);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000831 else
832 return -1;
833
834
835}
836
837
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000838//----------------------------------------------------------------------------
839// Get any other register in a register class, other than what is used
840// by operands of a machine instruction. Returns the unified reg number.
841//----------------------------------------------------------------------------
842int PhyRegAlloc::getUniRegNotUsedByThisInst(RegClass *RC,
843 const MachineInstr *MInst) {
844
845 bool *IsColorUsedArr = RC->getIsColorUsedArr();
846 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
847
848
849 for(unsigned i=0; i < NumAvailRegs ; i++) // Reset array
850 IsColorUsedArr[i] = false;
851
852 setRelRegsUsedByThisInst(RC, MInst);
853
854 unsigned c; // find first unused color
855 for( c=0; c < RC->getNumOfAvailRegs(); c++)
856 if( ! IsColorUsedArr[ c ] ) break;
857
858 if(c < NumAvailRegs)
859 return MRI.getUnifiedRegNum(RC->getID(), c);
860 else
861 assert( 0 && "FATAL: No free register could be found in reg class!!");
Chris Lattner697954c2002-01-20 22:54:45 +0000862 return 0;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000863}
864
865
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000866//----------------------------------------------------------------------------
867// This method modifies the IsColorUsedArr of the register class passed to it.
868// It sets the bits corresponding to the registers used by this machine
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000869// instructions. Both explicit and implicit operands are set.
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000870//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000871void PhyRegAlloc::setRelRegsUsedByThisInst(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000872 const MachineInstr *MInst ) {
873
874 bool *IsColorUsedArr = RC->getIsColorUsedArr();
875
876 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
877
878 const MachineOperand& Op = MInst->getOperand(OpNum);
879
880 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000881 Op.getOperandType() == MachineOperand::MO_CCRegister ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000882
883 const Value *const Val = Op.getVRegValue();
884
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000885 if( Val )
886 if( MRI.getRegClassIDOfValue(Val) == RC->getID() ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000887 int Reg;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000888 if( (Reg=Op.getAllocatedRegNum()) != -1) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000889 IsColorUsedArr[ Reg ] = true;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000890 }
891 else {
892 // it is possilbe that this operand still is not marked with
893 // a register but it has a LR and that received a color
894
895 LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
896 if( LROfVal)
897 if( LROfVal->hasColor() )
898 IsColorUsedArr[ LROfVal->getColor() ] = true;
899 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000900
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000901 } // if reg classes are the same
902 }
903 else if (Op.getOperandType() == MachineOperand::MO_MachineRegister) {
904 IsColorUsedArr[ Op.getMachineRegNum() ] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000905 }
906 }
907
908 // If there are implicit references, mark them as well
909
910 for(unsigned z=0; z < MInst->getNumImplicitRefs(); z++) {
911
912 LiveRange *const LRofImpRef =
913 LRI.getLiveRangeForValue( MInst->getImplicitRef(z) );
Chris Lattner697954c2002-01-20 22:54:45 +0000914
915 if(LRofImpRef && LRofImpRef->hasColor())
916 IsColorUsedArr[LRofImpRef->getColor()] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000917 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000918}
919
920
921
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000922
923
924
925
926
927//----------------------------------------------------------------------------
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000928// If there are delay slots for an instruction, the instructions
929// added after it must really go after the delayed instruction(s).
930// So, we move the InstrAfter of that instruction to the
931// corresponding delayed instruction using the following method.
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000932
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000933//----------------------------------------------------------------------------
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000934void PhyRegAlloc::move2DelayedInstr(const MachineInstr *OrigMI,
935 const MachineInstr *DelayedMI) {
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000936
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000937 // "added after" instructions of the original instr
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000938 std::vector<MachineInstr *> &OrigAft = AddedInstrMap[OrigMI].InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000939
940 // "added instructions" of the delayed instr
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000941 AddedInstrns &DelayAdI = AddedInstrMap[DelayedMI];
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000942
943 // "added after" instructions of the delayed instr
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000944 std::vector<MachineInstr *> &DelayedAft = DelayAdI.InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000945
946 // go thru all the "added after instructions" of the original instruction
947 // and append them to the "addded after instructions" of the delayed
948 // instructions
Chris Lattner697954c2002-01-20 22:54:45 +0000949 DelayedAft.insert(DelayedAft.end(), OrigAft.begin(), OrigAft.end());
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000950
951 // empty the "added after instructions" of the original instruction
952 OrigAft.clear();
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000953}
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000954
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000955//----------------------------------------------------------------------------
956// This method prints the code with registers after register allocation is
957// complete.
958//----------------------------------------------------------------------------
959void PhyRegAlloc::printMachineCode()
960{
961
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000962 cerr << "\n;************** Function " << Meth->getName()
Chris Lattner697954c2002-01-20 22:54:45 +0000963 << " *****************\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000964
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000965 for (Function::const_iterator BBI = Meth->begin(), BBE = Meth->end();
966 BBI != BBE; ++BBI) {
967 cerr << "\n"; printLabel(*BBI); cerr << ": ";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000968
969 // get the iterator for machine instructions
970 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
Vikram S. Adve48762092002-04-25 04:34:15 +0000971 MachineCodeForBasicBlock::iterator MII = MIVec.begin();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000972
973 // iterate over all the machine instructions in BB
Vikram S. Adve48762092002-04-25 04:34:15 +0000974 for( ; MII != MIVec.end(); ++MII) {
975 MachineInstr *const MInst = *MII;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000976
Chris Lattner697954c2002-01-20 22:54:45 +0000977 cerr << "\n\t";
978 cerr << TargetInstrDescriptors[MInst->getOpCode()].opCodeString;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000979
980 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000981 MachineOperand& Op = MInst->getOperand(OpNum);
982
983 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasanka97b8b442001-10-18 22:36:26 +0000984 Op.getOperandType() == MachineOperand::MO_CCRegister /*||
985 Op.getOperandType() == MachineOperand::MO_PCRelativeDisp*/ ) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000986
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000987 const Value *const Val = Op.getVRegValue () ;
Ruchira Sasankae727f852001-09-18 22:43:57 +0000988 // ****this code is temporary till NULL Values are fixed
989 if( ! Val ) {
Chris Lattner697954c2002-01-20 22:54:45 +0000990 cerr << "\t<*NULL*>";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000991 continue;
992 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000993
994 // if a label or a constant
Chris Lattnerdbe53042002-01-21 01:33:12 +0000995 if(isa<BasicBlock>(Val)) {
Chris Lattner697954c2002-01-20 22:54:45 +0000996 cerr << "\t"; printLabel( Op.getVRegValue () );
997 } else {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000998 // else it must be a register value
999 const int RegNum = Op.getAllocatedRegNum();
1000
Chris Lattner697954c2002-01-20 22:54:45 +00001001 cerr << "\t" << "%" << MRI.getUnifiedRegName( RegNum );
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001002 if (Val->hasName() )
Chris Lattner697954c2002-01-20 22:54:45 +00001003 cerr << "(" << Val->getName() << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001004 else
Chris Lattner697954c2002-01-20 22:54:45 +00001005 cerr << "(" << Val << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001006
1007 if( Op.opIsDef() )
Chris Lattner697954c2002-01-20 22:54:45 +00001008 cerr << "*";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001009
1010 const LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
1011 if( LROfVal )
1012 if( LROfVal->hasSpillOffset() )
Chris Lattner697954c2002-01-20 22:54:45 +00001013 cerr << "$";
Ruchira Sasankae727f852001-09-18 22:43:57 +00001014 }
1015
1016 }
1017 else if(Op.getOperandType() == MachineOperand::MO_MachineRegister) {
Chris Lattner697954c2002-01-20 22:54:45 +00001018 cerr << "\t" << "%" << MRI.getUnifiedRegName(Op.getMachineRegNum());
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001019 }
1020
1021 else
Chris Lattner697954c2002-01-20 22:54:45 +00001022 cerr << "\t" << Op; // use dump field
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001023 }
1024
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001025
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001026
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001027 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
Chris Lattner0665a5f2002-02-05 01:43:49 +00001028 if( NumOfImpRefs > 0) {
Chris Lattner697954c2002-01-20 22:54:45 +00001029 cerr << "\tImplicit:";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001030
Chris Lattner0665a5f2002-02-05 01:43:49 +00001031 for(unsigned z=0; z < NumOfImpRefs; z++)
1032 cerr << RAV(MInst->getImplicitRef(z)) << "\t";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001033 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001034
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001035 } // for all machine instructions
1036
Chris Lattner697954c2002-01-20 22:54:45 +00001037 cerr << "\n";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001038
1039 } // for all BBs
1040
Chris Lattner697954c2002-01-20 22:54:45 +00001041 cerr << "\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001042}
1043
Ruchira Sasankae727f852001-09-18 22:43:57 +00001044
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001045#if 0
1046
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001047//----------------------------------------------------------------------------
1048//
1049//----------------------------------------------------------------------------
1050
1051void PhyRegAlloc::colorCallRetArgs()
1052{
1053
1054 CallRetInstrListType &CallRetInstList = LRI.getCallRetInstrList();
1055 CallRetInstrListType::const_iterator It = CallRetInstList.begin();
1056
1057 for( ; It != CallRetInstList.end(); ++It ) {
1058
Ruchira Sasankaa90e7702001-10-15 16:26:38 +00001059 const MachineInstr *const CRMI = *It;
1060 unsigned OpCode = CRMI->getOpCode();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001061
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001062 // get the added instructions for this Call/Ret instruciton
Chris Lattner0b0ffa02002-04-09 05:13:04 +00001063 AddedInstrns &AI = AddedInstrMap[CRMI];
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001064
Chris Lattner0b0ffa02002-04-09 05:13:04 +00001065 // Tmp stack positions are needed by some calls that have spilled args
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001066 // So reset it before we call each such method
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001067 //mcInfo.popAllTempValues(TM);
1068
Vikram S. Adve12af1642001-11-08 04:48:50 +00001069
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001070 if (TM.getInstrInfo().isCall(OpCode))
Chris Lattner0b0ffa02002-04-09 05:13:04 +00001071 MRI.colorCallArgs(CRMI, LRI, &AI, *this);
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001072 else if (TM.getInstrInfo().isReturn(OpCode))
Chris Lattner0b0ffa02002-04-09 05:13:04 +00001073 MRI.colorRetValue(CRMI, LRI, &AI);
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001074 else
1075 assert(0 && "Non Call/Ret instrn in CallRetInstrList\n");
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001076 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001077}
1078
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001079#endif
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001080
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001081//----------------------------------------------------------------------------
1082
1083//----------------------------------------------------------------------------
1084void PhyRegAlloc::colorIncomingArgs()
1085{
1086 const BasicBlock *const FirstBB = Meth->front();
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001087 const MachineInstr *FirstMI = FirstBB->getMachineInstrVec().front();
1088 assert(FirstMI && "No machine instruction in entry BB");
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001089
Vikram S. Adve48762092002-04-25 04:34:15 +00001090 MRI.colorMethodArgs(Meth, LRI, &AddedInstrAtEntry);
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001091}
1092
Ruchira Sasankae727f852001-09-18 22:43:57 +00001093
1094//----------------------------------------------------------------------------
1095// Used to generate a label for a basic block
1096//----------------------------------------------------------------------------
Chris Lattner697954c2002-01-20 22:54:45 +00001097void PhyRegAlloc::printLabel(const Value *const Val) {
1098 if (Val->hasName())
1099 cerr << Val->getName();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001100 else
Chris Lattner697954c2002-01-20 22:54:45 +00001101 cerr << "Label" << Val;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001102}
1103
1104
Ruchira Sasankae727f852001-09-18 22:43:57 +00001105//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001106// This method calls setSugColorUsable method of each live range. This
1107// will determine whether the suggested color of LR is really usable.
1108// A suggested color is not usable when the suggested color is volatile
1109// AND when there are call interferences
1110//----------------------------------------------------------------------------
1111
1112void PhyRegAlloc::markUnusableSugColors()
1113{
Chris Lattner697954c2002-01-20 22:54:45 +00001114 if(DEBUG_RA ) cerr << "\nmarking unusable suggested colors ...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001115
1116 // hash map iterator
1117 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
1118 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
1119
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001120 for(; HMI != HMIEnd ; ++HMI ) {
1121 if (HMI->first) {
1122 LiveRange *L = HMI->second; // get the LiveRange
1123 if (L) {
1124 if(L->hasSuggestedColor()) {
1125 int RCID = L->getRegClass()->getID();
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001126 if( MRI.isRegVolatile( RCID, L->getSuggestedColor()) &&
1127 L->isCallInterference() )
1128 L->setSuggestedColorUsable( false );
1129 else
1130 L->setSuggestedColorUsable( true );
1131 }
1132 } // if L->hasSuggestedColor()
1133 }
1134 } // for all LR's in hash map
1135}
1136
1137
1138
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001139//----------------------------------------------------------------------------
1140// The following method will set the stack offsets of the live ranges that
1141// are decided to be spillled. This must be called just after coloring the
1142// LRs using the graph coloring algo. For each live range that is spilled,
1143// this method allocate a new spill position on the stack.
1144//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001145
Chris Lattner37730942002-02-05 03:52:29 +00001146void PhyRegAlloc::allocateStackSpace4SpilledLRs() {
1147 if (DEBUG_RA) cerr << "\nsetting LR stack offsets ...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001148
Chris Lattner37730942002-02-05 03:52:29 +00001149 LiveRangeMapType::const_iterator HMI = LRI.getLiveRangeMap()->begin();
1150 LiveRangeMapType::const_iterator HMIEnd = LRI.getLiveRangeMap()->end();
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001151
Chris Lattner37730942002-02-05 03:52:29 +00001152 for( ; HMI != HMIEnd ; ++HMI) {
1153 if (HMI->first && HMI->second) {
1154 LiveRange *L = HMI->second; // get the LiveRange
1155 if (!L->hasColor()) // NOTE: ** allocating the size of long Type **
1156 L->setSpillOffFromFP(mcInfo.allocateSpilledValue(TM, Type::LongTy));
1157 }
1158 } // for all LR's in hash map
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001159}
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001160
1161
1162
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001163//----------------------------------------------------------------------------
Ruchira Sasankae727f852001-09-18 22:43:57 +00001164// The entry pont to Register Allocation
1165//----------------------------------------------------------------------------
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001166
1167void PhyRegAlloc::allocateRegisters()
1168{
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001169
1170 // make sure that we put all register classes into the RegClassList
1171 // before we call constructLiveRanges (now done in the constructor of
1172 // PhyRegAlloc class).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001173 //
1174 LRI.constructLiveRanges(); // create LR info
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001175
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001176 if (DEBUG_RA)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001177 LRI.printLiveRanges();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001178
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001179 createIGNodeListsAndIGs(); // create IGNode list and IGs
1180
1181 buildInterferenceGraphs(); // build IGs in all reg classes
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001182
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001183
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001184 if (DEBUG_RA) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001185 // 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 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001193
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001194
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001195 LRI.coalesceLRs(); // coalesce all live ranges
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001196
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +00001197
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001198 if( DEBUG_RA) {
1199 // print all LRs in all reg classes
1200 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1201 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001202
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001203 // print IGs in all register classes
1204 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1205 RegClassList[ rc ]->printIG();
1206 }
1207
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001208
1209 // mark un-usable suggested color before graph coloring algorithm.
1210 // When this is done, the graph coloring algo will not reserve
1211 // suggested color unnecessarily - they can be used by another LR
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001212 //
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001213 markUnusableSugColors();
1214
1215 // color all register classes using the graph coloring algo
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001216 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1217 RegClassList[ rc ]->colorAllRegs();
1218
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001219 // Atter grpah coloring, if some LRs did not receive a color (i.e, spilled)
1220 // a poistion for such spilled LRs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001221 //
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001222 allocateStackSpace4SpilledLRs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001223
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001224 mcInfo.popAllTempValues(TM); // TODO **Check
1225
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001226 // color incoming args - if the correct color was not received
1227 // insert code to copy to the correct register
1228 //
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001229 colorIncomingArgs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001230
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001231 // Now update the machine code with register names and add any
1232 // additional code inserted by the register allocator to the instruction
1233 // stream
1234 //
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001235 updateMachineCode();
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001236
Chris Lattner045e7c82001-09-19 16:26:23 +00001237 if (DEBUG_RA) {
Vikram S. Adve12af1642001-11-08 04:48:50 +00001238 MachineCodeForMethod::get(Meth).dump();
Chris Lattner045e7c82001-09-19 16:26:23 +00001239 printMachineCode(); // only for DEBUGGING
1240 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001241}
1242
Ruchira Sasankae727f852001-09-18 22:43:57 +00001243
1244