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