30#define DEBUG_TYPE "hexagon-brelax"
50 StringRef getPassName()
const override {
51 return "Hexagon Branch Relaxation";
54 void getAnalysisUsage(AnalysisUsage &AU)
const override {
60 const HexagonInstrInfo *HII;
64 DenseMap<MachineBasicBlock*, unsigned> &BlockToInstOffset);
66 DenseMap<MachineBasicBlock*, unsigned> &BlockToInstOffset);
67 bool isJumpOutOfRange(MachineInstr &
MI,
68 DenseMap<MachineBasicBlock*, unsigned> &BlockToInstOffset);
71 char HexagonBranchRelaxation::ID = 0;
76 "Hexagon Branch Relaxation",
false,
false)
79 return new HexagonBranchRelaxation();
82bool HexagonBranchRelaxation::runOnMachineFunction(
MachineFunction &MF) {
86 HII = HST.getInstrInfo();
94 DenseMap<MachineBasicBlock*, unsigned> &OffsetMap) {
96 unsigned InstOffset = 0;
98 if (
B.getAlignment() !=
Align(1)) {
102 InstOffset =
alignTo(InstOffset,
B.getAlignment());
104 OffsetMap[&
B] = InstOffset;
105 for (
auto &
MI :
B.instrs()) {
106 InstOffset += HII->getSize(
MI);
108 if (
MI.isBranch() && HII->isExtendable(
MI))
123 DenseMap<MachineBasicBlock*, unsigned> BlockToInstOffset;
124 computeOffset(MF, BlockToInstOffset);
126 return reGenerateBranch(MF, BlockToInstOffset);
133bool HexagonBranchRelaxation::isJumpOutOfRange(MachineInstr &
MI,
134 DenseMap<MachineBasicBlock*, unsigned> &BlockToInstOffset) {
135 MachineBasicBlock &
B = *
MI.getParent();
136 auto FirstTerm =
B.getFirstInstrTerminator();
137 if (FirstTerm ==
B.instr_end())
140 if (HII->isExtended(
MI))
143 unsigned InstOffset = BlockToInstOffset[&
B];
144 unsigned Distance = 0;
151 MachineBasicBlock *
TBB =
nullptr, *FBB =
nullptr;
152 SmallVector<MachineOperand, 4>
Cond;
155 if (HII->analyzeBranch(
B,
TBB, FBB,
Cond,
false)) {
159 if (HII->isNewValueJump(*FirstTerm))
160 TBB = FirstTerm->getOperand(HII->getCExtOpNum(*FirstTerm)).getMBB();
162 if (
TBB && &
MI == &*FirstTerm) {
163 Distance = std::abs((
long long)InstOffset - BlockToInstOffset[
TBB])
165 return !HII->isJumpWithinBranchRange(*FirstTerm, Distance);
169 auto SecondTerm = std::next(FirstTerm);
170 assert(SecondTerm !=
B.instr_end() &&
171 (SecondTerm->isBranch() || SecondTerm->isCall()) &&
172 "Bad second terminator");
173 if (&
MI != &*SecondTerm)
176 Distance = std::abs((
long long)InstOffset - BlockToInstOffset[FBB])
178 return !HII->isJumpWithinBranchRange(*SecondTerm, Distance);
184 DenseMap<MachineBasicBlock*, unsigned> &BlockToInstOffset) {
189 if (!
MI.isBranch() || !isJumpOutOfRange(
MI, BlockToInstOffset))
192 << HII->isExtendable(
MI) <<
") isConstExtended("
193 << HII->isConstExtended(
MI) <<
") " <<
MI);
197 if (!HII->isExtendable(
MI) && !HII->isExtended(
MI)) {
198 LLVM_DEBUG(
dbgs() <<
"\tUnderimplemented relax branch instruction.\n");
201 int ExtOpNum = HII->getCExtOpNum(
MI);
202 MachineOperand &MO =
MI.getOperand(ExtOpNum);
206 assert(MO.
isMBB() &&
"Branch with unknown expandable field type");
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file defines the DenseMap class.
static cl::opt< uint32_t > BranchRelaxSafetyBuffer("branch-relax-safety-buffer", cl::init(200), cl::Hidden, cl::desc("safety buffer size"))
#define HEXAGON_INSTR_SIZE
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
SmallVector< MachineOperand, 4 > Cond
const SmallVectorImpl< MachineOperand > MachineBasicBlock * TBB
This file defines the SmallVector class.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
FunctionPass class - This class is used to implement most global optimizations.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
void addTargetFlag(unsigned F)
bool isMBB() const
isMBB - Tests if this is a MO_MachineBasicBlock operand.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
FunctionPass * createHexagonBranchRelaxation()
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.