1 //===----- HexagonNewValueJump.cpp - Hexagon Backend New Value Jump -------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This implements NewValueJump pass in Hexagon.
11 // Ideally, we should merge this as a Peephole pass prior to register
12 // allocation, but because we have a spill in between the feeder and new value
13 // jump instructions, we are forced to write after register allocation.
14 // Having said that, we should re-attempt to pull this earlier at some point
15 // in future.
16 
17 // The basic approach looks for sequence of predicated jump, compare instruciton
18 // that genereates the predicate and, the feeder to the predicate. Once it finds
19 // all, it collapses compare and jump instruction into a new valu jump
20 // intstructions.
21 //
22 //
23 //===----------------------------------------------------------------------===//
24 #include "Hexagon.h"
25 #include "HexagonInstrInfo.h"
26 #include "HexagonMachineFunctionInfo.h"
27 #include "HexagonRegisterInfo.h"
28 #include "HexagonSubtarget.h"
29 #include "HexagonTargetMachine.h"
30 #include "llvm/ADT/Statistic.h"
31 #include "llvm/CodeGen/LiveVariables.h"
32 #include "llvm/CodeGen/MachineFunctionPass.h"
33 #include "llvm/CodeGen/MachineInstrBuilder.h"
34 #include "llvm/CodeGen/MachineRegisterInfo.h"
35 #include "llvm/CodeGen/Passes.h"
36 #include "llvm/CodeGen/ScheduleDAGInstrs.h"
37 #include "llvm/PassSupport.h"
38 #include "llvm/Support/CommandLine.h"
39 #include "llvm/Support/Debug.h"
40 #include "llvm/Support/raw_ostream.h"
41 #include "llvm/Target/TargetInstrInfo.h"
42 #include "llvm/Target/TargetMachine.h"
43 #include "llvm/Target/TargetRegisterInfo.h"
44 using namespace llvm;
45 
46 #define DEBUG_TYPE "hexagon-nvj"
47 
48 STATISTIC(NumNVJGenerated, "Number of New Value Jump Instructions created");
49 
50 static cl::opt<int> DbgNVJCount("nvj-count", cl::init(-1), cl::Hidden,
51     cl::desc("Maximum number of predicated jumps to be converted to "
52     "New Value Jump"));
53 
54 static cl::opt<bool> DisableNewValueJumps("disable-nvjump", cl::Hidden,
55     cl::ZeroOrMore, cl::init(false),
56     cl::desc("Disable New Value Jumps"));
57 
58 namespace llvm {
59   FunctionPass *createHexagonNewValueJump();
60   void initializeHexagonNewValueJumpPass(PassRegistry&);
61 }
62 
63 
64 namespace {
65   struct HexagonNewValueJump : public MachineFunctionPass {
66     static char ID;
67 
68     HexagonNewValueJump() : MachineFunctionPass(ID) {}
69 
70     void getAnalysisUsage(AnalysisUsage &AU) const override {
71       AU.addRequired<MachineBranchProbabilityInfo>();
72       MachineFunctionPass::getAnalysisUsage(AU);
73     }
74 
75     StringRef getPassName() const override { return "Hexagon NewValueJump"; }
76 
77     bool runOnMachineFunction(MachineFunction &Fn) override;
78     MachineFunctionProperties getRequiredProperties() const override {
79       return MachineFunctionProperties().set(
80           MachineFunctionProperties::Property::NoVRegs);
81     }
82 
83   private:
84     const HexagonInstrInfo *QII;
85     const HexagonRegisterInfo *QRI;
86 
87     /// \brief A handle to the branch probability pass.
88     const MachineBranchProbabilityInfo *MBPI;
89 
90     bool isNewValueJumpCandidate(const MachineInstr &MI) const;
91   };
92 
93 } // end of anonymous namespace
94 
95 char HexagonNewValueJump::ID = 0;
96 
97 INITIALIZE_PASS_BEGIN(HexagonNewValueJump, "hexagon-nvj",
98                       "Hexagon NewValueJump", false, false)
99 INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
100 INITIALIZE_PASS_END(HexagonNewValueJump, "hexagon-nvj",
101                     "Hexagon NewValueJump", false, false)
102 
103 
104 // We have identified this II could be feeder to NVJ,
105 // verify that it can be.
106 static bool canBeFeederToNewValueJump(const HexagonInstrInfo *QII,
107                                       const TargetRegisterInfo *TRI,
108                                       MachineBasicBlock::iterator II,
109                                       MachineBasicBlock::iterator end,
110                                       MachineBasicBlock::iterator skip,
111                                       MachineFunction &MF) {
112 
113   // Predicated instruction can not be feeder to NVJ.
114   if (QII->isPredicated(*II))
115     return false;
116 
117   // Bail out if feederReg is a paired register (double regs in
118   // our case). One would think that we can check to see if a given
119   // register cmpReg1 or cmpReg2 is a sub register of feederReg
120   // using -- if (QRI->isSubRegister(feederReg, cmpReg1) logic
121   // before the callsite of this function
122   // But we can not as it comes in the following fashion.
123   //    %D0<def> = Hexagon_S2_lsr_r_p %D0<kill>, %R2<kill>
124   //    %R0<def> = KILL %R0, %D0<imp-use,kill>
125   //    %P0<def> = CMPEQri %R0<kill>, 0
126   // Hence, we need to check if it's a KILL instruction.
127   if (II->getOpcode() == TargetOpcode::KILL)
128     return false;
129 
130   if (II->isImplicitDef())
131     return false;
132 
133   // Make sure there there is no 'def' or 'use' of any of the uses of
134   // feeder insn between it's definition, this MI and jump, jmpInst
135   // skipping compare, cmpInst.
136   // Here's the example.
137   //    r21=memub(r22+r24<<#0)
138   //    p0 = cmp.eq(r21, #0)
139   //    r4=memub(r3+r21<<#0)
140   //    if (p0.new) jump:t .LBB29_45
141   // Without this check, it will be converted into
142   //    r4=memub(r3+r21<<#0)
143   //    r21=memub(r22+r24<<#0)
144   //    p0 = cmp.eq(r21, #0)
145   //    if (p0.new) jump:t .LBB29_45
146   // and result WAR hazards if converted to New Value Jump.
147 
148   for (unsigned i = 0; i < II->getNumOperands(); ++i) {
149     if (II->getOperand(i).isReg() &&
150         (II->getOperand(i).isUse() || II->getOperand(i).isDef())) {
151       MachineBasicBlock::iterator localII = II;
152       ++localII;
153       unsigned Reg = II->getOperand(i).getReg();
154       for (MachineBasicBlock::iterator localBegin = localII; localBegin != end;
155            ++localBegin) {
156         if (localBegin == skip)
157           continue;
158         // Check for Subregisters too.
159         if (localBegin->modifiesRegister(Reg, TRI) ||
160             localBegin->readsRegister(Reg, TRI))
161           return false;
162       }
163     }
164   }
165   return true;
166 }
167 
168 // These are the common checks that need to performed
169 // to determine if
170 // 1. compare instruction can be moved before jump.
171 // 2. feeder to the compare instruction can be moved before jump.
172 static bool commonChecksToProhibitNewValueJump(bool afterRA,
173                           MachineBasicBlock::iterator MII) {
174 
175   // If store in path, bail out.
176   if (MII->mayStore())
177     return false;
178 
179   // if call in path, bail out.
180   if (MII->isCall())
181     return false;
182 
183   // if NVJ is running prior to RA, do the following checks.
184   if (!afterRA) {
185     // The following Target Opcode instructions are spurious
186     // to new value jump. If they are in the path, bail out.
187     // KILL sets kill flag on the opcode. It also sets up a
188     // single register, out of pair.
189     //    %D0<def> = S2_lsr_r_p %D0<kill>, %R2<kill>
190     //    %R0<def> = KILL %R0, %D0<imp-use,kill>
191     //    %P0<def> = C2_cmpeqi %R0<kill>, 0
192     // PHI can be anything after RA.
193     // COPY can remateriaze things in between feeder, compare and nvj.
194     if (MII->getOpcode() == TargetOpcode::KILL ||
195         MII->getOpcode() == TargetOpcode::PHI ||
196         MII->getOpcode() == TargetOpcode::COPY)
197       return false;
198 
199     // The following pseudo Hexagon instructions sets "use" and "def"
200     // of registers by individual passes in the backend. At this time,
201     // we don't know the scope of usage and definitions of these
202     // instructions.
203     if (MII->getOpcode() == Hexagon::LDriw_pred ||
204         MII->getOpcode() == Hexagon::STriw_pred)
205       return false;
206   }
207 
208   return true;
209 }
210 
211 static bool canCompareBeNewValueJump(const HexagonInstrInfo *QII,
212                                      const TargetRegisterInfo *TRI,
213                                      MachineBasicBlock::iterator II,
214                                      unsigned pReg,
215                                      bool secondReg,
216                                      bool optLocation,
217                                      MachineBasicBlock::iterator end,
218                                      MachineFunction &MF) {
219 
220   MachineInstr &MI = *II;
221 
222   // If the second operand of the compare is an imm, make sure it's in the
223   // range specified by the arch.
224   if (!secondReg) {
225     int64_t v = MI.getOperand(2).getImm();
226     bool Valid = false;
227 
228     switch (MI.getOpcode()) {
229       case Hexagon::C2_cmpeqi:
230       case Hexagon::C4_cmpneqi:
231       case Hexagon::C2_cmpgti:
232       case Hexagon::C4_cmpltei:
233         Valid = (isUInt<5>(v) || v == -1);
234         break;
235       case Hexagon::C2_cmpgtui:
236       case Hexagon::C4_cmplteui:
237         Valid = isUInt<5>(v);
238         break;
239       case Hexagon::S2_tstbit_i:
240       case Hexagon::S4_ntstbit_i:
241         Valid = (v == 0);
242         break;
243     }
244 
245     if (!Valid)
246       return false;
247   }
248 
249   unsigned cmpReg1, cmpOp2 = 0; // cmpOp2 assignment silences compiler warning.
250   cmpReg1 = MI.getOperand(1).getReg();
251 
252   if (secondReg) {
253     cmpOp2 = MI.getOperand(2).getReg();
254 
255     // If the same register appears as both operands, we cannot generate a new
256     // value compare. Only one operand may use the .new suffix.
257     if (cmpReg1 == cmpOp2)
258       return false;
259 
260     // Make sure that that second register is not from COPY
261     // At machine code level, we don't need this, but if we decide
262     // to move new value jump prior to RA, we would be needing this.
263     MachineRegisterInfo &MRI = MF.getRegInfo();
264     if (secondReg && !TargetRegisterInfo::isPhysicalRegister(cmpOp2)) {
265       MachineInstr *def = MRI.getVRegDef(cmpOp2);
266       if (def->getOpcode() == TargetOpcode::COPY)
267         return false;
268     }
269   }
270 
271   // Walk the instructions after the compare (predicate def) to the jump,
272   // and satisfy the following conditions.
273   ++II;
274   for (MachineBasicBlock::iterator localII = II; localII != end; ++localII) {
275     if (localII->isDebugValue())
276       continue;
277 
278     // Check 1.
279     // If "common" checks fail, bail out.
280     if (!commonChecksToProhibitNewValueJump(optLocation, localII))
281       return false;
282 
283     // Check 2.
284     // If there is a def or use of predicate (result of compare), bail out.
285     if (localII->modifiesRegister(pReg, TRI) ||
286         localII->readsRegister(pReg, TRI))
287       return false;
288 
289     // Check 3.
290     // If there is a def of any of the use of the compare (operands of compare),
291     // bail out.
292     // Eg.
293     //    p0 = cmp.eq(r2, r0)
294     //    r2 = r4
295     //    if (p0.new) jump:t .LBB28_3
296     if (localII->modifiesRegister(cmpReg1, TRI) ||
297         (secondReg && localII->modifiesRegister(cmpOp2, TRI)))
298       return false;
299   }
300   return true;
301 }
302 
303 // Given a compare operator, return a matching New Value Jump compare operator.
304 // Make sure that MI here is included in isNewValueJumpCandidate.
305 static unsigned getNewValueJumpOpcode(MachineInstr *MI, int reg,
306                                       bool secondRegNewified,
307                                       MachineBasicBlock *jmpTarget,
308                                       const MachineBranchProbabilityInfo
309                                       *MBPI) {
310   bool taken = false;
311   MachineBasicBlock *Src = MI->getParent();
312   const BranchProbability Prediction =
313     MBPI->getEdgeProbability(Src, jmpTarget);
314 
315   if (Prediction >= BranchProbability(1,2))
316     taken = true;
317 
318   switch (MI->getOpcode()) {
319     case Hexagon::C2_cmpeq:
320       return taken ? Hexagon::J4_cmpeq_t_jumpnv_t
321                    : Hexagon::J4_cmpeq_t_jumpnv_nt;
322 
323     case Hexagon::C2_cmpeqi:
324       if (reg >= 0)
325         return taken ? Hexagon::J4_cmpeqi_t_jumpnv_t
326                      : Hexagon::J4_cmpeqi_t_jumpnv_nt;
327       return taken ? Hexagon::J4_cmpeqn1_t_jumpnv_t
328                    : Hexagon::J4_cmpeqn1_t_jumpnv_nt;
329 
330     case Hexagon::C4_cmpneqi:
331       if (reg >= 0)
332         return taken ? Hexagon::J4_cmpeqi_f_jumpnv_t
333                      : Hexagon::J4_cmpeqi_f_jumpnv_nt;
334       return taken ? Hexagon::J4_cmpeqn1_f_jumpnv_t :
335                      Hexagon::J4_cmpeqn1_f_jumpnv_nt;
336 
337     case Hexagon::C2_cmpgt:
338       if (secondRegNewified)
339         return taken ? Hexagon::J4_cmplt_t_jumpnv_t
340                      : Hexagon::J4_cmplt_t_jumpnv_nt;
341       return taken ? Hexagon::J4_cmpgt_t_jumpnv_t
342                    : Hexagon::J4_cmpgt_t_jumpnv_nt;
343 
344     case Hexagon::C2_cmpgti:
345       if (reg >= 0)
346         return taken ? Hexagon::J4_cmpgti_t_jumpnv_t
347                      : Hexagon::J4_cmpgti_t_jumpnv_nt;
348       return taken ? Hexagon::J4_cmpgtn1_t_jumpnv_t
349                    : Hexagon::J4_cmpgtn1_t_jumpnv_nt;
350 
351     case Hexagon::C2_cmpgtu:
352       if (secondRegNewified)
353         return taken ? Hexagon::J4_cmpltu_t_jumpnv_t
354                      : Hexagon::J4_cmpltu_t_jumpnv_nt;
355       return taken ? Hexagon::J4_cmpgtu_t_jumpnv_t
356                    : Hexagon::J4_cmpgtu_t_jumpnv_nt;
357 
358     case Hexagon::C2_cmpgtui:
359       return taken ? Hexagon::J4_cmpgtui_t_jumpnv_t
360                    : Hexagon::J4_cmpgtui_t_jumpnv_nt;
361 
362     case Hexagon::C4_cmpneq:
363       return taken ? Hexagon::J4_cmpeq_f_jumpnv_t
364                    : Hexagon::J4_cmpeq_f_jumpnv_nt;
365 
366     case Hexagon::C4_cmplte:
367       if (secondRegNewified)
368         return taken ? Hexagon::J4_cmplt_f_jumpnv_t
369                      : Hexagon::J4_cmplt_f_jumpnv_nt;
370       return taken ? Hexagon::J4_cmpgt_f_jumpnv_t
371                    : Hexagon::J4_cmpgt_f_jumpnv_nt;
372 
373     case Hexagon::C4_cmplteu:
374       if (secondRegNewified)
375         return taken ? Hexagon::J4_cmpltu_f_jumpnv_t
376                      : Hexagon::J4_cmpltu_f_jumpnv_nt;
377       return taken ? Hexagon::J4_cmpgtu_f_jumpnv_t
378                    : Hexagon::J4_cmpgtu_f_jumpnv_nt;
379 
380     case Hexagon::C4_cmpltei:
381       if (reg >= 0)
382         return taken ? Hexagon::J4_cmpgti_f_jumpnv_t
383                      : Hexagon::J4_cmpgti_f_jumpnv_nt;
384       return taken ? Hexagon::J4_cmpgtn1_f_jumpnv_t
385                    : Hexagon::J4_cmpgtn1_f_jumpnv_nt;
386 
387     case Hexagon::C4_cmplteui:
388       return taken ? Hexagon::J4_cmpgtui_f_jumpnv_t
389                    : Hexagon::J4_cmpgtui_f_jumpnv_nt;
390 
391     default:
392        llvm_unreachable("Could not find matching New Value Jump instruction.");
393   }
394   // return *some value* to avoid compiler warning
395   return 0;
396 }
397 
398 bool HexagonNewValueJump::isNewValueJumpCandidate(
399     const MachineInstr &MI) const {
400   switch (MI.getOpcode()) {
401   case Hexagon::C2_cmpeq:
402   case Hexagon::C2_cmpeqi:
403   case Hexagon::C2_cmpgt:
404   case Hexagon::C2_cmpgti:
405   case Hexagon::C2_cmpgtu:
406   case Hexagon::C2_cmpgtui:
407   case Hexagon::C4_cmpneq:
408   case Hexagon::C4_cmpneqi:
409   case Hexagon::C4_cmplte:
410   case Hexagon::C4_cmplteu:
411   case Hexagon::C4_cmpltei:
412   case Hexagon::C4_cmplteui:
413     return true;
414 
415   default:
416     return false;
417   }
418 }
419 
420 
421 bool HexagonNewValueJump::runOnMachineFunction(MachineFunction &MF) {
422 
423   DEBUG(dbgs() << "********** Hexagon New Value Jump **********\n"
424                << "********** Function: " << MF.getName() << "\n");
425 
426   if (skipFunction(*MF.getFunction()))
427     return false;
428 
429   // If we move NewValueJump before register allocation we'll need live variable
430   // analysis here too.
431 
432   QII = static_cast<const HexagonInstrInfo *>(MF.getSubtarget().getInstrInfo());
433   QRI = static_cast<const HexagonRegisterInfo *>(
434       MF.getSubtarget().getRegisterInfo());
435   MBPI = &getAnalysis<MachineBranchProbabilityInfo>();
436 
437   if (DisableNewValueJumps) {
438     return false;
439   }
440 
441   int nvjCount = DbgNVJCount;
442   int nvjGenerated = 0;
443 
444   // Loop through all the bb's of the function
445   for (MachineFunction::iterator MBBb = MF.begin(), MBBe = MF.end();
446        MBBb != MBBe; ++MBBb) {
447     MachineBasicBlock *MBB = &*MBBb;
448 
449     DEBUG(dbgs() << "** dumping bb ** " << MBB->getNumber() << "\n");
450     DEBUG(MBB->dump());
451     DEBUG(dbgs() << "\n" << "********** dumping instr bottom up **********\n");
452     bool foundJump    = false;
453     bool foundCompare = false;
454     bool invertPredicate = false;
455     unsigned predReg = 0; // predicate reg of the jump.
456     unsigned cmpReg1 = 0;
457     int cmpOp2 = 0;
458     MachineBasicBlock::iterator jmpPos;
459     MachineBasicBlock::iterator cmpPos;
460     MachineInstr *cmpInstr = nullptr, *jmpInstr = nullptr;
461     MachineBasicBlock *jmpTarget = nullptr;
462     bool afterRA = false;
463     bool isSecondOpReg = false;
464     bool isSecondOpNewified = false;
465     // Traverse the basic block - bottom up
466     for (MachineBasicBlock::iterator MII = MBB->end(), E = MBB->begin();
467          MII != E;) {
468       MachineInstr &MI = *--MII;
469       if (MI.isDebugValue()) {
470         continue;
471       }
472 
473       if ((nvjCount == 0) || (nvjCount > -1 && nvjCount <= nvjGenerated))
474         break;
475 
476       DEBUG(dbgs() << "Instr: "; MI.dump(); dbgs() << "\n");
477 
478       if (!foundJump && (MI.getOpcode() == Hexagon::J2_jumpt ||
479                          MI.getOpcode() == Hexagon::J2_jumptpt ||
480                          MI.getOpcode() == Hexagon::J2_jumpf ||
481                          MI.getOpcode() == Hexagon::J2_jumpfpt ||
482                          MI.getOpcode() == Hexagon::J2_jumptnewpt ||
483                          MI.getOpcode() == Hexagon::J2_jumptnew ||
484                          MI.getOpcode() == Hexagon::J2_jumpfnewpt ||
485                          MI.getOpcode() == Hexagon::J2_jumpfnew)) {
486         // This is where you would insert your compare and
487         // instr that feeds compare
488         jmpPos = MII;
489         jmpInstr = &MI;
490         predReg = MI.getOperand(0).getReg();
491         afterRA = TargetRegisterInfo::isPhysicalRegister(predReg);
492 
493         // If ifconverter had not messed up with the kill flags of the
494         // operands, the following check on the kill flag would suffice.
495         // if(!jmpInstr->getOperand(0).isKill()) break;
496 
497         // This predicate register is live out out of BB
498         // this would only work if we can actually use Live
499         // variable analysis on phy regs - but LLVM does not
500         // provide LV analysis on phys regs.
501         //if(LVs.isLiveOut(predReg, *MBB)) break;
502 
503         // Get all the successors of this block - which will always
504         // be 2. Check if the predicate register is live-in in those
505         // successor. If yes, we can not delete the predicate -
506         // I am doing this only because LLVM does not provide LiveOut
507         // at the BB level.
508         bool predLive = false;
509         for (MachineBasicBlock::const_succ_iterator SI = MBB->succ_begin(),
510                                                     SIE = MBB->succ_end();
511              SI != SIE; ++SI) {
512           MachineBasicBlock *succMBB = *SI;
513           if (succMBB->isLiveIn(predReg))
514             predLive = true;
515         }
516         if (predLive)
517           break;
518 
519         if (!MI.getOperand(1).isMBB())
520           continue;
521         jmpTarget = MI.getOperand(1).getMBB();
522         foundJump = true;
523         if (MI.getOpcode() == Hexagon::J2_jumpf ||
524             MI.getOpcode() == Hexagon::J2_jumpfnewpt ||
525             MI.getOpcode() == Hexagon::J2_jumpfnew) {
526           invertPredicate = true;
527         }
528         continue;
529       }
530 
531       // No new value jump if there is a barrier. A barrier has to be in its
532       // own packet. A barrier has zero operands. We conservatively bail out
533       // here if we see any instruction with zero operands.
534       if (foundJump && MI.getNumOperands() == 0)
535         break;
536 
537       if (foundJump && !foundCompare && MI.getOperand(0).isReg() &&
538           MI.getOperand(0).getReg() == predReg) {
539 
540         // Not all compares can be new value compare. Arch Spec: 7.6.1.1
541         if (isNewValueJumpCandidate(MI)) {
542 
543           assert(
544               (MI.getDesc().isCompare()) &&
545               "Only compare instruction can be collapsed into New Value Jump");
546           isSecondOpReg = MI.getOperand(2).isReg();
547 
548           if (!canCompareBeNewValueJump(QII, QRI, MII, predReg, isSecondOpReg,
549                                         afterRA, jmpPos, MF))
550             break;
551 
552           cmpInstr = &MI;
553           cmpPos = MII;
554           foundCompare = true;
555 
556           // We need cmpReg1 and cmpOp2(imm or reg) while building
557           // new value jump instruction.
558           cmpReg1 = MI.getOperand(1).getReg();
559 
560           if (isSecondOpReg)
561             cmpOp2 = MI.getOperand(2).getReg();
562           else
563             cmpOp2 = MI.getOperand(2).getImm();
564           continue;
565         }
566       }
567 
568       if (foundCompare && foundJump) {
569 
570         // If "common" checks fail, bail out on this BB.
571         if (!commonChecksToProhibitNewValueJump(afterRA, MII))
572           break;
573 
574         bool foundFeeder = false;
575         MachineBasicBlock::iterator feederPos = MII;
576         if (MI.getOperand(0).isReg() && MI.getOperand(0).isDef() &&
577             (MI.getOperand(0).getReg() == cmpReg1 ||
578              (isSecondOpReg &&
579               MI.getOperand(0).getReg() == (unsigned)cmpOp2))) {
580 
581           unsigned feederReg = MI.getOperand(0).getReg();
582 
583           // First try to see if we can get the feeder from the first operand
584           // of the compare. If we can not, and if secondOpReg is true
585           // (second operand of the compare is also register), try that one.
586           // TODO: Try to come up with some heuristic to figure out which
587           // feeder would benefit.
588 
589           if (feederReg == cmpReg1) {
590             if (!canBeFeederToNewValueJump(QII, QRI, MII, jmpPos, cmpPos, MF)) {
591               if (!isSecondOpReg)
592                 break;
593               else
594                 continue;
595             } else
596               foundFeeder = true;
597           }
598 
599           if (!foundFeeder && isSecondOpReg && feederReg == (unsigned)cmpOp2)
600             if (!canBeFeederToNewValueJump(QII, QRI, MII, jmpPos, cmpPos, MF))
601               break;
602 
603           if (isSecondOpReg) {
604             // In case of CMPLT, or CMPLTU, or EQ with the second register
605             // to newify, swap the operands.
606             unsigned COp = cmpInstr->getOpcode();
607             if ((COp == Hexagon::C2_cmpeq || COp == Hexagon::C4_cmpneq) &&
608                 (feederReg == (unsigned)cmpOp2)) {
609               unsigned tmp = cmpReg1;
610               cmpReg1 = cmpOp2;
611               cmpOp2 = tmp;
612             }
613 
614             // Now we have swapped the operands, all we need to check is,
615             // if the second operand (after swap) is the feeder.
616             // And if it is, make a note.
617             if (feederReg == (unsigned)cmpOp2)
618               isSecondOpNewified = true;
619           }
620 
621           // Now that we are moving feeder close the jump,
622           // make sure we are respecting the kill values of
623           // the operands of the feeder.
624 
625           auto TransferKills = [jmpPos,cmpPos] (MachineInstr &MI) {
626             for (MachineOperand &MO : MI.operands()) {
627               if (!MO.isReg() || !MO.isUse())
628                 continue;
629               unsigned UseR = MO.getReg();
630               for (auto I = std::next(MI.getIterator()); I != jmpPos; ++I) {
631                 if (I == cmpPos)
632                   continue;
633                 for (MachineOperand &Op : I->operands()) {
634                   if (!Op.isReg() || !Op.isUse() || !Op.isKill())
635                     continue;
636                   if (Op.getReg() != UseR)
637                     continue;
638                   // We found that there is kill of a use register
639                   // Set up a kill flag on the register
640                   Op.setIsKill(false);
641                   MO.setIsKill(true);
642                   return;
643                 }
644               }
645             }
646           };
647 
648           TransferKills(*feederPos);
649           TransferKills(*cmpPos);
650           bool MO1IsKill = cmpPos->killsRegister(cmpReg1, QRI);
651           bool MO2IsKill = isSecondOpReg && cmpPos->killsRegister(cmpOp2, QRI);
652 
653           MBB->splice(jmpPos, MI.getParent(), MI);
654           MBB->splice(jmpPos, MI.getParent(), cmpInstr);
655           DebugLoc dl = MI.getDebugLoc();
656           MachineInstr *NewMI;
657 
658           assert((isNewValueJumpCandidate(*cmpInstr)) &&
659                  "This compare is not a New Value Jump candidate.");
660           unsigned opc = getNewValueJumpOpcode(cmpInstr, cmpOp2,
661                                                isSecondOpNewified,
662                                                jmpTarget, MBPI);
663           if (invertPredicate)
664             opc = QII->getInvertedPredicatedOpcode(opc);
665 
666           if (isSecondOpReg)
667             NewMI = BuildMI(*MBB, jmpPos, dl, QII->get(opc))
668                         .addReg(cmpReg1, getKillRegState(MO1IsKill))
669                         .addReg(cmpOp2, getKillRegState(MO2IsKill))
670                         .addMBB(jmpTarget);
671 
672           else
673             NewMI = BuildMI(*MBB, jmpPos, dl, QII->get(opc))
674                         .addReg(cmpReg1, getKillRegState(MO1IsKill))
675                         .addImm(cmpOp2)
676                         .addMBB(jmpTarget);
677 
678           assert(NewMI && "New Value Jump Instruction Not created!");
679           (void)NewMI;
680           if (cmpInstr->getOperand(0).isReg() &&
681               cmpInstr->getOperand(0).isKill())
682             cmpInstr->getOperand(0).setIsKill(false);
683           if (cmpInstr->getOperand(1).isReg() &&
684               cmpInstr->getOperand(1).isKill())
685             cmpInstr->getOperand(1).setIsKill(false);
686           cmpInstr->eraseFromParent();
687           jmpInstr->eraseFromParent();
688           ++nvjGenerated;
689           ++NumNVJGenerated;
690           break;
691         }
692       }
693     }
694   }
695 
696   return true;
697 }
698 
699 FunctionPass *llvm::createHexagonNewValueJump() {
700   return new HexagonNewValueJump();
701 }
702