2014-05-24 20:50:23 +08:00
|
|
|
//===-- AArch64BranchRelaxation.cpp - AArch64 branch relaxation -----------===//
|
2014-03-29 18:18:08 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-05-24 20:50:23 +08:00
|
|
|
#include "AArch64.h"
|
|
|
|
#include "AArch64InstrInfo.h"
|
|
|
|
#include "AArch64MachineFunctionInfo.h"
|
2014-08-05 05:25:23 +08:00
|
|
|
#include "AArch64Subtarget.h"
|
2014-03-29 18:18:08 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2014-07-25 19:42:14 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2014-03-29 18:18:08 +08:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
2014-07-25 19:42:14 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2014-03-29 18:18:08 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/Format.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-05-24 20:50:23 +08:00
|
|
|
#define DEBUG_TYPE "aarch64-branch-relax"
|
2014-04-22 10:41:26 +08:00
|
|
|
|
2014-03-29 18:18:08 +08:00
|
|
|
STATISTIC(NumSplit, "Number of basic blocks split");
|
2016-08-23 09:30:30 +08:00
|
|
|
STATISTIC(NumConditionalRelaxed, "Number of conditional branches relaxed");
|
2014-03-29 18:18:08 +08:00
|
|
|
|
2015-08-06 00:12:10 +08:00
|
|
|
namespace llvm {
|
|
|
|
void initializeAArch64BranchRelaxationPass(PassRegistry &);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define AARCH64_BR_RELAX_NAME "AArch64 branch relaxation pass"
|
|
|
|
|
2014-03-29 18:18:08 +08:00
|
|
|
namespace {
|
2014-05-24 20:50:23 +08:00
|
|
|
class AArch64BranchRelaxation : public MachineFunctionPass {
|
2014-03-29 18:18:08 +08:00
|
|
|
/// BasicBlockInfo - Information about the offset and size of a single
|
|
|
|
/// basic block.
|
|
|
|
struct BasicBlockInfo {
|
|
|
|
/// Offset - Distance from the beginning of the function to the beginning
|
|
|
|
/// of this basic block.
|
|
|
|
///
|
|
|
|
/// The offset is always aligned as required by the basic block.
|
|
|
|
unsigned Offset;
|
|
|
|
|
|
|
|
/// Size - Size of the basic block in bytes. If the block contains
|
|
|
|
/// inline assembly, this is a worst case estimate.
|
|
|
|
///
|
|
|
|
/// The size does not include any alignment padding whether from the
|
|
|
|
/// beginning of the block, or from an aligned jump table at the end.
|
|
|
|
unsigned Size;
|
|
|
|
|
|
|
|
BasicBlockInfo() : Offset(0), Size(0) {}
|
|
|
|
|
|
|
|
/// Compute the offset immediately following this block. If LogAlign is
|
|
|
|
/// specified, return the offset the successor block will get if it has
|
|
|
|
/// this alignment.
|
|
|
|
unsigned postOffset(unsigned LogAlign = 0) const {
|
|
|
|
unsigned PO = Offset + Size;
|
|
|
|
unsigned Align = 1 << LogAlign;
|
|
|
|
return (PO + Align - 1) / Align * Align;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
SmallVector<BasicBlockInfo, 16> BlockInfo;
|
|
|
|
|
|
|
|
MachineFunction *MF;
|
2014-05-24 20:50:23 +08:00
|
|
|
const AArch64InstrInfo *TII;
|
2014-03-29 18:18:08 +08:00
|
|
|
|
|
|
|
bool relaxBranchInstructions();
|
|
|
|
void scanFunction();
|
2016-08-02 15:20:09 +08:00
|
|
|
MachineBasicBlock *splitBlockBeforeInstr(MachineInstr &MI);
|
2014-04-16 08:42:46 +08:00
|
|
|
void adjustBlockOffsets(MachineBasicBlock &MBB);
|
2016-08-02 16:06:17 +08:00
|
|
|
bool isBlockInRange(const MachineInstr &MI, const MachineBasicBlock &BB) const;
|
2016-08-02 16:30:06 +08:00
|
|
|
|
|
|
|
unsigned insertInvertedConditionalBranch(MachineBasicBlock &SrcBB,
|
|
|
|
MachineBasicBlock::iterator InsPt,
|
|
|
|
const DebugLoc &DL,
|
2016-08-02 16:06:17 +08:00
|
|
|
const MachineInstr &OldBr,
|
|
|
|
MachineBasicBlock &NewDestBB) const;
|
|
|
|
unsigned insertUnconditionalBranch(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock &NewDestBB,
|
|
|
|
const DebugLoc &DL) const;
|
|
|
|
|
2016-08-02 15:20:09 +08:00
|
|
|
bool fixupConditionalBranch(MachineInstr &MI);
|
2014-04-16 08:42:46 +08:00
|
|
|
void computeBlockSize(const MachineBasicBlock &MBB);
|
2016-08-02 16:06:17 +08:00
|
|
|
unsigned getInstrOffset(const MachineInstr &MI) const;
|
2014-03-29 18:18:08 +08:00
|
|
|
void dumpBBs();
|
|
|
|
void verify();
|
|
|
|
|
|
|
|
public:
|
|
|
|
static char ID;
|
2015-08-06 00:12:10 +08:00
|
|
|
AArch64BranchRelaxation() : MachineFunctionPass(ID) {
|
|
|
|
initializeAArch64BranchRelaxationPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2014-03-29 18:18:08 +08:00
|
|
|
|
2014-04-29 15:58:25 +08:00
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override;
|
2014-03-29 18:18:08 +08:00
|
|
|
|
2014-04-29 15:58:25 +08:00
|
|
|
const char *getPassName() const override {
|
2015-08-06 00:12:10 +08:00
|
|
|
return AARCH64_BR_RELAX_NAME;
|
2014-03-29 18:18:08 +08:00
|
|
|
}
|
|
|
|
};
|
2014-05-24 20:50:23 +08:00
|
|
|
char AArch64BranchRelaxation::ID = 0;
|
2015-06-23 17:49:53 +08:00
|
|
|
}
|
2014-03-29 18:18:08 +08:00
|
|
|
|
2015-08-06 00:12:10 +08:00
|
|
|
INITIALIZE_PASS(AArch64BranchRelaxation, "aarch64-branch-relax",
|
|
|
|
AARCH64_BR_RELAX_NAME, false, false)
|
|
|
|
|
2014-03-29 18:18:08 +08:00
|
|
|
/// verify - check BBOffsets, BBSizes, alignment of islands
|
2014-05-24 20:50:23 +08:00
|
|
|
void AArch64BranchRelaxation::verify() {
|
2014-03-29 18:18:08 +08:00
|
|
|
#ifndef NDEBUG
|
|
|
|
unsigned PrevNum = MF->begin()->getNumber();
|
2014-04-16 08:42:46 +08:00
|
|
|
for (MachineBasicBlock &MBB : *MF) {
|
|
|
|
unsigned Align = MBB.getAlignment();
|
|
|
|
unsigned Num = MBB.getNumber();
|
2014-03-29 18:18:08 +08:00
|
|
|
assert(BlockInfo[Num].Offset % (1u << Align) == 0);
|
|
|
|
assert(!Num || BlockInfo[PrevNum].postOffset() <= BlockInfo[Num].Offset);
|
|
|
|
PrevNum = Num;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/// print block size and offset information - debugging
|
2014-05-24 20:50:23 +08:00
|
|
|
void AArch64BranchRelaxation::dumpBBs() {
|
2014-04-04 07:43:26 +08:00
|
|
|
for (auto &MBB : *MF) {
|
2014-04-03 02:00:39 +08:00
|
|
|
const BasicBlockInfo &BBI = BlockInfo[MBB.getNumber()];
|
|
|
|
dbgs() << format("BB#%u\toffset=%08x\t", MBB.getNumber(), BBI.Offset)
|
2014-03-29 18:18:08 +08:00
|
|
|
<< format("size=%#x\n", BBI.Size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-02 16:06:17 +08:00
|
|
|
// FIXME: This is a less precise version of MachineBasicBlock::canFallThrough?
|
|
|
|
|
|
|
|
/// \returns true if the specified basic block can fallthrough
|
2014-03-29 18:18:08 +08:00
|
|
|
/// into the block immediately after it.
|
2016-08-02 16:06:17 +08:00
|
|
|
static bool hasFallthrough(const MachineBasicBlock &MBB) {
|
2014-03-29 18:18:08 +08:00
|
|
|
// Get the next machine basic block in the function.
|
2016-08-02 16:06:17 +08:00
|
|
|
MachineFunction::const_iterator MBBI(MBB);
|
|
|
|
|
2014-03-29 18:18:08 +08:00
|
|
|
// Can't fall off end of function.
|
2015-10-14 04:02:15 +08:00
|
|
|
auto NextBB = std::next(MBBI);
|
2016-08-02 16:06:17 +08:00
|
|
|
if (NextBB == MBB.getParent()->end())
|
2014-03-29 18:18:08 +08:00
|
|
|
return false;
|
|
|
|
|
2016-08-02 16:06:17 +08:00
|
|
|
return MBB.isSuccessor(&*NextBB);
|
2014-03-29 18:18:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// scanFunction - Do the initial scan of the function, building up
|
|
|
|
/// information about each block.
|
2014-05-24 20:50:23 +08:00
|
|
|
void AArch64BranchRelaxation::scanFunction() {
|
2014-03-29 18:18:08 +08:00
|
|
|
BlockInfo.clear();
|
|
|
|
BlockInfo.resize(MF->getNumBlockIDs());
|
|
|
|
|
|
|
|
// First thing, compute the size of all basic blocks, and see if the function
|
|
|
|
// has any inline assembly in it. If so, we have to be conservative about
|
|
|
|
// alignment assumptions, as we don't know for sure the size of any
|
|
|
|
// instructions in the inline assembly.
|
2014-04-16 08:42:46 +08:00
|
|
|
for (MachineBasicBlock &MBB : *MF)
|
|
|
|
computeBlockSize(MBB);
|
2014-03-29 18:18:08 +08:00
|
|
|
|
|
|
|
// Compute block offsets and known bits.
|
2014-04-16 08:42:46 +08:00
|
|
|
adjustBlockOffsets(*MF->begin());
|
2014-03-29 18:18:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// computeBlockSize - Compute the size for MBB.
|
|
|
|
/// This function updates BlockInfo directly.
|
2014-05-24 20:50:23 +08:00
|
|
|
void AArch64BranchRelaxation::computeBlockSize(const MachineBasicBlock &MBB) {
|
2014-03-29 18:18:08 +08:00
|
|
|
unsigned Size = 0;
|
2014-04-16 08:42:46 +08:00
|
|
|
for (const MachineInstr &MI : MBB)
|
2016-07-29 00:32:22 +08:00
|
|
|
Size += TII->getInstSizeInBytes(MI);
|
2014-04-16 08:42:46 +08:00
|
|
|
BlockInfo[MBB.getNumber()].Size = Size;
|
2014-03-29 18:18:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// getInstrOffset - Return the current offset of the specified machine
|
|
|
|
/// instruction from the start of the function. This offset changes as stuff is
|
|
|
|
/// moved around inside the function.
|
2016-08-02 16:06:17 +08:00
|
|
|
unsigned AArch64BranchRelaxation::getInstrOffset(const MachineInstr &MI) const {
|
|
|
|
const MachineBasicBlock *MBB = MI.getParent();
|
2014-03-29 18:18:08 +08:00
|
|
|
|
|
|
|
// The offset is composed of two things: the sum of the sizes of all MBB's
|
|
|
|
// before this instruction's block, and the offset from the start of the block
|
|
|
|
// it is in.
|
|
|
|
unsigned Offset = BlockInfo[MBB->getNumber()].Offset;
|
|
|
|
|
|
|
|
// Sum instructions before MI in MBB.
|
2016-08-02 16:06:17 +08:00
|
|
|
for (MachineBasicBlock::const_iterator I = MBB->begin(); &*I != &MI; ++I) {
|
2014-03-29 18:18:08 +08:00
|
|
|
assert(I != MBB->end() && "Didn't find MI in its own basic block?");
|
2016-07-29 00:32:22 +08:00
|
|
|
Offset += TII->getInstSizeInBytes(*I);
|
2014-03-29 18:18:08 +08:00
|
|
|
}
|
2016-08-02 15:20:09 +08:00
|
|
|
|
2014-03-29 18:18:08 +08:00
|
|
|
return Offset;
|
|
|
|
}
|
|
|
|
|
2014-05-24 20:50:23 +08:00
|
|
|
void AArch64BranchRelaxation::adjustBlockOffsets(MachineBasicBlock &Start) {
|
2014-04-16 08:42:46 +08:00
|
|
|
unsigned PrevNum = Start.getNumber();
|
|
|
|
for (auto &MBB : make_range(MachineFunction::iterator(Start), MF->end())) {
|
|
|
|
unsigned Num = MBB.getNumber();
|
2014-03-29 18:18:08 +08:00
|
|
|
if (!Num) // block zero is never changed from offset zero.
|
|
|
|
continue;
|
|
|
|
// Get the offset and known bits at the end of the layout predecessor.
|
|
|
|
// Include the alignment of the current block.
|
2014-04-16 08:42:46 +08:00
|
|
|
unsigned LogAlign = MBB.getAlignment();
|
2014-03-29 18:18:08 +08:00
|
|
|
BlockInfo[Num].Offset = BlockInfo[PrevNum].postOffset(LogAlign);
|
|
|
|
PrevNum = Num;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Split the basic block containing MI into two blocks, which are joined by
|
|
|
|
/// an unconditional branch. Update data structures and renumber blocks to
|
|
|
|
/// account for this change and returns the newly created block.
|
|
|
|
/// NOTE: Successor list of the original BB is out of date after this function,
|
|
|
|
/// and must be updated by the caller! Other transforms follow using this
|
|
|
|
/// utility function, so no point updating now rather than waiting.
|
|
|
|
MachineBasicBlock *
|
2016-08-02 15:20:09 +08:00
|
|
|
AArch64BranchRelaxation::splitBlockBeforeInstr(MachineInstr &MI) {
|
|
|
|
MachineBasicBlock *OrigBB = MI.getParent();
|
2014-03-29 18:18:08 +08:00
|
|
|
|
|
|
|
// Create a new MBB for the code after the OrigBB.
|
|
|
|
MachineBasicBlock *NewBB =
|
|
|
|
MF->CreateMachineBasicBlock(OrigBB->getBasicBlock());
|
2015-10-14 04:02:15 +08:00
|
|
|
MF->insert(++OrigBB->getIterator(), NewBB);
|
2014-03-29 18:18:08 +08:00
|
|
|
|
|
|
|
// Splice the instructions starting with MI over to NewBB.
|
2016-08-02 15:20:09 +08:00
|
|
|
NewBB->splice(NewBB->end(), OrigBB, MI.getIterator(), OrigBB->end());
|
2014-03-29 18:18:08 +08:00
|
|
|
|
|
|
|
// Add an unconditional branch from OrigBB to NewBB.
|
|
|
|
// Note the new unconditional branch is not being recorded.
|
|
|
|
// There doesn't seem to be meaningful DebugInfo available; this doesn't
|
|
|
|
// correspond to anything in the source.
|
2016-08-02 16:30:06 +08:00
|
|
|
insertUnconditionalBranch(*OrigBB, *NewBB, DebugLoc());
|
2014-03-29 18:18:08 +08:00
|
|
|
|
|
|
|
// Insert an entry into BlockInfo to align it properly with the block numbers.
|
|
|
|
BlockInfo.insert(BlockInfo.begin() + NewBB->getNumber(), BasicBlockInfo());
|
|
|
|
|
|
|
|
// Figure out how large the OrigBB is. As the first half of the original
|
|
|
|
// block, it cannot contain a tablejump. The size includes
|
|
|
|
// the new jump we added. (It should be possible to do this without
|
|
|
|
// recounting everything, but it's very confusing, and this is rarely
|
|
|
|
// executed.)
|
2014-04-16 08:42:46 +08:00
|
|
|
computeBlockSize(*OrigBB);
|
2014-03-29 18:18:08 +08:00
|
|
|
|
|
|
|
// Figure out how large the NewMBB is. As the second half of the original
|
|
|
|
// block, it may contain a tablejump.
|
2014-04-16 08:42:46 +08:00
|
|
|
computeBlockSize(*NewBB);
|
2014-03-29 18:18:08 +08:00
|
|
|
|
|
|
|
// All BBOffsets following these blocks must be modified.
|
2014-04-16 08:42:46 +08:00
|
|
|
adjustBlockOffsets(*OrigBB);
|
2014-03-29 18:18:08 +08:00
|
|
|
|
|
|
|
++NumSplit;
|
|
|
|
|
|
|
|
return NewBB;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// isBlockInRange - Returns true if the distance between specific MI and
|
|
|
|
/// specific BB can fit in MI's displacement field.
|
2016-08-02 16:06:17 +08:00
|
|
|
bool AArch64BranchRelaxation::isBlockInRange(
|
|
|
|
const MachineInstr &MI, const MachineBasicBlock &DestBB) const {
|
2014-03-29 18:18:08 +08:00
|
|
|
unsigned BrOffset = getInstrOffset(MI);
|
2016-08-02 15:20:09 +08:00
|
|
|
unsigned DestOffset = BlockInfo[DestBB.getNumber()].Offset;
|
2014-03-29 18:18:08 +08:00
|
|
|
|
2016-08-02 16:06:17 +08:00
|
|
|
if (TII->isBranchInRange(MI.getOpcode(), BrOffset, DestOffset))
|
|
|
|
return true;
|
2014-03-29 18:18:08 +08:00
|
|
|
|
2016-08-02 16:06:17 +08:00
|
|
|
DEBUG(
|
|
|
|
dbgs() << "Out of range branch to destination BB#" << DestBB.getNumber()
|
|
|
|
<< " from BB#" << MI.getParent()->getNumber()
|
|
|
|
<< " to " << DestOffset
|
|
|
|
<< " offset " << static_cast<int>(DestOffset - BrOffset)
|
|
|
|
<< '\t' << MI
|
|
|
|
);
|
2014-03-29 18:18:08 +08:00
|
|
|
|
2016-08-02 16:06:17 +08:00
|
|
|
return false;
|
2014-03-29 18:18:08 +08:00
|
|
|
}
|
|
|
|
|
2016-08-02 16:06:17 +08:00
|
|
|
static MachineBasicBlock *getDestBlock(const MachineInstr &MI) {
|
2016-08-02 15:20:09 +08:00
|
|
|
switch (MI.getOpcode()) {
|
2014-03-29 18:18:08 +08:00
|
|
|
default:
|
2014-06-18 13:05:13 +08:00
|
|
|
llvm_unreachable("unexpected opcode!");
|
2016-08-02 16:06:17 +08:00
|
|
|
case AArch64::B:
|
|
|
|
return MI.getOperand(0).getMBB();
|
2014-05-24 20:50:23 +08:00
|
|
|
case AArch64::TBZW:
|
|
|
|
case AArch64::TBNZW:
|
|
|
|
case AArch64::TBZX:
|
|
|
|
case AArch64::TBNZX:
|
2016-08-02 15:20:09 +08:00
|
|
|
return MI.getOperand(2).getMBB();
|
2014-05-24 20:50:23 +08:00
|
|
|
case AArch64::CBZW:
|
|
|
|
case AArch64::CBNZW:
|
|
|
|
case AArch64::CBZX:
|
|
|
|
case AArch64::CBNZX:
|
|
|
|
case AArch64::Bcc:
|
2016-08-02 15:20:09 +08:00
|
|
|
return MI.getOperand(1).getMBB();
|
2014-03-29 18:18:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned getOppositeConditionOpcode(unsigned Opc) {
|
|
|
|
switch (Opc) {
|
|
|
|
default:
|
2014-06-18 13:05:13 +08:00
|
|
|
llvm_unreachable("unexpected opcode!");
|
2014-05-24 20:50:23 +08:00
|
|
|
case AArch64::TBNZW: return AArch64::TBZW;
|
|
|
|
case AArch64::TBNZX: return AArch64::TBZX;
|
|
|
|
case AArch64::TBZW: return AArch64::TBNZW;
|
|
|
|
case AArch64::TBZX: return AArch64::TBNZX;
|
|
|
|
case AArch64::CBNZW: return AArch64::CBZW;
|
|
|
|
case AArch64::CBNZX: return AArch64::CBZX;
|
|
|
|
case AArch64::CBZW: return AArch64::CBNZW;
|
|
|
|
case AArch64::CBZX: return AArch64::CBNZX;
|
|
|
|
case AArch64::Bcc: return AArch64::Bcc; // Condition is an operand for Bcc.
|
2014-03-29 18:18:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-02 15:20:09 +08:00
|
|
|
static inline void invertBccCondition(MachineInstr &MI) {
|
|
|
|
assert(MI.getOpcode() == AArch64::Bcc && "Unexpected opcode!");
|
2016-08-02 16:06:17 +08:00
|
|
|
MachineOperand &CCOp = MI.getOperand(0);
|
|
|
|
|
|
|
|
AArch64CC::CondCode CC = static_cast<AArch64CC::CondCode>(CCOp.getImm());
|
|
|
|
CCOp.setImm(AArch64CC::getInvertedCondCode(CC));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Insert a conditional branch at the end of \p MBB to \p NewDestBB, using the
|
|
|
|
/// inverse condition of branch \p OldBr.
|
|
|
|
/// \returns The number of bytes added to the block.
|
|
|
|
unsigned AArch64BranchRelaxation::insertInvertedConditionalBranch(
|
2016-08-02 16:30:06 +08:00
|
|
|
MachineBasicBlock &SrcMBB,
|
|
|
|
MachineBasicBlock::iterator InsPt,
|
|
|
|
const DebugLoc &DL,
|
2016-08-02 16:06:17 +08:00
|
|
|
const MachineInstr &OldBr,
|
|
|
|
MachineBasicBlock &NewDestBB) const {
|
|
|
|
unsigned OppositeCondOpc = getOppositeConditionOpcode(OldBr.getOpcode());
|
|
|
|
|
|
|
|
MachineInstrBuilder MIB =
|
2016-08-02 16:30:06 +08:00
|
|
|
BuildMI(SrcMBB, InsPt, DL, TII->get(OppositeCondOpc))
|
2016-08-02 16:06:17 +08:00
|
|
|
.addOperand(OldBr.getOperand(0));
|
|
|
|
|
|
|
|
unsigned Opc = OldBr.getOpcode();
|
|
|
|
|
|
|
|
if (Opc == AArch64::TBZW || Opc == AArch64::TBNZW ||
|
|
|
|
Opc == AArch64::TBZX || Opc == AArch64::TBNZX)
|
|
|
|
MIB.addOperand(OldBr.getOperand(1));
|
|
|
|
|
|
|
|
if (OldBr.getOpcode() == AArch64::Bcc)
|
|
|
|
invertBccCondition(*MIB);
|
|
|
|
|
|
|
|
MIB.addMBB(&NewDestBB);
|
|
|
|
|
|
|
|
return TII->getInstSizeInBytes(*MIB);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Insert an unconditional branch at the end of \p MBB to \p DestBB.
|
|
|
|
/// \returns the number of bytes emitted.
|
|
|
|
unsigned AArch64BranchRelaxation::insertUnconditionalBranch(
|
|
|
|
MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock &DestBB,
|
|
|
|
const DebugLoc &DL) const {
|
|
|
|
MachineInstr *MI = BuildMI(&MBB, DL, TII->get(AArch64::B))
|
|
|
|
.addMBB(&DestBB);
|
|
|
|
|
|
|
|
return TII->getInstSizeInBytes(*MI);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void changeBranchDestBlock(MachineInstr &MI,
|
|
|
|
MachineBasicBlock &NewDestBB) {
|
|
|
|
unsigned OpNum = 0;
|
|
|
|
unsigned Opc = MI.getOpcode();
|
|
|
|
|
|
|
|
if (Opc != AArch64::B) {
|
|
|
|
OpNum = (Opc == AArch64::TBZW ||
|
|
|
|
Opc == AArch64::TBNZW ||
|
|
|
|
Opc == AArch64::TBZX ||
|
|
|
|
Opc == AArch64::TBNZX) ? 2 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
MI.getOperand(OpNum).setMBB(&NewDestBB);
|
2014-03-29 18:18:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// fixupConditionalBranch - Fix up a conditional branch whose destination is
|
|
|
|
/// too far away to fit in its displacement field. It is converted to an inverse
|
|
|
|
/// conditional branch + an unconditional branch to the destination.
|
2016-08-02 15:20:09 +08:00
|
|
|
bool AArch64BranchRelaxation::fixupConditionalBranch(MachineInstr &MI) {
|
2014-03-29 18:18:08 +08:00
|
|
|
MachineBasicBlock *DestBB = getDestBlock(MI);
|
|
|
|
|
|
|
|
// Add an unconditional branch to the destination and invert the branch
|
|
|
|
// condition to jump over it:
|
|
|
|
// tbz L1
|
|
|
|
// =>
|
|
|
|
// tbnz L2
|
|
|
|
// b L1
|
|
|
|
// L2:
|
|
|
|
|
|
|
|
// If the branch is at the end of its MBB and that has a fall-through block,
|
|
|
|
// direct the updated conditional branch to the fall-through block. Otherwise,
|
|
|
|
// split the MBB before the next instruction.
|
2016-08-02 15:20:09 +08:00
|
|
|
MachineBasicBlock *MBB = MI.getParent();
|
2014-03-29 18:18:08 +08:00
|
|
|
MachineInstr *BMI = &MBB->back();
|
2016-08-02 16:06:17 +08:00
|
|
|
bool NeedSplit = (BMI != &MI) || !hasFallthrough(*MBB);
|
2014-03-29 18:18:08 +08:00
|
|
|
|
2016-08-02 15:20:09 +08:00
|
|
|
if (BMI != &MI) {
|
2014-03-29 18:18:08 +08:00
|
|
|
if (std::next(MachineBasicBlock::iterator(MI)) ==
|
|
|
|
std::prev(MBB->getLastNonDebugInstr()) &&
|
2016-08-02 16:06:17 +08:00
|
|
|
BMI->isUnconditionalBranch()) {
|
|
|
|
// Last MI in the BB is an unconditional branch. We can simply invert the
|
2014-03-29 18:18:08 +08:00
|
|
|
// condition and swap destinations:
|
|
|
|
// beq L1
|
|
|
|
// b L2
|
|
|
|
// =>
|
|
|
|
// bne L2
|
|
|
|
// b L1
|
2016-08-02 16:06:17 +08:00
|
|
|
MachineBasicBlock *NewDest = getDestBlock(*BMI);
|
|
|
|
if (isBlockInRange(MI, *NewDest)) {
|
2014-03-29 18:18:08 +08:00
|
|
|
DEBUG(dbgs() << " Invert condition and swap its destination with "
|
|
|
|
<< *BMI);
|
2016-08-02 16:06:17 +08:00
|
|
|
changeBranchDestBlock(*BMI, *DestBB);
|
2016-08-02 16:30:06 +08:00
|
|
|
|
|
|
|
int NewSize =
|
|
|
|
insertInvertedConditionalBranch(*MBB, MI.getIterator(),
|
|
|
|
MI.getDebugLoc(), MI, *NewDest);
|
|
|
|
int OldSize = TII->getInstSizeInBytes(MI);
|
|
|
|
BlockInfo[MBB->getNumber()].Size += (NewSize - OldSize);
|
|
|
|
MI.eraseFromParent();
|
2014-03-29 18:18:08 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NeedSplit) {
|
|
|
|
// Analyze the branch so we know how to update the successor lists.
|
2016-08-12 01:22:59 +08:00
|
|
|
MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
|
2014-03-29 18:18:08 +08:00
|
|
|
SmallVector<MachineOperand, 2> Cond;
|
2016-08-12 01:22:59 +08:00
|
|
|
bool Fail = TII->analyzeBranch(*MBB, TBB, FBB, Cond, false);
|
|
|
|
assert(!Fail && "branches to relax should be analyzable");
|
|
|
|
(void)Fail;
|
2014-03-29 18:18:08 +08:00
|
|
|
|
|
|
|
MachineBasicBlock *NewBB = splitBlockBeforeInstr(MI);
|
|
|
|
// No need for the branch to the next block. We're adding an unconditional
|
|
|
|
// branch to the destination.
|
2016-07-29 00:32:22 +08:00
|
|
|
int delta = TII->getInstSizeInBytes(MBB->back());
|
2014-03-29 18:18:08 +08:00
|
|
|
BlockInfo[MBB->getNumber()].Size -= delta;
|
|
|
|
MBB->back().eraseFromParent();
|
|
|
|
// BlockInfo[SplitBB].Offset is wrong temporarily, fixed below
|
|
|
|
|
|
|
|
// Update the successor lists according to the transformation to follow.
|
|
|
|
// Do it here since if there's no split, no update is needed.
|
|
|
|
MBB->replaceSuccessor(FBB, NewBB);
|
|
|
|
NewBB->addSuccessor(FBB);
|
|
|
|
}
|
2016-08-02 16:06:17 +08:00
|
|
|
|
|
|
|
MachineBasicBlock &NextBB = *std::next(MachineFunction::iterator(MBB));
|
2014-03-29 18:18:08 +08:00
|
|
|
|
|
|
|
DEBUG(dbgs() << " Insert B to BB#" << DestBB->getNumber()
|
|
|
|
<< ", invert condition and change dest. to BB#"
|
2016-08-02 16:06:17 +08:00
|
|
|
<< NextBB.getNumber() << '\n');
|
2014-03-29 18:18:08 +08:00
|
|
|
|
2016-08-02 16:30:06 +08:00
|
|
|
unsigned &MBBSize = BlockInfo[MBB->getNumber()].Size;
|
|
|
|
|
2014-03-29 18:18:08 +08:00
|
|
|
// Insert a new conditional branch and a new unconditional branch.
|
2016-08-02 16:30:06 +08:00
|
|
|
MBBSize += insertInvertedConditionalBranch(*MBB, MBB->end(),
|
|
|
|
MI.getDebugLoc(), MI, NextBB);
|
2016-08-02 15:20:09 +08:00
|
|
|
|
2016-08-02 16:30:06 +08:00
|
|
|
MBBSize += insertUnconditionalBranch(*MBB, *DestBB, MI.getDebugLoc());
|
2014-03-29 18:18:08 +08:00
|
|
|
|
|
|
|
// Remove the old conditional branch. It may or may not still be in MBB.
|
2016-08-02 16:30:06 +08:00
|
|
|
MBBSize -= TII->getInstSizeInBytes(MI);
|
2016-08-02 15:20:09 +08:00
|
|
|
MI.eraseFromParent();
|
2014-03-29 18:18:08 +08:00
|
|
|
|
|
|
|
// Finally, keep the block offsets up to date.
|
2014-04-16 08:42:46 +08:00
|
|
|
adjustBlockOffsets(*MBB);
|
2014-03-29 18:18:08 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-24 20:50:23 +08:00
|
|
|
bool AArch64BranchRelaxation::relaxBranchInstructions() {
|
2014-03-29 18:18:08 +08:00
|
|
|
bool Changed = false;
|
|
|
|
// Relaxing branches involves creating new basic blocks, so re-eval
|
|
|
|
// end() for termination.
|
2016-06-17 05:21:49 +08:00
|
|
|
for (MachineFunction::iterator I = MF->begin(); I != MF->end(); ++I) {
|
|
|
|
MachineBasicBlock &MBB = *I;
|
2016-08-02 15:20:09 +08:00
|
|
|
MachineBasicBlock::iterator J = MBB.getFirstTerminator();
|
|
|
|
if (J == MBB.end())
|
|
|
|
continue;
|
|
|
|
|
2016-08-23 09:30:30 +08:00
|
|
|
MachineBasicBlock::iterator Next;
|
|
|
|
for (MachineBasicBlock::iterator J = MBB.getFirstTerminator();
|
|
|
|
J != MBB.end(); J = Next) {
|
|
|
|
Next = std::next(J);
|
|
|
|
MachineInstr &MI = *J;
|
|
|
|
|
|
|
|
if (MI.isConditionalBranch()) {
|
|
|
|
MachineBasicBlock *DestBB = getDestBlock(MI);
|
|
|
|
if (!isBlockInRange(MI, *DestBB)) {
|
|
|
|
if (Next != MBB.end() && Next->isConditionalBranch()) {
|
|
|
|
// If there are multiple conditional branches, this isn't an
|
|
|
|
// analyzable block. Split later terminators into a new block so
|
|
|
|
// each one will be analyzable.
|
|
|
|
|
|
|
|
MachineBasicBlock *NewBB = splitBlockBeforeInstr(*Next);
|
|
|
|
NewBB->transferSuccessors(&MBB);
|
|
|
|
MBB.addSuccessor(NewBB);
|
|
|
|
MBB.addSuccessor(DestBB);
|
|
|
|
|
|
|
|
// Cleanup potential unconditional branch to successor block.
|
|
|
|
NewBB->updateTerminator();
|
|
|
|
MBB.updateTerminator();
|
|
|
|
} else {
|
|
|
|
fixupConditionalBranch(MI);
|
|
|
|
++NumConditionalRelaxed;
|
|
|
|
}
|
|
|
|
|
|
|
|
Changed = true;
|
|
|
|
|
|
|
|
// This may have modified all of the terminators, so start over.
|
|
|
|
Next = MBB.getFirstTerminator();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2014-03-29 18:18:08 +08:00
|
|
|
}
|
|
|
|
}
|
2016-08-23 09:30:30 +08:00
|
|
|
|
2014-03-29 18:18:08 +08:00
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2014-05-24 20:50:23 +08:00
|
|
|
bool AArch64BranchRelaxation::runOnMachineFunction(MachineFunction &mf) {
|
2014-03-29 18:18:08 +08:00
|
|
|
MF = &mf;
|
|
|
|
|
2014-05-24 20:50:23 +08:00
|
|
|
DEBUG(dbgs() << "***** AArch64BranchRelaxation *****\n");
|
2014-03-29 18:18:08 +08:00
|
|
|
|
2016-08-02 16:06:17 +08:00
|
|
|
TII = MF->getSubtarget<AArch64Subtarget>().getInstrInfo();
|
2014-03-29 18:18:08 +08:00
|
|
|
|
|
|
|
// Renumber all of the machine basic blocks in the function, guaranteeing that
|
|
|
|
// the numbers agree with the position of the block in the function.
|
|
|
|
MF->RenumberBlocks();
|
|
|
|
|
|
|
|
// Do the initial scan of the function, building up information about the
|
|
|
|
// sizes of each block.
|
|
|
|
scanFunction();
|
|
|
|
|
2016-08-02 16:06:17 +08:00
|
|
|
DEBUG(dbgs() << " Basic blocks before relaxation\n"; dumpBBs(););
|
2014-03-29 18:18:08 +08:00
|
|
|
|
|
|
|
bool MadeChange = false;
|
|
|
|
while (relaxBranchInstructions())
|
|
|
|
MadeChange = true;
|
|
|
|
|
|
|
|
// After a while, this might be made debug-only, but it is not expensive.
|
|
|
|
verify();
|
|
|
|
|
2016-08-02 16:06:17 +08:00
|
|
|
DEBUG(dbgs() << " Basic blocks after relaxation\n\n"; dumpBBs());
|
2014-03-29 18:18:08 +08:00
|
|
|
|
|
|
|
BlockInfo.clear();
|
|
|
|
|
|
|
|
return MadeChange;
|
|
|
|
}
|
|
|
|
|
2016-03-25 22:37:43 +08:00
|
|
|
/// Returns an instance of the AArch64 Branch Relaxation pass.
|
2014-05-24 20:50:23 +08:00
|
|
|
FunctionPass *llvm::createAArch64BranchRelaxation() {
|
|
|
|
return new AArch64BranchRelaxation();
|
2014-03-29 18:18:08 +08:00
|
|
|
}
|