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