xref: /freebsd/contrib/llvm-project/llvm/lib/Target/Hexagon/HexagonNewValueJump.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //===- HexagonNewValueJump.cpp - Hexagon Backend New Value Jump -----------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This implements NewValueJump pass in Hexagon.
100b57cec5SDimitry Andric // Ideally, we should merge this as a Peephole pass prior to register
110b57cec5SDimitry Andric // allocation, but because we have a spill in between the feeder and new value
120b57cec5SDimitry Andric // jump instructions, we are forced to write after register allocation.
130b57cec5SDimitry Andric // Having said that, we should re-attempt to pull this earlier at some point
140b57cec5SDimitry Andric // in future.
150b57cec5SDimitry Andric 
160b57cec5SDimitry Andric // The basic approach looks for sequence of predicated jump, compare instruciton
170b57cec5SDimitry Andric // that genereates the predicate and, the feeder to the predicate. Once it finds
180b57cec5SDimitry Andric // all, it collapses compare and jump instruction into a new value jump
190b57cec5SDimitry Andric // intstructions.
200b57cec5SDimitry Andric //
210b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
220b57cec5SDimitry Andric 
23480093f4SDimitry Andric #include "llvm/InitializePasses.h"
240b57cec5SDimitry Andric #include "Hexagon.h"
250b57cec5SDimitry Andric #include "HexagonInstrInfo.h"
260b57cec5SDimitry Andric #include "HexagonRegisterInfo.h"
270b57cec5SDimitry Andric #include "HexagonSubtarget.h"
280b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h"
290b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h"
300b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
310b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
320b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
330b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
340b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
350b57cec5SDimitry Andric #include "llvm/CodeGen/MachineOperand.h"
360b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
370b57cec5SDimitry Andric #include "llvm/CodeGen/TargetOpcodes.h"
380b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
390b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
400b57cec5SDimitry Andric #include "llvm/IR/DebugLoc.h"
410b57cec5SDimitry Andric #include "llvm/MC/MCInstrDesc.h"
420b57cec5SDimitry Andric #include "llvm/Pass.h"
430b57cec5SDimitry Andric #include "llvm/Support/BranchProbability.h"
440b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
450b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
460b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
470b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h"
480b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
490b57cec5SDimitry Andric #include <cassert>
500b57cec5SDimitry Andric #include <cstdint>
510b57cec5SDimitry Andric #include <iterator>
520b57cec5SDimitry Andric 
530b57cec5SDimitry Andric using namespace llvm;
540b57cec5SDimitry Andric 
550b57cec5SDimitry Andric #define DEBUG_TYPE "hexagon-nvj"
560b57cec5SDimitry Andric 
570b57cec5SDimitry Andric STATISTIC(NumNVJGenerated, "Number of New Value Jump Instructions created");
580b57cec5SDimitry Andric 
590b57cec5SDimitry Andric static cl::opt<int> DbgNVJCount("nvj-count", cl::init(-1), cl::Hidden,
600b57cec5SDimitry Andric     cl::desc("Maximum number of predicated jumps to be converted to "
610b57cec5SDimitry Andric     "New Value Jump"));
620b57cec5SDimitry Andric 
630b57cec5SDimitry Andric static cl::opt<bool> DisableNewValueJumps("disable-nvjump", cl::Hidden,
640b57cec5SDimitry Andric                                           cl::desc("Disable New Value Jumps"));
650b57cec5SDimitry Andric 
660b57cec5SDimitry Andric namespace llvm {
670b57cec5SDimitry Andric 
680b57cec5SDimitry Andric FunctionPass *createHexagonNewValueJump();
690b57cec5SDimitry Andric void initializeHexagonNewValueJumpPass(PassRegistry&);
700b57cec5SDimitry Andric 
710b57cec5SDimitry Andric } // end namespace llvm
720b57cec5SDimitry Andric 
730b57cec5SDimitry Andric namespace {
740b57cec5SDimitry Andric 
750b57cec5SDimitry Andric   struct HexagonNewValueJump : public MachineFunctionPass {
760b57cec5SDimitry Andric     static char ID;
770b57cec5SDimitry Andric 
HexagonNewValueJump__anon8d77136c0111::HexagonNewValueJump780b57cec5SDimitry Andric     HexagonNewValueJump() : MachineFunctionPass(ID) {}
790b57cec5SDimitry Andric 
getAnalysisUsage__anon8d77136c0111::HexagonNewValueJump800b57cec5SDimitry Andric     void getAnalysisUsage(AnalysisUsage &AU) const override {
81*0fca6ea1SDimitry Andric       AU.addRequired<MachineBranchProbabilityInfoWrapperPass>();
820b57cec5SDimitry Andric       MachineFunctionPass::getAnalysisUsage(AU);
830b57cec5SDimitry Andric     }
840b57cec5SDimitry Andric 
getPassName__anon8d77136c0111::HexagonNewValueJump850b57cec5SDimitry Andric     StringRef getPassName() const override { return "Hexagon NewValueJump"; }
860b57cec5SDimitry Andric 
870b57cec5SDimitry Andric     bool runOnMachineFunction(MachineFunction &Fn) override;
880b57cec5SDimitry Andric 
getRequiredProperties__anon8d77136c0111::HexagonNewValueJump890b57cec5SDimitry Andric     MachineFunctionProperties getRequiredProperties() const override {
900b57cec5SDimitry Andric       return MachineFunctionProperties().set(
910b57cec5SDimitry Andric           MachineFunctionProperties::Property::NoVRegs);
920b57cec5SDimitry Andric     }
930b57cec5SDimitry Andric 
940b57cec5SDimitry Andric   private:
950b57cec5SDimitry Andric     const HexagonInstrInfo *QII;
960b57cec5SDimitry Andric     const HexagonRegisterInfo *QRI;
970b57cec5SDimitry Andric 
980b57cec5SDimitry Andric     /// A handle to the branch probability pass.
990b57cec5SDimitry Andric     const MachineBranchProbabilityInfo *MBPI;
1000b57cec5SDimitry Andric 
1010b57cec5SDimitry Andric     bool isNewValueJumpCandidate(const MachineInstr &MI) const;
1020b57cec5SDimitry Andric   };
1030b57cec5SDimitry Andric 
1040b57cec5SDimitry Andric } // end anonymous namespace
1050b57cec5SDimitry Andric 
1060b57cec5SDimitry Andric char HexagonNewValueJump::ID = 0;
1070b57cec5SDimitry Andric 
1080b57cec5SDimitry Andric INITIALIZE_PASS_BEGIN(HexagonNewValueJump, "hexagon-nvj",
1090b57cec5SDimitry Andric                       "Hexagon NewValueJump", false, false)
INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfoWrapperPass)110*0fca6ea1SDimitry Andric INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfoWrapperPass)
1110b57cec5SDimitry Andric INITIALIZE_PASS_END(HexagonNewValueJump, "hexagon-nvj",
1120b57cec5SDimitry Andric                     "Hexagon NewValueJump", false, false)
1130b57cec5SDimitry Andric 
1140b57cec5SDimitry Andric // We have identified this II could be feeder to NVJ,
1150b57cec5SDimitry Andric // verify that it can be.
1160b57cec5SDimitry Andric static bool canBeFeederToNewValueJump(const HexagonInstrInfo *QII,
1170b57cec5SDimitry Andric                                       const TargetRegisterInfo *TRI,
1180b57cec5SDimitry Andric                                       MachineBasicBlock::iterator II,
1190b57cec5SDimitry Andric                                       MachineBasicBlock::iterator end,
1200b57cec5SDimitry Andric                                       MachineBasicBlock::iterator skip,
1210b57cec5SDimitry Andric                                       MachineFunction &MF) {
1220b57cec5SDimitry Andric   // Predicated instruction can not be feeder to NVJ.
1230b57cec5SDimitry Andric   if (QII->isPredicated(*II))
1240b57cec5SDimitry Andric     return false;
1250b57cec5SDimitry Andric 
1260b57cec5SDimitry Andric   // Bail out if feederReg is a paired register (double regs in
1270b57cec5SDimitry Andric   // our case). One would think that we can check to see if a given
1280b57cec5SDimitry Andric   // register cmpReg1 or cmpReg2 is a sub register of feederReg
1290b57cec5SDimitry Andric   // using -- if (QRI->isSubRegister(feederReg, cmpReg1) logic
1300b57cec5SDimitry Andric   // before the callsite of this function
1310b57cec5SDimitry Andric   // But we can not as it comes in the following fashion.
1320b57cec5SDimitry Andric   //    %d0 = Hexagon_S2_lsr_r_p killed %d0, killed %r2
1330b57cec5SDimitry Andric   //    %r0 = KILL %r0, implicit killed %d0
1340b57cec5SDimitry Andric   //    %p0 = CMPEQri killed %r0, 0
1350b57cec5SDimitry Andric   // Hence, we need to check if it's a KILL instruction.
1360b57cec5SDimitry Andric   if (II->getOpcode() == TargetOpcode::KILL)
1370b57cec5SDimitry Andric     return false;
1380b57cec5SDimitry Andric 
1390b57cec5SDimitry Andric   if (II->isImplicitDef())
1400b57cec5SDimitry Andric     return false;
1410b57cec5SDimitry Andric 
1420b57cec5SDimitry Andric   if (QII->isSolo(*II))
1430b57cec5SDimitry Andric     return false;
1440b57cec5SDimitry Andric 
1450b57cec5SDimitry Andric   if (QII->isFloat(*II))
1460b57cec5SDimitry Andric     return false;
1470b57cec5SDimitry Andric 
1480b57cec5SDimitry Andric   // Make sure that the (unique) def operand is a register from IntRegs.
1490b57cec5SDimitry Andric   bool HadDef = false;
1500b57cec5SDimitry Andric   for (const MachineOperand &Op : II->operands()) {
1510b57cec5SDimitry Andric     if (!Op.isReg() || !Op.isDef())
1520b57cec5SDimitry Andric       continue;
1530b57cec5SDimitry Andric     if (HadDef)
1540b57cec5SDimitry Andric       return false;
1550b57cec5SDimitry Andric     HadDef = true;
1560b57cec5SDimitry Andric     if (!Hexagon::IntRegsRegClass.contains(Op.getReg()))
1570b57cec5SDimitry Andric       return false;
1580b57cec5SDimitry Andric   }
1590b57cec5SDimitry Andric   assert(HadDef);
1600b57cec5SDimitry Andric 
1610b57cec5SDimitry Andric   // Make sure there is no 'def' or 'use' of any of the uses of
1620b57cec5SDimitry Andric   // feeder insn between its definition, this MI and jump, jmpInst
1630b57cec5SDimitry Andric   // skipping compare, cmpInst.
1640b57cec5SDimitry Andric   // Here's the example.
1650b57cec5SDimitry Andric   //    r21=memub(r22+r24<<#0)
1660b57cec5SDimitry Andric   //    p0 = cmp.eq(r21, #0)
1670b57cec5SDimitry Andric   //    r4=memub(r3+r21<<#0)
1680b57cec5SDimitry Andric   //    if (p0.new) jump:t .LBB29_45
1690b57cec5SDimitry Andric   // Without this check, it will be converted into
1700b57cec5SDimitry Andric   //    r4=memub(r3+r21<<#0)
1710b57cec5SDimitry Andric   //    r21=memub(r22+r24<<#0)
1720b57cec5SDimitry Andric   //    p0 = cmp.eq(r21, #0)
1730b57cec5SDimitry Andric   //    if (p0.new) jump:t .LBB29_45
1740b57cec5SDimitry Andric   // and result WAR hazards if converted to New Value Jump.
1750b57cec5SDimitry Andric   for (unsigned i = 0; i < II->getNumOperands(); ++i) {
1760b57cec5SDimitry Andric     if (II->getOperand(i).isReg() &&
1770b57cec5SDimitry Andric         (II->getOperand(i).isUse() || II->getOperand(i).isDef())) {
1780b57cec5SDimitry Andric       MachineBasicBlock::iterator localII = II;
1790b57cec5SDimitry Andric       ++localII;
1808bcb0991SDimitry Andric       Register Reg = II->getOperand(i).getReg();
1810b57cec5SDimitry Andric       for (MachineBasicBlock::iterator localBegin = localII; localBegin != end;
1820b57cec5SDimitry Andric            ++localBegin) {
1830b57cec5SDimitry Andric         if (localBegin == skip)
1840b57cec5SDimitry Andric           continue;
1850b57cec5SDimitry Andric         // Check for Subregisters too.
1860b57cec5SDimitry Andric         if (localBegin->modifiesRegister(Reg, TRI) ||
1870b57cec5SDimitry Andric             localBegin->readsRegister(Reg, TRI))
1880b57cec5SDimitry Andric           return false;
1890b57cec5SDimitry Andric       }
1900b57cec5SDimitry Andric     }
1910b57cec5SDimitry Andric   }
1920b57cec5SDimitry Andric   return true;
1930b57cec5SDimitry Andric }
1940b57cec5SDimitry Andric 
1950b57cec5SDimitry Andric // These are the common checks that need to performed
1960b57cec5SDimitry Andric // to determine if
1970b57cec5SDimitry Andric // 1. compare instruction can be moved before jump.
1980b57cec5SDimitry Andric // 2. feeder to the compare instruction can be moved before jump.
commonChecksToProhibitNewValueJump(bool afterRA,MachineBasicBlock::iterator MII)1990b57cec5SDimitry Andric static bool commonChecksToProhibitNewValueJump(bool afterRA,
2000b57cec5SDimitry Andric                           MachineBasicBlock::iterator MII) {
2010b57cec5SDimitry Andric   // If store in path, bail out.
2020b57cec5SDimitry Andric   if (MII->mayStore())
2030b57cec5SDimitry Andric     return false;
2040b57cec5SDimitry Andric 
2050b57cec5SDimitry Andric   // if call in path, bail out.
2060b57cec5SDimitry Andric   if (MII->isCall())
2070b57cec5SDimitry Andric     return false;
2080b57cec5SDimitry Andric 
2090b57cec5SDimitry Andric   // if NVJ is running prior to RA, do the following checks.
2100b57cec5SDimitry Andric   if (!afterRA) {
2110b57cec5SDimitry Andric     // The following Target Opcode instructions are spurious
2120b57cec5SDimitry Andric     // to new value jump. If they are in the path, bail out.
2130b57cec5SDimitry Andric     // KILL sets kill flag on the opcode. It also sets up a
2140b57cec5SDimitry Andric     // single register, out of pair.
2150b57cec5SDimitry Andric     //    %d0 = S2_lsr_r_p killed %d0, killed %r2
2160b57cec5SDimitry Andric     //    %r0 = KILL %r0, implicit killed %d0
2170b57cec5SDimitry Andric     //    %p0 = C2_cmpeqi killed %r0, 0
2180b57cec5SDimitry Andric     // PHI can be anything after RA.
2190b57cec5SDimitry Andric     // COPY can remateriaze things in between feeder, compare and nvj.
2200b57cec5SDimitry Andric     if (MII->getOpcode() == TargetOpcode::KILL ||
2210b57cec5SDimitry Andric         MII->getOpcode() == TargetOpcode::PHI ||
2220b57cec5SDimitry Andric         MII->getOpcode() == TargetOpcode::COPY)
2230b57cec5SDimitry Andric       return false;
2240b57cec5SDimitry Andric 
2250b57cec5SDimitry Andric     // The following pseudo Hexagon instructions sets "use" and "def"
2260b57cec5SDimitry Andric     // of registers by individual passes in the backend. At this time,
2270b57cec5SDimitry Andric     // we don't know the scope of usage and definitions of these
2280b57cec5SDimitry Andric     // instructions.
2290b57cec5SDimitry Andric     if (MII->getOpcode() == Hexagon::LDriw_pred ||
2300b57cec5SDimitry Andric         MII->getOpcode() == Hexagon::STriw_pred)
2310b57cec5SDimitry Andric       return false;
2320b57cec5SDimitry Andric   }
2330b57cec5SDimitry Andric 
2340b57cec5SDimitry Andric   return true;
2350b57cec5SDimitry Andric }
2360b57cec5SDimitry Andric 
canCompareBeNewValueJump(const HexagonInstrInfo * QII,const TargetRegisterInfo * TRI,MachineBasicBlock::iterator II,unsigned pReg,bool secondReg,bool optLocation,MachineBasicBlock::iterator end,MachineFunction & MF)2370b57cec5SDimitry Andric static bool canCompareBeNewValueJump(const HexagonInstrInfo *QII,
2380b57cec5SDimitry Andric                                      const TargetRegisterInfo *TRI,
2390b57cec5SDimitry Andric                                      MachineBasicBlock::iterator II,
2400b57cec5SDimitry Andric                                      unsigned pReg,
2410b57cec5SDimitry Andric                                      bool secondReg,
2420b57cec5SDimitry Andric                                      bool optLocation,
2430b57cec5SDimitry Andric                                      MachineBasicBlock::iterator end,
2440b57cec5SDimitry Andric                                      MachineFunction &MF) {
2450b57cec5SDimitry Andric   MachineInstr &MI = *II;
2460b57cec5SDimitry Andric 
2470b57cec5SDimitry Andric   // If the second operand of the compare is an imm, make sure it's in the
2480b57cec5SDimitry Andric   // range specified by the arch.
2490b57cec5SDimitry Andric   if (!secondReg) {
2500b57cec5SDimitry Andric     const MachineOperand &Op2 = MI.getOperand(2);
2510b57cec5SDimitry Andric     if (!Op2.isImm())
2520b57cec5SDimitry Andric       return false;
2530b57cec5SDimitry Andric 
2540b57cec5SDimitry Andric     int64_t v = Op2.getImm();
2550b57cec5SDimitry Andric     bool Valid = false;
2560b57cec5SDimitry Andric 
2570b57cec5SDimitry Andric     switch (MI.getOpcode()) {
2580b57cec5SDimitry Andric       case Hexagon::C2_cmpeqi:
2590b57cec5SDimitry Andric       case Hexagon::C4_cmpneqi:
2600b57cec5SDimitry Andric       case Hexagon::C2_cmpgti:
2610b57cec5SDimitry Andric       case Hexagon::C4_cmpltei:
2620b57cec5SDimitry Andric         Valid = (isUInt<5>(v) || v == -1);
2630b57cec5SDimitry Andric         break;
2640b57cec5SDimitry Andric       case Hexagon::C2_cmpgtui:
2650b57cec5SDimitry Andric       case Hexagon::C4_cmplteui:
2660b57cec5SDimitry Andric         Valid = isUInt<5>(v);
2670b57cec5SDimitry Andric         break;
2680b57cec5SDimitry Andric       case Hexagon::S2_tstbit_i:
2690b57cec5SDimitry Andric       case Hexagon::S4_ntstbit_i:
2700b57cec5SDimitry Andric         Valid = (v == 0);
2710b57cec5SDimitry Andric         break;
2720b57cec5SDimitry Andric     }
2730b57cec5SDimitry Andric 
2740b57cec5SDimitry Andric     if (!Valid)
2750b57cec5SDimitry Andric       return false;
2760b57cec5SDimitry Andric   }
2770b57cec5SDimitry Andric 
2780b57cec5SDimitry Andric   unsigned cmpReg1, cmpOp2 = 0; // cmpOp2 assignment silences compiler warning.
2790b57cec5SDimitry Andric   cmpReg1 = MI.getOperand(1).getReg();
2800b57cec5SDimitry Andric 
2810b57cec5SDimitry Andric   if (secondReg) {
2820b57cec5SDimitry Andric     cmpOp2 = MI.getOperand(2).getReg();
2830b57cec5SDimitry Andric 
2840b57cec5SDimitry Andric     // If the same register appears as both operands, we cannot generate a new
2850b57cec5SDimitry Andric     // value compare. Only one operand may use the .new suffix.
2860b57cec5SDimitry Andric     if (cmpReg1 == cmpOp2)
2870b57cec5SDimitry Andric       return false;
2880b57cec5SDimitry Andric 
2890b57cec5SDimitry Andric     // Make sure that the second register is not from COPY
2900b57cec5SDimitry Andric     // at machine code level, we don't need this, but if we decide
2910b57cec5SDimitry Andric     // to move new value jump prior to RA, we would be needing this.
2920b57cec5SDimitry Andric     MachineRegisterInfo &MRI = MF.getRegInfo();
2935ffd83dbSDimitry Andric     if (!Register::isPhysicalRegister(cmpOp2)) {
2940b57cec5SDimitry Andric       MachineInstr *def = MRI.getVRegDef(cmpOp2);
2950b57cec5SDimitry Andric       if (def->getOpcode() == TargetOpcode::COPY)
2960b57cec5SDimitry Andric         return false;
2970b57cec5SDimitry Andric     }
2980b57cec5SDimitry Andric   }
2990b57cec5SDimitry Andric 
3000b57cec5SDimitry Andric   // Walk the instructions after the compare (predicate def) to the jump,
3010b57cec5SDimitry Andric   // and satisfy the following conditions.
3020b57cec5SDimitry Andric   ++II;
3030b57cec5SDimitry Andric   for (MachineBasicBlock::iterator localII = II; localII != end; ++localII) {
3040b57cec5SDimitry Andric     if (localII->isDebugInstr())
3050b57cec5SDimitry Andric       continue;
3060b57cec5SDimitry Andric 
3070b57cec5SDimitry Andric     // Check 1.
3080b57cec5SDimitry Andric     // If "common" checks fail, bail out.
3090b57cec5SDimitry Andric     if (!commonChecksToProhibitNewValueJump(optLocation, localII))
3100b57cec5SDimitry Andric       return false;
3110b57cec5SDimitry Andric 
3120b57cec5SDimitry Andric     // Check 2.
3130b57cec5SDimitry Andric     // If there is a def or use of predicate (result of compare), bail out.
3140b57cec5SDimitry Andric     if (localII->modifiesRegister(pReg, TRI) ||
3150b57cec5SDimitry Andric         localII->readsRegister(pReg, TRI))
3160b57cec5SDimitry Andric       return false;
3170b57cec5SDimitry Andric 
3180b57cec5SDimitry Andric     // Check 3.
3190b57cec5SDimitry Andric     // If there is a def of any of the use of the compare (operands of compare),
3200b57cec5SDimitry Andric     // bail out.
3210b57cec5SDimitry Andric     // Eg.
3220b57cec5SDimitry Andric     //    p0 = cmp.eq(r2, r0)
3230b57cec5SDimitry Andric     //    r2 = r4
3240b57cec5SDimitry Andric     //    if (p0.new) jump:t .LBB28_3
3250b57cec5SDimitry Andric     if (localII->modifiesRegister(cmpReg1, TRI) ||
3260b57cec5SDimitry Andric         (secondReg && localII->modifiesRegister(cmpOp2, TRI)))
3270b57cec5SDimitry Andric       return false;
3280b57cec5SDimitry Andric   }
3290b57cec5SDimitry Andric   return true;
3300b57cec5SDimitry Andric }
3310b57cec5SDimitry Andric 
3320b57cec5SDimitry Andric // Given a compare operator, return a matching New Value Jump compare operator.
3330b57cec5SDimitry Andric // Make sure that MI here is included in isNewValueJumpCandidate.
getNewValueJumpOpcode(MachineInstr * MI,int reg,bool secondRegNewified,MachineBasicBlock * jmpTarget,const MachineBranchProbabilityInfo * MBPI)3340b57cec5SDimitry Andric static unsigned getNewValueJumpOpcode(MachineInstr *MI, int reg,
3350b57cec5SDimitry Andric                                       bool secondRegNewified,
3360b57cec5SDimitry Andric                                       MachineBasicBlock *jmpTarget,
3370b57cec5SDimitry Andric                                       const MachineBranchProbabilityInfo
3380b57cec5SDimitry Andric                                       *MBPI) {
3390b57cec5SDimitry Andric   bool taken = false;
3400b57cec5SDimitry Andric   MachineBasicBlock *Src = MI->getParent();
3410b57cec5SDimitry Andric   const BranchProbability Prediction =
3420b57cec5SDimitry Andric     MBPI->getEdgeProbability(Src, jmpTarget);
3430b57cec5SDimitry Andric 
3440b57cec5SDimitry Andric   if (Prediction >= BranchProbability(1,2))
3450b57cec5SDimitry Andric     taken = true;
3460b57cec5SDimitry Andric 
3470b57cec5SDimitry Andric   switch (MI->getOpcode()) {
3480b57cec5SDimitry Andric     case Hexagon::C2_cmpeq:
3490b57cec5SDimitry Andric       return taken ? Hexagon::J4_cmpeq_t_jumpnv_t
3500b57cec5SDimitry Andric                    : Hexagon::J4_cmpeq_t_jumpnv_nt;
3510b57cec5SDimitry Andric 
3520b57cec5SDimitry Andric     case Hexagon::C2_cmpeqi:
3530b57cec5SDimitry Andric       if (reg >= 0)
3540b57cec5SDimitry Andric         return taken ? Hexagon::J4_cmpeqi_t_jumpnv_t
3550b57cec5SDimitry Andric                      : Hexagon::J4_cmpeqi_t_jumpnv_nt;
3560b57cec5SDimitry Andric       return taken ? Hexagon::J4_cmpeqn1_t_jumpnv_t
3570b57cec5SDimitry Andric                    : Hexagon::J4_cmpeqn1_t_jumpnv_nt;
3580b57cec5SDimitry Andric 
3590b57cec5SDimitry Andric     case Hexagon::C4_cmpneqi:
3600b57cec5SDimitry Andric       if (reg >= 0)
3610b57cec5SDimitry Andric         return taken ? Hexagon::J4_cmpeqi_f_jumpnv_t
3620b57cec5SDimitry Andric                      : Hexagon::J4_cmpeqi_f_jumpnv_nt;
3630b57cec5SDimitry Andric       return taken ? Hexagon::J4_cmpeqn1_f_jumpnv_t :
3640b57cec5SDimitry Andric                      Hexagon::J4_cmpeqn1_f_jumpnv_nt;
3650b57cec5SDimitry Andric 
3660b57cec5SDimitry Andric     case Hexagon::C2_cmpgt:
3670b57cec5SDimitry Andric       if (secondRegNewified)
3680b57cec5SDimitry Andric         return taken ? Hexagon::J4_cmplt_t_jumpnv_t
3690b57cec5SDimitry Andric                      : Hexagon::J4_cmplt_t_jumpnv_nt;
3700b57cec5SDimitry Andric       return taken ? Hexagon::J4_cmpgt_t_jumpnv_t
3710b57cec5SDimitry Andric                    : Hexagon::J4_cmpgt_t_jumpnv_nt;
3720b57cec5SDimitry Andric 
3730b57cec5SDimitry Andric     case Hexagon::C2_cmpgti:
3740b57cec5SDimitry Andric       if (reg >= 0)
3750b57cec5SDimitry Andric         return taken ? Hexagon::J4_cmpgti_t_jumpnv_t
3760b57cec5SDimitry Andric                      : Hexagon::J4_cmpgti_t_jumpnv_nt;
3770b57cec5SDimitry Andric       return taken ? Hexagon::J4_cmpgtn1_t_jumpnv_t
3780b57cec5SDimitry Andric                    : Hexagon::J4_cmpgtn1_t_jumpnv_nt;
3790b57cec5SDimitry Andric 
3800b57cec5SDimitry Andric     case Hexagon::C2_cmpgtu:
3810b57cec5SDimitry Andric       if (secondRegNewified)
3820b57cec5SDimitry Andric         return taken ? Hexagon::J4_cmpltu_t_jumpnv_t
3830b57cec5SDimitry Andric                      : Hexagon::J4_cmpltu_t_jumpnv_nt;
3840b57cec5SDimitry Andric       return taken ? Hexagon::J4_cmpgtu_t_jumpnv_t
3850b57cec5SDimitry Andric                    : Hexagon::J4_cmpgtu_t_jumpnv_nt;
3860b57cec5SDimitry Andric 
3870b57cec5SDimitry Andric     case Hexagon::C2_cmpgtui:
3880b57cec5SDimitry Andric       return taken ? Hexagon::J4_cmpgtui_t_jumpnv_t
3890b57cec5SDimitry Andric                    : Hexagon::J4_cmpgtui_t_jumpnv_nt;
3900b57cec5SDimitry Andric 
3910b57cec5SDimitry Andric     case Hexagon::C4_cmpneq:
3920b57cec5SDimitry Andric       return taken ? Hexagon::J4_cmpeq_f_jumpnv_t
3930b57cec5SDimitry Andric                    : Hexagon::J4_cmpeq_f_jumpnv_nt;
3940b57cec5SDimitry Andric 
3950b57cec5SDimitry Andric     case Hexagon::C4_cmplte:
3960b57cec5SDimitry Andric       if (secondRegNewified)
3970b57cec5SDimitry Andric         return taken ? Hexagon::J4_cmplt_f_jumpnv_t
3980b57cec5SDimitry Andric                      : Hexagon::J4_cmplt_f_jumpnv_nt;
3990b57cec5SDimitry Andric       return taken ? Hexagon::J4_cmpgt_f_jumpnv_t
4000b57cec5SDimitry Andric                    : Hexagon::J4_cmpgt_f_jumpnv_nt;
4010b57cec5SDimitry Andric 
4020b57cec5SDimitry Andric     case Hexagon::C4_cmplteu:
4030b57cec5SDimitry Andric       if (secondRegNewified)
4040b57cec5SDimitry Andric         return taken ? Hexagon::J4_cmpltu_f_jumpnv_t
4050b57cec5SDimitry Andric                      : Hexagon::J4_cmpltu_f_jumpnv_nt;
4060b57cec5SDimitry Andric       return taken ? Hexagon::J4_cmpgtu_f_jumpnv_t
4070b57cec5SDimitry Andric                    : Hexagon::J4_cmpgtu_f_jumpnv_nt;
4080b57cec5SDimitry Andric 
4090b57cec5SDimitry Andric     case Hexagon::C4_cmpltei:
4100b57cec5SDimitry Andric       if (reg >= 0)
4110b57cec5SDimitry Andric         return taken ? Hexagon::J4_cmpgti_f_jumpnv_t
4120b57cec5SDimitry Andric                      : Hexagon::J4_cmpgti_f_jumpnv_nt;
4130b57cec5SDimitry Andric       return taken ? Hexagon::J4_cmpgtn1_f_jumpnv_t
4140b57cec5SDimitry Andric                    : Hexagon::J4_cmpgtn1_f_jumpnv_nt;
4150b57cec5SDimitry Andric 
4160b57cec5SDimitry Andric     case Hexagon::C4_cmplteui:
4170b57cec5SDimitry Andric       return taken ? Hexagon::J4_cmpgtui_f_jumpnv_t
4180b57cec5SDimitry Andric                    : Hexagon::J4_cmpgtui_f_jumpnv_nt;
4190b57cec5SDimitry Andric 
4200b57cec5SDimitry Andric     default:
4210b57cec5SDimitry Andric        llvm_unreachable("Could not find matching New Value Jump instruction.");
4220b57cec5SDimitry Andric   }
4230b57cec5SDimitry Andric   // return *some value* to avoid compiler warning
4240b57cec5SDimitry Andric   return 0;
4250b57cec5SDimitry Andric }
4260b57cec5SDimitry Andric 
isNewValueJumpCandidate(const MachineInstr & MI) const4270b57cec5SDimitry Andric bool HexagonNewValueJump::isNewValueJumpCandidate(
4280b57cec5SDimitry Andric     const MachineInstr &MI) const {
4290b57cec5SDimitry Andric   switch (MI.getOpcode()) {
4300b57cec5SDimitry Andric   case Hexagon::C2_cmpeq:
4310b57cec5SDimitry Andric   case Hexagon::C2_cmpeqi:
4320b57cec5SDimitry Andric   case Hexagon::C2_cmpgt:
4330b57cec5SDimitry Andric   case Hexagon::C2_cmpgti:
4340b57cec5SDimitry Andric   case Hexagon::C2_cmpgtu:
4350b57cec5SDimitry Andric   case Hexagon::C2_cmpgtui:
4360b57cec5SDimitry Andric   case Hexagon::C4_cmpneq:
4370b57cec5SDimitry Andric   case Hexagon::C4_cmpneqi:
4380b57cec5SDimitry Andric   case Hexagon::C4_cmplte:
4390b57cec5SDimitry Andric   case Hexagon::C4_cmplteu:
4400b57cec5SDimitry Andric   case Hexagon::C4_cmpltei:
4410b57cec5SDimitry Andric   case Hexagon::C4_cmplteui:
4420b57cec5SDimitry Andric     return true;
4430b57cec5SDimitry Andric 
4440b57cec5SDimitry Andric   default:
4450b57cec5SDimitry Andric     return false;
4460b57cec5SDimitry Andric   }
4470b57cec5SDimitry Andric }
4480b57cec5SDimitry Andric 
runOnMachineFunction(MachineFunction & MF)4490b57cec5SDimitry Andric bool HexagonNewValueJump::runOnMachineFunction(MachineFunction &MF) {
4500b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "********** Hexagon New Value Jump **********\n"
4510b57cec5SDimitry Andric                     << "********** Function: " << MF.getName() << "\n");
4520b57cec5SDimitry Andric 
4530b57cec5SDimitry Andric   if (skipFunction(MF.getFunction()))
4540b57cec5SDimitry Andric     return false;
4550b57cec5SDimitry Andric 
4560b57cec5SDimitry Andric   // If we move NewValueJump before register allocation we'll need live variable
4570b57cec5SDimitry Andric   // analysis here too.
4580b57cec5SDimitry Andric 
4590b57cec5SDimitry Andric   QII = static_cast<const HexagonInstrInfo *>(MF.getSubtarget().getInstrInfo());
4600b57cec5SDimitry Andric   QRI = static_cast<const HexagonRegisterInfo *>(
4610b57cec5SDimitry Andric       MF.getSubtarget().getRegisterInfo());
462*0fca6ea1SDimitry Andric   MBPI = &getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI();
4630b57cec5SDimitry Andric 
4640b57cec5SDimitry Andric   if (DisableNewValueJumps ||
4650b57cec5SDimitry Andric       !MF.getSubtarget<HexagonSubtarget>().useNewValueJumps())
4660b57cec5SDimitry Andric     return false;
4670b57cec5SDimitry Andric 
4680b57cec5SDimitry Andric   int nvjCount = DbgNVJCount;
4690b57cec5SDimitry Andric   int nvjGenerated = 0;
4700b57cec5SDimitry Andric 
4710b57cec5SDimitry Andric   // Loop through all the bb's of the function
4720b57cec5SDimitry Andric   for (MachineFunction::iterator MBBb = MF.begin(), MBBe = MF.end();
4730b57cec5SDimitry Andric        MBBb != MBBe; ++MBBb) {
4740b57cec5SDimitry Andric     MachineBasicBlock *MBB = &*MBBb;
4750b57cec5SDimitry Andric 
4760b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "** dumping bb ** " << MBB->getNumber() << "\n");
4770b57cec5SDimitry Andric     LLVM_DEBUG(MBB->dump());
4780b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "\n"
4790b57cec5SDimitry Andric                       << "********** dumping instr bottom up **********\n");
4800b57cec5SDimitry Andric     bool foundJump    = false;
4810b57cec5SDimitry Andric     bool foundCompare = false;
4820b57cec5SDimitry Andric     bool invertPredicate = false;
4830b57cec5SDimitry Andric     unsigned predReg = 0; // predicate reg of the jump.
4840b57cec5SDimitry Andric     unsigned cmpReg1 = 0;
4850b57cec5SDimitry Andric     int cmpOp2 = 0;
4860b57cec5SDimitry Andric     MachineBasicBlock::iterator jmpPos;
4870b57cec5SDimitry Andric     MachineBasicBlock::iterator cmpPos;
4880b57cec5SDimitry Andric     MachineInstr *cmpInstr = nullptr, *jmpInstr = nullptr;
4890b57cec5SDimitry Andric     MachineBasicBlock *jmpTarget = nullptr;
4900b57cec5SDimitry Andric     bool afterRA = false;
4910b57cec5SDimitry Andric     bool isSecondOpReg = false;
4920b57cec5SDimitry Andric     bool isSecondOpNewified = false;
4930b57cec5SDimitry Andric     // Traverse the basic block - bottom up
4940b57cec5SDimitry Andric     for (MachineBasicBlock::iterator MII = MBB->end(), E = MBB->begin();
4950b57cec5SDimitry Andric          MII != E;) {
4960b57cec5SDimitry Andric       MachineInstr &MI = *--MII;
4970b57cec5SDimitry Andric       if (MI.isDebugInstr()) {
4980b57cec5SDimitry Andric         continue;
4990b57cec5SDimitry Andric       }
5000b57cec5SDimitry Andric 
5010b57cec5SDimitry Andric       if ((nvjCount == 0) || (nvjCount > -1 && nvjCount <= nvjGenerated))
5020b57cec5SDimitry Andric         break;
5030b57cec5SDimitry Andric 
5040b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Instr: "; MI.dump(); dbgs() << "\n");
5050b57cec5SDimitry Andric 
5060b57cec5SDimitry Andric       if (!foundJump && (MI.getOpcode() == Hexagon::J2_jumpt ||
5070b57cec5SDimitry Andric                          MI.getOpcode() == Hexagon::J2_jumptpt ||
5080b57cec5SDimitry Andric                          MI.getOpcode() == Hexagon::J2_jumpf ||
5090b57cec5SDimitry Andric                          MI.getOpcode() == Hexagon::J2_jumpfpt ||
5100b57cec5SDimitry Andric                          MI.getOpcode() == Hexagon::J2_jumptnewpt ||
5110b57cec5SDimitry Andric                          MI.getOpcode() == Hexagon::J2_jumptnew ||
5120b57cec5SDimitry Andric                          MI.getOpcode() == Hexagon::J2_jumpfnewpt ||
5130b57cec5SDimitry Andric                          MI.getOpcode() == Hexagon::J2_jumpfnew)) {
5140b57cec5SDimitry Andric         // This is where you would insert your compare and
5150b57cec5SDimitry Andric         // instr that feeds compare
5160b57cec5SDimitry Andric         jmpPos = MII;
5170b57cec5SDimitry Andric         jmpInstr = &MI;
5180b57cec5SDimitry Andric         predReg = MI.getOperand(0).getReg();
5198bcb0991SDimitry Andric         afterRA = Register::isPhysicalRegister(predReg);
5200b57cec5SDimitry Andric 
5210b57cec5SDimitry Andric         // If ifconverter had not messed up with the kill flags of the
5220b57cec5SDimitry Andric         // operands, the following check on the kill flag would suffice.
5230b57cec5SDimitry Andric         // if(!jmpInstr->getOperand(0).isKill()) break;
5240b57cec5SDimitry Andric 
5250b57cec5SDimitry Andric         // This predicate register is live out of BB
5260b57cec5SDimitry Andric         // this would only work if we can actually use Live
5270b57cec5SDimitry Andric         // variable analysis on phy regs - but LLVM does not
5280b57cec5SDimitry Andric         // provide LV analysis on phys regs.
5290b57cec5SDimitry Andric         //if(LVs.isLiveOut(predReg, *MBB)) break;
5300b57cec5SDimitry Andric 
5310b57cec5SDimitry Andric         // Get all the successors of this block - which will always
5320b57cec5SDimitry Andric         // be 2. Check if the predicate register is live-in in those
5330b57cec5SDimitry Andric         // successor. If yes, we can not delete the predicate -
5340b57cec5SDimitry Andric         // I am doing this only because LLVM does not provide LiveOut
5350b57cec5SDimitry Andric         // at the BB level.
5360b57cec5SDimitry Andric         bool predLive = false;
537349cc55cSDimitry Andric         for (const MachineBasicBlock *SuccMBB : MBB->successors())
538349cc55cSDimitry Andric           if (SuccMBB->isLiveIn(predReg))
5390b57cec5SDimitry Andric             predLive = true;
5400b57cec5SDimitry Andric         if (predLive)
5410b57cec5SDimitry Andric           break;
5420b57cec5SDimitry Andric 
5430b57cec5SDimitry Andric         if (!MI.getOperand(1).isMBB())
5440b57cec5SDimitry Andric           continue;
5450b57cec5SDimitry Andric         jmpTarget = MI.getOperand(1).getMBB();
5460b57cec5SDimitry Andric         foundJump = true;
5470b57cec5SDimitry Andric         if (MI.getOpcode() == Hexagon::J2_jumpf ||
5480b57cec5SDimitry Andric             MI.getOpcode() == Hexagon::J2_jumpfnewpt ||
5490b57cec5SDimitry Andric             MI.getOpcode() == Hexagon::J2_jumpfnew) {
5500b57cec5SDimitry Andric           invertPredicate = true;
5510b57cec5SDimitry Andric         }
5520b57cec5SDimitry Andric         continue;
5530b57cec5SDimitry Andric       }
5540b57cec5SDimitry Andric 
5550b57cec5SDimitry Andric       // No new value jump if there is a barrier. A barrier has to be in its
5560b57cec5SDimitry Andric       // own packet. A barrier has zero operands. We conservatively bail out
5570b57cec5SDimitry Andric       // here if we see any instruction with zero operands.
5580b57cec5SDimitry Andric       if (foundJump && MI.getNumOperands() == 0)
5590b57cec5SDimitry Andric         break;
5600b57cec5SDimitry Andric 
5610b57cec5SDimitry Andric       if (foundJump && !foundCompare && MI.getOperand(0).isReg() &&
5620b57cec5SDimitry Andric           MI.getOperand(0).getReg() == predReg) {
5630b57cec5SDimitry Andric         // Not all compares can be new value compare. Arch Spec: 7.6.1.1
5640b57cec5SDimitry Andric         if (isNewValueJumpCandidate(MI)) {
5650b57cec5SDimitry Andric           assert(
5660b57cec5SDimitry Andric               (MI.getDesc().isCompare()) &&
5670b57cec5SDimitry Andric               "Only compare instruction can be collapsed into New Value Jump");
5680b57cec5SDimitry Andric           isSecondOpReg = MI.getOperand(2).isReg();
5690b57cec5SDimitry Andric 
5700b57cec5SDimitry Andric           if (!canCompareBeNewValueJump(QII, QRI, MII, predReg, isSecondOpReg,
5710b57cec5SDimitry Andric                                         afterRA, jmpPos, MF))
5720b57cec5SDimitry Andric             break;
5730b57cec5SDimitry Andric 
5740b57cec5SDimitry Andric           cmpInstr = &MI;
5750b57cec5SDimitry Andric           cmpPos = MII;
5760b57cec5SDimitry Andric           foundCompare = true;
5770b57cec5SDimitry Andric 
5780b57cec5SDimitry Andric           // We need cmpReg1 and cmpOp2(imm or reg) while building
5790b57cec5SDimitry Andric           // new value jump instruction.
5800b57cec5SDimitry Andric           cmpReg1 = MI.getOperand(1).getReg();
5810b57cec5SDimitry Andric 
5820b57cec5SDimitry Andric           if (isSecondOpReg)
5830b57cec5SDimitry Andric             cmpOp2 = MI.getOperand(2).getReg();
5840b57cec5SDimitry Andric           else
5850b57cec5SDimitry Andric             cmpOp2 = MI.getOperand(2).getImm();
5860b57cec5SDimitry Andric           continue;
5870b57cec5SDimitry Andric         }
5880b57cec5SDimitry Andric       }
5890b57cec5SDimitry Andric 
5900b57cec5SDimitry Andric       if (foundCompare && foundJump) {
5910b57cec5SDimitry Andric         // If "common" checks fail, bail out on this BB.
5920b57cec5SDimitry Andric         if (!commonChecksToProhibitNewValueJump(afterRA, MII))
5930b57cec5SDimitry Andric           break;
5940b57cec5SDimitry Andric 
5950b57cec5SDimitry Andric         bool foundFeeder = false;
5960b57cec5SDimitry Andric         MachineBasicBlock::iterator feederPos = MII;
5970b57cec5SDimitry Andric         if (MI.getOperand(0).isReg() && MI.getOperand(0).isDef() &&
5980b57cec5SDimitry Andric             (MI.getOperand(0).getReg() == cmpReg1 ||
5990b57cec5SDimitry Andric              (isSecondOpReg &&
6000b57cec5SDimitry Andric               MI.getOperand(0).getReg() == (unsigned)cmpOp2))) {
6010b57cec5SDimitry Andric 
6028bcb0991SDimitry Andric           Register feederReg = MI.getOperand(0).getReg();
6030b57cec5SDimitry Andric 
6040b57cec5SDimitry Andric           // First try to see if we can get the feeder from the first operand
6050b57cec5SDimitry Andric           // of the compare. If we can not, and if secondOpReg is true
6060b57cec5SDimitry Andric           // (second operand of the compare is also register), try that one.
6070b57cec5SDimitry Andric           // TODO: Try to come up with some heuristic to figure out which
6080b57cec5SDimitry Andric           // feeder would benefit.
6090b57cec5SDimitry Andric 
6100b57cec5SDimitry Andric           if (feederReg == cmpReg1) {
6110b57cec5SDimitry Andric             if (!canBeFeederToNewValueJump(QII, QRI, MII, jmpPos, cmpPos, MF)) {
6120b57cec5SDimitry Andric               if (!isSecondOpReg)
6130b57cec5SDimitry Andric                 break;
6140b57cec5SDimitry Andric               else
6150b57cec5SDimitry Andric                 continue;
6160b57cec5SDimitry Andric             } else
6170b57cec5SDimitry Andric               foundFeeder = true;
6180b57cec5SDimitry Andric           }
6190b57cec5SDimitry Andric 
6200b57cec5SDimitry Andric           if (!foundFeeder && isSecondOpReg && feederReg == (unsigned)cmpOp2)
6210b57cec5SDimitry Andric             if (!canBeFeederToNewValueJump(QII, QRI, MII, jmpPos, cmpPos, MF))
6220b57cec5SDimitry Andric               break;
6230b57cec5SDimitry Andric 
6240b57cec5SDimitry Andric           if (isSecondOpReg) {
6250b57cec5SDimitry Andric             // In case of CMPLT, or CMPLTU, or EQ with the second register
6260b57cec5SDimitry Andric             // to newify, swap the operands.
6270b57cec5SDimitry Andric             unsigned COp = cmpInstr->getOpcode();
6280b57cec5SDimitry Andric             if ((COp == Hexagon::C2_cmpeq || COp == Hexagon::C4_cmpneq) &&
6290b57cec5SDimitry Andric                 (feederReg == (unsigned)cmpOp2)) {
6300b57cec5SDimitry Andric               unsigned tmp = cmpReg1;
6310b57cec5SDimitry Andric               cmpReg1 = cmpOp2;
6320b57cec5SDimitry Andric               cmpOp2 = tmp;
6330b57cec5SDimitry Andric             }
6340b57cec5SDimitry Andric 
6350b57cec5SDimitry Andric             // Now we have swapped the operands, all we need to check is,
6360b57cec5SDimitry Andric             // if the second operand (after swap) is the feeder.
6370b57cec5SDimitry Andric             // And if it is, make a note.
6380b57cec5SDimitry Andric             if (feederReg == (unsigned)cmpOp2)
6390b57cec5SDimitry Andric               isSecondOpNewified = true;
6400b57cec5SDimitry Andric           }
6410b57cec5SDimitry Andric 
6420b57cec5SDimitry Andric           // Now that we are moving feeder close the jump,
6430b57cec5SDimitry Andric           // make sure we are respecting the kill values of
6440b57cec5SDimitry Andric           // the operands of the feeder.
6450b57cec5SDimitry Andric 
6460b57cec5SDimitry Andric           auto TransferKills = [jmpPos,cmpPos] (MachineInstr &MI) {
6470b57cec5SDimitry Andric             for (MachineOperand &MO : MI.operands()) {
6480b57cec5SDimitry Andric               if (!MO.isReg() || !MO.isUse())
6490b57cec5SDimitry Andric                 continue;
6508bcb0991SDimitry Andric               Register UseR = MO.getReg();
6510b57cec5SDimitry Andric               for (auto I = std::next(MI.getIterator()); I != jmpPos; ++I) {
6520b57cec5SDimitry Andric                 if (I == cmpPos)
6530b57cec5SDimitry Andric                   continue;
6540b57cec5SDimitry Andric                 for (MachineOperand &Op : I->operands()) {
6550b57cec5SDimitry Andric                   if (!Op.isReg() || !Op.isUse() || !Op.isKill())
6560b57cec5SDimitry Andric                     continue;
6570b57cec5SDimitry Andric                   if (Op.getReg() != UseR)
6580b57cec5SDimitry Andric                     continue;
6590b57cec5SDimitry Andric                   // We found that there is kill of a use register
6600b57cec5SDimitry Andric                   // Set up a kill flag on the register
6610b57cec5SDimitry Andric                   Op.setIsKill(false);
6620b57cec5SDimitry Andric                   MO.setIsKill(true);
6630b57cec5SDimitry Andric                   return;
6640b57cec5SDimitry Andric                 }
6650b57cec5SDimitry Andric               }
6660b57cec5SDimitry Andric             }
6670b57cec5SDimitry Andric           };
6680b57cec5SDimitry Andric 
6690b57cec5SDimitry Andric           TransferKills(*feederPos);
6700b57cec5SDimitry Andric           TransferKills(*cmpPos);
6710b57cec5SDimitry Andric           bool MO1IsKill = cmpPos->killsRegister(cmpReg1, QRI);
6720b57cec5SDimitry Andric           bool MO2IsKill = isSecondOpReg && cmpPos->killsRegister(cmpOp2, QRI);
6730b57cec5SDimitry Andric 
6740b57cec5SDimitry Andric           MBB->splice(jmpPos, MI.getParent(), MI);
6750b57cec5SDimitry Andric           MBB->splice(jmpPos, MI.getParent(), cmpInstr);
6760b57cec5SDimitry Andric           DebugLoc dl = MI.getDebugLoc();
6770b57cec5SDimitry Andric           MachineInstr *NewMI;
6780b57cec5SDimitry Andric 
6790b57cec5SDimitry Andric           assert((isNewValueJumpCandidate(*cmpInstr)) &&
6800b57cec5SDimitry Andric                  "This compare is not a New Value Jump candidate.");
6810b57cec5SDimitry Andric           unsigned opc = getNewValueJumpOpcode(cmpInstr, cmpOp2,
6820b57cec5SDimitry Andric                                                isSecondOpNewified,
6830b57cec5SDimitry Andric                                                jmpTarget, MBPI);
6840b57cec5SDimitry Andric           if (invertPredicate)
6850b57cec5SDimitry Andric             opc = QII->getInvertedPredicatedOpcode(opc);
6860b57cec5SDimitry Andric 
6870b57cec5SDimitry Andric           if (isSecondOpReg)
6880b57cec5SDimitry Andric             NewMI = BuildMI(*MBB, jmpPos, dl, QII->get(opc))
6890b57cec5SDimitry Andric                         .addReg(cmpReg1, getKillRegState(MO1IsKill))
6900b57cec5SDimitry Andric                         .addReg(cmpOp2, getKillRegState(MO2IsKill))
6910b57cec5SDimitry Andric                         .addMBB(jmpTarget);
6920b57cec5SDimitry Andric 
6930b57cec5SDimitry Andric           else
6940b57cec5SDimitry Andric             NewMI = BuildMI(*MBB, jmpPos, dl, QII->get(opc))
6950b57cec5SDimitry Andric                         .addReg(cmpReg1, getKillRegState(MO1IsKill))
6960b57cec5SDimitry Andric                         .addImm(cmpOp2)
6970b57cec5SDimitry Andric                         .addMBB(jmpTarget);
6980b57cec5SDimitry Andric 
6990b57cec5SDimitry Andric           assert(NewMI && "New Value Jump Instruction Not created!");
7000b57cec5SDimitry Andric           (void)NewMI;
7010b57cec5SDimitry Andric           if (cmpInstr->getOperand(0).isReg() &&
7020b57cec5SDimitry Andric               cmpInstr->getOperand(0).isKill())
7030b57cec5SDimitry Andric             cmpInstr->getOperand(0).setIsKill(false);
7040b57cec5SDimitry Andric           if (cmpInstr->getOperand(1).isReg() &&
7050b57cec5SDimitry Andric               cmpInstr->getOperand(1).isKill())
7060b57cec5SDimitry Andric             cmpInstr->getOperand(1).setIsKill(false);
7070b57cec5SDimitry Andric           cmpInstr->eraseFromParent();
7080b57cec5SDimitry Andric           jmpInstr->eraseFromParent();
7090b57cec5SDimitry Andric           ++nvjGenerated;
7100b57cec5SDimitry Andric           ++NumNVJGenerated;
7110b57cec5SDimitry Andric           break;
7120b57cec5SDimitry Andric         }
7130b57cec5SDimitry Andric       }
7140b57cec5SDimitry Andric     }
7150b57cec5SDimitry Andric   }
7160b57cec5SDimitry Andric 
7170b57cec5SDimitry Andric   return true;
7180b57cec5SDimitry Andric }
7190b57cec5SDimitry Andric 
createHexagonNewValueJump()7200b57cec5SDimitry Andric FunctionPass *llvm::createHexagonNewValueJump() {
7210b57cec5SDimitry Andric   return new HexagonNewValueJump();
7220b57cec5SDimitry Andric }
723