Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 1 | #include "llvm/CodeGen/PhyRegAlloc.h" |
| 2 | |
| 3 | |
| 4 | PhyRegAlloc::PhyRegAlloc(const Method *const M, |
| 5 | const TargetMachine& tm, |
| 6 | MethodLiveVarInfo *const Lvi) |
| 7 | : RegClassList(), |
| 8 | Meth(M), TM(tm), LVI(Lvi), LRI(M, tm, RegClassList), |
| 9 | MRI( tm.getRegInfo() ), |
| 10 | NumOfRegClasses(MRI.getNumOfRegClasses()), |
| 11 | CallInstrList(), |
| 12 | AddedInstrMap() |
| 13 | |
| 14 | { |
| 15 | // **TODO: use an actual reserved color list |
| 16 | ReservedColorListType *RCL = new ReservedColorListType(); |
| 17 | |
| 18 | // create each RegisterClass and put in RegClassList |
| 19 | for( unsigned int rc=0; rc < NumOfRegClasses; rc++) |
| 20 | RegClassList.push_back( new RegClass(M, MRI.getMachineRegClass(rc), RCL) ); |
| 21 | |
| 22 | } |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | void PhyRegAlloc::createIGNodeListsAndIGs() |
| 30 | { |
Ruchira Sasanka | 0931a01 | 2001-09-15 19:06:58 +0000 | [diff] [blame] | 31 | if(DEBUG_RA ) cout << "Creating LR lists ..." << endl; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 32 | |
| 33 | // hash map iterator |
| 34 | LiveRangeMapType::const_iterator HMI = (LRI.getLiveRangeMap())->begin(); |
| 35 | |
| 36 | // hash map end |
| 37 | LiveRangeMapType::const_iterator HMIEnd = (LRI.getLiveRangeMap())->end(); |
| 38 | |
| 39 | for( ; HMI != HMIEnd ; ++HMI ) { |
| 40 | |
| 41 | LiveRange *L = (*HMI).second; // get the LiveRange |
| 42 | |
| 43 | if( (*HMI).first ) { |
| 44 | // if the Value * is not null, and LR |
| 45 | // is not yet written to the IGNodeList |
| 46 | if( !(L->getUserIGNode()) ) { |
| 47 | |
| 48 | RegClass *const RC = // RegClass of first value in the LR |
| 49 | //RegClassList [MRI.getRegClassIDOfValue(*(L->begin()))]; |
| 50 | RegClassList[ L->getRegClass()->getID() ]; |
| 51 | |
| 52 | RC-> addLRToIG( L ); // add this LR to an IG |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | // init RegClassList |
| 58 | for( unsigned int rc=0; rc < NumOfRegClasses ; rc++) |
| 59 | RegClassList[ rc ]->createInterferenceGraph(); |
| 60 | |
| 61 | if( DEBUG_RA) |
| 62 | cout << "LRLists Created!" << endl; |
| 63 | } |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | // Interence occurs only if the LR of Def (Inst or Arg) is of the same reg |
| 69 | // class as that of live var. The live var passed to this function is the |
| 70 | // LVset AFTER the instruction |
| 71 | |
| 72 | |
| 73 | void PhyRegAlloc::addInterference(const Value *const Def, |
| 74 | const LiveVarSet *const LVSet, |
| 75 | const bool isCallInst) { |
| 76 | |
| 77 | LiveVarSet::const_iterator LIt = LVSet->begin(); |
| 78 | |
| 79 | // get the live range of instruction |
| 80 | const LiveRange *const LROfDef = LRI.getLiveRangeForValue( Def ); |
| 81 | |
| 82 | IGNode *const IGNodeOfDef = LROfDef->getUserIGNode(); |
| 83 | assert( IGNodeOfDef ); |
| 84 | |
| 85 | RegClass *const RCOfDef = LROfDef->getRegClass(); |
| 86 | |
| 87 | // for each live var in live variable set |
| 88 | for( ; LIt != LVSet->end(); ++LIt) { |
| 89 | |
| 90 | if( DEBUG_RA > 1) { |
| 91 | cout << "< Def="; printValue(Def); |
| 92 | cout << ", Lvar="; printValue( *LIt); cout << "> "; |
| 93 | } |
| 94 | |
| 95 | // get the live range corresponding to live var |
| 96 | LiveRange *const LROfVar = LRI.getLiveRangeForValue(*LIt ); |
| 97 | |
| 98 | // LROfVar can be null if it is a const since a const |
| 99 | // doesn't have a dominating def - see Assumptions above |
| 100 | if( LROfVar) { |
| 101 | |
| 102 | if(LROfDef == LROfVar) // do not set interf for same LR |
| 103 | continue; |
| 104 | |
| 105 | // if 2 reg classes are the same set interference |
| 106 | if( RCOfDef == LROfVar->getRegClass() ){ |
| 107 | RCOfDef->setInterference( LROfDef, LROfVar); |
| 108 | |
| 109 | } |
| 110 | |
| 111 | //the live range of this var interferes with this call |
| 112 | if( isCallInst ) |
| 113 | LROfVar->addCallInterference( (const Instruction *const) Def ); |
| 114 | |
| 115 | } |
Ruchira Sasanka | 0931a01 | 2001-09-15 19:06:58 +0000 | [diff] [blame] | 116 | else if(DEBUG_RA > 1) { |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 117 | // we will not have LRs for values not explicitly allocated in the |
| 118 | // instruction stream (e.g., constants) |
| 119 | cout << " warning: no live range for " ; |
| 120 | printValue( *LIt); cout << endl; } |
| 121 | |
| 122 | } |
| 123 | |
| 124 | } |
| 125 | |
| 126 | |
| 127 | |
| 128 | void PhyRegAlloc::buildInterferenceGraphs() |
| 129 | { |
| 130 | |
| 131 | if(DEBUG_RA) cout << "Creating interference graphs ..." << endl; |
| 132 | |
| 133 | Method::const_iterator BBI = Meth->begin(); // random iterator for BBs |
| 134 | |
| 135 | for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order |
| 136 | |
| 137 | // get the iterator for machine instructions |
| 138 | const MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec(); |
| 139 | MachineCodeForBasicBlock::const_iterator |
| 140 | MInstIterator = MIVec.begin(); |
| 141 | |
| 142 | // iterate over all the machine instructions in BB |
| 143 | for( ; MInstIterator != MIVec.end(); ++MInstIterator) { |
| 144 | |
| 145 | const MachineInstr *const MInst = *MInstIterator; |
| 146 | |
| 147 | // get the LV set after the instruction |
| 148 | const LiveVarSet *const LVSetAI = |
| 149 | LVI->getLiveVarSetAfterMInst(MInst, *BBI); |
| 150 | |
| 151 | const bool isCallInst = TM.getInstrInfo().isCall(MInst->getOpCode()); |
| 152 | |
| 153 | // iterate over MI operands to find defs |
| 154 | for( MachineInstr::val_op_const_iterator OpI(MInst);!OpI.done(); ++OpI) { |
| 155 | |
| 156 | if( OpI.isDef() ) { |
| 157 | // create a new LR iff this operand is a def |
| 158 | addInterference(*OpI, LVSetAI, isCallInst ); |
| 159 | |
| 160 | } //if this is a def |
| 161 | |
| 162 | } // for all operands |
| 163 | |
| 164 | } // for all machine instructions in BB |
| 165 | |
| 166 | |
| 167 | // go thru LLVM instructions in the basic block and record all CALL |
| 168 | // instructions in the CallInstrList |
| 169 | BasicBlock::const_iterator InstIt = (*BBI)->begin(); |
| 170 | |
| 171 | for( ; InstIt != (*BBI)->end() ; ++ InstIt) { |
| 172 | |
| 173 | if( (*InstIt)->getOpcode() == Instruction::Call ) |
| 174 | CallInstrList.push_back( *InstIt ); |
| 175 | } |
| 176 | |
| 177 | } // for all BBs in method |
| 178 | |
| 179 | |
| 180 | // add interferences for method arguments. Since there are no explict |
| 181 | // defs in method for args, we have to add them manually |
| 182 | |
| 183 | addInterferencesForArgs(); // add interference for method args |
| 184 | |
| 185 | if( DEBUG_RA) |
| 186 | cout << "Interference graphs calculted!" << endl; |
| 187 | |
| 188 | } |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | void PhyRegAlloc::addInterferencesForArgs() |
| 194 | { |
| 195 | // get the InSet of root BB |
| 196 | const LiveVarSet *const InSet = LVI->getInSetOfBB( Meth->front() ); |
| 197 | |
| 198 | // get the argument list |
| 199 | const Method::ArgumentListType& ArgList = Meth->getArgumentList(); |
| 200 | |
| 201 | // get an iterator to arg list |
| 202 | Method::ArgumentListType::const_iterator ArgIt = ArgList.begin(); |
| 203 | |
| 204 | |
| 205 | for( ; ArgIt != ArgList.end() ; ++ArgIt) { // for each argument |
| 206 | addInterference( *ArgIt, InSet, false ); // add interferences between |
| 207 | // args and LVars at start |
| 208 | if( DEBUG_RA > 1) { |
| 209 | cout << " - %% adding interference for argument "; |
| 210 | printValue( (const Value *) *ArgIt); cout << endl; |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | |
| 216 | |
| 217 | void PhyRegAlloc::updateMachineCode() |
| 218 | { |
| 219 | |
| 220 | Method::const_iterator BBI = Meth->begin(); // random iterator for BBs |
| 221 | |
| 222 | for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order |
| 223 | |
Ruchira Sasanka | 0931a01 | 2001-09-15 19:06:58 +0000 | [diff] [blame] | 224 | // get the iterator for machine instructions |
| 225 | MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec(); |
| 226 | MachineCodeForBasicBlock::iterator MInstIterator = MIVec.begin(); |
| 227 | |
| 228 | // iterate over all the machine instructions in BB |
| 229 | for( ; MInstIterator != MIVec.end(); ++MInstIterator) { |
| 230 | |
| 231 | MachineInstr *const MInst = *MInstIterator; |
| 232 | |
| 233 | //for(MachineInstr::val_op_const_iterator OpI(MInst);!OpI.done();++OpI) { |
| 234 | |
| 235 | for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) { |
| 236 | |
| 237 | MachineOperand& Op = MInst->getOperand(OpNum); |
| 238 | |
| 239 | if( Op.getOperandType() == MachineOperand::MO_VirtualRegister || |
| 240 | Op.getOperandType() == MachineOperand::MO_CCRegister) { |
| 241 | |
| 242 | const Value *const Val = Op.getVRegValue(); |
| 243 | |
| 244 | // delete this condition checking later (must assert if Val is null) |
| 245 | if( !Val ) { |
| 246 | cout << "Error: NULL Value found in instr." << endl; |
| 247 | continue; |
| 248 | } |
| 249 | assert( Val && "Value is NULL"); |
| 250 | |
| 251 | const LiveRange *const LR = LRI.getLiveRangeForValue(Val); |
| 252 | |
| 253 | if ( !LR ) { |
| 254 | if( ! ( (Val->getType())->isLabelType() || |
| 255 | (Val->getValueType() == Value::ConstantVal) ) ) { |
| 256 | cout << "Warning: No LiveRange for: "; |
| 257 | printValue( Val); cout << endl; |
| 258 | } |
| 259 | |
| 260 | //assert( LR && "No LR found for Value"); |
| 261 | continue; |
| 262 | } |
| 263 | |
| 264 | unsigned RCID = (LR->getRegClass())->getID(); |
| 265 | |
| 266 | Op.setRegForValue( MRI.getUnifiedRegNum(RCID, LR->getColor()) ); |
| 267 | |
| 268 | int RegNum = MRI.getUnifiedRegNum(RCID, LR->getColor()); |
| 269 | |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | void PhyRegAlloc::printMachineCode() |
| 281 | { |
| 282 | |
| 283 | cout << endl << ";************** Method "; |
| 284 | cout << Meth->getName() << " *****************" << endl; |
| 285 | |
| 286 | Method::const_iterator BBI = Meth->begin(); // random iterator for BBs |
| 287 | |
| 288 | for( ; BBI != Meth->end(); ++BBI) { // traverse BBs in random order |
| 289 | |
| 290 | cout << endl ; printLabel( *BBI); cout << ": "; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 291 | |
| 292 | // get the iterator for machine instructions |
| 293 | MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec(); |
| 294 | MachineCodeForBasicBlock::iterator MInstIterator = MIVec.begin(); |
| 295 | |
| 296 | // iterate over all the machine instructions in BB |
| 297 | for( ; MInstIterator != MIVec.end(); ++MInstIterator) { |
| 298 | |
| 299 | MachineInstr *const MInst = *MInstIterator; |
| 300 | |
Ruchira Sasanka | 0931a01 | 2001-09-15 19:06:58 +0000 | [diff] [blame] | 301 | |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 302 | cout << endl << "\t"; |
| 303 | cout << TargetInstrDescriptors[MInst->getOpCode()].opCodeString; |
Ruchira Sasanka | 0931a01 | 2001-09-15 19:06:58 +0000 | [diff] [blame] | 304 | |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 305 | |
| 306 | //for(MachineInstr::val_op_const_iterator OpI(MInst);!OpI.done();++OpI) { |
| 307 | |
| 308 | for(unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) { |
| 309 | |
| 310 | MachineOperand& Op = MInst->getOperand(OpNum); |
| 311 | |
| 312 | if( Op.getOperandType() == MachineOperand::MO_VirtualRegister || |
| 313 | Op.getOperandType() == MachineOperand::MO_CCRegister) { |
| 314 | |
| 315 | const Value *const Val = Op.getVRegValue(); |
| 316 | |
| 317 | if( !Val ) { |
Ruchira Sasanka | 0931a01 | 2001-09-15 19:06:58 +0000 | [diff] [blame] | 318 | cout << "\t<*NULL Value*>"; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 319 | continue; |
| 320 | } |
| 321 | assert( Val && "Value is NULL"); |
| 322 | |
| 323 | const LiveRange *const LR = LRI.getLiveRangeForValue(Val); |
| 324 | |
| 325 | if ( !LR ) { |
Ruchira Sasanka | 0931a01 | 2001-09-15 19:06:58 +0000 | [diff] [blame] | 326 | |
| 327 | |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 328 | if( ! ( (Val->getType())->isLabelType() || |
| 329 | (Val->getValueType() == Value::ConstantVal) ) ) { |
| 330 | cout << "\t" << "<*No LiveRange for: "; |
| 331 | printValue( Val); cout << "*>"; |
| 332 | } |
| 333 | |
| 334 | |
| 335 | //assert( LR && "No LR found for Value"); |
| 336 | continue; |
| 337 | } |
| 338 | |
| 339 | unsigned RCID = (LR->getRegClass())->getID(); |
| 340 | |
| 341 | //cout << "Setting reg for value: "; printValue( Val ); |
| 342 | //cout << endl; |
| 343 | |
Ruchira Sasanka | 0931a01 | 2001-09-15 19:06:58 +0000 | [diff] [blame] | 344 | Op.setRegForValue( MRI.getUnifiedRegNum(RCID, LR->getColor()) ); |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 345 | |
| 346 | int RegNum = MRI.getUnifiedRegNum(RCID, LR->getColor()); |
| 347 | |
| 348 | cout << "\t" << "%" << MRI.getUnifiedRegName( RegNum ); |
| 349 | |
Ruchira Sasanka | 0931a01 | 2001-09-15 19:06:58 +0000 | [diff] [blame] | 350 | } |
| 351 | else if( Op.getOperandType() == MachineOperand:: MO_MachineRegister) { |
| 352 | cout << "\t" << "%"<< MRI.getUnifiedRegName( Op.getMachineRegNum() ); |
| 353 | } |
| 354 | else if( Op.getOperandType() == MachineOperand::MO_PCRelativeDisp ) { |
| 355 | const Value *const Val = Op.getVRegValue () ; |
| 356 | if( !Val ) { |
| 357 | cout << "\t<*NULL Value*>"; |
| 358 | continue; |
| 359 | } |
| 360 | if( (Val->getValueType() == Value::BasicBlockVal)) |
| 361 | { cout << "\t"; printLabel( Op.getVRegValue () ); } |
| 362 | else { cout << "\t"; printValue( Val ); } |
| 363 | } |
| 364 | |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 365 | else |
| 366 | cout << "\t" << Op; // use dump field |
| 367 | |
| 368 | } |
| 369 | |
| 370 | } |
Ruchira Sasanka | 0931a01 | 2001-09-15 19:06:58 +0000 | [diff] [blame] | 371 | |
| 372 | cout << endl; |
| 373 | |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 374 | } |
Ruchira Sasanka | 0931a01 | 2001-09-15 19:06:58 +0000 | [diff] [blame] | 375 | |
| 376 | cout << endl; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 377 | } |
| 378 | |
Ruchira Sasanka | 0931a01 | 2001-09-15 19:06:58 +0000 | [diff] [blame] | 379 | void PhyRegAlloc::printLabel(const Value *const Val) |
| 380 | { |
| 381 | if( Val->hasName() ) |
| 382 | cout << Val->getName(); |
| 383 | else |
| 384 | cout << "Label" << Val; |
| 385 | } |
| 386 | |
| 387 | |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 388 | |
| 389 | |
| 390 | |
| 391 | |
| 392 | |
| 393 | |
| 394 | void PhyRegAlloc::allocateRegisters() |
| 395 | { |
| 396 | constructLiveRanges(); // create LR info |
| 397 | |
| 398 | if( DEBUG_RA) |
| 399 | LRI.printLiveRanges(); |
| 400 | |
| 401 | createIGNodeListsAndIGs(); // create IGNode list and IGs |
| 402 | |
| 403 | buildInterferenceGraphs(); // build IGs in all reg classes |
| 404 | |
| 405 | |
| 406 | if( DEBUG_RA) { |
| 407 | // print all LRs in all reg classes |
| 408 | for( unsigned int rc=0; rc < NumOfRegClasses ; rc++) |
| 409 | RegClassList[ rc ]->printIGNodeList(); |
| 410 | |
| 411 | // print IGs in all register classes |
| 412 | for( unsigned int rc=0; rc < NumOfRegClasses ; rc++) |
| 413 | RegClassList[ rc ]->printIG(); |
| 414 | } |
| 415 | |
| 416 | LRI.coalesceLRs(); // coalesce all live ranges |
| 417 | |
| 418 | if( DEBUG_RA) { |
| 419 | // print all LRs in all reg classes |
| 420 | for( unsigned int rc=0; rc < NumOfRegClasses ; rc++) |
| 421 | RegClassList[ rc ]->printIGNodeList(); |
| 422 | |
| 423 | // print IGs in all register classes |
| 424 | for( unsigned int rc=0; rc < NumOfRegClasses ; rc++) |
| 425 | RegClassList[ rc ]->printIG(); |
| 426 | } |
| 427 | |
| 428 | MRI.colorArgs(Meth, LRI); // color method args |
Ruchira Sasanka | 0931a01 | 2001-09-15 19:06:58 +0000 | [diff] [blame] | 429 | // color call args of call instrns |
| 430 | MRI.colorCallArgs(CallInstrList, LRI, AddedInstrMap); |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 431 | |
| 432 | // color all register classes |
| 433 | for( unsigned int rc=0; rc < NumOfRegClasses ; rc++) |
| 434 | RegClassList[ rc ]->colorAllRegs(); |
| 435 | |
Ruchira Sasanka | 0931a01 | 2001-09-15 19:06:58 +0000 | [diff] [blame] | 436 | //updateMachineCode(); |
| 437 | PrintMachineInstructions(Meth); |
| 438 | printMachineCode(); // only for DEBUGGING |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | |
| 442 | |
| 443 | |