blob: 82ba0cc693cd7070f8e40a12cc8f406856a9e647 [file] [log] [blame]
Chris Lattner02e7a862002-08-09 20:08:03 +00001//===-- PhyRegAlloc.cpp ---------------------------------------------------===//
Vikram S. Adved9f85982001-11-08 04:48:50 +00002//
John Criswell482202a2003-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 Gaeke974bc662003-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 Lattner02e7a862002-08-09 20:08:03 +000020//
21//===----------------------------------------------------------------------===//
Ruchira Sasanka808568e2001-09-14 21:18:34 +000022
Brian Gaeke390d31c2003-10-23 20:32:55 +000023#include "AllocInfo.h"
Misha Brukmandc077752003-10-23 18:06:27 +000024#include "IGNode.h"
Chris Lattnerf1ab45b2003-09-01 20:09:04 +000025#include "PhyRegAlloc.h"
Chris Lattnera23969b2003-01-15 19:57:07 +000026#include "RegAllocCommon.h"
Chris Lattner1ff57d92003-01-15 21:14:01 +000027#include "RegClass.h"
Misha Brukmandc077752003-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 Lattner1f8d21e2003-09-30 20:13:59 +000034#include "llvm/CodeGen/FunctionLiveVarInfo.h"
35#include "llvm/CodeGen/InstrSelection.h"
Misha Brukmandc077752003-10-23 18:06:27 +000036#include "llvm/CodeGen/MachineFunction.h"
37#include "llvm/CodeGen/MachineFunctionInfo.h"
Brian Gaeke20c888f2003-09-21 02:50:21 +000038#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner81289362003-01-15 18:08:07 +000039#include "llvm/CodeGen/MachineInstrBuilder.h"
Vikram S. Adve2780d2d2002-05-19 15:29:31 +000040#include "llvm/CodeGen/MachineInstrAnnot.h"
Chris Lattner1f8d21e2003-09-30 20:13:59 +000041#include "llvm/CodeGen/Passes.h"
Chris Lattner1f8d21e2003-09-30 20:13:59 +000042#include "llvm/Support/InstIterator.h"
Misha Brukmandc077752003-10-23 18:06:27 +000043#include "llvm/Target/TargetInstrInfo.h"
Chris Lattnera73d6652002-09-15 07:07:55 +000044#include "Support/CommandLine.h"
Misha Brukmandc077752003-10-23 18:06:27 +000045#include "Support/SetOperations.h"
46#include "Support/STLExtras.h"
Brian Gaekee383a142003-09-21 03:57:37 +000047#include <cmath>
Vikram S. Adved9f85982001-11-08 04:48:50 +000048
Chris Lattnerafc86e92002-05-22 17:08:27 +000049RegAllocDebugLevel_t DEBUG_RA;
Vikram S. Adve0e56b362002-09-14 23:05:33 +000050
Chris Lattnerf5cad152002-07-22 02:10:13 +000051static cl::opt<RegAllocDebugLevel_t, true>
52DRA_opt("dregalloc", cl::Hidden, cl::location(DEBUG_RA),
53 cl::desc("enable register allocation debugging information"),
54 cl::values(
Vikram S. Adve0e56b362002-09-14 23:05:33 +000055 clEnumValN(RA_DEBUG_None , "n", "disable debug output"),
56 clEnumValN(RA_DEBUG_Results, "y", "debug output for allocation results"),
57 clEnumValN(RA_DEBUG_Coloring, "c", "debug output for graph coloring step"),
58 clEnumValN(RA_DEBUG_Interference,"ig","debug output for interference graphs"),
59 clEnumValN(RA_DEBUG_LiveRanges , "lr","debug output for live ranges"),
60 clEnumValN(RA_DEBUG_Verbose, "v", "extra debug output"),
Chris Lattnerf5cad152002-07-22 02:10:13 +000061 0));
Ruchira Sasanka8efbd102001-09-15 21:11:11 +000062
Brian Gaeke82585e02003-09-24 17:50:28 +000063static cl::opt<bool>
64SaveRegAllocState("save-ra-state", cl::Hidden,
65 cl::desc("write reg. allocator state into module"));
66
Brian Gaeke8c14ba92003-08-14 06:09:32 +000067FunctionPass *getRegisterAllocator(TargetMachine &T) {
Brian Gaekee1061012003-09-21 01:23:46 +000068 return new PhyRegAlloc (T);
Chris Lattnerf48173a2002-02-04 15:54:09 +000069}
Chris Lattner36aa5422002-02-04 00:33:08 +000070
Chris Lattner6b379da2003-09-23 15:13:04 +000071void PhyRegAlloc::getAnalysisUsage(AnalysisUsage &AU) const {
72 AU.addRequired<LoopInfo> ();
73 AU.addRequired<FunctionLiveVarInfo> ();
74}
75
76
Brian Gaekee3cf0722003-10-22 20:22:53 +000077/// Initialize interference graphs (one in each reg class) and IGNodeLists
78/// (one in each IG). The actual nodes will be pushed later.
79///
Chris Lattnerabafc3b2002-02-03 07:46:34 +000080void PhyRegAlloc::createIGNodeListsAndIGs() {
Chris Lattner71270652003-09-01 20:05:47 +000081 if (DEBUG_RA >= RA_DEBUG_LiveRanges) std::cerr << "Creating LR lists ...\n";
Ruchira Sasanka808568e2001-09-14 21:18:34 +000082
Brian Gaekee1061012003-09-21 01:23:46 +000083 LiveRangeMapType::const_iterator HMI = LRI->getLiveRangeMap()->begin();
Brian Gaekee1061012003-09-21 01:23:46 +000084 LiveRangeMapType::const_iterator HMIEnd = LRI->getLiveRangeMap()->end();
Ruchira Sasanka808568e2001-09-14 21:18:34 +000085
Chris Lattnerabafc3b2002-02-03 07:46:34 +000086 for (; HMI != HMIEnd ; ++HMI ) {
87 if (HMI->first) {
88 LiveRange *L = HMI->second; // get the LiveRange
89 if (!L) {
Vikram S. Adve0e56b362002-09-14 23:05:33 +000090 if (DEBUG_RA)
Chris Lattner71270652003-09-01 20:05:47 +000091 std::cerr << "\n**** ?!?WARNING: NULL LIVE RANGE FOUND FOR: "
Vikram S. Adve0e56b362002-09-14 23:05:33 +000092 << RAV(HMI->first) << "****\n";
Chris Lattnerabafc3b2002-02-03 07:46:34 +000093 continue;
94 }
Vikram S. Adve0e56b362002-09-14 23:05:33 +000095
96 // if the Value * is not null, and LR is not yet written to the IGNodeList
Chris Lattner113f4f42002-06-25 16:13:24 +000097 if (!(L->getUserIGNode()) ) {
Chris Lattnerabafc3b2002-02-03 07:46:34 +000098 RegClass *const RC = // RegClass of first value in the LR
Brian Gaeke82585e02003-09-24 17:50:28 +000099 RegClassList[ L->getRegClassID() ];
Chris Lattnerabafc3b2002-02-03 07:46:34 +0000100 RC->addLRToIG(L); // add this LR to an IG
101 }
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000102 }
103 }
Chris Lattnerabafc3b2002-02-03 07:46:34 +0000104
105 // init RegClassList
Chris Lattner113f4f42002-06-25 16:13:24 +0000106 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
Chris Lattnerabafc3b2002-02-03 07:46:34 +0000107 RegClassList[rc]->createInterferenceGraph();
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000108
Chris Lattner71270652003-09-01 20:05:47 +0000109 if (DEBUG_RA >= RA_DEBUG_LiveRanges) std::cerr << "LRLists Created!\n";
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000110}
111
112
Brian Gaekee3cf0722003-10-22 20:22:53 +0000113/// Add all interferences for a given instruction. Interference occurs only
114/// if the LR of Def (Inst or Arg) is of the same reg class as that of live
115/// var. The live var passed to this function is the LVset AFTER the
116/// instruction.
117///
118void PhyRegAlloc::addInterference(const Value *Def, const ValueSet *LVSet,
Chris Lattnerb0af9cd2002-02-05 02:52:05 +0000119 bool isCallInst) {
Chris Lattnerb0af9cd2002-02-05 02:52:05 +0000120 ValueSet::const_iterator LIt = LVSet->begin();
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000121
122 // get the live range of instruction
Brian Gaekee1061012003-09-21 01:23:46 +0000123 const LiveRange *const LROfDef = LRI->getLiveRangeForValue( Def );
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000124
125 IGNode *const IGNodeOfDef = LROfDef->getUserIGNode();
126 assert( IGNodeOfDef );
127
128 RegClass *const RCOfDef = LROfDef->getRegClass();
129
130 // for each live var in live variable set
Chris Lattner113f4f42002-06-25 16:13:24 +0000131 for ( ; LIt != LVSet->end(); ++LIt) {
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000132
Vikram S. Adve7228f0c2002-07-08 23:15:32 +0000133 if (DEBUG_RA >= RA_DEBUG_Verbose)
Chris Lattner71270652003-09-01 20:05:47 +0000134 std::cerr << "< Def=" << RAV(Def) << ", Lvar=" << RAV(*LIt) << "> ";
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000135
136 // get the live range corresponding to live var
Brian Gaekee1061012003-09-21 01:23:46 +0000137 LiveRange *LROfVar = LRI->getLiveRangeForValue(*LIt);
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000138
139 // LROfVar can be null if it is a const since a const
140 // doesn't have a dominating def - see Assumptions above
Vikram S. Adve0e56b362002-09-14 23:05:33 +0000141 if (LROfVar)
142 if (LROfDef != LROfVar) // do not set interf for same LR
143 if (RCOfDef == LROfVar->getRegClass()) // 2 reg classes are the same
144 RCOfDef->setInterference( LROfDef, LROfVar);
Ruchira Sasanka09d7a2a2001-10-19 17:21:03 +0000145 }
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000146}
147
Ruchira Sasanka09d7a2a2001-10-19 17:21:03 +0000148
Brian Gaekee3cf0722003-10-22 20:22:53 +0000149/// For a call instruction, this method sets the CallInterference flag in
150/// the LR of each variable live in the Live Variable Set live after the
151/// call instruction (except the return value of the call instruction - since
152/// the return value does not interfere with that call itself).
153///
Ruchira Sasanka09d7a2a2001-10-19 17:21:03 +0000154void PhyRegAlloc::setCallInterferences(const MachineInstr *MInst,
Chris Lattnerb0af9cd2002-02-05 02:52:05 +0000155 const ValueSet *LVSetAft) {
Vikram S. Adve0e56b362002-09-14 23:05:33 +0000156 if (DEBUG_RA >= RA_DEBUG_Interference)
Chris Lattner71270652003-09-01 20:05:47 +0000157 std::cerr << "\n For call inst: " << *MInst;
Ruchira Sasanka09d7a2a2001-10-19 17:21:03 +0000158
Ruchira Sasanka09d7a2a2001-10-19 17:21:03 +0000159 // for each live var in live variable set after machine inst
Vikram S. Adve1fce4cfa92003-07-02 01:24:00 +0000160 for (ValueSet::const_iterator LIt = LVSetAft->begin(), LEnd = LVSetAft->end();
161 LIt != LEnd; ++LIt) {
Ruchira Sasanka09d7a2a2001-10-19 17:21:03 +0000162
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +0000163 // get the live range corresponding to live var
Brian Gaekee1061012003-09-21 01:23:46 +0000164 LiveRange *const LR = LRI->getLiveRangeForValue(*LIt );
Ruchira Sasanka09d7a2a2001-10-19 17:21:03 +0000165
Ruchira Sasanka09d7a2a2001-10-19 17:21:03 +0000166 // LR can be null if it is a const since a const
167 // doesn't have a dominating def - see Assumptions above
Vikram S. Adve0e56b362002-09-14 23:05:33 +0000168 if (LR ) {
169 if (DEBUG_RA >= RA_DEBUG_Interference) {
Chris Lattner71270652003-09-01 20:05:47 +0000170 std::cerr << "\n\tLR after Call: ";
Vikram S. Adve0e56b362002-09-14 23:05:33 +0000171 printSet(*LR);
172 }
Ruchira Sasanka09d7a2a2001-10-19 17:21:03 +0000173 LR->setCallInterference();
Vikram S. Adve0e56b362002-09-14 23:05:33 +0000174 if (DEBUG_RA >= RA_DEBUG_Interference) {
Chris Lattner71270652003-09-01 20:05:47 +0000175 std::cerr << "\n ++After adding call interference for LR: " ;
Chris Lattnerb0af9cd2002-02-05 02:52:05 +0000176 printSet(*LR);
Ruchira Sasanka09d7a2a2001-10-19 17:21:03 +0000177 }
178 }
179
180 }
181
Vikram S. Adveaa2373d2002-03-31 18:54:37 +0000182 // Now find the LR of the return value of the call
183 // We do this because, we look at the LV set *after* the instruction
184 // to determine, which LRs must be saved across calls. The return value
185 // of the call is live in this set - but it does not interfere with call
186 // (i.e., we can allocate a volatile register to the return value)
Vikram S. Adve2780d2d2002-05-19 15:29:31 +0000187 CallArgsDescriptor* argDesc = CallArgsDescriptor::get(MInst);
188
189 if (const Value *RetVal = argDesc->getReturnValue()) {
Brian Gaekee1061012003-09-21 01:23:46 +0000190 LiveRange *RetValLR = LRI->getLiveRangeForValue( RetVal );
Vikram S. Adveaa2373d2002-03-31 18:54:37 +0000191 assert( RetValLR && "No LR for RetValue of call");
192 RetValLR->clearCallInterference();
193 }
194
195 // If the CALL is an indirect call, find the LR of the function pointer.
196 // That has a call interference because it conflicts with outgoing args.
Chris Lattner113f4f42002-06-25 16:13:24 +0000197 if (const Value *AddrVal = argDesc->getIndirectFuncPtr()) {
Brian Gaekee1061012003-09-21 01:23:46 +0000198 LiveRange *AddrValLR = LRI->getLiveRangeForValue( AddrVal );
Vikram S. Adveaa2373d2002-03-31 18:54:37 +0000199 assert( AddrValLR && "No LR for indirect addr val of call");
200 AddrValLR->setCallInterference();
201 }
Ruchira Sasanka09d7a2a2001-10-19 17:21:03 +0000202}
203
204
Brian Gaekee3cf0722003-10-22 20:22:53 +0000205/// Create interferences in the IG of each RegClass, and calculate the spill
206/// cost of each Live Range (it is done in this method to save another pass
207/// over the code).
208///
209void PhyRegAlloc::buildInterferenceGraphs() {
Vikram S. Adve0e56b362002-09-14 23:05:33 +0000210 if (DEBUG_RA >= RA_DEBUG_Interference)
Chris Lattner71270652003-09-01 20:05:47 +0000211 std::cerr << "Creating interference graphs ...\n";
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000212
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +0000213 unsigned BBLoopDepthCost;
Brian Gaekee1061012003-09-21 01:23:46 +0000214 for (MachineFunction::iterator BBI = MF->begin(), BBE = MF->end();
Chris Lattner62b7fd12002-04-07 20:49:59 +0000215 BBI != BBE; ++BBI) {
Chris Lattnerc9bd2c32002-10-28 19:22:04 +0000216 const MachineBasicBlock &MBB = *BBI;
217 const BasicBlock *BB = MBB.getBasicBlock();
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000218
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +0000219 // find the 10^(loop_depth) of this BB
Chris Lattnerc9bd2c32002-10-28 19:22:04 +0000220 BBLoopDepthCost = (unsigned)pow(10.0, LoopDepthCalc->getLoopDepth(BB));
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +0000221
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000222 // get the iterator for machine instructions
Chris Lattnerc9bd2c32002-10-28 19:22:04 +0000223 MachineBasicBlock::const_iterator MII = MBB.begin();
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000224
225 // iterate over all the machine instructions in BB
Chris Lattnerc9bd2c32002-10-28 19:22:04 +0000226 for ( ; MII != MBB.end(); ++MII) {
227 const MachineInstr *MInst = *MII;
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000228
229 // get the LV set after the instruction
Chris Lattnerc9bd2c32002-10-28 19:22:04 +0000230 const ValueSet &LVSetAI = LVI->getLiveVarSetAfterMInst(MInst, BB);
231 bool isCallInst = TM.getInstrInfo().isCall(MInst->getOpCode());
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000232
Brian Gaekee3cf0722003-10-22 20:22:53 +0000233 if (isCallInst) {
Misha Brukmanacda7df2003-09-11 22:34:13 +0000234 // set the isCallInterference flag of each live range which extends
235 // across this call instruction. This information is used by graph
236 // coloring algorithm to avoid allocating volatile colors to live ranges
Ruchira Sasanka09d7a2a2001-10-19 17:21:03 +0000237 // that span across calls (since they have to be saved/restored)
Chris Lattner7e5ee422002-02-05 04:20:12 +0000238 setCallInterferences(MInst, &LVSetAI);
Ruchira Sasanka09d7a2a2001-10-19 17:21:03 +0000239 }
240
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +0000241 // iterate over all MI operands to find defs
Chris Lattnerea13e0a2002-02-05 06:02:59 +0000242 for (MachineInstr::const_val_op_iterator OpI = MInst->begin(),
243 OpE = MInst->end(); OpI != OpE; ++OpI) {
Vikram S. Adve7366fa12003-05-27 00:05:23 +0000244 if (OpI.isDefOnly() || OpI.isDefAndUse()) // create a new LR since def
Chris Lattner7e5ee422002-02-05 04:20:12 +0000245 addInterference(*OpI, &LVSetAI, isCallInst);
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +0000246
247 // Calculate the spill cost of each live range
Brian Gaekee1061012003-09-21 01:23:46 +0000248 LiveRange *LR = LRI->getLiveRangeForValue(*OpI);
Chris Lattnerea13e0a2002-02-05 06:02:59 +0000249 if (LR) LR->addSpillCost(BBLoopDepthCost);
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +0000250 }
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000251
Brian Gaekee3cf0722003-10-22 20:22:53 +0000252 // Mark all operands of pseudo-instructions as interfering with one
253 // another. This must be done because pseudo-instructions may be
254 // expanded to multiple instructions by the assembler, so all the
255 // operands must get distinct registers.
Chris Lattnerabafc3b2002-02-03 07:46:34 +0000256 if (TM.getInstrInfo().isPseudoInstr(MInst->getOpCode()))
Ruchira Sasankaf1acecc2001-11-14 15:33:58 +0000257 addInterf4PseudoInstr(MInst);
258
Ruchira Sasanka5b8971f2001-10-16 01:23:19 +0000259 // Also add interference for any implicit definitions in a machine
260 // instr (currently, only calls have this).
Ruchira Sasanka5b8971f2001-10-16 01:23:19 +0000261 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
Vikram S. Adve7366fa12003-05-27 00:05:23 +0000262 for (unsigned z=0; z < NumOfImpRefs; z++)
263 if (MInst->getImplicitOp(z).opIsDefOnly() ||
264 MInst->getImplicitOp(z).opIsDefAndUse())
265 addInterference( MInst->getImplicitRef(z), &LVSetAI, isCallInst );
Ruchira Sasankab7a39722001-11-03 17:13:27 +0000266
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000267 } // for all machine instructions in BB
Chris Lattner62b7fd12002-04-07 20:49:59 +0000268 } // for all BBs in function
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000269
Misha Brukmanacda7df2003-09-11 22:34:13 +0000270 // add interferences for function arguments. Since there are no explicit
Chris Lattner62b7fd12002-04-07 20:49:59 +0000271 // defs in the function for args, we have to add them manually
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +0000272 addInterferencesForArgs();
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000273
Vikram S. Adve0e56b362002-09-14 23:05:33 +0000274 if (DEBUG_RA >= RA_DEBUG_Interference)
Chris Lattner71270652003-09-01 20:05:47 +0000275 std::cerr << "Interference graphs calculated!\n";
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000276}
277
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +0000278
Brian Gaekee3cf0722003-10-22 20:22:53 +0000279/// Mark all operands of the given MachineInstr as interfering with one
280/// another.
281///
Brian Gaeke43593b82003-09-21 02:24:09 +0000282void PhyRegAlloc::addInterf4PseudoInstr(const MachineInstr *MInst) {
Ruchira Sasankaa5147452001-11-15 15:00:53 +0000283 bool setInterf = false;
284
Brian Gaeke43593b82003-09-21 02:24:09 +0000285 // iterate over MI operands to find defs
Chris Lattnerea13e0a2002-02-05 06:02:59 +0000286 for (MachineInstr::const_val_op_iterator It1 = MInst->begin(),
287 ItE = MInst->end(); It1 != ItE; ++It1) {
Brian Gaekee1061012003-09-21 01:23:46 +0000288 const LiveRange *LROfOp1 = LRI->getLiveRangeForValue(*It1);
Chris Lattner71270652003-09-01 20:05:47 +0000289 assert((LROfOp1 || !It1.isUseOnly())&&"No LR for Def in PSEUDO insruction");
Ruchira Sasankaf1acecc2001-11-14 15:33:58 +0000290
Chris Lattnerea13e0a2002-02-05 06:02:59 +0000291 MachineInstr::const_val_op_iterator It2 = It1;
Chris Lattner113f4f42002-06-25 16:13:24 +0000292 for (++It2; It2 != ItE; ++It2) {
Brian Gaekee1061012003-09-21 01:23:46 +0000293 const LiveRange *LROfOp2 = LRI->getLiveRangeForValue(*It2);
Ruchira Sasankaa5147452001-11-15 15:00:53 +0000294
Chris Lattnerea13e0a2002-02-05 06:02:59 +0000295 if (LROfOp2) {
296 RegClass *RCOfOp1 = LROfOp1->getRegClass();
297 RegClass *RCOfOp2 = LROfOp2->getRegClass();
Ruchira Sasankaf1acecc2001-11-14 15:33:58 +0000298
Chris Lattner113f4f42002-06-25 16:13:24 +0000299 if (RCOfOp1 == RCOfOp2 ){
Ruchira Sasankaf1acecc2001-11-14 15:33:58 +0000300 RCOfOp1->setInterference( LROfOp1, LROfOp2 );
Ruchira Sasankaa5147452001-11-15 15:00:53 +0000301 setInterf = true;
Ruchira Sasankaf1acecc2001-11-14 15:33:58 +0000302 }
Ruchira Sasankaf1acecc2001-11-14 15:33:58 +0000303 } // if Op2 has a LR
Ruchira Sasankaf1acecc2001-11-14 15:33:58 +0000304 } // for all other defs in machine instr
Ruchira Sasankaf1acecc2001-11-14 15:33:58 +0000305 } // for all operands in an instruction
306
Chris Lattnerea13e0a2002-02-05 06:02:59 +0000307 if (!setInterf && MInst->getNumOperands() > 2) {
Chris Lattner71270652003-09-01 20:05:47 +0000308 std::cerr << "\nInterf not set for any operand in pseudo instr:\n";
309 std::cerr << *MInst;
Ruchira Sasankaa5147452001-11-15 15:00:53 +0000310 assert(0 && "Interf not set for pseudo instr with > 2 operands" );
Ruchira Sasankaa5147452001-11-15 15:00:53 +0000311 }
Ruchira Sasankaf1acecc2001-11-14 15:33:58 +0000312}
313
314
Brian Gaekee3cf0722003-10-22 20:22:53 +0000315/// Add interferences for incoming arguments to a function.
316///
Chris Lattnerb0af9cd2002-02-05 02:52:05 +0000317void PhyRegAlloc::addInterferencesForArgs() {
318 // get the InSet of root BB
Chris Lattnerc9bd2c32002-10-28 19:22:04 +0000319 const ValueSet &InSet = LVI->getInSetOfBB(&Fn->front());
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000320
Chris Lattnerc9bd2c32002-10-28 19:22:04 +0000321 for (Function::const_aiterator AI = Fn->abegin(); AI != Fn->aend(); ++AI) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000322 // add interferences between args and LVars at start
323 addInterference(AI, &InSet, false);
324
Vikram S. Adve0e56b362002-09-14 23:05:33 +0000325 if (DEBUG_RA >= RA_DEBUG_Interference)
Brian Gaekee3cf0722003-10-22 20:22:53 +0000326 std::cerr << " - %% adding interference for argument " << RAV(AI) << "\n";
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000327 }
328}
329
330
Brian Gaekee3cf0722003-10-22 20:22:53 +0000331/// The following are utility functions used solely by updateMachineCode and
332/// the functions that it calls. They should probably be folded back into
333/// updateMachineCode at some point.
334///
Vikram S. Adve7a4b3812002-04-25 04:34:15 +0000335
Brian Gaekee3cf0722003-10-22 20:22:53 +0000336// used by: updateMachineCode (1 time), PrependInstructions (1 time)
337inline void InsertBefore(MachineInstr* newMI, MachineBasicBlock& MBB,
338 MachineBasicBlock::iterator& MII) {
Chris Lattnerc9bd2c32002-10-28 19:22:04 +0000339 MII = MBB.insert(MII, newMI);
Vikram S. Adve3e54d6c2002-10-11 16:12:40 +0000340 ++MII;
341}
342
Brian Gaekee3cf0722003-10-22 20:22:53 +0000343// used by: AppendInstructions (1 time)
344inline void InsertAfter(MachineInstr* newMI, MachineBasicBlock& MBB,
345 MachineBasicBlock::iterator& MII) {
Vikram S. Adve3e54d6c2002-10-11 16:12:40 +0000346 ++MII; // insert before the next instruction
Chris Lattnerc9bd2c32002-10-28 19:22:04 +0000347 MII = MBB.insert(MII, newMI);
Vikram S. Adve3e54d6c2002-10-11 16:12:40 +0000348}
349
Brian Gaekee3cf0722003-10-22 20:22:53 +0000350// used by: updateMachineCode (1 time)
351inline void DeleteInstruction(MachineBasicBlock& MBB,
352 MachineBasicBlock::iterator& MII) {
Vikram S. Advea83804a2003-05-31 07:32:01 +0000353 MII = MBB.erase(MII);
354}
355
Brian Gaekee3cf0722003-10-22 20:22:53 +0000356// used by: updateMachineCode (1 time)
357inline void SubstituteInPlace(MachineInstr* newMI, MachineBasicBlock& MBB,
358 MachineBasicBlock::iterator MII) {
Vikram S. Adve3e54d6c2002-10-11 16:12:40 +0000359 *MII = newMI;
360}
361
Brian Gaekee3cf0722003-10-22 20:22:53 +0000362// used by: updateMachineCode (2 times)
363inline void PrependInstructions(std::vector<MachineInstr *> &IBef,
364 MachineBasicBlock& MBB,
365 MachineBasicBlock::iterator& MII,
366 const std::string& msg) {
367 if (!IBef.empty()) {
Vikram S. Adve7a4b3812002-04-25 04:34:15 +0000368 MachineInstr* OrigMI = *MII;
Vikram S. Adve2780d2d2002-05-19 15:29:31 +0000369 std::vector<MachineInstr *>::iterator AdIt;
Brian Gaekee3cf0722003-10-22 20:22:53 +0000370 for (AdIt = IBef.begin(); AdIt != IBef.end() ; ++AdIt) {
Vikram S. Adve7a4b3812002-04-25 04:34:15 +0000371 if (DEBUG_RA) {
Chris Lattner71270652003-09-01 20:05:47 +0000372 if (OrigMI) std::cerr << "For MInst:\n " << *OrigMI;
373 std::cerr << msg << "PREPENDed instr:\n " << **AdIt << "\n";
Vikram S. Adve7a4b3812002-04-25 04:34:15 +0000374 }
Chris Lattnerc9bd2c32002-10-28 19:22:04 +0000375 InsertBefore(*AdIt, MBB, MII);
Vikram S. Adve7a4b3812002-04-25 04:34:15 +0000376 }
377 }
378}
379
Brian Gaekee3cf0722003-10-22 20:22:53 +0000380// used by: updateMachineCode (1 time)
381inline void AppendInstructions(std::vector<MachineInstr *> &IAft,
382 MachineBasicBlock& MBB,
383 MachineBasicBlock::iterator& MII,
384 const std::string& msg) {
385 if (!IAft.empty()) {
Vikram S. Adve7a4b3812002-04-25 04:34:15 +0000386 MachineInstr* OrigMI = *MII;
Vikram S. Adve2780d2d2002-05-19 15:29:31 +0000387 std::vector<MachineInstr *>::iterator AdIt;
Brian Gaekee3cf0722003-10-22 20:22:53 +0000388 for ( AdIt = IAft.begin(); AdIt != IAft.end() ; ++AdIt ) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000389 if (DEBUG_RA) {
Chris Lattner71270652003-09-01 20:05:47 +0000390 if (OrigMI) std::cerr << "For MInst:\n " << *OrigMI;
391 std::cerr << msg << "APPENDed instr:\n " << **AdIt << "\n";
Vikram S. Adve7a4b3812002-04-25 04:34:15 +0000392 }
Chris Lattnerc9bd2c32002-10-28 19:22:04 +0000393 InsertAfter(*AdIt, MBB, MII);
Vikram S. Adve7a4b3812002-04-25 04:34:15 +0000394 }
395 }
396}
397
Brian Gaekee3cf0722003-10-22 20:22:53 +0000398/// Set the registers for operands in the given MachineInstr, if a register was
399/// successfully allocated. Return true if any of its operands has been marked
400/// for spill.
401///
Brian Gaekee1061012003-09-21 01:23:46 +0000402bool PhyRegAlloc::markAllocatedRegs(MachineInstr* MInst)
Vikram S. Advea83804a2003-05-31 07:32:01 +0000403{
Vikram S. Advec9cb3192003-07-29 19:49:21 +0000404 bool instrNeedsSpills = false;
405
Vikram S. Advea83804a2003-05-31 07:32:01 +0000406 // First, set the registers for operands in the machine instruction
407 // if a register was successfully allocated. Do this first because we
408 // will need to know which registers are already used by this instr'n.
Brian Gaekee3cf0722003-10-22 20:22:53 +0000409 for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
Vikram S. Advea83804a2003-05-31 07:32:01 +0000410 MachineOperand& Op = MInst->getOperand(OpNum);
411 if (Op.getType() == MachineOperand::MO_VirtualRegister ||
Brian Gaekee3cf0722003-10-22 20:22:53 +0000412 Op.getType() == MachineOperand::MO_CCRegister) {
Vikram S. Advea83804a2003-05-31 07:32:01 +0000413 const Value *const Val = Op.getVRegValue();
Brian Gaekee1061012003-09-21 01:23:46 +0000414 if (const LiveRange* LR = LRI->getLiveRangeForValue(Val)) {
Vikram S. Advec9cb3192003-07-29 19:49:21 +0000415 // Remember if any operand needs spilling
416 instrNeedsSpills |= LR->isMarkedForSpill();
417
418 // An operand may have a color whether or not it needs spilling
Vikram S. Advea83804a2003-05-31 07:32:01 +0000419 if (LR->hasColor())
420 MInst->SetRegForOperand(OpNum,
Brian Gaeke82585e02003-09-24 17:50:28 +0000421 MRI.getUnifiedRegNum(LR->getRegClassID(),
Vikram S. Advea83804a2003-05-31 07:32:01 +0000422 LR->getColor()));
Vikram S. Advec9cb3192003-07-29 19:49:21 +0000423 }
Vikram S. Advea83804a2003-05-31 07:32:01 +0000424 }
425 } // for each operand
Vikram S. Advec9cb3192003-07-29 19:49:21 +0000426
427 return instrNeedsSpills;
428}
429
Brian Gaekee3cf0722003-10-22 20:22:53 +0000430/// Mark allocated registers (using markAllocatedRegs()) on the instruction
431/// that MII points to. Then, if it's a call instruction, insert caller-saving
432/// code before and after it. Finally, insert spill code before and after it,
433/// using insertCode4SpilledLR().
434///
Vikram S. Advec9cb3192003-07-29 19:49:21 +0000435void PhyRegAlloc::updateInstruction(MachineBasicBlock::iterator& MII,
Brian Gaekee3cf0722003-10-22 20:22:53 +0000436 MachineBasicBlock &MBB) {
Vikram S. Advec9cb3192003-07-29 19:49:21 +0000437 MachineInstr* MInst = *MII;
438 unsigned Opcode = MInst->getOpCode();
439
440 // Reset tmp stack positions so they can be reused for each machine instr.
Brian Gaekee1061012003-09-21 01:23:46 +0000441 MF->getInfo()->popAllTempValues();
Vikram S. Advec9cb3192003-07-29 19:49:21 +0000442
443 // Mark the operands for which regs have been allocated.
Brian Gaekee1061012003-09-21 01:23:46 +0000444 bool instrNeedsSpills = markAllocatedRegs(*MII);
Vikram S. Advec9cb3192003-07-29 19:49:21 +0000445
446#ifndef NDEBUG
447 // Mark that the operands have been updated. Later,
448 // setRelRegsUsedByThisInst() is called to find registers used by each
449 // MachineInst, and it should not be used for an instruction until
450 // this is done. This flag just serves as a sanity check.
Vikram S. Advea83804a2003-05-31 07:32:01 +0000451 OperandsColoredMap[MInst] = true;
Vikram S. Advec9cb3192003-07-29 19:49:21 +0000452#endif
Vikram S. Advea83804a2003-05-31 07:32:01 +0000453
Vikram S. Adve45766ab2003-07-25 21:06:09 +0000454 // Now insert caller-saving code before/after the call.
455 // Do this before inserting spill code since some registers must be
456 // used by save/restore and spill code should not use those registers.
Vikram S. Adve45766ab2003-07-25 21:06:09 +0000457 if (TM.getInstrInfo().isCall(Opcode)) {
Vikram S. Advea83804a2003-05-31 07:32:01 +0000458 AddedInstrns &AI = AddedInstrMap[MInst];
Vikram S. Advec9cb3192003-07-29 19:49:21 +0000459 insertCallerSavingCode(AI.InstrnsBefore, AI.InstrnsAfter, MInst,
460 MBB.getBasicBlock());
Vikram S. Advea83804a2003-05-31 07:32:01 +0000461 }
Vikram S. Adve45766ab2003-07-25 21:06:09 +0000462
Vikram S. Advea83804a2003-05-31 07:32:01 +0000463 // Now insert spill code for remaining operands not allocated to
464 // registers. This must be done even for call return instructions
465 // since those are not handled by the special code above.
Vikram S. Advec9cb3192003-07-29 19:49:21 +0000466 if (instrNeedsSpills)
Brian Gaekee3cf0722003-10-22 20:22:53 +0000467 for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
Vikram S. Advec9cb3192003-07-29 19:49:21 +0000468 MachineOperand& Op = MInst->getOperand(OpNum);
469 if (Op.getType() == MachineOperand::MO_VirtualRegister ||
Brian Gaekee3cf0722003-10-22 20:22:53 +0000470 Op.getType() == MachineOperand::MO_CCRegister) {
Vikram S. Advec9cb3192003-07-29 19:49:21 +0000471 const Value* Val = Op.getVRegValue();
Brian Gaekee1061012003-09-21 01:23:46 +0000472 if (const LiveRange *LR = LRI->getLiveRangeForValue(Val))
Vikram S. Advec9cb3192003-07-29 19:49:21 +0000473 if (LR->isMarkedForSpill())
474 insertCode4SpilledLR(LR, MII, MBB, OpNum);
475 }
476 } // for each operand
Vikram S. Advea83804a2003-05-31 07:32:01 +0000477}
478
Brian Gaekee3cf0722003-10-22 20:22:53 +0000479/// Iterate over all the MachineBasicBlocks in the current function and set
480/// the allocated registers for each instruction (using updateInstruction()),
481/// after register allocation is complete. Then move code out of delay slots.
482///
Vikram S. Advea83804a2003-05-31 07:32:01 +0000483void PhyRegAlloc::updateMachineCode()
484{
Chris Lattner113f4f42002-06-25 16:13:24 +0000485 // Insert any instructions needed at method entry
Brian Gaekee1061012003-09-21 01:23:46 +0000486 MachineBasicBlock::iterator MII = MF->front().begin();
487 PrependInstructions(AddedInstrAtEntry.InstrnsBefore, MF->front(), MII,
Chris Lattner113f4f42002-06-25 16:13:24 +0000488 "At function entry: \n");
489 assert(AddedInstrAtEntry.InstrnsAfter.empty() &&
490 "InstrsAfter should be unnecessary since we are just inserting at "
491 "the function entry point here.");
Vikram S. Adve7a4b3812002-04-25 04:34:15 +0000492
Brian Gaekee1061012003-09-21 01:23:46 +0000493 for (MachineFunction::iterator BBI = MF->begin(), BBE = MF->end();
Chris Lattner62b7fd12002-04-07 20:49:59 +0000494 BBI != BBE; ++BBI) {
Chris Lattnerc9bd2c32002-10-28 19:22:04 +0000495 MachineBasicBlock &MBB = *BBI;
Vikram S. Advea83804a2003-05-31 07:32:01 +0000496
497 // Iterate over all machine instructions in BB and mark operands with
498 // their assigned registers or insert spill code, as appropriate.
499 // Also, fix operands of call/return instructions.
Vikram S. Advea83804a2003-05-31 07:32:01 +0000500 for (MachineBasicBlock::iterator MII = MBB.begin(); MII != MBB.end(); ++MII)
Vikram S. Advec9cb3192003-07-29 19:49:21 +0000501 if (! TM.getInstrInfo().isDummyPhiInstr((*MII)->getOpCode()))
502 updateInstruction(MII, MBB);
Vikram S. Advea83804a2003-05-31 07:32:01 +0000503
504 // Now, move code out of delay slots of branches and returns if needed.
505 // (Also, move "after" code from calls to the last delay slot instruction.)
506 // Moving code out of delay slots is needed in 2 situations:
507 // (1) If this is a branch and it needs instructions inserted after it,
508 // move any existing instructions out of the delay slot so that the
509 // instructions can go into the delay slot. This only supports the
510 // case that #instrsAfter <= #delay slots.
511 //
512 // (2) If any instruction in the delay slot needs
513 // instructions inserted, move it out of the delay slot and before the
514 // branch because putting code before or after it would be VERY BAD!
515 //
516 // If the annul bit of the branch is set, neither of these is legal!
517 // If so, we need to handle spill differently but annulling is not yet used.
Vikram S. Advea83804a2003-05-31 07:32:01 +0000518 for (MachineBasicBlock::iterator MII = MBB.begin();
519 MII != MBB.end(); ++MII)
520 if (unsigned delaySlots =
Brian Gaekee3cf0722003-10-22 20:22:53 +0000521 TM.getInstrInfo().getNumDelaySlots((*MII)->getOpCode())) {
Vikram S. Advec9cb3192003-07-29 19:49:21 +0000522 MachineInstr *MInst = *MII, *DelaySlotMI = *(MII+1);
523
Vikram S. Advea83804a2003-05-31 07:32:01 +0000524 // Check the 2 conditions above:
525 // (1) Does a branch need instructions added after it?
526 // (2) O/w does delay slot instr. need instrns before or after?
Vikram S. Advec9cb3192003-07-29 19:49:21 +0000527 bool isBranch = (TM.getInstrInfo().isBranch(MInst->getOpCode()) ||
528 TM.getInstrInfo().isReturn(MInst->getOpCode()));
529 bool cond1 = (isBranch &&
530 AddedInstrMap.count(MInst) &&
531 AddedInstrMap[MInst].InstrnsAfter.size() > 0);
532 bool cond2 = (AddedInstrMap.count(DelaySlotMI) &&
533 (AddedInstrMap[DelaySlotMI].InstrnsBefore.size() > 0 ||
534 AddedInstrMap[DelaySlotMI].InstrnsAfter.size() > 0));
Vikram S. Advea83804a2003-05-31 07:32:01 +0000535
Brian Gaekee3cf0722003-10-22 20:22:53 +0000536 if (cond1 || cond2) {
Vikram S. Advec9cb3192003-07-29 19:49:21 +0000537 assert((MInst->getOpCodeFlags() & AnnulFlag) == 0 &&
538 "FIXME: Moving an annulled delay slot instruction!");
539 assert(delaySlots==1 &&
540 "InsertBefore does not yet handle >1 delay slots!");
541 InsertBefore(DelaySlotMI, MBB, MII); // MII pts back to branch
Vikram S. Advea83804a2003-05-31 07:32:01 +0000542
543 // In case (1), delete it and don't replace with anything!
544 // Otherwise (i.e., case (2) only) replace it with a NOP.
545 if (cond1) {
Vikram S. Advec9cb3192003-07-29 19:49:21 +0000546 DeleteInstruction(MBB, ++MII); // MII now points to next inst.
547 --MII; // reset MII for ++MII of loop
Vikram S. Advea83804a2003-05-31 07:32:01 +0000548 }
Vikram S. Advec9cb3192003-07-29 19:49:21 +0000549 else
550 SubstituteInPlace(BuildMI(TM.getInstrInfo().getNOPOpCode(),1),
551 MBB, MII+1); // replace with NOP
552
553 if (DEBUG_RA) {
Chris Lattner71270652003-09-01 20:05:47 +0000554 std::cerr << "\nRegAlloc: Moved instr. with added code: "
Vikram S. Advec9cb3192003-07-29 19:49:21 +0000555 << *DelaySlotMI
556 << " out of delay slots of instr: " << *MInst;
Vikram S. Advea83804a2003-05-31 07:32:01 +0000557 }
558 }
Vikram S. Advec9cb3192003-07-29 19:49:21 +0000559 else
560 // For non-branch instr with delay slots (probably a call), move
561 // InstrAfter to the instr. in the last delay slot.
562 move2DelayedInstr(*MII, *(MII+delaySlots));
Vikram S. Advea83804a2003-05-31 07:32:01 +0000563 }
564
565 // Finally iterate over all instructions in BB and insert before/after
Vikram S. Adve45766ab2003-07-25 21:06:09 +0000566 for (MachineBasicBlock::iterator MII=MBB.begin(); MII != MBB.end(); ++MII) {
Vikram S. Adve7a4b3812002-04-25 04:34:15 +0000567 MachineInstr *MInst = *MII;
Vikram S. Adve45766ab2003-07-25 21:06:09 +0000568
Ruchira Sasankad1d5e972001-11-10 21:21:36 +0000569 // do not process Phis
Vikram S. Adve45766ab2003-07-25 21:06:09 +0000570 if (TM.getInstrInfo().isDummyPhiInstr(MInst->getOpCode()))
Ruchira Sasankad1d5e972001-11-10 21:21:36 +0000571 continue;
572
Vikram S. Adve45766ab2003-07-25 21:06:09 +0000573 // if there are any added instructions...
Chris Lattner113f4f42002-06-25 16:13:24 +0000574 if (AddedInstrMap.count(MInst)) {
Vikram S. Adve45766ab2003-07-25 21:06:09 +0000575 AddedInstrns &CallAI = AddedInstrMap[MInst];
576
577#ifndef NDEBUG
Vikram S. Advec9cb3192003-07-29 19:49:21 +0000578 bool isBranch = (TM.getInstrInfo().isBranch(MInst->getOpCode()) ||
579 TM.getInstrInfo().isReturn(MInst->getOpCode()));
580 assert((!isBranch ||
581 AddedInstrMap[MInst].InstrnsAfter.size() <=
582 TM.getInstrInfo().getNumDelaySlots(MInst->getOpCode())) &&
583 "Cannot put more than #delaySlots instrns after "
584 "branch or return! Need to handle temps differently.");
585#endif
586
587#ifndef NDEBUG
Vikram S. Adve45766ab2003-07-25 21:06:09 +0000588 // Temporary sanity checking code to detect whether the same machine
589 // instruction is ever inserted twice before/after a call.
590 // I suspect this is happening but am not sure. --Vikram, 7/1/03.
Vikram S. Adve45766ab2003-07-25 21:06:09 +0000591 std::set<const MachineInstr*> instrsSeen;
592 for (int i = 0, N = CallAI.InstrnsBefore.size(); i < N; ++i) {
593 assert(instrsSeen.count(CallAI.InstrnsBefore[i]) == 0 &&
594 "Duplicate machine instruction in InstrnsBefore!");
595 instrsSeen.insert(CallAI.InstrnsBefore[i]);
596 }
597 for (int i = 0, N = CallAI.InstrnsAfter.size(); i < N; ++i) {
598 assert(instrsSeen.count(CallAI.InstrnsAfter[i]) == 0 &&
599 "Duplicate machine instruction in InstrnsBefore/After!");
600 instrsSeen.insert(CallAI.InstrnsAfter[i]);
601 }
602#endif
603
604 // Now add the instructions before/after this MI.
605 // We do this here to ensure that spill for an instruction is inserted
606 // as close as possible to an instruction (see above insertCode4Spill)
Vikram S. Adve45766ab2003-07-25 21:06:09 +0000607 if (! CallAI.InstrnsBefore.empty())
608 PrependInstructions(CallAI.InstrnsBefore, MBB, MII,"");
609
610 if (! CallAI.InstrnsAfter.empty())
611 AppendInstructions(CallAI.InstrnsAfter, MBB, MII,"");
612
613 } // if there are any added instructions
Ruchira Sasanka5b8971f2001-10-16 01:23:19 +0000614 } // for each machine instruction
Ruchira Sasanka6fd95322001-09-15 19:06:58 +0000615 }
616}
617
618
Brian Gaekee3cf0722003-10-22 20:22:53 +0000619/// Insert spill code for AN operand whose LR was spilled. May be called
620/// repeatedly for a single MachineInstr if it has many spilled operands. On
621/// each call, it finds a register which is not live at that instruction and
622/// also which is not used by other spilled operands of the same
623/// instruction. Then it uses this register temporarily to accommodate the
624/// spilled value.
625///
Ruchira Sasanka19a51652001-11-08 16:43:25 +0000626void PhyRegAlloc::insertCode4SpilledLR(const LiveRange *LR,
Vikram S. Advec9cb3192003-07-29 19:49:21 +0000627 MachineBasicBlock::iterator& MII,
628 MachineBasicBlock &MBB,
Ruchira Sasanka19a51652001-11-08 16:43:25 +0000629 const unsigned OpNum) {
Vikram S. Advec9cb3192003-07-29 19:49:21 +0000630 MachineInstr *MInst = *MII;
631 const BasicBlock *BB = MBB.getBasicBlock();
632
Vikram S. Adve8fef3b82002-09-28 17:02:40 +0000633 assert((! TM.getInstrInfo().isCall(MInst->getOpCode()) || OpNum == 0) &&
634 "Outgoing arg of a call must be handled elsewhere (func arg ok)");
635 assert(! TM.getInstrInfo().isReturn(MInst->getOpCode()) &&
636 "Return value of a ret must be handled elsewhere");
Ruchira Sasanka90668992001-11-15 20:23:19 +0000637
Ruchira Sasanka19a51652001-11-08 16:43:25 +0000638 MachineOperand& Op = MInst->getOperand(OpNum);
Vikram S. Adve7366fa12003-05-27 00:05:23 +0000639 bool isDef = Op.opIsDefOnly();
640 bool isDefAndUse = Op.opIsDefAndUse();
Vikram S. Adve45766ab2003-07-25 21:06:09 +0000641 unsigned RegType = MRI.getRegTypeForLR(LR);
Ruchira Sasanka19a51652001-11-08 16:43:25 +0000642 int SpillOff = LR->getSpillOffFromFP();
643 RegClass *RC = LR->getRegClass();
Vikram S. Advec9cb3192003-07-29 19:49:21 +0000644
645 // Get the live-variable set to find registers free before this instr.
Vikram S. Adve7dc5b672003-08-12 22:22:24 +0000646 const ValueSet &LVSetBef = LVI->getLiveVarSetBeforeMInst(MInst, BB);
647
648#ifndef NDEBUG
649 // If this instr. is in the delay slot of a branch or return, we need to
650 // include all live variables before that branch or return -- we don't want to
651 // trample those! Verify that the set is included in the LV set before MInst.
Vikram S. Advec9cb3192003-07-29 19:49:21 +0000652 if (MII != MBB.begin()) {
653 MachineInstr *PredMI = *(MII-1);
Vikram S. Adve7dc5b672003-08-12 22:22:24 +0000654 if (unsigned DS = TM.getInstrInfo().getNumDelaySlots(PredMI->getOpCode()))
655 assert(set_difference(LVI->getLiveVarSetBeforeMInst(PredMI), LVSetBef)
656 .empty() && "Live-var set before branch should be included in "
657 "live-var set of each delay slot instruction!");
Vikram S. Advec9cb3192003-07-29 19:49:21 +0000658 }
Vikram S. Adve7dc5b672003-08-12 22:22:24 +0000659#endif
Vikram S. Adve6e9422e2001-11-12 23:26:35 +0000660
Brian Gaekee3cf0722003-10-22 20:22:53 +0000661 MF->getInfo()->pushTempValue(MRI.getSpilledRegSize(RegType));
Ruchira Sasanka19a51652001-11-08 16:43:25 +0000662
Chris Lattner71270652003-09-01 20:05:47 +0000663 std::vector<MachineInstr*> MIBef, MIAft;
664 std::vector<MachineInstr*> AdIMid;
Ruchira Sasanka90668992001-11-15 20:23:19 +0000665
Vikram S. Adve5224b192003-07-10 19:42:55 +0000666 // Choose a register to hold the spilled value, if one was not preallocated.
667 // This may insert code before and after MInst to free up the value. If so,
668 // this code should be first/last in the spill sequence before/after MInst.
669 int TmpRegU=(LR->hasColor()
Brian Gaeke82585e02003-09-24 17:50:28 +0000670 ? MRI.getUnifiedRegNum(LR->getRegClassID(),LR->getColor())
Vikram S. Adve5224b192003-07-10 19:42:55 +0000671 : getUsableUniRegAtMI(RegType, &LVSetBef, MInst, MIBef,MIAft));
Ruchira Sasanka90668992001-11-15 20:23:19 +0000672
Vikram S. Adve7228f0c2002-07-08 23:15:32 +0000673 // Set the operand first so that it this register does not get used
674 // as a scratch register for later calls to getUsableUniRegAtMI below
675 MInst->SetRegForOperand(OpNum, TmpRegU);
676
677 // get the added instructions for this instruction
Chris Lattner30e23da2002-04-09 05:13:04 +0000678 AddedInstrns &AI = AddedInstrMap[MInst];
Vikram S. Adve7228f0c2002-07-08 23:15:32 +0000679
680 // We may need a scratch register to copy the spilled value to/from memory.
681 // This may itself have to insert code to free up a scratch register.
682 // Any such code should go before (after) the spill code for a load (store).
Vikram S. Advea83804a2003-05-31 07:32:01 +0000683 // The scratch reg is not marked as used because it is only used
684 // for the copy and not used across MInst.
Vikram S. Adve7228f0c2002-07-08 23:15:32 +0000685 int scratchRegType = -1;
686 int scratchReg = -1;
Brian Gaekee3cf0722003-10-22 20:22:53 +0000687 if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType)) {
Chris Lattnerce64edd2002-10-22 23:16:21 +0000688 scratchReg = getUsableUniRegAtMI(scratchRegType, &LVSetBef,
689 MInst, MIBef, MIAft);
Vikram S. Adve7228f0c2002-07-08 23:15:32 +0000690 assert(scratchReg != MRI.getInvalidRegNum());
Vikram S. Adve7228f0c2002-07-08 23:15:32 +0000691 }
692
693 if (!isDef || isDefAndUse) {
Ruchira Sasanka19a51652001-11-08 16:43:25 +0000694 // for a USE, we have to load the value of LR from stack to a TmpReg
695 // and use the TmpReg as one operand of instruction
Vikram S. Adve2780d2d2002-05-19 15:29:31 +0000696
Vikram S. Adve7228f0c2002-07-08 23:15:32 +0000697 // actual loading instruction(s)
Vikram S. Advec9cb3192003-07-29 19:49:21 +0000698 MRI.cpMem2RegMI(AdIMid, MRI.getFramePointer(), SpillOff, TmpRegU,
699 RegType, scratchReg);
Ruchira Sasankadc709782001-11-08 19:11:30 +0000700
Vikram S. Adve7228f0c2002-07-08 23:15:32 +0000701 // the actual load should be after the instructions to free up TmpRegU
702 MIBef.insert(MIBef.end(), AdIMid.begin(), AdIMid.end());
703 AdIMid.clear();
704 }
705
Vikram S. Adve5224b192003-07-10 19:42:55 +0000706 if (isDef || isDefAndUse) { // if this is a Def
Ruchira Sasanka19a51652001-11-08 16:43:25 +0000707 // for a DEF, we have to store the value produced by this instruction
708 // on the stack position allocated for this LR
Vikram S. Adve2780d2d2002-05-19 15:29:31 +0000709
Vikram S. Adve7228f0c2002-07-08 23:15:32 +0000710 // actual storing instruction(s)
Vikram S. Advec9cb3192003-07-29 19:49:21 +0000711 MRI.cpReg2MemMI(AdIMid, TmpRegU, MRI.getFramePointer(), SpillOff,
712 RegType, scratchReg);
Vikram S. Adve2780d2d2002-05-19 15:29:31 +0000713
Vikram S. Adve7228f0c2002-07-08 23:15:32 +0000714 MIAft.insert(MIAft.begin(), AdIMid.begin(), AdIMid.end());
Ruchira Sasanka19a51652001-11-08 16:43:25 +0000715 } // if !DEF
Vikram S. Adve2780d2d2002-05-19 15:29:31 +0000716
Vikram S. Adve7228f0c2002-07-08 23:15:32 +0000717 // Finally, insert the entire spill code sequences before/after MInst
718 AI.InstrnsBefore.insert(AI.InstrnsBefore.end(), MIBef.begin(), MIBef.end());
719 AI.InstrnsAfter.insert(AI.InstrnsAfter.begin(), MIAft.begin(), MIAft.end());
720
Chris Lattner113f4f42002-06-25 16:13:24 +0000721 if (DEBUG_RA) {
Chris Lattner71270652003-09-01 20:05:47 +0000722 std::cerr << "\nFor Inst:\n " << *MInst;
723 std::cerr << "SPILLED LR# " << LR->getUserIGNode()->getIndex();
724 std::cerr << "; added Instructions:";
Anand Shukla046fe572002-07-09 19:18:56 +0000725 for_each(MIBef.begin(), MIBef.end(), std::mem_fun(&MachineInstr::dump));
726 for_each(MIAft.begin(), MIAft.end(), std::mem_fun(&MachineInstr::dump));
Chris Lattner113f4f42002-06-25 16:13:24 +0000727 }
Ruchira Sasanka19a51652001-11-08 16:43:25 +0000728}
729
730
Brian Gaekee3cf0722003-10-22 20:22:53 +0000731/// Insert caller saving/restoring instructions before/after a call machine
732/// instruction (before or after any other instructions that were inserted for
733/// the call).
734///
Vikram S. Advec9cb3192003-07-29 19:49:21 +0000735void
736PhyRegAlloc::insertCallerSavingCode(std::vector<MachineInstr*> &instrnsBefore,
737 std::vector<MachineInstr*> &instrnsAfter,
738 MachineInstr *CallMI,
Brian Gaekee3cf0722003-10-22 20:22:53 +0000739 const BasicBlock *BB) {
Vikram S. Advec9cb3192003-07-29 19:49:21 +0000740 assert(TM.getInstrInfo().isCall(CallMI->getOpCode()));
741
Brian Gaeke43593b82003-09-21 02:24:09 +0000742 // hash set to record which registers were saved/restored
Vikram S. Advec9cb3192003-07-29 19:49:21 +0000743 hash_set<unsigned> PushedRegSet;
744
745 CallArgsDescriptor* argDesc = CallArgsDescriptor::get(CallMI);
746
747 // if the call is to a instrumentation function, do not insert save and
748 // restore instructions the instrumentation function takes care of save
749 // restore for volatile regs.
750 //
751 // FIXME: this should be made general, not specific to the reoptimizer!
Vikram S. Advec9cb3192003-07-29 19:49:21 +0000752 const Function *Callee = argDesc->getCallInst()->getCalledFunction();
753 bool isLLVMFirstTrigger = Callee && Callee->getName() == "llvm_first_trigger";
754
755 // Now check if the call has a return value (using argDesc) and if so,
756 // find the LR of the TmpInstruction representing the return value register.
757 // (using the last or second-last *implicit operand* of the call MI).
758 // Insert it to to the PushedRegSet since we must not save that register
759 // and restore it after the call.
760 // We do this because, we look at the LV set *after* the instruction
761 // to determine, which LRs must be saved across calls. The return value
762 // of the call is live in this set - but we must not save/restore it.
Vikram S. Advec9cb3192003-07-29 19:49:21 +0000763 if (const Value *origRetVal = argDesc->getReturnValue()) {
764 unsigned retValRefNum = (CallMI->getNumImplicitRefs() -
765 (argDesc->getIndirectFuncPtr()? 1 : 2));
766 const TmpInstruction* tmpRetVal =
767 cast<TmpInstruction>(CallMI->getImplicitRef(retValRefNum));
768 assert(tmpRetVal->getOperand(0) == origRetVal &&
769 tmpRetVal->getType() == origRetVal->getType() &&
770 "Wrong implicit ref?");
Brian Gaekee1061012003-09-21 01:23:46 +0000771 LiveRange *RetValLR = LRI->getLiveRangeForValue(tmpRetVal);
Vikram S. Advec9cb3192003-07-29 19:49:21 +0000772 assert(RetValLR && "No LR for RetValue of call");
773
774 if (! RetValLR->isMarkedForSpill())
775 PushedRegSet.insert(MRI.getUnifiedRegNum(RetValLR->getRegClassID(),
776 RetValLR->getColor()));
777 }
778
779 const ValueSet &LVSetAft = LVI->getLiveVarSetAfterMInst(CallMI, BB);
780 ValueSet::const_iterator LIt = LVSetAft.begin();
781
782 // for each live var in live variable set after machine inst
783 for( ; LIt != LVSetAft.end(); ++LIt) {
Brian Gaeke43593b82003-09-21 02:24:09 +0000784 // get the live range corresponding to live var
Brian Gaekee1061012003-09-21 01:23:46 +0000785 LiveRange *const LR = LRI->getLiveRangeForValue(*LIt);
Vikram S. Advec9cb3192003-07-29 19:49:21 +0000786
787 // LR can be null if it is a const since a const
788 // doesn't have a dominating def - see Assumptions above
Brian Gaekee3cf0722003-10-22 20:22:53 +0000789 if (LR) {
790 if (! LR->isMarkedForSpill()) {
Vikram S. Advec9cb3192003-07-29 19:49:21 +0000791 assert(LR->hasColor() && "LR is neither spilled nor colored?");
792 unsigned RCID = LR->getRegClassID();
793 unsigned Color = LR->getColor();
794
795 if (MRI.isRegVolatile(RCID, Color) ) {
Brian Gaeke43593b82003-09-21 02:24:09 +0000796 // if this is a call to the first-level reoptimizer
797 // instrumentation entry point, and the register is not
798 // modified by call, don't save and restore it.
Vikram S. Advec9cb3192003-07-29 19:49:21 +0000799 if (isLLVMFirstTrigger && !MRI.modifiedByCall(RCID, Color))
800 continue;
801
802 // if the value is in both LV sets (i.e., live before and after
803 // the call machine instruction)
Vikram S. Advec9cb3192003-07-29 19:49:21 +0000804 unsigned Reg = MRI.getUnifiedRegNum(RCID, Color);
805
Brian Gaeke43593b82003-09-21 02:24:09 +0000806 // if we haven't already pushed this register...
Vikram S. Advec9cb3192003-07-29 19:49:21 +0000807 if( PushedRegSet.find(Reg) == PushedRegSet.end() ) {
Vikram S. Advec9cb3192003-07-29 19:49:21 +0000808 unsigned RegType = MRI.getRegTypeForLR(LR);
809
810 // Now get two instructions - to push on stack and pop from stack
811 // and add them to InstrnsBefore and InstrnsAfter of the
812 // call instruction
Vikram S. Advec9cb3192003-07-29 19:49:21 +0000813 int StackOff =
Brian Gaekee1061012003-09-21 01:23:46 +0000814 MF->getInfo()->pushTempValue(MRI.getSpilledRegSize(RegType));
Vikram S. Advec9cb3192003-07-29 19:49:21 +0000815
816 //---- Insert code for pushing the reg on stack ----------
817
818 std::vector<MachineInstr*> AdIBef, AdIAft;
819
820 // We may need a scratch register to copy the saved value
821 // to/from memory. This may itself have to insert code to
822 // free up a scratch register. Any such code should go before
823 // the save code. The scratch register, if any, is by default
824 // temporary and not "used" by the instruction unless the
825 // copy code itself decides to keep the value in the scratch reg.
826 int scratchRegType = -1;
827 int scratchReg = -1;
828 if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType))
829 { // Find a register not live in the LVSet before CallMI
830 const ValueSet &LVSetBef =
831 LVI->getLiveVarSetBeforeMInst(CallMI, BB);
832 scratchReg = getUsableUniRegAtMI(scratchRegType, &LVSetBef,
833 CallMI, AdIBef, AdIAft);
834 assert(scratchReg != MRI.getInvalidRegNum());
835 }
836
837 if (AdIBef.size() > 0)
838 instrnsBefore.insert(instrnsBefore.end(),
839 AdIBef.begin(), AdIBef.end());
840
841 MRI.cpReg2MemMI(instrnsBefore, Reg, MRI.getFramePointer(),
842 StackOff, RegType, scratchReg);
843
844 if (AdIAft.size() > 0)
845 instrnsBefore.insert(instrnsBefore.end(),
846 AdIAft.begin(), AdIAft.end());
847
848 //---- Insert code for popping the reg from the stack ----------
Vikram S. Advec9cb3192003-07-29 19:49:21 +0000849 AdIBef.clear();
850 AdIAft.clear();
851
852 // We may need a scratch register to copy the saved value
853 // from memory. This may itself have to insert code to
854 // free up a scratch register. Any such code should go
855 // after the save code. As above, scratch is not marked "used".
Vikram S. Advec9cb3192003-07-29 19:49:21 +0000856 scratchRegType = -1;
857 scratchReg = -1;
858 if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType))
859 { // Find a register not live in the LVSet after CallMI
860 scratchReg = getUsableUniRegAtMI(scratchRegType, &LVSetAft,
861 CallMI, AdIBef, AdIAft);
862 assert(scratchReg != MRI.getInvalidRegNum());
863 }
864
865 if (AdIBef.size() > 0)
866 instrnsAfter.insert(instrnsAfter.end(),
867 AdIBef.begin(), AdIBef.end());
868
869 MRI.cpMem2RegMI(instrnsAfter, MRI.getFramePointer(), StackOff,
870 Reg, RegType, scratchReg);
871
872 if (AdIAft.size() > 0)
873 instrnsAfter.insert(instrnsAfter.end(),
874 AdIAft.begin(), AdIAft.end());
875
876 PushedRegSet.insert(Reg);
877
878 if(DEBUG_RA) {
879 std::cerr << "\nFor call inst:" << *CallMI;
880 std::cerr << " -inserted caller saving instrs: Before:\n\t ";
881 for_each(instrnsBefore.begin(), instrnsBefore.end(),
882 std::mem_fun(&MachineInstr::dump));
883 std::cerr << " -and After:\n\t ";
884 for_each(instrnsAfter.begin(), instrnsAfter.end(),
885 std::mem_fun(&MachineInstr::dump));
886 }
887 } // if not already pushed
Vikram S. Advec9cb3192003-07-29 19:49:21 +0000888 } // if LR has a volatile color
Vikram S. Advec9cb3192003-07-29 19:49:21 +0000889 } // if LR has color
Vikram S. Advec9cb3192003-07-29 19:49:21 +0000890 } // if there is a LR for Var
Vikram S. Advec9cb3192003-07-29 19:49:21 +0000891 } // for each value in the LV set after instruction
892}
893
894
Brian Gaekee3cf0722003-10-22 20:22:53 +0000895/// Returns the unified register number of a temporary register to be used
896/// BEFORE MInst. If no register is available, it will pick one and modify
897/// MIBef and MIAft to contain instructions used to free up this returned
898/// register.
899///
Vikram S. Adve7228f0c2002-07-08 23:15:32 +0000900int PhyRegAlloc::getUsableUniRegAtMI(const int RegType,
901 const ValueSet *LVSetBef,
902 MachineInstr *MInst,
903 std::vector<MachineInstr*>& MIBef,
904 std::vector<MachineInstr*>& MIAft) {
Chris Lattner6a30b022002-10-28 04:45:29 +0000905 RegClass* RC = getRegClassByID(MRI.getRegClassIDOfRegType(RegType));
Vikram S. Adve7228f0c2002-07-08 23:15:32 +0000906
Brian Gaekee3cf0722003-10-22 20:22:53 +0000907 int RegU = getUnusedUniRegAtMI(RC, RegType, MInst, LVSetBef);
Vikram S. Adve7228f0c2002-07-08 23:15:32 +0000908
909 if (RegU == -1) {
Ruchira Sasanka51fc1c22001-11-03 20:41:22 +0000910 // we couldn't find an unused register. Generate code to free up a reg by
Ruchira Sasanka321ed7b2001-10-28 18:12:02 +0000911 // saving it on stack and restoring after the instruction
Vikram S. Adve7228f0c2002-07-08 23:15:32 +0000912
Brian Gaekee1061012003-09-21 01:23:46 +0000913 int TmpOff = MF->getInfo()->pushTempValue(MRI.getSpilledRegSize(RegType));
Vikram S. Adved9f85982001-11-08 04:48:50 +0000914
Vikram S. Adve45766ab2003-07-25 21:06:09 +0000915 RegU = getUniRegNotUsedByThisInst(RC, RegType, MInst);
Vikram S. Adve2780d2d2002-05-19 15:29:31 +0000916
Vikram S. Adve7228f0c2002-07-08 23:15:32 +0000917 // Check if we need a scratch register to copy this register to memory.
918 int scratchRegType = -1;
Brian Gaekee3cf0722003-10-22 20:22:53 +0000919 if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType)) {
Chris Lattner6a30b022002-10-28 04:45:29 +0000920 int scratchReg = getUsableUniRegAtMI(scratchRegType, LVSetBef,
921 MInst, MIBef, MIAft);
Vikram S. Adve7228f0c2002-07-08 23:15:32 +0000922 assert(scratchReg != MRI.getInvalidRegNum());
923
924 // We may as well hold the value in the scratch register instead
925 // of copying it to memory and back. But we have to mark the
926 // register as used by this instruction, so it does not get used
927 // as a scratch reg. by another operand or anyone else.
Chris Lattner30e98742003-08-05 22:11:13 +0000928 ScratchRegsUsed.insert(std::make_pair(MInst, scratchReg));
Vikram S. Adve7228f0c2002-07-08 23:15:32 +0000929 MRI.cpReg2RegMI(MIBef, RegU, scratchReg, RegType);
930 MRI.cpReg2RegMI(MIAft, scratchReg, RegU, RegType);
Brian Gaekee3cf0722003-10-22 20:22:53 +0000931 } else { // the register can be copied directly to/from memory so do it.
Vikram S. Adve7228f0c2002-07-08 23:15:32 +0000932 MRI.cpReg2MemMI(MIBef, RegU, MRI.getFramePointer(), TmpOff, RegType);
933 MRI.cpMem2RegMI(MIAft, MRI.getFramePointer(), TmpOff, RegU, RegType);
Brian Gaekee3cf0722003-10-22 20:22:53 +0000934 }
Ruchira Sasanka321ed7b2001-10-28 18:12:02 +0000935 }
Vikram S. Adve7228f0c2002-07-08 23:15:32 +0000936
Ruchira Sasanka90668992001-11-15 20:23:19 +0000937 return RegU;
Ruchira Sasanka321ed7b2001-10-28 18:12:02 +0000938}
939
Vikram S. Advec9cb3192003-07-29 19:49:21 +0000940
Brian Gaekee3cf0722003-10-22 20:22:53 +0000941/// Returns the register-class register number of a new unused register that
942/// can be used to accommodate a temporary value. May be called repeatedly
943/// for a single MachineInstr. On each call, it finds a register which is not
944/// live at that instruction and which is not used by any spilled operands of
945/// that instruction.
946///
947int PhyRegAlloc::getUnusedUniRegAtMI(RegClass *RC, const int RegType,
Vikram S. Advec9cb3192003-07-29 19:49:21 +0000948 const MachineInstr *MInst,
949 const ValueSet* LVSetBef) {
Vikram S. Adve45766ab2003-07-25 21:06:09 +0000950 RC->clearColorsUsed(); // Reset array
Vikram S. Advec9cb3192003-07-29 19:49:21 +0000951
952 if (LVSetBef == NULL) {
953 LVSetBef = &LVI->getLiveVarSetBeforeMInst(MInst);
954 assert(LVSetBef != NULL && "Unable to get live-var set before MInst?");
955 }
956
Chris Lattnerb0af9cd2002-02-05 02:52:05 +0000957 ValueSet::const_iterator LIt = LVSetBef->begin();
Ruchira Sasanka321ed7b2001-10-28 18:12:02 +0000958
959 // for each live var in live variable set after machine inst
Chris Lattner113f4f42002-06-25 16:13:24 +0000960 for ( ; LIt != LVSetBef->end(); ++LIt) {
Brian Gaeke43593b82003-09-21 02:24:09 +0000961 // Get the live range corresponding to live var, and its RegClass
Brian Gaekee1061012003-09-21 01:23:46 +0000962 LiveRange *const LRofLV = LRI->getLiveRangeForValue(*LIt );
Ruchira Sasanka321ed7b2001-10-28 18:12:02 +0000963
964 // LR can be null if it is a const since a const
965 // doesn't have a dominating def - see Assumptions above
Vikram S. Adve45766ab2003-07-25 21:06:09 +0000966 if (LRofLV && LRofLV->getRegClass() == RC && LRofLV->hasColor())
967 RC->markColorsUsed(LRofLV->getColor(),
968 MRI.getRegTypeForLR(LRofLV), RegType);
Ruchira Sasanka321ed7b2001-10-28 18:12:02 +0000969 }
970
971 // It is possible that one operand of this MInst was already spilled
972 // and it received some register temporarily. If that's the case,
973 // it is recorded in machine operand. We must skip such registers.
Vikram S. Adve45766ab2003-07-25 21:06:09 +0000974 setRelRegsUsedByThisInst(RC, RegType, MInst);
Ruchira Sasanka321ed7b2001-10-28 18:12:02 +0000975
Vikram S. Adve45766ab2003-07-25 21:06:09 +0000976 int unusedReg = RC->getUnusedColor(RegType); // find first unused color
977 if (unusedReg >= 0)
978 return MRI.getUnifiedRegNum(RC->getID(), unusedReg);
979
Chris Lattnerabe98192002-05-23 15:50:03 +0000980 return -1;
Ruchira Sasanka321ed7b2001-10-28 18:12:02 +0000981}
982
983
Brian Gaekee3cf0722003-10-22 20:22:53 +0000984/// Return the unified register number of a register in class RC which is not
985/// used by any operands of MInst.
986///
Ruchira Sasanka90668992001-11-15 20:23:19 +0000987int PhyRegAlloc::getUniRegNotUsedByThisInst(RegClass *RC,
Vikram S. Adve45766ab2003-07-25 21:06:09 +0000988 const int RegType,
Chris Lattnerabe98192002-05-23 15:50:03 +0000989 const MachineInstr *MInst) {
Vikram S. Adve45766ab2003-07-25 21:06:09 +0000990 RC->clearColorsUsed();
Ruchira Sasanka90668992001-11-15 20:23:19 +0000991
Vikram S. Adve45766ab2003-07-25 21:06:09 +0000992 setRelRegsUsedByThisInst(RC, RegType, MInst);
Ruchira Sasanka90668992001-11-15 20:23:19 +0000993
Vikram S. Adve45766ab2003-07-25 21:06:09 +0000994 // find the first unused color
995 int unusedReg = RC->getUnusedColor(RegType);
996 assert(unusedReg >= 0 &&
997 "FATAL: No free register could be found in reg class!!");
Ruchira Sasanka90668992001-11-15 20:23:19 +0000998
Vikram S. Adve45766ab2003-07-25 21:06:09 +0000999 return MRI.getUnifiedRegNum(RC->getID(), unusedReg);
Ruchira Sasanka90668992001-11-15 20:23:19 +00001000}
1001
1002
Brian Gaekee3cf0722003-10-22 20:22:53 +00001003/// Modify the IsColorUsedArr of register class RC, by setting the bits
1004/// corresponding to register RegNo. This is a helper method of
1005/// setRelRegsUsedByThisInst().
1006///
Chris Lattnerb05d3502003-08-05 21:55:58 +00001007static void markRegisterUsed(int RegNo, RegClass *RC, int RegType,
1008 const TargetRegInfo &TRI) {
1009 unsigned classId = 0;
1010 int classRegNum = TRI.getClassRegNum(RegNo, classId);
1011 if (RC->getID() == classId)
1012 RC->markColorsUsed(classRegNum, RegType, RegType);
1013}
1014
1015void PhyRegAlloc::setRelRegsUsedByThisInst(RegClass *RC, int RegType,
Brian Gaekee3cf0722003-10-22 20:22:53 +00001016 const MachineInstr *MI) {
Chris Lattnerb05d3502003-08-05 21:55:58 +00001017 assert(OperandsColoredMap[MI] == true &&
Vikram S. Advea83804a2003-05-31 07:32:01 +00001018 "Illegal to call setRelRegsUsedByThisInst() until colored operands "
1019 "are marked for an instruction.");
Ruchira Sasanka321ed7b2001-10-28 18:12:02 +00001020
Brian Gaekee3cf0722003-10-22 20:22:53 +00001021 // Add the registers already marked as used by the instruction. Both
1022 // explicit and implicit operands are set.
Chris Lattnerb05d3502003-08-05 21:55:58 +00001023 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
1024 if (MI->getOperand(i).hasAllocatedReg())
1025 markRegisterUsed(MI->getOperand(i).getAllocatedRegNum(), RC, RegType,MRI);
1026
1027 for (unsigned i = 0, e = MI->getNumImplicitRefs(); i != e; ++i)
1028 if (MI->getImplicitOp(i).hasAllocatedReg())
1029 markRegisterUsed(MI->getImplicitOp(i).getAllocatedRegNum(), RC,
1030 RegType,MRI);
1031
Chris Lattner30e98742003-08-05 22:11:13 +00001032 // Add all of the scratch registers that are used to save values across the
1033 // instruction (e.g., for saving state register values).
1034 std::pair<ScratchRegsUsedTy::iterator, ScratchRegsUsedTy::iterator>
1035 IR = ScratchRegsUsed.equal_range(MI);
1036 for (ScratchRegsUsedTy::iterator I = IR.first; I != IR.second; ++I)
1037 markRegisterUsed(I->second, RC, RegType, MRI);
Vikram S. Advea83804a2003-05-31 07:32:01 +00001038
Vikram S. Adve7228f0c2002-07-08 23:15:32 +00001039 // If there are implicit references, mark their allocated regs as well
Chris Lattnerb05d3502003-08-05 21:55:58 +00001040 for (unsigned z=0; z < MI->getNumImplicitRefs(); z++)
Vikram S. Adve7228f0c2002-07-08 23:15:32 +00001041 if (const LiveRange*
Brian Gaekee1061012003-09-21 01:23:46 +00001042 LRofImpRef = LRI->getLiveRangeForValue(MI->getImplicitRef(z)))
Vikram S. Adve7228f0c2002-07-08 23:15:32 +00001043 if (LRofImpRef->hasColor())
1044 // this implicit reference is in a LR that received a color
Vikram S. Adve45766ab2003-07-25 21:06:09 +00001045 RC->markColorsUsed(LRofImpRef->getColor(),
1046 MRI.getRegTypeForLR(LRofImpRef), RegType);
Ruchira Sasanka321ed7b2001-10-28 18:12:02 +00001047}
1048
1049
Brian Gaekee3cf0722003-10-22 20:22:53 +00001050/// If there are delay slots for an instruction, the instructions added after
1051/// it must really go after the delayed instruction(s). So, we Move the
1052/// InstrAfter of that instruction to the corresponding delayed instruction
1053/// using the following method.
1054///
Vikram S. Advea83804a2003-05-31 07:32:01 +00001055void PhyRegAlloc::move2DelayedInstr(const MachineInstr *OrigMI,
1056 const MachineInstr *DelayedMI)
1057{
Vikram S. Adve7dc5b672003-08-12 22:22:24 +00001058 // "added after" instructions of the original instr
1059 std::vector<MachineInstr *> &OrigAft = AddedInstrMap[OrigMI].InstrnsAfter;
1060
1061 if (DEBUG_RA && OrigAft.size() > 0) {
Chris Lattner71270652003-09-01 20:05:47 +00001062 std::cerr << "\nRegAlloc: Moved InstrnsAfter for: " << *OrigMI;
1063 std::cerr << " to last delay slot instrn: " << *DelayedMI;
Vikram S. Advec9cb3192003-07-29 19:49:21 +00001064 }
1065
Ruchira Sasankac300c6b2001-10-23 21:38:00 +00001066 // "added after" instructions of the delayed instr
Vikram S. Advec9cb3192003-07-29 19:49:21 +00001067 std::vector<MachineInstr *> &DelayedAft=AddedInstrMap[DelayedMI].InstrnsAfter;
Ruchira Sasankac300c6b2001-10-23 21:38:00 +00001068
1069 // go thru all the "added after instructions" of the original instruction
Vikram S. Advea83804a2003-05-31 07:32:01 +00001070 // and append them to the "added after instructions" of the delayed
Ruchira Sasankac300c6b2001-10-23 21:38:00 +00001071 // instructions
Chris Lattner7f74a562002-01-20 22:54:45 +00001072 DelayedAft.insert(DelayedAft.end(), OrigAft.begin(), OrigAft.end());
Ruchira Sasankac300c6b2001-10-23 21:38:00 +00001073
1074 // empty the "added after instructions" of the original instruction
1075 OrigAft.clear();
Ruchira Sasankac300c6b2001-10-23 21:38:00 +00001076}
Ruchira Sasanka6fd95322001-09-15 19:06:58 +00001077
Ruchira Sasanka8efbd102001-09-15 21:11:11 +00001078
Ruchira Sasankae3b9fad2001-09-30 23:11:59 +00001079void PhyRegAlloc::colorIncomingArgs()
1080{
Brian Gaekee1061012003-09-21 01:23:46 +00001081 MRI.colorMethodArgs(Fn, *LRI, AddedInstrAtEntry.InstrnsBefore,
Vikram S. Advec9cb3192003-07-29 19:49:21 +00001082 AddedInstrAtEntry.InstrnsAfter);
Ruchira Sasankae3b9fad2001-09-30 23:11:59 +00001083}
1084
Ruchira Sasanka1f331f22001-09-18 22:43:57 +00001085
Brian Gaekee3cf0722003-10-22 20:22:53 +00001086/// Determine whether the suggested color of each live range is really usable,
1087/// and then call its setSuggestedColorUsable() method to record the answer. A
1088/// suggested color is NOT usable when the suggested color is volatile AND
1089/// when there are call interferences.
1090///
Ruchira Sasanka01c55ba2001-10-19 21:39:31 +00001091void PhyRegAlloc::markUnusableSugColors()
1092{
Brian Gaekee1061012003-09-21 01:23:46 +00001093 LiveRangeMapType::const_iterator HMI = (LRI->getLiveRangeMap())->begin();
1094 LiveRangeMapType::const_iterator HMIEnd = (LRI->getLiveRangeMap())->end();
Ruchira Sasanka01c55ba2001-10-19 21:39:31 +00001095
Brian Gaeke43593b82003-09-21 02:24:09 +00001096 for (; HMI != HMIEnd ; ++HMI ) {
1097 if (HMI->first) {
1098 LiveRange *L = HMI->second; // get the LiveRange
Brian Gaeke82585e02003-09-24 17:50:28 +00001099 if (L && L->hasSuggestedColor ())
Brian Gaeke1542a8b2003-09-24 18:08:54 +00001100 L->setSuggestedColorUsable
1101 (!(MRI.isRegVolatile (L->getRegClassID (), L->getSuggestedColor ())
1102 && L->isCallInterference ()));
Brian Gaeke43593b82003-09-21 02:24:09 +00001103 }
1104 } // for all LR's in hash map
Ruchira Sasanka01c55ba2001-10-19 21:39:31 +00001105}
1106
1107
Brian Gaekee3cf0722003-10-22 20:22:53 +00001108/// For each live range that is spilled, allocates a new spill position on the
1109/// stack, and set the stack offsets of the live range that will be spilled to
1110/// that position. This must be called just after coloring the LRs.
1111///
Chris Lattnerd30f9892002-02-05 03:52:29 +00001112void PhyRegAlloc::allocateStackSpace4SpilledLRs() {
Chris Lattner71270652003-09-01 20:05:47 +00001113 if (DEBUG_RA) std::cerr << "\nSetting LR stack offsets for spills...\n";
Ruchira Sasanka01c55ba2001-10-19 21:39:31 +00001114
Brian Gaekee1061012003-09-21 01:23:46 +00001115 LiveRangeMapType::const_iterator HMI = LRI->getLiveRangeMap()->begin();
1116 LiveRangeMapType::const_iterator HMIEnd = LRI->getLiveRangeMap()->end();
Ruchira Sasanka321ed7b2001-10-28 18:12:02 +00001117
Chris Lattner113f4f42002-06-25 16:13:24 +00001118 for ( ; HMI != HMIEnd ; ++HMI) {
Chris Lattnerd30f9892002-02-05 03:52:29 +00001119 if (HMI->first && HMI->second) {
Vikram S. Adve5224b192003-07-10 19:42:55 +00001120 LiveRange *L = HMI->second; // get the LiveRange
1121 if (L->isMarkedForSpill()) { // NOTE: allocating size of long Type **
Brian Gaekee1061012003-09-21 01:23:46 +00001122 int stackOffset = MF->getInfo()->allocateSpilledValue(Type::LongTy);
Vikram S. Adve0e56b362002-09-14 23:05:33 +00001123 L->setSpillOffFromFP(stackOffset);
1124 if (DEBUG_RA)
Chris Lattner71270652003-09-01 20:05:47 +00001125 std::cerr << " LR# " << L->getUserIGNode()->getIndex()
Vikram S. Adve0e56b362002-09-14 23:05:33 +00001126 << ": stack-offset = " << stackOffset << "\n";
1127 }
Chris Lattnerd30f9892002-02-05 03:52:29 +00001128 }
1129 } // for all LR's in hash map
Ruchira Sasanka321ed7b2001-10-28 18:12:02 +00001130}
Ruchira Sasanka01c55ba2001-10-19 21:39:31 +00001131
Brian Gaeke20c888f2003-09-21 02:50:21 +00001132
Brian Gaekee3cf0722003-10-22 20:22:53 +00001133/// Save the global register allocation decisions made by the register
1134/// allocator so that they can be accessed later (sort of like "poor man's
1135/// debug info").
1136///
1137void PhyRegAlloc::saveState () {
Brian Gaeke390d31c2003-10-23 20:32:55 +00001138 std::vector<AllocInfo> &state = FnAllocState[Fn];
Brian Gaeke1542a8b2003-09-24 18:08:54 +00001139 unsigned Insn = 0;
1140 LiveRangeMapType::const_iterator HMIEnd = LRI->getLiveRangeMap ()->end ();
1141 for (const_inst_iterator II=inst_begin (Fn), IE=inst_end (Fn); II != IE; ++II)
1142 for (unsigned i = 0; i < (*II)->getNumOperands (); ++i) {
1143 const Value *V = (*II)->getOperand (i);
1144 // Don't worry about it unless it's something whose reg. we'll need.
1145 if (!isa<Argument> (V) && !isa<Instruction> (V))
1146 continue;
1147 LiveRangeMapType::const_iterator HMI = LRI->getLiveRangeMap ()->find (V);
1148 static const unsigned NotAllocated = 0, Allocated = 1, Spilled = 2;
1149 unsigned AllocState = NotAllocated;
1150 int Placement = -1;
1151 if ((HMI != HMIEnd) && HMI->second) {
1152 LiveRange *L = HMI->second;
1153 assert ((L->hasColor () || L->isMarkedForSpill ())
1154 && "Live range exists but not colored or spilled");
1155 if (L->hasColor()) {
1156 AllocState = Allocated;
1157 Placement = MRI.getUnifiedRegNum (L->getRegClassID (),
1158 L->getColor ());
1159 } else if (L->isMarkedForSpill ()) {
1160 AllocState = Spilled;
1161 assert (L->hasSpillOffset ()
1162 && "Live range marked for spill but has no spill offset");
1163 Placement = L->getSpillOffFromFP ();
1164 }
1165 }
Brian Gaeke390d31c2003-10-23 20:32:55 +00001166 state.push_back (AllocInfo (Insn, i, AllocState, Placement));
Brian Gaeke1542a8b2003-09-24 18:08:54 +00001167 }
Brian Gaeke1542a8b2003-09-24 18:08:54 +00001168}
1169
Brian Gaeke390d31c2003-10-23 20:32:55 +00001170
Brian Gaekee3cf0722003-10-22 20:22:53 +00001171/// Check the saved state filled in by saveState(), and abort if it looks
1172/// wrong. Only used when debugging.
1173///
1174void PhyRegAlloc::verifySavedState () {
1175 /// not yet implemented
1176}
Brian Gaeke1542a8b2003-09-24 18:08:54 +00001177
Brian Gaeke390d31c2003-10-23 20:32:55 +00001178/// Finish the job of saveState(), by collapsing FnAllocState into an LLVM
1179/// Constant and stuffing it inside the Module. (NOTE: Soon, there will be
1180/// other, better ways of storing the saved state; this one is cumbersome and
1181/// will never work with the JIT.)
1182///
Brian Gaeke1542a8b2003-09-24 18:08:54 +00001183bool PhyRegAlloc::doFinalization (Module &M) {
1184 if (!SaveRegAllocState)
1185 return false; // Nothing to do here, unless we're saving state.
1186
1187 // Convert FnAllocState to a single Constant array and add it
1188 // to the Module.
1189 ArrayType *AT = ArrayType::get (AllocInfo::getConstantType (), 0);
1190 std::vector<const Type *> TV;
1191 TV.push_back (Type::UIntTy);
1192 TV.push_back (AT);
1193 PointerType *PT = PointerType::get (StructType::get (TV));
1194
1195 std::vector<Constant *> allstate;
1196 for (Module::iterator I = M.begin (), E = M.end (); I != E; ++I) {
1197 Function *F = I;
1198 if (FnAllocState.find (F) == FnAllocState.end ()) {
1199 allstate.push_back (ConstantPointerNull::get (PT));
1200 } else {
Brian Gaeke390d31c2003-10-23 20:32:55 +00001201 std::vector<AllocInfo> &state = FnAllocState[F];
Brian Gaeke41fe18c2003-10-22 20:44:23 +00001202
1203 // Convert state into an LLVM ConstantArray, and put it in a
1204 // ConstantStruct (named S) along with its size.
Brian Gaeke390d31c2003-10-23 20:32:55 +00001205 std::vector<Constant *> stateConstants;
1206 for (unsigned i = 0, s = state.size (); i != s; ++i)
1207 stateConstants.push_back (state[i].toConstant ());
1208 unsigned Size = stateConstants.size ();
Brian Gaeke41fe18c2003-10-22 20:44:23 +00001209 ArrayType *AT = ArrayType::get (AllocInfo::getConstantType (), Size);
1210 std::vector<const Type *> TV;
1211 TV.push_back (Type::UIntTy);
1212 TV.push_back (AT);
1213 StructType *ST = StructType::get (TV);
1214 std::vector<Constant *> CV;
1215 CV.push_back (ConstantUInt::get (Type::UIntTy, Size));
Brian Gaeke390d31c2003-10-23 20:32:55 +00001216 CV.push_back (ConstantArray::get (AT, stateConstants));
Brian Gaeke41fe18c2003-10-22 20:44:23 +00001217 Constant *S = ConstantStruct::get (ST, CV);
1218
Brian Gaeke1542a8b2003-09-24 18:08:54 +00001219 GlobalVariable *GV =
Brian Gaeke41fe18c2003-10-22 20:44:23 +00001220 new GlobalVariable (ST, true,
1221 GlobalValue::InternalLinkage, S,
Brian Gaeke1542a8b2003-09-24 18:08:54 +00001222 F->getName () + ".regAllocState", &M);
Brian Gaeke41fe18c2003-10-22 20:44:23 +00001223
Brian Gaeke1542a8b2003-09-24 18:08:54 +00001224 // Have: { uint, [Size x { uint, uint, uint, int }] } *
1225 // Cast it to: { uint, [0 x { uint, uint, uint, int }] } *
1226 Constant *CE = ConstantExpr::getCast (ConstantPointerRef::get (GV), PT);
1227 allstate.push_back (CE);
1228 }
1229 }
1230
1231 unsigned Size = allstate.size ();
1232 // Final structure type is:
1233 // { uint, [Size x { uint, [0 x { uint, uint, uint, int }] } *] }
1234 std::vector<const Type *> TV2;
1235 TV2.push_back (Type::UIntTy);
1236 ArrayType *AT2 = ArrayType::get (PT, Size);
1237 TV2.push_back (AT2);
1238 StructType *ST2 = StructType::get (TV2);
1239 std::vector<Constant *> CV2;
1240 CV2.push_back (ConstantUInt::get (Type::UIntTy, Size));
1241 CV2.push_back (ConstantArray::get (AT2, allstate));
1242 new GlobalVariable (ST2, true, GlobalValue::InternalLinkage,
1243 ConstantStruct::get (ST2, CV2), "_llvm_regAllocState",
1244 &M);
1245 return false; // No error.
1246}
1247
1248
Brian Gaekee3cf0722003-10-22 20:22:53 +00001249/// Allocate registers for the machine code previously generated for F using
1250/// the graph-coloring algorithm.
1251///
Brian Gaekee1061012003-09-21 01:23:46 +00001252bool PhyRegAlloc::runOnFunction (Function &F) {
1253 if (DEBUG_RA)
1254 std::cerr << "\n********* Function "<< F.getName () << " ***********\n";
1255
1256 Fn = &F;
1257 MF = &MachineFunction::get (Fn);
1258 LVI = &getAnalysis<FunctionLiveVarInfo> ();
1259 LRI = new LiveRangeInfo (Fn, TM, RegClassList);
1260 LoopDepthCalc = &getAnalysis<LoopInfo> ();
1261
1262 // Create each RegClass for the target machine and add it to the
1263 // RegClassList. This must be done before calling constructLiveRanges().
1264 for (unsigned rc = 0; rc != NumOfRegClasses; ++rc)
1265 RegClassList.push_back (new RegClass (Fn, &TM.getRegInfo (),
1266 MRI.getMachineRegClass (rc)));
1267
1268 LRI->constructLiveRanges(); // create LR info
Vikram S. Adve0e56b362002-09-14 23:05:33 +00001269 if (DEBUG_RA >= RA_DEBUG_LiveRanges)
Brian Gaekee1061012003-09-21 01:23:46 +00001270 LRI->printLiveRanges();
Ruchira Sasankae3b9fad2001-09-30 23:11:59 +00001271
Ruchira Sasanka8efbd102001-09-15 21:11:11 +00001272 createIGNodeListsAndIGs(); // create IGNode list and IGs
1273
1274 buildInterferenceGraphs(); // build IGs in all reg classes
Ruchira Sasanka8efbd102001-09-15 21:11:11 +00001275
Vikram S. Adve0e56b362002-09-14 23:05:33 +00001276 if (DEBUG_RA >= RA_DEBUG_LiveRanges) {
Ruchira Sasanka8efbd102001-09-15 21:11:11 +00001277 // print all LRs in all reg classes
Chris Lattner113f4f42002-06-25 16:13:24 +00001278 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
1279 RegClassList[rc]->printIGNodeList();
Ruchira Sasankae3b9fad2001-09-30 23:11:59 +00001280
Ruchira Sasanka8efbd102001-09-15 21:11:11 +00001281 // print IGs in all register classes
Chris Lattner113f4f42002-06-25 16:13:24 +00001282 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
1283 RegClassList[rc]->printIG();
Ruchira Sasanka8efbd102001-09-15 21:11:11 +00001284 }
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +00001285
Brian Gaekee1061012003-09-21 01:23:46 +00001286 LRI->coalesceLRs(); // coalesce all live ranges
Ruchira Sasankab7a39722001-11-03 17:13:27 +00001287
Vikram S. Adve0e56b362002-09-14 23:05:33 +00001288 if (DEBUG_RA >= RA_DEBUG_LiveRanges) {
Ruchira Sasanka8efbd102001-09-15 21:11:11 +00001289 // print all LRs in all reg classes
Chris Lattnerc9bd2c32002-10-28 19:22:04 +00001290 for (unsigned rc=0; rc < NumOfRegClasses; rc++)
1291 RegClassList[rc]->printIGNodeList();
Ruchira Sasankae3b9fad2001-09-30 23:11:59 +00001292
Ruchira Sasanka8efbd102001-09-15 21:11:11 +00001293 // print IGs in all register classes
Chris Lattnerc9bd2c32002-10-28 19:22:04 +00001294 for (unsigned rc=0; rc < NumOfRegClasses; rc++)
1295 RegClassList[rc]->printIG();
Ruchira Sasanka8efbd102001-09-15 21:11:11 +00001296 }
1297
Ruchira Sasanka01c55ba2001-10-19 21:39:31 +00001298 // mark un-usable suggested color before graph coloring algorithm.
1299 // When this is done, the graph coloring algo will not reserve
1300 // suggested color unnecessarily - they can be used by another LR
1301 markUnusableSugColors();
1302
1303 // color all register classes using the graph coloring algo
Chris Lattner113f4f42002-06-25 16:13:24 +00001304 for (unsigned rc=0; rc < NumOfRegClasses ; rc++)
Chris Lattnerc9bd2c32002-10-28 19:22:04 +00001305 RegClassList[rc]->colorAllRegs();
Ruchira Sasanka8efbd102001-09-15 21:11:11 +00001306
Misha Brukmanacda7df2003-09-11 22:34:13 +00001307 // After graph coloring, if some LRs did not receive a color (i.e, spilled)
1308 // a position for such spilled LRs
Ruchira Sasanka321ed7b2001-10-28 18:12:02 +00001309 allocateStackSpace4SpilledLRs();
Ruchira Sasankae3b9fad2001-09-30 23:11:59 +00001310
Vikram S. Advea83804a2003-05-31 07:32:01 +00001311 // Reset the temp. area on the stack before use by the first instruction.
1312 // This will also happen after updating each instruction.
Brian Gaekee1061012003-09-21 01:23:46 +00001313 MF->getInfo()->popAllTempValues();
Ruchira Sasanka7dfa1652001-11-15 22:02:06 +00001314
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +00001315 // color incoming args - if the correct color was not received
1316 // insert code to copy to the correct register
Ruchira Sasankae3b9fad2001-09-30 23:11:59 +00001317 colorIncomingArgs();
Ruchira Sasankae3b9fad2001-09-30 23:11:59 +00001318
Brian Gaeke1542a8b2003-09-24 18:08:54 +00001319 // Save register allocation state for this function in a Constant.
1320 if (SaveRegAllocState)
1321 saveState();
Brian Gaekee3cf0722003-10-22 20:22:53 +00001322 if (DEBUG_RA) { // Check our work.
1323 verifySavedState ();
1324 }
Brian Gaeke1542a8b2003-09-24 18:08:54 +00001325
Brian Gaeke41fe18c2003-10-22 20:44:23 +00001326 // Now update the machine code with register names and add any additional
1327 // code inserted by the register allocator to the instruction stream.
Ruchira Sasanka8efbd102001-09-15 21:11:11 +00001328 updateMachineCode();
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +00001329
Chris Lattnerda876ef2001-09-19 16:26:23 +00001330 if (DEBUG_RA) {
Chris Lattner71270652003-09-01 20:05:47 +00001331 std::cerr << "\n**** Machine Code After Register Allocation:\n\n";
Brian Gaekee1061012003-09-21 01:23:46 +00001332 MF->dump();
Chris Lattnerda876ef2001-09-19 16:26:23 +00001333 }
Brian Gaekee1061012003-09-21 01:23:46 +00001334
1335 // Tear down temporary data structures
1336 for (unsigned rc = 0; rc < NumOfRegClasses; ++rc)
1337 delete RegClassList[rc];
1338 RegClassList.clear ();
1339 AddedInstrMap.clear ();
1340 OperandsColoredMap.clear ();
1341 ScratchRegsUsed.clear ();
1342 AddedInstrAtEntry.clear ();
1343 delete LRI;
Ruchira Sasanka8efbd102001-09-15 21:11:11 +00001344
Brian Gaekee1061012003-09-21 01:23:46 +00001345 if (DEBUG_RA) std::cerr << "\nRegister allocation complete!\n";
1346 return false; // Function was not modified
1347}