blob: 6ba63e3051d8070a19fcc9dabc383fa6a6e50b5d [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"
Vikram S. Adve12af1642001-11-08 04:48:50 +000017#include "llvm/Target/TargetMachine.h"
18#include "llvm/Target/MachineFrameInfo.h"
Chris Lattner697954c2002-01-20 22:54:45 +000019#include <iostream>
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000020#include <math.h>
Chris Lattner697954c2002-01-20 22:54:45 +000021using std::cerr;
Vikram S. Adve12af1642001-11-08 04:48:50 +000022
23
24// ***TODO: There are several places we add instructions. Validate the order
25// of adding these instructions.
Ruchira Sasanka174bded2001-10-28 18:12:02 +000026
Chris Lattner045e7c82001-09-19 16:26:23 +000027cl::Enum<RegAllocDebugLevel_t> DEBUG_RA("dregalloc", cl::NoFlags,
28 "enable register allocation debugging information",
29 clEnumValN(RA_DEBUG_None , "n", "disable debug output"),
30 clEnumValN(RA_DEBUG_Normal , "y", "enable debug output"),
31 clEnumValN(RA_DEBUG_Verbose, "v", "enable extra debug output"), 0);
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000032
33
Chris Lattner6dd98a62002-02-04 00:33:08 +000034bool RegisterAllocation::runOnMethod(Method *M) {
35 if (DEBUG_RA)
36 cerr << "\n******************** Method "<< M->getName()
37 << " ********************\n";
38
39 MethodLiveVarInfo LVI(M ); // Analyze live varaibles
40 LVI.analyze();
41
42 PhyRegAlloc PRA(M, Target, &LVI); // allocate registers
43 PRA.allocateRegisters();
44
45 if (DEBUG_RA) cerr << "\nRegister allocation complete!\n";
46 return false;
47}
48
49
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000050//----------------------------------------------------------------------------
51// Constructor: Init local composite objects and create register classes.
52//----------------------------------------------------------------------------
Vikram S. Adve12af1642001-11-08 04:48:50 +000053PhyRegAlloc::PhyRegAlloc(Method *M,
Ruchira Sasanka8e604792001-09-14 21:18:34 +000054 const TargetMachine& tm,
55 MethodLiveVarInfo *const Lvi)
Chris Lattner697954c2002-01-20 22:54:45 +000056 : TM(tm), Meth(M),
Vikram S. Adve12af1642001-11-08 04:48:50 +000057 mcInfo(MachineCodeForMethod::get(M)),
58 LVI(Lvi), LRI(M, tm, RegClassList),
Ruchira Sasanka8e604792001-09-14 21:18:34 +000059 MRI( tm.getRegInfo() ),
60 NumOfRegClasses(MRI.getNumOfRegClasses()),
Chris Lattner697954c2002-01-20 22:54:45 +000061 LoopDepthCalc(M) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +000062
63 // create each RegisterClass and put in RegClassList
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000064 //
Chris Lattner697954c2002-01-20 22:54:45 +000065 for(unsigned int rc=0; rc < NumOfRegClasses; rc++)
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000066 RegClassList.push_back( new RegClass(M, MRI.getMachineRegClass(rc),
67 &ResColList) );
Ruchira Sasanka8e604792001-09-14 21:18:34 +000068}
69
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000070
71//----------------------------------------------------------------------------
72// Destructor: Deletes register classes
73//----------------------------------------------------------------------------
74PhyRegAlloc::~PhyRegAlloc() {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000075 for( unsigned int rc=0; rc < NumOfRegClasses; rc++)
76 delete RegClassList[rc];
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000077}
78
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000079//----------------------------------------------------------------------------
80// This method initally creates interference graphs (one in each reg class)
81// and IGNodeList (one in each IG). The actual nodes will be pushed later.
82//----------------------------------------------------------------------------
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000083void PhyRegAlloc::createIGNodeListsAndIGs() {
84 if (DEBUG_RA) cerr << "Creating LR lists ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +000085
86 // hash map iterator
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000087 LiveRangeMapType::const_iterator HMI = LRI.getLiveRangeMap()->begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +000088
89 // hash map end
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000090 LiveRangeMapType::const_iterator HMIEnd = LRI.getLiveRangeMap()->end();
Ruchira Sasanka8e604792001-09-14 21:18:34 +000091
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000092 for (; HMI != HMIEnd ; ++HMI ) {
93 if (HMI->first) {
94 LiveRange *L = HMI->second; // get the LiveRange
95 if (!L) {
96 if( DEBUG_RA) {
97 cerr << "\n*?!?Warning: Null liver range found for: ";
98 printValue(HMI->first); cerr << "\n";
99 }
100 continue;
101 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000102 // if the Value * is not null, and LR
103 // is not yet written to the IGNodeList
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000104 if( !(L->getUserIGNode()) ) {
105 RegClass *const RC = // RegClass of first value in the LR
106 RegClassList[ L->getRegClass()->getID() ];
107
108 RC->addLRToIG(L); // add this LR to an IG
109 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000110 }
111 }
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000112
113 // init RegClassList
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000114 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000115 RegClassList[rc]->createInterferenceGraph();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000116
117 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000118 cerr << "LRLists Created!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000119}
120
121
122
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000123
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000124//----------------------------------------------------------------------------
125// This method will add all interferences at for a given instruction.
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000126// Interence occurs only if the LR of Def (Inst or Arg) is of the same reg
127// class as that of live var. The live var passed to this function is the
128// LVset AFTER the instruction
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000129//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000130void PhyRegAlloc::addInterference(const Value *const Def,
131 const LiveVarSet *const LVSet,
132 const bool isCallInst) {
133
134 LiveVarSet::const_iterator LIt = LVSet->begin();
135
136 // get the live range of instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000137 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000138 const LiveRange *const LROfDef = LRI.getLiveRangeForValue( Def );
139
140 IGNode *const IGNodeOfDef = LROfDef->getUserIGNode();
141 assert( IGNodeOfDef );
142
143 RegClass *const RCOfDef = LROfDef->getRegClass();
144
145 // for each live var in live variable set
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000146 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000147 for( ; LIt != LVSet->end(); ++LIt) {
148
149 if( DEBUG_RA > 1) {
Chris Lattner697954c2002-01-20 22:54:45 +0000150 cerr << "< Def="; printValue(Def);
151 cerr << ", Lvar="; printValue( *LIt); cerr << "> ";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000152 }
153
154 // get the live range corresponding to live var
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000155 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000156 LiveRange *const LROfVar = LRI.getLiveRangeForValue(*LIt );
157
158 // LROfVar can be null if it is a const since a const
159 // doesn't have a dominating def - see Assumptions above
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000160 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000161 if (LROfVar) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000162 if(LROfDef == LROfVar) // do not set interf for same LR
163 continue;
164
165 // if 2 reg classes are the same set interference
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000166 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000167 if(RCOfDef == LROfVar->getRegClass()) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000168 RCOfDef->setInterference( LROfDef, LROfVar);
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000169 } else if(DEBUG_RA > 1) {
170 // we will not have LRs for values not explicitly allocated in the
171 // instruction stream (e.g., constants)
172 cerr << " warning: no live range for " ;
173 printValue(*LIt); cerr << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000174 }
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000175 }
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000176 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000177}
178
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000179
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000180
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000181//----------------------------------------------------------------------------
182// For a call instruction, this method sets the CallInterference flag in
183// the LR of each variable live int the Live Variable Set live after the
184// call instruction (except the return value of the call instruction - since
185// the return value does not interfere with that call itself).
186//----------------------------------------------------------------------------
187
188void PhyRegAlloc::setCallInterferences(const MachineInstr *MInst,
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000189 const LiveVarSet *const LVSetAft ) {
190
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000191 // Now find the LR of the return value of the call
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000192 // We do this because, we look at the LV set *after* the instruction
193 // to determine, which LRs must be saved across calls. The return value
194 // of the call is live in this set - but it does not interfere with call
195 // (i.e., we can allocate a volatile register to the return value)
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000196 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000197 LiveRange *RetValLR = NULL;
Ruchira Sasankab3b6f532001-10-21 16:43:41 +0000198 const Value *RetVal = MRI.getCallInstRetVal( MInst );
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000199
Ruchira Sasankab3b6f532001-10-21 16:43:41 +0000200 if( RetVal ) {
201 RetValLR = LRI.getLiveRangeForValue( RetVal );
202 assert( RetValLR && "No LR for RetValue of call");
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000203 }
204
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000205 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000206 cerr << "\n For call inst: " << *MInst;
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000207
208 LiveVarSet::const_iterator LIt = LVSetAft->begin();
209
210 // for each live var in live variable set after machine inst
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000211 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000212 for( ; LIt != LVSetAft->end(); ++LIt) {
213
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000214 // get the live range corresponding to live var
215 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000216 LiveRange *const LR = LRI.getLiveRangeForValue(*LIt );
217
218 if( LR && DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000219 cerr << "\n\tLR Aft Call: ";
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000220 LR->printSet();
221 }
222
223
224 // LR can be null if it is a const since a const
225 // doesn't have a dominating def - see Assumptions above
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000226 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000227 if( LR && (LR != RetValLR) ) {
228 LR->setCallInterference();
229 if( DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000230 cerr << "\n ++Added call interf for LR: " ;
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000231 LR->printSet();
232 }
233 }
234
235 }
236
237}
238
239
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000240
241
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000242//----------------------------------------------------------------------------
243// This method will walk thru code and create interferences in the IG of
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000244// each RegClass. Also, this method calculates the spill cost of each
245// Live Range (it is done in this method to save another pass over the code).
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000246//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000247void PhyRegAlloc::buildInterferenceGraphs()
248{
249
Chris Lattner697954c2002-01-20 22:54:45 +0000250 if(DEBUG_RA) cerr << "Creating interference graphs ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000251
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000252 unsigned BBLoopDepthCost;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000253 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
254
255 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
256
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000257 // find the 10^(loop_depth) of this BB
258 //
259 BBLoopDepthCost = (unsigned) pow( 10.0, LoopDepthCalc.getLoopDepth(*BBI));
260
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000261 // get the iterator for machine instructions
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000262 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000263 const MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
264 MachineCodeForBasicBlock::const_iterator
265 MInstIterator = MIVec.begin();
266
267 // iterate over all the machine instructions in BB
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000268 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000269 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000270
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +0000271 const MachineInstr * MInst = *MInstIterator;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000272
273 // get the LV set after the instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000274 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000275 const LiveVarSet *const LVSetAI =
276 LVI->getLiveVarSetAfterMInst(MInst, *BBI);
277
278 const bool isCallInst = TM.getInstrInfo().isCall(MInst->getOpCode());
279
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000280 if( isCallInst ) {
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000281 // set the isCallInterference flag of each live range wich extends
282 // accross this call instruction. This information is used by graph
283 // coloring algo to avoid allocating volatile colors to live ranges
284 // that span across calls (since they have to be saved/restored)
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000285 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000286 setCallInterferences( MInst, LVSetAI);
287 }
288
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000289
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000290 // iterate over all MI operands to find defs
291 //
Chris Lattner7a176752001-12-04 00:03:30 +0000292 for( MachineInstr::val_const_op_iterator OpI(MInst);!OpI.done(); ++OpI) {
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000293
294 if( OpI.isDef() ) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000295 // create a new LR iff this operand is a def
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000296 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000297 addInterference(*OpI, LVSetAI, isCallInst );
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000298 }
299
300 // Calculate the spill cost of each live range
301 //
302 LiveRange *LR = LRI.getLiveRangeForValue( *OpI );
303 if( LR )
304 LR->addSpillCost(BBLoopDepthCost);
305 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000306
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000307
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000308 // if there are multiple defs in this instruction e.g. in SETX
309 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000310 if (TM.getInstrInfo().isPseudoInstr(MInst->getOpCode()))
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000311 addInterf4PseudoInstr(MInst);
312
313
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000314 // Also add interference for any implicit definitions in a machine
315 // instr (currently, only calls have this).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000316 //
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000317 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
318 if( NumOfImpRefs > 0 ) {
319 for(unsigned z=0; z < NumOfImpRefs; z++)
320 if( MInst->implicitRefIsDefined(z) )
321 addInterference( MInst->getImplicitRef(z), LVSetAI, isCallInst );
322 }
323
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +0000324
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000325 } // for all machine instructions in BB
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000326
327 } // for all BBs in method
328
329
330 // add interferences for method arguments. Since there are no explict
331 // defs in method for args, we have to add them manually
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000332 //
333 addInterferencesForArgs();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000334
335 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000336 cerr << "Interference graphs calculted!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000337
338}
339
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000340
341
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000342//--------------------------------------------------------------------------
343// Pseudo instructions will be exapnded to multiple instructions by the
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000344// assembler. Consequently, all the opernds must get distinct registers.
345// Therefore, we mark all operands of a pseudo instruction as they interfere
346// with one another.
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000347//--------------------------------------------------------------------------
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000348void PhyRegAlloc::addInterf4PseudoInstr(const MachineInstr *MInst) {
349
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000350 bool setInterf = false;
351
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000352 // iterate over MI operands to find defs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000353 //
Chris Lattner7a176752001-12-04 00:03:30 +0000354 for( MachineInstr::val_const_op_iterator It1(MInst);!It1.done(); ++It1) {
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000355
356 const LiveRange *const LROfOp1 = LRI.getLiveRangeForValue( *It1 );
357
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000358 if( !LROfOp1 && It1.isDef() )
359 assert( 0 && "No LR for Def in PSEUDO insruction");
360
Chris Lattner7a176752001-12-04 00:03:30 +0000361 MachineInstr::val_const_op_iterator It2 = It1;
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000362 ++It2;
363
364 for( ; !It2.done(); ++It2) {
365
366 const LiveRange *const LROfOp2 = LRI.getLiveRangeForValue( *It2 );
367
368 if( LROfOp2) {
369
370 RegClass *const RCOfOp1 = LROfOp1->getRegClass();
371 RegClass *const RCOfOp2 = LROfOp2->getRegClass();
372
373 if( RCOfOp1 == RCOfOp2 ){
374 RCOfOp1->setInterference( LROfOp1, LROfOp2 );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000375 setInterf = true;
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000376 }
377
378 } // if Op2 has a LR
379
380 } // for all other defs in machine instr
381
382 } // for all operands in an instruction
383
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000384 if( !setInterf && (MInst->getNumOperands() > 2) ) {
385 cerr << "\nInterf not set for any operand in pseudo instr:\n";
386 cerr << *MInst;
387 assert(0 && "Interf not set for pseudo instr with > 2 operands" );
388
389 }
390
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000391}
392
393
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000394
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000395//----------------------------------------------------------------------------
396// This method will add interferences for incoming arguments to a method.
397//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000398void PhyRegAlloc::addInterferencesForArgs()
399{
400 // get the InSet of root BB
401 const LiveVarSet *const InSet = LVI->getInSetOfBB( Meth->front() );
402
403 // get the argument list
404 const Method::ArgumentListType& ArgList = Meth->getArgumentList();
405
406 // get an iterator to arg list
407 Method::ArgumentListType::const_iterator ArgIt = ArgList.begin();
408
409
410 for( ; ArgIt != ArgList.end() ; ++ArgIt) { // for each argument
411 addInterference( *ArgIt, InSet, false ); // add interferences between
412 // args and LVars at start
413 if( DEBUG_RA > 1) {
Chris Lattner697954c2002-01-20 22:54:45 +0000414 cerr << " - %% adding interference for argument ";
415 printValue((const Value *)*ArgIt); cerr << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000416 }
417 }
418}
419
420
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000421
422
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000423//----------------------------------------------------------------------------
424// This method is called after register allocation is complete to set the
425// allocated reisters in the machine code. This code will add register numbers
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000426// to MachineOperands that contain a Value. Also it calls target specific
427// methods to produce caller saving instructions. At the end, it adds all
428// additional instructions produced by the register allocator to the
429// instruction stream.
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000430//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000431void PhyRegAlloc::updateMachineCode()
432{
433
434 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
435
436 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
437
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000438 // get the iterator for machine instructions
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000439 //
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000440 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
441 MachineCodeForBasicBlock::iterator MInstIterator = MIVec.begin();
442
443 // iterate over all the machine instructions in BB
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000444 //
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000445 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
446
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000447 MachineInstr *MInst = *MInstIterator;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000448
449 unsigned Opcode = MInst->getOpCode();
450
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000451 // do not process Phis
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000452 if (TM.getInstrInfo().isPhi(Opcode))
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000453 continue;
454
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000455 // Now insert speical instructions (if necessary) for call/return
456 // instructions.
457 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000458 if (TM.getInstrInfo().isCall(Opcode) ||
459 TM.getInstrInfo().isReturn(Opcode)) {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000460
461 AddedInstrns *AI = AddedInstrMap[ MInst];
462 if ( !AI ) {
463 AI = new AddedInstrns();
464 AddedInstrMap[ MInst ] = AI;
465 }
466
467 // Tmp stack poistions are needed by some calls that have spilled args
468 // So reset it before we call each such method
Ruchira Sasanka6a3db8c2002-01-07 21:09:06 +0000469 //
470 mcInfo.popAllTempValues(TM);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000471
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000472 if (TM.getInstrInfo().isCall(Opcode))
473 MRI.colorCallArgs(MInst, LRI, AI, *this, *BBI);
474 else if (TM.getInstrInfo().isReturn(Opcode))
475 MRI.colorRetValue(MInst, LRI, AI);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000476 }
477
478
479 /* -- Using above code instead of this
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000480
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000481 // if this machine instr is call, insert caller saving code
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000482
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000483 if( (TM.getInstrInfo()).isCall( MInst->getOpCode()) )
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000484 MRI.insertCallerSavingCode(MInst, *BBI, *this );
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000485
486 */
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000487
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000488
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000489 // reset the stack offset for temporary variables since we may
490 // need that to spill
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000491 // mcInfo.popAllTempValues(TM);
Ruchira Sasankaf90870f2001-11-15 22:02:06 +0000492 // TODO ** : do later
Vikram S. Adve12af1642001-11-08 04:48:50 +0000493
Chris Lattner7a176752001-12-04 00:03:30 +0000494 //for(MachineInstr::val_const_op_iterator OpI(MInst);!OpI.done();++OpI) {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000495
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000496
497 // Now replace set the registers for operands in the machine instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000498 //
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000499 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
500
501 MachineOperand& Op = MInst->getOperand(OpNum);
502
503 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
504 Op.getOperandType() == MachineOperand::MO_CCRegister) {
505
506 const Value *const Val = Op.getVRegValue();
507
508 // delete this condition checking later (must assert if Val is null)
Chris Lattner045e7c82001-09-19 16:26:23 +0000509 if( !Val) {
510 if (DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000511 cerr << "Warning: NULL Value found for operand\n";
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000512 continue;
513 }
514 assert( Val && "Value is NULL");
515
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000516 LiveRange *const LR = LRI.getLiveRangeForValue(Val);
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000517
518 if ( !LR ) {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000519
520 // nothing to worry if it's a const or a label
521
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000522 if (DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000523 cerr << "*NO LR for operand : " << Op ;
524 cerr << " [reg:" << Op.getAllocatedRegNum() << "]";
525 cerr << " in inst:\t" << *MInst << "\n";
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000526 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000527
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000528 // if register is not allocated, mark register as invalid
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000529 if( Op.getAllocatedRegNum() == -1)
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000530 Op.setRegForValue( MRI.getInvalidRegNum());
Ruchira Sasankae727f852001-09-18 22:43:57 +0000531
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000532
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000533 continue;
534 }
535
536 unsigned RCID = (LR->getRegClass())->getID();
537
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000538 if( LR->hasColor() ) {
539 Op.setRegForValue( MRI.getUnifiedRegNum(RCID, LR->getColor()) );
540 }
541 else {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000542
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000543 // LR did NOT receive a color (register). Now, insert spill code
544 // for spilled opeands in this machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000545
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000546 //assert(0 && "LR must be spilled");
547 insertCode4SpilledLR(LR, MInst, *BBI, OpNum );
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000548
549 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000550 }
551
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000552 } // for each operand
553
554
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000555 // Now add instructions that the register allocator inserts before/after
556 // this machine instructions (done only for calls/rets/incoming args)
557 // We do this here, to ensure that spill for an instruction is inserted
558 // closest as possible to an instruction (see above insertCode4Spill...)
559 //
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000560 // If there are instructions to be added, *before* this machine
561 // instruction, add them now.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000562 //
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000563 if( AddedInstrMap[ MInst ] ) {
Chris Lattner697954c2002-01-20 22:54:45 +0000564 std::deque<MachineInstr *> &IBef = AddedInstrMap[MInst]->InstrnsBefore;
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000565
566 if( ! IBef.empty() ) {
Chris Lattner697954c2002-01-20 22:54:45 +0000567 std::deque<MachineInstr *>::iterator AdIt;
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000568
569 for( AdIt = IBef.begin(); AdIt != IBef.end() ; ++AdIt ) {
570
571 if( DEBUG_RA) {
572 cerr << "For inst " << *MInst;
Chris Lattner697954c2002-01-20 22:54:45 +0000573 cerr << " PREPENDed instr: " << **AdIt << "\n";
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000574 }
575
576 MInstIterator = MIVec.insert( MInstIterator, *AdIt );
577 ++MInstIterator;
578 }
579
580 }
581
582 }
583
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000584 // If there are instructions to be added *after* this machine
585 // instruction, add them now
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000586 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000587 if(AddedInstrMap[MInst] &&
588 !AddedInstrMap[MInst]->InstrnsAfter.empty() ) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000589
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000590 // if there are delay slots for this instruction, the instructions
591 // added after it must really go after the delayed instruction(s)
592 // So, we move the InstrAfter of the current instruction to the
593 // corresponding delayed instruction
594
595 unsigned delay;
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000596 if ((delay=TM.getInstrInfo().getNumDelaySlots(MInst->getOpCode())) >0){
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000597 move2DelayedInstr(MInst, *(MInstIterator+delay) );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000598
Chris Lattner697954c2002-01-20 22:54:45 +0000599 if(DEBUG_RA) cerr<< "\nMoved an added instr after the delay slot";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000600 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000601
602 else {
603
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000604
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000605 // Here we can add the "instructions after" to the current
606 // instruction since there are no delay slots for this instruction
607
Chris Lattner697954c2002-01-20 22:54:45 +0000608 std::deque<MachineInstr *> &IAft = AddedInstrMap[MInst]->InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000609
610 if( ! IAft.empty() ) {
611
Chris Lattner697954c2002-01-20 22:54:45 +0000612 std::deque<MachineInstr *>::iterator AdIt;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000613
614 ++MInstIterator; // advance to the next instruction
615
616 for( AdIt = IAft.begin(); AdIt != IAft.end() ; ++AdIt ) {
617
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000618 if(DEBUG_RA) {
619 cerr << "For inst " << *MInst;
Chris Lattner697954c2002-01-20 22:54:45 +0000620 cerr << " APPENDed instr: " << **AdIt << "\n";
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000621 }
622
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000623 MInstIterator = MIVec.insert( MInstIterator, *AdIt );
624 ++MInstIterator;
625 }
626
627 // MInsterator already points to the next instr. Since the
628 // for loop also increments it, decrement it to point to the
629 // instruction added last
630 --MInstIterator;
631
632 }
633
634 } // if not delay
635
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000636 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000637
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000638 } // for each machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000639 }
640}
641
642
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000643
644//----------------------------------------------------------------------------
645// This method inserts spill code for AN operand whose LR was spilled.
646// This method may be called several times for a single machine instruction
647// if it contains many spilled operands. Each time it is called, it finds
648// a register which is not live at that instruction and also which is not
649// used by other spilled operands of the same instruction. Then it uses
650// this register temporarily to accomodate the spilled value.
651//----------------------------------------------------------------------------
652void PhyRegAlloc::insertCode4SpilledLR(const LiveRange *LR,
653 MachineInstr *MInst,
654 const BasicBlock *BB,
655 const unsigned OpNum) {
656
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000657 assert(! TM.getInstrInfo().isCall(MInst->getOpCode()) &&
658 (! TM.getInstrInfo().isReturn(MInst->getOpCode())) &&
659 "Arg of a call/ret must be handled elsewhere");
660
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000661 MachineOperand& Op = MInst->getOperand(OpNum);
662 bool isDef = MInst->operandIsDefined(OpNum);
663 unsigned RegType = MRI.getRegType( LR );
664 int SpillOff = LR->getSpillOffFromFP();
665 RegClass *RC = LR->getRegClass();
666 const LiveVarSet *LVSetBef = LVI->getLiveVarSetBeforeMInst(MInst, BB);
Vikram S. Adve00521d72001-11-12 23:26:35 +0000667
Chris Lattner697954c2002-01-20 22:54:45 +0000668 mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000669
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000670 MachineInstr *MIBef=NULL, *AdIMid=NULL, *MIAft=NULL;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000671
672 int TmpRegU = getUsableUniRegAtMI(RC, RegType, MInst,LVSetBef, MIBef, MIAft);
673
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000674 // get the added instructions for this instruciton
675 AddedInstrns *AI = AddedInstrMap[ MInst ];
676 if ( !AI ) {
677 AI = new AddedInstrns();
678 AddedInstrMap[ MInst ] = AI;
679 }
680
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000681
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000682 if( !isDef ) {
683
684 // for a USE, we have to load the value of LR from stack to a TmpReg
685 // and use the TmpReg as one operand of instruction
686
687 // actual loading instruction
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000688 AdIMid = MRI.cpMem2RegMI(MRI.getFramePointer(), SpillOff, TmpRegU,RegType);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000689
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000690 if(MIBef)
691 AI->InstrnsBefore.push_back(MIBef);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000692
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000693 AI->InstrnsBefore.push_back(AdIMid);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000694
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000695 if(MIAft)
696 AI->InstrnsAfter.push_front(MIAft);
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000697
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000698
699 }
700 else { // if this is a Def
701
702 // for a DEF, we have to store the value produced by this instruction
703 // on the stack position allocated for this LR
704
705 // actual storing instruction
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000706 AdIMid = MRI.cpReg2MemMI(TmpRegU, MRI.getFramePointer(), SpillOff,RegType);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000707
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000708 if (MIBef)
709 AI->InstrnsBefore.push_back(MIBef);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000710
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000711 AI->InstrnsAfter.push_front(AdIMid);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000712
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000713 if (MIAft)
714 AI->InstrnsAfter.push_front(MIAft);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000715
716 } // if !DEF
717
718 cerr << "\nFor Inst " << *MInst;
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000719 cerr << " - SPILLED LR: "; LR->printSet();
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000720 cerr << "\n - Added Instructions:";
721 if( MIBef ) cerr << *MIBef;
722 cerr << *AdIMid;
723 if( MIAft ) cerr << *MIAft;
724
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000725 Op.setRegForValue( TmpRegU ); // set the opearnd
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000726
727
728}
729
730
731
732
733
734
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000735//----------------------------------------------------------------------------
736// We can use the following method to get a temporary register to be used
737// BEFORE any given machine instruction. If there is a register available,
738// this method will simply return that register and set MIBef = MIAft = NULL.
739// Otherwise, it will return a register and MIAft and MIBef will contain
740// two instructions used to free up this returned register.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000741// Returned register number is the UNIFIED register number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000742//----------------------------------------------------------------------------
743
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000744int PhyRegAlloc::getUsableUniRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000745 const int RegType,
746 const MachineInstr *MInst,
747 const LiveVarSet *LVSetBef,
748 MachineInstr *MIBef,
749 MachineInstr *MIAft) {
750
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000751 int RegU = getUnusedUniRegAtMI(RC, MInst, LVSetBef);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000752
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000753
754 if( RegU != -1) {
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000755 // we found an unused register, so we can simply use it
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000756 MIBef = MIAft = NULL;
757 }
758 else {
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000759 // we couldn't find an unused register. Generate code to free up a reg by
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000760 // saving it on stack and restoring after the instruction
761
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000762 int TmpOff = mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
Vikram S. Adve12af1642001-11-08 04:48:50 +0000763
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000764 RegU = getUniRegNotUsedByThisInst(RC, MInst);
765 MIBef = MRI.cpReg2MemMI(RegU, MRI.getFramePointer(), TmpOff, RegType );
766 MIAft = MRI.cpMem2RegMI(MRI.getFramePointer(), TmpOff, RegU, RegType );
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000767 }
768
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000769 return RegU;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000770}
771
772//----------------------------------------------------------------------------
773// This method is called to get a new unused register that can be used to
774// accomodate a spilled value.
775// This method may be called several times for a single machine instruction
776// if it contains many spilled operands. Each time it is called, it finds
777// a register which is not live at that instruction and also which is not
778// used by other spilled operands of the same instruction.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000779// Return register number is relative to the register class. NOT
780// unified number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000781//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000782int PhyRegAlloc::getUnusedUniRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000783 const MachineInstr *MInst,
784 const LiveVarSet *LVSetBef) {
785
786 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
787
788 bool *IsColorUsedArr = RC->getIsColorUsedArr();
789
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000790 for(unsigned i=0; i < NumAvailRegs; i++) // Reset array
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000791 IsColorUsedArr[i] = false;
792
793 LiveVarSet::const_iterator LIt = LVSetBef->begin();
794
795 // for each live var in live variable set after machine inst
796 for( ; LIt != LVSetBef->end(); ++LIt) {
797
798 // get the live range corresponding to live var
799 LiveRange *const LRofLV = LRI.getLiveRangeForValue(*LIt );
800
801 // LR can be null if it is a const since a const
802 // doesn't have a dominating def - see Assumptions above
803 if( LRofLV )
804 if( LRofLV->hasColor() )
805 IsColorUsedArr[ LRofLV->getColor() ] = true;
806 }
807
808 // It is possible that one operand of this MInst was already spilled
809 // and it received some register temporarily. If that's the case,
810 // it is recorded in machine operand. We must skip such registers.
811
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000812 setRelRegsUsedByThisInst(RC, MInst);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000813
814 unsigned c; // find first unused color
815 for( c=0; c < NumAvailRegs; c++)
816 if( ! IsColorUsedArr[ c ] ) break;
817
818 if(c < NumAvailRegs)
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000819 return MRI.getUnifiedRegNum(RC->getID(), c);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000820 else
821 return -1;
822
823
824}
825
826
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000827//----------------------------------------------------------------------------
828// Get any other register in a register class, other than what is used
829// by operands of a machine instruction. Returns the unified reg number.
830//----------------------------------------------------------------------------
831int PhyRegAlloc::getUniRegNotUsedByThisInst(RegClass *RC,
832 const MachineInstr *MInst) {
833
834 bool *IsColorUsedArr = RC->getIsColorUsedArr();
835 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
836
837
838 for(unsigned i=0; i < NumAvailRegs ; i++) // Reset array
839 IsColorUsedArr[i] = false;
840
841 setRelRegsUsedByThisInst(RC, MInst);
842
843 unsigned c; // find first unused color
844 for( c=0; c < RC->getNumOfAvailRegs(); c++)
845 if( ! IsColorUsedArr[ c ] ) break;
846
847 if(c < NumAvailRegs)
848 return MRI.getUnifiedRegNum(RC->getID(), c);
849 else
850 assert( 0 && "FATAL: No free register could be found in reg class!!");
Chris Lattner697954c2002-01-20 22:54:45 +0000851 return 0;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000852}
853
854
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000855//----------------------------------------------------------------------------
856// This method modifies the IsColorUsedArr of the register class passed to it.
857// It sets the bits corresponding to the registers used by this machine
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000858// instructions. Both explicit and implicit operands are set.
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000859//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000860void PhyRegAlloc::setRelRegsUsedByThisInst(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000861 const MachineInstr *MInst ) {
862
863 bool *IsColorUsedArr = RC->getIsColorUsedArr();
864
865 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
866
867 const MachineOperand& Op = MInst->getOperand(OpNum);
868
869 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000870 Op.getOperandType() == MachineOperand::MO_CCRegister ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000871
872 const Value *const Val = Op.getVRegValue();
873
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000874 if( Val )
875 if( MRI.getRegClassIDOfValue(Val) == RC->getID() ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000876 int Reg;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000877 if( (Reg=Op.getAllocatedRegNum()) != -1) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000878 IsColorUsedArr[ Reg ] = true;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000879 }
880 else {
881 // it is possilbe that this operand still is not marked with
882 // a register but it has a LR and that received a color
883
884 LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
885 if( LROfVal)
886 if( LROfVal->hasColor() )
887 IsColorUsedArr[ LROfVal->getColor() ] = true;
888 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000889
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000890 } // if reg classes are the same
891 }
892 else if (Op.getOperandType() == MachineOperand::MO_MachineRegister) {
893 IsColorUsedArr[ Op.getMachineRegNum() ] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000894 }
895 }
896
897 // If there are implicit references, mark them as well
898
899 for(unsigned z=0; z < MInst->getNumImplicitRefs(); z++) {
900
901 LiveRange *const LRofImpRef =
902 LRI.getLiveRangeForValue( MInst->getImplicitRef(z) );
Chris Lattner697954c2002-01-20 22:54:45 +0000903
904 if(LRofImpRef && LRofImpRef->hasColor())
905 IsColorUsedArr[LRofImpRef->getColor()] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000906 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000907}
908
909
910
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000911
912
913
914
915
916//----------------------------------------------------------------------------
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000917// If there are delay slots for an instruction, the instructions
918// added after it must really go after the delayed instruction(s).
919// So, we move the InstrAfter of that instruction to the
920// corresponding delayed instruction using the following method.
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000921
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000922//----------------------------------------------------------------------------
923void PhyRegAlloc:: move2DelayedInstr(const MachineInstr *OrigMI,
924 const MachineInstr *DelayedMI) {
925
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000926 // "added after" instructions of the original instr
Chris Lattner697954c2002-01-20 22:54:45 +0000927 std::deque<MachineInstr *> &OrigAft = AddedInstrMap[OrigMI]->InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000928
929 // "added instructions" of the delayed instr
930 AddedInstrns *DelayAdI = AddedInstrMap[DelayedMI];
931
932 if(! DelayAdI ) { // create a new "added after" if necessary
933 DelayAdI = new AddedInstrns();
934 AddedInstrMap[DelayedMI] = DelayAdI;
935 }
936
937 // "added after" instructions of the delayed instr
Chris Lattner697954c2002-01-20 22:54:45 +0000938 std::deque<MachineInstr *> &DelayedAft = DelayAdI->InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000939
940 // go thru all the "added after instructions" of the original instruction
941 // and append them to the "addded after instructions" of the delayed
942 // instructions
Chris Lattner697954c2002-01-20 22:54:45 +0000943 DelayedAft.insert(DelayedAft.end(), OrigAft.begin(), OrigAft.end());
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000944
945 // empty the "added after instructions" of the original instruction
946 OrigAft.clear();
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000947}
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000948
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000949//----------------------------------------------------------------------------
950// This method prints the code with registers after register allocation is
951// complete.
952//----------------------------------------------------------------------------
953void PhyRegAlloc::printMachineCode()
954{
955
Chris Lattner697954c2002-01-20 22:54:45 +0000956 cerr << "\n;************** Method " << Meth->getName()
957 << " *****************\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000958
959 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
960
961 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
962
Chris Lattner697954c2002-01-20 22:54:45 +0000963 cerr << "\n"; printLabel( *BBI); cerr << ": ";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000964
965 // get the iterator for machine instructions
966 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
967 MachineCodeForBasicBlock::iterator MInstIterator = MIVec.begin();
968
969 // iterate over all the machine instructions in BB
970 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
971
972 MachineInstr *const MInst = *MInstIterator;
973
974
Chris Lattner697954c2002-01-20 22:54:45 +0000975 cerr << "\n\t";
976 cerr << TargetInstrDescriptors[MInst->getOpCode()].opCodeString;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000977
978
Chris Lattner7a176752001-12-04 00:03:30 +0000979 //for(MachineInstr::val_const_op_iterator OpI(MInst);!OpI.done();++OpI) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000980
981 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
982
983 MachineOperand& Op = MInst->getOperand(OpNum);
984
985 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasanka97b8b442001-10-18 22:36:26 +0000986 Op.getOperandType() == MachineOperand::MO_CCRegister /*||
987 Op.getOperandType() == MachineOperand::MO_PCRelativeDisp*/ ) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000988
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000989 const Value *const Val = Op.getVRegValue () ;
Ruchira Sasankae727f852001-09-18 22:43:57 +0000990 // ****this code is temporary till NULL Values are fixed
991 if( ! Val ) {
Chris Lattner697954c2002-01-20 22:54:45 +0000992 cerr << "\t<*NULL*>";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000993 continue;
994 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000995
996 // if a label or a constant
Chris Lattnerdbe53042002-01-21 01:33:12 +0000997 if(isa<BasicBlock>(Val)) {
Chris Lattner697954c2002-01-20 22:54:45 +0000998 cerr << "\t"; printLabel( Op.getVRegValue () );
999 } else {
Ruchira Sasankae727f852001-09-18 22:43:57 +00001000 // else it must be a register value
1001 const int RegNum = Op.getAllocatedRegNum();
1002
Chris Lattner697954c2002-01-20 22:54:45 +00001003 cerr << "\t" << "%" << MRI.getUnifiedRegName( RegNum );
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001004 if (Val->hasName() )
Chris Lattner697954c2002-01-20 22:54:45 +00001005 cerr << "(" << Val->getName() << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001006 else
Chris Lattner697954c2002-01-20 22:54:45 +00001007 cerr << "(" << Val << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001008
1009 if( Op.opIsDef() )
Chris Lattner697954c2002-01-20 22:54:45 +00001010 cerr << "*";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001011
1012 const LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
1013 if( LROfVal )
1014 if( LROfVal->hasSpillOffset() )
Chris Lattner697954c2002-01-20 22:54:45 +00001015 cerr << "$";
Ruchira Sasankae727f852001-09-18 22:43:57 +00001016 }
1017
1018 }
1019 else if(Op.getOperandType() == MachineOperand::MO_MachineRegister) {
Chris Lattner697954c2002-01-20 22:54:45 +00001020 cerr << "\t" << "%" << MRI.getUnifiedRegName(Op.getMachineRegNum());
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001021 }
1022
1023 else
Chris Lattner697954c2002-01-20 22:54:45 +00001024 cerr << "\t" << Op; // use dump field
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001025 }
1026
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001027
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001028
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001029 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
1030 if( NumOfImpRefs > 0 ) {
1031
Chris Lattner697954c2002-01-20 22:54:45 +00001032 cerr << "\tImplicit:";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001033
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001034 for(unsigned z=0; z < NumOfImpRefs; z++) {
1035 printValue( MInst->getImplicitRef(z) );
Chris Lattner697954c2002-01-20 22:54:45 +00001036 cerr << "\t";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001037 }
1038
1039 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001040
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001041 } // for all machine instructions
1042
Chris Lattner697954c2002-01-20 22:54:45 +00001043 cerr << "\n";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001044
1045 } // for all BBs
1046
Chris Lattner697954c2002-01-20 22:54:45 +00001047 cerr << "\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001048}
1049
Ruchira Sasankae727f852001-09-18 22:43:57 +00001050
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001051#if 0
1052
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001053//----------------------------------------------------------------------------
1054//
1055//----------------------------------------------------------------------------
1056
1057void PhyRegAlloc::colorCallRetArgs()
1058{
1059
1060 CallRetInstrListType &CallRetInstList = LRI.getCallRetInstrList();
1061 CallRetInstrListType::const_iterator It = CallRetInstList.begin();
1062
1063 for( ; It != CallRetInstList.end(); ++It ) {
1064
Ruchira Sasankaa90e7702001-10-15 16:26:38 +00001065 const MachineInstr *const CRMI = *It;
1066 unsigned OpCode = CRMI->getOpCode();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001067
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001068 // get the added instructions for this Call/Ret instruciton
1069 AddedInstrns *AI = AddedInstrMap[ CRMI ];
1070 if ( !AI ) {
1071 AI = new AddedInstrns();
1072 AddedInstrMap[ CRMI ] = AI;
1073 }
1074
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001075 // Tmp stack poistions are needed by some calls that have spilled args
1076 // So reset it before we call each such method
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001077 //mcInfo.popAllTempValues(TM);
1078
1079
Vikram S. Adve12af1642001-11-08 04:48:50 +00001080
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001081 if (TM.getInstrInfo().isCall(OpCode))
1082 MRI.colorCallArgs(CRMI, LRI, AI, *this);
1083 else if (TM.getInstrInfo().isReturn(OpCode))
Ruchira Sasankaa90e7702001-10-15 16:26:38 +00001084 MRI.colorRetValue( CRMI, LRI, AI );
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001085 else
1086 assert(0 && "Non Call/Ret instrn in CallRetInstrList\n");
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001087 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001088}
1089
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001090#endif
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001091
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001092//----------------------------------------------------------------------------
1093
1094//----------------------------------------------------------------------------
1095void PhyRegAlloc::colorIncomingArgs()
1096{
1097 const BasicBlock *const FirstBB = Meth->front();
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001098 const MachineInstr *FirstMI = FirstBB->getMachineInstrVec().front();
1099 assert(FirstMI && "No machine instruction in entry BB");
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001100
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001101 AddedInstrns *AI = AddedInstrMap[FirstMI];
1102 if (!AI)
1103 AddedInstrMap[FirstMI] = AI = new AddedInstrns();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001104
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001105 MRI.colorMethodArgs(Meth, LRI, AI);
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001106}
1107
Ruchira Sasankae727f852001-09-18 22:43:57 +00001108
1109//----------------------------------------------------------------------------
1110// Used to generate a label for a basic block
1111//----------------------------------------------------------------------------
Chris Lattner697954c2002-01-20 22:54:45 +00001112void PhyRegAlloc::printLabel(const Value *const Val) {
1113 if (Val->hasName())
1114 cerr << Val->getName();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001115 else
Chris Lattner697954c2002-01-20 22:54:45 +00001116 cerr << "Label" << Val;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001117}
1118
1119
Ruchira Sasankae727f852001-09-18 22:43:57 +00001120//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001121// This method calls setSugColorUsable method of each live range. This
1122// will determine whether the suggested color of LR is really usable.
1123// A suggested color is not usable when the suggested color is volatile
1124// AND when there are call interferences
1125//----------------------------------------------------------------------------
1126
1127void PhyRegAlloc::markUnusableSugColors()
1128{
Chris Lattner697954c2002-01-20 22:54:45 +00001129 if(DEBUG_RA ) cerr << "\nmarking unusable suggested colors ...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001130
1131 // hash map iterator
1132 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
1133 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
1134
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001135 for(; HMI != HMIEnd ; ++HMI ) {
1136 if (HMI->first) {
1137 LiveRange *L = HMI->second; // get the LiveRange
1138 if (L) {
1139 if(L->hasSuggestedColor()) {
1140 int RCID = L->getRegClass()->getID();
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001141 if( MRI.isRegVolatile( RCID, L->getSuggestedColor()) &&
1142 L->isCallInterference() )
1143 L->setSuggestedColorUsable( false );
1144 else
1145 L->setSuggestedColorUsable( true );
1146 }
1147 } // if L->hasSuggestedColor()
1148 }
1149 } // for all LR's in hash map
1150}
1151
1152
1153
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001154//----------------------------------------------------------------------------
1155// The following method will set the stack offsets of the live ranges that
1156// are decided to be spillled. This must be called just after coloring the
1157// LRs using the graph coloring algo. For each live range that is spilled,
1158// this method allocate a new spill position on the stack.
1159//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001160
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001161void PhyRegAlloc::allocateStackSpace4SpilledLRs()
1162{
Chris Lattner697954c2002-01-20 22:54:45 +00001163 if(DEBUG_RA ) cerr << "\nsetting LR stack offsets ...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001164
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001165 // hash map iterator
1166 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
1167 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
1168
1169 for( ; HMI != HMIEnd ; ++HMI ) {
Chris Lattner697954c2002-01-20 22:54:45 +00001170 if(HMI->first && HMI->second) {
1171 LiveRange *L = HMI->second; // get the LiveRange
1172 if( ! L->hasColor() )
1173 // NOTE: ** allocating the size of long Type **
1174 L->setSpillOffFromFP(mcInfo.allocateSpilledValue(TM, Type::LongTy));
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001175 }
1176 } // for all LR's in hash map
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001177}
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001178
1179
1180
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001181//----------------------------------------------------------------------------
Ruchira Sasankae727f852001-09-18 22:43:57 +00001182// The entry pont to Register Allocation
1183//----------------------------------------------------------------------------
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001184
1185void PhyRegAlloc::allocateRegisters()
1186{
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001187
1188 // make sure that we put all register classes into the RegClassList
1189 // before we call constructLiveRanges (now done in the constructor of
1190 // PhyRegAlloc class).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001191 //
1192 LRI.constructLiveRanges(); // create LR info
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001193
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001194 if (DEBUG_RA)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001195 LRI.printLiveRanges();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001196
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001197 createIGNodeListsAndIGs(); // create IGNode list and IGs
1198
1199 buildInterferenceGraphs(); // build IGs in all reg classes
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001200
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001201
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001202 if (DEBUG_RA) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001203 // print all LRs in all reg classes
1204 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1205 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001206
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001207 // print IGs in all register classes
1208 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1209 RegClassList[ rc ]->printIG();
1210 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001211
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001212
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001213 LRI.coalesceLRs(); // coalesce all live ranges
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001214
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +00001215
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001216 if( DEBUG_RA) {
1217 // print all LRs in all reg classes
1218 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1219 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001220
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001221 // print IGs in all register classes
1222 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1223 RegClassList[ rc ]->printIG();
1224 }
1225
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001226
1227 // mark un-usable suggested color before graph coloring algorithm.
1228 // When this is done, the graph coloring algo will not reserve
1229 // suggested color unnecessarily - they can be used by another LR
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001230 //
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001231 markUnusableSugColors();
1232
1233 // color all register classes using the graph coloring algo
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001234 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1235 RegClassList[ rc ]->colorAllRegs();
1236
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001237 // Atter grpah coloring, if some LRs did not receive a color (i.e, spilled)
1238 // a poistion for such spilled LRs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001239 //
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001240 allocateStackSpace4SpilledLRs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001241
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001242 mcInfo.popAllTempValues(TM); // TODO **Check
1243
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001244 // color incoming args - if the correct color was not received
1245 // insert code to copy to the correct register
1246 //
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001247 colorIncomingArgs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001248
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001249 // Now update the machine code with register names and add any
1250 // additional code inserted by the register allocator to the instruction
1251 // stream
1252 //
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001253 updateMachineCode();
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001254
Chris Lattner045e7c82001-09-19 16:26:23 +00001255 if (DEBUG_RA) {
Vikram S. Adve12af1642001-11-08 04:48:50 +00001256 MachineCodeForMethod::get(Meth).dump();
Chris Lattner045e7c82001-09-19 16:26:23 +00001257 printMachineCode(); // only for DEBUGGING
1258 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001259}
1260
Ruchira Sasankae727f852001-09-18 22:43:57 +00001261
1262