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