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 |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame^] | 152 | |
Ruchira Sasanka | 958faf3 | 2001-10-19 17:21:03 +0000 | [diff] [blame] | 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 | |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame^] | 161 | const Value *RetVal = MRI.getCallInstRetVal( MInst ); |
Ruchira Sasanka | 958faf3 | 2001-10-19 17:21:03 +0000 | [diff] [blame] | 162 | |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame^] | 163 | if( RetVal ) { |
| 164 | RetValLR = LRI.getLiveRangeForValue( RetVal ); |
| 165 | assert( RetValLR && "No LR for RetValue of call"); |
Ruchira Sasanka | 958faf3 | 2001-10-19 17:21:03 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Ruchira Sasanka | 958faf3 | 2001-10-19 17:21:03 +0000 | [diff] [blame] | 168 | if( DEBUG_RA) |
| 169 | cout << "\n For call inst: " << *MInst; |
| 170 | |
| 171 | LiveVarSet::const_iterator LIt = LVSetAft->begin(); |
| 172 | |
| 173 | // for each live var in live variable set after machine inst |
| 174 | for( ; LIt != LVSetAft->end(); ++LIt) { |
| 175 | |
| 176 | // get the live range corresponding to live var |
| 177 | LiveRange *const LR = LRI.getLiveRangeForValue(*LIt ); |
| 178 | |
| 179 | if( LR && DEBUG_RA) { |
| 180 | cout << "\n\tLR Aft Call: "; |
| 181 | LR->printSet(); |
| 182 | } |
| 183 | |
| 184 | |
| 185 | // LR can be null if it is a const since a const |
| 186 | // doesn't have a dominating def - see Assumptions above |
| 187 | if( LR && (LR != RetValLR) ) { |
| 188 | LR->setCallInterference(); |
| 189 | if( DEBUG_RA) { |
| 190 | cout << "\n ++Added call interf for LR: " ; |
| 191 | LR->printSet(); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | } |
| 196 | |
| 197 | } |
| 198 | |
| 199 | |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 200 | //---------------------------------------------------------------------------- |
| 201 | // This method will walk thru code and create interferences in the IG of |
| 202 | // each RegClass. |
| 203 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 204 | |
| 205 | void PhyRegAlloc::buildInterferenceGraphs() |
| 206 | { |
| 207 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 208 | if(DEBUG_RA) cout << "Creating interference graphs ..." << endl; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 209 | |
| 210 | Method::const_iterator BBI = Meth->begin(); // random iterator for BBs |
| 211 | |
| 212 | for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order |
| 213 | |
| 214 | // get the iterator for machine instructions |
| 215 | const MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec(); |
| 216 | MachineCodeForBasicBlock::const_iterator |
| 217 | MInstIterator = MIVec.begin(); |
| 218 | |
| 219 | // iterate over all the machine instructions in BB |
| 220 | for( ; MInstIterator != MIVec.end(); ++MInstIterator) { |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 221 | |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 222 | const MachineInstr *const MInst = *MInstIterator; |
| 223 | |
| 224 | // get the LV set after the instruction |
| 225 | const LiveVarSet *const LVSetAI = |
| 226 | LVI->getLiveVarSetAfterMInst(MInst, *BBI); |
| 227 | |
| 228 | const bool isCallInst = TM.getInstrInfo().isCall(MInst->getOpCode()); |
| 229 | |
Ruchira Sasanka | 958faf3 | 2001-10-19 17:21:03 +0000 | [diff] [blame] | 230 | if( isCallInst ) { |
| 231 | //cout << "\nFor call inst: " << *MInst; |
| 232 | |
| 233 | // set the isCallInterference flag of each live range wich extends |
| 234 | // accross this call instruction. This information is used by graph |
| 235 | // coloring algo to avoid allocating volatile colors to live ranges |
| 236 | // that span across calls (since they have to be saved/restored) |
| 237 | setCallInterferences( MInst, LVSetAI); |
| 238 | } |
| 239 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 240 | |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 241 | // iterate over MI operands to find defs |
| 242 | for( MachineInstr::val_op_const_iterator OpI(MInst);!OpI.done(); ++OpI) { |
| 243 | |
| 244 | if( OpI.isDef() ) { |
| 245 | // create a new LR iff this operand is a def |
| 246 | addInterference(*OpI, LVSetAI, isCallInst ); |
| 247 | |
| 248 | } //if this is a def |
| 249 | |
| 250 | } // for all operands |
| 251 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 252 | |
| 253 | // Also add interference for any implicit definitions in a machine |
| 254 | // instr (currently, only calls have this). |
| 255 | |
| 256 | unsigned NumOfImpRefs = MInst->getNumImplicitRefs(); |
| 257 | if( NumOfImpRefs > 0 ) { |
| 258 | for(unsigned z=0; z < NumOfImpRefs; z++) |
| 259 | if( MInst->implicitRefIsDefined(z) ) |
| 260 | addInterference( MInst->getImplicitRef(z), LVSetAI, isCallInst ); |
| 261 | } |
| 262 | |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 263 | } // for all machine instructions in BB |
| 264 | |
| 265 | |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 266 | #if 0 |
| 267 | |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 268 | // go thru LLVM instructions in the basic block and record all CALL |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 269 | // instructions and Return instructions in the CallInstrList |
| 270 | // This is done because since there are no reverse pointers in machine |
| 271 | // instructions to find the llvm instruction, when we encounter a call |
| 272 | // 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] | 273 | BasicBlock::const_iterator InstIt = (*BBI)->begin(); |
| 274 | |
| 275 | for( ; InstIt != (*BBI)->end() ; ++ InstIt) { |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 276 | unsigned OpCode = (*InstIt)->getOpcode(); |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 277 | |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 278 | if( OpCode == Instruction::Call ) |
| 279 | CallInstrList.push_back( *InstIt ); |
| 280 | |
| 281 | else if( OpCode == Instruction::Ret ) |
| 282 | RetInstrList.push_back( *InstIt ); |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 283 | } |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 284 | |
| 285 | #endif |
| 286 | |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 287 | |
| 288 | } // for all BBs in method |
| 289 | |
| 290 | |
| 291 | // add interferences for method arguments. Since there are no explict |
| 292 | // defs in method for args, we have to add them manually |
| 293 | |
| 294 | addInterferencesForArgs(); // add interference for method args |
| 295 | |
| 296 | if( DEBUG_RA) |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 297 | cout << "Interference graphs calculted!" << endl; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 298 | |
| 299 | } |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 304 | //---------------------------------------------------------------------------- |
| 305 | // This method will add interferences for incoming arguments to a method. |
| 306 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 307 | void PhyRegAlloc::addInterferencesForArgs() |
| 308 | { |
| 309 | // get the InSet of root BB |
| 310 | const LiveVarSet *const InSet = LVI->getInSetOfBB( Meth->front() ); |
| 311 | |
| 312 | // get the argument list |
| 313 | const Method::ArgumentListType& ArgList = Meth->getArgumentList(); |
| 314 | |
| 315 | // get an iterator to arg list |
| 316 | Method::ArgumentListType::const_iterator ArgIt = ArgList.begin(); |
| 317 | |
| 318 | |
| 319 | for( ; ArgIt != ArgList.end() ; ++ArgIt) { // for each argument |
| 320 | addInterference( *ArgIt, InSet, false ); // add interferences between |
| 321 | // args and LVars at start |
| 322 | if( DEBUG_RA > 1) { |
Ruchira Sasanka | 97b8b44 | 2001-10-18 22:36:26 +0000 | [diff] [blame] | 323 | cout << " - %% adding interference for argument "; |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 324 | printValue( (const Value *) *ArgIt); cout << endl; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 325 | } |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 330 | |
| 331 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 332 | // This method inserts caller saving/restoring instructons before/after |
| 333 | // a call machine instruction. |
| 334 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 335 | |
| 336 | |
| 337 | void PhyRegAlloc::insertCallerSavingCode(const MachineInstr *MInst, |
| 338 | const BasicBlock *BB ) |
| 339 | { |
Ruchira Sasanka | 958faf3 | 2001-10-19 17:21:03 +0000 | [diff] [blame] | 340 | // assert( (TM.getInstrInfo()).isCall( MInst->getOpCode() ) ); |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 341 | |
Ruchira Sasanka | 47c1372 | 2001-10-16 01:33:55 +0000 | [diff] [blame] | 342 | int StackOff = -8; // ****TODO : Change |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 343 | hash_set<unsigned> PushedRegSet; |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 344 | |
| 345 | // Now find the LR of the return value of the call |
| 346 | // The last *implicit operand* is the return value of a call |
| 347 | // Insert it to to he PushedRegSet since we must not save that register |
| 348 | // and restore it after the call. |
| 349 | // We do this because, we look at the LV set *after* the instruction |
| 350 | // to determine, which LRs must be saved across calls. The return value |
| 351 | // of the call is live in this set - but we must not save/restore it. |
| 352 | |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 353 | |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame^] | 354 | const Value *RetVal = MRI.getCallInstRetVal( MInst ); |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 355 | |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame^] | 356 | if( RetVal ) { |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 357 | |
Ruchira Sasanka | b3b6f53 | 2001-10-21 16:43:41 +0000 | [diff] [blame^] | 358 | LiveRange *RetValLR = LRI.getLiveRangeForValue( RetVal ); |
| 359 | assert( RetValLR && "No LR for RetValue of call"); |
| 360 | |
| 361 | PushedRegSet.insert( |
| 362 | MRI.getUnifiedRegNum((RetValLR->getRegClass())->getID(), |
| 363 | RetValLR->getColor() ) ); |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 367 | const LiveVarSet *LVSetAft = LVI->getLiveVarSetAfterMInst(MInst, BB); |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 368 | |
| 369 | LiveVarSet::const_iterator LIt = LVSetAft->begin(); |
| 370 | |
| 371 | // for each live var in live variable set after machine inst |
| 372 | for( ; LIt != LVSetAft->end(); ++LIt) { |
| 373 | |
| 374 | // get the live range corresponding to live var |
| 375 | LiveRange *const LR = LRI.getLiveRangeForValue(*LIt ); |
| 376 | |
Ruchira Sasanka | 958faf3 | 2001-10-19 17:21:03 +0000 | [diff] [blame] | 377 | // LR can be null if it is a const since a const |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 378 | // doesn't have a dominating def - see Assumptions above |
| 379 | if( LR ) { |
| 380 | |
| 381 | if( LR->hasColor() ) { |
| 382 | |
| 383 | unsigned RCID = (LR->getRegClass())->getID(); |
| 384 | unsigned Color = LR->getColor(); |
| 385 | |
| 386 | if ( MRI.isRegVolatile(RCID, Color) ) { |
| 387 | |
| 388 | // if the value is in both LV sets (i.e., live before and after |
| 389 | // the call machine instruction) |
Ruchira Sasanka | 97b8b44 | 2001-10-18 22:36:26 +0000 | [diff] [blame] | 390 | |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 391 | unsigned Reg = MRI.getUnifiedRegNum(RCID, Color); |
| 392 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 393 | if( PushedRegSet.find(Reg) == PushedRegSet.end() ) { |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 394 | |
| 395 | // if we haven't already pushed that register |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 396 | |
| 397 | unsigned RegType = MRI.getRegType( LR ); |
| 398 | |
| 399 | // Now get two instructions - to push on stack and pop from stack |
| 400 | // and add them to InstrnsBefore and InstrnsAfter of the |
| 401 | // call instruction |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 402 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 403 | MachineInstr *AdIBef = |
| 404 | MRI.cpReg2MemMI(Reg, MRI.getFramePointer(), StackOff, RegType ); |
| 405 | |
| 406 | MachineInstr *AdIAft = |
| 407 | MRI.cpMem2RegMI(MRI.getFramePointer(), StackOff, Reg, RegType ); |
| 408 | |
| 409 | ((AddedInstrMap[MInst])->InstrnsBefore).push_front(AdIBef); |
| 410 | ((AddedInstrMap[MInst])->InstrnsAfter).push_back(AdIAft); |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 411 | |
| 412 | PushedRegSet.insert( Reg ); |
Ruchira Sasanka | 47c1372 | 2001-10-16 01:33:55 +0000 | [diff] [blame] | 413 | StackOff -= 8; // ****TODO: Correct ?????? |
Ruchira Sasanka | 97b8b44 | 2001-10-18 22:36:26 +0000 | [diff] [blame] | 414 | |
| 415 | if(DEBUG_RA) { |
Ruchira Sasanka | 958faf3 | 2001-10-19 17:21:03 +0000 | [diff] [blame] | 416 | cerr << "\nFor callee save call inst:" << *MInst; |
Ruchira Sasanka | 97b8b44 | 2001-10-18 22:36:26 +0000 | [diff] [blame] | 417 | cerr << "\n -inserted caller saving instrs:\n\t "; |
| 418 | cerr << *AdIBef << "\n\t" << *AdIAft ; |
| 419 | } |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 420 | } // if not already pushed |
| 421 | |
| 422 | } // if LR has a volatile color |
| 423 | |
| 424 | } // if LR has color |
| 425 | |
| 426 | } // if there is a LR for Var |
| 427 | |
| 428 | } // for each value in the LV set after instruction |
| 429 | |
| 430 | } |
| 431 | |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 432 | |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 433 | //---------------------------------------------------------------------------- |
| 434 | // This method is called after register allocation is complete to set the |
| 435 | // allocated reisters in the machine code. This code will add register numbers |
| 436 | // to MachineOperands that contain a Value. |
| 437 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 438 | |
| 439 | void PhyRegAlloc::updateMachineCode() |
| 440 | { |
| 441 | |
| 442 | Method::const_iterator BBI = Meth->begin(); // random iterator for BBs |
| 443 | |
| 444 | for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order |
| 445 | |
Ruchira Sasanka | 0931a01 | 2001-09-15 19:06:58 +0000 | [diff] [blame] | 446 | // get the iterator for machine instructions |
| 447 | MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec(); |
| 448 | MachineCodeForBasicBlock::iterator MInstIterator = MIVec.begin(); |
| 449 | |
| 450 | // iterate over all the machine instructions in BB |
| 451 | for( ; MInstIterator != MIVec.end(); ++MInstIterator) { |
| 452 | |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 453 | MachineInstr *MInst = *MInstIterator; |
| 454 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 455 | // if this machine instr is call, insert caller saving code |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 456 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 457 | if( (TM.getInstrInfo()).isCall( MInst->getOpCode()) ) |
| 458 | insertCallerSavingCode(MInst, *BBI ); |
| 459 | |
| 460 | // If there are instructions to be added, *before* this machine |
| 461 | // instruction, add them now. |
| 462 | |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 463 | if( AddedInstrMap[ MInst ] ) { |
| 464 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 465 | deque<MachineInstr *> &IBef = (AddedInstrMap[MInst])->InstrnsBefore; |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 466 | |
| 467 | if( ! IBef.empty() ) { |
| 468 | |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 469 | deque<MachineInstr *>::iterator AdIt; |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 470 | |
| 471 | for( AdIt = IBef.begin(); AdIt != IBef.end() ; ++AdIt ) { |
| 472 | |
Ruchira Sasanka | 97b8b44 | 2001-10-18 22:36:26 +0000 | [diff] [blame] | 473 | if( DEBUG_RA) |
| 474 | cerr << " *$* PREPENDed instr " << *AdIt << endl; |
| 475 | |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 476 | MInstIterator = MIVec.insert( MInstIterator, *AdIt ); |
| 477 | ++MInstIterator; |
| 478 | } |
| 479 | |
| 480 | } |
| 481 | |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | |
Ruchira Sasanka | 0931a01 | 2001-09-15 19:06:58 +0000 | [diff] [blame] | 485 | |
| 486 | //for(MachineInstr::val_op_const_iterator OpI(MInst);!OpI.done();++OpI) { |
| 487 | |
| 488 | for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) { |
| 489 | |
| 490 | MachineOperand& Op = MInst->getOperand(OpNum); |
| 491 | |
| 492 | if( Op.getOperandType() == MachineOperand::MO_VirtualRegister || |
| 493 | Op.getOperandType() == MachineOperand::MO_CCRegister) { |
| 494 | |
| 495 | const Value *const Val = Op.getVRegValue(); |
| 496 | |
| 497 | // delete this condition checking later (must assert if Val is null) |
Chris Lattner | 045e7c8 | 2001-09-19 16:26:23 +0000 | [diff] [blame] | 498 | if( !Val) { |
| 499 | if (DEBUG_RA) |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 500 | cout << "Warning: NULL Value found for operand" << endl; |
Ruchira Sasanka | 0931a01 | 2001-09-15 19:06:58 +0000 | [diff] [blame] | 501 | continue; |
| 502 | } |
| 503 | assert( Val && "Value is NULL"); |
| 504 | |
| 505 | const LiveRange *const LR = LRI.getLiveRangeForValue(Val); |
| 506 | |
| 507 | if ( !LR ) { |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 508 | |
| 509 | // nothing to worry if it's a const or a label |
| 510 | |
Chris Lattner | 4c3aaa4 | 2001-09-19 16:09:04 +0000 | [diff] [blame] | 511 | if (DEBUG_RA) { |
Ruchira Sasanka | 1b732fd | 2001-10-16 16:34:44 +0000 | [diff] [blame] | 512 | cout << "*NO LR for operand : " << Op ; |
| 513 | cout << " [reg:" << Op.getAllocatedRegNum() << "]"; |
| 514 | cout << " in inst:\t" << *MInst << endl; |
Chris Lattner | 4c3aaa4 | 2001-09-19 16:09:04 +0000 | [diff] [blame] | 515 | } |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 516 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 517 | // if register is not allocated, mark register as invalid |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 518 | if( Op.getAllocatedRegNum() == -1) |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 519 | Op.setRegForValue( MRI.getInvalidRegNum()); |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 520 | |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 521 | #if 0 |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 522 | if( ((Val->getType())->isLabelType()) || |
| 523 | (Val->getValueType() == Value::ConstantVal) ) |
| 524 | ; // do nothing |
| 525 | |
| 526 | // The return address is not explicitly defined within a |
| 527 | // method. So, it is not colored by usual algorithm. In that case |
| 528 | // color it here. |
| 529 | |
| 530 | //else if (TM.getInstrInfo().isCall(MInst->getOpCode())) |
| 531 | //Op.setRegForValue( MRI.getCallAddressReg() ); |
| 532 | |
| 533 | //TM.getInstrInfo().isReturn(MInst->getOpCode()) |
| 534 | else if(TM.getInstrInfo().isReturn(MInst->getOpCode()) ) { |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 535 | if (DEBUG_RA) cout << endl << "RETURN found" << endl; |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 536 | Op.setRegForValue( MRI.getReturnAddressReg() ); |
| 537 | |
| 538 | } |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 539 | |
| 540 | if (Val->getValueType() == Value::InstructionVal) |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 541 | { |
Ruchira Sasanka | 1b732fd | 2001-10-16 16:34:44 +0000 | [diff] [blame] | 542 | if( DEBUG_RA ) { |
| 543 | cout << "!Warning: No LiveRange for: "; |
| 544 | printValue( Val); cout << " Type: " << Val->getValueType(); |
| 545 | cout << " RegVal=" << Op.getAllocatedRegNum() << endl; |
| 546 | } |
Ruchira Sasanka | 0931a01 | 2001-09-15 19:06:58 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 549 | #endif |
| 550 | |
Ruchira Sasanka | 0931a01 | 2001-09-15 19:06:58 +0000 | [diff] [blame] | 551 | continue; |
| 552 | } |
| 553 | |
| 554 | unsigned RCID = (LR->getRegClass())->getID(); |
| 555 | |
| 556 | Op.setRegForValue( MRI.getUnifiedRegNum(RCID, LR->getColor()) ); |
| 557 | |
| 558 | int RegNum = MRI.getUnifiedRegNum(RCID, LR->getColor()); |
| 559 | |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 560 | } |
| 561 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 562 | } // for each operand |
| 563 | |
| 564 | |
| 565 | // If there are instructions to be added *after* this machine |
| 566 | // instruction, add them now |
| 567 | |
| 568 | if( AddedInstrMap[ MInst ] ) { |
| 569 | |
| 570 | deque<MachineInstr *> &IAft = (AddedInstrMap[MInst])->InstrnsAfter; |
| 571 | |
| 572 | if( ! IAft.empty() ) { |
| 573 | |
| 574 | deque<MachineInstr *>::iterator AdIt; |
| 575 | |
| 576 | ++MInstIterator; // advance to the next instruction |
| 577 | |
| 578 | for( AdIt = IAft.begin(); AdIt != IAft.end() ; ++AdIt ) { |
| 579 | |
Ruchira Sasanka | 97b8b44 | 2001-10-18 22:36:26 +0000 | [diff] [blame] | 580 | if(DEBUG_RA) |
| 581 | cerr << " *#* APPENDed instr opcode: " << *AdIt << endl; |
| 582 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 583 | MInstIterator = MIVec.insert( MInstIterator, *AdIt ); |
| 584 | ++MInstIterator; |
| 585 | } |
| 586 | |
| 587 | // MInsterator already points to the next instr. Since the |
| 588 | // for loop also increments it, decrement it to point to the |
| 589 | // instruction added last |
| 590 | --MInstIterator; |
| 591 | |
| 592 | } |
| 593 | |
Ruchira Sasanka | 0931a01 | 2001-09-15 19:06:58 +0000 | [diff] [blame] | 594 | } |
| 595 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 596 | } // for each machine instruction |
Ruchira Sasanka | 0931a01 | 2001-09-15 19:06:58 +0000 | [diff] [blame] | 597 | } |
| 598 | } |
| 599 | |
| 600 | |
| 601 | |
| 602 | |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 603 | //---------------------------------------------------------------------------- |
| 604 | // This method prints the code with registers after register allocation is |
| 605 | // complete. |
| 606 | //---------------------------------------------------------------------------- |
| 607 | void PhyRegAlloc::printMachineCode() |
| 608 | { |
| 609 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 610 | cout << endl << ";************** Method "; |
| 611 | cout << Meth->getName() << " *****************" << endl; |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 612 | |
| 613 | Method::const_iterator BBI = Meth->begin(); // random iterator for BBs |
| 614 | |
| 615 | for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order |
| 616 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 617 | cout << endl ; printLabel( *BBI); cout << ": "; |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 618 | |
| 619 | // get the iterator for machine instructions |
| 620 | MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec(); |
| 621 | MachineCodeForBasicBlock::iterator MInstIterator = MIVec.begin(); |
| 622 | |
| 623 | // iterate over all the machine instructions in BB |
| 624 | for( ; MInstIterator != MIVec.end(); ++MInstIterator) { |
| 625 | |
| 626 | MachineInstr *const MInst = *MInstIterator; |
| 627 | |
| 628 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 629 | cout << endl << "\t"; |
| 630 | cout << TargetInstrDescriptors[MInst->getOpCode()].opCodeString; |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 631 | |
| 632 | |
| 633 | //for(MachineInstr::val_op_const_iterator OpI(MInst);!OpI.done();++OpI) { |
| 634 | |
| 635 | for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) { |
| 636 | |
| 637 | MachineOperand& Op = MInst->getOperand(OpNum); |
| 638 | |
| 639 | if( Op.getOperandType() == MachineOperand::MO_VirtualRegister || |
Ruchira Sasanka | 97b8b44 | 2001-10-18 22:36:26 +0000 | [diff] [blame] | 640 | Op.getOperandType() == MachineOperand::MO_CCRegister /*|| |
| 641 | Op.getOperandType() == MachineOperand::MO_PCRelativeDisp*/ ) { |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 642 | |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 643 | const Value *const Val = Op.getVRegValue () ; |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 644 | // ****this code is temporary till NULL Values are fixed |
| 645 | if( ! Val ) { |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 646 | cout << "\t<*NULL*>"; |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 647 | continue; |
| 648 | } |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 649 | |
| 650 | // if a label or a constant |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 651 | if( (Val->getValueType() == Value::BasicBlockVal) ) { |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 652 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 653 | cout << "\t"; printLabel( Op.getVRegValue () ); |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 654 | } |
| 655 | else { |
| 656 | // else it must be a register value |
| 657 | const int RegNum = Op.getAllocatedRegNum(); |
| 658 | |
Ruchira Sasanka | 0e62aa6 | 2001-10-19 21:39:31 +0000 | [diff] [blame] | 659 | cout << "\t" << "%" << MRI.getUnifiedRegName( RegNum ); |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | } |
| 663 | else if(Op.getOperandType() == MachineOperand::MO_MachineRegister) { |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 664 | cout << "\t" << "%" << MRI.getUnifiedRegName(Op.getMachineRegNum()); |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | else |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 668 | cout << "\t" << Op; // use dump field |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 669 | } |
| 670 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 671 | |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 672 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 673 | unsigned NumOfImpRefs = MInst->getNumImplicitRefs(); |
| 674 | if( NumOfImpRefs > 0 ) { |
| 675 | |
| 676 | cout << "\tImplicit:"; |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 677 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 678 | for(unsigned z=0; z < NumOfImpRefs; z++) { |
| 679 | printValue( MInst->getImplicitRef(z) ); |
| 680 | cout << "\t"; |
| 681 | } |
| 682 | |
| 683 | } |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 684 | |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 685 | } // for all machine instructions |
| 686 | |
| 687 | |
| 688 | cout << endl; |
| 689 | |
| 690 | } // for all BBs |
| 691 | |
| 692 | cout << endl; |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 693 | } |
| 694 | |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 695 | |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 696 | //---------------------------------------------------------------------------- |
| 697 | // |
| 698 | //---------------------------------------------------------------------------- |
| 699 | |
| 700 | void PhyRegAlloc::colorCallRetArgs() |
| 701 | { |
| 702 | |
| 703 | CallRetInstrListType &CallRetInstList = LRI.getCallRetInstrList(); |
| 704 | CallRetInstrListType::const_iterator It = CallRetInstList.begin(); |
| 705 | |
| 706 | for( ; It != CallRetInstList.end(); ++It ) { |
| 707 | |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 708 | const MachineInstr *const CRMI = *It; |
| 709 | unsigned OpCode = CRMI->getOpCode(); |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 710 | |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 711 | // get the added instructions for this Call/Ret instruciton |
| 712 | AddedInstrns *AI = AddedInstrMap[ CRMI ]; |
| 713 | if ( !AI ) { |
| 714 | AI = new AddedInstrns(); |
| 715 | AddedInstrMap[ CRMI ] = AI; |
| 716 | } |
| 717 | |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 718 | if( (TM.getInstrInfo()).isCall( OpCode ) ) |
| 719 | MRI.colorCallArgs( CRMI, LRI, AI ); |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 720 | |
Ruchira Sasanka | a90e770 | 2001-10-15 16:26:38 +0000 | [diff] [blame] | 721 | else if ( (TM.getInstrInfo()).isReturn(OpCode) ) |
| 722 | MRI.colorRetValue( CRMI, LRI, AI ); |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 723 | |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 724 | else assert( 0 && "Non Call/Ret instrn in CallRetInstrList\n" ); |
| 725 | |
| 726 | } |
| 727 | |
| 728 | } |
| 729 | |
Ruchira Sasanka | 0e62aa6 | 2001-10-19 21:39:31 +0000 | [diff] [blame] | 730 | |
| 731 | |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 732 | //---------------------------------------------------------------------------- |
| 733 | |
| 734 | //---------------------------------------------------------------------------- |
| 735 | void PhyRegAlloc::colorIncomingArgs() |
| 736 | { |
| 737 | const BasicBlock *const FirstBB = Meth->front(); |
| 738 | const MachineInstr *FirstMI = *((FirstBB->getMachineInstrVec()).begin()); |
| 739 | assert( FirstMI && "No machine instruction in entry BB"); |
| 740 | |
| 741 | AddedInstrns *AI = AddedInstrMap[ FirstMI ]; |
| 742 | if ( !AI ) { |
| 743 | AI = new AddedInstrns(); |
| 744 | AddedInstrMap[ FirstMI ] = AI; |
| 745 | } |
| 746 | |
| 747 | MRI.colorMethodArgs(Meth, LRI, AI ); |
| 748 | } |
| 749 | |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 750 | |
| 751 | //---------------------------------------------------------------------------- |
| 752 | // Used to generate a label for a basic block |
| 753 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 754 | void PhyRegAlloc::printLabel(const Value *const Val) |
| 755 | { |
| 756 | if( Val->hasName() ) |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 757 | cout << Val->getName(); |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 758 | else |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 759 | cout << "Label" << Val; |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 763 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | 0e62aa6 | 2001-10-19 21:39:31 +0000 | [diff] [blame] | 764 | // This method calls setSugColorUsable method of each live range. This |
| 765 | // will determine whether the suggested color of LR is really usable. |
| 766 | // A suggested color is not usable when the suggested color is volatile |
| 767 | // AND when there are call interferences |
| 768 | //---------------------------------------------------------------------------- |
| 769 | |
| 770 | void PhyRegAlloc::markUnusableSugColors() |
| 771 | { |
| 772 | if(DEBUG_RA ) cout << "Creating LR lists ..." << endl; |
| 773 | |
| 774 | // hash map iterator |
| 775 | LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin(); |
| 776 | LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end(); |
| 777 | |
| 778 | for( ; HMI != HMIEnd ; ++HMI ) { |
| 779 | |
| 780 | if( (*HMI).first ) { |
| 781 | |
| 782 | LiveRange *L = (*HMI).second; // get the LiveRange |
| 783 | |
| 784 | if(L) { |
| 785 | if( L->hasSuggestedColor() ) { |
| 786 | |
| 787 | int RCID = (L->getRegClass())->getID(); |
| 788 | if( MRI.isRegVolatile( RCID, L->getSuggestedColor()) && |
| 789 | L->isCallInterference() ) |
| 790 | L->setSuggestedColorUsable( false ); |
| 791 | else |
| 792 | L->setSuggestedColorUsable( true ); |
| 793 | } |
| 794 | } // if L->hasSuggestedColor() |
| 795 | } |
| 796 | } // for all LR's in hash map |
| 797 | } |
| 798 | |
| 799 | |
| 800 | |
| 801 | |
| 802 | |
| 803 | |
| 804 | |
| 805 | |
| 806 | |
| 807 | |
| 808 | |
| 809 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 810 | // The entry pont to Register Allocation |
| 811 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 812 | |
| 813 | void PhyRegAlloc::allocateRegisters() |
| 814 | { |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 815 | |
| 816 | // make sure that we put all register classes into the RegClassList |
| 817 | // before we call constructLiveRanges (now done in the constructor of |
| 818 | // PhyRegAlloc class). |
| 819 | |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 820 | constructLiveRanges(); // create LR info |
| 821 | |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 822 | if( DEBUG_RA ) |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 823 | LRI.printLiveRanges(); |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 824 | |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 825 | createIGNodeListsAndIGs(); // create IGNode list and IGs |
| 826 | |
| 827 | buildInterferenceGraphs(); // build IGs in all reg classes |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 828 | |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 829 | |
| 830 | if( DEBUG_RA ) { |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 831 | // print all LRs in all reg classes |
| 832 | for( unsigned int rc=0; rc < NumOfRegClasses ; rc++) |
| 833 | RegClassList[ rc ]->printIGNodeList(); |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 834 | |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 835 | // print IGs in all register classes |
| 836 | for( unsigned int rc=0; rc < NumOfRegClasses ; rc++) |
| 837 | RegClassList[ rc ]->printIG(); |
| 838 | } |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 839 | |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 840 | LRI.coalesceLRs(); // coalesce all live ranges |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 841 | |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 842 | if( DEBUG_RA) { |
| 843 | // print all LRs in all reg classes |
| 844 | for( unsigned int rc=0; rc < NumOfRegClasses ; rc++) |
| 845 | RegClassList[ rc ]->printIGNodeList(); |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 846 | |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 847 | // print IGs in all register classes |
| 848 | for( unsigned int rc=0; rc < NumOfRegClasses ; rc++) |
| 849 | RegClassList[ rc ]->printIG(); |
| 850 | } |
| 851 | |
Ruchira Sasanka | 0e62aa6 | 2001-10-19 21:39:31 +0000 | [diff] [blame] | 852 | |
| 853 | // mark un-usable suggested color before graph coloring algorithm. |
| 854 | // When this is done, the graph coloring algo will not reserve |
| 855 | // suggested color unnecessarily - they can be used by another LR |
| 856 | markUnusableSugColors(); |
| 857 | |
| 858 | // color all register classes using the graph coloring algo |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 859 | for( unsigned int rc=0; rc < NumOfRegClasses ; rc++) |
| 860 | RegClassList[ rc ]->colorAllRegs(); |
| 861 | |
Ruchira Sasanka | a5ab964 | 2001-09-30 23:11:59 +0000 | [diff] [blame] | 862 | |
| 863 | // color incoming args and call args |
| 864 | colorIncomingArgs(); |
| 865 | colorCallRetArgs(); |
| 866 | |
Ruchira Sasanka | 97b8b44 | 2001-10-18 22:36:26 +0000 | [diff] [blame] | 867 | |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 868 | updateMachineCode(); |
Chris Lattner | 045e7c8 | 2001-09-19 16:26:23 +0000 | [diff] [blame] | 869 | if (DEBUG_RA) { |
Ruchira Sasanka | 1b732fd | 2001-10-16 16:34:44 +0000 | [diff] [blame] | 870 | PrintMachineInstructions(Meth); |
Chris Lattner | 045e7c8 | 2001-09-19 16:26:23 +0000 | [diff] [blame] | 871 | printMachineCode(); // only for DEBUGGING |
| 872 | } |
Ruchira Sasanka | 6b0a8b5 | 2001-09-15 21:11:11 +0000 | [diff] [blame] | 873 | } |
| 874 | |
Ruchira Sasanka | e727f85 | 2001-09-18 22:43:57 +0000 | [diff] [blame] | 875 | |
| 876 | |
| 877 | |