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