blob: 616b012d77edc0b92988dde93cb173aeb3ddb386 [file] [log] [blame]
Chris Lattner179cdfb2002-08-09 20:08:03 +00001//===-- PhyRegAlloc.cpp ---------------------------------------------------===//
Vikram S. Adve12af1642001-11-08 04:48:50 +00002//
Brian Gaeke222bd532003-09-24 18:16:23 +00003// Traditional graph-coloring global register allocator currently used
4// by the SPARC back-end.
5//
6// NOTE: This register allocator has some special support
7// for the Reoptimizer, such as not saving some registers on calls to
8// the first-level instrumentation function.
9//
10// NOTE 2: This register allocator can save its state in a global
11// variable in the module it's working on. This feature is not
12// thread-safe; if you have doubts, leave it turned off.
Chris Lattner179cdfb2002-08-09 20:08:03 +000013//
14//===----------------------------------------------------------------------===//
Ruchira Sasanka8e604792001-09-14 21:18:34 +000015
Chris Lattner70b2f562003-09-01 20:09:04 +000016#include "PhyRegAlloc.h"
Chris Lattner4309e732003-01-15 19:57:07 +000017#include "RegAllocCommon.h"
Chris Lattner9d4ed152003-01-15 21:14:01 +000018#include "RegClass.h"
Chris Lattnerc083dcc2003-09-01 20:05:47 +000019#include "IGNode.h"
Chris Lattner797c1362003-09-30 20:13:59 +000020#include "llvm/CodeGen/FunctionLiveVarInfo.h"
21#include "llvm/CodeGen/InstrSelection.h"
Brian Gaeke874f4232003-09-21 02:50:21 +000022#include "llvm/CodeGen/MachineInstr.h"
Chris Lattnerf6ee49f2003-01-15 18:08:07 +000023#include "llvm/CodeGen/MachineInstrBuilder.h"
Vikram S. Advedabb41d2002-05-19 15:29:31 +000024#include "llvm/CodeGen/MachineInstrAnnot.h"
Misha Brukmanfce11432002-10-28 00:28:31 +000025#include "llvm/CodeGen/MachineFunction.h"
Chris Lattnere90fcb72002-12-28 20:35:34 +000026#include "llvm/CodeGen/MachineFunctionInfo.h"
Chris Lattner797c1362003-09-30 20:13:59 +000027#include "llvm/CodeGen/Passes.h"
Chris Lattner14ab1ce2002-02-04 17:48:00 +000028#include "llvm/Analysis/LoopInfo.h"
Chris Lattner3501fea2003-01-14 22:00:31 +000029#include "llvm/Target/TargetInstrInfo.h"
Chris Lattner37730942002-02-05 03:52:29 +000030#include "llvm/Type.h"
Vikram S. Advedabb41d2002-05-19 15:29:31 +000031#include "llvm/iOther.h"
Brian Gaeke6a256cc2003-09-24 18:08:54 +000032#include "llvm/DerivedTypes.h"
33#include "llvm/Constants.h"
Brian Gaeke6a256cc2003-09-24 18:08:54 +000034#include "llvm/Module.h"
Chris Lattner797c1362003-09-30 20:13:59 +000035#include "llvm/Support/InstIterator.h"
Vikram S. Advef5af6362002-07-08 23:15:32 +000036#include "Support/STLExtras.h"
Vikram S. Advefeb32982003-08-12 22:22:24 +000037#include "Support/SetOperations.h"
Chris Lattner4bc23482002-09-15 07:07:55 +000038#include "Support/CommandLine.h"
Brian Gaekebd353fb2003-09-21 03:57:37 +000039#include <cmath>
Vikram S. Adve12af1642001-11-08 04:48:50 +000040
Chris Lattner70e60cb2002-05-22 17:08:27 +000041RegAllocDebugLevel_t DEBUG_RA;
Vikram S. Adve39c94e12002-09-14 23:05:33 +000042
Chris Lattner5ff62e92002-07-22 02:10:13 +000043static cl::opt<RegAllocDebugLevel_t, true>
44DRA_opt("dregalloc", cl::Hidden, cl::location(DEBUG_RA),
45 cl::desc("enable register allocation debugging information"),
46 cl::values(
Vikram S. Adve39c94e12002-09-14 23:05:33 +000047 clEnumValN(RA_DEBUG_None , "n", "disable debug output"),
48 clEnumValN(RA_DEBUG_Results, "y", "debug output for allocation results"),
49 clEnumValN(RA_DEBUG_Coloring, "c", "debug output for graph coloring step"),
50 clEnumValN(RA_DEBUG_Interference,"ig","debug output for interference graphs"),
51 clEnumValN(RA_DEBUG_LiveRanges , "lr","debug output for live ranges"),
52 clEnumValN(RA_DEBUG_Verbose, "v", "extra debug output"),
Chris Lattner5ff62e92002-07-22 02:10:13 +000053 0));
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000054
Brian Gaeke59b1c562003-09-24 17:50:28 +000055static cl::opt<bool>
56SaveRegAllocState("save-ra-state", cl::Hidden,
57 cl::desc("write reg. allocator state into module"));
58
Brian Gaekebf3c4cf2003-08-14 06:09:32 +000059FunctionPass *getRegisterAllocator(TargetMachine &T) {
Brian Gaeke4efe3422003-09-21 01:23:46 +000060 return new PhyRegAlloc (T);
Chris Lattner2f9b28e2002-02-04 15:54:09 +000061}
Chris Lattner6dd98a62002-02-04 00:33:08 +000062
Chris Lattner8474f6f2003-09-23 15:13:04 +000063void PhyRegAlloc::getAnalysisUsage(AnalysisUsage &AU) const {
64 AU.addRequired<LoopInfo> ();
65 AU.addRequired<FunctionLiveVarInfo> ();
66}
67
68
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000069
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000070//----------------------------------------------------------------------------
Misha Brukman37f92e22003-09-11 22:34:13 +000071// This method initially creates interference graphs (one in each reg class)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000072// and IGNodeList (one in each IG). The actual nodes will be pushed later.
73//----------------------------------------------------------------------------
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000074void PhyRegAlloc::createIGNodeListsAndIGs() {
Chris Lattnerc083dcc2003-09-01 20:05:47 +000075 if (DEBUG_RA >= RA_DEBUG_LiveRanges) std::cerr << "Creating LR lists ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +000076
77 // hash map iterator
Brian Gaeke4efe3422003-09-21 01:23:46 +000078 LiveRangeMapType::const_iterator HMI = LRI->getLiveRangeMap()->begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +000079
80 // hash map end
Brian Gaeke4efe3422003-09-21 01:23:46 +000081 LiveRangeMapType::const_iterator HMIEnd = LRI->getLiveRangeMap()->end();
Ruchira Sasanka8e604792001-09-14 21:18:34 +000082
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000083 for (; HMI != HMIEnd ; ++HMI ) {
84 if (HMI->first) {
85 LiveRange *L = HMI->second; // get the LiveRange
86 if (!L) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +000087 if (DEBUG_RA)
Chris Lattnerc083dcc2003-09-01 20:05:47 +000088 std::cerr << "\n**** ?!?WARNING: NULL LIVE RANGE FOUND FOR: "
Vikram S. Adve39c94e12002-09-14 23:05:33 +000089 << RAV(HMI->first) << "****\n";
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000090 continue;
91 }
Vikram S. Adve39c94e12002-09-14 23:05:33 +000092
93 // if the Value * is not null, and LR is not yet written to the IGNodeList
Chris Lattner7e708292002-06-25 16:13:24 +000094 if (!(L->getUserIGNode()) ) {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000095 RegClass *const RC = // RegClass of first value in the LR
Brian Gaeke59b1c562003-09-24 17:50:28 +000096 RegClassList[ L->getRegClassID() ];
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000097 RC->addLRToIG(L); // add this LR to an IG
98 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +000099 }
100 }
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000101
102 // init RegClassList
Chris Lattner7e708292002-06-25 16:13:24 +0000103 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000104 RegClassList[rc]->createInterferenceGraph();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000105
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000106 if (DEBUG_RA >= RA_DEBUG_LiveRanges) std::cerr << "LRLists Created!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000107}
108
109
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000110//----------------------------------------------------------------------------
111// This method will add all interferences at for a given instruction.
Misha Brukman37f92e22003-09-11 22:34:13 +0000112// Interference occurs only if the LR of Def (Inst or Arg) is of the same reg
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000113// class as that of live var. The live var passed to this function is the
114// LVset AFTER the instruction
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000115//----------------------------------------------------------------------------
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000116
Chris Lattner296b7732002-02-05 02:52:05 +0000117void PhyRegAlloc::addInterference(const Value *Def,
118 const ValueSet *LVSet,
119 bool isCallInst) {
Chris Lattner296b7732002-02-05 02:52:05 +0000120 ValueSet::const_iterator LIt = LVSet->begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000121
122 // get the live range of instruction
Brian Gaeke4efe3422003-09-21 01:23:46 +0000123 const LiveRange *const LROfDef = LRI->getLiveRangeForValue( Def );
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000124
125 IGNode *const IGNodeOfDef = LROfDef->getUserIGNode();
126 assert( IGNodeOfDef );
127
128 RegClass *const RCOfDef = LROfDef->getRegClass();
129
130 // for each live var in live variable set
Chris Lattner7e708292002-06-25 16:13:24 +0000131 for ( ; LIt != LVSet->end(); ++LIt) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000132
Vikram S. Advef5af6362002-07-08 23:15:32 +0000133 if (DEBUG_RA >= RA_DEBUG_Verbose)
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000134 std::cerr << "< Def=" << RAV(Def) << ", Lvar=" << RAV(*LIt) << "> ";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000135
136 // get the live range corresponding to live var
Brian Gaeke4efe3422003-09-21 01:23:46 +0000137 LiveRange *LROfVar = LRI->getLiveRangeForValue(*LIt);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000138
139 // LROfVar can be null if it is a const since a const
140 // doesn't have a dominating def - see Assumptions above
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000141 if (LROfVar)
142 if (LROfDef != LROfVar) // do not set interf for same LR
143 if (RCOfDef == LROfVar->getRegClass()) // 2 reg classes are the same
144 RCOfDef->setInterference( LROfDef, LROfVar);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000145 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000146}
147
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000148
149//----------------------------------------------------------------------------
150// For a call instruction, this method sets the CallInterference flag in
151// the LR of each variable live int the Live Variable Set live after the
152// call instruction (except the return value of the call instruction - since
153// the return value does not interfere with that call itself).
154//----------------------------------------------------------------------------
155
156void PhyRegAlloc::setCallInterferences(const MachineInstr *MInst,
Chris Lattner296b7732002-02-05 02:52:05 +0000157 const ValueSet *LVSetAft) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000158 if (DEBUG_RA >= RA_DEBUG_Interference)
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000159 std::cerr << "\n For call inst: " << *MInst;
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000160
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000161 // for each live var in live variable set after machine inst
Vikram S. Adve65b2f402003-07-02 01:24:00 +0000162 for (ValueSet::const_iterator LIt = LVSetAft->begin(), LEnd = LVSetAft->end();
163 LIt != LEnd; ++LIt) {
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000164
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000165 // get the live range corresponding to live var
Brian Gaeke4efe3422003-09-21 01:23:46 +0000166 LiveRange *const LR = LRI->getLiveRangeForValue(*LIt );
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000167
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000168 // LR can be null if it is a const since a const
169 // doesn't have a dominating def - see Assumptions above
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000170 if (LR ) {
171 if (DEBUG_RA >= RA_DEBUG_Interference) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000172 std::cerr << "\n\tLR after Call: ";
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000173 printSet(*LR);
174 }
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000175 LR->setCallInterference();
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000176 if (DEBUG_RA >= RA_DEBUG_Interference) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000177 std::cerr << "\n ++After adding call interference for LR: " ;
Chris Lattner296b7732002-02-05 02:52:05 +0000178 printSet(*LR);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000179 }
180 }
181
182 }
183
Vikram S. Adve1a53f032002-03-31 18:54:37 +0000184 // Now find the LR of the return value of the call
185 // We do this because, we look at the LV set *after* the instruction
186 // to determine, which LRs must be saved across calls. The return value
187 // of the call is live in this set - but it does not interfere with call
188 // (i.e., we can allocate a volatile register to the return value)
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000189 CallArgsDescriptor* argDesc = CallArgsDescriptor::get(MInst);
190
191 if (const Value *RetVal = argDesc->getReturnValue()) {
Brian Gaeke4efe3422003-09-21 01:23:46 +0000192 LiveRange *RetValLR = LRI->getLiveRangeForValue( RetVal );
Vikram S. Adve1a53f032002-03-31 18:54:37 +0000193 assert( RetValLR && "No LR for RetValue of call");
194 RetValLR->clearCallInterference();
195 }
196
197 // If the CALL is an indirect call, find the LR of the function pointer.
198 // That has a call interference because it conflicts with outgoing args.
Chris Lattner7e708292002-06-25 16:13:24 +0000199 if (const Value *AddrVal = argDesc->getIndirectFuncPtr()) {
Brian Gaeke4efe3422003-09-21 01:23:46 +0000200 LiveRange *AddrValLR = LRI->getLiveRangeForValue( AddrVal );
Vikram S. Adve1a53f032002-03-31 18:54:37 +0000201 assert( AddrValLR && "No LR for indirect addr val of call");
202 AddrValLR->setCallInterference();
203 }
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000204}
205
206
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000207//----------------------------------------------------------------------------
208// This method will walk thru code and create interferences in the IG of
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000209// each RegClass. Also, this method calculates the spill cost of each
210// Live Range (it is done in this method to save another pass over the code).
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000211//----------------------------------------------------------------------------
Brian Gaeke43ce8fe2003-09-21 02:24:09 +0000212
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000213void PhyRegAlloc::buildInterferenceGraphs()
214{
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000215 if (DEBUG_RA >= RA_DEBUG_Interference)
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000216 std::cerr << "Creating interference graphs ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000217
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000218 unsigned BBLoopDepthCost;
Brian Gaeke4efe3422003-09-21 01:23:46 +0000219 for (MachineFunction::iterator BBI = MF->begin(), BBE = MF->end();
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000220 BBI != BBE; ++BBI) {
Chris Lattnerf726e772002-10-28 19:22:04 +0000221 const MachineBasicBlock &MBB = *BBI;
222 const BasicBlock *BB = MBB.getBasicBlock();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000223
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000224 // find the 10^(loop_depth) of this BB
Chris Lattnerf726e772002-10-28 19:22:04 +0000225 BBLoopDepthCost = (unsigned)pow(10.0, LoopDepthCalc->getLoopDepth(BB));
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000226
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000227 // get the iterator for machine instructions
Chris Lattnerf726e772002-10-28 19:22:04 +0000228 MachineBasicBlock::const_iterator MII = MBB.begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000229
230 // iterate over all the machine instructions in BB
Chris Lattnerf726e772002-10-28 19:22:04 +0000231 for ( ; MII != MBB.end(); ++MII) {
232 const MachineInstr *MInst = *MII;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000233
234 // get the LV set after the instruction
Chris Lattnerf726e772002-10-28 19:22:04 +0000235 const ValueSet &LVSetAI = LVI->getLiveVarSetAfterMInst(MInst, BB);
236 bool isCallInst = TM.getInstrInfo().isCall(MInst->getOpCode());
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000237
Chris Lattner7e708292002-06-25 16:13:24 +0000238 if (isCallInst ) {
Misha Brukman37f92e22003-09-11 22:34:13 +0000239 // set the isCallInterference flag of each live range which extends
240 // across this call instruction. This information is used by graph
241 // coloring algorithm to avoid allocating volatile colors to live ranges
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000242 // that span across calls (since they have to be saved/restored)
Chris Lattner748697d2002-02-05 04:20:12 +0000243 setCallInterferences(MInst, &LVSetAI);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000244 }
245
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000246 // iterate over all MI operands to find defs
Chris Lattner2f898d22002-02-05 06:02:59 +0000247 for (MachineInstr::const_val_op_iterator OpI = MInst->begin(),
248 OpE = MInst->end(); OpI != OpE; ++OpI) {
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000249 if (OpI.isDefOnly() || OpI.isDefAndUse()) // create a new LR since def
Chris Lattner748697d2002-02-05 04:20:12 +0000250 addInterference(*OpI, &LVSetAI, isCallInst);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000251
252 // Calculate the spill cost of each live range
Brian Gaeke4efe3422003-09-21 01:23:46 +0000253 LiveRange *LR = LRI->getLiveRangeForValue(*OpI);
Chris Lattner2f898d22002-02-05 06:02:59 +0000254 if (LR) LR->addSpillCost(BBLoopDepthCost);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000255 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000256
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000257 // if there are multiple defs in this instruction e.g. in SETX
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000258 if (TM.getInstrInfo().isPseudoInstr(MInst->getOpCode()))
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000259 addInterf4PseudoInstr(MInst);
260
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000261 // Also add interference for any implicit definitions in a machine
262 // instr (currently, only calls have this).
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000263 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000264 for (unsigned z=0; z < NumOfImpRefs; z++)
265 if (MInst->getImplicitOp(z).opIsDefOnly() ||
266 MInst->getImplicitOp(z).opIsDefAndUse())
267 addInterference( MInst->getImplicitRef(z), &LVSetAI, isCallInst );
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +0000268
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000269 } // for all machine instructions in BB
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000270 } // for all BBs in function
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000271
Misha Brukman37f92e22003-09-11 22:34:13 +0000272 // add interferences for function arguments. Since there are no explicit
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000273 // defs in the function for args, we have to add them manually
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000274 addInterferencesForArgs();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000275
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000276 if (DEBUG_RA >= RA_DEBUG_Interference)
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000277 std::cerr << "Interference graphs calculated!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000278}
279
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000280
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000281//--------------------------------------------------------------------------
Brian Gaeke43ce8fe2003-09-21 02:24:09 +0000282// Pseudo-instructions may be expanded to multiple instructions by the
283// assembler. Consequently, all the operands must get distinct registers.
284// Therefore, we mark all operands of a pseudo-instruction as interfering
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000285// with one another.
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000286//--------------------------------------------------------------------------
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000287
Brian Gaeke43ce8fe2003-09-21 02:24:09 +0000288void PhyRegAlloc::addInterf4PseudoInstr(const MachineInstr *MInst) {
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000289 bool setInterf = false;
290
Brian Gaeke43ce8fe2003-09-21 02:24:09 +0000291 // iterate over MI operands to find defs
Chris Lattner2f898d22002-02-05 06:02:59 +0000292 for (MachineInstr::const_val_op_iterator It1 = MInst->begin(),
293 ItE = MInst->end(); It1 != ItE; ++It1) {
Brian Gaeke4efe3422003-09-21 01:23:46 +0000294 const LiveRange *LROfOp1 = LRI->getLiveRangeForValue(*It1);
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000295 assert((LROfOp1 || !It1.isUseOnly())&&"No LR for Def in PSEUDO insruction");
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000296
Chris Lattner2f898d22002-02-05 06:02:59 +0000297 MachineInstr::const_val_op_iterator It2 = It1;
Chris Lattner7e708292002-06-25 16:13:24 +0000298 for (++It2; It2 != ItE; ++It2) {
Brian Gaeke4efe3422003-09-21 01:23:46 +0000299 const LiveRange *LROfOp2 = LRI->getLiveRangeForValue(*It2);
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000300
Chris Lattner2f898d22002-02-05 06:02:59 +0000301 if (LROfOp2) {
302 RegClass *RCOfOp1 = LROfOp1->getRegClass();
303 RegClass *RCOfOp2 = LROfOp2->getRegClass();
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000304
Chris Lattner7e708292002-06-25 16:13:24 +0000305 if (RCOfOp1 == RCOfOp2 ){
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000306 RCOfOp1->setInterference( LROfOp1, LROfOp2 );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000307 setInterf = true;
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000308 }
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000309 } // if Op2 has a LR
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000310 } // for all other defs in machine instr
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000311 } // for all operands in an instruction
312
Chris Lattner2f898d22002-02-05 06:02:59 +0000313 if (!setInterf && MInst->getNumOperands() > 2) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000314 std::cerr << "\nInterf not set for any operand in pseudo instr:\n";
315 std::cerr << *MInst;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000316 assert(0 && "Interf not set for pseudo instr with > 2 operands" );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000317 }
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000318}
319
320
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000321//----------------------------------------------------------------------------
Brian Gaeke43ce8fe2003-09-21 02:24:09 +0000322// This method adds interferences for incoming arguments to a function.
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000323//----------------------------------------------------------------------------
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000324
Chris Lattner296b7732002-02-05 02:52:05 +0000325void PhyRegAlloc::addInterferencesForArgs() {
326 // get the InSet of root BB
Chris Lattnerf726e772002-10-28 19:22:04 +0000327 const ValueSet &InSet = LVI->getInSetOfBB(&Fn->front());
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000328
Chris Lattnerf726e772002-10-28 19:22:04 +0000329 for (Function::const_aiterator AI = Fn->abegin(); AI != Fn->aend(); ++AI) {
Chris Lattner7e708292002-06-25 16:13:24 +0000330 // add interferences between args and LVars at start
331 addInterference(AI, &InSet, false);
332
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000333 if (DEBUG_RA >= RA_DEBUG_Interference)
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000334 std::cerr << " - %% adding interference for argument " << RAV(AI) << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000335 }
336}
337
338
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000339//----------------------------------------------------------------------------
340// This method is called after register allocation is complete to set the
Misha Brukman37f92e22003-09-11 22:34:13 +0000341// allocated registers in the machine code. This code will add register numbers
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000342// to MachineOperands that contain a Value. Also it calls target specific
343// methods to produce caller saving instructions. At the end, it adds all
344// additional instructions produced by the register allocator to the
345// instruction stream.
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000346//----------------------------------------------------------------------------
Vikram S. Adve48762092002-04-25 04:34:15 +0000347
348//-----------------------------
349// Utility functions used below
350//-----------------------------
351inline void
Vikram S. Advecb202e32002-10-11 16:12:40 +0000352InsertBefore(MachineInstr* newMI,
Chris Lattnerf726e772002-10-28 19:22:04 +0000353 MachineBasicBlock& MBB,
Chris Lattner32be9f62002-10-28 01:41:27 +0000354 MachineBasicBlock::iterator& MII)
Vikram S. Advecb202e32002-10-11 16:12:40 +0000355{
Chris Lattnerf726e772002-10-28 19:22:04 +0000356 MII = MBB.insert(MII, newMI);
Vikram S. Advecb202e32002-10-11 16:12:40 +0000357 ++MII;
358}
359
360inline void
361InsertAfter(MachineInstr* newMI,
Chris Lattnerf726e772002-10-28 19:22:04 +0000362 MachineBasicBlock& MBB,
Chris Lattner32be9f62002-10-28 01:41:27 +0000363 MachineBasicBlock::iterator& MII)
Vikram S. Advecb202e32002-10-11 16:12:40 +0000364{
365 ++MII; // insert before the next instruction
Chris Lattnerf726e772002-10-28 19:22:04 +0000366 MII = MBB.insert(MII, newMI);
Vikram S. Advecb202e32002-10-11 16:12:40 +0000367}
368
369inline void
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000370DeleteInstruction(MachineBasicBlock& MBB,
371 MachineBasicBlock::iterator& MII)
372{
373 MII = MBB.erase(MII);
374}
375
376inline void
Vikram S. Advecb202e32002-10-11 16:12:40 +0000377SubstituteInPlace(MachineInstr* newMI,
Chris Lattnerf726e772002-10-28 19:22:04 +0000378 MachineBasicBlock& MBB,
Chris Lattner32be9f62002-10-28 01:41:27 +0000379 MachineBasicBlock::iterator MII)
Vikram S. Advecb202e32002-10-11 16:12:40 +0000380{
381 *MII = newMI;
382}
383
384inline void
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000385PrependInstructions(std::vector<MachineInstr *> &IBef,
Chris Lattnerf726e772002-10-28 19:22:04 +0000386 MachineBasicBlock& MBB,
Chris Lattner32be9f62002-10-28 01:41:27 +0000387 MachineBasicBlock::iterator& MII,
Vikram S. Adve48762092002-04-25 04:34:15 +0000388 const std::string& msg)
389{
390 if (!IBef.empty())
391 {
392 MachineInstr* OrigMI = *MII;
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000393 std::vector<MachineInstr *>::iterator AdIt;
Vikram S. Adve48762092002-04-25 04:34:15 +0000394 for (AdIt = IBef.begin(); AdIt != IBef.end() ; ++AdIt)
395 {
396 if (DEBUG_RA) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000397 if (OrigMI) std::cerr << "For MInst:\n " << *OrigMI;
398 std::cerr << msg << "PREPENDed instr:\n " << **AdIt << "\n";
Vikram S. Adve48762092002-04-25 04:34:15 +0000399 }
Chris Lattnerf726e772002-10-28 19:22:04 +0000400 InsertBefore(*AdIt, MBB, MII);
Vikram S. Adve48762092002-04-25 04:34:15 +0000401 }
402 }
403}
404
405inline void
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000406AppendInstructions(std::vector<MachineInstr *> &IAft,
Chris Lattnerf726e772002-10-28 19:22:04 +0000407 MachineBasicBlock& MBB,
Chris Lattner32be9f62002-10-28 01:41:27 +0000408 MachineBasicBlock::iterator& MII,
Vikram S. Adve48762092002-04-25 04:34:15 +0000409 const std::string& msg)
410{
411 if (!IAft.empty())
412 {
413 MachineInstr* OrigMI = *MII;
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000414 std::vector<MachineInstr *>::iterator AdIt;
Chris Lattner7e708292002-06-25 16:13:24 +0000415 for ( AdIt = IAft.begin(); AdIt != IAft.end() ; ++AdIt )
Vikram S. Adve48762092002-04-25 04:34:15 +0000416 {
Chris Lattner7e708292002-06-25 16:13:24 +0000417 if (DEBUG_RA) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000418 if (OrigMI) std::cerr << "For MInst:\n " << *OrigMI;
419 std::cerr << msg << "APPENDed instr:\n " << **AdIt << "\n";
Vikram S. Adve48762092002-04-25 04:34:15 +0000420 }
Chris Lattnerf726e772002-10-28 19:22:04 +0000421 InsertAfter(*AdIt, MBB, MII);
Vikram S. Adve48762092002-04-25 04:34:15 +0000422 }
423 }
424}
425
Brian Gaeke4efe3422003-09-21 01:23:46 +0000426bool PhyRegAlloc::markAllocatedRegs(MachineInstr* MInst)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000427{
Vikram S. Adve814030a2003-07-29 19:49:21 +0000428 bool instrNeedsSpills = false;
429
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000430 // First, set the registers for operands in the machine instruction
431 // if a register was successfully allocated. Do this first because we
432 // will need to know which registers are already used by this instr'n.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000433 for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum)
434 {
435 MachineOperand& Op = MInst->getOperand(OpNum);
436 if (Op.getType() == MachineOperand::MO_VirtualRegister ||
437 Op.getType() == MachineOperand::MO_CCRegister)
438 {
439 const Value *const Val = Op.getVRegValue();
Brian Gaeke4efe3422003-09-21 01:23:46 +0000440 if (const LiveRange* LR = LRI->getLiveRangeForValue(Val)) {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000441 // Remember if any operand needs spilling
442 instrNeedsSpills |= LR->isMarkedForSpill();
443
444 // An operand may have a color whether or not it needs spilling
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000445 if (LR->hasColor())
446 MInst->SetRegForOperand(OpNum,
Brian Gaeke59b1c562003-09-24 17:50:28 +0000447 MRI.getUnifiedRegNum(LR->getRegClassID(),
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000448 LR->getColor()));
Vikram S. Adve814030a2003-07-29 19:49:21 +0000449 }
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000450 }
451 } // for each operand
Vikram S. Adve814030a2003-07-29 19:49:21 +0000452
453 return instrNeedsSpills;
454}
455
456void PhyRegAlloc::updateInstruction(MachineBasicBlock::iterator& MII,
457 MachineBasicBlock &MBB)
458{
459 MachineInstr* MInst = *MII;
460 unsigned Opcode = MInst->getOpCode();
461
462 // Reset tmp stack positions so they can be reused for each machine instr.
Brian Gaeke4efe3422003-09-21 01:23:46 +0000463 MF->getInfo()->popAllTempValues();
Vikram S. Adve814030a2003-07-29 19:49:21 +0000464
465 // Mark the operands for which regs have been allocated.
Brian Gaeke4efe3422003-09-21 01:23:46 +0000466 bool instrNeedsSpills = markAllocatedRegs(*MII);
Vikram S. Adve814030a2003-07-29 19:49:21 +0000467
468#ifndef NDEBUG
469 // Mark that the operands have been updated. Later,
470 // setRelRegsUsedByThisInst() is called to find registers used by each
471 // MachineInst, and it should not be used for an instruction until
472 // this is done. This flag just serves as a sanity check.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000473 OperandsColoredMap[MInst] = true;
Vikram S. Adve814030a2003-07-29 19:49:21 +0000474#endif
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000475
Vikram S. Advebc001b22003-07-25 21:06:09 +0000476 // Now insert caller-saving code before/after the call.
477 // Do this before inserting spill code since some registers must be
478 // used by save/restore and spill code should not use those registers.
Vikram S. Advebc001b22003-07-25 21:06:09 +0000479 if (TM.getInstrInfo().isCall(Opcode)) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000480 AddedInstrns &AI = AddedInstrMap[MInst];
Vikram S. Adve814030a2003-07-29 19:49:21 +0000481 insertCallerSavingCode(AI.InstrnsBefore, AI.InstrnsAfter, MInst,
482 MBB.getBasicBlock());
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000483 }
Vikram S. Advebc001b22003-07-25 21:06:09 +0000484
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000485 // Now insert spill code for remaining operands not allocated to
486 // registers. This must be done even for call return instructions
487 // since those are not handled by the special code above.
Vikram S. Adve814030a2003-07-29 19:49:21 +0000488 if (instrNeedsSpills)
489 for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum)
490 {
491 MachineOperand& Op = MInst->getOperand(OpNum);
492 if (Op.getType() == MachineOperand::MO_VirtualRegister ||
493 Op.getType() == MachineOperand::MO_CCRegister)
494 {
495 const Value* Val = Op.getVRegValue();
Brian Gaeke4efe3422003-09-21 01:23:46 +0000496 if (const LiveRange *LR = LRI->getLiveRangeForValue(Val))
Vikram S. Adve814030a2003-07-29 19:49:21 +0000497 if (LR->isMarkedForSpill())
498 insertCode4SpilledLR(LR, MII, MBB, OpNum);
499 }
500 } // for each operand
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000501}
502
503void PhyRegAlloc::updateMachineCode()
504{
Chris Lattner7e708292002-06-25 16:13:24 +0000505 // Insert any instructions needed at method entry
Brian Gaeke4efe3422003-09-21 01:23:46 +0000506 MachineBasicBlock::iterator MII = MF->front().begin();
507 PrependInstructions(AddedInstrAtEntry.InstrnsBefore, MF->front(), MII,
Chris Lattner7e708292002-06-25 16:13:24 +0000508 "At function entry: \n");
509 assert(AddedInstrAtEntry.InstrnsAfter.empty() &&
510 "InstrsAfter should be unnecessary since we are just inserting at "
511 "the function entry point here.");
Vikram S. Adve48762092002-04-25 04:34:15 +0000512
Brian Gaeke4efe3422003-09-21 01:23:46 +0000513 for (MachineFunction::iterator BBI = MF->begin(), BBE = MF->end();
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000514 BBI != BBE; ++BBI) {
Vikram S. Advecb202e32002-10-11 16:12:40 +0000515
Chris Lattnerf726e772002-10-28 19:22:04 +0000516 MachineBasicBlock &MBB = *BBI;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000517
518 // Iterate over all machine instructions in BB and mark operands with
519 // their assigned registers or insert spill code, as appropriate.
520 // Also, fix operands of call/return instructions.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000521 for (MachineBasicBlock::iterator MII = MBB.begin(); MII != MBB.end(); ++MII)
Vikram S. Adve814030a2003-07-29 19:49:21 +0000522 if (! TM.getInstrInfo().isDummyPhiInstr((*MII)->getOpCode()))
523 updateInstruction(MII, MBB);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000524
525 // Now, move code out of delay slots of branches and returns if needed.
526 // (Also, move "after" code from calls to the last delay slot instruction.)
527 // Moving code out of delay slots is needed in 2 situations:
528 // (1) If this is a branch and it needs instructions inserted after it,
529 // move any existing instructions out of the delay slot so that the
530 // instructions can go into the delay slot. This only supports the
531 // case that #instrsAfter <= #delay slots.
532 //
533 // (2) If any instruction in the delay slot needs
534 // instructions inserted, move it out of the delay slot and before the
535 // branch because putting code before or after it would be VERY BAD!
536 //
537 // If the annul bit of the branch is set, neither of these is legal!
538 // If so, we need to handle spill differently but annulling is not yet used.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000539 for (MachineBasicBlock::iterator MII = MBB.begin();
540 MII != MBB.end(); ++MII)
541 if (unsigned delaySlots =
542 TM.getInstrInfo().getNumDelaySlots((*MII)->getOpCode()))
543 {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000544 MachineInstr *MInst = *MII, *DelaySlotMI = *(MII+1);
545
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000546 // Check the 2 conditions above:
547 // (1) Does a branch need instructions added after it?
548 // (2) O/w does delay slot instr. need instrns before or after?
Vikram S. Adve814030a2003-07-29 19:49:21 +0000549 bool isBranch = (TM.getInstrInfo().isBranch(MInst->getOpCode()) ||
550 TM.getInstrInfo().isReturn(MInst->getOpCode()));
551 bool cond1 = (isBranch &&
552 AddedInstrMap.count(MInst) &&
553 AddedInstrMap[MInst].InstrnsAfter.size() > 0);
554 bool cond2 = (AddedInstrMap.count(DelaySlotMI) &&
555 (AddedInstrMap[DelaySlotMI].InstrnsBefore.size() > 0 ||
556 AddedInstrMap[DelaySlotMI].InstrnsAfter.size() > 0));
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000557
558 if (cond1 || cond2)
559 {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000560 assert((MInst->getOpCodeFlags() & AnnulFlag) == 0 &&
561 "FIXME: Moving an annulled delay slot instruction!");
562 assert(delaySlots==1 &&
563 "InsertBefore does not yet handle >1 delay slots!");
564 InsertBefore(DelaySlotMI, MBB, MII); // MII pts back to branch
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000565
566 // In case (1), delete it and don't replace with anything!
567 // Otherwise (i.e., case (2) only) replace it with a NOP.
568 if (cond1) {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000569 DeleteInstruction(MBB, ++MII); // MII now points to next inst.
570 --MII; // reset MII for ++MII of loop
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000571 }
Vikram S. Adve814030a2003-07-29 19:49:21 +0000572 else
573 SubstituteInPlace(BuildMI(TM.getInstrInfo().getNOPOpCode(),1),
574 MBB, MII+1); // replace with NOP
575
576 if (DEBUG_RA) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000577 std::cerr << "\nRegAlloc: Moved instr. with added code: "
Vikram S. Adve814030a2003-07-29 19:49:21 +0000578 << *DelaySlotMI
579 << " out of delay slots of instr: " << *MInst;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000580 }
581 }
Vikram S. Adve814030a2003-07-29 19:49:21 +0000582 else
583 // For non-branch instr with delay slots (probably a call), move
584 // InstrAfter to the instr. in the last delay slot.
585 move2DelayedInstr(*MII, *(MII+delaySlots));
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000586 }
587
588 // Finally iterate over all instructions in BB and insert before/after
Vikram S. Advebc001b22003-07-25 21:06:09 +0000589 for (MachineBasicBlock::iterator MII=MBB.begin(); MII != MBB.end(); ++MII) {
Vikram S. Adve48762092002-04-25 04:34:15 +0000590 MachineInstr *MInst = *MII;
Vikram S. Advebc001b22003-07-25 21:06:09 +0000591
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000592 // do not process Phis
Vikram S. Advebc001b22003-07-25 21:06:09 +0000593 if (TM.getInstrInfo().isDummyPhiInstr(MInst->getOpCode()))
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000594 continue;
595
Vikram S. Advebc001b22003-07-25 21:06:09 +0000596 // if there are any added instructions...
Chris Lattner7e708292002-06-25 16:13:24 +0000597 if (AddedInstrMap.count(MInst)) {
Vikram S. Advebc001b22003-07-25 21:06:09 +0000598 AddedInstrns &CallAI = AddedInstrMap[MInst];
599
600#ifndef NDEBUG
Vikram S. Adve814030a2003-07-29 19:49:21 +0000601 bool isBranch = (TM.getInstrInfo().isBranch(MInst->getOpCode()) ||
602 TM.getInstrInfo().isReturn(MInst->getOpCode()));
603 assert((!isBranch ||
604 AddedInstrMap[MInst].InstrnsAfter.size() <=
605 TM.getInstrInfo().getNumDelaySlots(MInst->getOpCode())) &&
606 "Cannot put more than #delaySlots instrns after "
607 "branch or return! Need to handle temps differently.");
608#endif
609
610#ifndef NDEBUG
Vikram S. Advebc001b22003-07-25 21:06:09 +0000611 // Temporary sanity checking code to detect whether the same machine
612 // instruction is ever inserted twice before/after a call.
613 // I suspect this is happening but am not sure. --Vikram, 7/1/03.
Vikram S. Advebc001b22003-07-25 21:06:09 +0000614 std::set<const MachineInstr*> instrsSeen;
615 for (int i = 0, N = CallAI.InstrnsBefore.size(); i < N; ++i) {
616 assert(instrsSeen.count(CallAI.InstrnsBefore[i]) == 0 &&
617 "Duplicate machine instruction in InstrnsBefore!");
618 instrsSeen.insert(CallAI.InstrnsBefore[i]);
619 }
620 for (int i = 0, N = CallAI.InstrnsAfter.size(); i < N; ++i) {
621 assert(instrsSeen.count(CallAI.InstrnsAfter[i]) == 0 &&
622 "Duplicate machine instruction in InstrnsBefore/After!");
623 instrsSeen.insert(CallAI.InstrnsAfter[i]);
624 }
625#endif
626
627 // Now add the instructions before/after this MI.
628 // We do this here to ensure that spill for an instruction is inserted
629 // as close as possible to an instruction (see above insertCode4Spill)
Vikram S. Advebc001b22003-07-25 21:06:09 +0000630 if (! CallAI.InstrnsBefore.empty())
631 PrependInstructions(CallAI.InstrnsBefore, MBB, MII,"");
632
633 if (! CallAI.InstrnsAfter.empty())
634 AppendInstructions(CallAI.InstrnsAfter, MBB, MII,"");
635
636 } // if there are any added instructions
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000637 } // for each machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000638 }
639}
640
641
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000642//----------------------------------------------------------------------------
643// This method inserts spill code for AN operand whose LR was spilled.
644// This method may be called several times for a single machine instruction
645// if it contains many spilled operands. Each time it is called, it finds
646// a register which is not live at that instruction and also which is not
647// used by other spilled operands of the same instruction. Then it uses
Misha Brukman37f92e22003-09-11 22:34:13 +0000648// this register temporarily to accommodate the spilled value.
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000649//----------------------------------------------------------------------------
Vikram S. Advebc001b22003-07-25 21:06:09 +0000650
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000651void PhyRegAlloc::insertCode4SpilledLR(const LiveRange *LR,
Vikram S. Adve814030a2003-07-29 19:49:21 +0000652 MachineBasicBlock::iterator& MII,
653 MachineBasicBlock &MBB,
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000654 const unsigned OpNum) {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000655 MachineInstr *MInst = *MII;
656 const BasicBlock *BB = MBB.getBasicBlock();
657
Vikram S. Advead9c9782002-09-28 17:02:40 +0000658 assert((! TM.getInstrInfo().isCall(MInst->getOpCode()) || OpNum == 0) &&
659 "Outgoing arg of a call must be handled elsewhere (func arg ok)");
660 assert(! TM.getInstrInfo().isReturn(MInst->getOpCode()) &&
661 "Return value of a ret must be handled elsewhere");
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000662
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000663 MachineOperand& Op = MInst->getOperand(OpNum);
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000664 bool isDef = Op.opIsDefOnly();
665 bool isDefAndUse = Op.opIsDefAndUse();
Vikram S. Advebc001b22003-07-25 21:06:09 +0000666 unsigned RegType = MRI.getRegTypeForLR(LR);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000667 int SpillOff = LR->getSpillOffFromFP();
668 RegClass *RC = LR->getRegClass();
Vikram S. Adve814030a2003-07-29 19:49:21 +0000669
670 // Get the live-variable set to find registers free before this instr.
Vikram S. Advefeb32982003-08-12 22:22:24 +0000671 const ValueSet &LVSetBef = LVI->getLiveVarSetBeforeMInst(MInst, BB);
672
673#ifndef NDEBUG
674 // If this instr. is in the delay slot of a branch or return, we need to
675 // include all live variables before that branch or return -- we don't want to
676 // trample those! Verify that the set is included in the LV set before MInst.
Vikram S. Adve814030a2003-07-29 19:49:21 +0000677 if (MII != MBB.begin()) {
678 MachineInstr *PredMI = *(MII-1);
Vikram S. Advefeb32982003-08-12 22:22:24 +0000679 if (unsigned DS = TM.getInstrInfo().getNumDelaySlots(PredMI->getOpCode()))
680 assert(set_difference(LVI->getLiveVarSetBeforeMInst(PredMI), LVSetBef)
681 .empty() && "Live-var set before branch should be included in "
682 "live-var set of each delay slot instruction!");
Vikram S. Adve814030a2003-07-29 19:49:21 +0000683 }
Vikram S. Advefeb32982003-08-12 22:22:24 +0000684#endif
Vikram S. Adve00521d72001-11-12 23:26:35 +0000685
Brian Gaeke4efe3422003-09-21 01:23:46 +0000686 MF->getInfo()->pushTempValue(MRI.getSpilledRegSize(RegType) );
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000687
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000688 std::vector<MachineInstr*> MIBef, MIAft;
689 std::vector<MachineInstr*> AdIMid;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000690
Vikram S. Adve3bf08922003-07-10 19:42:55 +0000691 // Choose a register to hold the spilled value, if one was not preallocated.
692 // This may insert code before and after MInst to free up the value. If so,
693 // this code should be first/last in the spill sequence before/after MInst.
694 int TmpRegU=(LR->hasColor()
Brian Gaeke59b1c562003-09-24 17:50:28 +0000695 ? MRI.getUnifiedRegNum(LR->getRegClassID(),LR->getColor())
Vikram S. Adve3bf08922003-07-10 19:42:55 +0000696 : getUsableUniRegAtMI(RegType, &LVSetBef, MInst, MIBef,MIAft));
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000697
Vikram S. Advef5af6362002-07-08 23:15:32 +0000698 // Set the operand first so that it this register does not get used
699 // as a scratch register for later calls to getUsableUniRegAtMI below
700 MInst->SetRegForOperand(OpNum, TmpRegU);
701
702 // get the added instructions for this instruction
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000703 AddedInstrns &AI = AddedInstrMap[MInst];
Vikram S. Advef5af6362002-07-08 23:15:32 +0000704
705 // We may need a scratch register to copy the spilled value to/from memory.
706 // This may itself have to insert code to free up a scratch register.
707 // Any such code should go before (after) the spill code for a load (store).
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000708 // The scratch reg is not marked as used because it is only used
709 // for the copy and not used across MInst.
Vikram S. Advef5af6362002-07-08 23:15:32 +0000710 int scratchRegType = -1;
711 int scratchReg = -1;
712 if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType))
713 {
Chris Lattner27a08932002-10-22 23:16:21 +0000714 scratchReg = getUsableUniRegAtMI(scratchRegType, &LVSetBef,
715 MInst, MIBef, MIAft);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000716 assert(scratchReg != MRI.getInvalidRegNum());
Vikram S. Advef5af6362002-07-08 23:15:32 +0000717 }
718
719 if (!isDef || isDefAndUse) {
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000720 // for a USE, we have to load the value of LR from stack to a TmpReg
721 // and use the TmpReg as one operand of instruction
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000722
Vikram S. Advef5af6362002-07-08 23:15:32 +0000723 // actual loading instruction(s)
Vikram S. Adve814030a2003-07-29 19:49:21 +0000724 MRI.cpMem2RegMI(AdIMid, MRI.getFramePointer(), SpillOff, TmpRegU,
725 RegType, scratchReg);
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000726
Vikram S. Advef5af6362002-07-08 23:15:32 +0000727 // the actual load should be after the instructions to free up TmpRegU
728 MIBef.insert(MIBef.end(), AdIMid.begin(), AdIMid.end());
729 AdIMid.clear();
730 }
731
Vikram S. Adve3bf08922003-07-10 19:42:55 +0000732 if (isDef || isDefAndUse) { // if this is a Def
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000733 // for a DEF, we have to store the value produced by this instruction
734 // on the stack position allocated for this LR
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000735
Vikram S. Advef5af6362002-07-08 23:15:32 +0000736 // actual storing instruction(s)
Vikram S. Adve814030a2003-07-29 19:49:21 +0000737 MRI.cpReg2MemMI(AdIMid, TmpRegU, MRI.getFramePointer(), SpillOff,
738 RegType, scratchReg);
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000739
Vikram S. Advef5af6362002-07-08 23:15:32 +0000740 MIAft.insert(MIAft.begin(), AdIMid.begin(), AdIMid.end());
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000741 } // if !DEF
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000742
Vikram S. Advef5af6362002-07-08 23:15:32 +0000743 // Finally, insert the entire spill code sequences before/after MInst
744 AI.InstrnsBefore.insert(AI.InstrnsBefore.end(), MIBef.begin(), MIBef.end());
745 AI.InstrnsAfter.insert(AI.InstrnsAfter.begin(), MIAft.begin(), MIAft.end());
746
Chris Lattner7e708292002-06-25 16:13:24 +0000747 if (DEBUG_RA) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000748 std::cerr << "\nFor Inst:\n " << *MInst;
749 std::cerr << "SPILLED LR# " << LR->getUserIGNode()->getIndex();
750 std::cerr << "; added Instructions:";
Anand Shuklad58290e2002-07-09 19:18:56 +0000751 for_each(MIBef.begin(), MIBef.end(), std::mem_fun(&MachineInstr::dump));
752 for_each(MIAft.begin(), MIAft.end(), std::mem_fun(&MachineInstr::dump));
Chris Lattner7e708292002-06-25 16:13:24 +0000753 }
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000754}
755
756
Vikram S. Adve814030a2003-07-29 19:49:21 +0000757//----------------------------------------------------------------------------
Misha Brukman37f92e22003-09-11 22:34:13 +0000758// This method inserts caller saving/restoring instructions before/after
Vikram S. Adve814030a2003-07-29 19:49:21 +0000759// a call machine instruction. The caller saving/restoring instructions are
760// inserted like:
761// ** caller saving instructions
762// other instructions inserted for the call by ColorCallArg
763// CALL instruction
764// other instructions inserted for the call ColorCallArg
765// ** caller restoring instructions
766//----------------------------------------------------------------------------
767
768void
769PhyRegAlloc::insertCallerSavingCode(std::vector<MachineInstr*> &instrnsBefore,
770 std::vector<MachineInstr*> &instrnsAfter,
771 MachineInstr *CallMI,
772 const BasicBlock *BB)
773{
774 assert(TM.getInstrInfo().isCall(CallMI->getOpCode()));
775
Brian Gaeke43ce8fe2003-09-21 02:24:09 +0000776 // hash set to record which registers were saved/restored
Vikram S. Adve814030a2003-07-29 19:49:21 +0000777 hash_set<unsigned> PushedRegSet;
778
779 CallArgsDescriptor* argDesc = CallArgsDescriptor::get(CallMI);
780
781 // if the call is to a instrumentation function, do not insert save and
782 // restore instructions the instrumentation function takes care of save
783 // restore for volatile regs.
784 //
785 // FIXME: this should be made general, not specific to the reoptimizer!
Vikram S. Adve814030a2003-07-29 19:49:21 +0000786 const Function *Callee = argDesc->getCallInst()->getCalledFunction();
787 bool isLLVMFirstTrigger = Callee && Callee->getName() == "llvm_first_trigger";
788
789 // Now check if the call has a return value (using argDesc) and if so,
790 // find the LR of the TmpInstruction representing the return value register.
791 // (using the last or second-last *implicit operand* of the call MI).
792 // Insert it to to the PushedRegSet since we must not save that register
793 // and restore it after the call.
794 // We do this because, we look at the LV set *after* the instruction
795 // to determine, which LRs must be saved across calls. The return value
796 // of the call is live in this set - but we must not save/restore it.
Vikram S. Adve814030a2003-07-29 19:49:21 +0000797 if (const Value *origRetVal = argDesc->getReturnValue()) {
798 unsigned retValRefNum = (CallMI->getNumImplicitRefs() -
799 (argDesc->getIndirectFuncPtr()? 1 : 2));
800 const TmpInstruction* tmpRetVal =
801 cast<TmpInstruction>(CallMI->getImplicitRef(retValRefNum));
802 assert(tmpRetVal->getOperand(0) == origRetVal &&
803 tmpRetVal->getType() == origRetVal->getType() &&
804 "Wrong implicit ref?");
Brian Gaeke4efe3422003-09-21 01:23:46 +0000805 LiveRange *RetValLR = LRI->getLiveRangeForValue(tmpRetVal);
Vikram S. Adve814030a2003-07-29 19:49:21 +0000806 assert(RetValLR && "No LR for RetValue of call");
807
808 if (! RetValLR->isMarkedForSpill())
809 PushedRegSet.insert(MRI.getUnifiedRegNum(RetValLR->getRegClassID(),
810 RetValLR->getColor()));
811 }
812
813 const ValueSet &LVSetAft = LVI->getLiveVarSetAfterMInst(CallMI, BB);
814 ValueSet::const_iterator LIt = LVSetAft.begin();
815
816 // for each live var in live variable set after machine inst
817 for( ; LIt != LVSetAft.end(); ++LIt) {
Brian Gaeke43ce8fe2003-09-21 02:24:09 +0000818 // get the live range corresponding to live var
Brian Gaeke4efe3422003-09-21 01:23:46 +0000819 LiveRange *const LR = LRI->getLiveRangeForValue(*LIt);
Vikram S. Adve814030a2003-07-29 19:49:21 +0000820
821 // LR can be null if it is a const since a const
822 // doesn't have a dominating def - see Assumptions above
823 if( LR ) {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000824 if(! LR->isMarkedForSpill()) {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000825 assert(LR->hasColor() && "LR is neither spilled nor colored?");
826 unsigned RCID = LR->getRegClassID();
827 unsigned Color = LR->getColor();
828
829 if (MRI.isRegVolatile(RCID, Color) ) {
Brian Gaeke43ce8fe2003-09-21 02:24:09 +0000830 // if this is a call to the first-level reoptimizer
831 // instrumentation entry point, and the register is not
832 // modified by call, don't save and restore it.
Vikram S. Adve814030a2003-07-29 19:49:21 +0000833 if (isLLVMFirstTrigger && !MRI.modifiedByCall(RCID, Color))
834 continue;
835
836 // if the value is in both LV sets (i.e., live before and after
837 // the call machine instruction)
Vikram S. Adve814030a2003-07-29 19:49:21 +0000838 unsigned Reg = MRI.getUnifiedRegNum(RCID, Color);
839
Brian Gaeke43ce8fe2003-09-21 02:24:09 +0000840 // if we haven't already pushed this register...
Vikram S. Adve814030a2003-07-29 19:49:21 +0000841 if( PushedRegSet.find(Reg) == PushedRegSet.end() ) {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000842 unsigned RegType = MRI.getRegTypeForLR(LR);
843
844 // Now get two instructions - to push on stack and pop from stack
845 // and add them to InstrnsBefore and InstrnsAfter of the
846 // call instruction
Vikram S. Adve814030a2003-07-29 19:49:21 +0000847 int StackOff =
Brian Gaeke4efe3422003-09-21 01:23:46 +0000848 MF->getInfo()->pushTempValue(MRI.getSpilledRegSize(RegType));
Vikram S. Adve814030a2003-07-29 19:49:21 +0000849
850 //---- Insert code for pushing the reg on stack ----------
851
852 std::vector<MachineInstr*> AdIBef, AdIAft;
853
854 // We may need a scratch register to copy the saved value
855 // to/from memory. This may itself have to insert code to
856 // free up a scratch register. Any such code should go before
857 // the save code. The scratch register, if any, is by default
858 // temporary and not "used" by the instruction unless the
859 // copy code itself decides to keep the value in the scratch reg.
860 int scratchRegType = -1;
861 int scratchReg = -1;
862 if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType))
863 { // Find a register not live in the LVSet before CallMI
864 const ValueSet &LVSetBef =
865 LVI->getLiveVarSetBeforeMInst(CallMI, BB);
866 scratchReg = getUsableUniRegAtMI(scratchRegType, &LVSetBef,
867 CallMI, AdIBef, AdIAft);
868 assert(scratchReg != MRI.getInvalidRegNum());
869 }
870
871 if (AdIBef.size() > 0)
872 instrnsBefore.insert(instrnsBefore.end(),
873 AdIBef.begin(), AdIBef.end());
874
875 MRI.cpReg2MemMI(instrnsBefore, Reg, MRI.getFramePointer(),
876 StackOff, RegType, scratchReg);
877
878 if (AdIAft.size() > 0)
879 instrnsBefore.insert(instrnsBefore.end(),
880 AdIAft.begin(), AdIAft.end());
881
882 //---- Insert code for popping the reg from the stack ----------
Vikram S. Adve814030a2003-07-29 19:49:21 +0000883 AdIBef.clear();
884 AdIAft.clear();
885
886 // We may need a scratch register to copy the saved value
887 // from memory. This may itself have to insert code to
888 // free up a scratch register. Any such code should go
889 // after the save code. As above, scratch is not marked "used".
Vikram S. Adve814030a2003-07-29 19:49:21 +0000890 scratchRegType = -1;
891 scratchReg = -1;
892 if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType))
893 { // Find a register not live in the LVSet after CallMI
894 scratchReg = getUsableUniRegAtMI(scratchRegType, &LVSetAft,
895 CallMI, AdIBef, AdIAft);
896 assert(scratchReg != MRI.getInvalidRegNum());
897 }
898
899 if (AdIBef.size() > 0)
900 instrnsAfter.insert(instrnsAfter.end(),
901 AdIBef.begin(), AdIBef.end());
902
903 MRI.cpMem2RegMI(instrnsAfter, MRI.getFramePointer(), StackOff,
904 Reg, RegType, scratchReg);
905
906 if (AdIAft.size() > 0)
907 instrnsAfter.insert(instrnsAfter.end(),
908 AdIAft.begin(), AdIAft.end());
909
910 PushedRegSet.insert(Reg);
911
912 if(DEBUG_RA) {
913 std::cerr << "\nFor call inst:" << *CallMI;
914 std::cerr << " -inserted caller saving instrs: Before:\n\t ";
915 for_each(instrnsBefore.begin(), instrnsBefore.end(),
916 std::mem_fun(&MachineInstr::dump));
917 std::cerr << " -and After:\n\t ";
918 for_each(instrnsAfter.begin(), instrnsAfter.end(),
919 std::mem_fun(&MachineInstr::dump));
920 }
921 } // if not already pushed
Vikram S. Adve814030a2003-07-29 19:49:21 +0000922 } // if LR has a volatile color
Vikram S. Adve814030a2003-07-29 19:49:21 +0000923 } // if LR has color
Vikram S. Adve814030a2003-07-29 19:49:21 +0000924 } // if there is a LR for Var
Vikram S. Adve814030a2003-07-29 19:49:21 +0000925 } // for each value in the LV set after instruction
926}
927
928
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000929//----------------------------------------------------------------------------
930// We can use the following method to get a temporary register to be used
931// BEFORE any given machine instruction. If there is a register available,
932// this method will simply return that register and set MIBef = MIAft = NULL.
933// Otherwise, it will return a register and MIAft and MIBef will contain
934// two instructions used to free up this returned register.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000935// Returned register number is the UNIFIED register number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000936//----------------------------------------------------------------------------
937
Vikram S. Advef5af6362002-07-08 23:15:32 +0000938int PhyRegAlloc::getUsableUniRegAtMI(const int RegType,
939 const ValueSet *LVSetBef,
940 MachineInstr *MInst,
941 std::vector<MachineInstr*>& MIBef,
942 std::vector<MachineInstr*>& MIAft) {
Chris Lattner133f0792002-10-28 04:45:29 +0000943 RegClass* RC = getRegClassByID(MRI.getRegClassIDOfRegType(RegType));
Vikram S. Advef5af6362002-07-08 23:15:32 +0000944
Vikram S. Advebc001b22003-07-25 21:06:09 +0000945 int RegU = getUnusedUniRegAtMI(RC, RegType, MInst, LVSetBef);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000946
947 if (RegU == -1) {
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000948 // we couldn't find an unused register. Generate code to free up a reg by
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000949 // saving it on stack and restoring after the instruction
Vikram S. Advef5af6362002-07-08 23:15:32 +0000950
Brian Gaeke4efe3422003-09-21 01:23:46 +0000951 int TmpOff = MF->getInfo()->pushTempValue(MRI.getSpilledRegSize(RegType));
Vikram S. Adve12af1642001-11-08 04:48:50 +0000952
Vikram S. Advebc001b22003-07-25 21:06:09 +0000953 RegU = getUniRegNotUsedByThisInst(RC, RegType, MInst);
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000954
Vikram S. Advef5af6362002-07-08 23:15:32 +0000955 // Check if we need a scratch register to copy this register to memory.
956 int scratchRegType = -1;
957 if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType))
958 {
Chris Lattner133f0792002-10-28 04:45:29 +0000959 int scratchReg = getUsableUniRegAtMI(scratchRegType, LVSetBef,
960 MInst, MIBef, MIAft);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000961 assert(scratchReg != MRI.getInvalidRegNum());
962
963 // We may as well hold the value in the scratch register instead
964 // of copying it to memory and back. But we have to mark the
965 // register as used by this instruction, so it does not get used
966 // as a scratch reg. by another operand or anyone else.
Chris Lattner3fd1f5b2003-08-05 22:11:13 +0000967 ScratchRegsUsed.insert(std::make_pair(MInst, scratchReg));
Vikram S. Advef5af6362002-07-08 23:15:32 +0000968 MRI.cpReg2RegMI(MIBef, RegU, scratchReg, RegType);
969 MRI.cpReg2RegMI(MIAft, scratchReg, RegU, RegType);
970 }
971 else
972 { // the register can be copied directly to/from memory so do it.
973 MRI.cpReg2MemMI(MIBef, RegU, MRI.getFramePointer(), TmpOff, RegType);
974 MRI.cpMem2RegMI(MIAft, MRI.getFramePointer(), TmpOff, RegU, RegType);
975 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000976 }
Vikram S. Advef5af6362002-07-08 23:15:32 +0000977
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000978 return RegU;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000979}
980
Vikram S. Adve814030a2003-07-29 19:49:21 +0000981
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000982//----------------------------------------------------------------------------
Vikram S. Adve814030a2003-07-29 19:49:21 +0000983// This method is called to get a new unused register that can be used
Misha Brukman37f92e22003-09-11 22:34:13 +0000984// to accommodate a temporary value. This method may be called several times
Vikram S. Adve814030a2003-07-29 19:49:21 +0000985// for a single machine instruction. Each time it is called, it finds a
986// register which is not live at that instruction and also which is not used
987// by other spilled operands of the same instruction. Return register number
988// is relative to the register class, NOT the unified number.
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000989//----------------------------------------------------------------------------
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000990
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000991int PhyRegAlloc::getUnusedUniRegAtMI(RegClass *RC,
Vikram S. Advebc001b22003-07-25 21:06:09 +0000992 const int RegType,
Vikram S. Adve814030a2003-07-29 19:49:21 +0000993 const MachineInstr *MInst,
994 const ValueSet* LVSetBef) {
Vikram S. Advebc001b22003-07-25 21:06:09 +0000995 RC->clearColorsUsed(); // Reset array
Vikram S. Adve814030a2003-07-29 19:49:21 +0000996
997 if (LVSetBef == NULL) {
998 LVSetBef = &LVI->getLiveVarSetBeforeMInst(MInst);
999 assert(LVSetBef != NULL && "Unable to get live-var set before MInst?");
1000 }
1001
Chris Lattner296b7732002-02-05 02:52:05 +00001002 ValueSet::const_iterator LIt = LVSetBef->begin();
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001003
1004 // for each live var in live variable set after machine inst
Chris Lattner7e708292002-06-25 16:13:24 +00001005 for ( ; LIt != LVSetBef->end(); ++LIt) {
Brian Gaeke43ce8fe2003-09-21 02:24:09 +00001006 // Get the live range corresponding to live var, and its RegClass
Brian Gaeke4efe3422003-09-21 01:23:46 +00001007 LiveRange *const LRofLV = LRI->getLiveRangeForValue(*LIt );
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001008
1009 // LR can be null if it is a const since a const
1010 // doesn't have a dominating def - see Assumptions above
Vikram S. Advebc001b22003-07-25 21:06:09 +00001011 if (LRofLV && LRofLV->getRegClass() == RC && LRofLV->hasColor())
1012 RC->markColorsUsed(LRofLV->getColor(),
1013 MRI.getRegTypeForLR(LRofLV), RegType);
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001014 }
1015
1016 // It is possible that one operand of this MInst was already spilled
1017 // and it received some register temporarily. If that's the case,
1018 // it is recorded in machine operand. We must skip such registers.
Vikram S. Advebc001b22003-07-25 21:06:09 +00001019 setRelRegsUsedByThisInst(RC, RegType, MInst);
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001020
Vikram S. Advebc001b22003-07-25 21:06:09 +00001021 int unusedReg = RC->getUnusedColor(RegType); // find first unused color
1022 if (unusedReg >= 0)
1023 return MRI.getUnifiedRegNum(RC->getID(), unusedReg);
1024
Chris Lattner85c54652002-05-23 15:50:03 +00001025 return -1;
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001026}
1027
1028
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001029//----------------------------------------------------------------------------
1030// Get any other register in a register class, other than what is used
1031// by operands of a machine instruction. Returns the unified reg number.
1032//----------------------------------------------------------------------------
Brian Gaeke43ce8fe2003-09-21 02:24:09 +00001033
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001034int PhyRegAlloc::getUniRegNotUsedByThisInst(RegClass *RC,
Vikram S. Advebc001b22003-07-25 21:06:09 +00001035 const int RegType,
Chris Lattner85c54652002-05-23 15:50:03 +00001036 const MachineInstr *MInst) {
Vikram S. Advebc001b22003-07-25 21:06:09 +00001037 RC->clearColorsUsed();
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001038
Vikram S. Advebc001b22003-07-25 21:06:09 +00001039 setRelRegsUsedByThisInst(RC, RegType, MInst);
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001040
Vikram S. Advebc001b22003-07-25 21:06:09 +00001041 // find the first unused color
1042 int unusedReg = RC->getUnusedColor(RegType);
1043 assert(unusedReg >= 0 &&
1044 "FATAL: No free register could be found in reg class!!");
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001045
Vikram S. Advebc001b22003-07-25 21:06:09 +00001046 return MRI.getUnifiedRegNum(RC->getID(), unusedReg);
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001047}
1048
1049
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001050//----------------------------------------------------------------------------
1051// This method modifies the IsColorUsedArr of the register class passed to it.
1052// It sets the bits corresponding to the registers used by this machine
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +00001053// instructions. Both explicit and implicit operands are set.
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001054//----------------------------------------------------------------------------
Vikram S. Advebc001b22003-07-25 21:06:09 +00001055
Chris Lattner3bed95b2003-08-05 21:55:58 +00001056static void markRegisterUsed(int RegNo, RegClass *RC, int RegType,
1057 const TargetRegInfo &TRI) {
1058 unsigned classId = 0;
1059 int classRegNum = TRI.getClassRegNum(RegNo, classId);
1060 if (RC->getID() == classId)
1061 RC->markColorsUsed(classRegNum, RegType, RegType);
1062}
1063
1064void PhyRegAlloc::setRelRegsUsedByThisInst(RegClass *RC, int RegType,
1065 const MachineInstr *MI)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001066{
Chris Lattner3bed95b2003-08-05 21:55:58 +00001067 assert(OperandsColoredMap[MI] == true &&
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001068 "Illegal to call setRelRegsUsedByThisInst() until colored operands "
1069 "are marked for an instruction.");
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001070
Chris Lattner3bed95b2003-08-05 21:55:58 +00001071 // Add the registers already marked as used by the instruction.
1072 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
1073 if (MI->getOperand(i).hasAllocatedReg())
1074 markRegisterUsed(MI->getOperand(i).getAllocatedRegNum(), RC, RegType,MRI);
1075
1076 for (unsigned i = 0, e = MI->getNumImplicitRefs(); i != e; ++i)
1077 if (MI->getImplicitOp(i).hasAllocatedReg())
1078 markRegisterUsed(MI->getImplicitOp(i).getAllocatedRegNum(), RC,
1079 RegType,MRI);
1080
Chris Lattner3fd1f5b2003-08-05 22:11:13 +00001081 // Add all of the scratch registers that are used to save values across the
1082 // instruction (e.g., for saving state register values).
1083 std::pair<ScratchRegsUsedTy::iterator, ScratchRegsUsedTy::iterator>
1084 IR = ScratchRegsUsed.equal_range(MI);
1085 for (ScratchRegsUsedTy::iterator I = IR.first; I != IR.second; ++I)
1086 markRegisterUsed(I->second, RC, RegType, MRI);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001087
Vikram S. Advef5af6362002-07-08 23:15:32 +00001088 // If there are implicit references, mark their allocated regs as well
Chris Lattner3bed95b2003-08-05 21:55:58 +00001089 for (unsigned z=0; z < MI->getNumImplicitRefs(); z++)
Vikram S. Advef5af6362002-07-08 23:15:32 +00001090 if (const LiveRange*
Brian Gaeke4efe3422003-09-21 01:23:46 +00001091 LRofImpRef = LRI->getLiveRangeForValue(MI->getImplicitRef(z)))
Vikram S. Advef5af6362002-07-08 23:15:32 +00001092 if (LRofImpRef->hasColor())
1093 // this implicit reference is in a LR that received a color
Vikram S. Advebc001b22003-07-25 21:06:09 +00001094 RC->markColorsUsed(LRofImpRef->getColor(),
1095 MRI.getRegTypeForLR(LRofImpRef), RegType);
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001096}
1097
1098
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001099//----------------------------------------------------------------------------
Ruchira Sasanka251d8db2001-10-23 21:38:00 +00001100// If there are delay slots for an instruction, the instructions
1101// added after it must really go after the delayed instruction(s).
1102// So, we move the InstrAfter of that instruction to the
1103// corresponding delayed instruction using the following method.
Ruchira Sasanka251d8db2001-10-23 21:38:00 +00001104//----------------------------------------------------------------------------
Ruchira Sasanka251d8db2001-10-23 21:38:00 +00001105
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001106void PhyRegAlloc::move2DelayedInstr(const MachineInstr *OrigMI,
1107 const MachineInstr *DelayedMI)
1108{
Vikram S. Advefeb32982003-08-12 22:22:24 +00001109 // "added after" instructions of the original instr
1110 std::vector<MachineInstr *> &OrigAft = AddedInstrMap[OrigMI].InstrnsAfter;
1111
1112 if (DEBUG_RA && OrigAft.size() > 0) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +00001113 std::cerr << "\nRegAlloc: Moved InstrnsAfter for: " << *OrigMI;
1114 std::cerr << " to last delay slot instrn: " << *DelayedMI;
Vikram S. Adve814030a2003-07-29 19:49:21 +00001115 }
1116
Ruchira Sasanka251d8db2001-10-23 21:38:00 +00001117 // "added after" instructions of the delayed instr
Vikram S. Adve814030a2003-07-29 19:49:21 +00001118 std::vector<MachineInstr *> &DelayedAft=AddedInstrMap[DelayedMI].InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +00001119
1120 // go thru all the "added after instructions" of the original instruction
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001121 // and append them to the "added after instructions" of the delayed
Ruchira Sasanka251d8db2001-10-23 21:38:00 +00001122 // instructions
Chris Lattner697954c2002-01-20 22:54:45 +00001123 DelayedAft.insert(DelayedAft.end(), OrigAft.begin(), OrigAft.end());
Ruchira Sasanka251d8db2001-10-23 21:38:00 +00001124
1125 // empty the "added after instructions" of the original instruction
1126 OrigAft.clear();
Ruchira Sasanka251d8db2001-10-23 21:38:00 +00001127}
Ruchira Sasanka0931a012001-09-15 19:06:58 +00001128
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001129
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001130void PhyRegAlloc::colorIncomingArgs()
1131{
Brian Gaeke4efe3422003-09-21 01:23:46 +00001132 MRI.colorMethodArgs(Fn, *LRI, AddedInstrAtEntry.InstrnsBefore,
Vikram S. Adve814030a2003-07-29 19:49:21 +00001133 AddedInstrAtEntry.InstrnsAfter);
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001134}
1135
Ruchira Sasankae727f852001-09-18 22:43:57 +00001136
1137//----------------------------------------------------------------------------
Brian Gaeke59b1c562003-09-24 17:50:28 +00001138// This method determines whether the suggested color of each live range
1139// is really usable, and then calls its setSuggestedColorUsable() method to
1140// record the answer. A suggested color is NOT usable when the suggested color
1141// is volatile AND when there are call interferences.
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001142//----------------------------------------------------------------------------
1143
1144void PhyRegAlloc::markUnusableSugColors()
1145{
Brian Gaeke4efe3422003-09-21 01:23:46 +00001146 LiveRangeMapType::const_iterator HMI = (LRI->getLiveRangeMap())->begin();
1147 LiveRangeMapType::const_iterator HMIEnd = (LRI->getLiveRangeMap())->end();
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001148
Brian Gaeke43ce8fe2003-09-21 02:24:09 +00001149 for (; HMI != HMIEnd ; ++HMI ) {
1150 if (HMI->first) {
1151 LiveRange *L = HMI->second; // get the LiveRange
Brian Gaeke59b1c562003-09-24 17:50:28 +00001152 if (L && L->hasSuggestedColor ())
Brian Gaeke6a256cc2003-09-24 18:08:54 +00001153 L->setSuggestedColorUsable
1154 (!(MRI.isRegVolatile (L->getRegClassID (), L->getSuggestedColor ())
1155 && L->isCallInterference ()));
Brian Gaeke43ce8fe2003-09-21 02:24:09 +00001156 }
1157 } // for all LR's in hash map
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001158}
1159
1160
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001161//----------------------------------------------------------------------------
1162// The following method will set the stack offsets of the live ranges that
Misha Brukman37f92e22003-09-11 22:34:13 +00001163// are decided to be spilled. This must be called just after coloring the
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001164// LRs using the graph coloring algo. For each live range that is spilled,
1165// this method allocate a new spill position on the stack.
1166//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001167
Chris Lattner37730942002-02-05 03:52:29 +00001168void PhyRegAlloc::allocateStackSpace4SpilledLRs() {
Chris Lattnerc083dcc2003-09-01 20:05:47 +00001169 if (DEBUG_RA) std::cerr << "\nSetting LR stack offsets for spills...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001170
Brian Gaeke4efe3422003-09-21 01:23:46 +00001171 LiveRangeMapType::const_iterator HMI = LRI->getLiveRangeMap()->begin();
1172 LiveRangeMapType::const_iterator HMIEnd = LRI->getLiveRangeMap()->end();
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001173
Chris Lattner7e708292002-06-25 16:13:24 +00001174 for ( ; HMI != HMIEnd ; ++HMI) {
Chris Lattner37730942002-02-05 03:52:29 +00001175 if (HMI->first && HMI->second) {
Vikram S. Adve3bf08922003-07-10 19:42:55 +00001176 LiveRange *L = HMI->second; // get the LiveRange
1177 if (L->isMarkedForSpill()) { // NOTE: allocating size of long Type **
Brian Gaeke4efe3422003-09-21 01:23:46 +00001178 int stackOffset = MF->getInfo()->allocateSpilledValue(Type::LongTy);
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001179 L->setSpillOffFromFP(stackOffset);
1180 if (DEBUG_RA)
Chris Lattnerc083dcc2003-09-01 20:05:47 +00001181 std::cerr << " LR# " << L->getUserIGNode()->getIndex()
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001182 << ": stack-offset = " << stackOffset << "\n";
1183 }
Chris Lattner37730942002-02-05 03:52:29 +00001184 }
1185 } // for all LR's in hash map
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001186}
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001187
Brian Gaeke874f4232003-09-21 02:50:21 +00001188
Brian Gaeke6a256cc2003-09-24 18:08:54 +00001189namespace {
1190 /// AllocInfo - Structure representing one instruction's
1191 /// operand's-worth of register allocation state. We create tables
1192 /// made out of these data structures to generate mapping information
1193 /// for this register allocator. (FIXME: This might move to a header
1194 /// file at some point.)
1195 ///
1196 struct AllocInfo {
1197 unsigned Instruction;
1198 unsigned Operand;
1199 unsigned AllocState;
1200 int Placement;
1201 AllocInfo (unsigned Instruction_, unsigned Operand_,
1202 unsigned AllocState_, int Placement_) :
1203 Instruction (Instruction_), Operand (Operand_),
1204 AllocState (AllocState_), Placement (Placement_) { }
1205 /// getConstantType - Return a StructType representing an AllocInfo
1206 /// object.
1207 ///
1208 static StructType *getConstantType () {
1209 std::vector<const Type *> TV;
1210 TV.push_back (Type::UIntTy);
1211 TV.push_back (Type::UIntTy);
1212 TV.push_back (Type::UIntTy);
1213 TV.push_back (Type::IntTy);
1214 return StructType::get (TV);
1215 }
1216 /// toConstant - Convert this AllocInfo into an LLVM Constant of type
1217 /// getConstantType(), and return the Constant.
1218 ///
1219 Constant *toConstant () const {
1220 StructType *ST = getConstantType ();
1221 std::vector<Constant *> CV;
1222 CV.push_back (ConstantUInt::get (Type::UIntTy, Instruction));
1223 CV.push_back (ConstantUInt::get (Type::UIntTy, Operand));
1224 CV.push_back (ConstantUInt::get (Type::UIntTy, AllocState));
1225 CV.push_back (ConstantSInt::get (Type::IntTy, Placement));
1226 return ConstantStruct::get (ST, CV);
1227 }
1228 };
1229}
1230
1231void PhyRegAlloc::saveState ()
1232{
1233 std::vector<Constant *> state;
1234 unsigned Insn = 0;
1235 LiveRangeMapType::const_iterator HMIEnd = LRI->getLiveRangeMap ()->end ();
1236 for (const_inst_iterator II=inst_begin (Fn), IE=inst_end (Fn); II != IE; ++II)
1237 for (unsigned i = 0; i < (*II)->getNumOperands (); ++i) {
1238 const Value *V = (*II)->getOperand (i);
1239 // Don't worry about it unless it's something whose reg. we'll need.
1240 if (!isa<Argument> (V) && !isa<Instruction> (V))
1241 continue;
1242 LiveRangeMapType::const_iterator HMI = LRI->getLiveRangeMap ()->find (V);
1243 static const unsigned NotAllocated = 0, Allocated = 1, Spilled = 2;
1244 unsigned AllocState = NotAllocated;
1245 int Placement = -1;
1246 if ((HMI != HMIEnd) && HMI->second) {
1247 LiveRange *L = HMI->second;
1248 assert ((L->hasColor () || L->isMarkedForSpill ())
1249 && "Live range exists but not colored or spilled");
1250 if (L->hasColor()) {
1251 AllocState = Allocated;
1252 Placement = MRI.getUnifiedRegNum (L->getRegClassID (),
1253 L->getColor ());
1254 } else if (L->isMarkedForSpill ()) {
1255 AllocState = Spilled;
1256 assert (L->hasSpillOffset ()
1257 && "Live range marked for spill but has no spill offset");
1258 Placement = L->getSpillOffFromFP ();
1259 }
1260 }
1261 state.push_back (AllocInfo (Insn, i, AllocState,
1262 Placement).toConstant ());
1263 }
1264 // Convert state into an LLVM ConstantArray, and put it in a
1265 // ConstantStruct (named S) along with its size.
1266 unsigned Size = state.size ();
1267 ArrayType *AT = ArrayType::get (AllocInfo::getConstantType (), Size);
1268 std::vector<const Type *> TV;
1269 TV.push_back (Type::UIntTy);
1270 TV.push_back (AT);
1271 StructType *ST = StructType::get (TV);
1272 std::vector<Constant *> CV;
1273 CV.push_back (ConstantUInt::get (Type::UIntTy, Size));
1274 CV.push_back (ConstantArray::get (AT, state));
1275 Constant *S = ConstantStruct::get (ST, CV);
1276 // Save S in the map containing register allocator state for this module.
1277 FnAllocState[Fn] = S;
1278}
1279
1280
1281bool PhyRegAlloc::doFinalization (Module &M) {
1282 if (!SaveRegAllocState)
1283 return false; // Nothing to do here, unless we're saving state.
1284
1285 // Convert FnAllocState to a single Constant array and add it
1286 // to the Module.
1287 ArrayType *AT = ArrayType::get (AllocInfo::getConstantType (), 0);
1288 std::vector<const Type *> TV;
1289 TV.push_back (Type::UIntTy);
1290 TV.push_back (AT);
1291 PointerType *PT = PointerType::get (StructType::get (TV));
1292
1293 std::vector<Constant *> allstate;
1294 for (Module::iterator I = M.begin (), E = M.end (); I != E; ++I) {
1295 Function *F = I;
1296 if (FnAllocState.find (F) == FnAllocState.end ()) {
1297 allstate.push_back (ConstantPointerNull::get (PT));
1298 } else {
1299 GlobalVariable *GV =
1300 new GlobalVariable (FnAllocState[F]->getType (), true,
1301 GlobalValue::InternalLinkage, FnAllocState[F],
1302 F->getName () + ".regAllocState", &M);
1303 // Have: { uint, [Size x { uint, uint, uint, int }] } *
1304 // Cast it to: { uint, [0 x { uint, uint, uint, int }] } *
1305 Constant *CE = ConstantExpr::getCast (ConstantPointerRef::get (GV), PT);
1306 allstate.push_back (CE);
1307 }
1308 }
1309
1310 unsigned Size = allstate.size ();
1311 // Final structure type is:
1312 // { uint, [Size x { uint, [0 x { uint, uint, uint, int }] } *] }
1313 std::vector<const Type *> TV2;
1314 TV2.push_back (Type::UIntTy);
1315 ArrayType *AT2 = ArrayType::get (PT, Size);
1316 TV2.push_back (AT2);
1317 StructType *ST2 = StructType::get (TV2);
1318 std::vector<Constant *> CV2;
1319 CV2.push_back (ConstantUInt::get (Type::UIntTy, Size));
1320 CV2.push_back (ConstantArray::get (AT2, allstate));
1321 new GlobalVariable (ST2, true, GlobalValue::InternalLinkage,
1322 ConstantStruct::get (ST2, CV2), "_llvm_regAllocState",
1323 &M);
1324 return false; // No error.
1325}
1326
1327
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001328//----------------------------------------------------------------------------
Brian Gaeke305f02d2003-09-16 15:38:05 +00001329// The entry point to Register Allocation
Ruchira Sasankae727f852001-09-18 22:43:57 +00001330//----------------------------------------------------------------------------
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001331
Brian Gaeke4efe3422003-09-21 01:23:46 +00001332bool PhyRegAlloc::runOnFunction (Function &F) {
1333 if (DEBUG_RA)
1334 std::cerr << "\n********* Function "<< F.getName () << " ***********\n";
1335
1336 Fn = &F;
1337 MF = &MachineFunction::get (Fn);
1338 LVI = &getAnalysis<FunctionLiveVarInfo> ();
1339 LRI = new LiveRangeInfo (Fn, TM, RegClassList);
1340 LoopDepthCalc = &getAnalysis<LoopInfo> ();
1341
1342 // Create each RegClass for the target machine and add it to the
1343 // RegClassList. This must be done before calling constructLiveRanges().
1344 for (unsigned rc = 0; rc != NumOfRegClasses; ++rc)
1345 RegClassList.push_back (new RegClass (Fn, &TM.getRegInfo (),
1346 MRI.getMachineRegClass (rc)));
1347
1348 LRI->constructLiveRanges(); // create LR info
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001349 if (DEBUG_RA >= RA_DEBUG_LiveRanges)
Brian Gaeke4efe3422003-09-21 01:23:46 +00001350 LRI->printLiveRanges();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001351
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001352 createIGNodeListsAndIGs(); // create IGNode list and IGs
1353
1354 buildInterferenceGraphs(); // build IGs in all reg classes
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001355
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001356 if (DEBUG_RA >= RA_DEBUG_LiveRanges) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001357 // print all LRs in all reg classes
Chris Lattner7e708292002-06-25 16:13:24 +00001358 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
1359 RegClassList[rc]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001360
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001361 // print IGs in all register classes
Chris Lattner7e708292002-06-25 16:13:24 +00001362 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
1363 RegClassList[rc]->printIG();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001364 }
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001365
Brian Gaeke4efe3422003-09-21 01:23:46 +00001366 LRI->coalesceLRs(); // coalesce all live ranges
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +00001367
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001368 if (DEBUG_RA >= RA_DEBUG_LiveRanges) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001369 // print all LRs in all reg classes
Chris Lattnerf726e772002-10-28 19:22:04 +00001370 for (unsigned rc=0; rc < NumOfRegClasses; rc++)
1371 RegClassList[rc]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001372
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001373 // print IGs in all register classes
Chris Lattnerf726e772002-10-28 19:22:04 +00001374 for (unsigned rc=0; rc < NumOfRegClasses; rc++)
1375 RegClassList[rc]->printIG();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001376 }
1377
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001378 // mark un-usable suggested color before graph coloring algorithm.
1379 // When this is done, the graph coloring algo will not reserve
1380 // suggested color unnecessarily - they can be used by another LR
1381 markUnusableSugColors();
1382
1383 // color all register classes using the graph coloring algo
Chris Lattner7e708292002-06-25 16:13:24 +00001384 for (unsigned rc=0; rc < NumOfRegClasses ; rc++)
Chris Lattnerf726e772002-10-28 19:22:04 +00001385 RegClassList[rc]->colorAllRegs();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001386
Misha Brukman37f92e22003-09-11 22:34:13 +00001387 // After graph coloring, if some LRs did not receive a color (i.e, spilled)
1388 // a position for such spilled LRs
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001389 allocateStackSpace4SpilledLRs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001390
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001391 // Reset the temp. area on the stack before use by the first instruction.
1392 // This will also happen after updating each instruction.
Brian Gaeke4efe3422003-09-21 01:23:46 +00001393 MF->getInfo()->popAllTempValues();
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001394
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001395 // color incoming args - if the correct color was not received
1396 // insert code to copy to the correct register
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001397 colorIncomingArgs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001398
Brian Gaeke6a256cc2003-09-24 18:08:54 +00001399 // Save register allocation state for this function in a Constant.
1400 if (SaveRegAllocState)
1401 saveState();
1402
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001403 // Now update the machine code with register names and add any
1404 // additional code inserted by the register allocator to the instruction
1405 // stream
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001406 updateMachineCode();
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001407
Chris Lattner045e7c82001-09-19 16:26:23 +00001408 if (DEBUG_RA) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +00001409 std::cerr << "\n**** Machine Code After Register Allocation:\n\n";
Brian Gaeke4efe3422003-09-21 01:23:46 +00001410 MF->dump();
Chris Lattner045e7c82001-09-19 16:26:23 +00001411 }
Brian Gaeke4efe3422003-09-21 01:23:46 +00001412
1413 // Tear down temporary data structures
1414 for (unsigned rc = 0; rc < NumOfRegClasses; ++rc)
1415 delete RegClassList[rc];
1416 RegClassList.clear ();
1417 AddedInstrMap.clear ();
1418 OperandsColoredMap.clear ();
1419 ScratchRegsUsed.clear ();
1420 AddedInstrAtEntry.clear ();
1421 delete LRI;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001422
Brian Gaeke4efe3422003-09-21 01:23:46 +00001423 if (DEBUG_RA) std::cerr << "\nRegister allocation complete!\n";
1424 return false; // Function was not modified
1425}