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