1 //===- BranchRelaxation.cpp -----------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "llvm/ADT/SmallVector.h" 10 #include "llvm/ADT/Statistic.h" 11 #include "llvm/CodeGen/LivePhysRegs.h" 12 #include "llvm/CodeGen/MachineBasicBlock.h" 13 #include "llvm/CodeGen/MachineFunction.h" 14 #include "llvm/CodeGen/MachineFunctionPass.h" 15 #include "llvm/CodeGen/MachineInstr.h" 16 #include "llvm/CodeGen/RegisterScavenging.h" 17 #include "llvm/CodeGen/TargetInstrInfo.h" 18 #include "llvm/CodeGen/TargetRegisterInfo.h" 19 #include "llvm/CodeGen/TargetSubtargetInfo.h" 20 #include "llvm/Config/llvm-config.h" 21 #include "llvm/IR/DebugLoc.h" 22 #include "llvm/InitializePasses.h" 23 #include "llvm/Pass.h" 24 #include "llvm/Support/Compiler.h" 25 #include "llvm/Support/Debug.h" 26 #include "llvm/Support/ErrorHandling.h" 27 #include "llvm/Support/Format.h" 28 #include "llvm/Support/raw_ostream.h" 29 #include <cassert> 30 #include <cstdint> 31 #include <iterator> 32 #include <memory> 33 34 using namespace llvm; 35 36 #define DEBUG_TYPE "branch-relaxation" 37 38 STATISTIC(NumSplit, "Number of basic blocks split"); 39 STATISTIC(NumConditionalRelaxed, "Number of conditional branches relaxed"); 40 STATISTIC(NumUnconditionalRelaxed, "Number of unconditional branches relaxed"); 41 42 #define BRANCH_RELAX_NAME "Branch relaxation pass" 43 44 namespace { 45 46 class BranchRelaxation : public MachineFunctionPass { 47 /// BasicBlockInfo - Information about the offset and size of a single 48 /// basic block. 49 struct BasicBlockInfo { 50 /// Offset - Distance from the beginning of the function to the beginning 51 /// of this basic block. 52 /// 53 /// The offset is always aligned as required by the basic block. 54 unsigned Offset = 0; 55 56 /// Size - Size of the basic block in bytes. If the block contains 57 /// inline assembly, this is a worst case estimate. 58 /// 59 /// The size does not include any alignment padding whether from the 60 /// beginning of the block, or from an aligned jump table at the end. 61 unsigned Size = 0; 62 63 BasicBlockInfo() = default; 64 65 /// Compute the offset immediately following this block. \p MBB is the next 66 /// block. 67 unsigned postOffset(const MachineBasicBlock &MBB) const { 68 const unsigned PO = Offset + Size; 69 const Align Alignment = MBB.getAlignment(); 70 const Align ParentAlign = MBB.getParent()->getAlignment(); 71 if (Alignment <= ParentAlign) 72 return alignTo(PO, Alignment); 73 74 // The alignment of this MBB is larger than the function's alignment, so we 75 // can't tell whether or not it will insert nops. Assume that it will. 76 return alignTo(PO, Alignment) + Alignment.value() - ParentAlign.value(); 77 } 78 }; 79 80 SmallVector<BasicBlockInfo, 16> BlockInfo; 81 std::unique_ptr<RegScavenger> RS; 82 LivePhysRegs LiveRegs; 83 84 MachineFunction *MF; 85 const TargetRegisterInfo *TRI; 86 const TargetInstrInfo *TII; 87 88 bool relaxBranchInstructions(); 89 void scanFunction(); 90 91 MachineBasicBlock *createNewBlockAfter(MachineBasicBlock &OrigMBB); 92 MachineBasicBlock *createNewBlockAfter(MachineBasicBlock &OrigMBB, 93 const BasicBlock *BB); 94 95 MachineBasicBlock *splitBlockBeforeInstr(MachineInstr &MI, 96 MachineBasicBlock *DestBB); 97 void adjustBlockOffsets(MachineBasicBlock &Start); 98 bool isBlockInRange(const MachineInstr &MI, const MachineBasicBlock &BB) const; 99 100 bool fixupConditionalBranch(MachineInstr &MI); 101 bool fixupUnconditionalBranch(MachineInstr &MI); 102 uint64_t computeBlockSize(const MachineBasicBlock &MBB) const; 103 unsigned getInstrOffset(const MachineInstr &MI) const; 104 void dumpBBs(); 105 void verify(); 106 107 public: 108 static char ID; 109 110 BranchRelaxation() : MachineFunctionPass(ID) {} 111 112 bool runOnMachineFunction(MachineFunction &MF) override; 113 114 StringRef getPassName() const override { return BRANCH_RELAX_NAME; } 115 }; 116 117 } // end anonymous namespace 118 119 char BranchRelaxation::ID = 0; 120 121 char &llvm::BranchRelaxationPassID = BranchRelaxation::ID; 122 123 INITIALIZE_PASS(BranchRelaxation, DEBUG_TYPE, BRANCH_RELAX_NAME, false, false) 124 125 /// verify - check BBOffsets, BBSizes, alignment of islands 126 void BranchRelaxation::verify() { 127 #ifndef NDEBUG 128 unsigned PrevNum = MF->begin()->getNumber(); 129 for (MachineBasicBlock &MBB : *MF) { 130 const unsigned Num = MBB.getNumber(); 131 assert(!Num || BlockInfo[PrevNum].postOffset(MBB) <= BlockInfo[Num].Offset); 132 assert(BlockInfo[Num].Size == computeBlockSize(MBB)); 133 PrevNum = Num; 134 } 135 #endif 136 } 137 138 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 139 /// print block size and offset information - debugging 140 LLVM_DUMP_METHOD void BranchRelaxation::dumpBBs() { 141 for (auto &MBB : *MF) { 142 const BasicBlockInfo &BBI = BlockInfo[MBB.getNumber()]; 143 dbgs() << format("%%bb.%u\toffset=%08x\t", MBB.getNumber(), BBI.Offset) 144 << format("size=%#x\n", BBI.Size); 145 } 146 } 147 #endif 148 149 /// scanFunction - Do the initial scan of the function, building up 150 /// information about each block. 151 void BranchRelaxation::scanFunction() { 152 BlockInfo.clear(); 153 BlockInfo.resize(MF->getNumBlockIDs()); 154 155 // First thing, compute the size of all basic blocks, and see if the function 156 // has any inline assembly in it. If so, we have to be conservative about 157 // alignment assumptions, as we don't know for sure the size of any 158 // instructions in the inline assembly. 159 for (MachineBasicBlock &MBB : *MF) 160 BlockInfo[MBB.getNumber()].Size = computeBlockSize(MBB); 161 162 // Compute block offsets and known bits. 163 adjustBlockOffsets(*MF->begin()); 164 } 165 166 /// computeBlockSize - Compute the size for MBB. 167 uint64_t BranchRelaxation::computeBlockSize(const MachineBasicBlock &MBB) const { 168 uint64_t Size = 0; 169 for (const MachineInstr &MI : MBB) 170 Size += TII->getInstSizeInBytes(MI); 171 return Size; 172 } 173 174 /// getInstrOffset - Return the current offset of the specified machine 175 /// instruction from the start of the function. This offset changes as stuff is 176 /// moved around inside the function. 177 unsigned BranchRelaxation::getInstrOffset(const MachineInstr &MI) const { 178 const MachineBasicBlock *MBB = MI.getParent(); 179 180 // The offset is composed of two things: the sum of the sizes of all MBB's 181 // before this instruction's block, and the offset from the start of the block 182 // it is in. 183 unsigned Offset = BlockInfo[MBB->getNumber()].Offset; 184 185 // Sum instructions before MI in MBB. 186 for (MachineBasicBlock::const_iterator I = MBB->begin(); &*I != &MI; ++I) { 187 assert(I != MBB->end() && "Didn't find MI in its own basic block?"); 188 Offset += TII->getInstSizeInBytes(*I); 189 } 190 191 return Offset; 192 } 193 194 void BranchRelaxation::adjustBlockOffsets(MachineBasicBlock &Start) { 195 unsigned PrevNum = Start.getNumber(); 196 for (auto &MBB : 197 make_range(std::next(MachineFunction::iterator(Start)), MF->end())) { 198 unsigned Num = MBB.getNumber(); 199 // Get the offset and known bits at the end of the layout predecessor. 200 // Include the alignment of the current block. 201 BlockInfo[Num].Offset = BlockInfo[PrevNum].postOffset(MBB); 202 203 PrevNum = Num; 204 } 205 } 206 207 /// Insert a new empty MachineBasicBlock and insert it after \p OrigMBB 208 MachineBasicBlock * 209 BranchRelaxation::createNewBlockAfter(MachineBasicBlock &OrigBB) { 210 return createNewBlockAfter(OrigBB, OrigBB.getBasicBlock()); 211 } 212 213 /// Insert a new empty MachineBasicBlock with \p BB as its BasicBlock 214 /// and insert it after \p OrigMBB 215 MachineBasicBlock * 216 BranchRelaxation::createNewBlockAfter(MachineBasicBlock &OrigMBB, 217 const BasicBlock *BB) { 218 // Create a new MBB for the code after the OrigBB. 219 MachineBasicBlock *NewBB = MF->CreateMachineBasicBlock(BB); 220 MF->insert(++OrigMBB.getIterator(), NewBB); 221 222 // Insert an entry into BlockInfo to align it properly with the block numbers. 223 BlockInfo.insert(BlockInfo.begin() + NewBB->getNumber(), BasicBlockInfo()); 224 225 return NewBB; 226 } 227 228 /// Split the basic block containing MI into two blocks, which are joined by 229 /// an unconditional branch. Update data structures and renumber blocks to 230 /// account for this change and returns the newly created block. 231 MachineBasicBlock *BranchRelaxation::splitBlockBeforeInstr(MachineInstr &MI, 232 MachineBasicBlock *DestBB) { 233 MachineBasicBlock *OrigBB = MI.getParent(); 234 235 // Create a new MBB for the code after the OrigBB. 236 MachineBasicBlock *NewBB = 237 MF->CreateMachineBasicBlock(OrigBB->getBasicBlock()); 238 MF->insert(++OrigBB->getIterator(), NewBB); 239 240 // Splice the instructions starting with MI over to NewBB. 241 NewBB->splice(NewBB->end(), OrigBB, MI.getIterator(), OrigBB->end()); 242 243 // Add an unconditional branch from OrigBB to NewBB. 244 // Note the new unconditional branch is not being recorded. 245 // There doesn't seem to be meaningful DebugInfo available; this doesn't 246 // correspond to anything in the source. 247 TII->insertUnconditionalBranch(*OrigBB, NewBB, DebugLoc()); 248 249 // Insert an entry into BlockInfo to align it properly with the block numbers. 250 BlockInfo.insert(BlockInfo.begin() + NewBB->getNumber(), BasicBlockInfo()); 251 252 NewBB->transferSuccessors(OrigBB); 253 OrigBB->addSuccessor(NewBB); 254 OrigBB->addSuccessor(DestBB); 255 256 // Cleanup potential unconditional branch to successor block. 257 // Note that updateTerminator may change the size of the blocks. 258 OrigBB->updateTerminator(NewBB); 259 260 // Figure out how large the OrigBB is. As the first half of the original 261 // block, it cannot contain a tablejump. The size includes 262 // the new jump we added. (It should be possible to do this without 263 // recounting everything, but it's very confusing, and this is rarely 264 // executed.) 265 BlockInfo[OrigBB->getNumber()].Size = computeBlockSize(*OrigBB); 266 267 // Figure out how large the NewMBB is. As the second half of the original 268 // block, it may contain a tablejump. 269 BlockInfo[NewBB->getNumber()].Size = computeBlockSize(*NewBB); 270 271 // All BBOffsets following these blocks must be modified. 272 adjustBlockOffsets(*OrigBB); 273 274 // Need to fix live-in lists if we track liveness. 275 if (TRI->trackLivenessAfterRegAlloc(*MF)) 276 computeAndAddLiveIns(LiveRegs, *NewBB); 277 278 ++NumSplit; 279 280 return NewBB; 281 } 282 283 /// isBlockInRange - Returns true if the distance between specific MI and 284 /// specific BB can fit in MI's displacement field. 285 bool BranchRelaxation::isBlockInRange( 286 const MachineInstr &MI, const MachineBasicBlock &DestBB) const { 287 288 // FAULTING_OP's destination is not encoded in the instruction stream 289 // and thus always in range. 290 if (MI.getOpcode() == TargetOpcode::FAULTING_OP) 291 return true; 292 293 int64_t BrOffset = getInstrOffset(MI); 294 int64_t DestOffset = BlockInfo[DestBB.getNumber()].Offset; 295 296 if (TII->isBranchOffsetInRange(MI.getOpcode(), DestOffset - BrOffset)) 297 return true; 298 299 LLVM_DEBUG(dbgs() << "Out of range branch to destination " 300 << printMBBReference(DestBB) << " from " 301 << printMBBReference(*MI.getParent()) << " to " 302 << DestOffset << " offset " << DestOffset - BrOffset << '\t' 303 << MI); 304 305 return false; 306 } 307 308 /// fixupConditionalBranch - Fix up a conditional branch whose destination is 309 /// too far away to fit in its displacement field. It is converted to an inverse 310 /// conditional branch + an unconditional branch to the destination. 311 bool BranchRelaxation::fixupConditionalBranch(MachineInstr &MI) { 312 DebugLoc DL = MI.getDebugLoc(); 313 MachineBasicBlock *MBB = MI.getParent(); 314 MachineBasicBlock *TBB = nullptr, *FBB = nullptr; 315 MachineBasicBlock *NewBB = nullptr; 316 SmallVector<MachineOperand, 4> Cond; 317 318 auto insertUncondBranch = [&](MachineBasicBlock *MBB, 319 MachineBasicBlock *DestBB) { 320 unsigned &BBSize = BlockInfo[MBB->getNumber()].Size; 321 int NewBrSize = 0; 322 TII->insertUnconditionalBranch(*MBB, DestBB, DL, &NewBrSize); 323 BBSize += NewBrSize; 324 }; 325 auto insertBranch = [&](MachineBasicBlock *MBB, MachineBasicBlock *TBB, 326 MachineBasicBlock *FBB, 327 SmallVectorImpl<MachineOperand>& Cond) { 328 unsigned &BBSize = BlockInfo[MBB->getNumber()].Size; 329 int NewBrSize = 0; 330 TII->insertBranch(*MBB, TBB, FBB, Cond, DL, &NewBrSize); 331 BBSize += NewBrSize; 332 }; 333 auto removeBranch = [&](MachineBasicBlock *MBB) { 334 unsigned &BBSize = BlockInfo[MBB->getNumber()].Size; 335 int RemovedSize = 0; 336 TII->removeBranch(*MBB, &RemovedSize); 337 BBSize -= RemovedSize; 338 }; 339 340 auto finalizeBlockChanges = [&](MachineBasicBlock *MBB, 341 MachineBasicBlock *NewBB) { 342 // Keep the block offsets up to date. 343 adjustBlockOffsets(*MBB); 344 345 // Need to fix live-in lists if we track liveness. 346 if (NewBB && TRI->trackLivenessAfterRegAlloc(*MF)) 347 computeAndAddLiveIns(LiveRegs, *NewBB); 348 }; 349 350 bool Fail = TII->analyzeBranch(*MBB, TBB, FBB, Cond); 351 assert(!Fail && "branches to be relaxed must be analyzable"); 352 (void)Fail; 353 354 // Add an unconditional branch to the destination and invert the branch 355 // condition to jump over it: 356 // tbz L1 357 // => 358 // tbnz L2 359 // b L1 360 // L2: 361 362 bool ReversedCond = !TII->reverseBranchCondition(Cond); 363 if (ReversedCond) { 364 if (FBB && isBlockInRange(MI, *FBB)) { 365 // Last MI in the BB is an unconditional branch. We can simply invert the 366 // condition and swap destinations: 367 // beq L1 368 // b L2 369 // => 370 // bne L2 371 // b L1 372 LLVM_DEBUG(dbgs() << " Invert condition and swap " 373 "its destination with " 374 << MBB->back()); 375 376 removeBranch(MBB); 377 insertBranch(MBB, FBB, TBB, Cond); 378 finalizeBlockChanges(MBB, nullptr); 379 return true; 380 } 381 if (FBB) { 382 // We need to split the basic block here to obtain two long-range 383 // unconditional branches. 384 NewBB = createNewBlockAfter(*MBB); 385 386 insertUncondBranch(NewBB, FBB); 387 // Update the succesor lists according to the transformation to follow. 388 // Do it here since if there's no split, no update is needed. 389 MBB->replaceSuccessor(FBB, NewBB); 390 NewBB->addSuccessor(FBB); 391 } 392 393 // We now have an appropriate fall-through block in place (either naturally or 394 // just created), so we can use the inverted the condition. 395 MachineBasicBlock &NextBB = *std::next(MachineFunction::iterator(MBB)); 396 397 LLVM_DEBUG(dbgs() << " Insert B to " << printMBBReference(*TBB) 398 << ", invert condition and change dest. to " 399 << printMBBReference(NextBB) << '\n'); 400 401 removeBranch(MBB); 402 // Insert a new conditional branch and a new unconditional branch. 403 insertBranch(MBB, &NextBB, TBB, Cond); 404 405 finalizeBlockChanges(MBB, NewBB); 406 return true; 407 } 408 // Branch cond can't be inverted. 409 // In this case we always add a block after the MBB. 410 LLVM_DEBUG(dbgs() << " The branch condition can't be inverted. " 411 << " Insert a new BB after " << MBB->back()); 412 413 if (!FBB) 414 FBB = &(*std::next(MachineFunction::iterator(MBB))); 415 416 // This is the block with cond. branch and the distance to TBB is too long. 417 // beq L1 418 // L2: 419 420 // We do the following transformation: 421 // beq NewBB 422 // b L2 423 // NewBB: 424 // b L1 425 // L2: 426 427 NewBB = createNewBlockAfter(*MBB); 428 insertUncondBranch(NewBB, TBB); 429 430 LLVM_DEBUG(dbgs() << " Insert cond B to the new BB " 431 << printMBBReference(*NewBB) 432 << " Keep the exiting condition.\n" 433 << " Insert B to " << printMBBReference(*FBB) << ".\n" 434 << " In the new BB: Insert B to " 435 << printMBBReference(*TBB) << ".\n"); 436 437 // Update the successor lists according to the transformation to follow. 438 MBB->replaceSuccessor(TBB, NewBB); 439 NewBB->addSuccessor(TBB); 440 441 // Replace branch in the current (MBB) block. 442 removeBranch(MBB); 443 insertBranch(MBB, NewBB, FBB, Cond); 444 445 finalizeBlockChanges(MBB, NewBB); 446 return true; 447 } 448 449 bool BranchRelaxation::fixupUnconditionalBranch(MachineInstr &MI) { 450 MachineBasicBlock *MBB = MI.getParent(); 451 SmallVector<MachineOperand, 4> Cond; 452 unsigned OldBrSize = TII->getInstSizeInBytes(MI); 453 MachineBasicBlock *DestBB = TII->getBranchDestBlock(MI); 454 455 int64_t DestOffset = BlockInfo[DestBB->getNumber()].Offset; 456 int64_t SrcOffset = getInstrOffset(MI); 457 458 assert(!TII->isBranchOffsetInRange(MI.getOpcode(), DestOffset - SrcOffset)); 459 460 BlockInfo[MBB->getNumber()].Size -= OldBrSize; 461 462 MachineBasicBlock *BranchBB = MBB; 463 464 // If this was an expanded conditional branch, there is already a single 465 // unconditional branch in a block. 466 if (!MBB->empty()) { 467 BranchBB = createNewBlockAfter(*MBB); 468 469 // Add live outs. 470 for (const MachineBasicBlock *Succ : MBB->successors()) { 471 for (const MachineBasicBlock::RegisterMaskPair &LiveIn : Succ->liveins()) 472 BranchBB->addLiveIn(LiveIn); 473 } 474 475 BranchBB->sortUniqueLiveIns(); 476 BranchBB->addSuccessor(DestBB); 477 MBB->replaceSuccessor(DestBB, BranchBB); 478 } 479 480 DebugLoc DL = MI.getDebugLoc(); 481 MI.eraseFromParent(); 482 483 // Create the optional restore block and, initially, place it at the end of 484 // function. That block will be placed later if it's used; otherwise, it will 485 // be erased. 486 MachineBasicBlock *RestoreBB = createNewBlockAfter(MF->back(), 487 DestBB->getBasicBlock()); 488 489 TII->insertIndirectBranch(*BranchBB, *DestBB, *RestoreBB, DL, 490 DestOffset - SrcOffset, RS.get()); 491 492 BlockInfo[BranchBB->getNumber()].Size = computeBlockSize(*BranchBB); 493 adjustBlockOffsets(*MBB); 494 495 // If RestoreBB is required, try to place just before DestBB. 496 if (!RestoreBB->empty()) { 497 // TODO: For multiple far branches to the same destination, there are 498 // chances that some restore blocks could be shared if they clobber the 499 // same registers and share the same restore sequence. So far, those 500 // restore blocks are just duplicated for each far branch. 501 assert(!DestBB->isEntryBlock()); 502 MachineBasicBlock *PrevBB = &*std::prev(DestBB->getIterator()); 503 // Fall through only if PrevBB has no unconditional branch as one of its 504 // terminators. 505 if (auto *FT = PrevBB->getLogicalFallThrough()) { 506 assert(FT == DestBB); 507 TII->insertUnconditionalBranch(*PrevBB, FT, DebugLoc()); 508 BlockInfo[PrevBB->getNumber()].Size = computeBlockSize(*PrevBB); 509 } 510 // Now, RestoreBB could be placed directly before DestBB. 511 MF->splice(DestBB->getIterator(), RestoreBB->getIterator()); 512 // Update successors and predecessors. 513 RestoreBB->addSuccessor(DestBB); 514 BranchBB->replaceSuccessor(DestBB, RestoreBB); 515 if (TRI->trackLivenessAfterRegAlloc(*MF)) 516 computeAndAddLiveIns(LiveRegs, *RestoreBB); 517 // Compute the restore block size. 518 BlockInfo[RestoreBB->getNumber()].Size = computeBlockSize(*RestoreBB); 519 // Update the offset starting from the previous block. 520 adjustBlockOffsets(*PrevBB); 521 } else { 522 // Remove restore block if it's not required. 523 MF->erase(RestoreBB); 524 } 525 526 return true; 527 } 528 529 bool BranchRelaxation::relaxBranchInstructions() { 530 bool Changed = false; 531 532 // Relaxing branches involves creating new basic blocks, so re-eval 533 // end() for termination. 534 for (MachineBasicBlock &MBB : *MF) { 535 // Empty block? 536 MachineBasicBlock::iterator Last = MBB.getLastNonDebugInstr(); 537 if (Last == MBB.end()) 538 continue; 539 540 // Expand the unconditional branch first if necessary. If there is a 541 // conditional branch, this will end up changing the branch destination of 542 // it to be over the newly inserted indirect branch block, which may avoid 543 // the need to try expanding the conditional branch first, saving an extra 544 // jump. 545 if (Last->isUnconditionalBranch()) { 546 // Unconditional branch destination might be unanalyzable, assume these 547 // are OK. 548 if (MachineBasicBlock *DestBB = TII->getBranchDestBlock(*Last)) { 549 if (!isBlockInRange(*Last, *DestBB)) { 550 fixupUnconditionalBranch(*Last); 551 ++NumUnconditionalRelaxed; 552 Changed = true; 553 } 554 } 555 } 556 557 // Loop over the conditional branches. 558 MachineBasicBlock::iterator Next; 559 for (MachineBasicBlock::iterator J = MBB.getFirstTerminator(); 560 J != MBB.end(); J = Next) { 561 Next = std::next(J); 562 MachineInstr &MI = *J; 563 564 if (!MI.isConditionalBranch()) 565 continue; 566 567 MachineBasicBlock *DestBB = TII->getBranchDestBlock(MI); 568 if (!isBlockInRange(MI, *DestBB)) { 569 if (Next != MBB.end() && Next->isConditionalBranch()) { 570 // If there are multiple conditional branches, this isn't an 571 // analyzable block. Split later terminators into a new block so 572 // each one will be analyzable. 573 574 splitBlockBeforeInstr(*Next, DestBB); 575 } else { 576 fixupConditionalBranch(MI); 577 ++NumConditionalRelaxed; 578 } 579 580 Changed = true; 581 582 // This may have modified all of the terminators, so start over. 583 Next = MBB.getFirstTerminator(); 584 } 585 } 586 } 587 588 return Changed; 589 } 590 591 bool BranchRelaxation::runOnMachineFunction(MachineFunction &mf) { 592 MF = &mf; 593 594 LLVM_DEBUG(dbgs() << "***** BranchRelaxation *****\n"); 595 596 const TargetSubtargetInfo &ST = MF->getSubtarget(); 597 TII = ST.getInstrInfo(); 598 599 TRI = ST.getRegisterInfo(); 600 if (TRI->trackLivenessAfterRegAlloc(*MF)) 601 RS.reset(new RegScavenger()); 602 603 // Renumber all of the machine basic blocks in the function, guaranteeing that 604 // the numbers agree with the position of the block in the function. 605 MF->RenumberBlocks(); 606 607 // Do the initial scan of the function, building up information about the 608 // sizes of each block. 609 scanFunction(); 610 611 LLVM_DEBUG(dbgs() << " Basic blocks before relaxation\n"; dumpBBs();); 612 613 bool MadeChange = false; 614 while (relaxBranchInstructions()) 615 MadeChange = true; 616 617 // After a while, this might be made debug-only, but it is not expensive. 618 verify(); 619 620 LLVM_DEBUG(dbgs() << " Basic blocks after relaxation\n\n"; dumpBBs()); 621 622 BlockInfo.clear(); 623 624 return MadeChange; 625 } 626