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