blob: e70c90b039426c05027c731a443513bb620416b7 [file] [log] [blame]
Chris Lattner179cdfb2002-08-09 20:08:03 +00001//===-- PhyRegAlloc.cpp ---------------------------------------------------===//
Vikram S. Adve12af1642001-11-08 04:48:50 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Brian Gaeke222bd532003-09-24 18:16:23 +000010// Traditional graph-coloring global register allocator currently used
11// by the SPARC back-end.
12//
13// NOTE: This register allocator has some special support
14// for the Reoptimizer, such as not saving some registers on calls to
15// the first-level instrumentation function.
16//
17// NOTE 2: This register allocator can save its state in a global
18// variable in the module it's working on. This feature is not
19// thread-safe; if you have doubts, leave it turned off.
Chris Lattner179cdfb2002-08-09 20:08:03 +000020//
21//===----------------------------------------------------------------------===//
Ruchira Sasanka8e604792001-09-14 21:18:34 +000022
Brian Gaeke537132b2003-10-23 20:32:55 +000023#include "AllocInfo.h"
Misha Brukman396c8c32003-10-23 18:06:27 +000024#include "IGNode.h"
Chris Lattner70b2f562003-09-01 20:09:04 +000025#include "PhyRegAlloc.h"
Chris Lattner4309e732003-01-15 19:57:07 +000026#include "RegAllocCommon.h"
Chris Lattner9d4ed152003-01-15 21:14:01 +000027#include "RegClass.h"
Misha Brukman396c8c32003-10-23 18:06:27 +000028#include "llvm/Constants.h"
29#include "llvm/DerivedTypes.h"
30#include "llvm/iOther.h"
31#include "llvm/Module.h"
32#include "llvm/Type.h"
33#include "llvm/Analysis/LoopInfo.h"
Chris Lattner797c1362003-09-30 20:13:59 +000034#include "llvm/CodeGen/FunctionLiveVarInfo.h"
35#include "llvm/CodeGen/InstrSelection.h"
Misha Brukman396c8c32003-10-23 18:06:27 +000036#include "llvm/CodeGen/MachineFunction.h"
37#include "llvm/CodeGen/MachineFunctionInfo.h"
Brian Gaeke874f4232003-09-21 02:50:21 +000038#include "llvm/CodeGen/MachineInstr.h"
Chris Lattnerf6ee49f2003-01-15 18:08:07 +000039#include "llvm/CodeGen/MachineInstrBuilder.h"
Vikram S. Advedabb41d2002-05-19 15:29:31 +000040#include "llvm/CodeGen/MachineInstrAnnot.h"
Chris Lattner797c1362003-09-30 20:13:59 +000041#include "llvm/CodeGen/Passes.h"
Chris Lattner797c1362003-09-30 20:13:59 +000042#include "llvm/Support/InstIterator.h"
Misha Brukman396c8c32003-10-23 18:06:27 +000043#include "llvm/Target/TargetInstrInfo.h"
Chris Lattner4bc23482002-09-15 07:07:55 +000044#include "Support/CommandLine.h"
Misha Brukman396c8c32003-10-23 18:06:27 +000045#include "Support/SetOperations.h"
46#include "Support/STLExtras.h"
Brian Gaekebd353fb2003-09-21 03:57:37 +000047#include <cmath>
Vikram S. Adve12af1642001-11-08 04:48:50 +000048
Chris Lattner70e60cb2002-05-22 17:08:27 +000049RegAllocDebugLevel_t DEBUG_RA;
Vikram S. Adve39c94e12002-09-14 23:05:33 +000050
Brian Gaeke8fc49342003-10-24 21:21:58 +000051/// The reoptimizer wants to be able to grovel through the register
52/// allocator's state after it has done its job. This is a hack.
53///
54PhyRegAlloc::SavedStateMapTy ExportedFnAllocState;
55const bool SaveStateToModule = false;
56
Chris Lattner5ff62e92002-07-22 02:10:13 +000057static cl::opt<RegAllocDebugLevel_t, true>
58DRA_opt("dregalloc", cl::Hidden, cl::location(DEBUG_RA),
59 cl::desc("enable register allocation debugging information"),
60 cl::values(
Vikram S. Adve39c94e12002-09-14 23:05:33 +000061 clEnumValN(RA_DEBUG_None , "n", "disable debug output"),
62 clEnumValN(RA_DEBUG_Results, "y", "debug output for allocation results"),
63 clEnumValN(RA_DEBUG_Coloring, "c", "debug output for graph coloring step"),
64 clEnumValN(RA_DEBUG_Interference,"ig","debug output for interference graphs"),
65 clEnumValN(RA_DEBUG_LiveRanges , "lr","debug output for live ranges"),
66 clEnumValN(RA_DEBUG_Verbose, "v", "extra debug output"),
Chris Lattner5ff62e92002-07-22 02:10:13 +000067 0));
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000068
Brian Gaeke59b1c562003-09-24 17:50:28 +000069static cl::opt<bool>
70SaveRegAllocState("save-ra-state", cl::Hidden,
71 cl::desc("write reg. allocator state into module"));
72
Brian Gaekebf3c4cf2003-08-14 06:09:32 +000073FunctionPass *getRegisterAllocator(TargetMachine &T) {
Brian Gaeke4efe3422003-09-21 01:23:46 +000074 return new PhyRegAlloc (T);
Chris Lattner2f9b28e2002-02-04 15:54:09 +000075}
Chris Lattner6dd98a62002-02-04 00:33:08 +000076
Chris Lattner8474f6f2003-09-23 15:13:04 +000077void PhyRegAlloc::getAnalysisUsage(AnalysisUsage &AU) const {
78 AU.addRequired<LoopInfo> ();
79 AU.addRequired<FunctionLiveVarInfo> ();
80}
81
82
Brian Gaekeaf843702003-10-22 20:22:53 +000083/// Initialize interference graphs (one in each reg class) and IGNodeLists
84/// (one in each IG). The actual nodes will be pushed later.
85///
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000086void PhyRegAlloc::createIGNodeListsAndIGs() {
Chris Lattnerc083dcc2003-09-01 20:05:47 +000087 if (DEBUG_RA >= RA_DEBUG_LiveRanges) std::cerr << "Creating LR lists ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +000088
Brian Gaeke4efe3422003-09-21 01:23:46 +000089 LiveRangeMapType::const_iterator HMI = LRI->getLiveRangeMap()->begin();
Brian Gaeke4efe3422003-09-21 01:23:46 +000090 LiveRangeMapType::const_iterator HMIEnd = LRI->getLiveRangeMap()->end();
Ruchira Sasanka8e604792001-09-14 21:18:34 +000091
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000092 for (; HMI != HMIEnd ; ++HMI ) {
93 if (HMI->first) {
94 LiveRange *L = HMI->second; // get the LiveRange
95 if (!L) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +000096 if (DEBUG_RA)
Chris Lattnerc083dcc2003-09-01 20:05:47 +000097 std::cerr << "\n**** ?!?WARNING: NULL LIVE RANGE FOUND FOR: "
Vikram S. Adve39c94e12002-09-14 23:05:33 +000098 << RAV(HMI->first) << "****\n";
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000099 continue;
100 }
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000101
102 // if the Value * is not null, and LR is not yet written to the IGNodeList
Chris Lattner7e708292002-06-25 16:13:24 +0000103 if (!(L->getUserIGNode()) ) {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000104 RegClass *const RC = // RegClass of first value in the LR
Brian Gaeke59b1c562003-09-24 17:50:28 +0000105 RegClassList[ L->getRegClassID() ];
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000106 RC->addLRToIG(L); // add this LR to an IG
107 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000108 }
109 }
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000110
111 // init RegClassList
Chris Lattner7e708292002-06-25 16:13:24 +0000112 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000113 RegClassList[rc]->createInterferenceGraph();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000114
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000115 if (DEBUG_RA >= RA_DEBUG_LiveRanges) std::cerr << "LRLists Created!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000116}
117
118
Brian Gaekeaf843702003-10-22 20:22:53 +0000119/// Add all interferences for a given instruction. Interference occurs only
120/// if the LR of Def (Inst or Arg) is of the same reg class as that of live
121/// var. The live var passed to this function is the LVset AFTER the
122/// instruction.
123///
124void PhyRegAlloc::addInterference(const Value *Def, const ValueSet *LVSet,
Chris Lattner296b7732002-02-05 02:52:05 +0000125 bool isCallInst) {
Chris Lattner296b7732002-02-05 02:52:05 +0000126 ValueSet::const_iterator LIt = LVSet->begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000127
128 // get the live range of instruction
Brian Gaeke4efe3422003-09-21 01:23:46 +0000129 const LiveRange *const LROfDef = LRI->getLiveRangeForValue( Def );
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000130
131 IGNode *const IGNodeOfDef = LROfDef->getUserIGNode();
132 assert( IGNodeOfDef );
133
134 RegClass *const RCOfDef = LROfDef->getRegClass();
135
136 // for each live var in live variable set
Chris Lattner7e708292002-06-25 16:13:24 +0000137 for ( ; LIt != LVSet->end(); ++LIt) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000138
Vikram S. Advef5af6362002-07-08 23:15:32 +0000139 if (DEBUG_RA >= RA_DEBUG_Verbose)
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000140 std::cerr << "< Def=" << RAV(Def) << ", Lvar=" << RAV(*LIt) << "> ";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000141
142 // get the live range corresponding to live var
Brian Gaeke4efe3422003-09-21 01:23:46 +0000143 LiveRange *LROfVar = LRI->getLiveRangeForValue(*LIt);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000144
145 // LROfVar can be null if it is a const since a const
146 // doesn't have a dominating def - see Assumptions above
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000147 if (LROfVar)
148 if (LROfDef != LROfVar) // do not set interf for same LR
149 if (RCOfDef == LROfVar->getRegClass()) // 2 reg classes are the same
150 RCOfDef->setInterference( LROfDef, LROfVar);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000151 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000152}
153
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000154
Brian Gaekeaf843702003-10-22 20:22:53 +0000155/// For a call instruction, this method sets the CallInterference flag in
156/// the LR of each variable live in the Live Variable Set live after the
157/// call instruction (except the return value of the call instruction - since
158/// the return value does not interfere with that call itself).
159///
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000160void PhyRegAlloc::setCallInterferences(const MachineInstr *MInst,
Chris Lattner296b7732002-02-05 02:52:05 +0000161 const ValueSet *LVSetAft) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000162 if (DEBUG_RA >= RA_DEBUG_Interference)
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000163 std::cerr << "\n For call inst: " << *MInst;
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000164
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000165 // for each live var in live variable set after machine inst
Vikram S. Adve65b2f402003-07-02 01:24:00 +0000166 for (ValueSet::const_iterator LIt = LVSetAft->begin(), LEnd = LVSetAft->end();
167 LIt != LEnd; ++LIt) {
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000168
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000169 // get the live range corresponding to live var
Brian Gaeke4efe3422003-09-21 01:23:46 +0000170 LiveRange *const LR = LRI->getLiveRangeForValue(*LIt );
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000171
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000172 // LR can be null if it is a const since a const
173 // doesn't have a dominating def - see Assumptions above
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000174 if (LR ) {
175 if (DEBUG_RA >= RA_DEBUG_Interference) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000176 std::cerr << "\n\tLR after Call: ";
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000177 printSet(*LR);
178 }
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000179 LR->setCallInterference();
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000180 if (DEBUG_RA >= RA_DEBUG_Interference) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000181 std::cerr << "\n ++After adding call interference for LR: " ;
Chris Lattner296b7732002-02-05 02:52:05 +0000182 printSet(*LR);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000183 }
184 }
185
186 }
187
Vikram S. Adve1a53f032002-03-31 18:54:37 +0000188 // Now find the LR of the return value of the call
189 // We do this because, we look at the LV set *after* the instruction
190 // to determine, which LRs must be saved across calls. The return value
191 // of the call is live in this set - but it does not interfere with call
192 // (i.e., we can allocate a volatile register to the return value)
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000193 CallArgsDescriptor* argDesc = CallArgsDescriptor::get(MInst);
194
195 if (const Value *RetVal = argDesc->getReturnValue()) {
Brian Gaeke4efe3422003-09-21 01:23:46 +0000196 LiveRange *RetValLR = LRI->getLiveRangeForValue( RetVal );
Vikram S. Adve1a53f032002-03-31 18:54:37 +0000197 assert( RetValLR && "No LR for RetValue of call");
198 RetValLR->clearCallInterference();
199 }
200
201 // If the CALL is an indirect call, find the LR of the function pointer.
202 // That has a call interference because it conflicts with outgoing args.
Chris Lattner7e708292002-06-25 16:13:24 +0000203 if (const Value *AddrVal = argDesc->getIndirectFuncPtr()) {
Brian Gaeke4efe3422003-09-21 01:23:46 +0000204 LiveRange *AddrValLR = LRI->getLiveRangeForValue( AddrVal );
Vikram S. Adve1a53f032002-03-31 18:54:37 +0000205 assert( AddrValLR && "No LR for indirect addr val of call");
206 AddrValLR->setCallInterference();
207 }
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000208}
209
210
Brian Gaekeaf843702003-10-22 20:22:53 +0000211/// Create interferences in the IG of each RegClass, and calculate the spill
212/// cost of each Live Range (it is done in this method to save another pass
213/// over the code).
214///
215void PhyRegAlloc::buildInterferenceGraphs() {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000216 if (DEBUG_RA >= RA_DEBUG_Interference)
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000217 std::cerr << "Creating interference graphs ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000218
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000219 unsigned BBLoopDepthCost;
Brian Gaeke4efe3422003-09-21 01:23:46 +0000220 for (MachineFunction::iterator BBI = MF->begin(), BBE = MF->end();
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000221 BBI != BBE; ++BBI) {
Chris Lattnerf726e772002-10-28 19:22:04 +0000222 const MachineBasicBlock &MBB = *BBI;
223 const BasicBlock *BB = MBB.getBasicBlock();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000224
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000225 // find the 10^(loop_depth) of this BB
Chris Lattnerf726e772002-10-28 19:22:04 +0000226 BBLoopDepthCost = (unsigned)pow(10.0, LoopDepthCalc->getLoopDepth(BB));
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000227
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000228 // get the iterator for machine instructions
Chris Lattnerf726e772002-10-28 19:22:04 +0000229 MachineBasicBlock::const_iterator MII = MBB.begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000230
231 // iterate over all the machine instructions in BB
Chris Lattnerf726e772002-10-28 19:22:04 +0000232 for ( ; MII != MBB.end(); ++MII) {
233 const MachineInstr *MInst = *MII;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000234
235 // get the LV set after the instruction
Chris Lattnerf726e772002-10-28 19:22:04 +0000236 const ValueSet &LVSetAI = LVI->getLiveVarSetAfterMInst(MInst, BB);
237 bool isCallInst = TM.getInstrInfo().isCall(MInst->getOpCode());
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000238
Brian Gaekeaf843702003-10-22 20:22:53 +0000239 if (isCallInst) {
Misha Brukman37f92e22003-09-11 22:34:13 +0000240 // set the isCallInterference flag of each live range which extends
241 // across this call instruction. This information is used by graph
242 // coloring algorithm to avoid allocating volatile colors to live ranges
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000243 // that span across calls (since they have to be saved/restored)
Chris Lattner748697d2002-02-05 04:20:12 +0000244 setCallInterferences(MInst, &LVSetAI);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000245 }
246
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000247 // iterate over all MI operands to find defs
Chris Lattner2f898d22002-02-05 06:02:59 +0000248 for (MachineInstr::const_val_op_iterator OpI = MInst->begin(),
249 OpE = MInst->end(); OpI != OpE; ++OpI) {
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000250 if (OpI.isDefOnly() || OpI.isDefAndUse()) // create a new LR since def
Chris Lattner748697d2002-02-05 04:20:12 +0000251 addInterference(*OpI, &LVSetAI, isCallInst);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000252
253 // Calculate the spill cost of each live range
Brian Gaeke4efe3422003-09-21 01:23:46 +0000254 LiveRange *LR = LRI->getLiveRangeForValue(*OpI);
Chris Lattner2f898d22002-02-05 06:02:59 +0000255 if (LR) LR->addSpillCost(BBLoopDepthCost);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000256 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000257
Brian Gaekeaf843702003-10-22 20:22:53 +0000258 // Mark all operands of pseudo-instructions as interfering with one
259 // another. This must be done because pseudo-instructions may be
260 // expanded to multiple instructions by the assembler, so all the
261 // operands must get distinct registers.
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000262 if (TM.getInstrInfo().isPseudoInstr(MInst->getOpCode()))
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000263 addInterf4PseudoInstr(MInst);
264
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000265 // Also add interference for any implicit definitions in a machine
266 // instr (currently, only calls have this).
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000267 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000268 for (unsigned z=0; z < NumOfImpRefs; z++)
269 if (MInst->getImplicitOp(z).opIsDefOnly() ||
270 MInst->getImplicitOp(z).opIsDefAndUse())
271 addInterference( MInst->getImplicitRef(z), &LVSetAI, isCallInst );
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +0000272
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000273 } // for all machine instructions in BB
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000274 } // for all BBs in function
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000275
Misha Brukman37f92e22003-09-11 22:34:13 +0000276 // add interferences for function arguments. Since there are no explicit
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000277 // defs in the function for args, we have to add them manually
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000278 addInterferencesForArgs();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000279
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000280 if (DEBUG_RA >= RA_DEBUG_Interference)
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000281 std::cerr << "Interference graphs calculated!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000282}
283
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000284
Brian Gaekeaf843702003-10-22 20:22:53 +0000285/// Mark all operands of the given MachineInstr as interfering with one
286/// another.
287///
Brian Gaeke43ce8fe2003-09-21 02:24:09 +0000288void PhyRegAlloc::addInterf4PseudoInstr(const MachineInstr *MInst) {
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000289 bool setInterf = false;
290
Brian Gaeke43ce8fe2003-09-21 02:24:09 +0000291 // iterate over MI operands to find defs
Chris Lattner2f898d22002-02-05 06:02:59 +0000292 for (MachineInstr::const_val_op_iterator It1 = MInst->begin(),
293 ItE = MInst->end(); It1 != ItE; ++It1) {
Brian Gaeke4efe3422003-09-21 01:23:46 +0000294 const LiveRange *LROfOp1 = LRI->getLiveRangeForValue(*It1);
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000295 assert((LROfOp1 || !It1.isUseOnly())&&"No LR for Def in PSEUDO insruction");
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000296
Chris Lattner2f898d22002-02-05 06:02:59 +0000297 MachineInstr::const_val_op_iterator It2 = It1;
Chris Lattner7e708292002-06-25 16:13:24 +0000298 for (++It2; It2 != ItE; ++It2) {
Brian Gaeke4efe3422003-09-21 01:23:46 +0000299 const LiveRange *LROfOp2 = LRI->getLiveRangeForValue(*It2);
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000300
Chris Lattner2f898d22002-02-05 06:02:59 +0000301 if (LROfOp2) {
302 RegClass *RCOfOp1 = LROfOp1->getRegClass();
303 RegClass *RCOfOp2 = LROfOp2->getRegClass();
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000304
Chris Lattner7e708292002-06-25 16:13:24 +0000305 if (RCOfOp1 == RCOfOp2 ){
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000306 RCOfOp1->setInterference( LROfOp1, LROfOp2 );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000307 setInterf = true;
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000308 }
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000309 } // if Op2 has a LR
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000310 } // for all other defs in machine instr
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000311 } // for all operands in an instruction
312
Chris Lattner2f898d22002-02-05 06:02:59 +0000313 if (!setInterf && MInst->getNumOperands() > 2) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000314 std::cerr << "\nInterf not set for any operand in pseudo instr:\n";
315 std::cerr << *MInst;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000316 assert(0 && "Interf not set for pseudo instr with > 2 operands" );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000317 }
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000318}
319
320
Brian Gaekeaf843702003-10-22 20:22:53 +0000321/// Add interferences for incoming arguments to a function.
322///
Chris Lattner296b7732002-02-05 02:52:05 +0000323void PhyRegAlloc::addInterferencesForArgs() {
324 // get the InSet of root BB
Chris Lattnerf726e772002-10-28 19:22:04 +0000325 const ValueSet &InSet = LVI->getInSetOfBB(&Fn->front());
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000326
Chris Lattnerf726e772002-10-28 19:22:04 +0000327 for (Function::const_aiterator AI = Fn->abegin(); AI != Fn->aend(); ++AI) {
Chris Lattner7e708292002-06-25 16:13:24 +0000328 // add interferences between args and LVars at start
329 addInterference(AI, &InSet, false);
330
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000331 if (DEBUG_RA >= RA_DEBUG_Interference)
Brian Gaekeaf843702003-10-22 20:22:53 +0000332 std::cerr << " - %% adding interference for argument " << RAV(AI) << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000333 }
334}
335
336
Brian Gaekeaf843702003-10-22 20:22:53 +0000337/// The following are utility functions used solely by updateMachineCode and
338/// the functions that it calls. They should probably be folded back into
339/// updateMachineCode at some point.
340///
Vikram S. Adve48762092002-04-25 04:34:15 +0000341
Brian Gaekeaf843702003-10-22 20:22:53 +0000342// used by: updateMachineCode (1 time), PrependInstructions (1 time)
343inline void InsertBefore(MachineInstr* newMI, MachineBasicBlock& MBB,
344 MachineBasicBlock::iterator& MII) {
Chris Lattnerf726e772002-10-28 19:22:04 +0000345 MII = MBB.insert(MII, newMI);
Vikram S. Advecb202e32002-10-11 16:12:40 +0000346 ++MII;
347}
348
Brian Gaekeaf843702003-10-22 20:22:53 +0000349// used by: AppendInstructions (1 time)
350inline void InsertAfter(MachineInstr* newMI, MachineBasicBlock& MBB,
351 MachineBasicBlock::iterator& MII) {
Vikram S. Advecb202e32002-10-11 16:12:40 +0000352 ++MII; // insert before the next instruction
Chris Lattnerf726e772002-10-28 19:22:04 +0000353 MII = MBB.insert(MII, newMI);
Vikram S. Advecb202e32002-10-11 16:12:40 +0000354}
355
Brian Gaekeaf843702003-10-22 20:22:53 +0000356// used by: updateMachineCode (1 time)
357inline void DeleteInstruction(MachineBasicBlock& MBB,
358 MachineBasicBlock::iterator& MII) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000359 MII = MBB.erase(MII);
360}
361
Brian Gaekeaf843702003-10-22 20:22:53 +0000362// used by: updateMachineCode (1 time)
363inline void SubstituteInPlace(MachineInstr* newMI, MachineBasicBlock& MBB,
364 MachineBasicBlock::iterator MII) {
Vikram S. Advecb202e32002-10-11 16:12:40 +0000365 *MII = newMI;
366}
367
Brian Gaekeaf843702003-10-22 20:22:53 +0000368// used by: updateMachineCode (2 times)
369inline void PrependInstructions(std::vector<MachineInstr *> &IBef,
370 MachineBasicBlock& MBB,
371 MachineBasicBlock::iterator& MII,
372 const std::string& msg) {
373 if (!IBef.empty()) {
Vikram S. Adve48762092002-04-25 04:34:15 +0000374 MachineInstr* OrigMI = *MII;
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000375 std::vector<MachineInstr *>::iterator AdIt;
Brian Gaekeaf843702003-10-22 20:22:53 +0000376 for (AdIt = IBef.begin(); AdIt != IBef.end() ; ++AdIt) {
Vikram S. Adve48762092002-04-25 04:34:15 +0000377 if (DEBUG_RA) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000378 if (OrigMI) std::cerr << "For MInst:\n " << *OrigMI;
379 std::cerr << msg << "PREPENDed instr:\n " << **AdIt << "\n";
Vikram S. Adve48762092002-04-25 04:34:15 +0000380 }
Chris Lattnerf726e772002-10-28 19:22:04 +0000381 InsertBefore(*AdIt, MBB, MII);
Vikram S. Adve48762092002-04-25 04:34:15 +0000382 }
383 }
384}
385
Brian Gaekeaf843702003-10-22 20:22:53 +0000386// used by: updateMachineCode (1 time)
387inline void AppendInstructions(std::vector<MachineInstr *> &IAft,
388 MachineBasicBlock& MBB,
389 MachineBasicBlock::iterator& MII,
390 const std::string& msg) {
391 if (!IAft.empty()) {
Vikram S. Adve48762092002-04-25 04:34:15 +0000392 MachineInstr* OrigMI = *MII;
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000393 std::vector<MachineInstr *>::iterator AdIt;
Brian Gaekeaf843702003-10-22 20:22:53 +0000394 for ( AdIt = IAft.begin(); AdIt != IAft.end() ; ++AdIt ) {
Chris Lattner7e708292002-06-25 16:13:24 +0000395 if (DEBUG_RA) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000396 if (OrigMI) std::cerr << "For MInst:\n " << *OrigMI;
397 std::cerr << msg << "APPENDed instr:\n " << **AdIt << "\n";
Vikram S. Adve48762092002-04-25 04:34:15 +0000398 }
Chris Lattnerf726e772002-10-28 19:22:04 +0000399 InsertAfter(*AdIt, MBB, MII);
Vikram S. Adve48762092002-04-25 04:34:15 +0000400 }
401 }
402}
403
Brian Gaekeaf843702003-10-22 20:22:53 +0000404/// Set the registers for operands in the given MachineInstr, if a register was
405/// successfully allocated. Return true if any of its operands has been marked
406/// for spill.
407///
Brian Gaeke4efe3422003-09-21 01:23:46 +0000408bool PhyRegAlloc::markAllocatedRegs(MachineInstr* MInst)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000409{
Vikram S. Adve814030a2003-07-29 19:49:21 +0000410 bool instrNeedsSpills = false;
411
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000412 // First, set the registers for operands in the machine instruction
413 // if a register was successfully allocated. Do this first because we
414 // will need to know which registers are already used by this instr'n.
Brian Gaekeaf843702003-10-22 20:22:53 +0000415 for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000416 MachineOperand& Op = MInst->getOperand(OpNum);
417 if (Op.getType() == MachineOperand::MO_VirtualRegister ||
Brian Gaekeaf843702003-10-22 20:22:53 +0000418 Op.getType() == MachineOperand::MO_CCRegister) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000419 const Value *const Val = Op.getVRegValue();
Brian Gaeke4efe3422003-09-21 01:23:46 +0000420 if (const LiveRange* LR = LRI->getLiveRangeForValue(Val)) {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000421 // Remember if any operand needs spilling
422 instrNeedsSpills |= LR->isMarkedForSpill();
423
424 // An operand may have a color whether or not it needs spilling
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000425 if (LR->hasColor())
426 MInst->SetRegForOperand(OpNum,
Brian Gaeke59b1c562003-09-24 17:50:28 +0000427 MRI.getUnifiedRegNum(LR->getRegClassID(),
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000428 LR->getColor()));
Vikram S. Adve814030a2003-07-29 19:49:21 +0000429 }
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000430 }
431 } // for each operand
Vikram S. Adve814030a2003-07-29 19:49:21 +0000432
433 return instrNeedsSpills;
434}
435
Brian Gaekeaf843702003-10-22 20:22:53 +0000436/// Mark allocated registers (using markAllocatedRegs()) on the instruction
437/// that MII points to. Then, if it's a call instruction, insert caller-saving
438/// code before and after it. Finally, insert spill code before and after it,
439/// using insertCode4SpilledLR().
440///
Vikram S. Adve814030a2003-07-29 19:49:21 +0000441void PhyRegAlloc::updateInstruction(MachineBasicBlock::iterator& MII,
Brian Gaekeaf843702003-10-22 20:22:53 +0000442 MachineBasicBlock &MBB) {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000443 MachineInstr* MInst = *MII;
444 unsigned Opcode = MInst->getOpCode();
445
446 // Reset tmp stack positions so they can be reused for each machine instr.
Brian Gaeke4efe3422003-09-21 01:23:46 +0000447 MF->getInfo()->popAllTempValues();
Vikram S. Adve814030a2003-07-29 19:49:21 +0000448
449 // Mark the operands for which regs have been allocated.
Brian Gaeke4efe3422003-09-21 01:23:46 +0000450 bool instrNeedsSpills = markAllocatedRegs(*MII);
Vikram S. Adve814030a2003-07-29 19:49:21 +0000451
452#ifndef NDEBUG
453 // Mark that the operands have been updated. Later,
454 // setRelRegsUsedByThisInst() is called to find registers used by each
455 // MachineInst, and it should not be used for an instruction until
456 // this is done. This flag just serves as a sanity check.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000457 OperandsColoredMap[MInst] = true;
Vikram S. Adve814030a2003-07-29 19:49:21 +0000458#endif
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000459
Vikram S. Advebc001b22003-07-25 21:06:09 +0000460 // Now insert caller-saving code before/after the call.
461 // Do this before inserting spill code since some registers must be
462 // used by save/restore and spill code should not use those registers.
Vikram S. Advebc001b22003-07-25 21:06:09 +0000463 if (TM.getInstrInfo().isCall(Opcode)) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000464 AddedInstrns &AI = AddedInstrMap[MInst];
Vikram S. Adve814030a2003-07-29 19:49:21 +0000465 insertCallerSavingCode(AI.InstrnsBefore, AI.InstrnsAfter, MInst,
466 MBB.getBasicBlock());
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000467 }
Vikram S. Advebc001b22003-07-25 21:06:09 +0000468
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000469 // Now insert spill code for remaining operands not allocated to
470 // registers. This must be done even for call return instructions
471 // since those are not handled by the special code above.
Vikram S. Adve814030a2003-07-29 19:49:21 +0000472 if (instrNeedsSpills)
Brian Gaekeaf843702003-10-22 20:22:53 +0000473 for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000474 MachineOperand& Op = MInst->getOperand(OpNum);
475 if (Op.getType() == MachineOperand::MO_VirtualRegister ||
Brian Gaekeaf843702003-10-22 20:22:53 +0000476 Op.getType() == MachineOperand::MO_CCRegister) {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000477 const Value* Val = Op.getVRegValue();
Brian Gaeke4efe3422003-09-21 01:23:46 +0000478 if (const LiveRange *LR = LRI->getLiveRangeForValue(Val))
Vikram S. Adve814030a2003-07-29 19:49:21 +0000479 if (LR->isMarkedForSpill())
480 insertCode4SpilledLR(LR, MII, MBB, OpNum);
481 }
482 } // for each operand
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000483}
484
Brian Gaekeaf843702003-10-22 20:22:53 +0000485/// Iterate over all the MachineBasicBlocks in the current function and set
486/// the allocated registers for each instruction (using updateInstruction()),
487/// after register allocation is complete. Then move code out of delay slots.
488///
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000489void PhyRegAlloc::updateMachineCode()
490{
Chris Lattner7e708292002-06-25 16:13:24 +0000491 // Insert any instructions needed at method entry
Brian Gaeke4efe3422003-09-21 01:23:46 +0000492 MachineBasicBlock::iterator MII = MF->front().begin();
493 PrependInstructions(AddedInstrAtEntry.InstrnsBefore, MF->front(), MII,
Chris Lattner7e708292002-06-25 16:13:24 +0000494 "At function entry: \n");
495 assert(AddedInstrAtEntry.InstrnsAfter.empty() &&
496 "InstrsAfter should be unnecessary since we are just inserting at "
497 "the function entry point here.");
Vikram S. Adve48762092002-04-25 04:34:15 +0000498
Brian Gaeke4efe3422003-09-21 01:23:46 +0000499 for (MachineFunction::iterator BBI = MF->begin(), BBE = MF->end();
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000500 BBI != BBE; ++BBI) {
Chris Lattnerf726e772002-10-28 19:22:04 +0000501 MachineBasicBlock &MBB = *BBI;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000502
503 // Iterate over all machine instructions in BB and mark operands with
504 // their assigned registers or insert spill code, as appropriate.
505 // Also, fix operands of call/return instructions.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000506 for (MachineBasicBlock::iterator MII = MBB.begin(); MII != MBB.end(); ++MII)
Vikram S. Adve814030a2003-07-29 19:49:21 +0000507 if (! TM.getInstrInfo().isDummyPhiInstr((*MII)->getOpCode()))
508 updateInstruction(MII, MBB);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000509
510 // Now, move code out of delay slots of branches and returns if needed.
511 // (Also, move "after" code from calls to the last delay slot instruction.)
512 // Moving code out of delay slots is needed in 2 situations:
513 // (1) If this is a branch and it needs instructions inserted after it,
514 // move any existing instructions out of the delay slot so that the
515 // instructions can go into the delay slot. This only supports the
516 // case that #instrsAfter <= #delay slots.
517 //
518 // (2) If any instruction in the delay slot needs
519 // instructions inserted, move it out of the delay slot and before the
520 // branch because putting code before or after it would be VERY BAD!
521 //
522 // If the annul bit of the branch is set, neither of these is legal!
523 // If so, we need to handle spill differently but annulling is not yet used.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000524 for (MachineBasicBlock::iterator MII = MBB.begin();
525 MII != MBB.end(); ++MII)
526 if (unsigned delaySlots =
Brian Gaekeaf843702003-10-22 20:22:53 +0000527 TM.getInstrInfo().getNumDelaySlots((*MII)->getOpCode())) {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000528 MachineInstr *MInst = *MII, *DelaySlotMI = *(MII+1);
529
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000530 // Check the 2 conditions above:
531 // (1) Does a branch need instructions added after it?
532 // (2) O/w does delay slot instr. need instrns before or after?
Vikram S. Adve814030a2003-07-29 19:49:21 +0000533 bool isBranch = (TM.getInstrInfo().isBranch(MInst->getOpCode()) ||
534 TM.getInstrInfo().isReturn(MInst->getOpCode()));
535 bool cond1 = (isBranch &&
536 AddedInstrMap.count(MInst) &&
537 AddedInstrMap[MInst].InstrnsAfter.size() > 0);
538 bool cond2 = (AddedInstrMap.count(DelaySlotMI) &&
539 (AddedInstrMap[DelaySlotMI].InstrnsBefore.size() > 0 ||
540 AddedInstrMap[DelaySlotMI].InstrnsAfter.size() > 0));
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000541
Brian Gaekeaf843702003-10-22 20:22:53 +0000542 if (cond1 || cond2) {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000543 assert((MInst->getOpCodeFlags() & AnnulFlag) == 0 &&
544 "FIXME: Moving an annulled delay slot instruction!");
545 assert(delaySlots==1 &&
546 "InsertBefore does not yet handle >1 delay slots!");
547 InsertBefore(DelaySlotMI, MBB, MII); // MII pts back to branch
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000548
549 // In case (1), delete it and don't replace with anything!
550 // Otherwise (i.e., case (2) only) replace it with a NOP.
551 if (cond1) {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000552 DeleteInstruction(MBB, ++MII); // MII now points to next inst.
553 --MII; // reset MII for ++MII of loop
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000554 }
Vikram S. Adve814030a2003-07-29 19:49:21 +0000555 else
556 SubstituteInPlace(BuildMI(TM.getInstrInfo().getNOPOpCode(),1),
557 MBB, MII+1); // replace with NOP
558
559 if (DEBUG_RA) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000560 std::cerr << "\nRegAlloc: Moved instr. with added code: "
Vikram S. Adve814030a2003-07-29 19:49:21 +0000561 << *DelaySlotMI
562 << " out of delay slots of instr: " << *MInst;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000563 }
564 }
Vikram S. Adve814030a2003-07-29 19:49:21 +0000565 else
566 // For non-branch instr with delay slots (probably a call), move
567 // InstrAfter to the instr. in the last delay slot.
568 move2DelayedInstr(*MII, *(MII+delaySlots));
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000569 }
570
571 // Finally iterate over all instructions in BB and insert before/after
Vikram S. Advebc001b22003-07-25 21:06:09 +0000572 for (MachineBasicBlock::iterator MII=MBB.begin(); MII != MBB.end(); ++MII) {
Vikram S. Adve48762092002-04-25 04:34:15 +0000573 MachineInstr *MInst = *MII;
Vikram S. Advebc001b22003-07-25 21:06:09 +0000574
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000575 // do not process Phis
Vikram S. Advebc001b22003-07-25 21:06:09 +0000576 if (TM.getInstrInfo().isDummyPhiInstr(MInst->getOpCode()))
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000577 continue;
578
Vikram S. Advebc001b22003-07-25 21:06:09 +0000579 // if there are any added instructions...
Chris Lattner7e708292002-06-25 16:13:24 +0000580 if (AddedInstrMap.count(MInst)) {
Vikram S. Advebc001b22003-07-25 21:06:09 +0000581 AddedInstrns &CallAI = AddedInstrMap[MInst];
582
583#ifndef NDEBUG
Vikram S. Adve814030a2003-07-29 19:49:21 +0000584 bool isBranch = (TM.getInstrInfo().isBranch(MInst->getOpCode()) ||
585 TM.getInstrInfo().isReturn(MInst->getOpCode()));
586 assert((!isBranch ||
587 AddedInstrMap[MInst].InstrnsAfter.size() <=
588 TM.getInstrInfo().getNumDelaySlots(MInst->getOpCode())) &&
589 "Cannot put more than #delaySlots instrns after "
590 "branch or return! Need to handle temps differently.");
591#endif
592
593#ifndef NDEBUG
Vikram S. Advebc001b22003-07-25 21:06:09 +0000594 // Temporary sanity checking code to detect whether the same machine
595 // instruction is ever inserted twice before/after a call.
596 // I suspect this is happening but am not sure. --Vikram, 7/1/03.
Vikram S. Advebc001b22003-07-25 21:06:09 +0000597 std::set<const MachineInstr*> instrsSeen;
598 for (int i = 0, N = CallAI.InstrnsBefore.size(); i < N; ++i) {
599 assert(instrsSeen.count(CallAI.InstrnsBefore[i]) == 0 &&
600 "Duplicate machine instruction in InstrnsBefore!");
601 instrsSeen.insert(CallAI.InstrnsBefore[i]);
602 }
603 for (int i = 0, N = CallAI.InstrnsAfter.size(); i < N; ++i) {
604 assert(instrsSeen.count(CallAI.InstrnsAfter[i]) == 0 &&
605 "Duplicate machine instruction in InstrnsBefore/After!");
606 instrsSeen.insert(CallAI.InstrnsAfter[i]);
607 }
608#endif
609
610 // Now add the instructions before/after this MI.
611 // We do this here to ensure that spill for an instruction is inserted
612 // as close as possible to an instruction (see above insertCode4Spill)
Vikram S. Advebc001b22003-07-25 21:06:09 +0000613 if (! CallAI.InstrnsBefore.empty())
614 PrependInstructions(CallAI.InstrnsBefore, MBB, MII,"");
615
616 if (! CallAI.InstrnsAfter.empty())
617 AppendInstructions(CallAI.InstrnsAfter, MBB, MII,"");
618
619 } // if there are any added instructions
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000620 } // for each machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000621 }
622}
623
624
Brian Gaekeaf843702003-10-22 20:22:53 +0000625/// Insert spill code for AN operand whose LR was spilled. May be called
626/// repeatedly for a single MachineInstr if it has many spilled operands. On
627/// each call, it finds a register which is not live at that instruction and
628/// also which is not used by other spilled operands of the same
629/// instruction. Then it uses this register temporarily to accommodate the
630/// spilled value.
631///
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000632void PhyRegAlloc::insertCode4SpilledLR(const LiveRange *LR,
Vikram S. Adve814030a2003-07-29 19:49:21 +0000633 MachineBasicBlock::iterator& MII,
634 MachineBasicBlock &MBB,
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000635 const unsigned OpNum) {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000636 MachineInstr *MInst = *MII;
637 const BasicBlock *BB = MBB.getBasicBlock();
638
Vikram S. Advead9c9782002-09-28 17:02:40 +0000639 assert((! TM.getInstrInfo().isCall(MInst->getOpCode()) || OpNum == 0) &&
640 "Outgoing arg of a call must be handled elsewhere (func arg ok)");
641 assert(! TM.getInstrInfo().isReturn(MInst->getOpCode()) &&
642 "Return value of a ret must be handled elsewhere");
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000643
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000644 MachineOperand& Op = MInst->getOperand(OpNum);
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000645 bool isDef = Op.opIsDefOnly();
646 bool isDefAndUse = Op.opIsDefAndUse();
Vikram S. Advebc001b22003-07-25 21:06:09 +0000647 unsigned RegType = MRI.getRegTypeForLR(LR);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000648 int SpillOff = LR->getSpillOffFromFP();
649 RegClass *RC = LR->getRegClass();
Vikram S. Adve814030a2003-07-29 19:49:21 +0000650
651 // Get the live-variable set to find registers free before this instr.
Vikram S. Advefeb32982003-08-12 22:22:24 +0000652 const ValueSet &LVSetBef = LVI->getLiveVarSetBeforeMInst(MInst, BB);
653
654#ifndef NDEBUG
655 // If this instr. is in the delay slot of a branch or return, we need to
656 // include all live variables before that branch or return -- we don't want to
657 // trample those! Verify that the set is included in the LV set before MInst.
Vikram S. Adve814030a2003-07-29 19:49:21 +0000658 if (MII != MBB.begin()) {
659 MachineInstr *PredMI = *(MII-1);
Vikram S. Advefeb32982003-08-12 22:22:24 +0000660 if (unsigned DS = TM.getInstrInfo().getNumDelaySlots(PredMI->getOpCode()))
661 assert(set_difference(LVI->getLiveVarSetBeforeMInst(PredMI), LVSetBef)
662 .empty() && "Live-var set before branch should be included in "
663 "live-var set of each delay slot instruction!");
Vikram S. Adve814030a2003-07-29 19:49:21 +0000664 }
Vikram S. Advefeb32982003-08-12 22:22:24 +0000665#endif
Vikram S. Adve00521d72001-11-12 23:26:35 +0000666
Brian Gaekeaf843702003-10-22 20:22:53 +0000667 MF->getInfo()->pushTempValue(MRI.getSpilledRegSize(RegType));
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000668
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000669 std::vector<MachineInstr*> MIBef, MIAft;
670 std::vector<MachineInstr*> AdIMid;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000671
Vikram S. Adve3bf08922003-07-10 19:42:55 +0000672 // Choose a register to hold the spilled value, if one was not preallocated.
673 // This may insert code before and after MInst to free up the value. If so,
674 // this code should be first/last in the spill sequence before/after MInst.
675 int TmpRegU=(LR->hasColor()
Brian Gaeke59b1c562003-09-24 17:50:28 +0000676 ? MRI.getUnifiedRegNum(LR->getRegClassID(),LR->getColor())
Vikram S. Adve3bf08922003-07-10 19:42:55 +0000677 : getUsableUniRegAtMI(RegType, &LVSetBef, MInst, MIBef,MIAft));
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000678
Vikram S. Advef5af6362002-07-08 23:15:32 +0000679 // Set the operand first so that it this register does not get used
680 // as a scratch register for later calls to getUsableUniRegAtMI below
681 MInst->SetRegForOperand(OpNum, TmpRegU);
682
683 // get the added instructions for this instruction
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000684 AddedInstrns &AI = AddedInstrMap[MInst];
Vikram S. Advef5af6362002-07-08 23:15:32 +0000685
686 // We may need a scratch register to copy the spilled value to/from memory.
687 // This may itself have to insert code to free up a scratch register.
688 // Any such code should go before (after) the spill code for a load (store).
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000689 // The scratch reg is not marked as used because it is only used
690 // for the copy and not used across MInst.
Vikram S. Advef5af6362002-07-08 23:15:32 +0000691 int scratchRegType = -1;
692 int scratchReg = -1;
Brian Gaekeaf843702003-10-22 20:22:53 +0000693 if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType)) {
Chris Lattner27a08932002-10-22 23:16:21 +0000694 scratchReg = getUsableUniRegAtMI(scratchRegType, &LVSetBef,
695 MInst, MIBef, MIAft);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000696 assert(scratchReg != MRI.getInvalidRegNum());
Vikram S. Advef5af6362002-07-08 23:15:32 +0000697 }
698
699 if (!isDef || isDefAndUse) {
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000700 // for a USE, we have to load the value of LR from stack to a TmpReg
701 // and use the TmpReg as one operand of instruction
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000702
Vikram S. Advef5af6362002-07-08 23:15:32 +0000703 // actual loading instruction(s)
Vikram S. Adve814030a2003-07-29 19:49:21 +0000704 MRI.cpMem2RegMI(AdIMid, MRI.getFramePointer(), SpillOff, TmpRegU,
705 RegType, scratchReg);
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000706
Vikram S. Advef5af6362002-07-08 23:15:32 +0000707 // the actual load should be after the instructions to free up TmpRegU
708 MIBef.insert(MIBef.end(), AdIMid.begin(), AdIMid.end());
709 AdIMid.clear();
710 }
711
Vikram S. Adve3bf08922003-07-10 19:42:55 +0000712 if (isDef || isDefAndUse) { // if this is a Def
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000713 // for a DEF, we have to store the value produced by this instruction
714 // on the stack position allocated for this LR
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000715
Vikram S. Advef5af6362002-07-08 23:15:32 +0000716 // actual storing instruction(s)
Vikram S. Adve814030a2003-07-29 19:49:21 +0000717 MRI.cpReg2MemMI(AdIMid, TmpRegU, MRI.getFramePointer(), SpillOff,
718 RegType, scratchReg);
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000719
Vikram S. Advef5af6362002-07-08 23:15:32 +0000720 MIAft.insert(MIAft.begin(), AdIMid.begin(), AdIMid.end());
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000721 } // if !DEF
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000722
Vikram S. Advef5af6362002-07-08 23:15:32 +0000723 // Finally, insert the entire spill code sequences before/after MInst
724 AI.InstrnsBefore.insert(AI.InstrnsBefore.end(), MIBef.begin(), MIBef.end());
725 AI.InstrnsAfter.insert(AI.InstrnsAfter.begin(), MIAft.begin(), MIAft.end());
726
Chris Lattner7e708292002-06-25 16:13:24 +0000727 if (DEBUG_RA) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000728 std::cerr << "\nFor Inst:\n " << *MInst;
729 std::cerr << "SPILLED LR# " << LR->getUserIGNode()->getIndex();
730 std::cerr << "; added Instructions:";
Anand Shuklad58290e2002-07-09 19:18:56 +0000731 for_each(MIBef.begin(), MIBef.end(), std::mem_fun(&MachineInstr::dump));
732 for_each(MIAft.begin(), MIAft.end(), std::mem_fun(&MachineInstr::dump));
Chris Lattner7e708292002-06-25 16:13:24 +0000733 }
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000734}
735
736
Brian Gaekeaf843702003-10-22 20:22:53 +0000737/// Insert caller saving/restoring instructions before/after a call machine
738/// instruction (before or after any other instructions that were inserted for
739/// the call).
740///
Vikram S. Adve814030a2003-07-29 19:49:21 +0000741void
742PhyRegAlloc::insertCallerSavingCode(std::vector<MachineInstr*> &instrnsBefore,
743 std::vector<MachineInstr*> &instrnsAfter,
744 MachineInstr *CallMI,
Brian Gaekeaf843702003-10-22 20:22:53 +0000745 const BasicBlock *BB) {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000746 assert(TM.getInstrInfo().isCall(CallMI->getOpCode()));
747
Brian Gaeke43ce8fe2003-09-21 02:24:09 +0000748 // hash set to record which registers were saved/restored
Vikram S. Adve814030a2003-07-29 19:49:21 +0000749 hash_set<unsigned> PushedRegSet;
750
751 CallArgsDescriptor* argDesc = CallArgsDescriptor::get(CallMI);
752
753 // if the call is to a instrumentation function, do not insert save and
754 // restore instructions the instrumentation function takes care of save
755 // restore for volatile regs.
756 //
757 // FIXME: this should be made general, not specific to the reoptimizer!
Vikram S. Adve814030a2003-07-29 19:49:21 +0000758 const Function *Callee = argDesc->getCallInst()->getCalledFunction();
759 bool isLLVMFirstTrigger = Callee && Callee->getName() == "llvm_first_trigger";
760
761 // Now check if the call has a return value (using argDesc) and if so,
762 // find the LR of the TmpInstruction representing the return value register.
763 // (using the last or second-last *implicit operand* of the call MI).
764 // Insert it to to the PushedRegSet since we must not save that register
765 // and restore it after the call.
766 // We do this because, we look at the LV set *after* the instruction
767 // to determine, which LRs must be saved across calls. The return value
768 // of the call is live in this set - but we must not save/restore it.
Vikram S. Adve814030a2003-07-29 19:49:21 +0000769 if (const Value *origRetVal = argDesc->getReturnValue()) {
770 unsigned retValRefNum = (CallMI->getNumImplicitRefs() -
771 (argDesc->getIndirectFuncPtr()? 1 : 2));
772 const TmpInstruction* tmpRetVal =
773 cast<TmpInstruction>(CallMI->getImplicitRef(retValRefNum));
774 assert(tmpRetVal->getOperand(0) == origRetVal &&
775 tmpRetVal->getType() == origRetVal->getType() &&
776 "Wrong implicit ref?");
Brian Gaeke4efe3422003-09-21 01:23:46 +0000777 LiveRange *RetValLR = LRI->getLiveRangeForValue(tmpRetVal);
Vikram S. Adve814030a2003-07-29 19:49:21 +0000778 assert(RetValLR && "No LR for RetValue of call");
779
780 if (! RetValLR->isMarkedForSpill())
781 PushedRegSet.insert(MRI.getUnifiedRegNum(RetValLR->getRegClassID(),
782 RetValLR->getColor()));
783 }
784
785 const ValueSet &LVSetAft = LVI->getLiveVarSetAfterMInst(CallMI, BB);
786 ValueSet::const_iterator LIt = LVSetAft.begin();
787
788 // for each live var in live variable set after machine inst
789 for( ; LIt != LVSetAft.end(); ++LIt) {
Brian Gaeke43ce8fe2003-09-21 02:24:09 +0000790 // get the live range corresponding to live var
Brian Gaeke4efe3422003-09-21 01:23:46 +0000791 LiveRange *const LR = LRI->getLiveRangeForValue(*LIt);
Vikram S. Adve814030a2003-07-29 19:49:21 +0000792
793 // LR can be null if it is a const since a const
794 // doesn't have a dominating def - see Assumptions above
Brian Gaekeaf843702003-10-22 20:22:53 +0000795 if (LR) {
796 if (! LR->isMarkedForSpill()) {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000797 assert(LR->hasColor() && "LR is neither spilled nor colored?");
798 unsigned RCID = LR->getRegClassID();
799 unsigned Color = LR->getColor();
800
801 if (MRI.isRegVolatile(RCID, Color) ) {
Brian Gaeke43ce8fe2003-09-21 02:24:09 +0000802 // if this is a call to the first-level reoptimizer
803 // instrumentation entry point, and the register is not
804 // modified by call, don't save and restore it.
Vikram S. Adve814030a2003-07-29 19:49:21 +0000805 if (isLLVMFirstTrigger && !MRI.modifiedByCall(RCID, Color))
806 continue;
807
808 // if the value is in both LV sets (i.e., live before and after
809 // the call machine instruction)
Vikram S. Adve814030a2003-07-29 19:49:21 +0000810 unsigned Reg = MRI.getUnifiedRegNum(RCID, Color);
811
Brian Gaeke43ce8fe2003-09-21 02:24:09 +0000812 // if we haven't already pushed this register...
Vikram S. Adve814030a2003-07-29 19:49:21 +0000813 if( PushedRegSet.find(Reg) == PushedRegSet.end() ) {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000814 unsigned RegType = MRI.getRegTypeForLR(LR);
815
816 // Now get two instructions - to push on stack and pop from stack
817 // and add them to InstrnsBefore and InstrnsAfter of the
818 // call instruction
Vikram S. Adve814030a2003-07-29 19:49:21 +0000819 int StackOff =
Brian Gaeke4efe3422003-09-21 01:23:46 +0000820 MF->getInfo()->pushTempValue(MRI.getSpilledRegSize(RegType));
Vikram S. Adve814030a2003-07-29 19:49:21 +0000821
822 //---- Insert code for pushing the reg on stack ----------
823
824 std::vector<MachineInstr*> AdIBef, AdIAft;
825
826 // We may need a scratch register to copy the saved value
827 // to/from memory. This may itself have to insert code to
828 // free up a scratch register. Any such code should go before
829 // the save code. The scratch register, if any, is by default
830 // temporary and not "used" by the instruction unless the
831 // copy code itself decides to keep the value in the scratch reg.
832 int scratchRegType = -1;
833 int scratchReg = -1;
834 if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType))
835 { // Find a register not live in the LVSet before CallMI
836 const ValueSet &LVSetBef =
837 LVI->getLiveVarSetBeforeMInst(CallMI, BB);
838 scratchReg = getUsableUniRegAtMI(scratchRegType, &LVSetBef,
839 CallMI, AdIBef, AdIAft);
840 assert(scratchReg != MRI.getInvalidRegNum());
841 }
842
843 if (AdIBef.size() > 0)
844 instrnsBefore.insert(instrnsBefore.end(),
845 AdIBef.begin(), AdIBef.end());
846
847 MRI.cpReg2MemMI(instrnsBefore, Reg, MRI.getFramePointer(),
848 StackOff, RegType, scratchReg);
849
850 if (AdIAft.size() > 0)
851 instrnsBefore.insert(instrnsBefore.end(),
852 AdIAft.begin(), AdIAft.end());
853
854 //---- Insert code for popping the reg from the stack ----------
Vikram S. Adve814030a2003-07-29 19:49:21 +0000855 AdIBef.clear();
856 AdIAft.clear();
857
858 // We may need a scratch register to copy the saved value
859 // from memory. This may itself have to insert code to
860 // free up a scratch register. Any such code should go
861 // after the save code. As above, scratch is not marked "used".
Vikram S. Adve814030a2003-07-29 19:49:21 +0000862 scratchRegType = -1;
863 scratchReg = -1;
864 if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType))
865 { // Find a register not live in the LVSet after CallMI
866 scratchReg = getUsableUniRegAtMI(scratchRegType, &LVSetAft,
867 CallMI, AdIBef, AdIAft);
868 assert(scratchReg != MRI.getInvalidRegNum());
869 }
870
871 if (AdIBef.size() > 0)
872 instrnsAfter.insert(instrnsAfter.end(),
873 AdIBef.begin(), AdIBef.end());
874
875 MRI.cpMem2RegMI(instrnsAfter, MRI.getFramePointer(), StackOff,
876 Reg, RegType, scratchReg);
877
878 if (AdIAft.size() > 0)
879 instrnsAfter.insert(instrnsAfter.end(),
880 AdIAft.begin(), AdIAft.end());
881
882 PushedRegSet.insert(Reg);
883
884 if(DEBUG_RA) {
885 std::cerr << "\nFor call inst:" << *CallMI;
886 std::cerr << " -inserted caller saving instrs: Before:\n\t ";
887 for_each(instrnsBefore.begin(), instrnsBefore.end(),
888 std::mem_fun(&MachineInstr::dump));
889 std::cerr << " -and After:\n\t ";
890 for_each(instrnsAfter.begin(), instrnsAfter.end(),
891 std::mem_fun(&MachineInstr::dump));
892 }
893 } // if not already pushed
Vikram S. Adve814030a2003-07-29 19:49:21 +0000894 } // if LR has a volatile color
Vikram S. Adve814030a2003-07-29 19:49:21 +0000895 } // if LR has color
Vikram S. Adve814030a2003-07-29 19:49:21 +0000896 } // if there is a LR for Var
Vikram S. Adve814030a2003-07-29 19:49:21 +0000897 } // for each value in the LV set after instruction
898}
899
900
Brian Gaekeaf843702003-10-22 20:22:53 +0000901/// Returns the unified register number of a temporary register to be used
902/// BEFORE MInst. If no register is available, it will pick one and modify
903/// MIBef and MIAft to contain instructions used to free up this returned
904/// register.
905///
Vikram S. Advef5af6362002-07-08 23:15:32 +0000906int PhyRegAlloc::getUsableUniRegAtMI(const int RegType,
907 const ValueSet *LVSetBef,
908 MachineInstr *MInst,
909 std::vector<MachineInstr*>& MIBef,
910 std::vector<MachineInstr*>& MIAft) {
Chris Lattner133f0792002-10-28 04:45:29 +0000911 RegClass* RC = getRegClassByID(MRI.getRegClassIDOfRegType(RegType));
Vikram S. Advef5af6362002-07-08 23:15:32 +0000912
Brian Gaekeaf843702003-10-22 20:22:53 +0000913 int RegU = getUnusedUniRegAtMI(RC, RegType, MInst, LVSetBef);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000914
915 if (RegU == -1) {
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000916 // we couldn't find an unused register. Generate code to free up a reg by
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000917 // saving it on stack and restoring after the instruction
Vikram S. Advef5af6362002-07-08 23:15:32 +0000918
Brian Gaeke4efe3422003-09-21 01:23:46 +0000919 int TmpOff = MF->getInfo()->pushTempValue(MRI.getSpilledRegSize(RegType));
Vikram S. Adve12af1642001-11-08 04:48:50 +0000920
Vikram S. Advebc001b22003-07-25 21:06:09 +0000921 RegU = getUniRegNotUsedByThisInst(RC, RegType, MInst);
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000922
Vikram S. Advef5af6362002-07-08 23:15:32 +0000923 // Check if we need a scratch register to copy this register to memory.
924 int scratchRegType = -1;
Brian Gaekeaf843702003-10-22 20:22:53 +0000925 if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType)) {
Chris Lattner133f0792002-10-28 04:45:29 +0000926 int scratchReg = getUsableUniRegAtMI(scratchRegType, LVSetBef,
927 MInst, MIBef, MIAft);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000928 assert(scratchReg != MRI.getInvalidRegNum());
929
930 // We may as well hold the value in the scratch register instead
931 // of copying it to memory and back. But we have to mark the
932 // register as used by this instruction, so it does not get used
933 // as a scratch reg. by another operand or anyone else.
Chris Lattner3fd1f5b2003-08-05 22:11:13 +0000934 ScratchRegsUsed.insert(std::make_pair(MInst, scratchReg));
Vikram S. Advef5af6362002-07-08 23:15:32 +0000935 MRI.cpReg2RegMI(MIBef, RegU, scratchReg, RegType);
936 MRI.cpReg2RegMI(MIAft, scratchReg, RegU, RegType);
Brian Gaekeaf843702003-10-22 20:22:53 +0000937 } else { // the register can be copied directly to/from memory so do it.
Vikram S. Advef5af6362002-07-08 23:15:32 +0000938 MRI.cpReg2MemMI(MIBef, RegU, MRI.getFramePointer(), TmpOff, RegType);
939 MRI.cpMem2RegMI(MIAft, MRI.getFramePointer(), TmpOff, RegU, RegType);
Brian Gaekeaf843702003-10-22 20:22:53 +0000940 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000941 }
Vikram S. Advef5af6362002-07-08 23:15:32 +0000942
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000943 return RegU;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000944}
945
Vikram S. Adve814030a2003-07-29 19:49:21 +0000946
Brian Gaekeaf843702003-10-22 20:22:53 +0000947/// Returns the register-class register number of a new unused register that
948/// can be used to accommodate a temporary value. May be called repeatedly
949/// for a single MachineInstr. On each call, it finds a register which is not
950/// live at that instruction and which is not used by any spilled operands of
951/// that instruction.
952///
953int PhyRegAlloc::getUnusedUniRegAtMI(RegClass *RC, const int RegType,
Vikram S. Adve814030a2003-07-29 19:49:21 +0000954 const MachineInstr *MInst,
955 const ValueSet* LVSetBef) {
Vikram S. Advebc001b22003-07-25 21:06:09 +0000956 RC->clearColorsUsed(); // Reset array
Vikram S. Adve814030a2003-07-29 19:49:21 +0000957
958 if (LVSetBef == NULL) {
959 LVSetBef = &LVI->getLiveVarSetBeforeMInst(MInst);
960 assert(LVSetBef != NULL && "Unable to get live-var set before MInst?");
961 }
962
Chris Lattner296b7732002-02-05 02:52:05 +0000963 ValueSet::const_iterator LIt = LVSetBef->begin();
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000964
965 // for each live var in live variable set after machine inst
Chris Lattner7e708292002-06-25 16:13:24 +0000966 for ( ; LIt != LVSetBef->end(); ++LIt) {
Brian Gaeke43ce8fe2003-09-21 02:24:09 +0000967 // Get the live range corresponding to live var, and its RegClass
Brian Gaeke4efe3422003-09-21 01:23:46 +0000968 LiveRange *const LRofLV = LRI->getLiveRangeForValue(*LIt );
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000969
970 // LR can be null if it is a const since a const
971 // doesn't have a dominating def - see Assumptions above
Vikram S. Advebc001b22003-07-25 21:06:09 +0000972 if (LRofLV && LRofLV->getRegClass() == RC && LRofLV->hasColor())
973 RC->markColorsUsed(LRofLV->getColor(),
974 MRI.getRegTypeForLR(LRofLV), RegType);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000975 }
976
977 // It is possible that one operand of this MInst was already spilled
978 // and it received some register temporarily. If that's the case,
979 // it is recorded in machine operand. We must skip such registers.
Vikram S. Advebc001b22003-07-25 21:06:09 +0000980 setRelRegsUsedByThisInst(RC, RegType, MInst);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000981
Vikram S. Advebc001b22003-07-25 21:06:09 +0000982 int unusedReg = RC->getUnusedColor(RegType); // find first unused color
983 if (unusedReg >= 0)
984 return MRI.getUnifiedRegNum(RC->getID(), unusedReg);
985
Chris Lattner85c54652002-05-23 15:50:03 +0000986 return -1;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000987}
988
989
Brian Gaekeaf843702003-10-22 20:22:53 +0000990/// Return the unified register number of a register in class RC which is not
991/// used by any operands of MInst.
992///
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000993int PhyRegAlloc::getUniRegNotUsedByThisInst(RegClass *RC,
Vikram S. Advebc001b22003-07-25 21:06:09 +0000994 const int RegType,
Chris Lattner85c54652002-05-23 15:50:03 +0000995 const MachineInstr *MInst) {
Vikram S. Advebc001b22003-07-25 21:06:09 +0000996 RC->clearColorsUsed();
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000997
Vikram S. Advebc001b22003-07-25 21:06:09 +0000998 setRelRegsUsedByThisInst(RC, RegType, MInst);
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000999
Vikram S. Advebc001b22003-07-25 21:06:09 +00001000 // find the first unused color
1001 int unusedReg = RC->getUnusedColor(RegType);
1002 assert(unusedReg >= 0 &&
1003 "FATAL: No free register could be found in reg class!!");
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001004
Vikram S. Advebc001b22003-07-25 21:06:09 +00001005 return MRI.getUnifiedRegNum(RC->getID(), unusedReg);
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001006}
1007
1008
Brian Gaekeaf843702003-10-22 20:22:53 +00001009/// Modify the IsColorUsedArr of register class RC, by setting the bits
1010/// corresponding to register RegNo. This is a helper method of
1011/// setRelRegsUsedByThisInst().
1012///
Chris Lattner3bed95b2003-08-05 21:55:58 +00001013static void markRegisterUsed(int RegNo, RegClass *RC, int RegType,
1014 const TargetRegInfo &TRI) {
1015 unsigned classId = 0;
1016 int classRegNum = TRI.getClassRegNum(RegNo, classId);
1017 if (RC->getID() == classId)
1018 RC->markColorsUsed(classRegNum, RegType, RegType);
1019}
1020
1021void PhyRegAlloc::setRelRegsUsedByThisInst(RegClass *RC, int RegType,
Brian Gaekeaf843702003-10-22 20:22:53 +00001022 const MachineInstr *MI) {
Chris Lattner3bed95b2003-08-05 21:55:58 +00001023 assert(OperandsColoredMap[MI] == true &&
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001024 "Illegal to call setRelRegsUsedByThisInst() until colored operands "
1025 "are marked for an instruction.");
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001026
Brian Gaekeaf843702003-10-22 20:22:53 +00001027 // Add the registers already marked as used by the instruction. Both
1028 // explicit and implicit operands are set.
Chris Lattner3bed95b2003-08-05 21:55:58 +00001029 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
1030 if (MI->getOperand(i).hasAllocatedReg())
1031 markRegisterUsed(MI->getOperand(i).getAllocatedRegNum(), RC, RegType,MRI);
1032
1033 for (unsigned i = 0, e = MI->getNumImplicitRefs(); i != e; ++i)
1034 if (MI->getImplicitOp(i).hasAllocatedReg())
1035 markRegisterUsed(MI->getImplicitOp(i).getAllocatedRegNum(), RC,
1036 RegType,MRI);
1037
Chris Lattner3fd1f5b2003-08-05 22:11:13 +00001038 // Add all of the scratch registers that are used to save values across the
1039 // instruction (e.g., for saving state register values).
1040 std::pair<ScratchRegsUsedTy::iterator, ScratchRegsUsedTy::iterator>
1041 IR = ScratchRegsUsed.equal_range(MI);
1042 for (ScratchRegsUsedTy::iterator I = IR.first; I != IR.second; ++I)
1043 markRegisterUsed(I->second, RC, RegType, MRI);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001044
Vikram S. Advef5af6362002-07-08 23:15:32 +00001045 // If there are implicit references, mark their allocated regs as well
Chris Lattner3bed95b2003-08-05 21:55:58 +00001046 for (unsigned z=0; z < MI->getNumImplicitRefs(); z++)
Vikram S. Advef5af6362002-07-08 23:15:32 +00001047 if (const LiveRange*
Brian Gaeke4efe3422003-09-21 01:23:46 +00001048 LRofImpRef = LRI->getLiveRangeForValue(MI->getImplicitRef(z)))
Vikram S. Advef5af6362002-07-08 23:15:32 +00001049 if (LRofImpRef->hasColor())
1050 // this implicit reference is in a LR that received a color
Vikram S. Advebc001b22003-07-25 21:06:09 +00001051 RC->markColorsUsed(LRofImpRef->getColor(),
1052 MRI.getRegTypeForLR(LRofImpRef), RegType);
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001053}
1054
1055
Brian Gaekeaf843702003-10-22 20:22:53 +00001056/// If there are delay slots for an instruction, the instructions added after
1057/// it must really go after the delayed instruction(s). So, we Move the
1058/// InstrAfter of that instruction to the corresponding delayed instruction
1059/// using the following method.
1060///
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001061void PhyRegAlloc::move2DelayedInstr(const MachineInstr *OrigMI,
1062 const MachineInstr *DelayedMI)
1063{
Vikram S. Advefeb32982003-08-12 22:22:24 +00001064 // "added after" instructions of the original instr
1065 std::vector<MachineInstr *> &OrigAft = AddedInstrMap[OrigMI].InstrnsAfter;
1066
1067 if (DEBUG_RA && OrigAft.size() > 0) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +00001068 std::cerr << "\nRegAlloc: Moved InstrnsAfter for: " << *OrigMI;
1069 std::cerr << " to last delay slot instrn: " << *DelayedMI;
Vikram S. Adve814030a2003-07-29 19:49:21 +00001070 }
1071
Ruchira Sasanka251d8db2001-10-23 21:38:00 +00001072 // "added after" instructions of the delayed instr
Vikram S. Adve814030a2003-07-29 19:49:21 +00001073 std::vector<MachineInstr *> &DelayedAft=AddedInstrMap[DelayedMI].InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +00001074
1075 // go thru all the "added after instructions" of the original instruction
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001076 // and append them to the "added after instructions" of the delayed
Ruchira Sasanka251d8db2001-10-23 21:38:00 +00001077 // instructions
Chris Lattner697954c2002-01-20 22:54:45 +00001078 DelayedAft.insert(DelayedAft.end(), OrigAft.begin(), OrigAft.end());
Ruchira Sasanka251d8db2001-10-23 21:38:00 +00001079
1080 // empty the "added after instructions" of the original instruction
1081 OrigAft.clear();
Ruchira Sasanka251d8db2001-10-23 21:38:00 +00001082}
Ruchira Sasanka0931a012001-09-15 19:06:58 +00001083
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001084
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001085void PhyRegAlloc::colorIncomingArgs()
1086{
Brian Gaeke4efe3422003-09-21 01:23:46 +00001087 MRI.colorMethodArgs(Fn, *LRI, AddedInstrAtEntry.InstrnsBefore,
Vikram S. Adve814030a2003-07-29 19:49:21 +00001088 AddedInstrAtEntry.InstrnsAfter);
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001089}
1090
Ruchira Sasankae727f852001-09-18 22:43:57 +00001091
Brian Gaekeaf843702003-10-22 20:22:53 +00001092/// Determine whether the suggested color of each live range is really usable,
1093/// and then call its setSuggestedColorUsable() method to record the answer. A
1094/// suggested color is NOT usable when the suggested color is volatile AND
1095/// when there are call interferences.
1096///
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001097void PhyRegAlloc::markUnusableSugColors()
1098{
Brian Gaeke4efe3422003-09-21 01:23:46 +00001099 LiveRangeMapType::const_iterator HMI = (LRI->getLiveRangeMap())->begin();
1100 LiveRangeMapType::const_iterator HMIEnd = (LRI->getLiveRangeMap())->end();
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001101
Brian Gaeke43ce8fe2003-09-21 02:24:09 +00001102 for (; HMI != HMIEnd ; ++HMI ) {
1103 if (HMI->first) {
1104 LiveRange *L = HMI->second; // get the LiveRange
Brian Gaeke59b1c562003-09-24 17:50:28 +00001105 if (L && L->hasSuggestedColor ())
Brian Gaeke6a256cc2003-09-24 18:08:54 +00001106 L->setSuggestedColorUsable
1107 (!(MRI.isRegVolatile (L->getRegClassID (), L->getSuggestedColor ())
1108 && L->isCallInterference ()));
Brian Gaeke43ce8fe2003-09-21 02:24:09 +00001109 }
1110 } // for all LR's in hash map
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001111}
1112
1113
Brian Gaekeaf843702003-10-22 20:22:53 +00001114/// For each live range that is spilled, allocates a new spill position on the
1115/// stack, and set the stack offsets of the live range that will be spilled to
1116/// that position. This must be called just after coloring the LRs.
1117///
Chris Lattner37730942002-02-05 03:52:29 +00001118void PhyRegAlloc::allocateStackSpace4SpilledLRs() {
Chris Lattnerc083dcc2003-09-01 20:05:47 +00001119 if (DEBUG_RA) std::cerr << "\nSetting LR stack offsets for spills...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001120
Brian Gaeke4efe3422003-09-21 01:23:46 +00001121 LiveRangeMapType::const_iterator HMI = LRI->getLiveRangeMap()->begin();
1122 LiveRangeMapType::const_iterator HMIEnd = LRI->getLiveRangeMap()->end();
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001123
Chris Lattner7e708292002-06-25 16:13:24 +00001124 for ( ; HMI != HMIEnd ; ++HMI) {
Chris Lattner37730942002-02-05 03:52:29 +00001125 if (HMI->first && HMI->second) {
Vikram S. Adve3bf08922003-07-10 19:42:55 +00001126 LiveRange *L = HMI->second; // get the LiveRange
1127 if (L->isMarkedForSpill()) { // NOTE: allocating size of long Type **
Brian Gaeke4efe3422003-09-21 01:23:46 +00001128 int stackOffset = MF->getInfo()->allocateSpilledValue(Type::LongTy);
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001129 L->setSpillOffFromFP(stackOffset);
1130 if (DEBUG_RA)
Chris Lattnerc083dcc2003-09-01 20:05:47 +00001131 std::cerr << " LR# " << L->getUserIGNode()->getIndex()
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001132 << ": stack-offset = " << stackOffset << "\n";
1133 }
Chris Lattner37730942002-02-05 03:52:29 +00001134 }
1135 } // for all LR's in hash map
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001136}
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001137
Brian Gaeke874f4232003-09-21 02:50:21 +00001138
Brian Gaekeaf843702003-10-22 20:22:53 +00001139/// Save the global register allocation decisions made by the register
1140/// allocator so that they can be accessed later (sort of like "poor man's
1141/// debug info").
1142///
1143void PhyRegAlloc::saveState () {
Brian Gaeke537132b2003-10-23 20:32:55 +00001144 std::vector<AllocInfo> &state = FnAllocState[Fn];
Brian Gaeke6a256cc2003-09-24 18:08:54 +00001145 unsigned Insn = 0;
1146 LiveRangeMapType::const_iterator HMIEnd = LRI->getLiveRangeMap ()->end ();
1147 for (const_inst_iterator II=inst_begin (Fn), IE=inst_end (Fn); II != IE; ++II)
1148 for (unsigned i = 0; i < (*II)->getNumOperands (); ++i) {
1149 const Value *V = (*II)->getOperand (i);
1150 // Don't worry about it unless it's something whose reg. we'll need.
1151 if (!isa<Argument> (V) && !isa<Instruction> (V))
1152 continue;
1153 LiveRangeMapType::const_iterator HMI = LRI->getLiveRangeMap ()->find (V);
1154 static const unsigned NotAllocated = 0, Allocated = 1, Spilled = 2;
1155 unsigned AllocState = NotAllocated;
1156 int Placement = -1;
1157 if ((HMI != HMIEnd) && HMI->second) {
1158 LiveRange *L = HMI->second;
1159 assert ((L->hasColor () || L->isMarkedForSpill ())
1160 && "Live range exists but not colored or spilled");
1161 if (L->hasColor()) {
1162 AllocState = Allocated;
1163 Placement = MRI.getUnifiedRegNum (L->getRegClassID (),
1164 L->getColor ());
1165 } else if (L->isMarkedForSpill ()) {
1166 AllocState = Spilled;
1167 assert (L->hasSpillOffset ()
1168 && "Live range marked for spill but has no spill offset");
1169 Placement = L->getSpillOffFromFP ();
1170 }
1171 }
Brian Gaeke537132b2003-10-23 20:32:55 +00001172 state.push_back (AllocInfo (Insn, i, AllocState, Placement));
Brian Gaeke6a256cc2003-09-24 18:08:54 +00001173 }
Brian Gaeke6a256cc2003-09-24 18:08:54 +00001174}
1175
Brian Gaeke537132b2003-10-23 20:32:55 +00001176
Brian Gaekeaf843702003-10-22 20:22:53 +00001177/// Check the saved state filled in by saveState(), and abort if it looks
1178/// wrong. Only used when debugging.
1179///
1180void PhyRegAlloc::verifySavedState () {
1181 /// not yet implemented
1182}
Brian Gaeke6a256cc2003-09-24 18:08:54 +00001183
Brian Gaeke537132b2003-10-23 20:32:55 +00001184/// Finish the job of saveState(), by collapsing FnAllocState into an LLVM
1185/// Constant and stuffing it inside the Module. (NOTE: Soon, there will be
1186/// other, better ways of storing the saved state; this one is cumbersome and
1187/// will never work with the JIT.)
1188///
Brian Gaeke6a256cc2003-09-24 18:08:54 +00001189bool PhyRegAlloc::doFinalization (Module &M) {
1190 if (!SaveRegAllocState)
1191 return false; // Nothing to do here, unless we're saving state.
1192
Brian Gaeke8fc49342003-10-24 21:21:58 +00001193 if (!SaveStateToModule) {
1194 ExportedFnAllocState = FnAllocState;
1195 return false;
1196 }
1197
Brian Gaeke6a256cc2003-09-24 18:08:54 +00001198 // Convert FnAllocState to a single Constant array and add it
1199 // to the Module.
1200 ArrayType *AT = ArrayType::get (AllocInfo::getConstantType (), 0);
1201 std::vector<const Type *> TV;
1202 TV.push_back (Type::UIntTy);
1203 TV.push_back (AT);
1204 PointerType *PT = PointerType::get (StructType::get (TV));
1205
1206 std::vector<Constant *> allstate;
1207 for (Module::iterator I = M.begin (), E = M.end (); I != E; ++I) {
1208 Function *F = I;
1209 if (FnAllocState.find (F) == FnAllocState.end ()) {
1210 allstate.push_back (ConstantPointerNull::get (PT));
1211 } else {
Brian Gaeke537132b2003-10-23 20:32:55 +00001212 std::vector<AllocInfo> &state = FnAllocState[F];
Brian Gaeke60a3c552003-10-22 20:44:23 +00001213
1214 // Convert state into an LLVM ConstantArray, and put it in a
1215 // ConstantStruct (named S) along with its size.
Brian Gaeke537132b2003-10-23 20:32:55 +00001216 std::vector<Constant *> stateConstants;
1217 for (unsigned i = 0, s = state.size (); i != s; ++i)
1218 stateConstants.push_back (state[i].toConstant ());
1219 unsigned Size = stateConstants.size ();
Brian Gaeke60a3c552003-10-22 20:44:23 +00001220 ArrayType *AT = ArrayType::get (AllocInfo::getConstantType (), Size);
1221 std::vector<const Type *> TV;
1222 TV.push_back (Type::UIntTy);
1223 TV.push_back (AT);
1224 StructType *ST = StructType::get (TV);
1225 std::vector<Constant *> CV;
1226 CV.push_back (ConstantUInt::get (Type::UIntTy, Size));
Brian Gaeke537132b2003-10-23 20:32:55 +00001227 CV.push_back (ConstantArray::get (AT, stateConstants));
Brian Gaeke60a3c552003-10-22 20:44:23 +00001228 Constant *S = ConstantStruct::get (ST, CV);
1229
Brian Gaeke6a256cc2003-09-24 18:08:54 +00001230 GlobalVariable *GV =
Brian Gaeke60a3c552003-10-22 20:44:23 +00001231 new GlobalVariable (ST, true,
1232 GlobalValue::InternalLinkage, S,
Brian Gaeke6a256cc2003-09-24 18:08:54 +00001233 F->getName () + ".regAllocState", &M);
Brian Gaeke60a3c552003-10-22 20:44:23 +00001234
Brian Gaeke6a256cc2003-09-24 18:08:54 +00001235 // Have: { uint, [Size x { uint, uint, uint, int }] } *
1236 // Cast it to: { uint, [0 x { uint, uint, uint, int }] } *
1237 Constant *CE = ConstantExpr::getCast (ConstantPointerRef::get (GV), PT);
1238 allstate.push_back (CE);
1239 }
1240 }
1241
1242 unsigned Size = allstate.size ();
1243 // Final structure type is:
1244 // { uint, [Size x { uint, [0 x { uint, uint, uint, int }] } *] }
1245 std::vector<const Type *> TV2;
1246 TV2.push_back (Type::UIntTy);
1247 ArrayType *AT2 = ArrayType::get (PT, Size);
1248 TV2.push_back (AT2);
1249 StructType *ST2 = StructType::get (TV2);
1250 std::vector<Constant *> CV2;
1251 CV2.push_back (ConstantUInt::get (Type::UIntTy, Size));
1252 CV2.push_back (ConstantArray::get (AT2, allstate));
1253 new GlobalVariable (ST2, true, GlobalValue::InternalLinkage,
1254 ConstantStruct::get (ST2, CV2), "_llvm_regAllocState",
1255 &M);
1256 return false; // No error.
1257}
1258
1259
Brian Gaekeaf843702003-10-22 20:22:53 +00001260/// Allocate registers for the machine code previously generated for F using
1261/// the graph-coloring algorithm.
1262///
Brian Gaeke4efe3422003-09-21 01:23:46 +00001263bool PhyRegAlloc::runOnFunction (Function &F) {
1264 if (DEBUG_RA)
1265 std::cerr << "\n********* Function "<< F.getName () << " ***********\n";
1266
1267 Fn = &F;
1268 MF = &MachineFunction::get (Fn);
1269 LVI = &getAnalysis<FunctionLiveVarInfo> ();
1270 LRI = new LiveRangeInfo (Fn, TM, RegClassList);
1271 LoopDepthCalc = &getAnalysis<LoopInfo> ();
1272
1273 // Create each RegClass for the target machine and add it to the
1274 // RegClassList. This must be done before calling constructLiveRanges().
1275 for (unsigned rc = 0; rc != NumOfRegClasses; ++rc)
1276 RegClassList.push_back (new RegClass (Fn, &TM.getRegInfo (),
1277 MRI.getMachineRegClass (rc)));
1278
1279 LRI->constructLiveRanges(); // create LR info
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001280 if (DEBUG_RA >= RA_DEBUG_LiveRanges)
Brian Gaeke4efe3422003-09-21 01:23:46 +00001281 LRI->printLiveRanges();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001282
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001283 createIGNodeListsAndIGs(); // create IGNode list and IGs
1284
1285 buildInterferenceGraphs(); // build IGs in all reg classes
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001286
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001287 if (DEBUG_RA >= RA_DEBUG_LiveRanges) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001288 // print all LRs in all reg classes
Chris Lattner7e708292002-06-25 16:13:24 +00001289 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
1290 RegClassList[rc]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001291
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001292 // print IGs in all register classes
Chris Lattner7e708292002-06-25 16:13:24 +00001293 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
1294 RegClassList[rc]->printIG();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001295 }
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001296
Brian Gaeke4efe3422003-09-21 01:23:46 +00001297 LRI->coalesceLRs(); // coalesce all live ranges
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +00001298
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001299 if (DEBUG_RA >= RA_DEBUG_LiveRanges) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001300 // print all LRs in all reg classes
Chris Lattnerf726e772002-10-28 19:22:04 +00001301 for (unsigned rc=0; rc < NumOfRegClasses; rc++)
1302 RegClassList[rc]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001303
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001304 // print IGs in all register classes
Chris Lattnerf726e772002-10-28 19:22:04 +00001305 for (unsigned rc=0; rc < NumOfRegClasses; rc++)
1306 RegClassList[rc]->printIG();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001307 }
1308
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001309 // mark un-usable suggested color before graph coloring algorithm.
1310 // When this is done, the graph coloring algo will not reserve
1311 // suggested color unnecessarily - they can be used by another LR
1312 markUnusableSugColors();
1313
1314 // color all register classes using the graph coloring algo
Chris Lattner7e708292002-06-25 16:13:24 +00001315 for (unsigned rc=0; rc < NumOfRegClasses ; rc++)
Chris Lattnerf726e772002-10-28 19:22:04 +00001316 RegClassList[rc]->colorAllRegs();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001317
Misha Brukman37f92e22003-09-11 22:34:13 +00001318 // After graph coloring, if some LRs did not receive a color (i.e, spilled)
1319 // a position for such spilled LRs
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001320 allocateStackSpace4SpilledLRs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001321
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001322 // Reset the temp. area on the stack before use by the first instruction.
1323 // This will also happen after updating each instruction.
Brian Gaeke4efe3422003-09-21 01:23:46 +00001324 MF->getInfo()->popAllTempValues();
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001325
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001326 // color incoming args - if the correct color was not received
1327 // insert code to copy to the correct register
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001328 colorIncomingArgs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001329
Brian Gaeke6a256cc2003-09-24 18:08:54 +00001330 // Save register allocation state for this function in a Constant.
1331 if (SaveRegAllocState)
1332 saveState();
Brian Gaekeaf843702003-10-22 20:22:53 +00001333 if (DEBUG_RA) { // Check our work.
1334 verifySavedState ();
1335 }
Brian Gaeke6a256cc2003-09-24 18:08:54 +00001336
Brian Gaeke60a3c552003-10-22 20:44:23 +00001337 // Now update the machine code with register names and add any additional
1338 // code inserted by the register allocator to the instruction stream.
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001339 updateMachineCode();
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001340
Chris Lattner045e7c82001-09-19 16:26:23 +00001341 if (DEBUG_RA) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +00001342 std::cerr << "\n**** Machine Code After Register Allocation:\n\n";
Brian Gaeke4efe3422003-09-21 01:23:46 +00001343 MF->dump();
Chris Lattner045e7c82001-09-19 16:26:23 +00001344 }
Brian Gaeke4efe3422003-09-21 01:23:46 +00001345
1346 // Tear down temporary data structures
1347 for (unsigned rc = 0; rc < NumOfRegClasses; ++rc)
1348 delete RegClassList[rc];
1349 RegClassList.clear ();
1350 AddedInstrMap.clear ();
1351 OperandsColoredMap.clear ();
1352 ScratchRegsUsed.clear ();
1353 AddedInstrAtEntry.clear ();
1354 delete LRI;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001355
Brian Gaeke4efe3422003-09-21 01:23:46 +00001356 if (DEBUG_RA) std::cerr << "\nRegister allocation complete!\n";
1357 return false; // Function was not modified
1358}