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