blob: 7efb469b457796043983740a8f72da825e83a107 [file] [log] [blame]
Vikram S. Adve12af1642001-11-08 04:48:50 +00001// $Id$
2//***************************************************************************
3// File:
4// PhyRegAlloc.cpp
5//
6// Purpose:
7// Register allocation for LLVM.
8//
9// History:
10// 9/10/01 - Ruchira Sasanka - created.
11//**************************************************************************/
Ruchira Sasanka8e604792001-09-14 21:18:34 +000012
Chris Lattner6dd98a62002-02-04 00:33:08 +000013#include "llvm/CodeGen/RegisterAllocation.h"
Vikram S. Adve12af1642001-11-08 04:48:50 +000014#include "llvm/CodeGen/PhyRegAlloc.h"
15#include "llvm/CodeGen/MachineInstr.h"
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000016#include "llvm/CodeGen/MachineCodeForMethod.h"
Chris Lattner0a8ed942002-02-04 05:56:09 +000017#include "llvm/Analysis/LiveVar/MethodLiveVarInfo.h"
Vikram S. Adve12af1642001-11-08 04:48:50 +000018#include "llvm/Target/TargetMachine.h"
19#include "llvm/Target/MachineFrameInfo.h"
Chris Lattner697954c2002-01-20 22:54:45 +000020#include <iostream>
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000021#include <math.h>
Chris Lattner697954c2002-01-20 22:54:45 +000022using std::cerr;
Vikram S. Adve12af1642001-11-08 04:48:50 +000023
24
25// ***TODO: There are several places we add instructions. Validate the order
26// of adding these instructions.
Ruchira Sasanka174bded2001-10-28 18:12:02 +000027
Chris Lattner045e7c82001-09-19 16:26:23 +000028cl::Enum<RegAllocDebugLevel_t> DEBUG_RA("dregalloc", cl::NoFlags,
29 "enable register allocation debugging information",
30 clEnumValN(RA_DEBUG_None , "n", "disable debug output"),
31 clEnumValN(RA_DEBUG_Normal , "y", "enable debug output"),
32 clEnumValN(RA_DEBUG_Verbose, "v", "enable extra debug output"), 0);
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000033
34
Chris Lattner6dd98a62002-02-04 00:33:08 +000035bool RegisterAllocation::runOnMethod(Method *M) {
36 if (DEBUG_RA)
37 cerr << "\n******************** Method "<< M->getName()
38 << " ********************\n";
39
Chris Lattner0a8ed942002-02-04 05:56:09 +000040 MethodLiveVarInfo LVI(M); // Analyze live varaibles
Chris Lattner6dd98a62002-02-04 00:33:08 +000041 LVI.analyze();
42
43 PhyRegAlloc PRA(M, Target, &LVI); // allocate registers
44 PRA.allocateRegisters();
45
46 if (DEBUG_RA) cerr << "\nRegister allocation complete!\n";
47 return false;
48}
49
50
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000051//----------------------------------------------------------------------------
52// Constructor: Init local composite objects and create register classes.
53//----------------------------------------------------------------------------
Vikram S. Adve12af1642001-11-08 04:48:50 +000054PhyRegAlloc::PhyRegAlloc(Method *M,
Ruchira Sasanka8e604792001-09-14 21:18:34 +000055 const TargetMachine& tm,
56 MethodLiveVarInfo *const Lvi)
Chris Lattner697954c2002-01-20 22:54:45 +000057 : TM(tm), Meth(M),
Vikram S. Adve12af1642001-11-08 04:48:50 +000058 mcInfo(MachineCodeForMethod::get(M)),
59 LVI(Lvi), LRI(M, tm, RegClassList),
Ruchira Sasanka8e604792001-09-14 21:18:34 +000060 MRI( tm.getRegInfo() ),
61 NumOfRegClasses(MRI.getNumOfRegClasses()),
Chris Lattner697954c2002-01-20 22:54:45 +000062 LoopDepthCalc(M) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +000063
64 // create each RegisterClass and put in RegClassList
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000065 //
Chris Lattner697954c2002-01-20 22:54:45 +000066 for(unsigned int rc=0; rc < NumOfRegClasses; rc++)
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000067 RegClassList.push_back( new RegClass(M, MRI.getMachineRegClass(rc),
68 &ResColList) );
Ruchira Sasanka8e604792001-09-14 21:18:34 +000069}
70
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000071
72//----------------------------------------------------------------------------
73// Destructor: Deletes register classes
74//----------------------------------------------------------------------------
75PhyRegAlloc::~PhyRegAlloc() {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000076 for( unsigned int rc=0; rc < NumOfRegClasses; rc++)
77 delete RegClassList[rc];
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000078}
79
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000080//----------------------------------------------------------------------------
81// This method initally creates interference graphs (one in each reg class)
82// and IGNodeList (one in each IG). The actual nodes will be pushed later.
83//----------------------------------------------------------------------------
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000084void PhyRegAlloc::createIGNodeListsAndIGs() {
85 if (DEBUG_RA) cerr << "Creating LR lists ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +000086
87 // hash map iterator
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000088 LiveRangeMapType::const_iterator HMI = LRI.getLiveRangeMap()->begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +000089
90 // hash map end
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000091 LiveRangeMapType::const_iterator HMIEnd = LRI.getLiveRangeMap()->end();
Ruchira Sasanka8e604792001-09-14 21:18:34 +000092
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000093 for (; HMI != HMIEnd ; ++HMI ) {
94 if (HMI->first) {
95 LiveRange *L = HMI->second; // get the LiveRange
96 if (!L) {
97 if( DEBUG_RA) {
98 cerr << "\n*?!?Warning: Null liver range found for: ";
99 printValue(HMI->first); cerr << "\n";
100 }
101 continue;
102 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000103 // if the Value * is not null, and LR
104 // is not yet written to the IGNodeList
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000105 if( !(L->getUserIGNode()) ) {
106 RegClass *const RC = // RegClass of first value in the LR
107 RegClassList[ L->getRegClass()->getID() ];
108
109 RC->addLRToIG(L); // add this LR to an IG
110 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000111 }
112 }
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000113
114 // init RegClassList
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000115 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000116 RegClassList[rc]->createInterferenceGraph();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000117
118 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000119 cerr << "LRLists Created!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000120}
121
122
123
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000124
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000125//----------------------------------------------------------------------------
126// This method will add all interferences at for a given instruction.
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000127// Interence occurs only if the LR of Def (Inst or Arg) is of the same reg
128// class as that of live var. The live var passed to this function is the
129// LVset AFTER the instruction
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000130//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000131void PhyRegAlloc::addInterference(const Value *const Def,
132 const LiveVarSet *const LVSet,
133 const bool isCallInst) {
134
135 LiveVarSet::const_iterator LIt = LVSet->begin();
136
137 // get the live range of instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000138 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000139 const LiveRange *const LROfDef = LRI.getLiveRangeForValue( Def );
140
141 IGNode *const IGNodeOfDef = LROfDef->getUserIGNode();
142 assert( IGNodeOfDef );
143
144 RegClass *const RCOfDef = LROfDef->getRegClass();
145
146 // for each live var in live variable set
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000147 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000148 for( ; LIt != LVSet->end(); ++LIt) {
149
150 if( DEBUG_RA > 1) {
Chris Lattner697954c2002-01-20 22:54:45 +0000151 cerr << "< Def="; printValue(Def);
152 cerr << ", Lvar="; printValue( *LIt); cerr << "> ";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000153 }
154
155 // get the live range corresponding to live var
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000156 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000157 LiveRange *const LROfVar = LRI.getLiveRangeForValue(*LIt );
158
159 // LROfVar can be null if it is a const since a const
160 // doesn't have a dominating def - see Assumptions above
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000161 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000162 if (LROfVar) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000163 if(LROfDef == LROfVar) // do not set interf for same LR
164 continue;
165
166 // if 2 reg classes are the same set interference
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000167 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000168 if(RCOfDef == LROfVar->getRegClass()) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000169 RCOfDef->setInterference( LROfDef, LROfVar);
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000170 } else if(DEBUG_RA > 1) {
171 // we will not have LRs for values not explicitly allocated in the
172 // instruction stream (e.g., constants)
173 cerr << " warning: no live range for " ;
174 printValue(*LIt); cerr << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000175 }
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000176 }
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000177 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000178}
179
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000180
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000181
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000182//----------------------------------------------------------------------------
183// For a call instruction, this method sets the CallInterference flag in
184// the LR of each variable live int the Live Variable Set live after the
185// call instruction (except the return value of the call instruction - since
186// the return value does not interfere with that call itself).
187//----------------------------------------------------------------------------
188
189void PhyRegAlloc::setCallInterferences(const MachineInstr *MInst,
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000190 const LiveVarSet *const LVSetAft ) {
191
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000192 // Now find the LR of the return value of the call
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000193 // We do this because, we look at the LV set *after* the instruction
194 // to determine, which LRs must be saved across calls. The return value
195 // of the call is live in this set - but it does not interfere with call
196 // (i.e., we can allocate a volatile register to the return value)
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000197 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000198 LiveRange *RetValLR = NULL;
Ruchira Sasankab3b6f532001-10-21 16:43:41 +0000199 const Value *RetVal = MRI.getCallInstRetVal( MInst );
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000200
Ruchira Sasankab3b6f532001-10-21 16:43:41 +0000201 if( RetVal ) {
202 RetValLR = LRI.getLiveRangeForValue( RetVal );
203 assert( RetValLR && "No LR for RetValue of call");
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000204 }
205
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000206 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000207 cerr << "\n For call inst: " << *MInst;
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000208
209 LiveVarSet::const_iterator LIt = LVSetAft->begin();
210
211 // for each live var in live variable set after machine inst
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000212 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000213 for( ; LIt != LVSetAft->end(); ++LIt) {
214
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000215 // get the live range corresponding to live var
216 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000217 LiveRange *const LR = LRI.getLiveRangeForValue(*LIt );
218
219 if( LR && DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000220 cerr << "\n\tLR Aft Call: ";
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000221 LR->printSet();
222 }
223
224
225 // LR can be null if it is a const since a const
226 // doesn't have a dominating def - see Assumptions above
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000227 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000228 if( LR && (LR != RetValLR) ) {
229 LR->setCallInterference();
230 if( DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000231 cerr << "\n ++Added call interf for LR: " ;
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000232 LR->printSet();
233 }
234 }
235
236 }
237
238}
239
240
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000241
242
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000243//----------------------------------------------------------------------------
244// This method will walk thru code and create interferences in the IG of
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000245// each RegClass. Also, this method calculates the spill cost of each
246// Live Range (it is done in this method to save another pass over the code).
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000247//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000248void PhyRegAlloc::buildInterferenceGraphs()
249{
250
Chris Lattner697954c2002-01-20 22:54:45 +0000251 if(DEBUG_RA) cerr << "Creating interference graphs ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000252
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000253 unsigned BBLoopDepthCost;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000254 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
255
256 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
257
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000258 // find the 10^(loop_depth) of this BB
259 //
260 BBLoopDepthCost = (unsigned) pow( 10.0, LoopDepthCalc.getLoopDepth(*BBI));
261
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000262 // get the iterator for machine instructions
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000263 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000264 const MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
265 MachineCodeForBasicBlock::const_iterator
266 MInstIterator = MIVec.begin();
267
268 // iterate over all the machine instructions in BB
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000269 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000270 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000271
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +0000272 const MachineInstr * MInst = *MInstIterator;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000273
274 // get the LV set after the instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000275 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000276 const LiveVarSet *const LVSetAI =
277 LVI->getLiveVarSetAfterMInst(MInst, *BBI);
278
279 const bool isCallInst = TM.getInstrInfo().isCall(MInst->getOpCode());
280
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000281 if( isCallInst ) {
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000282 // set the isCallInterference flag of each live range wich extends
283 // accross this call instruction. This information is used by graph
284 // coloring algo to avoid allocating volatile colors to live ranges
285 // that span across calls (since they have to be saved/restored)
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000286 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000287 setCallInterferences( MInst, LVSetAI);
288 }
289
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000290
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000291 // iterate over all MI operands to find defs
292 //
Chris Lattner7a176752001-12-04 00:03:30 +0000293 for( MachineInstr::val_const_op_iterator OpI(MInst);!OpI.done(); ++OpI) {
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000294
295 if( OpI.isDef() ) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000296 // create a new LR iff this operand is a def
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000297 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000298 addInterference(*OpI, LVSetAI, isCallInst );
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000299 }
300
301 // Calculate the spill cost of each live range
302 //
303 LiveRange *LR = LRI.getLiveRangeForValue( *OpI );
304 if( LR )
305 LR->addSpillCost(BBLoopDepthCost);
306 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000307
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000308
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000309 // if there are multiple defs in this instruction e.g. in SETX
310 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000311 if (TM.getInstrInfo().isPseudoInstr(MInst->getOpCode()))
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000312 addInterf4PseudoInstr(MInst);
313
314
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000315 // Also add interference for any implicit definitions in a machine
316 // instr (currently, only calls have this).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000317 //
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000318 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
319 if( NumOfImpRefs > 0 ) {
320 for(unsigned z=0; z < NumOfImpRefs; z++)
321 if( MInst->implicitRefIsDefined(z) )
322 addInterference( MInst->getImplicitRef(z), LVSetAI, isCallInst );
323 }
324
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +0000325
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000326 } // for all machine instructions in BB
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000327
328 } // for all BBs in method
329
330
331 // add interferences for method arguments. Since there are no explict
332 // defs in method for args, we have to add them manually
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000333 //
334 addInterferencesForArgs();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000335
336 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000337 cerr << "Interference graphs calculted!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000338
339}
340
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000341
342
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000343//--------------------------------------------------------------------------
344// Pseudo instructions will be exapnded to multiple instructions by the
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000345// assembler. Consequently, all the opernds must get distinct registers.
346// Therefore, we mark all operands of a pseudo instruction as they interfere
347// with one another.
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000348//--------------------------------------------------------------------------
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000349void PhyRegAlloc::addInterf4PseudoInstr(const MachineInstr *MInst) {
350
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000351 bool setInterf = false;
352
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000353 // iterate over MI operands to find defs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000354 //
Chris Lattner7a176752001-12-04 00:03:30 +0000355 for( MachineInstr::val_const_op_iterator It1(MInst);!It1.done(); ++It1) {
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000356
357 const LiveRange *const LROfOp1 = LRI.getLiveRangeForValue( *It1 );
358
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000359 if( !LROfOp1 && It1.isDef() )
360 assert( 0 && "No LR for Def in PSEUDO insruction");
361
Chris Lattner7a176752001-12-04 00:03:30 +0000362 MachineInstr::val_const_op_iterator It2 = It1;
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000363 ++It2;
364
365 for( ; !It2.done(); ++It2) {
366
367 const LiveRange *const LROfOp2 = LRI.getLiveRangeForValue( *It2 );
368
369 if( LROfOp2) {
370
371 RegClass *const RCOfOp1 = LROfOp1->getRegClass();
372 RegClass *const RCOfOp2 = LROfOp2->getRegClass();
373
374 if( RCOfOp1 == RCOfOp2 ){
375 RCOfOp1->setInterference( LROfOp1, LROfOp2 );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000376 setInterf = true;
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000377 }
378
379 } // if Op2 has a LR
380
381 } // for all other defs in machine instr
382
383 } // for all operands in an instruction
384
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000385 if( !setInterf && (MInst->getNumOperands() > 2) ) {
386 cerr << "\nInterf not set for any operand in pseudo instr:\n";
387 cerr << *MInst;
388 assert(0 && "Interf not set for pseudo instr with > 2 operands" );
389
390 }
391
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000392}
393
394
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000395
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000396//----------------------------------------------------------------------------
397// This method will add interferences for incoming arguments to a method.
398//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000399void PhyRegAlloc::addInterferencesForArgs()
400{
401 // get the InSet of root BB
402 const LiveVarSet *const InSet = LVI->getInSetOfBB( Meth->front() );
403
404 // get the argument list
405 const Method::ArgumentListType& ArgList = Meth->getArgumentList();
406
407 // get an iterator to arg list
408 Method::ArgumentListType::const_iterator ArgIt = ArgList.begin();
409
410
411 for( ; ArgIt != ArgList.end() ; ++ArgIt) { // for each argument
412 addInterference( *ArgIt, InSet, false ); // add interferences between
413 // args and LVars at start
414 if( DEBUG_RA > 1) {
Chris Lattner697954c2002-01-20 22:54:45 +0000415 cerr << " - %% adding interference for argument ";
416 printValue((const Value *)*ArgIt); cerr << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000417 }
418 }
419}
420
421
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000422
423
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000424//----------------------------------------------------------------------------
425// This method is called after register allocation is complete to set the
426// allocated reisters in the machine code. This code will add register numbers
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000427// to MachineOperands that contain a Value. Also it calls target specific
428// methods to produce caller saving instructions. At the end, it adds all
429// additional instructions produced by the register allocator to the
430// instruction stream.
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000431//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000432void PhyRegAlloc::updateMachineCode()
433{
434
435 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
436
437 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
438
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000439 // get the iterator for machine instructions
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000440 //
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000441 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
442 MachineCodeForBasicBlock::iterator MInstIterator = MIVec.begin();
443
444 // iterate over all the machine instructions in BB
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000445 //
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000446 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
447
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000448 MachineInstr *MInst = *MInstIterator;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000449
450 unsigned Opcode = MInst->getOpCode();
451
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000452 // do not process Phis
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000453 if (TM.getInstrInfo().isPhi(Opcode))
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000454 continue;
455
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000456 // Now insert speical instructions (if necessary) for call/return
457 // instructions.
458 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000459 if (TM.getInstrInfo().isCall(Opcode) ||
460 TM.getInstrInfo().isReturn(Opcode)) {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000461
462 AddedInstrns *AI = AddedInstrMap[ MInst];
463 if ( !AI ) {
464 AI = new AddedInstrns();
465 AddedInstrMap[ MInst ] = AI;
466 }
467
468 // Tmp stack poistions are needed by some calls that have spilled args
469 // So reset it before we call each such method
Ruchira Sasanka6a3db8c2002-01-07 21:09:06 +0000470 //
471 mcInfo.popAllTempValues(TM);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000472
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000473 if (TM.getInstrInfo().isCall(Opcode))
474 MRI.colorCallArgs(MInst, LRI, AI, *this, *BBI);
475 else if (TM.getInstrInfo().isReturn(Opcode))
476 MRI.colorRetValue(MInst, LRI, AI);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000477 }
478
479
480 /* -- Using above code instead of this
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000481
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000482 // if this machine instr is call, insert caller saving code
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000483
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000484 if( (TM.getInstrInfo()).isCall( MInst->getOpCode()) )
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000485 MRI.insertCallerSavingCode(MInst, *BBI, *this );
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000486
487 */
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000488
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000489
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000490 // reset the stack offset for temporary variables since we may
491 // need that to spill
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000492 // mcInfo.popAllTempValues(TM);
Ruchira Sasankaf90870f2001-11-15 22:02:06 +0000493 // TODO ** : do later
Vikram S. Adve12af1642001-11-08 04:48:50 +0000494
Chris Lattner7a176752001-12-04 00:03:30 +0000495 //for(MachineInstr::val_const_op_iterator OpI(MInst);!OpI.done();++OpI) {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000496
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000497
498 // Now replace set the registers for operands in the machine instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000499 //
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000500 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
501
502 MachineOperand& Op = MInst->getOperand(OpNum);
503
504 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
505 Op.getOperandType() == MachineOperand::MO_CCRegister) {
506
507 const Value *const Val = Op.getVRegValue();
508
509 // delete this condition checking later (must assert if Val is null)
Chris Lattner045e7c82001-09-19 16:26:23 +0000510 if( !Val) {
511 if (DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000512 cerr << "Warning: NULL Value found for operand\n";
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000513 continue;
514 }
515 assert( Val && "Value is NULL");
516
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000517 LiveRange *const LR = LRI.getLiveRangeForValue(Val);
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000518
519 if ( !LR ) {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000520
521 // nothing to worry if it's a const or a label
522
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000523 if (DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000524 cerr << "*NO LR for operand : " << Op ;
525 cerr << " [reg:" << Op.getAllocatedRegNum() << "]";
526 cerr << " in inst:\t" << *MInst << "\n";
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000527 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000528
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000529 // if register is not allocated, mark register as invalid
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000530 if( Op.getAllocatedRegNum() == -1)
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000531 Op.setRegForValue( MRI.getInvalidRegNum());
Ruchira Sasankae727f852001-09-18 22:43:57 +0000532
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000533
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000534 continue;
535 }
536
537 unsigned RCID = (LR->getRegClass())->getID();
538
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000539 if( LR->hasColor() ) {
540 Op.setRegForValue( MRI.getUnifiedRegNum(RCID, LR->getColor()) );
541 }
542 else {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000543
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000544 // LR did NOT receive a color (register). Now, insert spill code
545 // for spilled opeands in this machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000546
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000547 //assert(0 && "LR must be spilled");
548 insertCode4SpilledLR(LR, MInst, *BBI, OpNum );
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000549
550 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000551 }
552
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000553 } // for each operand
554
555
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000556 // Now add instructions that the register allocator inserts before/after
557 // this machine instructions (done only for calls/rets/incoming args)
558 // We do this here, to ensure that spill for an instruction is inserted
559 // closest as possible to an instruction (see above insertCode4Spill...)
560 //
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000561 // If there are instructions to be added, *before* this machine
562 // instruction, add them now.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000563 //
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000564 if( AddedInstrMap[ MInst ] ) {
Chris Lattner697954c2002-01-20 22:54:45 +0000565 std::deque<MachineInstr *> &IBef = AddedInstrMap[MInst]->InstrnsBefore;
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000566
567 if( ! IBef.empty() ) {
Chris Lattner697954c2002-01-20 22:54:45 +0000568 std::deque<MachineInstr *>::iterator AdIt;
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000569
570 for( AdIt = IBef.begin(); AdIt != IBef.end() ; ++AdIt ) {
571
572 if( DEBUG_RA) {
573 cerr << "For inst " << *MInst;
Chris Lattner697954c2002-01-20 22:54:45 +0000574 cerr << " PREPENDed instr: " << **AdIt << "\n";
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000575 }
576
577 MInstIterator = MIVec.insert( MInstIterator, *AdIt );
578 ++MInstIterator;
579 }
580
581 }
582
583 }
584
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000585 // If there are instructions to be added *after* this machine
586 // instruction, add them now
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000587 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000588 if(AddedInstrMap[MInst] &&
589 !AddedInstrMap[MInst]->InstrnsAfter.empty() ) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000590
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000591 // if there are delay slots for this instruction, the instructions
592 // added after it must really go after the delayed instruction(s)
593 // So, we move the InstrAfter of the current instruction to the
594 // corresponding delayed instruction
595
596 unsigned delay;
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000597 if ((delay=TM.getInstrInfo().getNumDelaySlots(MInst->getOpCode())) >0){
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000598 move2DelayedInstr(MInst, *(MInstIterator+delay) );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000599
Chris Lattner697954c2002-01-20 22:54:45 +0000600 if(DEBUG_RA) cerr<< "\nMoved an added instr after the delay slot";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000601 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000602
603 else {
604
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000605
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000606 // Here we can add the "instructions after" to the current
607 // instruction since there are no delay slots for this instruction
608
Chris Lattner697954c2002-01-20 22:54:45 +0000609 std::deque<MachineInstr *> &IAft = AddedInstrMap[MInst]->InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000610
611 if( ! IAft.empty() ) {
612
Chris Lattner697954c2002-01-20 22:54:45 +0000613 std::deque<MachineInstr *>::iterator AdIt;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000614
615 ++MInstIterator; // advance to the next instruction
616
617 for( AdIt = IAft.begin(); AdIt != IAft.end() ; ++AdIt ) {
618
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000619 if(DEBUG_RA) {
620 cerr << "For inst " << *MInst;
Chris Lattner697954c2002-01-20 22:54:45 +0000621 cerr << " APPENDed instr: " << **AdIt << "\n";
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000622 }
623
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000624 MInstIterator = MIVec.insert( MInstIterator, *AdIt );
625 ++MInstIterator;
626 }
627
628 // MInsterator already points to the next instr. Since the
629 // for loop also increments it, decrement it to point to the
630 // instruction added last
631 --MInstIterator;
632
633 }
634
635 } // if not delay
636
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000637 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000638
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000639 } // for each machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000640 }
641}
642
643
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000644
645//----------------------------------------------------------------------------
646// This method inserts spill code for AN operand whose LR was spilled.
647// This method may be called several times for a single machine instruction
648// if it contains many spilled operands. Each time it is called, it finds
649// a register which is not live at that instruction and also which is not
650// used by other spilled operands of the same instruction. Then it uses
651// this register temporarily to accomodate the spilled value.
652//----------------------------------------------------------------------------
653void PhyRegAlloc::insertCode4SpilledLR(const LiveRange *LR,
654 MachineInstr *MInst,
655 const BasicBlock *BB,
656 const unsigned OpNum) {
657
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000658 assert(! TM.getInstrInfo().isCall(MInst->getOpCode()) &&
659 (! TM.getInstrInfo().isReturn(MInst->getOpCode())) &&
660 "Arg of a call/ret must be handled elsewhere");
661
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000662 MachineOperand& Op = MInst->getOperand(OpNum);
663 bool isDef = MInst->operandIsDefined(OpNum);
664 unsigned RegType = MRI.getRegType( LR );
665 int SpillOff = LR->getSpillOffFromFP();
666 RegClass *RC = LR->getRegClass();
667 const LiveVarSet *LVSetBef = LVI->getLiveVarSetBeforeMInst(MInst, BB);
Vikram S. Adve00521d72001-11-12 23:26:35 +0000668
Chris Lattner697954c2002-01-20 22:54:45 +0000669 mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000670
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000671 MachineInstr *MIBef=NULL, *AdIMid=NULL, *MIAft=NULL;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000672
673 int TmpRegU = getUsableUniRegAtMI(RC, RegType, MInst,LVSetBef, MIBef, MIAft);
674
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000675 // get the added instructions for this instruciton
676 AddedInstrns *AI = AddedInstrMap[ MInst ];
677 if ( !AI ) {
678 AI = new AddedInstrns();
679 AddedInstrMap[ MInst ] = AI;
680 }
681
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000682
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000683 if( !isDef ) {
684
685 // for a USE, we have to load the value of LR from stack to a TmpReg
686 // and use the TmpReg as one operand of instruction
687
688 // actual loading instruction
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000689 AdIMid = MRI.cpMem2RegMI(MRI.getFramePointer(), SpillOff, TmpRegU,RegType);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000690
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000691 if(MIBef)
692 AI->InstrnsBefore.push_back(MIBef);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000693
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000694 AI->InstrnsBefore.push_back(AdIMid);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000695
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000696 if(MIAft)
697 AI->InstrnsAfter.push_front(MIAft);
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000698
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000699
700 }
701 else { // if this is a Def
702
703 // for a DEF, we have to store the value produced by this instruction
704 // on the stack position allocated for this LR
705
706 // actual storing instruction
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000707 AdIMid = MRI.cpReg2MemMI(TmpRegU, MRI.getFramePointer(), SpillOff,RegType);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000708
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000709 if (MIBef)
710 AI->InstrnsBefore.push_back(MIBef);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000711
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000712 AI->InstrnsAfter.push_front(AdIMid);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000713
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000714 if (MIAft)
715 AI->InstrnsAfter.push_front(MIAft);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000716
717 } // if !DEF
718
719 cerr << "\nFor Inst " << *MInst;
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000720 cerr << " - SPILLED LR: "; LR->printSet();
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000721 cerr << "\n - Added Instructions:";
722 if( MIBef ) cerr << *MIBef;
723 cerr << *AdIMid;
724 if( MIAft ) cerr << *MIAft;
725
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000726 Op.setRegForValue( TmpRegU ); // set the opearnd
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000727
728
729}
730
731
732
733
734
735
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000736//----------------------------------------------------------------------------
737// We can use the following method to get a temporary register to be used
738// BEFORE any given machine instruction. If there is a register available,
739// this method will simply return that register and set MIBef = MIAft = NULL.
740// Otherwise, it will return a register and MIAft and MIBef will contain
741// two instructions used to free up this returned register.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000742// Returned register number is the UNIFIED register number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000743//----------------------------------------------------------------------------
744
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000745int PhyRegAlloc::getUsableUniRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000746 const int RegType,
747 const MachineInstr *MInst,
748 const LiveVarSet *LVSetBef,
749 MachineInstr *MIBef,
750 MachineInstr *MIAft) {
751
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000752 int RegU = getUnusedUniRegAtMI(RC, MInst, LVSetBef);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000753
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000754
755 if( RegU != -1) {
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000756 // we found an unused register, so we can simply use it
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000757 MIBef = MIAft = NULL;
758 }
759 else {
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000760 // we couldn't find an unused register. Generate code to free up a reg by
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000761 // saving it on stack and restoring after the instruction
762
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000763 int TmpOff = mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
Vikram S. Adve12af1642001-11-08 04:48:50 +0000764
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000765 RegU = getUniRegNotUsedByThisInst(RC, MInst);
766 MIBef = MRI.cpReg2MemMI(RegU, MRI.getFramePointer(), TmpOff, RegType );
767 MIAft = MRI.cpMem2RegMI(MRI.getFramePointer(), TmpOff, RegU, RegType );
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000768 }
769
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000770 return RegU;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000771}
772
773//----------------------------------------------------------------------------
774// This method is called to get a new unused register that can be used to
775// accomodate a spilled value.
776// This method may be called several times for a single machine instruction
777// if it contains many spilled operands. Each time it is called, it finds
778// a register which is not live at that instruction and also which is not
779// used by other spilled operands of the same instruction.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000780// Return register number is relative to the register class. NOT
781// unified number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000782//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000783int PhyRegAlloc::getUnusedUniRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000784 const MachineInstr *MInst,
785 const LiveVarSet *LVSetBef) {
786
787 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
788
789 bool *IsColorUsedArr = RC->getIsColorUsedArr();
790
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000791 for(unsigned i=0; i < NumAvailRegs; i++) // Reset array
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000792 IsColorUsedArr[i] = false;
793
794 LiveVarSet::const_iterator LIt = LVSetBef->begin();
795
796 // for each live var in live variable set after machine inst
797 for( ; LIt != LVSetBef->end(); ++LIt) {
798
799 // get the live range corresponding to live var
800 LiveRange *const LRofLV = LRI.getLiveRangeForValue(*LIt );
801
802 // LR can be null if it is a const since a const
803 // doesn't have a dominating def - see Assumptions above
804 if( LRofLV )
805 if( LRofLV->hasColor() )
806 IsColorUsedArr[ LRofLV->getColor() ] = true;
807 }
808
809 // It is possible that one operand of this MInst was already spilled
810 // and it received some register temporarily. If that's the case,
811 // it is recorded in machine operand. We must skip such registers.
812
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000813 setRelRegsUsedByThisInst(RC, MInst);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000814
815 unsigned c; // find first unused color
816 for( c=0; c < NumAvailRegs; c++)
817 if( ! IsColorUsedArr[ c ] ) break;
818
819 if(c < NumAvailRegs)
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000820 return MRI.getUnifiedRegNum(RC->getID(), c);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000821 else
822 return -1;
823
824
825}
826
827
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000828//----------------------------------------------------------------------------
829// Get any other register in a register class, other than what is used
830// by operands of a machine instruction. Returns the unified reg number.
831//----------------------------------------------------------------------------
832int PhyRegAlloc::getUniRegNotUsedByThisInst(RegClass *RC,
833 const MachineInstr *MInst) {
834
835 bool *IsColorUsedArr = RC->getIsColorUsedArr();
836 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
837
838
839 for(unsigned i=0; i < NumAvailRegs ; i++) // Reset array
840 IsColorUsedArr[i] = false;
841
842 setRelRegsUsedByThisInst(RC, MInst);
843
844 unsigned c; // find first unused color
845 for( c=0; c < RC->getNumOfAvailRegs(); c++)
846 if( ! IsColorUsedArr[ c ] ) break;
847
848 if(c < NumAvailRegs)
849 return MRI.getUnifiedRegNum(RC->getID(), c);
850 else
851 assert( 0 && "FATAL: No free register could be found in reg class!!");
Chris Lattner697954c2002-01-20 22:54:45 +0000852 return 0;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000853}
854
855
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000856//----------------------------------------------------------------------------
857// This method modifies the IsColorUsedArr of the register class passed to it.
858// It sets the bits corresponding to the registers used by this machine
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000859// instructions. Both explicit and implicit operands are set.
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000860//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000861void PhyRegAlloc::setRelRegsUsedByThisInst(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000862 const MachineInstr *MInst ) {
863
864 bool *IsColorUsedArr = RC->getIsColorUsedArr();
865
866 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
867
868 const MachineOperand& Op = MInst->getOperand(OpNum);
869
870 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000871 Op.getOperandType() == MachineOperand::MO_CCRegister ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000872
873 const Value *const Val = Op.getVRegValue();
874
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000875 if( Val )
876 if( MRI.getRegClassIDOfValue(Val) == RC->getID() ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000877 int Reg;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000878 if( (Reg=Op.getAllocatedRegNum()) != -1) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000879 IsColorUsedArr[ Reg ] = true;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000880 }
881 else {
882 // it is possilbe that this operand still is not marked with
883 // a register but it has a LR and that received a color
884
885 LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
886 if( LROfVal)
887 if( LROfVal->hasColor() )
888 IsColorUsedArr[ LROfVal->getColor() ] = true;
889 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000890
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000891 } // if reg classes are the same
892 }
893 else if (Op.getOperandType() == MachineOperand::MO_MachineRegister) {
894 IsColorUsedArr[ Op.getMachineRegNum() ] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000895 }
896 }
897
898 // If there are implicit references, mark them as well
899
900 for(unsigned z=0; z < MInst->getNumImplicitRefs(); z++) {
901
902 LiveRange *const LRofImpRef =
903 LRI.getLiveRangeForValue( MInst->getImplicitRef(z) );
Chris Lattner697954c2002-01-20 22:54:45 +0000904
905 if(LRofImpRef && LRofImpRef->hasColor())
906 IsColorUsedArr[LRofImpRef->getColor()] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000907 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000908}
909
910
911
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000912
913
914
915
916
917//----------------------------------------------------------------------------
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000918// If there are delay slots for an instruction, the instructions
919// added after it must really go after the delayed instruction(s).
920// So, we move the InstrAfter of that instruction to the
921// corresponding delayed instruction using the following method.
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000922
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000923//----------------------------------------------------------------------------
924void PhyRegAlloc:: move2DelayedInstr(const MachineInstr *OrigMI,
925 const MachineInstr *DelayedMI) {
926
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000927 // "added after" instructions of the original instr
Chris Lattner697954c2002-01-20 22:54:45 +0000928 std::deque<MachineInstr *> &OrigAft = AddedInstrMap[OrigMI]->InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000929
930 // "added instructions" of the delayed instr
931 AddedInstrns *DelayAdI = AddedInstrMap[DelayedMI];
932
933 if(! DelayAdI ) { // create a new "added after" if necessary
934 DelayAdI = new AddedInstrns();
935 AddedInstrMap[DelayedMI] = DelayAdI;
936 }
937
938 // "added after" instructions of the delayed instr
Chris Lattner697954c2002-01-20 22:54:45 +0000939 std::deque<MachineInstr *> &DelayedAft = DelayAdI->InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000940
941 // go thru all the "added after instructions" of the original instruction
942 // and append them to the "addded after instructions" of the delayed
943 // instructions
Chris Lattner697954c2002-01-20 22:54:45 +0000944 DelayedAft.insert(DelayedAft.end(), OrigAft.begin(), OrigAft.end());
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000945
946 // empty the "added after instructions" of the original instruction
947 OrigAft.clear();
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000948}
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000949
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000950//----------------------------------------------------------------------------
951// This method prints the code with registers after register allocation is
952// complete.
953//----------------------------------------------------------------------------
954void PhyRegAlloc::printMachineCode()
955{
956
Chris Lattner697954c2002-01-20 22:54:45 +0000957 cerr << "\n;************** Method " << Meth->getName()
958 << " *****************\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000959
960 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
961
962 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
963
Chris Lattner697954c2002-01-20 22:54:45 +0000964 cerr << "\n"; printLabel( *BBI); cerr << ": ";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000965
966 // get the iterator for machine instructions
967 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
968 MachineCodeForBasicBlock::iterator MInstIterator = MIVec.begin();
969
970 // iterate over all the machine instructions in BB
971 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
972
973 MachineInstr *const MInst = *MInstIterator;
974
975
Chris Lattner697954c2002-01-20 22:54:45 +0000976 cerr << "\n\t";
977 cerr << TargetInstrDescriptors[MInst->getOpCode()].opCodeString;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000978
979
Chris Lattner7a176752001-12-04 00:03:30 +0000980 //for(MachineInstr::val_const_op_iterator OpI(MInst);!OpI.done();++OpI) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000981
982 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
983
984 MachineOperand& Op = MInst->getOperand(OpNum);
985
986 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasanka97b8b442001-10-18 22:36:26 +0000987 Op.getOperandType() == MachineOperand::MO_CCRegister /*||
988 Op.getOperandType() == MachineOperand::MO_PCRelativeDisp*/ ) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000989
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000990 const Value *const Val = Op.getVRegValue () ;
Ruchira Sasankae727f852001-09-18 22:43:57 +0000991 // ****this code is temporary till NULL Values are fixed
992 if( ! Val ) {
Chris Lattner697954c2002-01-20 22:54:45 +0000993 cerr << "\t<*NULL*>";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000994 continue;
995 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000996
997 // if a label or a constant
Chris Lattnerdbe53042002-01-21 01:33:12 +0000998 if(isa<BasicBlock>(Val)) {
Chris Lattner697954c2002-01-20 22:54:45 +0000999 cerr << "\t"; printLabel( Op.getVRegValue () );
1000 } else {
Ruchira Sasankae727f852001-09-18 22:43:57 +00001001 // else it must be a register value
1002 const int RegNum = Op.getAllocatedRegNum();
1003
Chris Lattner697954c2002-01-20 22:54:45 +00001004 cerr << "\t" << "%" << MRI.getUnifiedRegName( RegNum );
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001005 if (Val->hasName() )
Chris Lattner697954c2002-01-20 22:54:45 +00001006 cerr << "(" << Val->getName() << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001007 else
Chris Lattner697954c2002-01-20 22:54:45 +00001008 cerr << "(" << Val << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001009
1010 if( Op.opIsDef() )
Chris Lattner697954c2002-01-20 22:54:45 +00001011 cerr << "*";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001012
1013 const LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
1014 if( LROfVal )
1015 if( LROfVal->hasSpillOffset() )
Chris Lattner697954c2002-01-20 22:54:45 +00001016 cerr << "$";
Ruchira Sasankae727f852001-09-18 22:43:57 +00001017 }
1018
1019 }
1020 else if(Op.getOperandType() == MachineOperand::MO_MachineRegister) {
Chris Lattner697954c2002-01-20 22:54:45 +00001021 cerr << "\t" << "%" << MRI.getUnifiedRegName(Op.getMachineRegNum());
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001022 }
1023
1024 else
Chris Lattner697954c2002-01-20 22:54:45 +00001025 cerr << "\t" << Op; // use dump field
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001026 }
1027
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001028
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001029
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001030 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
1031 if( NumOfImpRefs > 0 ) {
1032
Chris Lattner697954c2002-01-20 22:54:45 +00001033 cerr << "\tImplicit:";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001034
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001035 for(unsigned z=0; z < NumOfImpRefs; z++) {
1036 printValue( MInst->getImplicitRef(z) );
Chris Lattner697954c2002-01-20 22:54:45 +00001037 cerr << "\t";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001038 }
1039
1040 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001041
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001042 } // for all machine instructions
1043
Chris Lattner697954c2002-01-20 22:54:45 +00001044 cerr << "\n";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001045
1046 } // for all BBs
1047
Chris Lattner697954c2002-01-20 22:54:45 +00001048 cerr << "\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001049}
1050
Ruchira Sasankae727f852001-09-18 22:43:57 +00001051
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001052#if 0
1053
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001054//----------------------------------------------------------------------------
1055//
1056//----------------------------------------------------------------------------
1057
1058void PhyRegAlloc::colorCallRetArgs()
1059{
1060
1061 CallRetInstrListType &CallRetInstList = LRI.getCallRetInstrList();
1062 CallRetInstrListType::const_iterator It = CallRetInstList.begin();
1063
1064 for( ; It != CallRetInstList.end(); ++It ) {
1065
Ruchira Sasankaa90e7702001-10-15 16:26:38 +00001066 const MachineInstr *const CRMI = *It;
1067 unsigned OpCode = CRMI->getOpCode();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001068
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001069 // get the added instructions for this Call/Ret instruciton
1070 AddedInstrns *AI = AddedInstrMap[ CRMI ];
1071 if ( !AI ) {
1072 AI = new AddedInstrns();
1073 AddedInstrMap[ CRMI ] = AI;
1074 }
1075
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001076 // Tmp stack poistions are needed by some calls that have spilled args
1077 // So reset it before we call each such method
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001078 //mcInfo.popAllTempValues(TM);
1079
1080
Vikram S. Adve12af1642001-11-08 04:48:50 +00001081
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001082 if (TM.getInstrInfo().isCall(OpCode))
1083 MRI.colorCallArgs(CRMI, LRI, AI, *this);
1084 else if (TM.getInstrInfo().isReturn(OpCode))
Ruchira Sasankaa90e7702001-10-15 16:26:38 +00001085 MRI.colorRetValue( CRMI, LRI, AI );
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001086 else
1087 assert(0 && "Non Call/Ret instrn in CallRetInstrList\n");
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001088 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001089}
1090
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001091#endif
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001092
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001093//----------------------------------------------------------------------------
1094
1095//----------------------------------------------------------------------------
1096void PhyRegAlloc::colorIncomingArgs()
1097{
1098 const BasicBlock *const FirstBB = Meth->front();
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001099 const MachineInstr *FirstMI = FirstBB->getMachineInstrVec().front();
1100 assert(FirstMI && "No machine instruction in entry BB");
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001101
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001102 AddedInstrns *AI = AddedInstrMap[FirstMI];
1103 if (!AI)
1104 AddedInstrMap[FirstMI] = AI = new AddedInstrns();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001105
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001106 MRI.colorMethodArgs(Meth, LRI, AI);
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001107}
1108
Ruchira Sasankae727f852001-09-18 22:43:57 +00001109
1110//----------------------------------------------------------------------------
1111// Used to generate a label for a basic block
1112//----------------------------------------------------------------------------
Chris Lattner697954c2002-01-20 22:54:45 +00001113void PhyRegAlloc::printLabel(const Value *const Val) {
1114 if (Val->hasName())
1115 cerr << Val->getName();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001116 else
Chris Lattner697954c2002-01-20 22:54:45 +00001117 cerr << "Label" << Val;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001118}
1119
1120
Ruchira Sasankae727f852001-09-18 22:43:57 +00001121//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001122// This method calls setSugColorUsable method of each live range. This
1123// will determine whether the suggested color of LR is really usable.
1124// A suggested color is not usable when the suggested color is volatile
1125// AND when there are call interferences
1126//----------------------------------------------------------------------------
1127
1128void PhyRegAlloc::markUnusableSugColors()
1129{
Chris Lattner697954c2002-01-20 22:54:45 +00001130 if(DEBUG_RA ) cerr << "\nmarking unusable suggested colors ...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001131
1132 // hash map iterator
1133 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
1134 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
1135
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001136 for(; HMI != HMIEnd ; ++HMI ) {
1137 if (HMI->first) {
1138 LiveRange *L = HMI->second; // get the LiveRange
1139 if (L) {
1140 if(L->hasSuggestedColor()) {
1141 int RCID = L->getRegClass()->getID();
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001142 if( MRI.isRegVolatile( RCID, L->getSuggestedColor()) &&
1143 L->isCallInterference() )
1144 L->setSuggestedColorUsable( false );
1145 else
1146 L->setSuggestedColorUsable( true );
1147 }
1148 } // if L->hasSuggestedColor()
1149 }
1150 } // for all LR's in hash map
1151}
1152
1153
1154
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001155//----------------------------------------------------------------------------
1156// The following method will set the stack offsets of the live ranges that
1157// are decided to be spillled. This must be called just after coloring the
1158// LRs using the graph coloring algo. For each live range that is spilled,
1159// this method allocate a new spill position on the stack.
1160//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001161
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001162void PhyRegAlloc::allocateStackSpace4SpilledLRs()
1163{
Chris Lattner697954c2002-01-20 22:54:45 +00001164 if(DEBUG_RA ) cerr << "\nsetting LR stack offsets ...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001165
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001166 // hash map iterator
1167 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
1168 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
1169
1170 for( ; HMI != HMIEnd ; ++HMI ) {
Chris Lattner697954c2002-01-20 22:54:45 +00001171 if(HMI->first && HMI->second) {
1172 LiveRange *L = HMI->second; // get the LiveRange
1173 if( ! L->hasColor() )
1174 // NOTE: ** allocating the size of long Type **
1175 L->setSpillOffFromFP(mcInfo.allocateSpilledValue(TM, Type::LongTy));
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001176 }
1177 } // for all LR's in hash map
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001178}
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001179
1180
1181
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001182//----------------------------------------------------------------------------
Ruchira Sasankae727f852001-09-18 22:43:57 +00001183// The entry pont to Register Allocation
1184//----------------------------------------------------------------------------
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001185
1186void PhyRegAlloc::allocateRegisters()
1187{
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001188
1189 // make sure that we put all register classes into the RegClassList
1190 // before we call constructLiveRanges (now done in the constructor of
1191 // PhyRegAlloc class).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001192 //
1193 LRI.constructLiveRanges(); // create LR info
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001194
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001195 if (DEBUG_RA)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001196 LRI.printLiveRanges();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001197
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001198 createIGNodeListsAndIGs(); // create IGNode list and IGs
1199
1200 buildInterferenceGraphs(); // build IGs in all reg classes
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001201
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001202
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001203 if (DEBUG_RA) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001204 // print all LRs in all reg classes
1205 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1206 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001207
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001208 // print IGs in all register classes
1209 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1210 RegClassList[ rc ]->printIG();
1211 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001212
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001213
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001214 LRI.coalesceLRs(); // coalesce all live ranges
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001215
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +00001216
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001217 if( DEBUG_RA) {
1218 // print all LRs in all reg classes
1219 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1220 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001221
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001222 // print IGs in all register classes
1223 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1224 RegClassList[ rc ]->printIG();
1225 }
1226
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001227
1228 // mark un-usable suggested color before graph coloring algorithm.
1229 // When this is done, the graph coloring algo will not reserve
1230 // suggested color unnecessarily - they can be used by another LR
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001231 //
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001232 markUnusableSugColors();
1233
1234 // color all register classes using the graph coloring algo
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001235 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1236 RegClassList[ rc ]->colorAllRegs();
1237
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001238 // Atter grpah coloring, if some LRs did not receive a color (i.e, spilled)
1239 // a poistion for such spilled LRs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001240 //
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001241 allocateStackSpace4SpilledLRs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001242
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001243 mcInfo.popAllTempValues(TM); // TODO **Check
1244
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001245 // color incoming args - if the correct color was not received
1246 // insert code to copy to the correct register
1247 //
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001248 colorIncomingArgs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001249
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001250 // Now update the machine code with register names and add any
1251 // additional code inserted by the register allocator to the instruction
1252 // stream
1253 //
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001254 updateMachineCode();
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001255
Chris Lattner045e7c82001-09-19 16:26:23 +00001256 if (DEBUG_RA) {
Vikram S. Adve12af1642001-11-08 04:48:50 +00001257 MachineCodeForMethod::get(Meth).dump();
Chris Lattner045e7c82001-09-19 16:26:23 +00001258 printMachineCode(); // only for DEBUGGING
1259 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001260}
1261
Ruchira Sasankae727f852001-09-18 22:43:57 +00001262
1263