blob: 41b27a6618412914315836804558f8a291396d77 [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"
Vikram S. Adve814030a2003-07-29 19:49:21 +000017#include "llvm/CodeGen/InstrSelection.h"
Chris Lattner14ab1ce2002-02-04 17:48:00 +000018#include "llvm/Analysis/LoopInfo.h"
Vikram S. Adve12af1642001-11-08 04:48:50 +000019#include "llvm/Target/TargetMachine.h"
Chris Lattner8bd66e62002-12-28 21:00:25 +000020#include "llvm/Target/TargetFrameInfo.h"
Chris Lattner3501fea2003-01-14 22:00:31 +000021#include "llvm/Target/TargetInstrInfo.h"
Vikram S. Advebc001b22003-07-25 21:06:09 +000022#include "llvm/Target/TargetRegInfo.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000023#include "llvm/Function.h"
Chris Lattner37730942002-02-05 03:52:29 +000024#include "llvm/Type.h"
Vikram S. Advedabb41d2002-05-19 15:29:31 +000025#include "llvm/iOther.h"
Vikram S. Advef5af6362002-07-08 23:15:32 +000026#include "Support/STLExtras.h"
Chris Lattner4bc23482002-09-15 07:07:55 +000027#include "Support/CommandLine.h"
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000028#include <math.h>
Chris Lattner697954c2002-01-20 22:54:45 +000029using std::cerr;
Anand Shuklacfb22d32002-06-25 20:55:50 +000030using std::vector;
Vikram S. Adve12af1642001-11-08 04:48:50 +000031
Chris Lattner70e60cb2002-05-22 17:08:27 +000032RegAllocDebugLevel_t DEBUG_RA;
Vikram S. Adve39c94e12002-09-14 23:05:33 +000033
Chris Lattner5ff62e92002-07-22 02:10:13 +000034static cl::opt<RegAllocDebugLevel_t, true>
35DRA_opt("dregalloc", cl::Hidden, cl::location(DEBUG_RA),
36 cl::desc("enable register allocation debugging information"),
37 cl::values(
Vikram S. Adve39c94e12002-09-14 23:05:33 +000038 clEnumValN(RA_DEBUG_None , "n", "disable debug output"),
39 clEnumValN(RA_DEBUG_Results, "y", "debug output for allocation results"),
40 clEnumValN(RA_DEBUG_Coloring, "c", "debug output for graph coloring step"),
41 clEnumValN(RA_DEBUG_Interference,"ig","debug output for interference graphs"),
42 clEnumValN(RA_DEBUG_LiveRanges , "lr","debug output for live ranges"),
43 clEnumValN(RA_DEBUG_Verbose, "v", "extra debug output"),
Chris Lattner5ff62e92002-07-22 02:10:13 +000044 0));
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000045
Chris Lattner2f9b28e2002-02-04 15:54:09 +000046//----------------------------------------------------------------------------
47// RegisterAllocation pass front end...
48//----------------------------------------------------------------------------
49namespace {
Chris Lattnerf57b8452002-04-27 06:56:12 +000050 class RegisterAllocator : public FunctionPass {
Chris Lattner2f9b28e2002-02-04 15:54:09 +000051 TargetMachine &Target;
52 public:
53 inline RegisterAllocator(TargetMachine &T) : Target(T) {}
Chris Lattner96c466b2002-04-29 14:57:45 +000054
55 const char *getPassName() const { return "Register Allocation"; }
Chris Lattner6dd98a62002-02-04 00:33:08 +000056
Chris Lattner7e708292002-06-25 16:13:24 +000057 bool runOnFunction(Function &F) {
Chris Lattner2f9b28e2002-02-04 15:54:09 +000058 if (DEBUG_RA)
Chris Lattner7e708292002-06-25 16:13:24 +000059 cerr << "\n********* Function "<< F.getName() << " ***********\n";
Chris Lattner2f9b28e2002-02-04 15:54:09 +000060
Chris Lattner7e708292002-06-25 16:13:24 +000061 PhyRegAlloc PRA(&F, Target, &getAnalysis<FunctionLiveVarInfo>(),
Chris Lattner1b7f7dc2002-04-28 16:21:30 +000062 &getAnalysis<LoopInfo>());
Chris Lattner2f9b28e2002-02-04 15:54:09 +000063 PRA.allocateRegisters();
64
65 if (DEBUG_RA) cerr << "\nRegister allocation complete!\n";
66 return false;
67 }
Chris Lattner4911c352002-02-04 17:39:42 +000068
Chris Lattnerf57b8452002-04-27 06:56:12 +000069 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerdd5b4952002-08-08 19:01:28 +000070 AU.addRequired<LoopInfo>();
71 AU.addRequired<FunctionLiveVarInfo>();
Chris Lattner4911c352002-02-04 17:39:42 +000072 }
Chris Lattner2f9b28e2002-02-04 15:54:09 +000073 };
Chris Lattner6dd98a62002-02-04 00:33:08 +000074}
75
Chris Lattnerf57b8452002-04-27 06:56:12 +000076Pass *getRegisterAllocator(TargetMachine &T) {
Chris Lattner2f9b28e2002-02-04 15:54:09 +000077 return new RegisterAllocator(T);
78}
Chris Lattner6dd98a62002-02-04 00:33:08 +000079
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000080//----------------------------------------------------------------------------
81// Constructor: Init local composite objects and create register classes.
82//----------------------------------------------------------------------------
Chris Lattner1b7f7dc2002-04-28 16:21:30 +000083PhyRegAlloc::PhyRegAlloc(Function *F, const TargetMachine& tm,
84 FunctionLiveVarInfo *Lvi, LoopInfo *LDC)
Chris Lattnerf726e772002-10-28 19:22:04 +000085 : TM(tm), Fn(F), MF(MachineFunction::get(F)), LVI(Lvi),
86 LRI(F, tm, RegClassList), MRI(tm.getRegInfo()),
87 NumOfRegClasses(MRI.getNumOfRegClasses()), LoopDepthCalc(LDC) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +000088
89 // create each RegisterClass and put in RegClassList
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000090 //
Chris Lattnerf726e772002-10-28 19:22:04 +000091 for (unsigned rc=0; rc != NumOfRegClasses; rc++)
Vikram S. Advebc001b22003-07-25 21:06:09 +000092 RegClassList.push_back(new RegClass(F, &tm.getRegInfo(),
93 MRI.getMachineRegClass(rc)));
Ruchira Sasanka8e604792001-09-14 21:18:34 +000094}
95
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000096
97//----------------------------------------------------------------------------
98// Destructor: Deletes register classes
99//----------------------------------------------------------------------------
100PhyRegAlloc::~PhyRegAlloc() {
Chris Lattner7e708292002-06-25 16:13:24 +0000101 for ( unsigned rc=0; rc < NumOfRegClasses; rc++)
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000102 delete RegClassList[rc];
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000103
104 AddedInstrMap.clear();
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000105}
106
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000107//----------------------------------------------------------------------------
108// This method initally creates interference graphs (one in each reg class)
109// and IGNodeList (one in each IG). The actual nodes will be pushed later.
110//----------------------------------------------------------------------------
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000111void PhyRegAlloc::createIGNodeListsAndIGs() {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000112 if (DEBUG_RA >= RA_DEBUG_LiveRanges) cerr << "Creating LR lists ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000113
114 // hash map iterator
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000115 LiveRangeMapType::const_iterator HMI = LRI.getLiveRangeMap()->begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000116
117 // hash map end
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000118 LiveRangeMapType::const_iterator HMIEnd = LRI.getLiveRangeMap()->end();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000119
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000120 for (; HMI != HMIEnd ; ++HMI ) {
121 if (HMI->first) {
122 LiveRange *L = HMI->second; // get the LiveRange
123 if (!L) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000124 if (DEBUG_RA)
125 cerr << "\n**** ?!?WARNING: NULL LIVE RANGE FOUND FOR: "
126 << RAV(HMI->first) << "****\n";
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000127 continue;
128 }
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000129
130 // if the Value * is not null, and LR is not yet written to the IGNodeList
Chris Lattner7e708292002-06-25 16:13:24 +0000131 if (!(L->getUserIGNode()) ) {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000132 RegClass *const RC = // RegClass of first value in the LR
133 RegClassList[ L->getRegClass()->getID() ];
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000134 RC->addLRToIG(L); // add this LR to an IG
135 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000136 }
137 }
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000138
139 // init RegClassList
Chris Lattner7e708292002-06-25 16:13:24 +0000140 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000141 RegClassList[rc]->createInterferenceGraph();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000142
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000143 if (DEBUG_RA >= RA_DEBUG_LiveRanges) cerr << "LRLists Created!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000144}
145
146
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000147//----------------------------------------------------------------------------
148// This method will add all interferences at for a given instruction.
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000149// Interence occurs only if the LR of Def (Inst or Arg) is of the same reg
150// class as that of live var. The live var passed to this function is the
151// LVset AFTER the instruction
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000152//----------------------------------------------------------------------------
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000153
Chris Lattner296b7732002-02-05 02:52:05 +0000154void PhyRegAlloc::addInterference(const Value *Def,
155 const ValueSet *LVSet,
156 bool isCallInst) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000157
Chris Lattner296b7732002-02-05 02:52:05 +0000158 ValueSet::const_iterator LIt = LVSet->begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000159
160 // get the live range of instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000161 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000162 const LiveRange *const LROfDef = LRI.getLiveRangeForValue( Def );
163
164 IGNode *const IGNodeOfDef = LROfDef->getUserIGNode();
165 assert( IGNodeOfDef );
166
167 RegClass *const RCOfDef = LROfDef->getRegClass();
168
169 // for each live var in live variable set
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000170 //
Chris Lattner7e708292002-06-25 16:13:24 +0000171 for ( ; LIt != LVSet->end(); ++LIt) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000172
Vikram S. Advef5af6362002-07-08 23:15:32 +0000173 if (DEBUG_RA >= RA_DEBUG_Verbose)
Chris Lattner0665a5f2002-02-05 01:43:49 +0000174 cerr << "< Def=" << RAV(Def) << ", Lvar=" << RAV(*LIt) << "> ";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000175
176 // get the live range corresponding to live var
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000177 //
Chris Lattner0665a5f2002-02-05 01:43:49 +0000178 LiveRange *LROfVar = LRI.getLiveRangeForValue(*LIt);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000179
180 // LROfVar can be null if it is a const since a const
181 // doesn't have a dominating def - see Assumptions above
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000182 //
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000183 if (LROfVar)
184 if (LROfDef != LROfVar) // do not set interf for same LR
185 if (RCOfDef == LROfVar->getRegClass()) // 2 reg classes are the same
186 RCOfDef->setInterference( LROfDef, LROfVar);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000187 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000188}
189
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000190
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000191
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000192//----------------------------------------------------------------------------
193// For a call instruction, this method sets the CallInterference flag in
194// the LR of each variable live int the Live Variable Set live after the
195// call instruction (except the return value of the call instruction - since
196// the return value does not interfere with that call itself).
197//----------------------------------------------------------------------------
198
199void PhyRegAlloc::setCallInterferences(const MachineInstr *MInst,
Chris Lattner296b7732002-02-05 02:52:05 +0000200 const ValueSet *LVSetAft) {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000201
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000202 if (DEBUG_RA >= RA_DEBUG_Interference)
Chris Lattner697954c2002-01-20 22:54:45 +0000203 cerr << "\n For call inst: " << *MInst;
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000204
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000205 // for each live var in live variable set after machine inst
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000206 //
Vikram S. Adve65b2f402003-07-02 01:24:00 +0000207 for (ValueSet::const_iterator LIt = LVSetAft->begin(), LEnd = LVSetAft->end();
208 LIt != LEnd; ++LIt) {
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000209
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000210 // get the live range corresponding to live var
211 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000212 LiveRange *const LR = LRI.getLiveRangeForValue(*LIt );
213
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000214 // LR can be null if it is a const since a const
215 // doesn't have a dominating def - see Assumptions above
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000216 //
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000217 if (LR ) {
218 if (DEBUG_RA >= RA_DEBUG_Interference) {
219 cerr << "\n\tLR after Call: ";
220 printSet(*LR);
221 }
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000222 LR->setCallInterference();
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000223 if (DEBUG_RA >= RA_DEBUG_Interference) {
224 cerr << "\n ++After adding call interference for LR: " ;
Chris Lattner296b7732002-02-05 02:52:05 +0000225 printSet(*LR);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000226 }
227 }
228
229 }
230
Vikram S. Adve1a53f032002-03-31 18:54:37 +0000231 // Now find the LR of the return value of the call
232 // We do this because, we look at the LV set *after* the instruction
233 // to determine, which LRs must be saved across calls. The return value
234 // of the call is live in this set - but it does not interfere with call
235 // (i.e., we can allocate a volatile register to the return value)
236 //
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000237 CallArgsDescriptor* argDesc = CallArgsDescriptor::get(MInst);
238
239 if (const Value *RetVal = argDesc->getReturnValue()) {
Vikram S. Adve1a53f032002-03-31 18:54:37 +0000240 LiveRange *RetValLR = LRI.getLiveRangeForValue( RetVal );
241 assert( RetValLR && "No LR for RetValue of call");
242 RetValLR->clearCallInterference();
243 }
244
245 // If the CALL is an indirect call, find the LR of the function pointer.
246 // That has a call interference because it conflicts with outgoing args.
Chris Lattner7e708292002-06-25 16:13:24 +0000247 if (const Value *AddrVal = argDesc->getIndirectFuncPtr()) {
Vikram S. Adve1a53f032002-03-31 18:54:37 +0000248 LiveRange *AddrValLR = LRI.getLiveRangeForValue( AddrVal );
249 assert( AddrValLR && "No LR for indirect addr val of call");
250 AddrValLR->setCallInterference();
251 }
252
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000253}
254
255
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000256
257
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000258//----------------------------------------------------------------------------
259// This method will walk thru code and create interferences in the IG of
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000260// each RegClass. Also, this method calculates the spill cost of each
261// Live Range (it is done in this method to save another pass over the code).
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000262//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000263void PhyRegAlloc::buildInterferenceGraphs()
264{
265
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000266 if (DEBUG_RA >= RA_DEBUG_Interference)
267 cerr << "Creating interference graphs ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000268
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000269 unsigned BBLoopDepthCost;
Chris Lattnerf726e772002-10-28 19:22:04 +0000270 for (MachineFunction::iterator BBI = MF.begin(), BBE = MF.end();
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000271 BBI != BBE; ++BBI) {
Chris Lattnerf726e772002-10-28 19:22:04 +0000272 const MachineBasicBlock &MBB = *BBI;
273 const BasicBlock *BB = MBB.getBasicBlock();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000274
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000275 // find the 10^(loop_depth) of this BB
276 //
Chris Lattnerf726e772002-10-28 19:22:04 +0000277 BBLoopDepthCost = (unsigned)pow(10.0, LoopDepthCalc->getLoopDepth(BB));
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000278
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000279 // get the iterator for machine instructions
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000280 //
Chris Lattnerf726e772002-10-28 19:22:04 +0000281 MachineBasicBlock::const_iterator MII = MBB.begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000282
283 // iterate over all the machine instructions in BB
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000284 //
Chris Lattnerf726e772002-10-28 19:22:04 +0000285 for ( ; MII != MBB.end(); ++MII) {
286 const MachineInstr *MInst = *MII;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000287
288 // get the LV set after the instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000289 //
Chris Lattnerf726e772002-10-28 19:22:04 +0000290 const ValueSet &LVSetAI = LVI->getLiveVarSetAfterMInst(MInst, BB);
291 bool isCallInst = TM.getInstrInfo().isCall(MInst->getOpCode());
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000292
Chris Lattner7e708292002-06-25 16:13:24 +0000293 if (isCallInst ) {
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000294 // set the isCallInterference flag of each live range wich extends
295 // accross this call instruction. This information is used by graph
296 // coloring algo to avoid allocating volatile colors to live ranges
297 // that span across calls (since they have to be saved/restored)
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000298 //
Chris Lattner748697d2002-02-05 04:20:12 +0000299 setCallInterferences(MInst, &LVSetAI);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000300 }
301
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000302 // iterate over all MI operands to find defs
303 //
Chris Lattner2f898d22002-02-05 06:02:59 +0000304 for (MachineInstr::const_val_op_iterator OpI = MInst->begin(),
305 OpE = MInst->end(); OpI != OpE; ++OpI) {
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000306 if (OpI.isDefOnly() || OpI.isDefAndUse()) // create a new LR since def
Chris Lattner748697d2002-02-05 04:20:12 +0000307 addInterference(*OpI, &LVSetAI, isCallInst);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000308
309 // Calculate the spill cost of each live range
310 //
Chris Lattner2f898d22002-02-05 06:02:59 +0000311 LiveRange *LR = LRI.getLiveRangeForValue(*OpI);
312 if (LR) LR->addSpillCost(BBLoopDepthCost);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000313 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000314
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000315
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000316 // if there are multiple defs in this instruction e.g. in SETX
317 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000318 if (TM.getInstrInfo().isPseudoInstr(MInst->getOpCode()))
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000319 addInterf4PseudoInstr(MInst);
320
321
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000322 // Also add interference for any implicit definitions in a machine
323 // instr (currently, only calls have this).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000324 //
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000325 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000326 for (unsigned z=0; z < NumOfImpRefs; z++)
327 if (MInst->getImplicitOp(z).opIsDefOnly() ||
328 MInst->getImplicitOp(z).opIsDefAndUse())
329 addInterference( MInst->getImplicitRef(z), &LVSetAI, isCallInst );
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +0000330
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000331 } // for all machine instructions in BB
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000332 } // for all BBs in function
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000333
334
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000335 // add interferences for function arguments. Since there are no explict
336 // defs in the function for args, we have to add them manually
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000337 //
338 addInterferencesForArgs();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000339
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000340 if (DEBUG_RA >= RA_DEBUG_Interference)
341 cerr << "Interference graphs calculated!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000342}
343
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000344
345
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000346//--------------------------------------------------------------------------
347// Pseudo instructions will be exapnded to multiple instructions by the
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000348// assembler. Consequently, all the opernds must get distinct registers.
349// Therefore, we mark all operands of a pseudo instruction as they interfere
350// with one another.
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000351//--------------------------------------------------------------------------
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000352void PhyRegAlloc::addInterf4PseudoInstr(const MachineInstr *MInst) {
353
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000354 bool setInterf = false;
355
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000356 // iterate over MI operands to find defs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000357 //
Chris Lattner2f898d22002-02-05 06:02:59 +0000358 for (MachineInstr::const_val_op_iterator It1 = MInst->begin(),
359 ItE = MInst->end(); It1 != ItE; ++It1) {
360 const LiveRange *LROfOp1 = LRI.getLiveRangeForValue(*It1);
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000361 assert((LROfOp1 || !It1.isUseOnly())&& "No LR for Def in PSEUDO insruction");
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000362
Chris Lattner2f898d22002-02-05 06:02:59 +0000363 MachineInstr::const_val_op_iterator It2 = It1;
Chris Lattner7e708292002-06-25 16:13:24 +0000364 for (++It2; It2 != ItE; ++It2) {
Chris Lattner2f898d22002-02-05 06:02:59 +0000365 const LiveRange *LROfOp2 = LRI.getLiveRangeForValue(*It2);
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000366
Chris Lattner2f898d22002-02-05 06:02:59 +0000367 if (LROfOp2) {
368 RegClass *RCOfOp1 = LROfOp1->getRegClass();
369 RegClass *RCOfOp2 = LROfOp2->getRegClass();
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000370
Chris Lattner7e708292002-06-25 16:13:24 +0000371 if (RCOfOp1 == RCOfOp2 ){
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000372 RCOfOp1->setInterference( LROfOp1, LROfOp2 );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000373 setInterf = true;
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000374 }
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000375 } // if Op2 has a LR
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000376 } // for all other defs in machine instr
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000377 } // for all operands in an instruction
378
Chris Lattner2f898d22002-02-05 06:02:59 +0000379 if (!setInterf && MInst->getNumOperands() > 2) {
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000380 cerr << "\nInterf not set for any operand in pseudo instr:\n";
381 cerr << *MInst;
382 assert(0 && "Interf not set for pseudo instr with > 2 operands" );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000383 }
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000384}
385
386
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000387
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000388//----------------------------------------------------------------------------
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000389// This method will add interferences for incoming arguments to a function.
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000390//----------------------------------------------------------------------------
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000391
Chris Lattner296b7732002-02-05 02:52:05 +0000392void PhyRegAlloc::addInterferencesForArgs() {
393 // get the InSet of root BB
Chris Lattnerf726e772002-10-28 19:22:04 +0000394 const ValueSet &InSet = LVI->getInSetOfBB(&Fn->front());
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000395
Chris Lattnerf726e772002-10-28 19:22:04 +0000396 for (Function::const_aiterator AI = Fn->abegin(); AI != Fn->aend(); ++AI) {
Chris Lattner7e708292002-06-25 16:13:24 +0000397 // add interferences between args and LVars at start
398 addInterference(AI, &InSet, false);
399
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000400 if (DEBUG_RA >= RA_DEBUG_Interference)
Chris Lattner7e708292002-06-25 16:13:24 +0000401 cerr << " - %% adding interference for argument " << RAV(AI) << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000402 }
403}
404
405
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000406//----------------------------------------------------------------------------
407// This method is called after register allocation is complete to set the
408// allocated reisters in the machine code. This code will add register numbers
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000409// to MachineOperands that contain a Value. Also it calls target specific
410// methods to produce caller saving instructions. At the end, it adds all
411// additional instructions produced by the register allocator to the
412// instruction stream.
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000413//----------------------------------------------------------------------------
Vikram S. Adve48762092002-04-25 04:34:15 +0000414
415//-----------------------------
416// Utility functions used below
417//-----------------------------
418inline void
Vikram S. Advecb202e32002-10-11 16:12:40 +0000419InsertBefore(MachineInstr* newMI,
Chris Lattnerf726e772002-10-28 19:22:04 +0000420 MachineBasicBlock& MBB,
Chris Lattner32be9f62002-10-28 01:41:27 +0000421 MachineBasicBlock::iterator& MII)
Vikram S. Advecb202e32002-10-11 16:12:40 +0000422{
Chris Lattnerf726e772002-10-28 19:22:04 +0000423 MII = MBB.insert(MII, newMI);
Vikram S. Advecb202e32002-10-11 16:12:40 +0000424 ++MII;
425}
426
427inline void
428InsertAfter(MachineInstr* newMI,
Chris Lattnerf726e772002-10-28 19:22:04 +0000429 MachineBasicBlock& MBB,
Chris Lattner32be9f62002-10-28 01:41:27 +0000430 MachineBasicBlock::iterator& MII)
Vikram S. Advecb202e32002-10-11 16:12:40 +0000431{
432 ++MII; // insert before the next instruction
Chris Lattnerf726e772002-10-28 19:22:04 +0000433 MII = MBB.insert(MII, newMI);
Vikram S. Advecb202e32002-10-11 16:12:40 +0000434}
435
436inline void
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000437DeleteInstruction(MachineBasicBlock& MBB,
438 MachineBasicBlock::iterator& MII)
439{
440 MII = MBB.erase(MII);
441}
442
443inline void
Vikram S. Advecb202e32002-10-11 16:12:40 +0000444SubstituteInPlace(MachineInstr* newMI,
Chris Lattnerf726e772002-10-28 19:22:04 +0000445 MachineBasicBlock& MBB,
Chris Lattner32be9f62002-10-28 01:41:27 +0000446 MachineBasicBlock::iterator MII)
Vikram S. Advecb202e32002-10-11 16:12:40 +0000447{
448 *MII = newMI;
449}
450
451inline void
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000452PrependInstructions(vector<MachineInstr *> &IBef,
Chris Lattnerf726e772002-10-28 19:22:04 +0000453 MachineBasicBlock& MBB,
Chris Lattner32be9f62002-10-28 01:41:27 +0000454 MachineBasicBlock::iterator& MII,
Vikram S. Adve48762092002-04-25 04:34:15 +0000455 const std::string& msg)
456{
457 if (!IBef.empty())
458 {
459 MachineInstr* OrigMI = *MII;
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000460 std::vector<MachineInstr *>::iterator AdIt;
Vikram S. Adve48762092002-04-25 04:34:15 +0000461 for (AdIt = IBef.begin(); AdIt != IBef.end() ; ++AdIt)
462 {
463 if (DEBUG_RA) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000464 if (OrigMI) cerr << "For MInst:\n " << *OrigMI;
465 cerr << msg << "PREPENDed instr:\n " << **AdIt << "\n";
Vikram S. Adve48762092002-04-25 04:34:15 +0000466 }
Chris Lattnerf726e772002-10-28 19:22:04 +0000467 InsertBefore(*AdIt, MBB, MII);
Vikram S. Adve48762092002-04-25 04:34:15 +0000468 }
469 }
470}
471
472inline void
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000473AppendInstructions(std::vector<MachineInstr *> &IAft,
Chris Lattnerf726e772002-10-28 19:22:04 +0000474 MachineBasicBlock& MBB,
Chris Lattner32be9f62002-10-28 01:41:27 +0000475 MachineBasicBlock::iterator& MII,
Vikram S. Adve48762092002-04-25 04:34:15 +0000476 const std::string& msg)
477{
478 if (!IAft.empty())
479 {
480 MachineInstr* OrigMI = *MII;
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000481 std::vector<MachineInstr *>::iterator AdIt;
Chris Lattner7e708292002-06-25 16:13:24 +0000482 for ( AdIt = IAft.begin(); AdIt != IAft.end() ; ++AdIt )
Vikram S. Adve48762092002-04-25 04:34:15 +0000483 {
Chris Lattner7e708292002-06-25 16:13:24 +0000484 if (DEBUG_RA) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000485 if (OrigMI) cerr << "For MInst:\n " << *OrigMI;
486 cerr << msg << "APPENDed instr:\n " << **AdIt << "\n";
Vikram S. Adve48762092002-04-25 04:34:15 +0000487 }
Chris Lattnerf726e772002-10-28 19:22:04 +0000488 InsertAfter(*AdIt, MBB, MII);
Vikram S. Adve48762092002-04-25 04:34:15 +0000489 }
490 }
491}
492
Vikram S. Adve814030a2003-07-29 19:49:21 +0000493static bool MarkAllocatedRegs(MachineInstr* MInst,
494 LiveRangeInfo& LRI,
495 const TargetRegInfo& MRI)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000496{
Vikram S. Adve814030a2003-07-29 19:49:21 +0000497 bool instrNeedsSpills = false;
498
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000499 // 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();
Vikram S. Adve814030a2003-07-29 19:49:21 +0000510 if (const LiveRange* LR = LRI.getLiveRangeForValue(Val)) {
511 // Remember if any operand needs spilling
512 instrNeedsSpills |= LR->isMarkedForSpill();
513
514 // An operand may have a color whether or not it needs spilling
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000515 if (LR->hasColor())
516 MInst->SetRegForOperand(OpNum,
517 MRI.getUnifiedRegNum(LR->getRegClass()->getID(),
518 LR->getColor()));
Vikram S. Adve814030a2003-07-29 19:49:21 +0000519 }
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000520 }
521 } // for each operand
Vikram S. Adve814030a2003-07-29 19:49:21 +0000522
523 return instrNeedsSpills;
524}
525
526void PhyRegAlloc::updateInstruction(MachineBasicBlock::iterator& MII,
527 MachineBasicBlock &MBB)
528{
529 MachineInstr* MInst = *MII;
530 unsigned Opcode = MInst->getOpCode();
531
532 // Reset tmp stack positions so they can be reused for each machine instr.
533 MF.getInfo()->popAllTempValues();
534
535 // Mark the operands for which regs have been allocated.
536 bool instrNeedsSpills = MarkAllocatedRegs(*MII, LRI, MRI);
537
538#ifndef NDEBUG
539 // Mark that the operands have been updated. Later,
540 // setRelRegsUsedByThisInst() is called to find registers used by each
541 // MachineInst, and it should not be used for an instruction until
542 // this is done. This flag just serves as a sanity check.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000543 OperandsColoredMap[MInst] = true;
Vikram S. Adve814030a2003-07-29 19:49:21 +0000544#endif
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000545
Vikram S. Advebc001b22003-07-25 21:06:09 +0000546 // Now insert caller-saving code before/after the call.
547 // Do this before inserting spill code since some registers must be
548 // used by save/restore and spill code should not use those registers.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000549 //
Vikram S. Advebc001b22003-07-25 21:06:09 +0000550 if (TM.getInstrInfo().isCall(Opcode)) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000551 AddedInstrns &AI = AddedInstrMap[MInst];
Vikram S. Adve814030a2003-07-29 19:49:21 +0000552 insertCallerSavingCode(AI.InstrnsBefore, AI.InstrnsAfter, MInst,
553 MBB.getBasicBlock());
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000554 }
Vikram S. Advebc001b22003-07-25 21:06:09 +0000555
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000556 // Now insert spill code for remaining operands not allocated to
557 // registers. This must be done even for call return instructions
558 // since those are not handled by the special code above.
Vikram S. Adve814030a2003-07-29 19:49:21 +0000559 if (instrNeedsSpills)
560 for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum)
561 {
562 MachineOperand& Op = MInst->getOperand(OpNum);
563 if (Op.getType() == MachineOperand::MO_VirtualRegister ||
564 Op.getType() == MachineOperand::MO_CCRegister)
565 {
566 const Value* Val = Op.getVRegValue();
567 if (const LiveRange *LR = LRI.getLiveRangeForValue(Val))
568 if (LR->isMarkedForSpill())
569 insertCode4SpilledLR(LR, MII, MBB, OpNum);
570 }
571 } // for each operand
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000572}
573
574void PhyRegAlloc::updateMachineCode()
575{
Chris Lattner7e708292002-06-25 16:13:24 +0000576 // Insert any instructions needed at method entry
Chris Lattnerf726e772002-10-28 19:22:04 +0000577 MachineBasicBlock::iterator MII = MF.front().begin();
578 PrependInstructions(AddedInstrAtEntry.InstrnsBefore, MF.front(), MII,
Chris Lattner7e708292002-06-25 16:13:24 +0000579 "At function entry: \n");
580 assert(AddedInstrAtEntry.InstrnsAfter.empty() &&
581 "InstrsAfter should be unnecessary since we are just inserting at "
582 "the function entry point here.");
Vikram S. Adve48762092002-04-25 04:34:15 +0000583
Chris Lattnerf726e772002-10-28 19:22:04 +0000584 for (MachineFunction::iterator BBI = MF.begin(), BBE = MF.end();
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000585 BBI != BBE; ++BBI) {
Vikram S. Advecb202e32002-10-11 16:12:40 +0000586
Chris Lattnerf726e772002-10-28 19:22:04 +0000587 MachineBasicBlock &MBB = *BBI;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000588
589 // Iterate over all machine instructions in BB and mark operands with
590 // their assigned registers or insert spill code, as appropriate.
591 // Also, fix operands of call/return instructions.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000592 for (MachineBasicBlock::iterator MII = MBB.begin(); MII != MBB.end(); ++MII)
Vikram S. Adve814030a2003-07-29 19:49:21 +0000593 if (! TM.getInstrInfo().isDummyPhiInstr((*MII)->getOpCode()))
594 updateInstruction(MII, MBB);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000595
596 // Now, move code out of delay slots of branches and returns if needed.
597 // (Also, move "after" code from calls to the last delay slot instruction.)
598 // Moving code out of delay slots is needed in 2 situations:
599 // (1) If this is a branch and it needs instructions inserted after it,
600 // move any existing instructions out of the delay slot so that the
601 // instructions can go into the delay slot. This only supports the
602 // case that #instrsAfter <= #delay slots.
603 //
604 // (2) If any instruction in the delay slot needs
605 // instructions inserted, move it out of the delay slot and before the
606 // branch because putting code before or after it would be VERY BAD!
607 //
608 // If the annul bit of the branch is set, neither of these is legal!
609 // If so, we need to handle spill differently but annulling is not yet used.
610 //
611 for (MachineBasicBlock::iterator MII = MBB.begin();
612 MII != MBB.end(); ++MII)
613 if (unsigned delaySlots =
614 TM.getInstrInfo().getNumDelaySlots((*MII)->getOpCode()))
615 {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000616 MachineInstr *MInst = *MII, *DelaySlotMI = *(MII+1);
617
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000618 // Check the 2 conditions above:
619 // (1) Does a branch need instructions added after it?
620 // (2) O/w does delay slot instr. need instrns before or after?
Vikram S. Adve814030a2003-07-29 19:49:21 +0000621 bool isBranch = (TM.getInstrInfo().isBranch(MInst->getOpCode()) ||
622 TM.getInstrInfo().isReturn(MInst->getOpCode()));
623 bool cond1 = (isBranch &&
624 AddedInstrMap.count(MInst) &&
625 AddedInstrMap[MInst].InstrnsAfter.size() > 0);
626 bool cond2 = (AddedInstrMap.count(DelaySlotMI) &&
627 (AddedInstrMap[DelaySlotMI].InstrnsBefore.size() > 0 ||
628 AddedInstrMap[DelaySlotMI].InstrnsAfter.size() > 0));
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000629
630 if (cond1 || cond2)
631 {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000632 assert((MInst->getOpCodeFlags() & AnnulFlag) == 0 &&
633 "FIXME: Moving an annulled delay slot instruction!");
634 assert(delaySlots==1 &&
635 "InsertBefore does not yet handle >1 delay slots!");
636 InsertBefore(DelaySlotMI, MBB, MII); // MII pts back to branch
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000637
638 // In case (1), delete it and don't replace with anything!
639 // Otherwise (i.e., case (2) only) replace it with a NOP.
640 if (cond1) {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000641 DeleteInstruction(MBB, ++MII); // MII now points to next inst.
642 --MII; // reset MII for ++MII of loop
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000643 }
Vikram S. Adve814030a2003-07-29 19:49:21 +0000644 else
645 SubstituteInPlace(BuildMI(TM.getInstrInfo().getNOPOpCode(),1),
646 MBB, MII+1); // replace with NOP
647
648 if (DEBUG_RA) {
649 cerr << "\nRegAlloc: Moved instr. with added code: "
650 << *DelaySlotMI
651 << " out of delay slots of instr: " << *MInst;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000652 }
653 }
Vikram S. Adve814030a2003-07-29 19:49:21 +0000654 else
655 // For non-branch instr with delay slots (probably a call), move
656 // InstrAfter to the instr. in the last delay slot.
657 move2DelayedInstr(*MII, *(MII+delaySlots));
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000658 }
659
660 // Finally iterate over all instructions in BB and insert before/after
661 //
Vikram S. Advebc001b22003-07-25 21:06:09 +0000662 for (MachineBasicBlock::iterator MII=MBB.begin(); MII != MBB.end(); ++MII) {
Vikram S. Adve48762092002-04-25 04:34:15 +0000663 MachineInstr *MInst = *MII;
Vikram S. Advebc001b22003-07-25 21:06:09 +0000664
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000665 // do not process Phis
Vikram S. Advebc001b22003-07-25 21:06:09 +0000666 if (TM.getInstrInfo().isDummyPhiInstr(MInst->getOpCode()))
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000667 continue;
668
Vikram S. Advebc001b22003-07-25 21:06:09 +0000669 // if there are any added instructions...
Chris Lattner7e708292002-06-25 16:13:24 +0000670 if (AddedInstrMap.count(MInst)) {
Vikram S. Advebc001b22003-07-25 21:06:09 +0000671 AddedInstrns &CallAI = AddedInstrMap[MInst];
672
673#ifndef NDEBUG
Vikram S. Adve814030a2003-07-29 19:49:21 +0000674 bool isBranch = (TM.getInstrInfo().isBranch(MInst->getOpCode()) ||
675 TM.getInstrInfo().isReturn(MInst->getOpCode()));
676 assert((!isBranch ||
677 AddedInstrMap[MInst].InstrnsAfter.size() <=
678 TM.getInstrInfo().getNumDelaySlots(MInst->getOpCode())) &&
679 "Cannot put more than #delaySlots instrns after "
680 "branch or return! Need to handle temps differently.");
681#endif
682
683#ifndef NDEBUG
Vikram S. Advebc001b22003-07-25 21:06:09 +0000684 // Temporary sanity checking code to detect whether the same machine
685 // instruction is ever inserted twice before/after a call.
686 // I suspect this is happening but am not sure. --Vikram, 7/1/03.
687 //
688 std::set<const MachineInstr*> instrsSeen;
689 for (int i = 0, N = CallAI.InstrnsBefore.size(); i < N; ++i) {
690 assert(instrsSeen.count(CallAI.InstrnsBefore[i]) == 0 &&
691 "Duplicate machine instruction in InstrnsBefore!");
692 instrsSeen.insert(CallAI.InstrnsBefore[i]);
693 }
694 for (int i = 0, N = CallAI.InstrnsAfter.size(); i < N; ++i) {
695 assert(instrsSeen.count(CallAI.InstrnsAfter[i]) == 0 &&
696 "Duplicate machine instruction in InstrnsBefore/After!");
697 instrsSeen.insert(CallAI.InstrnsAfter[i]);
698 }
699#endif
700
701 // Now add the instructions before/after this MI.
702 // We do this here to ensure that spill for an instruction is inserted
703 // as close as possible to an instruction (see above insertCode4Spill)
704 //
705 if (! CallAI.InstrnsBefore.empty())
706 PrependInstructions(CallAI.InstrnsBefore, MBB, MII,"");
707
708 if (! CallAI.InstrnsAfter.empty())
709 AppendInstructions(CallAI.InstrnsAfter, MBB, MII,"");
710
711 } // if there are any added instructions
Vikram S. Advecb202e32002-10-11 16:12:40 +0000712
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000713 } // for each machine instruction
Vikram S. Adve814030a2003-07-29 19:49:21 +0000714
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000715 }
716}
717
718
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000719
720//----------------------------------------------------------------------------
721// This method inserts spill code for AN operand whose LR was spilled.
722// This method may be called several times for a single machine instruction
723// if it contains many spilled operands. Each time it is called, it finds
724// a register which is not live at that instruction and also which is not
725// used by other spilled operands of the same instruction. Then it uses
726// this register temporarily to accomodate the spilled value.
727//----------------------------------------------------------------------------
Vikram S. Advebc001b22003-07-25 21:06:09 +0000728
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000729void PhyRegAlloc::insertCode4SpilledLR(const LiveRange *LR,
Vikram S. Adve814030a2003-07-29 19:49:21 +0000730 MachineBasicBlock::iterator& MII,
731 MachineBasicBlock &MBB,
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000732 const unsigned OpNum) {
733
Vikram S. Adve814030a2003-07-29 19:49:21 +0000734 MachineInstr *MInst = *MII;
735 const BasicBlock *BB = MBB.getBasicBlock();
736
Vikram S. Advead9c9782002-09-28 17:02:40 +0000737 assert((! TM.getInstrInfo().isCall(MInst->getOpCode()) || OpNum == 0) &&
738 "Outgoing arg of a call must be handled elsewhere (func arg ok)");
739 assert(! TM.getInstrInfo().isReturn(MInst->getOpCode()) &&
740 "Return value of a ret must be handled elsewhere");
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000741
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000742 MachineOperand& Op = MInst->getOperand(OpNum);
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000743 bool isDef = Op.opIsDefOnly();
744 bool isDefAndUse = Op.opIsDefAndUse();
Vikram S. Advebc001b22003-07-25 21:06:09 +0000745 unsigned RegType = MRI.getRegTypeForLR(LR);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000746 int SpillOff = LR->getSpillOffFromFP();
747 RegClass *RC = LR->getRegClass();
Vikram S. Adve814030a2003-07-29 19:49:21 +0000748
749 // Get the live-variable set to find registers free before this instr.
750 // If this instr. is in the delay slot of a branch or return, use the live
751 // var set before that branch or return -- we don't want to trample those!
752 //
753 MachineInstr *LiveBeforeThisMI = MInst;
754 if (MII != MBB.begin()) {
755 MachineInstr *PredMI = *(MII-1);
756 if (unsigned DS = TM.getInstrInfo().getNumDelaySlots(PredMI->getOpCode())) {
757 assert(DS == 1 && "Only checking immediate pred. for delay slots!");
758 LiveBeforeThisMI = PredMI;
759 }
760 }
761 const ValueSet &LVSetBef = LVI->getLiveVarSetBeforeMInst(LiveBeforeThisMI,BB);
Vikram S. Adve00521d72001-11-12 23:26:35 +0000762
Chris Lattnere90fcb72002-12-28 20:35:34 +0000763 MF.getInfo()->pushTempValue(MRI.getSpilledRegSize(RegType) );
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000764
Vikram S. Advef5af6362002-07-08 23:15:32 +0000765 vector<MachineInstr*> MIBef, MIAft;
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000766 vector<MachineInstr*> AdIMid;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000767
Vikram S. Adve3bf08922003-07-10 19:42:55 +0000768 // Choose a register to hold the spilled value, if one was not preallocated.
769 // This may insert code before and after MInst to free up the value. If so,
770 // this code should be first/last in the spill sequence before/after MInst.
771 int TmpRegU=(LR->hasColor()
772 ? MRI.getUnifiedRegNum(LR->getRegClass()->getID(),LR->getColor())
773 : getUsableUniRegAtMI(RegType, &LVSetBef, MInst, MIBef,MIAft));
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000774
Vikram S. Advef5af6362002-07-08 23:15:32 +0000775 // Set the operand first so that it this register does not get used
776 // as a scratch register for later calls to getUsableUniRegAtMI below
777 MInst->SetRegForOperand(OpNum, TmpRegU);
778
779 // get the added instructions for this instruction
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000780 AddedInstrns &AI = AddedInstrMap[MInst];
Vikram S. Advef5af6362002-07-08 23:15:32 +0000781
782 // We may need a scratch register to copy the spilled value to/from memory.
783 // This may itself have to insert code to free up a scratch register.
784 // Any such code should go before (after) the spill code for a load (store).
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000785 // The scratch reg is not marked as used because it is only used
786 // for the copy and not used across MInst.
Vikram S. Advef5af6362002-07-08 23:15:32 +0000787 int scratchRegType = -1;
788 int scratchReg = -1;
789 if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType))
790 {
Chris Lattner27a08932002-10-22 23:16:21 +0000791 scratchReg = getUsableUniRegAtMI(scratchRegType, &LVSetBef,
792 MInst, MIBef, MIAft);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000793 assert(scratchReg != MRI.getInvalidRegNum());
Vikram S. Advef5af6362002-07-08 23:15:32 +0000794 }
795
796 if (!isDef || isDefAndUse) {
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000797 // for a USE, we have to load the value of LR from stack to a TmpReg
798 // and use the TmpReg as one operand of instruction
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000799
Vikram S. Advef5af6362002-07-08 23:15:32 +0000800 // actual loading instruction(s)
Vikram S. Adve814030a2003-07-29 19:49:21 +0000801 MRI.cpMem2RegMI(AdIMid, MRI.getFramePointer(), SpillOff, TmpRegU,
802 RegType, scratchReg);
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000803
Vikram S. Advef5af6362002-07-08 23:15:32 +0000804 // the actual load should be after the instructions to free up TmpRegU
805 MIBef.insert(MIBef.end(), AdIMid.begin(), AdIMid.end());
806 AdIMid.clear();
807 }
808
Vikram S. Adve3bf08922003-07-10 19:42:55 +0000809 if (isDef || isDefAndUse) { // if this is a Def
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000810 // for a DEF, we have to store the value produced by this instruction
811 // on the stack position allocated for this LR
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000812
Vikram S. Advef5af6362002-07-08 23:15:32 +0000813 // actual storing instruction(s)
Vikram S. Adve814030a2003-07-29 19:49:21 +0000814 MRI.cpReg2MemMI(AdIMid, TmpRegU, MRI.getFramePointer(), SpillOff,
815 RegType, scratchReg);
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000816
Vikram S. Advef5af6362002-07-08 23:15:32 +0000817 MIAft.insert(MIAft.begin(), AdIMid.begin(), AdIMid.end());
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000818 } // if !DEF
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000819
Vikram S. Advef5af6362002-07-08 23:15:32 +0000820 // Finally, insert the entire spill code sequences before/after MInst
821 AI.InstrnsBefore.insert(AI.InstrnsBefore.end(), MIBef.begin(), MIBef.end());
822 AI.InstrnsAfter.insert(AI.InstrnsAfter.begin(), MIAft.begin(), MIAft.end());
823
Chris Lattner7e708292002-06-25 16:13:24 +0000824 if (DEBUG_RA) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000825 cerr << "\nFor Inst:\n " << *MInst;
826 cerr << "SPILLED LR# " << LR->getUserIGNode()->getIndex();
827 cerr << "; added Instructions:";
Anand Shuklad58290e2002-07-09 19:18:56 +0000828 for_each(MIBef.begin(), MIBef.end(), std::mem_fun(&MachineInstr::dump));
829 for_each(MIAft.begin(), MIAft.end(), std::mem_fun(&MachineInstr::dump));
Chris Lattner7e708292002-06-25 16:13:24 +0000830 }
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000831}
832
833
Vikram S. Adve814030a2003-07-29 19:49:21 +0000834
835//----------------------------------------------------------------------------
836// This method inserts caller saving/restoring instructons before/after
837// a call machine instruction. The caller saving/restoring instructions are
838// inserted like:
839// ** caller saving instructions
840// other instructions inserted for the call by ColorCallArg
841// CALL instruction
842// other instructions inserted for the call ColorCallArg
843// ** caller restoring instructions
844//----------------------------------------------------------------------------
845
846void
847PhyRegAlloc::insertCallerSavingCode(std::vector<MachineInstr*> &instrnsBefore,
848 std::vector<MachineInstr*> &instrnsAfter,
849 MachineInstr *CallMI,
850 const BasicBlock *BB)
851{
852 assert(TM.getInstrInfo().isCall(CallMI->getOpCode()));
853
854 // has set to record which registers were saved/restored
855 //
856 hash_set<unsigned> PushedRegSet;
857
858 CallArgsDescriptor* argDesc = CallArgsDescriptor::get(CallMI);
859
860 // if the call is to a instrumentation function, do not insert save and
861 // restore instructions the instrumentation function takes care of save
862 // restore for volatile regs.
863 //
864 // FIXME: this should be made general, not specific to the reoptimizer!
865 //
866 const Function *Callee = argDesc->getCallInst()->getCalledFunction();
867 bool isLLVMFirstTrigger = Callee && Callee->getName() == "llvm_first_trigger";
868
869 // Now check if the call has a return value (using argDesc) and if so,
870 // find the LR of the TmpInstruction representing the return value register.
871 // (using the last or second-last *implicit operand* of the call MI).
872 // Insert it to to the PushedRegSet since we must not save that register
873 // and restore it after the call.
874 // We do this because, we look at the LV set *after* the instruction
875 // to determine, which LRs must be saved across calls. The return value
876 // of the call is live in this set - but we must not save/restore it.
877 //
878 if (const Value *origRetVal = argDesc->getReturnValue()) {
879 unsigned retValRefNum = (CallMI->getNumImplicitRefs() -
880 (argDesc->getIndirectFuncPtr()? 1 : 2));
881 const TmpInstruction* tmpRetVal =
882 cast<TmpInstruction>(CallMI->getImplicitRef(retValRefNum));
883 assert(tmpRetVal->getOperand(0) == origRetVal &&
884 tmpRetVal->getType() == origRetVal->getType() &&
885 "Wrong implicit ref?");
886 LiveRange *RetValLR = LRI.getLiveRangeForValue(tmpRetVal);
887 assert(RetValLR && "No LR for RetValue of call");
888
889 if (! RetValLR->isMarkedForSpill())
890 PushedRegSet.insert(MRI.getUnifiedRegNum(RetValLR->getRegClassID(),
891 RetValLR->getColor()));
892 }
893
894 const ValueSet &LVSetAft = LVI->getLiveVarSetAfterMInst(CallMI, BB);
895 ValueSet::const_iterator LIt = LVSetAft.begin();
896
897 // for each live var in live variable set after machine inst
898 for( ; LIt != LVSetAft.end(); ++LIt) {
899
900 // get the live range corresponding to live var
901 LiveRange *const LR = LRI.getLiveRangeForValue(*LIt);
902
903 // LR can be null if it is a const since a const
904 // doesn't have a dominating def - see Assumptions above
905 if( LR ) {
906
907 if(! LR->isMarkedForSpill()) {
908
909 assert(LR->hasColor() && "LR is neither spilled nor colored?");
910 unsigned RCID = LR->getRegClassID();
911 unsigned Color = LR->getColor();
912
913 if (MRI.isRegVolatile(RCID, Color) ) {
914
915 //if the function is special LLVM function,
916 //And the register is not modified by call, don't save and restore
917 if (isLLVMFirstTrigger && !MRI.modifiedByCall(RCID, Color))
918 continue;
919
920 // if the value is in both LV sets (i.e., live before and after
921 // the call machine instruction)
922
923 unsigned Reg = MRI.getUnifiedRegNum(RCID, Color);
924
925 if( PushedRegSet.find(Reg) == PushedRegSet.end() ) {
926
927 // if we haven't already pushed that register
928
929 unsigned RegType = MRI.getRegTypeForLR(LR);
930
931 // Now get two instructions - to push on stack and pop from stack
932 // and add them to InstrnsBefore and InstrnsAfter of the
933 // call instruction
934 //
935 int StackOff =
936 MF.getInfo()->pushTempValue(MRI.getSpilledRegSize(RegType));
937
938 //---- Insert code for pushing the reg on stack ----------
939
940 std::vector<MachineInstr*> AdIBef, AdIAft;
941
942 // We may need a scratch register to copy the saved value
943 // to/from memory. This may itself have to insert code to
944 // free up a scratch register. Any such code should go before
945 // the save code. The scratch register, if any, is by default
946 // temporary and not "used" by the instruction unless the
947 // copy code itself decides to keep the value in the scratch reg.
948 int scratchRegType = -1;
949 int scratchReg = -1;
950 if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType))
951 { // Find a register not live in the LVSet before CallMI
952 const ValueSet &LVSetBef =
953 LVI->getLiveVarSetBeforeMInst(CallMI, BB);
954 scratchReg = getUsableUniRegAtMI(scratchRegType, &LVSetBef,
955 CallMI, AdIBef, AdIAft);
956 assert(scratchReg != MRI.getInvalidRegNum());
957 }
958
959 if (AdIBef.size() > 0)
960 instrnsBefore.insert(instrnsBefore.end(),
961 AdIBef.begin(), AdIBef.end());
962
963 MRI.cpReg2MemMI(instrnsBefore, Reg, MRI.getFramePointer(),
964 StackOff, RegType, scratchReg);
965
966 if (AdIAft.size() > 0)
967 instrnsBefore.insert(instrnsBefore.end(),
968 AdIAft.begin(), AdIAft.end());
969
970 //---- Insert code for popping the reg from the stack ----------
971
972 AdIBef.clear();
973 AdIAft.clear();
974
975 // We may need a scratch register to copy the saved value
976 // from memory. This may itself have to insert code to
977 // free up a scratch register. Any such code should go
978 // after the save code. As above, scratch is not marked "used".
979 //
980 scratchRegType = -1;
981 scratchReg = -1;
982 if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType))
983 { // Find a register not live in the LVSet after CallMI
984 scratchReg = getUsableUniRegAtMI(scratchRegType, &LVSetAft,
985 CallMI, AdIBef, AdIAft);
986 assert(scratchReg != MRI.getInvalidRegNum());
987 }
988
989 if (AdIBef.size() > 0)
990 instrnsAfter.insert(instrnsAfter.end(),
991 AdIBef.begin(), AdIBef.end());
992
993 MRI.cpMem2RegMI(instrnsAfter, MRI.getFramePointer(), StackOff,
994 Reg, RegType, scratchReg);
995
996 if (AdIAft.size() > 0)
997 instrnsAfter.insert(instrnsAfter.end(),
998 AdIAft.begin(), AdIAft.end());
999
1000 PushedRegSet.insert(Reg);
1001
1002 if(DEBUG_RA) {
1003 std::cerr << "\nFor call inst:" << *CallMI;
1004 std::cerr << " -inserted caller saving instrs: Before:\n\t ";
1005 for_each(instrnsBefore.begin(), instrnsBefore.end(),
1006 std::mem_fun(&MachineInstr::dump));
1007 std::cerr << " -and After:\n\t ";
1008 for_each(instrnsAfter.begin(), instrnsAfter.end(),
1009 std::mem_fun(&MachineInstr::dump));
1010 }
1011 } // if not already pushed
1012
1013 } // if LR has a volatile color
1014
1015 } // if LR has color
1016
1017 } // if there is a LR for Var
1018
1019 } // for each value in the LV set after instruction
1020}
1021
1022
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001023//----------------------------------------------------------------------------
1024// We can use the following method to get a temporary register to be used
1025// BEFORE any given machine instruction. If there is a register available,
1026// this method will simply return that register and set MIBef = MIAft = NULL.
1027// Otherwise, it will return a register and MIAft and MIBef will contain
1028// two instructions used to free up this returned register.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +00001029// Returned register number is the UNIFIED register number
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001030//----------------------------------------------------------------------------
1031
Vikram S. Advef5af6362002-07-08 23:15:32 +00001032int PhyRegAlloc::getUsableUniRegAtMI(const int RegType,
1033 const ValueSet *LVSetBef,
1034 MachineInstr *MInst,
1035 std::vector<MachineInstr*>& MIBef,
1036 std::vector<MachineInstr*>& MIAft) {
1037
Chris Lattner133f0792002-10-28 04:45:29 +00001038 RegClass* RC = getRegClassByID(MRI.getRegClassIDOfRegType(RegType));
Vikram S. Advef5af6362002-07-08 23:15:32 +00001039
Vikram S. Advebc001b22003-07-25 21:06:09 +00001040 int RegU = getUnusedUniRegAtMI(RC, RegType, MInst, LVSetBef);
Vikram S. Advef5af6362002-07-08 23:15:32 +00001041
1042 if (RegU == -1) {
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +00001043 // we couldn't find an unused register. Generate code to free up a reg by
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001044 // saving it on stack and restoring after the instruction
Vikram S. Advef5af6362002-07-08 23:15:32 +00001045
Chris Lattnere90fcb72002-12-28 20:35:34 +00001046 int TmpOff = MF.getInfo()->pushTempValue(MRI.getSpilledRegSize(RegType));
Vikram S. Adve12af1642001-11-08 04:48:50 +00001047
Vikram S. Advebc001b22003-07-25 21:06:09 +00001048 RegU = getUniRegNotUsedByThisInst(RC, RegType, MInst);
Vikram S. Advedabb41d2002-05-19 15:29:31 +00001049
Vikram S. Advef5af6362002-07-08 23:15:32 +00001050 // Check if we need a scratch register to copy this register to memory.
1051 int scratchRegType = -1;
1052 if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType))
1053 {
Chris Lattner133f0792002-10-28 04:45:29 +00001054 int scratchReg = getUsableUniRegAtMI(scratchRegType, LVSetBef,
1055 MInst, MIBef, MIAft);
Vikram S. Advef5af6362002-07-08 23:15:32 +00001056 assert(scratchReg != MRI.getInvalidRegNum());
1057
1058 // We may as well hold the value in the scratch register instead
1059 // of copying it to memory and back. But we have to mark the
1060 // register as used by this instruction, so it does not get used
1061 // as a scratch reg. by another operand or anyone else.
Chris Lattner27a08932002-10-22 23:16:21 +00001062 MInst->insertUsedReg(scratchReg);
Vikram S. Advef5af6362002-07-08 23:15:32 +00001063 MRI.cpReg2RegMI(MIBef, RegU, scratchReg, RegType);
1064 MRI.cpReg2RegMI(MIAft, scratchReg, RegU, RegType);
1065 }
1066 else
1067 { // the register can be copied directly to/from memory so do it.
1068 MRI.cpReg2MemMI(MIBef, RegU, MRI.getFramePointer(), TmpOff, RegType);
1069 MRI.cpMem2RegMI(MIAft, MRI.getFramePointer(), TmpOff, RegU, RegType);
1070 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001071 }
Vikram S. Advef5af6362002-07-08 23:15:32 +00001072
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001073 return RegU;
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001074}
1075
Vikram S. Adve814030a2003-07-29 19:49:21 +00001076
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001077//----------------------------------------------------------------------------
Vikram S. Adve814030a2003-07-29 19:49:21 +00001078// This method is called to get a new unused register that can be used
1079// to accomodate a temporary value. This method may be called several times
1080// for a single machine instruction. Each time it is called, it finds a
1081// register which is not live at that instruction and also which is not used
1082// by other spilled operands of the same instruction. Return register number
1083// is relative to the register class, NOT the unified number.
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001084//----------------------------------------------------------------------------
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001085
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001086int PhyRegAlloc::getUnusedUniRegAtMI(RegClass *RC,
Vikram S. Advebc001b22003-07-25 21:06:09 +00001087 const int RegType,
Vikram S. Adve814030a2003-07-29 19:49:21 +00001088 const MachineInstr *MInst,
1089 const ValueSet* LVSetBef) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001090
Vikram S. Advebc001b22003-07-25 21:06:09 +00001091 RC->clearColorsUsed(); // Reset array
Vikram S. Adve814030a2003-07-29 19:49:21 +00001092
1093 if (LVSetBef == NULL) {
1094 LVSetBef = &LVI->getLiveVarSetBeforeMInst(MInst);
1095 assert(LVSetBef != NULL && "Unable to get live-var set before MInst?");
1096 }
1097
Chris Lattner296b7732002-02-05 02:52:05 +00001098 ValueSet::const_iterator LIt = LVSetBef->begin();
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001099
1100 // for each live var in live variable set after machine inst
Chris Lattner7e708292002-06-25 16:13:24 +00001101 for ( ; LIt != LVSetBef->end(); ++LIt) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001102
Vikram S. Advebc001b22003-07-25 21:06:09 +00001103 // get the live range corresponding to live var, and its RegClass
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001104 LiveRange *const LRofLV = LRI.getLiveRangeForValue(*LIt );
1105
1106 // LR can be null if it is a const since a const
1107 // doesn't have a dominating def - see Assumptions above
Vikram S. Advebc001b22003-07-25 21:06:09 +00001108 if (LRofLV && LRofLV->getRegClass() == RC && LRofLV->hasColor())
1109 RC->markColorsUsed(LRofLV->getColor(),
1110 MRI.getRegTypeForLR(LRofLV), RegType);
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001111 }
1112
1113 // It is possible that one operand of this MInst was already spilled
1114 // and it received some register temporarily. If that's the case,
1115 // it is recorded in machine operand. We must skip such registers.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001116 //
Vikram S. Advebc001b22003-07-25 21:06:09 +00001117 setRelRegsUsedByThisInst(RC, RegType, MInst);
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001118
Vikram S. Advebc001b22003-07-25 21:06:09 +00001119 int unusedReg = RC->getUnusedColor(RegType); // find first unused color
1120 if (unusedReg >= 0)
1121 return MRI.getUnifiedRegNum(RC->getID(), unusedReg);
1122
Chris Lattner85c54652002-05-23 15:50:03 +00001123 return -1;
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001124}
1125
1126
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001127//----------------------------------------------------------------------------
1128// Get any other register in a register class, other than what is used
1129// by operands of a machine instruction. Returns the unified reg number.
1130//----------------------------------------------------------------------------
1131int PhyRegAlloc::getUniRegNotUsedByThisInst(RegClass *RC,
Vikram S. Advebc001b22003-07-25 21:06:09 +00001132 const int RegType,
Chris Lattner85c54652002-05-23 15:50:03 +00001133 const MachineInstr *MInst) {
Vikram S. Advebc001b22003-07-25 21:06:09 +00001134 RC->clearColorsUsed();
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001135
Vikram S. Advebc001b22003-07-25 21:06:09 +00001136 setRelRegsUsedByThisInst(RC, RegType, MInst);
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001137
Vikram S. Advebc001b22003-07-25 21:06:09 +00001138 // find the first unused color
1139 int unusedReg = RC->getUnusedColor(RegType);
1140 assert(unusedReg >= 0 &&
1141 "FATAL: No free register could be found in reg class!!");
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001142
Vikram S. Advebc001b22003-07-25 21:06:09 +00001143 return MRI.getUnifiedRegNum(RC->getID(), unusedReg);
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001144}
1145
1146
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001147//----------------------------------------------------------------------------
1148// This method modifies the IsColorUsedArr of the register class passed to it.
1149// It sets the bits corresponding to the registers used by this machine
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +00001150// instructions. Both explicit and implicit operands are set.
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001151//----------------------------------------------------------------------------
Vikram S. Advebc001b22003-07-25 21:06:09 +00001152
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001153void PhyRegAlloc::setRelRegsUsedByThisInst(RegClass *RC,
Vikram S. Advebc001b22003-07-25 21:06:09 +00001154 const int RegType,
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001155 const MachineInstr *MInst )
1156{
1157 assert(OperandsColoredMap[MInst] == true &&
1158 "Illegal to call setRelRegsUsedByThisInst() until colored operands "
1159 "are marked for an instruction.");
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001160
Vikram S. Advef5af6362002-07-08 23:15:32 +00001161 // Add the registers already marked as used by the instruction.
1162 // This should include any scratch registers that are used to save
1163 // values across the instruction (e.g., for saving state register values).
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001164 const std::set<int> &regsUsed = MInst->getRegsUsed();
Vikram S. Advebc001b22003-07-25 21:06:09 +00001165 for (std::set<int>::iterator I=regsUsed.begin(),E=regsUsed.end(); I != E; ++I)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001166 {
1167 int i = *I;
Vikram S. Advef5af6362002-07-08 23:15:32 +00001168 unsigned classId = 0;
Chris Lattner27a08932002-10-22 23:16:21 +00001169 int classRegNum = MRI.getClassRegNum(i, classId);
Vikram S. Advef5af6362002-07-08 23:15:32 +00001170 if (RC->getID() == classId)
Vikram S. Advebc001b22003-07-25 21:06:09 +00001171 RC->markColorsUsed(classRegNum, RegType, RegType);
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +00001172 }
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001173
Vikram S. Advef5af6362002-07-08 23:15:32 +00001174 // If there are implicit references, mark their allocated regs as well
1175 //
1176 for (unsigned z=0; z < MInst->getNumImplicitRefs(); z++)
1177 if (const LiveRange*
1178 LRofImpRef = LRI.getLiveRangeForValue(MInst->getImplicitRef(z)))
1179 if (LRofImpRef->hasColor())
1180 // this implicit reference is in a LR that received a color
Vikram S. Advebc001b22003-07-25 21:06:09 +00001181 RC->markColorsUsed(LRofImpRef->getColor(),
1182 MRI.getRegTypeForLR(LRofImpRef), RegType);
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001183}
1184
1185
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001186//----------------------------------------------------------------------------
Ruchira Sasanka251d8db2001-10-23 21:38:00 +00001187// If there are delay slots for an instruction, the instructions
1188// added after it must really go after the delayed instruction(s).
1189// So, we move the InstrAfter of that instruction to the
1190// corresponding delayed instruction using the following method.
Ruchira Sasanka251d8db2001-10-23 21:38:00 +00001191//----------------------------------------------------------------------------
Ruchira Sasanka251d8db2001-10-23 21:38:00 +00001192
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001193void PhyRegAlloc::move2DelayedInstr(const MachineInstr *OrigMI,
1194 const MachineInstr *DelayedMI)
1195{
Vikram S. Adve814030a2003-07-29 19:49:21 +00001196 if (DEBUG_RA) {
1197 cerr << "\nRegAlloc: Moved InstrnsAfter for: " << *OrigMI;
1198 cerr << " to last delay slot instrn: " << *DelayedMI;
1199 }
1200
Ruchira Sasanka251d8db2001-10-23 21:38:00 +00001201 // "added after" instructions of the original instr
Vikram S. Advedabb41d2002-05-19 15:29:31 +00001202 std::vector<MachineInstr *> &OrigAft = AddedInstrMap[OrigMI].InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +00001203
Ruchira Sasanka251d8db2001-10-23 21:38:00 +00001204 // "added after" instructions of the delayed instr
Vikram S. Adve814030a2003-07-29 19:49:21 +00001205 std::vector<MachineInstr *> &DelayedAft=AddedInstrMap[DelayedMI].InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +00001206
1207 // go thru all the "added after instructions" of the original instruction
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001208 // and append them to the "added after instructions" of the delayed
Ruchira Sasanka251d8db2001-10-23 21:38:00 +00001209 // instructions
Chris Lattner697954c2002-01-20 22:54:45 +00001210 DelayedAft.insert(DelayedAft.end(), OrigAft.begin(), OrigAft.end());
Ruchira Sasanka251d8db2001-10-23 21:38:00 +00001211
1212 // empty the "added after instructions" of the original instruction
1213 OrigAft.clear();
Ruchira Sasanka251d8db2001-10-23 21:38:00 +00001214}
Ruchira Sasanka0931a012001-09-15 19:06:58 +00001215
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001216//----------------------------------------------------------------------------
1217// This method prints the code with registers after register allocation is
1218// complete.
1219//----------------------------------------------------------------------------
1220void PhyRegAlloc::printMachineCode()
1221{
1222
Chris Lattnerf726e772002-10-28 19:22:04 +00001223 cerr << "\n;************** Function " << Fn->getName()
Chris Lattner697954c2002-01-20 22:54:45 +00001224 << " *****************\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001225
Chris Lattnerf726e772002-10-28 19:22:04 +00001226 for (MachineFunction::iterator BBI = MF.begin(), BBE = MF.end();
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001227 BBI != BBE; ++BBI) {
Chris Lattnerf726e772002-10-28 19:22:04 +00001228 cerr << "\n"; printLabel(BBI->getBasicBlock()); cerr << ": ";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001229
1230 // get the iterator for machine instructions
Chris Lattnerf726e772002-10-28 19:22:04 +00001231 MachineBasicBlock& MBB = *BBI;
1232 MachineBasicBlock::iterator MII = MBB.begin();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001233
1234 // iterate over all the machine instructions in BB
Chris Lattnerf726e772002-10-28 19:22:04 +00001235 for ( ; MII != MBB.end(); ++MII) {
Chris Lattnerd9512ca2002-10-29 17:35:39 +00001236 MachineInstr *MInst = *MII;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001237
Chris Lattner697954c2002-01-20 22:54:45 +00001238 cerr << "\n\t";
Chris Lattnerd9512ca2002-10-29 17:35:39 +00001239 cerr << TM.getInstrInfo().getName(MInst->getOpCode());
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001240
Chris Lattner7e708292002-06-25 16:13:24 +00001241 for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001242 MachineOperand& Op = MInst->getOperand(OpNum);
1243
Chris Lattner133f0792002-10-28 04:45:29 +00001244 if (Op.getType() == MachineOperand::MO_VirtualRegister ||
1245 Op.getType() == MachineOperand::MO_CCRegister /*||
1246 Op.getType() == MachineOperand::MO_PCRelativeDisp*/ ) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001247
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001248 const Value *const Val = Op.getVRegValue () ;
Ruchira Sasankae727f852001-09-18 22:43:57 +00001249 // ****this code is temporary till NULL Values are fixed
Chris Lattner7e708292002-06-25 16:13:24 +00001250 if (! Val ) {
Chris Lattner697954c2002-01-20 22:54:45 +00001251 cerr << "\t<*NULL*>";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001252 continue;
1253 }
Ruchira Sasankae727f852001-09-18 22:43:57 +00001254
1255 // if a label or a constant
Chris Lattner7e708292002-06-25 16:13:24 +00001256 if (isa<BasicBlock>(Val)) {
Chris Lattner697954c2002-01-20 22:54:45 +00001257 cerr << "\t"; printLabel( Op.getVRegValue () );
1258 } else {
Ruchira Sasankae727f852001-09-18 22:43:57 +00001259 // else it must be a register value
1260 const int RegNum = Op.getAllocatedRegNum();
1261
Chris Lattner697954c2002-01-20 22:54:45 +00001262 cerr << "\t" << "%" << MRI.getUnifiedRegName( RegNum );
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001263 if (Val->hasName() )
Chris Lattner697954c2002-01-20 22:54:45 +00001264 cerr << "(" << Val->getName() << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001265 else
Chris Lattner697954c2002-01-20 22:54:45 +00001266 cerr << "(" << Val << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001267
Vikram S. Adve5f2180c2003-05-27 00:05:23 +00001268 if (Op.opIsDefOnly() || Op.opIsDefAndUse())
Chris Lattner697954c2002-01-20 22:54:45 +00001269 cerr << "*";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001270
1271 const LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
Chris Lattner7e708292002-06-25 16:13:24 +00001272 if (LROfVal )
1273 if (LROfVal->hasSpillOffset() )
Chris Lattner697954c2002-01-20 22:54:45 +00001274 cerr << "$";
Ruchira Sasankae727f852001-09-18 22:43:57 +00001275 }
1276
1277 }
Chris Lattner133f0792002-10-28 04:45:29 +00001278 else if (Op.getType() == MachineOperand::MO_MachineRegister) {
Chris Lattner697954c2002-01-20 22:54:45 +00001279 cerr << "\t" << "%" << MRI.getUnifiedRegName(Op.getMachineRegNum());
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001280 }
1281
1282 else
Chris Lattner697954c2002-01-20 22:54:45 +00001283 cerr << "\t" << Op; // use dump field
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001284 }
1285
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001286
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001287
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001288 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
Chris Lattner7e708292002-06-25 16:13:24 +00001289 if (NumOfImpRefs > 0) {
Chris Lattner697954c2002-01-20 22:54:45 +00001290 cerr << "\tImplicit:";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001291
Chris Lattner7e708292002-06-25 16:13:24 +00001292 for (unsigned z=0; z < NumOfImpRefs; z++)
Chris Lattner0665a5f2002-02-05 01:43:49 +00001293 cerr << RAV(MInst->getImplicitRef(z)) << "\t";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001294 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001295
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001296 } // for all machine instructions
1297
Chris Lattner697954c2002-01-20 22:54:45 +00001298 cerr << "\n";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001299
1300 } // for all BBs
1301
Chris Lattner697954c2002-01-20 22:54:45 +00001302 cerr << "\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001303}
1304
Ruchira Sasankae727f852001-09-18 22:43:57 +00001305
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001306//----------------------------------------------------------------------------
1307
1308//----------------------------------------------------------------------------
1309void PhyRegAlloc::colorIncomingArgs()
1310{
Vikram S. Adve814030a2003-07-29 19:49:21 +00001311 MRI.colorMethodArgs(Fn, LRI, AddedInstrAtEntry.InstrnsBefore,
1312 AddedInstrAtEntry.InstrnsAfter);
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001313}
1314
Ruchira Sasankae727f852001-09-18 22:43:57 +00001315
1316//----------------------------------------------------------------------------
1317// Used to generate a label for a basic block
1318//----------------------------------------------------------------------------
Chris Lattnerf726e772002-10-28 19:22:04 +00001319void PhyRegAlloc::printLabel(const Value *Val) {
Chris Lattner697954c2002-01-20 22:54:45 +00001320 if (Val->hasName())
1321 cerr << Val->getName();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001322 else
Chris Lattnerf726e772002-10-28 19:22:04 +00001323 cerr << "Label" << Val;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001324}
1325
1326
Ruchira Sasankae727f852001-09-18 22:43:57 +00001327//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001328// This method calls setSugColorUsable method of each live range. This
1329// will determine whether the suggested color of LR is really usable.
1330// A suggested color is not usable when the suggested color is volatile
1331// AND when there are call interferences
1332//----------------------------------------------------------------------------
1333
1334void PhyRegAlloc::markUnusableSugColors()
1335{
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001336 // hash map iterator
1337 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
1338 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
1339
Chris Lattner7e708292002-06-25 16:13:24 +00001340 for (; HMI != HMIEnd ; ++HMI ) {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001341 if (HMI->first) {
1342 LiveRange *L = HMI->second; // get the LiveRange
1343 if (L) {
Chris Lattner7e708292002-06-25 16:13:24 +00001344 if (L->hasSuggestedColor()) {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001345 int RCID = L->getRegClass()->getID();
Chris Lattner7e708292002-06-25 16:13:24 +00001346 if (MRI.isRegVolatile( RCID, L->getSuggestedColor()) &&
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001347 L->isCallInterference() )
1348 L->setSuggestedColorUsable( false );
1349 else
1350 L->setSuggestedColorUsable( true );
1351 }
1352 } // if L->hasSuggestedColor()
1353 }
1354 } // for all LR's in hash map
1355}
1356
1357
1358
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001359//----------------------------------------------------------------------------
1360// The following method will set the stack offsets of the live ranges that
1361// are decided to be spillled. This must be called just after coloring the
1362// LRs using the graph coloring algo. For each live range that is spilled,
1363// this method allocate a new spill position on the stack.
1364//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001365
Chris Lattner37730942002-02-05 03:52:29 +00001366void PhyRegAlloc::allocateStackSpace4SpilledLRs() {
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001367 if (DEBUG_RA) cerr << "\nSetting LR stack offsets for spills...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001368
Chris Lattner37730942002-02-05 03:52:29 +00001369 LiveRangeMapType::const_iterator HMI = LRI.getLiveRangeMap()->begin();
1370 LiveRangeMapType::const_iterator HMIEnd = LRI.getLiveRangeMap()->end();
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001371
Chris Lattner7e708292002-06-25 16:13:24 +00001372 for ( ; HMI != HMIEnd ; ++HMI) {
Chris Lattner37730942002-02-05 03:52:29 +00001373 if (HMI->first && HMI->second) {
Vikram S. Adve3bf08922003-07-10 19:42:55 +00001374 LiveRange *L = HMI->second; // get the LiveRange
1375 if (L->isMarkedForSpill()) { // NOTE: allocating size of long Type **
Chris Lattnere90fcb72002-12-28 20:35:34 +00001376 int stackOffset = MF.getInfo()->allocateSpilledValue(Type::LongTy);
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001377 L->setSpillOffFromFP(stackOffset);
1378 if (DEBUG_RA)
1379 cerr << " LR# " << L->getUserIGNode()->getIndex()
1380 << ": stack-offset = " << stackOffset << "\n";
1381 }
Chris Lattner37730942002-02-05 03:52:29 +00001382 }
1383 } // for all LR's in hash map
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001384}
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001385
1386
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001387//----------------------------------------------------------------------------
Ruchira Sasankae727f852001-09-18 22:43:57 +00001388// The entry pont to Register Allocation
1389//----------------------------------------------------------------------------
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001390
1391void PhyRegAlloc::allocateRegisters()
1392{
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001393
1394 // make sure that we put all register classes into the RegClassList
1395 // before we call constructLiveRanges (now done in the constructor of
1396 // PhyRegAlloc class).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001397 //
1398 LRI.constructLiveRanges(); // create LR info
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001399
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001400 if (DEBUG_RA >= RA_DEBUG_LiveRanges)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001401 LRI.printLiveRanges();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001402
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001403 createIGNodeListsAndIGs(); // create IGNode list and IGs
1404
1405 buildInterferenceGraphs(); // build IGs in all reg classes
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001406
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001407
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001408 if (DEBUG_RA >= RA_DEBUG_LiveRanges) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001409 // print all LRs in all reg classes
Chris Lattner7e708292002-06-25 16:13:24 +00001410 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
1411 RegClassList[rc]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001412
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001413 // print IGs in all register classes
Chris Lattner7e708292002-06-25 16:13:24 +00001414 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
1415 RegClassList[rc]->printIG();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001416 }
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001417
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001418 LRI.coalesceLRs(); // coalesce all live ranges
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +00001419
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001420 if (DEBUG_RA >= RA_DEBUG_LiveRanges) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001421 // print all LRs in all reg classes
Chris Lattnerf726e772002-10-28 19:22:04 +00001422 for (unsigned rc=0; rc < NumOfRegClasses; rc++)
1423 RegClassList[rc]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001424
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001425 // print IGs in all register classes
Chris Lattnerf726e772002-10-28 19:22:04 +00001426 for (unsigned rc=0; rc < NumOfRegClasses; rc++)
1427 RegClassList[rc]->printIG();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001428 }
1429
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001430
1431 // mark un-usable suggested color before graph coloring algorithm.
1432 // When this is done, the graph coloring algo will not reserve
1433 // suggested color unnecessarily - they can be used by another LR
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001434 //
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001435 markUnusableSugColors();
1436
1437 // color all register classes using the graph coloring algo
Chris Lattner7e708292002-06-25 16:13:24 +00001438 for (unsigned rc=0; rc < NumOfRegClasses ; rc++)
Chris Lattnerf726e772002-10-28 19:22:04 +00001439 RegClassList[rc]->colorAllRegs();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001440
Chris Lattnere90fcb72002-12-28 20:35:34 +00001441 // Atter graph coloring, if some LRs did not receive a color (i.e, spilled)
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001442 // a poistion for such spilled LRs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001443 //
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001444 allocateStackSpace4SpilledLRs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001445
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001446 // Reset the temp. area on the stack before use by the first instruction.
1447 // This will also happen after updating each instruction.
1448 MF.getInfo()->popAllTempValues();
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001449
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001450 // color incoming args - if the correct color was not received
1451 // insert code to copy to the correct register
1452 //
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001453 colorIncomingArgs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001454
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001455 // Now update the machine code with register names and add any
1456 // additional code inserted by the register allocator to the instruction
1457 // stream
1458 //
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001459 updateMachineCode();
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001460
Chris Lattner045e7c82001-09-19 16:26:23 +00001461 if (DEBUG_RA) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001462 cerr << "\n**** Machine Code After Register Allocation:\n\n";
Chris Lattnerf726e772002-10-28 19:22:04 +00001463 MF.dump();
Chris Lattner045e7c82001-09-19 16:26:23 +00001464 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001465}
1466
Ruchira Sasankae727f852001-09-18 22:43:57 +00001467
1468