blob: c89e5aa0c4c44d0e80ff8dad1d149fd12fec21f9 [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
Misha Brukman396c8c32003-10-23 18:06:27 +000023#include "IGNode.h"
Chris Lattner70b2f562003-09-01 20:09:04 +000024#include "PhyRegAlloc.h"
Chris Lattner4309e732003-01-15 19:57:07 +000025#include "RegAllocCommon.h"
Chris Lattner9d4ed152003-01-15 21:14:01 +000026#include "RegClass.h"
Misha Brukman396c8c32003-10-23 18:06:27 +000027#include "llvm/Constants.h"
28#include "llvm/DerivedTypes.h"
29#include "llvm/iOther.h"
30#include "llvm/Module.h"
31#include "llvm/Type.h"
32#include "llvm/Analysis/LoopInfo.h"
Chris Lattner797c1362003-09-30 20:13:59 +000033#include "llvm/CodeGen/FunctionLiveVarInfo.h"
34#include "llvm/CodeGen/InstrSelection.h"
Misha Brukman396c8c32003-10-23 18:06:27 +000035#include "llvm/CodeGen/MachineFunction.h"
36#include "llvm/CodeGen/MachineFunctionInfo.h"
Brian Gaeke874f4232003-09-21 02:50:21 +000037#include "llvm/CodeGen/MachineInstr.h"
Chris Lattnerf6ee49f2003-01-15 18:08:07 +000038#include "llvm/CodeGen/MachineInstrBuilder.h"
Vikram S. Advedabb41d2002-05-19 15:29:31 +000039#include "llvm/CodeGen/MachineInstrAnnot.h"
Chris Lattner797c1362003-09-30 20:13:59 +000040#include "llvm/CodeGen/Passes.h"
Chris Lattner797c1362003-09-30 20:13:59 +000041#include "llvm/Support/InstIterator.h"
Misha Brukman396c8c32003-10-23 18:06:27 +000042#include "llvm/Target/TargetInstrInfo.h"
Chris Lattner4bc23482002-09-15 07:07:55 +000043#include "Support/CommandLine.h"
Misha Brukman396c8c32003-10-23 18:06:27 +000044#include "Support/SetOperations.h"
45#include "Support/STLExtras.h"
Brian Gaekebd353fb2003-09-21 03:57:37 +000046#include <cmath>
Vikram S. Adve12af1642001-11-08 04:48:50 +000047
Chris Lattner70e60cb2002-05-22 17:08:27 +000048RegAllocDebugLevel_t DEBUG_RA;
Vikram S. Adve39c94e12002-09-14 23:05:33 +000049
Chris Lattner5ff62e92002-07-22 02:10:13 +000050static cl::opt<RegAllocDebugLevel_t, true>
51DRA_opt("dregalloc", cl::Hidden, cl::location(DEBUG_RA),
52 cl::desc("enable register allocation debugging information"),
53 cl::values(
Vikram S. Adve39c94e12002-09-14 23:05:33 +000054 clEnumValN(RA_DEBUG_None , "n", "disable debug output"),
55 clEnumValN(RA_DEBUG_Results, "y", "debug output for allocation results"),
56 clEnumValN(RA_DEBUG_Coloring, "c", "debug output for graph coloring step"),
57 clEnumValN(RA_DEBUG_Interference,"ig","debug output for interference graphs"),
58 clEnumValN(RA_DEBUG_LiveRanges , "lr","debug output for live ranges"),
59 clEnumValN(RA_DEBUG_Verbose, "v", "extra debug output"),
Chris Lattner5ff62e92002-07-22 02:10:13 +000060 0));
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000061
Brian Gaeke59b1c562003-09-24 17:50:28 +000062static cl::opt<bool>
63SaveRegAllocState("save-ra-state", cl::Hidden,
64 cl::desc("write reg. allocator state into module"));
65
Brian Gaekebf3c4cf2003-08-14 06:09:32 +000066FunctionPass *getRegisterAllocator(TargetMachine &T) {
Brian Gaeke4efe3422003-09-21 01:23:46 +000067 return new PhyRegAlloc (T);
Chris Lattner2f9b28e2002-02-04 15:54:09 +000068}
Chris Lattner6dd98a62002-02-04 00:33:08 +000069
Chris Lattner8474f6f2003-09-23 15:13:04 +000070void PhyRegAlloc::getAnalysisUsage(AnalysisUsage &AU) const {
71 AU.addRequired<LoopInfo> ();
72 AU.addRequired<FunctionLiveVarInfo> ();
73}
74
75
Brian Gaekeaf843702003-10-22 20:22:53 +000076/// Initialize interference graphs (one in each reg class) and IGNodeLists
77/// (one in each IG). The actual nodes will be pushed later.
78///
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000079void PhyRegAlloc::createIGNodeListsAndIGs() {
Chris Lattnerc083dcc2003-09-01 20:05:47 +000080 if (DEBUG_RA >= RA_DEBUG_LiveRanges) std::cerr << "Creating LR lists ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +000081
Brian Gaeke4efe3422003-09-21 01:23:46 +000082 LiveRangeMapType::const_iterator HMI = LRI->getLiveRangeMap()->begin();
Brian Gaeke4efe3422003-09-21 01:23:46 +000083 LiveRangeMapType::const_iterator HMIEnd = LRI->getLiveRangeMap()->end();
Ruchira Sasanka8e604792001-09-14 21:18:34 +000084
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000085 for (; HMI != HMIEnd ; ++HMI ) {
86 if (HMI->first) {
87 LiveRange *L = HMI->second; // get the LiveRange
88 if (!L) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +000089 if (DEBUG_RA)
Chris Lattnerc083dcc2003-09-01 20:05:47 +000090 std::cerr << "\n**** ?!?WARNING: NULL LIVE RANGE FOUND FOR: "
Vikram S. Adve39c94e12002-09-14 23:05:33 +000091 << RAV(HMI->first) << "****\n";
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000092 continue;
93 }
Vikram S. Adve39c94e12002-09-14 23:05:33 +000094
95 // if the Value * is not null, and LR is not yet written to the IGNodeList
Chris Lattner7e708292002-06-25 16:13:24 +000096 if (!(L->getUserIGNode()) ) {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000097 RegClass *const RC = // RegClass of first value in the LR
Brian Gaeke59b1c562003-09-24 17:50:28 +000098 RegClassList[ L->getRegClassID() ];
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000099 RC->addLRToIG(L); // add this LR to an IG
100 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000101 }
102 }
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000103
104 // init RegClassList
Chris Lattner7e708292002-06-25 16:13:24 +0000105 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000106 RegClassList[rc]->createInterferenceGraph();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000107
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000108 if (DEBUG_RA >= RA_DEBUG_LiveRanges) std::cerr << "LRLists Created!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000109}
110
111
Brian Gaekeaf843702003-10-22 20:22:53 +0000112/// Add all interferences for a given instruction. Interference occurs only
113/// if the LR of Def (Inst or Arg) is of the same reg class as that of live
114/// var. The live var passed to this function is the LVset AFTER the
115/// instruction.
116///
117void PhyRegAlloc::addInterference(const Value *Def, const ValueSet *LVSet,
Chris Lattner296b7732002-02-05 02:52:05 +0000118 bool isCallInst) {
Chris Lattner296b7732002-02-05 02:52:05 +0000119 ValueSet::const_iterator LIt = LVSet->begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000120
121 // get the live range of instruction
Brian Gaeke4efe3422003-09-21 01:23:46 +0000122 const LiveRange *const LROfDef = LRI->getLiveRangeForValue( Def );
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000123
124 IGNode *const IGNodeOfDef = LROfDef->getUserIGNode();
125 assert( IGNodeOfDef );
126
127 RegClass *const RCOfDef = LROfDef->getRegClass();
128
129 // for each live var in live variable set
Chris Lattner7e708292002-06-25 16:13:24 +0000130 for ( ; LIt != LVSet->end(); ++LIt) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000131
Vikram S. Advef5af6362002-07-08 23:15:32 +0000132 if (DEBUG_RA >= RA_DEBUG_Verbose)
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000133 std::cerr << "< Def=" << RAV(Def) << ", Lvar=" << RAV(*LIt) << "> ";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000134
135 // get the live range corresponding to live var
Brian Gaeke4efe3422003-09-21 01:23:46 +0000136 LiveRange *LROfVar = LRI->getLiveRangeForValue(*LIt);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000137
138 // LROfVar can be null if it is a const since a const
139 // doesn't have a dominating def - see Assumptions above
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000140 if (LROfVar)
141 if (LROfDef != LROfVar) // do not set interf for same LR
142 if (RCOfDef == LROfVar->getRegClass()) // 2 reg classes are the same
143 RCOfDef->setInterference( LROfDef, LROfVar);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000144 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000145}
146
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000147
Brian Gaekeaf843702003-10-22 20:22:53 +0000148/// For a call instruction, this method sets the CallInterference flag in
149/// the LR of each variable live in the Live Variable Set live after the
150/// call instruction (except the return value of the call instruction - since
151/// the return value does not interfere with that call itself).
152///
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000153void PhyRegAlloc::setCallInterferences(const MachineInstr *MInst,
Chris Lattner296b7732002-02-05 02:52:05 +0000154 const ValueSet *LVSetAft) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000155 if (DEBUG_RA >= RA_DEBUG_Interference)
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000156 std::cerr << "\n For call inst: " << *MInst;
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000157
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000158 // for each live var in live variable set after machine inst
Vikram S. Adve65b2f402003-07-02 01:24:00 +0000159 for (ValueSet::const_iterator LIt = LVSetAft->begin(), LEnd = LVSetAft->end();
160 LIt != LEnd; ++LIt) {
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000161
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000162 // get the live range corresponding to live var
Brian Gaeke4efe3422003-09-21 01:23:46 +0000163 LiveRange *const LR = LRI->getLiveRangeForValue(*LIt );
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000164
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000165 // LR can be null if it is a const since a const
166 // doesn't have a dominating def - see Assumptions above
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000167 if (LR ) {
168 if (DEBUG_RA >= RA_DEBUG_Interference) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000169 std::cerr << "\n\tLR after Call: ";
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000170 printSet(*LR);
171 }
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000172 LR->setCallInterference();
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000173 if (DEBUG_RA >= RA_DEBUG_Interference) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000174 std::cerr << "\n ++After adding call interference for LR: " ;
Chris Lattner296b7732002-02-05 02:52:05 +0000175 printSet(*LR);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000176 }
177 }
178
179 }
180
Vikram S. Adve1a53f032002-03-31 18:54:37 +0000181 // Now find the LR of the return value of the call
182 // We do this because, we look at the LV set *after* the instruction
183 // to determine, which LRs must be saved across calls. The return value
184 // of the call is live in this set - but it does not interfere with call
185 // (i.e., we can allocate a volatile register to the return value)
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000186 CallArgsDescriptor* argDesc = CallArgsDescriptor::get(MInst);
187
188 if (const Value *RetVal = argDesc->getReturnValue()) {
Brian Gaeke4efe3422003-09-21 01:23:46 +0000189 LiveRange *RetValLR = LRI->getLiveRangeForValue( RetVal );
Vikram S. Adve1a53f032002-03-31 18:54:37 +0000190 assert( RetValLR && "No LR for RetValue of call");
191 RetValLR->clearCallInterference();
192 }
193
194 // If the CALL is an indirect call, find the LR of the function pointer.
195 // That has a call interference because it conflicts with outgoing args.
Chris Lattner7e708292002-06-25 16:13:24 +0000196 if (const Value *AddrVal = argDesc->getIndirectFuncPtr()) {
Brian Gaeke4efe3422003-09-21 01:23:46 +0000197 LiveRange *AddrValLR = LRI->getLiveRangeForValue( AddrVal );
Vikram S. Adve1a53f032002-03-31 18:54:37 +0000198 assert( AddrValLR && "No LR for indirect addr val of call");
199 AddrValLR->setCallInterference();
200 }
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000201}
202
203
Brian Gaekeaf843702003-10-22 20:22:53 +0000204/// Create interferences in the IG of each RegClass, and calculate the spill
205/// cost of each Live Range (it is done in this method to save another pass
206/// over the code).
207///
208void PhyRegAlloc::buildInterferenceGraphs() {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000209 if (DEBUG_RA >= RA_DEBUG_Interference)
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000210 std::cerr << "Creating interference graphs ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000211
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000212 unsigned BBLoopDepthCost;
Brian Gaeke4efe3422003-09-21 01:23:46 +0000213 for (MachineFunction::iterator BBI = MF->begin(), BBE = MF->end();
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000214 BBI != BBE; ++BBI) {
Chris Lattnerf726e772002-10-28 19:22:04 +0000215 const MachineBasicBlock &MBB = *BBI;
216 const BasicBlock *BB = MBB.getBasicBlock();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000217
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000218 // find the 10^(loop_depth) of this BB
Chris Lattnerf726e772002-10-28 19:22:04 +0000219 BBLoopDepthCost = (unsigned)pow(10.0, LoopDepthCalc->getLoopDepth(BB));
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000220
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000221 // get the iterator for machine instructions
Chris Lattnerf726e772002-10-28 19:22:04 +0000222 MachineBasicBlock::const_iterator MII = MBB.begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000223
224 // iterate over all the machine instructions in BB
Chris Lattnerf726e772002-10-28 19:22:04 +0000225 for ( ; MII != MBB.end(); ++MII) {
226 const MachineInstr *MInst = *MII;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000227
228 // get the LV set after the instruction
Chris Lattnerf726e772002-10-28 19:22:04 +0000229 const ValueSet &LVSetAI = LVI->getLiveVarSetAfterMInst(MInst, BB);
230 bool isCallInst = TM.getInstrInfo().isCall(MInst->getOpCode());
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000231
Brian Gaekeaf843702003-10-22 20:22:53 +0000232 if (isCallInst) {
Misha Brukman37f92e22003-09-11 22:34:13 +0000233 // set the isCallInterference flag of each live range which extends
234 // across this call instruction. This information is used by graph
235 // coloring algorithm to avoid allocating volatile colors to live ranges
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000236 // that span across calls (since they have to be saved/restored)
Chris Lattner748697d2002-02-05 04:20:12 +0000237 setCallInterferences(MInst, &LVSetAI);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000238 }
239
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000240 // iterate over all MI operands to find defs
Chris Lattner2f898d22002-02-05 06:02:59 +0000241 for (MachineInstr::const_val_op_iterator OpI = MInst->begin(),
242 OpE = MInst->end(); OpI != OpE; ++OpI) {
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000243 if (OpI.isDefOnly() || OpI.isDefAndUse()) // create a new LR since def
Chris Lattner748697d2002-02-05 04:20:12 +0000244 addInterference(*OpI, &LVSetAI, isCallInst);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000245
246 // Calculate the spill cost of each live range
Brian Gaeke4efe3422003-09-21 01:23:46 +0000247 LiveRange *LR = LRI->getLiveRangeForValue(*OpI);
Chris Lattner2f898d22002-02-05 06:02:59 +0000248 if (LR) LR->addSpillCost(BBLoopDepthCost);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000249 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000250
Brian Gaekeaf843702003-10-22 20:22:53 +0000251 // Mark all operands of pseudo-instructions as interfering with one
252 // another. This must be done because pseudo-instructions may be
253 // expanded to multiple instructions by the assembler, so all the
254 // operands must get distinct registers.
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000255 if (TM.getInstrInfo().isPseudoInstr(MInst->getOpCode()))
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000256 addInterf4PseudoInstr(MInst);
257
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000258 // Also add interference for any implicit definitions in a machine
259 // instr (currently, only calls have this).
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000260 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000261 for (unsigned z=0; z < NumOfImpRefs; z++)
262 if (MInst->getImplicitOp(z).opIsDefOnly() ||
263 MInst->getImplicitOp(z).opIsDefAndUse())
264 addInterference( MInst->getImplicitRef(z), &LVSetAI, isCallInst );
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +0000265
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000266 } // for all machine instructions in BB
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000267 } // for all BBs in function
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000268
Misha Brukman37f92e22003-09-11 22:34:13 +0000269 // add interferences for function arguments. Since there are no explicit
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000270 // defs in the function for args, we have to add them manually
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000271 addInterferencesForArgs();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000272
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000273 if (DEBUG_RA >= RA_DEBUG_Interference)
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000274 std::cerr << "Interference graphs calculated!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000275}
276
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000277
Brian Gaekeaf843702003-10-22 20:22:53 +0000278/// Mark all operands of the given MachineInstr as interfering with one
279/// another.
280///
Brian Gaeke43ce8fe2003-09-21 02:24:09 +0000281void PhyRegAlloc::addInterf4PseudoInstr(const MachineInstr *MInst) {
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000282 bool setInterf = false;
283
Brian Gaeke43ce8fe2003-09-21 02:24:09 +0000284 // iterate over MI operands to find defs
Chris Lattner2f898d22002-02-05 06:02:59 +0000285 for (MachineInstr::const_val_op_iterator It1 = MInst->begin(),
286 ItE = MInst->end(); It1 != ItE; ++It1) {
Brian Gaeke4efe3422003-09-21 01:23:46 +0000287 const LiveRange *LROfOp1 = LRI->getLiveRangeForValue(*It1);
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000288 assert((LROfOp1 || !It1.isUseOnly())&&"No LR for Def in PSEUDO insruction");
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000289
Chris Lattner2f898d22002-02-05 06:02:59 +0000290 MachineInstr::const_val_op_iterator It2 = It1;
Chris Lattner7e708292002-06-25 16:13:24 +0000291 for (++It2; It2 != ItE; ++It2) {
Brian Gaeke4efe3422003-09-21 01:23:46 +0000292 const LiveRange *LROfOp2 = LRI->getLiveRangeForValue(*It2);
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000293
Chris Lattner2f898d22002-02-05 06:02:59 +0000294 if (LROfOp2) {
295 RegClass *RCOfOp1 = LROfOp1->getRegClass();
296 RegClass *RCOfOp2 = LROfOp2->getRegClass();
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000297
Chris Lattner7e708292002-06-25 16:13:24 +0000298 if (RCOfOp1 == RCOfOp2 ){
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000299 RCOfOp1->setInterference( LROfOp1, LROfOp2 );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000300 setInterf = true;
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000301 }
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000302 } // if Op2 has a LR
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000303 } // for all other defs in machine instr
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000304 } // for all operands in an instruction
305
Chris Lattner2f898d22002-02-05 06:02:59 +0000306 if (!setInterf && MInst->getNumOperands() > 2) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000307 std::cerr << "\nInterf not set for any operand in pseudo instr:\n";
308 std::cerr << *MInst;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000309 assert(0 && "Interf not set for pseudo instr with > 2 operands" );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000310 }
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000311}
312
313
Brian Gaekeaf843702003-10-22 20:22:53 +0000314/// Add interferences for incoming arguments to a function.
315///
Chris Lattner296b7732002-02-05 02:52:05 +0000316void PhyRegAlloc::addInterferencesForArgs() {
317 // get the InSet of root BB
Chris Lattnerf726e772002-10-28 19:22:04 +0000318 const ValueSet &InSet = LVI->getInSetOfBB(&Fn->front());
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000319
Chris Lattnerf726e772002-10-28 19:22:04 +0000320 for (Function::const_aiterator AI = Fn->abegin(); AI != Fn->aend(); ++AI) {
Chris Lattner7e708292002-06-25 16:13:24 +0000321 // add interferences between args and LVars at start
322 addInterference(AI, &InSet, false);
323
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000324 if (DEBUG_RA >= RA_DEBUG_Interference)
Brian Gaekeaf843702003-10-22 20:22:53 +0000325 std::cerr << " - %% adding interference for argument " << RAV(AI) << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000326 }
327}
328
329
Brian Gaekeaf843702003-10-22 20:22:53 +0000330/// The following are utility functions used solely by updateMachineCode and
331/// the functions that it calls. They should probably be folded back into
332/// updateMachineCode at some point.
333///
Vikram S. Adve48762092002-04-25 04:34:15 +0000334
Brian Gaekeaf843702003-10-22 20:22:53 +0000335// used by: updateMachineCode (1 time), PrependInstructions (1 time)
336inline void InsertBefore(MachineInstr* newMI, MachineBasicBlock& MBB,
337 MachineBasicBlock::iterator& MII) {
Chris Lattnerf726e772002-10-28 19:22:04 +0000338 MII = MBB.insert(MII, newMI);
Vikram S. Advecb202e32002-10-11 16:12:40 +0000339 ++MII;
340}
341
Brian Gaekeaf843702003-10-22 20:22:53 +0000342// used by: AppendInstructions (1 time)
343inline void InsertAfter(MachineInstr* newMI, MachineBasicBlock& MBB,
344 MachineBasicBlock::iterator& MII) {
Vikram S. Advecb202e32002-10-11 16:12:40 +0000345 ++MII; // insert before the next instruction
Chris Lattnerf726e772002-10-28 19:22:04 +0000346 MII = MBB.insert(MII, newMI);
Vikram S. Advecb202e32002-10-11 16:12:40 +0000347}
348
Brian Gaekeaf843702003-10-22 20:22:53 +0000349// used by: updateMachineCode (1 time)
350inline void DeleteInstruction(MachineBasicBlock& MBB,
351 MachineBasicBlock::iterator& MII) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000352 MII = MBB.erase(MII);
353}
354
Brian Gaekeaf843702003-10-22 20:22:53 +0000355// used by: updateMachineCode (1 time)
356inline void SubstituteInPlace(MachineInstr* newMI, MachineBasicBlock& MBB,
357 MachineBasicBlock::iterator MII) {
Vikram S. Advecb202e32002-10-11 16:12:40 +0000358 *MII = newMI;
359}
360
Brian Gaekeaf843702003-10-22 20:22:53 +0000361// used by: updateMachineCode (2 times)
362inline void PrependInstructions(std::vector<MachineInstr *> &IBef,
363 MachineBasicBlock& MBB,
364 MachineBasicBlock::iterator& MII,
365 const std::string& msg) {
366 if (!IBef.empty()) {
Vikram S. Adve48762092002-04-25 04:34:15 +0000367 MachineInstr* OrigMI = *MII;
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000368 std::vector<MachineInstr *>::iterator AdIt;
Brian Gaekeaf843702003-10-22 20:22:53 +0000369 for (AdIt = IBef.begin(); AdIt != IBef.end() ; ++AdIt) {
Vikram S. Adve48762092002-04-25 04:34:15 +0000370 if (DEBUG_RA) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000371 if (OrigMI) std::cerr << "For MInst:\n " << *OrigMI;
372 std::cerr << msg << "PREPENDed instr:\n " << **AdIt << "\n";
Vikram S. Adve48762092002-04-25 04:34:15 +0000373 }
Chris Lattnerf726e772002-10-28 19:22:04 +0000374 InsertBefore(*AdIt, MBB, MII);
Vikram S. Adve48762092002-04-25 04:34:15 +0000375 }
376 }
377}
378
Brian Gaekeaf843702003-10-22 20:22:53 +0000379// used by: updateMachineCode (1 time)
380inline void AppendInstructions(std::vector<MachineInstr *> &IAft,
381 MachineBasicBlock& MBB,
382 MachineBasicBlock::iterator& MII,
383 const std::string& msg) {
384 if (!IAft.empty()) {
Vikram S. Adve48762092002-04-25 04:34:15 +0000385 MachineInstr* OrigMI = *MII;
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000386 std::vector<MachineInstr *>::iterator AdIt;
Brian Gaekeaf843702003-10-22 20:22:53 +0000387 for ( AdIt = IAft.begin(); AdIt != IAft.end() ; ++AdIt ) {
Chris Lattner7e708292002-06-25 16:13:24 +0000388 if (DEBUG_RA) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000389 if (OrigMI) std::cerr << "For MInst:\n " << *OrigMI;
390 std::cerr << msg << "APPENDed instr:\n " << **AdIt << "\n";
Vikram S. Adve48762092002-04-25 04:34:15 +0000391 }
Chris Lattnerf726e772002-10-28 19:22:04 +0000392 InsertAfter(*AdIt, MBB, MII);
Vikram S. Adve48762092002-04-25 04:34:15 +0000393 }
394 }
395}
396
Brian Gaekeaf843702003-10-22 20:22:53 +0000397/// Set the registers for operands in the given MachineInstr, if a register was
398/// successfully allocated. Return true if any of its operands has been marked
399/// for spill.
400///
Brian Gaeke4efe3422003-09-21 01:23:46 +0000401bool PhyRegAlloc::markAllocatedRegs(MachineInstr* MInst)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000402{
Vikram S. Adve814030a2003-07-29 19:49:21 +0000403 bool instrNeedsSpills = false;
404
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000405 // First, set the registers for operands in the machine instruction
406 // if a register was successfully allocated. Do this first because we
407 // will need to know which registers are already used by this instr'n.
Brian Gaekeaf843702003-10-22 20:22:53 +0000408 for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000409 MachineOperand& Op = MInst->getOperand(OpNum);
410 if (Op.getType() == MachineOperand::MO_VirtualRegister ||
Brian Gaekeaf843702003-10-22 20:22:53 +0000411 Op.getType() == MachineOperand::MO_CCRegister) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000412 const Value *const Val = Op.getVRegValue();
Brian Gaeke4efe3422003-09-21 01:23:46 +0000413 if (const LiveRange* LR = LRI->getLiveRangeForValue(Val)) {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000414 // Remember if any operand needs spilling
415 instrNeedsSpills |= LR->isMarkedForSpill();
416
417 // An operand may have a color whether or not it needs spilling
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000418 if (LR->hasColor())
419 MInst->SetRegForOperand(OpNum,
Brian Gaeke59b1c562003-09-24 17:50:28 +0000420 MRI.getUnifiedRegNum(LR->getRegClassID(),
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000421 LR->getColor()));
Vikram S. Adve814030a2003-07-29 19:49:21 +0000422 }
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000423 }
424 } // for each operand
Vikram S. Adve814030a2003-07-29 19:49:21 +0000425
426 return instrNeedsSpills;
427}
428
Brian Gaekeaf843702003-10-22 20:22:53 +0000429/// Mark allocated registers (using markAllocatedRegs()) on the instruction
430/// that MII points to. Then, if it's a call instruction, insert caller-saving
431/// code before and after it. Finally, insert spill code before and after it,
432/// using insertCode4SpilledLR().
433///
Vikram S. Adve814030a2003-07-29 19:49:21 +0000434void PhyRegAlloc::updateInstruction(MachineBasicBlock::iterator& MII,
Brian Gaekeaf843702003-10-22 20:22:53 +0000435 MachineBasicBlock &MBB) {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000436 MachineInstr* MInst = *MII;
437 unsigned Opcode = MInst->getOpCode();
438
439 // Reset tmp stack positions so they can be reused for each machine instr.
Brian Gaeke4efe3422003-09-21 01:23:46 +0000440 MF->getInfo()->popAllTempValues();
Vikram S. Adve814030a2003-07-29 19:49:21 +0000441
442 // Mark the operands for which regs have been allocated.
Brian Gaeke4efe3422003-09-21 01:23:46 +0000443 bool instrNeedsSpills = markAllocatedRegs(*MII);
Vikram S. Adve814030a2003-07-29 19:49:21 +0000444
445#ifndef NDEBUG
446 // Mark that the operands have been updated. Later,
447 // setRelRegsUsedByThisInst() is called to find registers used by each
448 // MachineInst, and it should not be used for an instruction until
449 // this is done. This flag just serves as a sanity check.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000450 OperandsColoredMap[MInst] = true;
Vikram S. Adve814030a2003-07-29 19:49:21 +0000451#endif
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000452
Vikram S. Advebc001b22003-07-25 21:06:09 +0000453 // Now insert caller-saving code before/after the call.
454 // Do this before inserting spill code since some registers must be
455 // used by save/restore and spill code should not use those registers.
Vikram S. Advebc001b22003-07-25 21:06:09 +0000456 if (TM.getInstrInfo().isCall(Opcode)) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000457 AddedInstrns &AI = AddedInstrMap[MInst];
Vikram S. Adve814030a2003-07-29 19:49:21 +0000458 insertCallerSavingCode(AI.InstrnsBefore, AI.InstrnsAfter, MInst,
459 MBB.getBasicBlock());
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000460 }
Vikram S. Advebc001b22003-07-25 21:06:09 +0000461
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000462 // Now insert spill code for remaining operands not allocated to
463 // registers. This must be done even for call return instructions
464 // since those are not handled by the special code above.
Vikram S. Adve814030a2003-07-29 19:49:21 +0000465 if (instrNeedsSpills)
Brian Gaekeaf843702003-10-22 20:22:53 +0000466 for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000467 MachineOperand& Op = MInst->getOperand(OpNum);
468 if (Op.getType() == MachineOperand::MO_VirtualRegister ||
Brian Gaekeaf843702003-10-22 20:22:53 +0000469 Op.getType() == MachineOperand::MO_CCRegister) {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000470 const Value* Val = Op.getVRegValue();
Brian Gaeke4efe3422003-09-21 01:23:46 +0000471 if (const LiveRange *LR = LRI->getLiveRangeForValue(Val))
Vikram S. Adve814030a2003-07-29 19:49:21 +0000472 if (LR->isMarkedForSpill())
473 insertCode4SpilledLR(LR, MII, MBB, OpNum);
474 }
475 } // for each operand
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000476}
477
Brian Gaekeaf843702003-10-22 20:22:53 +0000478/// Iterate over all the MachineBasicBlocks in the current function and set
479/// the allocated registers for each instruction (using updateInstruction()),
480/// after register allocation is complete. Then move code out of delay slots.
481///
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000482void PhyRegAlloc::updateMachineCode()
483{
Chris Lattner7e708292002-06-25 16:13:24 +0000484 // Insert any instructions needed at method entry
Brian Gaeke4efe3422003-09-21 01:23:46 +0000485 MachineBasicBlock::iterator MII = MF->front().begin();
486 PrependInstructions(AddedInstrAtEntry.InstrnsBefore, MF->front(), MII,
Chris Lattner7e708292002-06-25 16:13:24 +0000487 "At function entry: \n");
488 assert(AddedInstrAtEntry.InstrnsAfter.empty() &&
489 "InstrsAfter should be unnecessary since we are just inserting at "
490 "the function entry point here.");
Vikram S. Adve48762092002-04-25 04:34:15 +0000491
Brian Gaeke4efe3422003-09-21 01:23:46 +0000492 for (MachineFunction::iterator BBI = MF->begin(), BBE = MF->end();
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000493 BBI != BBE; ++BBI) {
Chris Lattnerf726e772002-10-28 19:22:04 +0000494 MachineBasicBlock &MBB = *BBI;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000495
496 // Iterate over all machine instructions in BB and mark operands with
497 // their assigned registers or insert spill code, as appropriate.
498 // Also, fix operands of call/return instructions.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000499 for (MachineBasicBlock::iterator MII = MBB.begin(); MII != MBB.end(); ++MII)
Vikram S. Adve814030a2003-07-29 19:49:21 +0000500 if (! TM.getInstrInfo().isDummyPhiInstr((*MII)->getOpCode()))
501 updateInstruction(MII, MBB);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000502
503 // Now, move code out of delay slots of branches and returns if needed.
504 // (Also, move "after" code from calls to the last delay slot instruction.)
505 // Moving code out of delay slots is needed in 2 situations:
506 // (1) If this is a branch and it needs instructions inserted after it,
507 // move any existing instructions out of the delay slot so that the
508 // instructions can go into the delay slot. This only supports the
509 // case that #instrsAfter <= #delay slots.
510 //
511 // (2) If any instruction in the delay slot needs
512 // instructions inserted, move it out of the delay slot and before the
513 // branch because putting code before or after it would be VERY BAD!
514 //
515 // If the annul bit of the branch is set, neither of these is legal!
516 // If so, we need to handle spill differently but annulling is not yet used.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000517 for (MachineBasicBlock::iterator MII = MBB.begin();
518 MII != MBB.end(); ++MII)
519 if (unsigned delaySlots =
Brian Gaekeaf843702003-10-22 20:22:53 +0000520 TM.getInstrInfo().getNumDelaySlots((*MII)->getOpCode())) {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000521 MachineInstr *MInst = *MII, *DelaySlotMI = *(MII+1);
522
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000523 // Check the 2 conditions above:
524 // (1) Does a branch need instructions added after it?
525 // (2) O/w does delay slot instr. need instrns before or after?
Vikram S. Adve814030a2003-07-29 19:49:21 +0000526 bool isBranch = (TM.getInstrInfo().isBranch(MInst->getOpCode()) ||
527 TM.getInstrInfo().isReturn(MInst->getOpCode()));
528 bool cond1 = (isBranch &&
529 AddedInstrMap.count(MInst) &&
530 AddedInstrMap[MInst].InstrnsAfter.size() > 0);
531 bool cond2 = (AddedInstrMap.count(DelaySlotMI) &&
532 (AddedInstrMap[DelaySlotMI].InstrnsBefore.size() > 0 ||
533 AddedInstrMap[DelaySlotMI].InstrnsAfter.size() > 0));
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000534
Brian Gaekeaf843702003-10-22 20:22:53 +0000535 if (cond1 || cond2) {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000536 assert((MInst->getOpCodeFlags() & AnnulFlag) == 0 &&
537 "FIXME: Moving an annulled delay slot instruction!");
538 assert(delaySlots==1 &&
539 "InsertBefore does not yet handle >1 delay slots!");
540 InsertBefore(DelaySlotMI, MBB, MII); // MII pts back to branch
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000541
542 // In case (1), delete it and don't replace with anything!
543 // Otherwise (i.e., case (2) only) replace it with a NOP.
544 if (cond1) {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000545 DeleteInstruction(MBB, ++MII); // MII now points to next inst.
546 --MII; // reset MII for ++MII of loop
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000547 }
Vikram S. Adve814030a2003-07-29 19:49:21 +0000548 else
549 SubstituteInPlace(BuildMI(TM.getInstrInfo().getNOPOpCode(),1),
550 MBB, MII+1); // replace with NOP
551
552 if (DEBUG_RA) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000553 std::cerr << "\nRegAlloc: Moved instr. with added code: "
Vikram S. Adve814030a2003-07-29 19:49:21 +0000554 << *DelaySlotMI
555 << " out of delay slots of instr: " << *MInst;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000556 }
557 }
Vikram S. Adve814030a2003-07-29 19:49:21 +0000558 else
559 // For non-branch instr with delay slots (probably a call), move
560 // InstrAfter to the instr. in the last delay slot.
561 move2DelayedInstr(*MII, *(MII+delaySlots));
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000562 }
563
564 // Finally iterate over all instructions in BB and insert before/after
Vikram S. Advebc001b22003-07-25 21:06:09 +0000565 for (MachineBasicBlock::iterator MII=MBB.begin(); MII != MBB.end(); ++MII) {
Vikram S. Adve48762092002-04-25 04:34:15 +0000566 MachineInstr *MInst = *MII;
Vikram S. Advebc001b22003-07-25 21:06:09 +0000567
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000568 // do not process Phis
Vikram S. Advebc001b22003-07-25 21:06:09 +0000569 if (TM.getInstrInfo().isDummyPhiInstr(MInst->getOpCode()))
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000570 continue;
571
Vikram S. Advebc001b22003-07-25 21:06:09 +0000572 // if there are any added instructions...
Chris Lattner7e708292002-06-25 16:13:24 +0000573 if (AddedInstrMap.count(MInst)) {
Vikram S. Advebc001b22003-07-25 21:06:09 +0000574 AddedInstrns &CallAI = AddedInstrMap[MInst];
575
576#ifndef NDEBUG
Vikram S. Adve814030a2003-07-29 19:49:21 +0000577 bool isBranch = (TM.getInstrInfo().isBranch(MInst->getOpCode()) ||
578 TM.getInstrInfo().isReturn(MInst->getOpCode()));
579 assert((!isBranch ||
580 AddedInstrMap[MInst].InstrnsAfter.size() <=
581 TM.getInstrInfo().getNumDelaySlots(MInst->getOpCode())) &&
582 "Cannot put more than #delaySlots instrns after "
583 "branch or return! Need to handle temps differently.");
584#endif
585
586#ifndef NDEBUG
Vikram S. Advebc001b22003-07-25 21:06:09 +0000587 // Temporary sanity checking code to detect whether the same machine
588 // instruction is ever inserted twice before/after a call.
589 // I suspect this is happening but am not sure. --Vikram, 7/1/03.
Vikram S. Advebc001b22003-07-25 21:06:09 +0000590 std::set<const MachineInstr*> instrsSeen;
591 for (int i = 0, N = CallAI.InstrnsBefore.size(); i < N; ++i) {
592 assert(instrsSeen.count(CallAI.InstrnsBefore[i]) == 0 &&
593 "Duplicate machine instruction in InstrnsBefore!");
594 instrsSeen.insert(CallAI.InstrnsBefore[i]);
595 }
596 for (int i = 0, N = CallAI.InstrnsAfter.size(); i < N; ++i) {
597 assert(instrsSeen.count(CallAI.InstrnsAfter[i]) == 0 &&
598 "Duplicate machine instruction in InstrnsBefore/After!");
599 instrsSeen.insert(CallAI.InstrnsAfter[i]);
600 }
601#endif
602
603 // Now add the instructions before/after this MI.
604 // We do this here to ensure that spill for an instruction is inserted
605 // as close as possible to an instruction (see above insertCode4Spill)
Vikram S. Advebc001b22003-07-25 21:06:09 +0000606 if (! CallAI.InstrnsBefore.empty())
607 PrependInstructions(CallAI.InstrnsBefore, MBB, MII,"");
608
609 if (! CallAI.InstrnsAfter.empty())
610 AppendInstructions(CallAI.InstrnsAfter, MBB, MII,"");
611
612 } // if there are any added instructions
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000613 } // for each machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000614 }
615}
616
617
Brian Gaekeaf843702003-10-22 20:22:53 +0000618/// Insert spill code for AN operand whose LR was spilled. May be called
619/// repeatedly for a single MachineInstr if it has many spilled operands. On
620/// each call, it finds a register which is not live at that instruction and
621/// also which is not used by other spilled operands of the same
622/// instruction. Then it uses this register temporarily to accommodate the
623/// spilled value.
624///
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000625void PhyRegAlloc::insertCode4SpilledLR(const LiveRange *LR,
Vikram S. Adve814030a2003-07-29 19:49:21 +0000626 MachineBasicBlock::iterator& MII,
627 MachineBasicBlock &MBB,
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000628 const unsigned OpNum) {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000629 MachineInstr *MInst = *MII;
630 const BasicBlock *BB = MBB.getBasicBlock();
631
Vikram S. Advead9c9782002-09-28 17:02:40 +0000632 assert((! TM.getInstrInfo().isCall(MInst->getOpCode()) || OpNum == 0) &&
633 "Outgoing arg of a call must be handled elsewhere (func arg ok)");
634 assert(! TM.getInstrInfo().isReturn(MInst->getOpCode()) &&
635 "Return value of a ret must be handled elsewhere");
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000636
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000637 MachineOperand& Op = MInst->getOperand(OpNum);
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000638 bool isDef = Op.opIsDefOnly();
639 bool isDefAndUse = Op.opIsDefAndUse();
Vikram S. Advebc001b22003-07-25 21:06:09 +0000640 unsigned RegType = MRI.getRegTypeForLR(LR);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000641 int SpillOff = LR->getSpillOffFromFP();
642 RegClass *RC = LR->getRegClass();
Vikram S. Adve814030a2003-07-29 19:49:21 +0000643
644 // Get the live-variable set to find registers free before this instr.
Vikram S. Advefeb32982003-08-12 22:22:24 +0000645 const ValueSet &LVSetBef = LVI->getLiveVarSetBeforeMInst(MInst, BB);
646
647#ifndef NDEBUG
648 // If this instr. is in the delay slot of a branch or return, we need to
649 // include all live variables before that branch or return -- we don't want to
650 // trample those! Verify that the set is included in the LV set before MInst.
Vikram S. Adve814030a2003-07-29 19:49:21 +0000651 if (MII != MBB.begin()) {
652 MachineInstr *PredMI = *(MII-1);
Vikram S. Advefeb32982003-08-12 22:22:24 +0000653 if (unsigned DS = TM.getInstrInfo().getNumDelaySlots(PredMI->getOpCode()))
654 assert(set_difference(LVI->getLiveVarSetBeforeMInst(PredMI), LVSetBef)
655 .empty() && "Live-var set before branch should be included in "
656 "live-var set of each delay slot instruction!");
Vikram S. Adve814030a2003-07-29 19:49:21 +0000657 }
Vikram S. Advefeb32982003-08-12 22:22:24 +0000658#endif
Vikram S. Adve00521d72001-11-12 23:26:35 +0000659
Brian Gaekeaf843702003-10-22 20:22:53 +0000660 MF->getInfo()->pushTempValue(MRI.getSpilledRegSize(RegType));
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000661
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000662 std::vector<MachineInstr*> MIBef, MIAft;
663 std::vector<MachineInstr*> AdIMid;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000664
Vikram S. Adve3bf08922003-07-10 19:42:55 +0000665 // Choose a register to hold the spilled value, if one was not preallocated.
666 // This may insert code before and after MInst to free up the value. If so,
667 // this code should be first/last in the spill sequence before/after MInst.
668 int TmpRegU=(LR->hasColor()
Brian Gaeke59b1c562003-09-24 17:50:28 +0000669 ? MRI.getUnifiedRegNum(LR->getRegClassID(),LR->getColor())
Vikram S. Adve3bf08922003-07-10 19:42:55 +0000670 : getUsableUniRegAtMI(RegType, &LVSetBef, MInst, MIBef,MIAft));
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000671
Vikram S. Advef5af6362002-07-08 23:15:32 +0000672 // Set the operand first so that it this register does not get used
673 // as a scratch register for later calls to getUsableUniRegAtMI below
674 MInst->SetRegForOperand(OpNum, TmpRegU);
675
676 // get the added instructions for this instruction
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000677 AddedInstrns &AI = AddedInstrMap[MInst];
Vikram S. Advef5af6362002-07-08 23:15:32 +0000678
679 // We may need a scratch register to copy the spilled value to/from memory.
680 // This may itself have to insert code to free up a scratch register.
681 // Any such code should go before (after) the spill code for a load (store).
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000682 // The scratch reg is not marked as used because it is only used
683 // for the copy and not used across MInst.
Vikram S. Advef5af6362002-07-08 23:15:32 +0000684 int scratchRegType = -1;
685 int scratchReg = -1;
Brian Gaekeaf843702003-10-22 20:22:53 +0000686 if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType)) {
Chris Lattner27a08932002-10-22 23:16:21 +0000687 scratchReg = getUsableUniRegAtMI(scratchRegType, &LVSetBef,
688 MInst, MIBef, MIAft);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000689 assert(scratchReg != MRI.getInvalidRegNum());
Vikram S. Advef5af6362002-07-08 23:15:32 +0000690 }
691
692 if (!isDef || isDefAndUse) {
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000693 // for a USE, we have to load the value of LR from stack to a TmpReg
694 // and use the TmpReg as one operand of instruction
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000695
Vikram S. Advef5af6362002-07-08 23:15:32 +0000696 // actual loading instruction(s)
Vikram S. Adve814030a2003-07-29 19:49:21 +0000697 MRI.cpMem2RegMI(AdIMid, MRI.getFramePointer(), SpillOff, TmpRegU,
698 RegType, scratchReg);
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000699
Vikram S. Advef5af6362002-07-08 23:15:32 +0000700 // the actual load should be after the instructions to free up TmpRegU
701 MIBef.insert(MIBef.end(), AdIMid.begin(), AdIMid.end());
702 AdIMid.clear();
703 }
704
Vikram S. Adve3bf08922003-07-10 19:42:55 +0000705 if (isDef || isDefAndUse) { // if this is a Def
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000706 // for a DEF, we have to store the value produced by this instruction
707 // on the stack position allocated for this LR
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000708
Vikram S. Advef5af6362002-07-08 23:15:32 +0000709 // actual storing instruction(s)
Vikram S. Adve814030a2003-07-29 19:49:21 +0000710 MRI.cpReg2MemMI(AdIMid, TmpRegU, MRI.getFramePointer(), SpillOff,
711 RegType, scratchReg);
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000712
Vikram S. Advef5af6362002-07-08 23:15:32 +0000713 MIAft.insert(MIAft.begin(), AdIMid.begin(), AdIMid.end());
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000714 } // if !DEF
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000715
Vikram S. Advef5af6362002-07-08 23:15:32 +0000716 // Finally, insert the entire spill code sequences before/after MInst
717 AI.InstrnsBefore.insert(AI.InstrnsBefore.end(), MIBef.begin(), MIBef.end());
718 AI.InstrnsAfter.insert(AI.InstrnsAfter.begin(), MIAft.begin(), MIAft.end());
719
Chris Lattner7e708292002-06-25 16:13:24 +0000720 if (DEBUG_RA) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000721 std::cerr << "\nFor Inst:\n " << *MInst;
722 std::cerr << "SPILLED LR# " << LR->getUserIGNode()->getIndex();
723 std::cerr << "; added Instructions:";
Anand Shuklad58290e2002-07-09 19:18:56 +0000724 for_each(MIBef.begin(), MIBef.end(), std::mem_fun(&MachineInstr::dump));
725 for_each(MIAft.begin(), MIAft.end(), std::mem_fun(&MachineInstr::dump));
Chris Lattner7e708292002-06-25 16:13:24 +0000726 }
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000727}
728
729
Brian Gaekeaf843702003-10-22 20:22:53 +0000730/// Insert caller saving/restoring instructions before/after a call machine
731/// instruction (before or after any other instructions that were inserted for
732/// the call).
733///
Vikram S. Adve814030a2003-07-29 19:49:21 +0000734void
735PhyRegAlloc::insertCallerSavingCode(std::vector<MachineInstr*> &instrnsBefore,
736 std::vector<MachineInstr*> &instrnsAfter,
737 MachineInstr *CallMI,
Brian Gaekeaf843702003-10-22 20:22:53 +0000738 const BasicBlock *BB) {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000739 assert(TM.getInstrInfo().isCall(CallMI->getOpCode()));
740
Brian Gaeke43ce8fe2003-09-21 02:24:09 +0000741 // hash set to record which registers were saved/restored
Vikram S. Adve814030a2003-07-29 19:49:21 +0000742 hash_set<unsigned> PushedRegSet;
743
744 CallArgsDescriptor* argDesc = CallArgsDescriptor::get(CallMI);
745
746 // if the call is to a instrumentation function, do not insert save and
747 // restore instructions the instrumentation function takes care of save
748 // restore for volatile regs.
749 //
750 // FIXME: this should be made general, not specific to the reoptimizer!
Vikram S. Adve814030a2003-07-29 19:49:21 +0000751 const Function *Callee = argDesc->getCallInst()->getCalledFunction();
752 bool isLLVMFirstTrigger = Callee && Callee->getName() == "llvm_first_trigger";
753
754 // Now check if the call has a return value (using argDesc) and if so,
755 // find the LR of the TmpInstruction representing the return value register.
756 // (using the last or second-last *implicit operand* of the call MI).
757 // Insert it to to the PushedRegSet since we must not save that register
758 // and restore it after the call.
759 // We do this because, we look at the LV set *after* the instruction
760 // to determine, which LRs must be saved across calls. The return value
761 // of the call is live in this set - but we must not save/restore it.
Vikram S. Adve814030a2003-07-29 19:49:21 +0000762 if (const Value *origRetVal = argDesc->getReturnValue()) {
763 unsigned retValRefNum = (CallMI->getNumImplicitRefs() -
764 (argDesc->getIndirectFuncPtr()? 1 : 2));
765 const TmpInstruction* tmpRetVal =
766 cast<TmpInstruction>(CallMI->getImplicitRef(retValRefNum));
767 assert(tmpRetVal->getOperand(0) == origRetVal &&
768 tmpRetVal->getType() == origRetVal->getType() &&
769 "Wrong implicit ref?");
Brian Gaeke4efe3422003-09-21 01:23:46 +0000770 LiveRange *RetValLR = LRI->getLiveRangeForValue(tmpRetVal);
Vikram S. Adve814030a2003-07-29 19:49:21 +0000771 assert(RetValLR && "No LR for RetValue of call");
772
773 if (! RetValLR->isMarkedForSpill())
774 PushedRegSet.insert(MRI.getUnifiedRegNum(RetValLR->getRegClassID(),
775 RetValLR->getColor()));
776 }
777
778 const ValueSet &LVSetAft = LVI->getLiveVarSetAfterMInst(CallMI, BB);
779 ValueSet::const_iterator LIt = LVSetAft.begin();
780
781 // for each live var in live variable set after machine inst
782 for( ; LIt != LVSetAft.end(); ++LIt) {
Brian Gaeke43ce8fe2003-09-21 02:24:09 +0000783 // get the live range corresponding to live var
Brian Gaeke4efe3422003-09-21 01:23:46 +0000784 LiveRange *const LR = LRI->getLiveRangeForValue(*LIt);
Vikram S. Adve814030a2003-07-29 19:49:21 +0000785
786 // LR can be null if it is a const since a const
787 // doesn't have a dominating def - see Assumptions above
Brian Gaekeaf843702003-10-22 20:22:53 +0000788 if (LR) {
789 if (! LR->isMarkedForSpill()) {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000790 assert(LR->hasColor() && "LR is neither spilled nor colored?");
791 unsigned RCID = LR->getRegClassID();
792 unsigned Color = LR->getColor();
793
794 if (MRI.isRegVolatile(RCID, Color) ) {
Brian Gaeke43ce8fe2003-09-21 02:24:09 +0000795 // if this is a call to the first-level reoptimizer
796 // instrumentation entry point, and the register is not
797 // modified by call, don't save and restore it.
Vikram S. Adve814030a2003-07-29 19:49:21 +0000798 if (isLLVMFirstTrigger && !MRI.modifiedByCall(RCID, Color))
799 continue;
800
801 // if the value is in both LV sets (i.e., live before and after
802 // the call machine instruction)
Vikram S. Adve814030a2003-07-29 19:49:21 +0000803 unsigned Reg = MRI.getUnifiedRegNum(RCID, Color);
804
Brian Gaeke43ce8fe2003-09-21 02:24:09 +0000805 // if we haven't already pushed this register...
Vikram S. Adve814030a2003-07-29 19:49:21 +0000806 if( PushedRegSet.find(Reg) == PushedRegSet.end() ) {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000807 unsigned RegType = MRI.getRegTypeForLR(LR);
808
809 // Now get two instructions - to push on stack and pop from stack
810 // and add them to InstrnsBefore and InstrnsAfter of the
811 // call instruction
Vikram S. Adve814030a2003-07-29 19:49:21 +0000812 int StackOff =
Brian Gaeke4efe3422003-09-21 01:23:46 +0000813 MF->getInfo()->pushTempValue(MRI.getSpilledRegSize(RegType));
Vikram S. Adve814030a2003-07-29 19:49:21 +0000814
815 //---- Insert code for pushing the reg on stack ----------
816
817 std::vector<MachineInstr*> AdIBef, AdIAft;
818
819 // We may need a scratch register to copy the saved value
820 // to/from memory. This may itself have to insert code to
821 // free up a scratch register. Any such code should go before
822 // the save code. The scratch register, if any, is by default
823 // temporary and not "used" by the instruction unless the
824 // copy code itself decides to keep the value in the scratch reg.
825 int scratchRegType = -1;
826 int scratchReg = -1;
827 if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType))
828 { // Find a register not live in the LVSet before CallMI
829 const ValueSet &LVSetBef =
830 LVI->getLiveVarSetBeforeMInst(CallMI, BB);
831 scratchReg = getUsableUniRegAtMI(scratchRegType, &LVSetBef,
832 CallMI, AdIBef, AdIAft);
833 assert(scratchReg != MRI.getInvalidRegNum());
834 }
835
836 if (AdIBef.size() > 0)
837 instrnsBefore.insert(instrnsBefore.end(),
838 AdIBef.begin(), AdIBef.end());
839
840 MRI.cpReg2MemMI(instrnsBefore, Reg, MRI.getFramePointer(),
841 StackOff, RegType, scratchReg);
842
843 if (AdIAft.size() > 0)
844 instrnsBefore.insert(instrnsBefore.end(),
845 AdIAft.begin(), AdIAft.end());
846
847 //---- Insert code for popping the reg from the stack ----------
Vikram S. Adve814030a2003-07-29 19:49:21 +0000848 AdIBef.clear();
849 AdIAft.clear();
850
851 // We may need a scratch register to copy the saved value
852 // from memory. This may itself have to insert code to
853 // free up a scratch register. Any such code should go
854 // after the save code. As above, scratch is not marked "used".
Vikram S. Adve814030a2003-07-29 19:49:21 +0000855 scratchRegType = -1;
856 scratchReg = -1;
857 if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType))
858 { // Find a register not live in the LVSet after CallMI
859 scratchReg = getUsableUniRegAtMI(scratchRegType, &LVSetAft,
860 CallMI, AdIBef, AdIAft);
861 assert(scratchReg != MRI.getInvalidRegNum());
862 }
863
864 if (AdIBef.size() > 0)
865 instrnsAfter.insert(instrnsAfter.end(),
866 AdIBef.begin(), AdIBef.end());
867
868 MRI.cpMem2RegMI(instrnsAfter, MRI.getFramePointer(), StackOff,
869 Reg, RegType, scratchReg);
870
871 if (AdIAft.size() > 0)
872 instrnsAfter.insert(instrnsAfter.end(),
873 AdIAft.begin(), AdIAft.end());
874
875 PushedRegSet.insert(Reg);
876
877 if(DEBUG_RA) {
878 std::cerr << "\nFor call inst:" << *CallMI;
879 std::cerr << " -inserted caller saving instrs: Before:\n\t ";
880 for_each(instrnsBefore.begin(), instrnsBefore.end(),
881 std::mem_fun(&MachineInstr::dump));
882 std::cerr << " -and After:\n\t ";
883 for_each(instrnsAfter.begin(), instrnsAfter.end(),
884 std::mem_fun(&MachineInstr::dump));
885 }
886 } // if not already pushed
Vikram S. Adve814030a2003-07-29 19:49:21 +0000887 } // if LR has a volatile color
Vikram S. Adve814030a2003-07-29 19:49:21 +0000888 } // if LR has color
Vikram S. Adve814030a2003-07-29 19:49:21 +0000889 } // if there is a LR for Var
Vikram S. Adve814030a2003-07-29 19:49:21 +0000890 } // for each value in the LV set after instruction
891}
892
893
Brian Gaekeaf843702003-10-22 20:22:53 +0000894/// Returns the unified register number of a temporary register to be used
895/// BEFORE MInst. If no register is available, it will pick one and modify
896/// MIBef and MIAft to contain instructions used to free up this returned
897/// register.
898///
Vikram S. Advef5af6362002-07-08 23:15:32 +0000899int PhyRegAlloc::getUsableUniRegAtMI(const int RegType,
900 const ValueSet *LVSetBef,
901 MachineInstr *MInst,
902 std::vector<MachineInstr*>& MIBef,
903 std::vector<MachineInstr*>& MIAft) {
Chris Lattner133f0792002-10-28 04:45:29 +0000904 RegClass* RC = getRegClassByID(MRI.getRegClassIDOfRegType(RegType));
Vikram S. Advef5af6362002-07-08 23:15:32 +0000905
Brian Gaekeaf843702003-10-22 20:22:53 +0000906 int RegU = getUnusedUniRegAtMI(RC, RegType, MInst, LVSetBef);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000907
908 if (RegU == -1) {
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000909 // we couldn't find an unused register. Generate code to free up a reg by
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000910 // saving it on stack and restoring after the instruction
Vikram S. Advef5af6362002-07-08 23:15:32 +0000911
Brian Gaeke4efe3422003-09-21 01:23:46 +0000912 int TmpOff = MF->getInfo()->pushTempValue(MRI.getSpilledRegSize(RegType));
Vikram S. Adve12af1642001-11-08 04:48:50 +0000913
Vikram S. Advebc001b22003-07-25 21:06:09 +0000914 RegU = getUniRegNotUsedByThisInst(RC, RegType, MInst);
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000915
Vikram S. Advef5af6362002-07-08 23:15:32 +0000916 // Check if we need a scratch register to copy this register to memory.
917 int scratchRegType = -1;
Brian Gaekeaf843702003-10-22 20:22:53 +0000918 if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType)) {
Chris Lattner133f0792002-10-28 04:45:29 +0000919 int scratchReg = getUsableUniRegAtMI(scratchRegType, LVSetBef,
920 MInst, MIBef, MIAft);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000921 assert(scratchReg != MRI.getInvalidRegNum());
922
923 // We may as well hold the value in the scratch register instead
924 // of copying it to memory and back. But we have to mark the
925 // register as used by this instruction, so it does not get used
926 // as a scratch reg. by another operand or anyone else.
Chris Lattner3fd1f5b2003-08-05 22:11:13 +0000927 ScratchRegsUsed.insert(std::make_pair(MInst, scratchReg));
Vikram S. Advef5af6362002-07-08 23:15:32 +0000928 MRI.cpReg2RegMI(MIBef, RegU, scratchReg, RegType);
929 MRI.cpReg2RegMI(MIAft, scratchReg, RegU, RegType);
Brian Gaekeaf843702003-10-22 20:22:53 +0000930 } else { // the register can be copied directly to/from memory so do it.
Vikram S. Advef5af6362002-07-08 23:15:32 +0000931 MRI.cpReg2MemMI(MIBef, RegU, MRI.getFramePointer(), TmpOff, RegType);
932 MRI.cpMem2RegMI(MIAft, MRI.getFramePointer(), TmpOff, RegU, RegType);
Brian Gaekeaf843702003-10-22 20:22:53 +0000933 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000934 }
Vikram S. Advef5af6362002-07-08 23:15:32 +0000935
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000936 return RegU;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000937}
938
Vikram S. Adve814030a2003-07-29 19:49:21 +0000939
Brian Gaekeaf843702003-10-22 20:22:53 +0000940/// Returns the register-class register number of a new unused register that
941/// can be used to accommodate a temporary value. May be called repeatedly
942/// for a single MachineInstr. On each call, it finds a register which is not
943/// live at that instruction and which is not used by any spilled operands of
944/// that instruction.
945///
946int PhyRegAlloc::getUnusedUniRegAtMI(RegClass *RC, const int RegType,
Vikram S. Adve814030a2003-07-29 19:49:21 +0000947 const MachineInstr *MInst,
948 const ValueSet* LVSetBef) {
Vikram S. Advebc001b22003-07-25 21:06:09 +0000949 RC->clearColorsUsed(); // Reset array
Vikram S. Adve814030a2003-07-29 19:49:21 +0000950
951 if (LVSetBef == NULL) {
952 LVSetBef = &LVI->getLiveVarSetBeforeMInst(MInst);
953 assert(LVSetBef != NULL && "Unable to get live-var set before MInst?");
954 }
955
Chris Lattner296b7732002-02-05 02:52:05 +0000956 ValueSet::const_iterator LIt = LVSetBef->begin();
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000957
958 // for each live var in live variable set after machine inst
Chris Lattner7e708292002-06-25 16:13:24 +0000959 for ( ; LIt != LVSetBef->end(); ++LIt) {
Brian Gaeke43ce8fe2003-09-21 02:24:09 +0000960 // Get the live range corresponding to live var, and its RegClass
Brian Gaeke4efe3422003-09-21 01:23:46 +0000961 LiveRange *const LRofLV = LRI->getLiveRangeForValue(*LIt );
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000962
963 // LR can be null if it is a const since a const
964 // doesn't have a dominating def - see Assumptions above
Vikram S. Advebc001b22003-07-25 21:06:09 +0000965 if (LRofLV && LRofLV->getRegClass() == RC && LRofLV->hasColor())
966 RC->markColorsUsed(LRofLV->getColor(),
967 MRI.getRegTypeForLR(LRofLV), RegType);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000968 }
969
970 // It is possible that one operand of this MInst was already spilled
971 // and it received some register temporarily. If that's the case,
972 // it is recorded in machine operand. We must skip such registers.
Vikram S. Advebc001b22003-07-25 21:06:09 +0000973 setRelRegsUsedByThisInst(RC, RegType, MInst);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000974
Vikram S. Advebc001b22003-07-25 21:06:09 +0000975 int unusedReg = RC->getUnusedColor(RegType); // find first unused color
976 if (unusedReg >= 0)
977 return MRI.getUnifiedRegNum(RC->getID(), unusedReg);
978
Chris Lattner85c54652002-05-23 15:50:03 +0000979 return -1;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000980}
981
982
Brian Gaekeaf843702003-10-22 20:22:53 +0000983/// Return the unified register number of a register in class RC which is not
984/// used by any operands of MInst.
985///
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000986int PhyRegAlloc::getUniRegNotUsedByThisInst(RegClass *RC,
Vikram S. Advebc001b22003-07-25 21:06:09 +0000987 const int RegType,
Chris Lattner85c54652002-05-23 15:50:03 +0000988 const MachineInstr *MInst) {
Vikram S. Advebc001b22003-07-25 21:06:09 +0000989 RC->clearColorsUsed();
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000990
Vikram S. Advebc001b22003-07-25 21:06:09 +0000991 setRelRegsUsedByThisInst(RC, RegType, MInst);
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000992
Vikram S. Advebc001b22003-07-25 21:06:09 +0000993 // find the first unused color
994 int unusedReg = RC->getUnusedColor(RegType);
995 assert(unusedReg >= 0 &&
996 "FATAL: No free register could be found in reg class!!");
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000997
Vikram S. Advebc001b22003-07-25 21:06:09 +0000998 return MRI.getUnifiedRegNum(RC->getID(), unusedReg);
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000999}
1000
1001
Brian Gaekeaf843702003-10-22 20:22:53 +00001002/// Modify the IsColorUsedArr of register class RC, by setting the bits
1003/// corresponding to register RegNo. This is a helper method of
1004/// setRelRegsUsedByThisInst().
1005///
Chris Lattner3bed95b2003-08-05 21:55:58 +00001006static void markRegisterUsed(int RegNo, RegClass *RC, int RegType,
1007 const TargetRegInfo &TRI) {
1008 unsigned classId = 0;
1009 int classRegNum = TRI.getClassRegNum(RegNo, classId);
1010 if (RC->getID() == classId)
1011 RC->markColorsUsed(classRegNum, RegType, RegType);
1012}
1013
1014void PhyRegAlloc::setRelRegsUsedByThisInst(RegClass *RC, int RegType,
Brian Gaekeaf843702003-10-22 20:22:53 +00001015 const MachineInstr *MI) {
Chris Lattner3bed95b2003-08-05 21:55:58 +00001016 assert(OperandsColoredMap[MI] == true &&
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001017 "Illegal to call setRelRegsUsedByThisInst() until colored operands "
1018 "are marked for an instruction.");
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001019
Brian Gaekeaf843702003-10-22 20:22:53 +00001020 // Add the registers already marked as used by the instruction. Both
1021 // explicit and implicit operands are set.
Chris Lattner3bed95b2003-08-05 21:55:58 +00001022 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
1023 if (MI->getOperand(i).hasAllocatedReg())
1024 markRegisterUsed(MI->getOperand(i).getAllocatedRegNum(), RC, RegType,MRI);
1025
1026 for (unsigned i = 0, e = MI->getNumImplicitRefs(); i != e; ++i)
1027 if (MI->getImplicitOp(i).hasAllocatedReg())
1028 markRegisterUsed(MI->getImplicitOp(i).getAllocatedRegNum(), RC,
1029 RegType,MRI);
1030
Chris Lattner3fd1f5b2003-08-05 22:11:13 +00001031 // Add all of the scratch registers that are used to save values across the
1032 // instruction (e.g., for saving state register values).
1033 std::pair<ScratchRegsUsedTy::iterator, ScratchRegsUsedTy::iterator>
1034 IR = ScratchRegsUsed.equal_range(MI);
1035 for (ScratchRegsUsedTy::iterator I = IR.first; I != IR.second; ++I)
1036 markRegisterUsed(I->second, RC, RegType, MRI);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001037
Vikram S. Advef5af6362002-07-08 23:15:32 +00001038 // If there are implicit references, mark their allocated regs as well
Chris Lattner3bed95b2003-08-05 21:55:58 +00001039 for (unsigned z=0; z < MI->getNumImplicitRefs(); z++)
Vikram S. Advef5af6362002-07-08 23:15:32 +00001040 if (const LiveRange*
Brian Gaeke4efe3422003-09-21 01:23:46 +00001041 LRofImpRef = LRI->getLiveRangeForValue(MI->getImplicitRef(z)))
Vikram S. Advef5af6362002-07-08 23:15:32 +00001042 if (LRofImpRef->hasColor())
1043 // this implicit reference is in a LR that received a color
Vikram S. Advebc001b22003-07-25 21:06:09 +00001044 RC->markColorsUsed(LRofImpRef->getColor(),
1045 MRI.getRegTypeForLR(LRofImpRef), RegType);
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001046}
1047
1048
Brian Gaekeaf843702003-10-22 20:22:53 +00001049/// If there are delay slots for an instruction, the instructions added after
1050/// it must really go after the delayed instruction(s). So, we Move the
1051/// InstrAfter of that instruction to the corresponding delayed instruction
1052/// using the following method.
1053///
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001054void PhyRegAlloc::move2DelayedInstr(const MachineInstr *OrigMI,
1055 const MachineInstr *DelayedMI)
1056{
Vikram S. Advefeb32982003-08-12 22:22:24 +00001057 // "added after" instructions of the original instr
1058 std::vector<MachineInstr *> &OrigAft = AddedInstrMap[OrigMI].InstrnsAfter;
1059
1060 if (DEBUG_RA && OrigAft.size() > 0) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +00001061 std::cerr << "\nRegAlloc: Moved InstrnsAfter for: " << *OrigMI;
1062 std::cerr << " to last delay slot instrn: " << *DelayedMI;
Vikram S. Adve814030a2003-07-29 19:49:21 +00001063 }
1064
Ruchira Sasanka251d8db2001-10-23 21:38:00 +00001065 // "added after" instructions of the delayed instr
Vikram S. Adve814030a2003-07-29 19:49:21 +00001066 std::vector<MachineInstr *> &DelayedAft=AddedInstrMap[DelayedMI].InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +00001067
1068 // go thru all the "added after instructions" of the original instruction
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001069 // and append them to the "added after instructions" of the delayed
Ruchira Sasanka251d8db2001-10-23 21:38:00 +00001070 // instructions
Chris Lattner697954c2002-01-20 22:54:45 +00001071 DelayedAft.insert(DelayedAft.end(), OrigAft.begin(), OrigAft.end());
Ruchira Sasanka251d8db2001-10-23 21:38:00 +00001072
1073 // empty the "added after instructions" of the original instruction
1074 OrigAft.clear();
Ruchira Sasanka251d8db2001-10-23 21:38:00 +00001075}
Ruchira Sasanka0931a012001-09-15 19:06:58 +00001076
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001077
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001078void PhyRegAlloc::colorIncomingArgs()
1079{
Brian Gaeke4efe3422003-09-21 01:23:46 +00001080 MRI.colorMethodArgs(Fn, *LRI, AddedInstrAtEntry.InstrnsBefore,
Vikram S. Adve814030a2003-07-29 19:49:21 +00001081 AddedInstrAtEntry.InstrnsAfter);
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001082}
1083
Ruchira Sasankae727f852001-09-18 22:43:57 +00001084
Brian Gaekeaf843702003-10-22 20:22:53 +00001085/// Determine whether the suggested color of each live range is really usable,
1086/// and then call its setSuggestedColorUsable() method to record the answer. A
1087/// suggested color is NOT usable when the suggested color is volatile AND
1088/// when there are call interferences.
1089///
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001090void PhyRegAlloc::markUnusableSugColors()
1091{
Brian Gaeke4efe3422003-09-21 01:23:46 +00001092 LiveRangeMapType::const_iterator HMI = (LRI->getLiveRangeMap())->begin();
1093 LiveRangeMapType::const_iterator HMIEnd = (LRI->getLiveRangeMap())->end();
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001094
Brian Gaeke43ce8fe2003-09-21 02:24:09 +00001095 for (; HMI != HMIEnd ; ++HMI ) {
1096 if (HMI->first) {
1097 LiveRange *L = HMI->second; // get the LiveRange
Brian Gaeke59b1c562003-09-24 17:50:28 +00001098 if (L && L->hasSuggestedColor ())
Brian Gaeke6a256cc2003-09-24 18:08:54 +00001099 L->setSuggestedColorUsable
1100 (!(MRI.isRegVolatile (L->getRegClassID (), L->getSuggestedColor ())
1101 && L->isCallInterference ()));
Brian Gaeke43ce8fe2003-09-21 02:24:09 +00001102 }
1103 } // for all LR's in hash map
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001104}
1105
1106
Brian Gaekeaf843702003-10-22 20:22:53 +00001107/// For each live range that is spilled, allocates a new spill position on the
1108/// stack, and set the stack offsets of the live range that will be spilled to
1109/// that position. This must be called just after coloring the LRs.
1110///
Chris Lattner37730942002-02-05 03:52:29 +00001111void PhyRegAlloc::allocateStackSpace4SpilledLRs() {
Chris Lattnerc083dcc2003-09-01 20:05:47 +00001112 if (DEBUG_RA) std::cerr << "\nSetting LR stack offsets for spills...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001113
Brian Gaeke4efe3422003-09-21 01:23:46 +00001114 LiveRangeMapType::const_iterator HMI = LRI->getLiveRangeMap()->begin();
1115 LiveRangeMapType::const_iterator HMIEnd = LRI->getLiveRangeMap()->end();
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001116
Chris Lattner7e708292002-06-25 16:13:24 +00001117 for ( ; HMI != HMIEnd ; ++HMI) {
Chris Lattner37730942002-02-05 03:52:29 +00001118 if (HMI->first && HMI->second) {
Vikram S. Adve3bf08922003-07-10 19:42:55 +00001119 LiveRange *L = HMI->second; // get the LiveRange
1120 if (L->isMarkedForSpill()) { // NOTE: allocating size of long Type **
Brian Gaeke4efe3422003-09-21 01:23:46 +00001121 int stackOffset = MF->getInfo()->allocateSpilledValue(Type::LongTy);
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001122 L->setSpillOffFromFP(stackOffset);
1123 if (DEBUG_RA)
Chris Lattnerc083dcc2003-09-01 20:05:47 +00001124 std::cerr << " LR# " << L->getUserIGNode()->getIndex()
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001125 << ": stack-offset = " << stackOffset << "\n";
1126 }
Chris Lattner37730942002-02-05 03:52:29 +00001127 }
1128 } // for all LR's in hash map
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001129}
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001130
Brian Gaeke874f4232003-09-21 02:50:21 +00001131
Brian Gaeke6a256cc2003-09-24 18:08:54 +00001132namespace {
1133 /// AllocInfo - Structure representing one instruction's
1134 /// operand's-worth of register allocation state. We create tables
1135 /// made out of these data structures to generate mapping information
1136 /// for this register allocator. (FIXME: This might move to a header
1137 /// file at some point.)
1138 ///
1139 struct AllocInfo {
1140 unsigned Instruction;
1141 unsigned Operand;
1142 unsigned AllocState;
1143 int Placement;
1144 AllocInfo (unsigned Instruction_, unsigned Operand_,
1145 unsigned AllocState_, int Placement_) :
1146 Instruction (Instruction_), Operand (Operand_),
1147 AllocState (AllocState_), Placement (Placement_) { }
1148 /// getConstantType - Return a StructType representing an AllocInfo
1149 /// object.
1150 ///
1151 static StructType *getConstantType () {
1152 std::vector<const Type *> TV;
1153 TV.push_back (Type::UIntTy);
1154 TV.push_back (Type::UIntTy);
1155 TV.push_back (Type::UIntTy);
1156 TV.push_back (Type::IntTy);
1157 return StructType::get (TV);
1158 }
1159 /// toConstant - Convert this AllocInfo into an LLVM Constant of type
1160 /// getConstantType(), and return the Constant.
1161 ///
1162 Constant *toConstant () const {
1163 StructType *ST = getConstantType ();
1164 std::vector<Constant *> CV;
1165 CV.push_back (ConstantUInt::get (Type::UIntTy, Instruction));
1166 CV.push_back (ConstantUInt::get (Type::UIntTy, Operand));
1167 CV.push_back (ConstantUInt::get (Type::UIntTy, AllocState));
1168 CV.push_back (ConstantSInt::get (Type::IntTy, Placement));
1169 return ConstantStruct::get (ST, CV);
1170 }
1171 };
1172}
1173
Brian Gaekeaf843702003-10-22 20:22:53 +00001174/// Save the global register allocation decisions made by the register
1175/// allocator so that they can be accessed later (sort of like "poor man's
1176/// debug info").
1177///
1178void PhyRegAlloc::saveState () {
Brian Gaeke60a3c552003-10-22 20:44:23 +00001179 std::vector<Constant *> &state = FnAllocState[Fn];
Brian Gaeke6a256cc2003-09-24 18:08:54 +00001180 unsigned Insn = 0;
1181 LiveRangeMapType::const_iterator HMIEnd = LRI->getLiveRangeMap ()->end ();
1182 for (const_inst_iterator II=inst_begin (Fn), IE=inst_end (Fn); II != IE; ++II)
1183 for (unsigned i = 0; i < (*II)->getNumOperands (); ++i) {
1184 const Value *V = (*II)->getOperand (i);
1185 // Don't worry about it unless it's something whose reg. we'll need.
1186 if (!isa<Argument> (V) && !isa<Instruction> (V))
1187 continue;
1188 LiveRangeMapType::const_iterator HMI = LRI->getLiveRangeMap ()->find (V);
1189 static const unsigned NotAllocated = 0, Allocated = 1, Spilled = 2;
1190 unsigned AllocState = NotAllocated;
1191 int Placement = -1;
1192 if ((HMI != HMIEnd) && HMI->second) {
1193 LiveRange *L = HMI->second;
1194 assert ((L->hasColor () || L->isMarkedForSpill ())
1195 && "Live range exists but not colored or spilled");
1196 if (L->hasColor()) {
1197 AllocState = Allocated;
1198 Placement = MRI.getUnifiedRegNum (L->getRegClassID (),
1199 L->getColor ());
1200 } else if (L->isMarkedForSpill ()) {
1201 AllocState = Spilled;
1202 assert (L->hasSpillOffset ()
1203 && "Live range marked for spill but has no spill offset");
1204 Placement = L->getSpillOffFromFP ();
1205 }
1206 }
1207 state.push_back (AllocInfo (Insn, i, AllocState,
1208 Placement).toConstant ());
1209 }
Brian Gaeke6a256cc2003-09-24 18:08:54 +00001210}
1211
Brian Gaekeaf843702003-10-22 20:22:53 +00001212/// Check the saved state filled in by saveState(), and abort if it looks
1213/// wrong. Only used when debugging.
1214///
1215void PhyRegAlloc::verifySavedState () {
1216 /// not yet implemented
1217}
Brian Gaeke6a256cc2003-09-24 18:08:54 +00001218
1219bool PhyRegAlloc::doFinalization (Module &M) {
1220 if (!SaveRegAllocState)
1221 return false; // Nothing to do here, unless we're saving state.
1222
1223 // Convert FnAllocState to a single Constant array and add it
1224 // to the Module.
1225 ArrayType *AT = ArrayType::get (AllocInfo::getConstantType (), 0);
1226 std::vector<const Type *> TV;
1227 TV.push_back (Type::UIntTy);
1228 TV.push_back (AT);
1229 PointerType *PT = PointerType::get (StructType::get (TV));
1230
1231 std::vector<Constant *> allstate;
1232 for (Module::iterator I = M.begin (), E = M.end (); I != E; ++I) {
1233 Function *F = I;
1234 if (FnAllocState.find (F) == FnAllocState.end ()) {
1235 allstate.push_back (ConstantPointerNull::get (PT));
1236 } else {
Brian Gaeke60a3c552003-10-22 20:44:23 +00001237 std::vector<Constant *> &state = FnAllocState[F];
1238
1239 // Convert state into an LLVM ConstantArray, and put it in a
1240 // ConstantStruct (named S) along with its size.
1241 unsigned Size = state.size ();
1242 ArrayType *AT = ArrayType::get (AllocInfo::getConstantType (), Size);
1243 std::vector<const Type *> TV;
1244 TV.push_back (Type::UIntTy);
1245 TV.push_back (AT);
1246 StructType *ST = StructType::get (TV);
1247 std::vector<Constant *> CV;
1248 CV.push_back (ConstantUInt::get (Type::UIntTy, Size));
1249 CV.push_back (ConstantArray::get (AT, state));
1250 Constant *S = ConstantStruct::get (ST, CV);
1251
Brian Gaeke6a256cc2003-09-24 18:08:54 +00001252 GlobalVariable *GV =
Brian Gaeke60a3c552003-10-22 20:44:23 +00001253 new GlobalVariable (ST, true,
1254 GlobalValue::InternalLinkage, S,
Brian Gaeke6a256cc2003-09-24 18:08:54 +00001255 F->getName () + ".regAllocState", &M);
Brian Gaeke60a3c552003-10-22 20:44:23 +00001256
Brian Gaeke6a256cc2003-09-24 18:08:54 +00001257 // Have: { uint, [Size x { uint, uint, uint, int }] } *
1258 // Cast it to: { uint, [0 x { uint, uint, uint, int }] } *
1259 Constant *CE = ConstantExpr::getCast (ConstantPointerRef::get (GV), PT);
1260 allstate.push_back (CE);
1261 }
1262 }
1263
1264 unsigned Size = allstate.size ();
1265 // Final structure type is:
1266 // { uint, [Size x { uint, [0 x { uint, uint, uint, int }] } *] }
1267 std::vector<const Type *> TV2;
1268 TV2.push_back (Type::UIntTy);
1269 ArrayType *AT2 = ArrayType::get (PT, Size);
1270 TV2.push_back (AT2);
1271 StructType *ST2 = StructType::get (TV2);
1272 std::vector<Constant *> CV2;
1273 CV2.push_back (ConstantUInt::get (Type::UIntTy, Size));
1274 CV2.push_back (ConstantArray::get (AT2, allstate));
1275 new GlobalVariable (ST2, true, GlobalValue::InternalLinkage,
1276 ConstantStruct::get (ST2, CV2), "_llvm_regAllocState",
1277 &M);
1278 return false; // No error.
1279}
1280
1281
Brian Gaekeaf843702003-10-22 20:22:53 +00001282/// Allocate registers for the machine code previously generated for F using
1283/// the graph-coloring algorithm.
1284///
Brian Gaeke4efe3422003-09-21 01:23:46 +00001285bool PhyRegAlloc::runOnFunction (Function &F) {
1286 if (DEBUG_RA)
1287 std::cerr << "\n********* Function "<< F.getName () << " ***********\n";
1288
1289 Fn = &F;
1290 MF = &MachineFunction::get (Fn);
1291 LVI = &getAnalysis<FunctionLiveVarInfo> ();
1292 LRI = new LiveRangeInfo (Fn, TM, RegClassList);
1293 LoopDepthCalc = &getAnalysis<LoopInfo> ();
1294
1295 // Create each RegClass for the target machine and add it to the
1296 // RegClassList. This must be done before calling constructLiveRanges().
1297 for (unsigned rc = 0; rc != NumOfRegClasses; ++rc)
1298 RegClassList.push_back (new RegClass (Fn, &TM.getRegInfo (),
1299 MRI.getMachineRegClass (rc)));
1300
1301 LRI->constructLiveRanges(); // create LR info
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001302 if (DEBUG_RA >= RA_DEBUG_LiveRanges)
Brian Gaeke4efe3422003-09-21 01:23:46 +00001303 LRI->printLiveRanges();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001304
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001305 createIGNodeListsAndIGs(); // create IGNode list and IGs
1306
1307 buildInterferenceGraphs(); // build IGs in all reg classes
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001308
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001309 if (DEBUG_RA >= RA_DEBUG_LiveRanges) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001310 // print all LRs in all reg classes
Chris Lattner7e708292002-06-25 16:13:24 +00001311 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
1312 RegClassList[rc]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001313
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001314 // print IGs in all register classes
Chris Lattner7e708292002-06-25 16:13:24 +00001315 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
1316 RegClassList[rc]->printIG();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001317 }
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001318
Brian Gaeke4efe3422003-09-21 01:23:46 +00001319 LRI->coalesceLRs(); // coalesce all live ranges
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +00001320
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001321 if (DEBUG_RA >= RA_DEBUG_LiveRanges) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001322 // print all LRs in all reg classes
Chris Lattnerf726e772002-10-28 19:22:04 +00001323 for (unsigned rc=0; rc < NumOfRegClasses; rc++)
1324 RegClassList[rc]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001325
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001326 // print IGs in all register classes
Chris Lattnerf726e772002-10-28 19:22:04 +00001327 for (unsigned rc=0; rc < NumOfRegClasses; rc++)
1328 RegClassList[rc]->printIG();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001329 }
1330
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001331 // mark un-usable suggested color before graph coloring algorithm.
1332 // When this is done, the graph coloring algo will not reserve
1333 // suggested color unnecessarily - they can be used by another LR
1334 markUnusableSugColors();
1335
1336 // color all register classes using the graph coloring algo
Chris Lattner7e708292002-06-25 16:13:24 +00001337 for (unsigned rc=0; rc < NumOfRegClasses ; rc++)
Chris Lattnerf726e772002-10-28 19:22:04 +00001338 RegClassList[rc]->colorAllRegs();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001339
Misha Brukman37f92e22003-09-11 22:34:13 +00001340 // After graph coloring, if some LRs did not receive a color (i.e, spilled)
1341 // a position for such spilled LRs
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001342 allocateStackSpace4SpilledLRs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001343
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001344 // Reset the temp. area on the stack before use by the first instruction.
1345 // This will also happen after updating each instruction.
Brian Gaeke4efe3422003-09-21 01:23:46 +00001346 MF->getInfo()->popAllTempValues();
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001347
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001348 // color incoming args - if the correct color was not received
1349 // insert code to copy to the correct register
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001350 colorIncomingArgs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001351
Brian Gaeke6a256cc2003-09-24 18:08:54 +00001352 // Save register allocation state for this function in a Constant.
1353 if (SaveRegAllocState)
1354 saveState();
Brian Gaekeaf843702003-10-22 20:22:53 +00001355 if (DEBUG_RA) { // Check our work.
1356 verifySavedState ();
1357 }
Brian Gaeke6a256cc2003-09-24 18:08:54 +00001358
Brian Gaeke60a3c552003-10-22 20:44:23 +00001359 // Now update the machine code with register names and add any additional
1360 // code inserted by the register allocator to the instruction stream.
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001361 updateMachineCode();
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001362
Chris Lattner045e7c82001-09-19 16:26:23 +00001363 if (DEBUG_RA) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +00001364 std::cerr << "\n**** Machine Code After Register Allocation:\n\n";
Brian Gaeke4efe3422003-09-21 01:23:46 +00001365 MF->dump();
Chris Lattner045e7c82001-09-19 16:26:23 +00001366 }
Brian Gaeke4efe3422003-09-21 01:23:46 +00001367
1368 // Tear down temporary data structures
1369 for (unsigned rc = 0; rc < NumOfRegClasses; ++rc)
1370 delete RegClassList[rc];
1371 RegClassList.clear ();
1372 AddedInstrMap.clear ();
1373 OperandsColoredMap.clear ();
1374 ScratchRegsUsed.clear ();
1375 AddedInstrAtEntry.clear ();
1376 delete LRI;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001377
Brian Gaeke4efe3422003-09-21 01:23:46 +00001378 if (DEBUG_RA) std::cerr << "\nRegister allocation complete!\n";
1379 return false; // Function was not modified
1380}