#include "llvm/InitializePasses.h"
#include "Hexagon.h"
#include "HexagonInstrInfo.h"
#include "HexagonRegisterInfo.h"
#include "HexagonSubtarget.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/CodeGen/MachineBasicBlock.h"
#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachineInstr.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/CodeGen/MachineOperand.h"
#include "llvm/CodeGen/MachineRegisterInfo.h"
#include "llvm/CodeGen/TargetOpcodes.h"
#include "llvm/CodeGen/TargetRegisterInfo.h"
#include "llvm/CodeGen/TargetSubtargetInfo.h"
#include "llvm/IR/DebugLoc.h"
#include "llvm/MC/MCInstrDesc.h"
#include "llvm/Pass.h"
#include "llvm/Support/BranchProbability.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/raw_ostream.h"
#include <cassert>
#include <cstdint>
#include <iterator>
using namespace llvm;
#define DEBUG_TYPE "hexagon-nvj"
STATISTIC(NumNVJGenerated, "Number of New Value Jump Instructions created");
static cl::opt<int> DbgNVJCount("nvj-count", cl::init(-1), cl::Hidden,
cl::desc("Maximum number of predicated jumps to be converted to "
"New Value Jump"));
static cl::opt<bool> DisableNewValueJumps("disable-nvjump", cl::Hidden,
cl::desc("Disable New Value Jumps"));
namespace llvm {
FunctionPass *createHexagonNewValueJump();
void initializeHexagonNewValueJumpPass(PassRegistry&);
}
namespace {
struct HexagonNewValueJump : public MachineFunctionPass {
static char ID;
HexagonNewValueJump() : MachineFunctionPass(ID) {}
void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.addRequired<MachineBranchProbabilityInfo>();
MachineFunctionPass::getAnalysisUsage(AU);
}
StringRef getPassName() const override { return "Hexagon NewValueJump"; }
bool runOnMachineFunction(MachineFunction &Fn) override;
MachineFunctionProperties getRequiredProperties() const override {
return MachineFunctionProperties().set(
MachineFunctionProperties::Property::NoVRegs);
}
private:
const HexagonInstrInfo *QII;
const HexagonRegisterInfo *QRI;
const MachineBranchProbabilityInfo *MBPI;
bool isNewValueJumpCandidate(const MachineInstr &MI) const;
};
}
char HexagonNewValueJump::ID = 0;
INITIALIZE_PASS_BEGIN(HexagonNewValueJump, "hexagon-nvj",
"Hexagon NewValueJump", false, false)
INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
INITIALIZE_PASS_END(HexagonNewValueJump, "hexagon-nvj",
"Hexagon NewValueJump", false, false)
static bool canBeFeederToNewValueJump(const HexagonInstrInfo *QII,
const TargetRegisterInfo *TRI,
MachineBasicBlock::iterator II,
MachineBasicBlock::iterator end,
MachineBasicBlock::iterator skip,
MachineFunction &MF) {
if (QII->isPredicated(*II))
return false;
if (II->getOpcode() == TargetOpcode::KILL)
return false;
if (II->isImplicitDef())
return false;
if (QII->isSolo(*II))
return false;
if (QII->isFloat(*II))
return false;
bool HadDef = false;
for (const MachineOperand &Op : II->operands()) {
if (!Op.isReg() || !Op.isDef())
continue;
if (HadDef)
return false;
HadDef = true;
if (!Hexagon::IntRegsRegClass.contains(Op.getReg()))
return false;
}
assert(HadDef);
for (unsigned i = 0; i < II->getNumOperands(); ++i) {
if (II->getOperand(i).isReg() &&
(II->getOperand(i).isUse() || II->getOperand(i).isDef())) {
MachineBasicBlock::iterator localII = II;
++localII;
Register Reg = II->getOperand(i).getReg();
for (MachineBasicBlock::iterator localBegin = localII; localBegin != end;
++localBegin) {
if (localBegin == skip)
continue;
if (localBegin->modifiesRegister(Reg, TRI) ||
localBegin->readsRegister(Reg, TRI))
return false;
}
}
}
return true;
}
static bool commonChecksToProhibitNewValueJump(bool afterRA,
MachineBasicBlock::iterator MII) {
if (MII->mayStore())
return false;
if (MII->isCall())
return false;
if (!afterRA) {
if (MII->getOpcode() == TargetOpcode::KILL ||
MII->getOpcode() == TargetOpcode::PHI ||
MII->getOpcode() == TargetOpcode::COPY)
return false;
if (MII->getOpcode() == Hexagon::LDriw_pred ||
MII->getOpcode() == Hexagon::STriw_pred)
return false;
}
return true;
}
static bool canCompareBeNewValueJump(const HexagonInstrInfo *QII,
const TargetRegisterInfo *TRI,
MachineBasicBlock::iterator II,
unsigned pReg,
bool secondReg,
bool optLocation,
MachineBasicBlock::iterator end,
MachineFunction &MF) {
MachineInstr &MI = *II;
if (!secondReg) {
const MachineOperand &Op2 = MI.getOperand(2);
if (!Op2.isImm())
return false;
int64_t v = Op2.getImm();
bool Valid = false;
switch (MI.getOpcode()) {
case Hexagon::C2_cmpeqi:
case Hexagon::C4_cmpneqi:
case Hexagon::C2_cmpgti:
case Hexagon::C4_cmpltei:
Valid = (isUInt<5>(v) || v == -1);
break;
case Hexagon::C2_cmpgtui:
case Hexagon::C4_cmplteui:
Valid = isUInt<5>(v);
break;
case Hexagon::S2_tstbit_i:
case Hexagon::S4_ntstbit_i:
Valid = (v == 0);
break;
}
if (!Valid)
return false;
}
unsigned cmpReg1, cmpOp2 = 0; cmpReg1 = MI.getOperand(1).getReg();
if (secondReg) {
cmpOp2 = MI.getOperand(2).getReg();
if (cmpReg1 == cmpOp2)
return false;
MachineRegisterInfo &MRI = MF.getRegInfo();
if (!Register::isPhysicalRegister(cmpOp2)) {
MachineInstr *def = MRI.getVRegDef(cmpOp2);
if (def->getOpcode() == TargetOpcode::COPY)
return false;
}
}
++II;
for (MachineBasicBlock::iterator localII = II; localII != end; ++localII) {
if (localII->isDebugInstr())
continue;
if (!commonChecksToProhibitNewValueJump(optLocation, localII))
return false;
if (localII->modifiesRegister(pReg, TRI) ||
localII->readsRegister(pReg, TRI))
return false;
if (localII->modifiesRegister(cmpReg1, TRI) ||
(secondReg && localII->modifiesRegister(cmpOp2, TRI)))
return false;
}
return true;
}
static unsigned getNewValueJumpOpcode(MachineInstr *MI, int reg,
bool secondRegNewified,
MachineBasicBlock *jmpTarget,
const MachineBranchProbabilityInfo
*MBPI) {
bool taken = false;
MachineBasicBlock *Src = MI->getParent();
const BranchProbability Prediction =
MBPI->getEdgeProbability(Src, jmpTarget);
if (Prediction >= BranchProbability(1,2))
taken = true;
switch (MI->getOpcode()) {
case Hexagon::C2_cmpeq:
return taken ? Hexagon::J4_cmpeq_t_jumpnv_t
: Hexagon::J4_cmpeq_t_jumpnv_nt;
case Hexagon::C2_cmpeqi:
if (reg >= 0)
return taken ? Hexagon::J4_cmpeqi_t_jumpnv_t
: Hexagon::J4_cmpeqi_t_jumpnv_nt;
return taken ? Hexagon::J4_cmpeqn1_t_jumpnv_t
: Hexagon::J4_cmpeqn1_t_jumpnv_nt;
case Hexagon::C4_cmpneqi:
if (reg >= 0)
return taken ? Hexagon::J4_cmpeqi_f_jumpnv_t
: Hexagon::J4_cmpeqi_f_jumpnv_nt;
return taken ? Hexagon::J4_cmpeqn1_f_jumpnv_t :
Hexagon::J4_cmpeqn1_f_jumpnv_nt;
case Hexagon::C2_cmpgt:
if (secondRegNewified)
return taken ? Hexagon::J4_cmplt_t_jumpnv_t
: Hexagon::J4_cmplt_t_jumpnv_nt;
return taken ? Hexagon::J4_cmpgt_t_jumpnv_t
: Hexagon::J4_cmpgt_t_jumpnv_nt;
case Hexagon::C2_cmpgti:
if (reg >= 0)
return taken ? Hexagon::J4_cmpgti_t_jumpnv_t
: Hexagon::J4_cmpgti_t_jumpnv_nt;
return taken ? Hexagon::J4_cmpgtn1_t_jumpnv_t
: Hexagon::J4_cmpgtn1_t_jumpnv_nt;
case Hexagon::C2_cmpgtu:
if (secondRegNewified)
return taken ? Hexagon::J4_cmpltu_t_jumpnv_t
: Hexagon::J4_cmpltu_t_jumpnv_nt;
return taken ? Hexagon::J4_cmpgtu_t_jumpnv_t
: Hexagon::J4_cmpgtu_t_jumpnv_nt;
case Hexagon::C2_cmpgtui:
return taken ? Hexagon::J4_cmpgtui_t_jumpnv_t
: Hexagon::J4_cmpgtui_t_jumpnv_nt;
case Hexagon::C4_cmpneq:
return taken ? Hexagon::J4_cmpeq_f_jumpnv_t
: Hexagon::J4_cmpeq_f_jumpnv_nt;
case Hexagon::C4_cmplte:
if (secondRegNewified)
return taken ? Hexagon::J4_cmplt_f_jumpnv_t
: Hexagon::J4_cmplt_f_jumpnv_nt;
return taken ? Hexagon::J4_cmpgt_f_jumpnv_t
: Hexagon::J4_cmpgt_f_jumpnv_nt;
case Hexagon::C4_cmplteu:
if (secondRegNewified)
return taken ? Hexagon::J4_cmpltu_f_jumpnv_t
: Hexagon::J4_cmpltu_f_jumpnv_nt;
return taken ? Hexagon::J4_cmpgtu_f_jumpnv_t
: Hexagon::J4_cmpgtu_f_jumpnv_nt;
case Hexagon::C4_cmpltei:
if (reg >= 0)
return taken ? Hexagon::J4_cmpgti_f_jumpnv_t
: Hexagon::J4_cmpgti_f_jumpnv_nt;
return taken ? Hexagon::J4_cmpgtn1_f_jumpnv_t
: Hexagon::J4_cmpgtn1_f_jumpnv_nt;
case Hexagon::C4_cmplteui:
return taken ? Hexagon::J4_cmpgtui_f_jumpnv_t
: Hexagon::J4_cmpgtui_f_jumpnv_nt;
default:
llvm_unreachable("Could not find matching New Value Jump instruction.");
}
return 0;
}
bool HexagonNewValueJump::isNewValueJumpCandidate(
const MachineInstr &MI) const {
switch (MI.getOpcode()) {
case Hexagon::C2_cmpeq:
case Hexagon::C2_cmpeqi:
case Hexagon::C2_cmpgt:
case Hexagon::C2_cmpgti:
case Hexagon::C2_cmpgtu:
case Hexagon::C2_cmpgtui:
case Hexagon::C4_cmpneq:
case Hexagon::C4_cmpneqi:
case Hexagon::C4_cmplte:
case Hexagon::C4_cmplteu:
case Hexagon::C4_cmpltei:
case Hexagon::C4_cmplteui:
return true;
default:
return false;
}
}
bool HexagonNewValueJump::runOnMachineFunction(MachineFunction &MF) {
LLVM_DEBUG(dbgs() << "********** Hexagon New Value Jump **********\n"
<< "********** Function: " << MF.getName() << "\n");
if (skipFunction(MF.getFunction()))
return false;
QII = static_cast<const HexagonInstrInfo *>(MF.getSubtarget().getInstrInfo());
QRI = static_cast<const HexagonRegisterInfo *>(
MF.getSubtarget().getRegisterInfo());
MBPI = &getAnalysis<MachineBranchProbabilityInfo>();
if (DisableNewValueJumps ||
!MF.getSubtarget<HexagonSubtarget>().useNewValueJumps())
return false;
int nvjCount = DbgNVJCount;
int nvjGenerated = 0;
for (MachineFunction::iterator MBBb = MF.begin(), MBBe = MF.end();
MBBb != MBBe; ++MBBb) {
MachineBasicBlock *MBB = &*MBBb;
LLVM_DEBUG(dbgs() << "** dumping bb ** " << MBB->getNumber() << "\n");
LLVM_DEBUG(MBB->dump());
LLVM_DEBUG(dbgs() << "\n"
<< "********** dumping instr bottom up **********\n");
bool foundJump = false;
bool foundCompare = false;
bool invertPredicate = false;
unsigned predReg = 0; unsigned cmpReg1 = 0;
int cmpOp2 = 0;
MachineBasicBlock::iterator jmpPos;
MachineBasicBlock::iterator cmpPos;
MachineInstr *cmpInstr = nullptr, *jmpInstr = nullptr;
MachineBasicBlock *jmpTarget = nullptr;
bool afterRA = false;
bool isSecondOpReg = false;
bool isSecondOpNewified = false;
for (MachineBasicBlock::iterator MII = MBB->end(), E = MBB->begin();
MII != E;) {
MachineInstr &MI = *--MII;
if (MI.isDebugInstr()) {
continue;
}
if ((nvjCount == 0) || (nvjCount > -1 && nvjCount <= nvjGenerated))
break;
LLVM_DEBUG(dbgs() << "Instr: "; MI.dump(); dbgs() << "\n");
if (!foundJump && (MI.getOpcode() == Hexagon::J2_jumpt ||
MI.getOpcode() == Hexagon::J2_jumptpt ||
MI.getOpcode() == Hexagon::J2_jumpf ||
MI.getOpcode() == Hexagon::J2_jumpfpt ||
MI.getOpcode() == Hexagon::J2_jumptnewpt ||
MI.getOpcode() == Hexagon::J2_jumptnew ||
MI.getOpcode() == Hexagon::J2_jumpfnewpt ||
MI.getOpcode() == Hexagon::J2_jumpfnew)) {
jmpPos = MII;
jmpInstr = &MI;
predReg = MI.getOperand(0).getReg();
afterRA = Register::isPhysicalRegister(predReg);
bool predLive = false;
for (const MachineBasicBlock *SuccMBB : MBB->successors())
if (SuccMBB->isLiveIn(predReg))
predLive = true;
if (predLive)
break;
if (!MI.getOperand(1).isMBB())
continue;
jmpTarget = MI.getOperand(1).getMBB();
foundJump = true;
if (MI.getOpcode() == Hexagon::J2_jumpf ||
MI.getOpcode() == Hexagon::J2_jumpfnewpt ||
MI.getOpcode() == Hexagon::J2_jumpfnew) {
invertPredicate = true;
}
continue;
}
if (foundJump && MI.getNumOperands() == 0)
break;
if (foundJump && !foundCompare && MI.getOperand(0).isReg() &&
MI.getOperand(0).getReg() == predReg) {
if (isNewValueJumpCandidate(MI)) {
assert(
(MI.getDesc().isCompare()) &&
"Only compare instruction can be collapsed into New Value Jump");
isSecondOpReg = MI.getOperand(2).isReg();
if (!canCompareBeNewValueJump(QII, QRI, MII, predReg, isSecondOpReg,
afterRA, jmpPos, MF))
break;
cmpInstr = &MI;
cmpPos = MII;
foundCompare = true;
cmpReg1 = MI.getOperand(1).getReg();
if (isSecondOpReg)
cmpOp2 = MI.getOperand(2).getReg();
else
cmpOp2 = MI.getOperand(2).getImm();
continue;
}
}
if (foundCompare && foundJump) {
if (!commonChecksToProhibitNewValueJump(afterRA, MII))
break;
bool foundFeeder = false;
MachineBasicBlock::iterator feederPos = MII;
if (MI.getOperand(0).isReg() && MI.getOperand(0).isDef() &&
(MI.getOperand(0).getReg() == cmpReg1 ||
(isSecondOpReg &&
MI.getOperand(0).getReg() == (unsigned)cmpOp2))) {
Register feederReg = MI.getOperand(0).getReg();
if (feederReg == cmpReg1) {
if (!canBeFeederToNewValueJump(QII, QRI, MII, jmpPos, cmpPos, MF)) {
if (!isSecondOpReg)
break;
else
continue;
} else
foundFeeder = true;
}
if (!foundFeeder && isSecondOpReg && feederReg == (unsigned)cmpOp2)
if (!canBeFeederToNewValueJump(QII, QRI, MII, jmpPos, cmpPos, MF))
break;
if (isSecondOpReg) {
unsigned COp = cmpInstr->getOpcode();
if ((COp == Hexagon::C2_cmpeq || COp == Hexagon::C4_cmpneq) &&
(feederReg == (unsigned)cmpOp2)) {
unsigned tmp = cmpReg1;
cmpReg1 = cmpOp2;
cmpOp2 = tmp;
}
if (feederReg == (unsigned)cmpOp2)
isSecondOpNewified = true;
}
auto TransferKills = [jmpPos,cmpPos] (MachineInstr &MI) {
for (MachineOperand &MO : MI.operands()) {
if (!MO.isReg() || !MO.isUse())
continue;
Register UseR = MO.getReg();
for (auto I = std::next(MI.getIterator()); I != jmpPos; ++I) {
if (I == cmpPos)
continue;
for (MachineOperand &Op : I->operands()) {
if (!Op.isReg() || !Op.isUse() || !Op.isKill())
continue;
if (Op.getReg() != UseR)
continue;
Op.setIsKill(false);
MO.setIsKill(true);
return;
}
}
}
};
TransferKills(*feederPos);
TransferKills(*cmpPos);
bool MO1IsKill = cmpPos->killsRegister(cmpReg1, QRI);
bool MO2IsKill = isSecondOpReg && cmpPos->killsRegister(cmpOp2, QRI);
MBB->splice(jmpPos, MI.getParent(), MI);
MBB->splice(jmpPos, MI.getParent(), cmpInstr);
DebugLoc dl = MI.getDebugLoc();
MachineInstr *NewMI;
assert((isNewValueJumpCandidate(*cmpInstr)) &&
"This compare is not a New Value Jump candidate.");
unsigned opc = getNewValueJumpOpcode(cmpInstr, cmpOp2,
isSecondOpNewified,
jmpTarget, MBPI);
if (invertPredicate)
opc = QII->getInvertedPredicatedOpcode(opc);
if (isSecondOpReg)
NewMI = BuildMI(*MBB, jmpPos, dl, QII->get(opc))
.addReg(cmpReg1, getKillRegState(MO1IsKill))
.addReg(cmpOp2, getKillRegState(MO2IsKill))
.addMBB(jmpTarget);
else
NewMI = BuildMI(*MBB, jmpPos, dl, QII->get(opc))
.addReg(cmpReg1, getKillRegState(MO1IsKill))
.addImm(cmpOp2)
.addMBB(jmpTarget);
assert(NewMI && "New Value Jump Instruction Not created!");
(void)NewMI;
if (cmpInstr->getOperand(0).isReg() &&
cmpInstr->getOperand(0).isKill())
cmpInstr->getOperand(0).setIsKill(false);
if (cmpInstr->getOperand(1).isReg() &&
cmpInstr->getOperand(1).isKill())
cmpInstr->getOperand(1).setIsKill(false);
cmpInstr->eraseFromParent();
jmpInstr->eraseFromParent();
++nvjGenerated;
++NumNVJGenerated;
break;
}
}
}
}
return true;
}
FunctionPass *llvm::createHexagonNewValueJump() {
return new HexagonNewValueJump();
}