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