blob: fd615545f234d4db45fc7adc980d73e5aa233bc7 [file] [log] [blame]
Chris Lattner179cdfb2002-08-09 20:08:03 +00001//===-- PhyRegAlloc.cpp ---------------------------------------------------===//
Vikram S. Adve12af1642001-11-08 04:48:50 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Brian Gaeke222bd532003-09-24 18:16:23 +000010// Traditional graph-coloring global register allocator currently used
11// by the SPARC back-end.
12//
13// NOTE: This register allocator has some special support
14// for the Reoptimizer, such as not saving some registers on calls to
15// the first-level instrumentation function.
16//
17// NOTE 2: This register allocator can save its state in a global
18// variable in the module it's working on. This feature is not
19// thread-safe; if you have doubts, leave it turned off.
Chris Lattner179cdfb2002-08-09 20:08:03 +000020//
21//===----------------------------------------------------------------------===//
Ruchira Sasanka8e604792001-09-14 21:18:34 +000022
Brian Gaeke537132b2003-10-23 20:32:55 +000023#include "AllocInfo.h"
Misha Brukman396c8c32003-10-23 18:06:27 +000024#include "IGNode.h"
Chris Lattner70b2f562003-09-01 20:09:04 +000025#include "PhyRegAlloc.h"
Chris Lattner4309e732003-01-15 19:57:07 +000026#include "RegAllocCommon.h"
Chris Lattner9d4ed152003-01-15 21:14:01 +000027#include "RegClass.h"
Misha Brukman396c8c32003-10-23 18:06:27 +000028#include "llvm/Constants.h"
29#include "llvm/DerivedTypes.h"
30#include "llvm/iOther.h"
31#include "llvm/Module.h"
32#include "llvm/Type.h"
33#include "llvm/Analysis/LoopInfo.h"
Chris Lattner797c1362003-09-30 20:13:59 +000034#include "llvm/CodeGen/FunctionLiveVarInfo.h"
35#include "llvm/CodeGen/InstrSelection.h"
Brian Gaeke3ceac852003-10-30 21:21:33 +000036#include "llvm/CodeGen/MachineCodeForInstruction.h"
Misha Brukman396c8c32003-10-23 18:06:27 +000037#include "llvm/CodeGen/MachineFunction.h"
38#include "llvm/CodeGen/MachineFunctionInfo.h"
Brian Gaeke874f4232003-09-21 02:50:21 +000039#include "llvm/CodeGen/MachineInstr.h"
Chris Lattnerf6ee49f2003-01-15 18:08:07 +000040#include "llvm/CodeGen/MachineInstrBuilder.h"
Vikram S. Advedabb41d2002-05-19 15:29:31 +000041#include "llvm/CodeGen/MachineInstrAnnot.h"
Chris Lattner797c1362003-09-30 20:13:59 +000042#include "llvm/CodeGen/Passes.h"
Chris Lattner797c1362003-09-30 20:13:59 +000043#include "llvm/Support/InstIterator.h"
Misha Brukman396c8c32003-10-23 18:06:27 +000044#include "llvm/Target/TargetInstrInfo.h"
Chris Lattner4bc23482002-09-15 07:07:55 +000045#include "Support/CommandLine.h"
Misha Brukman396c8c32003-10-23 18:06:27 +000046#include "Support/SetOperations.h"
47#include "Support/STLExtras.h"
Brian Gaekebd353fb2003-09-21 03:57:37 +000048#include <cmath>
Vikram S. Adve12af1642001-11-08 04:48:50 +000049
Chris Lattner70e60cb2002-05-22 17:08:27 +000050RegAllocDebugLevel_t DEBUG_RA;
Vikram S. Adve39c94e12002-09-14 23:05:33 +000051
Brian Gaeke8fc49342003-10-24 21:21:58 +000052/// The reoptimizer wants to be able to grovel through the register
53/// allocator's state after it has done its job. This is a hack.
54///
55PhyRegAlloc::SavedStateMapTy ExportedFnAllocState;
56const bool SaveStateToModule = false;
57
Chris Lattner5ff62e92002-07-22 02:10:13 +000058static cl::opt<RegAllocDebugLevel_t, true>
59DRA_opt("dregalloc", cl::Hidden, cl::location(DEBUG_RA),
60 cl::desc("enable register allocation debugging information"),
61 cl::values(
Vikram S. Adve39c94e12002-09-14 23:05:33 +000062 clEnumValN(RA_DEBUG_None , "n", "disable debug output"),
63 clEnumValN(RA_DEBUG_Results, "y", "debug output for allocation results"),
64 clEnumValN(RA_DEBUG_Coloring, "c", "debug output for graph coloring step"),
65 clEnumValN(RA_DEBUG_Interference,"ig","debug output for interference graphs"),
66 clEnumValN(RA_DEBUG_LiveRanges , "lr","debug output for live ranges"),
67 clEnumValN(RA_DEBUG_Verbose, "v", "extra debug output"),
Chris Lattner5ff62e92002-07-22 02:10:13 +000068 0));
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000069
Brian Gaeke59b1c562003-09-24 17:50:28 +000070static cl::opt<bool>
71SaveRegAllocState("save-ra-state", cl::Hidden,
72 cl::desc("write reg. allocator state into module"));
73
Brian Gaekebf3c4cf2003-08-14 06:09:32 +000074FunctionPass *getRegisterAllocator(TargetMachine &T) {
Brian Gaeke4efe3422003-09-21 01:23:46 +000075 return new PhyRegAlloc (T);
Chris Lattner2f9b28e2002-02-04 15:54:09 +000076}
Chris Lattner6dd98a62002-02-04 00:33:08 +000077
Chris Lattner8474f6f2003-09-23 15:13:04 +000078void PhyRegAlloc::getAnalysisUsage(AnalysisUsage &AU) const {
79 AU.addRequired<LoopInfo> ();
80 AU.addRequired<FunctionLiveVarInfo> ();
81}
82
83
Brian Gaekeaf843702003-10-22 20:22:53 +000084/// Initialize interference graphs (one in each reg class) and IGNodeLists
85/// (one in each IG). The actual nodes will be pushed later.
86///
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000087void PhyRegAlloc::createIGNodeListsAndIGs() {
Chris Lattnerc083dcc2003-09-01 20:05:47 +000088 if (DEBUG_RA >= RA_DEBUG_LiveRanges) std::cerr << "Creating LR lists ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +000089
Brian Gaeke4efe3422003-09-21 01:23:46 +000090 LiveRangeMapType::const_iterator HMI = LRI->getLiveRangeMap()->begin();
Brian Gaeke4efe3422003-09-21 01:23:46 +000091 LiveRangeMapType::const_iterator HMIEnd = LRI->getLiveRangeMap()->end();
Ruchira Sasanka8e604792001-09-14 21:18:34 +000092
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000093 for (; HMI != HMIEnd ; ++HMI ) {
94 if (HMI->first) {
95 LiveRange *L = HMI->second; // get the LiveRange
96 if (!L) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +000097 if (DEBUG_RA)
Chris Lattnerc083dcc2003-09-01 20:05:47 +000098 std::cerr << "\n**** ?!?WARNING: NULL LIVE RANGE FOUND FOR: "
Vikram S. Adve39c94e12002-09-14 23:05:33 +000099 << RAV(HMI->first) << "****\n";
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000100 continue;
101 }
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000102
103 // if the Value * is not null, and LR is not yet written to the IGNodeList
Chris Lattner7e708292002-06-25 16:13:24 +0000104 if (!(L->getUserIGNode()) ) {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000105 RegClass *const RC = // RegClass of first value in the LR
Brian Gaeke59b1c562003-09-24 17:50:28 +0000106 RegClassList[ L->getRegClassID() ];
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000107 RC->addLRToIG(L); // add this LR to an IG
108 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000109 }
110 }
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000111
112 // init RegClassList
Chris Lattner7e708292002-06-25 16:13:24 +0000113 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000114 RegClassList[rc]->createInterferenceGraph();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000115
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000116 if (DEBUG_RA >= RA_DEBUG_LiveRanges) std::cerr << "LRLists Created!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000117}
118
119
Brian Gaekeaf843702003-10-22 20:22:53 +0000120/// Add all interferences for a given instruction. Interference occurs only
121/// if the LR of Def (Inst or Arg) is of the same reg class as that of live
122/// var. The live var passed to this function is the LVset AFTER the
123/// instruction.
124///
125void PhyRegAlloc::addInterference(const Value *Def, const ValueSet *LVSet,
Chris Lattner296b7732002-02-05 02:52:05 +0000126 bool isCallInst) {
Chris Lattner296b7732002-02-05 02:52:05 +0000127 ValueSet::const_iterator LIt = LVSet->begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000128
129 // get the live range of instruction
Brian Gaeke4efe3422003-09-21 01:23:46 +0000130 const LiveRange *const LROfDef = LRI->getLiveRangeForValue( Def );
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000131
132 IGNode *const IGNodeOfDef = LROfDef->getUserIGNode();
133 assert( IGNodeOfDef );
134
135 RegClass *const RCOfDef = LROfDef->getRegClass();
136
137 // for each live var in live variable set
Chris Lattner7e708292002-06-25 16:13:24 +0000138 for ( ; LIt != LVSet->end(); ++LIt) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000139
Vikram S. Advef5af6362002-07-08 23:15:32 +0000140 if (DEBUG_RA >= RA_DEBUG_Verbose)
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000141 std::cerr << "< Def=" << RAV(Def) << ", Lvar=" << RAV(*LIt) << "> ";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000142
143 // get the live range corresponding to live var
Brian Gaeke4efe3422003-09-21 01:23:46 +0000144 LiveRange *LROfVar = LRI->getLiveRangeForValue(*LIt);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000145
146 // LROfVar can be null if it is a const since a const
147 // doesn't have a dominating def - see Assumptions above
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000148 if (LROfVar)
149 if (LROfDef != LROfVar) // do not set interf for same LR
150 if (RCOfDef == LROfVar->getRegClass()) // 2 reg classes are the same
151 RCOfDef->setInterference( LROfDef, LROfVar);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000152 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000153}
154
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000155
Brian Gaekeaf843702003-10-22 20:22:53 +0000156/// For a call instruction, this method sets the CallInterference flag in
157/// the LR of each variable live in the Live Variable Set live after the
158/// call instruction (except the return value of the call instruction - since
159/// the return value does not interfere with that call itself).
160///
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000161void PhyRegAlloc::setCallInterferences(const MachineInstr *MInst,
Chris Lattner296b7732002-02-05 02:52:05 +0000162 const ValueSet *LVSetAft) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000163 if (DEBUG_RA >= RA_DEBUG_Interference)
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000164 std::cerr << "\n For call inst: " << *MInst;
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000165
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000166 // for each live var in live variable set after machine inst
Vikram S. Adve65b2f402003-07-02 01:24:00 +0000167 for (ValueSet::const_iterator LIt = LVSetAft->begin(), LEnd = LVSetAft->end();
168 LIt != LEnd; ++LIt) {
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000169
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000170 // get the live range corresponding to live var
Brian Gaeke4efe3422003-09-21 01:23:46 +0000171 LiveRange *const LR = LRI->getLiveRangeForValue(*LIt );
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000172
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000173 // LR can be null if it is a const since a const
174 // doesn't have a dominating def - see Assumptions above
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000175 if (LR ) {
176 if (DEBUG_RA >= RA_DEBUG_Interference) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000177 std::cerr << "\n\tLR after Call: ";
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000178 printSet(*LR);
179 }
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000180 LR->setCallInterference();
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000181 if (DEBUG_RA >= RA_DEBUG_Interference) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000182 std::cerr << "\n ++After adding call interference for LR: " ;
Chris Lattner296b7732002-02-05 02:52:05 +0000183 printSet(*LR);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000184 }
185 }
186
187 }
188
Vikram S. Adve1a53f032002-03-31 18:54:37 +0000189 // Now find the LR of the return value of the call
190 // We do this because, we look at the LV set *after* the instruction
191 // to determine, which LRs must be saved across calls. The return value
192 // of the call is live in this set - but it does not interfere with call
193 // (i.e., we can allocate a volatile register to the return value)
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000194 CallArgsDescriptor* argDesc = CallArgsDescriptor::get(MInst);
195
196 if (const Value *RetVal = argDesc->getReturnValue()) {
Brian Gaeke4efe3422003-09-21 01:23:46 +0000197 LiveRange *RetValLR = LRI->getLiveRangeForValue( RetVal );
Vikram S. Adve1a53f032002-03-31 18:54:37 +0000198 assert( RetValLR && "No LR for RetValue of call");
199 RetValLR->clearCallInterference();
200 }
201
202 // If the CALL is an indirect call, find the LR of the function pointer.
203 // That has a call interference because it conflicts with outgoing args.
Chris Lattner7e708292002-06-25 16:13:24 +0000204 if (const Value *AddrVal = argDesc->getIndirectFuncPtr()) {
Brian Gaeke4efe3422003-09-21 01:23:46 +0000205 LiveRange *AddrValLR = LRI->getLiveRangeForValue( AddrVal );
Vikram S. Adve1a53f032002-03-31 18:54:37 +0000206 assert( AddrValLR && "No LR for indirect addr val of call");
207 AddrValLR->setCallInterference();
208 }
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000209}
210
211
Brian Gaekeaf843702003-10-22 20:22:53 +0000212/// Create interferences in the IG of each RegClass, and calculate the spill
213/// cost of each Live Range (it is done in this method to save another pass
214/// over the code).
215///
216void PhyRegAlloc::buildInterferenceGraphs() {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000217 if (DEBUG_RA >= RA_DEBUG_Interference)
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000218 std::cerr << "Creating interference graphs ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000219
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000220 unsigned BBLoopDepthCost;
Brian Gaeke4efe3422003-09-21 01:23:46 +0000221 for (MachineFunction::iterator BBI = MF->begin(), BBE = MF->end();
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000222 BBI != BBE; ++BBI) {
Chris Lattnerf726e772002-10-28 19:22:04 +0000223 const MachineBasicBlock &MBB = *BBI;
224 const BasicBlock *BB = MBB.getBasicBlock();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000225
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000226 // find the 10^(loop_depth) of this BB
Chris Lattnerf726e772002-10-28 19:22:04 +0000227 BBLoopDepthCost = (unsigned)pow(10.0, LoopDepthCalc->getLoopDepth(BB));
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000228
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000229 // get the iterator for machine instructions
Chris Lattnerf726e772002-10-28 19:22:04 +0000230 MachineBasicBlock::const_iterator MII = MBB.begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000231
232 // iterate over all the machine instructions in BB
Chris Lattnerf726e772002-10-28 19:22:04 +0000233 for ( ; MII != MBB.end(); ++MII) {
234 const MachineInstr *MInst = *MII;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000235
236 // get the LV set after the instruction
Chris Lattnerf726e772002-10-28 19:22:04 +0000237 const ValueSet &LVSetAI = LVI->getLiveVarSetAfterMInst(MInst, BB);
238 bool isCallInst = TM.getInstrInfo().isCall(MInst->getOpCode());
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000239
Brian Gaekeaf843702003-10-22 20:22:53 +0000240 if (isCallInst) {
Misha Brukman37f92e22003-09-11 22:34:13 +0000241 // set the isCallInterference flag of each live range which extends
242 // across this call instruction. This information is used by graph
243 // coloring algorithm to avoid allocating volatile colors to live ranges
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000244 // that span across calls (since they have to be saved/restored)
Chris Lattner748697d2002-02-05 04:20:12 +0000245 setCallInterferences(MInst, &LVSetAI);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000246 }
247
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000248 // iterate over all MI operands to find defs
Chris Lattner2f898d22002-02-05 06:02:59 +0000249 for (MachineInstr::const_val_op_iterator OpI = MInst->begin(),
250 OpE = MInst->end(); OpI != OpE; ++OpI) {
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000251 if (OpI.isDefOnly() || OpI.isDefAndUse()) // create a new LR since def
Chris Lattner748697d2002-02-05 04:20:12 +0000252 addInterference(*OpI, &LVSetAI, isCallInst);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000253
254 // Calculate the spill cost of each live range
Brian Gaeke4efe3422003-09-21 01:23:46 +0000255 LiveRange *LR = LRI->getLiveRangeForValue(*OpI);
Chris Lattner2f898d22002-02-05 06:02:59 +0000256 if (LR) LR->addSpillCost(BBLoopDepthCost);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000257 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000258
Brian Gaekeaf843702003-10-22 20:22:53 +0000259 // Mark all operands of pseudo-instructions as interfering with one
260 // another. This must be done because pseudo-instructions may be
261 // expanded to multiple instructions by the assembler, so all the
262 // operands must get distinct registers.
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000263 if (TM.getInstrInfo().isPseudoInstr(MInst->getOpCode()))
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000264 addInterf4PseudoInstr(MInst);
265
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000266 // Also add interference for any implicit definitions in a machine
267 // instr (currently, only calls have this).
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000268 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000269 for (unsigned z=0; z < NumOfImpRefs; z++)
270 if (MInst->getImplicitOp(z).opIsDefOnly() ||
271 MInst->getImplicitOp(z).opIsDefAndUse())
272 addInterference( MInst->getImplicitRef(z), &LVSetAI, isCallInst );
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +0000273
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000274 } // for all machine instructions in BB
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000275 } // for all BBs in function
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000276
Misha Brukman37f92e22003-09-11 22:34:13 +0000277 // add interferences for function arguments. Since there are no explicit
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000278 // defs in the function for args, we have to add them manually
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000279 addInterferencesForArgs();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000280
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000281 if (DEBUG_RA >= RA_DEBUG_Interference)
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000282 std::cerr << "Interference graphs calculated!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000283}
284
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000285
Brian Gaekeaf843702003-10-22 20:22:53 +0000286/// Mark all operands of the given MachineInstr as interfering with one
287/// another.
288///
Brian Gaeke43ce8fe2003-09-21 02:24:09 +0000289void PhyRegAlloc::addInterf4PseudoInstr(const MachineInstr *MInst) {
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000290 bool setInterf = false;
291
Brian Gaeke43ce8fe2003-09-21 02:24:09 +0000292 // iterate over MI operands to find defs
Chris Lattner2f898d22002-02-05 06:02:59 +0000293 for (MachineInstr::const_val_op_iterator It1 = MInst->begin(),
294 ItE = MInst->end(); It1 != ItE; ++It1) {
Brian Gaeke4efe3422003-09-21 01:23:46 +0000295 const LiveRange *LROfOp1 = LRI->getLiveRangeForValue(*It1);
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000296 assert((LROfOp1 || !It1.isUseOnly())&&"No LR for Def in PSEUDO insruction");
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000297
Chris Lattner2f898d22002-02-05 06:02:59 +0000298 MachineInstr::const_val_op_iterator It2 = It1;
Chris Lattner7e708292002-06-25 16:13:24 +0000299 for (++It2; It2 != ItE; ++It2) {
Brian Gaeke4efe3422003-09-21 01:23:46 +0000300 const LiveRange *LROfOp2 = LRI->getLiveRangeForValue(*It2);
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000301
Chris Lattner2f898d22002-02-05 06:02:59 +0000302 if (LROfOp2) {
303 RegClass *RCOfOp1 = LROfOp1->getRegClass();
304 RegClass *RCOfOp2 = LROfOp2->getRegClass();
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000305
Chris Lattner7e708292002-06-25 16:13:24 +0000306 if (RCOfOp1 == RCOfOp2 ){
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000307 RCOfOp1->setInterference( LROfOp1, LROfOp2 );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000308 setInterf = true;
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000309 }
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000310 } // if Op2 has a LR
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000311 } // for all other defs in machine instr
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000312 } // for all operands in an instruction
313
Chris Lattner2f898d22002-02-05 06:02:59 +0000314 if (!setInterf && MInst->getNumOperands() > 2) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000315 std::cerr << "\nInterf not set for any operand in pseudo instr:\n";
316 std::cerr << *MInst;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000317 assert(0 && "Interf not set for pseudo instr with > 2 operands" );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000318 }
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000319}
320
321
Brian Gaekeaf843702003-10-22 20:22:53 +0000322/// Add interferences for incoming arguments to a function.
323///
Chris Lattner296b7732002-02-05 02:52:05 +0000324void PhyRegAlloc::addInterferencesForArgs() {
325 // get the InSet of root BB
Chris Lattnerf726e772002-10-28 19:22:04 +0000326 const ValueSet &InSet = LVI->getInSetOfBB(&Fn->front());
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000327
Chris Lattnerf726e772002-10-28 19:22:04 +0000328 for (Function::const_aiterator AI = Fn->abegin(); AI != Fn->aend(); ++AI) {
Chris Lattner7e708292002-06-25 16:13:24 +0000329 // add interferences between args and LVars at start
330 addInterference(AI, &InSet, false);
331
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000332 if (DEBUG_RA >= RA_DEBUG_Interference)
Brian Gaekeaf843702003-10-22 20:22:53 +0000333 std::cerr << " - %% adding interference for argument " << RAV(AI) << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000334 }
335}
336
337
Brian Gaekeaf843702003-10-22 20:22:53 +0000338/// The following are utility functions used solely by updateMachineCode and
339/// the functions that it calls. They should probably be folded back into
340/// updateMachineCode at some point.
341///
Vikram S. Adve48762092002-04-25 04:34:15 +0000342
Brian Gaekeaf843702003-10-22 20:22:53 +0000343// used by: updateMachineCode (1 time), PrependInstructions (1 time)
344inline void InsertBefore(MachineInstr* newMI, MachineBasicBlock& MBB,
345 MachineBasicBlock::iterator& MII) {
Chris Lattnerf726e772002-10-28 19:22:04 +0000346 MII = MBB.insert(MII, newMI);
Vikram S. Advecb202e32002-10-11 16:12:40 +0000347 ++MII;
348}
349
Brian Gaekeaf843702003-10-22 20:22:53 +0000350// used by: AppendInstructions (1 time)
351inline void InsertAfter(MachineInstr* newMI, MachineBasicBlock& MBB,
352 MachineBasicBlock::iterator& MII) {
Vikram S. Advecb202e32002-10-11 16:12:40 +0000353 ++MII; // insert before the next instruction
Chris Lattnerf726e772002-10-28 19:22:04 +0000354 MII = MBB.insert(MII, newMI);
Vikram S. Advecb202e32002-10-11 16:12:40 +0000355}
356
Brian Gaekeaf843702003-10-22 20:22:53 +0000357// used by: updateMachineCode (1 time)
358inline void DeleteInstruction(MachineBasicBlock& MBB,
359 MachineBasicBlock::iterator& MII) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000360 MII = MBB.erase(MII);
361}
362
Brian Gaekeaf843702003-10-22 20:22:53 +0000363// used by: updateMachineCode (1 time)
364inline void SubstituteInPlace(MachineInstr* newMI, MachineBasicBlock& MBB,
365 MachineBasicBlock::iterator MII) {
Vikram S. Advecb202e32002-10-11 16:12:40 +0000366 *MII = newMI;
367}
368
Brian Gaekeaf843702003-10-22 20:22:53 +0000369// used by: updateMachineCode (2 times)
370inline void PrependInstructions(std::vector<MachineInstr *> &IBef,
371 MachineBasicBlock& MBB,
372 MachineBasicBlock::iterator& MII,
373 const std::string& msg) {
374 if (!IBef.empty()) {
Vikram S. Adve48762092002-04-25 04:34:15 +0000375 MachineInstr* OrigMI = *MII;
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000376 std::vector<MachineInstr *>::iterator AdIt;
Brian Gaekeaf843702003-10-22 20:22:53 +0000377 for (AdIt = IBef.begin(); AdIt != IBef.end() ; ++AdIt) {
Vikram S. Adve48762092002-04-25 04:34:15 +0000378 if (DEBUG_RA) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000379 if (OrigMI) std::cerr << "For MInst:\n " << *OrigMI;
380 std::cerr << msg << "PREPENDed instr:\n " << **AdIt << "\n";
Vikram S. Adve48762092002-04-25 04:34:15 +0000381 }
Chris Lattnerf726e772002-10-28 19:22:04 +0000382 InsertBefore(*AdIt, MBB, MII);
Vikram S. Adve48762092002-04-25 04:34:15 +0000383 }
384 }
385}
386
Brian Gaekeaf843702003-10-22 20:22:53 +0000387// used by: updateMachineCode (1 time)
388inline void AppendInstructions(std::vector<MachineInstr *> &IAft,
389 MachineBasicBlock& MBB,
390 MachineBasicBlock::iterator& MII,
391 const std::string& msg) {
392 if (!IAft.empty()) {
Vikram S. Adve48762092002-04-25 04:34:15 +0000393 MachineInstr* OrigMI = *MII;
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000394 std::vector<MachineInstr *>::iterator AdIt;
Brian Gaekeaf843702003-10-22 20:22:53 +0000395 for ( AdIt = IAft.begin(); AdIt != IAft.end() ; ++AdIt ) {
Chris Lattner7e708292002-06-25 16:13:24 +0000396 if (DEBUG_RA) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000397 if (OrigMI) std::cerr << "For MInst:\n " << *OrigMI;
398 std::cerr << msg << "APPENDed instr:\n " << **AdIt << "\n";
Vikram S. Adve48762092002-04-25 04:34:15 +0000399 }
Chris Lattnerf726e772002-10-28 19:22:04 +0000400 InsertAfter(*AdIt, MBB, MII);
Vikram S. Adve48762092002-04-25 04:34:15 +0000401 }
402 }
403}
404
Brian Gaekeaf843702003-10-22 20:22:53 +0000405/// Set the registers for operands in the given MachineInstr, if a register was
406/// successfully allocated. Return true if any of its operands has been marked
407/// for spill.
408///
Brian Gaeke4efe3422003-09-21 01:23:46 +0000409bool PhyRegAlloc::markAllocatedRegs(MachineInstr* MInst)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000410{
Vikram S. Adve814030a2003-07-29 19:49:21 +0000411 bool instrNeedsSpills = false;
412
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000413 // First, set the registers for operands in the machine instruction
414 // if a register was successfully allocated. Do this first because we
415 // will need to know which registers are already used by this instr'n.
Brian Gaekeaf843702003-10-22 20:22:53 +0000416 for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000417 MachineOperand& Op = MInst->getOperand(OpNum);
418 if (Op.getType() == MachineOperand::MO_VirtualRegister ||
Brian Gaekeaf843702003-10-22 20:22:53 +0000419 Op.getType() == MachineOperand::MO_CCRegister) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000420 const Value *const Val = Op.getVRegValue();
Brian Gaeke4efe3422003-09-21 01:23:46 +0000421 if (const LiveRange* LR = LRI->getLiveRangeForValue(Val)) {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000422 // Remember if any operand needs spilling
423 instrNeedsSpills |= LR->isMarkedForSpill();
424
425 // An operand may have a color whether or not it needs spilling
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000426 if (LR->hasColor())
427 MInst->SetRegForOperand(OpNum,
Brian Gaeke59b1c562003-09-24 17:50:28 +0000428 MRI.getUnifiedRegNum(LR->getRegClassID(),
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000429 LR->getColor()));
Vikram S. Adve814030a2003-07-29 19:49:21 +0000430 }
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000431 }
432 } // for each operand
Vikram S. Adve814030a2003-07-29 19:49:21 +0000433
434 return instrNeedsSpills;
435}
436
Brian Gaekeaf843702003-10-22 20:22:53 +0000437/// Mark allocated registers (using markAllocatedRegs()) on the instruction
438/// that MII points to. Then, if it's a call instruction, insert caller-saving
439/// code before and after it. Finally, insert spill code before and after it,
440/// using insertCode4SpilledLR().
441///
Vikram S. Adve814030a2003-07-29 19:49:21 +0000442void PhyRegAlloc::updateInstruction(MachineBasicBlock::iterator& MII,
Brian Gaekeaf843702003-10-22 20:22:53 +0000443 MachineBasicBlock &MBB) {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000444 MachineInstr* MInst = *MII;
445 unsigned Opcode = MInst->getOpCode();
446
447 // Reset tmp stack positions so they can be reused for each machine instr.
Brian Gaeke4efe3422003-09-21 01:23:46 +0000448 MF->getInfo()->popAllTempValues();
Vikram S. Adve814030a2003-07-29 19:49:21 +0000449
450 // Mark the operands for which regs have been allocated.
Brian Gaeke4efe3422003-09-21 01:23:46 +0000451 bool instrNeedsSpills = markAllocatedRegs(*MII);
Vikram S. Adve814030a2003-07-29 19:49:21 +0000452
453#ifndef NDEBUG
454 // Mark that the operands have been updated. Later,
455 // setRelRegsUsedByThisInst() is called to find registers used by each
456 // MachineInst, and it should not be used for an instruction until
457 // this is done. This flag just serves as a sanity check.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000458 OperandsColoredMap[MInst] = true;
Vikram S. Adve814030a2003-07-29 19:49:21 +0000459#endif
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000460
Vikram S. Advebc001b22003-07-25 21:06:09 +0000461 // Now insert caller-saving code before/after the call.
462 // Do this before inserting spill code since some registers must be
463 // used by save/restore and spill code should not use those registers.
Vikram S. Advebc001b22003-07-25 21:06:09 +0000464 if (TM.getInstrInfo().isCall(Opcode)) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000465 AddedInstrns &AI = AddedInstrMap[MInst];
Vikram S. Adve814030a2003-07-29 19:49:21 +0000466 insertCallerSavingCode(AI.InstrnsBefore, AI.InstrnsAfter, MInst,
467 MBB.getBasicBlock());
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000468 }
Vikram S. Advebc001b22003-07-25 21:06:09 +0000469
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000470 // Now insert spill code for remaining operands not allocated to
471 // registers. This must be done even for call return instructions
472 // since those are not handled by the special code above.
Vikram S. Adve814030a2003-07-29 19:49:21 +0000473 if (instrNeedsSpills)
Brian Gaekeaf843702003-10-22 20:22:53 +0000474 for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000475 MachineOperand& Op = MInst->getOperand(OpNum);
476 if (Op.getType() == MachineOperand::MO_VirtualRegister ||
Brian Gaekeaf843702003-10-22 20:22:53 +0000477 Op.getType() == MachineOperand::MO_CCRegister) {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000478 const Value* Val = Op.getVRegValue();
Brian Gaeke4efe3422003-09-21 01:23:46 +0000479 if (const LiveRange *LR = LRI->getLiveRangeForValue(Val))
Vikram S. Adve814030a2003-07-29 19:49:21 +0000480 if (LR->isMarkedForSpill())
481 insertCode4SpilledLR(LR, MII, MBB, OpNum);
482 }
483 } // for each operand
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000484}
485
Brian Gaekeaf843702003-10-22 20:22:53 +0000486/// Iterate over all the MachineBasicBlocks in the current function and set
487/// the allocated registers for each instruction (using updateInstruction()),
488/// after register allocation is complete. Then move code out of delay slots.
489///
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000490void PhyRegAlloc::updateMachineCode()
491{
Chris Lattner7e708292002-06-25 16:13:24 +0000492 // Insert any instructions needed at method entry
Brian Gaeke4efe3422003-09-21 01:23:46 +0000493 MachineBasicBlock::iterator MII = MF->front().begin();
494 PrependInstructions(AddedInstrAtEntry.InstrnsBefore, MF->front(), MII,
Chris Lattner7e708292002-06-25 16:13:24 +0000495 "At function entry: \n");
496 assert(AddedInstrAtEntry.InstrnsAfter.empty() &&
497 "InstrsAfter should be unnecessary since we are just inserting at "
498 "the function entry point here.");
Vikram S. Adve48762092002-04-25 04:34:15 +0000499
Brian Gaeke4efe3422003-09-21 01:23:46 +0000500 for (MachineFunction::iterator BBI = MF->begin(), BBE = MF->end();
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000501 BBI != BBE; ++BBI) {
Chris Lattnerf726e772002-10-28 19:22:04 +0000502 MachineBasicBlock &MBB = *BBI;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000503
504 // Iterate over all machine instructions in BB and mark operands with
505 // their assigned registers or insert spill code, as appropriate.
506 // Also, fix operands of call/return instructions.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000507 for (MachineBasicBlock::iterator MII = MBB.begin(); MII != MBB.end(); ++MII)
Vikram S. Adve814030a2003-07-29 19:49:21 +0000508 if (! TM.getInstrInfo().isDummyPhiInstr((*MII)->getOpCode()))
509 updateInstruction(MII, MBB);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000510
511 // Now, move code out of delay slots of branches and returns if needed.
512 // (Also, move "after" code from calls to the last delay slot instruction.)
513 // Moving code out of delay slots is needed in 2 situations:
514 // (1) If this is a branch and it needs instructions inserted after it,
515 // move any existing instructions out of the delay slot so that the
516 // instructions can go into the delay slot. This only supports the
517 // case that #instrsAfter <= #delay slots.
518 //
519 // (2) If any instruction in the delay slot needs
520 // instructions inserted, move it out of the delay slot and before the
521 // branch because putting code before or after it would be VERY BAD!
522 //
523 // If the annul bit of the branch is set, neither of these is legal!
524 // If so, we need to handle spill differently but annulling is not yet used.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000525 for (MachineBasicBlock::iterator MII = MBB.begin();
526 MII != MBB.end(); ++MII)
527 if (unsigned delaySlots =
Brian Gaekeaf843702003-10-22 20:22:53 +0000528 TM.getInstrInfo().getNumDelaySlots((*MII)->getOpCode())) {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000529 MachineInstr *MInst = *MII, *DelaySlotMI = *(MII+1);
530
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000531 // Check the 2 conditions above:
532 // (1) Does a branch need instructions added after it?
533 // (2) O/w does delay slot instr. need instrns before or after?
Vikram S. Adve814030a2003-07-29 19:49:21 +0000534 bool isBranch = (TM.getInstrInfo().isBranch(MInst->getOpCode()) ||
535 TM.getInstrInfo().isReturn(MInst->getOpCode()));
536 bool cond1 = (isBranch &&
537 AddedInstrMap.count(MInst) &&
538 AddedInstrMap[MInst].InstrnsAfter.size() > 0);
539 bool cond2 = (AddedInstrMap.count(DelaySlotMI) &&
540 (AddedInstrMap[DelaySlotMI].InstrnsBefore.size() > 0 ||
541 AddedInstrMap[DelaySlotMI].InstrnsAfter.size() > 0));
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000542
Brian Gaekeaf843702003-10-22 20:22:53 +0000543 if (cond1 || cond2) {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000544 assert((MInst->getOpCodeFlags() & AnnulFlag) == 0 &&
545 "FIXME: Moving an annulled delay slot instruction!");
546 assert(delaySlots==1 &&
547 "InsertBefore does not yet handle >1 delay slots!");
548 InsertBefore(DelaySlotMI, MBB, MII); // MII pts back to branch
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000549
550 // In case (1), delete it and don't replace with anything!
551 // Otherwise (i.e., case (2) only) replace it with a NOP.
552 if (cond1) {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000553 DeleteInstruction(MBB, ++MII); // MII now points to next inst.
554 --MII; // reset MII for ++MII of loop
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000555 }
Vikram S. Adve814030a2003-07-29 19:49:21 +0000556 else
557 SubstituteInPlace(BuildMI(TM.getInstrInfo().getNOPOpCode(),1),
558 MBB, MII+1); // replace with NOP
559
560 if (DEBUG_RA) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000561 std::cerr << "\nRegAlloc: Moved instr. with added code: "
Vikram S. Adve814030a2003-07-29 19:49:21 +0000562 << *DelaySlotMI
563 << " out of delay slots of instr: " << *MInst;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000564 }
565 }
Vikram S. Adve814030a2003-07-29 19:49:21 +0000566 else
567 // For non-branch instr with delay slots (probably a call), move
568 // InstrAfter to the instr. in the last delay slot.
569 move2DelayedInstr(*MII, *(MII+delaySlots));
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000570 }
571
572 // Finally iterate over all instructions in BB and insert before/after
Vikram S. Advebc001b22003-07-25 21:06:09 +0000573 for (MachineBasicBlock::iterator MII=MBB.begin(); MII != MBB.end(); ++MII) {
Vikram S. Adve48762092002-04-25 04:34:15 +0000574 MachineInstr *MInst = *MII;
Vikram S. Advebc001b22003-07-25 21:06:09 +0000575
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000576 // do not process Phis
Vikram S. Advebc001b22003-07-25 21:06:09 +0000577 if (TM.getInstrInfo().isDummyPhiInstr(MInst->getOpCode()))
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000578 continue;
579
Vikram S. Advebc001b22003-07-25 21:06:09 +0000580 // if there are any added instructions...
Chris Lattner7e708292002-06-25 16:13:24 +0000581 if (AddedInstrMap.count(MInst)) {
Vikram S. Advebc001b22003-07-25 21:06:09 +0000582 AddedInstrns &CallAI = AddedInstrMap[MInst];
583
584#ifndef NDEBUG
Vikram S. Adve814030a2003-07-29 19:49:21 +0000585 bool isBranch = (TM.getInstrInfo().isBranch(MInst->getOpCode()) ||
586 TM.getInstrInfo().isReturn(MInst->getOpCode()));
587 assert((!isBranch ||
588 AddedInstrMap[MInst].InstrnsAfter.size() <=
589 TM.getInstrInfo().getNumDelaySlots(MInst->getOpCode())) &&
590 "Cannot put more than #delaySlots instrns after "
591 "branch or return! Need to handle temps differently.");
592#endif
593
594#ifndef NDEBUG
Vikram S. Advebc001b22003-07-25 21:06:09 +0000595 // Temporary sanity checking code to detect whether the same machine
596 // instruction is ever inserted twice before/after a call.
597 // I suspect this is happening but am not sure. --Vikram, 7/1/03.
Vikram S. Advebc001b22003-07-25 21:06:09 +0000598 std::set<const MachineInstr*> instrsSeen;
599 for (int i = 0, N = CallAI.InstrnsBefore.size(); i < N; ++i) {
600 assert(instrsSeen.count(CallAI.InstrnsBefore[i]) == 0 &&
601 "Duplicate machine instruction in InstrnsBefore!");
602 instrsSeen.insert(CallAI.InstrnsBefore[i]);
603 }
604 for (int i = 0, N = CallAI.InstrnsAfter.size(); i < N; ++i) {
605 assert(instrsSeen.count(CallAI.InstrnsAfter[i]) == 0 &&
606 "Duplicate machine instruction in InstrnsBefore/After!");
607 instrsSeen.insert(CallAI.InstrnsAfter[i]);
608 }
609#endif
610
611 // Now add the instructions before/after this MI.
612 // We do this here to ensure that spill for an instruction is inserted
613 // as close as possible to an instruction (see above insertCode4Spill)
Vikram S. Advebc001b22003-07-25 21:06:09 +0000614 if (! CallAI.InstrnsBefore.empty())
615 PrependInstructions(CallAI.InstrnsBefore, MBB, MII,"");
616
617 if (! CallAI.InstrnsAfter.empty())
618 AppendInstructions(CallAI.InstrnsAfter, MBB, MII,"");
619
620 } // if there are any added instructions
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000621 } // for each machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000622 }
623}
624
625
Brian Gaekeaf843702003-10-22 20:22:53 +0000626/// Insert spill code for AN operand whose LR was spilled. May be called
627/// repeatedly for a single MachineInstr if it has many spilled operands. On
628/// each call, it finds a register which is not live at that instruction and
629/// also which is not used by other spilled operands of the same
630/// instruction. Then it uses this register temporarily to accommodate the
631/// spilled value.
632///
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000633void PhyRegAlloc::insertCode4SpilledLR(const LiveRange *LR,
Vikram S. Adve814030a2003-07-29 19:49:21 +0000634 MachineBasicBlock::iterator& MII,
635 MachineBasicBlock &MBB,
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000636 const unsigned OpNum) {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000637 MachineInstr *MInst = *MII;
638 const BasicBlock *BB = MBB.getBasicBlock();
639
Vikram S. Advead9c9782002-09-28 17:02:40 +0000640 assert((! TM.getInstrInfo().isCall(MInst->getOpCode()) || OpNum == 0) &&
641 "Outgoing arg of a call must be handled elsewhere (func arg ok)");
642 assert(! TM.getInstrInfo().isReturn(MInst->getOpCode()) &&
643 "Return value of a ret must be handled elsewhere");
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000644
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000645 MachineOperand& Op = MInst->getOperand(OpNum);
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000646 bool isDef = Op.opIsDefOnly();
647 bool isDefAndUse = Op.opIsDefAndUse();
Vikram S. Advebc001b22003-07-25 21:06:09 +0000648 unsigned RegType = MRI.getRegTypeForLR(LR);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000649 int SpillOff = LR->getSpillOffFromFP();
650 RegClass *RC = LR->getRegClass();
Vikram S. Adve814030a2003-07-29 19:49:21 +0000651
652 // Get the live-variable set to find registers free before this instr.
Vikram S. Advefeb32982003-08-12 22:22:24 +0000653 const ValueSet &LVSetBef = LVI->getLiveVarSetBeforeMInst(MInst, BB);
654
655#ifndef NDEBUG
656 // If this instr. is in the delay slot of a branch or return, we need to
657 // include all live variables before that branch or return -- we don't want to
658 // trample those! Verify that the set is included in the LV set before MInst.
Vikram S. Adve814030a2003-07-29 19:49:21 +0000659 if (MII != MBB.begin()) {
660 MachineInstr *PredMI = *(MII-1);
Vikram S. Advefeb32982003-08-12 22:22:24 +0000661 if (unsigned DS = TM.getInstrInfo().getNumDelaySlots(PredMI->getOpCode()))
662 assert(set_difference(LVI->getLiveVarSetBeforeMInst(PredMI), LVSetBef)
663 .empty() && "Live-var set before branch should be included in "
664 "live-var set of each delay slot instruction!");
Vikram S. Adve814030a2003-07-29 19:49:21 +0000665 }
Vikram S. Advefeb32982003-08-12 22:22:24 +0000666#endif
Vikram S. Adve00521d72001-11-12 23:26:35 +0000667
Brian Gaekeaf843702003-10-22 20:22:53 +0000668 MF->getInfo()->pushTempValue(MRI.getSpilledRegSize(RegType));
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000669
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000670 std::vector<MachineInstr*> MIBef, MIAft;
671 std::vector<MachineInstr*> AdIMid;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000672
Vikram S. Adve3bf08922003-07-10 19:42:55 +0000673 // Choose a register to hold the spilled value, if one was not preallocated.
674 // This may insert code before and after MInst to free up the value. If so,
675 // this code should be first/last in the spill sequence before/after MInst.
676 int TmpRegU=(LR->hasColor()
Brian Gaeke59b1c562003-09-24 17:50:28 +0000677 ? MRI.getUnifiedRegNum(LR->getRegClassID(),LR->getColor())
Vikram S. Adve3bf08922003-07-10 19:42:55 +0000678 : getUsableUniRegAtMI(RegType, &LVSetBef, MInst, MIBef,MIAft));
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000679
Vikram S. Advef5af6362002-07-08 23:15:32 +0000680 // Set the operand first so that it this register does not get used
681 // as a scratch register for later calls to getUsableUniRegAtMI below
682 MInst->SetRegForOperand(OpNum, TmpRegU);
683
684 // get the added instructions for this instruction
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000685 AddedInstrns &AI = AddedInstrMap[MInst];
Vikram S. Advef5af6362002-07-08 23:15:32 +0000686
687 // We may need a scratch register to copy the spilled value to/from memory.
688 // This may itself have to insert code to free up a scratch register.
689 // Any such code should go before (after) the spill code for a load (store).
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000690 // The scratch reg is not marked as used because it is only used
691 // for the copy and not used across MInst.
Vikram S. Advef5af6362002-07-08 23:15:32 +0000692 int scratchRegType = -1;
693 int scratchReg = -1;
Brian Gaekeaf843702003-10-22 20:22:53 +0000694 if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType)) {
Chris Lattner27a08932002-10-22 23:16:21 +0000695 scratchReg = getUsableUniRegAtMI(scratchRegType, &LVSetBef,
696 MInst, MIBef, MIAft);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000697 assert(scratchReg != MRI.getInvalidRegNum());
Vikram S. Advef5af6362002-07-08 23:15:32 +0000698 }
699
700 if (!isDef || isDefAndUse) {
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000701 // for a USE, we have to load the value of LR from stack to a TmpReg
702 // and use the TmpReg as one operand of instruction
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000703
Vikram S. Advef5af6362002-07-08 23:15:32 +0000704 // actual loading instruction(s)
Vikram S. Adve814030a2003-07-29 19:49:21 +0000705 MRI.cpMem2RegMI(AdIMid, MRI.getFramePointer(), SpillOff, TmpRegU,
706 RegType, scratchReg);
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000707
Vikram S. Advef5af6362002-07-08 23:15:32 +0000708 // the actual load should be after the instructions to free up TmpRegU
709 MIBef.insert(MIBef.end(), AdIMid.begin(), AdIMid.end());
710 AdIMid.clear();
711 }
712
Vikram S. Adve3bf08922003-07-10 19:42:55 +0000713 if (isDef || isDefAndUse) { // if this is a Def
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000714 // for a DEF, we have to store the value produced by this instruction
715 // on the stack position allocated for this LR
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000716
Vikram S. Advef5af6362002-07-08 23:15:32 +0000717 // actual storing instruction(s)
Vikram S. Adve814030a2003-07-29 19:49:21 +0000718 MRI.cpReg2MemMI(AdIMid, TmpRegU, MRI.getFramePointer(), SpillOff,
719 RegType, scratchReg);
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000720
Vikram S. Advef5af6362002-07-08 23:15:32 +0000721 MIAft.insert(MIAft.begin(), AdIMid.begin(), AdIMid.end());
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000722 } // if !DEF
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000723
Vikram S. Advef5af6362002-07-08 23:15:32 +0000724 // Finally, insert the entire spill code sequences before/after MInst
725 AI.InstrnsBefore.insert(AI.InstrnsBefore.end(), MIBef.begin(), MIBef.end());
726 AI.InstrnsAfter.insert(AI.InstrnsAfter.begin(), MIAft.begin(), MIAft.end());
727
Chris Lattner7e708292002-06-25 16:13:24 +0000728 if (DEBUG_RA) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000729 std::cerr << "\nFor Inst:\n " << *MInst;
730 std::cerr << "SPILLED LR# " << LR->getUserIGNode()->getIndex();
731 std::cerr << "; added Instructions:";
Anand Shuklad58290e2002-07-09 19:18:56 +0000732 for_each(MIBef.begin(), MIBef.end(), std::mem_fun(&MachineInstr::dump));
733 for_each(MIAft.begin(), MIAft.end(), std::mem_fun(&MachineInstr::dump));
Chris Lattner7e708292002-06-25 16:13:24 +0000734 }
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000735}
736
737
Brian Gaekeaf843702003-10-22 20:22:53 +0000738/// Insert caller saving/restoring instructions before/after a call machine
739/// instruction (before or after any other instructions that were inserted for
740/// the call).
741///
Vikram S. Adve814030a2003-07-29 19:49:21 +0000742void
743PhyRegAlloc::insertCallerSavingCode(std::vector<MachineInstr*> &instrnsBefore,
744 std::vector<MachineInstr*> &instrnsAfter,
745 MachineInstr *CallMI,
Brian Gaekeaf843702003-10-22 20:22:53 +0000746 const BasicBlock *BB) {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000747 assert(TM.getInstrInfo().isCall(CallMI->getOpCode()));
748
Brian Gaeke43ce8fe2003-09-21 02:24:09 +0000749 // hash set to record which registers were saved/restored
Vikram S. Adve814030a2003-07-29 19:49:21 +0000750 hash_set<unsigned> PushedRegSet;
751
752 CallArgsDescriptor* argDesc = CallArgsDescriptor::get(CallMI);
753
754 // if the call is to a instrumentation function, do not insert save and
755 // restore instructions the instrumentation function takes care of save
756 // restore for volatile regs.
757 //
758 // FIXME: this should be made general, not specific to the reoptimizer!
Vikram S. Adve814030a2003-07-29 19:49:21 +0000759 const Function *Callee = argDesc->getCallInst()->getCalledFunction();
760 bool isLLVMFirstTrigger = Callee && Callee->getName() == "llvm_first_trigger";
761
762 // Now check if the call has a return value (using argDesc) and if so,
763 // find the LR of the TmpInstruction representing the return value register.
764 // (using the last or second-last *implicit operand* of the call MI).
765 // Insert it to to the PushedRegSet since we must not save that register
766 // and restore it after the call.
767 // We do this because, we look at the LV set *after* the instruction
768 // to determine, which LRs must be saved across calls. The return value
769 // of the call is live in this set - but we must not save/restore it.
Vikram S. Adve814030a2003-07-29 19:49:21 +0000770 if (const Value *origRetVal = argDesc->getReturnValue()) {
771 unsigned retValRefNum = (CallMI->getNumImplicitRefs() -
772 (argDesc->getIndirectFuncPtr()? 1 : 2));
773 const TmpInstruction* tmpRetVal =
774 cast<TmpInstruction>(CallMI->getImplicitRef(retValRefNum));
775 assert(tmpRetVal->getOperand(0) == origRetVal &&
776 tmpRetVal->getType() == origRetVal->getType() &&
777 "Wrong implicit ref?");
Brian Gaeke4efe3422003-09-21 01:23:46 +0000778 LiveRange *RetValLR = LRI->getLiveRangeForValue(tmpRetVal);
Vikram S. Adve814030a2003-07-29 19:49:21 +0000779 assert(RetValLR && "No LR for RetValue of call");
780
781 if (! RetValLR->isMarkedForSpill())
782 PushedRegSet.insert(MRI.getUnifiedRegNum(RetValLR->getRegClassID(),
783 RetValLR->getColor()));
784 }
785
786 const ValueSet &LVSetAft = LVI->getLiveVarSetAfterMInst(CallMI, BB);
787 ValueSet::const_iterator LIt = LVSetAft.begin();
788
789 // for each live var in live variable set after machine inst
790 for( ; LIt != LVSetAft.end(); ++LIt) {
Brian Gaeke43ce8fe2003-09-21 02:24:09 +0000791 // get the live range corresponding to live var
Brian Gaeke4efe3422003-09-21 01:23:46 +0000792 LiveRange *const LR = LRI->getLiveRangeForValue(*LIt);
Vikram S. Adve814030a2003-07-29 19:49:21 +0000793
794 // LR can be null if it is a const since a const
795 // doesn't have a dominating def - see Assumptions above
Brian Gaekeaf843702003-10-22 20:22:53 +0000796 if (LR) {
797 if (! LR->isMarkedForSpill()) {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000798 assert(LR->hasColor() && "LR is neither spilled nor colored?");
799 unsigned RCID = LR->getRegClassID();
800 unsigned Color = LR->getColor();
801
802 if (MRI.isRegVolatile(RCID, Color) ) {
Brian Gaeke43ce8fe2003-09-21 02:24:09 +0000803 // if this is a call to the first-level reoptimizer
804 // instrumentation entry point, and the register is not
805 // modified by call, don't save and restore it.
Vikram S. Adve814030a2003-07-29 19:49:21 +0000806 if (isLLVMFirstTrigger && !MRI.modifiedByCall(RCID, Color))
807 continue;
808
809 // if the value is in both LV sets (i.e., live before and after
810 // the call machine instruction)
Vikram S. Adve814030a2003-07-29 19:49:21 +0000811 unsigned Reg = MRI.getUnifiedRegNum(RCID, Color);
812
Brian Gaeke43ce8fe2003-09-21 02:24:09 +0000813 // if we haven't already pushed this register...
Vikram S. Adve814030a2003-07-29 19:49:21 +0000814 if( PushedRegSet.find(Reg) == PushedRegSet.end() ) {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000815 unsigned RegType = MRI.getRegTypeForLR(LR);
816
817 // Now get two instructions - to push on stack and pop from stack
818 // and add them to InstrnsBefore and InstrnsAfter of the
819 // call instruction
Vikram S. Adve814030a2003-07-29 19:49:21 +0000820 int StackOff =
Brian Gaeke4efe3422003-09-21 01:23:46 +0000821 MF->getInfo()->pushTempValue(MRI.getSpilledRegSize(RegType));
Vikram S. Adve814030a2003-07-29 19:49:21 +0000822
823 //---- Insert code for pushing the reg on stack ----------
824
825 std::vector<MachineInstr*> AdIBef, AdIAft;
826
827 // We may need a scratch register to copy the saved value
828 // to/from memory. This may itself have to insert code to
829 // free up a scratch register. Any such code should go before
830 // the save code. The scratch register, if any, is by default
831 // temporary and not "used" by the instruction unless the
832 // copy code itself decides to keep the value in the scratch reg.
833 int scratchRegType = -1;
834 int scratchReg = -1;
835 if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType))
836 { // Find a register not live in the LVSet before CallMI
837 const ValueSet &LVSetBef =
838 LVI->getLiveVarSetBeforeMInst(CallMI, BB);
839 scratchReg = getUsableUniRegAtMI(scratchRegType, &LVSetBef,
840 CallMI, AdIBef, AdIAft);
841 assert(scratchReg != MRI.getInvalidRegNum());
842 }
843
844 if (AdIBef.size() > 0)
845 instrnsBefore.insert(instrnsBefore.end(),
846 AdIBef.begin(), AdIBef.end());
847
848 MRI.cpReg2MemMI(instrnsBefore, Reg, MRI.getFramePointer(),
849 StackOff, RegType, scratchReg);
850
851 if (AdIAft.size() > 0)
852 instrnsBefore.insert(instrnsBefore.end(),
853 AdIAft.begin(), AdIAft.end());
854
855 //---- Insert code for popping the reg from the stack ----------
Vikram S. Adve814030a2003-07-29 19:49:21 +0000856 AdIBef.clear();
857 AdIAft.clear();
858
859 // We may need a scratch register to copy the saved value
860 // from memory. This may itself have to insert code to
861 // free up a scratch register. Any such code should go
862 // after the save code. As above, scratch is not marked "used".
Vikram S. Adve814030a2003-07-29 19:49:21 +0000863 scratchRegType = -1;
864 scratchReg = -1;
865 if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType))
866 { // Find a register not live in the LVSet after CallMI
867 scratchReg = getUsableUniRegAtMI(scratchRegType, &LVSetAft,
868 CallMI, AdIBef, AdIAft);
869 assert(scratchReg != MRI.getInvalidRegNum());
870 }
871
872 if (AdIBef.size() > 0)
873 instrnsAfter.insert(instrnsAfter.end(),
874 AdIBef.begin(), AdIBef.end());
875
876 MRI.cpMem2RegMI(instrnsAfter, MRI.getFramePointer(), StackOff,
877 Reg, RegType, scratchReg);
878
879 if (AdIAft.size() > 0)
880 instrnsAfter.insert(instrnsAfter.end(),
881 AdIAft.begin(), AdIAft.end());
882
883 PushedRegSet.insert(Reg);
884
885 if(DEBUG_RA) {
886 std::cerr << "\nFor call inst:" << *CallMI;
887 std::cerr << " -inserted caller saving instrs: Before:\n\t ";
888 for_each(instrnsBefore.begin(), instrnsBefore.end(),
889 std::mem_fun(&MachineInstr::dump));
890 std::cerr << " -and After:\n\t ";
891 for_each(instrnsAfter.begin(), instrnsAfter.end(),
892 std::mem_fun(&MachineInstr::dump));
893 }
894 } // if not already pushed
Vikram S. Adve814030a2003-07-29 19:49:21 +0000895 } // if LR has a volatile color
Vikram S. Adve814030a2003-07-29 19:49:21 +0000896 } // if LR has color
Vikram S. Adve814030a2003-07-29 19:49:21 +0000897 } // if there is a LR for Var
Vikram S. Adve814030a2003-07-29 19:49:21 +0000898 } // for each value in the LV set after instruction
899}
900
901
Brian Gaekeaf843702003-10-22 20:22:53 +0000902/// Returns the unified register number of a temporary register to be used
903/// BEFORE MInst. If no register is available, it will pick one and modify
904/// MIBef and MIAft to contain instructions used to free up this returned
905/// register.
906///
Vikram S. Advef5af6362002-07-08 23:15:32 +0000907int PhyRegAlloc::getUsableUniRegAtMI(const int RegType,
908 const ValueSet *LVSetBef,
909 MachineInstr *MInst,
910 std::vector<MachineInstr*>& MIBef,
911 std::vector<MachineInstr*>& MIAft) {
Chris Lattner133f0792002-10-28 04:45:29 +0000912 RegClass* RC = getRegClassByID(MRI.getRegClassIDOfRegType(RegType));
Vikram S. Advef5af6362002-07-08 23:15:32 +0000913
Brian Gaekeaf843702003-10-22 20:22:53 +0000914 int RegU = getUnusedUniRegAtMI(RC, RegType, MInst, LVSetBef);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000915
916 if (RegU == -1) {
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000917 // we couldn't find an unused register. Generate code to free up a reg by
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000918 // saving it on stack and restoring after the instruction
Vikram S. Advef5af6362002-07-08 23:15:32 +0000919
Brian Gaeke4efe3422003-09-21 01:23:46 +0000920 int TmpOff = MF->getInfo()->pushTempValue(MRI.getSpilledRegSize(RegType));
Vikram S. Adve12af1642001-11-08 04:48:50 +0000921
Vikram S. Advebc001b22003-07-25 21:06:09 +0000922 RegU = getUniRegNotUsedByThisInst(RC, RegType, MInst);
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000923
Vikram S. Advef5af6362002-07-08 23:15:32 +0000924 // Check if we need a scratch register to copy this register to memory.
925 int scratchRegType = -1;
Brian Gaekeaf843702003-10-22 20:22:53 +0000926 if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType)) {
Chris Lattner133f0792002-10-28 04:45:29 +0000927 int scratchReg = getUsableUniRegAtMI(scratchRegType, LVSetBef,
928 MInst, MIBef, MIAft);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000929 assert(scratchReg != MRI.getInvalidRegNum());
930
931 // We may as well hold the value in the scratch register instead
932 // of copying it to memory and back. But we have to mark the
933 // register as used by this instruction, so it does not get used
934 // as a scratch reg. by another operand or anyone else.
Chris Lattner3fd1f5b2003-08-05 22:11:13 +0000935 ScratchRegsUsed.insert(std::make_pair(MInst, scratchReg));
Vikram S. Advef5af6362002-07-08 23:15:32 +0000936 MRI.cpReg2RegMI(MIBef, RegU, scratchReg, RegType);
937 MRI.cpReg2RegMI(MIAft, scratchReg, RegU, RegType);
Brian Gaekeaf843702003-10-22 20:22:53 +0000938 } else { // the register can be copied directly to/from memory so do it.
Vikram S. Advef5af6362002-07-08 23:15:32 +0000939 MRI.cpReg2MemMI(MIBef, RegU, MRI.getFramePointer(), TmpOff, RegType);
940 MRI.cpMem2RegMI(MIAft, MRI.getFramePointer(), TmpOff, RegU, RegType);
Brian Gaekeaf843702003-10-22 20:22:53 +0000941 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000942 }
Vikram S. Advef5af6362002-07-08 23:15:32 +0000943
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000944 return RegU;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000945}
946
Vikram S. Adve814030a2003-07-29 19:49:21 +0000947
Brian Gaekeaf843702003-10-22 20:22:53 +0000948/// Returns the register-class register number of a new unused register that
949/// can be used to accommodate a temporary value. May be called repeatedly
950/// for a single MachineInstr. On each call, it finds a register which is not
951/// live at that instruction and which is not used by any spilled operands of
952/// that instruction.
953///
954int PhyRegAlloc::getUnusedUniRegAtMI(RegClass *RC, const int RegType,
Vikram S. Adve814030a2003-07-29 19:49:21 +0000955 const MachineInstr *MInst,
956 const ValueSet* LVSetBef) {
Vikram S. Advebc001b22003-07-25 21:06:09 +0000957 RC->clearColorsUsed(); // Reset array
Vikram S. Adve814030a2003-07-29 19:49:21 +0000958
959 if (LVSetBef == NULL) {
960 LVSetBef = &LVI->getLiveVarSetBeforeMInst(MInst);
961 assert(LVSetBef != NULL && "Unable to get live-var set before MInst?");
962 }
963
Chris Lattner296b7732002-02-05 02:52:05 +0000964 ValueSet::const_iterator LIt = LVSetBef->begin();
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000965
966 // for each live var in live variable set after machine inst
Chris Lattner7e708292002-06-25 16:13:24 +0000967 for ( ; LIt != LVSetBef->end(); ++LIt) {
Brian Gaeke43ce8fe2003-09-21 02:24:09 +0000968 // Get the live range corresponding to live var, and its RegClass
Brian Gaeke4efe3422003-09-21 01:23:46 +0000969 LiveRange *const LRofLV = LRI->getLiveRangeForValue(*LIt );
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000970
971 // LR can be null if it is a const since a const
972 // doesn't have a dominating def - see Assumptions above
Vikram S. Advebc001b22003-07-25 21:06:09 +0000973 if (LRofLV && LRofLV->getRegClass() == RC && LRofLV->hasColor())
974 RC->markColorsUsed(LRofLV->getColor(),
975 MRI.getRegTypeForLR(LRofLV), RegType);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000976 }
977
978 // It is possible that one operand of this MInst was already spilled
979 // and it received some register temporarily. If that's the case,
980 // it is recorded in machine operand. We must skip such registers.
Vikram S. Advebc001b22003-07-25 21:06:09 +0000981 setRelRegsUsedByThisInst(RC, RegType, MInst);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000982
Vikram S. Advebc001b22003-07-25 21:06:09 +0000983 int unusedReg = RC->getUnusedColor(RegType); // find first unused color
984 if (unusedReg >= 0)
985 return MRI.getUnifiedRegNum(RC->getID(), unusedReg);
986
Chris Lattner85c54652002-05-23 15:50:03 +0000987 return -1;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000988}
989
990
Brian Gaekeaf843702003-10-22 20:22:53 +0000991/// Return the unified register number of a register in class RC which is not
992/// used by any operands of MInst.
993///
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000994int PhyRegAlloc::getUniRegNotUsedByThisInst(RegClass *RC,
Vikram S. Advebc001b22003-07-25 21:06:09 +0000995 const int RegType,
Chris Lattner85c54652002-05-23 15:50:03 +0000996 const MachineInstr *MInst) {
Vikram S. Advebc001b22003-07-25 21:06:09 +0000997 RC->clearColorsUsed();
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000998
Vikram S. Advebc001b22003-07-25 21:06:09 +0000999 setRelRegsUsedByThisInst(RC, RegType, MInst);
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001000
Vikram S. Advebc001b22003-07-25 21:06:09 +00001001 // find the first unused color
1002 int unusedReg = RC->getUnusedColor(RegType);
1003 assert(unusedReg >= 0 &&
1004 "FATAL: No free register could be found in reg class!!");
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001005
Vikram S. Advebc001b22003-07-25 21:06:09 +00001006 return MRI.getUnifiedRegNum(RC->getID(), unusedReg);
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001007}
1008
1009
Brian Gaekeaf843702003-10-22 20:22:53 +00001010/// Modify the IsColorUsedArr of register class RC, by setting the bits
1011/// corresponding to register RegNo. This is a helper method of
1012/// setRelRegsUsedByThisInst().
1013///
Chris Lattner3bed95b2003-08-05 21:55:58 +00001014static void markRegisterUsed(int RegNo, RegClass *RC, int RegType,
1015 const TargetRegInfo &TRI) {
1016 unsigned classId = 0;
1017 int classRegNum = TRI.getClassRegNum(RegNo, classId);
1018 if (RC->getID() == classId)
1019 RC->markColorsUsed(classRegNum, RegType, RegType);
1020}
1021
1022void PhyRegAlloc::setRelRegsUsedByThisInst(RegClass *RC, int RegType,
Brian Gaekeaf843702003-10-22 20:22:53 +00001023 const MachineInstr *MI) {
Chris Lattner3bed95b2003-08-05 21:55:58 +00001024 assert(OperandsColoredMap[MI] == true &&
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001025 "Illegal to call setRelRegsUsedByThisInst() until colored operands "
1026 "are marked for an instruction.");
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001027
Brian Gaekeaf843702003-10-22 20:22:53 +00001028 // Add the registers already marked as used by the instruction. Both
1029 // explicit and implicit operands are set.
Chris Lattner3bed95b2003-08-05 21:55:58 +00001030 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
1031 if (MI->getOperand(i).hasAllocatedReg())
1032 markRegisterUsed(MI->getOperand(i).getAllocatedRegNum(), RC, RegType,MRI);
1033
1034 for (unsigned i = 0, e = MI->getNumImplicitRefs(); i != e; ++i)
1035 if (MI->getImplicitOp(i).hasAllocatedReg())
1036 markRegisterUsed(MI->getImplicitOp(i).getAllocatedRegNum(), RC,
1037 RegType,MRI);
1038
Chris Lattner3fd1f5b2003-08-05 22:11:13 +00001039 // Add all of the scratch registers that are used to save values across the
1040 // instruction (e.g., for saving state register values).
1041 std::pair<ScratchRegsUsedTy::iterator, ScratchRegsUsedTy::iterator>
1042 IR = ScratchRegsUsed.equal_range(MI);
1043 for (ScratchRegsUsedTy::iterator I = IR.first; I != IR.second; ++I)
1044 markRegisterUsed(I->second, RC, RegType, MRI);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001045
Vikram S. Advef5af6362002-07-08 23:15:32 +00001046 // If there are implicit references, mark their allocated regs as well
Chris Lattner3bed95b2003-08-05 21:55:58 +00001047 for (unsigned z=0; z < MI->getNumImplicitRefs(); z++)
Vikram S. Advef5af6362002-07-08 23:15:32 +00001048 if (const LiveRange*
Brian Gaeke4efe3422003-09-21 01:23:46 +00001049 LRofImpRef = LRI->getLiveRangeForValue(MI->getImplicitRef(z)))
Vikram S. Advef5af6362002-07-08 23:15:32 +00001050 if (LRofImpRef->hasColor())
1051 // this implicit reference is in a LR that received a color
Vikram S. Advebc001b22003-07-25 21:06:09 +00001052 RC->markColorsUsed(LRofImpRef->getColor(),
1053 MRI.getRegTypeForLR(LRofImpRef), RegType);
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001054}
1055
1056
Brian Gaekeaf843702003-10-22 20:22:53 +00001057/// If there are delay slots for an instruction, the instructions added after
1058/// it must really go after the delayed instruction(s). So, we Move the
1059/// InstrAfter of that instruction to the corresponding delayed instruction
1060/// using the following method.
1061///
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001062void PhyRegAlloc::move2DelayedInstr(const MachineInstr *OrigMI,
1063 const MachineInstr *DelayedMI)
1064{
Vikram S. Advefeb32982003-08-12 22:22:24 +00001065 // "added after" instructions of the original instr
1066 std::vector<MachineInstr *> &OrigAft = AddedInstrMap[OrigMI].InstrnsAfter;
1067
1068 if (DEBUG_RA && OrigAft.size() > 0) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +00001069 std::cerr << "\nRegAlloc: Moved InstrnsAfter for: " << *OrigMI;
1070 std::cerr << " to last delay slot instrn: " << *DelayedMI;
Vikram S. Adve814030a2003-07-29 19:49:21 +00001071 }
1072
Ruchira Sasanka251d8db2001-10-23 21:38:00 +00001073 // "added after" instructions of the delayed instr
Vikram S. Adve814030a2003-07-29 19:49:21 +00001074 std::vector<MachineInstr *> &DelayedAft=AddedInstrMap[DelayedMI].InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +00001075
1076 // go thru all the "added after instructions" of the original instruction
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001077 // and append them to the "added after instructions" of the delayed
Ruchira Sasanka251d8db2001-10-23 21:38:00 +00001078 // instructions
Chris Lattner697954c2002-01-20 22:54:45 +00001079 DelayedAft.insert(DelayedAft.end(), OrigAft.begin(), OrigAft.end());
Ruchira Sasanka251d8db2001-10-23 21:38:00 +00001080
1081 // empty the "added after instructions" of the original instruction
1082 OrigAft.clear();
Ruchira Sasanka251d8db2001-10-23 21:38:00 +00001083}
Ruchira Sasanka0931a012001-09-15 19:06:58 +00001084
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001085
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001086void PhyRegAlloc::colorIncomingArgs()
1087{
Brian Gaeke4efe3422003-09-21 01:23:46 +00001088 MRI.colorMethodArgs(Fn, *LRI, AddedInstrAtEntry.InstrnsBefore,
Vikram S. Adve814030a2003-07-29 19:49:21 +00001089 AddedInstrAtEntry.InstrnsAfter);
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001090}
1091
Ruchira Sasankae727f852001-09-18 22:43:57 +00001092
Brian Gaekeaf843702003-10-22 20:22:53 +00001093/// Determine whether the suggested color of each live range is really usable,
1094/// and then call its setSuggestedColorUsable() method to record the answer. A
1095/// suggested color is NOT usable when the suggested color is volatile AND
1096/// when there are call interferences.
1097///
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001098void PhyRegAlloc::markUnusableSugColors()
1099{
Brian Gaeke4efe3422003-09-21 01:23:46 +00001100 LiveRangeMapType::const_iterator HMI = (LRI->getLiveRangeMap())->begin();
1101 LiveRangeMapType::const_iterator HMIEnd = (LRI->getLiveRangeMap())->end();
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001102
Brian Gaeke43ce8fe2003-09-21 02:24:09 +00001103 for (; HMI != HMIEnd ; ++HMI ) {
1104 if (HMI->first) {
1105 LiveRange *L = HMI->second; // get the LiveRange
Brian Gaeke59b1c562003-09-24 17:50:28 +00001106 if (L && L->hasSuggestedColor ())
Brian Gaeke6a256cc2003-09-24 18:08:54 +00001107 L->setSuggestedColorUsable
1108 (!(MRI.isRegVolatile (L->getRegClassID (), L->getSuggestedColor ())
1109 && L->isCallInterference ()));
Brian Gaeke43ce8fe2003-09-21 02:24:09 +00001110 }
1111 } // for all LR's in hash map
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001112}
1113
1114
Brian Gaekeaf843702003-10-22 20:22:53 +00001115/// For each live range that is spilled, allocates a new spill position on the
1116/// stack, and set the stack offsets of the live range that will be spilled to
1117/// that position. This must be called just after coloring the LRs.
1118///
Chris Lattner37730942002-02-05 03:52:29 +00001119void PhyRegAlloc::allocateStackSpace4SpilledLRs() {
Chris Lattnerc083dcc2003-09-01 20:05:47 +00001120 if (DEBUG_RA) std::cerr << "\nSetting LR stack offsets for spills...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001121
Brian Gaeke4efe3422003-09-21 01:23:46 +00001122 LiveRangeMapType::const_iterator HMI = LRI->getLiveRangeMap()->begin();
1123 LiveRangeMapType::const_iterator HMIEnd = LRI->getLiveRangeMap()->end();
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001124
Chris Lattner7e708292002-06-25 16:13:24 +00001125 for ( ; HMI != HMIEnd ; ++HMI) {
Chris Lattner37730942002-02-05 03:52:29 +00001126 if (HMI->first && HMI->second) {
Vikram S. Adve3bf08922003-07-10 19:42:55 +00001127 LiveRange *L = HMI->second; // get the LiveRange
1128 if (L->isMarkedForSpill()) { // NOTE: allocating size of long Type **
Brian Gaeke4efe3422003-09-21 01:23:46 +00001129 int stackOffset = MF->getInfo()->allocateSpilledValue(Type::LongTy);
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001130 L->setSpillOffFromFP(stackOffset);
1131 if (DEBUG_RA)
Chris Lattnerc083dcc2003-09-01 20:05:47 +00001132 std::cerr << " LR# " << L->getUserIGNode()->getIndex()
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001133 << ": stack-offset = " << stackOffset << "\n";
1134 }
Chris Lattner37730942002-02-05 03:52:29 +00001135 }
1136 } // for all LR's in hash map
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001137}
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001138
Brian Gaeke874f4232003-09-21 02:50:21 +00001139
Brian Gaeke21390412003-11-10 00:05:26 +00001140void PhyRegAlloc::saveStateForValue (std::vector<AllocInfo> &state,
1141 const Value *V, unsigned Insn, int Opnd) {
1142 LiveRangeMapType::const_iterator HMI = LRI->getLiveRangeMap ()->find (V);
1143 LiveRangeMapType::const_iterator HMIEnd = LRI->getLiveRangeMap ()->end ();
1144 AllocInfo::AllocStateTy AllocState = AllocInfo::NotAllocated;
1145 int Placement = -1;
1146 if ((HMI != HMIEnd) && HMI->second) {
1147 LiveRange *L = HMI->second;
1148 assert ((L->hasColor () || L->isMarkedForSpill ())
1149 && "Live range exists but not colored or spilled");
1150 if (L->hasColor ()) {
1151 AllocState = AllocInfo::Allocated;
1152 Placement = MRI.getUnifiedRegNum (L->getRegClassID (),
1153 L->getColor ());
1154 } else if (L->isMarkedForSpill ()) {
1155 AllocState = AllocInfo::Spilled;
1156 assert (L->hasSpillOffset ()
1157 && "Live range marked for spill but has no spill offset");
1158 Placement = L->getSpillOffFromFP ();
1159 }
1160 }
1161 state.push_back (AllocInfo (Insn, Opnd, AllocState, Placement));
1162}
1163
1164
Brian Gaekeaf843702003-10-22 20:22:53 +00001165/// Save the global register allocation decisions made by the register
1166/// allocator so that they can be accessed later (sort of like "poor man's
1167/// debug info").
1168///
1169void PhyRegAlloc::saveState () {
Brian Gaeke537132b2003-10-23 20:32:55 +00001170 std::vector<AllocInfo> &state = FnAllocState[Fn];
Brian Gaeke6a256cc2003-09-24 18:08:54 +00001171 unsigned Insn = 0;
Brian Gaeke3ceac852003-10-30 21:21:33 +00001172 for (const_inst_iterator II=inst_begin (Fn), IE=inst_end (Fn); II!=IE; ++II){
Brian Gaeke21390412003-11-10 00:05:26 +00001173 saveStateForValue (state, (*II), Insn, -1);
Brian Gaeke6a256cc2003-09-24 18:08:54 +00001174 for (unsigned i = 0; i < (*II)->getNumOperands (); ++i) {
1175 const Value *V = (*II)->getOperand (i);
Brian Gaeke21390412003-11-10 00:05:26 +00001176 // Don't worry about it unless it's something whose reg. we'll need.
1177 if (!isa<Argument> (V) && !isa<Instruction> (V))
1178 continue;
1179 saveStateForValue (state, V, Insn, i);
Brian Gaeke6a256cc2003-09-24 18:08:54 +00001180 }
Brian Gaeke3ceac852003-10-30 21:21:33 +00001181 ++Insn;
1182 }
Brian Gaeke6a256cc2003-09-24 18:08:54 +00001183}
1184
Brian Gaeke537132b2003-10-23 20:32:55 +00001185
Brian Gaekeaf843702003-10-22 20:22:53 +00001186/// Check the saved state filled in by saveState(), and abort if it looks
Brian Gaeke55766e12003-11-04 22:42:41 +00001187/// wrong. Only used when debugging. FIXME: Currently it just prints out
1188/// the state, which isn't quite as useful.
Brian Gaekeaf843702003-10-22 20:22:53 +00001189///
1190void PhyRegAlloc::verifySavedState () {
Brian Gaeke3ceac852003-10-30 21:21:33 +00001191 std::vector<AllocInfo> &state = FnAllocState[Fn];
1192 unsigned Insn = 0;
1193 for (const_inst_iterator II=inst_begin (Fn), IE=inst_end (Fn); II!=IE; ++II) {
1194 const Instruction *I = *II;
1195 MachineCodeForInstruction &Instrs = MachineCodeForInstruction::get (I);
1196 std::cerr << "Instruction:\n" << " " << *I << "\n"
1197 << "MachineCodeForInstruction:\n";
1198 for (unsigned i = 0, n = Instrs.size (); i != n; ++i)
1199 std::cerr << " " << *Instrs[i] << "\n";
1200 std::cerr << "FnAllocState:\n";
1201 for (unsigned i = 0; i < state.size (); ++i) {
1202 AllocInfo &S = state[i];
1203 if (Insn == S.Instruction) {
1204 std::cerr << " (Instruction " << S.Instruction
1205 << ", Operand " << S.Operand
1206 << ", AllocState " << S.allocStateToString ()
1207 << ", Placement " << S.Placement << ")\n";
1208 }
1209 }
1210 std::cerr << "----------\n";
1211 ++Insn;
1212 }
Brian Gaekeaf843702003-10-22 20:22:53 +00001213}
Brian Gaeke6a256cc2003-09-24 18:08:54 +00001214
Brian Gaekecce4e7a2003-11-04 18:25:56 +00001215
Brian Gaeke537132b2003-10-23 20:32:55 +00001216/// Finish the job of saveState(), by collapsing FnAllocState into an LLVM
1217/// Constant and stuffing it inside the Module. (NOTE: Soon, there will be
1218/// other, better ways of storing the saved state; this one is cumbersome and
Brian Gaeke21390412003-11-10 00:05:26 +00001219/// does not work well with the JIT.)
Brian Gaeke537132b2003-10-23 20:32:55 +00001220///
Brian Gaeke6a256cc2003-09-24 18:08:54 +00001221bool PhyRegAlloc::doFinalization (Module &M) {
1222 if (!SaveRegAllocState)
1223 return false; // Nothing to do here, unless we're saving state.
1224
Brian Gaekecce4e7a2003-11-04 18:25:56 +00001225 // If saving state into the module, just copy new elements to the
1226 // correct global.
Brian Gaeke8fc49342003-10-24 21:21:58 +00001227 if (!SaveStateToModule) {
1228 ExportedFnAllocState = FnAllocState;
Brian Gaekecce4e7a2003-11-04 18:25:56 +00001229 // FIXME: should ONLY copy new elements in FnAllocState
Brian Gaeke8fc49342003-10-24 21:21:58 +00001230 return false;
1231 }
1232
Brian Gaeke6a256cc2003-09-24 18:08:54 +00001233 // Convert FnAllocState to a single Constant array and add it
1234 // to the Module.
1235 ArrayType *AT = ArrayType::get (AllocInfo::getConstantType (), 0);
1236 std::vector<const Type *> TV;
1237 TV.push_back (Type::UIntTy);
1238 TV.push_back (AT);
1239 PointerType *PT = PointerType::get (StructType::get (TV));
1240
1241 std::vector<Constant *> allstate;
1242 for (Module::iterator I = M.begin (), E = M.end (); I != E; ++I) {
1243 Function *F = I;
Brian Gaeke55766e12003-11-04 22:42:41 +00001244 if (F->isExternal ()) continue;
Brian Gaeke6a256cc2003-09-24 18:08:54 +00001245 if (FnAllocState.find (F) == FnAllocState.end ()) {
1246 allstate.push_back (ConstantPointerNull::get (PT));
1247 } else {
Brian Gaeke537132b2003-10-23 20:32:55 +00001248 std::vector<AllocInfo> &state = FnAllocState[F];
Brian Gaeke60a3c552003-10-22 20:44:23 +00001249
1250 // Convert state into an LLVM ConstantArray, and put it in a
1251 // ConstantStruct (named S) along with its size.
Brian Gaeke537132b2003-10-23 20:32:55 +00001252 std::vector<Constant *> stateConstants;
1253 for (unsigned i = 0, s = state.size (); i != s; ++i)
1254 stateConstants.push_back (state[i].toConstant ());
1255 unsigned Size = stateConstants.size ();
Brian Gaeke60a3c552003-10-22 20:44:23 +00001256 ArrayType *AT = ArrayType::get (AllocInfo::getConstantType (), Size);
1257 std::vector<const Type *> TV;
1258 TV.push_back (Type::UIntTy);
1259 TV.push_back (AT);
1260 StructType *ST = StructType::get (TV);
1261 std::vector<Constant *> CV;
1262 CV.push_back (ConstantUInt::get (Type::UIntTy, Size));
Brian Gaeke537132b2003-10-23 20:32:55 +00001263 CV.push_back (ConstantArray::get (AT, stateConstants));
Brian Gaeke60a3c552003-10-22 20:44:23 +00001264 Constant *S = ConstantStruct::get (ST, CV);
1265
Brian Gaeke6a256cc2003-09-24 18:08:54 +00001266 GlobalVariable *GV =
Brian Gaeke60a3c552003-10-22 20:44:23 +00001267 new GlobalVariable (ST, true,
1268 GlobalValue::InternalLinkage, S,
Brian Gaeke6a256cc2003-09-24 18:08:54 +00001269 F->getName () + ".regAllocState", &M);
Brian Gaeke60a3c552003-10-22 20:44:23 +00001270
Brian Gaeke21390412003-11-10 00:05:26 +00001271 // Have: { uint, [Size x { uint, int, uint, int }] } *
1272 // Cast it to: { uint, [0 x { uint, int, uint, int }] } *
Brian Gaeke6a256cc2003-09-24 18:08:54 +00001273 Constant *CE = ConstantExpr::getCast (ConstantPointerRef::get (GV), PT);
1274 allstate.push_back (CE);
1275 }
1276 }
1277
1278 unsigned Size = allstate.size ();
1279 // Final structure type is:
Brian Gaeke21390412003-11-10 00:05:26 +00001280 // { uint, [Size x { uint, [0 x { uint, int, uint, int }] } *] }
Brian Gaeke6a256cc2003-09-24 18:08:54 +00001281 std::vector<const Type *> TV2;
1282 TV2.push_back (Type::UIntTy);
1283 ArrayType *AT2 = ArrayType::get (PT, Size);
1284 TV2.push_back (AT2);
1285 StructType *ST2 = StructType::get (TV2);
1286 std::vector<Constant *> CV2;
1287 CV2.push_back (ConstantUInt::get (Type::UIntTy, Size));
1288 CV2.push_back (ConstantArray::get (AT2, allstate));
1289 new GlobalVariable (ST2, true, GlobalValue::InternalLinkage,
1290 ConstantStruct::get (ST2, CV2), "_llvm_regAllocState",
1291 &M);
1292 return false; // No error.
1293}
1294
1295
Brian Gaekeaf843702003-10-22 20:22:53 +00001296/// Allocate registers for the machine code previously generated for F using
1297/// the graph-coloring algorithm.
1298///
Brian Gaeke4efe3422003-09-21 01:23:46 +00001299bool PhyRegAlloc::runOnFunction (Function &F) {
1300 if (DEBUG_RA)
1301 std::cerr << "\n********* Function "<< F.getName () << " ***********\n";
1302
1303 Fn = &F;
1304 MF = &MachineFunction::get (Fn);
1305 LVI = &getAnalysis<FunctionLiveVarInfo> ();
1306 LRI = new LiveRangeInfo (Fn, TM, RegClassList);
1307 LoopDepthCalc = &getAnalysis<LoopInfo> ();
1308
1309 // Create each RegClass for the target machine and add it to the
1310 // RegClassList. This must be done before calling constructLiveRanges().
1311 for (unsigned rc = 0; rc != NumOfRegClasses; ++rc)
1312 RegClassList.push_back (new RegClass (Fn, &TM.getRegInfo (),
1313 MRI.getMachineRegClass (rc)));
1314
1315 LRI->constructLiveRanges(); // create LR info
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001316 if (DEBUG_RA >= RA_DEBUG_LiveRanges)
Brian Gaeke4efe3422003-09-21 01:23:46 +00001317 LRI->printLiveRanges();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001318
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001319 createIGNodeListsAndIGs(); // create IGNode list and IGs
1320
1321 buildInterferenceGraphs(); // build IGs in all reg classes
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001322
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001323 if (DEBUG_RA >= RA_DEBUG_LiveRanges) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001324 // print all LRs in all reg classes
Chris Lattner7e708292002-06-25 16:13:24 +00001325 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
1326 RegClassList[rc]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001327
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001328 // print IGs in all register classes
Chris Lattner7e708292002-06-25 16:13:24 +00001329 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
1330 RegClassList[rc]->printIG();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001331 }
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001332
Brian Gaeke4efe3422003-09-21 01:23:46 +00001333 LRI->coalesceLRs(); // coalesce all live ranges
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +00001334
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001335 if (DEBUG_RA >= RA_DEBUG_LiveRanges) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001336 // print all LRs in all reg classes
Chris Lattnerf726e772002-10-28 19:22:04 +00001337 for (unsigned rc=0; rc < NumOfRegClasses; rc++)
1338 RegClassList[rc]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001339
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001340 // print IGs in all register classes
Chris Lattnerf726e772002-10-28 19:22:04 +00001341 for (unsigned rc=0; rc < NumOfRegClasses; rc++)
1342 RegClassList[rc]->printIG();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001343 }
1344
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001345 // mark un-usable suggested color before graph coloring algorithm.
1346 // When this is done, the graph coloring algo will not reserve
1347 // suggested color unnecessarily - they can be used by another LR
1348 markUnusableSugColors();
1349
1350 // color all register classes using the graph coloring algo
Chris Lattner7e708292002-06-25 16:13:24 +00001351 for (unsigned rc=0; rc < NumOfRegClasses ; rc++)
Chris Lattnerf726e772002-10-28 19:22:04 +00001352 RegClassList[rc]->colorAllRegs();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001353
Misha Brukman37f92e22003-09-11 22:34:13 +00001354 // After graph coloring, if some LRs did not receive a color (i.e, spilled)
1355 // a position for such spilled LRs
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001356 allocateStackSpace4SpilledLRs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001357
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001358 // Reset the temp. area on the stack before use by the first instruction.
1359 // This will also happen after updating each instruction.
Brian Gaeke4efe3422003-09-21 01:23:46 +00001360 MF->getInfo()->popAllTempValues();
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001361
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001362 // color incoming args - if the correct color was not received
1363 // insert code to copy to the correct register
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001364 colorIncomingArgs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001365
Brian Gaeke6a256cc2003-09-24 18:08:54 +00001366 // Save register allocation state for this function in a Constant.
1367 if (SaveRegAllocState)
1368 saveState();
Brian Gaekeaf843702003-10-22 20:22:53 +00001369 if (DEBUG_RA) { // Check our work.
1370 verifySavedState ();
1371 }
Brian Gaeke6a256cc2003-09-24 18:08:54 +00001372
Brian Gaeke60a3c552003-10-22 20:44:23 +00001373 // Now update the machine code with register names and add any additional
1374 // code inserted by the register allocator to the instruction stream.
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001375 updateMachineCode();
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001376
Chris Lattner045e7c82001-09-19 16:26:23 +00001377 if (DEBUG_RA) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +00001378 std::cerr << "\n**** Machine Code After Register Allocation:\n\n";
Brian Gaeke4efe3422003-09-21 01:23:46 +00001379 MF->dump();
Chris Lattner045e7c82001-09-19 16:26:23 +00001380 }
Brian Gaeke4efe3422003-09-21 01:23:46 +00001381
1382 // Tear down temporary data structures
1383 for (unsigned rc = 0; rc < NumOfRegClasses; ++rc)
1384 delete RegClassList[rc];
1385 RegClassList.clear ();
1386 AddedInstrMap.clear ();
1387 OperandsColoredMap.clear ();
1388 ScratchRegsUsed.clear ();
1389 AddedInstrAtEntry.clear ();
1390 delete LRI;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001391
Brian Gaeke4efe3422003-09-21 01:23:46 +00001392 if (DEBUG_RA) std::cerr << "\nRegister allocation complete!\n";
1393 return false; // Function was not modified
1394}