Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 1 | #include "llvm/CodeGen/PhyRegAlloc.h" |
| 2 | |
Chris Lattner | 045e7c8 | 2001-09-19 16:26:23 +0000 | [diff] [blame] | 3 | cl::Enum<RegAllocDebugLevel_t> DEBUG_RA("dregalloc", cl::NoFlags, |
| 4 | "enable register allocation debugging information", |
| 5 | clEnumValN(RA_DEBUG_None , "n", "disable debug output"), |
| 6 | clEnumValN(RA_DEBUG_Normal , "y", "enable debug output"), |
| 7 | clEnumValN(RA_DEBUG_Verbose, "v", "enable extra debug output"), 0); |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 8 | |
| 9 | |
| 10 | //---------------------------------------------------------------------------- |
| 11 | // Constructor: Init local composite objects and create register classes. |
| 12 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 13 | PhyRegAlloc::PhyRegAlloc(const Method *const M, |
| 14 | const TargetMachine& tm, |
| 15 | MethodLiveVarInfo *const Lvi) |
| 16 | : RegClassList(), |
| 17 | Meth(M), TM(tm), LVI(Lvi), LRI(M, tm, RegClassList), |
| 18 | MRI( tm.getRegInfo() ), |
| 19 | NumOfRegClasses(MRI.getNumOfRegClasses()), |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 20 | AddedInstrMap() |
| 21 | |
| 22 | { |
| 23 | // **TODO: use an actual reserved color list |
| 24 | ReservedColorListType *RCL = new ReservedColorListType(); |
| 25 | |
| 26 | // create each RegisterClass and put in RegClassList |
| 27 | for( unsigned int rc=0; rc < NumOfRegClasses; rc++) |
| 28 | RegClassList.push_back( new RegClass(M, MRI.getMachineRegClass(rc), RCL) ); |
| 29 | |
| 30 | } |
| 31 | |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 32 | //---------------------------------------------------------------------------- |
| 33 | // This method initally creates interference graphs (one in each reg class) |
| 34 | // and IGNodeList (one in each IG). The actual nodes will be pushed later. |
| 35 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 36 | |
| 37 | void PhyRegAlloc::createIGNodeListsAndIGs() |
| 38 | { |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 39 | if(DEBUG_RA ) cout << "Creating LR lists ..." << endl; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 40 | |
| 41 | // hash map iterator |
| 42 | LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin(); |
| 43 | |
| 44 | // hash map end |
| 45 | LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end(); |
| 46 | |
| 47 | for( ; HMI != HMIEnd ; ++HMI ) { |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 48 | |
| 49 | if( (*HMI).first ) { |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 50 | |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 51 | LiveRange *L = (*HMI).second; // get the LiveRange |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 52 | |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 53 | if( !L) { |
| 54 | if( DEBUG_RA) { |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 55 | cout << "\n*?!?Warning: Null liver range found for: "; |
| 56 | printValue( (*HMI).first) ; cout << endl; |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 57 | } |
| 58 | continue; |
| 59 | } |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 60 | // if the Value * is not null, and LR |
| 61 | // is not yet written to the IGNodeList |
| 62 | if( !(L->getUserIGNode()) ) { |
| 63 | |
| 64 | RegClass *const RC = // RegClass of first value in the LR |
| 65 | //RegClassList [MRI.getRegClassIDOfValue(*(L->begin()))]; |
| 66 | RegClassList[ L->getRegClass()->getID() ]; |
| 67 | |
| 68 | RC-> addLRToIG( L ); // add this LR to an IG |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // init RegClassList |
| 74 | for( unsigned int rc=0; rc < NumOfRegClasses ; rc++) |
| 75 | RegClassList[ rc ]->createInterferenceGraph(); |
| 76 | |
| 77 | if( DEBUG_RA) |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 78 | cout << "LRLists Created!" << endl; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | |
| 82 | |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 83 | //---------------------------------------------------------------------------- |
| 84 | // This method will add all interferences at for a given instruction. |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 85 | // Interence occurs only if the LR of Def (Inst or Arg) is of the same reg |
| 86 | // class as that of live var. The live var passed to this function is the |
| 87 | // LVset AFTER the instruction |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 88 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 89 | |
| 90 | void PhyRegAlloc::addInterference(const Value *const Def, |
| 91 | const LiveVarSet *const LVSet, |
| 92 | const bool isCallInst) { |
| 93 | |
| 94 | LiveVarSet::const_iterator LIt = LVSet->begin(); |
| 95 | |
| 96 | // get the live range of instruction |
| 97 | const LiveRange *const LROfDef = LRI.getLiveRangeForValue( Def ); |
| 98 | |
| 99 | IGNode *const IGNodeOfDef = LROfDef->getUserIGNode(); |
| 100 | assert( IGNodeOfDef ); |
| 101 | |
| 102 | RegClass *const RCOfDef = LROfDef->getRegClass(); |
| 103 | |
| 104 | // for each live var in live variable set |
| 105 | for( ; LIt != LVSet->end(); ++LIt) { |
| 106 | |
| 107 | if( DEBUG_RA > 1) { |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 108 | cout << "< Def="; printValue(Def); |
| 109 | cout << ", Lvar="; printValue( *LIt); cout << "> "; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | // get the live range corresponding to live var |
| 113 | LiveRange *const LROfVar = LRI.getLiveRangeForValue(*LIt ); |
| 114 | |
| 115 | // LROfVar can be null if it is a const since a const |
| 116 | // doesn't have a dominating def - see Assumptions above |
| 117 | if( LROfVar) { |
| 118 | |
| 119 | if(LROfDef == LROfVar) // do not set interf for same LR |
| 120 | continue; |
| 121 | |
| 122 | // if 2 reg classes are the same set interference |
| 123 | if( RCOfDef == LROfVar->getRegClass() ){ |
| 124 | RCOfDef->setInterference( LROfDef, LROfVar); |
| 125 | |
| 126 | } |
| 127 | |
Ruchira Sasanka | 0931a01 | 2001-09-15 19:06:58 +0000 | [diff] [blame] | 128 | else if(DEBUG_RA > 1) { |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 129 | // we will not have LRs for values not explicitly allocated in the |
| 130 | // instruction stream (e.g., constants) |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 131 | cout << " warning: no live range for " ; |
| 132 | printValue( *LIt); cout << endl; } |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 133 | |
Ruchira Sasanka | 958faf3 | 2001-10-19 17:21:03 +0000 | [diff] [blame] | 134 | } |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 135 | |
Ruchira Sasanka | 958faf3 | 2001-10-19 17:21:03 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Ruchira Sasanka | 958faf3 | 2001-10-19 17:21:03 +0000 | [diff] [blame] | 140 | |
| 141 | //---------------------------------------------------------------------------- |
| 142 | // For a call instruction, this method sets the CallInterference flag in |
| 143 | // the LR of each variable live int the Live Variable Set live after the |
| 144 | // call instruction (except the return value of the call instruction - since |
| 145 | // the return value does not interfere with that call itself). |
| 146 | //---------------------------------------------------------------------------- |
| 147 | |
| 148 | void PhyRegAlloc::setCallInterferences(const MachineInstr *MInst, |
| 149 | const LiveVarSet *const LVSetAft ) |
| 150 | { |
| 151 | // Now find the LR of the return value of the call |
| 152 | // The last *implicit operand* is the return value of a call |
| 153 | |
| 154 | // We do this because, we look at the LV set *after* the instruction |
| 155 | // to determine, which LRs must be saved across calls. The return value |
| 156 | // of the call is live in this set - but it does not interfere with call |
| 157 | // (i.e., we can allocate a volatile register to the return value) |
| 158 | |
| 159 | LiveRange *RetValLR = NULL; |
| 160 | |
| 161 | unsigned NumOfImpRefs = MInst->getNumImplicitRefs(); |
| 162 | if( NumOfImpRefs > 0 ) { |
| 163 | |
| 164 | if( MInst->implicitRefIsDefined(NumOfImpRefs-1) ) { |
| 165 | |
| 166 | const Value *RetVal = MInst->getImplicitRef(NumOfImpRefs-1); |
| 167 | RetValLR = LRI.getLiveRangeForValue( RetVal ); |
| 168 | assert( RetValLR && "No LR for RetValue of call"); |
| 169 | } |
| 170 | |
| 171 | } |
| 172 | |
| 173 | |
| 174 | if( DEBUG_RA) |
| 175 | cout << "\n For call inst: " << *MInst; |
| 176 | |
| 177 | LiveVarSet::const_iterator LIt = LVSetAft->begin(); |
| 178 | |
| 179 | // for each live var in live variable set after machine inst |
| 180 | for( ; LIt != LVSetAft->end(); ++LIt) { |
| 181 | |
| 182 | // get the live range corresponding to live var |
| 183 | LiveRange *const LR = LRI.getLiveRangeForValue(*LIt ); |
| 184 | |
| 185 | if( LR && DEBUG_RA) { |
| 186 | cout << "\n\tLR Aft Call: "; |
| 187 | LR->printSet(); |
| 188 | } |
| 189 | |
| 190 | |
| 191 | // LR can be null if it is a const since a const |
| 192 | // doesn't have a dominating def - see Assumptions above |
| 193 | if( LR && (LR != RetValLR) ) { |
| 194 | LR->setCallInterference(); |
| 195 | if( DEBUG_RA) { |
| 196 | cout << "\n ++Added call interf for LR: " ; |
| 197 | LR->printSet(); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | } |
| 202 | |
| 203 | } |
| 204 | |
| 205 | |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 206 | //---------------------------------------------------------------------------- |
| 207 | // This method will walk thru code and create interferences in the IG of |
| 208 | // each RegClass. |
| 209 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 210 | |
| 211 | void PhyRegAlloc::buildInterferenceGraphs() |
| 212 | { |
| 213 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 214 | if(DEBUG_RA) cout << "Creating interference graphs ..." << endl; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 215 | |
| 216 | Method::const_iterator BBI = Meth->begin(); // random iterator for BBs |
| 217 | |
| 218 | for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order |
| 219 | |
| 220 | // get the iterator for machine instructions |
| 221 | const MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec(); |
| 222 | MachineCodeForBasicBlock::const_iterator |
| 223 | MInstIterator = MIVec.begin(); |
| 224 | |
| 225 | // iterate over all the machine instructions in BB |
| 226 | for( ; MInstIterator != MIVec.end(); ++MInstIterator) { |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 227 | |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 228 | const MachineInstr *const MInst = *MInstIterator; |
| 229 | |
| 230 | // get the LV set after the instruction |
| 231 | const LiveVarSet *const LVSetAI = |
| 232 | LVI->getLiveVarSetAfterMInst(MInst, *BBI); |
| 233 | |
| 234 | const bool isCallInst = TM.getInstrInfo().isCall(MInst->getOpCode()); |
| 235 | |
Ruchira Sasanka | 958faf3 | 2001-10-19 17:21:03 +0000 | [diff] [blame] | 236 | if( isCallInst ) { |
| 237 | //cout << "\nFor call inst: " << *MInst; |
| 238 | |
| 239 | // set the isCallInterference flag of each live range wich extends |
| 240 | // accross this call instruction. This information is used by graph |
| 241 | // coloring algo to avoid allocating volatile colors to live ranges |
| 242 | // that span across calls (since they have to be saved/restored) |
| 243 | setCallInterferences( MInst, LVSetAI); |
| 244 | } |
| 245 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 246 | |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 247 | // iterate over MI operands to find defs |
| 248 | for( MachineInstr::val_op_const_iterator OpI(MInst);!OpI.done(); ++OpI) { |
| 249 | |
| 250 | if( OpI.isDef() ) { |
| 251 | // create a new LR iff this operand is a def |
| 252 | addInterference(*OpI, LVSetAI, isCallInst ); |
| 253 | |
| 254 | } //if this is a def |
| 255 | |
| 256 | } // for all operands |
| 257 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 258 | |
| 259 | // Also add interference for any implicit definitions in a machine |
| 260 | // instr (currently, only calls have this). |
| 261 | |
| 262 | unsigned NumOfImpRefs = MInst->getNumImplicitRefs(); |
| 263 | if( NumOfImpRefs > 0 ) { |
| 264 | for(unsigned z=0; z < NumOfImpRefs; z++) |
| 265 | if( MInst->implicitRefIsDefined(z) ) |
| 266 | addInterference( MInst->getImplicitRef(z), LVSetAI, isCallInst ); |
| 267 | } |
| 268 | |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 269 | } // for all machine instructions in BB |
| 270 | |
| 271 | |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 272 | #if 0 |
| 273 | |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 274 | // go thru LLVM instructions in the basic block and record all CALL |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 275 | // instructions and Return instructions in the CallInstrList |
| 276 | // This is done because since there are no reverse pointers in machine |
| 277 | // instructions to find the llvm instruction, when we encounter a call |
| 278 | // or a return whose args must be specailly colored (e.g., %o's for args) |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 279 | BasicBlock::const_iterator InstIt = (*BBI)->begin(); |
| 280 | |
| 281 | for( ; InstIt != (*BBI)->end() ; ++ InstIt) { |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 282 | unsigned OpCode = (*InstIt)->getOpcode(); |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 283 | |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 284 | if( OpCode == Instruction::Call ) |
| 285 | CallInstrList.push_back( *InstIt ); |
| 286 | |
| 287 | else if( OpCode == Instruction::Ret ) |
| 288 | RetInstrList.push_back( *InstIt ); |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 289 | } |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 290 | |
| 291 | #endif |
| 292 | |
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 | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 336 | |
| 337 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 338 | // This method inserts caller saving/restoring instructons before/after |
| 339 | // a call machine instruction. |
| 340 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 341 | |
| 342 | |
| 343 | void PhyRegAlloc::insertCallerSavingCode(const MachineInstr *MInst, |
| 344 | const BasicBlock *BB ) |
| 345 | { |
Ruchira Sasanka | 958faf3 | 2001-10-19 17:21:03 +0000 | [diff] [blame] | 346 | // assert( (TM.getInstrInfo()).isCall( MInst->getOpCode() ) ); |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 347 | |
Ruchira Sasanka | 47c1372 | 2001-10-16 01:33:55 +0000 | [diff] [blame] | 348 | int StackOff = -8; // ****TODO : Change |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 349 | hash_set<unsigned> PushedRegSet; |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 350 | |
| 351 | // Now find the LR of the return value of the call |
| 352 | // The last *implicit operand* is the return value of a call |
| 353 | // Insert it to to he PushedRegSet since we must not save that register |
| 354 | // and restore it after the call. |
| 355 | // We do this because, we look at the LV set *after* the instruction |
| 356 | // to determine, which LRs must be saved across calls. The return value |
| 357 | // of the call is live in this set - but we must not save/restore it. |
| 358 | |
| 359 | unsigned NumOfImpRefs = MInst->getNumImplicitRefs(); |
| 360 | if( NumOfImpRefs > 0 ) { |
| 361 | |
| 362 | if( MInst->implicitRefIsDefined(NumOfImpRefs-1) ) { |
| 363 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 364 | const Value *RetVal = MInst->getImplicitRef(NumOfImpRefs-1); |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 365 | LiveRange *RetValLR = LRI.getLiveRangeForValue( RetVal ); |
| 366 | assert( RetValLR && "No LR for RetValue of call"); |
| 367 | |
| 368 | PushedRegSet.insert( |
| 369 | MRI.getUnifiedRegNum((RetValLR->getRegClass())->getID(), |
| 370 | RetValLR->getColor() ) ); |
| 371 | } |
| 372 | |
| 373 | } |
| 374 | |
| 375 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 376 | const LiveVarSet *LVSetAft = LVI->getLiveVarSetAfterMInst(MInst, BB); |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 377 | |
| 378 | LiveVarSet::const_iterator LIt = LVSetAft->begin(); |
| 379 | |
| 380 | // for each live var in live variable set after machine inst |
| 381 | for( ; LIt != LVSetAft->end(); ++LIt) { |
| 382 | |
| 383 | // get the live range corresponding to live var |
| 384 | LiveRange *const LR = LRI.getLiveRangeForValue(*LIt ); |
| 385 | |
Ruchira Sasanka | 958faf3 | 2001-10-19 17:21:03 +0000 | [diff] [blame] | 386 | // LR can be null if it is a const since a const |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 387 | // doesn't have a dominating def - see Assumptions above |
| 388 | if( LR ) { |
| 389 | |
| 390 | if( LR->hasColor() ) { |
| 391 | |
| 392 | unsigned RCID = (LR->getRegClass())->getID(); |
| 393 | unsigned Color = LR->getColor(); |
| 394 | |
| 395 | if ( MRI.isRegVolatile(RCID, Color) ) { |
| 396 | |
| 397 | // if the value is in both LV sets (i.e., live before and after |
| 398 | // the call machine instruction) |
Ruchira Sasanka | 97b8b44 | 2001-10-18 22:36:26 +0000 | [diff] [blame] | 399 | |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 400 | unsigned Reg = MRI.getUnifiedRegNum(RCID, Color); |
| 401 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 402 | if( PushedRegSet.find(Reg) == PushedRegSet.end() ) { |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 403 | |
| 404 | // if we haven't already pushed that register |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 405 | |
| 406 | unsigned RegType = MRI.getRegType( LR ); |
| 407 | |
| 408 | // Now get two instructions - to push on stack and pop from stack |
| 409 | // and add them to InstrnsBefore and InstrnsAfter of the |
| 410 | // call instruction |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 411 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 412 | MachineInstr *AdIBef = |
| 413 | MRI.cpReg2MemMI(Reg, MRI.getFramePointer(), StackOff, RegType ); |
| 414 | |
| 415 | MachineInstr *AdIAft = |
| 416 | MRI.cpMem2RegMI(MRI.getFramePointer(), StackOff, Reg, RegType ); |
| 417 | |
| 418 | ((AddedInstrMap[MInst])->InstrnsBefore).push_front(AdIBef); |
| 419 | ((AddedInstrMap[MInst])->InstrnsAfter).push_back(AdIAft); |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 420 | |
| 421 | PushedRegSet.insert( Reg ); |
Ruchira Sasanka | 47c1372 | 2001-10-16 01:33:55 +0000 | [diff] [blame] | 422 | StackOff -= 8; // ****TODO: Correct ?????? |
Ruchira Sasanka | 97b8b44 | 2001-10-18 22:36:26 +0000 | [diff] [blame] | 423 | |
| 424 | if(DEBUG_RA) { |
Ruchira Sasanka | 958faf3 | 2001-10-19 17:21:03 +0000 | [diff] [blame] | 425 | cerr << "\nFor callee save call inst:" << *MInst; |
Ruchira Sasanka | 97b8b44 | 2001-10-18 22:36:26 +0000 | [diff] [blame] | 426 | cerr << "\n -inserted caller saving instrs:\n\t "; |
| 427 | cerr << *AdIBef << "\n\t" << *AdIAft ; |
| 428 | } |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 429 | } // if not already pushed |
| 430 | |
| 431 | } // if LR has a volatile color |
| 432 | |
| 433 | } // if LR has color |
| 434 | |
| 435 | } // if there is a LR for Var |
| 436 | |
| 437 | } // for each value in the LV set after instruction |
| 438 | |
| 439 | } |
| 440 | |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 441 | |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 442 | //---------------------------------------------------------------------------- |
| 443 | // This method is called after register allocation is complete to set the |
| 444 | // allocated reisters in the machine code. This code will add register numbers |
| 445 | // to MachineOperands that contain a Value. |
| 446 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 447 | |
| 448 | void PhyRegAlloc::updateMachineCode() |
| 449 | { |
| 450 | |
| 451 | Method::const_iterator BBI = Meth->begin(); // random iterator for BBs |
| 452 | |
| 453 | for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order |
| 454 | |
Ruchira Sasanka | 0931a01 | 2001-09-15 19:06:58 +0000 | [diff] [blame] | 455 | // get the iterator for machine instructions |
| 456 | MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec(); |
| 457 | MachineCodeForBasicBlock::iterator MInstIterator = MIVec.begin(); |
| 458 | |
| 459 | // iterate over all the machine instructions in BB |
| 460 | for( ; MInstIterator != MIVec.end(); ++MInstIterator) { |
| 461 | |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 462 | MachineInstr *MInst = *MInstIterator; |
| 463 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 464 | // if this machine instr is call, insert caller saving code |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 465 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 466 | if( (TM.getInstrInfo()).isCall( MInst->getOpCode()) ) |
| 467 | insertCallerSavingCode(MInst, *BBI ); |
| 468 | |
| 469 | // If there are instructions to be added, *before* this machine |
| 470 | // instruction, add them now. |
| 471 | |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 472 | if( AddedInstrMap[ MInst ] ) { |
| 473 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 474 | deque<MachineInstr *> &IBef = (AddedInstrMap[MInst])->InstrnsBefore; |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 475 | |
| 476 | if( ! IBef.empty() ) { |
| 477 | |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 478 | deque<MachineInstr *>::iterator AdIt; |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 479 | |
| 480 | for( AdIt = IBef.begin(); AdIt != IBef.end() ; ++AdIt ) { |
| 481 | |
Ruchira Sasanka | 97b8b44 | 2001-10-18 22:36:26 +0000 | [diff] [blame] | 482 | if( DEBUG_RA) |
| 483 | cerr << " *$* PREPENDed instr " << *AdIt << endl; |
| 484 | |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 485 | MInstIterator = MIVec.insert( MInstIterator, *AdIt ); |
| 486 | ++MInstIterator; |
| 487 | } |
| 488 | |
| 489 | } |
| 490 | |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | |
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 | |
| 514 | const LiveRange *const LR = LRI.getLiveRangeForValue(Val); |
| 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 | #if 0 |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 531 | if( ((Val->getType())->isLabelType()) || |
| 532 | (Val->getValueType() == Value::ConstantVal) ) |
| 533 | ; // do nothing |
| 534 | |
| 535 | // The return address is not explicitly defined within a |
| 536 | // method. So, it is not colored by usual algorithm. In that case |
| 537 | // color it here. |
| 538 | |
| 539 | //else if (TM.getInstrInfo().isCall(MInst->getOpCode())) |
| 540 | //Op.setRegForValue( MRI.getCallAddressReg() ); |
| 541 | |
| 542 | //TM.getInstrInfo().isReturn(MInst->getOpCode()) |
| 543 | else if(TM.getInstrInfo().isReturn(MInst->getOpCode()) ) { |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 544 | if (DEBUG_RA) cout << endl << "RETURN found" << endl; |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 545 | Op.setRegForValue( MRI.getReturnAddressReg() ); |
| 546 | |
| 547 | } |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 548 | |
| 549 | if (Val->getValueType() == Value::InstructionVal) |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 550 | { |
Ruchira Sasanka | 1b732fd | 2001-10-16 16:34:44 +0000 | [diff] [blame] | 551 | if( DEBUG_RA ) { |
| 552 | cout << "!Warning: No LiveRange for: "; |
| 553 | printValue( Val); cout << " Type: " << Val->getValueType(); |
| 554 | cout << " RegVal=" << Op.getAllocatedRegNum() << endl; |
| 555 | } |
Ruchira Sasanka | 0931a01 | 2001-09-15 19:06:58 +0000 | [diff] [blame] | 556 | } |
| 557 | |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 558 | #endif |
| 559 | |
Ruchira Sasanka | 0931a01 | 2001-09-15 19:06:58 +0000 | [diff] [blame] | 560 | continue; |
| 561 | } |
| 562 | |
| 563 | unsigned RCID = (LR->getRegClass())->getID(); |
| 564 | |
| 565 | Op.setRegForValue( MRI.getUnifiedRegNum(RCID, LR->getColor()) ); |
| 566 | |
| 567 | int RegNum = MRI.getUnifiedRegNum(RCID, LR->getColor()); |
| 568 | |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 569 | } |
| 570 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 571 | } // for each operand |
| 572 | |
| 573 | |
| 574 | // If there are instructions to be added *after* this machine |
| 575 | // instruction, add them now |
| 576 | |
| 577 | if( AddedInstrMap[ MInst ] ) { |
| 578 | |
| 579 | deque<MachineInstr *> &IAft = (AddedInstrMap[MInst])->InstrnsAfter; |
| 580 | |
| 581 | if( ! IAft.empty() ) { |
| 582 | |
| 583 | deque<MachineInstr *>::iterator AdIt; |
| 584 | |
| 585 | ++MInstIterator; // advance to the next instruction |
| 586 | |
| 587 | for( AdIt = IAft.begin(); AdIt != IAft.end() ; ++AdIt ) { |
| 588 | |
Ruchira Sasanka | 97b8b44 | 2001-10-18 22:36:26 +0000 | [diff] [blame] | 589 | if(DEBUG_RA) |
| 590 | cerr << " *#* APPENDed instr opcode: " << *AdIt << endl; |
| 591 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 592 | MInstIterator = MIVec.insert( MInstIterator, *AdIt ); |
| 593 | ++MInstIterator; |
| 594 | } |
| 595 | |
| 596 | // MInsterator already points to the next instr. Since the |
| 597 | // for loop also increments it, decrement it to point to the |
| 598 | // instruction added last |
| 599 | --MInstIterator; |
| 600 | |
| 601 | } |
| 602 | |
Ruchira Sasanka | 0931a01 | 2001-09-15 19:06:58 +0000 | [diff] [blame] | 603 | } |
| 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 | |
| 610 | |
| 611 | |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 612 | //---------------------------------------------------------------------------- |
| 613 | // This method prints the code with registers after register allocation is |
| 614 | // complete. |
| 615 | //---------------------------------------------------------------------------- |
| 616 | void PhyRegAlloc::printMachineCode() |
| 617 | { |
| 618 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 619 | cout << endl << ";************** Method "; |
| 620 | cout << Meth->getName() << " *****************" << endl; |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 621 | |
| 622 | Method::const_iterator BBI = Meth->begin(); // random iterator for BBs |
| 623 | |
| 624 | for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order |
| 625 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 626 | cout << endl ; printLabel( *BBI); cout << ": "; |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 627 | |
| 628 | // get the iterator for machine instructions |
| 629 | MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec(); |
| 630 | MachineCodeForBasicBlock::iterator MInstIterator = MIVec.begin(); |
| 631 | |
| 632 | // iterate over all the machine instructions in BB |
| 633 | for( ; MInstIterator != MIVec.end(); ++MInstIterator) { |
| 634 | |
| 635 | MachineInstr *const MInst = *MInstIterator; |
| 636 | |
| 637 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 638 | cout << endl << "\t"; |
| 639 | cout << TargetInstrDescriptors[MInst->getOpCode()].opCodeString; |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 640 | |
| 641 | |
| 642 | //for(MachineInstr::val_op_const_iterator OpI(MInst);!OpI.done();++OpI) { |
| 643 | |
| 644 | for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) { |
| 645 | |
| 646 | MachineOperand& Op = MInst->getOperand(OpNum); |
| 647 | |
| 648 | if( Op.getOperandType() == MachineOperand::MO_VirtualRegister || |
Ruchira Sasanka | 97b8b44 | 2001-10-18 22:36:26 +0000 | [diff] [blame] | 649 | Op.getOperandType() == MachineOperand::MO_CCRegister /*|| |
| 650 | Op.getOperandType() == MachineOperand::MO_PCRelativeDisp*/ ) { |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 651 | |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 652 | const Value *const Val = Op.getVRegValue () ; |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 653 | // ****this code is temporary till NULL Values are fixed |
| 654 | if( ! Val ) { |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 655 | cout << "\t<*NULL*>"; |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 656 | continue; |
| 657 | } |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 658 | |
| 659 | // if a label or a constant |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 660 | if( (Val->getValueType() == Value::BasicBlockVal) ) { |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 661 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 662 | cout << "\t"; printLabel( Op.getVRegValue () ); |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 663 | } |
| 664 | else { |
| 665 | // else it must be a register value |
| 666 | const int RegNum = Op.getAllocatedRegNum(); |
| 667 | |
Ruchira Sasanka | 0e62aa6 | 2001-10-19 21:39:31 +0000 | [diff] [blame] | 668 | cout << "\t" << "%" << MRI.getUnifiedRegName( RegNum ); |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | } |
| 672 | else if(Op.getOperandType() == MachineOperand::MO_MachineRegister) { |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 673 | cout << "\t" << "%" << MRI.getUnifiedRegName(Op.getMachineRegNum()); |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | else |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 677 | cout << "\t" << Op; // use dump field |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 678 | } |
| 679 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 680 | |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 681 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 682 | unsigned NumOfImpRefs = MInst->getNumImplicitRefs(); |
| 683 | if( NumOfImpRefs > 0 ) { |
| 684 | |
| 685 | cout << "\tImplicit:"; |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 686 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 687 | for(unsigned z=0; z < NumOfImpRefs; z++) { |
| 688 | printValue( MInst->getImplicitRef(z) ); |
| 689 | cout << "\t"; |
| 690 | } |
| 691 | |
| 692 | } |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 693 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 694 | } // for all machine instructions |
| 695 | |
| 696 | |
| 697 | cout << endl; |
| 698 | |
| 699 | } // for all BBs |
| 700 | |
| 701 | cout << endl; |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 702 | } |
| 703 | |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 704 | |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 705 | //---------------------------------------------------------------------------- |
| 706 | // |
| 707 | //---------------------------------------------------------------------------- |
| 708 | |
| 709 | void PhyRegAlloc::colorCallRetArgs() |
| 710 | { |
| 711 | |
| 712 | CallRetInstrListType &CallRetInstList = LRI.getCallRetInstrList(); |
| 713 | CallRetInstrListType::const_iterator It = CallRetInstList.begin(); |
| 714 | |
| 715 | for( ; It != CallRetInstList.end(); ++It ) { |
| 716 | |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 717 | const MachineInstr *const CRMI = *It; |
| 718 | unsigned OpCode = CRMI->getOpCode(); |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 719 | |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 720 | // get the added instructions for this Call/Ret instruciton |
| 721 | AddedInstrns *AI = AddedInstrMap[ CRMI ]; |
| 722 | if ( !AI ) { |
| 723 | AI = new AddedInstrns(); |
| 724 | AddedInstrMap[ CRMI ] = AI; |
| 725 | } |
| 726 | |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 727 | if( (TM.getInstrInfo()).isCall( OpCode ) ) |
| 728 | MRI.colorCallArgs( CRMI, LRI, AI ); |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 729 | |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 730 | else if ( (TM.getInstrInfo()).isReturn(OpCode) ) |
| 731 | MRI.colorRetValue( CRMI, LRI, AI ); |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 732 | |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 733 | else assert( 0 && "Non Call/Ret instrn in CallRetInstrList\n" ); |
| 734 | |
| 735 | } |
| 736 | |
| 737 | } |
| 738 | |
Ruchira Sasanka | 0e62aa6 | 2001-10-19 21:39:31 +0000 | [diff] [blame] | 739 | |
| 740 | |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 741 | //---------------------------------------------------------------------------- |
| 742 | |
| 743 | //---------------------------------------------------------------------------- |
| 744 | void PhyRegAlloc::colorIncomingArgs() |
| 745 | { |
| 746 | const BasicBlock *const FirstBB = Meth->front(); |
| 747 | const MachineInstr *FirstMI = *((FirstBB->getMachineInstrVec()).begin()); |
| 748 | assert( FirstMI && "No machine instruction in entry BB"); |
| 749 | |
| 750 | AddedInstrns *AI = AddedInstrMap[ FirstMI ]; |
| 751 | if ( !AI ) { |
| 752 | AI = new AddedInstrns(); |
| 753 | AddedInstrMap[ FirstMI ] = AI; |
| 754 | } |
| 755 | |
| 756 | MRI.colorMethodArgs(Meth, LRI, AI ); |
| 757 | } |
| 758 | |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 759 | |
| 760 | //---------------------------------------------------------------------------- |
| 761 | // Used to generate a label for a basic block |
| 762 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 763 | void PhyRegAlloc::printLabel(const Value *const Val) |
| 764 | { |
| 765 | if( Val->hasName() ) |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 766 | cout << Val->getName(); |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 767 | else |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 768 | cout << "Label" << Val; |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 772 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | 0e62aa6 | 2001-10-19 21:39:31 +0000 | [diff] [blame] | 773 | // This method calls setSugColorUsable method of each live range. This |
| 774 | // will determine whether the suggested color of LR is really usable. |
| 775 | // A suggested color is not usable when the suggested color is volatile |
| 776 | // AND when there are call interferences |
| 777 | //---------------------------------------------------------------------------- |
| 778 | |
| 779 | void PhyRegAlloc::markUnusableSugColors() |
| 780 | { |
| 781 | if(DEBUG_RA ) cout << "Creating LR lists ..." << endl; |
| 782 | |
| 783 | // hash map iterator |
| 784 | LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin(); |
| 785 | LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end(); |
| 786 | |
| 787 | for( ; HMI != HMIEnd ; ++HMI ) { |
| 788 | |
| 789 | if( (*HMI).first ) { |
| 790 | |
| 791 | LiveRange *L = (*HMI).second; // get the LiveRange |
| 792 | |
| 793 | if(L) { |
| 794 | if( L->hasSuggestedColor() ) { |
| 795 | |
| 796 | int RCID = (L->getRegClass())->getID(); |
| 797 | if( MRI.isRegVolatile( RCID, L->getSuggestedColor()) && |
| 798 | L->isCallInterference() ) |
| 799 | L->setSuggestedColorUsable( false ); |
| 800 | else |
| 801 | L->setSuggestedColorUsable( true ); |
| 802 | } |
| 803 | } // if L->hasSuggestedColor() |
| 804 | } |
| 805 | } // for all LR's in hash map |
| 806 | } |
| 807 | |
| 808 | |
| 809 | |
| 810 | |
| 811 | |
| 812 | |
| 813 | |
| 814 | |
| 815 | |
| 816 | |
| 817 | |
| 818 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 819 | // The entry pont to Register Allocation |
| 820 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 821 | |
| 822 | void PhyRegAlloc::allocateRegisters() |
| 823 | { |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 824 | |
| 825 | // make sure that we put all register classes into the RegClassList |
| 826 | // before we call constructLiveRanges (now done in the constructor of |
| 827 | // PhyRegAlloc class). |
| 828 | |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 829 | constructLiveRanges(); // create LR info |
| 830 | |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 831 | if( DEBUG_RA ) |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 832 | LRI.printLiveRanges(); |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 833 | |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 834 | createIGNodeListsAndIGs(); // create IGNode list and IGs |
| 835 | |
| 836 | buildInterferenceGraphs(); // build IGs in all reg classes |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 837 | |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 838 | |
| 839 | if( DEBUG_RA ) { |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 840 | // print all LRs in all reg classes |
| 841 | for( unsigned int rc=0; rc < NumOfRegClasses ; rc++) |
| 842 | RegClassList[ rc ]->printIGNodeList(); |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 843 | |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 844 | // print IGs in all register classes |
| 845 | for( unsigned int rc=0; rc < NumOfRegClasses ; rc++) |
| 846 | RegClassList[ rc ]->printIG(); |
| 847 | } |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 848 | |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 849 | LRI.coalesceLRs(); // coalesce all live ranges |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 850 | |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 851 | if( DEBUG_RA) { |
| 852 | // print all LRs in all reg classes |
| 853 | for( unsigned int rc=0; rc < NumOfRegClasses ; rc++) |
| 854 | RegClassList[ rc ]->printIGNodeList(); |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 855 | |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 856 | // print IGs in all register classes |
| 857 | for( unsigned int rc=0; rc < NumOfRegClasses ; rc++) |
| 858 | RegClassList[ rc ]->printIG(); |
| 859 | } |
| 860 | |
Ruchira Sasanka | 0e62aa6 | 2001-10-19 21:39:31 +0000 | [diff] [blame] | 861 | |
| 862 | // mark un-usable suggested color before graph coloring algorithm. |
| 863 | // When this is done, the graph coloring algo will not reserve |
| 864 | // suggested color unnecessarily - they can be used by another LR |
| 865 | markUnusableSugColors(); |
| 866 | |
| 867 | // color all register classes using the graph coloring algo |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 868 | for( unsigned int rc=0; rc < NumOfRegClasses ; rc++) |
| 869 | RegClassList[ rc ]->colorAllRegs(); |
| 870 | |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 871 | |
| 872 | // color incoming args and call args |
| 873 | colorIncomingArgs(); |
| 874 | colorCallRetArgs(); |
| 875 | |
Ruchira Sasanka | 97b8b44 | 2001-10-18 22:36:26 +0000 | [diff] [blame] | 876 | |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 877 | updateMachineCode(); |
Chris Lattner | 045e7c8 | 2001-09-19 16:26:23 +0000 | [diff] [blame] | 878 | if (DEBUG_RA) { |
Ruchira Sasanka | 1b732fd | 2001-10-16 16:34:44 +0000 | [diff] [blame] | 879 | PrintMachineInstructions(Meth); |
Chris Lattner | 045e7c8 | 2001-09-19 16:26:23 +0000 | [diff] [blame] | 880 | printMachineCode(); // only for DEBUGGING |
| 881 | } |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 882 | } |
| 883 | |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 884 | |
| 885 | |
| 886 | |