20 #include "llvm/Config/llvm-config.h" 36 #define DEBUG_TYPE "branch-relaxation" 38 STATISTIC(NumSplit,
"Number of basic blocks split");
39 STATISTIC(NumConditionalRelaxed,
"Number of conditional branches relaxed");
40 STATISTIC(NumUnconditionalRelaxed,
"Number of unconditional branches relaxed");
42 #define BRANCH_RELAX_NAME "Branch relaxation pass" 68 const unsigned PO = Offset +
Size;
74 if (Alignment <= ParentAlign)
84 std::unique_ptr<RegScavenger> RS;
91 bool relaxBranchInstructions();
129 unsigned PrevNum = MF->begin()->getNumber();
131 const unsigned Num = MBB.getNumber();
133 assert(!Num || BlockInfo[PrevNum].postOffset(MBB) <= BlockInfo[Num].
Offset);
134 assert(BlockInfo[Num].
Size == computeBlockSize(MBB));
140 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 143 for (
auto &MBB : *MF) {
153 void BranchRelaxation::scanFunction() {
155 BlockInfo.resize(MF->getNumBlockIDs());
162 BlockInfo[MBB.getNumber()].Size = computeBlockSize(MBB);
165 adjustBlockOffsets(*MF->begin());
169 uint64_t BranchRelaxation::computeBlockSize(
const MachineBasicBlock &MBB)
const {
172 Size +=
TII->getInstSizeInBytes(
MI);
179 unsigned BranchRelaxation::getInstrOffset(
const MachineInstr &
MI)
const {
189 assert(
I != MBB->
end() &&
"Didn't find MI in its own basic block?");
190 Offset +=
TII->getInstSizeInBytes(*
I);
204 BlockInfo[Num].Offset = BlockInfo[PrevNum].postOffset(MBB);
242 TII->insertUnconditionalBranch(*OrigBB, NewBB,
DebugLoc());
261 BlockInfo[OrigBB->
getNumber()].Size = computeBlockSize(*OrigBB);
265 BlockInfo[NewBB->
getNumber()].Size = computeBlockSize(*NewBB);
268 adjustBlockOffsets(*OrigBB);
271 if (
TRI->trackLivenessAfterRegAlloc(*MF))
281 bool BranchRelaxation::isBlockInRange(
283 int64_t BrOffset = getInstrOffset(MI);
284 int64_t DestOffset = BlockInfo[DestBB.
getNumber()].Offset;
286 if (
TII->isBranchOffsetInRange(MI.
getOpcode(), DestOffset - BrOffset))
292 << DestOffset <<
" offset " << DestOffset - BrOffset <<
'\t' 301 bool BranchRelaxation::fixupConditionalBranch(
MachineInstr &MI) {
310 unsigned &BBSize = BlockInfo[MBB->
getNumber()].Size;
312 TII->insertUnconditionalBranch(*MBB, DestBB, DL, &NewBrSize);
318 unsigned &BBSize = BlockInfo[MBB->
getNumber()].Size;
324 unsigned &BBSize = BlockInfo[MBB->
getNumber()].Size;
327 BBSize -= RemovedSize;
333 adjustBlockOffsets(*MBB);
336 if (NewBB &&
TRI->trackLivenessAfterRegAlloc(*MF))
341 assert(!Fail &&
"branches to be relaxed must be analyzable");
354 if (FBB && isBlockInRange(MI, *FBB)) {
363 "its destination with " 367 insertBranch(MBB, FBB, TBB, Cond);
368 finalizeBlockChanges(MBB,
nullptr);
374 NewBB = createNewBlockAfter(*MBB);
376 insertUncondBranch(NewBB, FBB);
388 <<
", invert condition and change dest. to " 393 insertBranch(MBB, &NextBB, TBB, Cond);
395 finalizeBlockChanges(MBB, NewBB);
401 <<
" Insert a new BB after " << MBB->
back());
417 NewBB = createNewBlockAfter(*MBB);
418 insertUncondBranch(NewBB, TBB);
422 <<
" Keep the exiting condition.\n" 424 <<
" In the new BB: Insert B to " 433 insertBranch(MBB, NewBB, FBB, Cond);
435 finalizeBlockChanges(MBB, NewBB);
439 bool BranchRelaxation::fixupUnconditionalBranch(
MachineInstr &MI) {
442 unsigned OldBrSize =
TII->getInstSizeInBytes(MI);
445 int64_t DestOffset = BlockInfo[DestBB->
getNumber()].Offset;
446 int64_t SrcOffset = getInstrOffset(MI);
450 BlockInfo[MBB->
getNumber()].Size -= OldBrSize;
457 BranchBB = createNewBlockAfter(*MBB);
472 BlockInfo[BranchBB->
getNumber()].Size +=
TII->insertIndirectBranch(
473 *BranchBB, *DestBB, DL, DestOffset - SrcOffset, RS.
get());
475 adjustBlockOffsets(*MBB);
479 bool BranchRelaxation::relaxBranchInstructions() {
480 bool Changed =
false;
489 if (Last == MBB.
end())
497 if (Last->isUnconditionalBranch()) {
501 if (!isBlockInRange(*Last, *DestBB)) {
502 fixupUnconditionalBranch(*Last);
503 ++NumUnconditionalRelaxed;
512 J != MBB.
end(); J = Next) {
518 if (!isBlockInRange(MI, *DestBB)) {
519 if (Next != MBB.
end() && Next->isConditionalBranch()) {
524 splitBlockBeforeInstr(*Next, DestBB);
526 fixupConditionalBranch(MI);
527 ++NumConditionalRelaxed;
551 if (
TRI->trackLivenessAfterRegAlloc(*MF))
556 MF->RenumberBlocks();
562 LLVM_DEBUG(
dbgs() <<
" Basic blocks before relaxation\n"; dumpBBs(););
564 bool MadeChange =
false;
565 while (relaxBranchInstructions())
571 LLVM_DEBUG(
dbgs() <<
" Basic blocks after relaxation\n\n"; dumpBBs());
DILocation * get() const
Get the underlying DILocation.
This class represents lattice values for constants.
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
Align getAlignment() const
getAlignment - Return the alignment of the function.
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
iterator getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic block.
void transferSuccessors(MachineBasicBlock *FromMBB)
Transfers all the successors from MBB to this machine basic block (i.e., copies all the successors Fr...
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
bool reverseBranchCondition(SmallVectorImpl< MachineOperand > &Cond) const override
Reverses the branch condition of the specified condition list, returning false on success and true if...
unsigned Offset
Offset - Distance from the beginning of the function to the beginning of this basic block...
char & BranchRelaxationPassID
BranchRelaxation - This pass replaces branches that need to jump further than is supported by a branc...
STATISTIC(NumFunctions, "Total number of functions")
unsigned const TargetRegisterInfo * TRI
uint64_t offsetToAlignment(uint64_t Value, Align Alignment)
Returns the offset to the next integer (mod 2**64) that is greater than or equal to Value and is a mu...
BasicBlockInfo - Information about the offset and size of a single basic block.
iterator_range< succ_iterator > successors()
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...
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Align getAlignment() const
Return alignment of the basic block.
iterator getLastNonDebugInstr()
Returns an iterator to the last non-debug instruction in the basic block, or end().
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
virtual const TargetInstrInfo * getInstrInfo() const
uint64_t value() const
This is a hole in the type system and should not be abused.
instr_iterator insert(instr_iterator I, MachineInstr *M)
Insert MI into the instruction list before I, possibly inside a bundle.
TargetInstrInfo - Interface to description of machine instruction set.
void addLiveIn(MCRegister PhysReg, LaneBitmask LaneMask=LaneBitmask::getAll())
Adds the specified register as a live in.
This file declares the machine register scavenger class.
self_iterator getIterator()
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
This file implements the LivePhysRegs utility for tracking liveness of physical registers.
bool verify(const TargetRegisterInfo &TRI) const
Check that information hold by this instance make sense for the given TRI.
This struct is a compact representation of a valid (non-zero power of two) alignment.
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
bool isConditionalBranch(QueryType Type=AnyInBundle) const
Return true if this is a branch which may fall through to the next instruction or may transfer contro...
Iterator for intrusive lists based on ilist_node.
void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
void updateTerminator()
Update the terminator instructions in block to account for changes to the layout. ...
unsigned Size
Size - Size of the basic block in bytes.
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 isAligned(Align Lhs, uint64_t SizeInBytes)
Checks that SizeInBytes is a multiple of the alignment.
void sortUniqueLiveIns()
Sorts and uniques the LiveIns vector.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
void replaceSuccessor(MachineBasicBlock *Old, MachineBasicBlock *New)
Replace successor OLD with NEW and update probability info.
unsigned removeBranch(MachineBasicBlock &MBB, int *BytesRemoved=nullptr) const override
Remove the branching code at the end of the specific MBB.
void computeAndAddLiveIns(LivePhysRegs &LiveRegs, MachineBasicBlock &MBB)
Convenience function combining computeLiveIns() and addLiveIns().
const MachineBasicBlock * getParent() const
TargetSubtargetInfo - Generic base class for all target subtargets.
Representation of each machine instruction.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
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 '...
A set of physical registers with utility functions to track liveness when walking backward/forward th...
Pair of physical register and lane mask.
const BasicBlock * getBasicBlock() const
Return the LLVM basic block that this instance corresponded to originally.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
#define BRANCH_RELAX_NAME
StringRef - Represent a constant reference to a string, i.e.
static cl::opt< bool > BranchRelaxation("aarch64-enable-branch-relax", cl::Hidden, cl::init(true), cl::desc("Relax out of range conditional branches"))