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