Index: include/llvm/Target/TargetInstrInfo.h =================================================================== --- include/llvm/Target/TargetInstrInfo.h +++ include/llvm/Target/TargetInstrInfo.h @@ -480,6 +480,18 @@ return true; } + /// Return true if the offset is in range of the branch instruction and + /// false otherwise. + virtual bool isBranchOffsetInRange(MachineInstr &MI, int64_t Offset) const { + return true; + } + + /// Return the opcode if there exist a branch instruction with a smaller + /// encoding, or -1 otherwise. + virtual int optimizeShortBranch(MachineInstr &MI, int64_t Offset) const { + return -1; + } + /// Represents a predicate at the MachineFunction level. The control flow a /// MachineBranchPredicate represents is: /// Index: lib/Target/ARM/ARMBaseInstrInfo.h =================================================================== --- lib/Target/ARM/ARMBaseInstrInfo.h +++ lib/Target/ARM/ARMBaseInstrInfo.h @@ -120,6 +120,8 @@ const ScheduleDAG *DAG) const override; // Branch analysis. + bool isBranchOffsetInRange(MachineInstr &MI, int64_t Offset) const override; + int optimizeShortBranch(MachineInstr &MI, int64_t Offset) const override; bool analyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB, MachineBasicBlock *&FBB, SmallVectorImpl &Cond, Index: lib/Target/ARM/ARMBaseInstrInfo.cpp =================================================================== --- lib/Target/ARM/ARMBaseInstrInfo.cpp +++ lib/Target/ARM/ARMBaseInstrInfo.cpp @@ -290,6 +290,31 @@ } // Branch analysis. +bool ARMBaseInstrInfo::isBranchOffsetInRange(MachineInstr &MI, + int64_t Offset) const { + switch (MI.getOpcode()) { + case ARM::tBcc: + if (Offset < -256 || Offset > 254) + return false; + case ARM::tB: + if (Offset < -2048 || Offset > 2046) + return false; + } + return true; +} + +// Map branch instructions to Thumb1 equivalent branches. +int ARMBaseInstrInfo::optimizeShortBranch(MachineInstr &MI, + int64_t Offset) const { + switch (MI.getOpcode()) { + case ARM::t2B: + return ARM::tB; + case ARM::t2Bcc: + return ARM::tBcc; + } + return -1; +} + bool ARMBaseInstrInfo::analyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB, MachineBasicBlock *&FBB,