blob: 409916fb02bcfc40de9192416db308ca3f42edd3 [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"
Chris Lattner4309e732003-01-15 19:57:07 +00008#include "RegAllocCommon.h"
Chris Lattner9d4ed152003-01-15 21:14:01 +00009#include "RegClass.h"
Chris Lattnercb6b4bd2002-10-29 16:51:05 +000010#include "llvm/CodeGen/IGNode.h"
Vikram S. Adve12af1642001-11-08 04:48:50 +000011#include "llvm/CodeGen/PhyRegAlloc.h"
Chris Lattnerf6ee49f2003-01-15 18:08:07 +000012#include "llvm/CodeGen/MachineInstrBuilder.h"
Vikram S. Advedabb41d2002-05-19 15:29:31 +000013#include "llvm/CodeGen/MachineInstrAnnot.h"
Misha Brukmanfce11432002-10-28 00:28:31 +000014#include "llvm/CodeGen/MachineFunction.h"
Chris Lattnere90fcb72002-12-28 20:35:34 +000015#include "llvm/CodeGen/MachineFunctionInfo.h"
Chris Lattner92ba2aa2003-01-14 23:05:08 +000016#include "llvm/CodeGen/FunctionLiveVarInfo.h"
Chris Lattner14ab1ce2002-02-04 17:48:00 +000017#include "llvm/Analysis/LoopInfo.h"
Vikram S. Adve12af1642001-11-08 04:48:50 +000018#include "llvm/Target/TargetMachine.h"
Chris Lattner8bd66e62002-12-28 21:00:25 +000019#include "llvm/Target/TargetFrameInfo.h"
Chris Lattner3501fea2003-01-14 22:00:31 +000020#include "llvm/Target/TargetInstrInfo.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000021#include "llvm/Function.h"
Chris Lattner37730942002-02-05 03:52:29 +000022#include "llvm/Type.h"
Vikram S. Advedabb41d2002-05-19 15:29:31 +000023#include "llvm/iOther.h"
Vikram S. Advef5af6362002-07-08 23:15:32 +000024#include "Support/STLExtras.h"
Chris Lattner4bc23482002-09-15 07:07:55 +000025#include "Support/CommandLine.h"
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000026#include <math.h>
Chris Lattner697954c2002-01-20 22:54:45 +000027using std::cerr;
Anand Shuklacfb22d32002-06-25 20:55:50 +000028using std::vector;
Vikram S. Adve12af1642001-11-08 04:48:50 +000029
Chris Lattner70e60cb2002-05-22 17:08:27 +000030RegAllocDebugLevel_t DEBUG_RA;
Vikram S. Adve39c94e12002-09-14 23:05:33 +000031
Chris Lattner5ff62e92002-07-22 02:10:13 +000032static cl::opt<RegAllocDebugLevel_t, true>
33DRA_opt("dregalloc", cl::Hidden, cl::location(DEBUG_RA),
34 cl::desc("enable register allocation debugging information"),
35 cl::values(
Vikram S. Adve39c94e12002-09-14 23:05:33 +000036 clEnumValN(RA_DEBUG_None , "n", "disable debug output"),
37 clEnumValN(RA_DEBUG_Results, "y", "debug output for allocation results"),
38 clEnumValN(RA_DEBUG_Coloring, "c", "debug output for graph coloring step"),
39 clEnumValN(RA_DEBUG_Interference,"ig","debug output for interference graphs"),
40 clEnumValN(RA_DEBUG_LiveRanges , "lr","debug output for live ranges"),
41 clEnumValN(RA_DEBUG_Verbose, "v", "extra debug output"),
Chris Lattner5ff62e92002-07-22 02:10:13 +000042 0));
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000043
Chris Lattner2f9b28e2002-02-04 15:54:09 +000044//----------------------------------------------------------------------------
45// RegisterAllocation pass front end...
46//----------------------------------------------------------------------------
47namespace {
Chris Lattnerf57b8452002-04-27 06:56:12 +000048 class RegisterAllocator : public FunctionPass {
Chris Lattner2f9b28e2002-02-04 15:54:09 +000049 TargetMachine &Target;
50 public:
51 inline RegisterAllocator(TargetMachine &T) : Target(T) {}
Chris Lattner96c466b2002-04-29 14:57:45 +000052
53 const char *getPassName() const { return "Register Allocation"; }
Chris Lattner6dd98a62002-02-04 00:33:08 +000054
Chris Lattner7e708292002-06-25 16:13:24 +000055 bool runOnFunction(Function &F) {
Chris Lattner2f9b28e2002-02-04 15:54:09 +000056 if (DEBUG_RA)
Chris Lattner7e708292002-06-25 16:13:24 +000057 cerr << "\n********* Function "<< F.getName() << " ***********\n";
Chris Lattner2f9b28e2002-02-04 15:54:09 +000058
Chris Lattner7e708292002-06-25 16:13:24 +000059 PhyRegAlloc PRA(&F, Target, &getAnalysis<FunctionLiveVarInfo>(),
Chris Lattner1b7f7dc2002-04-28 16:21:30 +000060 &getAnalysis<LoopInfo>());
Chris Lattner2f9b28e2002-02-04 15:54:09 +000061 PRA.allocateRegisters();
62
63 if (DEBUG_RA) cerr << "\nRegister allocation complete!\n";
64 return false;
65 }
Chris Lattner4911c352002-02-04 17:39:42 +000066
Chris Lattnerf57b8452002-04-27 06:56:12 +000067 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerdd5b4952002-08-08 19:01:28 +000068 AU.addRequired<LoopInfo>();
69 AU.addRequired<FunctionLiveVarInfo>();
Chris Lattner4911c352002-02-04 17:39:42 +000070 }
Chris Lattner2f9b28e2002-02-04 15:54:09 +000071 };
Chris Lattner6dd98a62002-02-04 00:33:08 +000072}
73
Chris Lattnerf57b8452002-04-27 06:56:12 +000074Pass *getRegisterAllocator(TargetMachine &T) {
Chris Lattner2f9b28e2002-02-04 15:54:09 +000075 return new RegisterAllocator(T);
76}
Chris Lattner6dd98a62002-02-04 00:33:08 +000077
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000078//----------------------------------------------------------------------------
79// Constructor: Init local composite objects and create register classes.
80//----------------------------------------------------------------------------
Chris Lattner1b7f7dc2002-04-28 16:21:30 +000081PhyRegAlloc::PhyRegAlloc(Function *F, const TargetMachine& tm,
82 FunctionLiveVarInfo *Lvi, LoopInfo *LDC)
Chris Lattnerf726e772002-10-28 19:22:04 +000083 : TM(tm), Fn(F), MF(MachineFunction::get(F)), LVI(Lvi),
84 LRI(F, tm, RegClassList), MRI(tm.getRegInfo()),
85 NumOfRegClasses(MRI.getNumOfRegClasses()), LoopDepthCalc(LDC) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +000086
87 // create each RegisterClass and put in RegClassList
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000088 //
Chris Lattnerf726e772002-10-28 19:22:04 +000089 for (unsigned rc=0; rc != NumOfRegClasses; rc++)
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000090 RegClassList.push_back(new RegClass(F, MRI.getMachineRegClass(rc),
91 &ResColList));
Ruchira Sasanka8e604792001-09-14 21:18:34 +000092}
93
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000094
95//----------------------------------------------------------------------------
96// Destructor: Deletes register classes
97//----------------------------------------------------------------------------
98PhyRegAlloc::~PhyRegAlloc() {
Chris Lattner7e708292002-06-25 16:13:24 +000099 for ( unsigned rc=0; rc < NumOfRegClasses; rc++)
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000100 delete RegClassList[rc];
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000101
102 AddedInstrMap.clear();
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000103}
104
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000105//----------------------------------------------------------------------------
106// This method initally creates interference graphs (one in each reg class)
107// and IGNodeList (one in each IG). The actual nodes will be pushed later.
108//----------------------------------------------------------------------------
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000109void PhyRegAlloc::createIGNodeListsAndIGs() {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000110 if (DEBUG_RA >= RA_DEBUG_LiveRanges) cerr << "Creating LR lists ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000111
112 // hash map iterator
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000113 LiveRangeMapType::const_iterator HMI = LRI.getLiveRangeMap()->begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000114
115 // hash map end
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000116 LiveRangeMapType::const_iterator HMIEnd = LRI.getLiveRangeMap()->end();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000117
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000118 for (; HMI != HMIEnd ; ++HMI ) {
119 if (HMI->first) {
120 LiveRange *L = HMI->second; // get the LiveRange
121 if (!L) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000122 if (DEBUG_RA)
123 cerr << "\n**** ?!?WARNING: NULL LIVE RANGE FOUND FOR: "
124 << RAV(HMI->first) << "****\n";
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000125 continue;
126 }
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000127
128 // if the Value * is not null, and LR is not yet written to the IGNodeList
Chris Lattner7e708292002-06-25 16:13:24 +0000129 if (!(L->getUserIGNode()) ) {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000130 RegClass *const RC = // RegClass of first value in the LR
131 RegClassList[ L->getRegClass()->getID() ];
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000132 RC->addLRToIG(L); // add this LR to an IG
133 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000134 }
135 }
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000136
137 // init RegClassList
Chris Lattner7e708292002-06-25 16:13:24 +0000138 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000139 RegClassList[rc]->createInterferenceGraph();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000140
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000141 if (DEBUG_RA >= RA_DEBUG_LiveRanges) cerr << "LRLists Created!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000142}
143
144
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000145//----------------------------------------------------------------------------
146// This method will add all interferences at for a given instruction.
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000147// Interence occurs only if the LR of Def (Inst or Arg) is of the same reg
148// class as that of live var. The live var passed to this function is the
149// LVset AFTER the instruction
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000150//----------------------------------------------------------------------------
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000151
Chris Lattner296b7732002-02-05 02:52:05 +0000152void PhyRegAlloc::addInterference(const Value *Def,
153 const ValueSet *LVSet,
154 bool isCallInst) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000155
Chris Lattner296b7732002-02-05 02:52:05 +0000156 ValueSet::const_iterator LIt = LVSet->begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000157
158 // get the live range of instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000159 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000160 const LiveRange *const LROfDef = LRI.getLiveRangeForValue( Def );
161
162 IGNode *const IGNodeOfDef = LROfDef->getUserIGNode();
163 assert( IGNodeOfDef );
164
165 RegClass *const RCOfDef = LROfDef->getRegClass();
166
167 // for each live var in live variable set
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000168 //
Chris Lattner7e708292002-06-25 16:13:24 +0000169 for ( ; LIt != LVSet->end(); ++LIt) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000170
Vikram S. Advef5af6362002-07-08 23:15:32 +0000171 if (DEBUG_RA >= RA_DEBUG_Verbose)
Chris Lattner0665a5f2002-02-05 01:43:49 +0000172 cerr << "< Def=" << RAV(Def) << ", Lvar=" << RAV(*LIt) << "> ";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000173
174 // get the live range corresponding to live var
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000175 //
Chris Lattner0665a5f2002-02-05 01:43:49 +0000176 LiveRange *LROfVar = LRI.getLiveRangeForValue(*LIt);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000177
178 // LROfVar can be null if it is a const since a const
179 // doesn't have a dominating def - see Assumptions above
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000180 //
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000181 if (LROfVar)
182 if (LROfDef != LROfVar) // do not set interf for same LR
183 if (RCOfDef == LROfVar->getRegClass()) // 2 reg classes are the same
184 RCOfDef->setInterference( LROfDef, LROfVar);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000185 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000186}
187
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000188
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000189
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000190//----------------------------------------------------------------------------
191// For a call instruction, this method sets the CallInterference flag in
192// the LR of each variable live int the Live Variable Set live after the
193// call instruction (except the return value of the call instruction - since
194// the return value does not interfere with that call itself).
195//----------------------------------------------------------------------------
196
197void PhyRegAlloc::setCallInterferences(const MachineInstr *MInst,
Chris Lattner296b7732002-02-05 02:52:05 +0000198 const ValueSet *LVSetAft) {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000199
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000200 if (DEBUG_RA >= RA_DEBUG_Interference)
Chris Lattner697954c2002-01-20 22:54:45 +0000201 cerr << "\n For call inst: " << *MInst;
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000202
Chris Lattner296b7732002-02-05 02:52:05 +0000203 ValueSet::const_iterator LIt = LVSetAft->begin();
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000204
205 // for each live var in live variable set after machine inst
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000206 //
Chris Lattner7e708292002-06-25 16:13:24 +0000207 for ( ; LIt != LVSetAft->end(); ++LIt) {
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000208
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000209 // get the live range corresponding to live var
210 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000211 LiveRange *const LR = LRI.getLiveRangeForValue(*LIt );
212
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000213 // LR can be null if it is a const since a const
214 // doesn't have a dominating def - see Assumptions above
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000215 //
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000216 if (LR ) {
217 if (DEBUG_RA >= RA_DEBUG_Interference) {
218 cerr << "\n\tLR after Call: ";
219 printSet(*LR);
220 }
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000221 LR->setCallInterference();
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000222 if (DEBUG_RA >= RA_DEBUG_Interference) {
223 cerr << "\n ++After adding call interference for LR: " ;
Chris Lattner296b7732002-02-05 02:52:05 +0000224 printSet(*LR);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000225 }
226 }
227
228 }
229
Vikram S. Adve1a53f032002-03-31 18:54:37 +0000230 // Now find the LR of the return value of the call
231 // We do this because, we look at the LV set *after* the instruction
232 // to determine, which LRs must be saved across calls. The return value
233 // of the call is live in this set - but it does not interfere with call
234 // (i.e., we can allocate a volatile register to the return value)
235 //
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000236 CallArgsDescriptor* argDesc = CallArgsDescriptor::get(MInst);
237
238 if (const Value *RetVal = argDesc->getReturnValue()) {
Vikram S. Adve1a53f032002-03-31 18:54:37 +0000239 LiveRange *RetValLR = LRI.getLiveRangeForValue( RetVal );
240 assert( RetValLR && "No LR for RetValue of call");
241 RetValLR->clearCallInterference();
242 }
243
244 // If the CALL is an indirect call, find the LR of the function pointer.
245 // That has a call interference because it conflicts with outgoing args.
Chris Lattner7e708292002-06-25 16:13:24 +0000246 if (const Value *AddrVal = argDesc->getIndirectFuncPtr()) {
Vikram S. Adve1a53f032002-03-31 18:54:37 +0000247 LiveRange *AddrValLR = LRI.getLiveRangeForValue( AddrVal );
248 assert( AddrValLR && "No LR for indirect addr val of call");
249 AddrValLR->setCallInterference();
250 }
251
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000252}
253
254
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000255
256
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000257//----------------------------------------------------------------------------
258// This method will walk thru code and create interferences in the IG of
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000259// each RegClass. Also, this method calculates the spill cost of each
260// Live Range (it is done in this method to save another pass over the code).
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000261//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000262void PhyRegAlloc::buildInterferenceGraphs()
263{
264
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000265 if (DEBUG_RA >= RA_DEBUG_Interference)
266 cerr << "Creating interference graphs ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000267
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000268 unsigned BBLoopDepthCost;
Chris Lattnerf726e772002-10-28 19:22:04 +0000269 for (MachineFunction::iterator BBI = MF.begin(), BBE = MF.end();
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000270 BBI != BBE; ++BBI) {
Chris Lattnerf726e772002-10-28 19:22:04 +0000271 const MachineBasicBlock &MBB = *BBI;
272 const BasicBlock *BB = MBB.getBasicBlock();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000273
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000274 // find the 10^(loop_depth) of this BB
275 //
Chris Lattnerf726e772002-10-28 19:22:04 +0000276 BBLoopDepthCost = (unsigned)pow(10.0, LoopDepthCalc->getLoopDepth(BB));
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000277
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000278 // get the iterator for machine instructions
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000279 //
Chris Lattnerf726e772002-10-28 19:22:04 +0000280 MachineBasicBlock::const_iterator MII = MBB.begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000281
282 // iterate over all the machine instructions in BB
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000283 //
Chris Lattnerf726e772002-10-28 19:22:04 +0000284 for ( ; MII != MBB.end(); ++MII) {
285 const MachineInstr *MInst = *MII;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000286
287 // get the LV set after the instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000288 //
Chris Lattnerf726e772002-10-28 19:22:04 +0000289 const ValueSet &LVSetAI = LVI->getLiveVarSetAfterMInst(MInst, BB);
290 bool isCallInst = TM.getInstrInfo().isCall(MInst->getOpCode());
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000291
Chris Lattner7e708292002-06-25 16:13:24 +0000292 if (isCallInst ) {
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000293 // set the isCallInterference flag of each live range wich extends
294 // accross this call instruction. This information is used by graph
295 // coloring algo to avoid allocating volatile colors to live ranges
296 // that span across calls (since they have to be saved/restored)
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000297 //
Chris Lattner748697d2002-02-05 04:20:12 +0000298 setCallInterferences(MInst, &LVSetAI);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000299 }
300
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000301 // iterate over all MI operands to find defs
302 //
Chris Lattner2f898d22002-02-05 06:02:59 +0000303 for (MachineInstr::const_val_op_iterator OpI = MInst->begin(),
304 OpE = MInst->end(); OpI != OpE; ++OpI) {
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000305 if (OpI.isDefOnly() || OpI.isDefAndUse()) // create a new LR since def
Chris Lattner748697d2002-02-05 04:20:12 +0000306 addInterference(*OpI, &LVSetAI, isCallInst);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000307
308 // Calculate the spill cost of each live range
309 //
Chris Lattner2f898d22002-02-05 06:02:59 +0000310 LiveRange *LR = LRI.getLiveRangeForValue(*OpI);
311 if (LR) LR->addSpillCost(BBLoopDepthCost);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000312 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000313
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000314
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000315 // if there are multiple defs in this instruction e.g. in SETX
316 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000317 if (TM.getInstrInfo().isPseudoInstr(MInst->getOpCode()))
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000318 addInterf4PseudoInstr(MInst);
319
320
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000321 // Also add interference for any implicit definitions in a machine
322 // instr (currently, only calls have this).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000323 //
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000324 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000325 for (unsigned z=0; z < NumOfImpRefs; z++)
326 if (MInst->getImplicitOp(z).opIsDefOnly() ||
327 MInst->getImplicitOp(z).opIsDefAndUse())
328 addInterference( MInst->getImplicitRef(z), &LVSetAI, isCallInst );
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +0000329
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000330 } // for all machine instructions in BB
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000331 } // for all BBs in function
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000332
333
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000334 // add interferences for function arguments. Since there are no explict
335 // defs in the function for args, we have to add them manually
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000336 //
337 addInterferencesForArgs();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000338
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000339 if (DEBUG_RA >= RA_DEBUG_Interference)
340 cerr << "Interference graphs calculated!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000341}
342
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000343
344
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000345//--------------------------------------------------------------------------
346// Pseudo instructions will be exapnded to multiple instructions by the
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000347// assembler. Consequently, all the opernds must get distinct registers.
348// Therefore, we mark all operands of a pseudo instruction as they interfere
349// with one another.
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000350//--------------------------------------------------------------------------
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000351void PhyRegAlloc::addInterf4PseudoInstr(const MachineInstr *MInst) {
352
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000353 bool setInterf = false;
354
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000355 // iterate over MI operands to find defs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000356 //
Chris Lattner2f898d22002-02-05 06:02:59 +0000357 for (MachineInstr::const_val_op_iterator It1 = MInst->begin(),
358 ItE = MInst->end(); It1 != ItE; ++It1) {
359 const LiveRange *LROfOp1 = LRI.getLiveRangeForValue(*It1);
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000360 assert((LROfOp1 || !It1.isUseOnly())&& "No LR for Def in PSEUDO insruction");
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000361
Chris Lattner2f898d22002-02-05 06:02:59 +0000362 MachineInstr::const_val_op_iterator It2 = It1;
Chris Lattner7e708292002-06-25 16:13:24 +0000363 for (++It2; It2 != ItE; ++It2) {
Chris Lattner2f898d22002-02-05 06:02:59 +0000364 const LiveRange *LROfOp2 = LRI.getLiveRangeForValue(*It2);
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000365
Chris Lattner2f898d22002-02-05 06:02:59 +0000366 if (LROfOp2) {
367 RegClass *RCOfOp1 = LROfOp1->getRegClass();
368 RegClass *RCOfOp2 = LROfOp2->getRegClass();
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000369
Chris Lattner7e708292002-06-25 16:13:24 +0000370 if (RCOfOp1 == RCOfOp2 ){
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000371 RCOfOp1->setInterference( LROfOp1, LROfOp2 );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000372 setInterf = true;
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000373 }
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000374 } // if Op2 has a LR
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000375 } // for all other defs in machine instr
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000376 } // for all operands in an instruction
377
Chris Lattner2f898d22002-02-05 06:02:59 +0000378 if (!setInterf && MInst->getNumOperands() > 2) {
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000379 cerr << "\nInterf not set for any operand in pseudo instr:\n";
380 cerr << *MInst;
381 assert(0 && "Interf not set for pseudo instr with > 2 operands" );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000382 }
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000383}
384
385
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000386
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000387//----------------------------------------------------------------------------
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000388// This method will add interferences for incoming arguments to a function.
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000389//----------------------------------------------------------------------------
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000390
Chris Lattner296b7732002-02-05 02:52:05 +0000391void PhyRegAlloc::addInterferencesForArgs() {
392 // get the InSet of root BB
Chris Lattnerf726e772002-10-28 19:22:04 +0000393 const ValueSet &InSet = LVI->getInSetOfBB(&Fn->front());
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000394
Chris Lattnerf726e772002-10-28 19:22:04 +0000395 for (Function::const_aiterator AI = Fn->abegin(); AI != Fn->aend(); ++AI) {
Chris Lattner7e708292002-06-25 16:13:24 +0000396 // add interferences between args and LVars at start
397 addInterference(AI, &InSet, false);
398
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000399 if (DEBUG_RA >= RA_DEBUG_Interference)
Chris Lattner7e708292002-06-25 16:13:24 +0000400 cerr << " - %% adding interference for argument " << RAV(AI) << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000401 }
402}
403
404
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000405//----------------------------------------------------------------------------
406// This method is called after register allocation is complete to set the
407// allocated reisters in the machine code. This code will add register numbers
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000408// to MachineOperands that contain a Value. Also it calls target specific
409// methods to produce caller saving instructions. At the end, it adds all
410// additional instructions produced by the register allocator to the
411// instruction stream.
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000412//----------------------------------------------------------------------------
Vikram S. Adve48762092002-04-25 04:34:15 +0000413
414//-----------------------------
415// Utility functions used below
416//-----------------------------
417inline void
Vikram S. Advecb202e32002-10-11 16:12:40 +0000418InsertBefore(MachineInstr* newMI,
Chris Lattnerf726e772002-10-28 19:22:04 +0000419 MachineBasicBlock& MBB,
Chris Lattner32be9f62002-10-28 01:41:27 +0000420 MachineBasicBlock::iterator& MII)
Vikram S. Advecb202e32002-10-11 16:12:40 +0000421{
Chris Lattnerf726e772002-10-28 19:22:04 +0000422 MII = MBB.insert(MII, newMI);
Vikram S. Advecb202e32002-10-11 16:12:40 +0000423 ++MII;
424}
425
426inline void
427InsertAfter(MachineInstr* newMI,
Chris Lattnerf726e772002-10-28 19:22:04 +0000428 MachineBasicBlock& MBB,
Chris Lattner32be9f62002-10-28 01:41:27 +0000429 MachineBasicBlock::iterator& MII)
Vikram S. Advecb202e32002-10-11 16:12:40 +0000430{
431 ++MII; // insert before the next instruction
Chris Lattnerf726e772002-10-28 19:22:04 +0000432 MII = MBB.insert(MII, newMI);
Vikram S. Advecb202e32002-10-11 16:12:40 +0000433}
434
435inline void
436SubstituteInPlace(MachineInstr* newMI,
Chris Lattnerf726e772002-10-28 19:22:04 +0000437 MachineBasicBlock& MBB,
Chris Lattner32be9f62002-10-28 01:41:27 +0000438 MachineBasicBlock::iterator MII)
Vikram S. Advecb202e32002-10-11 16:12:40 +0000439{
440 *MII = newMI;
441}
442
443inline void
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000444PrependInstructions(vector<MachineInstr *> &IBef,
Chris Lattnerf726e772002-10-28 19:22:04 +0000445 MachineBasicBlock& MBB,
Chris Lattner32be9f62002-10-28 01:41:27 +0000446 MachineBasicBlock::iterator& MII,
Vikram S. Adve48762092002-04-25 04:34:15 +0000447 const std::string& msg)
448{
449 if (!IBef.empty())
450 {
451 MachineInstr* OrigMI = *MII;
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000452 std::vector<MachineInstr *>::iterator AdIt;
Vikram S. Adve48762092002-04-25 04:34:15 +0000453 for (AdIt = IBef.begin(); AdIt != IBef.end() ; ++AdIt)
454 {
455 if (DEBUG_RA) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000456 if (OrigMI) cerr << "For MInst:\n " << *OrigMI;
457 cerr << msg << "PREPENDed instr:\n " << **AdIt << "\n";
Vikram S. Adve48762092002-04-25 04:34:15 +0000458 }
Chris Lattnerf726e772002-10-28 19:22:04 +0000459 InsertBefore(*AdIt, MBB, MII);
Vikram S. Adve48762092002-04-25 04:34:15 +0000460 }
461 }
462}
463
464inline void
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000465AppendInstructions(std::vector<MachineInstr *> &IAft,
Chris Lattnerf726e772002-10-28 19:22:04 +0000466 MachineBasicBlock& MBB,
Chris Lattner32be9f62002-10-28 01:41:27 +0000467 MachineBasicBlock::iterator& MII,
Vikram S. Adve48762092002-04-25 04:34:15 +0000468 const std::string& msg)
469{
470 if (!IAft.empty())
471 {
472 MachineInstr* OrigMI = *MII;
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000473 std::vector<MachineInstr *>::iterator AdIt;
Chris Lattner7e708292002-06-25 16:13:24 +0000474 for ( AdIt = IAft.begin(); AdIt != IAft.end() ; ++AdIt )
Vikram S. Adve48762092002-04-25 04:34:15 +0000475 {
Chris Lattner7e708292002-06-25 16:13:24 +0000476 if (DEBUG_RA) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000477 if (OrigMI) cerr << "For MInst:\n " << *OrigMI;
478 cerr << msg << "APPENDed instr:\n " << **AdIt << "\n";
Vikram S. Adve48762092002-04-25 04:34:15 +0000479 }
Chris Lattnerf726e772002-10-28 19:22:04 +0000480 InsertAfter(*AdIt, MBB, MII);
Vikram S. Adve48762092002-04-25 04:34:15 +0000481 }
482 }
483}
484
485
Chris Lattnerf726e772002-10-28 19:22:04 +0000486void PhyRegAlloc::updateMachineCode() {
Chris Lattner7e708292002-06-25 16:13:24 +0000487 // Insert any instructions needed at method entry
Chris Lattnerf726e772002-10-28 19:22:04 +0000488 MachineBasicBlock::iterator MII = MF.front().begin();
489 PrependInstructions(AddedInstrAtEntry.InstrnsBefore, MF.front(), MII,
Chris Lattner7e708292002-06-25 16:13:24 +0000490 "At function entry: \n");
491 assert(AddedInstrAtEntry.InstrnsAfter.empty() &&
492 "InstrsAfter should be unnecessary since we are just inserting at "
493 "the function entry point here.");
Vikram S. Adve48762092002-04-25 04:34:15 +0000494
Chris Lattnerf726e772002-10-28 19:22:04 +0000495 for (MachineFunction::iterator BBI = MF.begin(), BBE = MF.end();
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000496 BBI != BBE; ++BBI) {
Vikram S. Advecb202e32002-10-11 16:12:40 +0000497
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000498 // iterate over all the machine instructions in BB
Chris Lattnerf726e772002-10-28 19:22:04 +0000499 MachineBasicBlock &MBB = *BBI;
500 for (MachineBasicBlock::iterator MII = MBB.begin();
501 MII != MBB.end(); ++MII) {
502
Vikram S. Adve48762092002-04-25 04:34:15 +0000503 MachineInstr *MInst = *MII;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000504 unsigned Opcode = MInst->getOpCode();
505
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000506 // do not process Phis
Vikram S. Adve23a4c8f2002-03-18 03:37:19 +0000507 if (TM.getInstrInfo().isDummyPhiInstr(Opcode))
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000508 continue;
509
Vikram S. Advef5af6362002-07-08 23:15:32 +0000510 // Reset tmp stack positions so they can be reused for each machine instr.
Chris Lattnere90fcb72002-12-28 20:35:34 +0000511 MF.getInfo()->popAllTempValues();
Vikram S. Advef5af6362002-07-08 23:15:32 +0000512
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000513 // Now insert speical instructions (if necessary) for call/return
514 // instructions.
515 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000516 if (TM.getInstrInfo().isCall(Opcode) ||
Chris Lattnerf726e772002-10-28 19:22:04 +0000517 TM.getInstrInfo().isReturn(Opcode)) {
518 AddedInstrns &AI = AddedInstrMap[MInst];
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000519
Chris Lattnerf726e772002-10-28 19:22:04 +0000520 if (TM.getInstrInfo().isCall(Opcode))
521 MRI.colorCallArgs(MInst, LRI, &AI, *this, MBB.getBasicBlock());
522 else if (TM.getInstrInfo().isReturn(Opcode))
523 MRI.colorRetValue(MInst, LRI, &AI);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000524 }
525
Vikram S. Advef5af6362002-07-08 23:15:32 +0000526 // Set the registers for operands in the machine instruction
527 // if a register was successfully allocated. If not, insert
528 // code to spill the register value.
529 //
530 for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum)
531 {
532 MachineOperand& Op = MInst->getOperand(OpNum);
Chris Lattner133f0792002-10-28 04:45:29 +0000533 if (Op.getType() == MachineOperand::MO_VirtualRegister ||
534 Op.getType() == MachineOperand::MO_CCRegister)
Vikram S. Advef5af6362002-07-08 23:15:32 +0000535 {
536 const Value *const Val = Op.getVRegValue();
537
538 LiveRange *const LR = LRI.getLiveRangeForValue(Val);
539 if (!LR) // consts or labels will have no live range
540 {
541 // if register is not allocated, mark register as invalid
542 if (Op.getAllocatedRegNum() == -1)
543 MInst->SetRegForOperand(OpNum, MRI.getInvalidRegNum());
544 continue;
545 }
546
Chris Lattnerf726e772002-10-28 19:22:04 +0000547 if (LR->hasColor())
Vikram S. Advef5af6362002-07-08 23:15:32 +0000548 MInst->SetRegForOperand(OpNum,
549 MRI.getUnifiedRegNum(LR->getRegClass()->getID(),
550 LR->getColor()));
551 else
552 // LR did NOT receive a color (register). Insert spill code.
Chris Lattnerf726e772002-10-28 19:22:04 +0000553 insertCode4SpilledLR(LR, MInst, MBB.getBasicBlock(), OpNum);
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000554 }
Vikram S. Advef5af6362002-07-08 23:15:32 +0000555 } // for each operand
Vikram S. Advecb202e32002-10-11 16:12:40 +0000556
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000557 // Now add instructions that the register allocator inserts before/after
558 // this machine instructions (done only for calls/rets/incoming args)
559 // We do this here, to ensure that spill for an instruction is inserted
560 // closest as possible to an instruction (see above insertCode4Spill...)
561 //
Vikram S. Advecb202e32002-10-11 16:12:40 +0000562 // First, if the instruction in the delay slot of a branch needs
563 // instructions inserted, move it out of the delay slot and before the
564 // branch because putting code before or after it would be VERY BAD!
565 //
566 unsigned bumpIteratorBy = 0;
Chris Lattnerf726e772002-10-28 19:22:04 +0000567 if (MII != MBB.begin())
Vikram S. Advecb202e32002-10-11 16:12:40 +0000568 if (unsigned predDelaySlots =
569 TM.getInstrInfo().getNumDelaySlots((*(MII-1))->getOpCode()))
570 {
571 assert(predDelaySlots==1 && "Not handling multiple delay slots!");
572 if (TM.getInstrInfo().isBranch((*(MII-1))->getOpCode())
573 && (AddedInstrMap.count(MInst) ||
574 AddedInstrMap[MInst].InstrnsAfter.size() > 0))
575 {
576 // Current instruction is in the delay slot of a branch and it
577 // needs spill code inserted before or after it.
578 // Move it before the preceding branch.
Chris Lattnerf726e772002-10-28 19:22:04 +0000579 InsertBefore(MInst, MBB, --MII);
Chris Lattnerf6ee49f2003-01-15 18:08:07 +0000580 MachineInstr* nopI = BuildMI(TM.getInstrInfo().getNOPOpCode(),1);
Chris Lattnerf726e772002-10-28 19:22:04 +0000581 SubstituteInPlace(nopI, MBB, MII+1); // replace orig with NOP
Vikram S. Advecb202e32002-10-11 16:12:40 +0000582 --MII; // point to MInst in new location
583 bumpIteratorBy = 2; // later skip the branch and the NOP!
584 }
585 }
586
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000587 // If there are instructions to be added, *before* this machine
588 // instruction, add them now.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000589 //
Chris Lattner7e708292002-06-25 16:13:24 +0000590 if (AddedInstrMap.count(MInst)) {
Chris Lattnerf726e772002-10-28 19:22:04 +0000591 PrependInstructions(AddedInstrMap[MInst].InstrnsBefore, MBB, MII,"");
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000592 }
Vikram S. Adve48762092002-04-25 04:34:15 +0000593
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000594 // If there are instructions to be added *after* this machine
595 // instruction, add them now
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000596 //
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000597 if (!AddedInstrMap[MInst].InstrnsAfter.empty()) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000598
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000599 // if there are delay slots for this instruction, the instructions
600 // added after it must really go after the delayed instruction(s)
601 // So, we move the InstrAfter of the current instruction to the
602 // corresponding delayed instruction
Vikram S. Advecb202e32002-10-11 16:12:40 +0000603 if (unsigned delay =
604 TM.getInstrInfo().getNumDelaySlots(MInst->getOpCode())) {
605
606 // Delayed instructions are typically branches or calls. Let's make
607 // sure this is not a branch, otherwise "insert-after" is meaningless,
608 // and should never happen for any reason (spill code, register
609 // restores, etc.).
610 assert(! TM.getInstrInfo().isBranch(MInst->getOpCode()) &&
611 ! TM.getInstrInfo().isReturn(MInst->getOpCode()) &&
612 "INTERNAL ERROR: Register allocator should not be inserting "
613 "any code after a branch or return!");
614
Vikram S. Adve48762092002-04-25 04:34:15 +0000615 move2DelayedInstr(MInst, *(MII+delay) );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000616 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000617 else {
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000618 // Here we can add the "instructions after" to the current
619 // instruction since there are no delay slots for this instruction
Chris Lattnerf726e772002-10-28 19:22:04 +0000620 AppendInstructions(AddedInstrMap[MInst].InstrnsAfter, MBB, MII,"");
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000621 } // if not delay
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000622 }
Vikram S. Advecb202e32002-10-11 16:12:40 +0000623
624 // If we mucked with the instruction order above, adjust the loop iterator
625 if (bumpIteratorBy)
626 MII = MII + bumpIteratorBy;
627
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000628 } // for each machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000629 }
630}
631
632
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000633
634//----------------------------------------------------------------------------
635// This method inserts spill code for AN operand whose LR was spilled.
636// This method may be called several times for a single machine instruction
637// if it contains many spilled operands. Each time it is called, it finds
638// a register which is not live at that instruction and also which is not
639// used by other spilled operands of the same instruction. Then it uses
640// this register temporarily to accomodate the spilled value.
641//----------------------------------------------------------------------------
642void PhyRegAlloc::insertCode4SpilledLR(const LiveRange *LR,
643 MachineInstr *MInst,
644 const BasicBlock *BB,
645 const unsigned OpNum) {
646
Vikram S. Advead9c9782002-09-28 17:02:40 +0000647 assert((! TM.getInstrInfo().isCall(MInst->getOpCode()) || OpNum == 0) &&
648 "Outgoing arg of a call must be handled elsewhere (func arg ok)");
649 assert(! TM.getInstrInfo().isReturn(MInst->getOpCode()) &&
650 "Return value of a ret must be handled elsewhere");
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000651
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000652 MachineOperand& Op = MInst->getOperand(OpNum);
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000653 bool isDef = Op.opIsDefOnly();
654 bool isDefAndUse = Op.opIsDefAndUse();
Chris Lattner9d4ed152003-01-15 21:14:01 +0000655 unsigned RegType = MRI.getRegType(LR);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000656 int SpillOff = LR->getSpillOffFromFP();
657 RegClass *RC = LR->getRegClass();
Chris Lattner748697d2002-02-05 04:20:12 +0000658 const ValueSet &LVSetBef = LVI->getLiveVarSetBeforeMInst(MInst, BB);
Vikram S. Adve00521d72001-11-12 23:26:35 +0000659
Chris Lattnere90fcb72002-12-28 20:35:34 +0000660 MF.getInfo()->pushTempValue(MRI.getSpilledRegSize(RegType) );
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000661
Vikram S. Advef5af6362002-07-08 23:15:32 +0000662 vector<MachineInstr*> MIBef, MIAft;
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000663 vector<MachineInstr*> AdIMid;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000664
Vikram S. Advef5af6362002-07-08 23:15:32 +0000665 // Choose a register to hold the spilled value. This may insert code
666 // before and after MInst to free up the value. If so, this code should
667 // be first and last in the spill sequence before/after MInst.
668 int TmpRegU = getUsableUniRegAtMI(RegType, &LVSetBef, MInst, MIBef, MIAft);
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000669
Vikram S. Advef5af6362002-07-08 23:15:32 +0000670 // Set the operand first so that it this register does not get used
671 // as a scratch register for later calls to getUsableUniRegAtMI below
672 MInst->SetRegForOperand(OpNum, TmpRegU);
673
674 // get the added instructions for this instruction
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000675 AddedInstrns &AI = AddedInstrMap[MInst];
Vikram S. Advef5af6362002-07-08 23:15:32 +0000676
677 // We may need a scratch register to copy the spilled value to/from memory.
678 // This may itself have to insert code to free up a scratch register.
679 // Any such code should go before (after) the spill code for a load (store).
680 int scratchRegType = -1;
681 int scratchReg = -1;
682 if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType))
683 {
Chris Lattner27a08932002-10-22 23:16:21 +0000684 scratchReg = getUsableUniRegAtMI(scratchRegType, &LVSetBef,
685 MInst, MIBef, MIAft);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000686 assert(scratchReg != MRI.getInvalidRegNum());
Chris Lattner27a08932002-10-22 23:16:21 +0000687 MInst->insertUsedReg(scratchReg);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000688 }
689
690 if (!isDef || isDefAndUse) {
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000691 // for a USE, we have to load the value of LR from stack to a TmpReg
692 // and use the TmpReg as one operand of instruction
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000693
Vikram S. Advef5af6362002-07-08 23:15:32 +0000694 // actual loading instruction(s)
695 MRI.cpMem2RegMI(AdIMid, MRI.getFramePointer(), SpillOff, TmpRegU, RegType,
696 scratchReg);
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000697
Vikram S. Advef5af6362002-07-08 23:15:32 +0000698 // the actual load should be after the instructions to free up TmpRegU
699 MIBef.insert(MIBef.end(), AdIMid.begin(), AdIMid.end());
700 AdIMid.clear();
701 }
702
703 if (isDef) { // if this is a Def
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000704 // for a DEF, we have to store the value produced by this instruction
705 // on the stack position allocated for this LR
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000706
Vikram S. Advef5af6362002-07-08 23:15:32 +0000707 // actual storing instruction(s)
708 MRI.cpReg2MemMI(AdIMid, TmpRegU, MRI.getFramePointer(), SpillOff, RegType,
709 scratchReg);
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000710
Vikram S. Advef5af6362002-07-08 23:15:32 +0000711 MIAft.insert(MIAft.begin(), AdIMid.begin(), AdIMid.end());
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000712 } // if !DEF
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000713
Vikram S. Advef5af6362002-07-08 23:15:32 +0000714 // Finally, insert the entire spill code sequences before/after MInst
715 AI.InstrnsBefore.insert(AI.InstrnsBefore.end(), MIBef.begin(), MIBef.end());
716 AI.InstrnsAfter.insert(AI.InstrnsAfter.begin(), MIAft.begin(), MIAft.end());
717
Chris Lattner7e708292002-06-25 16:13:24 +0000718 if (DEBUG_RA) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000719 cerr << "\nFor Inst:\n " << *MInst;
720 cerr << "SPILLED LR# " << LR->getUserIGNode()->getIndex();
721 cerr << "; added Instructions:";
Anand Shuklad58290e2002-07-09 19:18:56 +0000722 for_each(MIBef.begin(), MIBef.end(), std::mem_fun(&MachineInstr::dump));
723 for_each(MIAft.begin(), MIAft.end(), std::mem_fun(&MachineInstr::dump));
Chris Lattner7e708292002-06-25 16:13:24 +0000724 }
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000725}
726
727
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000728//----------------------------------------------------------------------------
729// We can use the following method to get a temporary register to be used
730// BEFORE any given machine instruction. If there is a register available,
731// this method will simply return that register and set MIBef = MIAft = NULL.
732// Otherwise, it will return a register and MIAft and MIBef will contain
733// two instructions used to free up this returned register.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000734// Returned register number is the UNIFIED register number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000735//----------------------------------------------------------------------------
736
Vikram S. Advef5af6362002-07-08 23:15:32 +0000737int PhyRegAlloc::getUsableUniRegAtMI(const int RegType,
738 const ValueSet *LVSetBef,
739 MachineInstr *MInst,
740 std::vector<MachineInstr*>& MIBef,
741 std::vector<MachineInstr*>& MIAft) {
742
Chris Lattner133f0792002-10-28 04:45:29 +0000743 RegClass* RC = getRegClassByID(MRI.getRegClassIDOfRegType(RegType));
Vikram S. Advef5af6362002-07-08 23:15:32 +0000744
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000745 int RegU = getUnusedUniRegAtMI(RC, MInst, LVSetBef);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000746
747 if (RegU == -1) {
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000748 // we couldn't find an unused register. Generate code to free up a reg by
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000749 // saving it on stack and restoring after the instruction
Vikram S. Advef5af6362002-07-08 23:15:32 +0000750
Chris Lattnere90fcb72002-12-28 20:35:34 +0000751 int TmpOff = MF.getInfo()->pushTempValue(MRI.getSpilledRegSize(RegType));
Vikram S. Adve12af1642001-11-08 04:48:50 +0000752
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000753 RegU = getUniRegNotUsedByThisInst(RC, MInst);
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000754
Vikram S. Advef5af6362002-07-08 23:15:32 +0000755 // Check if we need a scratch register to copy this register to memory.
756 int scratchRegType = -1;
757 if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType))
758 {
Chris Lattner133f0792002-10-28 04:45:29 +0000759 int scratchReg = getUsableUniRegAtMI(scratchRegType, LVSetBef,
760 MInst, MIBef, MIAft);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000761 assert(scratchReg != MRI.getInvalidRegNum());
762
763 // We may as well hold the value in the scratch register instead
764 // of copying it to memory and back. But we have to mark the
765 // register as used by this instruction, so it does not get used
766 // as a scratch reg. by another operand or anyone else.
Chris Lattner27a08932002-10-22 23:16:21 +0000767 MInst->insertUsedReg(scratchReg);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000768 MRI.cpReg2RegMI(MIBef, RegU, scratchReg, RegType);
769 MRI.cpReg2RegMI(MIAft, scratchReg, RegU, RegType);
770 }
771 else
772 { // the register can be copied directly to/from memory so do it.
773 MRI.cpReg2MemMI(MIBef, RegU, MRI.getFramePointer(), TmpOff, RegType);
774 MRI.cpMem2RegMI(MIAft, MRI.getFramePointer(), TmpOff, RegU, RegType);
775 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000776 }
Vikram S. Advef5af6362002-07-08 23:15:32 +0000777
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000778 return RegU;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000779}
780
781//----------------------------------------------------------------------------
782// This method is called to get a new unused register that can be used to
783// accomodate a spilled value.
784// This method may be called several times for a single machine instruction
785// if it contains many spilled operands. Each time it is called, it finds
786// a register which is not live at that instruction and also which is not
787// used by other spilled operands of the same instruction.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000788// Return register number is relative to the register class. NOT
789// unified number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000790//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000791int PhyRegAlloc::getUnusedUniRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000792 const MachineInstr *MInst,
Chris Lattner296b7732002-02-05 02:52:05 +0000793 const ValueSet *LVSetBef) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000794
795 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
796
Chris Lattner85c54652002-05-23 15:50:03 +0000797 std::vector<bool> &IsColorUsedArr = RC->getIsColorUsedArr();
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000798
Chris Lattner7e708292002-06-25 16:13:24 +0000799 for (unsigned i=0; i < NumAvailRegs; i++) // Reset array
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000800 IsColorUsedArr[i] = false;
801
Chris Lattner296b7732002-02-05 02:52:05 +0000802 ValueSet::const_iterator LIt = LVSetBef->begin();
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000803
804 // for each live var in live variable set after machine inst
Chris Lattner7e708292002-06-25 16:13:24 +0000805 for ( ; LIt != LVSetBef->end(); ++LIt) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000806
807 // get the live range corresponding to live var
808 LiveRange *const LRofLV = LRI.getLiveRangeForValue(*LIt );
809
810 // LR can be null if it is a const since a const
811 // doesn't have a dominating def - see Assumptions above
Chris Lattner7e708292002-06-25 16:13:24 +0000812 if (LRofLV && LRofLV->getRegClass() == RC && LRofLV->hasColor() )
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000813 IsColorUsedArr[ LRofLV->getColor() ] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000814 }
815
816 // It is possible that one operand of this MInst was already spilled
817 // and it received some register temporarily. If that's the case,
818 // it is recorded in machine operand. We must skip such registers.
819
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000820 setRelRegsUsedByThisInst(RC, MInst);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000821
Chris Lattner7e708292002-06-25 16:13:24 +0000822 for (unsigned c=0; c < NumAvailRegs; c++) // find first unused color
Chris Lattner85c54652002-05-23 15:50:03 +0000823 if (!IsColorUsedArr[c])
824 return MRI.getUnifiedRegNum(RC->getID(), c);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000825
Chris Lattner85c54652002-05-23 15:50:03 +0000826 return -1;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000827}
828
829
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000830//----------------------------------------------------------------------------
831// Get any other register in a register class, other than what is used
832// by operands of a machine instruction. Returns the unified reg number.
833//----------------------------------------------------------------------------
834int PhyRegAlloc::getUniRegNotUsedByThisInst(RegClass *RC,
Chris Lattner85c54652002-05-23 15:50:03 +0000835 const MachineInstr *MInst) {
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000836
Chris Lattner85c54652002-05-23 15:50:03 +0000837 vector<bool> &IsColorUsedArr = RC->getIsColorUsedArr();
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000838 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
839
Chris Lattner7e708292002-06-25 16:13:24 +0000840 for (unsigned i=0; i < NumAvailRegs ; i++) // Reset array
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000841 IsColorUsedArr[i] = false;
842
843 setRelRegsUsedByThisInst(RC, MInst);
844
Chris Lattner7e708292002-06-25 16:13:24 +0000845 for (unsigned c=0; c < RC->getNumOfAvailRegs(); c++)// find first unused color
Chris Lattner85c54652002-05-23 15:50:03 +0000846 if (!IsColorUsedArr[c])
847 return MRI.getUnifiedRegNum(RC->getID(), c);
848
849 assert(0 && "FATAL: No free register could be found in reg class!!");
Chris Lattner697954c2002-01-20 22:54:45 +0000850 return 0;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000851}
852
853
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000854//----------------------------------------------------------------------------
855// This method modifies the IsColorUsedArr of the register class passed to it.
856// It sets the bits corresponding to the registers used by this machine
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000857// instructions. Both explicit and implicit operands are set.
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000858//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000859void PhyRegAlloc::setRelRegsUsedByThisInst(RegClass *RC,
Vikram S. Advef5af6362002-07-08 23:15:32 +0000860 const MachineInstr *MInst ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000861
Vikram S. Advef5af6362002-07-08 23:15:32 +0000862 vector<bool> &IsColorUsedArr = RC->getIsColorUsedArr();
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000863
Vikram S. Advef5af6362002-07-08 23:15:32 +0000864 // Add the registers already marked as used by the instruction.
865 // This should include any scratch registers that are used to save
866 // values across the instruction (e.g., for saving state register values).
Chris Lattner27a08932002-10-22 23:16:21 +0000867 const vector<bool> &regsUsed = MInst->getRegsUsed();
868 for (unsigned i = 0, e = regsUsed.size(); i != e; ++i)
869 if (regsUsed[i]) {
Vikram S. Advef5af6362002-07-08 23:15:32 +0000870 unsigned classId = 0;
Chris Lattner27a08932002-10-22 23:16:21 +0000871 int classRegNum = MRI.getClassRegNum(i, classId);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000872 if (RC->getID() == classId)
873 {
874 assert(classRegNum < (int) IsColorUsedArr.size() &&
875 "Illegal register number for this reg class?");
876 IsColorUsedArr[classRegNum] = true;
877 }
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000878 }
Vikram S. Advef5af6362002-07-08 23:15:32 +0000879
880 // Now add registers allocated to the live ranges of values used in
881 // the instruction. These are not yet recorded in the instruction.
882 for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum)
883 {
884 const MachineOperand& Op = MInst->getOperand(OpNum);
885
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000886 if (Op.getType() == MachineOperand::MO_VirtualRegister ||
887 Op.getType() == MachineOperand::MO_CCRegister)
Vikram S. Advef5af6362002-07-08 23:15:32 +0000888 if (const Value* Val = Op.getVRegValue())
Chris Lattner9d4ed152003-01-15 21:14:01 +0000889 if (MRI.getRegClassIDOfType(Val->getType()) == RC->getID())
Vikram S. Advef5af6362002-07-08 23:15:32 +0000890 if (Op.getAllocatedRegNum() == -1)
891 if (LiveRange *LROfVal = LRI.getLiveRangeForValue(Val))
892 if (LROfVal->hasColor() )
893 // this operand is in a LR that received a color
894 IsColorUsedArr[LROfVal->getColor()] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000895 }
Vikram S. Advef5af6362002-07-08 23:15:32 +0000896
897 // If there are implicit references, mark their allocated regs as well
898 //
899 for (unsigned z=0; z < MInst->getNumImplicitRefs(); z++)
900 if (const LiveRange*
901 LRofImpRef = LRI.getLiveRangeForValue(MInst->getImplicitRef(z)))
902 if (LRofImpRef->hasColor())
903 // this implicit reference is in a LR that received a color
904 IsColorUsedArr[LRofImpRef->getColor()] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000905}
906
907
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000908//----------------------------------------------------------------------------
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000909// If there are delay slots for an instruction, the instructions
910// added after it must really go after the delayed instruction(s).
911// So, we move the InstrAfter of that instruction to the
912// corresponding delayed instruction using the following method.
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000913
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000914//----------------------------------------------------------------------------
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000915void PhyRegAlloc::move2DelayedInstr(const MachineInstr *OrigMI,
916 const MachineInstr *DelayedMI) {
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000917
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000918 // "added after" instructions of the original instr
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000919 std::vector<MachineInstr *> &OrigAft = AddedInstrMap[OrigMI].InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000920
921 // "added instructions" of the delayed instr
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000922 AddedInstrns &DelayAdI = AddedInstrMap[DelayedMI];
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000923
924 // "added after" instructions of the delayed instr
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000925 std::vector<MachineInstr *> &DelayedAft = DelayAdI.InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000926
927 // go thru all the "added after instructions" of the original instruction
928 // and append them to the "addded after instructions" of the delayed
929 // instructions
Chris Lattner697954c2002-01-20 22:54:45 +0000930 DelayedAft.insert(DelayedAft.end(), OrigAft.begin(), OrigAft.end());
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000931
932 // empty the "added after instructions" of the original instruction
933 OrigAft.clear();
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000934}
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000935
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000936//----------------------------------------------------------------------------
937// This method prints the code with registers after register allocation is
938// complete.
939//----------------------------------------------------------------------------
940void PhyRegAlloc::printMachineCode()
941{
942
Chris Lattnerf726e772002-10-28 19:22:04 +0000943 cerr << "\n;************** Function " << Fn->getName()
Chris Lattner697954c2002-01-20 22:54:45 +0000944 << " *****************\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000945
Chris Lattnerf726e772002-10-28 19:22:04 +0000946 for (MachineFunction::iterator BBI = MF.begin(), BBE = MF.end();
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000947 BBI != BBE; ++BBI) {
Chris Lattnerf726e772002-10-28 19:22:04 +0000948 cerr << "\n"; printLabel(BBI->getBasicBlock()); cerr << ": ";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000949
950 // get the iterator for machine instructions
Chris Lattnerf726e772002-10-28 19:22:04 +0000951 MachineBasicBlock& MBB = *BBI;
952 MachineBasicBlock::iterator MII = MBB.begin();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000953
954 // iterate over all the machine instructions in BB
Chris Lattnerf726e772002-10-28 19:22:04 +0000955 for ( ; MII != MBB.end(); ++MII) {
Chris Lattnerd9512ca2002-10-29 17:35:39 +0000956 MachineInstr *MInst = *MII;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000957
Chris Lattner697954c2002-01-20 22:54:45 +0000958 cerr << "\n\t";
Chris Lattnerd9512ca2002-10-29 17:35:39 +0000959 cerr << TM.getInstrInfo().getName(MInst->getOpCode());
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000960
Chris Lattner7e708292002-06-25 16:13:24 +0000961 for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000962 MachineOperand& Op = MInst->getOperand(OpNum);
963
Chris Lattner133f0792002-10-28 04:45:29 +0000964 if (Op.getType() == MachineOperand::MO_VirtualRegister ||
965 Op.getType() == MachineOperand::MO_CCRegister /*||
966 Op.getType() == MachineOperand::MO_PCRelativeDisp*/ ) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000967
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000968 const Value *const Val = Op.getVRegValue () ;
Ruchira Sasankae727f852001-09-18 22:43:57 +0000969 // ****this code is temporary till NULL Values are fixed
Chris Lattner7e708292002-06-25 16:13:24 +0000970 if (! Val ) {
Chris Lattner697954c2002-01-20 22:54:45 +0000971 cerr << "\t<*NULL*>";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000972 continue;
973 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000974
975 // if a label or a constant
Chris Lattner7e708292002-06-25 16:13:24 +0000976 if (isa<BasicBlock>(Val)) {
Chris Lattner697954c2002-01-20 22:54:45 +0000977 cerr << "\t"; printLabel( Op.getVRegValue () );
978 } else {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000979 // else it must be a register value
980 const int RegNum = Op.getAllocatedRegNum();
981
Chris Lattner697954c2002-01-20 22:54:45 +0000982 cerr << "\t" << "%" << MRI.getUnifiedRegName( RegNum );
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000983 if (Val->hasName() )
Chris Lattner697954c2002-01-20 22:54:45 +0000984 cerr << "(" << Val->getName() << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000985 else
Chris Lattner697954c2002-01-20 22:54:45 +0000986 cerr << "(" << Val << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000987
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000988 if (Op.opIsDefOnly() || Op.opIsDefAndUse())
Chris Lattner697954c2002-01-20 22:54:45 +0000989 cerr << "*";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000990
991 const LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
Chris Lattner7e708292002-06-25 16:13:24 +0000992 if (LROfVal )
993 if (LROfVal->hasSpillOffset() )
Chris Lattner697954c2002-01-20 22:54:45 +0000994 cerr << "$";
Ruchira Sasankae727f852001-09-18 22:43:57 +0000995 }
996
997 }
Chris Lattner133f0792002-10-28 04:45:29 +0000998 else if (Op.getType() == MachineOperand::MO_MachineRegister) {
Chris Lattner697954c2002-01-20 22:54:45 +0000999 cerr << "\t" << "%" << MRI.getUnifiedRegName(Op.getMachineRegNum());
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001000 }
1001
1002 else
Chris Lattner697954c2002-01-20 22:54:45 +00001003 cerr << "\t" << Op; // use dump field
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001004 }
1005
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001006
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001007
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001008 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
Chris Lattner7e708292002-06-25 16:13:24 +00001009 if (NumOfImpRefs > 0) {
Chris Lattner697954c2002-01-20 22:54:45 +00001010 cerr << "\tImplicit:";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001011
Chris Lattner7e708292002-06-25 16:13:24 +00001012 for (unsigned z=0; z < NumOfImpRefs; z++)
Chris Lattner0665a5f2002-02-05 01:43:49 +00001013 cerr << RAV(MInst->getImplicitRef(z)) << "\t";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001014 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001015
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001016 } // for all machine instructions
1017
Chris Lattner697954c2002-01-20 22:54:45 +00001018 cerr << "\n";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001019
1020 } // for all BBs
1021
Chris Lattner697954c2002-01-20 22:54:45 +00001022 cerr << "\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001023}
1024
Ruchira Sasankae727f852001-09-18 22:43:57 +00001025
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001026//----------------------------------------------------------------------------
1027
1028//----------------------------------------------------------------------------
1029void PhyRegAlloc::colorIncomingArgs()
1030{
Chris Lattnerf726e772002-10-28 19:22:04 +00001031 MRI.colorMethodArgs(Fn, LRI, &AddedInstrAtEntry);
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001032}
1033
Ruchira Sasankae727f852001-09-18 22:43:57 +00001034
1035//----------------------------------------------------------------------------
1036// Used to generate a label for a basic block
1037//----------------------------------------------------------------------------
Chris Lattnerf726e772002-10-28 19:22:04 +00001038void PhyRegAlloc::printLabel(const Value *Val) {
Chris Lattner697954c2002-01-20 22:54:45 +00001039 if (Val->hasName())
1040 cerr << Val->getName();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001041 else
Chris Lattnerf726e772002-10-28 19:22:04 +00001042 cerr << "Label" << Val;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001043}
1044
1045
Ruchira Sasankae727f852001-09-18 22:43:57 +00001046//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001047// This method calls setSugColorUsable method of each live range. This
1048// will determine whether the suggested color of LR is really usable.
1049// A suggested color is not usable when the suggested color is volatile
1050// AND when there are call interferences
1051//----------------------------------------------------------------------------
1052
1053void PhyRegAlloc::markUnusableSugColors()
1054{
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001055 // hash map iterator
1056 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
1057 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
1058
Chris Lattner7e708292002-06-25 16:13:24 +00001059 for (; HMI != HMIEnd ; ++HMI ) {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001060 if (HMI->first) {
1061 LiveRange *L = HMI->second; // get the LiveRange
1062 if (L) {
Chris Lattner7e708292002-06-25 16:13:24 +00001063 if (L->hasSuggestedColor()) {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001064 int RCID = L->getRegClass()->getID();
Chris Lattner7e708292002-06-25 16:13:24 +00001065 if (MRI.isRegVolatile( RCID, L->getSuggestedColor()) &&
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001066 L->isCallInterference() )
1067 L->setSuggestedColorUsable( false );
1068 else
1069 L->setSuggestedColorUsable( true );
1070 }
1071 } // if L->hasSuggestedColor()
1072 }
1073 } // for all LR's in hash map
1074}
1075
1076
1077
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001078//----------------------------------------------------------------------------
1079// The following method will set the stack offsets of the live ranges that
1080// are decided to be spillled. This must be called just after coloring the
1081// LRs using the graph coloring algo. For each live range that is spilled,
1082// this method allocate a new spill position on the stack.
1083//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001084
Chris Lattner37730942002-02-05 03:52:29 +00001085void PhyRegAlloc::allocateStackSpace4SpilledLRs() {
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001086 if (DEBUG_RA) cerr << "\nSetting LR stack offsets for spills...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001087
Chris Lattner37730942002-02-05 03:52:29 +00001088 LiveRangeMapType::const_iterator HMI = LRI.getLiveRangeMap()->begin();
1089 LiveRangeMapType::const_iterator HMIEnd = LRI.getLiveRangeMap()->end();
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001090
Chris Lattner7e708292002-06-25 16:13:24 +00001091 for ( ; HMI != HMIEnd ; ++HMI) {
Chris Lattner37730942002-02-05 03:52:29 +00001092 if (HMI->first && HMI->second) {
1093 LiveRange *L = HMI->second; // get the LiveRange
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001094 if (!L->hasColor()) { // NOTE: ** allocating the size of long Type **
Chris Lattnere90fcb72002-12-28 20:35:34 +00001095 int stackOffset = MF.getInfo()->allocateSpilledValue(Type::LongTy);
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001096 L->setSpillOffFromFP(stackOffset);
1097 if (DEBUG_RA)
1098 cerr << " LR# " << L->getUserIGNode()->getIndex()
1099 << ": stack-offset = " << stackOffset << "\n";
1100 }
Chris Lattner37730942002-02-05 03:52:29 +00001101 }
1102 } // for all LR's in hash map
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001103}
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001104
1105
1106
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001107//----------------------------------------------------------------------------
Ruchira Sasankae727f852001-09-18 22:43:57 +00001108// The entry pont to Register Allocation
1109//----------------------------------------------------------------------------
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001110
1111void PhyRegAlloc::allocateRegisters()
1112{
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001113
1114 // make sure that we put all register classes into the RegClassList
1115 // before we call constructLiveRanges (now done in the constructor of
1116 // PhyRegAlloc class).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001117 //
1118 LRI.constructLiveRanges(); // create LR info
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001119
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001120 if (DEBUG_RA >= RA_DEBUG_LiveRanges)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001121 LRI.printLiveRanges();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001122
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001123 createIGNodeListsAndIGs(); // create IGNode list and IGs
1124
1125 buildInterferenceGraphs(); // build IGs in all reg classes
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001126
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001127
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001128 if (DEBUG_RA >= RA_DEBUG_LiveRanges) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001129 // print all LRs in all reg classes
Chris Lattner7e708292002-06-25 16:13:24 +00001130 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
1131 RegClassList[rc]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001132
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001133 // print IGs in all register classes
Chris Lattner7e708292002-06-25 16:13:24 +00001134 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
1135 RegClassList[rc]->printIG();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001136 }
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001137
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001138 LRI.coalesceLRs(); // coalesce all live ranges
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +00001139
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001140 if (DEBUG_RA >= RA_DEBUG_LiveRanges) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001141 // print all LRs in all reg classes
Chris Lattnerf726e772002-10-28 19:22:04 +00001142 for (unsigned rc=0; rc < NumOfRegClasses; rc++)
1143 RegClassList[rc]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001144
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001145 // print IGs in all register classes
Chris Lattnerf726e772002-10-28 19:22:04 +00001146 for (unsigned rc=0; rc < NumOfRegClasses; rc++)
1147 RegClassList[rc]->printIG();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001148 }
1149
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001150
1151 // mark un-usable suggested color before graph coloring algorithm.
1152 // When this is done, the graph coloring algo will not reserve
1153 // suggested color unnecessarily - they can be used by another LR
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001154 //
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001155 markUnusableSugColors();
1156
1157 // color all register classes using the graph coloring algo
Chris Lattner7e708292002-06-25 16:13:24 +00001158 for (unsigned rc=0; rc < NumOfRegClasses ; rc++)
Chris Lattnerf726e772002-10-28 19:22:04 +00001159 RegClassList[rc]->colorAllRegs();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001160
Chris Lattnere90fcb72002-12-28 20:35:34 +00001161 // Atter graph coloring, if some LRs did not receive a color (i.e, spilled)
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001162 // a poistion for such spilled LRs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001163 //
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001164 allocateStackSpace4SpilledLRs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001165
Chris Lattnere90fcb72002-12-28 20:35:34 +00001166 MF.getInfo()->popAllTempValues(); // TODO **Check
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001167
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001168 // color incoming args - if the correct color was not received
1169 // insert code to copy to the correct register
1170 //
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001171 colorIncomingArgs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001172
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001173 // Now update the machine code with register names and add any
1174 // additional code inserted by the register allocator to the instruction
1175 // stream
1176 //
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001177 updateMachineCode();
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001178
Chris Lattner045e7c82001-09-19 16:26:23 +00001179 if (DEBUG_RA) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001180 cerr << "\n**** Machine Code After Register Allocation:\n\n";
Chris Lattnerf726e772002-10-28 19:22:04 +00001181 MF.dump();
Chris Lattner045e7c82001-09-19 16:26:23 +00001182 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001183}
1184
Ruchira Sasankae727f852001-09-18 22:43:57 +00001185
1186