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