Lang Hames | 60f422f | 2010-07-17 07:34:01 +0000 | [diff] [blame] | 1 | //===-- llvm/CodeGen/Splitter.cpp - Splitter -----------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #define DEBUG_TYPE "loopsplitter" |
| 11 | |
| 12 | #include "Splitter.h" |
| 13 | |
| 14 | #include "SimpleRegisterCoalescing.h" |
| 15 | #include "llvm/Module.h" |
| 16 | #include "llvm/CodeGen/CalcSpillWeights.h" |
| 17 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
| 18 | #include "llvm/CodeGen/LiveStackAnalysis.h" |
| 19 | #include "llvm/CodeGen/MachineDominators.h" |
| 20 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 21 | #include "llvm/CodeGen/MachineFunction.h" |
| 22 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 23 | #include "llvm/CodeGen/SlotIndexes.h" |
| 24 | #include "llvm/Support/Debug.h" |
| 25 | #include "llvm/Support/raw_ostream.h" |
| 26 | #include "llvm/Target/TargetMachine.h" |
| 27 | #include "llvm/Target/TargetInstrInfo.h" |
| 28 | |
| 29 | using namespace llvm; |
| 30 | |
| 31 | char LoopSplitter::ID = 0; |
Owen Anderson | d13db2c | 2010-07-21 22:09:45 +0000 | [diff] [blame] | 32 | INITIALIZE_PASS(LoopSplitter, "loop-splitting", |
| 33 | "Split virtual regists across loop boundaries.", false, false); |
Lang Hames | 60f422f | 2010-07-17 07:34:01 +0000 | [diff] [blame] | 34 | |
| 35 | namespace llvm { |
| 36 | |
| 37 | class StartSlotComparator { |
| 38 | public: |
| 39 | StartSlotComparator(LiveIntervals &lis) : lis(lis) {} |
| 40 | bool operator()(const MachineBasicBlock *mbb1, |
| 41 | const MachineBasicBlock *mbb2) const { |
| 42 | return lis.getMBBStartIdx(mbb1) < lis.getMBBStartIdx(mbb2); |
| 43 | } |
| 44 | private: |
| 45 | LiveIntervals &lis; |
| 46 | }; |
| 47 | |
| 48 | class LoopSplit { |
| 49 | public: |
| 50 | LoopSplit(LoopSplitter &ls, LiveInterval &li, MachineLoop &loop) |
| 51 | : ls(ls), li(li), loop(loop), valid(true), inSplit(false), newLI(0) { |
| 52 | assert(TargetRegisterInfo::isVirtualRegister(li.reg) && |
| 53 | "Cannot split physical registers."); |
| 54 | } |
| 55 | |
| 56 | LiveInterval& getLI() const { return li; } |
| 57 | |
| 58 | MachineLoop& getLoop() const { return loop; } |
| 59 | |
| 60 | bool isValid() const { return valid; } |
| 61 | |
| 62 | bool isWorthwhile() const { return valid && (inSplit || !outSplits.empty()); } |
| 63 | |
| 64 | void invalidate() { valid = false; } |
| 65 | |
| 66 | void splitIncoming() { inSplit = true; } |
| 67 | |
| 68 | void splitOutgoing(MachineLoop::Edge &edge) { outSplits.insert(edge); } |
| 69 | |
| 70 | void addLoopInstr(MachineInstr *i) { loopInstrs.push_back(i); } |
| 71 | |
| 72 | void apply() { |
| 73 | assert(valid && "Attempt to apply invalid split."); |
| 74 | applyIncoming(); |
| 75 | applyOutgoing(); |
| 76 | copyRanges(); |
| 77 | renameInside(); |
| 78 | } |
| 79 | |
| 80 | private: |
| 81 | LoopSplitter &ls; |
| 82 | LiveInterval &li; |
| 83 | MachineLoop &loop; |
| 84 | bool valid, inSplit; |
| 85 | std::set<MachineLoop::Edge> outSplits; |
| 86 | std::vector<MachineInstr*> loopInstrs; |
| 87 | |
| 88 | LiveInterval *newLI; |
| 89 | std::map<VNInfo*, VNInfo*> vniMap; |
| 90 | |
| 91 | LiveInterval* getNewLI() { |
| 92 | if (newLI == 0) { |
| 93 | const TargetRegisterClass *trc = ls.mri->getRegClass(li.reg); |
| 94 | unsigned vreg = ls.mri->createVirtualRegister(trc); |
| 95 | newLI = &ls.lis->getOrCreateInterval(vreg); |
| 96 | } |
| 97 | return newLI; |
| 98 | } |
| 99 | |
| 100 | VNInfo* getNewVNI(VNInfo *oldVNI) { |
| 101 | VNInfo *newVNI = vniMap[oldVNI]; |
| 102 | |
| 103 | if (newVNI == 0) { |
| 104 | newVNI = getNewLI()->createValueCopy(oldVNI, |
| 105 | ls.lis->getVNInfoAllocator()); |
| 106 | vniMap[oldVNI] = newVNI; |
| 107 | } |
| 108 | |
| 109 | return newVNI; |
| 110 | } |
| 111 | |
| 112 | void applyIncoming() { |
| 113 | if (!inSplit) { |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | MachineBasicBlock *preHeader = loop.getLoopPreheader(); |
| 118 | if (preHeader == 0) { |
| 119 | assert(ls.canInsertPreHeader(loop) && |
| 120 | "Can't insert required preheader."); |
| 121 | preHeader = &ls.insertPreHeader(loop); |
| 122 | } |
| 123 | |
| 124 | LiveRange *preHeaderRange = |
| 125 | ls.lis->findExitingRange(li, preHeader); |
| 126 | assert(preHeaderRange != 0 && "Range not live into preheader."); |
| 127 | |
| 128 | // Insert the new copy. |
| 129 | MachineInstr *copy = BuildMI(*preHeader, |
| 130 | preHeader->getFirstTerminator(), |
| 131 | DebugLoc(), |
| 132 | ls.tii->get(TargetOpcode::COPY)) |
| 133 | .addReg(getNewLI()->reg, RegState::Define) |
| 134 | .addReg(li.reg, RegState::Kill); |
| 135 | |
| 136 | ls.lis->InsertMachineInstrInMaps(copy); |
| 137 | |
| 138 | SlotIndex copyDefIdx = ls.lis->getInstructionIndex(copy).getDefIndex(); |
| 139 | |
| 140 | VNInfo *newVal = getNewVNI(preHeaderRange->valno); |
| 141 | newVal->def = copyDefIdx; |
| 142 | newVal->setCopy(copy); |
| 143 | newVal->setIsDefAccurate(true); |
| 144 | li.removeRange(copyDefIdx, ls.lis->getMBBEndIdx(preHeader), true); |
| 145 | |
| 146 | getNewLI()->addRange(LiveRange(copyDefIdx, |
| 147 | ls.lis->getMBBEndIdx(preHeader), |
| 148 | newVal)); |
| 149 | } |
| 150 | |
| 151 | void applyOutgoing() { |
| 152 | |
| 153 | for (std::set<MachineLoop::Edge>::iterator osItr = outSplits.begin(), |
| 154 | osEnd = outSplits.end(); |
| 155 | osItr != osEnd; ++osItr) { |
| 156 | MachineLoop::Edge edge = *osItr; |
| 157 | MachineBasicBlock *outBlock = edge.second; |
| 158 | if (ls.isCriticalEdge(edge)) { |
| 159 | assert(ls.canSplitEdge(edge) && "Unsplitable critical edge."); |
| 160 | outBlock = &ls.splitEdge(edge, loop); |
| 161 | } |
| 162 | LiveRange *outRange = ls.lis->findEnteringRange(li, outBlock); |
| 163 | assert(outRange != 0 && "No exiting range?"); |
| 164 | |
| 165 | MachineInstr *copy = BuildMI(*outBlock, outBlock->begin(), |
| 166 | DebugLoc(), |
| 167 | ls.tii->get(TargetOpcode::COPY)) |
| 168 | .addReg(li.reg, RegState::Define) |
| 169 | .addReg(getNewLI()->reg, RegState::Kill); |
| 170 | |
| 171 | ls.lis->InsertMachineInstrInMaps(copy); |
| 172 | |
| 173 | SlotIndex copyDefIdx = ls.lis->getInstructionIndex(copy).getDefIndex(); |
| 174 | |
| 175 | // Blow away output range definition. |
| 176 | outRange->valno->def = ls.lis->getInvalidIndex(); |
| 177 | outRange->valno->setIsDefAccurate(false); |
| 178 | li.removeRange(ls.lis->getMBBStartIdx(outBlock), copyDefIdx); |
| 179 | |
| 180 | VNInfo *newVal = |
Jakob Stoklund Olesen | 011e591 | 2010-09-25 00:45:18 +0000 | [diff] [blame^] | 181 | getNewLI()->getNextValue(ls.lis->getMBBStartIdx(outBlock), |
Lang Hames | 60f422f | 2010-07-17 07:34:01 +0000 | [diff] [blame] | 182 | 0, false, ls.lis->getVNInfoAllocator()); |
| 183 | |
| 184 | getNewLI()->addRange(LiveRange(ls.lis->getMBBStartIdx(outBlock), |
| 185 | copyDefIdx, newVal)); |
| 186 | |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | void copyRange(LiveRange &lr) { |
| 191 | std::pair<bool, LoopSplitter::SlotPair> lsr = |
| 192 | ls.getLoopSubRange(lr, loop); |
| 193 | |
| 194 | if (!lsr.first) |
| 195 | return; |
| 196 | |
| 197 | LiveRange loopRange(lsr.second.first, lsr.second.second, |
| 198 | getNewVNI(lr.valno)); |
| 199 | |
| 200 | li.removeRange(loopRange.start, loopRange.end, true); |
| 201 | |
| 202 | getNewLI()->addRange(loopRange); |
| 203 | } |
| 204 | |
| 205 | void copyRanges() { |
| 206 | for (std::vector<MachineInstr*>::iterator iItr = loopInstrs.begin(), |
| 207 | iEnd = loopInstrs.end(); |
| 208 | iItr != iEnd; ++iItr) { |
| 209 | MachineInstr &instr = **iItr; |
| 210 | SlotIndex instrIdx = ls.lis->getInstructionIndex(&instr); |
| 211 | if (instr.modifiesRegister(li.reg, 0)) { |
| 212 | LiveRange *defRange = |
| 213 | li.getLiveRangeContaining(instrIdx.getDefIndex()); |
| 214 | if (defRange != 0) // May have caught this already. |
| 215 | copyRange(*defRange); |
| 216 | } |
| 217 | if (instr.readsRegister(li.reg, 0)) { |
| 218 | LiveRange *useRange = |
| 219 | li.getLiveRangeContaining(instrIdx.getUseIndex()); |
| 220 | if (useRange != 0) { // May have caught this already. |
| 221 | copyRange(*useRange); |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | for (MachineLoop::block_iterator bbItr = loop.block_begin(), |
| 227 | bbEnd = loop.block_end(); |
| 228 | bbItr != bbEnd; ++bbItr) { |
| 229 | MachineBasicBlock &loopBlock = **bbItr; |
| 230 | LiveRange *enteringRange = |
| 231 | ls.lis->findEnteringRange(li, &loopBlock); |
| 232 | if (enteringRange != 0) { |
| 233 | copyRange(*enteringRange); |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | void renameInside() { |
| 239 | for (std::vector<MachineInstr*>::iterator iItr = loopInstrs.begin(), |
| 240 | iEnd = loopInstrs.end(); |
| 241 | iItr != iEnd; ++iItr) { |
| 242 | MachineInstr &instr = **iItr; |
| 243 | for (unsigned i = 0; i < instr.getNumOperands(); ++i) { |
| 244 | MachineOperand &mop = instr.getOperand(i); |
| 245 | if (mop.isReg() && mop.getReg() == li.reg) { |
| 246 | mop.setReg(getNewLI()->reg); |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | }; |
| 253 | |
| 254 | void LoopSplitter::getAnalysisUsage(AnalysisUsage &au) const { |
| 255 | au.addRequired<MachineDominatorTree>(); |
| 256 | au.addPreserved<MachineDominatorTree>(); |
| 257 | au.addRequired<MachineLoopInfo>(); |
| 258 | au.addPreserved<MachineLoopInfo>(); |
| 259 | au.addPreserved<RegisterCoalescer>(); |
| 260 | au.addPreserved<CalculateSpillWeights>(); |
| 261 | au.addPreserved<LiveStacks>(); |
| 262 | au.addRequired<SlotIndexes>(); |
| 263 | au.addPreserved<SlotIndexes>(); |
| 264 | au.addRequired<LiveIntervals>(); |
| 265 | au.addPreserved<LiveIntervals>(); |
| 266 | MachineFunctionPass::getAnalysisUsage(au); |
| 267 | } |
| 268 | |
| 269 | bool LoopSplitter::runOnMachineFunction(MachineFunction &fn) { |
| 270 | |
| 271 | mf = &fn; |
| 272 | mri = &mf->getRegInfo(); |
| 273 | tii = mf->getTarget().getInstrInfo(); |
| 274 | tri = mf->getTarget().getRegisterInfo(); |
| 275 | sis = &getAnalysis<SlotIndexes>(); |
| 276 | lis = &getAnalysis<LiveIntervals>(); |
| 277 | mli = &getAnalysis<MachineLoopInfo>(); |
| 278 | mdt = &getAnalysis<MachineDominatorTree>(); |
| 279 | |
| 280 | fqn = mf->getFunction()->getParent()->getModuleIdentifier() + "." + |
| 281 | mf->getFunction()->getName().str(); |
| 282 | |
| 283 | dbgs() << "Splitting " << mf->getFunction()->getName() << "."; |
| 284 | |
| 285 | dumpOddTerminators(); |
| 286 | |
| 287 | // dbgs() << "----------------------------------------\n"; |
| 288 | // lis->dump(); |
| 289 | // dbgs() << "----------------------------------------\n"; |
| 290 | |
| 291 | // std::deque<MachineLoop*> loops; |
| 292 | // std::copy(mli->begin(), mli->end(), std::back_inserter(loops)); |
| 293 | // dbgs() << "Loops:\n"; |
| 294 | // while (!loops.empty()) { |
| 295 | // MachineLoop &loop = *loops.front(); |
| 296 | // loops.pop_front(); |
| 297 | // std::copy(loop.begin(), loop.end(), std::back_inserter(loops)); |
| 298 | |
| 299 | // dumpLoopInfo(loop); |
| 300 | // } |
| 301 | |
| 302 | //lis->dump(); |
| 303 | //exit(0); |
| 304 | |
| 305 | // Setup initial intervals. |
| 306 | for (LiveIntervals::iterator liItr = lis->begin(), liEnd = lis->end(); |
| 307 | liItr != liEnd; ++liItr) { |
| 308 | LiveInterval *li = liItr->second; |
| 309 | |
| 310 | if (TargetRegisterInfo::isVirtualRegister(li->reg) && |
| 311 | !lis->intervalIsInOneMBB(*li)) { |
| 312 | intervals.push_back(li); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | processIntervals(); |
| 317 | |
| 318 | intervals.clear(); |
| 319 | |
| 320 | // dbgs() << "----------------------------------------\n"; |
| 321 | // lis->dump(); |
| 322 | // dbgs() << "----------------------------------------\n"; |
| 323 | |
| 324 | dumpOddTerminators(); |
| 325 | |
| 326 | //exit(1); |
| 327 | |
| 328 | return false; |
| 329 | } |
| 330 | |
| 331 | void LoopSplitter::releaseMemory() { |
| 332 | fqn.clear(); |
| 333 | intervals.clear(); |
| 334 | loopRangeMap.clear(); |
| 335 | } |
| 336 | |
| 337 | void LoopSplitter::dumpOddTerminators() { |
| 338 | for (MachineFunction::iterator bbItr = mf->begin(), bbEnd = mf->end(); |
| 339 | bbItr != bbEnd; ++bbItr) { |
| 340 | MachineBasicBlock *mbb = &*bbItr; |
| 341 | MachineBasicBlock *a = 0, *b = 0; |
| 342 | SmallVector<MachineOperand, 4> c; |
| 343 | if (tii->AnalyzeBranch(*mbb, a, b, c)) { |
| 344 | dbgs() << "MBB#" << mbb->getNumber() << " has multiway terminator.\n"; |
| 345 | dbgs() << " Terminators:\n"; |
| 346 | for (MachineBasicBlock::iterator iItr = mbb->begin(), iEnd = mbb->end(); |
| 347 | iItr != iEnd; ++iItr) { |
| 348 | MachineInstr *instr= &*iItr; |
| 349 | dbgs() << " " << *instr << ""; |
| 350 | } |
| 351 | dbgs() << "\n Listed successors: [ "; |
| 352 | for (MachineBasicBlock::succ_iterator sItr = mbb->succ_begin(), sEnd = mbb->succ_end(); |
| 353 | sItr != sEnd; ++sItr) { |
| 354 | MachineBasicBlock *succMBB = *sItr; |
| 355 | dbgs() << succMBB->getNumber() << " "; |
| 356 | } |
| 357 | dbgs() << "]\n\n"; |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | void LoopSplitter::dumpLoopInfo(MachineLoop &loop) { |
| 363 | MachineBasicBlock &headerBlock = *loop.getHeader(); |
| 364 | typedef SmallVector<MachineLoop::Edge, 8> ExitEdgesList; |
| 365 | ExitEdgesList exitEdges; |
| 366 | loop.getExitEdges(exitEdges); |
| 367 | |
| 368 | dbgs() << " Header: BB#" << headerBlock.getNumber() << ", Contains: [ "; |
| 369 | for (std::vector<MachineBasicBlock*>::const_iterator |
| 370 | subBlockItr = loop.getBlocks().begin(), |
| 371 | subBlockEnd = loop.getBlocks().end(); |
| 372 | subBlockItr != subBlockEnd; ++subBlockItr) { |
| 373 | MachineBasicBlock &subBlock = **subBlockItr; |
| 374 | dbgs() << "BB#" << subBlock.getNumber() << " "; |
| 375 | } |
| 376 | dbgs() << "], Exit edges: [ "; |
| 377 | for (ExitEdgesList::iterator exitEdgeItr = exitEdges.begin(), |
| 378 | exitEdgeEnd = exitEdges.end(); |
| 379 | exitEdgeItr != exitEdgeEnd; ++exitEdgeItr) { |
| 380 | MachineLoop::Edge &exitEdge = *exitEdgeItr; |
| 381 | dbgs() << "(MBB#" << exitEdge.first->getNumber() |
| 382 | << ", MBB#" << exitEdge.second->getNumber() << ") "; |
| 383 | } |
| 384 | dbgs() << "], Sub-Loop Headers: [ "; |
| 385 | for (MachineLoop::iterator subLoopItr = loop.begin(), |
| 386 | subLoopEnd = loop.end(); |
| 387 | subLoopItr != subLoopEnd; ++subLoopItr) { |
| 388 | MachineLoop &subLoop = **subLoopItr; |
| 389 | MachineBasicBlock &subLoopBlock = *subLoop.getHeader(); |
| 390 | dbgs() << "BB#" << subLoopBlock.getNumber() << " "; |
| 391 | } |
| 392 | dbgs() << "]\n"; |
| 393 | } |
| 394 | |
| 395 | void LoopSplitter::updateTerminators(MachineBasicBlock &mbb) { |
| 396 | mbb.updateTerminator(); |
| 397 | |
| 398 | for (MachineBasicBlock::iterator miItr = mbb.begin(), miEnd = mbb.end(); |
| 399 | miItr != miEnd; ++miItr) { |
| 400 | if (lis->isNotInMIMap(miItr)) { |
| 401 | lis->InsertMachineInstrInMaps(miItr); |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | bool LoopSplitter::canInsertPreHeader(MachineLoop &loop) { |
| 407 | MachineBasicBlock *header = loop.getHeader(); |
| 408 | MachineBasicBlock *a = 0, *b = 0; |
| 409 | SmallVector<MachineOperand, 4> c; |
| 410 | |
| 411 | for (MachineBasicBlock::pred_iterator pbItr = header->pred_begin(), |
| 412 | pbEnd = header->pred_end(); |
| 413 | pbItr != pbEnd; ++pbItr) { |
| 414 | MachineBasicBlock *predBlock = *pbItr; |
| 415 | if (!!tii->AnalyzeBranch(*predBlock, a, b, c)) { |
| 416 | return false; |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | MachineFunction::iterator headerItr(header); |
| 421 | if (headerItr == mf->begin()) |
| 422 | return true; |
| 423 | MachineBasicBlock *headerLayoutPred = llvm::prior(headerItr); |
| 424 | assert(headerLayoutPred != 0 && "Header should have layout pred."); |
| 425 | |
| 426 | return (!tii->AnalyzeBranch(*headerLayoutPred, a, b, c)); |
| 427 | } |
| 428 | |
| 429 | MachineBasicBlock& LoopSplitter::insertPreHeader(MachineLoop &loop) { |
| 430 | assert(loop.getLoopPreheader() == 0 && "Loop already has preheader."); |
| 431 | |
| 432 | MachineBasicBlock &header = *loop.getHeader(); |
| 433 | |
| 434 | // Save the preds - we'll need to update them once we insert the preheader. |
| 435 | typedef std::set<MachineBasicBlock*> HeaderPreds; |
| 436 | HeaderPreds headerPreds; |
| 437 | |
| 438 | for (MachineBasicBlock::pred_iterator predItr = header.pred_begin(), |
| 439 | predEnd = header.pred_end(); |
| 440 | predItr != predEnd; ++predItr) { |
| 441 | if (!loop.contains(*predItr)) |
| 442 | headerPreds.insert(*predItr); |
| 443 | } |
| 444 | |
| 445 | assert(!headerPreds.empty() && "No predecessors for header?"); |
| 446 | |
| 447 | //dbgs() << fqn << " MBB#" << header.getNumber() << " inserting preheader..."; |
| 448 | |
| 449 | MachineBasicBlock *preHeader = |
| 450 | mf->CreateMachineBasicBlock(header.getBasicBlock()); |
| 451 | |
| 452 | assert(preHeader != 0 && "Failed to create pre-header."); |
| 453 | |
| 454 | mf->insert(header, preHeader); |
| 455 | |
| 456 | for (HeaderPreds::iterator hpItr = headerPreds.begin(), |
| 457 | hpEnd = headerPreds.end(); |
| 458 | hpItr != hpEnd; ++hpItr) { |
| 459 | assert(*hpItr != 0 && "How'd a null predecessor get into this set?"); |
| 460 | MachineBasicBlock &hp = **hpItr; |
| 461 | hp.ReplaceUsesOfBlockWith(&header, preHeader); |
| 462 | } |
| 463 | preHeader->addSuccessor(&header); |
| 464 | |
| 465 | MachineBasicBlock *oldLayoutPred = |
| 466 | llvm::prior(MachineFunction::iterator(preHeader)); |
| 467 | if (oldLayoutPred != 0) { |
| 468 | updateTerminators(*oldLayoutPred); |
| 469 | } |
| 470 | |
| 471 | lis->InsertMBBInMaps(preHeader); |
| 472 | |
| 473 | if (MachineLoop *parentLoop = loop.getParentLoop()) { |
| 474 | assert(parentLoop->getHeader() != loop.getHeader() && |
| 475 | "Parent loop has same header?"); |
| 476 | parentLoop->addBasicBlockToLoop(preHeader, mli->getBase()); |
| 477 | |
| 478 | // Invalidate all parent loop ranges. |
| 479 | while (parentLoop != 0) { |
| 480 | loopRangeMap.erase(parentLoop); |
| 481 | parentLoop = parentLoop->getParentLoop(); |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | for (LiveIntervals::iterator liItr = lis->begin(), |
| 486 | liEnd = lis->end(); |
| 487 | liItr != liEnd; ++liItr) { |
| 488 | LiveInterval &li = *liItr->second; |
| 489 | |
| 490 | // Is this safe for physregs? |
| 491 | // TargetRegisterInfo::isPhysicalRegister(li.reg) || |
| 492 | if (!lis->isLiveInToMBB(li, &header)) |
| 493 | continue; |
| 494 | |
| 495 | if (lis->isLiveInToMBB(li, preHeader)) { |
| 496 | assert(lis->isLiveOutOfMBB(li, preHeader) && |
| 497 | "Range terminates in newly added preheader?"); |
| 498 | continue; |
| 499 | } |
| 500 | |
| 501 | bool insertRange = false; |
| 502 | |
| 503 | for (MachineBasicBlock::pred_iterator predItr = preHeader->pred_begin(), |
| 504 | predEnd = preHeader->pred_end(); |
| 505 | predItr != predEnd; ++predItr) { |
| 506 | MachineBasicBlock *predMBB = *predItr; |
| 507 | if (lis->isLiveOutOfMBB(li, predMBB)) { |
| 508 | insertRange = true; |
| 509 | break; |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | if (!insertRange) |
| 514 | continue; |
| 515 | |
| 516 | VNInfo *newVal = li.getNextValue(lis->getMBBStartIdx(preHeader), |
| 517 | 0, false, lis->getVNInfoAllocator()); |
| 518 | li.addRange(LiveRange(lis->getMBBStartIdx(preHeader), |
| 519 | lis->getMBBEndIdx(preHeader), |
| 520 | newVal)); |
| 521 | } |
| 522 | |
| 523 | |
| 524 | //dbgs() << "Dumping SlotIndexes:\n"; |
| 525 | //sis->dump(); |
| 526 | |
| 527 | //dbgs() << "done. (Added MBB#" << preHeader->getNumber() << ")\n"; |
| 528 | |
| 529 | return *preHeader; |
| 530 | } |
| 531 | |
| 532 | bool LoopSplitter::isCriticalEdge(MachineLoop::Edge &edge) { |
| 533 | assert(edge.first->succ_size() > 1 && "Non-sensical edge."); |
| 534 | if (edge.second->pred_size() > 1) |
| 535 | return true; |
| 536 | return false; |
| 537 | } |
| 538 | |
| 539 | bool LoopSplitter::canSplitEdge(MachineLoop::Edge &edge) { |
| 540 | MachineFunction::iterator outBlockItr(edge.second); |
| 541 | if (outBlockItr == mf->begin()) |
| 542 | return true; |
| 543 | MachineBasicBlock *outBlockLayoutPred = llvm::prior(outBlockItr); |
| 544 | assert(outBlockLayoutPred != 0 && "Should have a layout pred if out!=begin."); |
| 545 | MachineBasicBlock *a = 0, *b = 0; |
| 546 | SmallVector<MachineOperand, 4> c; |
| 547 | return (!tii->AnalyzeBranch(*outBlockLayoutPred, a, b, c) && |
| 548 | !tii->AnalyzeBranch(*edge.first, a, b, c)); |
| 549 | } |
| 550 | |
| 551 | MachineBasicBlock& LoopSplitter::splitEdge(MachineLoop::Edge &edge, |
| 552 | MachineLoop &loop) { |
| 553 | |
| 554 | MachineBasicBlock &inBlock = *edge.first; |
| 555 | MachineBasicBlock &outBlock = *edge.second; |
| 556 | |
| 557 | assert((inBlock.succ_size() > 1) && (outBlock.pred_size() > 1) && |
| 558 | "Splitting non-critical edge?"); |
| 559 | |
| 560 | //dbgs() << fqn << " Splitting edge (MBB#" << inBlock.getNumber() |
| 561 | // << " -> MBB#" << outBlock.getNumber() << ")..."; |
| 562 | |
| 563 | MachineBasicBlock *splitBlock = |
| 564 | mf->CreateMachineBasicBlock(); |
| 565 | |
| 566 | assert(splitBlock != 0 && "Failed to create split block."); |
| 567 | |
| 568 | mf->insert(&outBlock, splitBlock); |
| 569 | |
| 570 | inBlock.ReplaceUsesOfBlockWith(&outBlock, splitBlock); |
| 571 | splitBlock->addSuccessor(&outBlock); |
| 572 | |
| 573 | MachineBasicBlock *oldLayoutPred = |
| 574 | llvm::prior(MachineFunction::iterator(splitBlock)); |
| 575 | if (oldLayoutPred != 0) { |
| 576 | updateTerminators(*oldLayoutPred); |
| 577 | } |
| 578 | |
| 579 | lis->InsertMBBInMaps(splitBlock); |
| 580 | |
| 581 | loopRangeMap.erase(&loop); |
| 582 | |
| 583 | MachineLoop *splitParentLoop = loop.getParentLoop(); |
| 584 | while (splitParentLoop != 0 && |
| 585 | !splitParentLoop->contains(&outBlock)) { |
| 586 | splitParentLoop = splitParentLoop->getParentLoop(); |
| 587 | } |
| 588 | |
| 589 | if (splitParentLoop != 0) { |
| 590 | assert(splitParentLoop->contains(&loop) && |
| 591 | "Split-block parent doesn't contain original loop?"); |
| 592 | splitParentLoop->addBasicBlockToLoop(splitBlock, mli->getBase()); |
| 593 | |
| 594 | // Invalidate all parent loop ranges. |
| 595 | while (splitParentLoop != 0) { |
| 596 | loopRangeMap.erase(splitParentLoop); |
| 597 | splitParentLoop = splitParentLoop->getParentLoop(); |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | |
| 602 | for (LiveIntervals::iterator liItr = lis->begin(), |
| 603 | liEnd = lis->end(); |
| 604 | liItr != liEnd; ++liItr) { |
| 605 | LiveInterval &li = *liItr->second; |
| 606 | bool intersects = lis->isLiveOutOfMBB(li, &inBlock) && |
| 607 | lis->isLiveInToMBB(li, &outBlock); |
| 608 | if (lis->isLiveInToMBB(li, splitBlock)) { |
| 609 | if (!intersects) { |
| 610 | li.removeRange(lis->getMBBStartIdx(splitBlock), |
| 611 | lis->getMBBEndIdx(splitBlock), true); |
| 612 | } |
| 613 | } else if (intersects) { |
| 614 | VNInfo *newVal = li.getNextValue(lis->getMBBStartIdx(splitBlock), |
| 615 | 0, false, lis->getVNInfoAllocator()); |
| 616 | li.addRange(LiveRange(lis->getMBBStartIdx(splitBlock), |
| 617 | lis->getMBBEndIdx(splitBlock), |
| 618 | newVal)); |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | //dbgs() << "done. (Added MBB#" << splitBlock->getNumber() << ")\n"; |
| 623 | |
| 624 | return *splitBlock; |
| 625 | } |
| 626 | |
| 627 | LoopSplitter::LoopRanges& LoopSplitter::getLoopRanges(MachineLoop &loop) { |
| 628 | typedef std::set<MachineBasicBlock*, StartSlotComparator> LoopMBBSet; |
| 629 | LoopRangeMap::iterator lrItr = loopRangeMap.find(&loop); |
| 630 | if (lrItr == loopRangeMap.end()) { |
| 631 | LoopMBBSet loopMBBs((StartSlotComparator(*lis))); |
| 632 | std::copy(loop.block_begin(), loop.block_end(), |
| 633 | std::inserter(loopMBBs, loopMBBs.begin())); |
| 634 | |
| 635 | assert(!loopMBBs.empty() && "No blocks in loop?"); |
| 636 | |
| 637 | LoopRanges &loopRanges = loopRangeMap[&loop]; |
| 638 | assert(loopRanges.empty() && "Loop encountered but not processed?"); |
| 639 | SlotIndex oldEnd = lis->getMBBEndIdx(*loopMBBs.begin()); |
| 640 | loopRanges.push_back( |
| 641 | std::make_pair(lis->getMBBStartIdx(*loopMBBs.begin()), |
| 642 | lis->getInvalidIndex())); |
| 643 | for (LoopMBBSet::iterator curBlockItr = llvm::next(loopMBBs.begin()), |
| 644 | curBlockEnd = loopMBBs.end(); |
| 645 | curBlockItr != curBlockEnd; ++curBlockItr) { |
| 646 | SlotIndex newStart = lis->getMBBStartIdx(*curBlockItr); |
| 647 | if (newStart != oldEnd) { |
| 648 | loopRanges.back().second = oldEnd; |
| 649 | loopRanges.push_back(std::make_pair(newStart, |
| 650 | lis->getInvalidIndex())); |
| 651 | } |
| 652 | oldEnd = lis->getMBBEndIdx(*curBlockItr); |
| 653 | } |
| 654 | |
| 655 | loopRanges.back().second = |
| 656 | lis->getMBBEndIdx(*llvm::prior(loopMBBs.end())); |
| 657 | |
| 658 | return loopRanges; |
| 659 | } |
| 660 | return lrItr->second; |
| 661 | } |
| 662 | |
| 663 | std::pair<bool, LoopSplitter::SlotPair> LoopSplitter::getLoopSubRange( |
| 664 | const LiveRange &lr, |
| 665 | MachineLoop &loop) { |
| 666 | LoopRanges &loopRanges = getLoopRanges(loop); |
| 667 | LoopRanges::iterator lrItr = loopRanges.begin(), |
| 668 | lrEnd = loopRanges.end(); |
| 669 | while (lrItr != lrEnd && lr.start >= lrItr->second) { |
| 670 | ++lrItr; |
| 671 | } |
| 672 | |
| 673 | if (lrItr == lrEnd) { |
| 674 | SlotIndex invalid = lis->getInvalidIndex(); |
| 675 | return std::make_pair(false, SlotPair(invalid, invalid)); |
| 676 | } |
| 677 | |
| 678 | SlotIndex srStart(lr.start < lrItr->first ? lrItr->first : lr.start); |
| 679 | SlotIndex srEnd(lr.end > lrItr->second ? lrItr->second : lr.end); |
| 680 | |
| 681 | return std::make_pair(true, SlotPair(srStart, srEnd)); |
| 682 | } |
| 683 | |
| 684 | void LoopSplitter::dumpLoopRanges(MachineLoop &loop) { |
| 685 | LoopRanges &loopRanges = getLoopRanges(loop); |
| 686 | dbgs() << "For loop MBB#" << loop.getHeader()->getNumber() << ", subranges are: [ "; |
| 687 | for (LoopRanges::iterator lrItr = loopRanges.begin(), lrEnd = loopRanges.end(); |
| 688 | lrItr != lrEnd; ++lrItr) { |
| 689 | dbgs() << "[" << lrItr->first << ", " << lrItr->second << ") "; |
| 690 | } |
| 691 | dbgs() << "]\n"; |
| 692 | } |
| 693 | |
| 694 | void LoopSplitter::processHeader(LoopSplit &split) { |
| 695 | MachineBasicBlock &header = *split.getLoop().getHeader(); |
| 696 | //dbgs() << " Processing loop header BB#" << header.getNumber() << "\n"; |
| 697 | |
| 698 | if (!lis->isLiveInToMBB(split.getLI(), &header)) |
| 699 | return; // Not live in, but nothing wrong so far. |
| 700 | |
| 701 | MachineBasicBlock *preHeader = split.getLoop().getLoopPreheader(); |
| 702 | if (!preHeader) { |
| 703 | |
| 704 | if (!canInsertPreHeader(split.getLoop())) { |
| 705 | split.invalidate(); |
| 706 | return; // Couldn't insert a pre-header. Bail on this interval. |
| 707 | } |
| 708 | |
| 709 | for (MachineBasicBlock::pred_iterator predItr = header.pred_begin(), |
| 710 | predEnd = header.pred_end(); |
| 711 | predItr != predEnd; ++predItr) { |
| 712 | if (lis->isLiveOutOfMBB(split.getLI(), *predItr)) { |
| 713 | split.splitIncoming(); |
| 714 | break; |
| 715 | } |
| 716 | } |
| 717 | } else if (lis->isLiveOutOfMBB(split.getLI(), preHeader)) { |
| 718 | split.splitIncoming(); |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | void LoopSplitter::processLoopExits(LoopSplit &split) { |
| 723 | typedef SmallVector<MachineLoop::Edge, 8> ExitEdgesList; |
| 724 | ExitEdgesList exitEdges; |
| 725 | split.getLoop().getExitEdges(exitEdges); |
| 726 | |
| 727 | //dbgs() << " Processing loop exits:\n"; |
| 728 | |
| 729 | for (ExitEdgesList::iterator exitEdgeItr = exitEdges.begin(), |
| 730 | exitEdgeEnd = exitEdges.end(); |
| 731 | exitEdgeItr != exitEdgeEnd; ++exitEdgeItr) { |
| 732 | MachineLoop::Edge exitEdge = *exitEdgeItr; |
| 733 | |
Lang Hames | 60f422f | 2010-07-17 07:34:01 +0000 | [diff] [blame] | 734 | LiveRange *outRange = |
| 735 | split.getLI().getLiveRangeContaining(lis->getMBBStartIdx(exitEdge.second)); |
| 736 | |
| 737 | if (outRange != 0) { |
| 738 | if (isCriticalEdge(exitEdge) && !canSplitEdge(exitEdge)) { |
| 739 | split.invalidate(); |
| 740 | return; |
| 741 | } |
| 742 | |
| 743 | split.splitOutgoing(exitEdge); |
| 744 | } |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | void LoopSplitter::processLoopUses(LoopSplit &split) { |
| 749 | std::set<MachineInstr*> processed; |
| 750 | |
| 751 | for (MachineRegisterInfo::reg_iterator |
| 752 | rItr = mri->reg_begin(split.getLI().reg), |
| 753 | rEnd = mri->reg_end(); |
| 754 | rItr != rEnd; ++rItr) { |
| 755 | MachineInstr &instr = *rItr; |
| 756 | if (split.getLoop().contains(&instr) && processed.count(&instr) == 0) { |
| 757 | split.addLoopInstr(&instr); |
| 758 | processed.insert(&instr); |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | //dbgs() << " Rewriting reg" << li.reg << " to reg" << newLI->reg |
| 763 | // << " in blocks [ "; |
| 764 | //dbgs() << "]\n"; |
| 765 | } |
| 766 | |
| 767 | bool LoopSplitter::splitOverLoop(LiveInterval &li, MachineLoop &loop) { |
| 768 | assert(TargetRegisterInfo::isVirtualRegister(li.reg) && |
| 769 | "Attempt to split physical register."); |
| 770 | |
| 771 | LoopSplit split(*this, li, loop); |
| 772 | processHeader(split); |
| 773 | if (split.isValid()) |
| 774 | processLoopExits(split); |
| 775 | if (split.isValid()) |
| 776 | processLoopUses(split); |
| 777 | if (split.isValid() /* && split.isWorthwhile() */) { |
| 778 | split.apply(); |
| 779 | DEBUG(dbgs() << "Success.\n"); |
| 780 | return true; |
| 781 | } |
| 782 | DEBUG(dbgs() << "Failed.\n"); |
| 783 | return false; |
| 784 | } |
| 785 | |
| 786 | void LoopSplitter::processInterval(LiveInterval &li) { |
| 787 | std::deque<MachineLoop*> loops; |
| 788 | std::copy(mli->begin(), mli->end(), std::back_inserter(loops)); |
| 789 | |
| 790 | while (!loops.empty()) { |
| 791 | MachineLoop &loop = *loops.front(); |
| 792 | loops.pop_front(); |
| 793 | DEBUG( |
| 794 | dbgs() << fqn << " reg" << li.reg << " " << li.weight << " BB#" |
| 795 | << loop.getHeader()->getNumber() << " "; |
| 796 | ); |
| 797 | if (!splitOverLoop(li, loop)) { |
| 798 | // Couldn't split over outer loop, schedule sub-loops to be checked. |
| 799 | std::copy(loop.begin(), loop.end(), std::back_inserter(loops)); |
| 800 | } |
| 801 | } |
| 802 | } |
| 803 | |
| 804 | void LoopSplitter::processIntervals() { |
| 805 | while (!intervals.empty()) { |
| 806 | LiveInterval &li = *intervals.front(); |
| 807 | intervals.pop_front(); |
| 808 | |
| 809 | assert(!lis->intervalIsInOneMBB(li) && |
| 810 | "Single interval in process worklist."); |
| 811 | |
| 812 | processInterval(li); |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | } |