22#include "llvm/Config/llvm-config.h"
39#define DEBUG_TYPE "branch-relaxation"
42STATISTIC(NumConditionalRelaxed,
"Number of conditional branches relaxed");
43STATISTIC(NumUnconditionalRelaxed,
"Number of unconditional branches relaxed");
45#define BRANCH_RELAX_NAME "Branch relaxation pass"
49class BranchRelaxation {
72 const Align Alignment =
MBB.getAlignment();
73 const Align ParentAlign =
MBB.getParent()->getAlignment();
74 if (Alignment <= ParentAlign)
79 return alignTo(PO, Alignment) + Alignment.value() - ParentAlign.
value();
89 RelaxedUnconditionals;
90 std::unique_ptr<RegScavenger> RS;
98 bool relaxBranchInstructions();
141char BranchRelaxationLegacy::ID = 0;
149void BranchRelaxation::
verify() {
151 unsigned PrevNum = MF->begin()->getNumber();
153 const unsigned Num =
MBB.getNumber();
154 assert(!Num || BlockInfo[PrevNum].postOffset(
MBB) <= BlockInfo[Num].
Offset);
161 J !=
MBB.end(); J = std::next(J)) {
163 if (!
MI.isConditionalBranch() && !
MI.isUnconditionalBranch())
165 if (
MI.getOpcode() == TargetOpcode::FAULTING_OP)
168 assert(isBlockInRange(
MI, *DestBB) ||
169 RelaxedUnconditionals.contains({&MBB, DestBB}));
175#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
178 for (
auto &
MBB : *MF) {
188void BranchRelaxation::scanFunction() {
190 BlockInfo.resize(MF->getNumBlockIDs());
192 TrampolineInsertionPoint =
nullptr;
193 RelaxedUnconditionals.clear();
200 for (MachineBasicBlock &
MBB : *MF) {
204 TrampolineInsertionPoint = &
MBB;
208 adjustBlockOffsets(*MF->begin());
210 if (TrampolineInsertionPoint ==
nullptr) {
211 LLVM_DEBUG(
dbgs() <<
" No suitable trampoline insertion point found in "
212 << MF->getName() <<
".\n");
218BranchRelaxation::computeBlockSize(
const MachineBasicBlock &
MBB)
const {
220 for (
const MachineInstr &
MI :
MBB)
228unsigned BranchRelaxation::getInstrOffset(
const MachineInstr &
MI)
const {
238 assert(
I !=
MBB->
end() &&
"Didn't find MI in its own basic block?");
245void BranchRelaxation::adjustBlockOffsets(MachineBasicBlock &Start) {
246 adjustBlockOffsets(Start, MF->end());
249void BranchRelaxation::adjustBlockOffsets(MachineBasicBlock &Start,
251 unsigned PrevNum =
Start.getNumber();
257 BlockInfo[Num].Offset = BlockInfo[PrevNum].postOffset(
MBB);
265BranchRelaxation::createNewBlockAfter(MachineBasicBlock &OrigBB) {
272BranchRelaxation::createNewBlockAfter(MachineBasicBlock &OrigMBB,
273 const BasicBlock *BB) {
275 MachineBasicBlock *NewBB = MF->CreateMachineBasicBlock(BB);
284 BlockInfo.insert(BlockInfo.begin() + NewBB->
getNumber(), BasicBlockInfo());
289 adjustBlockOffsets(OrigMBB, std::next(NewBB->
getIterator()));
298BranchRelaxation::splitBlockBeforeInstr(MachineInstr &
MI,
299 MachineBasicBlock *DestBB) {
303 MachineBasicBlock *NewBB =
313 NewBB->
splice(NewBB->
end(), OrigBB,
MI.getIterator(), OrigBB->
end());
319 TII->insertUnconditionalBranch(*OrigBB, NewBB,
DebugLoc());
322 BlockInfo.insert(BlockInfo.begin() + NewBB->
getNumber(), BasicBlockInfo());
337 BlockInfo[OrigBB->
getNumber()].Size = computeBlockSize(*OrigBB);
341 BlockInfo[NewBB->
getNumber()].Size = computeBlockSize(*NewBB);
344 adjustBlockOffsets(*OrigBB, std::next(NewBB->
getIterator()));
347 if (
TRI->trackLivenessAfterRegAlloc(*MF))
357bool BranchRelaxation::isBlockInRange(
const MachineInstr &
MI,
358 const MachineBasicBlock &DestBB)
const {
359 int64_t BrOffset = getInstrOffset(
MI);
360 int64_t DestOffset = BlockInfo[DestBB.
getNumber()].Offset;
362 const MachineBasicBlock *SrcBB =
MI.getParent();
364 if (
TII->isBranchOffsetInRange(
MI.getOpcode(),
367 : DestOffset - BrOffset))
373 << DestOffset <<
" offset " << DestOffset - BrOffset <<
'\t'
382bool BranchRelaxation::fixupConditionalBranch(MachineInstr &
MI) {
385 MachineBasicBlock *
TBB =
nullptr, *FBB =
nullptr;
386 MachineBasicBlock *NewBB =
nullptr;
389 auto insertUncondBranch = [&](MachineBasicBlock *
MBB,
390 MachineBasicBlock *DestBB) {
393 TII->insertUnconditionalBranch(*
MBB, DestBB,
DL, &NewBrSize);
396 auto insertBranch = [&](MachineBasicBlock *
MBB, MachineBasicBlock *
TBB,
397 MachineBasicBlock *FBB,
398 SmallVectorImpl<MachineOperand> &
Cond) {
404 auto removeBranch = [&](MachineBasicBlock *
MBB) {
408 BBSize -= RemovedSize;
413 assert(NewBB !=
nullptr &&
"can't update liveness for nullptr");
416 if (
TRI->trackLivenessAfterRegAlloc(*MF))
421 assert(!
Fail &&
"branches to be relaxed must be analyzable");
436 TrampolineInsertionPoint !=
nullptr) {
441 if (isBlockInRange(
MI, *NewBB)) {
445 insertUncondBranch(NewBB,
TBB);
453 insertBranch(
MBB, NewBB, FBB,
Cond);
455 TrampolineInsertionPoint = NewBB;
461 dbgs() <<
" Trampoline insertion point out of range for Bcc from "
479 if (FBB && isBlockInRange(
MI, *FBB)) {
488 "its destination with "
507 insertUncondBranch(
MBB,
TBB);
512 NewBB = createNewBlockAfter(*
MBB);
514 insertUncondBranch(NewBB, FBB);
527 <<
", invert condition and change dest. to "
538 <<
" Insert a new BB after " <<
MBB->
back());
554 NewBB = createNewBlockAfter(*
MBB);
555 insertUncondBranch(NewBB,
TBB);
559 <<
" Keep the exiting condition.\n"
561 <<
" In the new BB: Insert B to "
570 insertBranch(
MBB, NewBB, FBB,
Cond);
576bool BranchRelaxation::fixupUnconditionalBranch(MachineInstr &
MI) {
578 unsigned OldBrSize =
TII->getInstSizeInBytes(
MI);
579 MachineBasicBlock *DestBB =
TII->getBranchDestBlock(
MI);
581 int64_t DestOffset = BlockInfo[DestBB->
getNumber()].Offset;
582 int64_t SrcOffset = getInstrOffset(
MI);
587 : DestOffset - SrcOffset));
591 MachineBasicBlock *BranchBB =
MBB;
596 BranchBB = createNewBlockAfter(*
MBB);
600 for (
const MachineBasicBlock::RegisterMaskPair &LiveIn : Succ->liveins())
607 if (TrampolineInsertionPoint ==
MBB)
608 TrampolineInsertionPoint = BranchBB;
617 MachineBasicBlock *RestoreBB =
623 TII->insertIndirectBranch(*BranchBB, *DestBB, *RestoreBB,
DL,
626 : DestOffset - SrcOffset,
631 BlockInfo[BranchBB->
getNumber()].Size = computeBlockSize(*BranchBB);
635 if (!RestoreBB->
empty()) {
640 MachineBasicBlock *NewBB = createNewBlockAfter(*TrampolineInsertionPoint);
641 TII->insertUnconditionalBranch(*NewBB, DestBB,
DebugLoc());
642 BlockInfo[NewBB->
getNumber()].Size = computeBlockSize(*NewBB);
643 adjustBlockOffsets(*TrampolineInsertionPoint,
647 TrampolineInsertionPoint = NewBB;
663 MachineBasicBlock *PrevBB = &*std::prev(DestBB->
getIterator());
668 TII->insertUnconditionalBranch(*PrevBB, FT,
DebugLoc());
669 BlockInfo[PrevBB->
getNumber()].Size = computeBlockSize(*PrevBB);
676 if (
TRI->trackLivenessAfterRegAlloc(*MF))
679 BlockInfo[RestoreBB->
getNumber()].Size = computeBlockSize(*RestoreBB);
681 adjustBlockOffsets(*PrevBB, DestBB->
getIterator());
687 RelaxedUnconditionals.insert({BranchBB, RestoreBB});
690 MF->erase(RestoreBB);
691 RelaxedUnconditionals.insert({BranchBB, DestBB});
697bool BranchRelaxation::relaxBranchInstructions() {
702 for (MachineBasicBlock &
MBB : *MF) {
713 if (
Last->isUnconditionalBranch()) {
716 if (MachineBasicBlock *DestBB =
TII->getBranchDestBlock(*
Last)) {
718 !RelaxedUnconditionals.contains({&MBB, DestBB})) {
719 fixupUnconditionalBranch(*
Last);
720 ++NumUnconditionalRelaxed;
731 MachineInstr &
MI = *J;
733 if (!
MI.isConditionalBranch())
736 if (
MI.getOpcode() == TargetOpcode::FAULTING_OP)
741 MachineBasicBlock *DestBB =
TII->getBranchDestBlock(
MI);
742 if (!isBlockInRange(
MI, *DestBB)) {
748 splitBlockBeforeInstr(*
Next, DestBB);
750 fixupConditionalBranch(
MI);
751 ++NumConditionalRelaxed;
766 adjustBlockOffsets(MF->front());
785 TII = ST.getInstrInfo();
788 TRI = ST.getRegisterInfo();
789 if (
TRI->trackLivenessAfterRegAlloc(*MF))
794 MF->RenumberBlocks();
800 LLVM_DEBUG(
dbgs() <<
" Basic blocks before relaxation\n"; dumpBBs(););
802 bool MadeChange =
false;
803 while (relaxBranchInstructions())
809 LLVM_DEBUG(
dbgs() <<
" Basic blocks after relaxation\n\n"; dumpBBs());
812 RelaxedUnconditionals.clear();
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static cl::opt< bool > BranchRelaxation("aarch64-enable-branch-relax", cl::Hidden, cl::init(true), cl::desc("Relax out of range conditional branches"))
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
#define BRANCH_RELAX_NAME
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
const HexagonInstrInfo * TII
This file implements the LivePhysRegs utility for tracking liveness of physical registers.
Register const TargetRegisterInfo * TRI
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
static void updateLiveness(MachineFunction &MF)
Helper function to update the liveness information for the callee-saved registers.
const SmallVectorImpl< MachineOperand > MachineBasicBlock * TBB
const SmallVectorImpl< MachineOperand > & Cond
This file declares the machine register scavenger class.
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
LLVM Basic Block Representation.
LLVM_ABI PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
unsigned removeBranch(MachineBasicBlock &MBB, int *BytesRemoved=nullptr) const override
Remove the branching code at the end of the specific MBB.
bool analyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB, MachineBasicBlock *&FBB, SmallVectorImpl< MachineOperand > &Cond, bool AllowModify) const override
Analyze the branching code at the end of MBB, returning true if it cannot be understood (e....
bool reverseBranchCondition(SmallVectorImpl< MachineOperand > &Cond) const override
Reverses the branch condition of the specified condition list, returning false on success and true if...
unsigned insertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB, MachineBasicBlock *FBB, ArrayRef< MachineOperand > Cond, const DebugLoc &DL, int *BytesAdded=nullptr) const override
Insert branch code into the end of the specified MachineBasicBlock.
bool isTailCall(const MachineInstr &MI) const override
A set of physical registers with utility functions to track liveness when walking backward/forward th...
void setIsEndSection(bool V=true)
MachineInstrBundleIterator< const MachineInstr > const_iterator
MachineBasicBlock * getLogicalFallThrough()
Return the fallthrough block if the block can implicitly transfer control to it's successor,...
LLVM_ABI void replaceSuccessor(MachineBasicBlock *Old, MachineBasicBlock *New)
Replace successor OLD with NEW and update probability info.
LLVM_ABI void transferSuccessors(MachineBasicBlock *FromMBB)
Transfers all the successors from MBB to this machine basic block (i.e., copies all the successors Fr...
LLVM_ABI instr_iterator insert(instr_iterator I, MachineInstr *M)
Insert MI into the instruction list before I, possibly inside a bundle.
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
const BasicBlock * getBasicBlock() const
Return the LLVM basic block that this instance corresponded to originally.
LLVM_ABI void updateTerminator(MachineBasicBlock *PreviousLayoutSuccessor)
Update the terminator instructions in block to account for changes to block layout which may have bee...
LLVM_ABI iterator getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic block.
MBBSectionID getSectionID() const
Returns the section ID of this basic block.
LLVM_ABI bool isEntryBlock() const
Returns true if this is the entry block of the function.
LLVM_ABI void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
LLVM_ABI void sortUniqueLiveIns()
Sorts and uniques the LiveIns vector.
void setSectionID(MBBSectionID V)
Sets the section ID for this basic block.
LLVM_ABI iterator getLastNonDebugInstr(bool SkipPseudoOp=true)
Returns an iterator to the last non-debug instruction in the basic block, or end().
LLVM_ABI void eraseFromParent()
This method unlinks 'this' from the containing function and deletes it.
void addLiveIn(MCRegister PhysReg, LaneBitmask LaneMask=LaneBitmask::getAll())
Adds the specified register as a live in.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
bool isBeginSection() const
Returns true if this block begins any section.
iterator_range< succ_iterator > successors()
void splice(iterator Where, MachineBasicBlock *Other, iterator From)
Take an instruction from MBB 'Other' at the position From, and insert it into this MBB right before '...
bool isEndSection() const
Returns true if this block ends any section.
MachineInstrBundleIterator< MachineInstr > iterator
void setIsBeginSection(bool V=true)
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
BasicBlockListType::iterator iterator
Representation of each machine instruction.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Implements a dense probed hash-table based set with some number of buckets stored inline.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
TargetInstrInfo - Interface to description of machine instruction set.
Primary interface to the complete machine description for the target machine.
uint64_t getMaxCodeSize() const
Returns the maximum code size possible under the code model.
const Target & getTarget() const
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
TargetSubtargetInfo - Generic base class for all target subtargets.
self_iterator getIterator()
This is an optimization pass for GlobalISel generic memory operations.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
LLVM_ABI char & BranchRelaxationPassID
BranchRelaxation - This pass replaces branches that need to jump further than is supported by a branc...
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
constexpr uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
LLVM_ABI void computeAndAddLiveIns(LivePhysRegs &LiveRegs, MachineBasicBlock &MBB)
Convenience function combining computeLiveIns() and addLiveIns().
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Next
LLVM_ABI Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.
This struct is a compact representation of a valid (non-zero power of two) alignment.
constexpr uint64_t value() const
This is a hole in the type system and should not be abused.
BasicBlockInfo - Information about the offset and size of a single basic block.
unsigned Size
Size - Size of the basic block in bytes.
unsigned Offset
Offset - Distance from the beginning of the function to the beginning of this basic block.
LLVM_ABI static const MBBSectionID ColdSectionID