blob: 2fd8e558fdc7d7773e19dd1a5e3e9a3d20463e23 [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"
Brian Gaeke748fba12004-02-24 19:46:00 +000028#include "../LiveVar/FunctionLiveVarInfo.h"
Chris Lattner85015a02004-08-16 21:55:02 +000029#include "../MachineCodeForInstruction.h"
30#include "../MachineFunctionInfo.h"
Brian Gaekec9989812004-07-27 17:43:24 +000031#include "../SparcV9InstrInfo.h"
Brian Gaeke98ac7ac2004-08-04 07:29:53 +000032#include "../SparcV9TmpInstr.h"
Misha Brukman396c8c32003-10-23 18:06:27 +000033#include "llvm/Constants.h"
34#include "llvm/DerivedTypes.h"
Chris Lattner9670eec2004-07-29 17:11:37 +000035#include "llvm/Instructions.h"
Misha Brukman396c8c32003-10-23 18:06:27 +000036#include "llvm/Module.h"
37#include "llvm/Type.h"
38#include "llvm/Analysis/LoopInfo.h"
Misha Brukman396c8c32003-10-23 18:06:27 +000039#include "llvm/CodeGen/MachineFunction.h"
Brian Gaeke874f4232003-09-21 02:50:21 +000040#include "llvm/CodeGen/MachineInstr.h"
Chris Lattnerf6ee49f2003-01-15 18:08:07 +000041#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner08d49632004-02-29 19:12:51 +000042#include "../MachineInstrAnnot.h"
Chris Lattner797c1362003-09-30 20:13:59 +000043#include "llvm/CodeGen/Passes.h"
Chris Lattner797c1362003-09-30 20:13:59 +000044#include "llvm/Support/InstIterator.h"
Misha Brukman396c8c32003-10-23 18:06:27 +000045#include "llvm/Target/TargetInstrInfo.h"
Chris Lattner4bc23482002-09-15 07:07:55 +000046#include "Support/CommandLine.h"
Misha Brukman396c8c32003-10-23 18:06:27 +000047#include "Support/SetOperations.h"
48#include "Support/STLExtras.h"
Brian Gaekebd353fb2003-09-21 03:57:37 +000049#include <cmath>
Reid Spencer954da372004-07-04 12:19:56 +000050#include <iostream>
Vikram S. Adve12af1642001-11-08 04:48:50 +000051
Brian Gaeked0fde302003-11-11 22:41:34 +000052namespace llvm {
53
Chris Lattner70e60cb2002-05-22 17:08:27 +000054RegAllocDebugLevel_t DEBUG_RA;
Vikram S. Adve39c94e12002-09-14 23:05:33 +000055
Chris Lattner5ff62e92002-07-22 02:10:13 +000056static cl::opt<RegAllocDebugLevel_t, true>
57DRA_opt("dregalloc", cl::Hidden, cl::location(DEBUG_RA),
58 cl::desc("enable register allocation debugging information"),
59 cl::values(
Vikram S. Adve39c94e12002-09-14 23:05:33 +000060 clEnumValN(RA_DEBUG_None , "n", "disable debug output"),
61 clEnumValN(RA_DEBUG_Results, "y", "debug output for allocation results"),
62 clEnumValN(RA_DEBUG_Coloring, "c", "debug output for graph coloring step"),
63 clEnumValN(RA_DEBUG_Interference,"ig","debug output for interference graphs"),
64 clEnumValN(RA_DEBUG_LiveRanges , "lr","debug output for live ranges"),
65 clEnumValN(RA_DEBUG_Verbose, "v", "extra debug output"),
Chris Lattner4d143ee2004-07-16 00:08:28 +000066 clEnumValEnd));
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000067
Brian Gaeked1b36792004-03-10 22:21:03 +000068/// The reoptimizer wants to be able to grovel through the register
69/// allocator's state after it has done its job. This is a hack.
70///
71PhyRegAlloc::SavedStateMapTy ExportedFnAllocState;
72bool SaveRegAllocState = false;
73bool SaveStateToModule = true;
74static cl::opt<bool, true>
75SaveRegAllocStateOpt("save-ra-state", cl::Hidden,
76 cl::location (SaveRegAllocState),
77 cl::init(false),
Brian Gaeke59b1c562003-09-24 17:50:28 +000078 cl::desc("write reg. allocator state into module"));
79
Brian Gaekebf3c4cf2003-08-14 06:09:32 +000080FunctionPass *getRegisterAllocator(TargetMachine &T) {
Brian Gaeke4efe3422003-09-21 01:23:46 +000081 return new PhyRegAlloc (T);
Chris Lattner2f9b28e2002-02-04 15:54:09 +000082}
Chris Lattner6dd98a62002-02-04 00:33:08 +000083
Chris Lattner8474f6f2003-09-23 15:13:04 +000084void PhyRegAlloc::getAnalysisUsage(AnalysisUsage &AU) const {
85 AU.addRequired<LoopInfo> ();
86 AU.addRequired<FunctionLiveVarInfo> ();
87}
88
89
Brian Gaekeaf843702003-10-22 20:22:53 +000090/// Initialize interference graphs (one in each reg class) and IGNodeLists
91/// (one in each IG). The actual nodes will be pushed later.
92///
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000093void PhyRegAlloc::createIGNodeListsAndIGs() {
Chris Lattnerc083dcc2003-09-01 20:05:47 +000094 if (DEBUG_RA >= RA_DEBUG_LiveRanges) std::cerr << "Creating LR lists ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +000095
Brian Gaeke4efe3422003-09-21 01:23:46 +000096 LiveRangeMapType::const_iterator HMI = LRI->getLiveRangeMap()->begin();
Brian Gaeke4efe3422003-09-21 01:23:46 +000097 LiveRangeMapType::const_iterator HMIEnd = LRI->getLiveRangeMap()->end();
Ruchira Sasanka8e604792001-09-14 21:18:34 +000098
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000099 for (; HMI != HMIEnd ; ++HMI ) {
100 if (HMI->first) {
101 LiveRange *L = HMI->second; // get the LiveRange
102 if (!L) {
Brian Gaekeeb8863d2004-03-29 21:58:41 +0000103 if (DEBUG_RA && !isa<ConstantIntegral> (HMI->first))
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000104 std::cerr << "\n**** ?!?WARNING: NULL LIVE RANGE FOUND FOR: "
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000105 << RAV(HMI->first) << "****\n";
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000106 continue;
107 }
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000108
109 // if the Value * is not null, and LR is not yet written to the IGNodeList
Chris Lattner7e708292002-06-25 16:13:24 +0000110 if (!(L->getUserIGNode()) ) {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000111 RegClass *const RC = // RegClass of first value in the LR
Brian Gaeke59b1c562003-09-24 17:50:28 +0000112 RegClassList[ L->getRegClassID() ];
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000113 RC->addLRToIG(L); // add this LR to an IG
114 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000115 }
116 }
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000117
118 // init RegClassList
Chris Lattner7e708292002-06-25 16:13:24 +0000119 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000120 RegClassList[rc]->createInterferenceGraph();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000121
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000122 if (DEBUG_RA >= RA_DEBUG_LiveRanges) std::cerr << "LRLists Created!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000123}
124
125
Brian Gaekeaf843702003-10-22 20:22:53 +0000126/// Add all interferences for a given instruction. Interference occurs only
127/// if the LR of Def (Inst or Arg) is of the same reg class as that of live
128/// var. The live var passed to this function is the LVset AFTER the
129/// instruction.
130///
131void PhyRegAlloc::addInterference(const Value *Def, const ValueSet *LVSet,
Chris Lattner296b7732002-02-05 02:52:05 +0000132 bool isCallInst) {
Chris Lattner296b7732002-02-05 02:52:05 +0000133 ValueSet::const_iterator LIt = LVSet->begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000134
135 // get the live range of instruction
Brian Gaeke4efe3422003-09-21 01:23:46 +0000136 const LiveRange *const LROfDef = LRI->getLiveRangeForValue( Def );
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000137
138 IGNode *const IGNodeOfDef = LROfDef->getUserIGNode();
139 assert( IGNodeOfDef );
140
141 RegClass *const RCOfDef = LROfDef->getRegClass();
142
143 // for each live var in live variable set
Chris Lattner7e708292002-06-25 16:13:24 +0000144 for ( ; LIt != LVSet->end(); ++LIt) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000145
Vikram S. Advef5af6362002-07-08 23:15:32 +0000146 if (DEBUG_RA >= RA_DEBUG_Verbose)
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000147 std::cerr << "< Def=" << RAV(Def) << ", Lvar=" << RAV(*LIt) << "> ";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000148
149 // get the live range corresponding to live var
Brian Gaeke4efe3422003-09-21 01:23:46 +0000150 LiveRange *LROfVar = LRI->getLiveRangeForValue(*LIt);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000151
152 // LROfVar can be null if it is a const since a const
153 // doesn't have a dominating def - see Assumptions above
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000154 if (LROfVar)
155 if (LROfDef != LROfVar) // do not set interf for same LR
156 if (RCOfDef == LROfVar->getRegClass()) // 2 reg classes are the same
157 RCOfDef->setInterference( LROfDef, LROfVar);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000158 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000159}
160
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000161
Brian Gaekeaf843702003-10-22 20:22:53 +0000162/// For a call instruction, this method sets the CallInterference flag in
163/// the LR of each variable live in the Live Variable Set live after the
164/// call instruction (except the return value of the call instruction - since
165/// the return value does not interfere with that call itself).
166///
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000167void PhyRegAlloc::setCallInterferences(const MachineInstr *MInst,
Chris Lattner296b7732002-02-05 02:52:05 +0000168 const ValueSet *LVSetAft) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000169 if (DEBUG_RA >= RA_DEBUG_Interference)
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000170 std::cerr << "\n For call inst: " << *MInst;
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000171
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000172 // for each live var in live variable set after machine inst
Vikram S. Adve65b2f402003-07-02 01:24:00 +0000173 for (ValueSet::const_iterator LIt = LVSetAft->begin(), LEnd = LVSetAft->end();
174 LIt != LEnd; ++LIt) {
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000175
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000176 // get the live range corresponding to live var
Brian Gaekea308f802004-07-29 06:43:09 +0000177 LiveRange *const LR = LRI->getLiveRangeForValue(*LIt);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000178
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000179 // LR can be null if it is a const since a const
180 // doesn't have a dominating def - see Assumptions above
Brian Gaekea308f802004-07-29 06:43:09 +0000181 if (LR) {
182 if (DEBUG_RA >= RA_DEBUG_Interference)
183 std::cerr << "\n\tLR after Call: " << *LR << "\n";
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000184 LR->setCallInterference();
Brian Gaekea308f802004-07-29 06:43:09 +0000185 if (DEBUG_RA >= RA_DEBUG_Interference)
186 std::cerr << "\n ++After adding call interference for LR: " << *LR << "\n";
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000187 }
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000188 }
189
Vikram S. Adve1a53f032002-03-31 18:54:37 +0000190 // Now find the LR of the return value of the call
191 // We do this because, we look at the LV set *after* the instruction
192 // to determine, which LRs must be saved across calls. The return value
193 // of the call is live in this set - but it does not interfere with call
194 // (i.e., we can allocate a volatile register to the return value)
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000195 CallArgsDescriptor* argDesc = CallArgsDescriptor::get(MInst);
196
197 if (const Value *RetVal = argDesc->getReturnValue()) {
Brian Gaeke4efe3422003-09-21 01:23:46 +0000198 LiveRange *RetValLR = LRI->getLiveRangeForValue( RetVal );
Vikram S. Adve1a53f032002-03-31 18:54:37 +0000199 assert( RetValLR && "No LR for RetValue of call");
200 RetValLR->clearCallInterference();
201 }
202
203 // If the CALL is an indirect call, find the LR of the function pointer.
204 // That has a call interference because it conflicts with outgoing args.
Chris Lattner7e708292002-06-25 16:13:24 +0000205 if (const Value *AddrVal = argDesc->getIndirectFuncPtr()) {
Brian Gaeke4efe3422003-09-21 01:23:46 +0000206 LiveRange *AddrValLR = LRI->getLiveRangeForValue( AddrVal );
Vikram S. Adve1a53f032002-03-31 18:54:37 +0000207 assert( AddrValLR && "No LR for indirect addr val of call");
208 AddrValLR->setCallInterference();
209 }
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000210}
211
212
Brian Gaekeaf843702003-10-22 20:22:53 +0000213/// Create interferences in the IG of each RegClass, and calculate the spill
214/// cost of each Live Range (it is done in this method to save another pass
215/// over the code).
216///
217void PhyRegAlloc::buildInterferenceGraphs() {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000218 if (DEBUG_RA >= RA_DEBUG_Interference)
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000219 std::cerr << "Creating interference graphs ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000220
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000221 unsigned BBLoopDepthCost;
Brian Gaeke4efe3422003-09-21 01:23:46 +0000222 for (MachineFunction::iterator BBI = MF->begin(), BBE = MF->end();
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000223 BBI != BBE; ++BBI) {
Chris Lattnerf726e772002-10-28 19:22:04 +0000224 const MachineBasicBlock &MBB = *BBI;
225 const BasicBlock *BB = MBB.getBasicBlock();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000226
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000227 // find the 10^(loop_depth) of this BB
Chris Lattnerf726e772002-10-28 19:22:04 +0000228 BBLoopDepthCost = (unsigned)pow(10.0, LoopDepthCalc->getLoopDepth(BB));
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000229
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000230 // get the iterator for machine instructions
Chris Lattnerf726e772002-10-28 19:22:04 +0000231 MachineBasicBlock::const_iterator MII = MBB.begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000232
233 // iterate over all the machine instructions in BB
Chris Lattnerf726e772002-10-28 19:22:04 +0000234 for ( ; MII != MBB.end(); ++MII) {
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000235 const MachineInstr *MInst = MII;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000236
237 // get the LV set after the instruction
Chris Lattnerf726e772002-10-28 19:22:04 +0000238 const ValueSet &LVSetAI = LVI->getLiveVarSetAfterMInst(MInst, BB);
Chris Lattnerd029cd22004-06-02 05:55:25 +0000239 bool isCallInst = TM.getInstrInfo()->isCall(MInst->getOpcode());
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000240
Brian Gaekeaf843702003-10-22 20:22:53 +0000241 if (isCallInst) {
Misha Brukman37f92e22003-09-11 22:34:13 +0000242 // set the isCallInterference flag of each live range which extends
243 // across this call instruction. This information is used by graph
244 // coloring algorithm to avoid allocating volatile colors to live ranges
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000245 // that span across calls (since they have to be saved/restored)
Chris Lattner748697d2002-02-05 04:20:12 +0000246 setCallInterferences(MInst, &LVSetAI);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000247 }
248
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000249 // iterate over all MI operands to find defs
Chris Lattner2f898d22002-02-05 06:02:59 +0000250 for (MachineInstr::const_val_op_iterator OpI = MInst->begin(),
251 OpE = MInst->end(); OpI != OpE; ++OpI) {
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000252 if (OpI.isDef()) // create a new LR since def
Chris Lattner748697d2002-02-05 04:20:12 +0000253 addInterference(*OpI, &LVSetAI, isCallInst);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000254
255 // Calculate the spill cost of each live range
Brian Gaeke4efe3422003-09-21 01:23:46 +0000256 LiveRange *LR = LRI->getLiveRangeForValue(*OpI);
Chris Lattner2f898d22002-02-05 06:02:59 +0000257 if (LR) LR->addSpillCost(BBLoopDepthCost);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000258 }
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000259 // Also add interference for any implicit definitions in a machine
260 // instr (currently, only calls have this).
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000261 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000262 for (unsigned z=0; z < NumOfImpRefs; z++)
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000263 if (MInst->getImplicitOp(z).isDef())
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000264 addInterference( MInst->getImplicitRef(z), &LVSetAI, isCallInst );
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000265 } // for all machine instructions in BB
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000266 } // for all BBs in function
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000267
Misha Brukman37f92e22003-09-11 22:34:13 +0000268 // add interferences for function arguments. Since there are no explicit
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000269 // defs in the function for args, we have to add them manually
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000270 addInterferencesForArgs();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000271
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000272 if (DEBUG_RA >= RA_DEBUG_Interference)
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000273 std::cerr << "Interference graphs calculated!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000274}
275
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000276
Brian Gaekeaf843702003-10-22 20:22:53 +0000277/// Mark all operands of the given MachineInstr as interfering with one
278/// another.
279///
Brian Gaeke43ce8fe2003-09-21 02:24:09 +0000280void PhyRegAlloc::addInterf4PseudoInstr(const MachineInstr *MInst) {
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000281 bool setInterf = false;
282
Brian Gaeke43ce8fe2003-09-21 02:24:09 +0000283 // iterate over MI operands to find defs
Chris Lattner2f898d22002-02-05 06:02:59 +0000284 for (MachineInstr::const_val_op_iterator It1 = MInst->begin(),
285 ItE = MInst->end(); It1 != ItE; ++It1) {
Brian Gaeke4efe3422003-09-21 01:23:46 +0000286 const LiveRange *LROfOp1 = LRI->getLiveRangeForValue(*It1);
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000287 assert((LROfOp1 || It1.isDef()) && "No LR for Def in PSEUDO insruction");
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000288
Chris Lattner2f898d22002-02-05 06:02:59 +0000289 MachineInstr::const_val_op_iterator It2 = It1;
Chris Lattner7e708292002-06-25 16:13:24 +0000290 for (++It2; It2 != ItE; ++It2) {
Brian Gaeke4efe3422003-09-21 01:23:46 +0000291 const LiveRange *LROfOp2 = LRI->getLiveRangeForValue(*It2);
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000292
Chris Lattner2f898d22002-02-05 06:02:59 +0000293 if (LROfOp2) {
294 RegClass *RCOfOp1 = LROfOp1->getRegClass();
295 RegClass *RCOfOp2 = LROfOp2->getRegClass();
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000296
Chris Lattner7e708292002-06-25 16:13:24 +0000297 if (RCOfOp1 == RCOfOp2 ){
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000298 RCOfOp1->setInterference( LROfOp1, LROfOp2 );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000299 setInterf = true;
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000300 }
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000301 } // if Op2 has a LR
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000302 } // for all other defs in machine instr
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000303 } // for all operands in an instruction
304
Chris Lattner2f898d22002-02-05 06:02:59 +0000305 if (!setInterf && MInst->getNumOperands() > 2) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000306 std::cerr << "\nInterf not set for any operand in pseudo instr:\n";
307 std::cerr << *MInst;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000308 assert(0 && "Interf not set for pseudo instr with > 2 operands" );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000309 }
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000310}
311
312
Brian Gaekeaf843702003-10-22 20:22:53 +0000313/// Add interferences for incoming arguments to a function.
314///
Chris Lattner296b7732002-02-05 02:52:05 +0000315void PhyRegAlloc::addInterferencesForArgs() {
316 // get the InSet of root BB
Chris Lattnerf726e772002-10-28 19:22:04 +0000317 const ValueSet &InSet = LVI->getInSetOfBB(&Fn->front());
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000318
Chris Lattnerf726e772002-10-28 19:22:04 +0000319 for (Function::const_aiterator AI = Fn->abegin(); AI != Fn->aend(); ++AI) {
Chris Lattner7e708292002-06-25 16:13:24 +0000320 // add interferences between args and LVars at start
321 addInterference(AI, &InSet, false);
322
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000323 if (DEBUG_RA >= RA_DEBUG_Interference)
Brian Gaekeaf843702003-10-22 20:22:53 +0000324 std::cerr << " - %% adding interference for argument " << RAV(AI) << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000325 }
326}
327
328
Brian Gaekeaf843702003-10-22 20:22:53 +0000329/// The following are utility functions used solely by updateMachineCode and
330/// the functions that it calls. They should probably be folded back into
331/// updateMachineCode at some point.
332///
Vikram S. Adve48762092002-04-25 04:34:15 +0000333
Brian Gaekeaf843702003-10-22 20:22:53 +0000334// used by: updateMachineCode (1 time), PrependInstructions (1 time)
335inline void InsertBefore(MachineInstr* newMI, MachineBasicBlock& MBB,
336 MachineBasicBlock::iterator& MII) {
Chris Lattnerf726e772002-10-28 19:22:04 +0000337 MII = MBB.insert(MII, newMI);
Vikram S. Advecb202e32002-10-11 16:12:40 +0000338 ++MII;
339}
340
Brian Gaekeaf843702003-10-22 20:22:53 +0000341// used by: AppendInstructions (1 time)
342inline void InsertAfter(MachineInstr* newMI, MachineBasicBlock& MBB,
343 MachineBasicBlock::iterator& MII) {
Vikram S. Advecb202e32002-10-11 16:12:40 +0000344 ++MII; // insert before the next instruction
Chris Lattnerf726e772002-10-28 19:22:04 +0000345 MII = MBB.insert(MII, newMI);
Vikram S. Advecb202e32002-10-11 16:12:40 +0000346}
347
Brian Gaekeaf843702003-10-22 20:22:53 +0000348// used by: updateMachineCode (2 times)
349inline void PrependInstructions(std::vector<MachineInstr *> &IBef,
350 MachineBasicBlock& MBB,
351 MachineBasicBlock::iterator& MII,
352 const std::string& msg) {
353 if (!IBef.empty()) {
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000354 MachineInstr* OrigMI = MII;
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000355 std::vector<MachineInstr *>::iterator AdIt;
Brian Gaekeaf843702003-10-22 20:22:53 +0000356 for (AdIt = IBef.begin(); AdIt != IBef.end() ; ++AdIt) {
Vikram S. Adve48762092002-04-25 04:34:15 +0000357 if (DEBUG_RA) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000358 if (OrigMI) std::cerr << "For MInst:\n " << *OrigMI;
359 std::cerr << msg << "PREPENDed instr:\n " << **AdIt << "\n";
Vikram S. Adve48762092002-04-25 04:34:15 +0000360 }
Chris Lattnerf726e772002-10-28 19:22:04 +0000361 InsertBefore(*AdIt, MBB, MII);
Vikram S. Adve48762092002-04-25 04:34:15 +0000362 }
363 }
364}
365
Brian Gaekeaf843702003-10-22 20:22:53 +0000366// used by: updateMachineCode (1 time)
367inline void AppendInstructions(std::vector<MachineInstr *> &IAft,
368 MachineBasicBlock& MBB,
369 MachineBasicBlock::iterator& MII,
370 const std::string& msg) {
371 if (!IAft.empty()) {
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000372 MachineInstr* OrigMI = MII;
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000373 std::vector<MachineInstr *>::iterator AdIt;
Brian Gaekeaf843702003-10-22 20:22:53 +0000374 for ( AdIt = IAft.begin(); AdIt != IAft.end() ; ++AdIt ) {
Chris Lattner7e708292002-06-25 16:13:24 +0000375 if (DEBUG_RA) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000376 if (OrigMI) std::cerr << "For MInst:\n " << *OrigMI;
377 std::cerr << msg << "APPENDed instr:\n " << **AdIt << "\n";
Vikram S. Adve48762092002-04-25 04:34:15 +0000378 }
Chris Lattnerf726e772002-10-28 19:22:04 +0000379 InsertAfter(*AdIt, MBB, MII);
Vikram S. Adve48762092002-04-25 04:34:15 +0000380 }
381 }
382}
383
Brian Gaekeaf843702003-10-22 20:22:53 +0000384/// Set the registers for operands in the given MachineInstr, if a register was
385/// successfully allocated. Return true if any of its operands has been marked
386/// for spill.
387///
Brian Gaeke4efe3422003-09-21 01:23:46 +0000388bool PhyRegAlloc::markAllocatedRegs(MachineInstr* MInst)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000389{
Vikram S. Adve814030a2003-07-29 19:49:21 +0000390 bool instrNeedsSpills = false;
391
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000392 // First, set the registers for operands in the machine instruction
393 // if a register was successfully allocated. Do this first because we
394 // will need to know which registers are already used by this instr'n.
Brian Gaekeaf843702003-10-22 20:22:53 +0000395 for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000396 MachineOperand& Op = MInst->getOperand(OpNum);
397 if (Op.getType() == MachineOperand::MO_VirtualRegister ||
Brian Gaekeaf843702003-10-22 20:22:53 +0000398 Op.getType() == MachineOperand::MO_CCRegister) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000399 const Value *const Val = Op.getVRegValue();
Brian Gaeke4efe3422003-09-21 01:23:46 +0000400 if (const LiveRange* LR = LRI->getLiveRangeForValue(Val)) {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000401 // Remember if any operand needs spilling
402 instrNeedsSpills |= LR->isMarkedForSpill();
403
404 // An operand may have a color whether or not it needs spilling
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000405 if (LR->hasColor())
406 MInst->SetRegForOperand(OpNum,
Brian Gaeke59b1c562003-09-24 17:50:28 +0000407 MRI.getUnifiedRegNum(LR->getRegClassID(),
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000408 LR->getColor()));
Vikram S. Adve814030a2003-07-29 19:49:21 +0000409 }
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000410 }
411 } // for each operand
Vikram S. Adve814030a2003-07-29 19:49:21 +0000412
413 return instrNeedsSpills;
414}
415
Brian Gaekeaf843702003-10-22 20:22:53 +0000416/// Mark allocated registers (using markAllocatedRegs()) on the instruction
417/// that MII points to. Then, if it's a call instruction, insert caller-saving
418/// code before and after it. Finally, insert spill code before and after it,
419/// using insertCode4SpilledLR().
420///
Vikram S. Adve814030a2003-07-29 19:49:21 +0000421void PhyRegAlloc::updateInstruction(MachineBasicBlock::iterator& MII,
Brian Gaekeaf843702003-10-22 20:22:53 +0000422 MachineBasicBlock &MBB) {
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000423 MachineInstr* MInst = MII;
Brian Gaeke12c1d2c2004-02-11 20:47:34 +0000424 unsigned Opcode = MInst->getOpcode();
Vikram S. Adve814030a2003-07-29 19:49:21 +0000425
426 // Reset tmp stack positions so they can be reused for each machine instr.
Chris Lattnera1e51ff2004-08-18 18:13:37 +0000427 MF->getInfo<SparcV9FunctionInfo>()->popAllTempValues();
Vikram S. Adve814030a2003-07-29 19:49:21 +0000428
429 // Mark the operands for which regs have been allocated.
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000430 bool instrNeedsSpills = markAllocatedRegs(MII);
Vikram S. Adve814030a2003-07-29 19:49:21 +0000431
432#ifndef NDEBUG
433 // Mark that the operands have been updated. Later,
434 // setRelRegsUsedByThisInst() is called to find registers used by each
435 // MachineInst, and it should not be used for an instruction until
436 // this is done. This flag just serves as a sanity check.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000437 OperandsColoredMap[MInst] = true;
Vikram S. Adve814030a2003-07-29 19:49:21 +0000438#endif
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000439
Vikram S. Advebc001b22003-07-25 21:06:09 +0000440 // Now insert caller-saving code before/after the call.
441 // Do this before inserting spill code since some registers must be
442 // used by save/restore and spill code should not use those registers.
Chris Lattnerd029cd22004-06-02 05:55:25 +0000443 if (TM.getInstrInfo()->isCall(Opcode)) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000444 AddedInstrns &AI = AddedInstrMap[MInst];
Vikram S. Adve814030a2003-07-29 19:49:21 +0000445 insertCallerSavingCode(AI.InstrnsBefore, AI.InstrnsAfter, MInst,
446 MBB.getBasicBlock());
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000447 }
Vikram S. Advebc001b22003-07-25 21:06:09 +0000448
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000449 // Now insert spill code for remaining operands not allocated to
450 // registers. This must be done even for call return instructions
451 // since those are not handled by the special code above.
Vikram S. Adve814030a2003-07-29 19:49:21 +0000452 if (instrNeedsSpills)
Brian Gaekeaf843702003-10-22 20:22:53 +0000453 for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000454 MachineOperand& Op = MInst->getOperand(OpNum);
455 if (Op.getType() == MachineOperand::MO_VirtualRegister ||
Brian Gaekeaf843702003-10-22 20:22:53 +0000456 Op.getType() == MachineOperand::MO_CCRegister) {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000457 const Value* Val = Op.getVRegValue();
Brian Gaeke4efe3422003-09-21 01:23:46 +0000458 if (const LiveRange *LR = LRI->getLiveRangeForValue(Val))
Vikram S. Adve814030a2003-07-29 19:49:21 +0000459 if (LR->isMarkedForSpill())
460 insertCode4SpilledLR(LR, MII, MBB, OpNum);
461 }
462 } // for each operand
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000463}
464
Brian Gaekeaf843702003-10-22 20:22:53 +0000465/// Iterate over all the MachineBasicBlocks in the current function and set
466/// the allocated registers for each instruction (using updateInstruction()),
467/// after register allocation is complete. Then move code out of delay slots.
468///
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000469void PhyRegAlloc::updateMachineCode()
470{
Chris Lattner7e708292002-06-25 16:13:24 +0000471 // Insert any instructions needed at method entry
Brian Gaeke4efe3422003-09-21 01:23:46 +0000472 MachineBasicBlock::iterator MII = MF->front().begin();
473 PrependInstructions(AddedInstrAtEntry.InstrnsBefore, MF->front(), MII,
Chris Lattner7e708292002-06-25 16:13:24 +0000474 "At function entry: \n");
475 assert(AddedInstrAtEntry.InstrnsAfter.empty() &&
476 "InstrsAfter should be unnecessary since we are just inserting at "
477 "the function entry point here.");
Vikram S. Adve48762092002-04-25 04:34:15 +0000478
Brian Gaeke4efe3422003-09-21 01:23:46 +0000479 for (MachineFunction::iterator BBI = MF->begin(), BBE = MF->end();
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000480 BBI != BBE; ++BBI) {
Chris Lattnerf726e772002-10-28 19:22:04 +0000481 MachineBasicBlock &MBB = *BBI;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000482
483 // Iterate over all machine instructions in BB and mark operands with
484 // their assigned registers or insert spill code, as appropriate.
485 // Also, fix operands of call/return instructions.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000486 for (MachineBasicBlock::iterator MII = MBB.begin(); MII != MBB.end(); ++MII)
Chris Lattnerd029cd22004-06-02 05:55:25 +0000487 if (! TM.getInstrInfo()->isDummyPhiInstr(MII->getOpcode()))
Vikram S. Adve814030a2003-07-29 19:49:21 +0000488 updateInstruction(MII, MBB);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000489
490 // Now, move code out of delay slots of branches and returns if needed.
491 // (Also, move "after" code from calls to the last delay slot instruction.)
492 // Moving code out of delay slots is needed in 2 situations:
493 // (1) If this is a branch and it needs instructions inserted after it,
494 // move any existing instructions out of the delay slot so that the
495 // instructions can go into the delay slot. This only supports the
496 // case that #instrsAfter <= #delay slots.
497 //
498 // (2) If any instruction in the delay slot needs
499 // instructions inserted, move it out of the delay slot and before the
500 // branch because putting code before or after it would be VERY BAD!
501 //
502 // If the annul bit of the branch is set, neither of these is legal!
503 // If so, we need to handle spill differently but annulling is not yet used.
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000504 for (MachineBasicBlock::iterator MII = MBB.begin(); MII != MBB.end(); ++MII)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000505 if (unsigned delaySlots =
Chris Lattnerd029cd22004-06-02 05:55:25 +0000506 TM.getInstrInfo()->getNumDelaySlots(MII->getOpcode())) {
Alkis Evlogimenosf81af212004-02-14 01:18:34 +0000507 MachineBasicBlock::iterator DelaySlotMI = next(MII);
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000508 assert(DelaySlotMI != MBB.end() && "no instruction for delay slot");
Vikram S. Adve814030a2003-07-29 19:49:21 +0000509
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000510 // Check the 2 conditions above:
511 // (1) Does a branch need instructions added after it?
512 // (2) O/w does delay slot instr. need instrns before or after?
Chris Lattnerd029cd22004-06-02 05:55:25 +0000513 bool isBranch = (TM.getInstrInfo()->isBranch(MII->getOpcode()) ||
514 TM.getInstrInfo()->isReturn(MII->getOpcode()));
Vikram S. Adve814030a2003-07-29 19:49:21 +0000515 bool cond1 = (isBranch &&
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000516 AddedInstrMap.count(MII) &&
517 AddedInstrMap[MII].InstrnsAfter.size() > 0);
Vikram S. Adve814030a2003-07-29 19:49:21 +0000518 bool cond2 = (AddedInstrMap.count(DelaySlotMI) &&
519 (AddedInstrMap[DelaySlotMI].InstrnsBefore.size() > 0 ||
520 AddedInstrMap[DelaySlotMI].InstrnsAfter.size() > 0));
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000521
Brian Gaekeaf843702003-10-22 20:22:53 +0000522 if (cond1 || cond2) {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000523 assert(delaySlots==1 &&
524 "InsertBefore does not yet handle >1 delay slots!");
Vikram S. Adve814030a2003-07-29 19:49:21 +0000525
526 if (DEBUG_RA) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000527 std::cerr << "\nRegAlloc: Moved instr. with added code: "
Vikram S. Adve814030a2003-07-29 19:49:21 +0000528 << *DelaySlotMI
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000529 << " out of delay slots of instr: " << *MII;
530 }
531
532 // move instruction before branch
Chris Lattnerb4186e02004-03-31 21:59:59 +0000533 MBB.insert(MII, MBB.remove(DelaySlotMI++));
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000534
535 // On cond1 we are done (we already moved the
536 // instruction out of the delay slot). On cond2 we need
537 // to insert a nop in place of the moved instruction
538 if (cond2) {
Brian Gaekec9989812004-07-27 17:43:24 +0000539 MBB.insert(MII, BuildMI(V9::NOP, 1));
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000540 }
541 }
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000542 else {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000543 // For non-branch instr with delay slots (probably a call), move
544 // InstrAfter to the instr. in the last delay slot.
Alkis Evlogimenosf81af212004-02-14 01:18:34 +0000545 MachineBasicBlock::iterator tmp = next(MII, delaySlots);
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000546 move2DelayedInstr(MII, tmp);
547 }
548 }
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000549
550 // Finally iterate over all instructions in BB and insert before/after
Vikram S. Advebc001b22003-07-25 21:06:09 +0000551 for (MachineBasicBlock::iterator MII=MBB.begin(); MII != MBB.end(); ++MII) {
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000552 MachineInstr *MInst = MII;
Vikram S. Advebc001b22003-07-25 21:06:09 +0000553
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000554 // do not process Phis
Chris Lattnerd029cd22004-06-02 05:55:25 +0000555 if (TM.getInstrInfo()->isDummyPhiInstr(MInst->getOpcode()))
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000556 continue;
557
Vikram S. Advebc001b22003-07-25 21:06:09 +0000558 // if there are any added instructions...
Chris Lattner7e708292002-06-25 16:13:24 +0000559 if (AddedInstrMap.count(MInst)) {
Vikram S. Advebc001b22003-07-25 21:06:09 +0000560 AddedInstrns &CallAI = AddedInstrMap[MInst];
561
562#ifndef NDEBUG
Chris Lattnerd029cd22004-06-02 05:55:25 +0000563 bool isBranch = (TM.getInstrInfo()->isBranch(MInst->getOpcode()) ||
564 TM.getInstrInfo()->isReturn(MInst->getOpcode()));
Vikram S. Adve814030a2003-07-29 19:49:21 +0000565 assert((!isBranch ||
566 AddedInstrMap[MInst].InstrnsAfter.size() <=
Chris Lattnerd029cd22004-06-02 05:55:25 +0000567 TM.getInstrInfo()->getNumDelaySlots(MInst->getOpcode())) &&
Vikram S. Adve814030a2003-07-29 19:49:21 +0000568 "Cannot put more than #delaySlots instrns after "
569 "branch or return! Need to handle temps differently.");
570#endif
571
572#ifndef NDEBUG
Vikram S. Advebc001b22003-07-25 21:06:09 +0000573 // Temporary sanity checking code to detect whether the same machine
574 // instruction is ever inserted twice before/after a call.
575 // I suspect this is happening but am not sure. --Vikram, 7/1/03.
Vikram S. Advebc001b22003-07-25 21:06:09 +0000576 std::set<const MachineInstr*> instrsSeen;
577 for (int i = 0, N = CallAI.InstrnsBefore.size(); i < N; ++i) {
578 assert(instrsSeen.count(CallAI.InstrnsBefore[i]) == 0 &&
579 "Duplicate machine instruction in InstrnsBefore!");
580 instrsSeen.insert(CallAI.InstrnsBefore[i]);
581 }
582 for (int i = 0, N = CallAI.InstrnsAfter.size(); i < N; ++i) {
583 assert(instrsSeen.count(CallAI.InstrnsAfter[i]) == 0 &&
584 "Duplicate machine instruction in InstrnsBefore/After!");
585 instrsSeen.insert(CallAI.InstrnsAfter[i]);
586 }
587#endif
588
589 // Now add the instructions before/after this MI.
590 // We do this here to ensure that spill for an instruction is inserted
591 // as close as possible to an instruction (see above insertCode4Spill)
Vikram S. Advebc001b22003-07-25 21:06:09 +0000592 if (! CallAI.InstrnsBefore.empty())
593 PrependInstructions(CallAI.InstrnsBefore, MBB, MII,"");
594
595 if (! CallAI.InstrnsAfter.empty())
596 AppendInstructions(CallAI.InstrnsAfter, MBB, MII,"");
597
598 } // if there are any added instructions
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000599 } // for each machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000600 }
601}
602
603
Brian Gaekeaf843702003-10-22 20:22:53 +0000604/// Insert spill code for AN operand whose LR was spilled. May be called
605/// repeatedly for a single MachineInstr if it has many spilled operands. On
606/// each call, it finds a register which is not live at that instruction and
607/// also which is not used by other spilled operands of the same
608/// instruction. Then it uses this register temporarily to accommodate the
609/// spilled value.
610///
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000611void PhyRegAlloc::insertCode4SpilledLR(const LiveRange *LR,
Vikram S. Adve814030a2003-07-29 19:49:21 +0000612 MachineBasicBlock::iterator& MII,
613 MachineBasicBlock &MBB,
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000614 const unsigned OpNum) {
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000615 MachineInstr *MInst = MII;
Vikram S. Adve814030a2003-07-29 19:49:21 +0000616 const BasicBlock *BB = MBB.getBasicBlock();
617
Chris Lattnerd029cd22004-06-02 05:55:25 +0000618 assert((! TM.getInstrInfo()->isCall(MInst->getOpcode()) || OpNum == 0) &&
Vikram S. Advead9c9782002-09-28 17:02:40 +0000619 "Outgoing arg of a call must be handled elsewhere (func arg ok)");
Chris Lattnerd029cd22004-06-02 05:55:25 +0000620 assert(! TM.getInstrInfo()->isReturn(MInst->getOpcode()) &&
Vikram S. Advead9c9782002-09-28 17:02:40 +0000621 "Return value of a ret must be handled elsewhere");
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000622
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000623 MachineOperand& Op = MInst->getOperand(OpNum);
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000624 bool isDef = Op.isDef();
625 bool isUse = Op.isUse();
Vikram S. Advebc001b22003-07-25 21:06:09 +0000626 unsigned RegType = MRI.getRegTypeForLR(LR);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000627 int SpillOff = LR->getSpillOffFromFP();
628 RegClass *RC = LR->getRegClass();
Vikram S. Adve814030a2003-07-29 19:49:21 +0000629
630 // Get the live-variable set to find registers free before this instr.
Vikram S. Advefeb32982003-08-12 22:22:24 +0000631 const ValueSet &LVSetBef = LVI->getLiveVarSetBeforeMInst(MInst, BB);
632
633#ifndef NDEBUG
634 // If this instr. is in the delay slot of a branch or return, we need to
635 // include all live variables before that branch or return -- we don't want to
636 // trample those! Verify that the set is included in the LV set before MInst.
Vikram S. Adve814030a2003-07-29 19:49:21 +0000637 if (MII != MBB.begin()) {
Alkis Evlogimenosf81af212004-02-14 01:18:34 +0000638 MachineBasicBlock::iterator PredMI = prior(MII);
Chris Lattnerd029cd22004-06-02 05:55:25 +0000639 if (unsigned DS = TM.getInstrInfo()->getNumDelaySlots(PredMI->getOpcode()))
Vikram S. Advefeb32982003-08-12 22:22:24 +0000640 assert(set_difference(LVI->getLiveVarSetBeforeMInst(PredMI), LVSetBef)
641 .empty() && "Live-var set before branch should be included in "
642 "live-var set of each delay slot instruction!");
Vikram S. Adve814030a2003-07-29 19:49:21 +0000643 }
Vikram S. Advefeb32982003-08-12 22:22:24 +0000644#endif
Vikram S. Adve00521d72001-11-12 23:26:35 +0000645
Chris Lattnera1e51ff2004-08-18 18:13:37 +0000646 MF->getInfo<SparcV9FunctionInfo>()->pushTempValue(MRI.getSpilledRegSize(RegType));
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000647
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000648 std::vector<MachineInstr*> MIBef, MIAft;
649 std::vector<MachineInstr*> AdIMid;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000650
Vikram S. Adve3bf08922003-07-10 19:42:55 +0000651 // Choose a register to hold the spilled value, if one was not preallocated.
652 // This may insert code before and after MInst to free up the value. If so,
653 // this code should be first/last in the spill sequence before/after MInst.
654 int TmpRegU=(LR->hasColor()
Brian Gaeke59b1c562003-09-24 17:50:28 +0000655 ? MRI.getUnifiedRegNum(LR->getRegClassID(),LR->getColor())
Vikram S. Adve3bf08922003-07-10 19:42:55 +0000656 : getUsableUniRegAtMI(RegType, &LVSetBef, MInst, MIBef,MIAft));
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000657
Vikram S. Advef5af6362002-07-08 23:15:32 +0000658 // Set the operand first so that it this register does not get used
659 // as a scratch register for later calls to getUsableUniRegAtMI below
660 MInst->SetRegForOperand(OpNum, TmpRegU);
661
662 // get the added instructions for this instruction
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000663 AddedInstrns &AI = AddedInstrMap[MInst];
Vikram S. Advef5af6362002-07-08 23:15:32 +0000664
665 // We may need a scratch register to copy the spilled value to/from memory.
666 // This may itself have to insert code to free up a scratch register.
667 // Any such code should go before (after) the spill code for a load (store).
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000668 // The scratch reg is not marked as used because it is only used
669 // for the copy and not used across MInst.
Vikram S. Advef5af6362002-07-08 23:15:32 +0000670 int scratchRegType = -1;
671 int scratchReg = -1;
Brian Gaekeaf843702003-10-22 20:22:53 +0000672 if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType)) {
Chris Lattner27a08932002-10-22 23:16:21 +0000673 scratchReg = getUsableUniRegAtMI(scratchRegType, &LVSetBef,
674 MInst, MIBef, MIAft);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000675 assert(scratchReg != MRI.getInvalidRegNum());
Vikram S. Advef5af6362002-07-08 23:15:32 +0000676 }
677
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000678 if (isUse) {
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000679 // for a USE, we have to load the value of LR from stack to a TmpReg
680 // and use the TmpReg as one operand of instruction
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000681
Vikram S. Advef5af6362002-07-08 23:15:32 +0000682 // actual loading instruction(s)
Vikram S. Adve814030a2003-07-29 19:49:21 +0000683 MRI.cpMem2RegMI(AdIMid, MRI.getFramePointer(), SpillOff, TmpRegU,
684 RegType, scratchReg);
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000685
Vikram S. Advef5af6362002-07-08 23:15:32 +0000686 // the actual load should be after the instructions to free up TmpRegU
687 MIBef.insert(MIBef.end(), AdIMid.begin(), AdIMid.end());
688 AdIMid.clear();
689 }
690
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000691 if (isDef) { // if this is a Def
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000692 // for a DEF, we have to store the value produced by this instruction
693 // on the stack position allocated for this LR
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000694
Vikram S. Advef5af6362002-07-08 23:15:32 +0000695 // actual storing instruction(s)
Vikram S. Adve814030a2003-07-29 19:49:21 +0000696 MRI.cpReg2MemMI(AdIMid, TmpRegU, MRI.getFramePointer(), SpillOff,
697 RegType, scratchReg);
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000698
Vikram S. Advef5af6362002-07-08 23:15:32 +0000699 MIAft.insert(MIAft.begin(), AdIMid.begin(), AdIMid.end());
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000700 } // if !DEF
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000701
Vikram S. Advef5af6362002-07-08 23:15:32 +0000702 // Finally, insert the entire spill code sequences before/after MInst
703 AI.InstrnsBefore.insert(AI.InstrnsBefore.end(), MIBef.begin(), MIBef.end());
704 AI.InstrnsAfter.insert(AI.InstrnsAfter.begin(), MIAft.begin(), MIAft.end());
705
Chris Lattner7e708292002-06-25 16:13:24 +0000706 if (DEBUG_RA) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000707 std::cerr << "\nFor Inst:\n " << *MInst;
708 std::cerr << "SPILLED LR# " << LR->getUserIGNode()->getIndex();
709 std::cerr << "; added Instructions:";
Anand Shuklad58290e2002-07-09 19:18:56 +0000710 for_each(MIBef.begin(), MIBef.end(), std::mem_fun(&MachineInstr::dump));
711 for_each(MIAft.begin(), MIAft.end(), std::mem_fun(&MachineInstr::dump));
Chris Lattner7e708292002-06-25 16:13:24 +0000712 }
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000713}
714
715
Brian Gaekeaf843702003-10-22 20:22:53 +0000716/// Insert caller saving/restoring instructions before/after a call machine
717/// instruction (before or after any other instructions that were inserted for
718/// the call).
719///
Vikram S. Adve814030a2003-07-29 19:49:21 +0000720void
721PhyRegAlloc::insertCallerSavingCode(std::vector<MachineInstr*> &instrnsBefore,
722 std::vector<MachineInstr*> &instrnsAfter,
723 MachineInstr *CallMI,
Brian Gaekeaf843702003-10-22 20:22:53 +0000724 const BasicBlock *BB) {
Chris Lattnerd029cd22004-06-02 05:55:25 +0000725 assert(TM.getInstrInfo()->isCall(CallMI->getOpcode()));
Vikram S. Adve814030a2003-07-29 19:49:21 +0000726
Brian Gaeke43ce8fe2003-09-21 02:24:09 +0000727 // hash set to record which registers were saved/restored
Vikram S. Adve814030a2003-07-29 19:49:21 +0000728 hash_set<unsigned> PushedRegSet;
729
730 CallArgsDescriptor* argDesc = CallArgsDescriptor::get(CallMI);
731
732 // if the call is to a instrumentation function, do not insert save and
733 // restore instructions the instrumentation function takes care of save
734 // restore for volatile regs.
735 //
736 // FIXME: this should be made general, not specific to the reoptimizer!
Vikram S. Adve814030a2003-07-29 19:49:21 +0000737 const Function *Callee = argDesc->getCallInst()->getCalledFunction();
738 bool isLLVMFirstTrigger = Callee && Callee->getName() == "llvm_first_trigger";
739
740 // Now check if the call has a return value (using argDesc) and if so,
741 // find the LR of the TmpInstruction representing the return value register.
742 // (using the last or second-last *implicit operand* of the call MI).
743 // Insert it to to the PushedRegSet since we must not save that register
744 // and restore it after the call.
745 // We do this because, we look at the LV set *after* the instruction
746 // to determine, which LRs must be saved across calls. The return value
747 // of the call is live in this set - but we must not save/restore it.
Vikram S. Adve814030a2003-07-29 19:49:21 +0000748 if (const Value *origRetVal = argDesc->getReturnValue()) {
749 unsigned retValRefNum = (CallMI->getNumImplicitRefs() -
750 (argDesc->getIndirectFuncPtr()? 1 : 2));
751 const TmpInstruction* tmpRetVal =
752 cast<TmpInstruction>(CallMI->getImplicitRef(retValRefNum));
753 assert(tmpRetVal->getOperand(0) == origRetVal &&
754 tmpRetVal->getType() == origRetVal->getType() &&
755 "Wrong implicit ref?");
Brian Gaeke4efe3422003-09-21 01:23:46 +0000756 LiveRange *RetValLR = LRI->getLiveRangeForValue(tmpRetVal);
Vikram S. Adve814030a2003-07-29 19:49:21 +0000757 assert(RetValLR && "No LR for RetValue of call");
758
759 if (! RetValLR->isMarkedForSpill())
760 PushedRegSet.insert(MRI.getUnifiedRegNum(RetValLR->getRegClassID(),
761 RetValLR->getColor()));
762 }
763
764 const ValueSet &LVSetAft = LVI->getLiveVarSetAfterMInst(CallMI, BB);
765 ValueSet::const_iterator LIt = LVSetAft.begin();
766
767 // for each live var in live variable set after machine inst
768 for( ; LIt != LVSetAft.end(); ++LIt) {
Brian Gaeke43ce8fe2003-09-21 02:24:09 +0000769 // get the live range corresponding to live var
Brian Gaeke4efe3422003-09-21 01:23:46 +0000770 LiveRange *const LR = LRI->getLiveRangeForValue(*LIt);
Vikram S. Adve814030a2003-07-29 19:49:21 +0000771
772 // LR can be null if it is a const since a const
773 // doesn't have a dominating def - see Assumptions above
Brian Gaekeaf843702003-10-22 20:22:53 +0000774 if (LR) {
775 if (! LR->isMarkedForSpill()) {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000776 assert(LR->hasColor() && "LR is neither spilled nor colored?");
777 unsigned RCID = LR->getRegClassID();
778 unsigned Color = LR->getColor();
779
780 if (MRI.isRegVolatile(RCID, Color) ) {
Brian Gaeke43ce8fe2003-09-21 02:24:09 +0000781 // if this is a call to the first-level reoptimizer
782 // instrumentation entry point, and the register is not
783 // modified by call, don't save and restore it.
Vikram S. Adve814030a2003-07-29 19:49:21 +0000784 if (isLLVMFirstTrigger && !MRI.modifiedByCall(RCID, Color))
785 continue;
786
787 // if the value is in both LV sets (i.e., live before and after
788 // the call machine instruction)
Vikram S. Adve814030a2003-07-29 19:49:21 +0000789 unsigned Reg = MRI.getUnifiedRegNum(RCID, Color);
790
Brian Gaeke43ce8fe2003-09-21 02:24:09 +0000791 // if we haven't already pushed this register...
Vikram S. Adve814030a2003-07-29 19:49:21 +0000792 if( PushedRegSet.find(Reg) == PushedRegSet.end() ) {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000793 unsigned RegType = MRI.getRegTypeForLR(LR);
794
795 // Now get two instructions - to push on stack and pop from stack
796 // and add them to InstrnsBefore and InstrnsAfter of the
797 // call instruction
Vikram S. Adve814030a2003-07-29 19:49:21 +0000798 int StackOff =
Chris Lattnera1e51ff2004-08-18 18:13:37 +0000799 MF->getInfo<SparcV9FunctionInfo>()->pushTempValue(MRI.getSpilledRegSize(RegType));
Vikram S. Adve814030a2003-07-29 19:49:21 +0000800
801 //---- Insert code for pushing the reg on stack ----------
802
803 std::vector<MachineInstr*> AdIBef, AdIAft;
804
805 // We may need a scratch register to copy the saved value
806 // to/from memory. This may itself have to insert code to
807 // free up a scratch register. Any such code should go before
808 // the save code. The scratch register, if any, is by default
809 // temporary and not "used" by the instruction unless the
810 // copy code itself decides to keep the value in the scratch reg.
811 int scratchRegType = -1;
812 int scratchReg = -1;
813 if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType))
814 { // Find a register not live in the LVSet before CallMI
815 const ValueSet &LVSetBef =
816 LVI->getLiveVarSetBeforeMInst(CallMI, BB);
817 scratchReg = getUsableUniRegAtMI(scratchRegType, &LVSetBef,
818 CallMI, AdIBef, AdIAft);
819 assert(scratchReg != MRI.getInvalidRegNum());
820 }
821
822 if (AdIBef.size() > 0)
823 instrnsBefore.insert(instrnsBefore.end(),
824 AdIBef.begin(), AdIBef.end());
825
826 MRI.cpReg2MemMI(instrnsBefore, Reg, MRI.getFramePointer(),
827 StackOff, RegType, scratchReg);
828
829 if (AdIAft.size() > 0)
830 instrnsBefore.insert(instrnsBefore.end(),
831 AdIAft.begin(), AdIAft.end());
832
833 //---- Insert code for popping the reg from the stack ----------
Vikram S. Adve814030a2003-07-29 19:49:21 +0000834 AdIBef.clear();
835 AdIAft.clear();
836
837 // We may need a scratch register to copy the saved value
838 // from memory. This may itself have to insert code to
839 // free up a scratch register. Any such code should go
840 // after the save code. As above, scratch is not marked "used".
Vikram S. Adve814030a2003-07-29 19:49:21 +0000841 scratchRegType = -1;
842 scratchReg = -1;
843 if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType))
844 { // Find a register not live in the LVSet after CallMI
845 scratchReg = getUsableUniRegAtMI(scratchRegType, &LVSetAft,
846 CallMI, AdIBef, AdIAft);
847 assert(scratchReg != MRI.getInvalidRegNum());
848 }
849
850 if (AdIBef.size() > 0)
851 instrnsAfter.insert(instrnsAfter.end(),
852 AdIBef.begin(), AdIBef.end());
853
854 MRI.cpMem2RegMI(instrnsAfter, MRI.getFramePointer(), StackOff,
855 Reg, RegType, scratchReg);
856
857 if (AdIAft.size() > 0)
858 instrnsAfter.insert(instrnsAfter.end(),
859 AdIAft.begin(), AdIAft.end());
860
861 PushedRegSet.insert(Reg);
862
863 if(DEBUG_RA) {
864 std::cerr << "\nFor call inst:" << *CallMI;
865 std::cerr << " -inserted caller saving instrs: Before:\n\t ";
866 for_each(instrnsBefore.begin(), instrnsBefore.end(),
867 std::mem_fun(&MachineInstr::dump));
868 std::cerr << " -and After:\n\t ";
869 for_each(instrnsAfter.begin(), instrnsAfter.end(),
870 std::mem_fun(&MachineInstr::dump));
871 }
872 } // if not already pushed
Vikram S. Adve814030a2003-07-29 19:49:21 +0000873 } // if LR has a volatile color
Vikram S. Adve814030a2003-07-29 19:49:21 +0000874 } // if LR has color
Vikram S. Adve814030a2003-07-29 19:49:21 +0000875 } // if there is a LR for Var
Vikram S. Adve814030a2003-07-29 19:49:21 +0000876 } // for each value in the LV set after instruction
877}
878
879
Brian Gaekeaf843702003-10-22 20:22:53 +0000880/// Returns the unified register number of a temporary register to be used
881/// BEFORE MInst. If no register is available, it will pick one and modify
882/// MIBef and MIAft to contain instructions used to free up this returned
883/// register.
884///
Vikram S. Advef5af6362002-07-08 23:15:32 +0000885int PhyRegAlloc::getUsableUniRegAtMI(const int RegType,
886 const ValueSet *LVSetBef,
887 MachineInstr *MInst,
888 std::vector<MachineInstr*>& MIBef,
889 std::vector<MachineInstr*>& MIAft) {
Chris Lattner133f0792002-10-28 04:45:29 +0000890 RegClass* RC = getRegClassByID(MRI.getRegClassIDOfRegType(RegType));
Vikram S. Advef5af6362002-07-08 23:15:32 +0000891
Brian Gaekeaf843702003-10-22 20:22:53 +0000892 int RegU = getUnusedUniRegAtMI(RC, RegType, MInst, LVSetBef);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000893
894 if (RegU == -1) {
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000895 // we couldn't find an unused register. Generate code to free up a reg by
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000896 // saving it on stack and restoring after the instruction
Vikram S. Advef5af6362002-07-08 23:15:32 +0000897
Chris Lattnera1e51ff2004-08-18 18:13:37 +0000898 int TmpOff = MF->getInfo<SparcV9FunctionInfo>()->pushTempValue(MRI.getSpilledRegSize(RegType));
Vikram S. Adve12af1642001-11-08 04:48:50 +0000899
Vikram S. Advebc001b22003-07-25 21:06:09 +0000900 RegU = getUniRegNotUsedByThisInst(RC, RegType, MInst);
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000901
Vikram S. Advef5af6362002-07-08 23:15:32 +0000902 // Check if we need a scratch register to copy this register to memory.
903 int scratchRegType = -1;
Brian Gaekeaf843702003-10-22 20:22:53 +0000904 if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType)) {
Chris Lattner133f0792002-10-28 04:45:29 +0000905 int scratchReg = getUsableUniRegAtMI(scratchRegType, LVSetBef,
906 MInst, MIBef, MIAft);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000907 assert(scratchReg != MRI.getInvalidRegNum());
908
909 // We may as well hold the value in the scratch register instead
910 // of copying it to memory and back. But we have to mark the
911 // register as used by this instruction, so it does not get used
912 // as a scratch reg. by another operand or anyone else.
Chris Lattner3fd1f5b2003-08-05 22:11:13 +0000913 ScratchRegsUsed.insert(std::make_pair(MInst, scratchReg));
Vikram S. Advef5af6362002-07-08 23:15:32 +0000914 MRI.cpReg2RegMI(MIBef, RegU, scratchReg, RegType);
915 MRI.cpReg2RegMI(MIAft, scratchReg, RegU, RegType);
Brian Gaekeaf843702003-10-22 20:22:53 +0000916 } else { // the register can be copied directly to/from memory so do it.
Vikram S. Advef5af6362002-07-08 23:15:32 +0000917 MRI.cpReg2MemMI(MIBef, RegU, MRI.getFramePointer(), TmpOff, RegType);
918 MRI.cpMem2RegMI(MIAft, MRI.getFramePointer(), TmpOff, RegU, RegType);
Brian Gaekeaf843702003-10-22 20:22:53 +0000919 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000920 }
Vikram S. Advef5af6362002-07-08 23:15:32 +0000921
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000922 return RegU;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000923}
924
Vikram S. Adve814030a2003-07-29 19:49:21 +0000925
Brian Gaekeaf843702003-10-22 20:22:53 +0000926/// Returns the register-class register number of a new unused register that
927/// can be used to accommodate a temporary value. May be called repeatedly
928/// for a single MachineInstr. On each call, it finds a register which is not
929/// live at that instruction and which is not used by any spilled operands of
930/// that instruction.
931///
932int PhyRegAlloc::getUnusedUniRegAtMI(RegClass *RC, const int RegType,
Vikram S. Adve814030a2003-07-29 19:49:21 +0000933 const MachineInstr *MInst,
934 const ValueSet* LVSetBef) {
Vikram S. Advebc001b22003-07-25 21:06:09 +0000935 RC->clearColorsUsed(); // Reset array
Vikram S. Adve814030a2003-07-29 19:49:21 +0000936
937 if (LVSetBef == NULL) {
938 LVSetBef = &LVI->getLiveVarSetBeforeMInst(MInst);
939 assert(LVSetBef != NULL && "Unable to get live-var set before MInst?");
940 }
941
Chris Lattner296b7732002-02-05 02:52:05 +0000942 ValueSet::const_iterator LIt = LVSetBef->begin();
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000943
944 // for each live var in live variable set after machine inst
Chris Lattner7e708292002-06-25 16:13:24 +0000945 for ( ; LIt != LVSetBef->end(); ++LIt) {
Brian Gaeke43ce8fe2003-09-21 02:24:09 +0000946 // Get the live range corresponding to live var, and its RegClass
Brian Gaeke4efe3422003-09-21 01:23:46 +0000947 LiveRange *const LRofLV = LRI->getLiveRangeForValue(*LIt );
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000948
949 // LR can be null if it is a const since a const
950 // doesn't have a dominating def - see Assumptions above
Vikram S. Advebc001b22003-07-25 21:06:09 +0000951 if (LRofLV && LRofLV->getRegClass() == RC && LRofLV->hasColor())
952 RC->markColorsUsed(LRofLV->getColor(),
953 MRI.getRegTypeForLR(LRofLV), RegType);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000954 }
955
956 // It is possible that one operand of this MInst was already spilled
957 // and it received some register temporarily. If that's the case,
958 // it is recorded in machine operand. We must skip such registers.
Vikram S. Advebc001b22003-07-25 21:06:09 +0000959 setRelRegsUsedByThisInst(RC, RegType, MInst);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000960
Vikram S. Advebc001b22003-07-25 21:06:09 +0000961 int unusedReg = RC->getUnusedColor(RegType); // find first unused color
962 if (unusedReg >= 0)
963 return MRI.getUnifiedRegNum(RC->getID(), unusedReg);
964
Chris Lattner85c54652002-05-23 15:50:03 +0000965 return -1;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000966}
967
968
Brian Gaekeaf843702003-10-22 20:22:53 +0000969/// Return the unified register number of a register in class RC which is not
970/// used by any operands of MInst.
971///
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000972int PhyRegAlloc::getUniRegNotUsedByThisInst(RegClass *RC,
Vikram S. Advebc001b22003-07-25 21:06:09 +0000973 const int RegType,
Chris Lattner85c54652002-05-23 15:50:03 +0000974 const MachineInstr *MInst) {
Vikram S. Advebc001b22003-07-25 21:06:09 +0000975 RC->clearColorsUsed();
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000976
Vikram S. Advebc001b22003-07-25 21:06:09 +0000977 setRelRegsUsedByThisInst(RC, RegType, MInst);
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000978
Vikram S. Advebc001b22003-07-25 21:06:09 +0000979 // find the first unused color
980 int unusedReg = RC->getUnusedColor(RegType);
981 assert(unusedReg >= 0 &&
982 "FATAL: No free register could be found in reg class!!");
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000983
Vikram S. Advebc001b22003-07-25 21:06:09 +0000984 return MRI.getUnifiedRegNum(RC->getID(), unusedReg);
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000985}
986
987
Brian Gaekeaf843702003-10-22 20:22:53 +0000988/// Modify the IsColorUsedArr of register class RC, by setting the bits
989/// corresponding to register RegNo. This is a helper method of
990/// setRelRegsUsedByThisInst().
991///
Chris Lattner3bed95b2003-08-05 21:55:58 +0000992static void markRegisterUsed(int RegNo, RegClass *RC, int RegType,
Brian Gaeke498231b2004-06-03 02:45:09 +0000993 const SparcV9RegInfo &TRI) {
Chris Lattner3bed95b2003-08-05 21:55:58 +0000994 unsigned classId = 0;
995 int classRegNum = TRI.getClassRegNum(RegNo, classId);
996 if (RC->getID() == classId)
997 RC->markColorsUsed(classRegNum, RegType, RegType);
998}
999
1000void PhyRegAlloc::setRelRegsUsedByThisInst(RegClass *RC, int RegType,
Brian Gaekeaf843702003-10-22 20:22:53 +00001001 const MachineInstr *MI) {
Chris Lattner3bed95b2003-08-05 21:55:58 +00001002 assert(OperandsColoredMap[MI] == true &&
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001003 "Illegal to call setRelRegsUsedByThisInst() until colored operands "
1004 "are marked for an instruction.");
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001005
Brian Gaekeaf843702003-10-22 20:22:53 +00001006 // Add the registers already marked as used by the instruction. Both
1007 // explicit and implicit operands are set.
Chris Lattner3bed95b2003-08-05 21:55:58 +00001008 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
1009 if (MI->getOperand(i).hasAllocatedReg())
Alkis Evlogimenosbe766c72004-02-13 21:01:20 +00001010 markRegisterUsed(MI->getOperand(i).getReg(), RC, RegType,MRI);
Chris Lattner3bed95b2003-08-05 21:55:58 +00001011
1012 for (unsigned i = 0, e = MI->getNumImplicitRefs(); i != e; ++i)
1013 if (MI->getImplicitOp(i).hasAllocatedReg())
Alkis Evlogimenosbe766c72004-02-13 21:01:20 +00001014 markRegisterUsed(MI->getImplicitOp(i).getReg(), RC, RegType,MRI);
Chris Lattner3bed95b2003-08-05 21:55:58 +00001015
Chris Lattner3fd1f5b2003-08-05 22:11:13 +00001016 // Add all of the scratch registers that are used to save values across the
1017 // instruction (e.g., for saving state register values).
1018 std::pair<ScratchRegsUsedTy::iterator, ScratchRegsUsedTy::iterator>
1019 IR = ScratchRegsUsed.equal_range(MI);
1020 for (ScratchRegsUsedTy::iterator I = IR.first; I != IR.second; ++I)
1021 markRegisterUsed(I->second, RC, RegType, MRI);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001022
Vikram S. Advef5af6362002-07-08 23:15:32 +00001023 // If there are implicit references, mark their allocated regs as well
Chris Lattner3bed95b2003-08-05 21:55:58 +00001024 for (unsigned z=0; z < MI->getNumImplicitRefs(); z++)
Vikram S. Advef5af6362002-07-08 23:15:32 +00001025 if (const LiveRange*
Brian Gaeke4efe3422003-09-21 01:23:46 +00001026 LRofImpRef = LRI->getLiveRangeForValue(MI->getImplicitRef(z)))
Vikram S. Advef5af6362002-07-08 23:15:32 +00001027 if (LRofImpRef->hasColor())
1028 // this implicit reference is in a LR that received a color
Vikram S. Advebc001b22003-07-25 21:06:09 +00001029 RC->markColorsUsed(LRofImpRef->getColor(),
1030 MRI.getRegTypeForLR(LRofImpRef), RegType);
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001031}
1032
1033
Brian Gaekeaf843702003-10-22 20:22:53 +00001034/// If there are delay slots for an instruction, the instructions added after
1035/// it must really go after the delayed instruction(s). So, we Move the
1036/// InstrAfter of that instruction to the corresponding delayed instruction
1037/// using the following method.
1038///
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001039void PhyRegAlloc::move2DelayedInstr(const MachineInstr *OrigMI,
1040 const MachineInstr *DelayedMI)
1041{
Vikram S. Advefeb32982003-08-12 22:22:24 +00001042 // "added after" instructions of the original instr
1043 std::vector<MachineInstr *> &OrigAft = AddedInstrMap[OrigMI].InstrnsAfter;
1044
1045 if (DEBUG_RA && OrigAft.size() > 0) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +00001046 std::cerr << "\nRegAlloc: Moved InstrnsAfter for: " << *OrigMI;
1047 std::cerr << " to last delay slot instrn: " << *DelayedMI;
Vikram S. Adve814030a2003-07-29 19:49:21 +00001048 }
1049
Ruchira Sasanka251d8db2001-10-23 21:38:00 +00001050 // "added after" instructions of the delayed instr
Vikram S. Adve814030a2003-07-29 19:49:21 +00001051 std::vector<MachineInstr *> &DelayedAft=AddedInstrMap[DelayedMI].InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +00001052
1053 // go thru all the "added after instructions" of the original instruction
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001054 // and append them to the "added after instructions" of the delayed
Ruchira Sasanka251d8db2001-10-23 21:38:00 +00001055 // instructions
Chris Lattner697954c2002-01-20 22:54:45 +00001056 DelayedAft.insert(DelayedAft.end(), OrigAft.begin(), OrigAft.end());
Ruchira Sasanka251d8db2001-10-23 21:38:00 +00001057
1058 // empty the "added after instructions" of the original instruction
1059 OrigAft.clear();
Ruchira Sasanka251d8db2001-10-23 21:38:00 +00001060}
Ruchira Sasanka0931a012001-09-15 19:06:58 +00001061
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001062
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001063void PhyRegAlloc::colorIncomingArgs()
1064{
Brian Gaeke4efe3422003-09-21 01:23:46 +00001065 MRI.colorMethodArgs(Fn, *LRI, AddedInstrAtEntry.InstrnsBefore,
Vikram S. Adve814030a2003-07-29 19:49:21 +00001066 AddedInstrAtEntry.InstrnsAfter);
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001067}
1068
Ruchira Sasankae727f852001-09-18 22:43:57 +00001069
Brian Gaekeaf843702003-10-22 20:22:53 +00001070/// Determine whether the suggested color of each live range is really usable,
1071/// and then call its setSuggestedColorUsable() method to record the answer. A
1072/// suggested color is NOT usable when the suggested color is volatile AND
1073/// when there are call interferences.
1074///
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001075void PhyRegAlloc::markUnusableSugColors()
1076{
Brian Gaeke4efe3422003-09-21 01:23:46 +00001077 LiveRangeMapType::const_iterator HMI = (LRI->getLiveRangeMap())->begin();
1078 LiveRangeMapType::const_iterator HMIEnd = (LRI->getLiveRangeMap())->end();
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001079
Brian Gaeke43ce8fe2003-09-21 02:24:09 +00001080 for (; HMI != HMIEnd ; ++HMI ) {
1081 if (HMI->first) {
1082 LiveRange *L = HMI->second; // get the LiveRange
Brian Gaeke59b1c562003-09-24 17:50:28 +00001083 if (L && L->hasSuggestedColor ())
Brian Gaeke6a256cc2003-09-24 18:08:54 +00001084 L->setSuggestedColorUsable
1085 (!(MRI.isRegVolatile (L->getRegClassID (), L->getSuggestedColor ())
1086 && L->isCallInterference ()));
Brian Gaeke43ce8fe2003-09-21 02:24:09 +00001087 }
1088 } // for all LR's in hash map
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001089}
1090
1091
Brian Gaekeaf843702003-10-22 20:22:53 +00001092/// For each live range that is spilled, allocates a new spill position on the
1093/// stack, and set the stack offsets of the live range that will be spilled to
1094/// that position. This must be called just after coloring the LRs.
1095///
Chris Lattner37730942002-02-05 03:52:29 +00001096void PhyRegAlloc::allocateStackSpace4SpilledLRs() {
Chris Lattnerc083dcc2003-09-01 20:05:47 +00001097 if (DEBUG_RA) std::cerr << "\nSetting LR stack offsets for spills...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001098
Brian Gaeke4efe3422003-09-21 01:23:46 +00001099 LiveRangeMapType::const_iterator HMI = LRI->getLiveRangeMap()->begin();
1100 LiveRangeMapType::const_iterator HMIEnd = LRI->getLiveRangeMap()->end();
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001101
Chris Lattner7e708292002-06-25 16:13:24 +00001102 for ( ; HMI != HMIEnd ; ++HMI) {
Chris Lattner37730942002-02-05 03:52:29 +00001103 if (HMI->first && HMI->second) {
Vikram S. Adve3bf08922003-07-10 19:42:55 +00001104 LiveRange *L = HMI->second; // get the LiveRange
1105 if (L->isMarkedForSpill()) { // NOTE: allocating size of long Type **
Chris Lattnera1e51ff2004-08-18 18:13:37 +00001106 int stackOffset = MF->getInfo<SparcV9FunctionInfo>()->allocateSpilledValue(Type::LongTy);
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001107 L->setSpillOffFromFP(stackOffset);
1108 if (DEBUG_RA)
Chris Lattnerc083dcc2003-09-01 20:05:47 +00001109 std::cerr << " LR# " << L->getUserIGNode()->getIndex()
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001110 << ": stack-offset = " << stackOffset << "\n";
1111 }
Chris Lattner37730942002-02-05 03:52:29 +00001112 }
1113 } // for all LR's in hash map
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001114}
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001115
Brian Gaeke874f4232003-09-21 02:50:21 +00001116
Brian Gaeke21390412003-11-10 00:05:26 +00001117void PhyRegAlloc::saveStateForValue (std::vector<AllocInfo> &state,
Brian Gaeke54a76b82004-03-08 23:22:02 +00001118 const Value *V, int Insn, int Opnd) {
Brian Gaeke21390412003-11-10 00:05:26 +00001119 LiveRangeMapType::const_iterator HMI = LRI->getLiveRangeMap ()->find (V);
1120 LiveRangeMapType::const_iterator HMIEnd = LRI->getLiveRangeMap ()->end ();
1121 AllocInfo::AllocStateTy AllocState = AllocInfo::NotAllocated;
1122 int Placement = -1;
1123 if ((HMI != HMIEnd) && HMI->second) {
1124 LiveRange *L = HMI->second;
1125 assert ((L->hasColor () || L->isMarkedForSpill ())
1126 && "Live range exists but not colored or spilled");
1127 if (L->hasColor ()) {
1128 AllocState = AllocInfo::Allocated;
1129 Placement = MRI.getUnifiedRegNum (L->getRegClassID (),
1130 L->getColor ());
1131 } else if (L->isMarkedForSpill ()) {
1132 AllocState = AllocInfo::Spilled;
1133 assert (L->hasSpillOffset ()
1134 && "Live range marked for spill but has no spill offset");
1135 Placement = L->getSpillOffFromFP ();
1136 }
1137 }
1138 state.push_back (AllocInfo (Insn, Opnd, AllocState, Placement));
1139}
1140
1141
Brian Gaekeaf843702003-10-22 20:22:53 +00001142/// Save the global register allocation decisions made by the register
1143/// allocator so that they can be accessed later (sort of like "poor man's
1144/// debug info").
1145///
1146void PhyRegAlloc::saveState () {
Brian Gaeke537132b2003-10-23 20:32:55 +00001147 std::vector<AllocInfo> &state = FnAllocState[Fn];
Brian Gaeke54a76b82004-03-08 23:22:02 +00001148 unsigned ArgNum = 0;
1149 // Arguments encoded as instruction # -1
1150 for (Function::const_aiterator i=Fn->abegin (), e=Fn->aend (); i != e; ++i) {
1151 const Argument *Arg = &*i;
1152 saveStateForValue (state, Arg, -1, ArgNum);
1153 ++ArgNum;
1154 }
Brian Gaeke25d4b542004-05-30 07:08:43 +00001155 unsigned InstCount = 0;
Brian Gaeke54a76b82004-03-08 23:22:02 +00001156 // Instructions themselves encoded as operand # -1
Brian Gaeke3ceac852003-10-30 21:21:33 +00001157 for (const_inst_iterator II=inst_begin (Fn), IE=inst_end (Fn); II!=IE; ++II){
Brian Gaeke25d4b542004-05-30 07:08:43 +00001158 const Instruction *Inst = &*II;
1159 saveStateForValue (state, Inst, InstCount, -1);
1160 if (isa<PHINode> (Inst)) {
1161 MachineCodeForInstruction &MCforPN = MachineCodeForInstruction::get(Inst);
1162 // Last instr should be the copy...figure out what reg it is reading from
1163 if (Value *PhiCpRes = MCforPN.back()->getOperand(0).getVRegValueOrNull()){
1164 if (DEBUG_RA)
1165 std::cerr << "Found Phi copy result: " << PhiCpRes->getName()
1166 << " in: " << *MCforPN.back() << "\n";
1167 saveStateForValue (state, PhiCpRes, InstCount, -2);
1168 }
Brian Gaeke6a256cc2003-09-24 18:08:54 +00001169 }
Brian Gaeke25d4b542004-05-30 07:08:43 +00001170 ++InstCount;
Brian Gaeke3ceac852003-10-30 21:21:33 +00001171 }
Brian Gaeke6a256cc2003-09-24 18:08:54 +00001172}
1173
Brian Gaeke537132b2003-10-23 20:32:55 +00001174
Brian Gaeke6a256cc2003-09-24 18:08:54 +00001175bool PhyRegAlloc::doFinalization (Module &M) {
Brian Gaekecf68bd52004-03-11 06:45:52 +00001176 if (SaveRegAllocState) finishSavingState (M);
1177 return false;
1178}
1179
1180
1181/// Finish the job of saveState(), by collapsing FnAllocState into an LLVM
1182/// Constant and stuffing it inside the Module.
1183///
1184/// FIXME: There should be other, better ways of storing the saved
1185/// state; this one is cumbersome and does not work well with the JIT.
1186///
1187void PhyRegAlloc::finishSavingState (Module &M) {
Brian Gaekec760d642004-03-11 19:46:30 +00001188 if (DEBUG_RA)
1189 std::cerr << "---- Saving reg. alloc state; SaveStateToModule = "
1190 << SaveStateToModule << " ----\n";
Brian Gaeke6a256cc2003-09-24 18:08:54 +00001191
Brian Gaekecce4e7a2003-11-04 18:25:56 +00001192 // If saving state into the module, just copy new elements to the
1193 // correct global.
Brian Gaeke8fc49342003-10-24 21:21:58 +00001194 if (!SaveStateToModule) {
1195 ExportedFnAllocState = FnAllocState;
Brian Gaekecce4e7a2003-11-04 18:25:56 +00001196 // FIXME: should ONLY copy new elements in FnAllocState
Brian Gaekecf68bd52004-03-11 06:45:52 +00001197 return;
Brian Gaeke8fc49342003-10-24 21:21:58 +00001198 }
1199
Brian Gaeke6a256cc2003-09-24 18:08:54 +00001200 // Convert FnAllocState to a single Constant array and add it
1201 // to the Module.
1202 ArrayType *AT = ArrayType::get (AllocInfo::getConstantType (), 0);
1203 std::vector<const Type *> TV;
1204 TV.push_back (Type::UIntTy);
1205 TV.push_back (AT);
1206 PointerType *PT = PointerType::get (StructType::get (TV));
1207
1208 std::vector<Constant *> allstate;
1209 for (Module::iterator I = M.begin (), E = M.end (); I != E; ++I) {
1210 Function *F = I;
Brian Gaeke55766e12003-11-04 22:42:41 +00001211 if (F->isExternal ()) continue;
Brian Gaeke6a256cc2003-09-24 18:08:54 +00001212 if (FnAllocState.find (F) == FnAllocState.end ()) {
1213 allstate.push_back (ConstantPointerNull::get (PT));
1214 } else {
Brian Gaeke537132b2003-10-23 20:32:55 +00001215 std::vector<AllocInfo> &state = FnAllocState[F];
Brian Gaeke60a3c552003-10-22 20:44:23 +00001216
1217 // Convert state into an LLVM ConstantArray, and put it in a
1218 // ConstantStruct (named S) along with its size.
Brian Gaeke537132b2003-10-23 20:32:55 +00001219 std::vector<Constant *> stateConstants;
1220 for (unsigned i = 0, s = state.size (); i != s; ++i)
1221 stateConstants.push_back (state[i].toConstant ());
1222 unsigned Size = stateConstants.size ();
Brian Gaeke60a3c552003-10-22 20:44:23 +00001223 ArrayType *AT = ArrayType::get (AllocInfo::getConstantType (), Size);
1224 std::vector<const Type *> TV;
1225 TV.push_back (Type::UIntTy);
1226 TV.push_back (AT);
1227 StructType *ST = StructType::get (TV);
1228 std::vector<Constant *> CV;
1229 CV.push_back (ConstantUInt::get (Type::UIntTy, Size));
Brian Gaeke537132b2003-10-23 20:32:55 +00001230 CV.push_back (ConstantArray::get (AT, stateConstants));
Brian Gaeke60a3c552003-10-22 20:44:23 +00001231 Constant *S = ConstantStruct::get (ST, CV);
1232
Brian Gaeke6a256cc2003-09-24 18:08:54 +00001233 GlobalVariable *GV =
Brian Gaeke60a3c552003-10-22 20:44:23 +00001234 new GlobalVariable (ST, true,
1235 GlobalValue::InternalLinkage, S,
Brian Gaeke6a256cc2003-09-24 18:08:54 +00001236 F->getName () + ".regAllocState", &M);
Brian Gaeke60a3c552003-10-22 20:44:23 +00001237
Brian Gaeke21390412003-11-10 00:05:26 +00001238 // Have: { uint, [Size x { uint, int, uint, int }] } *
1239 // Cast it to: { uint, [0 x { uint, int, uint, int }] } *
Reid Spencer518310c2004-07-18 00:44:37 +00001240 Constant *CE = ConstantExpr::getCast (GV, PT);
Brian Gaeke6a256cc2003-09-24 18:08:54 +00001241 allstate.push_back (CE);
1242 }
1243 }
1244
1245 unsigned Size = allstate.size ();
1246 // Final structure type is:
Brian Gaeke21390412003-11-10 00:05:26 +00001247 // { uint, [Size x { uint, [0 x { uint, int, uint, int }] } *] }
Brian Gaeke6a256cc2003-09-24 18:08:54 +00001248 std::vector<const Type *> TV2;
1249 TV2.push_back (Type::UIntTy);
1250 ArrayType *AT2 = ArrayType::get (PT, Size);
1251 TV2.push_back (AT2);
1252 StructType *ST2 = StructType::get (TV2);
1253 std::vector<Constant *> CV2;
1254 CV2.push_back (ConstantUInt::get (Type::UIntTy, Size));
1255 CV2.push_back (ConstantArray::get (AT2, allstate));
Brian Gaekee9414ca2003-11-10 07:12:01 +00001256 new GlobalVariable (ST2, true, GlobalValue::ExternalLinkage,
Brian Gaeke6a256cc2003-09-24 18:08:54 +00001257 ConstantStruct::get (ST2, CV2), "_llvm_regAllocState",
1258 &M);
Brian Gaeke6a256cc2003-09-24 18:08:54 +00001259}
1260
1261
Brian Gaekeaf843702003-10-22 20:22:53 +00001262/// Allocate registers for the machine code previously generated for F using
1263/// the graph-coloring algorithm.
1264///
Brian Gaeke4efe3422003-09-21 01:23:46 +00001265bool PhyRegAlloc::runOnFunction (Function &F) {
1266 if (DEBUG_RA)
1267 std::cerr << "\n********* Function "<< F.getName () << " ***********\n";
1268
1269 Fn = &F;
1270 MF = &MachineFunction::get (Fn);
1271 LVI = &getAnalysis<FunctionLiveVarInfo> ();
1272 LRI = new LiveRangeInfo (Fn, TM, RegClassList);
1273 LoopDepthCalc = &getAnalysis<LoopInfo> ();
1274
1275 // Create each RegClass for the target machine and add it to the
1276 // RegClassList. This must be done before calling constructLiveRanges().
1277 for (unsigned rc = 0; rc != NumOfRegClasses; ++rc)
Chris Lattnerd029cd22004-06-02 05:55:25 +00001278 RegClassList.push_back (new RegClass (Fn, TM.getRegInfo(),
1279 MRI.getMachineRegClass(rc)));
Brian Gaeke4efe3422003-09-21 01:23:46 +00001280
1281 LRI->constructLiveRanges(); // create LR info
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001282 if (DEBUG_RA >= RA_DEBUG_LiveRanges)
Brian Gaeke4efe3422003-09-21 01:23:46 +00001283 LRI->printLiveRanges();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001284
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001285 createIGNodeListsAndIGs(); // create IGNode list and IGs
1286
1287 buildInterferenceGraphs(); // build IGs in all reg classes
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001288
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001289 if (DEBUG_RA >= RA_DEBUG_LiveRanges) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001290 // print all LRs in all reg classes
Chris Lattner7e708292002-06-25 16:13:24 +00001291 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
1292 RegClassList[rc]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001293
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001294 // print IGs in all register classes
Chris Lattner7e708292002-06-25 16:13:24 +00001295 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
1296 RegClassList[rc]->printIG();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001297 }
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001298
Brian Gaeke4efe3422003-09-21 01:23:46 +00001299 LRI->coalesceLRs(); // coalesce all live ranges
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +00001300
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001301 if (DEBUG_RA >= RA_DEBUG_LiveRanges) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001302 // print all LRs in all reg classes
Chris Lattnerf726e772002-10-28 19:22:04 +00001303 for (unsigned rc=0; rc < NumOfRegClasses; rc++)
1304 RegClassList[rc]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001305
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001306 // print IGs in all register classes
Chris Lattnerf726e772002-10-28 19:22:04 +00001307 for (unsigned rc=0; rc < NumOfRegClasses; rc++)
1308 RegClassList[rc]->printIG();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001309 }
1310
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001311 // mark un-usable suggested color before graph coloring algorithm.
1312 // When this is done, the graph coloring algo will not reserve
1313 // suggested color unnecessarily - they can be used by another LR
1314 markUnusableSugColors();
1315
1316 // color all register classes using the graph coloring algo
Chris Lattner7e708292002-06-25 16:13:24 +00001317 for (unsigned rc=0; rc < NumOfRegClasses ; rc++)
Chris Lattnerf726e772002-10-28 19:22:04 +00001318 RegClassList[rc]->colorAllRegs();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001319
Misha Brukman37f92e22003-09-11 22:34:13 +00001320 // After graph coloring, if some LRs did not receive a color (i.e, spilled)
1321 // a position for such spilled LRs
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001322 allocateStackSpace4SpilledLRs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001323
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001324 // Reset the temp. area on the stack before use by the first instruction.
1325 // This will also happen after updating each instruction.
Chris Lattnera1e51ff2004-08-18 18:13:37 +00001326 MF->getInfo<SparcV9FunctionInfo>()->popAllTempValues();
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001327
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001328 // color incoming args - if the correct color was not received
1329 // insert code to copy to the correct register
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001330 colorIncomingArgs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001331
Brian Gaeke6a256cc2003-09-24 18:08:54 +00001332 // Save register allocation state for this function in a Constant.
Brian Gaeke16ca1942004-08-06 19:11:43 +00001333 if (SaveRegAllocState)
Brian Gaeke6a256cc2003-09-24 18:08:54 +00001334 saveState();
1335
Brian Gaeke60a3c552003-10-22 20:44:23 +00001336 // Now update the machine code with register names and add any additional
1337 // code inserted by the register allocator to the instruction stream.
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001338 updateMachineCode();
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001339
Brian Gaeke16ca1942004-08-06 19:11:43 +00001340 if (SaveRegAllocState && !SaveStateToModule)
1341 finishSavingState (const_cast<Module&> (*Fn->getParent ()));
Brian Gaekea7afac22004-05-30 04:22:24 +00001342
Chris Lattner045e7c82001-09-19 16:26:23 +00001343 if (DEBUG_RA) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +00001344 std::cerr << "\n**** Machine Code After Register Allocation:\n\n";
Brian Gaeke4efe3422003-09-21 01:23:46 +00001345 MF->dump();
Chris Lattner045e7c82001-09-19 16:26:23 +00001346 }
Brian Gaeke4efe3422003-09-21 01:23:46 +00001347
1348 // Tear down temporary data structures
1349 for (unsigned rc = 0; rc < NumOfRegClasses; ++rc)
1350 delete RegClassList[rc];
1351 RegClassList.clear ();
1352 AddedInstrMap.clear ();
1353 OperandsColoredMap.clear ();
1354 ScratchRegsUsed.clear ();
1355 AddedInstrAtEntry.clear ();
1356 delete LRI;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001357
Brian Gaeke4efe3422003-09-21 01:23:46 +00001358 if (DEBUG_RA) std::cerr << "\nRegister allocation complete!\n";
1359 return false; // Function was not modified
1360}
Brian Gaeked0fde302003-11-11 22:41:34 +00001361
1362} // End llvm namespace