blob: 1e44e17fbdae908fbfce6bf9d3be866bb032c2bb [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 Lattner70b2f562003-09-01 20:09:04 +00008#include "PhyRegAlloc.h"
Chris Lattner4309e732003-01-15 19:57:07 +00009#include "RegAllocCommon.h"
Chris Lattner9d4ed152003-01-15 21:14:01 +000010#include "RegClass.h"
Chris Lattnerc083dcc2003-09-01 20:05:47 +000011#include "IGNode.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"
Vikram S. Advefeb32982003-08-12 22:22:24 +000027#include "Support/SetOperations.h"
Chris Lattner4bc23482002-09-15 07:07:55 +000028#include "Support/CommandLine.h"
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000029#include <math.h>
Vikram S. Adve12af1642001-11-08 04:48:50 +000030
Chris Lattner70e60cb2002-05-22 17:08:27 +000031RegAllocDebugLevel_t DEBUG_RA;
Vikram S. Adve39c94e12002-09-14 23:05:33 +000032
Chris Lattner5ff62e92002-07-22 02:10:13 +000033static cl::opt<RegAllocDebugLevel_t, true>
34DRA_opt("dregalloc", cl::Hidden, cl::location(DEBUG_RA),
35 cl::desc("enable register allocation debugging information"),
36 cl::values(
Vikram S. Adve39c94e12002-09-14 23:05:33 +000037 clEnumValN(RA_DEBUG_None , "n", "disable debug output"),
38 clEnumValN(RA_DEBUG_Results, "y", "debug output for allocation results"),
39 clEnumValN(RA_DEBUG_Coloring, "c", "debug output for graph coloring step"),
40 clEnumValN(RA_DEBUG_Interference,"ig","debug output for interference graphs"),
41 clEnumValN(RA_DEBUG_LiveRanges , "lr","debug output for live ranges"),
42 clEnumValN(RA_DEBUG_Verbose, "v", "extra debug output"),
Chris Lattner5ff62e92002-07-22 02:10:13 +000043 0));
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000044
Chris Lattner2f9b28e2002-02-04 15:54:09 +000045//----------------------------------------------------------------------------
46// RegisterAllocation pass front end...
47//----------------------------------------------------------------------------
48namespace {
Chris Lattnerf57b8452002-04-27 06:56:12 +000049 class RegisterAllocator : public FunctionPass {
Chris Lattner2f9b28e2002-02-04 15:54:09 +000050 TargetMachine &Target;
51 public:
52 inline RegisterAllocator(TargetMachine &T) : Target(T) {}
Chris Lattner96c466b2002-04-29 14:57:45 +000053
54 const char *getPassName() const { return "Register Allocation"; }
Chris Lattner6dd98a62002-02-04 00:33:08 +000055
Chris Lattner7e708292002-06-25 16:13:24 +000056 bool runOnFunction(Function &F) {
Chris Lattner2f9b28e2002-02-04 15:54:09 +000057 if (DEBUG_RA)
Chris Lattnerc083dcc2003-09-01 20:05:47 +000058 std::cerr << "\n********* Function "<< F.getName() << " ***********\n";
Chris Lattner2f9b28e2002-02-04 15:54:09 +000059
Chris Lattner7e708292002-06-25 16:13:24 +000060 PhyRegAlloc PRA(&F, Target, &getAnalysis<FunctionLiveVarInfo>(),
Chris Lattner1b7f7dc2002-04-28 16:21:30 +000061 &getAnalysis<LoopInfo>());
Chris Lattner2f9b28e2002-02-04 15:54:09 +000062 PRA.allocateRegisters();
63
Chris Lattnerc083dcc2003-09-01 20:05:47 +000064 if (DEBUG_RA) std::cerr << "\nRegister allocation complete!\n";
Chris Lattner2f9b28e2002-02-04 15:54:09 +000065 return false;
66 }
Chris Lattner4911c352002-02-04 17:39:42 +000067
Chris Lattnerf57b8452002-04-27 06:56:12 +000068 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerdd5b4952002-08-08 19:01:28 +000069 AU.addRequired<LoopInfo>();
70 AU.addRequired<FunctionLiveVarInfo>();
Chris Lattner4911c352002-02-04 17:39:42 +000071 }
Chris Lattner2f9b28e2002-02-04 15:54:09 +000072 };
Chris Lattner6dd98a62002-02-04 00:33:08 +000073}
74
Brian Gaekebf3c4cf2003-08-14 06:09:32 +000075FunctionPass *getRegisterAllocator(TargetMachine &T) {
Chris Lattner2f9b28e2002-02-04 15:54:09 +000076 return new RegisterAllocator(T);
77}
Chris Lattner6dd98a62002-02-04 00:33:08 +000078
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000079//----------------------------------------------------------------------------
80// Constructor: Init local composite objects and create register classes.
81//----------------------------------------------------------------------------
Chris Lattner1b7f7dc2002-04-28 16:21:30 +000082PhyRegAlloc::PhyRegAlloc(Function *F, const TargetMachine& tm,
83 FunctionLiveVarInfo *Lvi, LoopInfo *LDC)
Chris Lattnerf726e772002-10-28 19:22:04 +000084 : TM(tm), Fn(F), MF(MachineFunction::get(F)), LVI(Lvi),
85 LRI(F, tm, RegClassList), MRI(tm.getRegInfo()),
86 NumOfRegClasses(MRI.getNumOfRegClasses()), LoopDepthCalc(LDC) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +000087
88 // create each RegisterClass and put in RegClassList
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000089 //
Chris Lattnerf726e772002-10-28 19:22:04 +000090 for (unsigned rc=0; rc != NumOfRegClasses; rc++)
Vikram S. Advebc001b22003-07-25 21:06:09 +000091 RegClassList.push_back(new RegClass(F, &tm.getRegInfo(),
92 MRI.getMachineRegClass(rc)));
Ruchira Sasanka8e604792001-09-14 21:18:34 +000093}
94
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000095
96//----------------------------------------------------------------------------
97// Destructor: Deletes register classes
98//----------------------------------------------------------------------------
99PhyRegAlloc::~PhyRegAlloc() {
Chris Lattner7e708292002-06-25 16:13:24 +0000100 for ( unsigned rc=0; rc < NumOfRegClasses; rc++)
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000101 delete RegClassList[rc];
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000102
103 AddedInstrMap.clear();
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000104}
105
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000106//----------------------------------------------------------------------------
107// This method initally creates interference graphs (one in each reg class)
108// and IGNodeList (one in each IG). The actual nodes will be pushed later.
109//----------------------------------------------------------------------------
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000110void PhyRegAlloc::createIGNodeListsAndIGs() {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000111 if (DEBUG_RA >= RA_DEBUG_LiveRanges) std::cerr << "Creating LR lists ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000112
113 // hash map iterator
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000114 LiveRangeMapType::const_iterator HMI = LRI.getLiveRangeMap()->begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000115
116 // hash map end
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000117 LiveRangeMapType::const_iterator HMIEnd = LRI.getLiveRangeMap()->end();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000118
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000119 for (; HMI != HMIEnd ; ++HMI ) {
120 if (HMI->first) {
121 LiveRange *L = HMI->second; // get the LiveRange
122 if (!L) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000123 if (DEBUG_RA)
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000124 std::cerr << "\n**** ?!?WARNING: NULL LIVE RANGE FOUND FOR: "
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000125 << RAV(HMI->first) << "****\n";
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000126 continue;
127 }
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000128
129 // if the Value * is not null, and LR is not yet written to the IGNodeList
Chris Lattner7e708292002-06-25 16:13:24 +0000130 if (!(L->getUserIGNode()) ) {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000131 RegClass *const RC = // RegClass of first value in the LR
132 RegClassList[ L->getRegClass()->getID() ];
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000133 RC->addLRToIG(L); // add this LR to an IG
134 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000135 }
136 }
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000137
138 // init RegClassList
Chris Lattner7e708292002-06-25 16:13:24 +0000139 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000140 RegClassList[rc]->createInterferenceGraph();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000141
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000142 if (DEBUG_RA >= RA_DEBUG_LiveRanges) std::cerr << "LRLists Created!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000143}
144
145
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000146//----------------------------------------------------------------------------
147// This method will add all interferences at for a given instruction.
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000148// Interence occurs only if the LR of Def (Inst or Arg) is of the same reg
149// class as that of live var. The live var passed to this function is the
150// LVset AFTER the instruction
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000151//----------------------------------------------------------------------------
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000152
Chris Lattner296b7732002-02-05 02:52:05 +0000153void PhyRegAlloc::addInterference(const Value *Def,
154 const ValueSet *LVSet,
155 bool isCallInst) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000156
Chris Lattner296b7732002-02-05 02:52:05 +0000157 ValueSet::const_iterator LIt = LVSet->begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000158
159 // get the live range of instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000160 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000161 const LiveRange *const LROfDef = LRI.getLiveRangeForValue( Def );
162
163 IGNode *const IGNodeOfDef = LROfDef->getUserIGNode();
164 assert( IGNodeOfDef );
165
166 RegClass *const RCOfDef = LROfDef->getRegClass();
167
168 // for each live var in live variable set
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000169 //
Chris Lattner7e708292002-06-25 16:13:24 +0000170 for ( ; LIt != LVSet->end(); ++LIt) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000171
Vikram S. Advef5af6362002-07-08 23:15:32 +0000172 if (DEBUG_RA >= RA_DEBUG_Verbose)
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000173 std::cerr << "< Def=" << RAV(Def) << ", Lvar=" << RAV(*LIt) << "> ";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000174
175 // get the live range corresponding to live var
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000176 //
Chris Lattner0665a5f2002-02-05 01:43:49 +0000177 LiveRange *LROfVar = LRI.getLiveRangeForValue(*LIt);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000178
179 // LROfVar can be null if it is a const since a const
180 // doesn't have a dominating def - see Assumptions above
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000181 //
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000182 if (LROfVar)
183 if (LROfDef != LROfVar) // do not set interf for same LR
184 if (RCOfDef == LROfVar->getRegClass()) // 2 reg classes are the same
185 RCOfDef->setInterference( LROfDef, LROfVar);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000186 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000187}
188
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000189
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000190
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000191//----------------------------------------------------------------------------
192// For a call instruction, this method sets the CallInterference flag in
193// the LR of each variable live int the Live Variable Set live after the
194// call instruction (except the return value of the call instruction - since
195// the return value does not interfere with that call itself).
196//----------------------------------------------------------------------------
197
198void PhyRegAlloc::setCallInterferences(const MachineInstr *MInst,
Chris Lattner296b7732002-02-05 02:52:05 +0000199 const ValueSet *LVSetAft) {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000200
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000201 if (DEBUG_RA >= RA_DEBUG_Interference)
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000202 std::cerr << "\n For call inst: " << *MInst;
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000203
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000204 // for each live var in live variable set after machine inst
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000205 //
Vikram S. Adve65b2f402003-07-02 01:24:00 +0000206 for (ValueSet::const_iterator LIt = LVSetAft->begin(), LEnd = LVSetAft->end();
207 LIt != LEnd; ++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) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000218 std::cerr << "\n\tLR after Call: ";
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000219 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) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000223 std::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)
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000266 std::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)
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000340 std::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);
Chris Lattnerc083dcc2003-09-01 20:05:47 +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) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000379 std::cerr << "\nInterf not set for any operand in pseudo instr:\n";
380 std::cerr << *MInst;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000381 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 Lattnerc083dcc2003-09-01 20:05:47 +0000400 std::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
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000451PrependInstructions(std::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) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000463 if (OrigMI) std::cerr << "For MInst:\n " << *OrigMI;
464 std::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) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000484 if (OrigMI) std::cerr << "For MInst:\n " << *OrigMI;
485 std::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
Vikram S. Adve814030a2003-07-29 19:49:21 +0000492static bool MarkAllocatedRegs(MachineInstr* MInst,
493 LiveRangeInfo& LRI,
494 const TargetRegInfo& MRI)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000495{
Vikram S. Adve814030a2003-07-29 19:49:21 +0000496 bool instrNeedsSpills = false;
497
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000498 // First, set the registers for operands in the machine instruction
499 // if a register was successfully allocated. Do this first because we
500 // will need to know which registers are already used by this instr'n.
501 //
502 for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum)
503 {
504 MachineOperand& Op = MInst->getOperand(OpNum);
505 if (Op.getType() == MachineOperand::MO_VirtualRegister ||
506 Op.getType() == MachineOperand::MO_CCRegister)
507 {
508 const Value *const Val = Op.getVRegValue();
Vikram S. Adve814030a2003-07-29 19:49:21 +0000509 if (const LiveRange* LR = LRI.getLiveRangeForValue(Val)) {
510 // Remember if any operand needs spilling
511 instrNeedsSpills |= LR->isMarkedForSpill();
512
513 // An operand may have a color whether or not it needs spilling
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000514 if (LR->hasColor())
515 MInst->SetRegForOperand(OpNum,
516 MRI.getUnifiedRegNum(LR->getRegClass()->getID(),
517 LR->getColor()));
Vikram S. Adve814030a2003-07-29 19:49:21 +0000518 }
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000519 }
520 } // for each operand
Vikram S. Adve814030a2003-07-29 19:49:21 +0000521
522 return instrNeedsSpills;
523}
524
525void PhyRegAlloc::updateInstruction(MachineBasicBlock::iterator& MII,
526 MachineBasicBlock &MBB)
527{
528 MachineInstr* MInst = *MII;
529 unsigned Opcode = MInst->getOpCode();
530
531 // Reset tmp stack positions so they can be reused for each machine instr.
532 MF.getInfo()->popAllTempValues();
533
534 // Mark the operands for which regs have been allocated.
535 bool instrNeedsSpills = MarkAllocatedRegs(*MII, LRI, MRI);
536
537#ifndef NDEBUG
538 // Mark that the operands have been updated. Later,
539 // setRelRegsUsedByThisInst() is called to find registers used by each
540 // MachineInst, and it should not be used for an instruction until
541 // this is done. This flag just serves as a sanity check.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000542 OperandsColoredMap[MInst] = true;
Vikram S. Adve814030a2003-07-29 19:49:21 +0000543#endif
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000544
Vikram S. Advebc001b22003-07-25 21:06:09 +0000545 // Now insert caller-saving code before/after the call.
546 // Do this before inserting spill code since some registers must be
547 // used by save/restore and spill code should not use those registers.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000548 //
Vikram S. Advebc001b22003-07-25 21:06:09 +0000549 if (TM.getInstrInfo().isCall(Opcode)) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000550 AddedInstrns &AI = AddedInstrMap[MInst];
Vikram S. Adve814030a2003-07-29 19:49:21 +0000551 insertCallerSavingCode(AI.InstrnsBefore, AI.InstrnsAfter, MInst,
552 MBB.getBasicBlock());
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000553 }
Vikram S. Advebc001b22003-07-25 21:06:09 +0000554
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000555 // Now insert spill code for remaining operands not allocated to
556 // registers. This must be done even for call return instructions
557 // since those are not handled by the special code above.
Vikram S. Adve814030a2003-07-29 19:49:21 +0000558 if (instrNeedsSpills)
559 for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum)
560 {
561 MachineOperand& Op = MInst->getOperand(OpNum);
562 if (Op.getType() == MachineOperand::MO_VirtualRegister ||
563 Op.getType() == MachineOperand::MO_CCRegister)
564 {
565 const Value* Val = Op.getVRegValue();
566 if (const LiveRange *LR = LRI.getLiveRangeForValue(Val))
567 if (LR->isMarkedForSpill())
568 insertCode4SpilledLR(LR, MII, MBB, OpNum);
569 }
570 } // for each operand
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000571}
572
573void PhyRegAlloc::updateMachineCode()
574{
Chris Lattner7e708292002-06-25 16:13:24 +0000575 // Insert any instructions needed at method entry
Chris Lattnerf726e772002-10-28 19:22:04 +0000576 MachineBasicBlock::iterator MII = MF.front().begin();
577 PrependInstructions(AddedInstrAtEntry.InstrnsBefore, MF.front(), MII,
Chris Lattner7e708292002-06-25 16:13:24 +0000578 "At function entry: \n");
579 assert(AddedInstrAtEntry.InstrnsAfter.empty() &&
580 "InstrsAfter should be unnecessary since we are just inserting at "
581 "the function entry point here.");
Vikram S. Adve48762092002-04-25 04:34:15 +0000582
Chris Lattnerf726e772002-10-28 19:22:04 +0000583 for (MachineFunction::iterator BBI = MF.begin(), BBE = MF.end();
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000584 BBI != BBE; ++BBI) {
Vikram S. Advecb202e32002-10-11 16:12:40 +0000585
Chris Lattnerf726e772002-10-28 19:22:04 +0000586 MachineBasicBlock &MBB = *BBI;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000587
588 // Iterate over all machine instructions in BB and mark operands with
589 // their assigned registers or insert spill code, as appropriate.
590 // Also, fix operands of call/return instructions.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000591 for (MachineBasicBlock::iterator MII = MBB.begin(); MII != MBB.end(); ++MII)
Vikram S. Adve814030a2003-07-29 19:49:21 +0000592 if (! TM.getInstrInfo().isDummyPhiInstr((*MII)->getOpCode()))
593 updateInstruction(MII, MBB);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000594
595 // Now, move code out of delay slots of branches and returns if needed.
596 // (Also, move "after" code from calls to the last delay slot instruction.)
597 // Moving code out of delay slots is needed in 2 situations:
598 // (1) If this is a branch and it needs instructions inserted after it,
599 // move any existing instructions out of the delay slot so that the
600 // instructions can go into the delay slot. This only supports the
601 // case that #instrsAfter <= #delay slots.
602 //
603 // (2) If any instruction in the delay slot needs
604 // instructions inserted, move it out of the delay slot and before the
605 // branch because putting code before or after it would be VERY BAD!
606 //
607 // If the annul bit of the branch is set, neither of these is legal!
608 // If so, we need to handle spill differently but annulling is not yet used.
609 //
610 for (MachineBasicBlock::iterator MII = MBB.begin();
611 MII != MBB.end(); ++MII)
612 if (unsigned delaySlots =
613 TM.getInstrInfo().getNumDelaySlots((*MII)->getOpCode()))
614 {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000615 MachineInstr *MInst = *MII, *DelaySlotMI = *(MII+1);
616
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000617 // Check the 2 conditions above:
618 // (1) Does a branch need instructions added after it?
619 // (2) O/w does delay slot instr. need instrns before or after?
Vikram S. Adve814030a2003-07-29 19:49:21 +0000620 bool isBranch = (TM.getInstrInfo().isBranch(MInst->getOpCode()) ||
621 TM.getInstrInfo().isReturn(MInst->getOpCode()));
622 bool cond1 = (isBranch &&
623 AddedInstrMap.count(MInst) &&
624 AddedInstrMap[MInst].InstrnsAfter.size() > 0);
625 bool cond2 = (AddedInstrMap.count(DelaySlotMI) &&
626 (AddedInstrMap[DelaySlotMI].InstrnsBefore.size() > 0 ||
627 AddedInstrMap[DelaySlotMI].InstrnsAfter.size() > 0));
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000628
629 if (cond1 || cond2)
630 {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000631 assert((MInst->getOpCodeFlags() & AnnulFlag) == 0 &&
632 "FIXME: Moving an annulled delay slot instruction!");
633 assert(delaySlots==1 &&
634 "InsertBefore does not yet handle >1 delay slots!");
635 InsertBefore(DelaySlotMI, MBB, MII); // MII pts back to branch
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000636
637 // In case (1), delete it and don't replace with anything!
638 // Otherwise (i.e., case (2) only) replace it with a NOP.
639 if (cond1) {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000640 DeleteInstruction(MBB, ++MII); // MII now points to next inst.
641 --MII; // reset MII for ++MII of loop
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000642 }
Vikram S. Adve814030a2003-07-29 19:49:21 +0000643 else
644 SubstituteInPlace(BuildMI(TM.getInstrInfo().getNOPOpCode(),1),
645 MBB, MII+1); // replace with NOP
646
647 if (DEBUG_RA) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000648 std::cerr << "\nRegAlloc: Moved instr. with added code: "
Vikram S. Adve814030a2003-07-29 19:49:21 +0000649 << *DelaySlotMI
650 << " out of delay slots of instr: " << *MInst;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000651 }
652 }
Vikram S. Adve814030a2003-07-29 19:49:21 +0000653 else
654 // For non-branch instr with delay slots (probably a call), move
655 // InstrAfter to the instr. in the last delay slot.
656 move2DelayedInstr(*MII, *(MII+delaySlots));
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000657 }
658
659 // Finally iterate over all instructions in BB and insert before/after
660 //
Vikram S. Advebc001b22003-07-25 21:06:09 +0000661 for (MachineBasicBlock::iterator MII=MBB.begin(); MII != MBB.end(); ++MII) {
Vikram S. Adve48762092002-04-25 04:34:15 +0000662 MachineInstr *MInst = *MII;
Vikram S. Advebc001b22003-07-25 21:06:09 +0000663
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000664 // do not process Phis
Vikram S. Advebc001b22003-07-25 21:06:09 +0000665 if (TM.getInstrInfo().isDummyPhiInstr(MInst->getOpCode()))
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000666 continue;
667
Vikram S. Advebc001b22003-07-25 21:06:09 +0000668 // if there are any added instructions...
Chris Lattner7e708292002-06-25 16:13:24 +0000669 if (AddedInstrMap.count(MInst)) {
Vikram S. Advebc001b22003-07-25 21:06:09 +0000670 AddedInstrns &CallAI = AddedInstrMap[MInst];
671
672#ifndef NDEBUG
Vikram S. Adve814030a2003-07-29 19:49:21 +0000673 bool isBranch = (TM.getInstrInfo().isBranch(MInst->getOpCode()) ||
674 TM.getInstrInfo().isReturn(MInst->getOpCode()));
675 assert((!isBranch ||
676 AddedInstrMap[MInst].InstrnsAfter.size() <=
677 TM.getInstrInfo().getNumDelaySlots(MInst->getOpCode())) &&
678 "Cannot put more than #delaySlots instrns after "
679 "branch or return! Need to handle temps differently.");
680#endif
681
682#ifndef NDEBUG
Vikram S. Advebc001b22003-07-25 21:06:09 +0000683 // Temporary sanity checking code to detect whether the same machine
684 // instruction is ever inserted twice before/after a call.
685 // I suspect this is happening but am not sure. --Vikram, 7/1/03.
686 //
687 std::set<const MachineInstr*> instrsSeen;
688 for (int i = 0, N = CallAI.InstrnsBefore.size(); i < N; ++i) {
689 assert(instrsSeen.count(CallAI.InstrnsBefore[i]) == 0 &&
690 "Duplicate machine instruction in InstrnsBefore!");
691 instrsSeen.insert(CallAI.InstrnsBefore[i]);
692 }
693 for (int i = 0, N = CallAI.InstrnsAfter.size(); i < N; ++i) {
694 assert(instrsSeen.count(CallAI.InstrnsAfter[i]) == 0 &&
695 "Duplicate machine instruction in InstrnsBefore/After!");
696 instrsSeen.insert(CallAI.InstrnsAfter[i]);
697 }
698#endif
699
700 // Now add the instructions before/after this MI.
701 // We do this here to ensure that spill for an instruction is inserted
702 // as close as possible to an instruction (see above insertCode4Spill)
703 //
704 if (! CallAI.InstrnsBefore.empty())
705 PrependInstructions(CallAI.InstrnsBefore, MBB, MII,"");
706
707 if (! CallAI.InstrnsAfter.empty())
708 AppendInstructions(CallAI.InstrnsAfter, MBB, MII,"");
709
710 } // if there are any added instructions
Vikram S. Advecb202e32002-10-11 16:12:40 +0000711
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000712 } // for each machine instruction
Vikram S. Adve814030a2003-07-29 19:49:21 +0000713
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000714 }
715}
716
717
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000718
719//----------------------------------------------------------------------------
720// This method inserts spill code for AN operand whose LR was spilled.
721// This method may be called several times for a single machine instruction
722// if it contains many spilled operands. Each time it is called, it finds
723// a register which is not live at that instruction and also which is not
724// used by other spilled operands of the same instruction. Then it uses
725// this register temporarily to accomodate the spilled value.
726//----------------------------------------------------------------------------
Vikram S. Advebc001b22003-07-25 21:06:09 +0000727
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000728void PhyRegAlloc::insertCode4SpilledLR(const LiveRange *LR,
Vikram S. Adve814030a2003-07-29 19:49:21 +0000729 MachineBasicBlock::iterator& MII,
730 MachineBasicBlock &MBB,
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000731 const unsigned OpNum) {
732
Vikram S. Adve814030a2003-07-29 19:49:21 +0000733 MachineInstr *MInst = *MII;
734 const BasicBlock *BB = MBB.getBasicBlock();
735
Vikram S. Advead9c9782002-09-28 17:02:40 +0000736 assert((! TM.getInstrInfo().isCall(MInst->getOpCode()) || OpNum == 0) &&
737 "Outgoing arg of a call must be handled elsewhere (func arg ok)");
738 assert(! TM.getInstrInfo().isReturn(MInst->getOpCode()) &&
739 "Return value of a ret must be handled elsewhere");
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000740
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000741 MachineOperand& Op = MInst->getOperand(OpNum);
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000742 bool isDef = Op.opIsDefOnly();
743 bool isDefAndUse = Op.opIsDefAndUse();
Vikram S. Advebc001b22003-07-25 21:06:09 +0000744 unsigned RegType = MRI.getRegTypeForLR(LR);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000745 int SpillOff = LR->getSpillOffFromFP();
746 RegClass *RC = LR->getRegClass();
Vikram S. Adve814030a2003-07-29 19:49:21 +0000747
748 // Get the live-variable set to find registers free before this instr.
Vikram S. Advefeb32982003-08-12 22:22:24 +0000749 const ValueSet &LVSetBef = LVI->getLiveVarSetBeforeMInst(MInst, BB);
750
751#ifndef NDEBUG
752 // If this instr. is in the delay slot of a branch or return, we need to
753 // include all live variables before that branch or return -- we don't want to
754 // trample those! Verify that the set is included in the LV set before MInst.
Vikram S. Adve814030a2003-07-29 19:49:21 +0000755 //
Vikram S. Adve814030a2003-07-29 19:49:21 +0000756 if (MII != MBB.begin()) {
757 MachineInstr *PredMI = *(MII-1);
Vikram S. Advefeb32982003-08-12 22:22:24 +0000758 if (unsigned DS = TM.getInstrInfo().getNumDelaySlots(PredMI->getOpCode()))
759 assert(set_difference(LVI->getLiveVarSetBeforeMInst(PredMI), LVSetBef)
760 .empty() && "Live-var set before branch should be included in "
761 "live-var set of each delay slot instruction!");
Vikram S. Adve814030a2003-07-29 19:49:21 +0000762 }
Vikram S. Advefeb32982003-08-12 22:22:24 +0000763#endif
Vikram S. Adve00521d72001-11-12 23:26:35 +0000764
Chris Lattnere90fcb72002-12-28 20:35:34 +0000765 MF.getInfo()->pushTempValue(MRI.getSpilledRegSize(RegType) );
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000766
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000767 std::vector<MachineInstr*> MIBef, MIAft;
768 std::vector<MachineInstr*> AdIMid;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000769
Vikram S. Adve3bf08922003-07-10 19:42:55 +0000770 // Choose a register to hold the spilled value, if one was not preallocated.
771 // This may insert code before and after MInst to free up the value. If so,
772 // this code should be first/last in the spill sequence before/after MInst.
773 int TmpRegU=(LR->hasColor()
774 ? MRI.getUnifiedRegNum(LR->getRegClass()->getID(),LR->getColor())
775 : getUsableUniRegAtMI(RegType, &LVSetBef, MInst, MIBef,MIAft));
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000776
Vikram S. Advef5af6362002-07-08 23:15:32 +0000777 // Set the operand first so that it this register does not get used
778 // as a scratch register for later calls to getUsableUniRegAtMI below
779 MInst->SetRegForOperand(OpNum, TmpRegU);
780
781 // get the added instructions for this instruction
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000782 AddedInstrns &AI = AddedInstrMap[MInst];
Vikram S. Advef5af6362002-07-08 23:15:32 +0000783
784 // We may need a scratch register to copy the spilled value to/from memory.
785 // This may itself have to insert code to free up a scratch register.
786 // Any such code should go before (after) the spill code for a load (store).
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000787 // The scratch reg is not marked as used because it is only used
788 // for the copy and not used across MInst.
Vikram S. Advef5af6362002-07-08 23:15:32 +0000789 int scratchRegType = -1;
790 int scratchReg = -1;
791 if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType))
792 {
Chris Lattner27a08932002-10-22 23:16:21 +0000793 scratchReg = getUsableUniRegAtMI(scratchRegType, &LVSetBef,
794 MInst, MIBef, MIAft);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000795 assert(scratchReg != MRI.getInvalidRegNum());
Vikram S. Advef5af6362002-07-08 23:15:32 +0000796 }
797
798 if (!isDef || isDefAndUse) {
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000799 // for a USE, we have to load the value of LR from stack to a TmpReg
800 // and use the TmpReg as one operand of instruction
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000801
Vikram S. Advef5af6362002-07-08 23:15:32 +0000802 // actual loading instruction(s)
Vikram S. Adve814030a2003-07-29 19:49:21 +0000803 MRI.cpMem2RegMI(AdIMid, MRI.getFramePointer(), SpillOff, TmpRegU,
804 RegType, scratchReg);
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000805
Vikram S. Advef5af6362002-07-08 23:15:32 +0000806 // the actual load should be after the instructions to free up TmpRegU
807 MIBef.insert(MIBef.end(), AdIMid.begin(), AdIMid.end());
808 AdIMid.clear();
809 }
810
Vikram S. Adve3bf08922003-07-10 19:42:55 +0000811 if (isDef || isDefAndUse) { // if this is a Def
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000812 // for a DEF, we have to store the value produced by this instruction
813 // on the stack position allocated for this LR
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000814
Vikram S. Advef5af6362002-07-08 23:15:32 +0000815 // actual storing instruction(s)
Vikram S. Adve814030a2003-07-29 19:49:21 +0000816 MRI.cpReg2MemMI(AdIMid, TmpRegU, MRI.getFramePointer(), SpillOff,
817 RegType, scratchReg);
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000818
Vikram S. Advef5af6362002-07-08 23:15:32 +0000819 MIAft.insert(MIAft.begin(), AdIMid.begin(), AdIMid.end());
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000820 } // if !DEF
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000821
Vikram S. Advef5af6362002-07-08 23:15:32 +0000822 // Finally, insert the entire spill code sequences before/after MInst
823 AI.InstrnsBefore.insert(AI.InstrnsBefore.end(), MIBef.begin(), MIBef.end());
824 AI.InstrnsAfter.insert(AI.InstrnsAfter.begin(), MIAft.begin(), MIAft.end());
825
Chris Lattner7e708292002-06-25 16:13:24 +0000826 if (DEBUG_RA) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000827 std::cerr << "\nFor Inst:\n " << *MInst;
828 std::cerr << "SPILLED LR# " << LR->getUserIGNode()->getIndex();
829 std::cerr << "; added Instructions:";
Anand Shuklad58290e2002-07-09 19:18:56 +0000830 for_each(MIBef.begin(), MIBef.end(), std::mem_fun(&MachineInstr::dump));
831 for_each(MIAft.begin(), MIAft.end(), std::mem_fun(&MachineInstr::dump));
Chris Lattner7e708292002-06-25 16:13:24 +0000832 }
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000833}
834
835
Vikram S. Adve814030a2003-07-29 19:49:21 +0000836
837//----------------------------------------------------------------------------
838// This method inserts caller saving/restoring instructons before/after
839// a call machine instruction. The caller saving/restoring instructions are
840// inserted like:
841// ** caller saving instructions
842// other instructions inserted for the call by ColorCallArg
843// CALL instruction
844// other instructions inserted for the call ColorCallArg
845// ** caller restoring instructions
846//----------------------------------------------------------------------------
847
848void
849PhyRegAlloc::insertCallerSavingCode(std::vector<MachineInstr*> &instrnsBefore,
850 std::vector<MachineInstr*> &instrnsAfter,
851 MachineInstr *CallMI,
852 const BasicBlock *BB)
853{
854 assert(TM.getInstrInfo().isCall(CallMI->getOpCode()));
855
856 // has set to record which registers were saved/restored
857 //
858 hash_set<unsigned> PushedRegSet;
859
860 CallArgsDescriptor* argDesc = CallArgsDescriptor::get(CallMI);
861
862 // if the call is to a instrumentation function, do not insert save and
863 // restore instructions the instrumentation function takes care of save
864 // restore for volatile regs.
865 //
866 // FIXME: this should be made general, not specific to the reoptimizer!
867 //
868 const Function *Callee = argDesc->getCallInst()->getCalledFunction();
869 bool isLLVMFirstTrigger = Callee && Callee->getName() == "llvm_first_trigger";
870
871 // Now check if the call has a return value (using argDesc) and if so,
872 // find the LR of the TmpInstruction representing the return value register.
873 // (using the last or second-last *implicit operand* of the call MI).
874 // Insert it to to the PushedRegSet since we must not save that register
875 // and restore it after the call.
876 // We do this because, we look at the LV set *after* the instruction
877 // to determine, which LRs must be saved across calls. The return value
878 // of the call is live in this set - but we must not save/restore it.
879 //
880 if (const Value *origRetVal = argDesc->getReturnValue()) {
881 unsigned retValRefNum = (CallMI->getNumImplicitRefs() -
882 (argDesc->getIndirectFuncPtr()? 1 : 2));
883 const TmpInstruction* tmpRetVal =
884 cast<TmpInstruction>(CallMI->getImplicitRef(retValRefNum));
885 assert(tmpRetVal->getOperand(0) == origRetVal &&
886 tmpRetVal->getType() == origRetVal->getType() &&
887 "Wrong implicit ref?");
888 LiveRange *RetValLR = LRI.getLiveRangeForValue(tmpRetVal);
889 assert(RetValLR && "No LR for RetValue of call");
890
891 if (! RetValLR->isMarkedForSpill())
892 PushedRegSet.insert(MRI.getUnifiedRegNum(RetValLR->getRegClassID(),
893 RetValLR->getColor()));
894 }
895
896 const ValueSet &LVSetAft = LVI->getLiveVarSetAfterMInst(CallMI, BB);
897 ValueSet::const_iterator LIt = LVSetAft.begin();
898
899 // for each live var in live variable set after machine inst
900 for( ; LIt != LVSetAft.end(); ++LIt) {
901
902 // get the live range corresponding to live var
903 LiveRange *const LR = LRI.getLiveRangeForValue(*LIt);
904
905 // LR can be null if it is a const since a const
906 // doesn't have a dominating def - see Assumptions above
907 if( LR ) {
908
909 if(! LR->isMarkedForSpill()) {
910
911 assert(LR->hasColor() && "LR is neither spilled nor colored?");
912 unsigned RCID = LR->getRegClassID();
913 unsigned Color = LR->getColor();
914
915 if (MRI.isRegVolatile(RCID, Color) ) {
916
917 //if the function is special LLVM function,
918 //And the register is not modified by call, don't save and restore
919 if (isLLVMFirstTrigger && !MRI.modifiedByCall(RCID, Color))
920 continue;
921
922 // if the value is in both LV sets (i.e., live before and after
923 // the call machine instruction)
924
925 unsigned Reg = MRI.getUnifiedRegNum(RCID, Color);
926
927 if( PushedRegSet.find(Reg) == PushedRegSet.end() ) {
928
929 // if we haven't already pushed that register
930
931 unsigned RegType = MRI.getRegTypeForLR(LR);
932
933 // Now get two instructions - to push on stack and pop from stack
934 // and add them to InstrnsBefore and InstrnsAfter of the
935 // call instruction
936 //
937 int StackOff =
938 MF.getInfo()->pushTempValue(MRI.getSpilledRegSize(RegType));
939
940 //---- Insert code for pushing the reg on stack ----------
941
942 std::vector<MachineInstr*> AdIBef, AdIAft;
943
944 // We may need a scratch register to copy the saved value
945 // to/from memory. This may itself have to insert code to
946 // free up a scratch register. Any such code should go before
947 // the save code. The scratch register, if any, is by default
948 // temporary and not "used" by the instruction unless the
949 // copy code itself decides to keep the value in the scratch reg.
950 int scratchRegType = -1;
951 int scratchReg = -1;
952 if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType))
953 { // Find a register not live in the LVSet before CallMI
954 const ValueSet &LVSetBef =
955 LVI->getLiveVarSetBeforeMInst(CallMI, BB);
956 scratchReg = getUsableUniRegAtMI(scratchRegType, &LVSetBef,
957 CallMI, AdIBef, AdIAft);
958 assert(scratchReg != MRI.getInvalidRegNum());
959 }
960
961 if (AdIBef.size() > 0)
962 instrnsBefore.insert(instrnsBefore.end(),
963 AdIBef.begin(), AdIBef.end());
964
965 MRI.cpReg2MemMI(instrnsBefore, Reg, MRI.getFramePointer(),
966 StackOff, RegType, scratchReg);
967
968 if (AdIAft.size() > 0)
969 instrnsBefore.insert(instrnsBefore.end(),
970 AdIAft.begin(), AdIAft.end());
971
972 //---- Insert code for popping the reg from the stack ----------
Vikram S. Adve814030a2003-07-29 19:49:21 +0000973 AdIBef.clear();
974 AdIAft.clear();
975
976 // We may need a scratch register to copy the saved value
977 // from memory. This may itself have to insert code to
978 // free up a scratch register. Any such code should go
979 // after the save code. As above, scratch is not marked "used".
980 //
981 scratchRegType = -1;
982 scratchReg = -1;
983 if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType))
984 { // Find a register not live in the LVSet after CallMI
985 scratchReg = getUsableUniRegAtMI(scratchRegType, &LVSetAft,
986 CallMI, AdIBef, AdIAft);
987 assert(scratchReg != MRI.getInvalidRegNum());
988 }
989
990 if (AdIBef.size() > 0)
991 instrnsAfter.insert(instrnsAfter.end(),
992 AdIBef.begin(), AdIBef.end());
993
994 MRI.cpMem2RegMI(instrnsAfter, MRI.getFramePointer(), StackOff,
995 Reg, RegType, scratchReg);
996
997 if (AdIAft.size() > 0)
998 instrnsAfter.insert(instrnsAfter.end(),
999 AdIAft.begin(), AdIAft.end());
1000
1001 PushedRegSet.insert(Reg);
1002
1003 if(DEBUG_RA) {
1004 std::cerr << "\nFor call inst:" << *CallMI;
1005 std::cerr << " -inserted caller saving instrs: Before:\n\t ";
1006 for_each(instrnsBefore.begin(), instrnsBefore.end(),
1007 std::mem_fun(&MachineInstr::dump));
1008 std::cerr << " -and After:\n\t ";
1009 for_each(instrnsAfter.begin(), instrnsAfter.end(),
1010 std::mem_fun(&MachineInstr::dump));
1011 }
1012 } // if not already pushed
1013
1014 } // if LR has a volatile color
1015
1016 } // if LR has color
1017
1018 } // if there is a LR for Var
1019
1020 } // for each value in the LV set after instruction
1021}
1022
1023
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001024//----------------------------------------------------------------------------
1025// We can use the following method to get a temporary register to be used
1026// BEFORE any given machine instruction. If there is a register available,
1027// this method will simply return that register and set MIBef = MIAft = NULL.
1028// Otherwise, it will return a register and MIAft and MIBef will contain
1029// two instructions used to free up this returned register.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +00001030// Returned register number is the UNIFIED register number
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001031//----------------------------------------------------------------------------
1032
Vikram S. Advef5af6362002-07-08 23:15:32 +00001033int PhyRegAlloc::getUsableUniRegAtMI(const int RegType,
1034 const ValueSet *LVSetBef,
1035 MachineInstr *MInst,
1036 std::vector<MachineInstr*>& MIBef,
1037 std::vector<MachineInstr*>& MIAft) {
1038
Chris Lattner133f0792002-10-28 04:45:29 +00001039 RegClass* RC = getRegClassByID(MRI.getRegClassIDOfRegType(RegType));
Vikram S. Advef5af6362002-07-08 23:15:32 +00001040
Vikram S. Advebc001b22003-07-25 21:06:09 +00001041 int RegU = getUnusedUniRegAtMI(RC, RegType, MInst, LVSetBef);
Vikram S. Advef5af6362002-07-08 23:15:32 +00001042
1043 if (RegU == -1) {
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +00001044 // we couldn't find an unused register. Generate code to free up a reg by
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001045 // saving it on stack and restoring after the instruction
Vikram S. Advef5af6362002-07-08 23:15:32 +00001046
Chris Lattnere90fcb72002-12-28 20:35:34 +00001047 int TmpOff = MF.getInfo()->pushTempValue(MRI.getSpilledRegSize(RegType));
Vikram S. Adve12af1642001-11-08 04:48:50 +00001048
Vikram S. Advebc001b22003-07-25 21:06:09 +00001049 RegU = getUniRegNotUsedByThisInst(RC, RegType, MInst);
Vikram S. Advedabb41d2002-05-19 15:29:31 +00001050
Vikram S. Advef5af6362002-07-08 23:15:32 +00001051 // Check if we need a scratch register to copy this register to memory.
1052 int scratchRegType = -1;
1053 if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType))
1054 {
Chris Lattner133f0792002-10-28 04:45:29 +00001055 int scratchReg = getUsableUniRegAtMI(scratchRegType, LVSetBef,
1056 MInst, MIBef, MIAft);
Vikram S. Advef5af6362002-07-08 23:15:32 +00001057 assert(scratchReg != MRI.getInvalidRegNum());
1058
1059 // We may as well hold the value in the scratch register instead
1060 // of copying it to memory and back. But we have to mark the
1061 // register as used by this instruction, so it does not get used
1062 // as a scratch reg. by another operand or anyone else.
Chris Lattner3fd1f5b2003-08-05 22:11:13 +00001063 ScratchRegsUsed.insert(std::make_pair(MInst, scratchReg));
Vikram S. Advef5af6362002-07-08 23:15:32 +00001064 MRI.cpReg2RegMI(MIBef, RegU, scratchReg, RegType);
1065 MRI.cpReg2RegMI(MIAft, scratchReg, RegU, RegType);
1066 }
1067 else
1068 { // the register can be copied directly to/from memory so do it.
1069 MRI.cpReg2MemMI(MIBef, RegU, MRI.getFramePointer(), TmpOff, RegType);
1070 MRI.cpMem2RegMI(MIAft, MRI.getFramePointer(), TmpOff, RegU, RegType);
1071 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001072 }
Vikram S. Advef5af6362002-07-08 23:15:32 +00001073
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001074 return RegU;
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001075}
1076
Vikram S. Adve814030a2003-07-29 19:49:21 +00001077
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001078//----------------------------------------------------------------------------
Vikram S. Adve814030a2003-07-29 19:49:21 +00001079// This method is called to get a new unused register that can be used
1080// to accomodate a temporary value. This method may be called several times
1081// for a single machine instruction. Each time it is called, it finds a
1082// register which is not live at that instruction and also which is not used
1083// by other spilled operands of the same instruction. Return register number
1084// is relative to the register class, NOT the unified number.
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001085//----------------------------------------------------------------------------
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001086
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001087int PhyRegAlloc::getUnusedUniRegAtMI(RegClass *RC,
Vikram S. Advebc001b22003-07-25 21:06:09 +00001088 const int RegType,
Vikram S. Adve814030a2003-07-29 19:49:21 +00001089 const MachineInstr *MInst,
1090 const ValueSet* LVSetBef) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001091
Vikram S. Advebc001b22003-07-25 21:06:09 +00001092 RC->clearColorsUsed(); // Reset array
Vikram S. Adve814030a2003-07-29 19:49:21 +00001093
1094 if (LVSetBef == NULL) {
1095 LVSetBef = &LVI->getLiveVarSetBeforeMInst(MInst);
1096 assert(LVSetBef != NULL && "Unable to get live-var set before MInst?");
1097 }
1098
Chris Lattner296b7732002-02-05 02:52:05 +00001099 ValueSet::const_iterator LIt = LVSetBef->begin();
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001100
1101 // for each live var in live variable set after machine inst
Chris Lattner7e708292002-06-25 16:13:24 +00001102 for ( ; LIt != LVSetBef->end(); ++LIt) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001103
Vikram S. Advebc001b22003-07-25 21:06:09 +00001104 // get the live range corresponding to live var, and its RegClass
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001105 LiveRange *const LRofLV = LRI.getLiveRangeForValue(*LIt );
1106
1107 // LR can be null if it is a const since a const
1108 // doesn't have a dominating def - see Assumptions above
Vikram S. Advebc001b22003-07-25 21:06:09 +00001109 if (LRofLV && LRofLV->getRegClass() == RC && LRofLV->hasColor())
1110 RC->markColorsUsed(LRofLV->getColor(),
1111 MRI.getRegTypeForLR(LRofLV), RegType);
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001112 }
1113
1114 // It is possible that one operand of this MInst was already spilled
1115 // and it received some register temporarily. If that's the case,
1116 // it is recorded in machine operand. We must skip such registers.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001117 //
Vikram S. Advebc001b22003-07-25 21:06:09 +00001118 setRelRegsUsedByThisInst(RC, RegType, MInst);
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001119
Vikram S. Advebc001b22003-07-25 21:06:09 +00001120 int unusedReg = RC->getUnusedColor(RegType); // find first unused color
1121 if (unusedReg >= 0)
1122 return MRI.getUnifiedRegNum(RC->getID(), unusedReg);
1123
Chris Lattner85c54652002-05-23 15:50:03 +00001124 return -1;
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001125}
1126
1127
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001128//----------------------------------------------------------------------------
1129// Get any other register in a register class, other than what is used
1130// by operands of a machine instruction. Returns the unified reg number.
1131//----------------------------------------------------------------------------
1132int PhyRegAlloc::getUniRegNotUsedByThisInst(RegClass *RC,
Vikram S. Advebc001b22003-07-25 21:06:09 +00001133 const int RegType,
Chris Lattner85c54652002-05-23 15:50:03 +00001134 const MachineInstr *MInst) {
Vikram S. Advebc001b22003-07-25 21:06:09 +00001135 RC->clearColorsUsed();
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001136
Vikram S. Advebc001b22003-07-25 21:06:09 +00001137 setRelRegsUsedByThisInst(RC, RegType, MInst);
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001138
Vikram S. Advebc001b22003-07-25 21:06:09 +00001139 // find the first unused color
1140 int unusedReg = RC->getUnusedColor(RegType);
1141 assert(unusedReg >= 0 &&
1142 "FATAL: No free register could be found in reg class!!");
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001143
Vikram S. Advebc001b22003-07-25 21:06:09 +00001144 return MRI.getUnifiedRegNum(RC->getID(), unusedReg);
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001145}
1146
1147
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001148//----------------------------------------------------------------------------
1149// This method modifies the IsColorUsedArr of the register class passed to it.
1150// It sets the bits corresponding to the registers used by this machine
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +00001151// instructions. Both explicit and implicit operands are set.
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001152//----------------------------------------------------------------------------
Vikram S. Advebc001b22003-07-25 21:06:09 +00001153
Chris Lattner3bed95b2003-08-05 21:55:58 +00001154static void markRegisterUsed(int RegNo, RegClass *RC, int RegType,
1155 const TargetRegInfo &TRI) {
1156 unsigned classId = 0;
1157 int classRegNum = TRI.getClassRegNum(RegNo, classId);
1158 if (RC->getID() == classId)
1159 RC->markColorsUsed(classRegNum, RegType, RegType);
1160}
1161
1162void PhyRegAlloc::setRelRegsUsedByThisInst(RegClass *RC, int RegType,
1163 const MachineInstr *MI)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001164{
Chris Lattner3bed95b2003-08-05 21:55:58 +00001165 assert(OperandsColoredMap[MI] == true &&
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001166 "Illegal to call setRelRegsUsedByThisInst() until colored operands "
1167 "are marked for an instruction.");
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001168
Chris Lattner3bed95b2003-08-05 21:55:58 +00001169 // Add the registers already marked as used by the instruction.
1170 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
1171 if (MI->getOperand(i).hasAllocatedReg())
1172 markRegisterUsed(MI->getOperand(i).getAllocatedRegNum(), RC, RegType,MRI);
1173
1174 for (unsigned i = 0, e = MI->getNumImplicitRefs(); i != e; ++i)
1175 if (MI->getImplicitOp(i).hasAllocatedReg())
1176 markRegisterUsed(MI->getImplicitOp(i).getAllocatedRegNum(), RC,
1177 RegType,MRI);
1178
Chris Lattner3fd1f5b2003-08-05 22:11:13 +00001179 // Add all of the scratch registers that are used to save values across the
1180 // instruction (e.g., for saving state register values).
1181 std::pair<ScratchRegsUsedTy::iterator, ScratchRegsUsedTy::iterator>
1182 IR = ScratchRegsUsed.equal_range(MI);
1183 for (ScratchRegsUsedTy::iterator I = IR.first; I != IR.second; ++I)
1184 markRegisterUsed(I->second, RC, RegType, MRI);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001185
Vikram S. Advef5af6362002-07-08 23:15:32 +00001186 // If there are implicit references, mark their allocated regs as well
1187 //
Chris Lattner3bed95b2003-08-05 21:55:58 +00001188 for (unsigned z=0; z < MI->getNumImplicitRefs(); z++)
Vikram S. Advef5af6362002-07-08 23:15:32 +00001189 if (const LiveRange*
Chris Lattner3bed95b2003-08-05 21:55:58 +00001190 LRofImpRef = LRI.getLiveRangeForValue(MI->getImplicitRef(z)))
Vikram S. Advef5af6362002-07-08 23:15:32 +00001191 if (LRofImpRef->hasColor())
1192 // this implicit reference is in a LR that received a color
Vikram S. Advebc001b22003-07-25 21:06:09 +00001193 RC->markColorsUsed(LRofImpRef->getColor(),
1194 MRI.getRegTypeForLR(LRofImpRef), RegType);
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001195}
1196
1197
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001198//----------------------------------------------------------------------------
Ruchira Sasanka251d8db2001-10-23 21:38:00 +00001199// If there are delay slots for an instruction, the instructions
1200// added after it must really go after the delayed instruction(s).
1201// So, we move the InstrAfter of that instruction to the
1202// corresponding delayed instruction using the following method.
Ruchira Sasanka251d8db2001-10-23 21:38:00 +00001203//----------------------------------------------------------------------------
Ruchira Sasanka251d8db2001-10-23 21:38:00 +00001204
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001205void PhyRegAlloc::move2DelayedInstr(const MachineInstr *OrigMI,
1206 const MachineInstr *DelayedMI)
1207{
Vikram S. Advefeb32982003-08-12 22:22:24 +00001208 // "added after" instructions of the original instr
1209 std::vector<MachineInstr *> &OrigAft = AddedInstrMap[OrigMI].InstrnsAfter;
1210
1211 if (DEBUG_RA && OrigAft.size() > 0) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +00001212 std::cerr << "\nRegAlloc: Moved InstrnsAfter for: " << *OrigMI;
1213 std::cerr << " to last delay slot instrn: " << *DelayedMI;
Vikram S. Adve814030a2003-07-29 19:49:21 +00001214 }
1215
Ruchira Sasanka251d8db2001-10-23 21:38:00 +00001216 // "added after" instructions of the delayed instr
Vikram S. Adve814030a2003-07-29 19:49:21 +00001217 std::vector<MachineInstr *> &DelayedAft=AddedInstrMap[DelayedMI].InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +00001218
1219 // go thru all the "added after instructions" of the original instruction
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001220 // and append them to the "added after instructions" of the delayed
Ruchira Sasanka251d8db2001-10-23 21:38:00 +00001221 // instructions
Chris Lattner697954c2002-01-20 22:54:45 +00001222 DelayedAft.insert(DelayedAft.end(), OrigAft.begin(), OrigAft.end());
Ruchira Sasanka251d8db2001-10-23 21:38:00 +00001223
1224 // empty the "added after instructions" of the original instruction
1225 OrigAft.clear();
Ruchira Sasanka251d8db2001-10-23 21:38:00 +00001226}
Ruchira Sasanka0931a012001-09-15 19:06:58 +00001227
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001228//----------------------------------------------------------------------------
1229// This method prints the code with registers after register allocation is
1230// complete.
1231//----------------------------------------------------------------------------
1232void PhyRegAlloc::printMachineCode()
1233{
1234
Chris Lattnerc083dcc2003-09-01 20:05:47 +00001235 std::cerr << "\n;************** Function " << Fn->getName()
Chris Lattner697954c2002-01-20 22:54:45 +00001236 << " *****************\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001237
Chris Lattnerf726e772002-10-28 19:22:04 +00001238 for (MachineFunction::iterator BBI = MF.begin(), BBE = MF.end();
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001239 BBI != BBE; ++BBI) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +00001240 std::cerr << "\n"; printLabel(BBI->getBasicBlock()); std::cerr << ": ";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001241
1242 // get the iterator for machine instructions
Chris Lattnerf726e772002-10-28 19:22:04 +00001243 MachineBasicBlock& MBB = *BBI;
1244 MachineBasicBlock::iterator MII = MBB.begin();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001245
1246 // iterate over all the machine instructions in BB
Chris Lattnerf726e772002-10-28 19:22:04 +00001247 for ( ; MII != MBB.end(); ++MII) {
Chris Lattnerd9512ca2002-10-29 17:35:39 +00001248 MachineInstr *MInst = *MII;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001249
Chris Lattnerc083dcc2003-09-01 20:05:47 +00001250 std::cerr << "\n\t";
1251 std::cerr << TM.getInstrInfo().getName(MInst->getOpCode());
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001252
Chris Lattner7e708292002-06-25 16:13:24 +00001253 for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001254 MachineOperand& Op = MInst->getOperand(OpNum);
1255
Chris Lattner133f0792002-10-28 04:45:29 +00001256 if (Op.getType() == MachineOperand::MO_VirtualRegister ||
1257 Op.getType() == MachineOperand::MO_CCRegister /*||
1258 Op.getType() == MachineOperand::MO_PCRelativeDisp*/ ) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001259
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001260 const Value *const Val = Op.getVRegValue () ;
Ruchira Sasankae727f852001-09-18 22:43:57 +00001261 // ****this code is temporary till NULL Values are fixed
Chris Lattner7e708292002-06-25 16:13:24 +00001262 if (! Val ) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +00001263 std::cerr << "\t<*NULL*>";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001264 continue;
1265 }
Ruchira Sasankae727f852001-09-18 22:43:57 +00001266
1267 // if a label or a constant
Chris Lattner7e708292002-06-25 16:13:24 +00001268 if (isa<BasicBlock>(Val)) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +00001269 std::cerr << "\t"; printLabel( Op.getVRegValue () );
Chris Lattner697954c2002-01-20 22:54:45 +00001270 } else {
Ruchira Sasankae727f852001-09-18 22:43:57 +00001271 // else it must be a register value
1272 const int RegNum = Op.getAllocatedRegNum();
1273
Chris Lattnerc083dcc2003-09-01 20:05:47 +00001274 std::cerr << "\t" << "%" << MRI.getUnifiedRegName( RegNum );
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001275 if (Val->hasName() )
Chris Lattnerc083dcc2003-09-01 20:05:47 +00001276 std::cerr << "(" << Val->getName() << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001277 else
Chris Lattnerc083dcc2003-09-01 20:05:47 +00001278 std::cerr << "(" << Val << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001279
Vikram S. Adve5f2180c2003-05-27 00:05:23 +00001280 if (Op.opIsDefOnly() || Op.opIsDefAndUse())
Chris Lattnerc083dcc2003-09-01 20:05:47 +00001281 std::cerr << "*";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001282
1283 const LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
Chris Lattner7e708292002-06-25 16:13:24 +00001284 if (LROfVal )
1285 if (LROfVal->hasSpillOffset() )
Chris Lattnerc083dcc2003-09-01 20:05:47 +00001286 std::cerr << "$";
Ruchira Sasankae727f852001-09-18 22:43:57 +00001287 }
1288
1289 }
Chris Lattner133f0792002-10-28 04:45:29 +00001290 else if (Op.getType() == MachineOperand::MO_MachineRegister) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +00001291 std::cerr << "\t%" << MRI.getUnifiedRegName(Op.getMachineRegNum());
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001292 }
1293
1294 else
Chris Lattnerc083dcc2003-09-01 20:05:47 +00001295 std::cerr << "\t" << Op; // use dump field
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001296 }
1297
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001298
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001299
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001300 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
Chris Lattner7e708292002-06-25 16:13:24 +00001301 if (NumOfImpRefs > 0) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +00001302 std::cerr << "\tImplicit:";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001303
Chris Lattner7e708292002-06-25 16:13:24 +00001304 for (unsigned z=0; z < NumOfImpRefs; z++)
Chris Lattnerc083dcc2003-09-01 20:05:47 +00001305 std::cerr << RAV(MInst->getImplicitRef(z)) << "\t";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001306 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001307
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001308 } // for all machine instructions
1309
Chris Lattnerc083dcc2003-09-01 20:05:47 +00001310 std::cerr << "\n";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001311
1312 } // for all BBs
1313
Chris Lattnerc083dcc2003-09-01 20:05:47 +00001314 std::cerr << "\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001315}
1316
Ruchira Sasankae727f852001-09-18 22:43:57 +00001317
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001318//----------------------------------------------------------------------------
1319
1320//----------------------------------------------------------------------------
1321void PhyRegAlloc::colorIncomingArgs()
1322{
Vikram S. Adve814030a2003-07-29 19:49:21 +00001323 MRI.colorMethodArgs(Fn, LRI, AddedInstrAtEntry.InstrnsBefore,
1324 AddedInstrAtEntry.InstrnsAfter);
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001325}
1326
Ruchira Sasankae727f852001-09-18 22:43:57 +00001327
1328//----------------------------------------------------------------------------
1329// Used to generate a label for a basic block
1330//----------------------------------------------------------------------------
Chris Lattnerf726e772002-10-28 19:22:04 +00001331void PhyRegAlloc::printLabel(const Value *Val) {
Chris Lattner697954c2002-01-20 22:54:45 +00001332 if (Val->hasName())
Chris Lattnerc083dcc2003-09-01 20:05:47 +00001333 std::cerr << Val->getName();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001334 else
Chris Lattnerc083dcc2003-09-01 20:05:47 +00001335 std::cerr << "Label" << Val;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001336}
1337
1338
Ruchira Sasankae727f852001-09-18 22:43:57 +00001339//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001340// This method calls setSugColorUsable method of each live range. This
1341// will determine whether the suggested color of LR is really usable.
1342// A suggested color is not usable when the suggested color is volatile
1343// AND when there are call interferences
1344//----------------------------------------------------------------------------
1345
1346void PhyRegAlloc::markUnusableSugColors()
1347{
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001348 // hash map iterator
1349 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
1350 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
1351
Chris Lattner7e708292002-06-25 16:13:24 +00001352 for (; HMI != HMIEnd ; ++HMI ) {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001353 if (HMI->first) {
1354 LiveRange *L = HMI->second; // get the LiveRange
1355 if (L) {
Chris Lattner7e708292002-06-25 16:13:24 +00001356 if (L->hasSuggestedColor()) {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001357 int RCID = L->getRegClass()->getID();
Chris Lattner7e708292002-06-25 16:13:24 +00001358 if (MRI.isRegVolatile( RCID, L->getSuggestedColor()) &&
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001359 L->isCallInterference() )
1360 L->setSuggestedColorUsable( false );
1361 else
1362 L->setSuggestedColorUsable( true );
1363 }
1364 } // if L->hasSuggestedColor()
1365 }
1366 } // for all LR's in hash map
1367}
1368
1369
1370
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001371//----------------------------------------------------------------------------
1372// The following method will set the stack offsets of the live ranges that
1373// are decided to be spillled. This must be called just after coloring the
1374// LRs using the graph coloring algo. For each live range that is spilled,
1375// this method allocate a new spill position on the stack.
1376//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001377
Chris Lattner37730942002-02-05 03:52:29 +00001378void PhyRegAlloc::allocateStackSpace4SpilledLRs() {
Chris Lattnerc083dcc2003-09-01 20:05:47 +00001379 if (DEBUG_RA) std::cerr << "\nSetting LR stack offsets for spills...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001380
Chris Lattner37730942002-02-05 03:52:29 +00001381 LiveRangeMapType::const_iterator HMI = LRI.getLiveRangeMap()->begin();
1382 LiveRangeMapType::const_iterator HMIEnd = LRI.getLiveRangeMap()->end();
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001383
Chris Lattner7e708292002-06-25 16:13:24 +00001384 for ( ; HMI != HMIEnd ; ++HMI) {
Chris Lattner37730942002-02-05 03:52:29 +00001385 if (HMI->first && HMI->second) {
Vikram S. Adve3bf08922003-07-10 19:42:55 +00001386 LiveRange *L = HMI->second; // get the LiveRange
1387 if (L->isMarkedForSpill()) { // NOTE: allocating size of long Type **
Chris Lattnere90fcb72002-12-28 20:35:34 +00001388 int stackOffset = MF.getInfo()->allocateSpilledValue(Type::LongTy);
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001389 L->setSpillOffFromFP(stackOffset);
1390 if (DEBUG_RA)
Chris Lattnerc083dcc2003-09-01 20:05:47 +00001391 std::cerr << " LR# " << L->getUserIGNode()->getIndex()
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001392 << ": stack-offset = " << stackOffset << "\n";
1393 }
Chris Lattner37730942002-02-05 03:52:29 +00001394 }
1395 } // for all LR's in hash map
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001396}
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001397
1398
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001399//----------------------------------------------------------------------------
Ruchira Sasankae727f852001-09-18 22:43:57 +00001400// The entry pont to Register Allocation
1401//----------------------------------------------------------------------------
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001402
1403void PhyRegAlloc::allocateRegisters()
1404{
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001405
1406 // make sure that we put all register classes into the RegClassList
1407 // before we call constructLiveRanges (now done in the constructor of
1408 // PhyRegAlloc class).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001409 //
1410 LRI.constructLiveRanges(); // create LR info
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001411
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001412 if (DEBUG_RA >= RA_DEBUG_LiveRanges)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001413 LRI.printLiveRanges();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001414
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001415 createIGNodeListsAndIGs(); // create IGNode list and IGs
1416
1417 buildInterferenceGraphs(); // build IGs in all reg classes
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001418
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +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 Lattner7e708292002-06-25 16:13:24 +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 Lattner7e708292002-06-25 16:13:24 +00001426 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
1427 RegClassList[rc]->printIG();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001428 }
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001429
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001430 LRI.coalesceLRs(); // coalesce all live ranges
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +00001431
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001432 if (DEBUG_RA >= RA_DEBUG_LiveRanges) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001433 // print all LRs in all reg classes
Chris Lattnerf726e772002-10-28 19:22:04 +00001434 for (unsigned rc=0; rc < NumOfRegClasses; rc++)
1435 RegClassList[rc]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001436
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001437 // print IGs in all register classes
Chris Lattnerf726e772002-10-28 19:22:04 +00001438 for (unsigned rc=0; rc < NumOfRegClasses; rc++)
1439 RegClassList[rc]->printIG();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001440 }
1441
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001442
1443 // mark un-usable suggested color before graph coloring algorithm.
1444 // When this is done, the graph coloring algo will not reserve
1445 // suggested color unnecessarily - they can be used by another LR
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001446 //
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001447 markUnusableSugColors();
1448
1449 // color all register classes using the graph coloring algo
Chris Lattner7e708292002-06-25 16:13:24 +00001450 for (unsigned rc=0; rc < NumOfRegClasses ; rc++)
Chris Lattnerf726e772002-10-28 19:22:04 +00001451 RegClassList[rc]->colorAllRegs();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001452
Chris Lattnere90fcb72002-12-28 20:35:34 +00001453 // Atter graph coloring, if some LRs did not receive a color (i.e, spilled)
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001454 // a poistion for such spilled LRs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001455 //
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001456 allocateStackSpace4SpilledLRs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001457
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001458 // Reset the temp. area on the stack before use by the first instruction.
1459 // This will also happen after updating each instruction.
1460 MF.getInfo()->popAllTempValues();
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001461
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001462 // color incoming args - if the correct color was not received
1463 // insert code to copy to the correct register
1464 //
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001465 colorIncomingArgs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001466
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001467 // Now update the machine code with register names and add any
1468 // additional code inserted by the register allocator to the instruction
1469 // stream
1470 //
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001471 updateMachineCode();
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001472
Chris Lattner045e7c82001-09-19 16:26:23 +00001473 if (DEBUG_RA) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +00001474 std::cerr << "\n**** Machine Code After Register Allocation:\n\n";
Chris Lattnerf726e772002-10-28 19:22:04 +00001475 MF.dump();
Chris Lattner045e7c82001-09-19 16:26:23 +00001476 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001477}
1478
Ruchira Sasankae727f852001-09-18 22:43:57 +00001479
1480