blob: 4f12d697b14ecb23d73f92139d3c0c63dd3bd99b [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 Lattner483e14e2002-04-27 07:27:19 +000017#include "llvm/Analysis/LiveVar/FunctionLiveVarInfo.h"
Chris Lattner14ab1ce2002-02-04 17:48:00 +000018#include "llvm/Analysis/LoopInfo.h"
Vikram S. Adve12af1642001-11-08 04:48:50 +000019#include "llvm/Target/TargetMachine.h"
20#include "llvm/Target/MachineFrameInfo.h"
Chris Lattner221d6882002-02-12 21:07:25 +000021#include "llvm/BasicBlock.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000022#include "llvm/Function.h"
Chris Lattner37730942002-02-05 03:52:29 +000023#include "llvm/Type.h"
Chris Lattner697954c2002-01-20 22:54:45 +000024#include <iostream>
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000025#include <math.h>
Chris Lattner697954c2002-01-20 22:54:45 +000026using std::cerr;
Vikram S. Adve12af1642001-11-08 04:48:50 +000027
28
29// ***TODO: There are several places we add instructions. Validate the order
30// of adding these instructions.
Ruchira Sasanka174bded2001-10-28 18:12:02 +000031
Chris Lattner045e7c82001-09-19 16:26:23 +000032cl::Enum<RegAllocDebugLevel_t> DEBUG_RA("dregalloc", cl::NoFlags,
33 "enable register allocation debugging information",
34 clEnumValN(RA_DEBUG_None , "n", "disable debug output"),
35 clEnumValN(RA_DEBUG_Normal , "y", "enable debug output"),
36 clEnumValN(RA_DEBUG_Verbose, "v", "enable extra debug output"), 0);
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000037
38
Chris Lattner2f9b28e2002-02-04 15:54:09 +000039//----------------------------------------------------------------------------
40// RegisterAllocation pass front end...
41//----------------------------------------------------------------------------
42namespace {
Chris Lattnerf57b8452002-04-27 06:56:12 +000043 class RegisterAllocator : public FunctionPass {
Chris Lattner2f9b28e2002-02-04 15:54:09 +000044 TargetMachine &Target;
45 public:
46 inline RegisterAllocator(TargetMachine &T) : Target(T) {}
Chris Lattner6dd98a62002-02-04 00:33:08 +000047
Chris Lattnerf57b8452002-04-27 06:56:12 +000048 bool runOnFunction(Function *F) {
Chris Lattner2f9b28e2002-02-04 15:54:09 +000049 if (DEBUG_RA)
Chris Lattnerf57b8452002-04-27 06:56:12 +000050 cerr << "\n******************** Function "<< F->getName()
Chris Lattner2f9b28e2002-02-04 15:54:09 +000051 << " ********************\n";
52
Chris Lattner483e14e2002-04-27 07:27:19 +000053 PhyRegAlloc PRA(F, Target, &getAnalysis<FunctionLiveVarInfo>(),
Chris Lattner14ab1ce2002-02-04 17:48:00 +000054 &getAnalysis<cfg::LoopInfo>());
Chris Lattner2f9b28e2002-02-04 15:54:09 +000055 PRA.allocateRegisters();
56
57 if (DEBUG_RA) cerr << "\nRegister allocation complete!\n";
58 return false;
59 }
Chris Lattner4911c352002-02-04 17:39:42 +000060
Chris Lattnerf57b8452002-04-27 06:56:12 +000061 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
62 AU.addRequired(cfg::LoopInfo::ID);
Chris Lattner483e14e2002-04-27 07:27:19 +000063 AU.addRequired(FunctionLiveVarInfo::ID);
Chris Lattner4911c352002-02-04 17:39:42 +000064 }
Chris Lattner2f9b28e2002-02-04 15:54:09 +000065 };
Chris Lattner6dd98a62002-02-04 00:33:08 +000066}
67
Chris Lattnerf57b8452002-04-27 06:56:12 +000068Pass *getRegisterAllocator(TargetMachine &T) {
Chris Lattner2f9b28e2002-02-04 15:54:09 +000069 return new RegisterAllocator(T);
70}
Chris Lattner6dd98a62002-02-04 00:33:08 +000071
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000072//----------------------------------------------------------------------------
73// Constructor: Init local composite objects and create register classes.
74//----------------------------------------------------------------------------
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000075PhyRegAlloc::PhyRegAlloc(Function *F,
Ruchira Sasanka8e604792001-09-14 21:18:34 +000076 const TargetMachine& tm,
Chris Lattner483e14e2002-04-27 07:27:19 +000077 FunctionLiveVarInfo *Lvi,
Chris Lattner14ab1ce2002-02-04 17:48:00 +000078 cfg::LoopInfo *LDC)
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000079 : TM(tm), Meth(F),
80 mcInfo(MachineCodeForMethod::get(F)),
81 LVI(Lvi), LRI(F, tm, RegClassList),
82 MRI(tm.getRegInfo()),
Ruchira Sasanka8e604792001-09-14 21:18:34 +000083 NumOfRegClasses(MRI.getNumOfRegClasses()),
Chris Lattner4911c352002-02-04 17:39:42 +000084 LoopDepthCalc(LDC) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +000085
86 // create each RegisterClass and put in RegClassList
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000087 //
Chris Lattner697954c2002-01-20 22:54:45 +000088 for(unsigned int rc=0; rc < NumOfRegClasses; rc++)
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000089 RegClassList.push_back(new RegClass(F, MRI.getMachineRegClass(rc),
90 &ResColList));
Ruchira Sasanka8e604792001-09-14 21:18:34 +000091}
92
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000093
94//----------------------------------------------------------------------------
95// Destructor: Deletes register classes
96//----------------------------------------------------------------------------
97PhyRegAlloc::~PhyRegAlloc() {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +000098 for( unsigned int rc=0; rc < NumOfRegClasses; rc++)
99 delete RegClassList[rc];
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000100
101 AddedInstrMap.clear();
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000102}
103
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000104//----------------------------------------------------------------------------
105// This method initally creates interference graphs (one in each reg class)
106// and IGNodeList (one in each IG). The actual nodes will be pushed later.
107//----------------------------------------------------------------------------
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000108void PhyRegAlloc::createIGNodeListsAndIGs() {
109 if (DEBUG_RA) cerr << "Creating LR lists ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000110
111 // hash map iterator
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000112 LiveRangeMapType::const_iterator HMI = LRI.getLiveRangeMap()->begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000113
114 // hash map end
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000115 LiveRangeMapType::const_iterator HMIEnd = LRI.getLiveRangeMap()->end();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000116
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000117 for (; HMI != HMIEnd ; ++HMI ) {
118 if (HMI->first) {
119 LiveRange *L = HMI->second; // get the LiveRange
120 if (!L) {
121 if( DEBUG_RA) {
Chris Lattner0665a5f2002-02-05 01:43:49 +0000122 cerr << "\n*?!?Warning: Null liver range found for: "
123 << RAV(HMI->first) << "\n";
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000124 }
125 continue;
126 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000127 // if the Value * is not null, and LR
128 // is not yet written to the IGNodeList
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000129 if( !(L->getUserIGNode()) ) {
130 RegClass *const RC = // RegClass of first value in the LR
131 RegClassList[ L->getRegClass()->getID() ];
132
133 RC->addLRToIG(L); // add this LR to an IG
134 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000135 }
136 }
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000137
138 // init RegClassList
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000139 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000140 RegClassList[rc]->createInterferenceGraph();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000141
142 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000143 cerr << "LRLists Created!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000144}
145
146
147
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000148
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000149//----------------------------------------------------------------------------
150// This method will add all interferences at for a given instruction.
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000151// Interence occurs only if the LR of Def (Inst or Arg) is of the same reg
152// class as that of live var. The live var passed to this function is the
153// LVset AFTER the instruction
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000154//----------------------------------------------------------------------------
Chris Lattner296b7732002-02-05 02:52:05 +0000155void PhyRegAlloc::addInterference(const Value *Def,
156 const ValueSet *LVSet,
157 bool isCallInst) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000158
Chris Lattner296b7732002-02-05 02:52:05 +0000159 ValueSet::const_iterator LIt = LVSet->begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000160
161 // get the live range of instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000162 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000163 const LiveRange *const LROfDef = LRI.getLiveRangeForValue( Def );
164
165 IGNode *const IGNodeOfDef = LROfDef->getUserIGNode();
166 assert( IGNodeOfDef );
167
168 RegClass *const RCOfDef = LROfDef->getRegClass();
169
170 // for each live var in live variable set
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000171 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000172 for( ; LIt != LVSet->end(); ++LIt) {
173
Chris Lattner0665a5f2002-02-05 01:43:49 +0000174 if (DEBUG_RA > 1)
175 cerr << "< Def=" << RAV(Def) << ", Lvar=" << RAV(*LIt) << "> ";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000176
177 // get the live range corresponding to live var
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000178 //
Chris Lattner0665a5f2002-02-05 01:43:49 +0000179 LiveRange *LROfVar = LRI.getLiveRangeForValue(*LIt);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000180
181 // LROfVar can be null if it is a const since a const
182 // doesn't have a dominating def - see Assumptions above
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000183 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000184 if (LROfVar) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000185 if(LROfDef == LROfVar) // do not set interf for same LR
186 continue;
187
188 // if 2 reg classes are the same set interference
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000189 //
Chris Lattner0665a5f2002-02-05 01:43:49 +0000190 if (RCOfDef == LROfVar->getRegClass()) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000191 RCOfDef->setInterference( LROfDef, LROfVar);
Chris Lattner0665a5f2002-02-05 01:43:49 +0000192 } else if (DEBUG_RA > 1) {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000193 // we will not have LRs for values not explicitly allocated in the
194 // instruction stream (e.g., constants)
Chris Lattner0665a5f2002-02-05 01:43:49 +0000195 cerr << " warning: no live range for " << RAV(*LIt) << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000196 }
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000197 }
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000198 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000199}
200
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000201
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000202
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000203//----------------------------------------------------------------------------
204// For a call instruction, this method sets the CallInterference flag in
205// the LR of each variable live int the Live Variable Set live after the
206// call instruction (except the return value of the call instruction - since
207// the return value does not interfere with that call itself).
208//----------------------------------------------------------------------------
209
210void PhyRegAlloc::setCallInterferences(const MachineInstr *MInst,
Chris Lattner296b7732002-02-05 02:52:05 +0000211 const ValueSet *LVSetAft) {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000212
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000213 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000214 cerr << "\n For call inst: " << *MInst;
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000215
Chris Lattner296b7732002-02-05 02:52:05 +0000216 ValueSet::const_iterator LIt = LVSetAft->begin();
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000217
218 // for each live var in live variable set after machine inst
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000219 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000220 for( ; LIt != LVSetAft->end(); ++LIt) {
221
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000222 // get the live range corresponding to live var
223 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000224 LiveRange *const LR = LRI.getLiveRangeForValue(*LIt );
225
226 if( LR && DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000227 cerr << "\n\tLR Aft Call: ";
Chris Lattner296b7732002-02-05 02:52:05 +0000228 printSet(*LR);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000229 }
230
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000231 // LR can be null if it is a const since a const
232 // doesn't have a dominating def - see Assumptions above
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000233 //
Vikram S. Adve1a53f032002-03-31 18:54:37 +0000234 if( LR ) {
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000235 LR->setCallInterference();
236 if( DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000237 cerr << "\n ++Added call interf for LR: " ;
Chris Lattner296b7732002-02-05 02:52:05 +0000238 printSet(*LR);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000239 }
240 }
241
242 }
243
Vikram S. Adve1a53f032002-03-31 18:54:37 +0000244 // Now find the LR of the return value of the call
245 // We do this because, we look at the LV set *after* the instruction
246 // to determine, which LRs must be saved across calls. The return value
247 // of the call is live in this set - but it does not interfere with call
248 // (i.e., we can allocate a volatile register to the return value)
249 //
250 if( const Value *RetVal = MRI.getCallInstRetVal( MInst )) {
251 LiveRange *RetValLR = LRI.getLiveRangeForValue( RetVal );
252 assert( RetValLR && "No LR for RetValue of call");
253 RetValLR->clearCallInterference();
254 }
255
256 // If the CALL is an indirect call, find the LR of the function pointer.
257 // That has a call interference because it conflicts with outgoing args.
258 if( const Value *AddrVal = MRI.getCallInstIndirectAddrVal( MInst )) {
259 LiveRange *AddrValLR = LRI.getLiveRangeForValue( AddrVal );
260 assert( AddrValLR && "No LR for indirect addr val of call");
261 AddrValLR->setCallInterference();
262 }
263
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000264}
265
266
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000267
268
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000269//----------------------------------------------------------------------------
270// This method will walk thru code and create interferences in the IG of
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000271// each RegClass. Also, this method calculates the spill cost of each
272// Live Range (it is done in this method to save another pass over the code).
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000273//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000274void PhyRegAlloc::buildInterferenceGraphs()
275{
276
Chris Lattner697954c2002-01-20 22:54:45 +0000277 if(DEBUG_RA) cerr << "Creating interference graphs ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000278
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000279 unsigned BBLoopDepthCost;
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000280 for (Function::const_iterator BBI = Meth->begin(), BBE = Meth->end();
281 BBI != BBE; ++BBI) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000282
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000283 // find the 10^(loop_depth) of this BB
284 //
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000285 BBLoopDepthCost = (unsigned) pow(10.0, LoopDepthCalc->getLoopDepth(*BBI));
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000286
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000287 // get the iterator for machine instructions
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000288 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000289 const MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
Vikram S. Adve48762092002-04-25 04:34:15 +0000290 MachineCodeForBasicBlock::const_iterator MII = MIVec.begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000291
292 // iterate over all the machine instructions in BB
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000293 //
Vikram S. Adve48762092002-04-25 04:34:15 +0000294 for( ; MII != MIVec.end(); ++MII) {
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000295
Vikram S. Adve48762092002-04-25 04:34:15 +0000296 const MachineInstr *MInst = *MII;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000297
298 // get the LV set after the instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000299 //
Chris Lattner748697d2002-02-05 04:20:12 +0000300 const ValueSet &LVSetAI = LVI->getLiveVarSetAfterMInst(MInst, *BBI);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000301
302 const bool isCallInst = TM.getInstrInfo().isCall(MInst->getOpCode());
303
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000304 if( isCallInst ) {
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000305 // set the isCallInterference flag of each live range wich extends
306 // accross this call instruction. This information is used by graph
307 // coloring algo to avoid allocating volatile colors to live ranges
308 // that span across calls (since they have to be saved/restored)
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000309 //
Chris Lattner748697d2002-02-05 04:20:12 +0000310 setCallInterferences(MInst, &LVSetAI);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000311 }
312
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000313
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000314 // iterate over all MI operands to find defs
315 //
Chris Lattner2f898d22002-02-05 06:02:59 +0000316 for (MachineInstr::const_val_op_iterator OpI = MInst->begin(),
317 OpE = MInst->end(); OpI != OpE; ++OpI) {
318 if (OpI.isDef()) // create a new LR iff this operand is a def
Chris Lattner748697d2002-02-05 04:20:12 +0000319 addInterference(*OpI, &LVSetAI, isCallInst);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000320
321 // Calculate the spill cost of each live range
322 //
Chris Lattner2f898d22002-02-05 06:02:59 +0000323 LiveRange *LR = LRI.getLiveRangeForValue(*OpI);
324 if (LR) LR->addSpillCost(BBLoopDepthCost);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000325 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000326
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000327
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000328 // if there are multiple defs in this instruction e.g. in SETX
329 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000330 if (TM.getInstrInfo().isPseudoInstr(MInst->getOpCode()))
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000331 addInterf4PseudoInstr(MInst);
332
333
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000334 // Also add interference for any implicit definitions in a machine
335 // instr (currently, only calls have this).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000336 //
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000337 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
338 if( NumOfImpRefs > 0 ) {
339 for(unsigned z=0; z < NumOfImpRefs; z++)
340 if( MInst->implicitRefIsDefined(z) )
Chris Lattner748697d2002-02-05 04:20:12 +0000341 addInterference( MInst->getImplicitRef(z), &LVSetAI, isCallInst );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000342 }
343
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +0000344
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000345 } // for all machine instructions in BB
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000346 } // for all BBs in function
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000347
348
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000349 // add interferences for function arguments. Since there are no explict
350 // defs in the function for args, we have to add them manually
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000351 //
352 addInterferencesForArgs();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000353
354 if( DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000355 cerr << "Interference graphs calculted!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000356
357}
358
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000359
360
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000361//--------------------------------------------------------------------------
362// Pseudo instructions will be exapnded to multiple instructions by the
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000363// assembler. Consequently, all the opernds must get distinct registers.
364// Therefore, we mark all operands of a pseudo instruction as they interfere
365// with one another.
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000366//--------------------------------------------------------------------------
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000367void PhyRegAlloc::addInterf4PseudoInstr(const MachineInstr *MInst) {
368
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000369 bool setInterf = false;
370
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000371 // iterate over MI operands to find defs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000372 //
Chris Lattner2f898d22002-02-05 06:02:59 +0000373 for (MachineInstr::const_val_op_iterator It1 = MInst->begin(),
374 ItE = MInst->end(); It1 != ItE; ++It1) {
375 const LiveRange *LROfOp1 = LRI.getLiveRangeForValue(*It1);
376 assert((LROfOp1 || !It1.isDef()) && "No LR for Def in PSEUDO insruction");
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000377
Chris Lattner2f898d22002-02-05 06:02:59 +0000378 MachineInstr::const_val_op_iterator It2 = It1;
379 for(++It2; It2 != ItE; ++It2) {
380 const LiveRange *LROfOp2 = LRI.getLiveRangeForValue(*It2);
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000381
Chris Lattner2f898d22002-02-05 06:02:59 +0000382 if (LROfOp2) {
383 RegClass *RCOfOp1 = LROfOp1->getRegClass();
384 RegClass *RCOfOp2 = LROfOp2->getRegClass();
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000385
386 if( RCOfOp1 == RCOfOp2 ){
387 RCOfOp1->setInterference( LROfOp1, LROfOp2 );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000388 setInterf = true;
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000389 }
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000390 } // if Op2 has a LR
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000391 } // for all other defs in machine instr
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000392 } // for all operands in an instruction
393
Chris Lattner2f898d22002-02-05 06:02:59 +0000394 if (!setInterf && MInst->getNumOperands() > 2) {
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000395 cerr << "\nInterf not set for any operand in pseudo instr:\n";
396 cerr << *MInst;
397 assert(0 && "Interf not set for pseudo instr with > 2 operands" );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000398 }
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000399}
400
401
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000402
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000403//----------------------------------------------------------------------------
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000404// This method will add interferences for incoming arguments to a function.
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000405//----------------------------------------------------------------------------
Chris Lattner296b7732002-02-05 02:52:05 +0000406void PhyRegAlloc::addInterferencesForArgs() {
407 // get the InSet of root BB
Chris Lattner748697d2002-02-05 04:20:12 +0000408 const ValueSet &InSet = LVI->getInSetOfBB(Meth->front());
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000409
Chris Lattner296b7732002-02-05 02:52:05 +0000410 // get the argument list
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000411 const Function::ArgumentListType &ArgList = Meth->getArgumentList();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000412
Chris Lattner296b7732002-02-05 02:52:05 +0000413 // get an iterator to arg list
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000414 Function::ArgumentListType::const_iterator ArgIt = ArgList.begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000415
416
417 for( ; ArgIt != ArgList.end() ; ++ArgIt) { // for each argument
Chris Lattner748697d2002-02-05 04:20:12 +0000418 addInterference((Value*)*ArgIt, &InSet, false);// add interferences between
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000419 // args and LVars at start
Chris Lattner0665a5f2002-02-05 01:43:49 +0000420 if( DEBUG_RA > 1)
421 cerr << " - %% adding interference for argument "
422 << RAV((const Value *)*ArgIt) << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000423 }
424}
425
426
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000427//----------------------------------------------------------------------------
428// This method is called after register allocation is complete to set the
429// allocated reisters in the machine code. This code will add register numbers
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000430// to MachineOperands that contain a Value. Also it calls target specific
431// methods to produce caller saving instructions. At the end, it adds all
432// additional instructions produced by the register allocator to the
433// instruction stream.
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000434//----------------------------------------------------------------------------
Vikram S. Adve48762092002-04-25 04:34:15 +0000435
436//-----------------------------
437// Utility functions used below
438//-----------------------------
439inline void
440PrependInstructions(std::deque<MachineInstr *> &IBef,
441 MachineCodeForBasicBlock& MIVec,
442 MachineCodeForBasicBlock::iterator& MII,
443 const std::string& msg)
444{
445 if (!IBef.empty())
446 {
447 MachineInstr* OrigMI = *MII;
448 std::deque<MachineInstr *>::iterator AdIt;
449 for (AdIt = IBef.begin(); AdIt != IBef.end() ; ++AdIt)
450 {
451 if (DEBUG_RA) {
452 if (OrigMI) cerr << "For MInst: " << *OrigMI;
453 cerr << msg << " PREPENDed instr: " << **AdIt << "\n";
454 }
455 MII = MIVec.insert(MII, *AdIt);
456 ++MII;
457 }
458 }
459}
460
461inline void
462AppendInstructions(std::deque<MachineInstr *> &IAft,
463 MachineCodeForBasicBlock& MIVec,
464 MachineCodeForBasicBlock::iterator& MII,
465 const std::string& msg)
466{
467 if (!IAft.empty())
468 {
469 MachineInstr* OrigMI = *MII;
470 std::deque<MachineInstr *>::iterator AdIt;
471 for( AdIt = IAft.begin(); AdIt != IAft.end() ; ++AdIt )
472 {
473 if(DEBUG_RA) {
474 if (OrigMI) cerr << "For MInst: " << *OrigMI;
475 cerr << msg << " APPENDed instr: " << **AdIt << "\n";
476 }
477 ++MII; // insert before the next instruction
478 MII = MIVec.insert(MII, *AdIt);
479 }
480 }
481}
482
483
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000484void PhyRegAlloc::updateMachineCode()
485{
Vikram S. Adve48762092002-04-25 04:34:15 +0000486 const BasicBlock* entryBB = Meth->getEntryNode();
487 if (entryBB) {
488 MachineCodeForBasicBlock& MIVec = entryBB->getMachineInstrVec();
489 MachineCodeForBasicBlock::iterator MII = MIVec.begin();
490
491 // Insert any instructions needed at method entry
492 PrependInstructions(AddedInstrAtEntry.InstrnsBefore, MIVec, MII,
493 "At function entry: \n");
494 assert(AddedInstrAtEntry.InstrnsAfter.empty() &&
495 "InstrsAfter should be unnecessary since we are just inserting at "
496 "the function entry point here.");
497 }
498
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000499 for (Function::const_iterator BBI = Meth->begin(), BBE = Meth->end();
500 BBI != BBE; ++BBI) {
Vikram S. Adve48762092002-04-25 04:34:15 +0000501
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000502 // iterate over all the machine instructions in BB
Vikram S. Adve48762092002-04-25 04:34:15 +0000503 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
504 for(MachineCodeForBasicBlock::iterator MII = MIVec.begin();
505 MII != MIVec.end(); ++MII) {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000506
Vikram S. Adve48762092002-04-25 04:34:15 +0000507 MachineInstr *MInst = *MII;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000508
509 unsigned Opcode = MInst->getOpCode();
510
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000511 // do not process Phis
Vikram S. Adve23a4c8f2002-03-18 03:37:19 +0000512 if (TM.getInstrInfo().isDummyPhiInstr(Opcode))
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000513 continue;
514
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000515 // Now insert speical instructions (if necessary) for call/return
516 // instructions.
517 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000518 if (TM.getInstrInfo().isCall(Opcode) ||
519 TM.getInstrInfo().isReturn(Opcode)) {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000520
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000521 AddedInstrns &AI = AddedInstrMap[MInst];
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000522
523 // Tmp stack poistions are needed by some calls that have spilled args
524 // So reset it before we call each such method
Ruchira Sasanka6a3db8c2002-01-07 21:09:06 +0000525 //
526 mcInfo.popAllTempValues(TM);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000527
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000528 if (TM.getInstrInfo().isCall(Opcode))
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000529 MRI.colorCallArgs(MInst, LRI, &AI, *this, *BBI);
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000530 else if (TM.getInstrInfo().isReturn(Opcode))
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000531 MRI.colorRetValue(MInst, LRI, &AI);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000532 }
533
534
535 /* -- Using above code instead of this
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000536
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000537 // if this machine instr is call, insert caller saving code
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000538
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000539 if( (TM.getInstrInfo()).isCall( MInst->getOpCode()) )
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000540 MRI.insertCallerSavingCode(MInst, *BBI, *this );
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000541
542 */
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000543
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000544
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000545 // reset the stack offset for temporary variables since we may
546 // need that to spill
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000547 // mcInfo.popAllTempValues(TM);
Ruchira Sasankaf90870f2001-11-15 22:02:06 +0000548 // TODO ** : do later
Vikram S. Adve12af1642001-11-08 04:48:50 +0000549
Chris Lattner7a176752001-12-04 00:03:30 +0000550 //for(MachineInstr::val_const_op_iterator OpI(MInst);!OpI.done();++OpI) {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000551
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000552
553 // Now replace set the registers for operands in the machine instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000554 //
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000555 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
556
557 MachineOperand& Op = MInst->getOperand(OpNum);
558
559 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
560 Op.getOperandType() == MachineOperand::MO_CCRegister) {
561
562 const Value *const Val = Op.getVRegValue();
563
564 // delete this condition checking later (must assert if Val is null)
Chris Lattner045e7c82001-09-19 16:26:23 +0000565 if( !Val) {
566 if (DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000567 cerr << "Warning: NULL Value found for operand\n";
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000568 continue;
569 }
570 assert( Val && "Value is NULL");
571
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000572 LiveRange *const LR = LRI.getLiveRangeForValue(Val);
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000573
574 if ( !LR ) {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000575
576 // nothing to worry if it's a const or a label
577
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000578 if (DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +0000579 cerr << "*NO LR for operand : " << Op ;
580 cerr << " [reg:" << Op.getAllocatedRegNum() << "]";
581 cerr << " in inst:\t" << *MInst << "\n";
Chris Lattner4c3aaa42001-09-19 16:09:04 +0000582 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000583
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000584 // if register is not allocated, mark register as invalid
Ruchira Sasankaa90e7702001-10-15 16:26:38 +0000585 if( Op.getAllocatedRegNum() == -1)
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000586 Op.setRegForValue( MRI.getInvalidRegNum());
Ruchira Sasankae727f852001-09-18 22:43:57 +0000587
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +0000588
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000589 continue;
590 }
591
592 unsigned RCID = (LR->getRegClass())->getID();
593
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000594 if( LR->hasColor() ) {
595 Op.setRegForValue( MRI.getUnifiedRegNum(RCID, LR->getColor()) );
596 }
597 else {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000598
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000599 // LR did NOT receive a color (register). Now, insert spill code
600 // for spilled opeands in this machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000601
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000602 //assert(0 && "LR must be spilled");
603 insertCode4SpilledLR(LR, MInst, *BBI, OpNum );
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000604
605 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000606 }
607
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000608 } // for each operand
609
610
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000611 // Now add instructions that the register allocator inserts before/after
612 // this machine instructions (done only for calls/rets/incoming args)
613 // We do this here, to ensure that spill for an instruction is inserted
614 // closest as possible to an instruction (see above insertCode4Spill...)
615 //
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000616 // If there are instructions to be added, *before* this machine
617 // instruction, add them now.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000618 //
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000619 if(AddedInstrMap.count(MInst)) {
Vikram S. Adve48762092002-04-25 04:34:15 +0000620 PrependInstructions(AddedInstrMap[MInst].InstrnsBefore, MIVec, MII,"");
Ruchira Sasankaf221a2e2001-11-13 23:09:30 +0000621 }
Vikram S. Adve48762092002-04-25 04:34:15 +0000622
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000623 // If there are instructions to be added *after* this machine
624 // instruction, add them now
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000625 //
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000626 if (!AddedInstrMap[MInst].InstrnsAfter.empty()) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000627
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000628 // if there are delay slots for this instruction, the instructions
629 // added after it must really go after the delayed instruction(s)
630 // So, we move the InstrAfter of the current instruction to the
631 // corresponding delayed instruction
632
633 unsigned delay;
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000634 if ((delay=TM.getInstrInfo().getNumDelaySlots(MInst->getOpCode())) >0){
Vikram S. Adve48762092002-04-25 04:34:15 +0000635 move2DelayedInstr(MInst, *(MII+delay) );
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000636
Chris Lattner697954c2002-01-20 22:54:45 +0000637 if(DEBUG_RA) cerr<< "\nMoved an added instr after the delay slot";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000638 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000639
640 else {
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000641 // Here we can add the "instructions after" to the current
642 // instruction since there are no delay slots for this instruction
Vikram S. Adve48762092002-04-25 04:34:15 +0000643 AppendInstructions(AddedInstrMap[MInst].InstrnsAfter, MIVec, MII,"");
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000644 } // if not delay
645
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000646 }
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000647
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000648 } // for each machine instruction
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000649 }
650}
651
652
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000653
654//----------------------------------------------------------------------------
655// This method inserts spill code for AN operand whose LR was spilled.
656// This method may be called several times for a single machine instruction
657// if it contains many spilled operands. Each time it is called, it finds
658// a register which is not live at that instruction and also which is not
659// used by other spilled operands of the same instruction. Then it uses
660// this register temporarily to accomodate the spilled value.
661//----------------------------------------------------------------------------
662void PhyRegAlloc::insertCode4SpilledLR(const LiveRange *LR,
663 MachineInstr *MInst,
664 const BasicBlock *BB,
665 const unsigned OpNum) {
666
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000667 assert(! TM.getInstrInfo().isCall(MInst->getOpCode()) &&
668 (! TM.getInstrInfo().isReturn(MInst->getOpCode())) &&
669 "Arg of a call/ret must be handled elsewhere");
670
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000671 MachineOperand& Op = MInst->getOperand(OpNum);
672 bool isDef = MInst->operandIsDefined(OpNum);
673 unsigned RegType = MRI.getRegType( LR );
674 int SpillOff = LR->getSpillOffFromFP();
675 RegClass *RC = LR->getRegClass();
Chris Lattner748697d2002-02-05 04:20:12 +0000676 const ValueSet &LVSetBef = LVI->getLiveVarSetBeforeMInst(MInst, BB);
Vikram S. Adve00521d72001-11-12 23:26:35 +0000677
Chris Lattner697954c2002-01-20 22:54:45 +0000678 mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000679
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000680 MachineInstr *MIBef=NULL, *AdIMid=NULL, *MIAft=NULL;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000681
Chris Lattner748697d2002-02-05 04:20:12 +0000682 int TmpRegU = getUsableUniRegAtMI(RC, RegType, MInst,&LVSetBef, MIBef, MIAft);
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000683
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000684 // get the added instructions for this instruciton
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000685 AddedInstrns &AI = AddedInstrMap[MInst];
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000686
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000687 if (!isDef) {
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000688 // for a USE, we have to load the value of LR from stack to a TmpReg
689 // and use the TmpReg as one operand of instruction
690
691 // actual loading instruction
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000692 AdIMid = MRI.cpMem2RegMI(MRI.getFramePointer(), SpillOff, TmpRegU,RegType);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000693
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000694 if(MIBef)
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000695 AI.InstrnsBefore.push_back(MIBef);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000696
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000697 AI.InstrnsBefore.push_back(AdIMid);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000698
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000699 if(MIAft)
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000700 AI.InstrnsAfter.push_front(MIAft);
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000701
Chris Lattner296b7732002-02-05 02:52:05 +0000702 } else { // if this is a Def
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000703 // for a DEF, we have to store the value produced by this instruction
704 // on the stack position allocated for this LR
705
706 // actual storing instruction
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000707 AdIMid = MRI.cpReg2MemMI(TmpRegU, MRI.getFramePointer(), SpillOff,RegType);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000708
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000709 if (MIBef)
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000710 AI.InstrnsBefore.push_back(MIBef);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000711
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000712 AI.InstrnsAfter.push_front(AdIMid);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000713
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000714 if (MIAft)
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000715 AI.InstrnsAfter.push_front(MIAft);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000716
717 } // if !DEF
718
719 cerr << "\nFor Inst " << *MInst;
Chris Lattner296b7732002-02-05 02:52:05 +0000720 cerr << " - SPILLED LR: "; printSet(*LR);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000721 cerr << "\n - Added Instructions:";
Chris Lattner296b7732002-02-05 02:52:05 +0000722 if (MIBef) cerr << *MIBef;
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000723 cerr << *AdIMid;
Chris Lattner296b7732002-02-05 02:52:05 +0000724 if (MIAft) cerr << *MIAft;
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000725
Chris Lattner296b7732002-02-05 02:52:05 +0000726 Op.setRegForValue(TmpRegU); // set the opearnd
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000727}
728
729
730
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000731//----------------------------------------------------------------------------
732// We can use the following method to get a temporary register to be used
733// BEFORE any given machine instruction. If there is a register available,
734// this method will simply return that register and set MIBef = MIAft = NULL.
735// Otherwise, it will return a register and MIAft and MIBef will contain
736// two instructions used to free up this returned register.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000737// Returned register number is the UNIFIED register number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000738//----------------------------------------------------------------------------
739
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000740int PhyRegAlloc::getUsableUniRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000741 const int RegType,
742 const MachineInstr *MInst,
Chris Lattner296b7732002-02-05 02:52:05 +0000743 const ValueSet *LVSetBef,
Vikram S. Adve23a4c8f2002-03-18 03:37:19 +0000744 MachineInstr *&MIBef,
745 MachineInstr *&MIAft) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000746
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000747 int RegU = getUnusedUniRegAtMI(RC, MInst, LVSetBef);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000748
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000749
750 if( RegU != -1) {
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000751 // we found an unused register, so we can simply use it
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000752 MIBef = MIAft = NULL;
753 }
754 else {
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000755 // we couldn't find an unused register. Generate code to free up a reg by
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000756 // saving it on stack and restoring after the instruction
757
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000758 int TmpOff = mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) );
Vikram S. Adve12af1642001-11-08 04:48:50 +0000759
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000760 RegU = getUniRegNotUsedByThisInst(RC, MInst);
761 MIBef = MRI.cpReg2MemMI(RegU, MRI.getFramePointer(), TmpOff, RegType );
762 MIAft = MRI.cpMem2RegMI(MRI.getFramePointer(), TmpOff, RegU, RegType );
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000763 }
764
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000765 return RegU;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000766}
767
768//----------------------------------------------------------------------------
769// This method is called to get a new unused register that can be used to
770// accomodate a spilled value.
771// This method may be called several times for a single machine instruction
772// if it contains many spilled operands. Each time it is called, it finds
773// a register which is not live at that instruction and also which is not
774// used by other spilled operands of the same instruction.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +0000775// Return register number is relative to the register class. NOT
776// unified number
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000777//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000778int PhyRegAlloc::getUnusedUniRegAtMI(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000779 const MachineInstr *MInst,
Chris Lattner296b7732002-02-05 02:52:05 +0000780 const ValueSet *LVSetBef) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000781
782 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
783
784 bool *IsColorUsedArr = RC->getIsColorUsedArr();
785
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000786 for(unsigned i=0; i < NumAvailRegs; i++) // Reset array
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000787 IsColorUsedArr[i] = false;
788
Chris Lattner296b7732002-02-05 02:52:05 +0000789 ValueSet::const_iterator LIt = LVSetBef->begin();
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000790
791 // for each live var in live variable set after machine inst
792 for( ; LIt != LVSetBef->end(); ++LIt) {
793
794 // get the live range corresponding to live var
795 LiveRange *const LRofLV = LRI.getLiveRangeForValue(*LIt );
796
797 // LR can be null if it is a const since a const
798 // doesn't have a dominating def - see Assumptions above
799 if( LRofLV )
800 if( LRofLV->hasColor() )
801 IsColorUsedArr[ LRofLV->getColor() ] = true;
802 }
803
804 // It is possible that one operand of this MInst was already spilled
805 // and it received some register temporarily. If that's the case,
806 // it is recorded in machine operand. We must skip such registers.
807
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000808 setRelRegsUsedByThisInst(RC, MInst);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000809
810 unsigned c; // find first unused color
811 for( c=0; c < NumAvailRegs; c++)
812 if( ! IsColorUsedArr[ c ] ) break;
813
814 if(c < NumAvailRegs)
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000815 return MRI.getUnifiedRegNum(RC->getID(), c);
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000816 else
817 return -1;
818
819
820}
821
822
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000823//----------------------------------------------------------------------------
824// Get any other register in a register class, other than what is used
825// by operands of a machine instruction. Returns the unified reg number.
826//----------------------------------------------------------------------------
827int PhyRegAlloc::getUniRegNotUsedByThisInst(RegClass *RC,
828 const MachineInstr *MInst) {
829
830 bool *IsColorUsedArr = RC->getIsColorUsedArr();
831 unsigned NumAvailRegs = RC->getNumOfAvailRegs();
832
833
834 for(unsigned i=0; i < NumAvailRegs ; i++) // Reset array
835 IsColorUsedArr[i] = false;
836
837 setRelRegsUsedByThisInst(RC, MInst);
838
839 unsigned c; // find first unused color
840 for( c=0; c < RC->getNumOfAvailRegs(); c++)
841 if( ! IsColorUsedArr[ c ] ) break;
842
843 if(c < NumAvailRegs)
844 return MRI.getUnifiedRegNum(RC->getID(), c);
845 else
846 assert( 0 && "FATAL: No free register could be found in reg class!!");
Chris Lattner697954c2002-01-20 22:54:45 +0000847 return 0;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000848}
849
850
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000851//----------------------------------------------------------------------------
852// This method modifies the IsColorUsedArr of the register class passed to it.
853// It sets the bits corresponding to the registers used by this machine
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000854// instructions. Both explicit and implicit operands are set.
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000855//----------------------------------------------------------------------------
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000856void PhyRegAlloc::setRelRegsUsedByThisInst(RegClass *RC,
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000857 const MachineInstr *MInst ) {
858
859 bool *IsColorUsedArr = RC->getIsColorUsedArr();
860
861 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
862
863 const MachineOperand& Op = MInst->getOperand(OpNum);
864
865 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000866 Op.getOperandType() == MachineOperand::MO_CCRegister ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000867
868 const Value *const Val = Op.getVRegValue();
869
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000870 if( Val )
871 if( MRI.getRegClassIDOfValue(Val) == RC->getID() ) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000872 int Reg;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000873 if( (Reg=Op.getAllocatedRegNum()) != -1) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000874 IsColorUsedArr[ Reg ] = true;
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000875 }
876 else {
877 // it is possilbe that this operand still is not marked with
878 // a register but it has a LR and that received a color
879
880 LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
881 if( LROfVal)
882 if( LROfVal->hasColor() )
883 IsColorUsedArr[ LROfVal->getColor() ] = true;
884 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000885
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000886 } // if reg classes are the same
887 }
888 else if (Op.getOperandType() == MachineOperand::MO_MachineRegister) {
889 IsColorUsedArr[ Op.getMachineRegNum() ] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000890 }
891 }
892
893 // If there are implicit references, mark them as well
894
895 for(unsigned z=0; z < MInst->getNumImplicitRefs(); z++) {
896
897 LiveRange *const LRofImpRef =
898 LRI.getLiveRangeForValue( MInst->getImplicitRef(z) );
Chris Lattner697954c2002-01-20 22:54:45 +0000899
900 if(LRofImpRef && LRofImpRef->hasColor())
901 IsColorUsedArr[LRofImpRef->getColor()] = true;
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000902 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000903}
904
905
906
Ruchira Sasanka174bded2001-10-28 18:12:02 +0000907
908
909
910
911
912//----------------------------------------------------------------------------
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000913// If there are delay slots for an instruction, the instructions
914// added after it must really go after the delayed instruction(s).
915// So, we move the InstrAfter of that instruction to the
916// corresponding delayed instruction using the following method.
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000917
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000918//----------------------------------------------------------------------------
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000919void PhyRegAlloc::move2DelayedInstr(const MachineInstr *OrigMI,
920 const MachineInstr *DelayedMI) {
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000921
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000922 // "added after" instructions of the original instr
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000923 std::deque<MachineInstr *> &OrigAft = AddedInstrMap[OrigMI].InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000924
925 // "added instructions" of the delayed instr
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000926 AddedInstrns &DelayAdI = AddedInstrMap[DelayedMI];
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000927
928 // "added after" instructions of the delayed instr
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000929 std::deque<MachineInstr *> &DelayedAft = DelayAdI.InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000930
931 // go thru all the "added after instructions" of the original instruction
932 // and append them to the "addded after instructions" of the delayed
933 // instructions
Chris Lattner697954c2002-01-20 22:54:45 +0000934 DelayedAft.insert(DelayedAft.end(), OrigAft.begin(), OrigAft.end());
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000935
936 // empty the "added after instructions" of the original instruction
937 OrigAft.clear();
Ruchira Sasanka251d8db2001-10-23 21:38:00 +0000938}
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000939
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000940//----------------------------------------------------------------------------
941// This method prints the code with registers after register allocation is
942// complete.
943//----------------------------------------------------------------------------
944void PhyRegAlloc::printMachineCode()
945{
946
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000947 cerr << "\n;************** Function " << Meth->getName()
Chris Lattner697954c2002-01-20 22:54:45 +0000948 << " *****************\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000949
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000950 for (Function::const_iterator BBI = Meth->begin(), BBE = Meth->end();
951 BBI != BBE; ++BBI) {
952 cerr << "\n"; printLabel(*BBI); cerr << ": ";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000953
954 // get the iterator for machine instructions
955 MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
Vikram S. Adve48762092002-04-25 04:34:15 +0000956 MachineCodeForBasicBlock::iterator MII = MIVec.begin();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000957
958 // iterate over all the machine instructions in BB
Vikram S. Adve48762092002-04-25 04:34:15 +0000959 for( ; MII != MIVec.end(); ++MII) {
960 MachineInstr *const MInst = *MII;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000961
Chris Lattner697954c2002-01-20 22:54:45 +0000962 cerr << "\n\t";
963 cerr << TargetInstrDescriptors[MInst->getOpCode()].opCodeString;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000964
965 for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000966 MachineOperand& Op = MInst->getOperand(OpNum);
967
968 if( Op.getOperandType() == MachineOperand::MO_VirtualRegister ||
Ruchira Sasanka97b8b442001-10-18 22:36:26 +0000969 Op.getOperandType() == MachineOperand::MO_CCRegister /*||
970 Op.getOperandType() == MachineOperand::MO_PCRelativeDisp*/ ) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000971
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000972 const Value *const Val = Op.getVRegValue () ;
Ruchira Sasankae727f852001-09-18 22:43:57 +0000973 // ****this code is temporary till NULL Values are fixed
974 if( ! Val ) {
Chris Lattner697954c2002-01-20 22:54:45 +0000975 cerr << "\t<*NULL*>";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000976 continue;
977 }
Ruchira Sasankae727f852001-09-18 22:43:57 +0000978
979 // if a label or a constant
Chris Lattnerdbe53042002-01-21 01:33:12 +0000980 if(isa<BasicBlock>(Val)) {
Chris Lattner697954c2002-01-20 22:54:45 +0000981 cerr << "\t"; printLabel( Op.getVRegValue () );
982 } else {
Ruchira Sasankae727f852001-09-18 22:43:57 +0000983 // else it must be a register value
984 const int RegNum = Op.getAllocatedRegNum();
985
Chris Lattner697954c2002-01-20 22:54:45 +0000986 cerr << "\t" << "%" << MRI.getUnifiedRegName( RegNum );
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000987 if (Val->hasName() )
Chris Lattner697954c2002-01-20 22:54:45 +0000988 cerr << "(" << Val->getName() << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000989 else
Chris Lattner697954c2002-01-20 22:54:45 +0000990 cerr << "(" << Val << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000991
992 if( Op.opIsDef() )
Chris Lattner697954c2002-01-20 22:54:45 +0000993 cerr << "*";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000994
995 const LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
996 if( LROfVal )
997 if( LROfVal->hasSpillOffset() )
Chris Lattner697954c2002-01-20 22:54:45 +0000998 cerr << "$";
Ruchira Sasankae727f852001-09-18 22:43:57 +0000999 }
1000
1001 }
1002 else if(Op.getOperandType() == MachineOperand::MO_MachineRegister) {
Chris Lattner697954c2002-01-20 22:54:45 +00001003 cerr << "\t" << "%" << MRI.getUnifiedRegName(Op.getMachineRegNum());
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001004 }
1005
1006 else
Chris Lattner697954c2002-01-20 22:54:45 +00001007 cerr << "\t" << Op; // use dump field
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001008 }
1009
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001010
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001011
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001012 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
Chris Lattner0665a5f2002-02-05 01:43:49 +00001013 if( NumOfImpRefs > 0) {
Chris Lattner697954c2002-01-20 22:54:45 +00001014 cerr << "\tImplicit:";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001015
Chris Lattner0665a5f2002-02-05 01:43:49 +00001016 for(unsigned z=0; z < NumOfImpRefs; z++)
1017 cerr << RAV(MInst->getImplicitRef(z)) << "\t";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001018 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001019
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001020 } // for all machine instructions
1021
Chris Lattner697954c2002-01-20 22:54:45 +00001022 cerr << "\n";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001023
1024 } // for all BBs
1025
Chris Lattner697954c2002-01-20 22:54:45 +00001026 cerr << "\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001027}
1028
Ruchira Sasankae727f852001-09-18 22:43:57 +00001029
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001030#if 0
1031
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001032//----------------------------------------------------------------------------
1033//
1034//----------------------------------------------------------------------------
1035
1036void PhyRegAlloc::colorCallRetArgs()
1037{
1038
1039 CallRetInstrListType &CallRetInstList = LRI.getCallRetInstrList();
1040 CallRetInstrListType::const_iterator It = CallRetInstList.begin();
1041
1042 for( ; It != CallRetInstList.end(); ++It ) {
1043
Ruchira Sasankaa90e7702001-10-15 16:26:38 +00001044 const MachineInstr *const CRMI = *It;
1045 unsigned OpCode = CRMI->getOpCode();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001046
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001047 // get the added instructions for this Call/Ret instruciton
Chris Lattner0b0ffa02002-04-09 05:13:04 +00001048 AddedInstrns &AI = AddedInstrMap[CRMI];
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001049
Chris Lattner0b0ffa02002-04-09 05:13:04 +00001050 // Tmp stack positions are needed by some calls that have spilled args
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001051 // So reset it before we call each such method
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001052 //mcInfo.popAllTempValues(TM);
1053
Vikram S. Adve12af1642001-11-08 04:48:50 +00001054
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001055 if (TM.getInstrInfo().isCall(OpCode))
Chris Lattner0b0ffa02002-04-09 05:13:04 +00001056 MRI.colorCallArgs(CRMI, LRI, &AI, *this);
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001057 else if (TM.getInstrInfo().isReturn(OpCode))
Chris Lattner0b0ffa02002-04-09 05:13:04 +00001058 MRI.colorRetValue(CRMI, LRI, &AI);
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001059 else
1060 assert(0 && "Non Call/Ret instrn in CallRetInstrList\n");
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001061 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001062}
1063
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001064#endif
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001065
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001066//----------------------------------------------------------------------------
1067
1068//----------------------------------------------------------------------------
1069void PhyRegAlloc::colorIncomingArgs()
1070{
1071 const BasicBlock *const FirstBB = Meth->front();
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001072 const MachineInstr *FirstMI = FirstBB->getMachineInstrVec().front();
1073 assert(FirstMI && "No machine instruction in entry BB");
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001074
Vikram S. Adve48762092002-04-25 04:34:15 +00001075 MRI.colorMethodArgs(Meth, LRI, &AddedInstrAtEntry);
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001076}
1077
Ruchira Sasankae727f852001-09-18 22:43:57 +00001078
1079//----------------------------------------------------------------------------
1080// Used to generate a label for a basic block
1081//----------------------------------------------------------------------------
Chris Lattner697954c2002-01-20 22:54:45 +00001082void PhyRegAlloc::printLabel(const Value *const Val) {
1083 if (Val->hasName())
1084 cerr << Val->getName();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001085 else
Chris Lattner697954c2002-01-20 22:54:45 +00001086 cerr << "Label" << Val;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001087}
1088
1089
Ruchira Sasankae727f852001-09-18 22:43:57 +00001090//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001091// This method calls setSugColorUsable method of each live range. This
1092// will determine whether the suggested color of LR is really usable.
1093// A suggested color is not usable when the suggested color is volatile
1094// AND when there are call interferences
1095//----------------------------------------------------------------------------
1096
1097void PhyRegAlloc::markUnusableSugColors()
1098{
Chris Lattner697954c2002-01-20 22:54:45 +00001099 if(DEBUG_RA ) cerr << "\nmarking unusable suggested colors ...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001100
1101 // hash map iterator
1102 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
1103 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
1104
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001105 for(; HMI != HMIEnd ; ++HMI ) {
1106 if (HMI->first) {
1107 LiveRange *L = HMI->second; // get the LiveRange
1108 if (L) {
1109 if(L->hasSuggestedColor()) {
1110 int RCID = L->getRegClass()->getID();
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001111 if( MRI.isRegVolatile( RCID, L->getSuggestedColor()) &&
1112 L->isCallInterference() )
1113 L->setSuggestedColorUsable( false );
1114 else
1115 L->setSuggestedColorUsable( true );
1116 }
1117 } // if L->hasSuggestedColor()
1118 }
1119 } // for all LR's in hash map
1120}
1121
1122
1123
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001124//----------------------------------------------------------------------------
1125// The following method will set the stack offsets of the live ranges that
1126// are decided to be spillled. This must be called just after coloring the
1127// LRs using the graph coloring algo. For each live range that is spilled,
1128// this method allocate a new spill position on the stack.
1129//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001130
Chris Lattner37730942002-02-05 03:52:29 +00001131void PhyRegAlloc::allocateStackSpace4SpilledLRs() {
1132 if (DEBUG_RA) cerr << "\nsetting LR stack offsets ...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001133
Chris Lattner37730942002-02-05 03:52:29 +00001134 LiveRangeMapType::const_iterator HMI = LRI.getLiveRangeMap()->begin();
1135 LiveRangeMapType::const_iterator HMIEnd = LRI.getLiveRangeMap()->end();
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001136
Chris Lattner37730942002-02-05 03:52:29 +00001137 for( ; HMI != HMIEnd ; ++HMI) {
1138 if (HMI->first && HMI->second) {
1139 LiveRange *L = HMI->second; // get the LiveRange
1140 if (!L->hasColor()) // NOTE: ** allocating the size of long Type **
1141 L->setSpillOffFromFP(mcInfo.allocateSpilledValue(TM, Type::LongTy));
1142 }
1143 } // for all LR's in hash map
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001144}
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001145
1146
1147
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001148//----------------------------------------------------------------------------
Ruchira Sasankae727f852001-09-18 22:43:57 +00001149// The entry pont to Register Allocation
1150//----------------------------------------------------------------------------
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001151
1152void PhyRegAlloc::allocateRegisters()
1153{
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001154
1155 // make sure that we put all register classes into the RegClassList
1156 // before we call constructLiveRanges (now done in the constructor of
1157 // PhyRegAlloc class).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001158 //
1159 LRI.constructLiveRanges(); // create LR info
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001160
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001161 if (DEBUG_RA)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001162 LRI.printLiveRanges();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001163
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001164 createIGNodeListsAndIGs(); // create IGNode list and IGs
1165
1166 buildInterferenceGraphs(); // build IGs in all reg classes
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001167
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001168
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001169 if (DEBUG_RA) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001170 // print all LRs in all reg classes
1171 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1172 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001173
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001174 // print IGs in all register classes
1175 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1176 RegClassList[ rc ]->printIG();
1177 }
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001178
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001179
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001180 LRI.coalesceLRs(); // coalesce all live ranges
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001181
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +00001182
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001183 if( DEBUG_RA) {
1184 // print all LRs in all reg classes
1185 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1186 RegClassList[ rc ]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001187
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001188 // print IGs in all register classes
1189 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1190 RegClassList[ rc ]->printIG();
1191 }
1192
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001193
1194 // mark un-usable suggested color before graph coloring algorithm.
1195 // When this is done, the graph coloring algo will not reserve
1196 // suggested color unnecessarily - they can be used by another LR
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001197 //
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001198 markUnusableSugColors();
1199
1200 // color all register classes using the graph coloring algo
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001201 for( unsigned int rc=0; rc < NumOfRegClasses ; rc++)
1202 RegClassList[ rc ]->colorAllRegs();
1203
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001204 // Atter grpah coloring, if some LRs did not receive a color (i.e, spilled)
1205 // a poistion for such spilled LRs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001206 //
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001207 allocateStackSpace4SpilledLRs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001208
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001209 mcInfo.popAllTempValues(TM); // TODO **Check
1210
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001211 // color incoming args - if the correct color was not received
1212 // insert code to copy to the correct register
1213 //
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001214 colorIncomingArgs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001215
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001216 // Now update the machine code with register names and add any
1217 // additional code inserted by the register allocator to the instruction
1218 // stream
1219 //
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001220 updateMachineCode();
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001221
Chris Lattner045e7c82001-09-19 16:26:23 +00001222 if (DEBUG_RA) {
Vikram S. Adve12af1642001-11-08 04:48:50 +00001223 MachineCodeForMethod::get(Meth).dump();
Chris Lattner045e7c82001-09-19 16:26:23 +00001224 printMachineCode(); // only for DEBUGGING
1225 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001226}
1227
Ruchira Sasankae727f852001-09-18 22:43:57 +00001228
1229