blob: 422541c5845e565c7bfbc92d571b10847d7ee332 [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 Lattner30adeb62002-02-04 16:36:59 +000020#include "llvm/Method.h"
Chris Lattner697954c2002-01-20 22:54:45 +000021#include <iostream>
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000022#include <math.h>
Chris Lattner697954c2002-01-20 22:54:45 +000023using std::cerr;
Vikram S. Adve12af1642001-11-08 04:48:50 +000024
25
26// ***TODO: There are several places we add instructions. Validate the order
27// of adding these instructions.
Ruchira Sasanka174bded2001-10-28 18:12:02 +000028
Chris Lattner045e7c82001-09-19 16:26:23 +000029cl::Enum<RegAllocDebugLevel_t> DEBUG_RA("dregalloc", cl::NoFlags,
30 "enable register allocation debugging information",
31 clEnumValN(RA_DEBUG_None , "n", "disable debug output"),
32 clEnumValN(RA_DEBUG_Normal , "y", "enable debug output"),
33 clEnumValN(RA_DEBUG_Verbose, "v", "enable extra debug output"), 0);
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000034
35
Chris Lattner2f9b28e2002-02-04 15:54:09 +000036//----------------------------------------------------------------------------
37// RegisterAllocation pass front end...
38//----------------------------------------------------------------------------
39namespace {
40 class RegisterAllocator : public MethodPass {
41 TargetMachine &Target;
42 public:
43 inline RegisterAllocator(TargetMachine &T) : Target(T) {}
Chris Lattner6dd98a62002-02-04 00:33:08 +000044
Chris Lattner2f9b28e2002-02-04 15:54:09 +000045 bool runOnMethod(Method *M) {
46 if (DEBUG_RA)
47 cerr << "\n******************** Method "<< M->getName()
48 << " ********************\n";
49
50 MethodLiveVarInfo LVI(M); // Analyze live varaibles
51 LVI.analyze();
52
53 PhyRegAlloc PRA(M, Target, &LVI); // allocate registers
54 PRA.allocateRegisters();
55
56 if (DEBUG_RA) cerr << "\nRegister allocation complete!\n";
57 return false;
58 }
59 };
Chris Lattner6dd98a62002-02-04 00:33:08 +000060}
61
Chris Lattner2f9b28e2002-02-04 15:54:09 +000062MethodPass *getRegisterAllocator(TargetMachine &T) {
63 return new RegisterAllocator(T);
64}
Chris Lattner6dd98a62002-02-04 00:33:08 +000065
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000066//----------------------------------------------------------------------------
67// Constructor: Init local composite objects and create register classes.
68//----------------------------------------------------------------------------
Vikram S. Adve12af1642001-11-08 04:48:50 +000069PhyRegAlloc::PhyRegAlloc(Method *M,
Ruchira Sasanka8e604792001-09-14 21:18:34 +000070 const TargetMachine& tm,
71 MethodLiveVarInfo *const Lvi)
Chris Lattner697954c2002-01-20 22:54:45 +000072 : TM(tm), Meth(M),
Vikram S. Adve12af1642001-11-08 04:48:50 +000073 mcInfo(MachineCodeForMethod::get(M)),
74 LVI(Lvi), LRI(M, tm, RegClassList),
Ruchira Sasanka8e604792001-09-14 21:18:34 +000075 MRI( tm.getRegInfo() ),
76 NumOfRegClasses(MRI.getNumOfRegClasses()),
Chris Lattner697954c2002-01-20 22:54:45 +000077 LoopDepthCalc(M) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +000078
79 // create each RegisterClass and put in RegClassList
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000080 //
Chris Lattner697954c2002-01-20 22:54:45 +000081 for(unsigned int rc=0; rc < NumOfRegClasses; rc++)
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000082 RegClassList.push_back( new RegClass(M, MRI.getMachineRegClass(rc),
83 &ResColList) );
Ruchira Sasanka8e604792001-09-14 21:18:34 +000084}
85
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000086
87//----------------------------------------------------------------------------
88// Destructor: Deletes register classes
89//----------------------------------------------------------------------------
90PhyRegAlloc::~PhyRegAlloc() {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000091 for( unsigned int rc=0; rc < NumOfRegClasses; rc++)
92 delete RegClassList[rc];
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000093}
94
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000095//----------------------------------------------------------------------------
96// This method initally creates interference graphs (one in each reg class)
97// and IGNodeList (one in each IG). The actual nodes will be pushed later.
98//----------------------------------------------------------------------------
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000099void PhyRegAlloc::createIGNodeListsAndIGs() {
100 if (DEBUG_RA) cerr << "Creating LR lists ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000101
102 // hash map iterator
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000103 LiveRangeMapType::const_iterator HMI = LRI.getLiveRangeMap()->begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000104
105 // hash map end
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000106 LiveRangeMapType::const_iterator HMIEnd = LRI.getLiveRangeMap()->end();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000107
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000108 for (; HMI != HMIEnd ; ++HMI ) {
109 if (HMI->first) {
110 LiveRange *L = HMI->second; // get the LiveRange
111 if (!L) {
112 if( DEBUG_RA) {
113 cerr << "\n*?!?Warning: Null liver range found for: ";
114 printValue(HMI->first); cerr << "\n";
115 }
116 continue;
117 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000118 // if the Value * is not null, and LR
119 // is not yet written to the IGNodeList
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000120 if( !(L->getUserIGNode()) ) {
121 RegClass *const RC = // RegClass of first value in the LR
122 RegClassList[ L->getRegClass()->getID() ];
123
124 RC->addLRToIG(L); // add this LR to an IG
125 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000126 }
127 }
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000128
129 // init RegClassList
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000130 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000131 RegClassList[rc]->createInterferenceGraph();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000132
133 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000134 cerr << "LRLists Created!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000135}
136
137
138
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000139
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000140//----------------------------------------------------------------------------
141// This method will add all interferences at for a given instruction.
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000142// Interence occurs only if the LR of Def (Inst or Arg) is of the same reg
143// class as that of live var. The live var passed to this function is the
144// LVset AFTER the instruction
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000145//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000146void PhyRegAlloc::addInterference(const Value *const Def,
147 const LiveVarSet *const LVSet,
148 const bool isCallInst) {
149
150 LiveVarSet::const_iterator LIt = LVSet->begin();
151
152 // get the live range of instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000153 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000154 const LiveRange *const LROfDef = LRI.getLiveRangeForValue( Def );
155
156 IGNode *const IGNodeOfDef = LROfDef->getUserIGNode();
157 assert( IGNodeOfDef );
158
159 RegClass *const RCOfDef = LROfDef->getRegClass();
160
161 // for each live var in live variable set
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000162 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000163 for( ; LIt != LVSet->end(); ++LIt) {
164
165 if( DEBUG_RA > 1) {
Chris Lattner697954c2002-01-20 22:54:45 +0000166 cerr << "< Def="; printValue(Def);
167 cerr << ", Lvar="; printValue( *LIt); cerr << "> ";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000168 }
169
170 // get the live range corresponding to live var
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000171 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000172 LiveRange *const LROfVar = LRI.getLiveRangeForValue(*LIt );
173
174 // LROfVar can be null if it is a const since a const
175 // doesn't have a dominating def - see Assumptions above
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000176 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000177 if (LROfVar) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000178 if(LROfDef == LROfVar) // do not set interf for same LR
179 continue;
180
181 // if 2 reg classes are the same set interference
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000182 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000183 if(RCOfDef == LROfVar->getRegClass()) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000184 RCOfDef->setInterference( LROfDef, LROfVar);
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000185 } else if(DEBUG_RA > 1) {
186 // we will not have LRs for values not explicitly allocated in the
187 // instruction stream (e.g., constants)
188 cerr << " warning: no live range for " ;
189 printValue(*LIt); cerr << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000190 }
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000191 }
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000192 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000193}
194
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000195
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000196
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000197//----------------------------------------------------------------------------
198// For a call instruction, this method sets the CallInterference flag in
199// the LR of each variable live int the Live Variable Set live after the
200// call instruction (except the return value of the call instruction - since
201// the return value does not interfere with that call itself).
202//----------------------------------------------------------------------------
203
204void PhyRegAlloc::setCallInterferences(const MachineInstr *MInst,
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000205 const LiveVarSet *const LVSetAft ) {
206
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000207 // Now find the LR of the return value of the call
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000208 // We do this because, we look at the LV set *after* the instruction
209 // to determine, which LRs must be saved across calls. The return value
210 // of the call is live in this set - but it does not interfere with call
211 // (i.e., we can allocate a volatile register to the return value)
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000212 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000213 LiveRange *RetValLR = NULL;
Ruchira Sasankab3b6f532001-10-21 16:43:41 +0000214 const Value *RetVal = MRI.getCallInstRetVal( MInst );
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000215
Ruchira Sasankab3b6f532001-10-21 16:43:41 +0000216 if( RetVal ) {
217 RetValLR = LRI.getLiveRangeForValue( RetVal );
218 assert( RetValLR && "No LR for RetValue of call");
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000219 }
220
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000221 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000222 cerr << "\n For call inst: " << *MInst;
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000223
224 LiveVarSet::const_iterator LIt = LVSetAft->begin();
225
226 // for each live var in live variable set after machine inst
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000227 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000228 for( ; LIt != LVSetAft->end(); ++LIt) {
229
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000230 // get the live range corresponding to live var
231 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000232 LiveRange *const LR = LRI.getLiveRangeForValue(*LIt );
233
234 if( LR && DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000235 cerr << "\n\tLR Aft Call: ";
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000236 LR->printSet();
237 }
238
239
240 // LR can be null if it is a const since a const
241 // doesn't have a dominating def - see Assumptions above
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000242 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000243 if( LR && (LR != RetValLR) ) {
244 LR->setCallInterference();
245 if( DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000246 cerr << "\n ++Added call interf for LR: " ;
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000247 LR->printSet();
248 }
249 }
250
251 }
252
253}
254
255
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000256
257
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000258//----------------------------------------------------------------------------
259// This method will walk thru code and create interferences in the IG of
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000260// each RegClass. Also, this method calculates the spill cost of each
261// Live Range (it is done in this method to save another pass over the code).
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000262//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000263void PhyRegAlloc::buildInterferenceGraphs()
264{
265
Chris Lattner697954c2002-01-20 22:54:45 +0000266 if(DEBUG_RA) cerr << "Creating interference graphs ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000267
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000268 unsigned BBLoopDepthCost;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000269 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
270
271 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
272
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000273 // find the 10^(loop_depth) of this BB
274 //
275 BBLoopDepthCost = (unsigned) pow( 10.0, LoopDepthCalc.getLoopDepth(*BBI));
276
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000277 // get the iterator for machine instructions
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000278 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000279 const MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
280 MachineCodeForBasicBlock::const_iterator
281 MInstIterator = MIVec.begin();
282
283 // iterate over all the machine instructions in BB
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000284 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000285 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000286
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +0000287 const MachineInstr * MInst = *MInstIterator;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000288
289 // get the LV set after the instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000290 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000291 const LiveVarSet *const LVSetAI =
292 LVI->getLiveVarSetAfterMInst(MInst, *BBI);
293
294 const bool isCallInst = TM.getInstrInfo().isCall(MInst->getOpCode());
295
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000296 if( isCallInst ) {
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000297 // set the isCallInterference flag of each live range wich extends
298 // accross this call instruction. This information is used by graph
299 // coloring algo to avoid allocating volatile colors to live ranges
300 // that span across calls (since they have to be saved/restored)
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000301 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000302 setCallInterferences( MInst, LVSetAI);
303 }
304
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000305
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000306 // iterate over all MI operands to find defs
307 //
Chris Lattner7a176752001-12-04 00:03:30 +0000308 for( MachineInstr::val_const_op_iterator OpI(MInst);!OpI.done(); ++OpI) {
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000309
310 if( OpI.isDef() ) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000311 // create a new LR iff this operand is a def
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000312 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000313 addInterference(*OpI, LVSetAI, isCallInst );
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000314 }
315
316 // Calculate the spill cost of each live range
317 //
318 LiveRange *LR = LRI.getLiveRangeForValue( *OpI );
319 if( LR )
320 LR->addSpillCost(BBLoopDepthCost);
321 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000322
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000323
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000324 // if there are multiple defs in this instruction e.g. in SETX
325 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000326 if (TM.getInstrInfo().isPseudoInstr(MInst->getOpCode()))
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000327 addInterf4PseudoInstr(MInst);
328
329
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000330 // Also add interference for any implicit definitions in a machine
331 // instr (currently, only calls have this).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000332 //
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000333 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
334 if( NumOfImpRefs > 0 ) {
335 for(unsigned z=0; z < NumOfImpRefs; z++)
336 if( MInst->implicitRefIsDefined(z) )
337 addInterference( MInst->getImplicitRef(z), LVSetAI, isCallInst );
338 }
339
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +0000340
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000341 } // for all machine instructions in BB
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000342
343 } // for all BBs in method
344
345
346 // add interferences for method arguments. Since there are no explict
347 // defs in method for args, we have to add them manually
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000348 //
349 addInterferencesForArgs();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000350
351 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000352 cerr << "Interference graphs calculted!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000353
354}
355
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000356
357
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000358//--------------------------------------------------------------------------
359// Pseudo instructions will be exapnded to multiple instructions by the
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000360// assembler. Consequently, all the opernds must get distinct registers.
361// Therefore, we mark all operands of a pseudo instruction as they interfere
362// with one another.
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000363//--------------------------------------------------------------------------
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000364void PhyRegAlloc::addInterf4PseudoInstr(const MachineInstr *MInst) {
365
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000366 bool setInterf = false;
367
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000368 // iterate over MI operands to find defs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000369 //
Chris Lattner7a176752001-12-04 00:03:30 +0000370 for( MachineInstr::val_const_op_iterator It1(MInst);!It1.done(); ++It1) {
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000371
372 const LiveRange *const LROfOp1 = LRI.getLiveRangeForValue( *It1 );
373
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000374 if( !LROfOp1 && It1.isDef() )
375 assert( 0 && "No LR for Def in PSEUDO insruction");
376
Chris Lattner7a176752001-12-04 00:03:30 +0000377 MachineInstr::val_const_op_iterator It2 = It1;
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000378 ++It2;
379
380 for( ; !It2.done(); ++It2) {
381
382 const LiveRange *const LROfOp2 = LRI.getLiveRangeForValue( *It2 );
383
384 if( LROfOp2) {
385
386 RegClass *const RCOfOp1 = LROfOp1->getRegClass();
387 RegClass *const RCOfOp2 = LROfOp2->getRegClass();
388
389 if( RCOfOp1 == RCOfOp2 ){
390 RCOfOp1->setInterference( LROfOp1, LROfOp2 );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000391 setInterf = true;
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000392 }
393
394 } // if Op2 has a LR
395
396 } // for all other defs in machine instr
397
398 } // for all operands in an instruction
399
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000400 if( !setInterf && (MInst->getNumOperands() > 2) ) {
401 cerr << "\nInterf not set for any operand in pseudo instr:\n";
402 cerr << *MInst;
403 assert(0 && "Interf not set for pseudo instr with > 2 operands" );
404
405 }
406
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000407}
408
409
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000410
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000411//----------------------------------------------------------------------------
412// This method will add interferences for incoming arguments to a method.
413//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000414void PhyRegAlloc::addInterferencesForArgs()
415{
416 // get the InSet of root BB
417 const LiveVarSet *const InSet = LVI->getInSetOfBB( Meth->front() );
418
419 // get the argument list
420 const Method::ArgumentListType& ArgList = Meth->getArgumentList();
421
422 // get an iterator to arg list
423 Method::ArgumentListType::const_iterator ArgIt = ArgList.begin();
424
425
426 for( ; ArgIt != ArgList.end() ; ++ArgIt) { // for each argument
Chris Lattner30adeb62002-02-04 16:36:59 +0000427 addInterference((Value*)*ArgIt, InSet, false); // add interferences between
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000428 // args and LVars at start
429 if( DEBUG_RA > 1) {
Chris Lattner697954c2002-01-20 22:54:45 +0000430 cerr << " - %% adding interference for argument ";
431 printValue((const Value *)*ArgIt); cerr << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000432 }
433 }
434}
435
436
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000437
438
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000439//----------------------------------------------------------------------------
440// This method is called after register allocation is complete to set the
441// allocated reisters in the machine code. This code will add register numbers
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000442// to MachineOperands that contain a Value. Also it calls target specific
443// methods to produce caller saving instructions. At the end, it adds all
444// additional instructions produced by the register allocator to the
445// instruction stream.
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000446//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000447void PhyRegAlloc::updateMachineCode()
448{
449
450 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
451
452 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
453
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000454 // get the iterator for machine instructions
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000455 //
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000456 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
457 MachineCodeForBasicBlock::iterator MInstIterator = MIVec.begin();
458
459 // iterate over all the machine instructions in BB
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000460 //
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000461 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
462
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000463 MachineInstr *MInst = *MInstIterator;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000464
465 unsigned Opcode = MInst->getOpCode();
466
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000467 // do not process Phis
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000468 if (TM.getInstrInfo().isPhi(Opcode))
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000469 continue;
470
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000471 // Now insert speical instructions (if necessary) for call/return
472 // instructions.
473 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000474 if (TM.getInstrInfo().isCall(Opcode) ||
475 TM.getInstrInfo().isReturn(Opcode)) {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000476
477 AddedInstrns *AI = AddedInstrMap[ MInst];
478 if ( !AI ) {
479 AI = new AddedInstrns();
480 AddedInstrMap[ MInst ] = AI;
481 }
482
483 // Tmp stack poistions are needed by some calls that have spilled args
484 // So reset it before we call each such method
Ruchira Sasanka6a3db8c2002-01-07 21:09:06 +0000485 //
486 mcInfo.popAllTempValues(TM);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000487
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000488 if (TM.getInstrInfo().isCall(Opcode))
489 MRI.colorCallArgs(MInst, LRI, AI, *this, *BBI);
490 else if (TM.getInstrInfo().isReturn(Opcode))
491 MRI.colorRetValue(MInst, LRI, AI);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000492 }
493
494
495 /* -- Using above code instead of this
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000496
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000497 // if this machine instr is call, insert caller saving code
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000498
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000499 if( (TM.getInstrInfo()).isCall( MInst->getOpCode()) )
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000500 MRI.insertCallerSavingCode(MInst, *BBI, *this );
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000501
502 */
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000503
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000504
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000505 // reset the stack offset for temporary variables since we may
506 // need that to spill
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000507 // mcInfo.popAllTempValues(TM);
Ruchira Sasankaf90870f2001-11-15 22:02:06 +0000508 // TODO ** : do later
Vikram S. Adve12af1642001-11-08 04:48:50 +0000509
Chris Lattner7a176752001-12-04 00:03:30 +0000510 //for(MachineInstr::val_const_op_iterator OpI(MInst);!OpI.done();++OpI) {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000511
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000512
513 // Now replace set the registers for operands in the machine instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000514 //
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000515 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
516
517 MachineOperand& Op = MInst->getOperand(OpNum);
518
519 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
520 Op.getOperandType() == MachineOperand::MO_CCRegister) {
521
522 const Value *const Val = Op.getVRegValue();
523
524 // delete this condition checking later (must assert if Val is null)
Chris Lattner045e7c82001-09-19 16:26:23 +0000525 if( !Val) {
526 if (DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000527 cerr << "Warning: NULL Value found for operand\n";
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000528 continue;
529 }
530 assert( Val && "Value is NULL");
531
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000532 LiveRange *const LR = LRI.getLiveRangeForValue(Val);
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000533
534 if ( !LR ) {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000535
536 // nothing to worry if it's a const or a label
537
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000538 if (DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000539 cerr << "*NO LR for operand : " << Op ;
540 cerr << " [reg:" << Op.getAllocatedRegNum() << "]";
541 cerr << " in inst:\t" << *MInst << "\n";
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000542 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000543
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000544 // if register is not allocated, mark register as invalid
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000545 if( Op.getAllocatedRegNum() == -1)
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000546 Op.setRegForValue( MRI.getInvalidRegNum());
Ruchira Sasankae727f852001-09-18 22:43:57 +0000547
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000548
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000549 continue;
550 }
551
552 unsigned RCID = (LR->getRegClass())->getID();
553
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000554 if( LR->hasColor() ) {
555 Op.setRegForValue( MRI.getUnifiedRegNum(RCID, LR->getColor()) );
556 }
557 else {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000558
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000559 // LR did NOT receive a color (register). Now, insert spill code
560 // for spilled opeands in this machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000561
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000562 //assert(0 && "LR must be spilled");
563 insertCode4SpilledLR(LR, MInst, *BBI, OpNum );
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000564
565 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000566 }
567
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000568 } // for each operand
569
570
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000571 // Now add instructions that the register allocator inserts before/after
572 // this machine instructions (done only for calls/rets/incoming args)
573 // We do this here, to ensure that spill for an instruction is inserted
574 // closest as possible to an instruction (see above insertCode4Spill...)
575 //
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000576 // If there are instructions to be added, *before* this machine
577 // instruction, add them now.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000578 //
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000579 if( AddedInstrMap[ MInst ] ) {
Chris Lattner697954c2002-01-20 22:54:45 +0000580 std::deque<MachineInstr *> &IBef = AddedInstrMap[MInst]->InstrnsBefore;
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000581
582 if( ! IBef.empty() ) {
Chris Lattner697954c2002-01-20 22:54:45 +0000583 std::deque<MachineInstr *>::iterator AdIt;
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000584
585 for( AdIt = IBef.begin(); AdIt != IBef.end() ; ++AdIt ) {
586
587 if( DEBUG_RA) {
588 cerr << "For inst " << *MInst;
Chris Lattner697954c2002-01-20 22:54:45 +0000589 cerr << " PREPENDed instr: " << **AdIt << "\n";
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000590 }
591
592 MInstIterator = MIVec.insert( MInstIterator, *AdIt );
593 ++MInstIterator;
594 }
595
596 }
597
598 }
599
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000600 // If there are instructions to be added *after* this machine
601 // instruction, add them now
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000602 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000603 if(AddedInstrMap[MInst] &&
604 !AddedInstrMap[MInst]->InstrnsAfter.empty() ) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000605
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000606 // if there are delay slots for this instruction, the instructions
607 // added after it must really go after the delayed instruction(s)
608 // So, we move the InstrAfter of the current instruction to the
609 // corresponding delayed instruction
610
611 unsigned delay;
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000612 if ((delay=TM.getInstrInfo().getNumDelaySlots(MInst->getOpCode())) >0){
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000613 move2DelayedInstr(MInst, *(MInstIterator+delay) );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000614
Chris Lattner697954c2002-01-20 22:54:45 +0000615 if(DEBUG_RA) cerr<< "\nMoved an added instr after the delay slot";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000616 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000617
618 else {
619
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000620
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000621 // Here we can add the "instructions after" to the current
622 // instruction since there are no delay slots for this instruction
623
Chris Lattner697954c2002-01-20 22:54:45 +0000624 std::deque<MachineInstr *> &IAft = AddedInstrMap[MInst]->InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000625
626 if( ! IAft.empty() ) {
627
Chris Lattner697954c2002-01-20 22:54:45 +0000628 std::deque<MachineInstr *>::iterator AdIt;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000629
630 ++MInstIterator; // advance to the next instruction
631
632 for( AdIt = IAft.begin(); AdIt != IAft.end() ; ++AdIt ) {
633
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000634 if(DEBUG_RA) {
635 cerr << "For inst " << *MInst;
Chris Lattner697954c2002-01-20 22:54:45 +0000636 cerr << " APPENDed instr: " << **AdIt << "\n";
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000637 }
638
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000639 MInstIterator = MIVec.insert( MInstIterator, *AdIt );
640 ++MInstIterator;
641 }
642
643 // MInsterator already points to the next instr. Since the
644 // for loop also increments it, decrement it to point to the
645 // instruction added last
646 --MInstIterator;
647
648 }
649
650 } // if not delay
651
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000652 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000653
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000654 } // for each machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000655 }
656}
657
658
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000659
660//----------------------------------------------------------------------------
661// This method inserts spill code for AN operand whose LR was spilled.
662// This method may be called several times for a single machine instruction
663// if it contains many spilled operands. Each time it is called, it finds
664// a register which is not live at that instruction and also which is not
665// used by other spilled operands of the same instruction. Then it uses
666// this register temporarily to accomodate the spilled value.
667//----------------------------------------------------------------------------
668void PhyRegAlloc::insertCode4SpilledLR(const LiveRange *LR,
669 MachineInstr *MInst,
670 const BasicBlock *BB,
671 const unsigned OpNum) {
672
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000673 assert(! TM.getInstrInfo().isCall(MInst->getOpCode()) &&
674 (! TM.getInstrInfo().isReturn(MInst->getOpCode())) &&
675 "Arg of a call/ret must be handled elsewhere");
676
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000677 MachineOperand& Op = MInst->getOperand(OpNum);
678 bool isDef = MInst->operandIsDefined(OpNum);
679 unsigned RegType = MRI.getRegType( LR );
680 int SpillOff = LR->getSpillOffFromFP();
681 RegClass *RC = LR->getRegClass();
682 const LiveVarSet *LVSetBef = LVI->getLiveVarSetBeforeMInst(MInst, BB);
Vikram S. Adve00521d72001-11-12 23:26:35 +0000683
Chris Lattner697954c2002-01-20 22:54:45 +0000684 mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000685
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000686 MachineInstr *MIBef=NULL, *AdIMid=NULL, *MIAft=NULL;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000687
688 int TmpRegU = getUsableUniRegAtMI(RC, RegType, MInst,LVSetBef, MIBef, MIAft);
689
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000690 // get the added instructions for this instruciton
691 AddedInstrns *AI = AddedInstrMap[ MInst ];
692 if ( !AI ) {
693 AI = new AddedInstrns();
694 AddedInstrMap[ MInst ] = AI;
695 }
696
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000697
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000698 if( !isDef ) {
699
700 // for a USE, we have to load the value of LR from stack to a TmpReg
701 // and use the TmpReg as one operand of instruction
702
703 // actual loading instruction
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000704 AdIMid = MRI.cpMem2RegMI(MRI.getFramePointer(), SpillOff, TmpRegU,RegType);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000705
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000706 if(MIBef)
707 AI->InstrnsBefore.push_back(MIBef);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000708
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000709 AI->InstrnsBefore.push_back(AdIMid);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000710
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000711 if(MIAft)
712 AI->InstrnsAfter.push_front(MIAft);
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000713
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000714
715 }
716 else { // if this is a Def
717
718 // for a DEF, we have to store the value produced by this instruction
719 // on the stack position allocated for this LR
720
721 // actual storing instruction
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000722 AdIMid = MRI.cpReg2MemMI(TmpRegU, MRI.getFramePointer(), SpillOff,RegType);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000723
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000724 if (MIBef)
725 AI->InstrnsBefore.push_back(MIBef);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000726
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000727 AI->InstrnsAfter.push_front(AdIMid);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000728
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000729 if (MIAft)
730 AI->InstrnsAfter.push_front(MIAft);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000731
732 } // if !DEF
733
734 cerr << "\nFor Inst " << *MInst;
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000735 cerr << " - SPILLED LR: "; LR->printSet();
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000736 cerr << "\n - Added Instructions:";
737 if( MIBef ) cerr << *MIBef;
738 cerr << *AdIMid;
739 if( MIAft ) cerr << *MIAft;
740
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000741 Op.setRegForValue( TmpRegU ); // set the opearnd
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000742
743
744}
745
746
747
748
749
750
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000751//----------------------------------------------------------------------------
752// We can use the following method to get a temporary register to be used
753// BEFORE any given machine instruction. If there is a register available,
754// this method will simply return that register and set MIBef = MIAft = NULL.
755// Otherwise, it will return a register and MIAft and MIBef will contain
756// two instructions used to free up this returned register.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000757// Returned register number is the UNIFIED register number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000758//----------------------------------------------------------------------------
759
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000760int PhyRegAlloc::getUsableUniRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000761 const int RegType,
762 const MachineInstr *MInst,
763 const LiveVarSet *LVSetBef,
764 MachineInstr *MIBef,
765 MachineInstr *MIAft) {
766
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000767 int RegU = getUnusedUniRegAtMI(RC, MInst, LVSetBef);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000768
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000769
770 if( RegU != -1) {
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000771 // we found an unused register, so we can simply use it
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000772 MIBef = MIAft = NULL;
773 }
774 else {
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000775 // we couldn't find an unused register. Generate code to free up a reg by
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000776 // saving it on stack and restoring after the instruction
777
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000778 int TmpOff = mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
Vikram S. Adve12af1642001-11-08 04:48:50 +0000779
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000780 RegU = getUniRegNotUsedByThisInst(RC, MInst);
781 MIBef = MRI.cpReg2MemMI(RegU, MRI.getFramePointer(), TmpOff, RegType );
782 MIAft = MRI.cpMem2RegMI(MRI.getFramePointer(), TmpOff, RegU, RegType );
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000783 }
784
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000785 return RegU;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000786}
787
788//----------------------------------------------------------------------------
789// This method is called to get a new unused register that can be used to
790// accomodate a spilled value.
791// This method may be called several times for a single machine instruction
792// if it contains many spilled operands. Each time it is called, it finds
793// a register which is not live at that instruction and also which is not
794// used by other spilled operands of the same instruction.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000795// Return register number is relative to the register class. NOT
796// unified number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000797//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000798int PhyRegAlloc::getUnusedUniRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000799 const MachineInstr *MInst,
800 const LiveVarSet *LVSetBef) {
801
802 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
803
804 bool *IsColorUsedArr = RC->getIsColorUsedArr();
805
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000806 for(unsigned i=0; i < NumAvailRegs; i++) // Reset array
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000807 IsColorUsedArr[i] = false;
808
809 LiveVarSet::const_iterator LIt = LVSetBef->begin();
810
811 // for each live var in live variable set after machine inst
812 for( ; LIt != LVSetBef->end(); ++LIt) {
813
814 // get the live range corresponding to live var
815 LiveRange *const LRofLV = LRI.getLiveRangeForValue(*LIt );
816
817 // LR can be null if it is a const since a const
818 // doesn't have a dominating def - see Assumptions above
819 if( LRofLV )
820 if( LRofLV->hasColor() )
821 IsColorUsedArr[ LRofLV->getColor() ] = true;
822 }
823
824 // It is possible that one operand of this MInst was already spilled
825 // and it received some register temporarily. If that's the case,
826 // it is recorded in machine operand. We must skip such registers.
827
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000828 setRelRegsUsedByThisInst(RC, MInst);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000829
830 unsigned c; // find first unused color
831 for( c=0; c < NumAvailRegs; c++)
832 if( ! IsColorUsedArr[ c ] ) break;
833
834 if(c < NumAvailRegs)
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000835 return MRI.getUnifiedRegNum(RC->getID(), c);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000836 else
837 return -1;
838
839
840}
841
842
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000843//----------------------------------------------------------------------------
844// Get any other register in a register class, other than what is used
845// by operands of a machine instruction. Returns the unified reg number.
846//----------------------------------------------------------------------------
847int PhyRegAlloc::getUniRegNotUsedByThisInst(RegClass *RC,
848 const MachineInstr *MInst) {
849
850 bool *IsColorUsedArr = RC->getIsColorUsedArr();
851 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
852
853
854 for(unsigned i=0; i < NumAvailRegs ; i++) // Reset array
855 IsColorUsedArr[i] = false;
856
857 setRelRegsUsedByThisInst(RC, MInst);
858
859 unsigned c; // find first unused color
860 for( c=0; c < RC->getNumOfAvailRegs(); c++)
861 if( ! IsColorUsedArr[ c ] ) break;
862
863 if(c < NumAvailRegs)
864 return MRI.getUnifiedRegNum(RC->getID(), c);
865 else
866 assert( 0 && "FATAL: No free register could be found in reg class!!");
Chris Lattner697954c2002-01-20 22:54:45 +0000867 return 0;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000868}
869
870
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000871//----------------------------------------------------------------------------
872// This method modifies the IsColorUsedArr of the register class passed to it.
873// It sets the bits corresponding to the registers used by this machine
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000874// instructions. Both explicit and implicit operands are set.
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000875//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000876void PhyRegAlloc::setRelRegsUsedByThisInst(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000877 const MachineInstr *MInst ) {
878
879 bool *IsColorUsedArr = RC->getIsColorUsedArr();
880
881 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
882
883 const MachineOperand& Op = MInst->getOperand(OpNum);
884
885 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000886 Op.getOperandType() == MachineOperand::MO_CCRegister ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000887
888 const Value *const Val = Op.getVRegValue();
889
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000890 if( Val )
891 if( MRI.getRegClassIDOfValue(Val) == RC->getID() ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000892 int Reg;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000893 if( (Reg=Op.getAllocatedRegNum()) != -1) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000894 IsColorUsedArr[ Reg ] = true;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000895 }
896 else {
897 // it is possilbe that this operand still is not marked with
898 // a register but it has a LR and that received a color
899
900 LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
901 if( LROfVal)
902 if( LROfVal->hasColor() )
903 IsColorUsedArr[ LROfVal->getColor() ] = true;
904 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000905
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000906 } // if reg classes are the same
907 }
908 else if (Op.getOperandType() == MachineOperand::MO_MachineRegister) {
909 IsColorUsedArr[ Op.getMachineRegNum() ] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000910 }
911 }
912
913 // If there are implicit references, mark them as well
914
915 for(unsigned z=0; z < MInst->getNumImplicitRefs(); z++) {
916
917 LiveRange *const LRofImpRef =
918 LRI.getLiveRangeForValue( MInst->getImplicitRef(z) );
Chris Lattner697954c2002-01-20 22:54:45 +0000919
920 if(LRofImpRef && LRofImpRef->hasColor())
921 IsColorUsedArr[LRofImpRef->getColor()] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000922 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000923}
924
925
926
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000927
928
929
930
931
932//----------------------------------------------------------------------------
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000933// If there are delay slots for an instruction, the instructions
934// added after it must really go after the delayed instruction(s).
935// So, we move the InstrAfter of that instruction to the
936// corresponding delayed instruction using the following method.
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000937
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000938//----------------------------------------------------------------------------
939void PhyRegAlloc:: move2DelayedInstr(const MachineInstr *OrigMI,
940 const MachineInstr *DelayedMI) {
941
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000942 // "added after" instructions of the original instr
Chris Lattner697954c2002-01-20 22:54:45 +0000943 std::deque<MachineInstr *> &OrigAft = AddedInstrMap[OrigMI]->InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000944
945 // "added instructions" of the delayed instr
946 AddedInstrns *DelayAdI = AddedInstrMap[DelayedMI];
947
948 if(! DelayAdI ) { // create a new "added after" if necessary
949 DelayAdI = new AddedInstrns();
950 AddedInstrMap[DelayedMI] = DelayAdI;
951 }
952
953 // "added after" instructions of the delayed instr
Chris Lattner697954c2002-01-20 22:54:45 +0000954 std::deque<MachineInstr *> &DelayedAft = DelayAdI->InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000955
956 // go thru all the "added after instructions" of the original instruction
957 // and append them to the "addded after instructions" of the delayed
958 // instructions
Chris Lattner697954c2002-01-20 22:54:45 +0000959 DelayedAft.insert(DelayedAft.end(), OrigAft.begin(), OrigAft.end());
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000960
961 // empty the "added after instructions" of the original instruction
962 OrigAft.clear();
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000963}
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000964
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000965//----------------------------------------------------------------------------
966// This method prints the code with registers after register allocation is
967// complete.
968//----------------------------------------------------------------------------
969void PhyRegAlloc::printMachineCode()
970{
971
Chris Lattner697954c2002-01-20 22:54:45 +0000972 cerr << "\n;************** Method " << Meth->getName()
973 << " *****************\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000974
975 Method::const_iterator BBI = Meth->begin(); // random iterator for BBs
976
977 for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order
978
Chris Lattner697954c2002-01-20 22:54:45 +0000979 cerr << "\n"; printLabel( *BBI); cerr << ": ";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000980
981 // get the iterator for machine instructions
982 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
983 MachineCodeForBasicBlock::iterator MInstIterator = MIVec.begin();
984
985 // iterate over all the machine instructions in BB
986 for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
987
988 MachineInstr *const MInst = *MInstIterator;
989
990
Chris Lattner697954c2002-01-20 22:54:45 +0000991 cerr << "\n\t";
992 cerr << TargetInstrDescriptors[MInst->getOpCode()].opCodeString;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000993
994
Chris Lattner7a176752001-12-04 00:03:30 +0000995 //for(MachineInstr::val_const_op_iterator OpI(MInst);!OpI.done();++OpI) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000996
997 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
998
999 MachineOperand& Op = MInst->getOperand(OpNum);
1000
1001 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasanka97b8b442001-10-18 22:36:26 +00001002 Op.getOperandType() == MachineOperand::MO_CCRegister /*||
1003 Op.getOperandType() == MachineOperand::MO_PCRelativeDisp*/ ) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001004
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001005 const Value *const Val = Op.getVRegValue () ;
Ruchira Sasankae727f852001-09-18 22:43:57 +00001006 // ****this code is temporary till NULL Values are fixed
1007 if( ! Val ) {
Chris Lattner697954c2002-01-20 22:54:45 +00001008 cerr << "\t<*NULL*>";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001009 continue;
1010 }
Ruchira Sasankae727f852001-09-18 22:43:57 +00001011
1012 // if a label or a constant
Chris Lattnerdbe53042002-01-21 01:33:12 +00001013 if(isa<BasicBlock>(Val)) {
Chris Lattner697954c2002-01-20 22:54:45 +00001014 cerr << "\t"; printLabel( Op.getVRegValue () );
1015 } else {
Ruchira Sasankae727f852001-09-18 22:43:57 +00001016 // else it must be a register value
1017 const int RegNum = Op.getAllocatedRegNum();
1018
Chris Lattner697954c2002-01-20 22:54:45 +00001019 cerr << "\t" << "%" << MRI.getUnifiedRegName( RegNum );
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001020 if (Val->hasName() )
Chris Lattner697954c2002-01-20 22:54:45 +00001021 cerr << "(" << Val->getName() << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001022 else
Chris Lattner697954c2002-01-20 22:54:45 +00001023 cerr << "(" << Val << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001024
1025 if( Op.opIsDef() )
Chris Lattner697954c2002-01-20 22:54:45 +00001026 cerr << "*";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001027
1028 const LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
1029 if( LROfVal )
1030 if( LROfVal->hasSpillOffset() )
Chris Lattner697954c2002-01-20 22:54:45 +00001031 cerr << "$";
Ruchira Sasankae727f852001-09-18 22:43:57 +00001032 }
1033
1034 }
1035 else if(Op.getOperandType() == MachineOperand::MO_MachineRegister) {
Chris Lattner697954c2002-01-20 22:54:45 +00001036 cerr << "\t" << "%" << MRI.getUnifiedRegName(Op.getMachineRegNum());
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001037 }
1038
1039 else
Chris Lattner697954c2002-01-20 22:54:45 +00001040 cerr << "\t" << Op; // use dump field
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001041 }
1042
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001043
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001044
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001045 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
1046 if( NumOfImpRefs > 0 ) {
1047
Chris Lattner697954c2002-01-20 22:54:45 +00001048 cerr << "\tImplicit:";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001049
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001050 for(unsigned z=0; z < NumOfImpRefs; z++) {
1051 printValue( MInst->getImplicitRef(z) );
Chris Lattner697954c2002-01-20 22:54:45 +00001052 cerr << "\t";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001053 }
1054
1055 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001056
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001057 } // for all machine instructions
1058
Chris Lattner697954c2002-01-20 22:54:45 +00001059 cerr << "\n";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001060
1061 } // for all BBs
1062
Chris Lattner697954c2002-01-20 22:54:45 +00001063 cerr << "\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001064}
1065
Ruchira Sasankae727f852001-09-18 22:43:57 +00001066
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001067#if 0
1068
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001069//----------------------------------------------------------------------------
1070//
1071//----------------------------------------------------------------------------
1072
1073void PhyRegAlloc::colorCallRetArgs()
1074{
1075
1076 CallRetInstrListType &CallRetInstList = LRI.getCallRetInstrList();
1077 CallRetInstrListType::const_iterator It = CallRetInstList.begin();
1078
1079 for( ; It != CallRetInstList.end(); ++It ) {
1080
Ruchira Sasankaa90e7702001-10-15 16:26:38 +00001081 const MachineInstr *const CRMI = *It;
1082 unsigned OpCode = CRMI->getOpCode();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001083
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001084 // get the added instructions for this Call/Ret instruciton
1085 AddedInstrns *AI = AddedInstrMap[ CRMI ];
1086 if ( !AI ) {
1087 AI = new AddedInstrns();
1088 AddedInstrMap[ CRMI ] = AI;
1089 }
1090
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001091 // Tmp stack poistions are needed by some calls that have spilled args
1092 // So reset it before we call each such method
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001093 //mcInfo.popAllTempValues(TM);
1094
1095
Vikram S. Adve12af1642001-11-08 04:48:50 +00001096
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001097 if (TM.getInstrInfo().isCall(OpCode))
1098 MRI.colorCallArgs(CRMI, LRI, AI, *this);
1099 else if (TM.getInstrInfo().isReturn(OpCode))
Ruchira Sasankaa90e7702001-10-15 16:26:38 +00001100 MRI.colorRetValue( CRMI, LRI, AI );
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001101 else
1102 assert(0 && "Non Call/Ret instrn in CallRetInstrList\n");
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001103 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001104}
1105
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001106#endif
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001107
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001108//----------------------------------------------------------------------------
1109
1110//----------------------------------------------------------------------------
1111void PhyRegAlloc::colorIncomingArgs()
1112{
1113 const BasicBlock *const FirstBB = Meth->front();
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001114 const MachineInstr *FirstMI = FirstBB->getMachineInstrVec().front();
1115 assert(FirstMI && "No machine instruction in entry BB");
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001116
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001117 AddedInstrns *AI = AddedInstrMap[FirstMI];
1118 if (!AI)
1119 AddedInstrMap[FirstMI] = AI = new AddedInstrns();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001120
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001121 MRI.colorMethodArgs(Meth, LRI, AI);
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001122}
1123
Ruchira Sasankae727f852001-09-18 22:43:57 +00001124
1125//----------------------------------------------------------------------------
1126// Used to generate a label for a basic block
1127//----------------------------------------------------------------------------
Chris Lattner697954c2002-01-20 22:54:45 +00001128void PhyRegAlloc::printLabel(const Value *const Val) {
1129 if (Val->hasName())
1130 cerr << Val->getName();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001131 else
Chris Lattner697954c2002-01-20 22:54:45 +00001132 cerr << "Label" << Val;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001133}
1134
1135
Ruchira Sasankae727f852001-09-18 22:43:57 +00001136//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001137// This method calls setSugColorUsable method of each live range. This
1138// will determine whether the suggested color of LR is really usable.
1139// A suggested color is not usable when the suggested color is volatile
1140// AND when there are call interferences
1141//----------------------------------------------------------------------------
1142
1143void PhyRegAlloc::markUnusableSugColors()
1144{
Chris Lattner697954c2002-01-20 22:54:45 +00001145 if(DEBUG_RA ) cerr << "\nmarking unusable suggested colors ...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001146
1147 // hash map iterator
1148 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
1149 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
1150
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001151 for(; HMI != HMIEnd ; ++HMI ) {
1152 if (HMI->first) {
1153 LiveRange *L = HMI->second; // get the LiveRange
1154 if (L) {
1155 if(L->hasSuggestedColor()) {
1156 int RCID = L->getRegClass()->getID();
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001157 if( MRI.isRegVolatile( RCID, L->getSuggestedColor()) &&
1158 L->isCallInterference() )
1159 L->setSuggestedColorUsable( false );
1160 else
1161 L->setSuggestedColorUsable( true );
1162 }
1163 } // if L->hasSuggestedColor()
1164 }
1165 } // for all LR's in hash map
1166}
1167
1168
1169
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001170//----------------------------------------------------------------------------
1171// The following method will set the stack offsets of the live ranges that
1172// are decided to be spillled. This must be called just after coloring the
1173// LRs using the graph coloring algo. For each live range that is spilled,
1174// this method allocate a new spill position on the stack.
1175//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001176
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001177void PhyRegAlloc::allocateStackSpace4SpilledLRs()
1178{
Chris Lattner697954c2002-01-20 22:54:45 +00001179 if(DEBUG_RA ) cerr << "\nsetting LR stack offsets ...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001180
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001181 // hash map iterator
1182 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
1183 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
1184
1185 for( ; HMI != HMIEnd ; ++HMI ) {
Chris Lattner697954c2002-01-20 22:54:45 +00001186 if(HMI->first && HMI->second) {
1187 LiveRange *L = HMI->second; // get the LiveRange
1188 if( ! L->hasColor() )
1189 // NOTE: ** allocating the size of long Type **
1190 L->setSpillOffFromFP(mcInfo.allocateSpilledValue(TM, Type::LongTy));
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001191 }
1192 } // for all LR's in hash map
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001193}
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001194
1195
1196
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001197//----------------------------------------------------------------------------
Ruchira Sasankae727f852001-09-18 22:43:57 +00001198// The entry pont to Register Allocation
1199//----------------------------------------------------------------------------
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001200
1201void PhyRegAlloc::allocateRegisters()
1202{
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001203
1204 // make sure that we put all register classes into the RegClassList
1205 // before we call constructLiveRanges (now done in the constructor of
1206 // PhyRegAlloc class).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001207 //
1208 LRI.constructLiveRanges(); // create LR info
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001209
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001210 if (DEBUG_RA)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001211 LRI.printLiveRanges();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001212
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001213 createIGNodeListsAndIGs(); // create IGNode list and IGs
1214
1215 buildInterferenceGraphs(); // build IGs in all reg classes
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001216
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001217
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001218 if (DEBUG_RA) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001219 // print all LRs in all reg classes
1220 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1221 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001222
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001223 // print IGs in all register classes
1224 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1225 RegClassList[ rc ]->printIG();
1226 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001227
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001228
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001229 LRI.coalesceLRs(); // coalesce all live ranges
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001230
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +00001231
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001232 if( DEBUG_RA) {
1233 // print all LRs in all reg classes
1234 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1235 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001236
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001237 // print IGs in all register classes
1238 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1239 RegClassList[ rc ]->printIG();
1240 }
1241
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001242
1243 // mark un-usable suggested color before graph coloring algorithm.
1244 // When this is done, the graph coloring algo will not reserve
1245 // suggested color unnecessarily - they can be used by another LR
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001246 //
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001247 markUnusableSugColors();
1248
1249 // color all register classes using the graph coloring algo
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001250 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1251 RegClassList[ rc ]->colorAllRegs();
1252
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001253 // Atter grpah coloring, if some LRs did not receive a color (i.e, spilled)
1254 // a poistion for such spilled LRs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001255 //
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001256 allocateStackSpace4SpilledLRs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001257
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001258 mcInfo.popAllTempValues(TM); // TODO **Check
1259
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001260 // color incoming args - if the correct color was not received
1261 // insert code to copy to the correct register
1262 //
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001263 colorIncomingArgs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001264
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001265 // Now update the machine code with register names and add any
1266 // additional code inserted by the register allocator to the instruction
1267 // stream
1268 //
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001269 updateMachineCode();
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001270
Chris Lattner045e7c82001-09-19 16:26:23 +00001271 if (DEBUG_RA) {
Vikram S. Adve12af1642001-11-08 04:48:50 +00001272 MachineCodeForMethod::get(Meth).dump();
Chris Lattner045e7c82001-09-19 16:26:23 +00001273 printMachineCode(); // only for DEBUGGING
1274 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001275}
1276
Ruchira Sasankae727f852001-09-18 22:43:57 +00001277
1278