blob: 613c16db751af454fcb9d3ba55e7093d03af72b5 [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
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000436DeleteInstruction(MachineBasicBlock& MBB,
437 MachineBasicBlock::iterator& MII)
438{
439 MII = MBB.erase(MII);
440}
441
442inline void
Vikram S. Advecb202e32002-10-11 16:12:40 +0000443SubstituteInPlace(MachineInstr* newMI,
Chris Lattnerf726e772002-10-28 19:22:04 +0000444 MachineBasicBlock& MBB,
Chris Lattner32be9f62002-10-28 01:41:27 +0000445 MachineBasicBlock::iterator MII)
Vikram S. Advecb202e32002-10-11 16:12:40 +0000446{
447 *MII = newMI;
448}
449
450inline void
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000451PrependInstructions(vector<MachineInstr *> &IBef,
Chris Lattnerf726e772002-10-28 19:22:04 +0000452 MachineBasicBlock& MBB,
Chris Lattner32be9f62002-10-28 01:41:27 +0000453 MachineBasicBlock::iterator& MII,
Vikram S. Adve48762092002-04-25 04:34:15 +0000454 const std::string& msg)
455{
456 if (!IBef.empty())
457 {
458 MachineInstr* OrigMI = *MII;
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000459 std::vector<MachineInstr *>::iterator AdIt;
Vikram S. Adve48762092002-04-25 04:34:15 +0000460 for (AdIt = IBef.begin(); AdIt != IBef.end() ; ++AdIt)
461 {
462 if (DEBUG_RA) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000463 if (OrigMI) cerr << "For MInst:\n " << *OrigMI;
464 cerr << msg << "PREPENDed instr:\n " << **AdIt << "\n";
Vikram S. Adve48762092002-04-25 04:34:15 +0000465 }
Chris Lattnerf726e772002-10-28 19:22:04 +0000466 InsertBefore(*AdIt, MBB, MII);
Vikram S. Adve48762092002-04-25 04:34:15 +0000467 }
468 }
469}
470
471inline void
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000472AppendInstructions(std::vector<MachineInstr *> &IAft,
Chris Lattnerf726e772002-10-28 19:22:04 +0000473 MachineBasicBlock& MBB,
Chris Lattner32be9f62002-10-28 01:41:27 +0000474 MachineBasicBlock::iterator& MII,
Vikram S. Adve48762092002-04-25 04:34:15 +0000475 const std::string& msg)
476{
477 if (!IAft.empty())
478 {
479 MachineInstr* OrigMI = *MII;
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000480 std::vector<MachineInstr *>::iterator AdIt;
Chris Lattner7e708292002-06-25 16:13:24 +0000481 for ( AdIt = IAft.begin(); AdIt != IAft.end() ; ++AdIt )
Vikram S. Adve48762092002-04-25 04:34:15 +0000482 {
Chris Lattner7e708292002-06-25 16:13:24 +0000483 if (DEBUG_RA) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000484 if (OrigMI) cerr << "For MInst:\n " << *OrigMI;
485 cerr << msg << "APPENDed instr:\n " << **AdIt << "\n";
Vikram S. Adve48762092002-04-25 04:34:15 +0000486 }
Chris Lattnerf726e772002-10-28 19:22:04 +0000487 InsertAfter(*AdIt, MBB, MII);
Vikram S. Adve48762092002-04-25 04:34:15 +0000488 }
489 }
490}
491
492
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000493void PhyRegAlloc::updateInstruction(MachineInstr* MInst, BasicBlock* BB)
494{
495 unsigned Opcode = MInst->getOpCode();
496
497 // Reset tmp stack positions so they can be reused for each machine instr.
498 MF.getInfo()->popAllTempValues();
499
500 // First, set the registers for operands in the machine instruction
501 // if a register was successfully allocated. Do this first because we
502 // will need to know which registers are already used by this instr'n.
503 //
504 for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum)
505 {
506 MachineOperand& Op = MInst->getOperand(OpNum);
507 if (Op.getType() == MachineOperand::MO_VirtualRegister ||
508 Op.getType() == MachineOperand::MO_CCRegister)
509 {
510 const Value *const Val = Op.getVRegValue();
511 if (const LiveRange* LR = LRI.getLiveRangeForValue(Val))
512 if (LR->hasColor())
513 MInst->SetRegForOperand(OpNum,
514 MRI.getUnifiedRegNum(LR->getRegClass()->getID(),
515 LR->getColor()));
516 }
517 } // for each operand
518
519 // Mark that the operands have been updated. setRelRegsUsedByThisInst()
520 // is called to find registers used by each MachineInst, and it should not
521 // be used for an instruction until this is done. This flag just serves
522 // as a sanity check.
523 OperandsColoredMap[MInst] = true;
524
525 // Now insert special instructions (if necessary) for call/return
526 // instructions. Do this before inserting spill code since some
527 // registers must be used by outgoing call arguments or the return value
528 // of a call, and spill code should not use those registers.
529 //
530 if (TM.getInstrInfo().isCall(Opcode) ||
531 TM.getInstrInfo().isReturn(Opcode)) {
532 AddedInstrns &AI = AddedInstrMap[MInst];
533
534 if (TM.getInstrInfo().isCall(Opcode))
535 MRI.colorCallArgs(MInst, LRI, &AI, *this, BB);
536 else if (TM.getInstrInfo().isReturn(Opcode))
537 MRI.colorRetValue(MInst, LRI, &AI);
538 }
539
540 // Now insert spill code for remaining operands not allocated to
541 // registers. This must be done even for call return instructions
542 // since those are not handled by the special code above.
543 for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum)
544 {
545 MachineOperand& Op = MInst->getOperand(OpNum);
546 if (Op.getType() == MachineOperand::MO_VirtualRegister ||
547 Op.getType() == MachineOperand::MO_CCRegister)
548 {
549 const Value* Val = Op.getVRegValue();
550 if (const LiveRange *LR = LRI.getLiveRangeForValue(Val))
551 if (! LR->hasColor())
552 insertCode4SpilledLR(LR, MInst, BB, OpNum);
553 }
554 } // for each operand
555}
556
557void PhyRegAlloc::updateMachineCode()
558{
Chris Lattner7e708292002-06-25 16:13:24 +0000559 // Insert any instructions needed at method entry
Chris Lattnerf726e772002-10-28 19:22:04 +0000560 MachineBasicBlock::iterator MII = MF.front().begin();
561 PrependInstructions(AddedInstrAtEntry.InstrnsBefore, MF.front(), MII,
Chris Lattner7e708292002-06-25 16:13:24 +0000562 "At function entry: \n");
563 assert(AddedInstrAtEntry.InstrnsAfter.empty() &&
564 "InstrsAfter should be unnecessary since we are just inserting at "
565 "the function entry point here.");
Vikram S. Adve48762092002-04-25 04:34:15 +0000566
Chris Lattnerf726e772002-10-28 19:22:04 +0000567 for (MachineFunction::iterator BBI = MF.begin(), BBE = MF.end();
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000568 BBI != BBE; ++BBI) {
Vikram S. Advecb202e32002-10-11 16:12:40 +0000569
Chris Lattnerf726e772002-10-28 19:22:04 +0000570 MachineBasicBlock &MBB = *BBI;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000571
572 // Iterate over all machine instructions in BB and mark operands with
573 // their assigned registers or insert spill code, as appropriate.
574 // Also, fix operands of call/return instructions.
575 //
576 for (MachineBasicBlock::iterator MII = MBB.begin(); MII != MBB.end(); ++MII)
577 if (!TM.getInstrInfo().isDummyPhiInstr((*MII)->getOpCode())) // ignore Phis
578 updateInstruction(*MII, MBB.getBasicBlock());
579
580 // Now, move code out of delay slots of branches and returns if needed.
581 // (Also, move "after" code from calls to the last delay slot instruction.)
582 // Moving code out of delay slots is needed in 2 situations:
583 // (1) If this is a branch and it needs instructions inserted after it,
584 // move any existing instructions out of the delay slot so that the
585 // instructions can go into the delay slot. This only supports the
586 // case that #instrsAfter <= #delay slots.
587 //
588 // (2) If any instruction in the delay slot needs
589 // instructions inserted, move it out of the delay slot and before the
590 // branch because putting code before or after it would be VERY BAD!
591 //
592 // If the annul bit of the branch is set, neither of these is legal!
593 // If so, we need to handle spill differently but annulling is not yet used.
594 //
595 for (MachineBasicBlock::iterator MII = MBB.begin();
596 MII != MBB.end(); ++MII)
597 if (unsigned delaySlots =
598 TM.getInstrInfo().getNumDelaySlots((*MII)->getOpCode()))
599 {
600 assert(delaySlots==1 && "Not handling multiple delay slots!");
601
602 MachineInstr *MInst = *MII;
603 MachineInstr *MDelayInst = *(MII+1);
604
605 // Check the 2 conditions above:
606 // (1) Does a branch need instructions added after it?
607 // (2) O/w does delay slot instr. need instrns before or after?
608 bool isBranch = (TM.getInstrInfo().isBranch((*MII)->getOpCode()) ||
609 TM.getInstrInfo().isReturn((*MII)->getOpCode()));
610 bool cond1 = isBranch && AddedInstrMap[MInst].InstrnsAfter.size() > 0;
611 bool cond2 = (AddedInstrMap.count(MDelayInst) ||
612 AddedInstrMap[MDelayInst].InstrnsAfter.size() > 0);
613
614 if (cond1 || cond2)
615 {
616 // Move delay slot instrn before the preceding branch.
617 // InsertBefore() modifies MII to point to the branch again.
618 assert(((*MII)->getOpCodeFlags() & AnnulFlag) == 0 &&
619 "FIXME: Annul bit must be turned off here!");
620 InsertBefore(MDelayInst, MBB, MII);
621
622 // In case (1), delete it and don't replace with anything!
623 // Otherwise (i.e., case (2) only) replace it with a NOP.
624 if (cond1) {
625 assert(AddedInstrMap[MInst].InstrnsAfter.size() <= delaySlots &&
626 "Cannot put more than #delaySlots spill instrns after "
627 "branch or return! Need to handle spill differently.");
628 DeleteInstruction(MBB, MII); // MII now points to next inst.
629 }
630 else {
631 MachineInstr* nopI =BuildMI(TM.getInstrInfo().getNOPOpCode(),1);
632 SubstituteInPlace(nopI, MBB, MII+1); // replace with NOP
633 }
634 }
635
636 // If this is not a branch or return (probably a call),
637 // the Instrnsafter, if any, must really go after the last
638 // delay slot. Move the InstrAfter to the instr. in that slot.
639 // We must do this after the previous code because the instructions
640 // in delay slots may get moved out by that code.
641 //
642 if (!isBranch)
643 move2DelayedInstr(MInst, *(MII+delaySlots));
644 }
645
646 // Finally iterate over all instructions in BB and insert before/after
647 //
Chris Lattnerf726e772002-10-28 19:22:04 +0000648 for (MachineBasicBlock::iterator MII = MBB.begin();
649 MII != MBB.end(); ++MII) {
650
Vikram S. Adve48762092002-04-25 04:34:15 +0000651 MachineInstr *MInst = *MII;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000652 unsigned Opcode = MInst->getOpCode();
653
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000654 // do not process Phis
Vikram S. Adve23a4c8f2002-03-18 03:37:19 +0000655 if (TM.getInstrInfo().isDummyPhiInstr(Opcode))
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000656 continue;
657
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000658 // Now add instructions that the register allocator inserts before/after
659 // this machine instructions (done only for calls/rets/incoming args)
660 // We do this here, to ensure that spill for an instruction is inserted
661 // closest as possible to an instruction (see above insertCode4Spill...)
Vikram S. Advecb202e32002-10-11 16:12:40 +0000662
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000663 // If there are instructions to be added, *before* this machine
664 // instruction, add them now.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000665 //
Chris Lattner7e708292002-06-25 16:13:24 +0000666 if (AddedInstrMap.count(MInst)) {
Chris Lattnerf726e772002-10-28 19:22:04 +0000667 PrependInstructions(AddedInstrMap[MInst].InstrnsBefore, MBB, MII,"");
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000668 }
Vikram S. Adve48762092002-04-25 04:34:15 +0000669
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000670 // If there are instructions to be added *after* this machine
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000671 // instruction, add them now. All cases with delay slots have been
672 // c
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000673 if (!AddedInstrMap[MInst].InstrnsAfter.empty()) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000674 AppendInstructions(AddedInstrMap[MInst].InstrnsAfter, MBB, MII,"");
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000675 }
Vikram S. Advecb202e32002-10-11 16:12:40 +0000676
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000677 } // for each machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000678 }
679}
680
681
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000682
683//----------------------------------------------------------------------------
684// This method inserts spill code for AN operand whose LR was spilled.
685// This method may be called several times for a single machine instruction
686// if it contains many spilled operands. Each time it is called, it finds
687// a register which is not live at that instruction and also which is not
688// used by other spilled operands of the same instruction. Then it uses
689// this register temporarily to accomodate the spilled value.
690//----------------------------------------------------------------------------
691void PhyRegAlloc::insertCode4SpilledLR(const LiveRange *LR,
692 MachineInstr *MInst,
693 const BasicBlock *BB,
694 const unsigned OpNum) {
695
Vikram S. Advead9c9782002-09-28 17:02:40 +0000696 assert((! TM.getInstrInfo().isCall(MInst->getOpCode()) || OpNum == 0) &&
697 "Outgoing arg of a call must be handled elsewhere (func arg ok)");
698 assert(! TM.getInstrInfo().isReturn(MInst->getOpCode()) &&
699 "Return value of a ret must be handled elsewhere");
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000700
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000701 MachineOperand& Op = MInst->getOperand(OpNum);
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000702 bool isDef = Op.opIsDefOnly();
703 bool isDefAndUse = Op.opIsDefAndUse();
Chris Lattner9d4ed152003-01-15 21:14:01 +0000704 unsigned RegType = MRI.getRegType(LR);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000705 int SpillOff = LR->getSpillOffFromFP();
706 RegClass *RC = LR->getRegClass();
Chris Lattner748697d2002-02-05 04:20:12 +0000707 const ValueSet &LVSetBef = LVI->getLiveVarSetBeforeMInst(MInst, BB);
Vikram S. Adve00521d72001-11-12 23:26:35 +0000708
Chris Lattnere90fcb72002-12-28 20:35:34 +0000709 MF.getInfo()->pushTempValue(MRI.getSpilledRegSize(RegType) );
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000710
Vikram S. Advef5af6362002-07-08 23:15:32 +0000711 vector<MachineInstr*> MIBef, MIAft;
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000712 vector<MachineInstr*> AdIMid;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000713
Vikram S. Advef5af6362002-07-08 23:15:32 +0000714 // Choose a register to hold the spilled value. This may insert code
715 // before and after MInst to free up the value. If so, this code should
716 // be first and last in the spill sequence before/after MInst.
717 int TmpRegU = getUsableUniRegAtMI(RegType, &LVSetBef, MInst, MIBef, MIAft);
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000718
Vikram S. Advef5af6362002-07-08 23:15:32 +0000719 // Set the operand first so that it this register does not get used
720 // as a scratch register for later calls to getUsableUniRegAtMI below
721 MInst->SetRegForOperand(OpNum, TmpRegU);
722
723 // get the added instructions for this instruction
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000724 AddedInstrns &AI = AddedInstrMap[MInst];
Vikram S. Advef5af6362002-07-08 23:15:32 +0000725
726 // We may need a scratch register to copy the spilled value to/from memory.
727 // This may itself have to insert code to free up a scratch register.
728 // Any such code should go before (after) the spill code for a load (store).
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000729 // The scratch reg is not marked as used because it is only used
730 // for the copy and not used across MInst.
Vikram S. Advef5af6362002-07-08 23:15:32 +0000731 int scratchRegType = -1;
732 int scratchReg = -1;
733 if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType))
734 {
Chris Lattner27a08932002-10-22 23:16:21 +0000735 scratchReg = getUsableUniRegAtMI(scratchRegType, &LVSetBef,
736 MInst, MIBef, MIAft);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000737 assert(scratchReg != MRI.getInvalidRegNum());
Vikram S. Advef5af6362002-07-08 23:15:32 +0000738 }
739
740 if (!isDef || isDefAndUse) {
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000741 // for a USE, we have to load the value of LR from stack to a TmpReg
742 // and use the TmpReg as one operand of instruction
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000743
Vikram S. Advef5af6362002-07-08 23:15:32 +0000744 // actual loading instruction(s)
745 MRI.cpMem2RegMI(AdIMid, MRI.getFramePointer(), SpillOff, TmpRegU, RegType,
746 scratchReg);
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000747
Vikram S. Advef5af6362002-07-08 23:15:32 +0000748 // the actual load should be after the instructions to free up TmpRegU
749 MIBef.insert(MIBef.end(), AdIMid.begin(), AdIMid.end());
750 AdIMid.clear();
751 }
752
753 if (isDef) { // if this is a Def
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000754 // for a DEF, we have to store the value produced by this instruction
755 // on the stack position allocated for this LR
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000756
Vikram S. Advef5af6362002-07-08 23:15:32 +0000757 // actual storing instruction(s)
758 MRI.cpReg2MemMI(AdIMid, TmpRegU, MRI.getFramePointer(), SpillOff, RegType,
759 scratchReg);
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000760
Vikram S. Advef5af6362002-07-08 23:15:32 +0000761 MIAft.insert(MIAft.begin(), AdIMid.begin(), AdIMid.end());
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000762 } // if !DEF
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000763
Vikram S. Advef5af6362002-07-08 23:15:32 +0000764 // Finally, insert the entire spill code sequences before/after MInst
765 AI.InstrnsBefore.insert(AI.InstrnsBefore.end(), MIBef.begin(), MIBef.end());
766 AI.InstrnsAfter.insert(AI.InstrnsAfter.begin(), MIAft.begin(), MIAft.end());
767
Chris Lattner7e708292002-06-25 16:13:24 +0000768 if (DEBUG_RA) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000769 cerr << "\nFor Inst:\n " << *MInst;
770 cerr << "SPILLED LR# " << LR->getUserIGNode()->getIndex();
771 cerr << "; added Instructions:";
Anand Shuklad58290e2002-07-09 19:18:56 +0000772 for_each(MIBef.begin(), MIBef.end(), std::mem_fun(&MachineInstr::dump));
773 for_each(MIAft.begin(), MIAft.end(), std::mem_fun(&MachineInstr::dump));
Chris Lattner7e708292002-06-25 16:13:24 +0000774 }
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000775}
776
777
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000778//----------------------------------------------------------------------------
779// We can use the following method to get a temporary register to be used
780// BEFORE any given machine instruction. If there is a register available,
781// this method will simply return that register and set MIBef = MIAft = NULL.
782// Otherwise, it will return a register and MIAft and MIBef will contain
783// two instructions used to free up this returned register.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000784// Returned register number is the UNIFIED register number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000785//----------------------------------------------------------------------------
786
Vikram S. Advef5af6362002-07-08 23:15:32 +0000787int PhyRegAlloc::getUsableUniRegAtMI(const int RegType,
788 const ValueSet *LVSetBef,
789 MachineInstr *MInst,
790 std::vector<MachineInstr*>& MIBef,
791 std::vector<MachineInstr*>& MIAft) {
792
Chris Lattner133f0792002-10-28 04:45:29 +0000793 RegClass* RC = getRegClassByID(MRI.getRegClassIDOfRegType(RegType));
Vikram S. Advef5af6362002-07-08 23:15:32 +0000794
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000795 int RegU = getUnusedUniRegAtMI(RC, MInst, LVSetBef);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000796
797 if (RegU == -1) {
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000798 // we couldn't find an unused register. Generate code to free up a reg by
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000799 // saving it on stack and restoring after the instruction
Vikram S. Advef5af6362002-07-08 23:15:32 +0000800
Chris Lattnere90fcb72002-12-28 20:35:34 +0000801 int TmpOff = MF.getInfo()->pushTempValue(MRI.getSpilledRegSize(RegType));
Vikram S. Adve12af1642001-11-08 04:48:50 +0000802
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000803 RegU = getUniRegNotUsedByThisInst(RC, MInst);
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000804
Vikram S. Advef5af6362002-07-08 23:15:32 +0000805 // Check if we need a scratch register to copy this register to memory.
806 int scratchRegType = -1;
807 if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType))
808 {
Chris Lattner133f0792002-10-28 04:45:29 +0000809 int scratchReg = getUsableUniRegAtMI(scratchRegType, LVSetBef,
810 MInst, MIBef, MIAft);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000811 assert(scratchReg != MRI.getInvalidRegNum());
812
813 // We may as well hold the value in the scratch register instead
814 // of copying it to memory and back. But we have to mark the
815 // register as used by this instruction, so it does not get used
816 // as a scratch reg. by another operand or anyone else.
Chris Lattner27a08932002-10-22 23:16:21 +0000817 MInst->insertUsedReg(scratchReg);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000818 MRI.cpReg2RegMI(MIBef, RegU, scratchReg, RegType);
819 MRI.cpReg2RegMI(MIAft, scratchReg, RegU, RegType);
820 }
821 else
822 { // the register can be copied directly to/from memory so do it.
823 MRI.cpReg2MemMI(MIBef, RegU, MRI.getFramePointer(), TmpOff, RegType);
824 MRI.cpMem2RegMI(MIAft, MRI.getFramePointer(), TmpOff, RegU, RegType);
825 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000826 }
Vikram S. Advef5af6362002-07-08 23:15:32 +0000827
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000828 return RegU;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000829}
830
831//----------------------------------------------------------------------------
832// This method is called to get a new unused register that can be used to
833// accomodate a spilled value.
834// This method may be called several times for a single machine instruction
835// if it contains many spilled operands. Each time it is called, it finds
836// a register which is not live at that instruction and also which is not
837// used by other spilled operands of the same instruction.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000838// Return register number is relative to the register class. NOT
839// unified number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000840//----------------------------------------------------------------------------
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000841
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000842int PhyRegAlloc::getUnusedUniRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000843 const MachineInstr *MInst,
Chris Lattner296b7732002-02-05 02:52:05 +0000844 const ValueSet *LVSetBef) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000845
846 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
847
Chris Lattner85c54652002-05-23 15:50:03 +0000848 std::vector<bool> &IsColorUsedArr = RC->getIsColorUsedArr();
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000849
Chris Lattner7e708292002-06-25 16:13:24 +0000850 for (unsigned i=0; i < NumAvailRegs; i++) // Reset array
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000851 IsColorUsedArr[i] = false;
852
Chris Lattner296b7732002-02-05 02:52:05 +0000853 ValueSet::const_iterator LIt = LVSetBef->begin();
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000854
855 // for each live var in live variable set after machine inst
Chris Lattner7e708292002-06-25 16:13:24 +0000856 for ( ; LIt != LVSetBef->end(); ++LIt) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000857
858 // get the live range corresponding to live var
859 LiveRange *const LRofLV = LRI.getLiveRangeForValue(*LIt );
860
861 // LR can be null if it is a const since a const
862 // doesn't have a dominating def - see Assumptions above
Chris Lattner7e708292002-06-25 16:13:24 +0000863 if (LRofLV && LRofLV->getRegClass() == RC && LRofLV->hasColor() )
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000864 IsColorUsedArr[ LRofLV->getColor() ] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000865 }
866
867 // It is possible that one operand of this MInst was already spilled
868 // and it received some register temporarily. If that's the case,
869 // it is recorded in machine operand. We must skip such registers.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000870 //
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000871 setRelRegsUsedByThisInst(RC, MInst);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000872
Chris Lattner7e708292002-06-25 16:13:24 +0000873 for (unsigned c=0; c < NumAvailRegs; c++) // find first unused color
Chris Lattner85c54652002-05-23 15:50:03 +0000874 if (!IsColorUsedArr[c])
875 return MRI.getUnifiedRegNum(RC->getID(), c);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000876
Chris Lattner85c54652002-05-23 15:50:03 +0000877 return -1;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000878}
879
880
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000881//----------------------------------------------------------------------------
882// Get any other register in a register class, other than what is used
883// by operands of a machine instruction. Returns the unified reg number.
884//----------------------------------------------------------------------------
885int PhyRegAlloc::getUniRegNotUsedByThisInst(RegClass *RC,
Chris Lattner85c54652002-05-23 15:50:03 +0000886 const MachineInstr *MInst) {
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000887
Chris Lattner85c54652002-05-23 15:50:03 +0000888 vector<bool> &IsColorUsedArr = RC->getIsColorUsedArr();
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000889 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
890
Chris Lattner7e708292002-06-25 16:13:24 +0000891 for (unsigned i=0; i < NumAvailRegs ; i++) // Reset array
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000892 IsColorUsedArr[i] = false;
893
894 setRelRegsUsedByThisInst(RC, MInst);
895
Chris Lattner7e708292002-06-25 16:13:24 +0000896 for (unsigned c=0; c < RC->getNumOfAvailRegs(); c++)// find first unused color
Chris Lattner85c54652002-05-23 15:50:03 +0000897 if (!IsColorUsedArr[c])
898 return MRI.getUnifiedRegNum(RC->getID(), c);
899
900 assert(0 && "FATAL: No free register could be found in reg class!!");
Chris Lattner697954c2002-01-20 22:54:45 +0000901 return 0;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000902}
903
904
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000905//----------------------------------------------------------------------------
906// This method modifies the IsColorUsedArr of the register class passed to it.
907// It sets the bits corresponding to the registers used by this machine
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000908// instructions. Both explicit and implicit operands are set.
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000909//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000910void PhyRegAlloc::setRelRegsUsedByThisInst(RegClass *RC,
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000911 const MachineInstr *MInst )
912{
913 assert(OperandsColoredMap[MInst] == true &&
914 "Illegal to call setRelRegsUsedByThisInst() until colored operands "
915 "are marked for an instruction.");
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000916
Vikram S. Advef5af6362002-07-08 23:15:32 +0000917 vector<bool> &IsColorUsedArr = RC->getIsColorUsedArr();
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000918
Vikram S. Advef5af6362002-07-08 23:15:32 +0000919 // Add the registers already marked as used by the instruction.
920 // This should include any scratch registers that are used to save
921 // values across the instruction (e.g., for saving state register values).
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000922 const std::set<int> &regsUsed = MInst->getRegsUsed();
923 for (std::set<int>::iterator I=regsUsed.begin(), E=regsUsed.end(); I != E; ++I)
924 {
925 int i = *I;
Vikram S. Advef5af6362002-07-08 23:15:32 +0000926 unsigned classId = 0;
Chris Lattner27a08932002-10-22 23:16:21 +0000927 int classRegNum = MRI.getClassRegNum(i, classId);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000928 if (RC->getID() == classId)
929 {
930 assert(classRegNum < (int) IsColorUsedArr.size() &&
931 "Illegal register number for this reg class?");
932 IsColorUsedArr[classRegNum] = true;
933 }
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000934 }
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000935
Vikram S. Advef5af6362002-07-08 23:15:32 +0000936 // If there are implicit references, mark their allocated regs as well
937 //
938 for (unsigned z=0; z < MInst->getNumImplicitRefs(); z++)
939 if (const LiveRange*
940 LRofImpRef = LRI.getLiveRangeForValue(MInst->getImplicitRef(z)))
941 if (LRofImpRef->hasColor())
942 // this implicit reference is in a LR that received a color
943 IsColorUsedArr[LRofImpRef->getColor()] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000944}
945
946
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000947//----------------------------------------------------------------------------
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000948// If there are delay slots for an instruction, the instructions
949// added after it must really go after the delayed instruction(s).
950// So, we move the InstrAfter of that instruction to the
951// corresponding delayed instruction using the following method.
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000952//----------------------------------------------------------------------------
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000953
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000954void PhyRegAlloc::move2DelayedInstr(const MachineInstr *OrigMI,
955 const MachineInstr *DelayedMI)
956{
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000957 // "added after" instructions of the original instr
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000958 std::vector<MachineInstr *> &OrigAft = AddedInstrMap[OrigMI].InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000959
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000960 // "added after" instructions of the delayed instr
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000961 std::vector<MachineInstr *> &DelayedAft =AddedInstrMap[DelayedMI].InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000962
963 // go thru all the "added after instructions" of the original instruction
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000964 // and append them to the "added after instructions" of the delayed
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000965 // instructions
Chris Lattner697954c2002-01-20 22:54:45 +0000966 DelayedAft.insert(DelayedAft.end(), OrigAft.begin(), OrigAft.end());
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000967
968 // empty the "added after instructions" of the original instruction
969 OrigAft.clear();
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000970}
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000971
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000972//----------------------------------------------------------------------------
973// This method prints the code with registers after register allocation is
974// complete.
975//----------------------------------------------------------------------------
976void PhyRegAlloc::printMachineCode()
977{
978
Chris Lattnerf726e772002-10-28 19:22:04 +0000979 cerr << "\n;************** Function " << Fn->getName()
Chris Lattner697954c2002-01-20 22:54:45 +0000980 << " *****************\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000981
Chris Lattnerf726e772002-10-28 19:22:04 +0000982 for (MachineFunction::iterator BBI = MF.begin(), BBE = MF.end();
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000983 BBI != BBE; ++BBI) {
Chris Lattnerf726e772002-10-28 19:22:04 +0000984 cerr << "\n"; printLabel(BBI->getBasicBlock()); cerr << ": ";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000985
986 // get the iterator for machine instructions
Chris Lattnerf726e772002-10-28 19:22:04 +0000987 MachineBasicBlock& MBB = *BBI;
988 MachineBasicBlock::iterator MII = MBB.begin();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000989
990 // iterate over all the machine instructions in BB
Chris Lattnerf726e772002-10-28 19:22:04 +0000991 for ( ; MII != MBB.end(); ++MII) {
Chris Lattnerd9512ca2002-10-29 17:35:39 +0000992 MachineInstr *MInst = *MII;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000993
Chris Lattner697954c2002-01-20 22:54:45 +0000994 cerr << "\n\t";
Chris Lattnerd9512ca2002-10-29 17:35:39 +0000995 cerr << TM.getInstrInfo().getName(MInst->getOpCode());
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000996
Chris Lattner7e708292002-06-25 16:13:24 +0000997 for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000998 MachineOperand& Op = MInst->getOperand(OpNum);
999
Chris Lattner133f0792002-10-28 04:45:29 +00001000 if (Op.getType() == MachineOperand::MO_VirtualRegister ||
1001 Op.getType() == MachineOperand::MO_CCRegister /*||
1002 Op.getType() == MachineOperand::MO_PCRelativeDisp*/ ) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001003
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001004 const Value *const Val = Op.getVRegValue () ;
Ruchira Sasankae727f852001-09-18 22:43:57 +00001005 // ****this code is temporary till NULL Values are fixed
Chris Lattner7e708292002-06-25 16:13:24 +00001006 if (! Val ) {
Chris Lattner697954c2002-01-20 22:54:45 +00001007 cerr << "\t<*NULL*>";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001008 continue;
1009 }
Ruchira Sasankae727f852001-09-18 22:43:57 +00001010
1011 // if a label or a constant
Chris Lattner7e708292002-06-25 16:13:24 +00001012 if (isa<BasicBlock>(Val)) {
Chris Lattner697954c2002-01-20 22:54:45 +00001013 cerr << "\t"; printLabel( Op.getVRegValue () );
1014 } else {
Ruchira Sasankae727f852001-09-18 22:43:57 +00001015 // else it must be a register value
1016 const int RegNum = Op.getAllocatedRegNum();
1017
Chris Lattner697954c2002-01-20 22:54:45 +00001018 cerr << "\t" << "%" << MRI.getUnifiedRegName( RegNum );
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001019 if (Val->hasName() )
Chris Lattner697954c2002-01-20 22:54:45 +00001020 cerr << "(" << Val->getName() << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001021 else
Chris Lattner697954c2002-01-20 22:54:45 +00001022 cerr << "(" << Val << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001023
Vikram S. Adve5f2180c2003-05-27 00:05:23 +00001024 if (Op.opIsDefOnly() || Op.opIsDefAndUse())
Chris Lattner697954c2002-01-20 22:54:45 +00001025 cerr << "*";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001026
1027 const LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
Chris Lattner7e708292002-06-25 16:13:24 +00001028 if (LROfVal )
1029 if (LROfVal->hasSpillOffset() )
Chris Lattner697954c2002-01-20 22:54:45 +00001030 cerr << "$";
Ruchira Sasankae727f852001-09-18 22:43:57 +00001031 }
1032
1033 }
Chris Lattner133f0792002-10-28 04:45:29 +00001034 else if (Op.getType() == MachineOperand::MO_MachineRegister) {
Chris Lattner697954c2002-01-20 22:54:45 +00001035 cerr << "\t" << "%" << MRI.getUnifiedRegName(Op.getMachineRegNum());
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001036 }
1037
1038 else
Chris Lattner697954c2002-01-20 22:54:45 +00001039 cerr << "\t" << Op; // use dump field
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001040 }
1041
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001042
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001043
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001044 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
Chris Lattner7e708292002-06-25 16:13:24 +00001045 if (NumOfImpRefs > 0) {
Chris Lattner697954c2002-01-20 22:54:45 +00001046 cerr << "\tImplicit:";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001047
Chris Lattner7e708292002-06-25 16:13:24 +00001048 for (unsigned z=0; z < NumOfImpRefs; z++)
Chris Lattner0665a5f2002-02-05 01:43:49 +00001049 cerr << RAV(MInst->getImplicitRef(z)) << "\t";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001050 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001051
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001052 } // for all machine instructions
1053
Chris Lattner697954c2002-01-20 22:54:45 +00001054 cerr << "\n";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001055
1056 } // for all BBs
1057
Chris Lattner697954c2002-01-20 22:54:45 +00001058 cerr << "\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001059}
1060
Ruchira Sasankae727f852001-09-18 22:43:57 +00001061
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001062//----------------------------------------------------------------------------
1063
1064//----------------------------------------------------------------------------
1065void PhyRegAlloc::colorIncomingArgs()
1066{
Chris Lattnerf726e772002-10-28 19:22:04 +00001067 MRI.colorMethodArgs(Fn, LRI, &AddedInstrAtEntry);
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001068}
1069
Ruchira Sasankae727f852001-09-18 22:43:57 +00001070
1071//----------------------------------------------------------------------------
1072// Used to generate a label for a basic block
1073//----------------------------------------------------------------------------
Chris Lattnerf726e772002-10-28 19:22:04 +00001074void PhyRegAlloc::printLabel(const Value *Val) {
Chris Lattner697954c2002-01-20 22:54:45 +00001075 if (Val->hasName())
1076 cerr << Val->getName();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001077 else
Chris Lattnerf726e772002-10-28 19:22:04 +00001078 cerr << "Label" << Val;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001079}
1080
1081
Ruchira Sasankae727f852001-09-18 22:43:57 +00001082//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001083// This method calls setSugColorUsable method of each live range. This
1084// will determine whether the suggested color of LR is really usable.
1085// A suggested color is not usable when the suggested color is volatile
1086// AND when there are call interferences
1087//----------------------------------------------------------------------------
1088
1089void PhyRegAlloc::markUnusableSugColors()
1090{
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001091 // hash map iterator
1092 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
1093 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
1094
Chris Lattner7e708292002-06-25 16:13:24 +00001095 for (; HMI != HMIEnd ; ++HMI ) {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001096 if (HMI->first) {
1097 LiveRange *L = HMI->second; // get the LiveRange
1098 if (L) {
Chris Lattner7e708292002-06-25 16:13:24 +00001099 if (L->hasSuggestedColor()) {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001100 int RCID = L->getRegClass()->getID();
Chris Lattner7e708292002-06-25 16:13:24 +00001101 if (MRI.isRegVolatile( RCID, L->getSuggestedColor()) &&
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001102 L->isCallInterference() )
1103 L->setSuggestedColorUsable( false );
1104 else
1105 L->setSuggestedColorUsable( true );
1106 }
1107 } // if L->hasSuggestedColor()
1108 }
1109 } // for all LR's in hash map
1110}
1111
1112
1113
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001114//----------------------------------------------------------------------------
1115// The following method will set the stack offsets of the live ranges that
1116// are decided to be spillled. This must be called just after coloring the
1117// LRs using the graph coloring algo. For each live range that is spilled,
1118// this method allocate a new spill position on the stack.
1119//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001120
Chris Lattner37730942002-02-05 03:52:29 +00001121void PhyRegAlloc::allocateStackSpace4SpilledLRs() {
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001122 if (DEBUG_RA) cerr << "\nSetting LR stack offsets for spills...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001123
Chris Lattner37730942002-02-05 03:52:29 +00001124 LiveRangeMapType::const_iterator HMI = LRI.getLiveRangeMap()->begin();
1125 LiveRangeMapType::const_iterator HMIEnd = LRI.getLiveRangeMap()->end();
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001126
Chris Lattner7e708292002-06-25 16:13:24 +00001127 for ( ; HMI != HMIEnd ; ++HMI) {
Chris Lattner37730942002-02-05 03:52:29 +00001128 if (HMI->first && HMI->second) {
1129 LiveRange *L = HMI->second; // get the LiveRange
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001130 if (!L->hasColor()) { // NOTE: ** allocating the size of long Type **
Chris Lattnere90fcb72002-12-28 20:35:34 +00001131 int stackOffset = MF.getInfo()->allocateSpilledValue(Type::LongTy);
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001132 L->setSpillOffFromFP(stackOffset);
1133 if (DEBUG_RA)
1134 cerr << " LR# " << L->getUserIGNode()->getIndex()
1135 << ": stack-offset = " << stackOffset << "\n";
1136 }
Chris Lattner37730942002-02-05 03:52:29 +00001137 }
1138 } // for all LR's in hash map
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001139}
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001140
1141
1142
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001143//----------------------------------------------------------------------------
Ruchira Sasankae727f852001-09-18 22:43:57 +00001144// The entry pont to Register Allocation
1145//----------------------------------------------------------------------------
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001146
1147void PhyRegAlloc::allocateRegisters()
1148{
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001149
1150 // make sure that we put all register classes into the RegClassList
1151 // before we call constructLiveRanges (now done in the constructor of
1152 // PhyRegAlloc class).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001153 //
1154 LRI.constructLiveRanges(); // create LR info
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001155
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001156 if (DEBUG_RA >= RA_DEBUG_LiveRanges)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001157 LRI.printLiveRanges();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001158
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001159 createIGNodeListsAndIGs(); // create IGNode list and IGs
1160
1161 buildInterferenceGraphs(); // build IGs in all reg classes
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001162
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001163
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001164 if (DEBUG_RA >= RA_DEBUG_LiveRanges) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001165 // print all LRs in all reg classes
Chris Lattner7e708292002-06-25 16:13:24 +00001166 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
1167 RegClassList[rc]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001168
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001169 // print IGs in all register classes
Chris Lattner7e708292002-06-25 16:13:24 +00001170 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
1171 RegClassList[rc]->printIG();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001172 }
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001173
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001174 LRI.coalesceLRs(); // coalesce all live ranges
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +00001175
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001176 if (DEBUG_RA >= RA_DEBUG_LiveRanges) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001177 // print all LRs in all reg classes
Chris Lattnerf726e772002-10-28 19:22:04 +00001178 for (unsigned rc=0; rc < NumOfRegClasses; rc++)
1179 RegClassList[rc]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001180
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001181 // print IGs in all register classes
Chris Lattnerf726e772002-10-28 19:22:04 +00001182 for (unsigned rc=0; rc < NumOfRegClasses; rc++)
1183 RegClassList[rc]->printIG();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001184 }
1185
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001186
1187 // mark un-usable suggested color before graph coloring algorithm.
1188 // When this is done, the graph coloring algo will not reserve
1189 // suggested color unnecessarily - they can be used by another LR
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001190 //
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001191 markUnusableSugColors();
1192
1193 // color all register classes using the graph coloring algo
Chris Lattner7e708292002-06-25 16:13:24 +00001194 for (unsigned rc=0; rc < NumOfRegClasses ; rc++)
Chris Lattnerf726e772002-10-28 19:22:04 +00001195 RegClassList[rc]->colorAllRegs();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001196
Chris Lattnere90fcb72002-12-28 20:35:34 +00001197 // Atter graph coloring, if some LRs did not receive a color (i.e, spilled)
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001198 // a poistion for such spilled LRs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001199 //
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001200 allocateStackSpace4SpilledLRs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001201
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001202 // Reset the temp. area on the stack before use by the first instruction.
1203 // This will also happen after updating each instruction.
1204 MF.getInfo()->popAllTempValues();
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001205
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001206 // color incoming args - if the correct color was not received
1207 // insert code to copy to the correct register
1208 //
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001209 colorIncomingArgs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001210
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001211 // Now update the machine code with register names and add any
1212 // additional code inserted by the register allocator to the instruction
1213 // stream
1214 //
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001215 updateMachineCode();
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001216
Chris Lattner045e7c82001-09-19 16:26:23 +00001217 if (DEBUG_RA) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001218 cerr << "\n**** Machine Code After Register Allocation:\n\n";
Chris Lattnerf726e772002-10-28 19:22:04 +00001219 MF.dump();
Chris Lattner045e7c82001-09-19 16:26:23 +00001220 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001221}
1222
Ruchira Sasankae727f852001-09-18 22:43:57 +00001223
1224