Chris Lattner | 179cdfb | 2002-08-09 20:08:03 +0000 | [diff] [blame^] | 1 | //===-- PhyRegAlloc.cpp ---------------------------------------------------===// |
Vikram S. Adve | 12af164 | 2001-11-08 04:48:50 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 179cdfb | 2002-08-09 20:08:03 +0000 | [diff] [blame^] | 3 | // Register allocation for LLVM. |
| 4 | // |
| 5 | //===----------------------------------------------------------------------===// |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 6 | |
Chris Lattner | 6dd98a6 | 2002-02-04 00:33:08 +0000 | [diff] [blame] | 7 | #include "llvm/CodeGen/RegisterAllocation.h" |
Vikram S. Adve | 12af164 | 2001-11-08 04:48:50 +0000 | [diff] [blame] | 8 | #include "llvm/CodeGen/PhyRegAlloc.h" |
| 9 | #include "llvm/CodeGen/MachineInstr.h" |
Vikram S. Adve | dabb41d | 2002-05-19 15:29:31 +0000 | [diff] [blame] | 10 | #include "llvm/CodeGen/MachineInstrAnnot.h" |
Vikram S. Adve | f5af636 | 2002-07-08 23:15:32 +0000 | [diff] [blame] | 11 | #include "llvm/CodeGen/MachineCodeForBasicBlock.h" |
Chris Lattner | dd1e40b | 2002-02-03 07:46:34 +0000 | [diff] [blame] | 12 | #include "llvm/CodeGen/MachineCodeForMethod.h" |
Chris Lattner | 483e14e | 2002-04-27 07:27:19 +0000 | [diff] [blame] | 13 | #include "llvm/Analysis/LiveVar/FunctionLiveVarInfo.h" |
Chris Lattner | 14ab1ce | 2002-02-04 17:48:00 +0000 | [diff] [blame] | 14 | #include "llvm/Analysis/LoopInfo.h" |
Vikram S. Adve | 12af164 | 2001-11-08 04:48:50 +0000 | [diff] [blame] | 15 | #include "llvm/Target/TargetMachine.h" |
| 16 | #include "llvm/Target/MachineFrameInfo.h" |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 17 | #include "llvm/Function.h" |
Chris Lattner | 3773094 | 2002-02-05 03:52:29 +0000 | [diff] [blame] | 18 | #include "llvm/Type.h" |
Vikram S. Adve | dabb41d | 2002-05-19 15:29:31 +0000 | [diff] [blame] | 19 | #include "llvm/iOther.h" |
Chris Lattner | c6f3ae5 | 2002-04-29 17:42:12 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/RegAllocCommon.h" |
Chris Lattner | 70e60cb | 2002-05-22 17:08:27 +0000 | [diff] [blame] | 21 | #include "Support/CommandLine.h" |
Vikram S. Adve | f5af636 | 2002-07-08 23:15:32 +0000 | [diff] [blame] | 22 | #include "Support/STLExtras.h" |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 23 | #include <math.h> |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 24 | using std::cerr; |
Anand Shukla | cfb22d3 | 2002-06-25 20:55:50 +0000 | [diff] [blame] | 25 | using std::vector; |
Vikram S. Adve | 12af164 | 2001-11-08 04:48:50 +0000 | [diff] [blame] | 26 | |
Chris Lattner | 70e60cb | 2002-05-22 17:08:27 +0000 | [diff] [blame] | 27 | RegAllocDebugLevel_t DEBUG_RA; |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 28 | static cl::opt<RegAllocDebugLevel_t, true> |
| 29 | DRA_opt("dregalloc", cl::Hidden, cl::location(DEBUG_RA), |
| 30 | cl::desc("enable register allocation debugging information"), |
| 31 | cl::values( |
Chris Lattner | 045e7c8 | 2001-09-19 16:26:23 +0000 | [diff] [blame] | 32 | clEnumValN(RA_DEBUG_None , "n", "disable debug output"), |
| 33 | clEnumValN(RA_DEBUG_Normal , "y", "enable debug output"), |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 34 | clEnumValN(RA_DEBUG_Verbose, "v", "enable extra debug output"), |
| 35 | 0)); |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 36 | |
| 37 | |
Chris Lattner | 2f9b28e | 2002-02-04 15:54:09 +0000 | [diff] [blame] | 38 | //---------------------------------------------------------------------------- |
| 39 | // RegisterAllocation pass front end... |
| 40 | //---------------------------------------------------------------------------- |
| 41 | namespace { |
Chris Lattner | f57b845 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 42 | class RegisterAllocator : public FunctionPass { |
Chris Lattner | 2f9b28e | 2002-02-04 15:54:09 +0000 | [diff] [blame] | 43 | TargetMachine &Target; |
| 44 | public: |
| 45 | inline RegisterAllocator(TargetMachine &T) : Target(T) {} |
Chris Lattner | 96c466b | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 46 | |
| 47 | const char *getPassName() const { return "Register Allocation"; } |
Chris Lattner | 6dd98a6 | 2002-02-04 00:33:08 +0000 | [diff] [blame] | 48 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 49 | bool runOnFunction(Function &F) { |
Chris Lattner | 2f9b28e | 2002-02-04 15:54:09 +0000 | [diff] [blame] | 50 | if (DEBUG_RA) |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 51 | cerr << "\n********* Function "<< F.getName() << " ***********\n"; |
Chris Lattner | 2f9b28e | 2002-02-04 15:54:09 +0000 | [diff] [blame] | 52 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 53 | PhyRegAlloc PRA(&F, Target, &getAnalysis<FunctionLiveVarInfo>(), |
Chris Lattner | 1b7f7dc | 2002-04-28 16:21:30 +0000 | [diff] [blame] | 54 | &getAnalysis<LoopInfo>()); |
Chris Lattner | 2f9b28e | 2002-02-04 15:54:09 +0000 | [diff] [blame] | 55 | PRA.allocateRegisters(); |
| 56 | |
| 57 | if (DEBUG_RA) cerr << "\nRegister allocation complete!\n"; |
| 58 | return false; |
| 59 | } |
Chris Lattner | 4911c35 | 2002-02-04 17:39:42 +0000 | [diff] [blame] | 60 | |
Chris Lattner | f57b845 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 61 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | dd5b495 | 2002-08-08 19:01:28 +0000 | [diff] [blame] | 62 | AU.addRequired<LoopInfo>(); |
| 63 | AU.addRequired<FunctionLiveVarInfo>(); |
Chris Lattner | 4911c35 | 2002-02-04 17:39:42 +0000 | [diff] [blame] | 64 | } |
Chris Lattner | 2f9b28e | 2002-02-04 15:54:09 +0000 | [diff] [blame] | 65 | }; |
Chris Lattner | 6dd98a6 | 2002-02-04 00:33:08 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Chris Lattner | f57b845 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 68 | Pass *getRegisterAllocator(TargetMachine &T) { |
Chris Lattner | 2f9b28e | 2002-02-04 15:54:09 +0000 | [diff] [blame] | 69 | return new RegisterAllocator(T); |
| 70 | } |
Chris Lattner | 6dd98a6 | 2002-02-04 00:33:08 +0000 | [diff] [blame] | 71 | |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 72 | //---------------------------------------------------------------------------- |
| 73 | // Constructor: Init local composite objects and create register classes. |
| 74 | //---------------------------------------------------------------------------- |
Chris Lattner | 1b7f7dc | 2002-04-28 16:21:30 +0000 | [diff] [blame] | 75 | PhyRegAlloc::PhyRegAlloc(Function *F, const TargetMachine& tm, |
| 76 | FunctionLiveVarInfo *Lvi, LoopInfo *LDC) |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 77 | : TM(tm), Meth(F), |
| 78 | mcInfo(MachineCodeForMethod::get(F)), |
| 79 | LVI(Lvi), LRI(F, tm, RegClassList), |
| 80 | MRI(tm.getRegInfo()), |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 81 | NumOfRegClasses(MRI.getNumOfRegClasses()), |
Chris Lattner | 4911c35 | 2002-02-04 17:39:42 +0000 | [diff] [blame] | 82 | LoopDepthCalc(LDC) { |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 83 | |
| 84 | // create each RegisterClass and put in RegClassList |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 85 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 86 | for (unsigned rc=0; rc < NumOfRegClasses; rc++) |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 87 | RegClassList.push_back(new RegClass(F, MRI.getMachineRegClass(rc), |
| 88 | &ResColList)); |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 91 | |
| 92 | //---------------------------------------------------------------------------- |
| 93 | // Destructor: Deletes register classes |
| 94 | //---------------------------------------------------------------------------- |
| 95 | PhyRegAlloc::~PhyRegAlloc() { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 96 | for ( unsigned rc=0; rc < NumOfRegClasses; rc++) |
Chris Lattner | dd1e40b | 2002-02-03 07:46:34 +0000 | [diff] [blame] | 97 | delete RegClassList[rc]; |
Chris Lattner | 0b0ffa0 | 2002-04-09 05:13:04 +0000 | [diff] [blame] | 98 | |
| 99 | AddedInstrMap.clear(); |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 102 | //---------------------------------------------------------------------------- |
| 103 | // This method initally creates interference graphs (one in each reg class) |
| 104 | // and IGNodeList (one in each IG). The actual nodes will be pushed later. |
| 105 | //---------------------------------------------------------------------------- |
Chris Lattner | dd1e40b | 2002-02-03 07:46:34 +0000 | [diff] [blame] | 106 | void PhyRegAlloc::createIGNodeListsAndIGs() { |
| 107 | if (DEBUG_RA) cerr << "Creating LR lists ...\n"; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 108 | |
| 109 | // hash map iterator |
Chris Lattner | dd1e40b | 2002-02-03 07:46:34 +0000 | [diff] [blame] | 110 | LiveRangeMapType::const_iterator HMI = LRI.getLiveRangeMap()->begin(); |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 111 | |
| 112 | // hash map end |
Chris Lattner | dd1e40b | 2002-02-03 07:46:34 +0000 | [diff] [blame] | 113 | LiveRangeMapType::const_iterator HMIEnd = LRI.getLiveRangeMap()->end(); |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 114 | |
Chris Lattner | dd1e40b | 2002-02-03 07:46:34 +0000 | [diff] [blame] | 115 | for (; HMI != HMIEnd ; ++HMI ) { |
| 116 | if (HMI->first) { |
| 117 | LiveRange *L = HMI->second; // get the LiveRange |
| 118 | if (!L) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 119 | if (DEBUG_RA) { |
Chris Lattner | 0665a5f | 2002-02-05 01:43:49 +0000 | [diff] [blame] | 120 | cerr << "\n*?!?Warning: Null liver range found for: " |
| 121 | << RAV(HMI->first) << "\n"; |
Chris Lattner | dd1e40b | 2002-02-03 07:46:34 +0000 | [diff] [blame] | 122 | } |
| 123 | continue; |
| 124 | } |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 125 | // if the Value * is not null, and LR |
| 126 | // is not yet written to the IGNodeList |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 127 | if (!(L->getUserIGNode()) ) { |
Chris Lattner | dd1e40b | 2002-02-03 07:46:34 +0000 | [diff] [blame] | 128 | RegClass *const RC = // RegClass of first value in the LR |
| 129 | RegClassList[ L->getRegClass()->getID() ]; |
| 130 | |
| 131 | RC->addLRToIG(L); // add this LR to an IG |
| 132 | } |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 133 | } |
| 134 | } |
Chris Lattner | dd1e40b | 2002-02-03 07:46:34 +0000 | [diff] [blame] | 135 | |
| 136 | // init RegClassList |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 137 | for ( unsigned rc=0; rc < NumOfRegClasses ; rc++) |
Chris Lattner | dd1e40b | 2002-02-03 07:46:34 +0000 | [diff] [blame] | 138 | RegClassList[rc]->createInterferenceGraph(); |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 139 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 140 | if (DEBUG_RA) |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 141 | cerr << "LRLists Created!\n"; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | |
| 145 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 146 | |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 147 | //---------------------------------------------------------------------------- |
| 148 | // This method will add all interferences at for a given instruction. |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 149 | // Interence occurs only if the LR of Def (Inst or Arg) is of the same reg |
| 150 | // class as that of live var. The live var passed to this function is the |
| 151 | // LVset AFTER the instruction |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 152 | //---------------------------------------------------------------------------- |
Chris Lattner | 296b773 | 2002-02-05 02:52:05 +0000 | [diff] [blame] | 153 | void PhyRegAlloc::addInterference(const Value *Def, |
| 154 | const ValueSet *LVSet, |
| 155 | bool isCallInst) { |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 156 | |
Chris Lattner | 296b773 | 2002-02-05 02:52:05 +0000 | [diff] [blame] | 157 | ValueSet::const_iterator LIt = LVSet->begin(); |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 158 | |
| 159 | // get the live range of instruction |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 160 | // |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 161 | const LiveRange *const LROfDef = LRI.getLiveRangeForValue( Def ); |
| 162 | |
| 163 | IGNode *const IGNodeOfDef = LROfDef->getUserIGNode(); |
| 164 | assert( IGNodeOfDef ); |
| 165 | |
| 166 | RegClass *const RCOfDef = LROfDef->getRegClass(); |
| 167 | |
| 168 | // for each live var in live variable set |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 169 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 170 | for ( ; LIt != LVSet->end(); ++LIt) { |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 171 | |
Vikram S. Adve | f5af636 | 2002-07-08 23:15:32 +0000 | [diff] [blame] | 172 | if (DEBUG_RA >= RA_DEBUG_Verbose) |
Chris Lattner | 0665a5f | 2002-02-05 01:43:49 +0000 | [diff] [blame] | 173 | cerr << "< Def=" << RAV(Def) << ", Lvar=" << RAV(*LIt) << "> "; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 174 | |
| 175 | // get the live range corresponding to live var |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 176 | // |
Chris Lattner | 0665a5f | 2002-02-05 01:43:49 +0000 | [diff] [blame] | 177 | LiveRange *LROfVar = LRI.getLiveRangeForValue(*LIt); |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 178 | |
| 179 | // LROfVar can be null if it is a const since a const |
| 180 | // doesn't have a dominating def - see Assumptions above |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 181 | // |
Chris Lattner | dd1e40b | 2002-02-03 07:46:34 +0000 | [diff] [blame] | 182 | if (LROfVar) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 183 | if (LROfDef == LROfVar) // do not set interf for same LR |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 184 | continue; |
| 185 | |
| 186 | // if 2 reg classes are the same set interference |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 187 | // |
Chris Lattner | 0665a5f | 2002-02-05 01:43:49 +0000 | [diff] [blame] | 188 | if (RCOfDef == LROfVar->getRegClass()) { |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 189 | RCOfDef->setInterference( LROfDef, LROfVar); |
Vikram S. Adve | f5af636 | 2002-07-08 23:15:32 +0000 | [diff] [blame] | 190 | } else if (DEBUG_RA >= RA_DEBUG_Verbose) { |
Chris Lattner | dd1e40b | 2002-02-03 07:46:34 +0000 | [diff] [blame] | 191 | // we will not have LRs for values not explicitly allocated in the |
| 192 | // instruction stream (e.g., constants) |
Chris Lattner | 0665a5f | 2002-02-05 01:43:49 +0000 | [diff] [blame] | 193 | cerr << " warning: no live range for " << RAV(*LIt) << "\n"; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 194 | } |
Ruchira Sasanka | 958faf3 | 2001-10-19 17:21:03 +0000 | [diff] [blame] | 195 | } |
Ruchira Sasanka | 958faf3 | 2001-10-19 17:21:03 +0000 | [diff] [blame] | 196 | } |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Ruchira Sasanka | 958faf3 | 2001-10-19 17:21:03 +0000 | [diff] [blame] | 199 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 200 | |
Ruchira Sasanka | 958faf3 | 2001-10-19 17:21:03 +0000 | [diff] [blame] | 201 | //---------------------------------------------------------------------------- |
| 202 | // For a call instruction, this method sets the CallInterference flag in |
| 203 | // the LR of each variable live int the Live Variable Set live after the |
| 204 | // call instruction (except the return value of the call instruction - since |
| 205 | // the return value does not interfere with that call itself). |
| 206 | //---------------------------------------------------------------------------- |
| 207 | |
| 208 | void PhyRegAlloc::setCallInterferences(const MachineInstr *MInst, |
Chris Lattner | 296b773 | 2002-02-05 02:52:05 +0000 | [diff] [blame] | 209 | const ValueSet *LVSetAft) { |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 210 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 211 | if (DEBUG_RA) |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 212 | cerr << "\n For call inst: " << *MInst; |
Ruchira Sasanka | 958faf3 | 2001-10-19 17:21:03 +0000 | [diff] [blame] | 213 | |
Chris Lattner | 296b773 | 2002-02-05 02:52:05 +0000 | [diff] [blame] | 214 | ValueSet::const_iterator LIt = LVSetAft->begin(); |
Ruchira Sasanka | 958faf3 | 2001-10-19 17:21:03 +0000 | [diff] [blame] | 215 | |
| 216 | // for each live var in live variable set after machine inst |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 217 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 218 | for ( ; LIt != LVSetAft->end(); ++LIt) { |
Ruchira Sasanka | 958faf3 | 2001-10-19 17:21:03 +0000 | [diff] [blame] | 219 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 220 | // get the live range corresponding to live var |
| 221 | // |
Ruchira Sasanka | 958faf3 | 2001-10-19 17:21:03 +0000 | [diff] [blame] | 222 | LiveRange *const LR = LRI.getLiveRangeForValue(*LIt ); |
| 223 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 224 | if (LR && DEBUG_RA) { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 225 | cerr << "\n\tLR Aft Call: "; |
Chris Lattner | 296b773 | 2002-02-05 02:52:05 +0000 | [diff] [blame] | 226 | printSet(*LR); |
Ruchira Sasanka | 958faf3 | 2001-10-19 17:21:03 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Ruchira Sasanka | 958faf3 | 2001-10-19 17:21:03 +0000 | [diff] [blame] | 229 | // LR can be null if it is a const since a const |
| 230 | // doesn't have a dominating def - see Assumptions above |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 231 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 232 | if (LR ) { |
Ruchira Sasanka | 958faf3 | 2001-10-19 17:21:03 +0000 | [diff] [blame] | 233 | LR->setCallInterference(); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 234 | if (DEBUG_RA) { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 235 | cerr << "\n ++Added call interf for LR: " ; |
Chris Lattner | 296b773 | 2002-02-05 02:52:05 +0000 | [diff] [blame] | 236 | printSet(*LR); |
Ruchira Sasanka | 958faf3 | 2001-10-19 17:21:03 +0000 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | |
| 240 | } |
| 241 | |
Vikram S. Adve | 1a53f03 | 2002-03-31 18:54:37 +0000 | [diff] [blame] | 242 | // Now find the LR of the return value of the call |
| 243 | // We do this because, we look at the LV set *after* the instruction |
| 244 | // to determine, which LRs must be saved across calls. The return value |
| 245 | // of the call is live in this set - but it does not interfere with call |
| 246 | // (i.e., we can allocate a volatile register to the return value) |
| 247 | // |
Vikram S. Adve | dabb41d | 2002-05-19 15:29:31 +0000 | [diff] [blame] | 248 | CallArgsDescriptor* argDesc = CallArgsDescriptor::get(MInst); |
| 249 | |
| 250 | if (const Value *RetVal = argDesc->getReturnValue()) { |
Vikram S. Adve | 1a53f03 | 2002-03-31 18:54:37 +0000 | [diff] [blame] | 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. |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 258 | if (const Value *AddrVal = argDesc->getIndirectFuncPtr()) { |
Vikram S. Adve | 1a53f03 | 2002-03-31 18:54:37 +0000 | [diff] [blame] | 259 | LiveRange *AddrValLR = LRI.getLiveRangeForValue( AddrVal ); |
| 260 | assert( AddrValLR && "No LR for indirect addr val of call"); |
| 261 | AddrValLR->setCallInterference(); |
| 262 | } |
| 263 | |
Ruchira Sasanka | 958faf3 | 2001-10-19 17:21:03 +0000 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 267 | |
| 268 | |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 269 | //---------------------------------------------------------------------------- |
| 270 | // This method will walk thru code and create interferences in the IG of |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 271 | // 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 Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 273 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 274 | void PhyRegAlloc::buildInterferenceGraphs() |
| 275 | { |
| 276 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 277 | if (DEBUG_RA) cerr << "Creating interference graphs ...\n"; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 278 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 279 | unsigned BBLoopDepthCost; |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 280 | for (Function::const_iterator BBI = Meth->begin(), BBE = Meth->end(); |
| 281 | BBI != BBE; ++BBI) { |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 282 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 283 | // find the 10^(loop_depth) of this BB |
| 284 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 285 | BBLoopDepthCost = (unsigned)pow(10.0, LoopDepthCalc->getLoopDepth(BBI)); |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 286 | |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 287 | // get the iterator for machine instructions |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 288 | // |
Vikram S. Adve | f5af636 | 2002-07-08 23:15:32 +0000 | [diff] [blame] | 289 | const MachineCodeForBasicBlock& MIVec = MachineCodeForBasicBlock::get(BBI); |
Vikram S. Adve | 4876209 | 2002-04-25 04:34:15 +0000 | [diff] [blame] | 290 | MachineCodeForBasicBlock::const_iterator MII = MIVec.begin(); |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 291 | |
| 292 | // iterate over all the machine instructions in BB |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 293 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 294 | for ( ; MII != MIVec.end(); ++MII) { |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 295 | |
Vikram S. Adve | 4876209 | 2002-04-25 04:34:15 +0000 | [diff] [blame] | 296 | const MachineInstr *MInst = *MII; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 297 | |
| 298 | // get the LV set after the instruction |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 299 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 300 | const ValueSet &LVSetAI = LVI->getLiveVarSetAfterMInst(MInst, BBI); |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 301 | |
| 302 | const bool isCallInst = TM.getInstrInfo().isCall(MInst->getOpCode()); |
| 303 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 304 | if (isCallInst ) { |
Ruchira Sasanka | 958faf3 | 2001-10-19 17:21:03 +0000 | [diff] [blame] | 305 | // 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 Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 309 | // |
Chris Lattner | 748697d | 2002-02-05 04:20:12 +0000 | [diff] [blame] | 310 | setCallInterferences(MInst, &LVSetAI); |
Ruchira Sasanka | 958faf3 | 2001-10-19 17:21:03 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 313 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 314 | // iterate over all MI operands to find defs |
| 315 | // |
Chris Lattner | 2f898d2 | 2002-02-05 06:02:59 +0000 | [diff] [blame] | 316 | 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 Lattner | 748697d | 2002-02-05 04:20:12 +0000 | [diff] [blame] | 319 | addInterference(*OpI, &LVSetAI, isCallInst); |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 320 | |
| 321 | // Calculate the spill cost of each live range |
| 322 | // |
Chris Lattner | 2f898d2 | 2002-02-05 06:02:59 +0000 | [diff] [blame] | 323 | LiveRange *LR = LRI.getLiveRangeForValue(*OpI); |
| 324 | if (LR) LR->addSpillCost(BBLoopDepthCost); |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 325 | } |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 326 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 327 | |
Ruchira Sasanka | 22ccb1b | 2001-11-14 15:33:58 +0000 | [diff] [blame] | 328 | // if there are multiple defs in this instruction e.g. in SETX |
| 329 | // |
Chris Lattner | dd1e40b | 2002-02-03 07:46:34 +0000 | [diff] [blame] | 330 | if (TM.getInstrInfo().isPseudoInstr(MInst->getOpCode())) |
Ruchira Sasanka | 22ccb1b | 2001-11-14 15:33:58 +0000 | [diff] [blame] | 331 | addInterf4PseudoInstr(MInst); |
| 332 | |
| 333 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 334 | // Also add interference for any implicit definitions in a machine |
| 335 | // instr (currently, only calls have this). |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 336 | // |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 337 | unsigned NumOfImpRefs = MInst->getNumImplicitRefs(); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 338 | if ( NumOfImpRefs > 0 ) { |
| 339 | for (unsigned z=0; z < NumOfImpRefs; z++) |
| 340 | if (MInst->implicitRefIsDefined(z) ) |
Chris Lattner | 748697d | 2002-02-05 04:20:12 +0000 | [diff] [blame] | 341 | addInterference( MInst->getImplicitRef(z), &LVSetAI, isCallInst ); |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Ruchira Sasanka | ef1b0cb | 2001-11-03 17:13:27 +0000 | [diff] [blame] | 344 | |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 345 | } // for all machine instructions in BB |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 346 | } // for all BBs in function |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 347 | |
| 348 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 349 | // add interferences for function arguments. Since there are no explict |
| 350 | // defs in the function for args, we have to add them manually |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 351 | // |
| 352 | addInterferencesForArgs(); |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 353 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 354 | if (DEBUG_RA) |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 355 | cerr << "Interference graphs calculted!\n"; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 356 | |
| 357 | } |
| 358 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 359 | |
| 360 | |
Ruchira Sasanka | 22ccb1b | 2001-11-14 15:33:58 +0000 | [diff] [blame] | 361 | //-------------------------------------------------------------------------- |
| 362 | // Pseudo instructions will be exapnded to multiple instructions by the |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 363 | // 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 Sasanka | 22ccb1b | 2001-11-14 15:33:58 +0000 | [diff] [blame] | 366 | //-------------------------------------------------------------------------- |
Ruchira Sasanka | 22ccb1b | 2001-11-14 15:33:58 +0000 | [diff] [blame] | 367 | void PhyRegAlloc::addInterf4PseudoInstr(const MachineInstr *MInst) { |
| 368 | |
Ruchira Sasanka | f6dfca1 | 2001-11-15 15:00:53 +0000 | [diff] [blame] | 369 | bool setInterf = false; |
| 370 | |
Ruchira Sasanka | 22ccb1b | 2001-11-14 15:33:58 +0000 | [diff] [blame] | 371 | // iterate over MI operands to find defs |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 372 | // |
Chris Lattner | 2f898d2 | 2002-02-05 06:02:59 +0000 | [diff] [blame] | 373 | 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 Sasanka | 22ccb1b | 2001-11-14 15:33:58 +0000 | [diff] [blame] | 377 | |
Chris Lattner | 2f898d2 | 2002-02-05 06:02:59 +0000 | [diff] [blame] | 378 | MachineInstr::const_val_op_iterator It2 = It1; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 379 | for (++It2; It2 != ItE; ++It2) { |
Chris Lattner | 2f898d2 | 2002-02-05 06:02:59 +0000 | [diff] [blame] | 380 | const LiveRange *LROfOp2 = LRI.getLiveRangeForValue(*It2); |
Ruchira Sasanka | f6dfca1 | 2001-11-15 15:00:53 +0000 | [diff] [blame] | 381 | |
Chris Lattner | 2f898d2 | 2002-02-05 06:02:59 +0000 | [diff] [blame] | 382 | if (LROfOp2) { |
| 383 | RegClass *RCOfOp1 = LROfOp1->getRegClass(); |
| 384 | RegClass *RCOfOp2 = LROfOp2->getRegClass(); |
Ruchira Sasanka | 22ccb1b | 2001-11-14 15:33:58 +0000 | [diff] [blame] | 385 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 386 | if (RCOfOp1 == RCOfOp2 ){ |
Ruchira Sasanka | 22ccb1b | 2001-11-14 15:33:58 +0000 | [diff] [blame] | 387 | RCOfOp1->setInterference( LROfOp1, LROfOp2 ); |
Ruchira Sasanka | f6dfca1 | 2001-11-15 15:00:53 +0000 | [diff] [blame] | 388 | setInterf = true; |
Ruchira Sasanka | 22ccb1b | 2001-11-14 15:33:58 +0000 | [diff] [blame] | 389 | } |
Ruchira Sasanka | 22ccb1b | 2001-11-14 15:33:58 +0000 | [diff] [blame] | 390 | } // if Op2 has a LR |
Ruchira Sasanka | 22ccb1b | 2001-11-14 15:33:58 +0000 | [diff] [blame] | 391 | } // for all other defs in machine instr |
Ruchira Sasanka | 22ccb1b | 2001-11-14 15:33:58 +0000 | [diff] [blame] | 392 | } // for all operands in an instruction |
| 393 | |
Chris Lattner | 2f898d2 | 2002-02-05 06:02:59 +0000 | [diff] [blame] | 394 | if (!setInterf && MInst->getNumOperands() > 2) { |
Ruchira Sasanka | f6dfca1 | 2001-11-15 15:00:53 +0000 | [diff] [blame] | 395 | 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 Sasanka | f6dfca1 | 2001-11-15 15:00:53 +0000 | [diff] [blame] | 398 | } |
Ruchira Sasanka | 22ccb1b | 2001-11-14 15:33:58 +0000 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 402 | |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 403 | //---------------------------------------------------------------------------- |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 404 | // This method will add interferences for incoming arguments to a function. |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 405 | //---------------------------------------------------------------------------- |
Chris Lattner | 296b773 | 2002-02-05 02:52:05 +0000 | [diff] [blame] | 406 | void PhyRegAlloc::addInterferencesForArgs() { |
| 407 | // get the InSet of root BB |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 408 | const ValueSet &InSet = LVI->getInSetOfBB(&Meth->front()); |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 409 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 410 | for (Function::const_aiterator AI = Meth->abegin(); AI != Meth->aend(); ++AI) { |
| 411 | // add interferences between args and LVars at start |
| 412 | addInterference(AI, &InSet, false); |
| 413 | |
Vikram S. Adve | f5af636 | 2002-07-08 23:15:32 +0000 | [diff] [blame] | 414 | if (DEBUG_RA >= RA_DEBUG_Verbose) |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 415 | cerr << " - %% adding interference for argument " << RAV(AI) << "\n"; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 416 | } |
| 417 | } |
| 418 | |
| 419 | |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 420 | //---------------------------------------------------------------------------- |
| 421 | // This method is called after register allocation is complete to set the |
| 422 | // allocated reisters in the machine code. This code will add register numbers |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 423 | // to MachineOperands that contain a Value. Also it calls target specific |
| 424 | // methods to produce caller saving instructions. At the end, it adds all |
| 425 | // additional instructions produced by the register allocator to the |
| 426 | // instruction stream. |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 427 | //---------------------------------------------------------------------------- |
Vikram S. Adve | 4876209 | 2002-04-25 04:34:15 +0000 | [diff] [blame] | 428 | |
| 429 | //----------------------------- |
| 430 | // Utility functions used below |
| 431 | //----------------------------- |
| 432 | inline void |
Vikram S. Adve | dabb41d | 2002-05-19 15:29:31 +0000 | [diff] [blame] | 433 | PrependInstructions(vector<MachineInstr *> &IBef, |
Vikram S. Adve | 4876209 | 2002-04-25 04:34:15 +0000 | [diff] [blame] | 434 | MachineCodeForBasicBlock& MIVec, |
| 435 | MachineCodeForBasicBlock::iterator& MII, |
| 436 | const std::string& msg) |
| 437 | { |
| 438 | if (!IBef.empty()) |
| 439 | { |
| 440 | MachineInstr* OrigMI = *MII; |
Vikram S. Adve | dabb41d | 2002-05-19 15:29:31 +0000 | [diff] [blame] | 441 | std::vector<MachineInstr *>::iterator AdIt; |
Vikram S. Adve | 4876209 | 2002-04-25 04:34:15 +0000 | [diff] [blame] | 442 | for (AdIt = IBef.begin(); AdIt != IBef.end() ; ++AdIt) |
| 443 | { |
| 444 | if (DEBUG_RA) { |
| 445 | if (OrigMI) cerr << "For MInst: " << *OrigMI; |
| 446 | cerr << msg << " PREPENDed instr: " << **AdIt << "\n"; |
| 447 | } |
| 448 | MII = MIVec.insert(MII, *AdIt); |
| 449 | ++MII; |
| 450 | } |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | inline void |
Vikram S. Adve | dabb41d | 2002-05-19 15:29:31 +0000 | [diff] [blame] | 455 | AppendInstructions(std::vector<MachineInstr *> &IAft, |
Vikram S. Adve | 4876209 | 2002-04-25 04:34:15 +0000 | [diff] [blame] | 456 | MachineCodeForBasicBlock& MIVec, |
| 457 | MachineCodeForBasicBlock::iterator& MII, |
| 458 | const std::string& msg) |
| 459 | { |
| 460 | if (!IAft.empty()) |
| 461 | { |
| 462 | MachineInstr* OrigMI = *MII; |
Vikram S. Adve | dabb41d | 2002-05-19 15:29:31 +0000 | [diff] [blame] | 463 | std::vector<MachineInstr *>::iterator AdIt; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 464 | for ( AdIt = IAft.begin(); AdIt != IAft.end() ; ++AdIt ) |
Vikram S. Adve | 4876209 | 2002-04-25 04:34:15 +0000 | [diff] [blame] | 465 | { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 466 | if (DEBUG_RA) { |
Vikram S. Adve | 4876209 | 2002-04-25 04:34:15 +0000 | [diff] [blame] | 467 | if (OrigMI) cerr << "For MInst: " << *OrigMI; |
| 468 | cerr << msg << " APPENDed instr: " << **AdIt << "\n"; |
| 469 | } |
| 470 | ++MII; // insert before the next instruction |
| 471 | MII = MIVec.insert(MII, *AdIt); |
| 472 | } |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 477 | void PhyRegAlloc::updateMachineCode() |
| 478 | { |
Vikram S. Adve | f5af636 | 2002-07-08 23:15:32 +0000 | [diff] [blame] | 479 | MachineCodeForBasicBlock& MIVec = MachineCodeForBasicBlock::get(&Meth->getEntryNode()); |
Vikram S. Adve | 4876209 | 2002-04-25 04:34:15 +0000 | [diff] [blame] | 480 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 481 | // Insert any instructions needed at method entry |
| 482 | MachineCodeForBasicBlock::iterator MII = MIVec.begin(); |
| 483 | PrependInstructions(AddedInstrAtEntry.InstrnsBefore, MIVec, MII, |
| 484 | "At function entry: \n"); |
| 485 | assert(AddedInstrAtEntry.InstrnsAfter.empty() && |
| 486 | "InstrsAfter should be unnecessary since we are just inserting at " |
| 487 | "the function entry point here."); |
Vikram S. Adve | 4876209 | 2002-04-25 04:34:15 +0000 | [diff] [blame] | 488 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 489 | for (Function::const_iterator BBI = Meth->begin(), BBE = Meth->end(); |
| 490 | BBI != BBE; ++BBI) { |
Vikram S. Adve | 4876209 | 2002-04-25 04:34:15 +0000 | [diff] [blame] | 491 | |
Ruchira Sasanka | 0931a01 | 2001-09-15 19:06:58 +0000 | [diff] [blame] | 492 | // iterate over all the machine instructions in BB |
Vikram S. Adve | f5af636 | 2002-07-08 23:15:32 +0000 | [diff] [blame] | 493 | MachineCodeForBasicBlock &MIVec = MachineCodeForBasicBlock::get(BBI); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 494 | for (MachineCodeForBasicBlock::iterator MII = MIVec.begin(); |
Vikram S. Adve | 4876209 | 2002-04-25 04:34:15 +0000 | [diff] [blame] | 495 | MII != MIVec.end(); ++MII) { |
Ruchira Sasanka | 0931a01 | 2001-09-15 19:06:58 +0000 | [diff] [blame] | 496 | |
Vikram S. Adve | 4876209 | 2002-04-25 04:34:15 +0000 | [diff] [blame] | 497 | MachineInstr *MInst = *MII; |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 498 | |
| 499 | unsigned Opcode = MInst->getOpCode(); |
| 500 | |
Ruchira Sasanka | 65480b7 | 2001-11-10 21:21:36 +0000 | [diff] [blame] | 501 | // do not process Phis |
Vikram S. Adve | 23a4c8f | 2002-03-18 03:37:19 +0000 | [diff] [blame] | 502 | if (TM.getInstrInfo().isDummyPhiInstr(Opcode)) |
Ruchira Sasanka | 65480b7 | 2001-11-10 21:21:36 +0000 | [diff] [blame] | 503 | continue; |
| 504 | |
Vikram S. Adve | f5af636 | 2002-07-08 23:15:32 +0000 | [diff] [blame] | 505 | // Reset tmp stack positions so they can be reused for each machine instr. |
| 506 | mcInfo.popAllTempValues(TM); |
| 507 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 508 | // Now insert speical instructions (if necessary) for call/return |
| 509 | // instructions. |
| 510 | // |
Chris Lattner | dd1e40b | 2002-02-03 07:46:34 +0000 | [diff] [blame] | 511 | if (TM.getInstrInfo().isCall(Opcode) || |
| 512 | TM.getInstrInfo().isReturn(Opcode)) { |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 513 | |
Chris Lattner | 0b0ffa0 | 2002-04-09 05:13:04 +0000 | [diff] [blame] | 514 | AddedInstrns &AI = AddedInstrMap[MInst]; |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 515 | |
Chris Lattner | dd1e40b | 2002-02-03 07:46:34 +0000 | [diff] [blame] | 516 | if (TM.getInstrInfo().isCall(Opcode)) |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 517 | MRI.colorCallArgs(MInst, LRI, &AI, *this, BBI); |
Chris Lattner | dd1e40b | 2002-02-03 07:46:34 +0000 | [diff] [blame] | 518 | else if (TM.getInstrInfo().isReturn(Opcode)) |
Chris Lattner | 0b0ffa0 | 2002-04-09 05:13:04 +0000 | [diff] [blame] | 519 | MRI.colorRetValue(MInst, LRI, &AI); |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 520 | } |
| 521 | |
Vikram S. Adve | f5af636 | 2002-07-08 23:15:32 +0000 | [diff] [blame] | 522 | // Set the registers for operands in the machine instruction |
| 523 | // if a register was successfully allocated. If not, insert |
| 524 | // code to spill the register value. |
| 525 | // |
| 526 | for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) |
| 527 | { |
| 528 | MachineOperand& Op = MInst->getOperand(OpNum); |
| 529 | if (Op.getOperandType() == MachineOperand::MO_VirtualRegister || |
| 530 | Op.getOperandType() == MachineOperand::MO_CCRegister) |
| 531 | { |
| 532 | const Value *const Val = Op.getVRegValue(); |
| 533 | |
| 534 | LiveRange *const LR = LRI.getLiveRangeForValue(Val); |
| 535 | if (!LR) // consts or labels will have no live range |
| 536 | { |
| 537 | // if register is not allocated, mark register as invalid |
| 538 | if (Op.getAllocatedRegNum() == -1) |
| 539 | MInst->SetRegForOperand(OpNum, MRI.getInvalidRegNum()); |
| 540 | continue; |
| 541 | } |
| 542 | |
| 543 | if (LR->hasColor() ) |
| 544 | MInst->SetRegForOperand(OpNum, |
| 545 | MRI.getUnifiedRegNum(LR->getRegClass()->getID(), |
| 546 | LR->getColor())); |
| 547 | else |
| 548 | // LR did NOT receive a color (register). Insert spill code. |
| 549 | insertCode4SpilledLR(LR, MInst, BBI, OpNum ); |
Chris Lattner | 4c3aaa4 | 2001-09-19 16:09:04 +0000 | [diff] [blame] | 550 | } |
Vikram S. Adve | f5af636 | 2002-07-08 23:15:32 +0000 | [diff] [blame] | 551 | } // for each operand |
| 552 | |
| 553 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 554 | // Now add instructions that the register allocator inserts before/after |
| 555 | // this machine instructions (done only for calls/rets/incoming args) |
| 556 | // We do this here, to ensure that spill for an instruction is inserted |
| 557 | // closest as possible to an instruction (see above insertCode4Spill...) |
| 558 | // |
Ruchira Sasanka | f221a2e | 2001-11-13 23:09:30 +0000 | [diff] [blame] | 559 | // If there are instructions to be added, *before* this machine |
| 560 | // instruction, add them now. |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 561 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 562 | if (AddedInstrMap.count(MInst)) { |
Vikram S. Adve | 4876209 | 2002-04-25 04:34:15 +0000 | [diff] [blame] | 563 | PrependInstructions(AddedInstrMap[MInst].InstrnsBefore, MIVec, MII,""); |
Ruchira Sasanka | f221a2e | 2001-11-13 23:09:30 +0000 | [diff] [blame] | 564 | } |
Vikram S. Adve | 4876209 | 2002-04-25 04:34:15 +0000 | [diff] [blame] | 565 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 566 | // If there are instructions to be added *after* this machine |
| 567 | // instruction, add them now |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 568 | // |
Chris Lattner | 0b0ffa0 | 2002-04-09 05:13:04 +0000 | [diff] [blame] | 569 | if (!AddedInstrMap[MInst].InstrnsAfter.empty()) { |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 570 | |
Ruchira Sasanka | 251d8db | 2001-10-23 21:38:00 +0000 | [diff] [blame] | 571 | // if there are delay slots for this instruction, the instructions |
| 572 | // added after it must really go after the delayed instruction(s) |
| 573 | // So, we move the InstrAfter of the current instruction to the |
| 574 | // corresponding delayed instruction |
| 575 | |
| 576 | unsigned delay; |
Chris Lattner | dd1e40b | 2002-02-03 07:46:34 +0000 | [diff] [blame] | 577 | if ((delay=TM.getInstrInfo().getNumDelaySlots(MInst->getOpCode())) >0){ |
Vikram S. Adve | 4876209 | 2002-04-25 04:34:15 +0000 | [diff] [blame] | 578 | move2DelayedInstr(MInst, *(MII+delay) ); |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 579 | } |
Ruchira Sasanka | 251d8db | 2001-10-23 21:38:00 +0000 | [diff] [blame] | 580 | else { |
Ruchira Sasanka | 251d8db | 2001-10-23 21:38:00 +0000 | [diff] [blame] | 581 | // Here we can add the "instructions after" to the current |
| 582 | // instruction since there are no delay slots for this instruction |
Vikram S. Adve | 4876209 | 2002-04-25 04:34:15 +0000 | [diff] [blame] | 583 | AppendInstructions(AddedInstrMap[MInst].InstrnsAfter, MIVec, MII,""); |
Ruchira Sasanka | 251d8db | 2001-10-23 21:38:00 +0000 | [diff] [blame] | 584 | } // if not delay |
Ruchira Sasanka | 0931a01 | 2001-09-15 19:06:58 +0000 | [diff] [blame] | 585 | } |
Ruchira Sasanka | 251d8db | 2001-10-23 21:38:00 +0000 | [diff] [blame] | 586 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 587 | } // for each machine instruction |
Ruchira Sasanka | 0931a01 | 2001-09-15 19:06:58 +0000 | [diff] [blame] | 588 | } |
| 589 | } |
| 590 | |
| 591 | |
Ruchira Sasanka | 5a61d85 | 2001-11-08 16:43:25 +0000 | [diff] [blame] | 592 | |
| 593 | //---------------------------------------------------------------------------- |
| 594 | // This method inserts spill code for AN operand whose LR was spilled. |
| 595 | // This method may be called several times for a single machine instruction |
| 596 | // if it contains many spilled operands. Each time it is called, it finds |
| 597 | // a register which is not live at that instruction and also which is not |
| 598 | // used by other spilled operands of the same instruction. Then it uses |
| 599 | // this register temporarily to accomodate the spilled value. |
| 600 | //---------------------------------------------------------------------------- |
| 601 | void PhyRegAlloc::insertCode4SpilledLR(const LiveRange *LR, |
| 602 | MachineInstr *MInst, |
| 603 | const BasicBlock *BB, |
| 604 | const unsigned OpNum) { |
| 605 | |
Ruchira Sasanka | ba9d5db | 2001-11-15 20:23:19 +0000 | [diff] [blame] | 606 | assert(! TM.getInstrInfo().isCall(MInst->getOpCode()) && |
| 607 | (! TM.getInstrInfo().isReturn(MInst->getOpCode())) && |
| 608 | "Arg of a call/ret must be handled elsewhere"); |
| 609 | |
Ruchira Sasanka | 5a61d85 | 2001-11-08 16:43:25 +0000 | [diff] [blame] | 610 | MachineOperand& Op = MInst->getOperand(OpNum); |
| 611 | bool isDef = MInst->operandIsDefined(OpNum); |
Vikram S. Adve | f5af636 | 2002-07-08 23:15:32 +0000 | [diff] [blame] | 612 | bool isDefAndUse = MInst->operandIsDefinedAndUsed(OpNum); |
Ruchira Sasanka | 5a61d85 | 2001-11-08 16:43:25 +0000 | [diff] [blame] | 613 | unsigned RegType = MRI.getRegType( LR ); |
| 614 | int SpillOff = LR->getSpillOffFromFP(); |
| 615 | RegClass *RC = LR->getRegClass(); |
Chris Lattner | 748697d | 2002-02-05 04:20:12 +0000 | [diff] [blame] | 616 | const ValueSet &LVSetBef = LVI->getLiveVarSetBeforeMInst(MInst, BB); |
Vikram S. Adve | 00521d7 | 2001-11-12 23:26:35 +0000 | [diff] [blame] | 617 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 618 | mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) ); |
Ruchira Sasanka | 5a61d85 | 2001-11-08 16:43:25 +0000 | [diff] [blame] | 619 | |
Vikram S. Adve | f5af636 | 2002-07-08 23:15:32 +0000 | [diff] [blame] | 620 | vector<MachineInstr*> MIBef, MIAft; |
Vikram S. Adve | dabb41d | 2002-05-19 15:29:31 +0000 | [diff] [blame] | 621 | vector<MachineInstr*> AdIMid; |
Ruchira Sasanka | ba9d5db | 2001-11-15 20:23:19 +0000 | [diff] [blame] | 622 | |
Vikram S. Adve | f5af636 | 2002-07-08 23:15:32 +0000 | [diff] [blame] | 623 | // Choose a register to hold the spilled value. This may insert code |
| 624 | // before and after MInst to free up the value. If so, this code should |
| 625 | // be first and last in the spill sequence before/after MInst. |
| 626 | int TmpRegU = getUsableUniRegAtMI(RegType, &LVSetBef, MInst, MIBef, MIAft); |
Ruchira Sasanka | ba9d5db | 2001-11-15 20:23:19 +0000 | [diff] [blame] | 627 | |
Vikram S. Adve | f5af636 | 2002-07-08 23:15:32 +0000 | [diff] [blame] | 628 | // Set the operand first so that it this register does not get used |
| 629 | // as a scratch register for later calls to getUsableUniRegAtMI below |
| 630 | MInst->SetRegForOperand(OpNum, TmpRegU); |
| 631 | |
| 632 | // get the added instructions for this instruction |
Chris Lattner | 0b0ffa0 | 2002-04-09 05:13:04 +0000 | [diff] [blame] | 633 | AddedInstrns &AI = AddedInstrMap[MInst]; |
Vikram S. Adve | f5af636 | 2002-07-08 23:15:32 +0000 | [diff] [blame] | 634 | |
| 635 | // We may need a scratch register to copy the spilled value to/from memory. |
| 636 | // This may itself have to insert code to free up a scratch register. |
| 637 | // Any such code should go before (after) the spill code for a load (store). |
| 638 | int scratchRegType = -1; |
| 639 | int scratchReg = -1; |
| 640 | if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType)) |
| 641 | { |
| 642 | scratchReg = this->getUsableUniRegAtMI(scratchRegType, &LVSetBef, |
| 643 | MInst, MIBef, MIAft); |
| 644 | assert(scratchReg != MRI.getInvalidRegNum()); |
| 645 | MInst->getRegsUsed().insert(scratchReg); |
| 646 | } |
| 647 | |
| 648 | if (!isDef || isDefAndUse) { |
Ruchira Sasanka | 5a61d85 | 2001-11-08 16:43:25 +0000 | [diff] [blame] | 649 | // for a USE, we have to load the value of LR from stack to a TmpReg |
| 650 | // and use the TmpReg as one operand of instruction |
Vikram S. Adve | dabb41d | 2002-05-19 15:29:31 +0000 | [diff] [blame] | 651 | |
Vikram S. Adve | f5af636 | 2002-07-08 23:15:32 +0000 | [diff] [blame] | 652 | // actual loading instruction(s) |
| 653 | MRI.cpMem2RegMI(AdIMid, MRI.getFramePointer(), SpillOff, TmpRegU, RegType, |
| 654 | scratchReg); |
Ruchira Sasanka | 226f1f0 | 2001-11-08 19:11:30 +0000 | [diff] [blame] | 655 | |
Vikram S. Adve | f5af636 | 2002-07-08 23:15:32 +0000 | [diff] [blame] | 656 | // the actual load should be after the instructions to free up TmpRegU |
| 657 | MIBef.insert(MIBef.end(), AdIMid.begin(), AdIMid.end()); |
| 658 | AdIMid.clear(); |
| 659 | } |
| 660 | |
| 661 | if (isDef) { // if this is a Def |
Ruchira Sasanka | 5a61d85 | 2001-11-08 16:43:25 +0000 | [diff] [blame] | 662 | // for a DEF, we have to store the value produced by this instruction |
| 663 | // on the stack position allocated for this LR |
Vikram S. Adve | dabb41d | 2002-05-19 15:29:31 +0000 | [diff] [blame] | 664 | |
Vikram S. Adve | f5af636 | 2002-07-08 23:15:32 +0000 | [diff] [blame] | 665 | // actual storing instruction(s) |
| 666 | MRI.cpReg2MemMI(AdIMid, TmpRegU, MRI.getFramePointer(), SpillOff, RegType, |
| 667 | scratchReg); |
Vikram S. Adve | dabb41d | 2002-05-19 15:29:31 +0000 | [diff] [blame] | 668 | |
Vikram S. Adve | f5af636 | 2002-07-08 23:15:32 +0000 | [diff] [blame] | 669 | MIAft.insert(MIAft.begin(), AdIMid.begin(), AdIMid.end()); |
Ruchira Sasanka | 5a61d85 | 2001-11-08 16:43:25 +0000 | [diff] [blame] | 670 | } // if !DEF |
Vikram S. Adve | dabb41d | 2002-05-19 15:29:31 +0000 | [diff] [blame] | 671 | |
Vikram S. Adve | f5af636 | 2002-07-08 23:15:32 +0000 | [diff] [blame] | 672 | // Finally, insert the entire spill code sequences before/after MInst |
| 673 | AI.InstrnsBefore.insert(AI.InstrnsBefore.end(), MIBef.begin(), MIBef.end()); |
| 674 | AI.InstrnsAfter.insert(AI.InstrnsAfter.begin(), MIAft.begin(), MIAft.end()); |
| 675 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 676 | if (DEBUG_RA) { |
| 677 | cerr << "\nFor Inst " << *MInst; |
| 678 | cerr << " - SPILLED LR: "; printSet(*LR); |
| 679 | cerr << "\n - Added Instructions:"; |
Anand Shukla | d58290e | 2002-07-09 19:18:56 +0000 | [diff] [blame] | 680 | for_each(MIBef.begin(), MIBef.end(), std::mem_fun(&MachineInstr::dump)); |
| 681 | for_each(MIAft.begin(), MIAft.end(), std::mem_fun(&MachineInstr::dump)); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 682 | } |
Ruchira Sasanka | 5a61d85 | 2001-11-08 16:43:25 +0000 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 686 | //---------------------------------------------------------------------------- |
| 687 | // We can use the following method to get a temporary register to be used |
| 688 | // BEFORE any given machine instruction. If there is a register available, |
| 689 | // this method will simply return that register and set MIBef = MIAft = NULL. |
| 690 | // Otherwise, it will return a register and MIAft and MIBef will contain |
| 691 | // two instructions used to free up this returned register. |
Ruchira Sasanka | 80b1a1a | 2001-11-03 20:41:22 +0000 | [diff] [blame] | 692 | // Returned register number is the UNIFIED register number |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 693 | //---------------------------------------------------------------------------- |
| 694 | |
Vikram S. Adve | f5af636 | 2002-07-08 23:15:32 +0000 | [diff] [blame] | 695 | int PhyRegAlloc::getUsableUniRegAtMI(const int RegType, |
| 696 | const ValueSet *LVSetBef, |
| 697 | MachineInstr *MInst, |
| 698 | std::vector<MachineInstr*>& MIBef, |
| 699 | std::vector<MachineInstr*>& MIAft) { |
| 700 | |
| 701 | RegClass* RC = this->getRegClassByID(MRI.getRegClassIDOfRegType(RegType)); |
| 702 | |
Ruchira Sasanka | ba9d5db | 2001-11-15 20:23:19 +0000 | [diff] [blame] | 703 | int RegU = getUnusedUniRegAtMI(RC, MInst, LVSetBef); |
Vikram S. Adve | f5af636 | 2002-07-08 23:15:32 +0000 | [diff] [blame] | 704 | |
| 705 | if (RegU == -1) { |
Ruchira Sasanka | 80b1a1a | 2001-11-03 20:41:22 +0000 | [diff] [blame] | 706 | // we couldn't find an unused register. Generate code to free up a reg by |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 707 | // saving it on stack and restoring after the instruction |
Vikram S. Adve | f5af636 | 2002-07-08 23:15:32 +0000 | [diff] [blame] | 708 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 709 | int TmpOff = mcInfo.pushTempValue(TM, MRI.getSpilledRegSize(RegType) ); |
Vikram S. Adve | 12af164 | 2001-11-08 04:48:50 +0000 | [diff] [blame] | 710 | |
Ruchira Sasanka | ba9d5db | 2001-11-15 20:23:19 +0000 | [diff] [blame] | 711 | RegU = getUniRegNotUsedByThisInst(RC, MInst); |
Vikram S. Adve | dabb41d | 2002-05-19 15:29:31 +0000 | [diff] [blame] | 712 | |
Vikram S. Adve | f5af636 | 2002-07-08 23:15:32 +0000 | [diff] [blame] | 713 | // Check if we need a scratch register to copy this register to memory. |
| 714 | int scratchRegType = -1; |
| 715 | if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType)) |
| 716 | { |
| 717 | int scratchReg = this->getUsableUniRegAtMI(scratchRegType, LVSetBef, |
| 718 | MInst, MIBef, MIAft); |
| 719 | assert(scratchReg != MRI.getInvalidRegNum()); |
| 720 | |
| 721 | // We may as well hold the value in the scratch register instead |
| 722 | // of copying it to memory and back. But we have to mark the |
| 723 | // register as used by this instruction, so it does not get used |
| 724 | // as a scratch reg. by another operand or anyone else. |
| 725 | MInst->getRegsUsed().insert(scratchReg); |
| 726 | MRI.cpReg2RegMI(MIBef, RegU, scratchReg, RegType); |
| 727 | MRI.cpReg2RegMI(MIAft, scratchReg, RegU, RegType); |
| 728 | } |
| 729 | else |
| 730 | { // the register can be copied directly to/from memory so do it. |
| 731 | MRI.cpReg2MemMI(MIBef, RegU, MRI.getFramePointer(), TmpOff, RegType); |
| 732 | MRI.cpMem2RegMI(MIAft, MRI.getFramePointer(), TmpOff, RegU, RegType); |
| 733 | } |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 734 | } |
Vikram S. Adve | f5af636 | 2002-07-08 23:15:32 +0000 | [diff] [blame] | 735 | |
Ruchira Sasanka | ba9d5db | 2001-11-15 20:23:19 +0000 | [diff] [blame] | 736 | return RegU; |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | //---------------------------------------------------------------------------- |
| 740 | // This method is called to get a new unused register that can be used to |
| 741 | // accomodate a spilled value. |
| 742 | // This method may be called several times for a single machine instruction |
| 743 | // if it contains many spilled operands. Each time it is called, it finds |
| 744 | // a register which is not live at that instruction and also which is not |
| 745 | // used by other spilled operands of the same instruction. |
Ruchira Sasanka | 80b1a1a | 2001-11-03 20:41:22 +0000 | [diff] [blame] | 746 | // Return register number is relative to the register class. NOT |
| 747 | // unified number |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 748 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | ba9d5db | 2001-11-15 20:23:19 +0000 | [diff] [blame] | 749 | int PhyRegAlloc::getUnusedUniRegAtMI(RegClass *RC, |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 750 | const MachineInstr *MInst, |
Chris Lattner | 296b773 | 2002-02-05 02:52:05 +0000 | [diff] [blame] | 751 | const ValueSet *LVSetBef) { |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 752 | |
| 753 | unsigned NumAvailRegs = RC->getNumOfAvailRegs(); |
| 754 | |
Chris Lattner | 85c5465 | 2002-05-23 15:50:03 +0000 | [diff] [blame] | 755 | std::vector<bool> &IsColorUsedArr = RC->getIsColorUsedArr(); |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 756 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 757 | for (unsigned i=0; i < NumAvailRegs; i++) // Reset array |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 758 | IsColorUsedArr[i] = false; |
| 759 | |
Chris Lattner | 296b773 | 2002-02-05 02:52:05 +0000 | [diff] [blame] | 760 | ValueSet::const_iterator LIt = LVSetBef->begin(); |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 761 | |
| 762 | // for each live var in live variable set after machine inst |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 763 | for ( ; LIt != LVSetBef->end(); ++LIt) { |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 764 | |
| 765 | // get the live range corresponding to live var |
| 766 | LiveRange *const LRofLV = LRI.getLiveRangeForValue(*LIt ); |
| 767 | |
| 768 | // LR can be null if it is a const since a const |
| 769 | // doesn't have a dominating def - see Assumptions above |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 770 | if (LRofLV && LRofLV->getRegClass() == RC && LRofLV->hasColor() ) |
Vikram S. Adve | dabb41d | 2002-05-19 15:29:31 +0000 | [diff] [blame] | 771 | IsColorUsedArr[ LRofLV->getColor() ] = true; |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | // It is possible that one operand of this MInst was already spilled |
| 775 | // and it received some register temporarily. If that's the case, |
| 776 | // it is recorded in machine operand. We must skip such registers. |
| 777 | |
Ruchira Sasanka | ba9d5db | 2001-11-15 20:23:19 +0000 | [diff] [blame] | 778 | setRelRegsUsedByThisInst(RC, MInst); |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 779 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 780 | for (unsigned c=0; c < NumAvailRegs; c++) // find first unused color |
Chris Lattner | 85c5465 | 2002-05-23 15:50:03 +0000 | [diff] [blame] | 781 | if (!IsColorUsedArr[c]) |
| 782 | return MRI.getUnifiedRegNum(RC->getID(), c); |
Vikram S. Adve | f5af636 | 2002-07-08 23:15:32 +0000 | [diff] [blame] | 783 | |
Chris Lattner | 85c5465 | 2002-05-23 15:50:03 +0000 | [diff] [blame] | 784 | return -1; |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | |
Ruchira Sasanka | ba9d5db | 2001-11-15 20:23:19 +0000 | [diff] [blame] | 788 | //---------------------------------------------------------------------------- |
| 789 | // Get any other register in a register class, other than what is used |
| 790 | // by operands of a machine instruction. Returns the unified reg number. |
| 791 | //---------------------------------------------------------------------------- |
| 792 | int PhyRegAlloc::getUniRegNotUsedByThisInst(RegClass *RC, |
Chris Lattner | 85c5465 | 2002-05-23 15:50:03 +0000 | [diff] [blame] | 793 | const MachineInstr *MInst) { |
Ruchira Sasanka | ba9d5db | 2001-11-15 20:23:19 +0000 | [diff] [blame] | 794 | |
Chris Lattner | 85c5465 | 2002-05-23 15:50:03 +0000 | [diff] [blame] | 795 | vector<bool> &IsColorUsedArr = RC->getIsColorUsedArr(); |
Ruchira Sasanka | ba9d5db | 2001-11-15 20:23:19 +0000 | [diff] [blame] | 796 | unsigned NumAvailRegs = RC->getNumOfAvailRegs(); |
| 797 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 798 | for (unsigned i=0; i < NumAvailRegs ; i++) // Reset array |
Ruchira Sasanka | ba9d5db | 2001-11-15 20:23:19 +0000 | [diff] [blame] | 799 | IsColorUsedArr[i] = false; |
| 800 | |
| 801 | setRelRegsUsedByThisInst(RC, MInst); |
| 802 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 803 | for (unsigned c=0; c < RC->getNumOfAvailRegs(); c++)// find first unused color |
Chris Lattner | 85c5465 | 2002-05-23 15:50:03 +0000 | [diff] [blame] | 804 | if (!IsColorUsedArr[c]) |
| 805 | return MRI.getUnifiedRegNum(RC->getID(), c); |
| 806 | |
| 807 | assert(0 && "FATAL: No free register could be found in reg class!!"); |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 808 | return 0; |
Ruchira Sasanka | ba9d5db | 2001-11-15 20:23:19 +0000 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 812 | //---------------------------------------------------------------------------- |
| 813 | // This method modifies the IsColorUsedArr of the register class passed to it. |
| 814 | // It sets the bits corresponding to the registers used by this machine |
Ruchira Sasanka | f6dfca1 | 2001-11-15 15:00:53 +0000 | [diff] [blame] | 815 | // instructions. Both explicit and implicit operands are set. |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 816 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | ba9d5db | 2001-11-15 20:23:19 +0000 | [diff] [blame] | 817 | void PhyRegAlloc::setRelRegsUsedByThisInst(RegClass *RC, |
Vikram S. Adve | f5af636 | 2002-07-08 23:15:32 +0000 | [diff] [blame] | 818 | const MachineInstr *MInst ) { |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 819 | |
Vikram S. Adve | f5af636 | 2002-07-08 23:15:32 +0000 | [diff] [blame] | 820 | vector<bool> &IsColorUsedArr = RC->getIsColorUsedArr(); |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 821 | |
Vikram S. Adve | f5af636 | 2002-07-08 23:15:32 +0000 | [diff] [blame] | 822 | // Add the registers already marked as used by the instruction. |
| 823 | // This should include any scratch registers that are used to save |
| 824 | // values across the instruction (e.g., for saving state register values). |
| 825 | const hash_set<int>& regsUsed = MInst->getRegsUsed(); |
| 826 | for (hash_set<int>::const_iterator SI=regsUsed.begin(), SE=regsUsed.end(); |
| 827 | SI != SE; ++SI) |
| 828 | { |
| 829 | unsigned classId = 0; |
| 830 | int classRegNum = MRI.getClassRegNum(*SI, classId); |
| 831 | if (RC->getID() == classId) |
| 832 | { |
| 833 | assert(classRegNum < (int) IsColorUsedArr.size() && |
| 834 | "Illegal register number for this reg class?"); |
| 835 | IsColorUsedArr[classRegNum] = true; |
| 836 | } |
Ruchira Sasanka | f6dfca1 | 2001-11-15 15:00:53 +0000 | [diff] [blame] | 837 | } |
Vikram S. Adve | f5af636 | 2002-07-08 23:15:32 +0000 | [diff] [blame] | 838 | |
| 839 | // Now add registers allocated to the live ranges of values used in |
| 840 | // the instruction. These are not yet recorded in the instruction. |
| 841 | for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) |
| 842 | { |
| 843 | const MachineOperand& Op = MInst->getOperand(OpNum); |
| 844 | |
| 845 | if (Op.getOperandType() == MachineOperand::MO_VirtualRegister || |
| 846 | Op.getOperandType() == MachineOperand::MO_CCRegister) |
| 847 | if (const Value* Val = Op.getVRegValue()) |
| 848 | if (MRI.getRegClassIDOfValue(Val) == RC->getID()) |
| 849 | if (Op.getAllocatedRegNum() == -1) |
| 850 | if (LiveRange *LROfVal = LRI.getLiveRangeForValue(Val)) |
| 851 | if (LROfVal->hasColor() ) |
| 852 | // this operand is in a LR that received a color |
| 853 | IsColorUsedArr[LROfVal->getColor()] = true; |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 854 | } |
Vikram S. Adve | f5af636 | 2002-07-08 23:15:32 +0000 | [diff] [blame] | 855 | |
| 856 | // If there are implicit references, mark their allocated regs as well |
| 857 | // |
| 858 | for (unsigned z=0; z < MInst->getNumImplicitRefs(); z++) |
| 859 | if (const LiveRange* |
| 860 | LRofImpRef = LRI.getLiveRangeForValue(MInst->getImplicitRef(z))) |
| 861 | if (LRofImpRef->hasColor()) |
| 862 | // this implicit reference is in a LR that received a color |
| 863 | IsColorUsedArr[LRofImpRef->getColor()] = true; |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 867 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | 251d8db | 2001-10-23 21:38:00 +0000 | [diff] [blame] | 868 | // If there are delay slots for an instruction, the instructions |
| 869 | // added after it must really go after the delayed instruction(s). |
| 870 | // So, we move the InstrAfter of that instruction to the |
| 871 | // corresponding delayed instruction using the following method. |
Ruchira Sasanka | 0931a01 | 2001-09-15 19:06:58 +0000 | [diff] [blame] | 872 | |
Ruchira Sasanka | 251d8db | 2001-10-23 21:38:00 +0000 | [diff] [blame] | 873 | //---------------------------------------------------------------------------- |
Chris Lattner | 0b0ffa0 | 2002-04-09 05:13:04 +0000 | [diff] [blame] | 874 | void PhyRegAlloc::move2DelayedInstr(const MachineInstr *OrigMI, |
| 875 | const MachineInstr *DelayedMI) { |
Ruchira Sasanka | 251d8db | 2001-10-23 21:38:00 +0000 | [diff] [blame] | 876 | |
Ruchira Sasanka | 251d8db | 2001-10-23 21:38:00 +0000 | [diff] [blame] | 877 | // "added after" instructions of the original instr |
Vikram S. Adve | dabb41d | 2002-05-19 15:29:31 +0000 | [diff] [blame] | 878 | std::vector<MachineInstr *> &OrigAft = AddedInstrMap[OrigMI].InstrnsAfter; |
Ruchira Sasanka | 251d8db | 2001-10-23 21:38:00 +0000 | [diff] [blame] | 879 | |
| 880 | // "added instructions" of the delayed instr |
Chris Lattner | 0b0ffa0 | 2002-04-09 05:13:04 +0000 | [diff] [blame] | 881 | AddedInstrns &DelayAdI = AddedInstrMap[DelayedMI]; |
Ruchira Sasanka | 251d8db | 2001-10-23 21:38:00 +0000 | [diff] [blame] | 882 | |
| 883 | // "added after" instructions of the delayed instr |
Vikram S. Adve | dabb41d | 2002-05-19 15:29:31 +0000 | [diff] [blame] | 884 | std::vector<MachineInstr *> &DelayedAft = DelayAdI.InstrnsAfter; |
Ruchira Sasanka | 251d8db | 2001-10-23 21:38:00 +0000 | [diff] [blame] | 885 | |
| 886 | // go thru all the "added after instructions" of the original instruction |
| 887 | // and append them to the "addded after instructions" of the delayed |
| 888 | // instructions |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 889 | DelayedAft.insert(DelayedAft.end(), OrigAft.begin(), OrigAft.end()); |
Ruchira Sasanka | 251d8db | 2001-10-23 21:38:00 +0000 | [diff] [blame] | 890 | |
| 891 | // empty the "added after instructions" of the original instruction |
| 892 | OrigAft.clear(); |
Ruchira Sasanka | 251d8db | 2001-10-23 21:38:00 +0000 | [diff] [blame] | 893 | } |
Ruchira Sasanka | 0931a01 | 2001-09-15 19:06:58 +0000 | [diff] [blame] | 894 | |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 895 | //---------------------------------------------------------------------------- |
| 896 | // This method prints the code with registers after register allocation is |
| 897 | // complete. |
| 898 | //---------------------------------------------------------------------------- |
| 899 | void PhyRegAlloc::printMachineCode() |
| 900 | { |
| 901 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 902 | cerr << "\n;************** Function " << Meth->getName() |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 903 | << " *****************\n"; |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 904 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 905 | for (Function::const_iterator BBI = Meth->begin(), BBE = Meth->end(); |
| 906 | BBI != BBE; ++BBI) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 907 | cerr << "\n"; printLabel(BBI); cerr << ": "; |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 908 | |
| 909 | // get the iterator for machine instructions |
Vikram S. Adve | f5af636 | 2002-07-08 23:15:32 +0000 | [diff] [blame] | 910 | MachineCodeForBasicBlock& MIVec = MachineCodeForBasicBlock::get(BBI); |
Vikram S. Adve | 4876209 | 2002-04-25 04:34:15 +0000 | [diff] [blame] | 911 | MachineCodeForBasicBlock::iterator MII = MIVec.begin(); |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 912 | |
| 913 | // iterate over all the machine instructions in BB |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 914 | for ( ; MII != MIVec.end(); ++MII) { |
Vikram S. Adve | 4876209 | 2002-04-25 04:34:15 +0000 | [diff] [blame] | 915 | MachineInstr *const MInst = *MII; |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 916 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 917 | cerr << "\n\t"; |
| 918 | cerr << TargetInstrDescriptors[MInst->getOpCode()].opCodeString; |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 919 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 920 | for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) { |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 921 | MachineOperand& Op = MInst->getOperand(OpNum); |
| 922 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 923 | if (Op.getOperandType() == MachineOperand::MO_VirtualRegister || |
Ruchira Sasanka | 97b8b44 | 2001-10-18 22:36:26 +0000 | [diff] [blame] | 924 | Op.getOperandType() == MachineOperand::MO_CCRegister /*|| |
| 925 | Op.getOperandType() == MachineOperand::MO_PCRelativeDisp*/ ) { |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 926 | |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 927 | const Value *const Val = Op.getVRegValue () ; |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 928 | // ****this code is temporary till NULL Values are fixed |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 929 | if (! Val ) { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 930 | cerr << "\t<*NULL*>"; |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 931 | continue; |
| 932 | } |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 933 | |
| 934 | // if a label or a constant |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 935 | if (isa<BasicBlock>(Val)) { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 936 | cerr << "\t"; printLabel( Op.getVRegValue () ); |
| 937 | } else { |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 938 | // else it must be a register value |
| 939 | const int RegNum = Op.getAllocatedRegNum(); |
| 940 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 941 | cerr << "\t" << "%" << MRI.getUnifiedRegName( RegNum ); |
Ruchira Sasanka | ba9d5db | 2001-11-15 20:23:19 +0000 | [diff] [blame] | 942 | if (Val->hasName() ) |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 943 | cerr << "(" << Val->getName() << ")"; |
Ruchira Sasanka | ba9d5db | 2001-11-15 20:23:19 +0000 | [diff] [blame] | 944 | else |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 945 | cerr << "(" << Val << ")"; |
Ruchira Sasanka | ba9d5db | 2001-11-15 20:23:19 +0000 | [diff] [blame] | 946 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 947 | if (Op.opIsDef() ) |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 948 | cerr << "*"; |
Ruchira Sasanka | ba9d5db | 2001-11-15 20:23:19 +0000 | [diff] [blame] | 949 | |
| 950 | const LiveRange *LROfVal = LRI.getLiveRangeForValue(Val); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 951 | if (LROfVal ) |
| 952 | if (LROfVal->hasSpillOffset() ) |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 953 | cerr << "$"; |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | } |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 957 | else if (Op.getOperandType() == MachineOperand::MO_MachineRegister) { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 958 | cerr << "\t" << "%" << MRI.getUnifiedRegName(Op.getMachineRegNum()); |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 959 | } |
| 960 | |
| 961 | else |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 962 | cerr << "\t" << Op; // use dump field |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 963 | } |
| 964 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 965 | |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 966 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 967 | unsigned NumOfImpRefs = MInst->getNumImplicitRefs(); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 968 | if (NumOfImpRefs > 0) { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 969 | cerr << "\tImplicit:"; |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 970 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 971 | for (unsigned z=0; z < NumOfImpRefs; z++) |
Chris Lattner | 0665a5f | 2002-02-05 01:43:49 +0000 | [diff] [blame] | 972 | cerr << RAV(MInst->getImplicitRef(z)) << "\t"; |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 973 | } |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 974 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 975 | } // for all machine instructions |
| 976 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 977 | cerr << "\n"; |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 978 | |
| 979 | } // for all BBs |
| 980 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 981 | cerr << "\n"; |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 982 | } |
| 983 | |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 984 | |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 985 | //---------------------------------------------------------------------------- |
| 986 | |
| 987 | //---------------------------------------------------------------------------- |
| 988 | void PhyRegAlloc::colorIncomingArgs() |
| 989 | { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 990 | const BasicBlock &FirstBB = Meth->front(); |
Vikram S. Adve | f5af636 | 2002-07-08 23:15:32 +0000 | [diff] [blame] | 991 | const MachineInstr *FirstMI = MachineCodeForBasicBlock::get(&FirstBB).front(); |
Chris Lattner | dd1e40b | 2002-02-03 07:46:34 +0000 | [diff] [blame] | 992 | assert(FirstMI && "No machine instruction in entry BB"); |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 993 | |
Vikram S. Adve | 4876209 | 2002-04-25 04:34:15 +0000 | [diff] [blame] | 994 | MRI.colorMethodArgs(Meth, LRI, &AddedInstrAtEntry); |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 995 | } |
| 996 | |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 997 | |
| 998 | //---------------------------------------------------------------------------- |
| 999 | // Used to generate a label for a basic block |
| 1000 | //---------------------------------------------------------------------------- |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 1001 | void PhyRegAlloc::printLabel(const Value *const Val) { |
| 1002 | if (Val->hasName()) |
| 1003 | cerr << Val->getName(); |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 1004 | else |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 1005 | cerr << "Label" << Val; |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 1006 | } |
| 1007 | |
| 1008 | |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 1009 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | 0e62aa6 | 2001-10-19 21:39:31 +0000 | [diff] [blame] | 1010 | // This method calls setSugColorUsable method of each live range. This |
| 1011 | // will determine whether the suggested color of LR is really usable. |
| 1012 | // A suggested color is not usable when the suggested color is volatile |
| 1013 | // AND when there are call interferences |
| 1014 | //---------------------------------------------------------------------------- |
| 1015 | |
| 1016 | void PhyRegAlloc::markUnusableSugColors() |
| 1017 | { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1018 | if (DEBUG_RA ) cerr << "\nmarking unusable suggested colors ...\n"; |
Ruchira Sasanka | 0e62aa6 | 2001-10-19 21:39:31 +0000 | [diff] [blame] | 1019 | |
| 1020 | // hash map iterator |
| 1021 | LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin(); |
| 1022 | LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end(); |
| 1023 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1024 | for (; HMI != HMIEnd ; ++HMI ) { |
Chris Lattner | dd1e40b | 2002-02-03 07:46:34 +0000 | [diff] [blame] | 1025 | if (HMI->first) { |
| 1026 | LiveRange *L = HMI->second; // get the LiveRange |
| 1027 | if (L) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1028 | if (L->hasSuggestedColor()) { |
Chris Lattner | dd1e40b | 2002-02-03 07:46:34 +0000 | [diff] [blame] | 1029 | int RCID = L->getRegClass()->getID(); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1030 | if (MRI.isRegVolatile( RCID, L->getSuggestedColor()) && |
Ruchira Sasanka | 0e62aa6 | 2001-10-19 21:39:31 +0000 | [diff] [blame] | 1031 | L->isCallInterference() ) |
| 1032 | L->setSuggestedColorUsable( false ); |
| 1033 | else |
| 1034 | L->setSuggestedColorUsable( true ); |
| 1035 | } |
| 1036 | } // if L->hasSuggestedColor() |
| 1037 | } |
| 1038 | } // for all LR's in hash map |
| 1039 | } |
| 1040 | |
| 1041 | |
| 1042 | |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 1043 | //---------------------------------------------------------------------------- |
| 1044 | // The following method will set the stack offsets of the live ranges that |
| 1045 | // are decided to be spillled. This must be called just after coloring the |
| 1046 | // LRs using the graph coloring algo. For each live range that is spilled, |
| 1047 | // this method allocate a new spill position on the stack. |
| 1048 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | 0e62aa6 | 2001-10-19 21:39:31 +0000 | [diff] [blame] | 1049 | |
Chris Lattner | 3773094 | 2002-02-05 03:52:29 +0000 | [diff] [blame] | 1050 | void PhyRegAlloc::allocateStackSpace4SpilledLRs() { |
| 1051 | if (DEBUG_RA) cerr << "\nsetting LR stack offsets ...\n"; |
Ruchira Sasanka | 0e62aa6 | 2001-10-19 21:39:31 +0000 | [diff] [blame] | 1052 | |
Chris Lattner | 3773094 | 2002-02-05 03:52:29 +0000 | [diff] [blame] | 1053 | LiveRangeMapType::const_iterator HMI = LRI.getLiveRangeMap()->begin(); |
| 1054 | LiveRangeMapType::const_iterator HMIEnd = LRI.getLiveRangeMap()->end(); |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 1055 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1056 | for ( ; HMI != HMIEnd ; ++HMI) { |
Chris Lattner | 3773094 | 2002-02-05 03:52:29 +0000 | [diff] [blame] | 1057 | if (HMI->first && HMI->second) { |
| 1058 | LiveRange *L = HMI->second; // get the LiveRange |
| 1059 | if (!L->hasColor()) // NOTE: ** allocating the size of long Type ** |
| 1060 | L->setSpillOffFromFP(mcInfo.allocateSpilledValue(TM, Type::LongTy)); |
| 1061 | } |
| 1062 | } // for all LR's in hash map |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 1063 | } |
Ruchira Sasanka | 0e62aa6 | 2001-10-19 21:39:31 +0000 | [diff] [blame] | 1064 | |
| 1065 | |
| 1066 | |
Ruchira Sasanka | 0e62aa6 | 2001-10-19 21:39:31 +0000 | [diff] [blame] | 1067 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 1068 | // The entry pont to Register Allocation |
| 1069 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 1070 | |
| 1071 | void PhyRegAlloc::allocateRegisters() |
| 1072 | { |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 1073 | |
| 1074 | // make sure that we put all register classes into the RegClassList |
| 1075 | // before we call constructLiveRanges (now done in the constructor of |
| 1076 | // PhyRegAlloc class). |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 1077 | // |
| 1078 | LRI.constructLiveRanges(); // create LR info |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 1079 | |
Chris Lattner | dd1e40b | 2002-02-03 07:46:34 +0000 | [diff] [blame] | 1080 | if (DEBUG_RA) |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 1081 | LRI.printLiveRanges(); |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 1082 | |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 1083 | createIGNodeListsAndIGs(); // create IGNode list and IGs |
| 1084 | |
| 1085 | buildInterferenceGraphs(); // build IGs in all reg classes |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 1086 | |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 1087 | |
Chris Lattner | dd1e40b | 2002-02-03 07:46:34 +0000 | [diff] [blame] | 1088 | if (DEBUG_RA) { |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 1089 | // print all LRs in all reg classes |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1090 | for ( unsigned rc=0; rc < NumOfRegClasses ; rc++) |
| 1091 | RegClassList[rc]->printIGNodeList(); |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 1092 | |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 1093 | // print IGs in all register classes |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1094 | for ( unsigned rc=0; rc < NumOfRegClasses ; rc++) |
| 1095 | RegClassList[rc]->printIG(); |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 1096 | } |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 1097 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 1098 | |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 1099 | LRI.coalesceLRs(); // coalesce all live ranges |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 1100 | |
Ruchira Sasanka | ef1b0cb | 2001-11-03 17:13:27 +0000 | [diff] [blame] | 1101 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1102 | if (DEBUG_RA) { |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 1103 | // print all LRs in all reg classes |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1104 | for ( unsigned rc=0; rc < NumOfRegClasses ; rc++) |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 1105 | RegClassList[ rc ]->printIGNodeList(); |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 1106 | |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 1107 | // print IGs in all register classes |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1108 | for ( unsigned rc=0; rc < NumOfRegClasses ; rc++) |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 1109 | RegClassList[ rc ]->printIG(); |
| 1110 | } |
| 1111 | |
Ruchira Sasanka | 0e62aa6 | 2001-10-19 21:39:31 +0000 | [diff] [blame] | 1112 | |
| 1113 | // mark un-usable suggested color before graph coloring algorithm. |
| 1114 | // When this is done, the graph coloring algo will not reserve |
| 1115 | // suggested color unnecessarily - they can be used by another LR |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 1116 | // |
Ruchira Sasanka | 0e62aa6 | 2001-10-19 21:39:31 +0000 | [diff] [blame] | 1117 | markUnusableSugColors(); |
| 1118 | |
| 1119 | // color all register classes using the graph coloring algo |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1120 | for (unsigned rc=0; rc < NumOfRegClasses ; rc++) |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 1121 | RegClassList[ rc ]->colorAllRegs(); |
| 1122 | |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 1123 | // Atter grpah coloring, if some LRs did not receive a color (i.e, spilled) |
| 1124 | // a poistion for such spilled LRs |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 1125 | // |
Ruchira Sasanka | 174bded | 2001-10-28 18:12:02 +0000 | [diff] [blame] | 1126 | allocateStackSpace4SpilledLRs(); |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 1127 | |
Ruchira Sasanka | f90870f | 2001-11-15 22:02:06 +0000 | [diff] [blame] | 1128 | mcInfo.popAllTempValues(TM); // TODO **Check |
| 1129 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 1130 | // color incoming args - if the correct color was not received |
| 1131 | // insert code to copy to the correct register |
| 1132 | // |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 1133 | colorIncomingArgs(); |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 1134 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 1135 | // Now update the machine code with register names and add any |
| 1136 | // additional code inserted by the register allocator to the instruction |
| 1137 | // stream |
| 1138 | // |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 1139 | updateMachineCode(); |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 1140 | |
Chris Lattner | 045e7c8 | 2001-09-19 16:26:23 +0000 | [diff] [blame] | 1141 | if (DEBUG_RA) { |
Vikram S. Adve | 12af164 | 2001-11-08 04:48:50 +0000 | [diff] [blame] | 1142 | MachineCodeForMethod::get(Meth).dump(); |
Chris Lattner | 045e7c8 | 2001-09-19 16:26:23 +0000 | [diff] [blame] | 1143 | printMachineCode(); // only for DEBUGGING |
| 1144 | } |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 1145 | } |
| 1146 | |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 1147 | |
| 1148 | |