109467b48Spatrick //===- HexagonNewValueJump.cpp - Hexagon Backend New Value Jump -----------===//
209467b48Spatrick //
309467b48Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
409467b48Spatrick // See https://llvm.org/LICENSE.txt for license information.
509467b48Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
609467b48Spatrick //
709467b48Spatrick //===----------------------------------------------------------------------===//
809467b48Spatrick //
909467b48Spatrick // This implements NewValueJump pass in Hexagon.
1009467b48Spatrick // Ideally, we should merge this as a Peephole pass prior to register
1109467b48Spatrick // allocation, but because we have a spill in between the feeder and new value
1209467b48Spatrick // jump instructions, we are forced to write after register allocation.
1309467b48Spatrick // Having said that, we should re-attempt to pull this earlier at some point
1409467b48Spatrick // in future.
1509467b48Spatrick
1609467b48Spatrick // The basic approach looks for sequence of predicated jump, compare instruciton
1709467b48Spatrick // that genereates the predicate and, the feeder to the predicate. Once it finds
1809467b48Spatrick // all, it collapses compare and jump instruction into a new value jump
1909467b48Spatrick // intstructions.
2009467b48Spatrick //
2109467b48Spatrick //===----------------------------------------------------------------------===//
2209467b48Spatrick
2309467b48Spatrick #include "llvm/InitializePasses.h"
2409467b48Spatrick #include "Hexagon.h"
2509467b48Spatrick #include "HexagonInstrInfo.h"
2609467b48Spatrick #include "HexagonRegisterInfo.h"
2709467b48Spatrick #include "HexagonSubtarget.h"
2809467b48Spatrick #include "llvm/ADT/Statistic.h"
2909467b48Spatrick #include "llvm/CodeGen/MachineBasicBlock.h"
3009467b48Spatrick #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
3109467b48Spatrick #include "llvm/CodeGen/MachineFunction.h"
3209467b48Spatrick #include "llvm/CodeGen/MachineFunctionPass.h"
3309467b48Spatrick #include "llvm/CodeGen/MachineInstr.h"
3409467b48Spatrick #include "llvm/CodeGen/MachineInstrBuilder.h"
3509467b48Spatrick #include "llvm/CodeGen/MachineOperand.h"
3609467b48Spatrick #include "llvm/CodeGen/MachineRegisterInfo.h"
3709467b48Spatrick #include "llvm/CodeGen/TargetOpcodes.h"
3809467b48Spatrick #include "llvm/CodeGen/TargetRegisterInfo.h"
3909467b48Spatrick #include "llvm/CodeGen/TargetSubtargetInfo.h"
4009467b48Spatrick #include "llvm/IR/DebugLoc.h"
4109467b48Spatrick #include "llvm/MC/MCInstrDesc.h"
4209467b48Spatrick #include "llvm/Pass.h"
4309467b48Spatrick #include "llvm/Support/BranchProbability.h"
4409467b48Spatrick #include "llvm/Support/CommandLine.h"
4509467b48Spatrick #include "llvm/Support/Debug.h"
4609467b48Spatrick #include "llvm/Support/ErrorHandling.h"
4709467b48Spatrick #include "llvm/Support/MathExtras.h"
4809467b48Spatrick #include "llvm/Support/raw_ostream.h"
4909467b48Spatrick #include <cassert>
5009467b48Spatrick #include <cstdint>
5109467b48Spatrick #include <iterator>
5209467b48Spatrick
5309467b48Spatrick using namespace llvm;
5409467b48Spatrick
5509467b48Spatrick #define DEBUG_TYPE "hexagon-nvj"
5609467b48Spatrick
5709467b48Spatrick STATISTIC(NumNVJGenerated, "Number of New Value Jump Instructions created");
5809467b48Spatrick
5909467b48Spatrick static cl::opt<int> DbgNVJCount("nvj-count", cl::init(-1), cl::Hidden,
6009467b48Spatrick cl::desc("Maximum number of predicated jumps to be converted to "
6109467b48Spatrick "New Value Jump"));
6209467b48Spatrick
6309467b48Spatrick static cl::opt<bool> DisableNewValueJumps("disable-nvjump", cl::Hidden,
6409467b48Spatrick cl::desc("Disable New Value Jumps"));
6509467b48Spatrick
6609467b48Spatrick namespace llvm {
6709467b48Spatrick
6809467b48Spatrick FunctionPass *createHexagonNewValueJump();
6909467b48Spatrick void initializeHexagonNewValueJumpPass(PassRegistry&);
7009467b48Spatrick
7109467b48Spatrick } // end namespace llvm
7209467b48Spatrick
7309467b48Spatrick namespace {
7409467b48Spatrick
7509467b48Spatrick struct HexagonNewValueJump : public MachineFunctionPass {
7609467b48Spatrick static char ID;
7709467b48Spatrick
HexagonNewValueJump__anon4adcc2430111::HexagonNewValueJump7809467b48Spatrick HexagonNewValueJump() : MachineFunctionPass(ID) {}
7909467b48Spatrick
getAnalysisUsage__anon4adcc2430111::HexagonNewValueJump8009467b48Spatrick void getAnalysisUsage(AnalysisUsage &AU) const override {
8109467b48Spatrick AU.addRequired<MachineBranchProbabilityInfo>();
8209467b48Spatrick MachineFunctionPass::getAnalysisUsage(AU);
8309467b48Spatrick }
8409467b48Spatrick
getPassName__anon4adcc2430111::HexagonNewValueJump8509467b48Spatrick StringRef getPassName() const override { return "Hexagon NewValueJump"; }
8609467b48Spatrick
8709467b48Spatrick bool runOnMachineFunction(MachineFunction &Fn) override;
8809467b48Spatrick
getRequiredProperties__anon4adcc2430111::HexagonNewValueJump8909467b48Spatrick MachineFunctionProperties getRequiredProperties() const override {
9009467b48Spatrick return MachineFunctionProperties().set(
9109467b48Spatrick MachineFunctionProperties::Property::NoVRegs);
9209467b48Spatrick }
9309467b48Spatrick
9409467b48Spatrick private:
9509467b48Spatrick const HexagonInstrInfo *QII;
9609467b48Spatrick const HexagonRegisterInfo *QRI;
9709467b48Spatrick
9809467b48Spatrick /// A handle to the branch probability pass.
9909467b48Spatrick const MachineBranchProbabilityInfo *MBPI;
10009467b48Spatrick
10109467b48Spatrick bool isNewValueJumpCandidate(const MachineInstr &MI) const;
10209467b48Spatrick };
10309467b48Spatrick
10409467b48Spatrick } // end anonymous namespace
10509467b48Spatrick
10609467b48Spatrick char HexagonNewValueJump::ID = 0;
10709467b48Spatrick
10809467b48Spatrick INITIALIZE_PASS_BEGIN(HexagonNewValueJump, "hexagon-nvj",
10909467b48Spatrick "Hexagon NewValueJump", false, false)
INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)11009467b48Spatrick INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
11109467b48Spatrick INITIALIZE_PASS_END(HexagonNewValueJump, "hexagon-nvj",
11209467b48Spatrick "Hexagon NewValueJump", false, false)
11309467b48Spatrick
11409467b48Spatrick // We have identified this II could be feeder to NVJ,
11509467b48Spatrick // verify that it can be.
11609467b48Spatrick static bool canBeFeederToNewValueJump(const HexagonInstrInfo *QII,
11709467b48Spatrick const TargetRegisterInfo *TRI,
11809467b48Spatrick MachineBasicBlock::iterator II,
11909467b48Spatrick MachineBasicBlock::iterator end,
12009467b48Spatrick MachineBasicBlock::iterator skip,
12109467b48Spatrick MachineFunction &MF) {
12209467b48Spatrick // Predicated instruction can not be feeder to NVJ.
12309467b48Spatrick if (QII->isPredicated(*II))
12409467b48Spatrick return false;
12509467b48Spatrick
12609467b48Spatrick // Bail out if feederReg is a paired register (double regs in
12709467b48Spatrick // our case). One would think that we can check to see if a given
12809467b48Spatrick // register cmpReg1 or cmpReg2 is a sub register of feederReg
12909467b48Spatrick // using -- if (QRI->isSubRegister(feederReg, cmpReg1) logic
13009467b48Spatrick // before the callsite of this function
13109467b48Spatrick // But we can not as it comes in the following fashion.
13209467b48Spatrick // %d0 = Hexagon_S2_lsr_r_p killed %d0, killed %r2
13309467b48Spatrick // %r0 = KILL %r0, implicit killed %d0
13409467b48Spatrick // %p0 = CMPEQri killed %r0, 0
13509467b48Spatrick // Hence, we need to check if it's a KILL instruction.
13609467b48Spatrick if (II->getOpcode() == TargetOpcode::KILL)
13709467b48Spatrick return false;
13809467b48Spatrick
13909467b48Spatrick if (II->isImplicitDef())
14009467b48Spatrick return false;
14109467b48Spatrick
14209467b48Spatrick if (QII->isSolo(*II))
14309467b48Spatrick return false;
14409467b48Spatrick
14509467b48Spatrick if (QII->isFloat(*II))
14609467b48Spatrick return false;
14709467b48Spatrick
14809467b48Spatrick // Make sure that the (unique) def operand is a register from IntRegs.
14909467b48Spatrick bool HadDef = false;
15009467b48Spatrick for (const MachineOperand &Op : II->operands()) {
15109467b48Spatrick if (!Op.isReg() || !Op.isDef())
15209467b48Spatrick continue;
15309467b48Spatrick if (HadDef)
15409467b48Spatrick return false;
15509467b48Spatrick HadDef = true;
15609467b48Spatrick if (!Hexagon::IntRegsRegClass.contains(Op.getReg()))
15709467b48Spatrick return false;
15809467b48Spatrick }
15909467b48Spatrick assert(HadDef);
16009467b48Spatrick
16109467b48Spatrick // Make sure there is no 'def' or 'use' of any of the uses of
16209467b48Spatrick // feeder insn between its definition, this MI and jump, jmpInst
16309467b48Spatrick // skipping compare, cmpInst.
16409467b48Spatrick // Here's the example.
16509467b48Spatrick // r21=memub(r22+r24<<#0)
16609467b48Spatrick // p0 = cmp.eq(r21, #0)
16709467b48Spatrick // r4=memub(r3+r21<<#0)
16809467b48Spatrick // if (p0.new) jump:t .LBB29_45
16909467b48Spatrick // Without this check, it will be converted into
17009467b48Spatrick // r4=memub(r3+r21<<#0)
17109467b48Spatrick // r21=memub(r22+r24<<#0)
17209467b48Spatrick // p0 = cmp.eq(r21, #0)
17309467b48Spatrick // if (p0.new) jump:t .LBB29_45
17409467b48Spatrick // and result WAR hazards if converted to New Value Jump.
17509467b48Spatrick for (unsigned i = 0; i < II->getNumOperands(); ++i) {
17609467b48Spatrick if (II->getOperand(i).isReg() &&
17709467b48Spatrick (II->getOperand(i).isUse() || II->getOperand(i).isDef())) {
17809467b48Spatrick MachineBasicBlock::iterator localII = II;
17909467b48Spatrick ++localII;
18009467b48Spatrick Register Reg = II->getOperand(i).getReg();
18109467b48Spatrick for (MachineBasicBlock::iterator localBegin = localII; localBegin != end;
18209467b48Spatrick ++localBegin) {
18309467b48Spatrick if (localBegin == skip)
18409467b48Spatrick continue;
18509467b48Spatrick // Check for Subregisters too.
18609467b48Spatrick if (localBegin->modifiesRegister(Reg, TRI) ||
18709467b48Spatrick localBegin->readsRegister(Reg, TRI))
18809467b48Spatrick return false;
18909467b48Spatrick }
19009467b48Spatrick }
19109467b48Spatrick }
19209467b48Spatrick return true;
19309467b48Spatrick }
19409467b48Spatrick
19509467b48Spatrick // These are the common checks that need to performed
19609467b48Spatrick // to determine if
19709467b48Spatrick // 1. compare instruction can be moved before jump.
19809467b48Spatrick // 2. feeder to the compare instruction can be moved before jump.
commonChecksToProhibitNewValueJump(bool afterRA,MachineBasicBlock::iterator MII)19909467b48Spatrick static bool commonChecksToProhibitNewValueJump(bool afterRA,
20009467b48Spatrick MachineBasicBlock::iterator MII) {
20109467b48Spatrick // If store in path, bail out.
20209467b48Spatrick if (MII->mayStore())
20309467b48Spatrick return false;
20409467b48Spatrick
20509467b48Spatrick // if call in path, bail out.
20609467b48Spatrick if (MII->isCall())
20709467b48Spatrick return false;
20809467b48Spatrick
20909467b48Spatrick // if NVJ is running prior to RA, do the following checks.
21009467b48Spatrick if (!afterRA) {
21109467b48Spatrick // The following Target Opcode instructions are spurious
21209467b48Spatrick // to new value jump. If they are in the path, bail out.
21309467b48Spatrick // KILL sets kill flag on the opcode. It also sets up a
21409467b48Spatrick // single register, out of pair.
21509467b48Spatrick // %d0 = S2_lsr_r_p killed %d0, killed %r2
21609467b48Spatrick // %r0 = KILL %r0, implicit killed %d0
21709467b48Spatrick // %p0 = C2_cmpeqi killed %r0, 0
21809467b48Spatrick // PHI can be anything after RA.
21909467b48Spatrick // COPY can remateriaze things in between feeder, compare and nvj.
22009467b48Spatrick if (MII->getOpcode() == TargetOpcode::KILL ||
22109467b48Spatrick MII->getOpcode() == TargetOpcode::PHI ||
22209467b48Spatrick MII->getOpcode() == TargetOpcode::COPY)
22309467b48Spatrick return false;
22409467b48Spatrick
22509467b48Spatrick // The following pseudo Hexagon instructions sets "use" and "def"
22609467b48Spatrick // of registers by individual passes in the backend. At this time,
22709467b48Spatrick // we don't know the scope of usage and definitions of these
22809467b48Spatrick // instructions.
22909467b48Spatrick if (MII->getOpcode() == Hexagon::LDriw_pred ||
23009467b48Spatrick MII->getOpcode() == Hexagon::STriw_pred)
23109467b48Spatrick return false;
23209467b48Spatrick }
23309467b48Spatrick
23409467b48Spatrick return true;
23509467b48Spatrick }
23609467b48Spatrick
canCompareBeNewValueJump(const HexagonInstrInfo * QII,const TargetRegisterInfo * TRI,MachineBasicBlock::iterator II,unsigned pReg,bool secondReg,bool optLocation,MachineBasicBlock::iterator end,MachineFunction & MF)23709467b48Spatrick static bool canCompareBeNewValueJump(const HexagonInstrInfo *QII,
23809467b48Spatrick const TargetRegisterInfo *TRI,
23909467b48Spatrick MachineBasicBlock::iterator II,
24009467b48Spatrick unsigned pReg,
24109467b48Spatrick bool secondReg,
24209467b48Spatrick bool optLocation,
24309467b48Spatrick MachineBasicBlock::iterator end,
24409467b48Spatrick MachineFunction &MF) {
24509467b48Spatrick MachineInstr &MI = *II;
24609467b48Spatrick
24709467b48Spatrick // If the second operand of the compare is an imm, make sure it's in the
24809467b48Spatrick // range specified by the arch.
24909467b48Spatrick if (!secondReg) {
25009467b48Spatrick const MachineOperand &Op2 = MI.getOperand(2);
25109467b48Spatrick if (!Op2.isImm())
25209467b48Spatrick return false;
25309467b48Spatrick
25409467b48Spatrick int64_t v = Op2.getImm();
25509467b48Spatrick bool Valid = false;
25609467b48Spatrick
25709467b48Spatrick switch (MI.getOpcode()) {
25809467b48Spatrick case Hexagon::C2_cmpeqi:
25909467b48Spatrick case Hexagon::C4_cmpneqi:
26009467b48Spatrick case Hexagon::C2_cmpgti:
26109467b48Spatrick case Hexagon::C4_cmpltei:
26209467b48Spatrick Valid = (isUInt<5>(v) || v == -1);
26309467b48Spatrick break;
26409467b48Spatrick case Hexagon::C2_cmpgtui:
26509467b48Spatrick case Hexagon::C4_cmplteui:
26609467b48Spatrick Valid = isUInt<5>(v);
26709467b48Spatrick break;
26809467b48Spatrick case Hexagon::S2_tstbit_i:
26909467b48Spatrick case Hexagon::S4_ntstbit_i:
27009467b48Spatrick Valid = (v == 0);
27109467b48Spatrick break;
27209467b48Spatrick }
27309467b48Spatrick
27409467b48Spatrick if (!Valid)
27509467b48Spatrick return false;
27609467b48Spatrick }
27709467b48Spatrick
27809467b48Spatrick unsigned cmpReg1, cmpOp2 = 0; // cmpOp2 assignment silences compiler warning.
27909467b48Spatrick cmpReg1 = MI.getOperand(1).getReg();
28009467b48Spatrick
28109467b48Spatrick if (secondReg) {
28209467b48Spatrick cmpOp2 = MI.getOperand(2).getReg();
28309467b48Spatrick
28409467b48Spatrick // If the same register appears as both operands, we cannot generate a new
28509467b48Spatrick // value compare. Only one operand may use the .new suffix.
28609467b48Spatrick if (cmpReg1 == cmpOp2)
28709467b48Spatrick return false;
28809467b48Spatrick
28909467b48Spatrick // Make sure that the second register is not from COPY
29009467b48Spatrick // at machine code level, we don't need this, but if we decide
29109467b48Spatrick // to move new value jump prior to RA, we would be needing this.
29209467b48Spatrick MachineRegisterInfo &MRI = MF.getRegInfo();
293097a140dSpatrick if (!Register::isPhysicalRegister(cmpOp2)) {
29409467b48Spatrick MachineInstr *def = MRI.getVRegDef(cmpOp2);
29509467b48Spatrick if (def->getOpcode() == TargetOpcode::COPY)
29609467b48Spatrick return false;
29709467b48Spatrick }
29809467b48Spatrick }
29909467b48Spatrick
30009467b48Spatrick // Walk the instructions after the compare (predicate def) to the jump,
30109467b48Spatrick // and satisfy the following conditions.
30209467b48Spatrick ++II;
30309467b48Spatrick for (MachineBasicBlock::iterator localII = II; localII != end; ++localII) {
30409467b48Spatrick if (localII->isDebugInstr())
30509467b48Spatrick continue;
30609467b48Spatrick
30709467b48Spatrick // Check 1.
30809467b48Spatrick // If "common" checks fail, bail out.
30909467b48Spatrick if (!commonChecksToProhibitNewValueJump(optLocation, localII))
31009467b48Spatrick return false;
31109467b48Spatrick
31209467b48Spatrick // Check 2.
31309467b48Spatrick // If there is a def or use of predicate (result of compare), bail out.
31409467b48Spatrick if (localII->modifiesRegister(pReg, TRI) ||
31509467b48Spatrick localII->readsRegister(pReg, TRI))
31609467b48Spatrick return false;
31709467b48Spatrick
31809467b48Spatrick // Check 3.
31909467b48Spatrick // If there is a def of any of the use of the compare (operands of compare),
32009467b48Spatrick // bail out.
32109467b48Spatrick // Eg.
32209467b48Spatrick // p0 = cmp.eq(r2, r0)
32309467b48Spatrick // r2 = r4
32409467b48Spatrick // if (p0.new) jump:t .LBB28_3
32509467b48Spatrick if (localII->modifiesRegister(cmpReg1, TRI) ||
32609467b48Spatrick (secondReg && localII->modifiesRegister(cmpOp2, TRI)))
32709467b48Spatrick return false;
32809467b48Spatrick }
32909467b48Spatrick return true;
33009467b48Spatrick }
33109467b48Spatrick
33209467b48Spatrick // Given a compare operator, return a matching New Value Jump compare operator.
33309467b48Spatrick // Make sure that MI here is included in isNewValueJumpCandidate.
getNewValueJumpOpcode(MachineInstr * MI,int reg,bool secondRegNewified,MachineBasicBlock * jmpTarget,const MachineBranchProbabilityInfo * MBPI)33409467b48Spatrick static unsigned getNewValueJumpOpcode(MachineInstr *MI, int reg,
33509467b48Spatrick bool secondRegNewified,
33609467b48Spatrick MachineBasicBlock *jmpTarget,
33709467b48Spatrick const MachineBranchProbabilityInfo
33809467b48Spatrick *MBPI) {
33909467b48Spatrick bool taken = false;
34009467b48Spatrick MachineBasicBlock *Src = MI->getParent();
34109467b48Spatrick const BranchProbability Prediction =
34209467b48Spatrick MBPI->getEdgeProbability(Src, jmpTarget);
34309467b48Spatrick
34409467b48Spatrick if (Prediction >= BranchProbability(1,2))
34509467b48Spatrick taken = true;
34609467b48Spatrick
34709467b48Spatrick switch (MI->getOpcode()) {
34809467b48Spatrick case Hexagon::C2_cmpeq:
34909467b48Spatrick return taken ? Hexagon::J4_cmpeq_t_jumpnv_t
35009467b48Spatrick : Hexagon::J4_cmpeq_t_jumpnv_nt;
35109467b48Spatrick
35209467b48Spatrick case Hexagon::C2_cmpeqi:
35309467b48Spatrick if (reg >= 0)
35409467b48Spatrick return taken ? Hexagon::J4_cmpeqi_t_jumpnv_t
35509467b48Spatrick : Hexagon::J4_cmpeqi_t_jumpnv_nt;
35609467b48Spatrick return taken ? Hexagon::J4_cmpeqn1_t_jumpnv_t
35709467b48Spatrick : Hexagon::J4_cmpeqn1_t_jumpnv_nt;
35809467b48Spatrick
35909467b48Spatrick case Hexagon::C4_cmpneqi:
36009467b48Spatrick if (reg >= 0)
36109467b48Spatrick return taken ? Hexagon::J4_cmpeqi_f_jumpnv_t
36209467b48Spatrick : Hexagon::J4_cmpeqi_f_jumpnv_nt;
36309467b48Spatrick return taken ? Hexagon::J4_cmpeqn1_f_jumpnv_t :
36409467b48Spatrick Hexagon::J4_cmpeqn1_f_jumpnv_nt;
36509467b48Spatrick
36609467b48Spatrick case Hexagon::C2_cmpgt:
36709467b48Spatrick if (secondRegNewified)
36809467b48Spatrick return taken ? Hexagon::J4_cmplt_t_jumpnv_t
36909467b48Spatrick : Hexagon::J4_cmplt_t_jumpnv_nt;
37009467b48Spatrick return taken ? Hexagon::J4_cmpgt_t_jumpnv_t
37109467b48Spatrick : Hexagon::J4_cmpgt_t_jumpnv_nt;
37209467b48Spatrick
37309467b48Spatrick case Hexagon::C2_cmpgti:
37409467b48Spatrick if (reg >= 0)
37509467b48Spatrick return taken ? Hexagon::J4_cmpgti_t_jumpnv_t
37609467b48Spatrick : Hexagon::J4_cmpgti_t_jumpnv_nt;
37709467b48Spatrick return taken ? Hexagon::J4_cmpgtn1_t_jumpnv_t
37809467b48Spatrick : Hexagon::J4_cmpgtn1_t_jumpnv_nt;
37909467b48Spatrick
38009467b48Spatrick case Hexagon::C2_cmpgtu:
38109467b48Spatrick if (secondRegNewified)
38209467b48Spatrick return taken ? Hexagon::J4_cmpltu_t_jumpnv_t
38309467b48Spatrick : Hexagon::J4_cmpltu_t_jumpnv_nt;
38409467b48Spatrick return taken ? Hexagon::J4_cmpgtu_t_jumpnv_t
38509467b48Spatrick : Hexagon::J4_cmpgtu_t_jumpnv_nt;
38609467b48Spatrick
38709467b48Spatrick case Hexagon::C2_cmpgtui:
38809467b48Spatrick return taken ? Hexagon::J4_cmpgtui_t_jumpnv_t
38909467b48Spatrick : Hexagon::J4_cmpgtui_t_jumpnv_nt;
39009467b48Spatrick
39109467b48Spatrick case Hexagon::C4_cmpneq:
39209467b48Spatrick return taken ? Hexagon::J4_cmpeq_f_jumpnv_t
39309467b48Spatrick : Hexagon::J4_cmpeq_f_jumpnv_nt;
39409467b48Spatrick
39509467b48Spatrick case Hexagon::C4_cmplte:
39609467b48Spatrick if (secondRegNewified)
39709467b48Spatrick return taken ? Hexagon::J4_cmplt_f_jumpnv_t
39809467b48Spatrick : Hexagon::J4_cmplt_f_jumpnv_nt;
39909467b48Spatrick return taken ? Hexagon::J4_cmpgt_f_jumpnv_t
40009467b48Spatrick : Hexagon::J4_cmpgt_f_jumpnv_nt;
40109467b48Spatrick
40209467b48Spatrick case Hexagon::C4_cmplteu:
40309467b48Spatrick if (secondRegNewified)
40409467b48Spatrick return taken ? Hexagon::J4_cmpltu_f_jumpnv_t
40509467b48Spatrick : Hexagon::J4_cmpltu_f_jumpnv_nt;
40609467b48Spatrick return taken ? Hexagon::J4_cmpgtu_f_jumpnv_t
40709467b48Spatrick : Hexagon::J4_cmpgtu_f_jumpnv_nt;
40809467b48Spatrick
40909467b48Spatrick case Hexagon::C4_cmpltei:
41009467b48Spatrick if (reg >= 0)
41109467b48Spatrick return taken ? Hexagon::J4_cmpgti_f_jumpnv_t
41209467b48Spatrick : Hexagon::J4_cmpgti_f_jumpnv_nt;
41309467b48Spatrick return taken ? Hexagon::J4_cmpgtn1_f_jumpnv_t
41409467b48Spatrick : Hexagon::J4_cmpgtn1_f_jumpnv_nt;
41509467b48Spatrick
41609467b48Spatrick case Hexagon::C4_cmplteui:
41709467b48Spatrick return taken ? Hexagon::J4_cmpgtui_f_jumpnv_t
41809467b48Spatrick : Hexagon::J4_cmpgtui_f_jumpnv_nt;
41909467b48Spatrick
42009467b48Spatrick default:
42109467b48Spatrick llvm_unreachable("Could not find matching New Value Jump instruction.");
42209467b48Spatrick }
42309467b48Spatrick // return *some value* to avoid compiler warning
42409467b48Spatrick return 0;
42509467b48Spatrick }
42609467b48Spatrick
isNewValueJumpCandidate(const MachineInstr & MI) const42709467b48Spatrick bool HexagonNewValueJump::isNewValueJumpCandidate(
42809467b48Spatrick const MachineInstr &MI) const {
42909467b48Spatrick switch (MI.getOpcode()) {
43009467b48Spatrick case Hexagon::C2_cmpeq:
43109467b48Spatrick case Hexagon::C2_cmpeqi:
43209467b48Spatrick case Hexagon::C2_cmpgt:
43309467b48Spatrick case Hexagon::C2_cmpgti:
43409467b48Spatrick case Hexagon::C2_cmpgtu:
43509467b48Spatrick case Hexagon::C2_cmpgtui:
43609467b48Spatrick case Hexagon::C4_cmpneq:
43709467b48Spatrick case Hexagon::C4_cmpneqi:
43809467b48Spatrick case Hexagon::C4_cmplte:
43909467b48Spatrick case Hexagon::C4_cmplteu:
44009467b48Spatrick case Hexagon::C4_cmpltei:
44109467b48Spatrick case Hexagon::C4_cmplteui:
44209467b48Spatrick return true;
44309467b48Spatrick
44409467b48Spatrick default:
44509467b48Spatrick return false;
44609467b48Spatrick }
44709467b48Spatrick }
44809467b48Spatrick
runOnMachineFunction(MachineFunction & MF)44909467b48Spatrick bool HexagonNewValueJump::runOnMachineFunction(MachineFunction &MF) {
45009467b48Spatrick LLVM_DEBUG(dbgs() << "********** Hexagon New Value Jump **********\n"
45109467b48Spatrick << "********** Function: " << MF.getName() << "\n");
45209467b48Spatrick
45309467b48Spatrick if (skipFunction(MF.getFunction()))
45409467b48Spatrick return false;
45509467b48Spatrick
45609467b48Spatrick // If we move NewValueJump before register allocation we'll need live variable
45709467b48Spatrick // analysis here too.
45809467b48Spatrick
45909467b48Spatrick QII = static_cast<const HexagonInstrInfo *>(MF.getSubtarget().getInstrInfo());
46009467b48Spatrick QRI = static_cast<const HexagonRegisterInfo *>(
46109467b48Spatrick MF.getSubtarget().getRegisterInfo());
46209467b48Spatrick MBPI = &getAnalysis<MachineBranchProbabilityInfo>();
46309467b48Spatrick
46409467b48Spatrick if (DisableNewValueJumps ||
46509467b48Spatrick !MF.getSubtarget<HexagonSubtarget>().useNewValueJumps())
46609467b48Spatrick return false;
46709467b48Spatrick
46809467b48Spatrick int nvjCount = DbgNVJCount;
46909467b48Spatrick int nvjGenerated = 0;
47009467b48Spatrick
47109467b48Spatrick // Loop through all the bb's of the function
47209467b48Spatrick for (MachineFunction::iterator MBBb = MF.begin(), MBBe = MF.end();
47309467b48Spatrick MBBb != MBBe; ++MBBb) {
47409467b48Spatrick MachineBasicBlock *MBB = &*MBBb;
47509467b48Spatrick
47609467b48Spatrick LLVM_DEBUG(dbgs() << "** dumping bb ** " << MBB->getNumber() << "\n");
47709467b48Spatrick LLVM_DEBUG(MBB->dump());
47809467b48Spatrick LLVM_DEBUG(dbgs() << "\n"
47909467b48Spatrick << "********** dumping instr bottom up **********\n");
48009467b48Spatrick bool foundJump = false;
48109467b48Spatrick bool foundCompare = false;
48209467b48Spatrick bool invertPredicate = false;
48309467b48Spatrick unsigned predReg = 0; // predicate reg of the jump.
48409467b48Spatrick unsigned cmpReg1 = 0;
48509467b48Spatrick int cmpOp2 = 0;
48609467b48Spatrick MachineBasicBlock::iterator jmpPos;
48709467b48Spatrick MachineBasicBlock::iterator cmpPos;
48809467b48Spatrick MachineInstr *cmpInstr = nullptr, *jmpInstr = nullptr;
48909467b48Spatrick MachineBasicBlock *jmpTarget = nullptr;
49009467b48Spatrick bool afterRA = false;
49109467b48Spatrick bool isSecondOpReg = false;
49209467b48Spatrick bool isSecondOpNewified = false;
49309467b48Spatrick // Traverse the basic block - bottom up
49409467b48Spatrick for (MachineBasicBlock::iterator MII = MBB->end(), E = MBB->begin();
49509467b48Spatrick MII != E;) {
49609467b48Spatrick MachineInstr &MI = *--MII;
49709467b48Spatrick if (MI.isDebugInstr()) {
49809467b48Spatrick continue;
49909467b48Spatrick }
50009467b48Spatrick
50109467b48Spatrick if ((nvjCount == 0) || (nvjCount > -1 && nvjCount <= nvjGenerated))
50209467b48Spatrick break;
50309467b48Spatrick
50409467b48Spatrick LLVM_DEBUG(dbgs() << "Instr: "; MI.dump(); dbgs() << "\n");
50509467b48Spatrick
50609467b48Spatrick if (!foundJump && (MI.getOpcode() == Hexagon::J2_jumpt ||
50709467b48Spatrick MI.getOpcode() == Hexagon::J2_jumptpt ||
50809467b48Spatrick MI.getOpcode() == Hexagon::J2_jumpf ||
50909467b48Spatrick MI.getOpcode() == Hexagon::J2_jumpfpt ||
51009467b48Spatrick MI.getOpcode() == Hexagon::J2_jumptnewpt ||
51109467b48Spatrick MI.getOpcode() == Hexagon::J2_jumptnew ||
51209467b48Spatrick MI.getOpcode() == Hexagon::J2_jumpfnewpt ||
51309467b48Spatrick MI.getOpcode() == Hexagon::J2_jumpfnew)) {
51409467b48Spatrick // This is where you would insert your compare and
51509467b48Spatrick // instr that feeds compare
51609467b48Spatrick jmpPos = MII;
51709467b48Spatrick jmpInstr = &MI;
51809467b48Spatrick predReg = MI.getOperand(0).getReg();
51909467b48Spatrick afterRA = Register::isPhysicalRegister(predReg);
52009467b48Spatrick
52109467b48Spatrick // If ifconverter had not messed up with the kill flags of the
52209467b48Spatrick // operands, the following check on the kill flag would suffice.
52309467b48Spatrick // if(!jmpInstr->getOperand(0).isKill()) break;
52409467b48Spatrick
52509467b48Spatrick // This predicate register is live out of BB
52609467b48Spatrick // this would only work if we can actually use Live
52709467b48Spatrick // variable analysis on phy regs - but LLVM does not
52809467b48Spatrick // provide LV analysis on phys regs.
52909467b48Spatrick //if(LVs.isLiveOut(predReg, *MBB)) break;
53009467b48Spatrick
53109467b48Spatrick // Get all the successors of this block - which will always
53209467b48Spatrick // be 2. Check if the predicate register is live-in in those
53309467b48Spatrick // successor. If yes, we can not delete the predicate -
53409467b48Spatrick // I am doing this only because LLVM does not provide LiveOut
53509467b48Spatrick // at the BB level.
53609467b48Spatrick bool predLive = false;
537*d415bd75Srobert for (const MachineBasicBlock *SuccMBB : MBB->successors())
538*d415bd75Srobert if (SuccMBB->isLiveIn(predReg))
53909467b48Spatrick predLive = true;
54009467b48Spatrick if (predLive)
54109467b48Spatrick break;
54209467b48Spatrick
54309467b48Spatrick if (!MI.getOperand(1).isMBB())
54409467b48Spatrick continue;
54509467b48Spatrick jmpTarget = MI.getOperand(1).getMBB();
54609467b48Spatrick foundJump = true;
54709467b48Spatrick if (MI.getOpcode() == Hexagon::J2_jumpf ||
54809467b48Spatrick MI.getOpcode() == Hexagon::J2_jumpfnewpt ||
54909467b48Spatrick MI.getOpcode() == Hexagon::J2_jumpfnew) {
55009467b48Spatrick invertPredicate = true;
55109467b48Spatrick }
55209467b48Spatrick continue;
55309467b48Spatrick }
55409467b48Spatrick
55509467b48Spatrick // No new value jump if there is a barrier. A barrier has to be in its
55609467b48Spatrick // own packet. A barrier has zero operands. We conservatively bail out
55709467b48Spatrick // here if we see any instruction with zero operands.
55809467b48Spatrick if (foundJump && MI.getNumOperands() == 0)
55909467b48Spatrick break;
56009467b48Spatrick
56109467b48Spatrick if (foundJump && !foundCompare && MI.getOperand(0).isReg() &&
56209467b48Spatrick MI.getOperand(0).getReg() == predReg) {
56309467b48Spatrick // Not all compares can be new value compare. Arch Spec: 7.6.1.1
56409467b48Spatrick if (isNewValueJumpCandidate(MI)) {
56509467b48Spatrick assert(
56609467b48Spatrick (MI.getDesc().isCompare()) &&
56709467b48Spatrick "Only compare instruction can be collapsed into New Value Jump");
56809467b48Spatrick isSecondOpReg = MI.getOperand(2).isReg();
56909467b48Spatrick
57009467b48Spatrick if (!canCompareBeNewValueJump(QII, QRI, MII, predReg, isSecondOpReg,
57109467b48Spatrick afterRA, jmpPos, MF))
57209467b48Spatrick break;
57309467b48Spatrick
57409467b48Spatrick cmpInstr = &MI;
57509467b48Spatrick cmpPos = MII;
57609467b48Spatrick foundCompare = true;
57709467b48Spatrick
57809467b48Spatrick // We need cmpReg1 and cmpOp2(imm or reg) while building
57909467b48Spatrick // new value jump instruction.
58009467b48Spatrick cmpReg1 = MI.getOperand(1).getReg();
58109467b48Spatrick
58209467b48Spatrick if (isSecondOpReg)
58309467b48Spatrick cmpOp2 = MI.getOperand(2).getReg();
58409467b48Spatrick else
58509467b48Spatrick cmpOp2 = MI.getOperand(2).getImm();
58609467b48Spatrick continue;
58709467b48Spatrick }
58809467b48Spatrick }
58909467b48Spatrick
59009467b48Spatrick if (foundCompare && foundJump) {
59109467b48Spatrick // If "common" checks fail, bail out on this BB.
59209467b48Spatrick if (!commonChecksToProhibitNewValueJump(afterRA, MII))
59309467b48Spatrick break;
59409467b48Spatrick
59509467b48Spatrick bool foundFeeder = false;
59609467b48Spatrick MachineBasicBlock::iterator feederPos = MII;
59709467b48Spatrick if (MI.getOperand(0).isReg() && MI.getOperand(0).isDef() &&
59809467b48Spatrick (MI.getOperand(0).getReg() == cmpReg1 ||
59909467b48Spatrick (isSecondOpReg &&
60009467b48Spatrick MI.getOperand(0).getReg() == (unsigned)cmpOp2))) {
60109467b48Spatrick
60209467b48Spatrick Register feederReg = MI.getOperand(0).getReg();
60309467b48Spatrick
60409467b48Spatrick // First try to see if we can get the feeder from the first operand
60509467b48Spatrick // of the compare. If we can not, and if secondOpReg is true
60609467b48Spatrick // (second operand of the compare is also register), try that one.
60709467b48Spatrick // TODO: Try to come up with some heuristic to figure out which
60809467b48Spatrick // feeder would benefit.
60909467b48Spatrick
61009467b48Spatrick if (feederReg == cmpReg1) {
61109467b48Spatrick if (!canBeFeederToNewValueJump(QII, QRI, MII, jmpPos, cmpPos, MF)) {
61209467b48Spatrick if (!isSecondOpReg)
61309467b48Spatrick break;
61409467b48Spatrick else
61509467b48Spatrick continue;
61609467b48Spatrick } else
61709467b48Spatrick foundFeeder = true;
61809467b48Spatrick }
61909467b48Spatrick
62009467b48Spatrick if (!foundFeeder && isSecondOpReg && feederReg == (unsigned)cmpOp2)
62109467b48Spatrick if (!canBeFeederToNewValueJump(QII, QRI, MII, jmpPos, cmpPos, MF))
62209467b48Spatrick break;
62309467b48Spatrick
62409467b48Spatrick if (isSecondOpReg) {
62509467b48Spatrick // In case of CMPLT, or CMPLTU, or EQ with the second register
62609467b48Spatrick // to newify, swap the operands.
62709467b48Spatrick unsigned COp = cmpInstr->getOpcode();
62809467b48Spatrick if ((COp == Hexagon::C2_cmpeq || COp == Hexagon::C4_cmpneq) &&
62909467b48Spatrick (feederReg == (unsigned)cmpOp2)) {
63009467b48Spatrick unsigned tmp = cmpReg1;
63109467b48Spatrick cmpReg1 = cmpOp2;
63209467b48Spatrick cmpOp2 = tmp;
63309467b48Spatrick }
63409467b48Spatrick
63509467b48Spatrick // Now we have swapped the operands, all we need to check is,
63609467b48Spatrick // if the second operand (after swap) is the feeder.
63709467b48Spatrick // And if it is, make a note.
63809467b48Spatrick if (feederReg == (unsigned)cmpOp2)
63909467b48Spatrick isSecondOpNewified = true;
64009467b48Spatrick }
64109467b48Spatrick
64209467b48Spatrick // Now that we are moving feeder close the jump,
64309467b48Spatrick // make sure we are respecting the kill values of
64409467b48Spatrick // the operands of the feeder.
64509467b48Spatrick
64609467b48Spatrick auto TransferKills = [jmpPos,cmpPos] (MachineInstr &MI) {
64709467b48Spatrick for (MachineOperand &MO : MI.operands()) {
64809467b48Spatrick if (!MO.isReg() || !MO.isUse())
64909467b48Spatrick continue;
65009467b48Spatrick Register UseR = MO.getReg();
65109467b48Spatrick for (auto I = std::next(MI.getIterator()); I != jmpPos; ++I) {
65209467b48Spatrick if (I == cmpPos)
65309467b48Spatrick continue;
65409467b48Spatrick for (MachineOperand &Op : I->operands()) {
65509467b48Spatrick if (!Op.isReg() || !Op.isUse() || !Op.isKill())
65609467b48Spatrick continue;
65709467b48Spatrick if (Op.getReg() != UseR)
65809467b48Spatrick continue;
65909467b48Spatrick // We found that there is kill of a use register
66009467b48Spatrick // Set up a kill flag on the register
66109467b48Spatrick Op.setIsKill(false);
66209467b48Spatrick MO.setIsKill(true);
66309467b48Spatrick return;
66409467b48Spatrick }
66509467b48Spatrick }
66609467b48Spatrick }
66709467b48Spatrick };
66809467b48Spatrick
66909467b48Spatrick TransferKills(*feederPos);
67009467b48Spatrick TransferKills(*cmpPos);
67109467b48Spatrick bool MO1IsKill = cmpPos->killsRegister(cmpReg1, QRI);
67209467b48Spatrick bool MO2IsKill = isSecondOpReg && cmpPos->killsRegister(cmpOp2, QRI);
67309467b48Spatrick
67409467b48Spatrick MBB->splice(jmpPos, MI.getParent(), MI);
67509467b48Spatrick MBB->splice(jmpPos, MI.getParent(), cmpInstr);
67609467b48Spatrick DebugLoc dl = MI.getDebugLoc();
67709467b48Spatrick MachineInstr *NewMI;
67809467b48Spatrick
67909467b48Spatrick assert((isNewValueJumpCandidate(*cmpInstr)) &&
68009467b48Spatrick "This compare is not a New Value Jump candidate.");
68109467b48Spatrick unsigned opc = getNewValueJumpOpcode(cmpInstr, cmpOp2,
68209467b48Spatrick isSecondOpNewified,
68309467b48Spatrick jmpTarget, MBPI);
68409467b48Spatrick if (invertPredicate)
68509467b48Spatrick opc = QII->getInvertedPredicatedOpcode(opc);
68609467b48Spatrick
68709467b48Spatrick if (isSecondOpReg)
68809467b48Spatrick NewMI = BuildMI(*MBB, jmpPos, dl, QII->get(opc))
68909467b48Spatrick .addReg(cmpReg1, getKillRegState(MO1IsKill))
69009467b48Spatrick .addReg(cmpOp2, getKillRegState(MO2IsKill))
69109467b48Spatrick .addMBB(jmpTarget);
69209467b48Spatrick
69309467b48Spatrick else
69409467b48Spatrick NewMI = BuildMI(*MBB, jmpPos, dl, QII->get(opc))
69509467b48Spatrick .addReg(cmpReg1, getKillRegState(MO1IsKill))
69609467b48Spatrick .addImm(cmpOp2)
69709467b48Spatrick .addMBB(jmpTarget);
69809467b48Spatrick
69909467b48Spatrick assert(NewMI && "New Value Jump Instruction Not created!");
70009467b48Spatrick (void)NewMI;
70109467b48Spatrick if (cmpInstr->getOperand(0).isReg() &&
70209467b48Spatrick cmpInstr->getOperand(0).isKill())
70309467b48Spatrick cmpInstr->getOperand(0).setIsKill(false);
70409467b48Spatrick if (cmpInstr->getOperand(1).isReg() &&
70509467b48Spatrick cmpInstr->getOperand(1).isKill())
70609467b48Spatrick cmpInstr->getOperand(1).setIsKill(false);
70709467b48Spatrick cmpInstr->eraseFromParent();
70809467b48Spatrick jmpInstr->eraseFromParent();
70909467b48Spatrick ++nvjGenerated;
71009467b48Spatrick ++NumNVJGenerated;
71109467b48Spatrick break;
71209467b48Spatrick }
71309467b48Spatrick }
71409467b48Spatrick }
71509467b48Spatrick }
71609467b48Spatrick
71709467b48Spatrick return true;
71809467b48Spatrick }
71909467b48Spatrick
createHexagonNewValueJump()72009467b48Spatrick FunctionPass *llvm::createHexagonNewValueJump() {
72109467b48Spatrick return new HexagonNewValueJump();
72209467b48Spatrick }
723