blob: e884463ae29fc451ec4a6cebcf6cd85cc86e7040 [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;
Anand Shuklacfb22d32002-06-25 20:55:50 +000031using std::vector;
Vikram S. Adve12af1642001-11-08 04:48:50 +000032
Chris Lattner70e60cb2002-05-22 17:08:27 +000033RegAllocDebugLevel_t DEBUG_RA;
34static cl::Enum<RegAllocDebugLevel_t> DEBUG_RA_c(DEBUG_RA, "dregalloc",
35 cl::Hidden,
Chris Lattner045e7c82001-09-19 16:26:23 +000036 "enable register allocation debugging information",
37 clEnumValN(RA_DEBUG_None , "n", "disable debug output"),
38 clEnumValN(RA_DEBUG_Normal , "y", "enable debug output"),
39 clEnumValN(RA_DEBUG_Verbose, "v", "enable extra debug output"), 0);
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000040
41
Chris Lattner2f9b28e2002-02-04 15:54:09 +000042//----------------------------------------------------------------------------
43// RegisterAllocation pass front end...
44//----------------------------------------------------------------------------
45namespace {
Chris Lattnerf57b8452002-04-27 06:56:12 +000046 class RegisterAllocator : public FunctionPass {
Chris Lattner2f9b28e2002-02-04 15:54:09 +000047 TargetMachine &Target;
48 public:
49 inline RegisterAllocator(TargetMachine &T) : Target(T) {}
Chris Lattner96c466b2002-04-29 14:57:45 +000050
51 const char *getPassName() const { return "Register Allocation"; }
Chris Lattner6dd98a62002-02-04 00:33:08 +000052
Chris Lattner7e708292002-06-25 16:13:24 +000053 bool runOnFunction(Function &F) {
Chris Lattner2f9b28e2002-02-04 15:54:09 +000054 if (DEBUG_RA)
Chris Lattner7e708292002-06-25 16:13:24 +000055 cerr << "\n********* Function "<< F.getName() << " ***********\n";
Chris Lattner2f9b28e2002-02-04 15:54:09 +000056
Chris Lattner7e708292002-06-25 16:13:24 +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 Lattner7e708292002-06-25 16:13:24 +000090 for (unsigned 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 Lattner7e708292002-06-25 16:13:24 +0000100 for ( unsigned rc=0; rc < NumOfRegClasses; rc++)
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000101 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) {
Chris Lattner7e708292002-06-25 16:13:24 +0000123 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 Lattner7e708292002-06-25 16:13:24 +0000131 if (!(L->getUserIGNode()) ) {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000132 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
Chris Lattner7e708292002-06-25 16:13:24 +0000141 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000142 RegClassList[rc]->createInterferenceGraph();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000143
Chris Lattner7e708292002-06-25 16:13:24 +0000144 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 //
Chris Lattner7e708292002-06-25 16:13:24 +0000174 for ( ; LIt != LVSet->end(); ++LIt) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000175
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) {
Chris Lattner7e708292002-06-25 16:13:24 +0000187 if (LROfDef == LROfVar) // do not set interf for same LR
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000188 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
Chris Lattner7e708292002-06-25 16:13:24 +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 //
Chris Lattner7e708292002-06-25 16:13:24 +0000222 for ( ; LIt != LVSetAft->end(); ++LIt) {
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000223
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
Chris Lattner7e708292002-06-25 16:13:24 +0000228 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 //
Chris Lattner7e708292002-06-25 16:13:24 +0000236 if (LR ) {
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000237 LR->setCallInterference();
Chris Lattner7e708292002-06-25 16:13:24 +0000238 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.
Chris Lattner7e708292002-06-25 16:13:24 +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 Lattner7e708292002-06-25 16:13:24 +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 Lattner7e708292002-06-25 16:13:24 +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 //
Chris Lattner7e708292002-06-25 16:13:24 +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 //
Chris Lattner7e708292002-06-25 16:13:24 +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 Lattner7e708292002-06-25 16:13:24 +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
Chris Lattner7e708292002-06-25 16:13:24 +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();
Chris Lattner7e708292002-06-25 16:13:24 +0000342 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
Chris Lattner7e708292002-06-25 16:13:24 +0000358 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;
Chris Lattner7e708292002-06-25 16:13:24 +0000383 for (++It2; It2 != ItE; ++It2) {
Chris Lattner2f898d22002-02-05 06:02:59 +0000384 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
Chris Lattner7e708292002-06-25 16:13:24 +0000390 if (RCOfOp1 == RCOfOp2 ){
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000391 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 Lattner7e708292002-06-25 16:13:24 +0000412 const ValueSet &InSet = LVI->getInSetOfBB(&Meth->front());
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000413
Chris Lattner7e708292002-06-25 16:13:24 +0000414 for (Function::const_aiterator AI = Meth->abegin(); AI != Meth->aend(); ++AI) {
415 // add interferences between args and LVars at start
416 addInterference(AI, &InSet, false);
417
418 if (DEBUG_RA > 1)
419 cerr << " - %% adding interference for argument " << RAV(AI) << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000420 }
421}
422
423
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000424//----------------------------------------------------------------------------
425// This method is called after register allocation is complete to set the
426// allocated reisters in the machine code. This code will add register numbers
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000427// to MachineOperands that contain a Value. Also it calls target specific
428// methods to produce caller saving instructions. At the end, it adds all
429// additional instructions produced by the register allocator to the
430// instruction stream.
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000431//----------------------------------------------------------------------------
Vikram S. Adve48762092002-04-25 04:34:15 +0000432
433//-----------------------------
434// Utility functions used below
435//-----------------------------
436inline void
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000437PrependInstructions(vector<MachineInstr *> &IBef,
Vikram S. Adve48762092002-04-25 04:34:15 +0000438 MachineCodeForBasicBlock& MIVec,
439 MachineCodeForBasicBlock::iterator& MII,
440 const std::string& msg)
441{
442 if (!IBef.empty())
443 {
444 MachineInstr* OrigMI = *MII;
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000445 std::vector<MachineInstr *>::iterator AdIt;
Vikram S. Adve48762092002-04-25 04:34:15 +0000446 for (AdIt = IBef.begin(); AdIt != IBef.end() ; ++AdIt)
447 {
448 if (DEBUG_RA) {
449 if (OrigMI) cerr << "For MInst: " << *OrigMI;
450 cerr << msg << " PREPENDed instr: " << **AdIt << "\n";
451 }
452 MII = MIVec.insert(MII, *AdIt);
453 ++MII;
454 }
455 }
456}
457
458inline void
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000459AppendInstructions(std::vector<MachineInstr *> &IAft,
Vikram S. Adve48762092002-04-25 04:34:15 +0000460 MachineCodeForBasicBlock& MIVec,
461 MachineCodeForBasicBlock::iterator& MII,
462 const std::string& msg)
463{
464 if (!IAft.empty())
465 {
466 MachineInstr* OrigMI = *MII;
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000467 std::vector<MachineInstr *>::iterator AdIt;
Chris Lattner7e708292002-06-25 16:13:24 +0000468 for ( AdIt = IAft.begin(); AdIt != IAft.end() ; ++AdIt )
Vikram S. Adve48762092002-04-25 04:34:15 +0000469 {
Chris Lattner7e708292002-06-25 16:13:24 +0000470 if (DEBUG_RA) {
Vikram S. Adve48762092002-04-25 04:34:15 +0000471 if (OrigMI) cerr << "For MInst: " << *OrigMI;
472 cerr << msg << " APPENDed instr: " << **AdIt << "\n";
473 }
474 ++MII; // insert before the next instruction
475 MII = MIVec.insert(MII, *AdIt);
476 }
477 }
478}
479
480
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000481void PhyRegAlloc::updateMachineCode()
482{
Chris Lattner7e708292002-06-25 16:13:24 +0000483 MachineCodeForBasicBlock& MIVec = Meth->getEntryNode().getMachineInstrVec();
Vikram S. Adve48762092002-04-25 04:34:15 +0000484
Chris Lattner7e708292002-06-25 16:13:24 +0000485 // Insert any instructions needed at method entry
486 MachineCodeForBasicBlock::iterator MII = MIVec.begin();
487 PrependInstructions(AddedInstrAtEntry.InstrnsBefore, MIVec, MII,
488 "At function entry: \n");
489 assert(AddedInstrAtEntry.InstrnsAfter.empty() &&
490 "InstrsAfter should be unnecessary since we are just inserting at "
491 "the function entry point here.");
Vikram S. Adve48762092002-04-25 04:34:15 +0000492
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000493 for (Function::const_iterator BBI = Meth->begin(), BBE = Meth->end();
494 BBI != BBE; ++BBI) {
Vikram S. Adve48762092002-04-25 04:34:15 +0000495
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000496 // iterate over all the machine instructions in BB
Chris Lattner7e708292002-06-25 16:13:24 +0000497 MachineCodeForBasicBlock &MIVec = BBI->getMachineInstrVec();
498 for (MachineCodeForBasicBlock::iterator MII = MIVec.begin();
Vikram S. Adve48762092002-04-25 04:34:15 +0000499 MII != MIVec.end(); ++MII) {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000500
Vikram S. Adve48762092002-04-25 04:34:15 +0000501 MachineInstr *MInst = *MII;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000502
503 unsigned Opcode = MInst->getOpCode();
504
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000505 // do not process Phis
Vikram S. Adve23a4c8f2002-03-18 03:37:19 +0000506 if (TM.getInstrInfo().isDummyPhiInstr(Opcode))
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000507 continue;
508
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000509 // Now insert speical instructions (if necessary) for call/return
510 // instructions.
511 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000512 if (TM.getInstrInfo().isCall(Opcode) ||
513 TM.getInstrInfo().isReturn(Opcode)) {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000514
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000515 AddedInstrns &AI = AddedInstrMap[MInst];
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000516
517 // Tmp stack poistions are needed by some calls that have spilled args
518 // So reset it before we call each such method
Ruchira Sasanka6a3db8c2002-01-07 21:09:06 +0000519 //
520 mcInfo.popAllTempValues(TM);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000521
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000522 if (TM.getInstrInfo().isCall(Opcode))
Chris Lattner7e708292002-06-25 16:13:24 +0000523 MRI.colorCallArgs(MInst, LRI, &AI, *this, BBI);
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000524 else if (TM.getInstrInfo().isReturn(Opcode))
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000525 MRI.colorRetValue(MInst, LRI, &AI);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000526 }
527
528
529 /* -- Using above code instead of this
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000530
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000531 // if this machine instr is call, insert caller saving code
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000532
Chris Lattner7e708292002-06-25 16:13:24 +0000533 if ((TM.getInstrInfo()).isCall( MInst->getOpCode()) )
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000534 MRI.insertCallerSavingCode(MInst, *BBI, *this );
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000535
536 */
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000537
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000538
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000539 // reset the stack offset for temporary variables since we may
540 // need that to spill
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000541 // mcInfo.popAllTempValues(TM);
Ruchira Sasankaf90870f2001-11-15 22:02:06 +0000542 // TODO ** : do later
Vikram S. Adve12af1642001-11-08 04:48:50 +0000543
Chris Lattner7e708292002-06-25 16:13:24 +0000544 //for (MachineInstr::val_const_op_iterator OpI(MInst);!OpI.done();++OpI) {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000545
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000546
547 // Now replace set the registers for operands in the machine instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000548 //
Chris Lattner7e708292002-06-25 16:13:24 +0000549 for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000550
551 MachineOperand& Op = MInst->getOperand(OpNum);
552
Chris Lattner7e708292002-06-25 16:13:24 +0000553 if (Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000554 Op.getOperandType() == MachineOperand::MO_CCRegister) {
555
556 const Value *const Val = Op.getVRegValue();
557
558 // delete this condition checking later (must assert if Val is null)
Chris Lattner7e708292002-06-25 16:13:24 +0000559 if (!Val) {
Chris Lattner045e7c82001-09-19 16:26:23 +0000560 if (DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000561 cerr << "Warning: NULL Value found for operand\n";
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000562 continue;
563 }
564 assert( Val && "Value is NULL");
565
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000566 LiveRange *const LR = LRI.getLiveRangeForValue(Val);
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000567
Chris Lattner7e708292002-06-25 16:13:24 +0000568 if (!LR ) {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000569
570 // nothing to worry if it's a const or a label
571
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000572 if (DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000573 cerr << "*NO LR for operand : " << Op ;
574 cerr << " [reg:" << Op.getAllocatedRegNum() << "]";
575 cerr << " in inst:\t" << *MInst << "\n";
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000576 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000577
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000578 // if register is not allocated, mark register as invalid
Chris Lattner7e708292002-06-25 16:13:24 +0000579 if (Op.getAllocatedRegNum() == -1)
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000580 Op.setRegForValue( MRI.getInvalidRegNum());
Ruchira Sasankae727f852001-09-18 22:43:57 +0000581
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000582
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000583 continue;
584 }
585
586 unsigned RCID = (LR->getRegClass())->getID();
587
Chris Lattner7e708292002-06-25 16:13:24 +0000588 if (LR->hasColor() ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000589 Op.setRegForValue( MRI.getUnifiedRegNum(RCID, LR->getColor()) );
590 }
591 else {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000592
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000593 // LR did NOT receive a color (register). Now, insert spill code
594 // for spilled opeands in this machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000595
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000596 //assert(0 && "LR must be spilled");
Chris Lattner7e708292002-06-25 16:13:24 +0000597 insertCode4SpilledLR(LR, MInst, BBI, OpNum );
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000598
599 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000600 }
601
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000602 } // for each operand
603
604
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000605 // Now add instructions that the register allocator inserts before/after
606 // this machine instructions (done only for calls/rets/incoming args)
607 // We do this here, to ensure that spill for an instruction is inserted
608 // closest as possible to an instruction (see above insertCode4Spill...)
609 //
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000610 // If there are instructions to be added, *before* this machine
611 // instruction, add them now.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000612 //
Chris Lattner7e708292002-06-25 16:13:24 +0000613 if (AddedInstrMap.count(MInst)) {
Vikram S. Adve48762092002-04-25 04:34:15 +0000614 PrependInstructions(AddedInstrMap[MInst].InstrnsBefore, MIVec, MII,"");
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000615 }
Vikram S. Adve48762092002-04-25 04:34:15 +0000616
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000617 // If there are instructions to be added *after* this machine
618 // instruction, add them now
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000619 //
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000620 if (!AddedInstrMap[MInst].InstrnsAfter.empty()) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000621
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000622 // if there are delay slots for this instruction, the instructions
623 // added after it must really go after the delayed instruction(s)
624 // So, we move the InstrAfter of the current instruction to the
625 // corresponding delayed instruction
626
627 unsigned delay;
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000628 if ((delay=TM.getInstrInfo().getNumDelaySlots(MInst->getOpCode())) >0){
Vikram S. Adve48762092002-04-25 04:34:15 +0000629 move2DelayedInstr(MInst, *(MII+delay) );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000630
Chris Lattner7e708292002-06-25 16:13:24 +0000631 if (DEBUG_RA) cerr<< "\nMoved an added instr after the delay slot";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000632 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000633
634 else {
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000635 // Here we can add the "instructions after" to the current
636 // instruction since there are no delay slots for this instruction
Vikram S. Adve48762092002-04-25 04:34:15 +0000637 AppendInstructions(AddedInstrMap[MInst].InstrnsAfter, MIVec, MII,"");
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000638 } // if not delay
639
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000640 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000641
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000642 } // for each machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000643 }
644}
645
646
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000647
648//----------------------------------------------------------------------------
649// This method inserts spill code for AN operand whose LR was spilled.
650// This method may be called several times for a single machine instruction
651// if it contains many spilled operands. Each time it is called, it finds
652// a register which is not live at that instruction and also which is not
653// used by other spilled operands of the same instruction. Then it uses
654// this register temporarily to accomodate the spilled value.
655//----------------------------------------------------------------------------
656void PhyRegAlloc::insertCode4SpilledLR(const LiveRange *LR,
657 MachineInstr *MInst,
658 const BasicBlock *BB,
659 const unsigned OpNum) {
660
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000661 assert(! TM.getInstrInfo().isCall(MInst->getOpCode()) &&
662 (! TM.getInstrInfo().isReturn(MInst->getOpCode())) &&
663 "Arg of a call/ret must be handled elsewhere");
664
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000665 MachineOperand& Op = MInst->getOperand(OpNum);
666 bool isDef = MInst->operandIsDefined(OpNum);
667 unsigned RegType = MRI.getRegType( LR );
668 int SpillOff = LR->getSpillOffFromFP();
669 RegClass *RC = LR->getRegClass();
Chris Lattner748697d2002-02-05 04:20:12 +0000670 const ValueSet &LVSetBef = LVI->getLiveVarSetBeforeMInst(MInst, BB);
Vikram S. Adve00521d72001-11-12 23:26:35 +0000671
Chris Lattner697954c2002-01-20 22:54:45 +0000672 mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000673
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000674 MachineInstr *MIBef=NULL, *MIAft=NULL;
675 vector<MachineInstr*> AdIMid;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000676
Chris Lattner748697d2002-02-05 04:20:12 +0000677 int TmpRegU = getUsableUniRegAtMI(RC, RegType, MInst,&LVSetBef, MIBef, MIAft);
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000678
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000679 // get the added instructions for this instruciton
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000680 AddedInstrns &AI = AddedInstrMap[MInst];
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000681
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000682 if (!isDef) {
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000683 // for a USE, we have to load the value of LR from stack to a TmpReg
684 // and use the TmpReg as one operand of instruction
685
686 // actual loading instruction
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000687 MRI.cpMem2RegMI(MRI.getFramePointer(), SpillOff, TmpRegU,RegType, AdIMid);
688 AI.InstrnsBefore.insert(AI.InstrnsBefore.end(),
689 AdIMid.begin(), AdIMid.end());
690
Chris Lattner7e708292002-06-25 16:13:24 +0000691 if (MIBef)
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000692 AI.InstrnsBefore.push_back(MIBef);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000693
Chris Lattner7e708292002-06-25 16:13:24 +0000694 if (MIAft)
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000695 AI.InstrnsAfter.insert(AI.InstrnsAfter.begin(), MIAft);
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000696
Chris Lattner296b7732002-02-05 02:52:05 +0000697 } else { // if this is a Def
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000698 // for a DEF, we have to store the value produced by this instruction
699 // on the stack position allocated for this LR
700
701 // actual storing instruction
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000702 MRI.cpReg2MemMI(TmpRegU, MRI.getFramePointer(), SpillOff,RegType, AdIMid);
703
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000704 if (MIBef)
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000705 AI.InstrnsBefore.push_back(MIBef);
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000706
707 AI.InstrnsAfter.insert(AI.InstrnsAfter.begin(),
708 AdIMid.begin(), AdIMid.end());
709
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000710 if (MIAft)
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000711 AI.InstrnsAfter.insert(AI.InstrnsAfter.begin(), MIAft);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000712
713 } // if !DEF
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000714
Chris Lattner7e708292002-06-25 16:13:24 +0000715 if (DEBUG_RA) {
716 cerr << "\nFor Inst " << *MInst;
717 cerr << " - SPILLED LR: "; printSet(*LR);
718 cerr << "\n - Added Instructions:";
719 if (MIBef) cerr << *MIBef;
720 for (vector<MachineInstr*>::const_iterator II=AdIMid.begin();
721 II != AdIMid.end(); ++II)
722 cerr << **II;
723 if (MIAft) cerr << *MIAft;
724 }
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000725
Chris Lattner296b7732002-02-05 02:52:05 +0000726 Op.setRegForValue(TmpRegU); // set the opearnd
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000727}
728
729
730
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000731//----------------------------------------------------------------------------
732// We can use the following method to get a temporary register to be used
733// BEFORE any given machine instruction. If there is a register available,
734// this method will simply return that register and set MIBef = MIAft = NULL.
735// Otherwise, it will return a register and MIAft and MIBef will contain
736// two instructions used to free up this returned register.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000737// Returned register number is the UNIFIED register number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000738//----------------------------------------------------------------------------
739
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000740int PhyRegAlloc::getUsableUniRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000741 const int RegType,
742 const MachineInstr *MInst,
Chris Lattner296b7732002-02-05 02:52:05 +0000743 const ValueSet *LVSetBef,
Vikram S. Adve23a4c8f2002-03-18 03:37:19 +0000744 MachineInstr *&MIBef,
745 MachineInstr *&MIAft) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000746
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000747 int RegU = getUnusedUniRegAtMI(RC, MInst, LVSetBef);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000748
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000749
Chris Lattner7e708292002-06-25 16:13:24 +0000750 if (RegU != -1) {
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000751 // we found an unused register, so we can simply use it
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000752 MIBef = MIAft = NULL;
753 }
754 else {
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000755 // we couldn't find an unused register. Generate code to free up a reg by
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000756 // saving it on stack and restoring after the instruction
757
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000758 int TmpOff = mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
Vikram S. Adve12af1642001-11-08 04:48:50 +0000759
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000760 RegU = getUniRegNotUsedByThisInst(RC, MInst);
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000761
762 vector<MachineInstr*> mvec;
763
764 MRI.cpReg2MemMI(RegU, MRI.getFramePointer(), TmpOff, RegType, mvec);
765 assert(mvec.size() == 1 && "Need to return a vector here too");
766 MIBef = * mvec.begin();
767
768 MRI.cpMem2RegMI(MRI.getFramePointer(), TmpOff, RegU, RegType, mvec);
769 assert(mvec.size() == 1 && "Need to return a vector here too");
770 MIAft = * mvec.begin();
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000771 }
772
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000773 return RegU;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000774}
775
776//----------------------------------------------------------------------------
777// This method is called to get a new unused register that can be used to
778// accomodate a spilled value.
779// This method may be called several times for a single machine instruction
780// if it contains many spilled operands. Each time it is called, it finds
781// a register which is not live at that instruction and also which is not
782// used by other spilled operands of the same instruction.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000783// Return register number is relative to the register class. NOT
784// unified number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000785//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000786int PhyRegAlloc::getUnusedUniRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000787 const MachineInstr *MInst,
Chris Lattner296b7732002-02-05 02:52:05 +0000788 const ValueSet *LVSetBef) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000789
790 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
791
Chris Lattner85c54652002-05-23 15:50:03 +0000792 std::vector<bool> &IsColorUsedArr = RC->getIsColorUsedArr();
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000793
Chris Lattner7e708292002-06-25 16:13:24 +0000794 for (unsigned i=0; i < NumAvailRegs; i++) // Reset array
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000795 IsColorUsedArr[i] = false;
796
Chris Lattner296b7732002-02-05 02:52:05 +0000797 ValueSet::const_iterator LIt = LVSetBef->begin();
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000798
799 // for each live var in live variable set after machine inst
Chris Lattner7e708292002-06-25 16:13:24 +0000800 for ( ; LIt != LVSetBef->end(); ++LIt) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000801
802 // get the live range corresponding to live var
803 LiveRange *const LRofLV = LRI.getLiveRangeForValue(*LIt );
804
805 // LR can be null if it is a const since a const
806 // doesn't have a dominating def - see Assumptions above
Chris Lattner7e708292002-06-25 16:13:24 +0000807 if (LRofLV && LRofLV->getRegClass() == RC && LRofLV->hasColor() )
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000808 IsColorUsedArr[ LRofLV->getColor() ] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000809 }
810
811 // It is possible that one operand of this MInst was already spilled
812 // and it received some register temporarily. If that's the case,
813 // it is recorded in machine operand. We must skip such registers.
814
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000815 setRelRegsUsedByThisInst(RC, MInst);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000816
Chris Lattner7e708292002-06-25 16:13:24 +0000817 for (unsigned c=0; c < NumAvailRegs; c++) // find first unused color
Chris Lattner85c54652002-05-23 15:50:03 +0000818 if (!IsColorUsedArr[c])
819 return MRI.getUnifiedRegNum(RC->getID(), c);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000820
Chris Lattner85c54652002-05-23 15:50:03 +0000821 return -1;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000822}
823
824
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000825//----------------------------------------------------------------------------
826// Get any other register in a register class, other than what is used
827// by operands of a machine instruction. Returns the unified reg number.
828//----------------------------------------------------------------------------
829int PhyRegAlloc::getUniRegNotUsedByThisInst(RegClass *RC,
Chris Lattner85c54652002-05-23 15:50:03 +0000830 const MachineInstr *MInst) {
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000831
Chris Lattner85c54652002-05-23 15:50:03 +0000832 vector<bool> &IsColorUsedArr = RC->getIsColorUsedArr();
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000833 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
834
835
Chris Lattner7e708292002-06-25 16:13:24 +0000836 for (unsigned i=0; i < NumAvailRegs ; i++) // Reset array
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000837 IsColorUsedArr[i] = false;
838
839 setRelRegsUsedByThisInst(RC, MInst);
840
Chris Lattner7e708292002-06-25 16:13:24 +0000841 for (unsigned c=0; c < RC->getNumOfAvailRegs(); c++)// find first unused color
Chris Lattner85c54652002-05-23 15:50:03 +0000842 if (!IsColorUsedArr[c])
843 return MRI.getUnifiedRegNum(RC->getID(), c);
844
845 assert(0 && "FATAL: No free register could be found in reg class!!");
Chris Lattner697954c2002-01-20 22:54:45 +0000846 return 0;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000847}
848
849
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000850//----------------------------------------------------------------------------
851// This method modifies the IsColorUsedArr of the register class passed to it.
852// It sets the bits corresponding to the registers used by this machine
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000853// instructions. Both explicit and implicit operands are set.
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000854//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000855void PhyRegAlloc::setRelRegsUsedByThisInst(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000856 const MachineInstr *MInst ) {
857
Chris Lattner85c54652002-05-23 15:50:03 +0000858 vector<bool> &IsColorUsedArr = RC->getIsColorUsedArr();
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000859
Chris Lattner7e708292002-06-25 16:13:24 +0000860 for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000861
862 const MachineOperand& Op = MInst->getOperand(OpNum);
863
Chris Lattner7e708292002-06-25 16:13:24 +0000864 if (Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000865 Op.getOperandType() == MachineOperand::MO_CCRegister ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000866
867 const Value *const Val = Op.getVRegValue();
868
Chris Lattner7e708292002-06-25 16:13:24 +0000869 if (Val )
870 if (MRI.getRegClassIDOfValue(Val) == RC->getID() ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000871 int Reg;
Chris Lattner7e708292002-06-25 16:13:24 +0000872 if ((Reg=Op.getAllocatedRegNum()) != -1) {
Chris Lattner85c54652002-05-23 15:50:03 +0000873 IsColorUsedArr[Reg] = true;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000874 }
875 else {
876 // it is possilbe that this operand still is not marked with
877 // a register but it has a LR and that received a color
878
879 LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
Chris Lattner7e708292002-06-25 16:13:24 +0000880 if (LROfVal)
881 if (LROfVal->hasColor() )
Chris Lattner85c54652002-05-23 15:50:03 +0000882 IsColorUsedArr[LROfVal->getColor()] = true;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000883 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000884
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000885 } // if reg classes are the same
886 }
887 else if (Op.getOperandType() == MachineOperand::MO_MachineRegister) {
Chris Lattner85c54652002-05-23 15:50:03 +0000888 assert((unsigned)Op.getMachineRegNum() < IsColorUsedArr.size());
889 IsColorUsedArr[Op.getMachineRegNum()] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000890 }
891 }
892
893 // If there are implicit references, mark them as well
894
Chris Lattner7e708292002-06-25 16:13:24 +0000895 for (unsigned z=0; z < MInst->getNumImplicitRefs(); z++) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000896
897 LiveRange *const LRofImpRef =
898 LRI.getLiveRangeForValue( MInst->getImplicitRef(z) );
Chris Lattner697954c2002-01-20 22:54:45 +0000899
Chris Lattner7e708292002-06-25 16:13:24 +0000900 if (LRofImpRef && LRofImpRef->hasColor())
Chris Lattner697954c2002-01-20 22:54:45 +0000901 IsColorUsedArr[LRofImpRef->getColor()] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000902 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000903}
904
905
906
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000907
908
909
910
911
912//----------------------------------------------------------------------------
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000913// If there are delay slots for an instruction, the instructions
914// added after it must really go after the delayed instruction(s).
915// So, we move the InstrAfter of that instruction to the
916// corresponding delayed instruction using the following method.
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000917
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000918//----------------------------------------------------------------------------
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000919void PhyRegAlloc::move2DelayedInstr(const MachineInstr *OrigMI,
920 const MachineInstr *DelayedMI) {
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000921
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000922 // "added after" instructions of the original instr
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000923 std::vector<MachineInstr *> &OrigAft = AddedInstrMap[OrigMI].InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000924
925 // "added instructions" of the delayed instr
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000926 AddedInstrns &DelayAdI = AddedInstrMap[DelayedMI];
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000927
928 // "added after" instructions of the delayed instr
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000929 std::vector<MachineInstr *> &DelayedAft = DelayAdI.InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000930
931 // go thru all the "added after instructions" of the original instruction
932 // and append them to the "addded after instructions" of the delayed
933 // instructions
Chris Lattner697954c2002-01-20 22:54:45 +0000934 DelayedAft.insert(DelayedAft.end(), OrigAft.begin(), OrigAft.end());
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000935
936 // empty the "added after instructions" of the original instruction
937 OrigAft.clear();
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000938}
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000939
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000940//----------------------------------------------------------------------------
941// This method prints the code with registers after register allocation is
942// complete.
943//----------------------------------------------------------------------------
944void PhyRegAlloc::printMachineCode()
945{
946
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000947 cerr << "\n;************** Function " << Meth->getName()
Chris Lattner697954c2002-01-20 22:54:45 +0000948 << " *****************\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000949
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000950 for (Function::const_iterator BBI = Meth->begin(), BBE = Meth->end();
951 BBI != BBE; ++BBI) {
Chris Lattner7e708292002-06-25 16:13:24 +0000952 cerr << "\n"; printLabel(BBI); cerr << ": ";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000953
954 // get the iterator for machine instructions
Chris Lattner7e708292002-06-25 16:13:24 +0000955 MachineCodeForBasicBlock& MIVec = BBI->getMachineInstrVec();
Vikram S. Adve48762092002-04-25 04:34:15 +0000956 MachineCodeForBasicBlock::iterator MII = MIVec.begin();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000957
958 // iterate over all the machine instructions in BB
Chris Lattner7e708292002-06-25 16:13:24 +0000959 for ( ; MII != MIVec.end(); ++MII) {
Vikram S. Adve48762092002-04-25 04:34:15 +0000960 MachineInstr *const MInst = *MII;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000961
Chris Lattner697954c2002-01-20 22:54:45 +0000962 cerr << "\n\t";
963 cerr << TargetInstrDescriptors[MInst->getOpCode()].opCodeString;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000964
Chris Lattner7e708292002-06-25 16:13:24 +0000965 for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000966 MachineOperand& Op = MInst->getOperand(OpNum);
967
Chris Lattner7e708292002-06-25 16:13:24 +0000968 if (Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasanka97b8b442001-10-18 22:36:26 +0000969 Op.getOperandType() == MachineOperand::MO_CCRegister /*||
970 Op.getOperandType() == MachineOperand::MO_PCRelativeDisp*/ ) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000971
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000972 const Value *const Val = Op.getVRegValue () ;
Ruchira Sasankae727f852001-09-18 22:43:57 +0000973 // ****this code is temporary till NULL Values are fixed
Chris Lattner7e708292002-06-25 16:13:24 +0000974 if (! Val ) {
Chris Lattner697954c2002-01-20 22:54:45 +0000975 cerr << "\t<*NULL*>";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000976 continue;
977 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000978
979 // if a label or a constant
Chris Lattner7e708292002-06-25 16:13:24 +0000980 if (isa<BasicBlock>(Val)) {
Chris Lattner697954c2002-01-20 22:54:45 +0000981 cerr << "\t"; printLabel( Op.getVRegValue () );
982 } else {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000983 // else it must be a register value
984 const int RegNum = Op.getAllocatedRegNum();
985
Chris Lattner697954c2002-01-20 22:54:45 +0000986 cerr << "\t" << "%" << MRI.getUnifiedRegName( RegNum );
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000987 if (Val->hasName() )
Chris Lattner697954c2002-01-20 22:54:45 +0000988 cerr << "(" << Val->getName() << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000989 else
Chris Lattner697954c2002-01-20 22:54:45 +0000990 cerr << "(" << Val << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000991
Chris Lattner7e708292002-06-25 16:13:24 +0000992 if (Op.opIsDef() )
Chris Lattner697954c2002-01-20 22:54:45 +0000993 cerr << "*";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000994
995 const LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
Chris Lattner7e708292002-06-25 16:13:24 +0000996 if (LROfVal )
997 if (LROfVal->hasSpillOffset() )
Chris Lattner697954c2002-01-20 22:54:45 +0000998 cerr << "$";
Ruchira Sasankae727f852001-09-18 22:43:57 +0000999 }
1000
1001 }
Chris Lattner7e708292002-06-25 16:13:24 +00001002 else if (Op.getOperandType() == MachineOperand::MO_MachineRegister) {
Chris Lattner697954c2002-01-20 22:54:45 +00001003 cerr << "\t" << "%" << MRI.getUnifiedRegName(Op.getMachineRegNum());
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001004 }
1005
1006 else
Chris Lattner697954c2002-01-20 22:54:45 +00001007 cerr << "\t" << Op; // use dump field
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001008 }
1009
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001010
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001011
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001012 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
Chris Lattner7e708292002-06-25 16:13:24 +00001013 if (NumOfImpRefs > 0) {
Chris Lattner697954c2002-01-20 22:54:45 +00001014 cerr << "\tImplicit:";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001015
Chris Lattner7e708292002-06-25 16:13:24 +00001016 for (unsigned z=0; z < NumOfImpRefs; z++)
Chris Lattner0665a5f2002-02-05 01:43:49 +00001017 cerr << RAV(MInst->getImplicitRef(z)) << "\t";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001018 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001019
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001020 } // for all machine instructions
1021
Chris Lattner697954c2002-01-20 22:54:45 +00001022 cerr << "\n";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001023
1024 } // for all BBs
1025
Chris Lattner697954c2002-01-20 22:54:45 +00001026 cerr << "\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001027}
1028
Ruchira Sasankae727f852001-09-18 22:43:57 +00001029
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001030#if 0
1031
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001032//----------------------------------------------------------------------------
1033//
1034//----------------------------------------------------------------------------
1035
1036void PhyRegAlloc::colorCallRetArgs()
1037{
1038
1039 CallRetInstrListType &CallRetInstList = LRI.getCallRetInstrList();
1040 CallRetInstrListType::const_iterator It = CallRetInstList.begin();
1041
Chris Lattner7e708292002-06-25 16:13:24 +00001042 for ( ; It != CallRetInstList.end(); ++It ) {
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001043
Ruchira Sasankaa90e7702001-10-15 16:26:38 +00001044 const MachineInstr *const CRMI = *It;
1045 unsigned OpCode = CRMI->getOpCode();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001046
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001047 // get the added instructions for this Call/Ret instruciton
Chris Lattner0b0ffa02002-04-09 05:13:04 +00001048 AddedInstrns &AI = AddedInstrMap[CRMI];
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001049
Chris Lattner0b0ffa02002-04-09 05:13:04 +00001050 // Tmp stack positions are needed by some calls that have spilled args
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001051 // So reset it before we call each such method
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001052 //mcInfo.popAllTempValues(TM);
1053
Vikram S. Adve12af1642001-11-08 04:48:50 +00001054
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001055 if (TM.getInstrInfo().isCall(OpCode))
Chris Lattner0b0ffa02002-04-09 05:13:04 +00001056 MRI.colorCallArgs(CRMI, LRI, &AI, *this);
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001057 else if (TM.getInstrInfo().isReturn(OpCode))
Chris Lattner0b0ffa02002-04-09 05:13:04 +00001058 MRI.colorRetValue(CRMI, LRI, &AI);
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001059 else
1060 assert(0 && "Non Call/Ret instrn in CallRetInstrList\n");
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001061 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001062}
1063
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001064#endif
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001065
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001066//----------------------------------------------------------------------------
1067
1068//----------------------------------------------------------------------------
1069void PhyRegAlloc::colorIncomingArgs()
1070{
Chris Lattner7e708292002-06-25 16:13:24 +00001071 const BasicBlock &FirstBB = Meth->front();
1072 const MachineInstr *FirstMI = FirstBB.getMachineInstrVec().front();
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001073 assert(FirstMI && "No machine instruction in entry BB");
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001074
Vikram S. Adve48762092002-04-25 04:34:15 +00001075 MRI.colorMethodArgs(Meth, LRI, &AddedInstrAtEntry);
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001076}
1077
Ruchira Sasankae727f852001-09-18 22:43:57 +00001078
1079//----------------------------------------------------------------------------
1080// Used to generate a label for a basic block
1081//----------------------------------------------------------------------------
Chris Lattner697954c2002-01-20 22:54:45 +00001082void PhyRegAlloc::printLabel(const Value *const Val) {
1083 if (Val->hasName())
1084 cerr << Val->getName();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001085 else
Chris Lattner697954c2002-01-20 22:54:45 +00001086 cerr << "Label" << Val;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001087}
1088
1089
Ruchira Sasankae727f852001-09-18 22:43:57 +00001090//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001091// This method calls setSugColorUsable method of each live range. This
1092// will determine whether the suggested color of LR is really usable.
1093// A suggested color is not usable when the suggested color is volatile
1094// AND when there are call interferences
1095//----------------------------------------------------------------------------
1096
1097void PhyRegAlloc::markUnusableSugColors()
1098{
Chris Lattner7e708292002-06-25 16:13:24 +00001099 if (DEBUG_RA ) cerr << "\nmarking unusable suggested colors ...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001100
1101 // hash map iterator
1102 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
1103 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
1104
Chris Lattner7e708292002-06-25 16:13:24 +00001105 for (; HMI != HMIEnd ; ++HMI ) {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001106 if (HMI->first) {
1107 LiveRange *L = HMI->second; // get the LiveRange
1108 if (L) {
Chris Lattner7e708292002-06-25 16:13:24 +00001109 if (L->hasSuggestedColor()) {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001110 int RCID = L->getRegClass()->getID();
Chris Lattner7e708292002-06-25 16:13:24 +00001111 if (MRI.isRegVolatile( RCID, L->getSuggestedColor()) &&
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001112 L->isCallInterference() )
1113 L->setSuggestedColorUsable( false );
1114 else
1115 L->setSuggestedColorUsable( true );
1116 }
1117 } // if L->hasSuggestedColor()
1118 }
1119 } // for all LR's in hash map
1120}
1121
1122
1123
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001124//----------------------------------------------------------------------------
1125// The following method will set the stack offsets of the live ranges that
1126// are decided to be spillled. This must be called just after coloring the
1127// LRs using the graph coloring algo. For each live range that is spilled,
1128// this method allocate a new spill position on the stack.
1129//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001130
Chris Lattner37730942002-02-05 03:52:29 +00001131void PhyRegAlloc::allocateStackSpace4SpilledLRs() {
1132 if (DEBUG_RA) cerr << "\nsetting LR stack offsets ...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001133
Chris Lattner37730942002-02-05 03:52:29 +00001134 LiveRangeMapType::const_iterator HMI = LRI.getLiveRangeMap()->begin();
1135 LiveRangeMapType::const_iterator HMIEnd = LRI.getLiveRangeMap()->end();
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001136
Chris Lattner7e708292002-06-25 16:13:24 +00001137 for ( ; HMI != HMIEnd ; ++HMI) {
Chris Lattner37730942002-02-05 03:52:29 +00001138 if (HMI->first && HMI->second) {
1139 LiveRange *L = HMI->second; // get the LiveRange
1140 if (!L->hasColor()) // NOTE: ** allocating the size of long Type **
1141 L->setSpillOffFromFP(mcInfo.allocateSpilledValue(TM, Type::LongTy));
1142 }
1143 } // for all LR's in hash map
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001144}
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001145
1146
1147
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001148//----------------------------------------------------------------------------
Ruchira Sasankae727f852001-09-18 22:43:57 +00001149// The entry pont to Register Allocation
1150//----------------------------------------------------------------------------
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001151
1152void PhyRegAlloc::allocateRegisters()
1153{
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001154
1155 // make sure that we put all register classes into the RegClassList
1156 // before we call constructLiveRanges (now done in the constructor of
1157 // PhyRegAlloc class).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001158 //
1159 LRI.constructLiveRanges(); // create LR info
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001160
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001161 if (DEBUG_RA)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001162 LRI.printLiveRanges();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001163
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001164 createIGNodeListsAndIGs(); // create IGNode list and IGs
1165
1166 buildInterferenceGraphs(); // build IGs in all reg classes
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001167
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001168
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001169 if (DEBUG_RA) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001170 // print all LRs in all reg classes
Chris Lattner7e708292002-06-25 16:13:24 +00001171 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
1172 RegClassList[rc]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001173
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001174 // print IGs in all register classes
Chris Lattner7e708292002-06-25 16:13:24 +00001175 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
1176 RegClassList[rc]->printIG();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001177 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001178
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001179
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001180 LRI.coalesceLRs(); // coalesce all live ranges
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001181
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +00001182
Chris Lattner7e708292002-06-25 16:13:24 +00001183 if (DEBUG_RA) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001184 // print all LRs in all reg classes
Chris Lattner7e708292002-06-25 16:13:24 +00001185 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001186 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001187
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001188 // print IGs in all register classes
Chris Lattner7e708292002-06-25 16:13:24 +00001189 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001190 RegClassList[ rc ]->printIG();
1191 }
1192
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001193
1194 // mark un-usable suggested color before graph coloring algorithm.
1195 // When this is done, the graph coloring algo will not reserve
1196 // suggested color unnecessarily - they can be used by another LR
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001197 //
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001198 markUnusableSugColors();
1199
1200 // color all register classes using the graph coloring algo
Chris Lattner7e708292002-06-25 16:13:24 +00001201 for (unsigned rc=0; rc < NumOfRegClasses ; rc++)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001202 RegClassList[ rc ]->colorAllRegs();
1203
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001204 // Atter grpah coloring, if some LRs did not receive a color (i.e, spilled)
1205 // a poistion for such spilled LRs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001206 //
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001207 allocateStackSpace4SpilledLRs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001208
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001209 mcInfo.popAllTempValues(TM); // TODO **Check
1210
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001211 // color incoming args - if the correct color was not received
1212 // insert code to copy to the correct register
1213 //
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001214 colorIncomingArgs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001215
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001216 // Now update the machine code with register names and add any
1217 // additional code inserted by the register allocator to the instruction
1218 // stream
1219 //
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001220 updateMachineCode();
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001221
Chris Lattner045e7c82001-09-19 16:26:23 +00001222 if (DEBUG_RA) {
Vikram S. Adve12af1642001-11-08 04:48:50 +00001223 MachineCodeForMethod::get(Meth).dump();
Chris Lattner045e7c82001-09-19 16:26:23 +00001224 printMachineCode(); // only for DEBUGGING
1225 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001226}
1227
Ruchira Sasankae727f852001-09-18 22:43:57 +00001228
1229