blob: 7cacd65607390ab4c6d2f4939f047b519a7d30fd [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
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000203 // for each live var in live variable set after machine inst
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000204 //
Vikram S. Adve65b2f402003-07-02 01:24:00 +0000205 for (ValueSet::const_iterator LIt = LVSetAft->begin(), LEnd = LVSetAft->end();
206 LIt != LEnd; ++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) {
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000304 if (OpI.isDefOnly() || OpI.isDefAndUse()) // create a new LR since 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();
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000324 for (unsigned z=0; z < NumOfImpRefs; z++)
325 if (MInst->getImplicitOp(z).opIsDefOnly() ||
326 MInst->getImplicitOp(z).opIsDefAndUse())
327 addInterference( MInst->getImplicitRef(z), &LVSetAI, isCallInst );
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +0000328
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000329 } // for all machine instructions in BB
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000330 } // for all BBs in function
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000331
332
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000333 // add interferences for function arguments. Since there are no explict
334 // defs in the function for args, we have to add them manually
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000335 //
336 addInterferencesForArgs();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000337
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000338 if (DEBUG_RA >= RA_DEBUG_Interference)
339 cerr << "Interference graphs calculated!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000340}
341
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000342
343
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000344//--------------------------------------------------------------------------
345// Pseudo instructions will be exapnded to multiple instructions by the
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000346// assembler. Consequently, all the opernds must get distinct registers.
347// Therefore, we mark all operands of a pseudo instruction as they interfere
348// with one another.
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000349//--------------------------------------------------------------------------
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000350void PhyRegAlloc::addInterf4PseudoInstr(const MachineInstr *MInst) {
351
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000352 bool setInterf = false;
353
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000354 // iterate over MI operands to find defs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000355 //
Chris Lattner2f898d22002-02-05 06:02:59 +0000356 for (MachineInstr::const_val_op_iterator It1 = MInst->begin(),
357 ItE = MInst->end(); It1 != ItE; ++It1) {
358 const LiveRange *LROfOp1 = LRI.getLiveRangeForValue(*It1);
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000359 assert((LROfOp1 || !It1.isUseOnly())&& "No LR for Def in PSEUDO insruction");
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000360
Chris Lattner2f898d22002-02-05 06:02:59 +0000361 MachineInstr::const_val_op_iterator It2 = It1;
Chris Lattner7e708292002-06-25 16:13:24 +0000362 for (++It2; It2 != ItE; ++It2) {
Chris Lattner2f898d22002-02-05 06:02:59 +0000363 const LiveRange *LROfOp2 = LRI.getLiveRangeForValue(*It2);
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000364
Chris Lattner2f898d22002-02-05 06:02:59 +0000365 if (LROfOp2) {
366 RegClass *RCOfOp1 = LROfOp1->getRegClass();
367 RegClass *RCOfOp2 = LROfOp2->getRegClass();
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000368
Chris Lattner7e708292002-06-25 16:13:24 +0000369 if (RCOfOp1 == RCOfOp2 ){
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000370 RCOfOp1->setInterference( LROfOp1, LROfOp2 );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000371 setInterf = true;
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000372 }
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000373 } // if Op2 has a LR
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000374 } // for all other defs in machine instr
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000375 } // for all operands in an instruction
376
Chris Lattner2f898d22002-02-05 06:02:59 +0000377 if (!setInterf && MInst->getNumOperands() > 2) {
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000378 cerr << "\nInterf not set for any operand in pseudo instr:\n";
379 cerr << *MInst;
380 assert(0 && "Interf not set for pseudo instr with > 2 operands" );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000381 }
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000382}
383
384
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000385
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000386//----------------------------------------------------------------------------
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000387// This method will add interferences for incoming arguments to a function.
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000388//----------------------------------------------------------------------------
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000389
Chris Lattner296b7732002-02-05 02:52:05 +0000390void PhyRegAlloc::addInterferencesForArgs() {
391 // get the InSet of root BB
Chris Lattnerf726e772002-10-28 19:22:04 +0000392 const ValueSet &InSet = LVI->getInSetOfBB(&Fn->front());
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000393
Chris Lattnerf726e772002-10-28 19:22:04 +0000394 for (Function::const_aiterator AI = Fn->abegin(); AI != Fn->aend(); ++AI) {
Chris Lattner7e708292002-06-25 16:13:24 +0000395 // add interferences between args and LVars at start
396 addInterference(AI, &InSet, false);
397
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000398 if (DEBUG_RA >= RA_DEBUG_Interference)
Chris Lattner7e708292002-06-25 16:13:24 +0000399 cerr << " - %% adding interference for argument " << RAV(AI) << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000400 }
401}
402
403
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000404//----------------------------------------------------------------------------
405// This method is called after register allocation is complete to set the
406// allocated reisters in the machine code. This code will add register numbers
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000407// to MachineOperands that contain a Value. Also it calls target specific
408// methods to produce caller saving instructions. At the end, it adds all
409// additional instructions produced by the register allocator to the
410// instruction stream.
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000411//----------------------------------------------------------------------------
Vikram S. Adve48762092002-04-25 04:34:15 +0000412
413//-----------------------------
414// Utility functions used below
415//-----------------------------
416inline void
Vikram S. Advecb202e32002-10-11 16:12:40 +0000417InsertBefore(MachineInstr* newMI,
Chris Lattnerf726e772002-10-28 19:22:04 +0000418 MachineBasicBlock& MBB,
Chris Lattner32be9f62002-10-28 01:41:27 +0000419 MachineBasicBlock::iterator& MII)
Vikram S. Advecb202e32002-10-11 16:12:40 +0000420{
Chris Lattnerf726e772002-10-28 19:22:04 +0000421 MII = MBB.insert(MII, newMI);
Vikram S. Advecb202e32002-10-11 16:12:40 +0000422 ++MII;
423}
424
425inline void
426InsertAfter(MachineInstr* newMI,
Chris Lattnerf726e772002-10-28 19:22:04 +0000427 MachineBasicBlock& MBB,
Chris Lattner32be9f62002-10-28 01:41:27 +0000428 MachineBasicBlock::iterator& MII)
Vikram S. Advecb202e32002-10-11 16:12:40 +0000429{
430 ++MII; // insert before the next instruction
Chris Lattnerf726e772002-10-28 19:22:04 +0000431 MII = MBB.insert(MII, newMI);
Vikram S. Advecb202e32002-10-11 16:12:40 +0000432}
433
434inline void
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000435DeleteInstruction(MachineBasicBlock& MBB,
436 MachineBasicBlock::iterator& MII)
437{
438 MII = MBB.erase(MII);
439}
440
441inline void
Vikram S. Advecb202e32002-10-11 16:12:40 +0000442SubstituteInPlace(MachineInstr* newMI,
Chris Lattnerf726e772002-10-28 19:22:04 +0000443 MachineBasicBlock& MBB,
Chris Lattner32be9f62002-10-28 01:41:27 +0000444 MachineBasicBlock::iterator MII)
Vikram S. Advecb202e32002-10-11 16:12:40 +0000445{
446 *MII = newMI;
447}
448
449inline void
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000450PrependInstructions(vector<MachineInstr *> &IBef,
Chris Lattnerf726e772002-10-28 19:22:04 +0000451 MachineBasicBlock& MBB,
Chris Lattner32be9f62002-10-28 01:41:27 +0000452 MachineBasicBlock::iterator& MII,
Vikram S. Adve48762092002-04-25 04:34:15 +0000453 const std::string& msg)
454{
455 if (!IBef.empty())
456 {
457 MachineInstr* OrigMI = *MII;
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000458 std::vector<MachineInstr *>::iterator AdIt;
Vikram S. Adve48762092002-04-25 04:34:15 +0000459 for (AdIt = IBef.begin(); AdIt != IBef.end() ; ++AdIt)
460 {
461 if (DEBUG_RA) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000462 if (OrigMI) cerr << "For MInst:\n " << *OrigMI;
463 cerr << msg << "PREPENDed instr:\n " << **AdIt << "\n";
Vikram S. Adve48762092002-04-25 04:34:15 +0000464 }
Chris Lattnerf726e772002-10-28 19:22:04 +0000465 InsertBefore(*AdIt, MBB, MII);
Vikram S. Adve48762092002-04-25 04:34:15 +0000466 }
467 }
468}
469
470inline void
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000471AppendInstructions(std::vector<MachineInstr *> &IAft,
Chris Lattnerf726e772002-10-28 19:22:04 +0000472 MachineBasicBlock& MBB,
Chris Lattner32be9f62002-10-28 01:41:27 +0000473 MachineBasicBlock::iterator& MII,
Vikram S. Adve48762092002-04-25 04:34:15 +0000474 const std::string& msg)
475{
476 if (!IAft.empty())
477 {
478 MachineInstr* OrigMI = *MII;
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000479 std::vector<MachineInstr *>::iterator AdIt;
Chris Lattner7e708292002-06-25 16:13:24 +0000480 for ( AdIt = IAft.begin(); AdIt != IAft.end() ; ++AdIt )
Vikram S. Adve48762092002-04-25 04:34:15 +0000481 {
Chris Lattner7e708292002-06-25 16:13:24 +0000482 if (DEBUG_RA) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000483 if (OrigMI) cerr << "For MInst:\n " << *OrigMI;
484 cerr << msg << "APPENDed instr:\n " << **AdIt << "\n";
Vikram S. Adve48762092002-04-25 04:34:15 +0000485 }
Chris Lattnerf726e772002-10-28 19:22:04 +0000486 InsertAfter(*AdIt, MBB, MII);
Vikram S. Adve48762092002-04-25 04:34:15 +0000487 }
488 }
489}
490
491
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000492void PhyRegAlloc::updateInstruction(MachineInstr* MInst, BasicBlock* BB)
493{
494 unsigned Opcode = MInst->getOpCode();
495
496 // Reset tmp stack positions so they can be reused for each machine instr.
497 MF.getInfo()->popAllTempValues();
498
499 // First, set the registers for operands in the machine instruction
500 // if a register was successfully allocated. Do this first because we
501 // will need to know which registers are already used by this instr'n.
502 //
503 for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum)
504 {
505 MachineOperand& Op = MInst->getOperand(OpNum);
506 if (Op.getType() == MachineOperand::MO_VirtualRegister ||
507 Op.getType() == MachineOperand::MO_CCRegister)
508 {
509 const Value *const Val = Op.getVRegValue();
510 if (const LiveRange* LR = LRI.getLiveRangeForValue(Val))
511 if (LR->hasColor())
512 MInst->SetRegForOperand(OpNum,
513 MRI.getUnifiedRegNum(LR->getRegClass()->getID(),
514 LR->getColor()));
515 }
516 } // for each operand
517
518 // Mark that the operands have been updated. setRelRegsUsedByThisInst()
519 // is called to find registers used by each MachineInst, and it should not
520 // be used for an instruction until this is done. This flag just serves
521 // as a sanity check.
522 OperandsColoredMap[MInst] = true;
523
524 // Now insert special instructions (if necessary) for call/return
525 // instructions. Do this before inserting spill code since some
526 // registers must be used by outgoing call arguments or the return value
527 // of a call, and spill code should not use those registers.
528 //
529 if (TM.getInstrInfo().isCall(Opcode) ||
530 TM.getInstrInfo().isReturn(Opcode)) {
531 AddedInstrns &AI = AddedInstrMap[MInst];
532
533 if (TM.getInstrInfo().isCall(Opcode))
534 MRI.colorCallArgs(MInst, LRI, &AI, *this, BB);
535 else if (TM.getInstrInfo().isReturn(Opcode))
536 MRI.colorRetValue(MInst, LRI, &AI);
537 }
538
539 // Now insert spill code for remaining operands not allocated to
540 // registers. This must be done even for call return instructions
541 // since those are not handled by the special code above.
542 for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum)
543 {
544 MachineOperand& Op = MInst->getOperand(OpNum);
545 if (Op.getType() == MachineOperand::MO_VirtualRegister ||
546 Op.getType() == MachineOperand::MO_CCRegister)
547 {
548 const Value* Val = Op.getVRegValue();
549 if (const LiveRange *LR = LRI.getLiveRangeForValue(Val))
Vikram S. Adve3bf08922003-07-10 19:42:55 +0000550 if (LR->isMarkedForSpill())
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000551 insertCode4SpilledLR(LR, MInst, BB, OpNum);
552 }
553 } // for each operand
554}
555
556void PhyRegAlloc::updateMachineCode()
557{
Chris Lattner7e708292002-06-25 16:13:24 +0000558 // Insert any instructions needed at method entry
Chris Lattnerf726e772002-10-28 19:22:04 +0000559 MachineBasicBlock::iterator MII = MF.front().begin();
560 PrependInstructions(AddedInstrAtEntry.InstrnsBefore, MF.front(), MII,
Chris Lattner7e708292002-06-25 16:13:24 +0000561 "At function entry: \n");
562 assert(AddedInstrAtEntry.InstrnsAfter.empty() &&
563 "InstrsAfter should be unnecessary since we are just inserting at "
564 "the function entry point here.");
Vikram S. Adve48762092002-04-25 04:34:15 +0000565
Chris Lattnerf726e772002-10-28 19:22:04 +0000566 for (MachineFunction::iterator BBI = MF.begin(), BBE = MF.end();
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000567 BBI != BBE; ++BBI) {
Vikram S. Advecb202e32002-10-11 16:12:40 +0000568
Chris Lattnerf726e772002-10-28 19:22:04 +0000569 MachineBasicBlock &MBB = *BBI;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000570
571 // Iterate over all machine instructions in BB and mark operands with
572 // their assigned registers or insert spill code, as appropriate.
573 // Also, fix operands of call/return instructions.
574 //
575 for (MachineBasicBlock::iterator MII = MBB.begin(); MII != MBB.end(); ++MII)
576 if (!TM.getInstrInfo().isDummyPhiInstr((*MII)->getOpCode())) // ignore Phis
577 updateInstruction(*MII, MBB.getBasicBlock());
578
579 // Now, move code out of delay slots of branches and returns if needed.
580 // (Also, move "after" code from calls to the last delay slot instruction.)
581 // Moving code out of delay slots is needed in 2 situations:
582 // (1) If this is a branch and it needs instructions inserted after it,
583 // move any existing instructions out of the delay slot so that the
584 // instructions can go into the delay slot. This only supports the
585 // case that #instrsAfter <= #delay slots.
586 //
587 // (2) If any instruction in the delay slot needs
588 // instructions inserted, move it out of the delay slot and before the
589 // branch because putting code before or after it would be VERY BAD!
590 //
591 // If the annul bit of the branch is set, neither of these is legal!
592 // If so, we need to handle spill differently but annulling is not yet used.
593 //
594 for (MachineBasicBlock::iterator MII = MBB.begin();
595 MII != MBB.end(); ++MII)
596 if (unsigned delaySlots =
597 TM.getInstrInfo().getNumDelaySlots((*MII)->getOpCode()))
598 {
599 assert(delaySlots==1 && "Not handling multiple delay slots!");
600
601 MachineInstr *MInst = *MII;
602 MachineInstr *MDelayInst = *(MII+1);
603
604 // Check the 2 conditions above:
605 // (1) Does a branch need instructions added after it?
606 // (2) O/w does delay slot instr. need instrns before or after?
607 bool isBranch = (TM.getInstrInfo().isBranch((*MII)->getOpCode()) ||
608 TM.getInstrInfo().isReturn((*MII)->getOpCode()));
609 bool cond1 = isBranch && AddedInstrMap[MInst].InstrnsAfter.size() > 0;
610 bool cond2 = (AddedInstrMap.count(MDelayInst) ||
611 AddedInstrMap[MDelayInst].InstrnsAfter.size() > 0);
612
613 if (cond1 || cond2)
614 {
615 // Move delay slot instrn before the preceding branch.
616 // InsertBefore() modifies MII to point to the branch again.
617 assert(((*MII)->getOpCodeFlags() & AnnulFlag) == 0 &&
618 "FIXME: Annul bit must be turned off here!");
619 InsertBefore(MDelayInst, MBB, MII);
620
621 // In case (1), delete it and don't replace with anything!
622 // Otherwise (i.e., case (2) only) replace it with a NOP.
623 if (cond1) {
624 assert(AddedInstrMap[MInst].InstrnsAfter.size() <= delaySlots &&
625 "Cannot put more than #delaySlots spill instrns after "
626 "branch or return! Need to handle spill differently.");
627 DeleteInstruction(MBB, MII); // MII now points to next inst.
628 }
629 else {
630 MachineInstr* nopI =BuildMI(TM.getInstrInfo().getNOPOpCode(),1);
631 SubstituteInPlace(nopI, MBB, MII+1); // replace with NOP
632 }
633 }
634
635 // If this is not a branch or return (probably a call),
636 // the Instrnsafter, if any, must really go after the last
637 // delay slot. Move the InstrAfter to the instr. in that slot.
638 // We must do this after the previous code because the instructions
639 // in delay slots may get moved out by that code.
640 //
641 if (!isBranch)
642 move2DelayedInstr(MInst, *(MII+delaySlots));
643 }
644
645 // Finally iterate over all instructions in BB and insert before/after
646 //
Chris Lattnerf726e772002-10-28 19:22:04 +0000647 for (MachineBasicBlock::iterator MII = MBB.begin();
648 MII != MBB.end(); ++MII) {
649
Vikram S. Adve48762092002-04-25 04:34:15 +0000650 MachineInstr *MInst = *MII;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000651 unsigned Opcode = MInst->getOpCode();
652
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000653 // do not process Phis
Vikram S. Adve23a4c8f2002-03-18 03:37:19 +0000654 if (TM.getInstrInfo().isDummyPhiInstr(Opcode))
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000655 continue;
656
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000657 // Now add instructions that the register allocator inserts before/after
658 // this machine instructions (done only for calls/rets/incoming args)
659 // We do this here, to ensure that spill for an instruction is inserted
660 // closest as possible to an instruction (see above insertCode4Spill...)
Vikram S. Advecb202e32002-10-11 16:12:40 +0000661
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000662 // If there are instructions to be added, *before* this machine
663 // instruction, add them now.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000664 //
Chris Lattner7e708292002-06-25 16:13:24 +0000665 if (AddedInstrMap.count(MInst)) {
Chris Lattnerf726e772002-10-28 19:22:04 +0000666 PrependInstructions(AddedInstrMap[MInst].InstrnsBefore, MBB, MII,"");
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000667 }
Vikram S. Adve48762092002-04-25 04:34:15 +0000668
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000669 // If there are instructions to be added *after* this machine
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000670 // instruction, add them now. All cases with delay slots have been
671 // c
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000672 if (!AddedInstrMap[MInst].InstrnsAfter.empty()) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000673 AppendInstructions(AddedInstrMap[MInst].InstrnsAfter, MBB, MII,"");
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000674 }
Vikram S. Advecb202e32002-10-11 16:12:40 +0000675
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000676 } // for each machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000677 }
678}
679
680
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000681
682//----------------------------------------------------------------------------
683// This method inserts spill code for AN operand whose LR was spilled.
684// This method may be called several times for a single machine instruction
685// if it contains many spilled operands. Each time it is called, it finds
686// a register which is not live at that instruction and also which is not
687// used by other spilled operands of the same instruction. Then it uses
688// this register temporarily to accomodate the spilled value.
689//----------------------------------------------------------------------------
690void PhyRegAlloc::insertCode4SpilledLR(const LiveRange *LR,
691 MachineInstr *MInst,
692 const BasicBlock *BB,
693 const unsigned OpNum) {
694
Vikram S. Advead9c9782002-09-28 17:02:40 +0000695 assert((! TM.getInstrInfo().isCall(MInst->getOpCode()) || OpNum == 0) &&
696 "Outgoing arg of a call must be handled elsewhere (func arg ok)");
697 assert(! TM.getInstrInfo().isReturn(MInst->getOpCode()) &&
698 "Return value of a ret must be handled elsewhere");
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000699
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000700 MachineOperand& Op = MInst->getOperand(OpNum);
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000701 bool isDef = Op.opIsDefOnly();
702 bool isDefAndUse = Op.opIsDefAndUse();
Chris Lattner9d4ed152003-01-15 21:14:01 +0000703 unsigned RegType = MRI.getRegType(LR);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000704 int SpillOff = LR->getSpillOffFromFP();
705 RegClass *RC = LR->getRegClass();
Chris Lattner748697d2002-02-05 04:20:12 +0000706 const ValueSet &LVSetBef = LVI->getLiveVarSetBeforeMInst(MInst, BB);
Vikram S. Adve00521d72001-11-12 23:26:35 +0000707
Chris Lattnere90fcb72002-12-28 20:35:34 +0000708 MF.getInfo()->pushTempValue(MRI.getSpilledRegSize(RegType) );
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000709
Vikram S. Advef5af6362002-07-08 23:15:32 +0000710 vector<MachineInstr*> MIBef, MIAft;
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000711 vector<MachineInstr*> AdIMid;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000712
Vikram S. Adve3bf08922003-07-10 19:42:55 +0000713 // Choose a register to hold the spilled value, if one was not preallocated.
714 // This may insert code before and after MInst to free up the value. If so,
715 // this code should be first/last in the spill sequence before/after MInst.
716 int TmpRegU=(LR->hasColor()
717 ? MRI.getUnifiedRegNum(LR->getRegClass()->getID(),LR->getColor())
718 : getUsableUniRegAtMI(RegType, &LVSetBef, MInst, MIBef,MIAft));
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000719
Vikram S. Advef5af6362002-07-08 23:15:32 +0000720 // Set the operand first so that it this register does not get used
721 // as a scratch register for later calls to getUsableUniRegAtMI below
722 MInst->SetRegForOperand(OpNum, TmpRegU);
723
724 // get the added instructions for this instruction
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000725 AddedInstrns &AI = AddedInstrMap[MInst];
Vikram S. Advef5af6362002-07-08 23:15:32 +0000726
727 // We may need a scratch register to copy the spilled value to/from memory.
728 // This may itself have to insert code to free up a scratch register.
729 // Any such code should go before (after) the spill code for a load (store).
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000730 // The scratch reg is not marked as used because it is only used
731 // for the copy and not used across MInst.
Vikram S. Advef5af6362002-07-08 23:15:32 +0000732 int scratchRegType = -1;
733 int scratchReg = -1;
734 if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType))
735 {
Chris Lattner27a08932002-10-22 23:16:21 +0000736 scratchReg = getUsableUniRegAtMI(scratchRegType, &LVSetBef,
737 MInst, MIBef, MIAft);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000738 assert(scratchReg != MRI.getInvalidRegNum());
Vikram S. Advef5af6362002-07-08 23:15:32 +0000739 }
740
741 if (!isDef || isDefAndUse) {
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000742 // for a USE, we have to load the value of LR from stack to a TmpReg
743 // and use the TmpReg as one operand of instruction
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000744
Vikram S. Advef5af6362002-07-08 23:15:32 +0000745 // actual loading instruction(s)
746 MRI.cpMem2RegMI(AdIMid, MRI.getFramePointer(), SpillOff, TmpRegU, RegType,
747 scratchReg);
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000748
Vikram S. Advef5af6362002-07-08 23:15:32 +0000749 // the actual load should be after the instructions to free up TmpRegU
750 MIBef.insert(MIBef.end(), AdIMid.begin(), AdIMid.end());
751 AdIMid.clear();
752 }
753
Vikram S. Adve3bf08922003-07-10 19:42:55 +0000754 if (isDef || isDefAndUse) { // if this is a Def
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000755 // for a DEF, we have to store the value produced by this instruction
756 // on the stack position allocated for this LR
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000757
Vikram S. Advef5af6362002-07-08 23:15:32 +0000758 // actual storing instruction(s)
759 MRI.cpReg2MemMI(AdIMid, TmpRegU, MRI.getFramePointer(), SpillOff, RegType,
760 scratchReg);
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000761
Vikram S. Advef5af6362002-07-08 23:15:32 +0000762 MIAft.insert(MIAft.begin(), AdIMid.begin(), AdIMid.end());
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000763 } // if !DEF
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000764
Vikram S. Advef5af6362002-07-08 23:15:32 +0000765 // Finally, insert the entire spill code sequences before/after MInst
766 AI.InstrnsBefore.insert(AI.InstrnsBefore.end(), MIBef.begin(), MIBef.end());
767 AI.InstrnsAfter.insert(AI.InstrnsAfter.begin(), MIAft.begin(), MIAft.end());
768
Chris Lattner7e708292002-06-25 16:13:24 +0000769 if (DEBUG_RA) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000770 cerr << "\nFor Inst:\n " << *MInst;
771 cerr << "SPILLED LR# " << LR->getUserIGNode()->getIndex();
772 cerr << "; added Instructions:";
Anand Shuklad58290e2002-07-09 19:18:56 +0000773 for_each(MIBef.begin(), MIBef.end(), std::mem_fun(&MachineInstr::dump));
774 for_each(MIAft.begin(), MIAft.end(), std::mem_fun(&MachineInstr::dump));
Chris Lattner7e708292002-06-25 16:13:24 +0000775 }
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000776}
777
778
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000779//----------------------------------------------------------------------------
780// We can use the following method to get a temporary register to be used
781// BEFORE any given machine instruction. If there is a register available,
782// this method will simply return that register and set MIBef = MIAft = NULL.
783// Otherwise, it will return a register and MIAft and MIBef will contain
784// two instructions used to free up this returned register.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000785// Returned register number is the UNIFIED register number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000786//----------------------------------------------------------------------------
787
Vikram S. Advef5af6362002-07-08 23:15:32 +0000788int PhyRegAlloc::getUsableUniRegAtMI(const int RegType,
789 const ValueSet *LVSetBef,
790 MachineInstr *MInst,
791 std::vector<MachineInstr*>& MIBef,
792 std::vector<MachineInstr*>& MIAft) {
793
Chris Lattner133f0792002-10-28 04:45:29 +0000794 RegClass* RC = getRegClassByID(MRI.getRegClassIDOfRegType(RegType));
Vikram S. Advef5af6362002-07-08 23:15:32 +0000795
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000796 int RegU = getUnusedUniRegAtMI(RC, MInst, LVSetBef);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000797
798 if (RegU == -1) {
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000799 // we couldn't find an unused register. Generate code to free up a reg by
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000800 // saving it on stack and restoring after the instruction
Vikram S. Advef5af6362002-07-08 23:15:32 +0000801
Chris Lattnere90fcb72002-12-28 20:35:34 +0000802 int TmpOff = MF.getInfo()->pushTempValue(MRI.getSpilledRegSize(RegType));
Vikram S. Adve12af1642001-11-08 04:48:50 +0000803
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000804 RegU = getUniRegNotUsedByThisInst(RC, MInst);
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000805
Vikram S. Advef5af6362002-07-08 23:15:32 +0000806 // Check if we need a scratch register to copy this register to memory.
807 int scratchRegType = -1;
808 if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType))
809 {
Chris Lattner133f0792002-10-28 04:45:29 +0000810 int scratchReg = getUsableUniRegAtMI(scratchRegType, LVSetBef,
811 MInst, MIBef, MIAft);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000812 assert(scratchReg != MRI.getInvalidRegNum());
813
814 // We may as well hold the value in the scratch register instead
815 // of copying it to memory and back. But we have to mark the
816 // register as used by this instruction, so it does not get used
817 // as a scratch reg. by another operand or anyone else.
Chris Lattner27a08932002-10-22 23:16:21 +0000818 MInst->insertUsedReg(scratchReg);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000819 MRI.cpReg2RegMI(MIBef, RegU, scratchReg, RegType);
820 MRI.cpReg2RegMI(MIAft, scratchReg, RegU, RegType);
821 }
822 else
823 { // the register can be copied directly to/from memory so do it.
824 MRI.cpReg2MemMI(MIBef, RegU, MRI.getFramePointer(), TmpOff, RegType);
825 MRI.cpMem2RegMI(MIAft, MRI.getFramePointer(), TmpOff, RegU, RegType);
826 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000827 }
Vikram S. Advef5af6362002-07-08 23:15:32 +0000828
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000829 return RegU;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000830}
831
832//----------------------------------------------------------------------------
833// This method is called to get a new unused register that can be used to
834// accomodate a spilled value.
835// This method may be called several times for a single machine instruction
836// if it contains many spilled operands. Each time it is called, it finds
837// a register which is not live at that instruction and also which is not
838// used by other spilled operands of the same instruction.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000839// Return register number is relative to the register class. NOT
840// unified number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000841//----------------------------------------------------------------------------
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000842
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000843int PhyRegAlloc::getUnusedUniRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000844 const MachineInstr *MInst,
Chris Lattner296b7732002-02-05 02:52:05 +0000845 const ValueSet *LVSetBef) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000846
847 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
848
Chris Lattner85c54652002-05-23 15:50:03 +0000849 std::vector<bool> &IsColorUsedArr = RC->getIsColorUsedArr();
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000850
Chris Lattner7e708292002-06-25 16:13:24 +0000851 for (unsigned i=0; i < NumAvailRegs; i++) // Reset array
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000852 IsColorUsedArr[i] = false;
853
Chris Lattner296b7732002-02-05 02:52:05 +0000854 ValueSet::const_iterator LIt = LVSetBef->begin();
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000855
856 // for each live var in live variable set after machine inst
Chris Lattner7e708292002-06-25 16:13:24 +0000857 for ( ; LIt != LVSetBef->end(); ++LIt) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000858
859 // get the live range corresponding to live var
860 LiveRange *const LRofLV = LRI.getLiveRangeForValue(*LIt );
861
862 // LR can be null if it is a const since a const
863 // doesn't have a dominating def - see Assumptions above
Chris Lattner7e708292002-06-25 16:13:24 +0000864 if (LRofLV && LRofLV->getRegClass() == RC && LRofLV->hasColor() )
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000865 IsColorUsedArr[ LRofLV->getColor() ] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000866 }
867
868 // It is possible that one operand of this MInst was already spilled
869 // and it received some register temporarily. If that's the case,
870 // it is recorded in machine operand. We must skip such registers.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000871 //
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000872 setRelRegsUsedByThisInst(RC, MInst);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000873
Chris Lattner7e708292002-06-25 16:13:24 +0000874 for (unsigned c=0; c < NumAvailRegs; c++) // find first unused color
Chris Lattner85c54652002-05-23 15:50:03 +0000875 if (!IsColorUsedArr[c])
876 return MRI.getUnifiedRegNum(RC->getID(), c);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000877
Chris Lattner85c54652002-05-23 15:50:03 +0000878 return -1;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000879}
880
881
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000882//----------------------------------------------------------------------------
883// Get any other register in a register class, other than what is used
884// by operands of a machine instruction. Returns the unified reg number.
885//----------------------------------------------------------------------------
886int PhyRegAlloc::getUniRegNotUsedByThisInst(RegClass *RC,
Chris Lattner85c54652002-05-23 15:50:03 +0000887 const MachineInstr *MInst) {
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000888
Chris Lattner85c54652002-05-23 15:50:03 +0000889 vector<bool> &IsColorUsedArr = RC->getIsColorUsedArr();
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000890 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
891
Chris Lattner7e708292002-06-25 16:13:24 +0000892 for (unsigned i=0; i < NumAvailRegs ; i++) // Reset array
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000893 IsColorUsedArr[i] = false;
894
895 setRelRegsUsedByThisInst(RC, MInst);
896
Chris Lattner7e708292002-06-25 16:13:24 +0000897 for (unsigned c=0; c < RC->getNumOfAvailRegs(); c++)// find first unused color
Chris Lattner85c54652002-05-23 15:50:03 +0000898 if (!IsColorUsedArr[c])
899 return MRI.getUnifiedRegNum(RC->getID(), c);
900
901 assert(0 && "FATAL: No free register could be found in reg class!!");
Chris Lattner697954c2002-01-20 22:54:45 +0000902 return 0;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000903}
904
905
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000906//----------------------------------------------------------------------------
907// This method modifies the IsColorUsedArr of the register class passed to it.
908// It sets the bits corresponding to the registers used by this machine
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000909// instructions. Both explicit and implicit operands are set.
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000910//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000911void PhyRegAlloc::setRelRegsUsedByThisInst(RegClass *RC,
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000912 const MachineInstr *MInst )
913{
914 assert(OperandsColoredMap[MInst] == true &&
915 "Illegal to call setRelRegsUsedByThisInst() until colored operands "
916 "are marked for an instruction.");
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000917
Vikram S. Advef5af6362002-07-08 23:15:32 +0000918 vector<bool> &IsColorUsedArr = RC->getIsColorUsedArr();
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000919
Vikram S. Advef5af6362002-07-08 23:15:32 +0000920 // Add the registers already marked as used by the instruction.
921 // This should include any scratch registers that are used to save
922 // values across the instruction (e.g., for saving state register values).
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000923 const std::set<int> &regsUsed = MInst->getRegsUsed();
924 for (std::set<int>::iterator I=regsUsed.begin(), E=regsUsed.end(); I != E; ++I)
925 {
926 int i = *I;
Vikram S. Advef5af6362002-07-08 23:15:32 +0000927 unsigned classId = 0;
Chris Lattner27a08932002-10-22 23:16:21 +0000928 int classRegNum = MRI.getClassRegNum(i, classId);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000929 if (RC->getID() == classId)
930 {
931 assert(classRegNum < (int) IsColorUsedArr.size() &&
932 "Illegal register number for this reg class?");
933 IsColorUsedArr[classRegNum] = true;
934 }
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000935 }
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000936
Vikram S. Advef5af6362002-07-08 23:15:32 +0000937 // If there are implicit references, mark their allocated regs as well
938 //
939 for (unsigned z=0; z < MInst->getNumImplicitRefs(); z++)
940 if (const LiveRange*
941 LRofImpRef = LRI.getLiveRangeForValue(MInst->getImplicitRef(z)))
942 if (LRofImpRef->hasColor())
943 // this implicit reference is in a LR that received a color
944 IsColorUsedArr[LRofImpRef->getColor()] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000945}
946
947
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000948//----------------------------------------------------------------------------
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000949// If there are delay slots for an instruction, the instructions
950// added after it must really go after the delayed instruction(s).
951// So, we move the InstrAfter of that instruction to the
952// corresponding delayed instruction using the following method.
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000953//----------------------------------------------------------------------------
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000954
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000955void PhyRegAlloc::move2DelayedInstr(const MachineInstr *OrigMI,
956 const MachineInstr *DelayedMI)
957{
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000958 // "added after" instructions of the original instr
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000959 std::vector<MachineInstr *> &OrigAft = AddedInstrMap[OrigMI].InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000960
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000961 // "added after" instructions of the delayed instr
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000962 std::vector<MachineInstr *> &DelayedAft =AddedInstrMap[DelayedMI].InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000963
964 // go thru all the "added after instructions" of the original instruction
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000965 // and append them to the "added after instructions" of the delayed
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000966 // instructions
Chris Lattner697954c2002-01-20 22:54:45 +0000967 DelayedAft.insert(DelayedAft.end(), OrigAft.begin(), OrigAft.end());
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000968
969 // empty the "added after instructions" of the original instruction
970 OrigAft.clear();
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000971}
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000972
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000973//----------------------------------------------------------------------------
974// This method prints the code with registers after register allocation is
975// complete.
976//----------------------------------------------------------------------------
977void PhyRegAlloc::printMachineCode()
978{
979
Chris Lattnerf726e772002-10-28 19:22:04 +0000980 cerr << "\n;************** Function " << Fn->getName()
Chris Lattner697954c2002-01-20 22:54:45 +0000981 << " *****************\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000982
Chris Lattnerf726e772002-10-28 19:22:04 +0000983 for (MachineFunction::iterator BBI = MF.begin(), BBE = MF.end();
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000984 BBI != BBE; ++BBI) {
Chris Lattnerf726e772002-10-28 19:22:04 +0000985 cerr << "\n"; printLabel(BBI->getBasicBlock()); cerr << ": ";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000986
987 // get the iterator for machine instructions
Chris Lattnerf726e772002-10-28 19:22:04 +0000988 MachineBasicBlock& MBB = *BBI;
989 MachineBasicBlock::iterator MII = MBB.begin();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000990
991 // iterate over all the machine instructions in BB
Chris Lattnerf726e772002-10-28 19:22:04 +0000992 for ( ; MII != MBB.end(); ++MII) {
Chris Lattnerd9512ca2002-10-29 17:35:39 +0000993 MachineInstr *MInst = *MII;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000994
Chris Lattner697954c2002-01-20 22:54:45 +0000995 cerr << "\n\t";
Chris Lattnerd9512ca2002-10-29 17:35:39 +0000996 cerr << TM.getInstrInfo().getName(MInst->getOpCode());
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000997
Chris Lattner7e708292002-06-25 16:13:24 +0000998 for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000999 MachineOperand& Op = MInst->getOperand(OpNum);
1000
Chris Lattner133f0792002-10-28 04:45:29 +00001001 if (Op.getType() == MachineOperand::MO_VirtualRegister ||
1002 Op.getType() == MachineOperand::MO_CCRegister /*||
1003 Op.getType() == MachineOperand::MO_PCRelativeDisp*/ ) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001004
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001005 const Value *const Val = Op.getVRegValue () ;
Ruchira Sasankae727f852001-09-18 22:43:57 +00001006 // ****this code is temporary till NULL Values are fixed
Chris Lattner7e708292002-06-25 16:13:24 +00001007 if (! Val ) {
Chris Lattner697954c2002-01-20 22:54:45 +00001008 cerr << "\t<*NULL*>";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001009 continue;
1010 }
Ruchira Sasankae727f852001-09-18 22:43:57 +00001011
1012 // if a label or a constant
Chris Lattner7e708292002-06-25 16:13:24 +00001013 if (isa<BasicBlock>(Val)) {
Chris Lattner697954c2002-01-20 22:54:45 +00001014 cerr << "\t"; printLabel( Op.getVRegValue () );
1015 } else {
Ruchira Sasankae727f852001-09-18 22:43:57 +00001016 // else it must be a register value
1017 const int RegNum = Op.getAllocatedRegNum();
1018
Chris Lattner697954c2002-01-20 22:54:45 +00001019 cerr << "\t" << "%" << MRI.getUnifiedRegName( RegNum );
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001020 if (Val->hasName() )
Chris Lattner697954c2002-01-20 22:54:45 +00001021 cerr << "(" << Val->getName() << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001022 else
Chris Lattner697954c2002-01-20 22:54:45 +00001023 cerr << "(" << Val << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001024
Vikram S. Adve5f2180c2003-05-27 00:05:23 +00001025 if (Op.opIsDefOnly() || Op.opIsDefAndUse())
Chris Lattner697954c2002-01-20 22:54:45 +00001026 cerr << "*";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001027
1028 const LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
Chris Lattner7e708292002-06-25 16:13:24 +00001029 if (LROfVal )
1030 if (LROfVal->hasSpillOffset() )
Chris Lattner697954c2002-01-20 22:54:45 +00001031 cerr << "$";
Ruchira Sasankae727f852001-09-18 22:43:57 +00001032 }
1033
1034 }
Chris Lattner133f0792002-10-28 04:45:29 +00001035 else if (Op.getType() == MachineOperand::MO_MachineRegister) {
Chris Lattner697954c2002-01-20 22:54:45 +00001036 cerr << "\t" << "%" << MRI.getUnifiedRegName(Op.getMachineRegNum());
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001037 }
1038
1039 else
Chris Lattner697954c2002-01-20 22:54:45 +00001040 cerr << "\t" << Op; // use dump field
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001041 }
1042
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001043
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001044
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001045 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
Chris Lattner7e708292002-06-25 16:13:24 +00001046 if (NumOfImpRefs > 0) {
Chris Lattner697954c2002-01-20 22:54:45 +00001047 cerr << "\tImplicit:";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001048
Chris Lattner7e708292002-06-25 16:13:24 +00001049 for (unsigned z=0; z < NumOfImpRefs; z++)
Chris Lattner0665a5f2002-02-05 01:43:49 +00001050 cerr << RAV(MInst->getImplicitRef(z)) << "\t";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001051 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001052
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001053 } // for all machine instructions
1054
Chris Lattner697954c2002-01-20 22:54:45 +00001055 cerr << "\n";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001056
1057 } // for all BBs
1058
Chris Lattner697954c2002-01-20 22:54:45 +00001059 cerr << "\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001060}
1061
Ruchira Sasankae727f852001-09-18 22:43:57 +00001062
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001063//----------------------------------------------------------------------------
1064
1065//----------------------------------------------------------------------------
1066void PhyRegAlloc::colorIncomingArgs()
1067{
Chris Lattnerf726e772002-10-28 19:22:04 +00001068 MRI.colorMethodArgs(Fn, LRI, &AddedInstrAtEntry);
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001069}
1070
Ruchira Sasankae727f852001-09-18 22:43:57 +00001071
1072//----------------------------------------------------------------------------
1073// Used to generate a label for a basic block
1074//----------------------------------------------------------------------------
Chris Lattnerf726e772002-10-28 19:22:04 +00001075void PhyRegAlloc::printLabel(const Value *Val) {
Chris Lattner697954c2002-01-20 22:54:45 +00001076 if (Val->hasName())
1077 cerr << Val->getName();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001078 else
Chris Lattnerf726e772002-10-28 19:22:04 +00001079 cerr << "Label" << Val;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001080}
1081
1082
Ruchira Sasankae727f852001-09-18 22:43:57 +00001083//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001084// This method calls setSugColorUsable method of each live range. This
1085// will determine whether the suggested color of LR is really usable.
1086// A suggested color is not usable when the suggested color is volatile
1087// AND when there are call interferences
1088//----------------------------------------------------------------------------
1089
1090void PhyRegAlloc::markUnusableSugColors()
1091{
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001092 // hash map iterator
1093 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
1094 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
1095
Chris Lattner7e708292002-06-25 16:13:24 +00001096 for (; HMI != HMIEnd ; ++HMI ) {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001097 if (HMI->first) {
1098 LiveRange *L = HMI->second; // get the LiveRange
1099 if (L) {
Chris Lattner7e708292002-06-25 16:13:24 +00001100 if (L->hasSuggestedColor()) {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001101 int RCID = L->getRegClass()->getID();
Chris Lattner7e708292002-06-25 16:13:24 +00001102 if (MRI.isRegVolatile( RCID, L->getSuggestedColor()) &&
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001103 L->isCallInterference() )
1104 L->setSuggestedColorUsable( false );
1105 else
1106 L->setSuggestedColorUsable( true );
1107 }
1108 } // if L->hasSuggestedColor()
1109 }
1110 } // for all LR's in hash map
1111}
1112
1113
1114
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001115//----------------------------------------------------------------------------
1116// The following method will set the stack offsets of the live ranges that
1117// are decided to be spillled. This must be called just after coloring the
1118// LRs using the graph coloring algo. For each live range that is spilled,
1119// this method allocate a new spill position on the stack.
1120//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001121
Chris Lattner37730942002-02-05 03:52:29 +00001122void PhyRegAlloc::allocateStackSpace4SpilledLRs() {
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001123 if (DEBUG_RA) cerr << "\nSetting LR stack offsets for spills...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001124
Chris Lattner37730942002-02-05 03:52:29 +00001125 LiveRangeMapType::const_iterator HMI = LRI.getLiveRangeMap()->begin();
1126 LiveRangeMapType::const_iterator HMIEnd = LRI.getLiveRangeMap()->end();
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001127
Chris Lattner7e708292002-06-25 16:13:24 +00001128 for ( ; HMI != HMIEnd ; ++HMI) {
Chris Lattner37730942002-02-05 03:52:29 +00001129 if (HMI->first && HMI->second) {
Vikram S. Adve3bf08922003-07-10 19:42:55 +00001130 LiveRange *L = HMI->second; // get the LiveRange
1131 if (L->isMarkedForSpill()) { // NOTE: allocating size of long Type **
Chris Lattnere90fcb72002-12-28 20:35:34 +00001132 int stackOffset = MF.getInfo()->allocateSpilledValue(Type::LongTy);
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001133 L->setSpillOffFromFP(stackOffset);
1134 if (DEBUG_RA)
1135 cerr << " LR# " << L->getUserIGNode()->getIndex()
1136 << ": stack-offset = " << stackOffset << "\n";
1137 }
Chris Lattner37730942002-02-05 03:52:29 +00001138 }
1139 } // for all LR's in hash map
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001140}
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001141
1142
1143
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001144//----------------------------------------------------------------------------
Ruchira Sasankae727f852001-09-18 22:43:57 +00001145// The entry pont to Register Allocation
1146//----------------------------------------------------------------------------
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001147
1148void PhyRegAlloc::allocateRegisters()
1149{
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001150
1151 // make sure that we put all register classes into the RegClassList
1152 // before we call constructLiveRanges (now done in the constructor of
1153 // PhyRegAlloc class).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001154 //
1155 LRI.constructLiveRanges(); // create LR info
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001156
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001157 if (DEBUG_RA >= RA_DEBUG_LiveRanges)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001158 LRI.printLiveRanges();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001159
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001160 createIGNodeListsAndIGs(); // create IGNode list and IGs
1161
1162 buildInterferenceGraphs(); // build IGs in all reg classes
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001163
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001164
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001165 if (DEBUG_RA >= RA_DEBUG_LiveRanges) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001166 // print all LRs in all reg classes
Chris Lattner7e708292002-06-25 16:13:24 +00001167 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
1168 RegClassList[rc]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001169
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001170 // print IGs in all register classes
Chris Lattner7e708292002-06-25 16:13:24 +00001171 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
1172 RegClassList[rc]->printIG();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001173 }
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001174
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001175 LRI.coalesceLRs(); // coalesce all live ranges
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +00001176
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001177 if (DEBUG_RA >= RA_DEBUG_LiveRanges) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001178 // print all LRs in all reg classes
Chris Lattnerf726e772002-10-28 19:22:04 +00001179 for (unsigned rc=0; rc < NumOfRegClasses; rc++)
1180 RegClassList[rc]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001181
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001182 // print IGs in all register classes
Chris Lattnerf726e772002-10-28 19:22:04 +00001183 for (unsigned rc=0; rc < NumOfRegClasses; rc++)
1184 RegClassList[rc]->printIG();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001185 }
1186
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001187
1188 // mark un-usable suggested color before graph coloring algorithm.
1189 // When this is done, the graph coloring algo will not reserve
1190 // suggested color unnecessarily - they can be used by another LR
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001191 //
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001192 markUnusableSugColors();
1193
1194 // color all register classes using the graph coloring algo
Chris Lattner7e708292002-06-25 16:13:24 +00001195 for (unsigned rc=0; rc < NumOfRegClasses ; rc++)
Chris Lattnerf726e772002-10-28 19:22:04 +00001196 RegClassList[rc]->colorAllRegs();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001197
Chris Lattnere90fcb72002-12-28 20:35:34 +00001198 // Atter graph coloring, if some LRs did not receive a color (i.e, spilled)
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001199 // a poistion for such spilled LRs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001200 //
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001201 allocateStackSpace4SpilledLRs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001202
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001203 // Reset the temp. area on the stack before use by the first instruction.
1204 // This will also happen after updating each instruction.
1205 MF.getInfo()->popAllTempValues();
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001206
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001207 // color incoming args - if the correct color was not received
1208 // insert code to copy to the correct register
1209 //
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001210 colorIncomingArgs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001211
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001212 // Now update the machine code with register names and add any
1213 // additional code inserted by the register allocator to the instruction
1214 // stream
1215 //
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001216 updateMachineCode();
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001217
Chris Lattner045e7c82001-09-19 16:26:23 +00001218 if (DEBUG_RA) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001219 cerr << "\n**** Machine Code After Register Allocation:\n\n";
Chris Lattnerf726e772002-10-28 19:22:04 +00001220 MF.dump();
Chris Lattner045e7c82001-09-19 16:26:23 +00001221 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001222}
1223
Ruchira Sasankae727f852001-09-18 22:43:57 +00001224
1225