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