blob: 2290dc490267ab7df9d9c1cee7b1e48ffbff8148 [file] [log] [blame]
Chris Lattner179cdfb2002-08-09 20:08:03 +00001//===-- PhyRegAlloc.cpp ---------------------------------------------------===//
Vikram S. Adve12af1642001-11-08 04:48:50 +00002//
Chris Lattner179cdfb2002-08-09 20:08:03 +00003// Register allocation for LLVM.
4//
5//===----------------------------------------------------------------------===//
Ruchira Sasanka8e604792001-09-14 21:18:34 +00006
Chris Lattner6dd98a62002-02-04 00:33:08 +00007#include "llvm/CodeGen/RegisterAllocation.h"
Vikram S. Adve39c94e12002-09-14 23:05:33 +00008#include "llvm/CodeGen/RegAllocCommon.h"
Chris Lattnercb6b4bd2002-10-29 16:51:05 +00009#include "llvm/CodeGen/IGNode.h"
Vikram S. Adve12af1642001-11-08 04:48:50 +000010#include "llvm/CodeGen/PhyRegAlloc.h"
Chris Lattnerf6ee49f2003-01-15 18:08:07 +000011#include "llvm/CodeGen/MachineInstrBuilder.h"
Vikram S. Advedabb41d2002-05-19 15:29:31 +000012#include "llvm/CodeGen/MachineInstrAnnot.h"
Misha Brukmanfce11432002-10-28 00:28:31 +000013#include "llvm/CodeGen/MachineFunction.h"
Chris Lattnere90fcb72002-12-28 20:35:34 +000014#include "llvm/CodeGen/MachineFunctionInfo.h"
Chris Lattner92ba2aa2003-01-14 23:05:08 +000015#include "llvm/CodeGen/FunctionLiveVarInfo.h"
Chris Lattner14ab1ce2002-02-04 17:48:00 +000016#include "llvm/Analysis/LoopInfo.h"
Vikram S. Adve12af1642001-11-08 04:48:50 +000017#include "llvm/Target/TargetMachine.h"
Chris Lattner8bd66e62002-12-28 21:00:25 +000018#include "llvm/Target/TargetFrameInfo.h"
Chris Lattner3501fea2003-01-14 22:00:31 +000019#include "llvm/Target/TargetInstrInfo.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000020#include "llvm/Function.h"
Chris Lattner37730942002-02-05 03:52:29 +000021#include "llvm/Type.h"
Vikram S. Advedabb41d2002-05-19 15:29:31 +000022#include "llvm/iOther.h"
Vikram S. Advef5af6362002-07-08 23:15:32 +000023#include "Support/STLExtras.h"
Chris Lattner4bc23482002-09-15 07:07:55 +000024#include "Support/CommandLine.h"
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000025#include <math.h>
Chris Lattner697954c2002-01-20 22:54:45 +000026using std::cerr;
Anand Shuklacfb22d32002-06-25 20:55:50 +000027using std::vector;
Vikram S. Adve12af1642001-11-08 04:48:50 +000028
Chris Lattner70e60cb2002-05-22 17:08:27 +000029RegAllocDebugLevel_t DEBUG_RA;
Vikram S. Adve39c94e12002-09-14 23:05:33 +000030
Chris Lattner5ff62e92002-07-22 02:10:13 +000031static cl::opt<RegAllocDebugLevel_t, true>
32DRA_opt("dregalloc", cl::Hidden, cl::location(DEBUG_RA),
33 cl::desc("enable register allocation debugging information"),
34 cl::values(
Vikram S. Adve39c94e12002-09-14 23:05:33 +000035 clEnumValN(RA_DEBUG_None , "n", "disable debug output"),
36 clEnumValN(RA_DEBUG_Results, "y", "debug output for allocation results"),
37 clEnumValN(RA_DEBUG_Coloring, "c", "debug output for graph coloring step"),
38 clEnumValN(RA_DEBUG_Interference,"ig","debug output for interference graphs"),
39 clEnumValN(RA_DEBUG_LiveRanges , "lr","debug output for live ranges"),
40 clEnumValN(RA_DEBUG_Verbose, "v", "extra debug output"),
Chris Lattner5ff62e92002-07-22 02:10:13 +000041 0));
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000042
Chris Lattner2f9b28e2002-02-04 15:54:09 +000043//----------------------------------------------------------------------------
44// RegisterAllocation pass front end...
45//----------------------------------------------------------------------------
46namespace {
Chris Lattnerf57b8452002-04-27 06:56:12 +000047 class RegisterAllocator : public FunctionPass {
Chris Lattner2f9b28e2002-02-04 15:54:09 +000048 TargetMachine &Target;
49 public:
50 inline RegisterAllocator(TargetMachine &T) : Target(T) {}
Chris Lattner96c466b2002-04-29 14:57:45 +000051
52 const char *getPassName() const { return "Register Allocation"; }
Chris Lattner6dd98a62002-02-04 00:33:08 +000053
Chris Lattner7e708292002-06-25 16:13:24 +000054 bool runOnFunction(Function &F) {
Chris Lattner2f9b28e2002-02-04 15:54:09 +000055 if (DEBUG_RA)
Chris Lattner7e708292002-06-25 16:13:24 +000056 cerr << "\n********* Function "<< F.getName() << " ***********\n";
Chris Lattner2f9b28e2002-02-04 15:54:09 +000057
Chris Lattner7e708292002-06-25 16:13:24 +000058 PhyRegAlloc PRA(&F, Target, &getAnalysis<FunctionLiveVarInfo>(),
Chris Lattner1b7f7dc2002-04-28 16:21:30 +000059 &getAnalysis<LoopInfo>());
Chris Lattner2f9b28e2002-02-04 15:54:09 +000060 PRA.allocateRegisters();
61
62 if (DEBUG_RA) cerr << "\nRegister allocation complete!\n";
63 return false;
64 }
Chris Lattner4911c352002-02-04 17:39:42 +000065
Chris Lattnerf57b8452002-04-27 06:56:12 +000066 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerdd5b4952002-08-08 19:01:28 +000067 AU.addRequired<LoopInfo>();
68 AU.addRequired<FunctionLiveVarInfo>();
Chris Lattner4911c352002-02-04 17:39:42 +000069 }
Chris Lattner2f9b28e2002-02-04 15:54:09 +000070 };
Chris Lattner6dd98a62002-02-04 00:33:08 +000071}
72
Chris Lattnerf57b8452002-04-27 06:56:12 +000073Pass *getRegisterAllocator(TargetMachine &T) {
Chris Lattner2f9b28e2002-02-04 15:54:09 +000074 return new RegisterAllocator(T);
75}
Chris Lattner6dd98a62002-02-04 00:33:08 +000076
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000077//----------------------------------------------------------------------------
78// Constructor: Init local composite objects and create register classes.
79//----------------------------------------------------------------------------
Chris Lattner1b7f7dc2002-04-28 16:21:30 +000080PhyRegAlloc::PhyRegAlloc(Function *F, const TargetMachine& tm,
81 FunctionLiveVarInfo *Lvi, LoopInfo *LDC)
Chris Lattnerf726e772002-10-28 19:22:04 +000082 : TM(tm), Fn(F), MF(MachineFunction::get(F)), LVI(Lvi),
83 LRI(F, tm, RegClassList), MRI(tm.getRegInfo()),
84 NumOfRegClasses(MRI.getNumOfRegClasses()), LoopDepthCalc(LDC) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +000085
86 // create each RegisterClass and put in RegClassList
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000087 //
Chris Lattnerf726e772002-10-28 19:22:04 +000088 for (unsigned rc=0; rc != NumOfRegClasses; rc++)
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000089 RegClassList.push_back(new RegClass(F, MRI.getMachineRegClass(rc),
90 &ResColList));
Ruchira Sasanka8e604792001-09-14 21:18:34 +000091}
92
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000093
94//----------------------------------------------------------------------------
95// Destructor: Deletes register classes
96//----------------------------------------------------------------------------
97PhyRegAlloc::~PhyRegAlloc() {
Chris Lattner7e708292002-06-25 16:13:24 +000098 for ( unsigned rc=0; rc < NumOfRegClasses; rc++)
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000099 delete RegClassList[rc];
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000100
101 AddedInstrMap.clear();
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000102}
103
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000104//----------------------------------------------------------------------------
105// This method initally creates interference graphs (one in each reg class)
106// and IGNodeList (one in each IG). The actual nodes will be pushed later.
107//----------------------------------------------------------------------------
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000108void PhyRegAlloc::createIGNodeListsAndIGs() {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000109 if (DEBUG_RA >= RA_DEBUG_LiveRanges) cerr << "Creating LR lists ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000110
111 // hash map iterator
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000112 LiveRangeMapType::const_iterator HMI = LRI.getLiveRangeMap()->begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000113
114 // hash map end
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000115 LiveRangeMapType::const_iterator HMIEnd = LRI.getLiveRangeMap()->end();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000116
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000117 for (; HMI != HMIEnd ; ++HMI ) {
118 if (HMI->first) {
119 LiveRange *L = HMI->second; // get the LiveRange
120 if (!L) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000121 if (DEBUG_RA)
122 cerr << "\n**** ?!?WARNING: NULL LIVE RANGE FOUND FOR: "
123 << RAV(HMI->first) << "****\n";
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000124 continue;
125 }
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000126
127 // if the Value * is not null, and LR is not yet written to the IGNodeList
Chris Lattner7e708292002-06-25 16:13:24 +0000128 if (!(L->getUserIGNode()) ) {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000129 RegClass *const RC = // RegClass of first value in the LR
130 RegClassList[ L->getRegClass()->getID() ];
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000131 RC->addLRToIG(L); // add this LR to an IG
132 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000133 }
134 }
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000135
136 // init RegClassList
Chris Lattner7e708292002-06-25 16:13:24 +0000137 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000138 RegClassList[rc]->createInterferenceGraph();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000139
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000140 if (DEBUG_RA >= RA_DEBUG_LiveRanges) cerr << "LRLists Created!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000141}
142
143
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000144//----------------------------------------------------------------------------
145// This method will add all interferences at for a given instruction.
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000146// Interence occurs only if the LR of Def (Inst or Arg) is of the same reg
147// class as that of live var. The live var passed to this function is the
148// LVset AFTER the instruction
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000149//----------------------------------------------------------------------------
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000150
Chris Lattner296b7732002-02-05 02:52:05 +0000151void PhyRegAlloc::addInterference(const Value *Def,
152 const ValueSet *LVSet,
153 bool isCallInst) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000154
Chris Lattner296b7732002-02-05 02:52:05 +0000155 ValueSet::const_iterator LIt = LVSet->begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000156
157 // get the live range of instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000158 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000159 const LiveRange *const LROfDef = LRI.getLiveRangeForValue( Def );
160
161 IGNode *const IGNodeOfDef = LROfDef->getUserIGNode();
162 assert( IGNodeOfDef );
163
164 RegClass *const RCOfDef = LROfDef->getRegClass();
165
166 // for each live var in live variable set
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000167 //
Chris Lattner7e708292002-06-25 16:13:24 +0000168 for ( ; LIt != LVSet->end(); ++LIt) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000169
Vikram S. Advef5af6362002-07-08 23:15:32 +0000170 if (DEBUG_RA >= RA_DEBUG_Verbose)
Chris Lattner0665a5f2002-02-05 01:43:49 +0000171 cerr << "< Def=" << RAV(Def) << ", Lvar=" << RAV(*LIt) << "> ";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000172
173 // get the live range corresponding to live var
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000174 //
Chris Lattner0665a5f2002-02-05 01:43:49 +0000175 LiveRange *LROfVar = LRI.getLiveRangeForValue(*LIt);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000176
177 // LROfVar can be null if it is a const since a const
178 // doesn't have a dominating def - see Assumptions above
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000179 //
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000180 if (LROfVar)
181 if (LROfDef != LROfVar) // do not set interf for same LR
182 if (RCOfDef == LROfVar->getRegClass()) // 2 reg classes are the same
183 RCOfDef->setInterference( LROfDef, LROfVar);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000184 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000185}
186
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000187
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000188
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000189//----------------------------------------------------------------------------
190// For a call instruction, this method sets the CallInterference flag in
191// the LR of each variable live int the Live Variable Set live after the
192// call instruction (except the return value of the call instruction - since
193// the return value does not interfere with that call itself).
194//----------------------------------------------------------------------------
195
196void PhyRegAlloc::setCallInterferences(const MachineInstr *MInst,
Chris Lattner296b7732002-02-05 02:52:05 +0000197 const ValueSet *LVSetAft) {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000198
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000199 if (DEBUG_RA >= RA_DEBUG_Interference)
Chris Lattner697954c2002-01-20 22:54:45 +0000200 cerr << "\n For call inst: " << *MInst;
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000201
Chris Lattner296b7732002-02-05 02:52:05 +0000202 ValueSet::const_iterator LIt = LVSetAft->begin();
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000203
204 // for each live var in live variable set after machine inst
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000205 //
Chris Lattner7e708292002-06-25 16:13:24 +0000206 for ( ; LIt != LVSetAft->end(); ++LIt) {
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000207
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000208 // get the live range corresponding to live var
209 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000210 LiveRange *const LR = LRI.getLiveRangeForValue(*LIt );
211
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000212 // LR can be null if it is a const since a const
213 // doesn't have a dominating def - see Assumptions above
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000214 //
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000215 if (LR ) {
216 if (DEBUG_RA >= RA_DEBUG_Interference) {
217 cerr << "\n\tLR after Call: ";
218 printSet(*LR);
219 }
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000220 LR->setCallInterference();
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000221 if (DEBUG_RA >= RA_DEBUG_Interference) {
222 cerr << "\n ++After adding call interference for LR: " ;
Chris Lattner296b7732002-02-05 02:52:05 +0000223 printSet(*LR);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000224 }
225 }
226
227 }
228
Vikram S. Adve1a53f032002-03-31 18:54:37 +0000229 // Now find the LR of the return value of the call
230 // We do this because, we look at the LV set *after* the instruction
231 // to determine, which LRs must be saved across calls. The return value
232 // of the call is live in this set - but it does not interfere with call
233 // (i.e., we can allocate a volatile register to the return value)
234 //
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000235 CallArgsDescriptor* argDesc = CallArgsDescriptor::get(MInst);
236
237 if (const Value *RetVal = argDesc->getReturnValue()) {
Vikram S. Adve1a53f032002-03-31 18:54:37 +0000238 LiveRange *RetValLR = LRI.getLiveRangeForValue( RetVal );
239 assert( RetValLR && "No LR for RetValue of call");
240 RetValLR->clearCallInterference();
241 }
242
243 // If the CALL is an indirect call, find the LR of the function pointer.
244 // That has a call interference because it conflicts with outgoing args.
Chris Lattner7e708292002-06-25 16:13:24 +0000245 if (const Value *AddrVal = argDesc->getIndirectFuncPtr()) {
Vikram S. Adve1a53f032002-03-31 18:54:37 +0000246 LiveRange *AddrValLR = LRI.getLiveRangeForValue( AddrVal );
247 assert( AddrValLR && "No LR for indirect addr val of call");
248 AddrValLR->setCallInterference();
249 }
250
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000251}
252
253
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000254
255
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000256//----------------------------------------------------------------------------
257// This method will walk thru code and create interferences in the IG of
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000258// each RegClass. Also, this method calculates the spill cost of each
259// Live Range (it is done in this method to save another pass over the code).
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000260//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000261void PhyRegAlloc::buildInterferenceGraphs()
262{
263
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000264 if (DEBUG_RA >= RA_DEBUG_Interference)
265 cerr << "Creating interference graphs ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000266
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000267 unsigned BBLoopDepthCost;
Chris Lattnerf726e772002-10-28 19:22:04 +0000268 for (MachineFunction::iterator BBI = MF.begin(), BBE = MF.end();
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000269 BBI != BBE; ++BBI) {
Chris Lattnerf726e772002-10-28 19:22:04 +0000270 const MachineBasicBlock &MBB = *BBI;
271 const BasicBlock *BB = MBB.getBasicBlock();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000272
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000273 // find the 10^(loop_depth) of this BB
274 //
Chris Lattnerf726e772002-10-28 19:22:04 +0000275 BBLoopDepthCost = (unsigned)pow(10.0, LoopDepthCalc->getLoopDepth(BB));
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000276
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000277 // get the iterator for machine instructions
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000278 //
Chris Lattnerf726e772002-10-28 19:22:04 +0000279 MachineBasicBlock::const_iterator MII = MBB.begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000280
281 // iterate over all the machine instructions in BB
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000282 //
Chris Lattnerf726e772002-10-28 19:22:04 +0000283 for ( ; MII != MBB.end(); ++MII) {
284 const MachineInstr *MInst = *MII;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000285
286 // get the LV set after the instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000287 //
Chris Lattnerf726e772002-10-28 19:22:04 +0000288 const ValueSet &LVSetAI = LVI->getLiveVarSetAfterMInst(MInst, BB);
289 bool isCallInst = TM.getInstrInfo().isCall(MInst->getOpCode());
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000290
Chris Lattner7e708292002-06-25 16:13:24 +0000291 if (isCallInst ) {
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000292 // set the isCallInterference flag of each live range wich extends
293 // accross this call instruction. This information is used by graph
294 // coloring algo to avoid allocating volatile colors to live ranges
295 // that span across calls (since they have to be saved/restored)
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000296 //
Chris Lattner748697d2002-02-05 04:20:12 +0000297 setCallInterferences(MInst, &LVSetAI);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000298 }
299
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000300 // iterate over all MI operands to find defs
301 //
Chris Lattner2f898d22002-02-05 06:02:59 +0000302 for (MachineInstr::const_val_op_iterator OpI = MInst->begin(),
303 OpE = MInst->end(); OpI != OpE; ++OpI) {
304 if (OpI.isDef()) // create a new LR iff this operand is a def
Chris Lattner748697d2002-02-05 04:20:12 +0000305 addInterference(*OpI, &LVSetAI, isCallInst);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000306
307 // Calculate the spill cost of each live range
308 //
Chris Lattner2f898d22002-02-05 06:02:59 +0000309 LiveRange *LR = LRI.getLiveRangeForValue(*OpI);
310 if (LR) LR->addSpillCost(BBLoopDepthCost);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000311 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000312
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000313
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000314 // if there are multiple defs in this instruction e.g. in SETX
315 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000316 if (TM.getInstrInfo().isPseudoInstr(MInst->getOpCode()))
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000317 addInterf4PseudoInstr(MInst);
318
319
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000320 // Also add interference for any implicit definitions in a machine
321 // instr (currently, only calls have this).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000322 //
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000323 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
Chris Lattner7e708292002-06-25 16:13:24 +0000324 if ( NumOfImpRefs > 0 ) {
325 for (unsigned z=0; z < NumOfImpRefs; z++)
326 if (MInst->implicitRefIsDefined(z) )
Chris Lattner748697d2002-02-05 04:20:12 +0000327 addInterference( MInst->getImplicitRef(z), &LVSetAI, isCallInst );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000328 }
329
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +0000330
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000331 } // for all machine instructions in BB
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000332 } // for all BBs in function
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000333
334
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000335 // add interferences for function arguments. Since there are no explict
336 // defs in the function for args, we have to add them manually
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000337 //
338 addInterferencesForArgs();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000339
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000340 if (DEBUG_RA >= RA_DEBUG_Interference)
341 cerr << "Interference graphs calculated!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000342}
343
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000344
345
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000346//--------------------------------------------------------------------------
347// Pseudo instructions will be exapnded to multiple instructions by the
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000348// assembler. Consequently, all the opernds must get distinct registers.
349// Therefore, we mark all operands of a pseudo instruction as they interfere
350// with one another.
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000351//--------------------------------------------------------------------------
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000352void PhyRegAlloc::addInterf4PseudoInstr(const MachineInstr *MInst) {
353
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000354 bool setInterf = false;
355
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000356 // iterate over MI operands to find defs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000357 //
Chris Lattner2f898d22002-02-05 06:02:59 +0000358 for (MachineInstr::const_val_op_iterator It1 = MInst->begin(),
359 ItE = MInst->end(); It1 != ItE; ++It1) {
360 const LiveRange *LROfOp1 = LRI.getLiveRangeForValue(*It1);
361 assert((LROfOp1 || !It1.isDef()) && "No LR for Def in PSEUDO insruction");
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000362
Chris Lattner2f898d22002-02-05 06:02:59 +0000363 MachineInstr::const_val_op_iterator It2 = It1;
Chris Lattner7e708292002-06-25 16:13:24 +0000364 for (++It2; It2 != ItE; ++It2) {
Chris Lattner2f898d22002-02-05 06:02:59 +0000365 const LiveRange *LROfOp2 = LRI.getLiveRangeForValue(*It2);
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000366
Chris Lattner2f898d22002-02-05 06:02:59 +0000367 if (LROfOp2) {
368 RegClass *RCOfOp1 = LROfOp1->getRegClass();
369 RegClass *RCOfOp2 = LROfOp2->getRegClass();
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000370
Chris Lattner7e708292002-06-25 16:13:24 +0000371 if (RCOfOp1 == RCOfOp2 ){
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000372 RCOfOp1->setInterference( LROfOp1, LROfOp2 );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000373 setInterf = true;
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000374 }
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000375 } // if Op2 has a LR
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000376 } // for all other defs in machine instr
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000377 } // for all operands in an instruction
378
Chris Lattner2f898d22002-02-05 06:02:59 +0000379 if (!setInterf && MInst->getNumOperands() > 2) {
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000380 cerr << "\nInterf not set for any operand in pseudo instr:\n";
381 cerr << *MInst;
382 assert(0 && "Interf not set for pseudo instr with > 2 operands" );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000383 }
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000384}
385
386
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000387
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000388//----------------------------------------------------------------------------
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000389// This method will add interferences for incoming arguments to a function.
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000390//----------------------------------------------------------------------------
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000391
Chris Lattner296b7732002-02-05 02:52:05 +0000392void PhyRegAlloc::addInterferencesForArgs() {
393 // get the InSet of root BB
Chris Lattnerf726e772002-10-28 19:22:04 +0000394 const ValueSet &InSet = LVI->getInSetOfBB(&Fn->front());
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000395
Chris Lattnerf726e772002-10-28 19:22:04 +0000396 for (Function::const_aiterator AI = Fn->abegin(); AI != Fn->aend(); ++AI) {
Chris Lattner7e708292002-06-25 16:13:24 +0000397 // add interferences between args and LVars at start
398 addInterference(AI, &InSet, false);
399
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000400 if (DEBUG_RA >= RA_DEBUG_Interference)
Chris Lattner7e708292002-06-25 16:13:24 +0000401 cerr << " - %% adding interference for argument " << RAV(AI) << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000402 }
403}
404
405
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000406//----------------------------------------------------------------------------
407// This method is called after register allocation is complete to set the
408// allocated reisters in the machine code. This code will add register numbers
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000409// to MachineOperands that contain a Value. Also it calls target specific
410// methods to produce caller saving instructions. At the end, it adds all
411// additional instructions produced by the register allocator to the
412// instruction stream.
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000413//----------------------------------------------------------------------------
Vikram S. Adve48762092002-04-25 04:34:15 +0000414
415//-----------------------------
416// Utility functions used below
417//-----------------------------
418inline void
Vikram S. Advecb202e32002-10-11 16:12:40 +0000419InsertBefore(MachineInstr* newMI,
Chris Lattnerf726e772002-10-28 19:22:04 +0000420 MachineBasicBlock& MBB,
Chris Lattner32be9f62002-10-28 01:41:27 +0000421 MachineBasicBlock::iterator& MII)
Vikram S. Advecb202e32002-10-11 16:12:40 +0000422{
Chris Lattnerf726e772002-10-28 19:22:04 +0000423 MII = MBB.insert(MII, newMI);
Vikram S. Advecb202e32002-10-11 16:12:40 +0000424 ++MII;
425}
426
427inline void
428InsertAfter(MachineInstr* newMI,
Chris Lattnerf726e772002-10-28 19:22:04 +0000429 MachineBasicBlock& MBB,
Chris Lattner32be9f62002-10-28 01:41:27 +0000430 MachineBasicBlock::iterator& MII)
Vikram S. Advecb202e32002-10-11 16:12:40 +0000431{
432 ++MII; // insert before the next instruction
Chris Lattnerf726e772002-10-28 19:22:04 +0000433 MII = MBB.insert(MII, newMI);
Vikram S. Advecb202e32002-10-11 16:12:40 +0000434}
435
436inline void
437SubstituteInPlace(MachineInstr* newMI,
Chris Lattnerf726e772002-10-28 19:22:04 +0000438 MachineBasicBlock& MBB,
Chris Lattner32be9f62002-10-28 01:41:27 +0000439 MachineBasicBlock::iterator MII)
Vikram S. Advecb202e32002-10-11 16:12:40 +0000440{
441 *MII = newMI;
442}
443
444inline void
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000445PrependInstructions(vector<MachineInstr *> &IBef,
Chris Lattnerf726e772002-10-28 19:22:04 +0000446 MachineBasicBlock& MBB,
Chris Lattner32be9f62002-10-28 01:41:27 +0000447 MachineBasicBlock::iterator& MII,
Vikram S. Adve48762092002-04-25 04:34:15 +0000448 const std::string& msg)
449{
450 if (!IBef.empty())
451 {
452 MachineInstr* OrigMI = *MII;
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000453 std::vector<MachineInstr *>::iterator AdIt;
Vikram S. Adve48762092002-04-25 04:34:15 +0000454 for (AdIt = IBef.begin(); AdIt != IBef.end() ; ++AdIt)
455 {
456 if (DEBUG_RA) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000457 if (OrigMI) cerr << "For MInst:\n " << *OrigMI;
458 cerr << msg << "PREPENDed instr:\n " << **AdIt << "\n";
Vikram S. Adve48762092002-04-25 04:34:15 +0000459 }
Chris Lattnerf726e772002-10-28 19:22:04 +0000460 InsertBefore(*AdIt, MBB, MII);
Vikram S. Adve48762092002-04-25 04:34:15 +0000461 }
462 }
463}
464
465inline void
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000466AppendInstructions(std::vector<MachineInstr *> &IAft,
Chris Lattnerf726e772002-10-28 19:22:04 +0000467 MachineBasicBlock& MBB,
Chris Lattner32be9f62002-10-28 01:41:27 +0000468 MachineBasicBlock::iterator& MII,
Vikram S. Adve48762092002-04-25 04:34:15 +0000469 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;
Chris Lattner7e708292002-06-25 16:13:24 +0000475 for ( AdIt = IAft.begin(); AdIt != IAft.end() ; ++AdIt )
Vikram S. Adve48762092002-04-25 04:34:15 +0000476 {
Chris Lattner7e708292002-06-25 16:13:24 +0000477 if (DEBUG_RA) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000478 if (OrigMI) cerr << "For MInst:\n " << *OrigMI;
479 cerr << msg << "APPENDed instr:\n " << **AdIt << "\n";
Vikram S. Adve48762092002-04-25 04:34:15 +0000480 }
Chris Lattnerf726e772002-10-28 19:22:04 +0000481 InsertAfter(*AdIt, MBB, MII);
Vikram S. Adve48762092002-04-25 04:34:15 +0000482 }
483 }
484}
485
486
Chris Lattnerf726e772002-10-28 19:22:04 +0000487void PhyRegAlloc::updateMachineCode() {
Chris Lattner7e708292002-06-25 16:13:24 +0000488 // Insert any instructions needed at method entry
Chris Lattnerf726e772002-10-28 19:22:04 +0000489 MachineBasicBlock::iterator MII = MF.front().begin();
490 PrependInstructions(AddedInstrAtEntry.InstrnsBefore, MF.front(), MII,
Chris Lattner7e708292002-06-25 16:13:24 +0000491 "At function entry: \n");
492 assert(AddedInstrAtEntry.InstrnsAfter.empty() &&
493 "InstrsAfter should be unnecessary since we are just inserting at "
494 "the function entry point here.");
Vikram S. Adve48762092002-04-25 04:34:15 +0000495
Chris Lattnerf726e772002-10-28 19:22:04 +0000496 for (MachineFunction::iterator BBI = MF.begin(), BBE = MF.end();
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000497 BBI != BBE; ++BBI) {
Vikram S. Advecb202e32002-10-11 16:12:40 +0000498
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000499 // iterate over all the machine instructions in BB
Chris Lattnerf726e772002-10-28 19:22:04 +0000500 MachineBasicBlock &MBB = *BBI;
501 for (MachineBasicBlock::iterator MII = MBB.begin();
502 MII != MBB.end(); ++MII) {
503
Vikram S. Adve48762092002-04-25 04:34:15 +0000504 MachineInstr *MInst = *MII;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000505 unsigned Opcode = MInst->getOpCode();
506
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000507 // do not process Phis
Vikram S. Adve23a4c8f2002-03-18 03:37:19 +0000508 if (TM.getInstrInfo().isDummyPhiInstr(Opcode))
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000509 continue;
510
Vikram S. Advef5af6362002-07-08 23:15:32 +0000511 // Reset tmp stack positions so they can be reused for each machine instr.
Chris Lattnere90fcb72002-12-28 20:35:34 +0000512 MF.getInfo()->popAllTempValues();
Vikram S. Advef5af6362002-07-08 23:15:32 +0000513
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000514 // Now insert speical instructions (if necessary) for call/return
515 // instructions.
516 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000517 if (TM.getInstrInfo().isCall(Opcode) ||
Chris Lattnerf726e772002-10-28 19:22:04 +0000518 TM.getInstrInfo().isReturn(Opcode)) {
519 AddedInstrns &AI = AddedInstrMap[MInst];
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000520
Chris Lattnerf726e772002-10-28 19:22:04 +0000521 if (TM.getInstrInfo().isCall(Opcode))
522 MRI.colorCallArgs(MInst, LRI, &AI, *this, MBB.getBasicBlock());
523 else if (TM.getInstrInfo().isReturn(Opcode))
524 MRI.colorRetValue(MInst, LRI, &AI);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000525 }
526
Vikram S. Advef5af6362002-07-08 23:15:32 +0000527 // Set the registers for operands in the machine instruction
528 // if a register was successfully allocated. If not, insert
529 // code to spill the register value.
530 //
531 for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum)
532 {
533 MachineOperand& Op = MInst->getOperand(OpNum);
Chris Lattner133f0792002-10-28 04:45:29 +0000534 if (Op.getType() == MachineOperand::MO_VirtualRegister ||
535 Op.getType() == MachineOperand::MO_CCRegister)
Vikram S. Advef5af6362002-07-08 23:15:32 +0000536 {
537 const Value *const Val = Op.getVRegValue();
538
539 LiveRange *const LR = LRI.getLiveRangeForValue(Val);
540 if (!LR) // consts or labels will have no live range
541 {
542 // if register is not allocated, mark register as invalid
543 if (Op.getAllocatedRegNum() == -1)
544 MInst->SetRegForOperand(OpNum, MRI.getInvalidRegNum());
545 continue;
546 }
547
Chris Lattnerf726e772002-10-28 19:22:04 +0000548 if (LR->hasColor())
Vikram S. Advef5af6362002-07-08 23:15:32 +0000549 MInst->SetRegForOperand(OpNum,
550 MRI.getUnifiedRegNum(LR->getRegClass()->getID(),
551 LR->getColor()));
552 else
553 // LR did NOT receive a color (register). Insert spill code.
Chris Lattnerf726e772002-10-28 19:22:04 +0000554 insertCode4SpilledLR(LR, MInst, MBB.getBasicBlock(), OpNum);
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000555 }
Vikram S. Advef5af6362002-07-08 23:15:32 +0000556 } // for each operand
Vikram S. Advecb202e32002-10-11 16:12:40 +0000557
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000558 // Now add instructions that the register allocator inserts before/after
559 // this machine instructions (done only for calls/rets/incoming args)
560 // We do this here, to ensure that spill for an instruction is inserted
561 // closest as possible to an instruction (see above insertCode4Spill...)
562 //
Vikram S. Advecb202e32002-10-11 16:12:40 +0000563 // First, if the instruction in the delay slot of a branch needs
564 // instructions inserted, move it out of the delay slot and before the
565 // branch because putting code before or after it would be VERY BAD!
566 //
567 unsigned bumpIteratorBy = 0;
Chris Lattnerf726e772002-10-28 19:22:04 +0000568 if (MII != MBB.begin())
Vikram S. Advecb202e32002-10-11 16:12:40 +0000569 if (unsigned predDelaySlots =
570 TM.getInstrInfo().getNumDelaySlots((*(MII-1))->getOpCode()))
571 {
572 assert(predDelaySlots==1 && "Not handling multiple delay slots!");
573 if (TM.getInstrInfo().isBranch((*(MII-1))->getOpCode())
574 && (AddedInstrMap.count(MInst) ||
575 AddedInstrMap[MInst].InstrnsAfter.size() > 0))
576 {
577 // Current instruction is in the delay slot of a branch and it
578 // needs spill code inserted before or after it.
579 // Move it before the preceding branch.
Chris Lattnerf726e772002-10-28 19:22:04 +0000580 InsertBefore(MInst, MBB, --MII);
Chris Lattnerf6ee49f2003-01-15 18:08:07 +0000581 MachineInstr* nopI = BuildMI(TM.getInstrInfo().getNOPOpCode(),1);
Chris Lattnerf726e772002-10-28 19:22:04 +0000582 SubstituteInPlace(nopI, MBB, MII+1); // replace orig with NOP
Vikram S. Advecb202e32002-10-11 16:12:40 +0000583 --MII; // point to MInst in new location
584 bumpIteratorBy = 2; // later skip the branch and the NOP!
585 }
586 }
587
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000588 // If there are instructions to be added, *before* this machine
589 // instruction, add them now.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000590 //
Chris Lattner7e708292002-06-25 16:13:24 +0000591 if (AddedInstrMap.count(MInst)) {
Chris Lattnerf726e772002-10-28 19:22:04 +0000592 PrependInstructions(AddedInstrMap[MInst].InstrnsBefore, MBB, MII,"");
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000593 }
Vikram S. Adve48762092002-04-25 04:34:15 +0000594
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000595 // If there are instructions to be added *after* this machine
596 // instruction, add them now
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000597 //
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000598 if (!AddedInstrMap[MInst].InstrnsAfter.empty()) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000599
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000600 // if there are delay slots for this instruction, the instructions
601 // added after it must really go after the delayed instruction(s)
602 // So, we move the InstrAfter of the current instruction to the
603 // corresponding delayed instruction
Vikram S. Advecb202e32002-10-11 16:12:40 +0000604 if (unsigned delay =
605 TM.getInstrInfo().getNumDelaySlots(MInst->getOpCode())) {
606
607 // Delayed instructions are typically branches or calls. Let's make
608 // sure this is not a branch, otherwise "insert-after" is meaningless,
609 // and should never happen for any reason (spill code, register
610 // restores, etc.).
611 assert(! TM.getInstrInfo().isBranch(MInst->getOpCode()) &&
612 ! TM.getInstrInfo().isReturn(MInst->getOpCode()) &&
613 "INTERNAL ERROR: Register allocator should not be inserting "
614 "any code after a branch or return!");
615
Vikram S. Adve48762092002-04-25 04:34:15 +0000616 move2DelayedInstr(MInst, *(MII+delay) );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000617 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000618 else {
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000619 // Here we can add the "instructions after" to the current
620 // instruction since there are no delay slots for this instruction
Chris Lattnerf726e772002-10-28 19:22:04 +0000621 AppendInstructions(AddedInstrMap[MInst].InstrnsAfter, MBB, MII,"");
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000622 } // if not delay
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000623 }
Vikram S. Advecb202e32002-10-11 16:12:40 +0000624
625 // If we mucked with the instruction order above, adjust the loop iterator
626 if (bumpIteratorBy)
627 MII = MII + bumpIteratorBy;
628
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000629 } // for each machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000630 }
631}
632
633
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000634
635//----------------------------------------------------------------------------
636// This method inserts spill code for AN operand whose LR was spilled.
637// This method may be called several times for a single machine instruction
638// if it contains many spilled operands. Each time it is called, it finds
639// a register which is not live at that instruction and also which is not
640// used by other spilled operands of the same instruction. Then it uses
641// this register temporarily to accomodate the spilled value.
642//----------------------------------------------------------------------------
643void PhyRegAlloc::insertCode4SpilledLR(const LiveRange *LR,
644 MachineInstr *MInst,
645 const BasicBlock *BB,
646 const unsigned OpNum) {
647
Vikram S. Advead9c9782002-09-28 17:02:40 +0000648 assert((! TM.getInstrInfo().isCall(MInst->getOpCode()) || OpNum == 0) &&
649 "Outgoing arg of a call must be handled elsewhere (func arg ok)");
650 assert(! TM.getInstrInfo().isReturn(MInst->getOpCode()) &&
651 "Return value of a ret must be handled elsewhere");
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000652
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000653 MachineOperand& Op = MInst->getOperand(OpNum);
654 bool isDef = MInst->operandIsDefined(OpNum);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000655 bool isDefAndUse = MInst->operandIsDefinedAndUsed(OpNum);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000656 unsigned RegType = MRI.getRegType( LR );
657 int SpillOff = LR->getSpillOffFromFP();
658 RegClass *RC = LR->getRegClass();
Chris Lattner748697d2002-02-05 04:20:12 +0000659 const ValueSet &LVSetBef = LVI->getLiveVarSetBeforeMInst(MInst, BB);
Vikram S. Adve00521d72001-11-12 23:26:35 +0000660
Chris Lattnere90fcb72002-12-28 20:35:34 +0000661 MF.getInfo()->pushTempValue(MRI.getSpilledRegSize(RegType) );
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000662
Vikram S. Advef5af6362002-07-08 23:15:32 +0000663 vector<MachineInstr*> MIBef, MIAft;
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000664 vector<MachineInstr*> AdIMid;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000665
Vikram S. Advef5af6362002-07-08 23:15:32 +0000666 // Choose a register to hold the spilled value. This may insert code
667 // before and after MInst to free up the value. If so, this code should
668 // be first and last in the spill sequence before/after MInst.
669 int TmpRegU = getUsableUniRegAtMI(RegType, &LVSetBef, MInst, MIBef, MIAft);
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000670
Vikram S. Advef5af6362002-07-08 23:15:32 +0000671 // Set the operand first so that it this register does not get used
672 // as a scratch register for later calls to getUsableUniRegAtMI below
673 MInst->SetRegForOperand(OpNum, TmpRegU);
674
675 // get the added instructions for this instruction
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000676 AddedInstrns &AI = AddedInstrMap[MInst];
Vikram S. Advef5af6362002-07-08 23:15:32 +0000677
678 // We may need a scratch register to copy the spilled value to/from memory.
679 // This may itself have to insert code to free up a scratch register.
680 // Any such code should go before (after) the spill code for a load (store).
681 int scratchRegType = -1;
682 int scratchReg = -1;
683 if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType))
684 {
Chris Lattner27a08932002-10-22 23:16:21 +0000685 scratchReg = getUsableUniRegAtMI(scratchRegType, &LVSetBef,
686 MInst, MIBef, MIAft);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000687 assert(scratchReg != MRI.getInvalidRegNum());
Chris Lattner27a08932002-10-22 23:16:21 +0000688 MInst->insertUsedReg(scratchReg);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000689 }
690
691 if (!isDef || isDefAndUse) {
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000692 // for a USE, we have to load the value of LR from stack to a TmpReg
693 // and use the TmpReg as one operand of instruction
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000694
Vikram S. Advef5af6362002-07-08 23:15:32 +0000695 // actual loading instruction(s)
696 MRI.cpMem2RegMI(AdIMid, MRI.getFramePointer(), SpillOff, TmpRegU, RegType,
697 scratchReg);
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000698
Vikram S. Advef5af6362002-07-08 23:15:32 +0000699 // the actual load should be after the instructions to free up TmpRegU
700 MIBef.insert(MIBef.end(), AdIMid.begin(), AdIMid.end());
701 AdIMid.clear();
702 }
703
704 if (isDef) { // if this is a Def
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000705 // for a DEF, we have to store the value produced by this instruction
706 // on the stack position allocated for this LR
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000707
Vikram S. Advef5af6362002-07-08 23:15:32 +0000708 // actual storing instruction(s)
709 MRI.cpReg2MemMI(AdIMid, TmpRegU, MRI.getFramePointer(), SpillOff, RegType,
710 scratchReg);
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000711
Vikram S. Advef5af6362002-07-08 23:15:32 +0000712 MIAft.insert(MIAft.begin(), AdIMid.begin(), AdIMid.end());
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000713 } // if !DEF
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000714
Vikram S. Advef5af6362002-07-08 23:15:32 +0000715 // Finally, insert the entire spill code sequences before/after MInst
716 AI.InstrnsBefore.insert(AI.InstrnsBefore.end(), MIBef.begin(), MIBef.end());
717 AI.InstrnsAfter.insert(AI.InstrnsAfter.begin(), MIAft.begin(), MIAft.end());
718
Chris Lattner7e708292002-06-25 16:13:24 +0000719 if (DEBUG_RA) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000720 cerr << "\nFor Inst:\n " << *MInst;
721 cerr << "SPILLED LR# " << LR->getUserIGNode()->getIndex();
722 cerr << "; added Instructions:";
Anand Shuklad58290e2002-07-09 19:18:56 +0000723 for_each(MIBef.begin(), MIBef.end(), std::mem_fun(&MachineInstr::dump));
724 for_each(MIAft.begin(), MIAft.end(), std::mem_fun(&MachineInstr::dump));
Chris Lattner7e708292002-06-25 16:13:24 +0000725 }
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000726}
727
728
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000729//----------------------------------------------------------------------------
730// We can use the following method to get a temporary register to be used
731// BEFORE any given machine instruction. If there is a register available,
732// this method will simply return that register and set MIBef = MIAft = NULL.
733// Otherwise, it will return a register and MIAft and MIBef will contain
734// two instructions used to free up this returned register.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000735// Returned register number is the UNIFIED register number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000736//----------------------------------------------------------------------------
737
Vikram S. Advef5af6362002-07-08 23:15:32 +0000738int PhyRegAlloc::getUsableUniRegAtMI(const int RegType,
739 const ValueSet *LVSetBef,
740 MachineInstr *MInst,
741 std::vector<MachineInstr*>& MIBef,
742 std::vector<MachineInstr*>& MIAft) {
743
Chris Lattner133f0792002-10-28 04:45:29 +0000744 RegClass* RC = getRegClassByID(MRI.getRegClassIDOfRegType(RegType));
Vikram S. Advef5af6362002-07-08 23:15:32 +0000745
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000746 int RegU = getUnusedUniRegAtMI(RC, MInst, LVSetBef);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000747
748 if (RegU == -1) {
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000749 // we couldn't find an unused register. Generate code to free up a reg by
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000750 // saving it on stack and restoring after the instruction
Vikram S. Advef5af6362002-07-08 23:15:32 +0000751
Chris Lattnere90fcb72002-12-28 20:35:34 +0000752 int TmpOff = MF.getInfo()->pushTempValue(MRI.getSpilledRegSize(RegType));
Vikram S. Adve12af1642001-11-08 04:48:50 +0000753
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000754 RegU = getUniRegNotUsedByThisInst(RC, MInst);
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000755
Vikram S. Advef5af6362002-07-08 23:15:32 +0000756 // Check if we need a scratch register to copy this register to memory.
757 int scratchRegType = -1;
758 if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType))
759 {
Chris Lattner133f0792002-10-28 04:45:29 +0000760 int scratchReg = getUsableUniRegAtMI(scratchRegType, LVSetBef,
761 MInst, MIBef, MIAft);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000762 assert(scratchReg != MRI.getInvalidRegNum());
763
764 // We may as well hold the value in the scratch register instead
765 // of copying it to memory and back. But we have to mark the
766 // register as used by this instruction, so it does not get used
767 // as a scratch reg. by another operand or anyone else.
Chris Lattner27a08932002-10-22 23:16:21 +0000768 MInst->insertUsedReg(scratchReg);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000769 MRI.cpReg2RegMI(MIBef, RegU, scratchReg, RegType);
770 MRI.cpReg2RegMI(MIAft, scratchReg, RegU, RegType);
771 }
772 else
773 { // the register can be copied directly to/from memory so do it.
774 MRI.cpReg2MemMI(MIBef, RegU, MRI.getFramePointer(), TmpOff, RegType);
775 MRI.cpMem2RegMI(MIAft, MRI.getFramePointer(), TmpOff, RegU, RegType);
776 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000777 }
Vikram S. Advef5af6362002-07-08 23:15:32 +0000778
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000779 return RegU;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000780}
781
782//----------------------------------------------------------------------------
783// This method is called to get a new unused register that can be used to
784// accomodate a spilled value.
785// This method may be called several times for a single machine instruction
786// if it contains many spilled operands. Each time it is called, it finds
787// a register which is not live at that instruction and also which is not
788// used by other spilled operands of the same instruction.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000789// Return register number is relative to the register class. NOT
790// unified number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000791//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000792int PhyRegAlloc::getUnusedUniRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000793 const MachineInstr *MInst,
Chris Lattner296b7732002-02-05 02:52:05 +0000794 const ValueSet *LVSetBef) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000795
796 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
797
Chris Lattner85c54652002-05-23 15:50:03 +0000798 std::vector<bool> &IsColorUsedArr = RC->getIsColorUsedArr();
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000799
Chris Lattner7e708292002-06-25 16:13:24 +0000800 for (unsigned i=0; i < NumAvailRegs; i++) // Reset array
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000801 IsColorUsedArr[i] = false;
802
Chris Lattner296b7732002-02-05 02:52:05 +0000803 ValueSet::const_iterator LIt = LVSetBef->begin();
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000804
805 // for each live var in live variable set after machine inst
Chris Lattner7e708292002-06-25 16:13:24 +0000806 for ( ; LIt != LVSetBef->end(); ++LIt) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000807
808 // get the live range corresponding to live var
809 LiveRange *const LRofLV = LRI.getLiveRangeForValue(*LIt );
810
811 // LR can be null if it is a const since a const
812 // doesn't have a dominating def - see Assumptions above
Chris Lattner7e708292002-06-25 16:13:24 +0000813 if (LRofLV && LRofLV->getRegClass() == RC && LRofLV->hasColor() )
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000814 IsColorUsedArr[ LRofLV->getColor() ] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000815 }
816
817 // It is possible that one operand of this MInst was already spilled
818 // and it received some register temporarily. If that's the case,
819 // it is recorded in machine operand. We must skip such registers.
820
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000821 setRelRegsUsedByThisInst(RC, MInst);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000822
Chris Lattner7e708292002-06-25 16:13:24 +0000823 for (unsigned c=0; c < NumAvailRegs; c++) // find first unused color
Chris Lattner85c54652002-05-23 15:50:03 +0000824 if (!IsColorUsedArr[c])
825 return MRI.getUnifiedRegNum(RC->getID(), c);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000826
Chris Lattner85c54652002-05-23 15:50:03 +0000827 return -1;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000828}
829
830
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000831//----------------------------------------------------------------------------
832// Get any other register in a register class, other than what is used
833// by operands of a machine instruction. Returns the unified reg number.
834//----------------------------------------------------------------------------
835int PhyRegAlloc::getUniRegNotUsedByThisInst(RegClass *RC,
Chris Lattner85c54652002-05-23 15:50:03 +0000836 const MachineInstr *MInst) {
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000837
Chris Lattner85c54652002-05-23 15:50:03 +0000838 vector<bool> &IsColorUsedArr = RC->getIsColorUsedArr();
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000839 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
840
Chris Lattner7e708292002-06-25 16:13:24 +0000841 for (unsigned i=0; i < NumAvailRegs ; i++) // Reset array
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000842 IsColorUsedArr[i] = false;
843
844 setRelRegsUsedByThisInst(RC, MInst);
845
Chris Lattner7e708292002-06-25 16:13:24 +0000846 for (unsigned c=0; c < RC->getNumOfAvailRegs(); c++)// find first unused color
Chris Lattner85c54652002-05-23 15:50:03 +0000847 if (!IsColorUsedArr[c])
848 return MRI.getUnifiedRegNum(RC->getID(), c);
849
850 assert(0 && "FATAL: No free register could be found in reg class!!");
Chris Lattner697954c2002-01-20 22:54:45 +0000851 return 0;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000852}
853
854
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000855//----------------------------------------------------------------------------
856// This method modifies the IsColorUsedArr of the register class passed to it.
857// It sets the bits corresponding to the registers used by this machine
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000858// instructions. Both explicit and implicit operands are set.
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000859//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000860void PhyRegAlloc::setRelRegsUsedByThisInst(RegClass *RC,
Vikram S. Advef5af6362002-07-08 23:15:32 +0000861 const MachineInstr *MInst ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000862
Vikram S. Advef5af6362002-07-08 23:15:32 +0000863 vector<bool> &IsColorUsedArr = RC->getIsColorUsedArr();
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000864
Vikram S. Advef5af6362002-07-08 23:15:32 +0000865 // Add the registers already marked as used by the instruction.
866 // This should include any scratch registers that are used to save
867 // values across the instruction (e.g., for saving state register values).
Chris Lattner27a08932002-10-22 23:16:21 +0000868 const vector<bool> &regsUsed = MInst->getRegsUsed();
869 for (unsigned i = 0, e = regsUsed.size(); i != e; ++i)
870 if (regsUsed[i]) {
Vikram S. Advef5af6362002-07-08 23:15:32 +0000871 unsigned classId = 0;
Chris Lattner27a08932002-10-22 23:16:21 +0000872 int classRegNum = MRI.getClassRegNum(i, classId);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000873 if (RC->getID() == classId)
874 {
875 assert(classRegNum < (int) IsColorUsedArr.size() &&
876 "Illegal register number for this reg class?");
877 IsColorUsedArr[classRegNum] = true;
878 }
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000879 }
Vikram S. Advef5af6362002-07-08 23:15:32 +0000880
881 // Now add registers allocated to the live ranges of values used in
882 // the instruction. These are not yet recorded in the instruction.
883 for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum)
884 {
885 const MachineOperand& Op = MInst->getOperand(OpNum);
886
Chris Lattner133f0792002-10-28 04:45:29 +0000887 if (MInst->getOperandType(OpNum) == MachineOperand::MO_VirtualRegister ||
888 MInst->getOperandType(OpNum) == MachineOperand::MO_CCRegister)
Vikram S. Advef5af6362002-07-08 23:15:32 +0000889 if (const Value* Val = Op.getVRegValue())
890 if (MRI.getRegClassIDOfValue(Val) == RC->getID())
891 if (Op.getAllocatedRegNum() == -1)
892 if (LiveRange *LROfVal = LRI.getLiveRangeForValue(Val))
893 if (LROfVal->hasColor() )
894 // this operand is in a LR that received a color
895 IsColorUsedArr[LROfVal->getColor()] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000896 }
Vikram S. Advef5af6362002-07-08 23:15:32 +0000897
898 // If there are implicit references, mark their allocated regs as well
899 //
900 for (unsigned z=0; z < MInst->getNumImplicitRefs(); z++)
901 if (const LiveRange*
902 LRofImpRef = LRI.getLiveRangeForValue(MInst->getImplicitRef(z)))
903 if (LRofImpRef->hasColor())
904 // this implicit reference is in a LR that received a color
905 IsColorUsedArr[LRofImpRef->getColor()] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000906}
907
908
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000909//----------------------------------------------------------------------------
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000910// If there are delay slots for an instruction, the instructions
911// added after it must really go after the delayed instruction(s).
912// So, we move the InstrAfter of that instruction to the
913// corresponding delayed instruction using the following method.
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000914
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000915//----------------------------------------------------------------------------
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000916void PhyRegAlloc::move2DelayedInstr(const MachineInstr *OrigMI,
917 const MachineInstr *DelayedMI) {
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000918
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000919 // "added after" instructions of the original instr
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000920 std::vector<MachineInstr *> &OrigAft = AddedInstrMap[OrigMI].InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000921
922 // "added instructions" of the delayed instr
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000923 AddedInstrns &DelayAdI = AddedInstrMap[DelayedMI];
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000924
925 // "added after" instructions of the delayed instr
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000926 std::vector<MachineInstr *> &DelayedAft = DelayAdI.InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000927
928 // go thru all the "added after instructions" of the original instruction
929 // and append them to the "addded after instructions" of the delayed
930 // instructions
Chris Lattner697954c2002-01-20 22:54:45 +0000931 DelayedAft.insert(DelayedAft.end(), OrigAft.begin(), OrigAft.end());
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000932
933 // empty the "added after instructions" of the original instruction
934 OrigAft.clear();
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000935}
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000936
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000937//----------------------------------------------------------------------------
938// This method prints the code with registers after register allocation is
939// complete.
940//----------------------------------------------------------------------------
941void PhyRegAlloc::printMachineCode()
942{
943
Chris Lattnerf726e772002-10-28 19:22:04 +0000944 cerr << "\n;************** Function " << Fn->getName()
Chris Lattner697954c2002-01-20 22:54:45 +0000945 << " *****************\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000946
Chris Lattnerf726e772002-10-28 19:22:04 +0000947 for (MachineFunction::iterator BBI = MF.begin(), BBE = MF.end();
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000948 BBI != BBE; ++BBI) {
Chris Lattnerf726e772002-10-28 19:22:04 +0000949 cerr << "\n"; printLabel(BBI->getBasicBlock()); cerr << ": ";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000950
951 // get the iterator for machine instructions
Chris Lattnerf726e772002-10-28 19:22:04 +0000952 MachineBasicBlock& MBB = *BBI;
953 MachineBasicBlock::iterator MII = MBB.begin();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000954
955 // iterate over all the machine instructions in BB
Chris Lattnerf726e772002-10-28 19:22:04 +0000956 for ( ; MII != MBB.end(); ++MII) {
Chris Lattnerd9512ca2002-10-29 17:35:39 +0000957 MachineInstr *MInst = *MII;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000958
Chris Lattner697954c2002-01-20 22:54:45 +0000959 cerr << "\n\t";
Chris Lattnerd9512ca2002-10-29 17:35:39 +0000960 cerr << TM.getInstrInfo().getName(MInst->getOpCode());
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000961
Chris Lattner7e708292002-06-25 16:13:24 +0000962 for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000963 MachineOperand& Op = MInst->getOperand(OpNum);
964
Chris Lattner133f0792002-10-28 04:45:29 +0000965 if (Op.getType() == MachineOperand::MO_VirtualRegister ||
966 Op.getType() == MachineOperand::MO_CCRegister /*||
967 Op.getType() == MachineOperand::MO_PCRelativeDisp*/ ) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000968
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000969 const Value *const Val = Op.getVRegValue () ;
Ruchira Sasankae727f852001-09-18 22:43:57 +0000970 // ****this code is temporary till NULL Values are fixed
Chris Lattner7e708292002-06-25 16:13:24 +0000971 if (! Val ) {
Chris Lattner697954c2002-01-20 22:54:45 +0000972 cerr << "\t<*NULL*>";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000973 continue;
974 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000975
976 // if a label or a constant
Chris Lattner7e708292002-06-25 16:13:24 +0000977 if (isa<BasicBlock>(Val)) {
Chris Lattner697954c2002-01-20 22:54:45 +0000978 cerr << "\t"; printLabel( Op.getVRegValue () );
979 } else {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000980 // else it must be a register value
981 const int RegNum = Op.getAllocatedRegNum();
982
Chris Lattner697954c2002-01-20 22:54:45 +0000983 cerr << "\t" << "%" << MRI.getUnifiedRegName( RegNum );
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000984 if (Val->hasName() )
Chris Lattner697954c2002-01-20 22:54:45 +0000985 cerr << "(" << Val->getName() << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000986 else
Chris Lattner697954c2002-01-20 22:54:45 +0000987 cerr << "(" << Val << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000988
Chris Lattner7e708292002-06-25 16:13:24 +0000989 if (Op.opIsDef() )
Chris Lattner697954c2002-01-20 22:54:45 +0000990 cerr << "*";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000991
992 const LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
Chris Lattner7e708292002-06-25 16:13:24 +0000993 if (LROfVal )
994 if (LROfVal->hasSpillOffset() )
Chris Lattner697954c2002-01-20 22:54:45 +0000995 cerr << "$";
Ruchira Sasankae727f852001-09-18 22:43:57 +0000996 }
997
998 }
Chris Lattner133f0792002-10-28 04:45:29 +0000999 else if (Op.getType() == MachineOperand::MO_MachineRegister) {
Chris Lattner697954c2002-01-20 22:54:45 +00001000 cerr << "\t" << "%" << MRI.getUnifiedRegName(Op.getMachineRegNum());
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001001 }
1002
1003 else
Chris Lattner697954c2002-01-20 22:54:45 +00001004 cerr << "\t" << Op; // use dump field
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001005 }
1006
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001007
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001008
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001009 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
Chris Lattner7e708292002-06-25 16:13:24 +00001010 if (NumOfImpRefs > 0) {
Chris Lattner697954c2002-01-20 22:54:45 +00001011 cerr << "\tImplicit:";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001012
Chris Lattner7e708292002-06-25 16:13:24 +00001013 for (unsigned z=0; z < NumOfImpRefs; z++)
Chris Lattner0665a5f2002-02-05 01:43:49 +00001014 cerr << RAV(MInst->getImplicitRef(z)) << "\t";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001015 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001016
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001017 } // for all machine instructions
1018
Chris Lattner697954c2002-01-20 22:54:45 +00001019 cerr << "\n";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001020
1021 } // for all BBs
1022
Chris Lattner697954c2002-01-20 22:54:45 +00001023 cerr << "\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001024}
1025
Ruchira Sasankae727f852001-09-18 22:43:57 +00001026
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001027//----------------------------------------------------------------------------
1028
1029//----------------------------------------------------------------------------
1030void PhyRegAlloc::colorIncomingArgs()
1031{
Chris Lattnerf726e772002-10-28 19:22:04 +00001032 MRI.colorMethodArgs(Fn, LRI, &AddedInstrAtEntry);
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001033}
1034
Ruchira Sasankae727f852001-09-18 22:43:57 +00001035
1036//----------------------------------------------------------------------------
1037// Used to generate a label for a basic block
1038//----------------------------------------------------------------------------
Chris Lattnerf726e772002-10-28 19:22:04 +00001039void PhyRegAlloc::printLabel(const Value *Val) {
Chris Lattner697954c2002-01-20 22:54:45 +00001040 if (Val->hasName())
1041 cerr << Val->getName();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001042 else
Chris Lattnerf726e772002-10-28 19:22:04 +00001043 cerr << "Label" << Val;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001044}
1045
1046
Ruchira Sasankae727f852001-09-18 22:43:57 +00001047//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001048// This method calls setSugColorUsable method of each live range. This
1049// will determine whether the suggested color of LR is really usable.
1050// A suggested color is not usable when the suggested color is volatile
1051// AND when there are call interferences
1052//----------------------------------------------------------------------------
1053
1054void PhyRegAlloc::markUnusableSugColors()
1055{
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001056 // hash map iterator
1057 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
1058 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
1059
Chris Lattner7e708292002-06-25 16:13:24 +00001060 for (; HMI != HMIEnd ; ++HMI ) {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001061 if (HMI->first) {
1062 LiveRange *L = HMI->second; // get the LiveRange
1063 if (L) {
Chris Lattner7e708292002-06-25 16:13:24 +00001064 if (L->hasSuggestedColor()) {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001065 int RCID = L->getRegClass()->getID();
Chris Lattner7e708292002-06-25 16:13:24 +00001066 if (MRI.isRegVolatile( RCID, L->getSuggestedColor()) &&
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001067 L->isCallInterference() )
1068 L->setSuggestedColorUsable( false );
1069 else
1070 L->setSuggestedColorUsable( true );
1071 }
1072 } // if L->hasSuggestedColor()
1073 }
1074 } // for all LR's in hash map
1075}
1076
1077
1078
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001079//----------------------------------------------------------------------------
1080// The following method will set the stack offsets of the live ranges that
1081// are decided to be spillled. This must be called just after coloring the
1082// LRs using the graph coloring algo. For each live range that is spilled,
1083// this method allocate a new spill position on the stack.
1084//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001085
Chris Lattner37730942002-02-05 03:52:29 +00001086void PhyRegAlloc::allocateStackSpace4SpilledLRs() {
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001087 if (DEBUG_RA) cerr << "\nSetting LR stack offsets for spills...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001088
Chris Lattner37730942002-02-05 03:52:29 +00001089 LiveRangeMapType::const_iterator HMI = LRI.getLiveRangeMap()->begin();
1090 LiveRangeMapType::const_iterator HMIEnd = LRI.getLiveRangeMap()->end();
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001091
Chris Lattner7e708292002-06-25 16:13:24 +00001092 for ( ; HMI != HMIEnd ; ++HMI) {
Chris Lattner37730942002-02-05 03:52:29 +00001093 if (HMI->first && HMI->second) {
1094 LiveRange *L = HMI->second; // get the LiveRange
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001095 if (!L->hasColor()) { // NOTE: ** allocating the size of long Type **
Chris Lattnere90fcb72002-12-28 20:35:34 +00001096 int stackOffset = MF.getInfo()->allocateSpilledValue(Type::LongTy);
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001097 L->setSpillOffFromFP(stackOffset);
1098 if (DEBUG_RA)
1099 cerr << " LR# " << L->getUserIGNode()->getIndex()
1100 << ": stack-offset = " << stackOffset << "\n";
1101 }
Chris Lattner37730942002-02-05 03:52:29 +00001102 }
1103 } // for all LR's in hash map
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001104}
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001105
1106
1107
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001108//----------------------------------------------------------------------------
Ruchira Sasankae727f852001-09-18 22:43:57 +00001109// The entry pont to Register Allocation
1110//----------------------------------------------------------------------------
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001111
1112void PhyRegAlloc::allocateRegisters()
1113{
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001114
1115 // make sure that we put all register classes into the RegClassList
1116 // before we call constructLiveRanges (now done in the constructor of
1117 // PhyRegAlloc class).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001118 //
1119 LRI.constructLiveRanges(); // create LR info
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001120
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001121 if (DEBUG_RA >= RA_DEBUG_LiveRanges)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001122 LRI.printLiveRanges();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001123
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001124 createIGNodeListsAndIGs(); // create IGNode list and IGs
1125
1126 buildInterferenceGraphs(); // build IGs in all reg classes
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001127
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001128
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001129 if (DEBUG_RA >= RA_DEBUG_LiveRanges) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001130 // print all LRs in all reg classes
Chris Lattner7e708292002-06-25 16:13:24 +00001131 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
1132 RegClassList[rc]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001133
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001134 // print IGs in all register classes
Chris Lattner7e708292002-06-25 16:13:24 +00001135 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
1136 RegClassList[rc]->printIG();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001137 }
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001138
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001139 LRI.coalesceLRs(); // coalesce all live ranges
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +00001140
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001141 if (DEBUG_RA >= RA_DEBUG_LiveRanges) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001142 // print all LRs in all reg classes
Chris Lattnerf726e772002-10-28 19:22:04 +00001143 for (unsigned rc=0; rc < NumOfRegClasses; rc++)
1144 RegClassList[rc]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001145
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001146 // print IGs in all register classes
Chris Lattnerf726e772002-10-28 19:22:04 +00001147 for (unsigned rc=0; rc < NumOfRegClasses; rc++)
1148 RegClassList[rc]->printIG();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001149 }
1150
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001151
1152 // mark un-usable suggested color before graph coloring algorithm.
1153 // When this is done, the graph coloring algo will not reserve
1154 // suggested color unnecessarily - they can be used by another LR
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001155 //
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001156 markUnusableSugColors();
1157
1158 // color all register classes using the graph coloring algo
Chris Lattner7e708292002-06-25 16:13:24 +00001159 for (unsigned rc=0; rc < NumOfRegClasses ; rc++)
Chris Lattnerf726e772002-10-28 19:22:04 +00001160 RegClassList[rc]->colorAllRegs();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001161
Chris Lattnere90fcb72002-12-28 20:35:34 +00001162 // Atter graph coloring, if some LRs did not receive a color (i.e, spilled)
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001163 // a poistion for such spilled LRs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001164 //
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001165 allocateStackSpace4SpilledLRs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001166
Chris Lattnere90fcb72002-12-28 20:35:34 +00001167 MF.getInfo()->popAllTempValues(); // TODO **Check
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001168
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001169 // color incoming args - if the correct color was not received
1170 // insert code to copy to the correct register
1171 //
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001172 colorIncomingArgs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001173
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001174 // Now update the machine code with register names and add any
1175 // additional code inserted by the register allocator to the instruction
1176 // stream
1177 //
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001178 updateMachineCode();
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001179
Chris Lattner045e7c82001-09-19 16:26:23 +00001180 if (DEBUG_RA) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001181 cerr << "\n**** Machine Code After Register Allocation:\n\n";
Chris Lattnerf726e772002-10-28 19:22:04 +00001182 MF.dump();
Chris Lattner045e7c82001-09-19 16:26:23 +00001183 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001184}
1185
Ruchira Sasankae727f852001-09-18 22:43:57 +00001186
1187