]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/Target/Hexagon/HexagonNewValueJump.cpp
MFV r322217: 8418 zfs_prop_get_table() call in zfs_validate_name() is a no-op
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / Target / Hexagon / HexagonNewValueJump.cpp
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>
51 DbgNVJCount("nvj-count", cl::init(-1), cl::Hidden, cl::desc(
52   "Maximum number of predicated jumps to be converted to 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     const HexagonInstrInfo    *QII;
67     const HexagonRegisterInfo *QRI;
68
69   public:
70     static char ID;
71
72     HexagonNewValueJump() : MachineFunctionPass(ID) {}
73
74     void getAnalysisUsage(AnalysisUsage &AU) const override {
75       AU.addRequired<MachineBranchProbabilityInfo>();
76       MachineFunctionPass::getAnalysisUsage(AU);
77     }
78
79     StringRef getPassName() const override { return "Hexagon NewValueJump"; }
80
81     bool runOnMachineFunction(MachineFunction &Fn) override;
82     MachineFunctionProperties getRequiredProperties() const override {
83       return MachineFunctionProperties().set(
84           MachineFunctionProperties::Property::NoVRegs);
85     }
86
87   private:
88     /// \brief A handle to the branch probability pass.
89     const MachineBranchProbabilityInfo *MBPI;
90
91     bool isNewValueJumpCandidate(const MachineInstr &MI) const;
92   };
93
94 } // end of anonymous namespace
95
96 char HexagonNewValueJump::ID = 0;
97
98 INITIALIZE_PASS_BEGIN(HexagonNewValueJump, "hexagon-nvj",
99                       "Hexagon NewValueJump", false, false)
100 INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
101 INITIALIZE_PASS_END(HexagonNewValueJump, "hexagon-nvj",
102                     "Hexagon NewValueJump", false, false)
103
104
105 // We have identified this II could be feeder to NVJ,
106 // verify that it can be.
107 static bool canBeFeederToNewValueJump(const HexagonInstrInfo *QII,
108                                       const TargetRegisterInfo *TRI,
109                                       MachineBasicBlock::iterator II,
110                                       MachineBasicBlock::iterator end,
111                                       MachineBasicBlock::iterator skip,
112                                       MachineFunction &MF) {
113
114   // Predicated instruction can not be feeder to NVJ.
115   if (QII->isPredicated(*II))
116     return false;
117
118   // Bail out if feederReg is a paired register (double regs in
119   // our case). One would think that we can check to see if a given
120   // register cmpReg1 or cmpReg2 is a sub register of feederReg
121   // using -- if (QRI->isSubRegister(feederReg, cmpReg1) logic
122   // before the callsite of this function
123   // But we can not as it comes in the following fashion.
124   //    %D0<def> = Hexagon_S2_lsr_r_p %D0<kill>, %R2<kill>
125   //    %R0<def> = KILL %R0, %D0<imp-use,kill>
126   //    %P0<def> = CMPEQri %R0<kill>, 0
127   // Hence, we need to check if it's a KILL instruction.
128   if (II->getOpcode() == TargetOpcode::KILL)
129     return false;
130
131   if (II->isImplicitDef())
132     return false;
133
134   // Make sure there there is no 'def' or 'use' of any of the uses of
135   // feeder insn between it's definition, this MI and jump, jmpInst
136   // skipping compare, cmpInst.
137   // Here's the example.
138   //    r21=memub(r22+r24<<#0)
139   //    p0 = cmp.eq(r21, #0)
140   //    r4=memub(r3+r21<<#0)
141   //    if (p0.new) jump:t .LBB29_45
142   // Without this check, it will be converted into
143   //    r4=memub(r3+r21<<#0)
144   //    r21=memub(r22+r24<<#0)
145   //    p0 = cmp.eq(r21, #0)
146   //    if (p0.new) jump:t .LBB29_45
147   // and result WAR hazards if converted to New Value Jump.
148
149   for (unsigned i = 0; i < II->getNumOperands(); ++i) {
150     if (II->getOperand(i).isReg() &&
151         (II->getOperand(i).isUse() || II->getOperand(i).isDef())) {
152       MachineBasicBlock::iterator localII = II;
153       ++localII;
154       unsigned Reg = II->getOperand(i).getReg();
155       for (MachineBasicBlock::iterator localBegin = localII;
156                         localBegin != end; ++localBegin) {
157         if (localBegin == skip ) 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->getDesc().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::C2_cmpgti:
231         Valid = (isUInt<5>(v) || v == -1);
232         break;
233       case Hexagon::C2_cmpgtui:
234         Valid = isUInt<5>(v);
235         break;
236       case Hexagon::S2_tstbit_i:
237       case Hexagon::S4_ntstbit_i:
238         Valid = (v == 0);
239         break;
240     }
241
242     if (!Valid)
243       return false;
244   }
245
246   unsigned cmpReg1, cmpOp2 = 0; // cmpOp2 assignment silences compiler warning.
247   cmpReg1 = MI.getOperand(1).getReg();
248
249   if (secondReg) {
250     cmpOp2 = MI.getOperand(2).getReg();
251
252     // If the same register appears as both operands, we cannot generate a new
253     // value compare. Only one operand may use the .new suffix.
254     if (cmpReg1 == cmpOp2)
255       return false;
256
257     // Make sure that that second register is not from COPY
258     // At machine code level, we don't need this, but if we decide
259     // to move new value jump prior to RA, we would be needing this.
260     MachineRegisterInfo &MRI = MF.getRegInfo();
261     if (secondReg && !TargetRegisterInfo::isPhysicalRegister(cmpOp2)) {
262       MachineInstr *def = MRI.getVRegDef(cmpOp2);
263       if (def->getOpcode() == TargetOpcode::COPY)
264         return false;
265     }
266   }
267
268   // Walk the instructions after the compare (predicate def) to the jump,
269   // and satisfy the following conditions.
270   ++II ;
271   for (MachineBasicBlock::iterator localII = II; localII != end;
272        ++localII) {
273     if (localII->isDebugValue())
274       continue;
275
276     // Check 1.
277     // If "common" checks fail, bail out.
278     if (!commonChecksToProhibitNewValueJump(optLocation, localII))
279       return false;
280
281     // Check 2.
282     // If there is a def or use of predicate (result of compare), bail out.
283     if (localII->modifiesRegister(pReg, TRI) ||
284         localII->readsRegister(pReg, TRI))
285       return false;
286
287     // Check 3.
288     // If there is a def of any of the use of the compare (operands of compare),
289     // bail out.
290     // Eg.
291     //    p0 = cmp.eq(r2, r0)
292     //    r2 = r4
293     //    if (p0.new) jump:t .LBB28_3
294     if (localII->modifiesRegister(cmpReg1, TRI) ||
295         (secondReg && localII->modifiesRegister(cmpOp2, TRI)))
296       return false;
297   }
298   return true;
299 }
300
301
302 // Given a compare operator, return a matching New Value Jump compare operator.
303 // Make sure that MI here is included in isNewValueJumpCandidate.
304 static unsigned getNewValueJumpOpcode(MachineInstr *MI, int reg,
305                                       bool secondRegNewified,
306                                       MachineBasicBlock *jmpTarget,
307                                       const MachineBranchProbabilityInfo
308                                       *MBPI) {
309   bool taken = false;
310   MachineBasicBlock *Src = MI->getParent();
311   const BranchProbability Prediction =
312     MBPI->getEdgeProbability(Src, jmpTarget);
313
314   if (Prediction >= BranchProbability(1,2))
315     taken = true;
316
317   switch (MI->getOpcode()) {
318     case Hexagon::C2_cmpeq:
319       return taken ? Hexagon::J4_cmpeq_t_jumpnv_t
320                    : Hexagon::J4_cmpeq_t_jumpnv_nt;
321
322     case Hexagon::C2_cmpeqi: {
323       if (reg >= 0)
324         return taken ? Hexagon::J4_cmpeqi_t_jumpnv_t
325                      : Hexagon::J4_cmpeqi_t_jumpnv_nt;
326       else
327         return taken ? Hexagon::J4_cmpeqn1_t_jumpnv_t
328                      : Hexagon::J4_cmpeqn1_t_jumpnv_nt;
329     }
330
331     case Hexagon::C2_cmpgt: {
332       if (secondRegNewified)
333         return taken ? Hexagon::J4_cmplt_t_jumpnv_t
334                      : Hexagon::J4_cmplt_t_jumpnv_nt;
335       else
336         return taken ? Hexagon::J4_cmpgt_t_jumpnv_t
337                      : Hexagon::J4_cmpgt_t_jumpnv_nt;
338     }
339
340     case Hexagon::C2_cmpgti: {
341       if (reg >= 0)
342         return taken ? Hexagon::J4_cmpgti_t_jumpnv_t
343                      : Hexagon::J4_cmpgti_t_jumpnv_nt;
344       else
345         return taken ? Hexagon::J4_cmpgtn1_t_jumpnv_t
346                      : Hexagon::J4_cmpgtn1_t_jumpnv_nt;
347     }
348
349     case Hexagon::C2_cmpgtu: {
350       if (secondRegNewified)
351         return taken ? Hexagon::J4_cmpltu_t_jumpnv_t
352                      : Hexagon::J4_cmpltu_t_jumpnv_nt;
353       else
354         return taken ? Hexagon::J4_cmpgtu_t_jumpnv_t
355                      : Hexagon::J4_cmpgtu_t_jumpnv_nt;
356     }
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     default:
381        llvm_unreachable("Could not find matching New Value Jump instruction.");
382   }
383   // return *some value* to avoid compiler warning
384   return 0;
385 }
386
387 bool HexagonNewValueJump::isNewValueJumpCandidate(
388     const MachineInstr &MI) const {
389   switch (MI.getOpcode()) {
390   case Hexagon::C2_cmpeq:
391   case Hexagon::C2_cmpeqi:
392   case Hexagon::C2_cmpgt:
393   case Hexagon::C2_cmpgti:
394   case Hexagon::C2_cmpgtu:
395   case Hexagon::C2_cmpgtui:
396   case Hexagon::C4_cmpneq:
397   case Hexagon::C4_cmplte:
398   case Hexagon::C4_cmplteu:
399     return true;
400
401   default:
402     return false;
403   }
404 }
405
406
407 bool HexagonNewValueJump::runOnMachineFunction(MachineFunction &MF) {
408
409   DEBUG(dbgs() << "********** Hexagon New Value Jump **********\n"
410                << "********** Function: "
411                << MF.getName() << "\n");
412
413   if (skipFunction(*MF.getFunction()))
414     return false;
415
416   // If we move NewValueJump before register allocation we'll need live variable
417   // analysis here too.
418
419   QII = static_cast<const HexagonInstrInfo *>(MF.getSubtarget().getInstrInfo());
420   QRI = static_cast<const HexagonRegisterInfo *>(
421       MF.getSubtarget().getRegisterInfo());
422   MBPI = &getAnalysis<MachineBranchProbabilityInfo>();
423
424   if (DisableNewValueJumps) {
425     return false;
426   }
427
428   int nvjCount = DbgNVJCount;
429   int nvjGenerated = 0;
430
431   // Loop through all the bb's of the function
432   for (MachineFunction::iterator MBBb = MF.begin(), MBBe = MF.end();
433         MBBb != MBBe; ++MBBb) {
434     MachineBasicBlock *MBB = &*MBBb;
435
436     DEBUG(dbgs() << "** dumping bb ** "
437                  << MBB->getNumber() << "\n");
438     DEBUG(MBB->dump());
439     DEBUG(dbgs() << "\n" << "********** dumping instr bottom up **********\n");
440     bool foundJump    = false;
441     bool foundCompare = false;
442     bool invertPredicate = false;
443     unsigned predReg = 0; // predicate reg of the jump.
444     unsigned cmpReg1 = 0;
445     int cmpOp2 = 0;
446     MachineBasicBlock::iterator jmpPos;
447     MachineBasicBlock::iterator cmpPos;
448     MachineInstr *cmpInstr = nullptr, *jmpInstr = nullptr;
449     MachineBasicBlock *jmpTarget = nullptr;
450     bool afterRA = false;
451     bool isSecondOpReg = false;
452     bool isSecondOpNewified = false;
453     // Traverse the basic block - bottom up
454     for (MachineBasicBlock::iterator MII = MBB->end(), E = MBB->begin();
455              MII != E;) {
456       MachineInstr &MI = *--MII;
457       if (MI.isDebugValue()) {
458         continue;
459       }
460
461       if ((nvjCount == 0) || (nvjCount > -1 && nvjCount <= nvjGenerated))
462         break;
463
464       DEBUG(dbgs() << "Instr: "; MI.dump(); dbgs() << "\n");
465
466       if (!foundJump && (MI.getOpcode() == Hexagon::J2_jumpt ||
467                          MI.getOpcode() == Hexagon::J2_jumptpt ||
468                          MI.getOpcode() == Hexagon::J2_jumpf ||
469                          MI.getOpcode() == Hexagon::J2_jumpfpt ||
470                          MI.getOpcode() == Hexagon::J2_jumptnewpt ||
471                          MI.getOpcode() == Hexagon::J2_jumptnew ||
472                          MI.getOpcode() == Hexagon::J2_jumpfnewpt ||
473                          MI.getOpcode() == Hexagon::J2_jumpfnew)) {
474         // This is where you would insert your compare and
475         // instr that feeds compare
476         jmpPos = MII;
477         jmpInstr = &MI;
478         predReg = MI.getOperand(0).getReg();
479         afterRA = TargetRegisterInfo::isPhysicalRegister(predReg);
480
481         // If ifconverter had not messed up with the kill flags of the
482         // operands, the following check on the kill flag would suffice.
483         // if(!jmpInstr->getOperand(0).isKill()) break;
484
485         // This predicate register is live out out of BB
486         // this would only work if we can actually use Live
487         // variable analysis on phy regs - but LLVM does not
488         // provide LV analysis on phys regs.
489         //if(LVs.isLiveOut(predReg, *MBB)) break;
490
491         // Get all the successors of this block - which will always
492         // be 2. Check if the predicate register is live-in in those
493         // successor. If yes, we can not delete the predicate -
494         // I am doing this only because LLVM does not provide LiveOut
495         // at the BB level.
496         bool predLive = false;
497         for (MachineBasicBlock::const_succ_iterator SI = MBB->succ_begin(),
498                             SIE = MBB->succ_end(); SI != SIE; ++SI) {
499           MachineBasicBlock* succMBB = *SI;
500          if (succMBB->isLiveIn(predReg)) {
501             predLive = true;
502           }
503         }
504         if (predLive)
505           break;
506
507         if (!MI.getOperand(1).isMBB())
508           continue;
509         jmpTarget = MI.getOperand(1).getMBB();
510         foundJump = true;
511         if (MI.getOpcode() == Hexagon::J2_jumpf ||
512             MI.getOpcode() == Hexagon::J2_jumpfnewpt ||
513             MI.getOpcode() == Hexagon::J2_jumpfnew) {
514           invertPredicate = true;
515         }
516         continue;
517       }
518
519       // No new value jump if there is a barrier. A barrier has to be in its
520       // own packet. A barrier has zero operands. We conservatively bail out
521       // here if we see any instruction with zero operands.
522       if (foundJump && MI.getNumOperands() == 0)
523         break;
524
525       if (foundJump && !foundCompare && MI.getOperand(0).isReg() &&
526           MI.getOperand(0).getReg() == predReg) {
527
528         // Not all compares can be new value compare. Arch Spec: 7.6.1.1
529         if (isNewValueJumpCandidate(MI)) {
530
531           assert(
532               (MI.getDesc().isCompare()) &&
533               "Only compare instruction can be collapsed into New Value Jump");
534           isSecondOpReg = MI.getOperand(2).isReg();
535
536           if (!canCompareBeNewValueJump(QII, QRI, MII, predReg, isSecondOpReg,
537                                         afterRA, jmpPos, MF))
538             break;
539
540           cmpInstr = &MI;
541           cmpPos = MII;
542           foundCompare = true;
543
544           // We need cmpReg1 and cmpOp2(imm or reg) while building
545           // new value jump instruction.
546           cmpReg1 = MI.getOperand(1).getReg();
547
548           if (isSecondOpReg)
549             cmpOp2 = MI.getOperand(2).getReg();
550           else
551             cmpOp2 = MI.getOperand(2).getImm();
552           continue;
553         }
554       }
555
556       if (foundCompare && foundJump) {
557
558         // If "common" checks fail, bail out on this BB.
559         if (!commonChecksToProhibitNewValueJump(afterRA, MII))
560           break;
561
562         bool foundFeeder = false;
563         MachineBasicBlock::iterator feederPos = MII;
564         if (MI.getOperand(0).isReg() && MI.getOperand(0).isDef() &&
565             (MI.getOperand(0).getReg() == cmpReg1 ||
566              (isSecondOpReg &&
567               MI.getOperand(0).getReg() == (unsigned)cmpOp2))) {
568
569           unsigned feederReg = MI.getOperand(0).getReg();
570
571           // First try to see if we can get the feeder from the first operand
572           // of the compare. If we can not, and if secondOpReg is true
573           // (second operand of the compare is also register), try that one.
574           // TODO: Try to come up with some heuristic to figure out which
575           // feeder would benefit.
576
577           if (feederReg == cmpReg1) {
578             if (!canBeFeederToNewValueJump(QII, QRI, MII, jmpPos, cmpPos, MF)) {
579               if (!isSecondOpReg)
580                 break;
581               else
582                 continue;
583             } else
584               foundFeeder = true;
585           }
586
587           if (!foundFeeder &&
588                isSecondOpReg &&
589                feederReg == (unsigned) cmpOp2)
590             if (!canBeFeederToNewValueJump(QII, QRI, MII, jmpPos, cmpPos, MF))
591               break;
592
593           if (isSecondOpReg) {
594             // In case of CMPLT, or CMPLTU, or EQ with the second register
595             // to newify, swap the operands.
596             unsigned COp = cmpInstr->getOpcode();
597             if ((COp == Hexagon::C2_cmpeq || COp == Hexagon::C4_cmpneq) &&
598                 (feederReg == (unsigned) cmpOp2)) {
599               unsigned tmp = cmpReg1;
600               cmpReg1 = cmpOp2;
601               cmpOp2 = tmp;
602             }
603
604             // Now we have swapped the operands, all we need to check is,
605             // if the second operand (after swap) is the feeder.
606             // And if it is, make a note.
607             if (feederReg == (unsigned)cmpOp2)
608               isSecondOpNewified = true;
609           }
610
611           // Now that we are moving feeder close the jump,
612           // make sure we are respecting the kill values of
613           // the operands of the feeder.
614
615           auto TransferKills = [jmpPos,cmpPos] (MachineInstr &MI) {
616             for (MachineOperand &MO : MI.operands()) {
617               if (!MO.isReg() || !MO.isUse())
618                 continue;
619               unsigned UseR = MO.getReg();
620               for (auto I = std::next(MI.getIterator()); I != jmpPos; ++I) {
621                 if (I == cmpPos)
622                   continue;
623                 for (MachineOperand &Op : I->operands()) {
624                   if (!Op.isReg() || !Op.isUse() || !Op.isKill())
625                     continue;
626                   if (Op.getReg() != UseR)
627                     continue;
628                   // We found that there is kill of a use register
629                   // Set up a kill flag on the register
630                   Op.setIsKill(false);
631                   MO.setIsKill(true);
632                   return;
633                 }
634               }
635             }
636           };
637
638           TransferKills(*feederPos);
639           TransferKills(*cmpPos);
640           bool MO1IsKill = cmpPos->killsRegister(cmpReg1, QRI);
641           bool MO2IsKill = isSecondOpReg && cmpPos->killsRegister(cmpOp2, QRI);
642
643           MBB->splice(jmpPos, MI.getParent(), MI);
644           MBB->splice(jmpPos, MI.getParent(), cmpInstr);
645           DebugLoc dl = MI.getDebugLoc();
646           MachineInstr *NewMI;
647
648           assert((isNewValueJumpCandidate(*cmpInstr)) &&
649                  "This compare is not a New Value Jump candidate.");
650           unsigned opc = getNewValueJumpOpcode(cmpInstr, cmpOp2,
651                                                isSecondOpNewified,
652                                                jmpTarget, MBPI);
653           if (invertPredicate)
654             opc = QII->getInvertedPredicatedOpcode(opc);
655
656           if (isSecondOpReg)
657             NewMI = BuildMI(*MBB, jmpPos, dl,
658                                   QII->get(opc))
659                                     .addReg(cmpReg1, getKillRegState(MO1IsKill))
660                                     .addReg(cmpOp2, getKillRegState(MO2IsKill))
661                                     .addMBB(jmpTarget);
662
663           else
664             NewMI = BuildMI(*MBB, jmpPos, dl,
665                                   QII->get(opc))
666                                     .addReg(cmpReg1, getKillRegState(MO1IsKill))
667                                     .addImm(cmpOp2)
668                                     .addMBB(jmpTarget);
669
670           assert(NewMI && "New Value Jump Instruction Not created!");
671           (void)NewMI;
672           if (cmpInstr->getOperand(0).isReg() &&
673               cmpInstr->getOperand(0).isKill())
674             cmpInstr->getOperand(0).setIsKill(false);
675           if (cmpInstr->getOperand(1).isReg() &&
676               cmpInstr->getOperand(1).isKill())
677             cmpInstr->getOperand(1).setIsKill(false);
678           cmpInstr->eraseFromParent();
679           jmpInstr->eraseFromParent();
680           ++nvjGenerated;
681           ++NumNVJGenerated;
682           break;
683         }
684       }
685     }
686   }
687
688   return true;
689
690 }
691
692 FunctionPass *llvm::createHexagonNewValueJump() {
693   return new HexagonNewValueJump();
694 }