blob: 6d40e1d082461a25d3c36a173d6ebd60f86f4d04 [file] [log] [blame]
Chris Lattner179cdfb2002-08-09 20:08:03 +00001//===-- PhyRegAlloc.cpp ---------------------------------------------------===//
Vikram S. Adve12af1642001-11-08 04:48:50 +00002//
Chris Lattner179cdfb2002-08-09 20:08:03 +00003// Register allocation for LLVM.
4//
5//===----------------------------------------------------------------------===//
Ruchira Sasanka8e604792001-09-14 21:18:34 +00006
Chris Lattner6dd98a62002-02-04 00:33:08 +00007#include "llvm/CodeGen/RegisterAllocation.h"
Chris Lattner4309e732003-01-15 19:57:07 +00008#include "RegAllocCommon.h"
Chris Lattner9d4ed152003-01-15 21:14:01 +00009#include "RegClass.h"
Chris Lattnercb6b4bd2002-10-29 16:51:05 +000010#include "llvm/CodeGen/IGNode.h"
Vikram S. Adve12af1642001-11-08 04:48:50 +000011#include "llvm/CodeGen/PhyRegAlloc.h"
Chris Lattnerf6ee49f2003-01-15 18:08:07 +000012#include "llvm/CodeGen/MachineInstrBuilder.h"
Vikram S. Advedabb41d2002-05-19 15:29:31 +000013#include "llvm/CodeGen/MachineInstrAnnot.h"
Misha Brukmanfce11432002-10-28 00:28:31 +000014#include "llvm/CodeGen/MachineFunction.h"
Chris Lattnere90fcb72002-12-28 20:35:34 +000015#include "llvm/CodeGen/MachineFunctionInfo.h"
Chris Lattner92ba2aa2003-01-14 23:05:08 +000016#include "llvm/CodeGen/FunctionLiveVarInfo.h"
Vikram S. Adve814030a2003-07-29 19:49:21 +000017#include "llvm/CodeGen/InstrSelection.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"
Chris Lattner8bd66e62002-12-28 21:00:25 +000020#include "llvm/Target/TargetFrameInfo.h"
Chris Lattner3501fea2003-01-14 22:00:31 +000021#include "llvm/Target/TargetInstrInfo.h"
Vikram S. Advebc001b22003-07-25 21:06:09 +000022#include "llvm/Target/TargetRegInfo.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000023#include "llvm/Function.h"
Chris Lattner37730942002-02-05 03:52:29 +000024#include "llvm/Type.h"
Vikram S. Advedabb41d2002-05-19 15:29:31 +000025#include "llvm/iOther.h"
Vikram S. Advef5af6362002-07-08 23:15:32 +000026#include "Support/STLExtras.h"
Vikram S. Advefeb32982003-08-12 22:22:24 +000027#include "Support/SetOperations.h"
Chris Lattner4bc23482002-09-15 07:07:55 +000028#include "Support/CommandLine.h"
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000029#include <math.h>
Chris Lattner697954c2002-01-20 22:54:45 +000030using std::cerr;
Anand Shuklacfb22d32002-06-25 20:55:50 +000031using std::vector;
Vikram S. Adve12af1642001-11-08 04:48:50 +000032
Chris Lattner70e60cb2002-05-22 17:08:27 +000033RegAllocDebugLevel_t DEBUG_RA;
Vikram S. Adve39c94e12002-09-14 23:05:33 +000034
Chris Lattner5ff62e92002-07-22 02:10:13 +000035static cl::opt<RegAllocDebugLevel_t, true>
36DRA_opt("dregalloc", cl::Hidden, cl::location(DEBUG_RA),
37 cl::desc("enable register allocation debugging information"),
38 cl::values(
Vikram S. Adve39c94e12002-09-14 23:05:33 +000039 clEnumValN(RA_DEBUG_None , "n", "disable debug output"),
40 clEnumValN(RA_DEBUG_Results, "y", "debug output for allocation results"),
41 clEnumValN(RA_DEBUG_Coloring, "c", "debug output for graph coloring step"),
42 clEnumValN(RA_DEBUG_Interference,"ig","debug output for interference graphs"),
43 clEnumValN(RA_DEBUG_LiveRanges , "lr","debug output for live ranges"),
44 clEnumValN(RA_DEBUG_Verbose, "v", "extra debug output"),
Chris Lattner5ff62e92002-07-22 02:10:13 +000045 0));
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000046
Chris Lattner2f9b28e2002-02-04 15:54:09 +000047//----------------------------------------------------------------------------
48// RegisterAllocation pass front end...
49//----------------------------------------------------------------------------
50namespace {
Chris Lattnerf57b8452002-04-27 06:56:12 +000051 class RegisterAllocator : public FunctionPass {
Chris Lattner2f9b28e2002-02-04 15:54:09 +000052 TargetMachine &Target;
53 public:
54 inline RegisterAllocator(TargetMachine &T) : Target(T) {}
Chris Lattner96c466b2002-04-29 14:57:45 +000055
56 const char *getPassName() const { return "Register Allocation"; }
Chris Lattner6dd98a62002-02-04 00:33:08 +000057
Chris Lattner7e708292002-06-25 16:13:24 +000058 bool runOnFunction(Function &F) {
Chris Lattner2f9b28e2002-02-04 15:54:09 +000059 if (DEBUG_RA)
Chris Lattner7e708292002-06-25 16:13:24 +000060 cerr << "\n********* Function "<< F.getName() << " ***********\n";
Chris Lattner2f9b28e2002-02-04 15:54:09 +000061
Chris Lattner7e708292002-06-25 16:13:24 +000062 PhyRegAlloc PRA(&F, Target, &getAnalysis<FunctionLiveVarInfo>(),
Chris Lattner1b7f7dc2002-04-28 16:21:30 +000063 &getAnalysis<LoopInfo>());
Chris Lattner2f9b28e2002-02-04 15:54:09 +000064 PRA.allocateRegisters();
65
66 if (DEBUG_RA) cerr << "\nRegister allocation complete!\n";
67 return false;
68 }
Chris Lattner4911c352002-02-04 17:39:42 +000069
Chris Lattnerf57b8452002-04-27 06:56:12 +000070 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerdd5b4952002-08-08 19:01:28 +000071 AU.addRequired<LoopInfo>();
72 AU.addRequired<FunctionLiveVarInfo>();
Chris Lattner4911c352002-02-04 17:39:42 +000073 }
Chris Lattner2f9b28e2002-02-04 15:54:09 +000074 };
Chris Lattner6dd98a62002-02-04 00:33:08 +000075}
76
Brian Gaekebf3c4cf2003-08-14 06:09:32 +000077FunctionPass *getRegisterAllocator(TargetMachine &T) {
Chris Lattner2f9b28e2002-02-04 15:54:09 +000078 return new RegisterAllocator(T);
79}
Chris Lattner6dd98a62002-02-04 00:33:08 +000080
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +000081//----------------------------------------------------------------------------
82// Constructor: Init local composite objects and create register classes.
83//----------------------------------------------------------------------------
Chris Lattner1b7f7dc2002-04-28 16:21:30 +000084PhyRegAlloc::PhyRegAlloc(Function *F, const TargetMachine& tm,
85 FunctionLiveVarInfo *Lvi, LoopInfo *LDC)
Chris Lattnerf726e772002-10-28 19:22:04 +000086 : TM(tm), Fn(F), MF(MachineFunction::get(F)), LVI(Lvi),
87 LRI(F, tm, RegClassList), MRI(tm.getRegInfo()),
88 NumOfRegClasses(MRI.getNumOfRegClasses()), LoopDepthCalc(LDC) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +000089
90 // create each RegisterClass and put in RegClassList
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000091 //
Chris Lattnerf726e772002-10-28 19:22:04 +000092 for (unsigned rc=0; rc != NumOfRegClasses; rc++)
Vikram S. Advebc001b22003-07-25 21:06:09 +000093 RegClassList.push_back(new RegClass(F, &tm.getRegInfo(),
94 MRI.getMachineRegClass(rc)));
Ruchira Sasanka8e604792001-09-14 21:18:34 +000095}
96
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000097
98//----------------------------------------------------------------------------
99// Destructor: Deletes register classes
100//----------------------------------------------------------------------------
101PhyRegAlloc::~PhyRegAlloc() {
Chris Lattner7e708292002-06-25 16:13:24 +0000102 for ( unsigned rc=0; rc < NumOfRegClasses; rc++)
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000103 delete RegClassList[rc];
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000104
105 AddedInstrMap.clear();
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000106}
107
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000108//----------------------------------------------------------------------------
109// This method initally creates interference graphs (one in each reg class)
110// and IGNodeList (one in each IG). The actual nodes will be pushed later.
111//----------------------------------------------------------------------------
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000112void PhyRegAlloc::createIGNodeListsAndIGs() {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000113 if (DEBUG_RA >= RA_DEBUG_LiveRanges) cerr << "Creating LR lists ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000114
115 // hash map iterator
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000116 LiveRangeMapType::const_iterator HMI = LRI.getLiveRangeMap()->begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000117
118 // hash map end
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000119 LiveRangeMapType::const_iterator HMIEnd = LRI.getLiveRangeMap()->end();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000120
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000121 for (; HMI != HMIEnd ; ++HMI ) {
122 if (HMI->first) {
123 LiveRange *L = HMI->second; // get the LiveRange
124 if (!L) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000125 if (DEBUG_RA)
126 cerr << "\n**** ?!?WARNING: NULL LIVE RANGE FOUND FOR: "
127 << RAV(HMI->first) << "****\n";
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000128 continue;
129 }
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000130
131 // if the Value * is not null, and LR is not yet written to the IGNodeList
Chris Lattner7e708292002-06-25 16:13:24 +0000132 if (!(L->getUserIGNode()) ) {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000133 RegClass *const RC = // RegClass of first value in the LR
134 RegClassList[ L->getRegClass()->getID() ];
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000135 RC->addLRToIG(L); // add this LR to an IG
136 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000137 }
138 }
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000139
140 // init RegClassList
Chris Lattner7e708292002-06-25 16:13:24 +0000141 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000142 RegClassList[rc]->createInterferenceGraph();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000143
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000144 if (DEBUG_RA >= RA_DEBUG_LiveRanges) cerr << "LRLists Created!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000145}
146
147
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000148//----------------------------------------------------------------------------
149// This method will add all interferences at for a given instruction.
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000150// Interence occurs only if the LR of Def (Inst or Arg) is of the same reg
151// class as that of live var. The live var passed to this function is the
152// LVset AFTER the instruction
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000153//----------------------------------------------------------------------------
Vikram S. Adve39c94e12002-09-14 23:05:33 +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 //
Chris Lattner7e708292002-06-25 16:13:24 +0000172 for ( ; LIt != LVSet->end(); ++LIt) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000173
Vikram S. Advef5af6362002-07-08 23:15:32 +0000174 if (DEBUG_RA >= RA_DEBUG_Verbose)
Chris Lattner0665a5f2002-02-05 01:43:49 +0000175 cerr << "< Def=" << RAV(Def) << ", Lvar=" << RAV(*LIt) << "> ";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000176
177 // get the live range corresponding to live var
Vikram S. Adve39c94e12002-09-14 23:05:33 +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 //
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000184 if (LROfVar)
185 if (LROfDef != LROfVar) // do not set interf for same LR
186 if (RCOfDef == LROfVar->getRegClass()) // 2 reg classes are the same
187 RCOfDef->setInterference( LROfDef, LROfVar);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000188 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000189}
190
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000191
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000192
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000193//----------------------------------------------------------------------------
194// For a call instruction, this method sets the CallInterference flag in
195// the LR of each variable live int the Live Variable Set live after the
196// call instruction (except the return value of the call instruction - since
197// the return value does not interfere with that call itself).
198//----------------------------------------------------------------------------
199
200void PhyRegAlloc::setCallInterferences(const MachineInstr *MInst,
Chris Lattner296b7732002-02-05 02:52:05 +0000201 const ValueSet *LVSetAft) {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000202
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000203 if (DEBUG_RA >= RA_DEBUG_Interference)
Chris Lattner697954c2002-01-20 22:54:45 +0000204 cerr << "\n For call inst: " << *MInst;
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000205
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000206 // for each live var in live variable set after machine inst
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000207 //
Vikram S. Adve65b2f402003-07-02 01:24:00 +0000208 for (ValueSet::const_iterator LIt = LVSetAft->begin(), LEnd = LVSetAft->end();
209 LIt != LEnd; ++LIt) {
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000210
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000211 // get the live range corresponding to live var
212 //
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000213 LiveRange *const LR = LRI.getLiveRangeForValue(*LIt );
214
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000215 // LR can be null if it is a const since a const
216 // doesn't have a dominating def - see Assumptions above
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000217 //
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000218 if (LR ) {
219 if (DEBUG_RA >= RA_DEBUG_Interference) {
220 cerr << "\n\tLR after Call: ";
221 printSet(*LR);
222 }
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000223 LR->setCallInterference();
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000224 if (DEBUG_RA >= RA_DEBUG_Interference) {
225 cerr << "\n ++After adding call interference for LR: " ;
Chris Lattner296b7732002-02-05 02:52:05 +0000226 printSet(*LR);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000227 }
228 }
229
230 }
231
Vikram S. Adve1a53f032002-03-31 18:54:37 +0000232 // Now find the LR of the return value of the call
233 // We do this because, we look at the LV set *after* the instruction
234 // to determine, which LRs must be saved across calls. The return value
235 // of the call is live in this set - but it does not interfere with call
236 // (i.e., we can allocate a volatile register to the return value)
237 //
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000238 CallArgsDescriptor* argDesc = CallArgsDescriptor::get(MInst);
239
240 if (const Value *RetVal = argDesc->getReturnValue()) {
Vikram S. Adve1a53f032002-03-31 18:54:37 +0000241 LiveRange *RetValLR = LRI.getLiveRangeForValue( RetVal );
242 assert( RetValLR && "No LR for RetValue of call");
243 RetValLR->clearCallInterference();
244 }
245
246 // If the CALL is an indirect call, find the LR of the function pointer.
247 // That has a call interference because it conflicts with outgoing args.
Chris Lattner7e708292002-06-25 16:13:24 +0000248 if (const Value *AddrVal = argDesc->getIndirectFuncPtr()) {
Vikram S. Adve1a53f032002-03-31 18:54:37 +0000249 LiveRange *AddrValLR = LRI.getLiveRangeForValue( AddrVal );
250 assert( AddrValLR && "No LR for indirect addr val of call");
251 AddrValLR->setCallInterference();
252 }
253
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000254}
255
256
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000257
258
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000259//----------------------------------------------------------------------------
260// This method will walk thru code and create interferences in the IG of
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000261// each RegClass. Also, this method calculates the spill cost of each
262// Live Range (it is done in this method to save another pass over the code).
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000263//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000264void PhyRegAlloc::buildInterferenceGraphs()
265{
266
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000267 if (DEBUG_RA >= RA_DEBUG_Interference)
268 cerr << "Creating interference graphs ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000269
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000270 unsigned BBLoopDepthCost;
Chris Lattnerf726e772002-10-28 19:22:04 +0000271 for (MachineFunction::iterator BBI = MF.begin(), BBE = MF.end();
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000272 BBI != BBE; ++BBI) {
Chris Lattnerf726e772002-10-28 19:22:04 +0000273 const MachineBasicBlock &MBB = *BBI;
274 const BasicBlock *BB = MBB.getBasicBlock();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000275
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000276 // find the 10^(loop_depth) of this BB
277 //
Chris Lattnerf726e772002-10-28 19:22:04 +0000278 BBLoopDepthCost = (unsigned)pow(10.0, LoopDepthCalc->getLoopDepth(BB));
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000279
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000280 // get the iterator for machine instructions
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000281 //
Chris Lattnerf726e772002-10-28 19:22:04 +0000282 MachineBasicBlock::const_iterator MII = MBB.begin();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000283
284 // iterate over all the machine instructions in BB
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000285 //
Chris Lattnerf726e772002-10-28 19:22:04 +0000286 for ( ; MII != MBB.end(); ++MII) {
287 const MachineInstr *MInst = *MII;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000288
289 // get the LV set after the instruction
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000290 //
Chris Lattnerf726e772002-10-28 19:22:04 +0000291 const ValueSet &LVSetAI = LVI->getLiveVarSetAfterMInst(MInst, BB);
292 bool isCallInst = TM.getInstrInfo().isCall(MInst->getOpCode());
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000293
Chris Lattner7e708292002-06-25 16:13:24 +0000294 if (isCallInst ) {
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000295 // set the isCallInterference flag of each live range wich extends
296 // accross this call instruction. This information is used by graph
297 // coloring algo to avoid allocating volatile colors to live ranges
298 // that span across calls (since they have to be saved/restored)
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000299 //
Chris Lattner748697d2002-02-05 04:20:12 +0000300 setCallInterferences(MInst, &LVSetAI);
Ruchira Sasanka958faf32001-10-19 17:21:03 +0000301 }
302
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000303 // iterate over all MI operands to find defs
304 //
Chris Lattner2f898d22002-02-05 06:02:59 +0000305 for (MachineInstr::const_val_op_iterator OpI = MInst->begin(),
306 OpE = MInst->end(); OpI != OpE; ++OpI) {
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000307 if (OpI.isDefOnly() || OpI.isDefAndUse()) // create a new LR since def
Chris Lattner748697d2002-02-05 04:20:12 +0000308 addInterference(*OpI, &LVSetAI, isCallInst);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000309
310 // Calculate the spill cost of each live range
311 //
Chris Lattner2f898d22002-02-05 06:02:59 +0000312 LiveRange *LR = LRI.getLiveRangeForValue(*OpI);
313 if (LR) LR->addSpillCost(BBLoopDepthCost);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000314 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000315
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000316
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000317 // if there are multiple defs in this instruction e.g. in SETX
318 //
Chris Lattnerdd1e40b2002-02-03 07:46:34 +0000319 if (TM.getInstrInfo().isPseudoInstr(MInst->getOpCode()))
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000320 addInterf4PseudoInstr(MInst);
321
322
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000323 // Also add interference for any implicit definitions in a machine
324 // instr (currently, only calls have this).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000325 //
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000326 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000327 for (unsigned z=0; z < NumOfImpRefs; z++)
328 if (MInst->getImplicitOp(z).opIsDefOnly() ||
329 MInst->getImplicitOp(z).opIsDefAndUse())
330 addInterference( MInst->getImplicitRef(z), &LVSetAI, isCallInst );
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +0000331
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000332 } // for all machine instructions in BB
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000333 } // for all BBs in function
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000334
335
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000336 // add interferences for function arguments. Since there are no explict
337 // defs in the function for args, we have to add them manually
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000338 //
339 addInterferencesForArgs();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000340
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000341 if (DEBUG_RA >= RA_DEBUG_Interference)
342 cerr << "Interference graphs calculated!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000343}
344
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000345
346
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000347//--------------------------------------------------------------------------
348// Pseudo instructions will be exapnded to multiple instructions by the
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000349// assembler. Consequently, all the opernds must get distinct registers.
350// Therefore, we mark all operands of a pseudo instruction as they interfere
351// with one another.
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000352//--------------------------------------------------------------------------
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000353void PhyRegAlloc::addInterf4PseudoInstr(const MachineInstr *MInst) {
354
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000355 bool setInterf = false;
356
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000357 // iterate over MI operands to find defs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000358 //
Chris Lattner2f898d22002-02-05 06:02:59 +0000359 for (MachineInstr::const_val_op_iterator It1 = MInst->begin(),
360 ItE = MInst->end(); It1 != ItE; ++It1) {
361 const LiveRange *LROfOp1 = LRI.getLiveRangeForValue(*It1);
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000362 assert((LROfOp1 || !It1.isUseOnly())&& "No LR for Def in PSEUDO insruction");
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000363
Chris Lattner2f898d22002-02-05 06:02:59 +0000364 MachineInstr::const_val_op_iterator It2 = It1;
Chris Lattner7e708292002-06-25 16:13:24 +0000365 for (++It2; It2 != ItE; ++It2) {
Chris Lattner2f898d22002-02-05 06:02:59 +0000366 const LiveRange *LROfOp2 = LRI.getLiveRangeForValue(*It2);
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000367
Chris Lattner2f898d22002-02-05 06:02:59 +0000368 if (LROfOp2) {
369 RegClass *RCOfOp1 = LROfOp1->getRegClass();
370 RegClass *RCOfOp2 = LROfOp2->getRegClass();
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000371
Chris Lattner7e708292002-06-25 16:13:24 +0000372 if (RCOfOp1 == RCOfOp2 ){
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000373 RCOfOp1->setInterference( LROfOp1, LROfOp2 );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000374 setInterf = true;
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000375 }
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000376 } // if Op2 has a LR
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000377 } // for all other defs in machine instr
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000378 } // for all operands in an instruction
379
Chris Lattner2f898d22002-02-05 06:02:59 +0000380 if (!setInterf && MInst->getNumOperands() > 2) {
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000381 cerr << "\nInterf not set for any operand in pseudo instr:\n";
382 cerr << *MInst;
383 assert(0 && "Interf not set for pseudo instr with > 2 operands" );
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +0000384 }
Ruchira Sasanka22ccb1b2001-11-14 15:33:58 +0000385}
386
387
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000388
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000389//----------------------------------------------------------------------------
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000390// This method will add interferences for incoming arguments to a function.
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000391//----------------------------------------------------------------------------
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000392
Chris Lattner296b7732002-02-05 02:52:05 +0000393void PhyRegAlloc::addInterferencesForArgs() {
394 // get the InSet of root BB
Chris Lattnerf726e772002-10-28 19:22:04 +0000395 const ValueSet &InSet = LVI->getInSetOfBB(&Fn->front());
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000396
Chris Lattnerf726e772002-10-28 19:22:04 +0000397 for (Function::const_aiterator AI = Fn->abegin(); AI != Fn->aend(); ++AI) {
Chris Lattner7e708292002-06-25 16:13:24 +0000398 // add interferences between args and LVars at start
399 addInterference(AI, &InSet, false);
400
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000401 if (DEBUG_RA >= RA_DEBUG_Interference)
Chris Lattner7e708292002-06-25 16:13:24 +0000402 cerr << " - %% adding interference for argument " << RAV(AI) << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000403 }
404}
405
406
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000407//----------------------------------------------------------------------------
408// This method is called after register allocation is complete to set the
409// allocated reisters in the machine code. This code will add register numbers
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000410// to MachineOperands that contain a Value. Also it calls target specific
411// methods to produce caller saving instructions. At the end, it adds all
412// additional instructions produced by the register allocator to the
413// instruction stream.
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +0000414//----------------------------------------------------------------------------
Vikram S. Adve48762092002-04-25 04:34:15 +0000415
416//-----------------------------
417// Utility functions used below
418//-----------------------------
419inline void
Vikram S. Advecb202e32002-10-11 16:12:40 +0000420InsertBefore(MachineInstr* newMI,
Chris Lattnerf726e772002-10-28 19:22:04 +0000421 MachineBasicBlock& MBB,
Chris Lattner32be9f62002-10-28 01:41:27 +0000422 MachineBasicBlock::iterator& MII)
Vikram S. Advecb202e32002-10-11 16:12:40 +0000423{
Chris Lattnerf726e772002-10-28 19:22:04 +0000424 MII = MBB.insert(MII, newMI);
Vikram S. Advecb202e32002-10-11 16:12:40 +0000425 ++MII;
426}
427
428inline void
429InsertAfter(MachineInstr* newMI,
Chris Lattnerf726e772002-10-28 19:22:04 +0000430 MachineBasicBlock& MBB,
Chris Lattner32be9f62002-10-28 01:41:27 +0000431 MachineBasicBlock::iterator& MII)
Vikram S. Advecb202e32002-10-11 16:12:40 +0000432{
433 ++MII; // insert before the next instruction
Chris Lattnerf726e772002-10-28 19:22:04 +0000434 MII = MBB.insert(MII, newMI);
Vikram S. Advecb202e32002-10-11 16:12:40 +0000435}
436
437inline void
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000438DeleteInstruction(MachineBasicBlock& MBB,
439 MachineBasicBlock::iterator& MII)
440{
441 MII = MBB.erase(MII);
442}
443
444inline void
Vikram S. Advecb202e32002-10-11 16:12:40 +0000445SubstituteInPlace(MachineInstr* newMI,
Chris Lattnerf726e772002-10-28 19:22:04 +0000446 MachineBasicBlock& MBB,
Chris Lattner32be9f62002-10-28 01:41:27 +0000447 MachineBasicBlock::iterator MII)
Vikram S. Advecb202e32002-10-11 16:12:40 +0000448{
449 *MII = newMI;
450}
451
452inline void
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000453PrependInstructions(vector<MachineInstr *> &IBef,
Chris Lattnerf726e772002-10-28 19:22:04 +0000454 MachineBasicBlock& MBB,
Chris Lattner32be9f62002-10-28 01:41:27 +0000455 MachineBasicBlock::iterator& MII,
Vikram S. Adve48762092002-04-25 04:34:15 +0000456 const std::string& msg)
457{
458 if (!IBef.empty())
459 {
460 MachineInstr* OrigMI = *MII;
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000461 std::vector<MachineInstr *>::iterator AdIt;
Vikram S. Adve48762092002-04-25 04:34:15 +0000462 for (AdIt = IBef.begin(); AdIt != IBef.end() ; ++AdIt)
463 {
464 if (DEBUG_RA) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000465 if (OrigMI) cerr << "For MInst:\n " << *OrigMI;
466 cerr << msg << "PREPENDed instr:\n " << **AdIt << "\n";
Vikram S. Adve48762092002-04-25 04:34:15 +0000467 }
Chris Lattnerf726e772002-10-28 19:22:04 +0000468 InsertBefore(*AdIt, MBB, MII);
Vikram S. Adve48762092002-04-25 04:34:15 +0000469 }
470 }
471}
472
473inline void
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000474AppendInstructions(std::vector<MachineInstr *> &IAft,
Chris Lattnerf726e772002-10-28 19:22:04 +0000475 MachineBasicBlock& MBB,
Chris Lattner32be9f62002-10-28 01:41:27 +0000476 MachineBasicBlock::iterator& MII,
Vikram S. Adve48762092002-04-25 04:34:15 +0000477 const std::string& msg)
478{
479 if (!IAft.empty())
480 {
481 MachineInstr* OrigMI = *MII;
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000482 std::vector<MachineInstr *>::iterator AdIt;
Chris Lattner7e708292002-06-25 16:13:24 +0000483 for ( AdIt = IAft.begin(); AdIt != IAft.end() ; ++AdIt )
Vikram S. Adve48762092002-04-25 04:34:15 +0000484 {
Chris Lattner7e708292002-06-25 16:13:24 +0000485 if (DEBUG_RA) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000486 if (OrigMI) cerr << "For MInst:\n " << *OrigMI;
487 cerr << msg << "APPENDed instr:\n " << **AdIt << "\n";
Vikram S. Adve48762092002-04-25 04:34:15 +0000488 }
Chris Lattnerf726e772002-10-28 19:22:04 +0000489 InsertAfter(*AdIt, MBB, MII);
Vikram S. Adve48762092002-04-25 04:34:15 +0000490 }
491 }
492}
493
Vikram S. Adve814030a2003-07-29 19:49:21 +0000494static bool MarkAllocatedRegs(MachineInstr* MInst,
495 LiveRangeInfo& LRI,
496 const TargetRegInfo& MRI)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000497{
Vikram S. Adve814030a2003-07-29 19:49:21 +0000498 bool instrNeedsSpills = false;
499
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000500 // First, set the registers for operands in the machine instruction
501 // if a register was successfully allocated. Do this first because we
502 // will need to know which registers are already used by this instr'n.
503 //
504 for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum)
505 {
506 MachineOperand& Op = MInst->getOperand(OpNum);
507 if (Op.getType() == MachineOperand::MO_VirtualRegister ||
508 Op.getType() == MachineOperand::MO_CCRegister)
509 {
510 const Value *const Val = Op.getVRegValue();
Vikram S. Adve814030a2003-07-29 19:49:21 +0000511 if (const LiveRange* LR = LRI.getLiveRangeForValue(Val)) {
512 // Remember if any operand needs spilling
513 instrNeedsSpills |= LR->isMarkedForSpill();
514
515 // An operand may have a color whether or not it needs spilling
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000516 if (LR->hasColor())
517 MInst->SetRegForOperand(OpNum,
518 MRI.getUnifiedRegNum(LR->getRegClass()->getID(),
519 LR->getColor()));
Vikram S. Adve814030a2003-07-29 19:49:21 +0000520 }
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000521 }
522 } // for each operand
Vikram S. Adve814030a2003-07-29 19:49:21 +0000523
524 return instrNeedsSpills;
525}
526
527void PhyRegAlloc::updateInstruction(MachineBasicBlock::iterator& MII,
528 MachineBasicBlock &MBB)
529{
530 MachineInstr* MInst = *MII;
531 unsigned Opcode = MInst->getOpCode();
532
533 // Reset tmp stack positions so they can be reused for each machine instr.
534 MF.getInfo()->popAllTempValues();
535
536 // Mark the operands for which regs have been allocated.
537 bool instrNeedsSpills = MarkAllocatedRegs(*MII, LRI, MRI);
538
539#ifndef NDEBUG
540 // Mark that the operands have been updated. Later,
541 // setRelRegsUsedByThisInst() is called to find registers used by each
542 // MachineInst, and it should not be used for an instruction until
543 // this is done. This flag just serves as a sanity check.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000544 OperandsColoredMap[MInst] = true;
Vikram S. Adve814030a2003-07-29 19:49:21 +0000545#endif
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000546
Vikram S. Advebc001b22003-07-25 21:06:09 +0000547 // Now insert caller-saving code before/after the call.
548 // Do this before inserting spill code since some registers must be
549 // used by save/restore and spill code should not use those registers.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000550 //
Vikram S. Advebc001b22003-07-25 21:06:09 +0000551 if (TM.getInstrInfo().isCall(Opcode)) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000552 AddedInstrns &AI = AddedInstrMap[MInst];
Vikram S. Adve814030a2003-07-29 19:49:21 +0000553 insertCallerSavingCode(AI.InstrnsBefore, AI.InstrnsAfter, MInst,
554 MBB.getBasicBlock());
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000555 }
Vikram S. Advebc001b22003-07-25 21:06:09 +0000556
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000557 // Now insert spill code for remaining operands not allocated to
558 // registers. This must be done even for call return instructions
559 // since those are not handled by the special code above.
Vikram S. Adve814030a2003-07-29 19:49:21 +0000560 if (instrNeedsSpills)
561 for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum)
562 {
563 MachineOperand& Op = MInst->getOperand(OpNum);
564 if (Op.getType() == MachineOperand::MO_VirtualRegister ||
565 Op.getType() == MachineOperand::MO_CCRegister)
566 {
567 const Value* Val = Op.getVRegValue();
568 if (const LiveRange *LR = LRI.getLiveRangeForValue(Val))
569 if (LR->isMarkedForSpill())
570 insertCode4SpilledLR(LR, MII, MBB, OpNum);
571 }
572 } // for each operand
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000573}
574
575void PhyRegAlloc::updateMachineCode()
576{
Chris Lattner7e708292002-06-25 16:13:24 +0000577 // Insert any instructions needed at method entry
Chris Lattnerf726e772002-10-28 19:22:04 +0000578 MachineBasicBlock::iterator MII = MF.front().begin();
579 PrependInstructions(AddedInstrAtEntry.InstrnsBefore, MF.front(), MII,
Chris Lattner7e708292002-06-25 16:13:24 +0000580 "At function entry: \n");
581 assert(AddedInstrAtEntry.InstrnsAfter.empty() &&
582 "InstrsAfter should be unnecessary since we are just inserting at "
583 "the function entry point here.");
Vikram S. Adve48762092002-04-25 04:34:15 +0000584
Chris Lattnerf726e772002-10-28 19:22:04 +0000585 for (MachineFunction::iterator BBI = MF.begin(), BBE = MF.end();
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000586 BBI != BBE; ++BBI) {
Vikram S. Advecb202e32002-10-11 16:12:40 +0000587
Chris Lattnerf726e772002-10-28 19:22:04 +0000588 MachineBasicBlock &MBB = *BBI;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000589
590 // Iterate over all machine instructions in BB and mark operands with
591 // their assigned registers or insert spill code, as appropriate.
592 // Also, fix operands of call/return instructions.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000593 for (MachineBasicBlock::iterator MII = MBB.begin(); MII != MBB.end(); ++MII)
Vikram S. Adve814030a2003-07-29 19:49:21 +0000594 if (! TM.getInstrInfo().isDummyPhiInstr((*MII)->getOpCode()))
595 updateInstruction(MII, MBB);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000596
597 // Now, move code out of delay slots of branches and returns if needed.
598 // (Also, move "after" code from calls to the last delay slot instruction.)
599 // Moving code out of delay slots is needed in 2 situations:
600 // (1) If this is a branch and it needs instructions inserted after it,
601 // move any existing instructions out of the delay slot so that the
602 // instructions can go into the delay slot. This only supports the
603 // case that #instrsAfter <= #delay slots.
604 //
605 // (2) If any instruction in the delay slot needs
606 // instructions inserted, move it out of the delay slot and before the
607 // branch because putting code before or after it would be VERY BAD!
608 //
609 // If the annul bit of the branch is set, neither of these is legal!
610 // If so, we need to handle spill differently but annulling is not yet used.
611 //
612 for (MachineBasicBlock::iterator MII = MBB.begin();
613 MII != MBB.end(); ++MII)
614 if (unsigned delaySlots =
615 TM.getInstrInfo().getNumDelaySlots((*MII)->getOpCode()))
616 {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000617 MachineInstr *MInst = *MII, *DelaySlotMI = *(MII+1);
618
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000619 // Check the 2 conditions above:
620 // (1) Does a branch need instructions added after it?
621 // (2) O/w does delay slot instr. need instrns before or after?
Vikram S. Adve814030a2003-07-29 19:49:21 +0000622 bool isBranch = (TM.getInstrInfo().isBranch(MInst->getOpCode()) ||
623 TM.getInstrInfo().isReturn(MInst->getOpCode()));
624 bool cond1 = (isBranch &&
625 AddedInstrMap.count(MInst) &&
626 AddedInstrMap[MInst].InstrnsAfter.size() > 0);
627 bool cond2 = (AddedInstrMap.count(DelaySlotMI) &&
628 (AddedInstrMap[DelaySlotMI].InstrnsBefore.size() > 0 ||
629 AddedInstrMap[DelaySlotMI].InstrnsAfter.size() > 0));
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000630
631 if (cond1 || cond2)
632 {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000633 assert((MInst->getOpCodeFlags() & AnnulFlag) == 0 &&
634 "FIXME: Moving an annulled delay slot instruction!");
635 assert(delaySlots==1 &&
636 "InsertBefore does not yet handle >1 delay slots!");
637 InsertBefore(DelaySlotMI, MBB, MII); // MII pts back to branch
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000638
639 // In case (1), delete it and don't replace with anything!
640 // Otherwise (i.e., case (2) only) replace it with a NOP.
641 if (cond1) {
Vikram S. Adve814030a2003-07-29 19:49:21 +0000642 DeleteInstruction(MBB, ++MII); // MII now points to next inst.
643 --MII; // reset MII for ++MII of loop
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000644 }
Vikram S. Adve814030a2003-07-29 19:49:21 +0000645 else
646 SubstituteInPlace(BuildMI(TM.getInstrInfo().getNOPOpCode(),1),
647 MBB, MII+1); // replace with NOP
648
649 if (DEBUG_RA) {
650 cerr << "\nRegAlloc: Moved instr. with added code: "
651 << *DelaySlotMI
652 << " out of delay slots of instr: " << *MInst;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000653 }
654 }
Vikram S. Adve814030a2003-07-29 19:49:21 +0000655 else
656 // For non-branch instr with delay slots (probably a call), move
657 // InstrAfter to the instr. in the last delay slot.
658 move2DelayedInstr(*MII, *(MII+delaySlots));
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000659 }
660
661 // Finally iterate over all instructions in BB and insert before/after
662 //
Vikram S. Advebc001b22003-07-25 21:06:09 +0000663 for (MachineBasicBlock::iterator MII=MBB.begin(); MII != MBB.end(); ++MII) {
Vikram S. Adve48762092002-04-25 04:34:15 +0000664 MachineInstr *MInst = *MII;
Vikram S. Advebc001b22003-07-25 21:06:09 +0000665
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000666 // do not process Phis
Vikram S. Advebc001b22003-07-25 21:06:09 +0000667 if (TM.getInstrInfo().isDummyPhiInstr(MInst->getOpCode()))
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000668 continue;
669
Vikram S. Advebc001b22003-07-25 21:06:09 +0000670 // if there are any added instructions...
Chris Lattner7e708292002-06-25 16:13:24 +0000671 if (AddedInstrMap.count(MInst)) {
Vikram S. Advebc001b22003-07-25 21:06:09 +0000672 AddedInstrns &CallAI = AddedInstrMap[MInst];
673
674#ifndef NDEBUG
Vikram S. Adve814030a2003-07-29 19:49:21 +0000675 bool isBranch = (TM.getInstrInfo().isBranch(MInst->getOpCode()) ||
676 TM.getInstrInfo().isReturn(MInst->getOpCode()));
677 assert((!isBranch ||
678 AddedInstrMap[MInst].InstrnsAfter.size() <=
679 TM.getInstrInfo().getNumDelaySlots(MInst->getOpCode())) &&
680 "Cannot put more than #delaySlots instrns after "
681 "branch or return! Need to handle temps differently.");
682#endif
683
684#ifndef NDEBUG
Vikram S. Advebc001b22003-07-25 21:06:09 +0000685 // Temporary sanity checking code to detect whether the same machine
686 // instruction is ever inserted twice before/after a call.
687 // I suspect this is happening but am not sure. --Vikram, 7/1/03.
688 //
689 std::set<const MachineInstr*> instrsSeen;
690 for (int i = 0, N = CallAI.InstrnsBefore.size(); i < N; ++i) {
691 assert(instrsSeen.count(CallAI.InstrnsBefore[i]) == 0 &&
692 "Duplicate machine instruction in InstrnsBefore!");
693 instrsSeen.insert(CallAI.InstrnsBefore[i]);
694 }
695 for (int i = 0, N = CallAI.InstrnsAfter.size(); i < N; ++i) {
696 assert(instrsSeen.count(CallAI.InstrnsAfter[i]) == 0 &&
697 "Duplicate machine instruction in InstrnsBefore/After!");
698 instrsSeen.insert(CallAI.InstrnsAfter[i]);
699 }
700#endif
701
702 // Now add the instructions before/after this MI.
703 // We do this here to ensure that spill for an instruction is inserted
704 // as close as possible to an instruction (see above insertCode4Spill)
705 //
706 if (! CallAI.InstrnsBefore.empty())
707 PrependInstructions(CallAI.InstrnsBefore, MBB, MII,"");
708
709 if (! CallAI.InstrnsAfter.empty())
710 AppendInstructions(CallAI.InstrnsAfter, MBB, MII,"");
711
712 } // if there are any added instructions
Vikram S. Advecb202e32002-10-11 16:12:40 +0000713
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000714 } // for each machine instruction
Vikram S. Adve814030a2003-07-29 19:49:21 +0000715
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000716 }
717}
718
719
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000720
721//----------------------------------------------------------------------------
722// This method inserts spill code for AN operand whose LR was spilled.
723// This method may be called several times for a single machine instruction
724// if it contains many spilled operands. Each time it is called, it finds
725// a register which is not live at that instruction and also which is not
726// used by other spilled operands of the same instruction. Then it uses
727// this register temporarily to accomodate the spilled value.
728//----------------------------------------------------------------------------
Vikram S. Advebc001b22003-07-25 21:06:09 +0000729
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000730void PhyRegAlloc::insertCode4SpilledLR(const LiveRange *LR,
Vikram S. Adve814030a2003-07-29 19:49:21 +0000731 MachineBasicBlock::iterator& MII,
732 MachineBasicBlock &MBB,
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000733 const unsigned OpNum) {
734
Vikram S. Adve814030a2003-07-29 19:49:21 +0000735 MachineInstr *MInst = *MII;
736 const BasicBlock *BB = MBB.getBasicBlock();
737
Vikram S. Advead9c9782002-09-28 17:02:40 +0000738 assert((! TM.getInstrInfo().isCall(MInst->getOpCode()) || OpNum == 0) &&
739 "Outgoing arg of a call must be handled elsewhere (func arg ok)");
740 assert(! TM.getInstrInfo().isReturn(MInst->getOpCode()) &&
741 "Return value of a ret must be handled elsewhere");
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000742
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000743 MachineOperand& Op = MInst->getOperand(OpNum);
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000744 bool isDef = Op.opIsDefOnly();
745 bool isDefAndUse = Op.opIsDefAndUse();
Vikram S. Advebc001b22003-07-25 21:06:09 +0000746 unsigned RegType = MRI.getRegTypeForLR(LR);
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000747 int SpillOff = LR->getSpillOffFromFP();
748 RegClass *RC = LR->getRegClass();
Vikram S. Adve814030a2003-07-29 19:49:21 +0000749
750 // Get the live-variable set to find registers free before this instr.
Vikram S. Advefeb32982003-08-12 22:22:24 +0000751 const ValueSet &LVSetBef = LVI->getLiveVarSetBeforeMInst(MInst, BB);
752
753#ifndef NDEBUG
754 // If this instr. is in the delay slot of a branch or return, we need to
755 // include all live variables before that branch or return -- we don't want to
756 // trample those! Verify that the set is included in the LV set before MInst.
Vikram S. Adve814030a2003-07-29 19:49:21 +0000757 //
Vikram S. Adve814030a2003-07-29 19:49:21 +0000758 if (MII != MBB.begin()) {
759 MachineInstr *PredMI = *(MII-1);
Vikram S. Advefeb32982003-08-12 22:22:24 +0000760 if (unsigned DS = TM.getInstrInfo().getNumDelaySlots(PredMI->getOpCode()))
761 assert(set_difference(LVI->getLiveVarSetBeforeMInst(PredMI), LVSetBef)
762 .empty() && "Live-var set before branch should be included in "
763 "live-var set of each delay slot instruction!");
Vikram S. Adve814030a2003-07-29 19:49:21 +0000764 }
Vikram S. Advefeb32982003-08-12 22:22:24 +0000765#endif
Vikram S. Adve00521d72001-11-12 23:26:35 +0000766
Chris Lattnere90fcb72002-12-28 20:35:34 +0000767 MF.getInfo()->pushTempValue(MRI.getSpilledRegSize(RegType) );
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000768
Vikram S. Advef5af6362002-07-08 23:15:32 +0000769 vector<MachineInstr*> MIBef, MIAft;
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000770 vector<MachineInstr*> AdIMid;
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000771
Vikram S. Adve3bf08922003-07-10 19:42:55 +0000772 // Choose a register to hold the spilled value, if one was not preallocated.
773 // This may insert code before and after MInst to free up the value. If so,
774 // this code should be first/last in the spill sequence before/after MInst.
775 int TmpRegU=(LR->hasColor()
776 ? MRI.getUnifiedRegNum(LR->getRegClass()->getID(),LR->getColor())
777 : getUsableUniRegAtMI(RegType, &LVSetBef, MInst, MIBef,MIAft));
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +0000778
Vikram S. Advef5af6362002-07-08 23:15:32 +0000779 // Set the operand first so that it this register does not get used
780 // as a scratch register for later calls to getUsableUniRegAtMI below
781 MInst->SetRegForOperand(OpNum, TmpRegU);
782
783 // get the added instructions for this instruction
Chris Lattner0b0ffa02002-04-09 05:13:04 +0000784 AddedInstrns &AI = AddedInstrMap[MInst];
Vikram S. Advef5af6362002-07-08 23:15:32 +0000785
786 // We may need a scratch register to copy the spilled value to/from memory.
787 // This may itself have to insert code to free up a scratch register.
788 // Any such code should go before (after) the spill code for a load (store).
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000789 // The scratch reg is not marked as used because it is only used
790 // for the copy and not used across MInst.
Vikram S. Advef5af6362002-07-08 23:15:32 +0000791 int scratchRegType = -1;
792 int scratchReg = -1;
793 if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType))
794 {
Chris Lattner27a08932002-10-22 23:16:21 +0000795 scratchReg = getUsableUniRegAtMI(scratchRegType, &LVSetBef,
796 MInst, MIBef, MIAft);
Vikram S. Advef5af6362002-07-08 23:15:32 +0000797 assert(scratchReg != MRI.getInvalidRegNum());
Vikram S. Advef5af6362002-07-08 23:15:32 +0000798 }
799
800 if (!isDef || isDefAndUse) {
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000801 // for a USE, we have to load the value of LR from stack to a TmpReg
802 // and use the TmpReg as one operand of instruction
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000803
Vikram S. Advef5af6362002-07-08 23:15:32 +0000804 // actual loading instruction(s)
Vikram S. Adve814030a2003-07-29 19:49:21 +0000805 MRI.cpMem2RegMI(AdIMid, MRI.getFramePointer(), SpillOff, TmpRegU,
806 RegType, scratchReg);
Ruchira Sasanka226f1f02001-11-08 19:11:30 +0000807
Vikram S. Advef5af6362002-07-08 23:15:32 +0000808 // the actual load should be after the instructions to free up TmpRegU
809 MIBef.insert(MIBef.end(), AdIMid.begin(), AdIMid.end());
810 AdIMid.clear();
811 }
812
Vikram S. Adve3bf08922003-07-10 19:42:55 +0000813 if (isDef || isDefAndUse) { // if this is a Def
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000814 // for a DEF, we have to store the value produced by this instruction
815 // on the stack position allocated for this LR
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000816
Vikram S. Advef5af6362002-07-08 23:15:32 +0000817 // actual storing instruction(s)
Vikram S. Adve814030a2003-07-29 19:49:21 +0000818 MRI.cpReg2MemMI(AdIMid, TmpRegU, MRI.getFramePointer(), SpillOff,
819 RegType, scratchReg);
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000820
Vikram S. Advef5af6362002-07-08 23:15:32 +0000821 MIAft.insert(MIAft.begin(), AdIMid.begin(), AdIMid.end());
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000822 } // if !DEF
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000823
Vikram S. Advef5af6362002-07-08 23:15:32 +0000824 // Finally, insert the entire spill code sequences before/after MInst
825 AI.InstrnsBefore.insert(AI.InstrnsBefore.end(), MIBef.begin(), MIBef.end());
826 AI.InstrnsAfter.insert(AI.InstrnsAfter.begin(), MIAft.begin(), MIAft.end());
827
Chris Lattner7e708292002-06-25 16:13:24 +0000828 if (DEBUG_RA) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000829 cerr << "\nFor Inst:\n " << *MInst;
830 cerr << "SPILLED LR# " << LR->getUserIGNode()->getIndex();
831 cerr << "; added Instructions:";
Anand Shuklad58290e2002-07-09 19:18:56 +0000832 for_each(MIBef.begin(), MIBef.end(), std::mem_fun(&MachineInstr::dump));
833 for_each(MIAft.begin(), MIAft.end(), std::mem_fun(&MachineInstr::dump));
Chris Lattner7e708292002-06-25 16:13:24 +0000834 }
Ruchira Sasanka5a61d852001-11-08 16:43:25 +0000835}
836
837
Vikram S. Adve814030a2003-07-29 19:49:21 +0000838
839//----------------------------------------------------------------------------
840// This method inserts caller saving/restoring instructons before/after
841// a call machine instruction. The caller saving/restoring instructions are
842// inserted like:
843// ** caller saving instructions
844// other instructions inserted for the call by ColorCallArg
845// CALL instruction
846// other instructions inserted for the call ColorCallArg
847// ** caller restoring instructions
848//----------------------------------------------------------------------------
849
850void
851PhyRegAlloc::insertCallerSavingCode(std::vector<MachineInstr*> &instrnsBefore,
852 std::vector<MachineInstr*> &instrnsAfter,
853 MachineInstr *CallMI,
854 const BasicBlock *BB)
855{
856 assert(TM.getInstrInfo().isCall(CallMI->getOpCode()));
857
858 // has set to record which registers were saved/restored
859 //
860 hash_set<unsigned> PushedRegSet;
861
862 CallArgsDescriptor* argDesc = CallArgsDescriptor::get(CallMI);
863
864 // if the call is to a instrumentation function, do not insert save and
865 // restore instructions the instrumentation function takes care of save
866 // restore for volatile regs.
867 //
868 // FIXME: this should be made general, not specific to the reoptimizer!
869 //
870 const Function *Callee = argDesc->getCallInst()->getCalledFunction();
871 bool isLLVMFirstTrigger = Callee && Callee->getName() == "llvm_first_trigger";
872
873 // Now check if the call has a return value (using argDesc) and if so,
874 // find the LR of the TmpInstruction representing the return value register.
875 // (using the last or second-last *implicit operand* of the call MI).
876 // Insert it to to the PushedRegSet since we must not save that register
877 // and restore it after the call.
878 // We do this because, we look at the LV set *after* the instruction
879 // to determine, which LRs must be saved across calls. The return value
880 // of the call is live in this set - but we must not save/restore it.
881 //
882 if (const Value *origRetVal = argDesc->getReturnValue()) {
883 unsigned retValRefNum = (CallMI->getNumImplicitRefs() -
884 (argDesc->getIndirectFuncPtr()? 1 : 2));
885 const TmpInstruction* tmpRetVal =
886 cast<TmpInstruction>(CallMI->getImplicitRef(retValRefNum));
887 assert(tmpRetVal->getOperand(0) == origRetVal &&
888 tmpRetVal->getType() == origRetVal->getType() &&
889 "Wrong implicit ref?");
890 LiveRange *RetValLR = LRI.getLiveRangeForValue(tmpRetVal);
891 assert(RetValLR && "No LR for RetValue of call");
892
893 if (! RetValLR->isMarkedForSpill())
894 PushedRegSet.insert(MRI.getUnifiedRegNum(RetValLR->getRegClassID(),
895 RetValLR->getColor()));
896 }
897
898 const ValueSet &LVSetAft = LVI->getLiveVarSetAfterMInst(CallMI, BB);
899 ValueSet::const_iterator LIt = LVSetAft.begin();
900
901 // for each live var in live variable set after machine inst
902 for( ; LIt != LVSetAft.end(); ++LIt) {
903
904 // get the live range corresponding to live var
905 LiveRange *const LR = LRI.getLiveRangeForValue(*LIt);
906
907 // LR can be null if it is a const since a const
908 // doesn't have a dominating def - see Assumptions above
909 if( LR ) {
910
911 if(! LR->isMarkedForSpill()) {
912
913 assert(LR->hasColor() && "LR is neither spilled nor colored?");
914 unsigned RCID = LR->getRegClassID();
915 unsigned Color = LR->getColor();
916
917 if (MRI.isRegVolatile(RCID, Color) ) {
918
919 //if the function is special LLVM function,
920 //And the register is not modified by call, don't save and restore
921 if (isLLVMFirstTrigger && !MRI.modifiedByCall(RCID, Color))
922 continue;
923
924 // if the value is in both LV sets (i.e., live before and after
925 // the call machine instruction)
926
927 unsigned Reg = MRI.getUnifiedRegNum(RCID, Color);
928
929 if( PushedRegSet.find(Reg) == PushedRegSet.end() ) {
930
931 // if we haven't already pushed that register
932
933 unsigned RegType = MRI.getRegTypeForLR(LR);
934
935 // Now get two instructions - to push on stack and pop from stack
936 // and add them to InstrnsBefore and InstrnsAfter of the
937 // call instruction
938 //
939 int StackOff =
940 MF.getInfo()->pushTempValue(MRI.getSpilledRegSize(RegType));
941
942 //---- Insert code for pushing the reg on stack ----------
943
944 std::vector<MachineInstr*> AdIBef, AdIAft;
945
946 // We may need a scratch register to copy the saved value
947 // to/from memory. This may itself have to insert code to
948 // free up a scratch register. Any such code should go before
949 // the save code. The scratch register, if any, is by default
950 // temporary and not "used" by the instruction unless the
951 // copy code itself decides to keep the value in the scratch reg.
952 int scratchRegType = -1;
953 int scratchReg = -1;
954 if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType))
955 { // Find a register not live in the LVSet before CallMI
956 const ValueSet &LVSetBef =
957 LVI->getLiveVarSetBeforeMInst(CallMI, BB);
958 scratchReg = getUsableUniRegAtMI(scratchRegType, &LVSetBef,
959 CallMI, AdIBef, AdIAft);
960 assert(scratchReg != MRI.getInvalidRegNum());
961 }
962
963 if (AdIBef.size() > 0)
964 instrnsBefore.insert(instrnsBefore.end(),
965 AdIBef.begin(), AdIBef.end());
966
967 MRI.cpReg2MemMI(instrnsBefore, Reg, MRI.getFramePointer(),
968 StackOff, RegType, scratchReg);
969
970 if (AdIAft.size() > 0)
971 instrnsBefore.insert(instrnsBefore.end(),
972 AdIAft.begin(), AdIAft.end());
973
974 //---- Insert code for popping the reg from the stack ----------
975
976 AdIBef.clear();
977 AdIAft.clear();
978
979 // We may need a scratch register to copy the saved value
980 // from memory. This may itself have to insert code to
981 // free up a scratch register. Any such code should go
982 // after the save code. As above, scratch is not marked "used".
983 //
984 scratchRegType = -1;
985 scratchReg = -1;
986 if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType))
987 { // Find a register not live in the LVSet after CallMI
988 scratchReg = getUsableUniRegAtMI(scratchRegType, &LVSetAft,
989 CallMI, AdIBef, AdIAft);
990 assert(scratchReg != MRI.getInvalidRegNum());
991 }
992
993 if (AdIBef.size() > 0)
994 instrnsAfter.insert(instrnsAfter.end(),
995 AdIBef.begin(), AdIBef.end());
996
997 MRI.cpMem2RegMI(instrnsAfter, MRI.getFramePointer(), StackOff,
998 Reg, RegType, scratchReg);
999
1000 if (AdIAft.size() > 0)
1001 instrnsAfter.insert(instrnsAfter.end(),
1002 AdIAft.begin(), AdIAft.end());
1003
1004 PushedRegSet.insert(Reg);
1005
1006 if(DEBUG_RA) {
1007 std::cerr << "\nFor call inst:" << *CallMI;
1008 std::cerr << " -inserted caller saving instrs: Before:\n\t ";
1009 for_each(instrnsBefore.begin(), instrnsBefore.end(),
1010 std::mem_fun(&MachineInstr::dump));
1011 std::cerr << " -and After:\n\t ";
1012 for_each(instrnsAfter.begin(), instrnsAfter.end(),
1013 std::mem_fun(&MachineInstr::dump));
1014 }
1015 } // if not already pushed
1016
1017 } // if LR has a volatile color
1018
1019 } // if LR has color
1020
1021 } // if there is a LR for Var
1022
1023 } // for each value in the LV set after instruction
1024}
1025
1026
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001027//----------------------------------------------------------------------------
1028// We can use the following method to get a temporary register to be used
1029// BEFORE any given machine instruction. If there is a register available,
1030// this method will simply return that register and set MIBef = MIAft = NULL.
1031// Otherwise, it will return a register and MIAft and MIBef will contain
1032// two instructions used to free up this returned register.
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +00001033// Returned register number is the UNIFIED register number
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001034//----------------------------------------------------------------------------
1035
Vikram S. Advef5af6362002-07-08 23:15:32 +00001036int PhyRegAlloc::getUsableUniRegAtMI(const int RegType,
1037 const ValueSet *LVSetBef,
1038 MachineInstr *MInst,
1039 std::vector<MachineInstr*>& MIBef,
1040 std::vector<MachineInstr*>& MIAft) {
1041
Chris Lattner133f0792002-10-28 04:45:29 +00001042 RegClass* RC = getRegClassByID(MRI.getRegClassIDOfRegType(RegType));
Vikram S. Advef5af6362002-07-08 23:15:32 +00001043
Vikram S. Advebc001b22003-07-25 21:06:09 +00001044 int RegU = getUnusedUniRegAtMI(RC, RegType, MInst, LVSetBef);
Vikram S. Advef5af6362002-07-08 23:15:32 +00001045
1046 if (RegU == -1) {
Ruchira Sasanka80b1a1a2001-11-03 20:41:22 +00001047 // we couldn't find an unused register. Generate code to free up a reg by
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001048 // saving it on stack and restoring after the instruction
Vikram S. Advef5af6362002-07-08 23:15:32 +00001049
Chris Lattnere90fcb72002-12-28 20:35:34 +00001050 int TmpOff = MF.getInfo()->pushTempValue(MRI.getSpilledRegSize(RegType));
Vikram S. Adve12af1642001-11-08 04:48:50 +00001051
Vikram S. Advebc001b22003-07-25 21:06:09 +00001052 RegU = getUniRegNotUsedByThisInst(RC, RegType, MInst);
Vikram S. Advedabb41d2002-05-19 15:29:31 +00001053
Vikram S. Advef5af6362002-07-08 23:15:32 +00001054 // Check if we need a scratch register to copy this register to memory.
1055 int scratchRegType = -1;
1056 if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType))
1057 {
Chris Lattner133f0792002-10-28 04:45:29 +00001058 int scratchReg = getUsableUniRegAtMI(scratchRegType, LVSetBef,
1059 MInst, MIBef, MIAft);
Vikram S. Advef5af6362002-07-08 23:15:32 +00001060 assert(scratchReg != MRI.getInvalidRegNum());
1061
1062 // We may as well hold the value in the scratch register instead
1063 // of copying it to memory and back. But we have to mark the
1064 // register as used by this instruction, so it does not get used
1065 // as a scratch reg. by another operand or anyone else.
Chris Lattner3fd1f5b2003-08-05 22:11:13 +00001066 ScratchRegsUsed.insert(std::make_pair(MInst, scratchReg));
Vikram S. Advef5af6362002-07-08 23:15:32 +00001067 MRI.cpReg2RegMI(MIBef, RegU, scratchReg, RegType);
1068 MRI.cpReg2RegMI(MIAft, scratchReg, RegU, RegType);
1069 }
1070 else
1071 { // the register can be copied directly to/from memory so do it.
1072 MRI.cpReg2MemMI(MIBef, RegU, MRI.getFramePointer(), TmpOff, RegType);
1073 MRI.cpMem2RegMI(MIAft, MRI.getFramePointer(), TmpOff, RegU, RegType);
1074 }
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001075 }
Vikram S. Advef5af6362002-07-08 23:15:32 +00001076
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001077 return RegU;
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001078}
1079
Vikram S. Adve814030a2003-07-29 19:49:21 +00001080
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001081//----------------------------------------------------------------------------
Vikram S. Adve814030a2003-07-29 19:49:21 +00001082// This method is called to get a new unused register that can be used
1083// to accomodate a temporary value. This method may be called several times
1084// for a single machine instruction. Each time it is called, it finds a
1085// register which is not live at that instruction and also which is not used
1086// by other spilled operands of the same instruction. Return register number
1087// is relative to the register class, NOT the unified number.
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001088//----------------------------------------------------------------------------
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001089
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001090int PhyRegAlloc::getUnusedUniRegAtMI(RegClass *RC,
Vikram S. Advebc001b22003-07-25 21:06:09 +00001091 const int RegType,
Vikram S. Adve814030a2003-07-29 19:49:21 +00001092 const MachineInstr *MInst,
1093 const ValueSet* LVSetBef) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001094
Vikram S. Advebc001b22003-07-25 21:06:09 +00001095 RC->clearColorsUsed(); // Reset array
Vikram S. Adve814030a2003-07-29 19:49:21 +00001096
1097 if (LVSetBef == NULL) {
1098 LVSetBef = &LVI->getLiveVarSetBeforeMInst(MInst);
1099 assert(LVSetBef != NULL && "Unable to get live-var set before MInst?");
1100 }
1101
Chris Lattner296b7732002-02-05 02:52:05 +00001102 ValueSet::const_iterator LIt = LVSetBef->begin();
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001103
1104 // for each live var in live variable set after machine inst
Chris Lattner7e708292002-06-25 16:13:24 +00001105 for ( ; LIt != LVSetBef->end(); ++LIt) {
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001106
Vikram S. Advebc001b22003-07-25 21:06:09 +00001107 // get the live range corresponding to live var, and its RegClass
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001108 LiveRange *const LRofLV = LRI.getLiveRangeForValue(*LIt );
1109
1110 // LR can be null if it is a const since a const
1111 // doesn't have a dominating def - see Assumptions above
Vikram S. Advebc001b22003-07-25 21:06:09 +00001112 if (LRofLV && LRofLV->getRegClass() == RC && LRofLV->hasColor())
1113 RC->markColorsUsed(LRofLV->getColor(),
1114 MRI.getRegTypeForLR(LRofLV), RegType);
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001115 }
1116
1117 // It is possible that one operand of this MInst was already spilled
1118 // and it received some register temporarily. If that's the case,
1119 // it is recorded in machine operand. We must skip such registers.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001120 //
Vikram S. Advebc001b22003-07-25 21:06:09 +00001121 setRelRegsUsedByThisInst(RC, RegType, MInst);
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001122
Vikram S. Advebc001b22003-07-25 21:06:09 +00001123 int unusedReg = RC->getUnusedColor(RegType); // find first unused color
1124 if (unusedReg >= 0)
1125 return MRI.getUnifiedRegNum(RC->getID(), unusedReg);
1126
Chris Lattner85c54652002-05-23 15:50:03 +00001127 return -1;
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001128}
1129
1130
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001131//----------------------------------------------------------------------------
1132// Get any other register in a register class, other than what is used
1133// by operands of a machine instruction. Returns the unified reg number.
1134//----------------------------------------------------------------------------
1135int PhyRegAlloc::getUniRegNotUsedByThisInst(RegClass *RC,
Vikram S. Advebc001b22003-07-25 21:06:09 +00001136 const int RegType,
Chris Lattner85c54652002-05-23 15:50:03 +00001137 const MachineInstr *MInst) {
Vikram S. Advebc001b22003-07-25 21:06:09 +00001138 RC->clearColorsUsed();
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001139
Vikram S. Advebc001b22003-07-25 21:06:09 +00001140 setRelRegsUsedByThisInst(RC, RegType, MInst);
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001141
Vikram S. Advebc001b22003-07-25 21:06:09 +00001142 // find the first unused color
1143 int unusedReg = RC->getUnusedColor(RegType);
1144 assert(unusedReg >= 0 &&
1145 "FATAL: No free register could be found in reg class!!");
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001146
Vikram S. Advebc001b22003-07-25 21:06:09 +00001147 return MRI.getUnifiedRegNum(RC->getID(), unusedReg);
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001148}
1149
1150
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001151//----------------------------------------------------------------------------
1152// This method modifies the IsColorUsedArr of the register class passed to it.
1153// It sets the bits corresponding to the registers used by this machine
Ruchira Sasankaf6dfca12001-11-15 15:00:53 +00001154// instructions. Both explicit and implicit operands are set.
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001155//----------------------------------------------------------------------------
Vikram S. Advebc001b22003-07-25 21:06:09 +00001156
Chris Lattner3bed95b2003-08-05 21:55:58 +00001157static void markRegisterUsed(int RegNo, RegClass *RC, int RegType,
1158 const TargetRegInfo &TRI) {
1159 unsigned classId = 0;
1160 int classRegNum = TRI.getClassRegNum(RegNo, classId);
1161 if (RC->getID() == classId)
1162 RC->markColorsUsed(classRegNum, RegType, RegType);
1163}
1164
1165void PhyRegAlloc::setRelRegsUsedByThisInst(RegClass *RC, int RegType,
1166 const MachineInstr *MI)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001167{
Chris Lattner3bed95b2003-08-05 21:55:58 +00001168 assert(OperandsColoredMap[MI] == true &&
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001169 "Illegal to call setRelRegsUsedByThisInst() until colored operands "
1170 "are marked for an instruction.");
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001171
Chris Lattner3bed95b2003-08-05 21:55:58 +00001172 // Add the registers already marked as used by the instruction.
1173 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
1174 if (MI->getOperand(i).hasAllocatedReg())
1175 markRegisterUsed(MI->getOperand(i).getAllocatedRegNum(), RC, RegType,MRI);
1176
1177 for (unsigned i = 0, e = MI->getNumImplicitRefs(); i != e; ++i)
1178 if (MI->getImplicitOp(i).hasAllocatedReg())
1179 markRegisterUsed(MI->getImplicitOp(i).getAllocatedRegNum(), RC,
1180 RegType,MRI);
1181
Chris Lattner3fd1f5b2003-08-05 22:11:13 +00001182 // Add all of the scratch registers that are used to save values across the
1183 // instruction (e.g., for saving state register values).
1184 std::pair<ScratchRegsUsedTy::iterator, ScratchRegsUsedTy::iterator>
1185 IR = ScratchRegsUsed.equal_range(MI);
1186 for (ScratchRegsUsedTy::iterator I = IR.first; I != IR.second; ++I)
1187 markRegisterUsed(I->second, RC, RegType, MRI);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001188
Vikram S. Advef5af6362002-07-08 23:15:32 +00001189 // If there are implicit references, mark their allocated regs as well
1190 //
Chris Lattner3bed95b2003-08-05 21:55:58 +00001191 for (unsigned z=0; z < MI->getNumImplicitRefs(); z++)
Vikram S. Advef5af6362002-07-08 23:15:32 +00001192 if (const LiveRange*
Chris Lattner3bed95b2003-08-05 21:55:58 +00001193 LRofImpRef = LRI.getLiveRangeForValue(MI->getImplicitRef(z)))
Vikram S. Advef5af6362002-07-08 23:15:32 +00001194 if (LRofImpRef->hasColor())
1195 // this implicit reference is in a LR that received a color
Vikram S. Advebc001b22003-07-25 21:06:09 +00001196 RC->markColorsUsed(LRofImpRef->getColor(),
1197 MRI.getRegTypeForLR(LRofImpRef), RegType);
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001198}
1199
1200
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001201//----------------------------------------------------------------------------
Ruchira Sasanka251d8db2001-10-23 21:38:00 +00001202// If there are delay slots for an instruction, the instructions
1203// added after it must really go after the delayed instruction(s).
1204// So, we move the InstrAfter of that instruction to the
1205// corresponding delayed instruction using the following method.
Ruchira Sasanka251d8db2001-10-23 21:38:00 +00001206//----------------------------------------------------------------------------
Ruchira Sasanka251d8db2001-10-23 21:38:00 +00001207
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001208void PhyRegAlloc::move2DelayedInstr(const MachineInstr *OrigMI,
1209 const MachineInstr *DelayedMI)
1210{
Vikram S. Advefeb32982003-08-12 22:22:24 +00001211 // "added after" instructions of the original instr
1212 std::vector<MachineInstr *> &OrigAft = AddedInstrMap[OrigMI].InstrnsAfter;
1213
1214 if (DEBUG_RA && OrigAft.size() > 0) {
Vikram S. Adve814030a2003-07-29 19:49:21 +00001215 cerr << "\nRegAlloc: Moved InstrnsAfter for: " << *OrigMI;
1216 cerr << " to last delay slot instrn: " << *DelayedMI;
1217 }
1218
Ruchira Sasanka251d8db2001-10-23 21:38:00 +00001219 // "added after" instructions of the delayed instr
Vikram S. Adve814030a2003-07-29 19:49:21 +00001220 std::vector<MachineInstr *> &DelayedAft=AddedInstrMap[DelayedMI].InstrnsAfter;
Ruchira Sasanka251d8db2001-10-23 21:38:00 +00001221
1222 // go thru all the "added after instructions" of the original instruction
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001223 // and append them to the "added after instructions" of the delayed
Ruchira Sasanka251d8db2001-10-23 21:38:00 +00001224 // instructions
Chris Lattner697954c2002-01-20 22:54:45 +00001225 DelayedAft.insert(DelayedAft.end(), OrigAft.begin(), OrigAft.end());
Ruchira Sasanka251d8db2001-10-23 21:38:00 +00001226
1227 // empty the "added after instructions" of the original instruction
1228 OrigAft.clear();
Ruchira Sasanka251d8db2001-10-23 21:38:00 +00001229}
Ruchira Sasanka0931a012001-09-15 19:06:58 +00001230
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001231//----------------------------------------------------------------------------
1232// This method prints the code with registers after register allocation is
1233// complete.
1234//----------------------------------------------------------------------------
1235void PhyRegAlloc::printMachineCode()
1236{
1237
Chris Lattnerf726e772002-10-28 19:22:04 +00001238 cerr << "\n;************** Function " << Fn->getName()
Chris Lattner697954c2002-01-20 22:54:45 +00001239 << " *****************\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001240
Chris Lattnerf726e772002-10-28 19:22:04 +00001241 for (MachineFunction::iterator BBI = MF.begin(), BBE = MF.end();
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001242 BBI != BBE; ++BBI) {
Chris Lattnerf726e772002-10-28 19:22:04 +00001243 cerr << "\n"; printLabel(BBI->getBasicBlock()); cerr << ": ";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001244
1245 // get the iterator for machine instructions
Chris Lattnerf726e772002-10-28 19:22:04 +00001246 MachineBasicBlock& MBB = *BBI;
1247 MachineBasicBlock::iterator MII = MBB.begin();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001248
1249 // iterate over all the machine instructions in BB
Chris Lattnerf726e772002-10-28 19:22:04 +00001250 for ( ; MII != MBB.end(); ++MII) {
Chris Lattnerd9512ca2002-10-29 17:35:39 +00001251 MachineInstr *MInst = *MII;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001252
Chris Lattner697954c2002-01-20 22:54:45 +00001253 cerr << "\n\t";
Chris Lattnerd9512ca2002-10-29 17:35:39 +00001254 cerr << TM.getInstrInfo().getName(MInst->getOpCode());
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001255
Chris Lattner7e708292002-06-25 16:13:24 +00001256 for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001257 MachineOperand& Op = MInst->getOperand(OpNum);
1258
Chris Lattner133f0792002-10-28 04:45:29 +00001259 if (Op.getType() == MachineOperand::MO_VirtualRegister ||
1260 Op.getType() == MachineOperand::MO_CCRegister /*||
1261 Op.getType() == MachineOperand::MO_PCRelativeDisp*/ ) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001262
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001263 const Value *const Val = Op.getVRegValue () ;
Ruchira Sasankae727f852001-09-18 22:43:57 +00001264 // ****this code is temporary till NULL Values are fixed
Chris Lattner7e708292002-06-25 16:13:24 +00001265 if (! Val ) {
Chris Lattner697954c2002-01-20 22:54:45 +00001266 cerr << "\t<*NULL*>";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001267 continue;
1268 }
Ruchira Sasankae727f852001-09-18 22:43:57 +00001269
1270 // if a label or a constant
Chris Lattner7e708292002-06-25 16:13:24 +00001271 if (isa<BasicBlock>(Val)) {
Chris Lattner697954c2002-01-20 22:54:45 +00001272 cerr << "\t"; printLabel( Op.getVRegValue () );
1273 } else {
Ruchira Sasankae727f852001-09-18 22:43:57 +00001274 // else it must be a register value
1275 const int RegNum = Op.getAllocatedRegNum();
1276
Chris Lattner697954c2002-01-20 22:54:45 +00001277 cerr << "\t" << "%" << MRI.getUnifiedRegName( RegNum );
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001278 if (Val->hasName() )
Chris Lattner697954c2002-01-20 22:54:45 +00001279 cerr << "(" << Val->getName() << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001280 else
Chris Lattner697954c2002-01-20 22:54:45 +00001281 cerr << "(" << Val << ")";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001282
Vikram S. Adve5f2180c2003-05-27 00:05:23 +00001283 if (Op.opIsDefOnly() || Op.opIsDefAndUse())
Chris Lattner697954c2002-01-20 22:54:45 +00001284 cerr << "*";
Ruchira Sasankaba9d5db2001-11-15 20:23:19 +00001285
1286 const LiveRange *LROfVal = LRI.getLiveRangeForValue(Val);
Chris Lattner7e708292002-06-25 16:13:24 +00001287 if (LROfVal )
1288 if (LROfVal->hasSpillOffset() )
Chris Lattner697954c2002-01-20 22:54:45 +00001289 cerr << "$";
Ruchira Sasankae727f852001-09-18 22:43:57 +00001290 }
1291
1292 }
Chris Lattner133f0792002-10-28 04:45:29 +00001293 else if (Op.getType() == MachineOperand::MO_MachineRegister) {
Chris Lattner697954c2002-01-20 22:54:45 +00001294 cerr << "\t" << "%" << MRI.getUnifiedRegName(Op.getMachineRegNum());
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001295 }
1296
1297 else
Chris Lattner697954c2002-01-20 22:54:45 +00001298 cerr << "\t" << Op; // use dump field
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001299 }
1300
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001301
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001302
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001303 unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
Chris Lattner7e708292002-06-25 16:13:24 +00001304 if (NumOfImpRefs > 0) {
Chris Lattner697954c2002-01-20 22:54:45 +00001305 cerr << "\tImplicit:";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001306
Chris Lattner7e708292002-06-25 16:13:24 +00001307 for (unsigned z=0; z < NumOfImpRefs; z++)
Chris Lattner0665a5f2002-02-05 01:43:49 +00001308 cerr << RAV(MInst->getImplicitRef(z)) << "\t";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001309 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001310
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001311 } // for all machine instructions
1312
Chris Lattner697954c2002-01-20 22:54:45 +00001313 cerr << "\n";
Ruchira Sasankac4d4b762001-10-16 01:23:19 +00001314
1315 } // for all BBs
1316
Chris Lattner697954c2002-01-20 22:54:45 +00001317 cerr << "\n";
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001318}
1319
Ruchira Sasankae727f852001-09-18 22:43:57 +00001320
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001321//----------------------------------------------------------------------------
1322
1323//----------------------------------------------------------------------------
1324void PhyRegAlloc::colorIncomingArgs()
1325{
Vikram S. Adve814030a2003-07-29 19:49:21 +00001326 MRI.colorMethodArgs(Fn, LRI, AddedInstrAtEntry.InstrnsBefore,
1327 AddedInstrAtEntry.InstrnsAfter);
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001328}
1329
Ruchira Sasankae727f852001-09-18 22:43:57 +00001330
1331//----------------------------------------------------------------------------
1332// Used to generate a label for a basic block
1333//----------------------------------------------------------------------------
Chris Lattnerf726e772002-10-28 19:22:04 +00001334void PhyRegAlloc::printLabel(const Value *Val) {
Chris Lattner697954c2002-01-20 22:54:45 +00001335 if (Val->hasName())
1336 cerr << Val->getName();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001337 else
Chris Lattnerf726e772002-10-28 19:22:04 +00001338 cerr << "Label" << Val;
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001339}
1340
1341
Ruchira Sasankae727f852001-09-18 22:43:57 +00001342//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001343// This method calls setSugColorUsable method of each live range. This
1344// will determine whether the suggested color of LR is really usable.
1345// A suggested color is not usable when the suggested color is volatile
1346// AND when there are call interferences
1347//----------------------------------------------------------------------------
1348
1349void PhyRegAlloc::markUnusableSugColors()
1350{
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001351 // hash map iterator
1352 LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin();
1353 LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end();
1354
Chris Lattner7e708292002-06-25 16:13:24 +00001355 for (; HMI != HMIEnd ; ++HMI ) {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001356 if (HMI->first) {
1357 LiveRange *L = HMI->second; // get the LiveRange
1358 if (L) {
Chris Lattner7e708292002-06-25 16:13:24 +00001359 if (L->hasSuggestedColor()) {
Chris Lattnerdd1e40b2002-02-03 07:46:34 +00001360 int RCID = L->getRegClass()->getID();
Chris Lattner7e708292002-06-25 16:13:24 +00001361 if (MRI.isRegVolatile( RCID, L->getSuggestedColor()) &&
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001362 L->isCallInterference() )
1363 L->setSuggestedColorUsable( false );
1364 else
1365 L->setSuggestedColorUsable( true );
1366 }
1367 } // if L->hasSuggestedColor()
1368 }
1369 } // for all LR's in hash map
1370}
1371
1372
1373
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001374//----------------------------------------------------------------------------
1375// The following method will set the stack offsets of the live ranges that
1376// are decided to be spillled. This must be called just after coloring the
1377// LRs using the graph coloring algo. For each live range that is spilled,
1378// this method allocate a new spill position on the stack.
1379//----------------------------------------------------------------------------
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001380
Chris Lattner37730942002-02-05 03:52:29 +00001381void PhyRegAlloc::allocateStackSpace4SpilledLRs() {
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001382 if (DEBUG_RA) cerr << "\nSetting LR stack offsets for spills...\n";
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001383
Chris Lattner37730942002-02-05 03:52:29 +00001384 LiveRangeMapType::const_iterator HMI = LRI.getLiveRangeMap()->begin();
1385 LiveRangeMapType::const_iterator HMIEnd = LRI.getLiveRangeMap()->end();
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001386
Chris Lattner7e708292002-06-25 16:13:24 +00001387 for ( ; HMI != HMIEnd ; ++HMI) {
Chris Lattner37730942002-02-05 03:52:29 +00001388 if (HMI->first && HMI->second) {
Vikram S. Adve3bf08922003-07-10 19:42:55 +00001389 LiveRange *L = HMI->second; // get the LiveRange
1390 if (L->isMarkedForSpill()) { // NOTE: allocating size of long Type **
Chris Lattnere90fcb72002-12-28 20:35:34 +00001391 int stackOffset = MF.getInfo()->allocateSpilledValue(Type::LongTy);
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001392 L->setSpillOffFromFP(stackOffset);
1393 if (DEBUG_RA)
1394 cerr << " LR# " << L->getUserIGNode()->getIndex()
1395 << ": stack-offset = " << stackOffset << "\n";
1396 }
Chris Lattner37730942002-02-05 03:52:29 +00001397 }
1398 } // for all LR's in hash map
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001399}
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001400
1401
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001402//----------------------------------------------------------------------------
Ruchira Sasankae727f852001-09-18 22:43:57 +00001403// The entry pont to Register Allocation
1404//----------------------------------------------------------------------------
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001405
1406void PhyRegAlloc::allocateRegisters()
1407{
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001408
1409 // make sure that we put all register classes into the RegClassList
1410 // before we call constructLiveRanges (now done in the constructor of
1411 // PhyRegAlloc class).
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001412 //
1413 LRI.constructLiveRanges(); // create LR info
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001414
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001415 if (DEBUG_RA >= RA_DEBUG_LiveRanges)
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001416 LRI.printLiveRanges();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001417
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001418 createIGNodeListsAndIGs(); // create IGNode list and IGs
1419
1420 buildInterferenceGraphs(); // build IGs in all reg classes
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001421
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001422
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001423 if (DEBUG_RA >= RA_DEBUG_LiveRanges) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001424 // print all LRs in all reg classes
Chris Lattner7e708292002-06-25 16:13:24 +00001425 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
1426 RegClassList[rc]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001427
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001428 // print IGs in all register classes
Chris Lattner7e708292002-06-25 16:13:24 +00001429 for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
1430 RegClassList[rc]->printIG();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001431 }
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001432
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001433 LRI.coalesceLRs(); // coalesce all live ranges
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +00001434
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001435 if (DEBUG_RA >= RA_DEBUG_LiveRanges) {
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001436 // print all LRs in all reg classes
Chris Lattnerf726e772002-10-28 19:22:04 +00001437 for (unsigned rc=0; rc < NumOfRegClasses; rc++)
1438 RegClassList[rc]->printIGNodeList();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001439
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001440 // print IGs in all register classes
Chris Lattnerf726e772002-10-28 19:22:04 +00001441 for (unsigned rc=0; rc < NumOfRegClasses; rc++)
1442 RegClassList[rc]->printIG();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001443 }
1444
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001445
1446 // mark un-usable suggested color before graph coloring algorithm.
1447 // When this is done, the graph coloring algo will not reserve
1448 // suggested color unnecessarily - they can be used by another LR
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001449 //
Ruchira Sasanka0e62aa62001-10-19 21:39:31 +00001450 markUnusableSugColors();
1451
1452 // color all register classes using the graph coloring algo
Chris Lattner7e708292002-06-25 16:13:24 +00001453 for (unsigned rc=0; rc < NumOfRegClasses ; rc++)
Chris Lattnerf726e772002-10-28 19:22:04 +00001454 RegClassList[rc]->colorAllRegs();
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001455
Chris Lattnere90fcb72002-12-28 20:35:34 +00001456 // Atter graph coloring, if some LRs did not receive a color (i.e, spilled)
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001457 // a poistion for such spilled LRs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001458 //
Ruchira Sasanka174bded2001-10-28 18:12:02 +00001459 allocateStackSpace4SpilledLRs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001460
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001461 // Reset the temp. area on the stack before use by the first instruction.
1462 // This will also happen after updating each instruction.
1463 MF.getInfo()->popAllTempValues();
Ruchira Sasankaf90870f2001-11-15 22:02:06 +00001464
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001465 // color incoming args - if the correct color was not received
1466 // insert code to copy to the correct register
1467 //
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001468 colorIncomingArgs();
Ruchira Sasankaa5ab9642001-09-30 23:11:59 +00001469
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001470 // Now update the machine code with register names and add any
1471 // additional code inserted by the register allocator to the instruction
1472 // stream
1473 //
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001474 updateMachineCode();
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00001475
Chris Lattner045e7c82001-09-19 16:26:23 +00001476 if (DEBUG_RA) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001477 cerr << "\n**** Machine Code After Register Allocation:\n\n";
Chris Lattnerf726e772002-10-28 19:22:04 +00001478 MF.dump();
Chris Lattner045e7c82001-09-19 16:26:23 +00001479 }
Ruchira Sasanka6b0a8b52001-09-15 21:11:11 +00001480}
1481
Ruchira Sasankae727f852001-09-18 22:43:57 +00001482
1483