24 #define DEBUG_TYPE "branch-relaxation"
26 STATISTIC(NumSplit,
"Number of basic blocks split");
27 STATISTIC(NumConditionalRelaxed,
"Number of conditional branches relaxed");
28 STATISTIC(NumUnconditionalRelaxed,
"Number of unconditional branches relaxed");
30 #define BRANCH_RELAX_NAME "Branch relaxation pass"
60 unsigned AlignAmt = 1 <<
Align;
62 if (Align <= ParentAlign)
72 std::unique_ptr<RegScavenger> RS;
79 bool relaxBranchInstructions();
117 unsigned PrevNum = MF->begin()->getNumber();
119 unsigned Align = MBB.getAlignment();
120 unsigned Num = MBB.getNumber();
122 assert(!Num || BlockInfo[PrevNum].postOffset(MBB) <= BlockInfo[Num].
Offset);
129 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
132 for (
auto &MBB : *MF) {
142 void BranchRelaxation::scanFunction() {
144 BlockInfo.resize(MF->getNumBlockIDs());
154 adjustBlockOffsets(*MF->begin());
161 Size +=
TII->getInstSizeInBytes(
MI);
168 unsigned BranchRelaxation::getInstrOffset(
const MachineInstr &
MI)
const {
178 assert(
I != MBB->
end() &&
"Didn't find MI in its own basic block?");
179 Offset +=
TII->getInstSizeInBytes(*
I);
193 BlockInfo[Num].Offset = BlockInfo[PrevNum].postOffset(MBB);
231 TII->insertUnconditionalBranch(*OrigBB, NewBB,
DebugLoc());
258 adjustBlockOffsets(*OrigBB);
261 if (TRI->trackLivenessAfterRegAlloc(*MF))
271 bool BranchRelaxation::isBlockInRange(
273 int64_t BrOffset = getInstrOffset(MI);
274 int64_t DestOffset = BlockInfo[DestBB.
getNumber()].Offset;
276 if (
TII->isBranchOffsetInRange(MI.
getOpcode(), DestOffset - BrOffset))
280 dbgs() <<
"Out of range branch to destination BB#" << DestBB.
getNumber()
282 <<
" to " << DestOffset
283 <<
" offset " << DestOffset - BrOffset
293 bool BranchRelaxation::fixupConditionalBranch(
MachineInstr &MI) {
300 assert(!Fail &&
"branches to be relaxed must be analyzable");
311 if (FBB && isBlockInRange(MI, *FBB)) {
319 DEBUG(
dbgs() <<
" Invert condition and swap "
320 "its destination with " << MBB->
back());
323 int OldSize = 0, NewSize = 0;
327 BlockInfo[MBB->
getNumber()].Size += (NewSize - OldSize);
332 auto &NewBB = *MF->CreateMachineBasicBlock(MBB->
getBasicBlock());
339 unsigned &NewBBSize = BlockInfo[NewBB.
getNumber()].Size;
341 TII->insertUnconditionalBranch(NewBB, FBB, DL, &NewBrSize);
342 NewBBSize += NewBrSize;
350 if (TRI->trackLivenessAfterRegAlloc(*MF))
359 <<
", invert condition and change dest. to BB#"
362 unsigned &MBBSize = BlockInfo[MBB->
getNumber()].Size;
368 MBBSize -= RemovedSize;
372 MBBSize += AddedSize;
375 adjustBlockOffsets(*MBB);
379 bool BranchRelaxation::fixupUnconditionalBranch(
MachineInstr &MI) {
382 unsigned OldBrSize =
TII->getInstSizeInBytes(MI);
385 int64_t DestOffset = BlockInfo[DestBB->
getNumber()].Offset;
386 int64_t SrcOffset = getInstrOffset(MI);
390 BlockInfo[MBB->
getNumber()].Size -= OldBrSize;
397 BranchBB = createNewBlockAfter(*MBB);
412 BlockInfo[BranchBB->
getNumber()].Size +=
TII->insertIndirectBranch(
413 *BranchBB, *DestBB, DL, DestOffset - SrcOffset, RS.
get());
415 adjustBlockOffsets(*MBB);
419 bool BranchRelaxation::relaxBranchInstructions() {
420 bool Changed =
false;
429 if (Last == MBB.
end())
437 if (Last->isUnconditionalBranch()) {
441 if (!isBlockInRange(*Last, *DestBB)) {
442 fixupUnconditionalBranch(*Last);
443 ++NumUnconditionalRelaxed;
452 J != MBB.
end(); J = Next) {
458 if (!isBlockInRange(MI, *DestBB)) {
459 if (Next != MBB.
end() && Next->isConditionalBranch()) {
464 splitBlockBeforeInstr(*Next, DestBB);
466 fixupConditionalBranch(MI);
467 ++NumConditionalRelaxed;
485 DEBUG(
dbgs() <<
"***** BranchRelaxation *****\n");
491 if (TRI->trackLivenessAfterRegAlloc(*MF))
496 MF->RenumberBlocks();
502 DEBUG(
dbgs() <<
" Basic blocks before relaxation\n"; dumpBBs(););
504 bool MadeChange =
false;
505 while (relaxBranchInstructions())
511 DEBUG(
dbgs() <<
" Basic blocks after relaxation\n\n"; dumpBBs());
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
unsigned getAlignment() const
getAlignment - Return the alignment (log2, not bytes) of the function.
STATISTIC(NumFunctions,"Total number of functions")
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
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...
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...
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...
void computeBlockSize(MachineFunction *MF, MachineBasicBlock *MBB, BasicBlockInfo &BBI)
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...
DILocation * get() const
Get the underlying DILocation.
const HexagonInstrInfo * TII
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
iterator getLastNonDebugInstr()
Returns an iterator to the last non-debug instruction in the basic block, or end().
instr_iterator insert(instr_iterator I, MachineInstr *M)
Insert MI into the instruction list before I, possibly inside a bundle.
const BasicBlock * getBasicBlock() const
Return the LLVM basic block that this instance corresponded to originally.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
const MachineBasicBlock * getParent() const
format_object< Ts...> format(const char *Fmt, const Ts &...Vals)
These are helper functions used to produce formatted output.
TargetInstrInfo - Interface to description of machine instruction set.
bool verify(const TargetRegisterInfo &TRI) const
Check that information hold by this instance make sense for the given TRI.
void addLiveIn(MCPhysReg PhysReg, LaneBitmask LaneMask=LaneBitmask::getAll())
Adds the specified register as a live in.
void computeLiveIns(LivePhysRegs &LiveRegs, const MachineRegisterInfo &MRI, MachineBasicBlock &MBB)
Computes the live-in list for MBB assuming all of its successors live-in lists are up-to-date...
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.
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
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.
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.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
TargetSubtargetInfo - Generic base class for all target subtargets.
Representation of each machine instruction.
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.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
virtual const TargetInstrInfo * getInstrInfo() const
#define BRANCH_RELAX_NAME
uint64_t OffsetToAlignment(uint64_t Value, uint64_t Align)
Returns the offset to the next integer (mod 2**64) that is greater than or equal to Value and is a mu...
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
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"))
unsigned getAlignment() const
Return alignment of the basic block.