1 //===- HexagonInstrInfo.cpp - Hexagon Instruction Information -------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains the Hexagon implementation of the TargetInstrInfo class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "HexagonInstrInfo.h"
14 #include "Hexagon.h"
15 #include "HexagonFrameLowering.h"
16 #include "HexagonHazardRecognizer.h"
17 #include "HexagonRegisterInfo.h"
18 #include "HexagonSubtarget.h"
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/SmallPtrSet.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/StringRef.h"
23 #include "llvm/CodeGen/DFAPacketizer.h"
24 #include "llvm/CodeGen/LivePhysRegs.h"
25 #include "llvm/CodeGen/MachineBasicBlock.h"
26 #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
27 #include "llvm/CodeGen/MachineFrameInfo.h"
28 #include "llvm/CodeGen/MachineFunction.h"
29 #include "llvm/CodeGen/MachineInstr.h"
30 #include "llvm/CodeGen/MachineInstrBuilder.h"
31 #include "llvm/CodeGen/MachineInstrBundle.h"
32 #include "llvm/CodeGen/MachineLoopInfo.h"
33 #include "llvm/CodeGen/MachineMemOperand.h"
34 #include "llvm/CodeGen/MachineOperand.h"
35 #include "llvm/CodeGen/MachineRegisterInfo.h"
36 #include "llvm/CodeGen/ScheduleDAG.h"
37 #include "llvm/CodeGen/TargetInstrInfo.h"
38 #include "llvm/CodeGen/TargetOpcodes.h"
39 #include "llvm/CodeGen/TargetRegisterInfo.h"
40 #include "llvm/CodeGen/TargetSubtargetInfo.h"
41 #include "llvm/IR/DebugLoc.h"
42 #include "llvm/MC/MCAsmInfo.h"
43 #include "llvm/MC/MCInstrDesc.h"
44 #include "llvm/MC/MCInstrItineraries.h"
45 #include "llvm/MC/MCRegisterInfo.h"
46 #include "llvm/Support/BranchProbability.h"
47 #include "llvm/Support/CommandLine.h"
48 #include "llvm/Support/Debug.h"
49 #include "llvm/Support/ErrorHandling.h"
50 #include "llvm/Support/MachineValueType.h"
51 #include "llvm/Support/MathExtras.h"
52 #include "llvm/Support/raw_ostream.h"
53 #include "llvm/Target/TargetMachine.h"
54 #include <cassert>
55 #include <cctype>
56 #include <cstdint>
57 #include <cstring>
58 #include <iterator>
59 #include <string>
60 #include <utility>
61 
62 using namespace llvm;
63 
64 #define DEBUG_TYPE "hexagon-instrinfo"
65 
66 #define GET_INSTRINFO_CTOR_DTOR
67 #define GET_INSTRMAP_INFO
68 #include "HexagonDepTimingClasses.h"
69 #include "HexagonGenDFAPacketizer.inc"
70 #include "HexagonGenInstrInfo.inc"
71 
72 cl::opt<bool> ScheduleInlineAsm("hexagon-sched-inline-asm", cl::Hidden,
73   cl::init(false), cl::desc("Do not consider inline-asm a scheduling/"
74                             "packetization boundary."));
75 
76 static cl::opt<bool> EnableBranchPrediction("hexagon-enable-branch-prediction",
77   cl::Hidden, cl::init(true), cl::desc("Enable branch prediction"));
78 
79 static cl::opt<bool> DisableNVSchedule("disable-hexagon-nv-schedule",
80   cl::Hidden, cl::ZeroOrMore, cl::init(false),
81   cl::desc("Disable schedule adjustment for new value stores."));
82 
83 static cl::opt<bool> EnableTimingClassLatency(
84   "enable-timing-class-latency", cl::Hidden, cl::init(false),
85   cl::desc("Enable timing class latency"));
86 
87 static cl::opt<bool> EnableALUForwarding(
88   "enable-alu-forwarding", cl::Hidden, cl::init(true),
89   cl::desc("Enable vec alu forwarding"));
90 
91 static cl::opt<bool> EnableACCForwarding(
92   "enable-acc-forwarding", cl::Hidden, cl::init(true),
93   cl::desc("Enable vec acc forwarding"));
94 
95 static cl::opt<bool> BranchRelaxAsmLarge("branch-relax-asm-large",
96   cl::init(true), cl::Hidden, cl::ZeroOrMore, cl::desc("branch relax asm"));
97 
98 static cl::opt<bool> UseDFAHazardRec("dfa-hazard-rec",
99   cl::init(true), cl::Hidden, cl::ZeroOrMore,
100   cl::desc("Use the DFA based hazard recognizer."));
101 
102 /// Constants for Hexagon instructions.
103 const int Hexagon_MEMW_OFFSET_MAX = 4095;
104 const int Hexagon_MEMW_OFFSET_MIN = -4096;
105 const int Hexagon_MEMD_OFFSET_MAX = 8191;
106 const int Hexagon_MEMD_OFFSET_MIN = -8192;
107 const int Hexagon_MEMH_OFFSET_MAX = 2047;
108 const int Hexagon_MEMH_OFFSET_MIN = -2048;
109 const int Hexagon_MEMB_OFFSET_MAX = 1023;
110 const int Hexagon_MEMB_OFFSET_MIN = -1024;
111 const int Hexagon_ADDI_OFFSET_MAX = 32767;
112 const int Hexagon_ADDI_OFFSET_MIN = -32768;
113 
114 // Pin the vtable to this file.
115 void HexagonInstrInfo::anchor() {}
116 
117 HexagonInstrInfo::HexagonInstrInfo(HexagonSubtarget &ST)
118   : HexagonGenInstrInfo(Hexagon::ADJCALLSTACKDOWN, Hexagon::ADJCALLSTACKUP),
119     Subtarget(ST) {}
120 
121 namespace llvm {
122 namespace HexagonFUnits {
123   bool isSlot0Only(unsigned units);
124 }
125 }
126 
127 static bool isIntRegForSubInst(unsigned Reg) {
128   return (Reg >= Hexagon::R0 && Reg <= Hexagon::R7) ||
129          (Reg >= Hexagon::R16 && Reg <= Hexagon::R23);
130 }
131 
132 static bool isDblRegForSubInst(unsigned Reg, const HexagonRegisterInfo &HRI) {
133   return isIntRegForSubInst(HRI.getSubReg(Reg, Hexagon::isub_lo)) &&
134          isIntRegForSubInst(HRI.getSubReg(Reg, Hexagon::isub_hi));
135 }
136 
137 /// Calculate number of instructions excluding the debug instructions.
138 static unsigned nonDbgMICount(MachineBasicBlock::const_instr_iterator MIB,
139                               MachineBasicBlock::const_instr_iterator MIE) {
140   unsigned Count = 0;
141   for (; MIB != MIE; ++MIB) {
142     if (!MIB->isDebugInstr())
143       ++Count;
144   }
145   return Count;
146 }
147 
148 /// Find the hardware loop instruction used to set-up the specified loop.
149 /// On Hexagon, we have two instructions used to set-up the hardware loop
150 /// (LOOP0, LOOP1) with corresponding endloop (ENDLOOP0, ENDLOOP1) instructions
151 /// to indicate the end of a loop.
152 MachineInstr *HexagonInstrInfo::findLoopInstr(MachineBasicBlock *BB,
153       unsigned EndLoopOp, MachineBasicBlock *TargetBB,
154       SmallPtrSet<MachineBasicBlock *, 8> &Visited) const {
155   unsigned LOOPi;
156   unsigned LOOPr;
157   if (EndLoopOp == Hexagon::ENDLOOP0) {
158     LOOPi = Hexagon::J2_loop0i;
159     LOOPr = Hexagon::J2_loop0r;
160   } else { // EndLoopOp == Hexagon::EndLOOP1
161     LOOPi = Hexagon::J2_loop1i;
162     LOOPr = Hexagon::J2_loop1r;
163   }
164 
165   // The loop set-up instruction will be in a predecessor block
166   for (MachineBasicBlock *PB : BB->predecessors()) {
167     // If this has been visited, already skip it.
168     if (!Visited.insert(PB).second)
169       continue;
170     if (PB == BB)
171       continue;
172     for (MachineInstr &I : llvm::reverse(PB->instrs())) {
173       unsigned Opc = I.getOpcode();
174       if (Opc == LOOPi || Opc == LOOPr)
175         return &I;
176       // We've reached a different loop, which means the loop01 has been
177       // removed.
178       if (Opc == EndLoopOp && I.getOperand(0).getMBB() != TargetBB)
179         return nullptr;
180     }
181     // Check the predecessors for the LOOP instruction.
182     if (MachineInstr *Loop = findLoopInstr(PB, EndLoopOp, TargetBB, Visited))
183       return Loop;
184   }
185   return nullptr;
186 }
187 
188 /// Gather register def/uses from MI.
189 /// This treats possible (predicated) defs as actually happening ones
190 /// (conservatively).
191 static inline void parseOperands(const MachineInstr &MI,
192       SmallVector<unsigned, 4> &Defs, SmallVector<unsigned, 8> &Uses) {
193   Defs.clear();
194   Uses.clear();
195 
196   for (const MachineOperand &MO : MI.operands()) {
197     if (!MO.isReg())
198       continue;
199 
200     Register Reg = MO.getReg();
201     if (!Reg)
202       continue;
203 
204     if (MO.isUse())
205       Uses.push_back(MO.getReg());
206 
207     if (MO.isDef())
208       Defs.push_back(MO.getReg());
209   }
210 }
211 
212 // Position dependent, so check twice for swap.
213 static bool isDuplexPairMatch(unsigned Ga, unsigned Gb) {
214   switch (Ga) {
215   case HexagonII::HSIG_None:
216   default:
217     return false;
218   case HexagonII::HSIG_L1:
219     return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_A);
220   case HexagonII::HSIG_L2:
221     return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
222             Gb == HexagonII::HSIG_A);
223   case HexagonII::HSIG_S1:
224     return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
225             Gb == HexagonII::HSIG_S1 || Gb == HexagonII::HSIG_A);
226   case HexagonII::HSIG_S2:
227     return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
228             Gb == HexagonII::HSIG_S1 || Gb == HexagonII::HSIG_S2 ||
229             Gb == HexagonII::HSIG_A);
230   case HexagonII::HSIG_A:
231     return (Gb == HexagonII::HSIG_A);
232   case HexagonII::HSIG_Compound:
233     return (Gb == HexagonII::HSIG_Compound);
234   }
235   return false;
236 }
237 
238 /// isLoadFromStackSlot - If the specified machine instruction is a direct
239 /// load from a stack slot, return the virtual or physical register number of
240 /// the destination along with the FrameIndex of the loaded stack slot.  If
241 /// not, return 0.  This predicate must return 0 if the instruction has
242 /// any side effects other than loading from the stack slot.
243 unsigned HexagonInstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
244                                                int &FrameIndex) const {
245   switch (MI.getOpcode()) {
246     default:
247       break;
248     case Hexagon::L2_loadri_io:
249     case Hexagon::L2_loadrd_io:
250     case Hexagon::V6_vL32b_ai:
251     case Hexagon::V6_vL32b_nt_ai:
252     case Hexagon::V6_vL32Ub_ai:
253     case Hexagon::LDriw_pred:
254     case Hexagon::LDriw_ctr:
255     case Hexagon::PS_vloadrq_ai:
256     case Hexagon::PS_vloadrw_ai:
257     case Hexagon::PS_vloadrw_nt_ai: {
258       const MachineOperand OpFI = MI.getOperand(1);
259       if (!OpFI.isFI())
260         return 0;
261       const MachineOperand OpOff = MI.getOperand(2);
262       if (!OpOff.isImm() || OpOff.getImm() != 0)
263         return 0;
264       FrameIndex = OpFI.getIndex();
265       return MI.getOperand(0).getReg();
266     }
267 
268     case Hexagon::L2_ploadrit_io:
269     case Hexagon::L2_ploadrif_io:
270     case Hexagon::L2_ploadrdt_io:
271     case Hexagon::L2_ploadrdf_io: {
272       const MachineOperand OpFI = MI.getOperand(2);
273       if (!OpFI.isFI())
274         return 0;
275       const MachineOperand OpOff = MI.getOperand(3);
276       if (!OpOff.isImm() || OpOff.getImm() != 0)
277         return 0;
278       FrameIndex = OpFI.getIndex();
279       return MI.getOperand(0).getReg();
280     }
281   }
282 
283   return 0;
284 }
285 
286 /// isStoreToStackSlot - If the specified machine instruction is a direct
287 /// store to a stack slot, return the virtual or physical register number of
288 /// the source reg along with the FrameIndex of the loaded stack slot.  If
289 /// not, return 0.  This predicate must return 0 if the instruction has
290 /// any side effects other than storing to the stack slot.
291 unsigned HexagonInstrInfo::isStoreToStackSlot(const MachineInstr &MI,
292                                               int &FrameIndex) const {
293   switch (MI.getOpcode()) {
294     default:
295       break;
296     case Hexagon::S2_storerb_io:
297     case Hexagon::S2_storerh_io:
298     case Hexagon::S2_storeri_io:
299     case Hexagon::S2_storerd_io:
300     case Hexagon::V6_vS32b_ai:
301     case Hexagon::V6_vS32Ub_ai:
302     case Hexagon::STriw_pred:
303     case Hexagon::STriw_ctr:
304     case Hexagon::PS_vstorerq_ai:
305     case Hexagon::PS_vstorerw_ai: {
306       const MachineOperand &OpFI = MI.getOperand(0);
307       if (!OpFI.isFI())
308         return 0;
309       const MachineOperand &OpOff = MI.getOperand(1);
310       if (!OpOff.isImm() || OpOff.getImm() != 0)
311         return 0;
312       FrameIndex = OpFI.getIndex();
313       return MI.getOperand(2).getReg();
314     }
315 
316     case Hexagon::S2_pstorerbt_io:
317     case Hexagon::S2_pstorerbf_io:
318     case Hexagon::S2_pstorerht_io:
319     case Hexagon::S2_pstorerhf_io:
320     case Hexagon::S2_pstorerit_io:
321     case Hexagon::S2_pstorerif_io:
322     case Hexagon::S2_pstorerdt_io:
323     case Hexagon::S2_pstorerdf_io: {
324       const MachineOperand &OpFI = MI.getOperand(1);
325       if (!OpFI.isFI())
326         return 0;
327       const MachineOperand &OpOff = MI.getOperand(2);
328       if (!OpOff.isImm() || OpOff.getImm() != 0)
329         return 0;
330       FrameIndex = OpFI.getIndex();
331       return MI.getOperand(3).getReg();
332     }
333   }
334 
335   return 0;
336 }
337 
338 /// This function checks if the instruction or bundle of instructions
339 /// has load from stack slot and returns frameindex and machine memory
340 /// operand of that instruction if true.
341 bool HexagonInstrInfo::hasLoadFromStackSlot(
342     const MachineInstr &MI,
343     SmallVectorImpl<const MachineMemOperand *> &Accesses) const {
344   if (MI.isBundle()) {
345     const MachineBasicBlock *MBB = MI.getParent();
346     MachineBasicBlock::const_instr_iterator MII = MI.getIterator();
347     for (++MII; MII != MBB->instr_end() && MII->isInsideBundle(); ++MII)
348       if (TargetInstrInfo::hasLoadFromStackSlot(*MII, Accesses))
349         return true;
350     return false;
351   }
352 
353   return TargetInstrInfo::hasLoadFromStackSlot(MI, Accesses);
354 }
355 
356 /// This function checks if the instruction or bundle of instructions
357 /// has store to stack slot and returns frameindex and machine memory
358 /// operand of that instruction if true.
359 bool HexagonInstrInfo::hasStoreToStackSlot(
360     const MachineInstr &MI,
361     SmallVectorImpl<const MachineMemOperand *> &Accesses) const {
362   if (MI.isBundle()) {
363     const MachineBasicBlock *MBB = MI.getParent();
364     MachineBasicBlock::const_instr_iterator MII = MI.getIterator();
365     for (++MII; MII != MBB->instr_end() && MII->isInsideBundle(); ++MII)
366       if (TargetInstrInfo::hasStoreToStackSlot(*MII, Accesses))
367         return true;
368     return false;
369   }
370 
371   return TargetInstrInfo::hasStoreToStackSlot(MI, Accesses);
372 }
373 
374 /// This function can analyze one/two way branching only and should (mostly) be
375 /// called by target independent side.
376 /// First entry is always the opcode of the branching instruction, except when
377 /// the Cond vector is supposed to be empty, e.g., when analyzeBranch fails, a
378 /// BB with only unconditional jump. Subsequent entries depend upon the opcode,
379 /// e.g. Jump_c p will have
380 /// Cond[0] = Jump_c
381 /// Cond[1] = p
382 /// HW-loop ENDLOOP:
383 /// Cond[0] = ENDLOOP
384 /// Cond[1] = MBB
385 /// New value jump:
386 /// Cond[0] = Hexagon::CMPEQri_f_Jumpnv_t_V4 -- specific opcode
387 /// Cond[1] = R
388 /// Cond[2] = Imm
389 bool HexagonInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
390                                      MachineBasicBlock *&TBB,
391                                      MachineBasicBlock *&FBB,
392                                      SmallVectorImpl<MachineOperand> &Cond,
393                                      bool AllowModify) const {
394   TBB = nullptr;
395   FBB = nullptr;
396   Cond.clear();
397 
398   // If the block has no terminators, it just falls into the block after it.
399   MachineBasicBlock::instr_iterator I = MBB.instr_end();
400   if (I == MBB.instr_begin())
401     return false;
402 
403   // A basic block may looks like this:
404   //
405   //  [   insn
406   //     EH_LABEL
407   //      insn
408   //      insn
409   //      insn
410   //     EH_LABEL
411   //      insn     ]
412   //
413   // It has two succs but does not have a terminator
414   // Don't know how to handle it.
415   do {
416     --I;
417     if (I->isEHLabel())
418       // Don't analyze EH branches.
419       return true;
420   } while (I != MBB.instr_begin());
421 
422   I = MBB.instr_end();
423   --I;
424 
425   while (I->isDebugInstr()) {
426     if (I == MBB.instr_begin())
427       return false;
428     --I;
429   }
430 
431   bool JumpToBlock = I->getOpcode() == Hexagon::J2_jump &&
432                      I->getOperand(0).isMBB();
433   // Delete the J2_jump if it's equivalent to a fall-through.
434   if (AllowModify && JumpToBlock &&
435       MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
436     LLVM_DEBUG(dbgs() << "\nErasing the jump to successor block\n";);
437     I->eraseFromParent();
438     I = MBB.instr_end();
439     if (I == MBB.instr_begin())
440       return false;
441     --I;
442   }
443   if (!isUnpredicatedTerminator(*I))
444     return false;
445 
446   // Get the last instruction in the block.
447   MachineInstr *LastInst = &*I;
448   MachineInstr *SecondLastInst = nullptr;
449   // Find one more terminator if present.
450   while (true) {
451     if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(*I)) {
452       if (!SecondLastInst)
453         SecondLastInst = &*I;
454       else
455         // This is a third branch.
456         return true;
457     }
458     if (I == MBB.instr_begin())
459       break;
460     --I;
461   }
462 
463   int LastOpcode = LastInst->getOpcode();
464   int SecLastOpcode = SecondLastInst ? SecondLastInst->getOpcode() : 0;
465   // If the branch target is not a basic block, it could be a tail call.
466   // (It is, if the target is a function.)
467   if (LastOpcode == Hexagon::J2_jump && !LastInst->getOperand(0).isMBB())
468     return true;
469   if (SecLastOpcode == Hexagon::J2_jump &&
470       !SecondLastInst->getOperand(0).isMBB())
471     return true;
472 
473   bool LastOpcodeHasJMP_c = PredOpcodeHasJMP_c(LastOpcode);
474   bool LastOpcodeHasNVJump = isNewValueJump(*LastInst);
475 
476   if (LastOpcodeHasJMP_c && !LastInst->getOperand(1).isMBB())
477     return true;
478 
479   // If there is only one terminator instruction, process it.
480   if (LastInst && !SecondLastInst) {
481     if (LastOpcode == Hexagon::J2_jump) {
482       TBB = LastInst->getOperand(0).getMBB();
483       return false;
484     }
485     if (isEndLoopN(LastOpcode)) {
486       TBB = LastInst->getOperand(0).getMBB();
487       Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
488       Cond.push_back(LastInst->getOperand(0));
489       return false;
490     }
491     if (LastOpcodeHasJMP_c) {
492       TBB = LastInst->getOperand(1).getMBB();
493       Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
494       Cond.push_back(LastInst->getOperand(0));
495       return false;
496     }
497     // Only supporting rr/ri versions of new-value jumps.
498     if (LastOpcodeHasNVJump && (LastInst->getNumExplicitOperands() == 3)) {
499       TBB = LastInst->getOperand(2).getMBB();
500       Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
501       Cond.push_back(LastInst->getOperand(0));
502       Cond.push_back(LastInst->getOperand(1));
503       return false;
504     }
505     LLVM_DEBUG(dbgs() << "\nCant analyze " << printMBBReference(MBB)
506                       << " with one jump\n";);
507     // Otherwise, don't know what this is.
508     return true;
509   }
510 
511   bool SecLastOpcodeHasJMP_c = PredOpcodeHasJMP_c(SecLastOpcode);
512   bool SecLastOpcodeHasNVJump = isNewValueJump(*SecondLastInst);
513   if (SecLastOpcodeHasJMP_c && (LastOpcode == Hexagon::J2_jump)) {
514     if (!SecondLastInst->getOperand(1).isMBB())
515       return true;
516     TBB =  SecondLastInst->getOperand(1).getMBB();
517     Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
518     Cond.push_back(SecondLastInst->getOperand(0));
519     FBB = LastInst->getOperand(0).getMBB();
520     return false;
521   }
522 
523   // Only supporting rr/ri versions of new-value jumps.
524   if (SecLastOpcodeHasNVJump &&
525       (SecondLastInst->getNumExplicitOperands() == 3) &&
526       (LastOpcode == Hexagon::J2_jump)) {
527     TBB = SecondLastInst->getOperand(2).getMBB();
528     Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
529     Cond.push_back(SecondLastInst->getOperand(0));
530     Cond.push_back(SecondLastInst->getOperand(1));
531     FBB = LastInst->getOperand(0).getMBB();
532     return false;
533   }
534 
535   // If the block ends with two Hexagon:JMPs, handle it.  The second one is not
536   // executed, so remove it.
537   if (SecLastOpcode == Hexagon::J2_jump && LastOpcode == Hexagon::J2_jump) {
538     TBB = SecondLastInst->getOperand(0).getMBB();
539     I = LastInst->getIterator();
540     if (AllowModify)
541       I->eraseFromParent();
542     return false;
543   }
544 
545   // If the block ends with an ENDLOOP, and J2_jump, handle it.
546   if (isEndLoopN(SecLastOpcode) && LastOpcode == Hexagon::J2_jump) {
547     TBB = SecondLastInst->getOperand(0).getMBB();
548     Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
549     Cond.push_back(SecondLastInst->getOperand(0));
550     FBB = LastInst->getOperand(0).getMBB();
551     return false;
552   }
553   LLVM_DEBUG(dbgs() << "\nCant analyze " << printMBBReference(MBB)
554                     << " with two jumps";);
555   // Otherwise, can't handle this.
556   return true;
557 }
558 
559 unsigned HexagonInstrInfo::removeBranch(MachineBasicBlock &MBB,
560                                         int *BytesRemoved) const {
561   assert(!BytesRemoved && "code size not handled");
562 
563   LLVM_DEBUG(dbgs() << "\nRemoving branches out of " << printMBBReference(MBB));
564   MachineBasicBlock::iterator I = MBB.end();
565   unsigned Count = 0;
566   while (I != MBB.begin()) {
567     --I;
568     if (I->isDebugInstr())
569       continue;
570     // Only removing branches from end of MBB.
571     if (!I->isBranch())
572       return Count;
573     if (Count && (I->getOpcode() == Hexagon::J2_jump))
574       llvm_unreachable("Malformed basic block: unconditional branch not last");
575     MBB.erase(&MBB.back());
576     I = MBB.end();
577     ++Count;
578   }
579   return Count;
580 }
581 
582 unsigned HexagonInstrInfo::insertBranch(MachineBasicBlock &MBB,
583                                         MachineBasicBlock *TBB,
584                                         MachineBasicBlock *FBB,
585                                         ArrayRef<MachineOperand> Cond,
586                                         const DebugLoc &DL,
587                                         int *BytesAdded) const {
588   unsigned BOpc   = Hexagon::J2_jump;
589   unsigned BccOpc = Hexagon::J2_jumpt;
590   assert(validateBranchCond(Cond) && "Invalid branching condition");
591   assert(TBB && "insertBranch must not be told to insert a fallthrough");
592   assert(!BytesAdded && "code size not handled");
593 
594   // Check if reverseBranchCondition has asked to reverse this branch
595   // If we want to reverse the branch an odd number of times, we want
596   // J2_jumpf.
597   if (!Cond.empty() && Cond[0].isImm())
598     BccOpc = Cond[0].getImm();
599 
600   if (!FBB) {
601     if (Cond.empty()) {
602       // Due to a bug in TailMerging/CFG Optimization, we need to add a
603       // special case handling of a predicated jump followed by an
604       // unconditional jump. If not, Tail Merging and CFG Optimization go
605       // into an infinite loop.
606       MachineBasicBlock *NewTBB, *NewFBB;
607       SmallVector<MachineOperand, 4> Cond;
608       auto Term = MBB.getFirstTerminator();
609       if (Term != MBB.end() && isPredicated(*Term) &&
610           !analyzeBranch(MBB, NewTBB, NewFBB, Cond, false) &&
611           MachineFunction::iterator(NewTBB) == ++MBB.getIterator()) {
612         reverseBranchCondition(Cond);
613         removeBranch(MBB);
614         return insertBranch(MBB, TBB, nullptr, Cond, DL);
615       }
616       BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
617     } else if (isEndLoopN(Cond[0].getImm())) {
618       int EndLoopOp = Cond[0].getImm();
619       assert(Cond[1].isMBB());
620       // Since we're adding an ENDLOOP, there better be a LOOP instruction.
621       // Check for it, and change the BB target if needed.
622       SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
623       MachineInstr *Loop = findLoopInstr(TBB, EndLoopOp, Cond[1].getMBB(),
624                                          VisitedBBs);
625       assert(Loop != nullptr && "Inserting an ENDLOOP without a LOOP");
626       Loop->getOperand(0).setMBB(TBB);
627       // Add the ENDLOOP after the finding the LOOP0.
628       BuildMI(&MBB, DL, get(EndLoopOp)).addMBB(TBB);
629     } else if (isNewValueJump(Cond[0].getImm())) {
630       assert((Cond.size() == 3) && "Only supporting rr/ri version of nvjump");
631       // New value jump
632       // (ins IntRegs:$src1, IntRegs:$src2, brtarget:$offset)
633       // (ins IntRegs:$src1, u5Imm:$src2, brtarget:$offset)
634       unsigned Flags1 = getUndefRegState(Cond[1].isUndef());
635       LLVM_DEBUG(dbgs() << "\nInserting NVJump for "
636                         << printMBBReference(MBB););
637       if (Cond[2].isReg()) {
638         unsigned Flags2 = getUndefRegState(Cond[2].isUndef());
639         BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[1].getReg(), Flags1).
640           addReg(Cond[2].getReg(), Flags2).addMBB(TBB);
641       } else if(Cond[2].isImm()) {
642         BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[1].getReg(), Flags1).
643           addImm(Cond[2].getImm()).addMBB(TBB);
644       } else
645         llvm_unreachable("Invalid condition for branching");
646     } else {
647       assert((Cond.size() == 2) && "Malformed cond vector");
648       const MachineOperand &RO = Cond[1];
649       unsigned Flags = getUndefRegState(RO.isUndef());
650       BuildMI(&MBB, DL, get(BccOpc)).addReg(RO.getReg(), Flags).addMBB(TBB);
651     }
652     return 1;
653   }
654   assert((!Cond.empty()) &&
655          "Cond. cannot be empty when multiple branchings are required");
656   assert((!isNewValueJump(Cond[0].getImm())) &&
657          "NV-jump cannot be inserted with another branch");
658   // Special case for hardware loops.  The condition is a basic block.
659   if (isEndLoopN(Cond[0].getImm())) {
660     int EndLoopOp = Cond[0].getImm();
661     assert(Cond[1].isMBB());
662     // Since we're adding an ENDLOOP, there better be a LOOP instruction.
663     // Check for it, and change the BB target if needed.
664     SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
665     MachineInstr *Loop = findLoopInstr(TBB, EndLoopOp, Cond[1].getMBB(),
666                                        VisitedBBs);
667     assert(Loop != nullptr && "Inserting an ENDLOOP without a LOOP");
668     Loop->getOperand(0).setMBB(TBB);
669     // Add the ENDLOOP after the finding the LOOP0.
670     BuildMI(&MBB, DL, get(EndLoopOp)).addMBB(TBB);
671   } else {
672     const MachineOperand &RO = Cond[1];
673     unsigned Flags = getUndefRegState(RO.isUndef());
674     BuildMI(&MBB, DL, get(BccOpc)).addReg(RO.getReg(), Flags).addMBB(TBB);
675   }
676   BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
677 
678   return 2;
679 }
680 
681 namespace {
682 class HexagonPipelinerLoopInfo : public TargetInstrInfo::PipelinerLoopInfo {
683   MachineInstr *Loop, *EndLoop;
684   MachineFunction *MF;
685   const HexagonInstrInfo *TII;
686   int64_t TripCount;
687   Register LoopCount;
688   DebugLoc DL;
689 
690 public:
691   HexagonPipelinerLoopInfo(MachineInstr *Loop, MachineInstr *EndLoop)
692       : Loop(Loop), EndLoop(EndLoop), MF(Loop->getParent()->getParent()),
693         TII(MF->getSubtarget<HexagonSubtarget>().getInstrInfo()),
694         DL(Loop->getDebugLoc()) {
695     // Inspect the Loop instruction up-front, as it may be deleted when we call
696     // createTripCountGreaterCondition.
697     TripCount = Loop->getOpcode() == Hexagon::J2_loop0r
698                     ? -1
699                     : Loop->getOperand(1).getImm();
700     if (TripCount == -1)
701       LoopCount = Loop->getOperand(1).getReg();
702   }
703 
704   bool shouldIgnoreForPipelining(const MachineInstr *MI) const override {
705     // Only ignore the terminator.
706     return MI == EndLoop;
707   }
708 
709   Optional<bool>
710   createTripCountGreaterCondition(int TC, MachineBasicBlock &MBB,
711                                   SmallVectorImpl<MachineOperand> &Cond) override {
712     if (TripCount == -1) {
713       // Check if we're done with the loop.
714       unsigned Done = TII->createVR(MF, MVT::i1);
715       MachineInstr *NewCmp = BuildMI(&MBB, DL,
716                                      TII->get(Hexagon::C2_cmpgtui), Done)
717                                  .addReg(LoopCount)
718                                  .addImm(TC);
719       Cond.push_back(MachineOperand::CreateImm(Hexagon::J2_jumpf));
720       Cond.push_back(NewCmp->getOperand(0));
721       return {};
722     }
723 
724     return TripCount > TC;
725   }
726 
727   void setPreheader(MachineBasicBlock *NewPreheader) override {
728     NewPreheader->splice(NewPreheader->getFirstTerminator(), Loop->getParent(),
729                          Loop);
730   }
731 
732   void adjustTripCount(int TripCountAdjust) override {
733     // If the loop trip count is a compile-time value, then just change the
734     // value.
735     if (Loop->getOpcode() == Hexagon::J2_loop0i ||
736         Loop->getOpcode() == Hexagon::J2_loop1i) {
737       int64_t TripCount = Loop->getOperand(1).getImm() + TripCountAdjust;
738       assert(TripCount > 0 && "Can't create an empty or negative loop!");
739       Loop->getOperand(1).setImm(TripCount);
740       return;
741     }
742 
743     // The loop trip count is a run-time value. We generate code to subtract
744     // one from the trip count, and update the loop instruction.
745     Register LoopCount = Loop->getOperand(1).getReg();
746     Register NewLoopCount = TII->createVR(MF, MVT::i32);
747     BuildMI(*Loop->getParent(), Loop, Loop->getDebugLoc(),
748             TII->get(Hexagon::A2_addi), NewLoopCount)
749         .addReg(LoopCount)
750         .addImm(TripCountAdjust);
751     Loop->getOperand(1).setReg(NewLoopCount);
752   }
753 
754   void disposed() override { Loop->eraseFromParent(); }
755 };
756 } // namespace
757 
758 std::unique_ptr<TargetInstrInfo::PipelinerLoopInfo>
759 HexagonInstrInfo::analyzeLoopForPipelining(MachineBasicBlock *LoopBB) const {
760   // We really "analyze" only hardware loops right now.
761   MachineBasicBlock::iterator I = LoopBB->getFirstTerminator();
762 
763   if (I != LoopBB->end() && isEndLoopN(I->getOpcode())) {
764     SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
765     MachineInstr *LoopInst = findLoopInstr(
766         LoopBB, I->getOpcode(), I->getOperand(0).getMBB(), VisitedBBs);
767     if (LoopInst)
768       return std::make_unique<HexagonPipelinerLoopInfo>(LoopInst, &*I);
769   }
770   return nullptr;
771 }
772 
773 bool HexagonInstrInfo::isProfitableToIfCvt(MachineBasicBlock &MBB,
774       unsigned NumCycles, unsigned ExtraPredCycles,
775       BranchProbability Probability) const {
776   return nonDbgBBSize(&MBB) <= 3;
777 }
778 
779 bool HexagonInstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB,
780       unsigned NumTCycles, unsigned ExtraTCycles, MachineBasicBlock &FMBB,
781       unsigned NumFCycles, unsigned ExtraFCycles, BranchProbability Probability)
782       const {
783   return nonDbgBBSize(&TMBB) <= 3 && nonDbgBBSize(&FMBB) <= 3;
784 }
785 
786 bool HexagonInstrInfo::isProfitableToDupForIfCvt(MachineBasicBlock &MBB,
787       unsigned NumInstrs, BranchProbability Probability) const {
788   return NumInstrs <= 4;
789 }
790 
791 static void getLiveInRegsAt(LivePhysRegs &Regs, const MachineInstr &MI) {
792   SmallVector<std::pair<MCPhysReg, const MachineOperand*>,2> Clobbers;
793   const MachineBasicBlock &B = *MI.getParent();
794   Regs.addLiveIns(B);
795   auto E = MachineBasicBlock::const_iterator(MI.getIterator());
796   for (auto I = B.begin(); I != E; ++I) {
797     Clobbers.clear();
798     Regs.stepForward(*I, Clobbers);
799   }
800 }
801 
802 static void getLiveOutRegsAt(LivePhysRegs &Regs, const MachineInstr &MI) {
803   const MachineBasicBlock &B = *MI.getParent();
804   Regs.addLiveOuts(B);
805   auto E = ++MachineBasicBlock::const_iterator(MI.getIterator()).getReverse();
806   for (auto I = B.rbegin(); I != E; ++I)
807     Regs.stepBackward(*I);
808 }
809 
810 void HexagonInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
811                                    MachineBasicBlock::iterator I,
812                                    const DebugLoc &DL, MCRegister DestReg,
813                                    MCRegister SrcReg, bool KillSrc) const {
814   const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
815   unsigned KillFlag = getKillRegState(KillSrc);
816 
817   if (Hexagon::IntRegsRegClass.contains(SrcReg, DestReg)) {
818     BuildMI(MBB, I, DL, get(Hexagon::A2_tfr), DestReg)
819       .addReg(SrcReg, KillFlag);
820     return;
821   }
822   if (Hexagon::DoubleRegsRegClass.contains(SrcReg, DestReg)) {
823     BuildMI(MBB, I, DL, get(Hexagon::A2_tfrp), DestReg)
824       .addReg(SrcReg, KillFlag);
825     return;
826   }
827   if (Hexagon::PredRegsRegClass.contains(SrcReg, DestReg)) {
828     // Map Pd = Ps to Pd = or(Ps, Ps).
829     BuildMI(MBB, I, DL, get(Hexagon::C2_or), DestReg)
830       .addReg(SrcReg).addReg(SrcReg, KillFlag);
831     return;
832   }
833   if (Hexagon::CtrRegsRegClass.contains(DestReg) &&
834       Hexagon::IntRegsRegClass.contains(SrcReg)) {
835     BuildMI(MBB, I, DL, get(Hexagon::A2_tfrrcr), DestReg)
836       .addReg(SrcReg, KillFlag);
837     return;
838   }
839   if (Hexagon::IntRegsRegClass.contains(DestReg) &&
840       Hexagon::CtrRegsRegClass.contains(SrcReg)) {
841     BuildMI(MBB, I, DL, get(Hexagon::A2_tfrcrr), DestReg)
842       .addReg(SrcReg, KillFlag);
843     return;
844   }
845   if (Hexagon::ModRegsRegClass.contains(DestReg) &&
846       Hexagon::IntRegsRegClass.contains(SrcReg)) {
847     BuildMI(MBB, I, DL, get(Hexagon::A2_tfrrcr), DestReg)
848       .addReg(SrcReg, KillFlag);
849     return;
850   }
851   if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
852       Hexagon::IntRegsRegClass.contains(DestReg)) {
853     BuildMI(MBB, I, DL, get(Hexagon::C2_tfrpr), DestReg)
854       .addReg(SrcReg, KillFlag);
855     return;
856   }
857   if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
858       Hexagon::PredRegsRegClass.contains(DestReg)) {
859     BuildMI(MBB, I, DL, get(Hexagon::C2_tfrrp), DestReg)
860       .addReg(SrcReg, KillFlag);
861     return;
862   }
863   if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
864       Hexagon::IntRegsRegClass.contains(DestReg)) {
865     BuildMI(MBB, I, DL, get(Hexagon::C2_tfrpr), DestReg)
866       .addReg(SrcReg, KillFlag);
867     return;
868   }
869   if (Hexagon::HvxVRRegClass.contains(SrcReg, DestReg)) {
870     BuildMI(MBB, I, DL, get(Hexagon::V6_vassign), DestReg).
871       addReg(SrcReg, KillFlag);
872     return;
873   }
874   if (Hexagon::HvxWRRegClass.contains(SrcReg, DestReg)) {
875     LivePhysRegs LiveAtMI(HRI);
876     getLiveInRegsAt(LiveAtMI, *I);
877     Register SrcLo = HRI.getSubReg(SrcReg, Hexagon::vsub_lo);
878     Register SrcHi = HRI.getSubReg(SrcReg, Hexagon::vsub_hi);
879     unsigned UndefLo = getUndefRegState(!LiveAtMI.contains(SrcLo));
880     unsigned UndefHi = getUndefRegState(!LiveAtMI.contains(SrcHi));
881     BuildMI(MBB, I, DL, get(Hexagon::V6_vcombine), DestReg)
882       .addReg(SrcHi, KillFlag | UndefHi)
883       .addReg(SrcLo, KillFlag | UndefLo);
884     return;
885   }
886   if (Hexagon::HvxQRRegClass.contains(SrcReg, DestReg)) {
887     BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and), DestReg)
888       .addReg(SrcReg)
889       .addReg(SrcReg, KillFlag);
890     return;
891   }
892   if (Hexagon::HvxQRRegClass.contains(SrcReg) &&
893       Hexagon::HvxVRRegClass.contains(DestReg)) {
894     llvm_unreachable("Unimplemented pred to vec");
895     return;
896   }
897   if (Hexagon::HvxQRRegClass.contains(DestReg) &&
898       Hexagon::HvxVRRegClass.contains(SrcReg)) {
899     llvm_unreachable("Unimplemented vec to pred");
900     return;
901   }
902 
903 #ifndef NDEBUG
904   // Show the invalid registers to ease debugging.
905   dbgs() << "Invalid registers for copy in " << printMBBReference(MBB) << ": "
906          << printReg(DestReg, &HRI) << " = " << printReg(SrcReg, &HRI) << '\n';
907 #endif
908   llvm_unreachable("Unimplemented");
909 }
910 
911 void HexagonInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
912       MachineBasicBlock::iterator I, Register SrcReg, bool isKill, int FI,
913       const TargetRegisterClass *RC, const TargetRegisterInfo *TRI) const {
914   DebugLoc DL = MBB.findDebugLoc(I);
915   MachineFunction &MF = *MBB.getParent();
916   MachineFrameInfo &MFI = MF.getFrameInfo();
917   unsigned KillFlag = getKillRegState(isKill);
918 
919   MachineMemOperand *MMO = MF.getMachineMemOperand(
920       MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOStore,
921       MFI.getObjectSize(FI), MFI.getObjectAlign(FI));
922 
923   if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
924     BuildMI(MBB, I, DL, get(Hexagon::S2_storeri_io))
925       .addFrameIndex(FI).addImm(0)
926       .addReg(SrcReg, KillFlag).addMemOperand(MMO);
927   } else if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC)) {
928     BuildMI(MBB, I, DL, get(Hexagon::S2_storerd_io))
929       .addFrameIndex(FI).addImm(0)
930       .addReg(SrcReg, KillFlag).addMemOperand(MMO);
931   } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
932     BuildMI(MBB, I, DL, get(Hexagon::STriw_pred))
933       .addFrameIndex(FI).addImm(0)
934       .addReg(SrcReg, KillFlag).addMemOperand(MMO);
935   } else if (Hexagon::ModRegsRegClass.hasSubClassEq(RC)) {
936     BuildMI(MBB, I, DL, get(Hexagon::STriw_ctr))
937       .addFrameIndex(FI).addImm(0)
938       .addReg(SrcReg, KillFlag).addMemOperand(MMO);
939   } else if (Hexagon::HvxQRRegClass.hasSubClassEq(RC)) {
940     BuildMI(MBB, I, DL, get(Hexagon::PS_vstorerq_ai))
941       .addFrameIndex(FI).addImm(0)
942       .addReg(SrcReg, KillFlag).addMemOperand(MMO);
943   } else if (Hexagon::HvxVRRegClass.hasSubClassEq(RC)) {
944     BuildMI(MBB, I, DL, get(Hexagon::PS_vstorerv_ai))
945       .addFrameIndex(FI).addImm(0)
946       .addReg(SrcReg, KillFlag).addMemOperand(MMO);
947   } else if (Hexagon::HvxWRRegClass.hasSubClassEq(RC)) {
948     BuildMI(MBB, I, DL, get(Hexagon::PS_vstorerw_ai))
949       .addFrameIndex(FI).addImm(0)
950       .addReg(SrcReg, KillFlag).addMemOperand(MMO);
951   } else {
952     llvm_unreachable("Unimplemented");
953   }
954 }
955 
956 void HexagonInstrInfo::loadRegFromStackSlot(
957     MachineBasicBlock &MBB, MachineBasicBlock::iterator I, Register DestReg,
958     int FI, const TargetRegisterClass *RC,
959     const TargetRegisterInfo *TRI) const {
960   DebugLoc DL = MBB.findDebugLoc(I);
961   MachineFunction &MF = *MBB.getParent();
962   MachineFrameInfo &MFI = MF.getFrameInfo();
963 
964   MachineMemOperand *MMO = MF.getMachineMemOperand(
965       MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOLoad,
966       MFI.getObjectSize(FI), MFI.getObjectAlign(FI));
967 
968   if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
969     BuildMI(MBB, I, DL, get(Hexagon::L2_loadri_io), DestReg)
970       .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
971   } else if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC)) {
972     BuildMI(MBB, I, DL, get(Hexagon::L2_loadrd_io), DestReg)
973       .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
974   } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
975     BuildMI(MBB, I, DL, get(Hexagon::LDriw_pred), DestReg)
976       .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
977   } else if (Hexagon::ModRegsRegClass.hasSubClassEq(RC)) {
978     BuildMI(MBB, I, DL, get(Hexagon::LDriw_ctr), DestReg)
979       .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
980   } else if (Hexagon::HvxQRRegClass.hasSubClassEq(RC)) {
981     BuildMI(MBB, I, DL, get(Hexagon::PS_vloadrq_ai), DestReg)
982       .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
983   } else if (Hexagon::HvxVRRegClass.hasSubClassEq(RC)) {
984     BuildMI(MBB, I, DL, get(Hexagon::PS_vloadrv_ai), DestReg)
985       .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
986   } else if (Hexagon::HvxWRRegClass.hasSubClassEq(RC)) {
987     BuildMI(MBB, I, DL, get(Hexagon::PS_vloadrw_ai), DestReg)
988       .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
989   } else {
990     llvm_unreachable("Can't store this register to stack slot");
991   }
992 }
993 
994 /// expandPostRAPseudo - This function is called for all pseudo instructions
995 /// that remain after register allocation. Many pseudo instructions are
996 /// created to help register allocation. This is the place to convert them
997 /// into real instructions. The target can edit MI in place, or it can insert
998 /// new instructions and erase MI. The function should return true if
999 /// anything was changed.
1000 bool HexagonInstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
1001   MachineBasicBlock &MBB = *MI.getParent();
1002   MachineFunction &MF = *MBB.getParent();
1003   MachineRegisterInfo &MRI = MF.getRegInfo();
1004   const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
1005   LivePhysRegs LiveIn(HRI), LiveOut(HRI);
1006   DebugLoc DL = MI.getDebugLoc();
1007   unsigned Opc = MI.getOpcode();
1008 
1009   auto RealCirc = [&](unsigned Opc, bool HasImm, unsigned MxOp) {
1010     Register Mx = MI.getOperand(MxOp).getReg();
1011     unsigned CSx = (Mx == Hexagon::M0 ? Hexagon::CS0 : Hexagon::CS1);
1012     BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrrcr), CSx)
1013         .add(MI.getOperand((HasImm ? 5 : 4)));
1014     auto MIB = BuildMI(MBB, MI, DL, get(Opc)).add(MI.getOperand(0))
1015         .add(MI.getOperand(1)).add(MI.getOperand(2)).add(MI.getOperand(3));
1016     if (HasImm)
1017       MIB.add(MI.getOperand(4));
1018     MIB.addReg(CSx, RegState::Implicit);
1019     MBB.erase(MI);
1020     return true;
1021   };
1022 
1023   auto UseAligned = [&](const MachineInstr &MI, Align NeedAlign) {
1024     if (MI.memoperands().empty())
1025       return false;
1026     return all_of(MI.memoperands(), [NeedAlign](const MachineMemOperand *MMO) {
1027       return MMO->getAlign() >= NeedAlign;
1028     });
1029   };
1030 
1031   switch (Opc) {
1032     case TargetOpcode::COPY: {
1033       MachineOperand &MD = MI.getOperand(0);
1034       MachineOperand &MS = MI.getOperand(1);
1035       MachineBasicBlock::iterator MBBI = MI.getIterator();
1036       if (MD.getReg() != MS.getReg() && !MS.isUndef()) {
1037         copyPhysReg(MBB, MI, DL, MD.getReg(), MS.getReg(), MS.isKill());
1038         std::prev(MBBI)->copyImplicitOps(*MBB.getParent(), MI);
1039       }
1040       MBB.erase(MBBI);
1041       return true;
1042     }
1043     case Hexagon::PS_aligna:
1044       BuildMI(MBB, MI, DL, get(Hexagon::A2_andir), MI.getOperand(0).getReg())
1045           .addReg(HRI.getFrameRegister())
1046           .addImm(-MI.getOperand(1).getImm());
1047       MBB.erase(MI);
1048       return true;
1049     case Hexagon::V6_vassignp: {
1050       Register SrcReg = MI.getOperand(1).getReg();
1051       Register DstReg = MI.getOperand(0).getReg();
1052       Register SrcLo = HRI.getSubReg(SrcReg, Hexagon::vsub_lo);
1053       Register SrcHi = HRI.getSubReg(SrcReg, Hexagon::vsub_hi);
1054       getLiveInRegsAt(LiveIn, MI);
1055       unsigned UndefLo = getUndefRegState(!LiveIn.contains(SrcLo));
1056       unsigned UndefHi = getUndefRegState(!LiveIn.contains(SrcHi));
1057       unsigned Kill = getKillRegState(MI.getOperand(1).isKill());
1058       BuildMI(MBB, MI, DL, get(Hexagon::V6_vcombine), DstReg)
1059           .addReg(SrcHi, UndefHi)
1060           .addReg(SrcLo, Kill | UndefLo);
1061       MBB.erase(MI);
1062       return true;
1063     }
1064     case Hexagon::V6_lo: {
1065       Register SrcReg = MI.getOperand(1).getReg();
1066       Register DstReg = MI.getOperand(0).getReg();
1067       Register SrcSubLo = HRI.getSubReg(SrcReg, Hexagon::vsub_lo);
1068       copyPhysReg(MBB, MI, DL, DstReg, SrcSubLo, MI.getOperand(1).isKill());
1069       MBB.erase(MI);
1070       MRI.clearKillFlags(SrcSubLo);
1071       return true;
1072     }
1073     case Hexagon::V6_hi: {
1074       Register SrcReg = MI.getOperand(1).getReg();
1075       Register DstReg = MI.getOperand(0).getReg();
1076       Register SrcSubHi = HRI.getSubReg(SrcReg, Hexagon::vsub_hi);
1077       copyPhysReg(MBB, MI, DL, DstReg, SrcSubHi, MI.getOperand(1).isKill());
1078       MBB.erase(MI);
1079       MRI.clearKillFlags(SrcSubHi);
1080       return true;
1081     }
1082     case Hexagon::PS_vloadrv_ai: {
1083       Register DstReg = MI.getOperand(0).getReg();
1084       const MachineOperand &BaseOp = MI.getOperand(1);
1085       assert(BaseOp.getSubReg() == 0);
1086       int Offset = MI.getOperand(2).getImm();
1087       Align NeedAlign = HRI.getSpillAlign(Hexagon::HvxVRRegClass);
1088       unsigned NewOpc = UseAligned(MI, NeedAlign) ? Hexagon::V6_vL32b_ai
1089                                                   : Hexagon::V6_vL32Ub_ai;
1090       BuildMI(MBB, MI, DL, get(NewOpc), DstReg)
1091           .addReg(BaseOp.getReg(), getRegState(BaseOp))
1092           .addImm(Offset)
1093           .cloneMemRefs(MI);
1094       MBB.erase(MI);
1095       return true;
1096     }
1097     case Hexagon::PS_vloadrw_ai: {
1098       Register DstReg = MI.getOperand(0).getReg();
1099       const MachineOperand &BaseOp = MI.getOperand(1);
1100       assert(BaseOp.getSubReg() == 0);
1101       int Offset = MI.getOperand(2).getImm();
1102       unsigned VecOffset = HRI.getSpillSize(Hexagon::HvxVRRegClass);
1103       Align NeedAlign = HRI.getSpillAlign(Hexagon::HvxVRRegClass);
1104       unsigned NewOpc = UseAligned(MI, NeedAlign) ? Hexagon::V6_vL32b_ai
1105                                                   : Hexagon::V6_vL32Ub_ai;
1106       BuildMI(MBB, MI, DL, get(NewOpc),
1107               HRI.getSubReg(DstReg, Hexagon::vsub_lo))
1108           .addReg(BaseOp.getReg(), getRegState(BaseOp) & ~RegState::Kill)
1109           .addImm(Offset)
1110           .cloneMemRefs(MI);
1111       BuildMI(MBB, MI, DL, get(NewOpc),
1112               HRI.getSubReg(DstReg, Hexagon::vsub_hi))
1113           .addReg(BaseOp.getReg(), getRegState(BaseOp))
1114           .addImm(Offset + VecOffset)
1115           .cloneMemRefs(MI);
1116       MBB.erase(MI);
1117       return true;
1118     }
1119     case Hexagon::PS_vstorerv_ai: {
1120       const MachineOperand &SrcOp = MI.getOperand(2);
1121       assert(SrcOp.getSubReg() == 0);
1122       const MachineOperand &BaseOp = MI.getOperand(0);
1123       assert(BaseOp.getSubReg() == 0);
1124       int Offset = MI.getOperand(1).getImm();
1125       Align NeedAlign = HRI.getSpillAlign(Hexagon::HvxVRRegClass);
1126       unsigned NewOpc = UseAligned(MI, NeedAlign) ? Hexagon::V6_vS32b_ai
1127                                                   : Hexagon::V6_vS32Ub_ai;
1128       BuildMI(MBB, MI, DL, get(NewOpc))
1129           .addReg(BaseOp.getReg(), getRegState(BaseOp))
1130           .addImm(Offset)
1131           .addReg(SrcOp.getReg(), getRegState(SrcOp))
1132           .cloneMemRefs(MI);
1133       MBB.erase(MI);
1134       return true;
1135     }
1136     case Hexagon::PS_vstorerw_ai: {
1137       Register SrcReg = MI.getOperand(2).getReg();
1138       const MachineOperand &BaseOp = MI.getOperand(0);
1139       assert(BaseOp.getSubReg() == 0);
1140       int Offset = MI.getOperand(1).getImm();
1141       unsigned VecOffset = HRI.getSpillSize(Hexagon::HvxVRRegClass);
1142       Align NeedAlign = HRI.getSpillAlign(Hexagon::HvxVRRegClass);
1143       unsigned NewOpc = UseAligned(MI, NeedAlign) ? Hexagon::V6_vS32b_ai
1144                                                   : Hexagon::V6_vS32Ub_ai;
1145       BuildMI(MBB, MI, DL, get(NewOpc))
1146           .addReg(BaseOp.getReg(), getRegState(BaseOp) & ~RegState::Kill)
1147           .addImm(Offset)
1148           .addReg(HRI.getSubReg(SrcReg, Hexagon::vsub_lo))
1149           .cloneMemRefs(MI);
1150       BuildMI(MBB, MI, DL, get(NewOpc))
1151           .addReg(BaseOp.getReg(), getRegState(BaseOp))
1152           .addImm(Offset + VecOffset)
1153           .addReg(HRI.getSubReg(SrcReg, Hexagon::vsub_hi))
1154           .cloneMemRefs(MI);
1155       MBB.erase(MI);
1156       return true;
1157     }
1158     case Hexagon::PS_true: {
1159       Register Reg = MI.getOperand(0).getReg();
1160       BuildMI(MBB, MI, DL, get(Hexagon::C2_orn), Reg)
1161         .addReg(Reg, RegState::Undef)
1162         .addReg(Reg, RegState::Undef);
1163       MBB.erase(MI);
1164       return true;
1165     }
1166     case Hexagon::PS_false: {
1167       Register Reg = MI.getOperand(0).getReg();
1168       BuildMI(MBB, MI, DL, get(Hexagon::C2_andn), Reg)
1169         .addReg(Reg, RegState::Undef)
1170         .addReg(Reg, RegState::Undef);
1171       MBB.erase(MI);
1172       return true;
1173     }
1174     case Hexagon::PS_qtrue: {
1175       BuildMI(MBB, MI, DL, get(Hexagon::V6_veqw), MI.getOperand(0).getReg())
1176         .addReg(Hexagon::V0, RegState::Undef)
1177         .addReg(Hexagon::V0, RegState::Undef);
1178       MBB.erase(MI);
1179       return true;
1180     }
1181     case Hexagon::PS_qfalse: {
1182       BuildMI(MBB, MI, DL, get(Hexagon::V6_vgtw), MI.getOperand(0).getReg())
1183         .addReg(Hexagon::V0, RegState::Undef)
1184         .addReg(Hexagon::V0, RegState::Undef);
1185       MBB.erase(MI);
1186       return true;
1187     }
1188     case Hexagon::PS_vdd0: {
1189       Register Vd = MI.getOperand(0).getReg();
1190       BuildMI(MBB, MI, DL, get(Hexagon::V6_vsubw_dv), Vd)
1191         .addReg(Vd, RegState::Undef)
1192         .addReg(Vd, RegState::Undef);
1193       MBB.erase(MI);
1194       return true;
1195     }
1196     case Hexagon::PS_vmulw: {
1197       // Expand a 64-bit vector multiply into 2 32-bit scalar multiplies.
1198       Register DstReg = MI.getOperand(0).getReg();
1199       Register Src1Reg = MI.getOperand(1).getReg();
1200       Register Src2Reg = MI.getOperand(2).getReg();
1201       Register Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::isub_hi);
1202       Register Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::isub_lo);
1203       Register Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::isub_hi);
1204       Register Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::isub_lo);
1205       BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_mpyi),
1206               HRI.getSubReg(DstReg, Hexagon::isub_hi))
1207           .addReg(Src1SubHi)
1208           .addReg(Src2SubHi);
1209       BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_mpyi),
1210               HRI.getSubReg(DstReg, Hexagon::isub_lo))
1211           .addReg(Src1SubLo)
1212           .addReg(Src2SubLo);
1213       MBB.erase(MI);
1214       MRI.clearKillFlags(Src1SubHi);
1215       MRI.clearKillFlags(Src1SubLo);
1216       MRI.clearKillFlags(Src2SubHi);
1217       MRI.clearKillFlags(Src2SubLo);
1218       return true;
1219     }
1220     case Hexagon::PS_vmulw_acc: {
1221       // Expand 64-bit vector multiply with addition into 2 scalar multiplies.
1222       Register DstReg = MI.getOperand(0).getReg();
1223       Register Src1Reg = MI.getOperand(1).getReg();
1224       Register Src2Reg = MI.getOperand(2).getReg();
1225       Register Src3Reg = MI.getOperand(3).getReg();
1226       Register Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::isub_hi);
1227       Register Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::isub_lo);
1228       Register Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::isub_hi);
1229       Register Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::isub_lo);
1230       Register Src3SubHi = HRI.getSubReg(Src3Reg, Hexagon::isub_hi);
1231       Register Src3SubLo = HRI.getSubReg(Src3Reg, Hexagon::isub_lo);
1232       BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_maci),
1233               HRI.getSubReg(DstReg, Hexagon::isub_hi))
1234           .addReg(Src1SubHi)
1235           .addReg(Src2SubHi)
1236           .addReg(Src3SubHi);
1237       BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_maci),
1238               HRI.getSubReg(DstReg, Hexagon::isub_lo))
1239           .addReg(Src1SubLo)
1240           .addReg(Src2SubLo)
1241           .addReg(Src3SubLo);
1242       MBB.erase(MI);
1243       MRI.clearKillFlags(Src1SubHi);
1244       MRI.clearKillFlags(Src1SubLo);
1245       MRI.clearKillFlags(Src2SubHi);
1246       MRI.clearKillFlags(Src2SubLo);
1247       MRI.clearKillFlags(Src3SubHi);
1248       MRI.clearKillFlags(Src3SubLo);
1249       return true;
1250     }
1251     case Hexagon::PS_pselect: {
1252       const MachineOperand &Op0 = MI.getOperand(0);
1253       const MachineOperand &Op1 = MI.getOperand(1);
1254       const MachineOperand &Op2 = MI.getOperand(2);
1255       const MachineOperand &Op3 = MI.getOperand(3);
1256       Register Rd = Op0.getReg();
1257       Register Pu = Op1.getReg();
1258       Register Rs = Op2.getReg();
1259       Register Rt = Op3.getReg();
1260       DebugLoc DL = MI.getDebugLoc();
1261       unsigned K1 = getKillRegState(Op1.isKill());
1262       unsigned K2 = getKillRegState(Op2.isKill());
1263       unsigned K3 = getKillRegState(Op3.isKill());
1264       if (Rd != Rs)
1265         BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpt), Rd)
1266           .addReg(Pu, (Rd == Rt) ? K1 : 0)
1267           .addReg(Rs, K2);
1268       if (Rd != Rt)
1269         BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpf), Rd)
1270           .addReg(Pu, K1)
1271           .addReg(Rt, K3);
1272       MBB.erase(MI);
1273       return true;
1274     }
1275     case Hexagon::PS_vselect: {
1276       const MachineOperand &Op0 = MI.getOperand(0);
1277       const MachineOperand &Op1 = MI.getOperand(1);
1278       const MachineOperand &Op2 = MI.getOperand(2);
1279       const MachineOperand &Op3 = MI.getOperand(3);
1280       getLiveOutRegsAt(LiveOut, MI);
1281       bool IsDestLive = !LiveOut.available(MRI, Op0.getReg());
1282       Register PReg = Op1.getReg();
1283       assert(Op1.getSubReg() == 0);
1284       unsigned PState = getRegState(Op1);
1285 
1286       if (Op0.getReg() != Op2.getReg()) {
1287         unsigned S = Op0.getReg() != Op3.getReg() ? PState & ~RegState::Kill
1288                                                   : PState;
1289         auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vcmov))
1290                      .add(Op0)
1291                      .addReg(PReg, S)
1292                      .add(Op2);
1293         if (IsDestLive)
1294           T.addReg(Op0.getReg(), RegState::Implicit);
1295         IsDestLive = true;
1296       }
1297       if (Op0.getReg() != Op3.getReg()) {
1298         auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vncmov))
1299                      .add(Op0)
1300                      .addReg(PReg, PState)
1301                      .add(Op3);
1302         if (IsDestLive)
1303           T.addReg(Op0.getReg(), RegState::Implicit);
1304       }
1305       MBB.erase(MI);
1306       return true;
1307     }
1308     case Hexagon::PS_wselect: {
1309       MachineOperand &Op0 = MI.getOperand(0);
1310       MachineOperand &Op1 = MI.getOperand(1);
1311       MachineOperand &Op2 = MI.getOperand(2);
1312       MachineOperand &Op3 = MI.getOperand(3);
1313       getLiveOutRegsAt(LiveOut, MI);
1314       bool IsDestLive = !LiveOut.available(MRI, Op0.getReg());
1315       Register PReg = Op1.getReg();
1316       assert(Op1.getSubReg() == 0);
1317       unsigned PState = getRegState(Op1);
1318 
1319       if (Op0.getReg() != Op2.getReg()) {
1320         unsigned S = Op0.getReg() != Op3.getReg() ? PState & ~RegState::Kill
1321                                                   : PState;
1322         Register SrcLo = HRI.getSubReg(Op2.getReg(), Hexagon::vsub_lo);
1323         Register SrcHi = HRI.getSubReg(Op2.getReg(), Hexagon::vsub_hi);
1324         auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vccombine))
1325                      .add(Op0)
1326                      .addReg(PReg, S)
1327                      .addReg(SrcHi)
1328                      .addReg(SrcLo);
1329         if (IsDestLive)
1330           T.addReg(Op0.getReg(), RegState::Implicit);
1331         IsDestLive = true;
1332       }
1333       if (Op0.getReg() != Op3.getReg()) {
1334         Register SrcLo = HRI.getSubReg(Op3.getReg(), Hexagon::vsub_lo);
1335         Register SrcHi = HRI.getSubReg(Op3.getReg(), Hexagon::vsub_hi);
1336         auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vnccombine))
1337                      .add(Op0)
1338                      .addReg(PReg, PState)
1339                      .addReg(SrcHi)
1340                      .addReg(SrcLo);
1341         if (IsDestLive)
1342           T.addReg(Op0.getReg(), RegState::Implicit);
1343       }
1344       MBB.erase(MI);
1345       return true;
1346     }
1347 
1348     case Hexagon::PS_crash: {
1349       // Generate a misaligned load that is guaranteed to cause a crash.
1350       class CrashPseudoSourceValue : public PseudoSourceValue {
1351       public:
1352         CrashPseudoSourceValue(const TargetInstrInfo &TII)
1353           : PseudoSourceValue(TargetCustom, TII) {}
1354 
1355         bool isConstant(const MachineFrameInfo *) const override {
1356           return false;
1357         }
1358         bool isAliased(const MachineFrameInfo *) const override {
1359           return false;
1360         }
1361         bool mayAlias(const MachineFrameInfo *) const override {
1362           return false;
1363         }
1364         void printCustom(raw_ostream &OS) const override {
1365           OS << "MisalignedCrash";
1366         }
1367       };
1368 
1369       static const CrashPseudoSourceValue CrashPSV(*this);
1370       MachineMemOperand *MMO = MF.getMachineMemOperand(
1371           MachinePointerInfo(&CrashPSV),
1372           MachineMemOperand::MOLoad | MachineMemOperand::MOVolatile, 8,
1373           Align(1));
1374       BuildMI(MBB, MI, DL, get(Hexagon::PS_loadrdabs), Hexagon::D13)
1375         .addImm(0xBADC0FEE)  // Misaligned load.
1376         .addMemOperand(MMO);
1377       MBB.erase(MI);
1378       return true;
1379     }
1380 
1381     case Hexagon::PS_tailcall_i:
1382       MI.setDesc(get(Hexagon::J2_jump));
1383       return true;
1384     case Hexagon::PS_tailcall_r:
1385     case Hexagon::PS_jmpret:
1386       MI.setDesc(get(Hexagon::J2_jumpr));
1387       return true;
1388     case Hexagon::PS_jmprett:
1389       MI.setDesc(get(Hexagon::J2_jumprt));
1390       return true;
1391     case Hexagon::PS_jmpretf:
1392       MI.setDesc(get(Hexagon::J2_jumprf));
1393       return true;
1394     case Hexagon::PS_jmprettnewpt:
1395       MI.setDesc(get(Hexagon::J2_jumprtnewpt));
1396       return true;
1397     case Hexagon::PS_jmpretfnewpt:
1398       MI.setDesc(get(Hexagon::J2_jumprfnewpt));
1399       return true;
1400     case Hexagon::PS_jmprettnew:
1401       MI.setDesc(get(Hexagon::J2_jumprtnew));
1402       return true;
1403     case Hexagon::PS_jmpretfnew:
1404       MI.setDesc(get(Hexagon::J2_jumprfnew));
1405       return true;
1406 
1407     case Hexagon::PS_loadrub_pci:
1408       return RealCirc(Hexagon::L2_loadrub_pci, /*HasImm*/true,  /*MxOp*/4);
1409     case Hexagon::PS_loadrb_pci:
1410       return RealCirc(Hexagon::L2_loadrb_pci,  /*HasImm*/true,  /*MxOp*/4);
1411     case Hexagon::PS_loadruh_pci:
1412       return RealCirc(Hexagon::L2_loadruh_pci, /*HasImm*/true,  /*MxOp*/4);
1413     case Hexagon::PS_loadrh_pci:
1414       return RealCirc(Hexagon::L2_loadrh_pci,  /*HasImm*/true,  /*MxOp*/4);
1415     case Hexagon::PS_loadri_pci:
1416       return RealCirc(Hexagon::L2_loadri_pci,  /*HasImm*/true,  /*MxOp*/4);
1417     case Hexagon::PS_loadrd_pci:
1418       return RealCirc(Hexagon::L2_loadrd_pci,  /*HasImm*/true,  /*MxOp*/4);
1419     case Hexagon::PS_loadrub_pcr:
1420       return RealCirc(Hexagon::L2_loadrub_pcr, /*HasImm*/false, /*MxOp*/3);
1421     case Hexagon::PS_loadrb_pcr:
1422       return RealCirc(Hexagon::L2_loadrb_pcr,  /*HasImm*/false, /*MxOp*/3);
1423     case Hexagon::PS_loadruh_pcr:
1424       return RealCirc(Hexagon::L2_loadruh_pcr, /*HasImm*/false, /*MxOp*/3);
1425     case Hexagon::PS_loadrh_pcr:
1426       return RealCirc(Hexagon::L2_loadrh_pcr,  /*HasImm*/false, /*MxOp*/3);
1427     case Hexagon::PS_loadri_pcr:
1428       return RealCirc(Hexagon::L2_loadri_pcr,  /*HasImm*/false, /*MxOp*/3);
1429     case Hexagon::PS_loadrd_pcr:
1430       return RealCirc(Hexagon::L2_loadrd_pcr,  /*HasImm*/false, /*MxOp*/3);
1431     case Hexagon::PS_storerb_pci:
1432       return RealCirc(Hexagon::S2_storerb_pci, /*HasImm*/true,  /*MxOp*/3);
1433     case Hexagon::PS_storerh_pci:
1434       return RealCirc(Hexagon::S2_storerh_pci, /*HasImm*/true,  /*MxOp*/3);
1435     case Hexagon::PS_storerf_pci:
1436       return RealCirc(Hexagon::S2_storerf_pci, /*HasImm*/true,  /*MxOp*/3);
1437     case Hexagon::PS_storeri_pci:
1438       return RealCirc(Hexagon::S2_storeri_pci, /*HasImm*/true,  /*MxOp*/3);
1439     case Hexagon::PS_storerd_pci:
1440       return RealCirc(Hexagon::S2_storerd_pci, /*HasImm*/true,  /*MxOp*/3);
1441     case Hexagon::PS_storerb_pcr:
1442       return RealCirc(Hexagon::S2_storerb_pcr, /*HasImm*/false, /*MxOp*/2);
1443     case Hexagon::PS_storerh_pcr:
1444       return RealCirc(Hexagon::S2_storerh_pcr, /*HasImm*/false, /*MxOp*/2);
1445     case Hexagon::PS_storerf_pcr:
1446       return RealCirc(Hexagon::S2_storerf_pcr, /*HasImm*/false, /*MxOp*/2);
1447     case Hexagon::PS_storeri_pcr:
1448       return RealCirc(Hexagon::S2_storeri_pcr, /*HasImm*/false, /*MxOp*/2);
1449     case Hexagon::PS_storerd_pcr:
1450       return RealCirc(Hexagon::S2_storerd_pcr, /*HasImm*/false, /*MxOp*/2);
1451   }
1452 
1453   return false;
1454 }
1455 
1456 MachineBasicBlock::instr_iterator
1457 HexagonInstrInfo::expandVGatherPseudo(MachineInstr &MI) const {
1458   MachineBasicBlock &MBB = *MI.getParent();
1459   const DebugLoc &DL = MI.getDebugLoc();
1460   unsigned Opc = MI.getOpcode();
1461   MachineBasicBlock::iterator First;
1462 
1463   switch (Opc) {
1464     case Hexagon::V6_vgathermh_pseudo:
1465       First = BuildMI(MBB, MI, DL, get(Hexagon::V6_vgathermh))
1466                   .add(MI.getOperand(1))
1467                   .add(MI.getOperand(2))
1468                   .add(MI.getOperand(3));
1469       BuildMI(MBB, MI, DL, get(Hexagon::V6_vS32b_new_ai))
1470           .add(MI.getOperand(0))
1471           .addImm(0)
1472           .addReg(Hexagon::VTMP);
1473       MBB.erase(MI);
1474       return First.getInstrIterator();
1475 
1476     case Hexagon::V6_vgathermw_pseudo:
1477       First = BuildMI(MBB, MI, DL, get(Hexagon::V6_vgathermw))
1478                   .add(MI.getOperand(1))
1479                   .add(MI.getOperand(2))
1480                   .add(MI.getOperand(3));
1481       BuildMI(MBB, MI, DL, get(Hexagon::V6_vS32b_new_ai))
1482           .add(MI.getOperand(0))
1483           .addImm(0)
1484           .addReg(Hexagon::VTMP);
1485       MBB.erase(MI);
1486       return First.getInstrIterator();
1487 
1488     case Hexagon::V6_vgathermhw_pseudo:
1489       First = BuildMI(MBB, MI, DL, get(Hexagon::V6_vgathermhw))
1490                   .add(MI.getOperand(1))
1491                   .add(MI.getOperand(2))
1492                   .add(MI.getOperand(3));
1493       BuildMI(MBB, MI, DL, get(Hexagon::V6_vS32b_new_ai))
1494           .add(MI.getOperand(0))
1495           .addImm(0)
1496           .addReg(Hexagon::VTMP);
1497       MBB.erase(MI);
1498       return First.getInstrIterator();
1499 
1500     case Hexagon::V6_vgathermhq_pseudo:
1501       First = BuildMI(MBB, MI, DL, get(Hexagon::V6_vgathermhq))
1502                   .add(MI.getOperand(1))
1503                   .add(MI.getOperand(2))
1504                   .add(MI.getOperand(3))
1505                   .add(MI.getOperand(4));
1506       BuildMI(MBB, MI, DL, get(Hexagon::V6_vS32b_new_ai))
1507           .add(MI.getOperand(0))
1508           .addImm(0)
1509           .addReg(Hexagon::VTMP);
1510       MBB.erase(MI);
1511       return First.getInstrIterator();
1512 
1513     case Hexagon::V6_vgathermwq_pseudo:
1514       First = BuildMI(MBB, MI, DL, get(Hexagon::V6_vgathermwq))
1515                   .add(MI.getOperand(1))
1516                   .add(MI.getOperand(2))
1517                   .add(MI.getOperand(3))
1518                   .add(MI.getOperand(4));
1519       BuildMI(MBB, MI, DL, get(Hexagon::V6_vS32b_new_ai))
1520           .add(MI.getOperand(0))
1521           .addImm(0)
1522           .addReg(Hexagon::VTMP);
1523       MBB.erase(MI);
1524       return First.getInstrIterator();
1525 
1526     case Hexagon::V6_vgathermhwq_pseudo:
1527       First = BuildMI(MBB, MI, DL, get(Hexagon::V6_vgathermhwq))
1528                   .add(MI.getOperand(1))
1529                   .add(MI.getOperand(2))
1530                   .add(MI.getOperand(3))
1531                   .add(MI.getOperand(4));
1532       BuildMI(MBB, MI, DL, get(Hexagon::V6_vS32b_new_ai))
1533           .add(MI.getOperand(0))
1534           .addImm(0)
1535           .addReg(Hexagon::VTMP);
1536       MBB.erase(MI);
1537       return First.getInstrIterator();
1538   }
1539 
1540   return MI.getIterator();
1541 }
1542 
1543 // We indicate that we want to reverse the branch by
1544 // inserting the reversed branching opcode.
1545 bool HexagonInstrInfo::reverseBranchCondition(
1546       SmallVectorImpl<MachineOperand> &Cond) const {
1547   if (Cond.empty())
1548     return true;
1549   assert(Cond[0].isImm() && "First entry in the cond vector not imm-val");
1550   unsigned opcode = Cond[0].getImm();
1551   //unsigned temp;
1552   assert(get(opcode).isBranch() && "Should be a branching condition.");
1553   if (isEndLoopN(opcode))
1554     return true;
1555   unsigned NewOpcode = getInvertedPredicatedOpcode(opcode);
1556   Cond[0].setImm(NewOpcode);
1557   return false;
1558 }
1559 
1560 void HexagonInstrInfo::insertNoop(MachineBasicBlock &MBB,
1561       MachineBasicBlock::iterator MI) const {
1562   DebugLoc DL;
1563   BuildMI(MBB, MI, DL, get(Hexagon::A2_nop));
1564 }
1565 
1566 bool HexagonInstrInfo::isPostIncrement(const MachineInstr &MI) const {
1567   return getAddrMode(MI) == HexagonII::PostInc;
1568 }
1569 
1570 // Returns true if an instruction is predicated irrespective of the predicate
1571 // sense. For example, all of the following will return true.
1572 // if (p0) R1 = add(R2, R3)
1573 // if (!p0) R1 = add(R2, R3)
1574 // if (p0.new) R1 = add(R2, R3)
1575 // if (!p0.new) R1 = add(R2, R3)
1576 // Note: New-value stores are not included here as in the current
1577 // implementation, we don't need to check their predicate sense.
1578 bool HexagonInstrInfo::isPredicated(const MachineInstr &MI) const {
1579   const uint64_t F = MI.getDesc().TSFlags;
1580   return (F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask;
1581 }
1582 
1583 bool HexagonInstrInfo::PredicateInstruction(
1584     MachineInstr &MI, ArrayRef<MachineOperand> Cond) const {
1585   if (Cond.empty() || isNewValueJump(Cond[0].getImm()) ||
1586       isEndLoopN(Cond[0].getImm())) {
1587     LLVM_DEBUG(dbgs() << "\nCannot predicate:"; MI.dump(););
1588     return false;
1589   }
1590   int Opc = MI.getOpcode();
1591   assert (isPredicable(MI) && "Expected predicable instruction");
1592   bool invertJump = predOpcodeHasNot(Cond);
1593 
1594   // We have to predicate MI "in place", i.e. after this function returns,
1595   // MI will need to be transformed into a predicated form. To avoid com-
1596   // plicated manipulations with the operands (handling tied operands,
1597   // etc.), build a new temporary instruction, then overwrite MI with it.
1598 
1599   MachineBasicBlock &B = *MI.getParent();
1600   DebugLoc DL = MI.getDebugLoc();
1601   unsigned PredOpc = getCondOpcode(Opc, invertJump);
1602   MachineInstrBuilder T = BuildMI(B, MI, DL, get(PredOpc));
1603   unsigned NOp = 0, NumOps = MI.getNumOperands();
1604   while (NOp < NumOps) {
1605     MachineOperand &Op = MI.getOperand(NOp);
1606     if (!Op.isReg() || !Op.isDef() || Op.isImplicit())
1607       break;
1608     T.add(Op);
1609     NOp++;
1610   }
1611 
1612   unsigned PredReg, PredRegPos, PredRegFlags;
1613   bool GotPredReg = getPredReg(Cond, PredReg, PredRegPos, PredRegFlags);
1614   (void)GotPredReg;
1615   assert(GotPredReg);
1616   T.addReg(PredReg, PredRegFlags);
1617   while (NOp < NumOps)
1618     T.add(MI.getOperand(NOp++));
1619 
1620   MI.setDesc(get(PredOpc));
1621   while (unsigned n = MI.getNumOperands())
1622     MI.RemoveOperand(n-1);
1623   for (unsigned i = 0, n = T->getNumOperands(); i < n; ++i)
1624     MI.addOperand(T->getOperand(i));
1625 
1626   MachineBasicBlock::instr_iterator TI = T->getIterator();
1627   B.erase(TI);
1628 
1629   MachineRegisterInfo &MRI = B.getParent()->getRegInfo();
1630   MRI.clearKillFlags(PredReg);
1631   return true;
1632 }
1633 
1634 bool HexagonInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
1635       ArrayRef<MachineOperand> Pred2) const {
1636   // TODO: Fix this
1637   return false;
1638 }
1639 
1640 bool HexagonInstrInfo::ClobbersPredicate(MachineInstr &MI,
1641                                          std::vector<MachineOperand> &Pred,
1642                                          bool SkipDead) const {
1643   const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
1644 
1645   for (const MachineOperand &MO : MI.operands()) {
1646     if (MO.isReg()) {
1647       if (!MO.isDef())
1648         continue;
1649       const TargetRegisterClass* RC = HRI.getMinimalPhysRegClass(MO.getReg());
1650       if (RC == &Hexagon::PredRegsRegClass) {
1651         Pred.push_back(MO);
1652         return true;
1653       }
1654       continue;
1655     } else if (MO.isRegMask()) {
1656       for (unsigned PR : Hexagon::PredRegsRegClass) {
1657         if (!MI.modifiesRegister(PR, &HRI))
1658           continue;
1659         Pred.push_back(MO);
1660         return true;
1661       }
1662     }
1663   }
1664   return false;
1665 }
1666 
1667 bool HexagonInstrInfo::isPredicable(const MachineInstr &MI) const {
1668   if (!MI.getDesc().isPredicable())
1669     return false;
1670 
1671   if (MI.isCall() || isTailCall(MI)) {
1672     if (!Subtarget.usePredicatedCalls())
1673       return false;
1674   }
1675 
1676   // HVX loads are not predicable on v60, but are on v62.
1677   if (!Subtarget.hasV62Ops()) {
1678     switch (MI.getOpcode()) {
1679       case Hexagon::V6_vL32b_ai:
1680       case Hexagon::V6_vL32b_pi:
1681       case Hexagon::V6_vL32b_ppu:
1682       case Hexagon::V6_vL32b_cur_ai:
1683       case Hexagon::V6_vL32b_cur_pi:
1684       case Hexagon::V6_vL32b_cur_ppu:
1685       case Hexagon::V6_vL32b_nt_ai:
1686       case Hexagon::V6_vL32b_nt_pi:
1687       case Hexagon::V6_vL32b_nt_ppu:
1688       case Hexagon::V6_vL32b_tmp_ai:
1689       case Hexagon::V6_vL32b_tmp_pi:
1690       case Hexagon::V6_vL32b_tmp_ppu:
1691       case Hexagon::V6_vL32b_nt_cur_ai:
1692       case Hexagon::V6_vL32b_nt_cur_pi:
1693       case Hexagon::V6_vL32b_nt_cur_ppu:
1694       case Hexagon::V6_vL32b_nt_tmp_ai:
1695       case Hexagon::V6_vL32b_nt_tmp_pi:
1696       case Hexagon::V6_vL32b_nt_tmp_ppu:
1697         return false;
1698     }
1699   }
1700   return true;
1701 }
1702 
1703 bool HexagonInstrInfo::isSchedulingBoundary(const MachineInstr &MI,
1704                                             const MachineBasicBlock *MBB,
1705                                             const MachineFunction &MF) const {
1706   // Debug info is never a scheduling boundary. It's necessary to be explicit
1707   // due to the special treatment of IT instructions below, otherwise a
1708   // dbg_value followed by an IT will result in the IT instruction being
1709   // considered a scheduling hazard, which is wrong. It should be the actual
1710   // instruction preceding the dbg_value instruction(s), just like it is
1711   // when debug info is not present.
1712   if (MI.isDebugInstr())
1713     return false;
1714 
1715   // Throwing call is a boundary.
1716   if (MI.isCall()) {
1717     // Don't mess around with no return calls.
1718     if (doesNotReturn(MI))
1719       return true;
1720     // If any of the block's successors is a landing pad, this could be a
1721     // throwing call.
1722     for (auto I : MBB->successors())
1723       if (I->isEHPad())
1724         return true;
1725   }
1726 
1727   // Terminators and labels can't be scheduled around.
1728   if (MI.getDesc().isTerminator() || MI.isPosition())
1729     return true;
1730 
1731   // INLINEASM_BR can jump to another block
1732   if (MI.getOpcode() == TargetOpcode::INLINEASM_BR)
1733     return true;
1734 
1735   if (MI.isInlineAsm() && !ScheduleInlineAsm)
1736     return true;
1737 
1738   return false;
1739 }
1740 
1741 /// Measure the specified inline asm to determine an approximation of its
1742 /// length.
1743 /// Comments (which run till the next SeparatorString or newline) do not
1744 /// count as an instruction.
1745 /// Any other non-whitespace text is considered an instruction, with
1746 /// multiple instructions separated by SeparatorString or newlines.
1747 /// Variable-length instructions are not handled here; this function
1748 /// may be overloaded in the target code to do that.
1749 /// Hexagon counts the number of ##'s and adjust for that many
1750 /// constant exenders.
1751 unsigned HexagonInstrInfo::getInlineAsmLength(const char *Str,
1752                                               const MCAsmInfo &MAI,
1753                                               const TargetSubtargetInfo *STI) const {
1754   StringRef AStr(Str);
1755   // Count the number of instructions in the asm.
1756   bool atInsnStart = true;
1757   unsigned Length = 0;
1758   const unsigned MaxInstLength = MAI.getMaxInstLength(STI);
1759   for (; *Str; ++Str) {
1760     if (*Str == '\n' || strncmp(Str, MAI.getSeparatorString(),
1761                                 strlen(MAI.getSeparatorString())) == 0)
1762       atInsnStart = true;
1763     if (atInsnStart && !isSpace(static_cast<unsigned char>(*Str))) {
1764       Length += MaxInstLength;
1765       atInsnStart = false;
1766     }
1767     if (atInsnStart && strncmp(Str, MAI.getCommentString().data(),
1768                                MAI.getCommentString().size()) == 0)
1769       atInsnStart = false;
1770   }
1771 
1772   // Add to size number of constant extenders seen * 4.
1773   StringRef Occ("##");
1774   Length += AStr.count(Occ)*4;
1775   return Length;
1776 }
1777 
1778 ScheduleHazardRecognizer*
1779 HexagonInstrInfo::CreateTargetPostRAHazardRecognizer(
1780       const InstrItineraryData *II, const ScheduleDAG *DAG) const {
1781   if (UseDFAHazardRec)
1782     return new HexagonHazardRecognizer(II, this, Subtarget);
1783   return TargetInstrInfo::CreateTargetPostRAHazardRecognizer(II, DAG);
1784 }
1785 
1786 /// For a comparison instruction, return the source registers in
1787 /// \p SrcReg and \p SrcReg2 if having two register operands, and the value it
1788 /// compares against in CmpValue. Return true if the comparison instruction
1789 /// can be analyzed.
1790 bool HexagonInstrInfo::analyzeCompare(const MachineInstr &MI, Register &SrcReg,
1791                                       Register &SrcReg2, int64_t &Mask,
1792                                       int64_t &Value) const {
1793   unsigned Opc = MI.getOpcode();
1794 
1795   // Set mask and the first source register.
1796   switch (Opc) {
1797     case Hexagon::C2_cmpeq:
1798     case Hexagon::C2_cmpeqp:
1799     case Hexagon::C2_cmpgt:
1800     case Hexagon::C2_cmpgtp:
1801     case Hexagon::C2_cmpgtu:
1802     case Hexagon::C2_cmpgtup:
1803     case Hexagon::C4_cmpneq:
1804     case Hexagon::C4_cmplte:
1805     case Hexagon::C4_cmplteu:
1806     case Hexagon::C2_cmpeqi:
1807     case Hexagon::C2_cmpgti:
1808     case Hexagon::C2_cmpgtui:
1809     case Hexagon::C4_cmpneqi:
1810     case Hexagon::C4_cmplteui:
1811     case Hexagon::C4_cmpltei:
1812       SrcReg = MI.getOperand(1).getReg();
1813       Mask = ~0;
1814       break;
1815     case Hexagon::A4_cmpbeq:
1816     case Hexagon::A4_cmpbgt:
1817     case Hexagon::A4_cmpbgtu:
1818     case Hexagon::A4_cmpbeqi:
1819     case Hexagon::A4_cmpbgti:
1820     case Hexagon::A4_cmpbgtui:
1821       SrcReg = MI.getOperand(1).getReg();
1822       Mask = 0xFF;
1823       break;
1824     case Hexagon::A4_cmpheq:
1825     case Hexagon::A4_cmphgt:
1826     case Hexagon::A4_cmphgtu:
1827     case Hexagon::A4_cmpheqi:
1828     case Hexagon::A4_cmphgti:
1829     case Hexagon::A4_cmphgtui:
1830       SrcReg = MI.getOperand(1).getReg();
1831       Mask = 0xFFFF;
1832       break;
1833   }
1834 
1835   // Set the value/second source register.
1836   switch (Opc) {
1837     case Hexagon::C2_cmpeq:
1838     case Hexagon::C2_cmpeqp:
1839     case Hexagon::C2_cmpgt:
1840     case Hexagon::C2_cmpgtp:
1841     case Hexagon::C2_cmpgtu:
1842     case Hexagon::C2_cmpgtup:
1843     case Hexagon::A4_cmpbeq:
1844     case Hexagon::A4_cmpbgt:
1845     case Hexagon::A4_cmpbgtu:
1846     case Hexagon::A4_cmpheq:
1847     case Hexagon::A4_cmphgt:
1848     case Hexagon::A4_cmphgtu:
1849     case Hexagon::C4_cmpneq:
1850     case Hexagon::C4_cmplte:
1851     case Hexagon::C4_cmplteu:
1852       SrcReg2 = MI.getOperand(2).getReg();
1853       return true;
1854 
1855     case Hexagon::C2_cmpeqi:
1856     case Hexagon::C2_cmpgtui:
1857     case Hexagon::C2_cmpgti:
1858     case Hexagon::C4_cmpneqi:
1859     case Hexagon::C4_cmplteui:
1860     case Hexagon::C4_cmpltei:
1861     case Hexagon::A4_cmpbeqi:
1862     case Hexagon::A4_cmpbgti:
1863     case Hexagon::A4_cmpbgtui:
1864     case Hexagon::A4_cmpheqi:
1865     case Hexagon::A4_cmphgti:
1866     case Hexagon::A4_cmphgtui: {
1867       SrcReg2 = 0;
1868       const MachineOperand &Op2 = MI.getOperand(2);
1869       if (!Op2.isImm())
1870         return false;
1871       Value = MI.getOperand(2).getImm();
1872       return true;
1873     }
1874   }
1875 
1876   return false;
1877 }
1878 
1879 unsigned HexagonInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
1880                                            const MachineInstr &MI,
1881                                            unsigned *PredCost) const {
1882   return getInstrTimingClassLatency(ItinData, MI);
1883 }
1884 
1885 DFAPacketizer *HexagonInstrInfo::CreateTargetScheduleState(
1886     const TargetSubtargetInfo &STI) const {
1887   const InstrItineraryData *II = STI.getInstrItineraryData();
1888   return static_cast<const HexagonSubtarget&>(STI).createDFAPacketizer(II);
1889 }
1890 
1891 // Inspired by this pair:
1892 //  %r13 = L2_loadri_io %r29, 136; mem:LD4[FixedStack0]
1893 //  S2_storeri_io %r29, 132, killed %r1; flags:  mem:ST4[FixedStack1]
1894 // Currently AA considers the addresses in these instructions to be aliasing.
1895 bool HexagonInstrInfo::areMemAccessesTriviallyDisjoint(
1896     const MachineInstr &MIa, const MachineInstr &MIb) const {
1897   if (MIa.hasUnmodeledSideEffects() || MIb.hasUnmodeledSideEffects() ||
1898       MIa.hasOrderedMemoryRef() || MIb.hasOrderedMemoryRef())
1899     return false;
1900 
1901   // Instructions that are pure loads, not loads and stores like memops are not
1902   // dependent.
1903   if (MIa.mayLoad() && !isMemOp(MIa) && MIb.mayLoad() && !isMemOp(MIb))
1904     return true;
1905 
1906   // Get the base register in MIa.
1907   unsigned BasePosA, OffsetPosA;
1908   if (!getBaseAndOffsetPosition(MIa, BasePosA, OffsetPosA))
1909     return false;
1910   const MachineOperand &BaseA = MIa.getOperand(BasePosA);
1911   Register BaseRegA = BaseA.getReg();
1912   unsigned BaseSubA = BaseA.getSubReg();
1913 
1914   // Get the base register in MIb.
1915   unsigned BasePosB, OffsetPosB;
1916   if (!getBaseAndOffsetPosition(MIb, BasePosB, OffsetPosB))
1917     return false;
1918   const MachineOperand &BaseB = MIb.getOperand(BasePosB);
1919   Register BaseRegB = BaseB.getReg();
1920   unsigned BaseSubB = BaseB.getSubReg();
1921 
1922   if (BaseRegA != BaseRegB || BaseSubA != BaseSubB)
1923     return false;
1924 
1925   // Get the access sizes.
1926   unsigned SizeA = getMemAccessSize(MIa);
1927   unsigned SizeB = getMemAccessSize(MIb);
1928 
1929   // Get the offsets. Handle immediates only for now.
1930   const MachineOperand &OffA = MIa.getOperand(OffsetPosA);
1931   const MachineOperand &OffB = MIb.getOperand(OffsetPosB);
1932   if (!MIa.getOperand(OffsetPosA).isImm() ||
1933       !MIb.getOperand(OffsetPosB).isImm())
1934     return false;
1935   int OffsetA = isPostIncrement(MIa) ? 0 : OffA.getImm();
1936   int OffsetB = isPostIncrement(MIb) ? 0 : OffB.getImm();
1937 
1938   // This is a mem access with the same base register and known offsets from it.
1939   // Reason about it.
1940   if (OffsetA > OffsetB) {
1941     uint64_t OffDiff = (uint64_t)((int64_t)OffsetA - (int64_t)OffsetB);
1942     return SizeB <= OffDiff;
1943   }
1944   if (OffsetA < OffsetB) {
1945     uint64_t OffDiff = (uint64_t)((int64_t)OffsetB - (int64_t)OffsetA);
1946     return SizeA <= OffDiff;
1947   }
1948 
1949   return false;
1950 }
1951 
1952 /// If the instruction is an increment of a constant value, return the amount.
1953 bool HexagonInstrInfo::getIncrementValue(const MachineInstr &MI,
1954       int &Value) const {
1955   if (isPostIncrement(MI)) {
1956     unsigned BasePos = 0, OffsetPos = 0;
1957     if (!getBaseAndOffsetPosition(MI, BasePos, OffsetPos))
1958       return false;
1959     const MachineOperand &OffsetOp = MI.getOperand(OffsetPos);
1960     if (OffsetOp.isImm()) {
1961       Value = OffsetOp.getImm();
1962       return true;
1963     }
1964   } else if (MI.getOpcode() == Hexagon::A2_addi) {
1965     const MachineOperand &AddOp = MI.getOperand(2);
1966     if (AddOp.isImm()) {
1967       Value = AddOp.getImm();
1968       return true;
1969     }
1970   }
1971 
1972   return false;
1973 }
1974 
1975 std::pair<unsigned, unsigned>
1976 HexagonInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF) const {
1977   return std::make_pair(TF & ~HexagonII::MO_Bitmasks,
1978                         TF & HexagonII::MO_Bitmasks);
1979 }
1980 
1981 ArrayRef<std::pair<unsigned, const char*>>
1982 HexagonInstrInfo::getSerializableDirectMachineOperandTargetFlags() const {
1983   using namespace HexagonII;
1984 
1985   static const std::pair<unsigned, const char*> Flags[] = {
1986     {MO_PCREL,  "hexagon-pcrel"},
1987     {MO_GOT,    "hexagon-got"},
1988     {MO_LO16,   "hexagon-lo16"},
1989     {MO_HI16,   "hexagon-hi16"},
1990     {MO_GPREL,  "hexagon-gprel"},
1991     {MO_GDGOT,  "hexagon-gdgot"},
1992     {MO_GDPLT,  "hexagon-gdplt"},
1993     {MO_IE,     "hexagon-ie"},
1994     {MO_IEGOT,  "hexagon-iegot"},
1995     {MO_TPREL,  "hexagon-tprel"}
1996   };
1997   return makeArrayRef(Flags);
1998 }
1999 
2000 ArrayRef<std::pair<unsigned, const char*>>
2001 HexagonInstrInfo::getSerializableBitmaskMachineOperandTargetFlags() const {
2002   using namespace HexagonII;
2003 
2004   static const std::pair<unsigned, const char*> Flags[] = {
2005     {HMOTF_ConstExtended, "hexagon-ext"}
2006   };
2007   return makeArrayRef(Flags);
2008 }
2009 
2010 unsigned HexagonInstrInfo::createVR(MachineFunction *MF, MVT VT) const {
2011   MachineRegisterInfo &MRI = MF->getRegInfo();
2012   const TargetRegisterClass *TRC;
2013   if (VT == MVT::i1) {
2014     TRC = &Hexagon::PredRegsRegClass;
2015   } else if (VT == MVT::i32 || VT == MVT::f32) {
2016     TRC = &Hexagon::IntRegsRegClass;
2017   } else if (VT == MVT::i64 || VT == MVT::f64) {
2018     TRC = &Hexagon::DoubleRegsRegClass;
2019   } else {
2020     llvm_unreachable("Cannot handle this register class");
2021   }
2022 
2023   Register NewReg = MRI.createVirtualRegister(TRC);
2024   return NewReg;
2025 }
2026 
2027 bool HexagonInstrInfo::isAbsoluteSet(const MachineInstr &MI) const {
2028   return (getAddrMode(MI) == HexagonII::AbsoluteSet);
2029 }
2030 
2031 bool HexagonInstrInfo::isAccumulator(const MachineInstr &MI) const {
2032   const uint64_t F = MI.getDesc().TSFlags;
2033   return((F >> HexagonII::AccumulatorPos) & HexagonII::AccumulatorMask);
2034 }
2035 
2036 bool HexagonInstrInfo::isBaseImmOffset(const MachineInstr &MI) const {
2037   return getAddrMode(MI) == HexagonII::BaseImmOffset;
2038 }
2039 
2040 bool HexagonInstrInfo::isComplex(const MachineInstr &MI) const {
2041   return !isTC1(MI) && !isTC2Early(MI) && !MI.getDesc().mayLoad() &&
2042          !MI.getDesc().mayStore() &&
2043          MI.getDesc().getOpcode() != Hexagon::S2_allocframe &&
2044          MI.getDesc().getOpcode() != Hexagon::L2_deallocframe &&
2045          !isMemOp(MI) && !MI.isBranch() && !MI.isReturn() && !MI.isCall();
2046 }
2047 
2048 // Return true if the instruction is a compund branch instruction.
2049 bool HexagonInstrInfo::isCompoundBranchInstr(const MachineInstr &MI) const {
2050   return getType(MI) == HexagonII::TypeCJ && MI.isBranch();
2051 }
2052 
2053 // TODO: In order to have isExtendable for fpimm/f32Ext, we need to handle
2054 // isFPImm and later getFPImm as well.
2055 bool HexagonInstrInfo::isConstExtended(const MachineInstr &MI) const {
2056   const uint64_t F = MI.getDesc().TSFlags;
2057   unsigned isExtended = (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
2058   if (isExtended) // Instruction must be extended.
2059     return true;
2060 
2061   unsigned isExtendable =
2062     (F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask;
2063   if (!isExtendable)
2064     return false;
2065 
2066   if (MI.isCall())
2067     return false;
2068 
2069   short ExtOpNum = getCExtOpNum(MI);
2070   const MachineOperand &MO = MI.getOperand(ExtOpNum);
2071   // Use MO operand flags to determine if MO
2072   // has the HMOTF_ConstExtended flag set.
2073   if (MO.getTargetFlags() & HexagonII::HMOTF_ConstExtended)
2074     return true;
2075   // If this is a Machine BB address we are talking about, and it is
2076   // not marked as extended, say so.
2077   if (MO.isMBB())
2078     return false;
2079 
2080   // We could be using an instruction with an extendable immediate and shoehorn
2081   // a global address into it. If it is a global address it will be constant
2082   // extended. We do this for COMBINE.
2083   if (MO.isGlobal() || MO.isSymbol() || MO.isBlockAddress() ||
2084       MO.isJTI() || MO.isCPI() || MO.isFPImm())
2085     return true;
2086 
2087   // If the extendable operand is not 'Immediate' type, the instruction should
2088   // have 'isExtended' flag set.
2089   assert(MO.isImm() && "Extendable operand must be Immediate type");
2090 
2091   int MinValue = getMinValue(MI);
2092   int MaxValue = getMaxValue(MI);
2093   int ImmValue = MO.getImm();
2094 
2095   return (ImmValue < MinValue || ImmValue > MaxValue);
2096 }
2097 
2098 bool HexagonInstrInfo::isDeallocRet(const MachineInstr &MI) const {
2099   switch (MI.getOpcode()) {
2100   case Hexagon::L4_return:
2101   case Hexagon::L4_return_t:
2102   case Hexagon::L4_return_f:
2103   case Hexagon::L4_return_tnew_pnt:
2104   case Hexagon::L4_return_fnew_pnt:
2105   case Hexagon::L4_return_tnew_pt:
2106   case Hexagon::L4_return_fnew_pt:
2107     return true;
2108   }
2109   return false;
2110 }
2111 
2112 // Return true when ConsMI uses a register defined by ProdMI.
2113 bool HexagonInstrInfo::isDependent(const MachineInstr &ProdMI,
2114       const MachineInstr &ConsMI) const {
2115   if (!ProdMI.getDesc().getNumDefs())
2116     return false;
2117   const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
2118 
2119   SmallVector<unsigned, 4> DefsA;
2120   SmallVector<unsigned, 4> DefsB;
2121   SmallVector<unsigned, 8> UsesA;
2122   SmallVector<unsigned, 8> UsesB;
2123 
2124   parseOperands(ProdMI, DefsA, UsesA);
2125   parseOperands(ConsMI, DefsB, UsesB);
2126 
2127   for (auto &RegA : DefsA)
2128     for (auto &RegB : UsesB) {
2129       // True data dependency.
2130       if (RegA == RegB)
2131         return true;
2132 
2133       if (Register::isPhysicalRegister(RegA))
2134         for (MCSubRegIterator SubRegs(RegA, &HRI); SubRegs.isValid(); ++SubRegs)
2135           if (RegB == *SubRegs)
2136             return true;
2137 
2138       if (Register::isPhysicalRegister(RegB))
2139         for (MCSubRegIterator SubRegs(RegB, &HRI); SubRegs.isValid(); ++SubRegs)
2140           if (RegA == *SubRegs)
2141             return true;
2142     }
2143 
2144   return false;
2145 }
2146 
2147 // Returns true if the instruction is alread a .cur.
2148 bool HexagonInstrInfo::isDotCurInst(const MachineInstr &MI) const {
2149   switch (MI.getOpcode()) {
2150   case Hexagon::V6_vL32b_cur_pi:
2151   case Hexagon::V6_vL32b_cur_ai:
2152     return true;
2153   }
2154   return false;
2155 }
2156 
2157 // Returns true, if any one of the operands is a dot new
2158 // insn, whether it is predicated dot new or register dot new.
2159 bool HexagonInstrInfo::isDotNewInst(const MachineInstr &MI) const {
2160   if (isNewValueInst(MI) || (isPredicated(MI) && isPredicatedNew(MI)))
2161     return true;
2162 
2163   return false;
2164 }
2165 
2166 /// Symmetrical. See if these two instructions are fit for duplex pair.
2167 bool HexagonInstrInfo::isDuplexPair(const MachineInstr &MIa,
2168       const MachineInstr &MIb) const {
2169   HexagonII::SubInstructionGroup MIaG = getDuplexCandidateGroup(MIa);
2170   HexagonII::SubInstructionGroup MIbG = getDuplexCandidateGroup(MIb);
2171   return (isDuplexPairMatch(MIaG, MIbG) || isDuplexPairMatch(MIbG, MIaG));
2172 }
2173 
2174 bool HexagonInstrInfo::isEarlySourceInstr(const MachineInstr &MI) const {
2175   if (MI.mayLoadOrStore() || MI.isCompare())
2176     return true;
2177 
2178   // Multiply
2179   unsigned SchedClass = MI.getDesc().getSchedClass();
2180   return is_TC4x(SchedClass) || is_TC3x(SchedClass);
2181 }
2182 
2183 bool HexagonInstrInfo::isEndLoopN(unsigned Opcode) const {
2184   return (Opcode == Hexagon::ENDLOOP0 ||
2185           Opcode == Hexagon::ENDLOOP1);
2186 }
2187 
2188 bool HexagonInstrInfo::isExpr(unsigned OpType) const {
2189   switch(OpType) {
2190   case MachineOperand::MO_MachineBasicBlock:
2191   case MachineOperand::MO_GlobalAddress:
2192   case MachineOperand::MO_ExternalSymbol:
2193   case MachineOperand::MO_JumpTableIndex:
2194   case MachineOperand::MO_ConstantPoolIndex:
2195   case MachineOperand::MO_BlockAddress:
2196     return true;
2197   default:
2198     return false;
2199   }
2200 }
2201 
2202 bool HexagonInstrInfo::isExtendable(const MachineInstr &MI) const {
2203   const MCInstrDesc &MID = MI.getDesc();
2204   const uint64_t F = MID.TSFlags;
2205   if ((F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask)
2206     return true;
2207 
2208   // TODO: This is largely obsolete now. Will need to be removed
2209   // in consecutive patches.
2210   switch (MI.getOpcode()) {
2211     // PS_fi and PS_fia remain special cases.
2212     case Hexagon::PS_fi:
2213     case Hexagon::PS_fia:
2214       return true;
2215     default:
2216       return false;
2217   }
2218   return  false;
2219 }
2220 
2221 // This returns true in two cases:
2222 // - The OP code itself indicates that this is an extended instruction.
2223 // - One of MOs has been marked with HMOTF_ConstExtended flag.
2224 bool HexagonInstrInfo::isExtended(const MachineInstr &MI) const {
2225   // First check if this is permanently extended op code.
2226   const uint64_t F = MI.getDesc().TSFlags;
2227   if ((F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask)
2228     return true;
2229   // Use MO operand flags to determine if one of MI's operands
2230   // has HMOTF_ConstExtended flag set.
2231   for (const MachineOperand &MO : MI.operands())
2232     if (MO.getTargetFlags() & HexagonII::HMOTF_ConstExtended)
2233       return true;
2234   return  false;
2235 }
2236 
2237 bool HexagonInstrInfo::isFloat(const MachineInstr &MI) const {
2238   unsigned Opcode = MI.getOpcode();
2239   const uint64_t F = get(Opcode).TSFlags;
2240   return (F >> HexagonII::FPPos) & HexagonII::FPMask;
2241 }
2242 
2243 // No V60 HVX VMEM with A_INDIRECT.
2244 bool HexagonInstrInfo::isHVXMemWithAIndirect(const MachineInstr &I,
2245       const MachineInstr &J) const {
2246   if (!isHVXVec(I))
2247     return false;
2248   if (!I.mayLoad() && !I.mayStore())
2249     return false;
2250   return J.isIndirectBranch() || isIndirectCall(J) || isIndirectL4Return(J);
2251 }
2252 
2253 bool HexagonInstrInfo::isIndirectCall(const MachineInstr &MI) const {
2254   switch (MI.getOpcode()) {
2255   case Hexagon::J2_callr:
2256   case Hexagon::J2_callrf:
2257   case Hexagon::J2_callrt:
2258   case Hexagon::PS_call_nr:
2259     return true;
2260   }
2261   return false;
2262 }
2263 
2264 bool HexagonInstrInfo::isIndirectL4Return(const MachineInstr &MI) const {
2265   switch (MI.getOpcode()) {
2266   case Hexagon::L4_return:
2267   case Hexagon::L4_return_t:
2268   case Hexagon::L4_return_f:
2269   case Hexagon::L4_return_fnew_pnt:
2270   case Hexagon::L4_return_fnew_pt:
2271   case Hexagon::L4_return_tnew_pnt:
2272   case Hexagon::L4_return_tnew_pt:
2273     return true;
2274   }
2275   return false;
2276 }
2277 
2278 bool HexagonInstrInfo::isJumpR(const MachineInstr &MI) const {
2279   switch (MI.getOpcode()) {
2280   case Hexagon::J2_jumpr:
2281   case Hexagon::J2_jumprt:
2282   case Hexagon::J2_jumprf:
2283   case Hexagon::J2_jumprtnewpt:
2284   case Hexagon::J2_jumprfnewpt:
2285   case Hexagon::J2_jumprtnew:
2286   case Hexagon::J2_jumprfnew:
2287     return true;
2288   }
2289   return false;
2290 }
2291 
2292 // Return true if a given MI can accommodate given offset.
2293 // Use abs estimate as oppose to the exact number.
2294 // TODO: This will need to be changed to use MC level
2295 // definition of instruction extendable field size.
2296 bool HexagonInstrInfo::isJumpWithinBranchRange(const MachineInstr &MI,
2297       unsigned offset) const {
2298   // This selection of jump instructions matches to that what
2299   // analyzeBranch can parse, plus NVJ.
2300   if (isNewValueJump(MI)) // r9:2
2301     return isInt<11>(offset);
2302 
2303   switch (MI.getOpcode()) {
2304   // Still missing Jump to address condition on register value.
2305   default:
2306     return false;
2307   case Hexagon::J2_jump: // bits<24> dst; // r22:2
2308   case Hexagon::J2_call:
2309   case Hexagon::PS_call_nr:
2310     return isInt<24>(offset);
2311   case Hexagon::J2_jumpt: //bits<17> dst; // r15:2
2312   case Hexagon::J2_jumpf:
2313   case Hexagon::J2_jumptnew:
2314   case Hexagon::J2_jumptnewpt:
2315   case Hexagon::J2_jumpfnew:
2316   case Hexagon::J2_jumpfnewpt:
2317   case Hexagon::J2_callt:
2318   case Hexagon::J2_callf:
2319     return isInt<17>(offset);
2320   case Hexagon::J2_loop0i:
2321   case Hexagon::J2_loop0iext:
2322   case Hexagon::J2_loop0r:
2323   case Hexagon::J2_loop0rext:
2324   case Hexagon::J2_loop1i:
2325   case Hexagon::J2_loop1iext:
2326   case Hexagon::J2_loop1r:
2327   case Hexagon::J2_loop1rext:
2328     return isInt<9>(offset);
2329   // TODO: Add all the compound branches here. Can we do this in Relation model?
2330   case Hexagon::J4_cmpeqi_tp0_jump_nt:
2331   case Hexagon::J4_cmpeqi_tp1_jump_nt:
2332   case Hexagon::J4_cmpeqn1_tp0_jump_nt:
2333   case Hexagon::J4_cmpeqn1_tp1_jump_nt:
2334     return isInt<11>(offset);
2335   }
2336 }
2337 
2338 bool HexagonInstrInfo::isLateInstrFeedsEarlyInstr(const MachineInstr &LRMI,
2339       const MachineInstr &ESMI) const {
2340   bool isLate = isLateResultInstr(LRMI);
2341   bool isEarly = isEarlySourceInstr(ESMI);
2342 
2343   LLVM_DEBUG(dbgs() << "V60" << (isLate ? "-LR  " : " --  "));
2344   LLVM_DEBUG(LRMI.dump());
2345   LLVM_DEBUG(dbgs() << "V60" << (isEarly ? "-ES  " : " --  "));
2346   LLVM_DEBUG(ESMI.dump());
2347 
2348   if (isLate && isEarly) {
2349     LLVM_DEBUG(dbgs() << "++Is Late Result feeding Early Source\n");
2350     return true;
2351   }
2352 
2353   return false;
2354 }
2355 
2356 bool HexagonInstrInfo::isLateResultInstr(const MachineInstr &MI) const {
2357   switch (MI.getOpcode()) {
2358   case TargetOpcode::EXTRACT_SUBREG:
2359   case TargetOpcode::INSERT_SUBREG:
2360   case TargetOpcode::SUBREG_TO_REG:
2361   case TargetOpcode::REG_SEQUENCE:
2362   case TargetOpcode::IMPLICIT_DEF:
2363   case TargetOpcode::COPY:
2364   case TargetOpcode::INLINEASM:
2365   case TargetOpcode::PHI:
2366     return false;
2367   default:
2368     break;
2369   }
2370 
2371   unsigned SchedClass = MI.getDesc().getSchedClass();
2372   return !is_TC1(SchedClass);
2373 }
2374 
2375 bool HexagonInstrInfo::isLateSourceInstr(const MachineInstr &MI) const {
2376   // Instructions with iclass A_CVI_VX and attribute A_CVI_LATE uses a multiply
2377   // resource, but all operands can be received late like an ALU instruction.
2378   return getType(MI) == HexagonII::TypeCVI_VX_LATE;
2379 }
2380 
2381 bool HexagonInstrInfo::isLoopN(const MachineInstr &MI) const {
2382   unsigned Opcode = MI.getOpcode();
2383   return Opcode == Hexagon::J2_loop0i    ||
2384          Opcode == Hexagon::J2_loop0r    ||
2385          Opcode == Hexagon::J2_loop0iext ||
2386          Opcode == Hexagon::J2_loop0rext ||
2387          Opcode == Hexagon::J2_loop1i    ||
2388          Opcode == Hexagon::J2_loop1r    ||
2389          Opcode == Hexagon::J2_loop1iext ||
2390          Opcode == Hexagon::J2_loop1rext;
2391 }
2392 
2393 bool HexagonInstrInfo::isMemOp(const MachineInstr &MI) const {
2394   switch (MI.getOpcode()) {
2395     default: return false;
2396     case Hexagon::L4_iadd_memopw_io:
2397     case Hexagon::L4_isub_memopw_io:
2398     case Hexagon::L4_add_memopw_io:
2399     case Hexagon::L4_sub_memopw_io:
2400     case Hexagon::L4_and_memopw_io:
2401     case Hexagon::L4_or_memopw_io:
2402     case Hexagon::L4_iadd_memoph_io:
2403     case Hexagon::L4_isub_memoph_io:
2404     case Hexagon::L4_add_memoph_io:
2405     case Hexagon::L4_sub_memoph_io:
2406     case Hexagon::L4_and_memoph_io:
2407     case Hexagon::L4_or_memoph_io:
2408     case Hexagon::L4_iadd_memopb_io:
2409     case Hexagon::L4_isub_memopb_io:
2410     case Hexagon::L4_add_memopb_io:
2411     case Hexagon::L4_sub_memopb_io:
2412     case Hexagon::L4_and_memopb_io:
2413     case Hexagon::L4_or_memopb_io:
2414     case Hexagon::L4_ior_memopb_io:
2415     case Hexagon::L4_ior_memoph_io:
2416     case Hexagon::L4_ior_memopw_io:
2417     case Hexagon::L4_iand_memopb_io:
2418     case Hexagon::L4_iand_memoph_io:
2419     case Hexagon::L4_iand_memopw_io:
2420     return true;
2421   }
2422   return false;
2423 }
2424 
2425 bool HexagonInstrInfo::isNewValue(const MachineInstr &MI) const {
2426   const uint64_t F = MI.getDesc().TSFlags;
2427   return (F >> HexagonII::NewValuePos) & HexagonII::NewValueMask;
2428 }
2429 
2430 bool HexagonInstrInfo::isNewValue(unsigned Opcode) const {
2431   const uint64_t F = get(Opcode).TSFlags;
2432   return (F >> HexagonII::NewValuePos) & HexagonII::NewValueMask;
2433 }
2434 
2435 bool HexagonInstrInfo::isNewValueInst(const MachineInstr &MI) const {
2436   return isNewValueJump(MI) || isNewValueStore(MI);
2437 }
2438 
2439 bool HexagonInstrInfo::isNewValueJump(const MachineInstr &MI) const {
2440   return isNewValue(MI) && MI.isBranch();
2441 }
2442 
2443 bool HexagonInstrInfo::isNewValueJump(unsigned Opcode) const {
2444   return isNewValue(Opcode) && get(Opcode).isBranch() && isPredicated(Opcode);
2445 }
2446 
2447 bool HexagonInstrInfo::isNewValueStore(const MachineInstr &MI) const {
2448   const uint64_t F = MI.getDesc().TSFlags;
2449   return (F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask;
2450 }
2451 
2452 bool HexagonInstrInfo::isNewValueStore(unsigned Opcode) const {
2453   const uint64_t F = get(Opcode).TSFlags;
2454   return (F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask;
2455 }
2456 
2457 // Returns true if a particular operand is extendable for an instruction.
2458 bool HexagonInstrInfo::isOperandExtended(const MachineInstr &MI,
2459     unsigned OperandNum) const {
2460   const uint64_t F = MI.getDesc().TSFlags;
2461   return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask)
2462           == OperandNum;
2463 }
2464 
2465 bool HexagonInstrInfo::isPredicatedNew(const MachineInstr &MI) const {
2466   const uint64_t F = MI.getDesc().TSFlags;
2467   assert(isPredicated(MI));
2468   return (F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask;
2469 }
2470 
2471 bool HexagonInstrInfo::isPredicatedNew(unsigned Opcode) const {
2472   const uint64_t F = get(Opcode).TSFlags;
2473   assert(isPredicated(Opcode));
2474   return (F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask;
2475 }
2476 
2477 bool HexagonInstrInfo::isPredicatedTrue(const MachineInstr &MI) const {
2478   const uint64_t F = MI.getDesc().TSFlags;
2479   return !((F >> HexagonII::PredicatedFalsePos) &
2480            HexagonII::PredicatedFalseMask);
2481 }
2482 
2483 bool HexagonInstrInfo::isPredicatedTrue(unsigned Opcode) const {
2484   const uint64_t F = get(Opcode).TSFlags;
2485   // Make sure that the instruction is predicated.
2486   assert((F>> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
2487   return !((F >> HexagonII::PredicatedFalsePos) &
2488            HexagonII::PredicatedFalseMask);
2489 }
2490 
2491 bool HexagonInstrInfo::isPredicated(unsigned Opcode) const {
2492   const uint64_t F = get(Opcode).TSFlags;
2493   return (F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask;
2494 }
2495 
2496 bool HexagonInstrInfo::isPredicateLate(unsigned Opcode) const {
2497   const uint64_t F = get(Opcode).TSFlags;
2498   return (F >> HexagonII::PredicateLatePos) & HexagonII::PredicateLateMask;
2499 }
2500 
2501 bool HexagonInstrInfo::isPredictedTaken(unsigned Opcode) const {
2502   const uint64_t F = get(Opcode).TSFlags;
2503   assert(get(Opcode).isBranch() &&
2504          (isPredicatedNew(Opcode) || isNewValue(Opcode)));
2505   return (F >> HexagonII::TakenPos) & HexagonII::TakenMask;
2506 }
2507 
2508 bool HexagonInstrInfo::isSaveCalleeSavedRegsCall(const MachineInstr &MI) const {
2509   return MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4 ||
2510          MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_EXT ||
2511          MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_PIC ||
2512          MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_EXT_PIC;
2513 }
2514 
2515 bool HexagonInstrInfo::isSignExtendingLoad(const MachineInstr &MI) const {
2516   switch (MI.getOpcode()) {
2517   // Byte
2518   case Hexagon::L2_loadrb_io:
2519   case Hexagon::L4_loadrb_ur:
2520   case Hexagon::L4_loadrb_ap:
2521   case Hexagon::L2_loadrb_pr:
2522   case Hexagon::L2_loadrb_pbr:
2523   case Hexagon::L2_loadrb_pi:
2524   case Hexagon::L2_loadrb_pci:
2525   case Hexagon::L2_loadrb_pcr:
2526   case Hexagon::L2_loadbsw2_io:
2527   case Hexagon::L4_loadbsw2_ur:
2528   case Hexagon::L4_loadbsw2_ap:
2529   case Hexagon::L2_loadbsw2_pr:
2530   case Hexagon::L2_loadbsw2_pbr:
2531   case Hexagon::L2_loadbsw2_pi:
2532   case Hexagon::L2_loadbsw2_pci:
2533   case Hexagon::L2_loadbsw2_pcr:
2534   case Hexagon::L2_loadbsw4_io:
2535   case Hexagon::L4_loadbsw4_ur:
2536   case Hexagon::L4_loadbsw4_ap:
2537   case Hexagon::L2_loadbsw4_pr:
2538   case Hexagon::L2_loadbsw4_pbr:
2539   case Hexagon::L2_loadbsw4_pi:
2540   case Hexagon::L2_loadbsw4_pci:
2541   case Hexagon::L2_loadbsw4_pcr:
2542   case Hexagon::L4_loadrb_rr:
2543   case Hexagon::L2_ploadrbt_io:
2544   case Hexagon::L2_ploadrbt_pi:
2545   case Hexagon::L2_ploadrbf_io:
2546   case Hexagon::L2_ploadrbf_pi:
2547   case Hexagon::L2_ploadrbtnew_io:
2548   case Hexagon::L2_ploadrbfnew_io:
2549   case Hexagon::L4_ploadrbt_rr:
2550   case Hexagon::L4_ploadrbf_rr:
2551   case Hexagon::L4_ploadrbtnew_rr:
2552   case Hexagon::L4_ploadrbfnew_rr:
2553   case Hexagon::L2_ploadrbtnew_pi:
2554   case Hexagon::L2_ploadrbfnew_pi:
2555   case Hexagon::L4_ploadrbt_abs:
2556   case Hexagon::L4_ploadrbf_abs:
2557   case Hexagon::L4_ploadrbtnew_abs:
2558   case Hexagon::L4_ploadrbfnew_abs:
2559   case Hexagon::L2_loadrbgp:
2560   // Half
2561   case Hexagon::L2_loadrh_io:
2562   case Hexagon::L4_loadrh_ur:
2563   case Hexagon::L4_loadrh_ap:
2564   case Hexagon::L2_loadrh_pr:
2565   case Hexagon::L2_loadrh_pbr:
2566   case Hexagon::L2_loadrh_pi:
2567   case Hexagon::L2_loadrh_pci:
2568   case Hexagon::L2_loadrh_pcr:
2569   case Hexagon::L4_loadrh_rr:
2570   case Hexagon::L2_ploadrht_io:
2571   case Hexagon::L2_ploadrht_pi:
2572   case Hexagon::L2_ploadrhf_io:
2573   case Hexagon::L2_ploadrhf_pi:
2574   case Hexagon::L2_ploadrhtnew_io:
2575   case Hexagon::L2_ploadrhfnew_io:
2576   case Hexagon::L4_ploadrht_rr:
2577   case Hexagon::L4_ploadrhf_rr:
2578   case Hexagon::L4_ploadrhtnew_rr:
2579   case Hexagon::L4_ploadrhfnew_rr:
2580   case Hexagon::L2_ploadrhtnew_pi:
2581   case Hexagon::L2_ploadrhfnew_pi:
2582   case Hexagon::L4_ploadrht_abs:
2583   case Hexagon::L4_ploadrhf_abs:
2584   case Hexagon::L4_ploadrhtnew_abs:
2585   case Hexagon::L4_ploadrhfnew_abs:
2586   case Hexagon::L2_loadrhgp:
2587     return true;
2588   default:
2589     return false;
2590   }
2591 }
2592 
2593 bool HexagonInstrInfo::isSolo(const MachineInstr &MI) const {
2594   const uint64_t F = MI.getDesc().TSFlags;
2595   return (F >> HexagonII::SoloPos) & HexagonII::SoloMask;
2596 }
2597 
2598 bool HexagonInstrInfo::isSpillPredRegOp(const MachineInstr &MI) const {
2599   switch (MI.getOpcode()) {
2600   case Hexagon::STriw_pred:
2601   case Hexagon::LDriw_pred:
2602     return true;
2603   default:
2604     return false;
2605   }
2606 }
2607 
2608 bool HexagonInstrInfo::isTailCall(const MachineInstr &MI) const {
2609   if (!MI.isBranch())
2610     return false;
2611 
2612   for (auto &Op : MI.operands())
2613     if (Op.isGlobal() || Op.isSymbol())
2614       return true;
2615   return false;
2616 }
2617 
2618 // Returns true when SU has a timing class TC1.
2619 bool HexagonInstrInfo::isTC1(const MachineInstr &MI) const {
2620   unsigned SchedClass = MI.getDesc().getSchedClass();
2621   return is_TC1(SchedClass);
2622 }
2623 
2624 bool HexagonInstrInfo::isTC2(const MachineInstr &MI) const {
2625   unsigned SchedClass = MI.getDesc().getSchedClass();
2626   return is_TC2(SchedClass);
2627 }
2628 
2629 bool HexagonInstrInfo::isTC2Early(const MachineInstr &MI) const {
2630   unsigned SchedClass = MI.getDesc().getSchedClass();
2631   return is_TC2early(SchedClass);
2632 }
2633 
2634 bool HexagonInstrInfo::isTC4x(const MachineInstr &MI) const {
2635   unsigned SchedClass = MI.getDesc().getSchedClass();
2636   return is_TC4x(SchedClass);
2637 }
2638 
2639 // Schedule this ASAP.
2640 bool HexagonInstrInfo::isToBeScheduledASAP(const MachineInstr &MI1,
2641       const MachineInstr &MI2) const {
2642   if (mayBeCurLoad(MI1)) {
2643     // if (result of SU is used in Next) return true;
2644     Register DstReg = MI1.getOperand(0).getReg();
2645     int N = MI2.getNumOperands();
2646     for (int I = 0; I < N; I++)
2647       if (MI2.getOperand(I).isReg() && DstReg == MI2.getOperand(I).getReg())
2648         return true;
2649   }
2650   if (mayBeNewStore(MI2))
2651     if (MI2.getOpcode() == Hexagon::V6_vS32b_pi)
2652       if (MI1.getOperand(0).isReg() && MI2.getOperand(3).isReg() &&
2653           MI1.getOperand(0).getReg() == MI2.getOperand(3).getReg())
2654         return true;
2655   return false;
2656 }
2657 
2658 bool HexagonInstrInfo::isHVXVec(const MachineInstr &MI) const {
2659   const uint64_t V = getType(MI);
2660   return HexagonII::TypeCVI_FIRST <= V && V <= HexagonII::TypeCVI_LAST;
2661 }
2662 
2663 // Check if the Offset is a valid auto-inc imm by Load/Store Type.
2664 bool HexagonInstrInfo::isValidAutoIncImm(const EVT VT, int Offset) const {
2665   int Size = VT.getSizeInBits() / 8;
2666   if (Offset % Size != 0)
2667     return false;
2668   int Count = Offset / Size;
2669 
2670   switch (VT.getSimpleVT().SimpleTy) {
2671     // For scalars the auto-inc is s4
2672     case MVT::i8:
2673     case MVT::i16:
2674     case MVT::i32:
2675     case MVT::i64:
2676     case MVT::f32:
2677     case MVT::f64:
2678     case MVT::v2i16:
2679     case MVT::v2i32:
2680     case MVT::v4i8:
2681     case MVT::v4i16:
2682     case MVT::v8i8:
2683       return isInt<4>(Count);
2684     // For HVX vectors the auto-inc is s3
2685     case MVT::v64i8:
2686     case MVT::v32i16:
2687     case MVT::v16i32:
2688     case MVT::v8i64:
2689     case MVT::v128i8:
2690     case MVT::v64i16:
2691     case MVT::v32i32:
2692     case MVT::v16i64:
2693       return isInt<3>(Count);
2694     default:
2695       break;
2696   }
2697 
2698   llvm_unreachable("Not an valid type!");
2699 }
2700 
2701 bool HexagonInstrInfo::isValidOffset(unsigned Opcode, int Offset,
2702       const TargetRegisterInfo *TRI, bool Extend) const {
2703   // This function is to check whether the "Offset" is in the correct range of
2704   // the given "Opcode". If "Offset" is not in the correct range, "A2_addi" is
2705   // inserted to calculate the final address. Due to this reason, the function
2706   // assumes that the "Offset" has correct alignment.
2707   // We used to assert if the offset was not properly aligned, however,
2708   // there are cases where a misaligned pointer recast can cause this
2709   // problem, and we need to allow for it. The front end warns of such
2710   // misaligns with respect to load size.
2711   switch (Opcode) {
2712   case Hexagon::PS_vstorerq_ai:
2713   case Hexagon::PS_vstorerv_ai:
2714   case Hexagon::PS_vstorerw_ai:
2715   case Hexagon::PS_vstorerw_nt_ai:
2716   case Hexagon::PS_vloadrq_ai:
2717   case Hexagon::PS_vloadrv_ai:
2718   case Hexagon::PS_vloadrw_ai:
2719   case Hexagon::PS_vloadrw_nt_ai:
2720   case Hexagon::V6_vL32b_ai:
2721   case Hexagon::V6_vS32b_ai:
2722   case Hexagon::V6_vS32b_qpred_ai:
2723   case Hexagon::V6_vS32b_nqpred_ai:
2724   case Hexagon::V6_vL32b_nt_ai:
2725   case Hexagon::V6_vS32b_nt_ai:
2726   case Hexagon::V6_vL32Ub_ai:
2727   case Hexagon::V6_vS32Ub_ai: {
2728     unsigned VectorSize = TRI->getSpillSize(Hexagon::HvxVRRegClass);
2729     assert(isPowerOf2_32(VectorSize));
2730     if (Offset & (VectorSize-1))
2731       return false;
2732     return isInt<4>(Offset >> Log2_32(VectorSize));
2733   }
2734 
2735   case Hexagon::J2_loop0i:
2736   case Hexagon::J2_loop1i:
2737     return isUInt<10>(Offset);
2738 
2739   case Hexagon::S4_storeirb_io:
2740   case Hexagon::S4_storeirbt_io:
2741   case Hexagon::S4_storeirbf_io:
2742     return isUInt<6>(Offset);
2743 
2744   case Hexagon::S4_storeirh_io:
2745   case Hexagon::S4_storeirht_io:
2746   case Hexagon::S4_storeirhf_io:
2747     return isShiftedUInt<6,1>(Offset);
2748 
2749   case Hexagon::S4_storeiri_io:
2750   case Hexagon::S4_storeirit_io:
2751   case Hexagon::S4_storeirif_io:
2752     return isShiftedUInt<6,2>(Offset);
2753   }
2754 
2755   if (Extend)
2756     return true;
2757 
2758   switch (Opcode) {
2759   case Hexagon::L2_loadri_io:
2760   case Hexagon::S2_storeri_io:
2761     return (Offset >= Hexagon_MEMW_OFFSET_MIN) &&
2762       (Offset <= Hexagon_MEMW_OFFSET_MAX);
2763 
2764   case Hexagon::L2_loadrd_io:
2765   case Hexagon::S2_storerd_io:
2766     return (Offset >= Hexagon_MEMD_OFFSET_MIN) &&
2767       (Offset <= Hexagon_MEMD_OFFSET_MAX);
2768 
2769   case Hexagon::L2_loadrh_io:
2770   case Hexagon::L2_loadruh_io:
2771   case Hexagon::S2_storerh_io:
2772   case Hexagon::S2_storerf_io:
2773     return (Offset >= Hexagon_MEMH_OFFSET_MIN) &&
2774       (Offset <= Hexagon_MEMH_OFFSET_MAX);
2775 
2776   case Hexagon::L2_loadrb_io:
2777   case Hexagon::L2_loadrub_io:
2778   case Hexagon::S2_storerb_io:
2779     return (Offset >= Hexagon_MEMB_OFFSET_MIN) &&
2780       (Offset <= Hexagon_MEMB_OFFSET_MAX);
2781 
2782   case Hexagon::A2_addi:
2783     return (Offset >= Hexagon_ADDI_OFFSET_MIN) &&
2784       (Offset <= Hexagon_ADDI_OFFSET_MAX);
2785 
2786   case Hexagon::L4_iadd_memopw_io:
2787   case Hexagon::L4_isub_memopw_io:
2788   case Hexagon::L4_add_memopw_io:
2789   case Hexagon::L4_sub_memopw_io:
2790   case Hexagon::L4_and_memopw_io:
2791   case Hexagon::L4_or_memopw_io:
2792     return (0 <= Offset && Offset <= 255);
2793 
2794   case Hexagon::L4_iadd_memoph_io:
2795   case Hexagon::L4_isub_memoph_io:
2796   case Hexagon::L4_add_memoph_io:
2797   case Hexagon::L4_sub_memoph_io:
2798   case Hexagon::L4_and_memoph_io:
2799   case Hexagon::L4_or_memoph_io:
2800     return (0 <= Offset && Offset <= 127);
2801 
2802   case Hexagon::L4_iadd_memopb_io:
2803   case Hexagon::L4_isub_memopb_io:
2804   case Hexagon::L4_add_memopb_io:
2805   case Hexagon::L4_sub_memopb_io:
2806   case Hexagon::L4_and_memopb_io:
2807   case Hexagon::L4_or_memopb_io:
2808     return (0 <= Offset && Offset <= 63);
2809 
2810   // LDriw_xxx and STriw_xxx are pseudo operations, so it has to take offset of
2811   // any size. Later pass knows how to handle it.
2812   case Hexagon::STriw_pred:
2813   case Hexagon::LDriw_pred:
2814   case Hexagon::STriw_ctr:
2815   case Hexagon::LDriw_ctr:
2816     return true;
2817 
2818   case Hexagon::PS_fi:
2819   case Hexagon::PS_fia:
2820   case Hexagon::INLINEASM:
2821     return true;
2822 
2823   case Hexagon::L2_ploadrbt_io:
2824   case Hexagon::L2_ploadrbf_io:
2825   case Hexagon::L2_ploadrubt_io:
2826   case Hexagon::L2_ploadrubf_io:
2827   case Hexagon::S2_pstorerbt_io:
2828   case Hexagon::S2_pstorerbf_io:
2829     return isUInt<6>(Offset);
2830 
2831   case Hexagon::L2_ploadrht_io:
2832   case Hexagon::L2_ploadrhf_io:
2833   case Hexagon::L2_ploadruht_io:
2834   case Hexagon::L2_ploadruhf_io:
2835   case Hexagon::S2_pstorerht_io:
2836   case Hexagon::S2_pstorerhf_io:
2837     return isShiftedUInt<6,1>(Offset);
2838 
2839   case Hexagon::L2_ploadrit_io:
2840   case Hexagon::L2_ploadrif_io:
2841   case Hexagon::S2_pstorerit_io:
2842   case Hexagon::S2_pstorerif_io:
2843     return isShiftedUInt<6,2>(Offset);
2844 
2845   case Hexagon::L2_ploadrdt_io:
2846   case Hexagon::L2_ploadrdf_io:
2847   case Hexagon::S2_pstorerdt_io:
2848   case Hexagon::S2_pstorerdf_io:
2849     return isShiftedUInt<6,3>(Offset);
2850   } // switch
2851 
2852   llvm_unreachable("No offset range is defined for this opcode. "
2853                    "Please define it in the above switch statement!");
2854 }
2855 
2856 bool HexagonInstrInfo::isVecAcc(const MachineInstr &MI) const {
2857   return isHVXVec(MI) && isAccumulator(MI);
2858 }
2859 
2860 bool HexagonInstrInfo::isVecALU(const MachineInstr &MI) const {
2861   const uint64_t F = get(MI.getOpcode()).TSFlags;
2862   const uint64_t V = ((F >> HexagonII::TypePos) & HexagonII::TypeMask);
2863   return
2864     V == HexagonII::TypeCVI_VA         ||
2865     V == HexagonII::TypeCVI_VA_DV;
2866 }
2867 
2868 bool HexagonInstrInfo::isVecUsableNextPacket(const MachineInstr &ProdMI,
2869       const MachineInstr &ConsMI) const {
2870   if (EnableACCForwarding && isVecAcc(ProdMI) && isVecAcc(ConsMI))
2871     return true;
2872 
2873   if (EnableALUForwarding && (isVecALU(ConsMI) || isLateSourceInstr(ConsMI)))
2874     return true;
2875 
2876   if (mayBeNewStore(ConsMI))
2877     return true;
2878 
2879   return false;
2880 }
2881 
2882 bool HexagonInstrInfo::isZeroExtendingLoad(const MachineInstr &MI) const {
2883   switch (MI.getOpcode()) {
2884   // Byte
2885   case Hexagon::L2_loadrub_io:
2886   case Hexagon::L4_loadrub_ur:
2887   case Hexagon::L4_loadrub_ap:
2888   case Hexagon::L2_loadrub_pr:
2889   case Hexagon::L2_loadrub_pbr:
2890   case Hexagon::L2_loadrub_pi:
2891   case Hexagon::L2_loadrub_pci:
2892   case Hexagon::L2_loadrub_pcr:
2893   case Hexagon::L2_loadbzw2_io:
2894   case Hexagon::L4_loadbzw2_ur:
2895   case Hexagon::L4_loadbzw2_ap:
2896   case Hexagon::L2_loadbzw2_pr:
2897   case Hexagon::L2_loadbzw2_pbr:
2898   case Hexagon::L2_loadbzw2_pi:
2899   case Hexagon::L2_loadbzw2_pci:
2900   case Hexagon::L2_loadbzw2_pcr:
2901   case Hexagon::L2_loadbzw4_io:
2902   case Hexagon::L4_loadbzw4_ur:
2903   case Hexagon::L4_loadbzw4_ap:
2904   case Hexagon::L2_loadbzw4_pr:
2905   case Hexagon::L2_loadbzw4_pbr:
2906   case Hexagon::L2_loadbzw4_pi:
2907   case Hexagon::L2_loadbzw4_pci:
2908   case Hexagon::L2_loadbzw4_pcr:
2909   case Hexagon::L4_loadrub_rr:
2910   case Hexagon::L2_ploadrubt_io:
2911   case Hexagon::L2_ploadrubt_pi:
2912   case Hexagon::L2_ploadrubf_io:
2913   case Hexagon::L2_ploadrubf_pi:
2914   case Hexagon::L2_ploadrubtnew_io:
2915   case Hexagon::L2_ploadrubfnew_io:
2916   case Hexagon::L4_ploadrubt_rr:
2917   case Hexagon::L4_ploadrubf_rr:
2918   case Hexagon::L4_ploadrubtnew_rr:
2919   case Hexagon::L4_ploadrubfnew_rr:
2920   case Hexagon::L2_ploadrubtnew_pi:
2921   case Hexagon::L2_ploadrubfnew_pi:
2922   case Hexagon::L4_ploadrubt_abs:
2923   case Hexagon::L4_ploadrubf_abs:
2924   case Hexagon::L4_ploadrubtnew_abs:
2925   case Hexagon::L4_ploadrubfnew_abs:
2926   case Hexagon::L2_loadrubgp:
2927   // Half
2928   case Hexagon::L2_loadruh_io:
2929   case Hexagon::L4_loadruh_ur:
2930   case Hexagon::L4_loadruh_ap:
2931   case Hexagon::L2_loadruh_pr:
2932   case Hexagon::L2_loadruh_pbr:
2933   case Hexagon::L2_loadruh_pi:
2934   case Hexagon::L2_loadruh_pci:
2935   case Hexagon::L2_loadruh_pcr:
2936   case Hexagon::L4_loadruh_rr:
2937   case Hexagon::L2_ploadruht_io:
2938   case Hexagon::L2_ploadruht_pi:
2939   case Hexagon::L2_ploadruhf_io:
2940   case Hexagon::L2_ploadruhf_pi:
2941   case Hexagon::L2_ploadruhtnew_io:
2942   case Hexagon::L2_ploadruhfnew_io:
2943   case Hexagon::L4_ploadruht_rr:
2944   case Hexagon::L4_ploadruhf_rr:
2945   case Hexagon::L4_ploadruhtnew_rr:
2946   case Hexagon::L4_ploadruhfnew_rr:
2947   case Hexagon::L2_ploadruhtnew_pi:
2948   case Hexagon::L2_ploadruhfnew_pi:
2949   case Hexagon::L4_ploadruht_abs:
2950   case Hexagon::L4_ploadruhf_abs:
2951   case Hexagon::L4_ploadruhtnew_abs:
2952   case Hexagon::L4_ploadruhfnew_abs:
2953   case Hexagon::L2_loadruhgp:
2954     return true;
2955   default:
2956     return false;
2957   }
2958 }
2959 
2960 // Add latency to instruction.
2961 bool HexagonInstrInfo::addLatencyToSchedule(const MachineInstr &MI1,
2962       const MachineInstr &MI2) const {
2963   if (isHVXVec(MI1) && isHVXVec(MI2))
2964     if (!isVecUsableNextPacket(MI1, MI2))
2965       return true;
2966   return false;
2967 }
2968 
2969 /// Get the base register and byte offset of a load/store instr.
2970 bool HexagonInstrInfo::getMemOperandsWithOffsetWidth(
2971     const MachineInstr &LdSt, SmallVectorImpl<const MachineOperand *> &BaseOps,
2972     int64_t &Offset, bool &OffsetIsScalable, unsigned &Width,
2973     const TargetRegisterInfo *TRI) const {
2974   OffsetIsScalable = false;
2975   const MachineOperand *BaseOp = getBaseAndOffset(LdSt, Offset, Width);
2976   if (!BaseOp || !BaseOp->isReg())
2977     return false;
2978   BaseOps.push_back(BaseOp);
2979   return true;
2980 }
2981 
2982 /// Can these instructions execute at the same time in a bundle.
2983 bool HexagonInstrInfo::canExecuteInBundle(const MachineInstr &First,
2984       const MachineInstr &Second) const {
2985   if (Second.mayStore() && First.getOpcode() == Hexagon::S2_allocframe) {
2986     const MachineOperand &Op = Second.getOperand(0);
2987     if (Op.isReg() && Op.isUse() && Op.getReg() == Hexagon::R29)
2988       return true;
2989   }
2990   if (DisableNVSchedule)
2991     return false;
2992   if (mayBeNewStore(Second)) {
2993     // Make sure the definition of the first instruction is the value being
2994     // stored.
2995     const MachineOperand &Stored =
2996       Second.getOperand(Second.getNumOperands() - 1);
2997     if (!Stored.isReg())
2998       return false;
2999     for (unsigned i = 0, e = First.getNumOperands(); i < e; ++i) {
3000       const MachineOperand &Op = First.getOperand(i);
3001       if (Op.isReg() && Op.isDef() && Op.getReg() == Stored.getReg())
3002         return true;
3003     }
3004   }
3005   return false;
3006 }
3007 
3008 bool HexagonInstrInfo::doesNotReturn(const MachineInstr &CallMI) const {
3009   unsigned Opc = CallMI.getOpcode();
3010   return Opc == Hexagon::PS_call_nr || Opc == Hexagon::PS_callr_nr;
3011 }
3012 
3013 bool HexagonInstrInfo::hasEHLabel(const MachineBasicBlock *B) const {
3014   for (auto &I : *B)
3015     if (I.isEHLabel())
3016       return true;
3017   return false;
3018 }
3019 
3020 // Returns true if an instruction can be converted into a non-extended
3021 // equivalent instruction.
3022 bool HexagonInstrInfo::hasNonExtEquivalent(const MachineInstr &MI) const {
3023   short NonExtOpcode;
3024   // Check if the instruction has a register form that uses register in place
3025   // of the extended operand, if so return that as the non-extended form.
3026   if (Hexagon::getRegForm(MI.getOpcode()) >= 0)
3027     return true;
3028 
3029   if (MI.getDesc().mayLoad() || MI.getDesc().mayStore()) {
3030     // Check addressing mode and retrieve non-ext equivalent instruction.
3031 
3032     switch (getAddrMode(MI)) {
3033     case HexagonII::Absolute:
3034       // Load/store with absolute addressing mode can be converted into
3035       // base+offset mode.
3036       NonExtOpcode = Hexagon::changeAddrMode_abs_io(MI.getOpcode());
3037       break;
3038     case HexagonII::BaseImmOffset:
3039       // Load/store with base+offset addressing mode can be converted into
3040       // base+register offset addressing mode. However left shift operand should
3041       // be set to 0.
3042       NonExtOpcode = Hexagon::changeAddrMode_io_rr(MI.getOpcode());
3043       break;
3044     case HexagonII::BaseLongOffset:
3045       NonExtOpcode = Hexagon::changeAddrMode_ur_rr(MI.getOpcode());
3046       break;
3047     default:
3048       return false;
3049     }
3050     if (NonExtOpcode < 0)
3051       return false;
3052     return true;
3053   }
3054   return false;
3055 }
3056 
3057 bool HexagonInstrInfo::hasPseudoInstrPair(const MachineInstr &MI) const {
3058   return Hexagon::getRealHWInstr(MI.getOpcode(),
3059                                  Hexagon::InstrType_Pseudo) >= 0;
3060 }
3061 
3062 bool HexagonInstrInfo::hasUncondBranch(const MachineBasicBlock *B)
3063       const {
3064   MachineBasicBlock::const_iterator I = B->getFirstTerminator(), E = B->end();
3065   while (I != E) {
3066     if (I->isBarrier())
3067       return true;
3068     ++I;
3069   }
3070   return false;
3071 }
3072 
3073 // Returns true, if a LD insn can be promoted to a cur load.
3074 bool HexagonInstrInfo::mayBeCurLoad(const MachineInstr &MI) const {
3075   const uint64_t F = MI.getDesc().TSFlags;
3076   return ((F >> HexagonII::mayCVLoadPos) & HexagonII::mayCVLoadMask) &&
3077          Subtarget.hasV60Ops();
3078 }
3079 
3080 // Returns true, if a ST insn can be promoted to a new-value store.
3081 bool HexagonInstrInfo::mayBeNewStore(const MachineInstr &MI) const {
3082   if (MI.mayStore() && !Subtarget.useNewValueStores())
3083     return false;
3084 
3085   const uint64_t F = MI.getDesc().TSFlags;
3086   return (F >> HexagonII::mayNVStorePos) & HexagonII::mayNVStoreMask;
3087 }
3088 
3089 bool HexagonInstrInfo::producesStall(const MachineInstr &ProdMI,
3090       const MachineInstr &ConsMI) const {
3091   // There is no stall when ProdMI is not a V60 vector.
3092   if (!isHVXVec(ProdMI))
3093     return false;
3094 
3095   // There is no stall when ProdMI and ConsMI are not dependent.
3096   if (!isDependent(ProdMI, ConsMI))
3097     return false;
3098 
3099   // When Forward Scheduling is enabled, there is no stall if ProdMI and ConsMI
3100   // are scheduled in consecutive packets.
3101   if (isVecUsableNextPacket(ProdMI, ConsMI))
3102     return false;
3103 
3104   return true;
3105 }
3106 
3107 bool HexagonInstrInfo::producesStall(const MachineInstr &MI,
3108       MachineBasicBlock::const_instr_iterator BII) const {
3109   // There is no stall when I is not a V60 vector.
3110   if (!isHVXVec(MI))
3111     return false;
3112 
3113   MachineBasicBlock::const_instr_iterator MII = BII;
3114   MachineBasicBlock::const_instr_iterator MIE = MII->getParent()->instr_end();
3115 
3116   if (!MII->isBundle())
3117     return producesStall(*MII, MI);
3118 
3119   for (++MII; MII != MIE && MII->isInsideBundle(); ++MII) {
3120     const MachineInstr &J = *MII;
3121     if (producesStall(J, MI))
3122       return true;
3123   }
3124   return false;
3125 }
3126 
3127 bool HexagonInstrInfo::predCanBeUsedAsDotNew(const MachineInstr &MI,
3128       unsigned PredReg) const {
3129   for (const MachineOperand &MO : MI.operands()) {
3130     // Predicate register must be explicitly defined.
3131     if (MO.isRegMask() && MO.clobbersPhysReg(PredReg))
3132       return false;
3133     if (MO.isReg() && MO.isDef() && MO.isImplicit() && (MO.getReg() == PredReg))
3134       return false;
3135   }
3136 
3137   // Instruction that produce late predicate cannot be used as sources of
3138   // dot-new.
3139   switch (MI.getOpcode()) {
3140     case Hexagon::A4_addp_c:
3141     case Hexagon::A4_subp_c:
3142     case Hexagon::A4_tlbmatch:
3143     case Hexagon::A5_ACS:
3144     case Hexagon::F2_sfinvsqrta:
3145     case Hexagon::F2_sfrecipa:
3146     case Hexagon::J2_endloop0:
3147     case Hexagon::J2_endloop01:
3148     case Hexagon::J2_ploop1si:
3149     case Hexagon::J2_ploop1sr:
3150     case Hexagon::J2_ploop2si:
3151     case Hexagon::J2_ploop2sr:
3152     case Hexagon::J2_ploop3si:
3153     case Hexagon::J2_ploop3sr:
3154     case Hexagon::S2_cabacdecbin:
3155     case Hexagon::S2_storew_locked:
3156     case Hexagon::S4_stored_locked:
3157       return false;
3158   }
3159   return true;
3160 }
3161 
3162 bool HexagonInstrInfo::PredOpcodeHasJMP_c(unsigned Opcode) const {
3163   return Opcode == Hexagon::J2_jumpt      ||
3164          Opcode == Hexagon::J2_jumptpt    ||
3165          Opcode == Hexagon::J2_jumpf      ||
3166          Opcode == Hexagon::J2_jumpfpt    ||
3167          Opcode == Hexagon::J2_jumptnew   ||
3168          Opcode == Hexagon::J2_jumpfnew   ||
3169          Opcode == Hexagon::J2_jumptnewpt ||
3170          Opcode == Hexagon::J2_jumpfnewpt;
3171 }
3172 
3173 bool HexagonInstrInfo::predOpcodeHasNot(ArrayRef<MachineOperand> Cond) const {
3174   if (Cond.empty() || !isPredicated(Cond[0].getImm()))
3175     return false;
3176   return !isPredicatedTrue(Cond[0].getImm());
3177 }
3178 
3179 unsigned HexagonInstrInfo::getAddrMode(const MachineInstr &MI) const {
3180   const uint64_t F = MI.getDesc().TSFlags;
3181   return (F >> HexagonII::AddrModePos) & HexagonII::AddrModeMask;
3182 }
3183 
3184 // Returns the base register in a memory access (load/store). The offset is
3185 // returned in Offset and the access size is returned in AccessSize.
3186 // If the base operand has a subregister or the offset field does not contain
3187 // an immediate value, return nullptr.
3188 MachineOperand *HexagonInstrInfo::getBaseAndOffset(const MachineInstr &MI,
3189                                                    int64_t &Offset,
3190                                                    unsigned &AccessSize) const {
3191   // Return if it is not a base+offset type instruction or a MemOp.
3192   if (getAddrMode(MI) != HexagonII::BaseImmOffset &&
3193       getAddrMode(MI) != HexagonII::BaseLongOffset &&
3194       !isMemOp(MI) && !isPostIncrement(MI))
3195     return nullptr;
3196 
3197   AccessSize = getMemAccessSize(MI);
3198 
3199   unsigned BasePos = 0, OffsetPos = 0;
3200   if (!getBaseAndOffsetPosition(MI, BasePos, OffsetPos))
3201     return nullptr;
3202 
3203   // Post increment updates its EA after the mem access,
3204   // so we need to treat its offset as zero.
3205   if (isPostIncrement(MI)) {
3206     Offset = 0;
3207   } else {
3208     const MachineOperand &OffsetOp = MI.getOperand(OffsetPos);
3209     if (!OffsetOp.isImm())
3210       return nullptr;
3211     Offset = OffsetOp.getImm();
3212   }
3213 
3214   const MachineOperand &BaseOp = MI.getOperand(BasePos);
3215   if (BaseOp.getSubReg() != 0)
3216     return nullptr;
3217   return &const_cast<MachineOperand&>(BaseOp);
3218 }
3219 
3220 /// Return the position of the base and offset operands for this instruction.
3221 bool HexagonInstrInfo::getBaseAndOffsetPosition(const MachineInstr &MI,
3222       unsigned &BasePos, unsigned &OffsetPos) const {
3223   if (!isAddrModeWithOffset(MI) && !isPostIncrement(MI))
3224     return false;
3225 
3226   // Deal with memops first.
3227   if (isMemOp(MI)) {
3228     BasePos = 0;
3229     OffsetPos = 1;
3230   } else if (MI.mayStore()) {
3231     BasePos = 0;
3232     OffsetPos = 1;
3233   } else if (MI.mayLoad()) {
3234     BasePos = 1;
3235     OffsetPos = 2;
3236   } else
3237     return false;
3238 
3239   if (isPredicated(MI)) {
3240     BasePos++;
3241     OffsetPos++;
3242   }
3243   if (isPostIncrement(MI)) {
3244     BasePos++;
3245     OffsetPos++;
3246   }
3247 
3248   if (!MI.getOperand(BasePos).isReg() || !MI.getOperand(OffsetPos).isImm())
3249     return false;
3250 
3251   return true;
3252 }
3253 
3254 // Inserts branching instructions in reverse order of their occurrence.
3255 // e.g. jump_t t1 (i1)
3256 // jump t2        (i2)
3257 // Jumpers = {i2, i1}
3258 SmallVector<MachineInstr*, 2> HexagonInstrInfo::getBranchingInstrs(
3259       MachineBasicBlock& MBB) const {
3260   SmallVector<MachineInstr*, 2> Jumpers;
3261   // If the block has no terminators, it just falls into the block after it.
3262   MachineBasicBlock::instr_iterator I = MBB.instr_end();
3263   if (I == MBB.instr_begin())
3264     return Jumpers;
3265 
3266   // A basic block may looks like this:
3267   //
3268   //  [   insn
3269   //     EH_LABEL
3270   //      insn
3271   //      insn
3272   //      insn
3273   //     EH_LABEL
3274   //      insn     ]
3275   //
3276   // It has two succs but does not have a terminator
3277   // Don't know how to handle it.
3278   do {
3279     --I;
3280     if (I->isEHLabel())
3281       return Jumpers;
3282   } while (I != MBB.instr_begin());
3283 
3284   I = MBB.instr_end();
3285   --I;
3286 
3287   while (I->isDebugInstr()) {
3288     if (I == MBB.instr_begin())
3289       return Jumpers;
3290     --I;
3291   }
3292   if (!isUnpredicatedTerminator(*I))
3293     return Jumpers;
3294 
3295   // Get the last instruction in the block.
3296   MachineInstr *LastInst = &*I;
3297   Jumpers.push_back(LastInst);
3298   MachineInstr *SecondLastInst = nullptr;
3299   // Find one more terminator if present.
3300   do {
3301     if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(*I)) {
3302       if (!SecondLastInst) {
3303         SecondLastInst = &*I;
3304         Jumpers.push_back(SecondLastInst);
3305       } else // This is a third branch.
3306         return Jumpers;
3307     }
3308     if (I == MBB.instr_begin())
3309       break;
3310     --I;
3311   } while (true);
3312   return Jumpers;
3313 }
3314 
3315 // Returns Operand Index for the constant extended instruction.
3316 unsigned HexagonInstrInfo::getCExtOpNum(const MachineInstr &MI) const {
3317   const uint64_t F = MI.getDesc().TSFlags;
3318   return (F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask;
3319 }
3320 
3321 // See if instruction could potentially be a duplex candidate.
3322 // If so, return its group. Zero otherwise.
3323 HexagonII::CompoundGroup HexagonInstrInfo::getCompoundCandidateGroup(
3324       const MachineInstr &MI) const {
3325   unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
3326 
3327   switch (MI.getOpcode()) {
3328   default:
3329     return HexagonII::HCG_None;
3330   //
3331   // Compound pairs.
3332   // "p0=cmp.eq(Rs16,Rt16); if (p0.new) jump:nt #r9:2"
3333   // "Rd16=#U6 ; jump #r9:2"
3334   // "Rd16=Rs16 ; jump #r9:2"
3335   //
3336   case Hexagon::C2_cmpeq:
3337   case Hexagon::C2_cmpgt:
3338   case Hexagon::C2_cmpgtu:
3339     DstReg = MI.getOperand(0).getReg();
3340     Src1Reg = MI.getOperand(1).getReg();
3341     Src2Reg = MI.getOperand(2).getReg();
3342     if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3343         (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
3344         isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg))
3345       return HexagonII::HCG_A;
3346     break;
3347   case Hexagon::C2_cmpeqi:
3348   case Hexagon::C2_cmpgti:
3349   case Hexagon::C2_cmpgtui:
3350     // P0 = cmp.eq(Rs,#u2)
3351     DstReg = MI.getOperand(0).getReg();
3352     SrcReg = MI.getOperand(1).getReg();
3353     if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3354         (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
3355         isIntRegForSubInst(SrcReg) && MI.getOperand(2).isImm() &&
3356         ((isUInt<5>(MI.getOperand(2).getImm())) ||
3357          (MI.getOperand(2).getImm() == -1)))
3358       return HexagonII::HCG_A;
3359     break;
3360   case Hexagon::A2_tfr:
3361     // Rd = Rs
3362     DstReg = MI.getOperand(0).getReg();
3363     SrcReg = MI.getOperand(1).getReg();
3364     if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3365       return HexagonII::HCG_A;
3366     break;
3367   case Hexagon::A2_tfrsi:
3368     // Rd = #u6
3369     // Do not test for #u6 size since the const is getting extended
3370     // regardless and compound could be formed.
3371     DstReg = MI.getOperand(0).getReg();
3372     if (isIntRegForSubInst(DstReg))
3373       return HexagonII::HCG_A;
3374     break;
3375   case Hexagon::S2_tstbit_i:
3376     DstReg = MI.getOperand(0).getReg();
3377     Src1Reg = MI.getOperand(1).getReg();
3378     if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3379         (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
3380         MI.getOperand(2).isImm() &&
3381         isIntRegForSubInst(Src1Reg) && (MI.getOperand(2).getImm() == 0))
3382       return HexagonII::HCG_A;
3383     break;
3384   // The fact that .new form is used pretty much guarantees
3385   // that predicate register will match. Nevertheless,
3386   // there could be some false positives without additional
3387   // checking.
3388   case Hexagon::J2_jumptnew:
3389   case Hexagon::J2_jumpfnew:
3390   case Hexagon::J2_jumptnewpt:
3391   case Hexagon::J2_jumpfnewpt:
3392     Src1Reg = MI.getOperand(0).getReg();
3393     if (Hexagon::PredRegsRegClass.contains(Src1Reg) &&
3394         (Hexagon::P0 == Src1Reg || Hexagon::P1 == Src1Reg))
3395       return HexagonII::HCG_B;
3396     break;
3397   // Transfer and jump:
3398   // Rd=#U6 ; jump #r9:2
3399   // Rd=Rs ; jump #r9:2
3400   // Do not test for jump range here.
3401   case Hexagon::J2_jump:
3402   case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
3403   case Hexagon::RESTORE_DEALLOC_RET_JMP_V4_PIC:
3404     return HexagonII::HCG_C;
3405   }
3406 
3407   return HexagonII::HCG_None;
3408 }
3409 
3410 // Returns -1 when there is no opcode found.
3411 unsigned HexagonInstrInfo::getCompoundOpcode(const MachineInstr &GA,
3412       const MachineInstr &GB) const {
3413   assert(getCompoundCandidateGroup(GA) == HexagonII::HCG_A);
3414   assert(getCompoundCandidateGroup(GB) == HexagonII::HCG_B);
3415   if ((GA.getOpcode() != Hexagon::C2_cmpeqi) ||
3416       (GB.getOpcode() != Hexagon::J2_jumptnew))
3417     return -1u;
3418   Register DestReg = GA.getOperand(0).getReg();
3419   if (!GB.readsRegister(DestReg))
3420     return -1u;
3421   if (DestReg != Hexagon::P0 && DestReg != Hexagon::P1)
3422     return -1u;
3423   // The value compared against must be either u5 or -1.
3424   const MachineOperand &CmpOp = GA.getOperand(2);
3425   if (!CmpOp.isImm())
3426     return -1u;
3427   int V = CmpOp.getImm();
3428   if (V == -1)
3429     return DestReg == Hexagon::P0 ? Hexagon::J4_cmpeqn1_tp0_jump_nt
3430                                   : Hexagon::J4_cmpeqn1_tp1_jump_nt;
3431   if (!isUInt<5>(V))
3432     return -1u;
3433   return DestReg == Hexagon::P0 ? Hexagon::J4_cmpeqi_tp0_jump_nt
3434                                 : Hexagon::J4_cmpeqi_tp1_jump_nt;
3435 }
3436 
3437 // Returns -1 if there is no opcode found.
3438 int HexagonInstrInfo::getDuplexOpcode(const MachineInstr &MI,
3439                                       bool ForBigCore) const {
3440   // Static table to switch the opcodes across Tiny Core and Big Core.
3441   // dup_ opcodes are Big core opcodes.
3442   // NOTE: There are special instructions that need to handled later.
3443   // L4_return* instructions, they will only occupy SLOT0 (on big core too).
3444   // PS_jmpret - This pseudo translates to J2_jumpr which occupies only SLOT2.
3445   // The compiler need to base the root instruction to L6_return_map_to_raw
3446   // which can go any slot.
3447   static const std::map<unsigned, unsigned> DupMap = {
3448       {Hexagon::A2_add, Hexagon::dup_A2_add},
3449       {Hexagon::A2_addi, Hexagon::dup_A2_addi},
3450       {Hexagon::A2_andir, Hexagon::dup_A2_andir},
3451       {Hexagon::A2_combineii, Hexagon::dup_A2_combineii},
3452       {Hexagon::A2_sxtb, Hexagon::dup_A2_sxtb},
3453       {Hexagon::A2_sxth, Hexagon::dup_A2_sxth},
3454       {Hexagon::A2_tfr, Hexagon::dup_A2_tfr},
3455       {Hexagon::A2_tfrsi, Hexagon::dup_A2_tfrsi},
3456       {Hexagon::A2_zxtb, Hexagon::dup_A2_zxtb},
3457       {Hexagon::A2_zxth, Hexagon::dup_A2_zxth},
3458       {Hexagon::A4_combineii, Hexagon::dup_A4_combineii},
3459       {Hexagon::A4_combineir, Hexagon::dup_A4_combineir},
3460       {Hexagon::A4_combineri, Hexagon::dup_A4_combineri},
3461       {Hexagon::C2_cmoveif, Hexagon::dup_C2_cmoveif},
3462       {Hexagon::C2_cmoveit, Hexagon::dup_C2_cmoveit},
3463       {Hexagon::C2_cmovenewif, Hexagon::dup_C2_cmovenewif},
3464       {Hexagon::C2_cmovenewit, Hexagon::dup_C2_cmovenewit},
3465       {Hexagon::C2_cmpeqi, Hexagon::dup_C2_cmpeqi},
3466       {Hexagon::L2_deallocframe, Hexagon::dup_L2_deallocframe},
3467       {Hexagon::L2_loadrb_io, Hexagon::dup_L2_loadrb_io},
3468       {Hexagon::L2_loadrd_io, Hexagon::dup_L2_loadrd_io},
3469       {Hexagon::L2_loadrh_io, Hexagon::dup_L2_loadrh_io},
3470       {Hexagon::L2_loadri_io, Hexagon::dup_L2_loadri_io},
3471       {Hexagon::L2_loadrub_io, Hexagon::dup_L2_loadrub_io},
3472       {Hexagon::L2_loadruh_io, Hexagon::dup_L2_loadruh_io},
3473       {Hexagon::S2_allocframe, Hexagon::dup_S2_allocframe},
3474       {Hexagon::S2_storerb_io, Hexagon::dup_S2_storerb_io},
3475       {Hexagon::S2_storerd_io, Hexagon::dup_S2_storerd_io},
3476       {Hexagon::S2_storerh_io, Hexagon::dup_S2_storerh_io},
3477       {Hexagon::S2_storeri_io, Hexagon::dup_S2_storeri_io},
3478       {Hexagon::S4_storeirb_io, Hexagon::dup_S4_storeirb_io},
3479       {Hexagon::S4_storeiri_io, Hexagon::dup_S4_storeiri_io},
3480   };
3481   unsigned OpNum = MI.getOpcode();
3482   // Conversion to Big core.
3483   if (ForBigCore) {
3484     auto Iter = DupMap.find(OpNum);
3485     if (Iter != DupMap.end())
3486       return Iter->second;
3487   } else { // Conversion to Tiny core.
3488     for (auto Iter = DupMap.begin(), End = DupMap.end(); Iter != End; ++Iter)
3489       if (Iter->second == OpNum)
3490         return Iter->first;
3491   }
3492   return -1;
3493 }
3494 
3495 int HexagonInstrInfo::getCondOpcode(int Opc, bool invertPredicate) const {
3496   enum Hexagon::PredSense inPredSense;
3497   inPredSense = invertPredicate ? Hexagon::PredSense_false :
3498                                   Hexagon::PredSense_true;
3499   int CondOpcode = Hexagon::getPredOpcode(Opc, inPredSense);
3500   if (CondOpcode >= 0) // Valid Conditional opcode/instruction
3501     return CondOpcode;
3502 
3503   llvm_unreachable("Unexpected predicable instruction");
3504 }
3505 
3506 // Return the cur value instruction for a given store.
3507 int HexagonInstrInfo::getDotCurOp(const MachineInstr &MI) const {
3508   switch (MI.getOpcode()) {
3509   default: llvm_unreachable("Unknown .cur type");
3510   case Hexagon::V6_vL32b_pi:
3511     return Hexagon::V6_vL32b_cur_pi;
3512   case Hexagon::V6_vL32b_ai:
3513     return Hexagon::V6_vL32b_cur_ai;
3514   case Hexagon::V6_vL32b_nt_pi:
3515     return Hexagon::V6_vL32b_nt_cur_pi;
3516   case Hexagon::V6_vL32b_nt_ai:
3517     return Hexagon::V6_vL32b_nt_cur_ai;
3518   }
3519   return 0;
3520 }
3521 
3522 // Return the regular version of the .cur instruction.
3523 int HexagonInstrInfo::getNonDotCurOp(const MachineInstr &MI) const {
3524   switch (MI.getOpcode()) {
3525   default: llvm_unreachable("Unknown .cur type");
3526   case Hexagon::V6_vL32b_cur_pi:
3527     return Hexagon::V6_vL32b_pi;
3528   case Hexagon::V6_vL32b_cur_ai:
3529     return Hexagon::V6_vL32b_ai;
3530   case Hexagon::V6_vL32b_nt_cur_pi:
3531     return Hexagon::V6_vL32b_nt_pi;
3532   case Hexagon::V6_vL32b_nt_cur_ai:
3533     return Hexagon::V6_vL32b_nt_ai;
3534   }
3535   return 0;
3536 }
3537 
3538 // The diagram below shows the steps involved in the conversion of a predicated
3539 // store instruction to its .new predicated new-value form.
3540 //
3541 // Note: It doesn't include conditional new-value stores as they can't be
3542 // converted to .new predicate.
3543 //
3544 //               p.new NV store [ if(p0.new)memw(R0+#0)=R2.new ]
3545 //                ^           ^
3546 //               /             \ (not OK. it will cause new-value store to be
3547 //              /               X conditional on p0.new while R2 producer is
3548 //             /                 \ on p0)
3549 //            /                   \.
3550 //     p.new store                 p.old NV store
3551 // [if(p0.new)memw(R0+#0)=R2]    [if(p0)memw(R0+#0)=R2.new]
3552 //            ^                  ^
3553 //             \                /
3554 //              \              /
3555 //               \            /
3556 //                 p.old store
3557 //             [if (p0)memw(R0+#0)=R2]
3558 //
3559 // The following set of instructions further explains the scenario where
3560 // conditional new-value store becomes invalid when promoted to .new predicate
3561 // form.
3562 //
3563 // { 1) if (p0) r0 = add(r1, r2)
3564 //   2) p0 = cmp.eq(r3, #0) }
3565 //
3566 //   3) if (p0) memb(r1+#0) = r0  --> this instruction can't be grouped with
3567 // the first two instructions because in instr 1, r0 is conditional on old value
3568 // of p0 but its use in instr 3 is conditional on p0 modified by instr 2 which
3569 // is not valid for new-value stores.
3570 // Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
3571 // from the "Conditional Store" list. Because a predicated new value store
3572 // would NOT be promoted to a double dot new store. See diagram below:
3573 // This function returns yes for those stores that are predicated but not
3574 // yet promoted to predicate dot new instructions.
3575 //
3576 //                          +---------------------+
3577 //                    /-----| if (p0) memw(..)=r0 |---------\~
3578 //                   ||     +---------------------+         ||
3579 //          promote  ||       /\       /\                   ||  promote
3580 //                   ||      /||\     /||\                  ||
3581 //                  \||/    demote     ||                  \||/
3582 //                   \/       ||       ||                   \/
3583 //       +-------------------------+   ||   +-------------------------+
3584 //       | if (p0.new) memw(..)=r0 |   ||   | if (p0) memw(..)=r0.new |
3585 //       +-------------------------+   ||   +-------------------------+
3586 //                        ||           ||         ||
3587 //                        ||         demote      \||/
3588 //                      promote        ||         \/ NOT possible
3589 //                        ||           ||         /\~
3590 //                       \||/          ||        /||\~
3591 //                        \/           ||         ||
3592 //                      +-----------------------------+
3593 //                      | if (p0.new) memw(..)=r0.new |
3594 //                      +-----------------------------+
3595 //                           Double Dot New Store
3596 //
3597 // Returns the most basic instruction for the .new predicated instructions and
3598 // new-value stores.
3599 // For example, all of the following instructions will be converted back to the
3600 // same instruction:
3601 // 1) if (p0.new) memw(R0+#0) = R1.new  --->
3602 // 2) if (p0) memw(R0+#0)= R1.new      -------> if (p0) memw(R0+#0) = R1
3603 // 3) if (p0.new) memw(R0+#0) = R1      --->
3604 //
3605 // To understand the translation of instruction 1 to its original form, consider
3606 // a packet with 3 instructions.
3607 // { p0 = cmp.eq(R0,R1)
3608 //   if (p0.new) R2 = add(R3, R4)
3609 //   R5 = add (R3, R1)
3610 // }
3611 // if (p0) memw(R5+#0) = R2 <--- trying to include it in the previous packet
3612 //
3613 // This instruction can be part of the previous packet only if both p0 and R2
3614 // are promoted to .new values. This promotion happens in steps, first
3615 // predicate register is promoted to .new and in the next iteration R2 is
3616 // promoted. Therefore, in case of dependence check failure (due to R5) during
3617 // next iteration, it should be converted back to its most basic form.
3618 
3619 // Return the new value instruction for a given store.
3620 int HexagonInstrInfo::getDotNewOp(const MachineInstr &MI) const {
3621   int NVOpcode = Hexagon::getNewValueOpcode(MI.getOpcode());
3622   if (NVOpcode >= 0) // Valid new-value store instruction.
3623     return NVOpcode;
3624 
3625   switch (MI.getOpcode()) {
3626   default:
3627     report_fatal_error(Twine("Unknown .new type: ") +
3628                        std::to_string(MI.getOpcode()));
3629   case Hexagon::S4_storerb_ur:
3630     return Hexagon::S4_storerbnew_ur;
3631 
3632   case Hexagon::S2_storerb_pci:
3633     return Hexagon::S2_storerb_pci;
3634 
3635   case Hexagon::S2_storeri_pci:
3636     return Hexagon::S2_storeri_pci;
3637 
3638   case Hexagon::S2_storerh_pci:
3639     return Hexagon::S2_storerh_pci;
3640 
3641   case Hexagon::S2_storerd_pci:
3642     return Hexagon::S2_storerd_pci;
3643 
3644   case Hexagon::S2_storerf_pci:
3645     return Hexagon::S2_storerf_pci;
3646 
3647   case Hexagon::V6_vS32b_ai:
3648     return Hexagon::V6_vS32b_new_ai;
3649 
3650   case Hexagon::V6_vS32b_pi:
3651     return Hexagon::V6_vS32b_new_pi;
3652   }
3653   return 0;
3654 }
3655 
3656 // Returns the opcode to use when converting MI, which is a conditional jump,
3657 // into a conditional instruction which uses the .new value of the predicate.
3658 // We also use branch probabilities to add a hint to the jump.
3659 // If MBPI is null, all edges will be treated as equally likely for the
3660 // purposes of establishing a predication hint.
3661 int HexagonInstrInfo::getDotNewPredJumpOp(const MachineInstr &MI,
3662       const MachineBranchProbabilityInfo *MBPI) const {
3663   // We assume that block can have at most two successors.
3664   const MachineBasicBlock *Src = MI.getParent();
3665   const MachineOperand &BrTarget = MI.getOperand(1);
3666   bool Taken = false;
3667   const BranchProbability OneHalf(1, 2);
3668 
3669   auto getEdgeProbability = [MBPI] (const MachineBasicBlock *Src,
3670                                     const MachineBasicBlock *Dst) {
3671     if (MBPI)
3672       return MBPI->getEdgeProbability(Src, Dst);
3673     return BranchProbability(1, Src->succ_size());
3674   };
3675 
3676   if (BrTarget.isMBB()) {
3677     const MachineBasicBlock *Dst = BrTarget.getMBB();
3678     Taken = getEdgeProbability(Src, Dst) >= OneHalf;
3679   } else {
3680     // The branch target is not a basic block (most likely a function).
3681     // Since BPI only gives probabilities for targets that are basic blocks,
3682     // try to identify another target of this branch (potentially a fall-
3683     // -through) and check the probability of that target.
3684     //
3685     // The only handled branch combinations are:
3686     // - one conditional branch,
3687     // - one conditional branch followed by one unconditional branch.
3688     // Otherwise, assume not-taken.
3689     assert(MI.isConditionalBranch());
3690     const MachineBasicBlock &B = *MI.getParent();
3691     bool SawCond = false, Bad = false;
3692     for (const MachineInstr &I : B) {
3693       if (!I.isBranch())
3694         continue;
3695       if (I.isConditionalBranch()) {
3696         SawCond = true;
3697         if (&I != &MI) {
3698           Bad = true;
3699           break;
3700         }
3701       }
3702       if (I.isUnconditionalBranch() && !SawCond) {
3703         Bad = true;
3704         break;
3705       }
3706     }
3707     if (!Bad) {
3708       MachineBasicBlock::const_instr_iterator It(MI);
3709       MachineBasicBlock::const_instr_iterator NextIt = std::next(It);
3710       if (NextIt == B.instr_end()) {
3711         // If this branch is the last, look for the fall-through block.
3712         for (const MachineBasicBlock *SB : B.successors()) {
3713           if (!B.isLayoutSuccessor(SB))
3714             continue;
3715           Taken = getEdgeProbability(Src, SB) < OneHalf;
3716           break;
3717         }
3718       } else {
3719         assert(NextIt->isUnconditionalBranch());
3720         // Find the first MBB operand and assume it's the target.
3721         const MachineBasicBlock *BT = nullptr;
3722         for (const MachineOperand &Op : NextIt->operands()) {
3723           if (!Op.isMBB())
3724             continue;
3725           BT = Op.getMBB();
3726           break;
3727         }
3728         Taken = BT && getEdgeProbability(Src, BT) < OneHalf;
3729       }
3730     } // if (!Bad)
3731   }
3732 
3733   // The Taken flag should be set to something reasonable by this point.
3734 
3735   switch (MI.getOpcode()) {
3736   case Hexagon::J2_jumpt:
3737     return Taken ? Hexagon::J2_jumptnewpt : Hexagon::J2_jumptnew;
3738   case Hexagon::J2_jumpf:
3739     return Taken ? Hexagon::J2_jumpfnewpt : Hexagon::J2_jumpfnew;
3740 
3741   default:
3742     llvm_unreachable("Unexpected jump instruction.");
3743   }
3744 }
3745 
3746 // Return .new predicate version for an instruction.
3747 int HexagonInstrInfo::getDotNewPredOp(const MachineInstr &MI,
3748       const MachineBranchProbabilityInfo *MBPI) const {
3749   switch (MI.getOpcode()) {
3750   // Condtional Jumps
3751   case Hexagon::J2_jumpt:
3752   case Hexagon::J2_jumpf:
3753     return getDotNewPredJumpOp(MI, MBPI);
3754   }
3755 
3756   int NewOpcode = Hexagon::getPredNewOpcode(MI.getOpcode());
3757   if (NewOpcode >= 0)
3758     return NewOpcode;
3759   return 0;
3760 }
3761 
3762 int HexagonInstrInfo::getDotOldOp(const MachineInstr &MI) const {
3763   int NewOp = MI.getOpcode();
3764   if (isPredicated(NewOp) && isPredicatedNew(NewOp)) { // Get predicate old form
3765     NewOp = Hexagon::getPredOldOpcode(NewOp);
3766     // All Hexagon architectures have prediction bits on dot-new branches,
3767     // but only Hexagon V60+ has prediction bits on dot-old ones. Make sure
3768     // to pick the right opcode when converting back to dot-old.
3769     if (!Subtarget.getFeatureBits()[Hexagon::ArchV60]) {
3770       switch (NewOp) {
3771       case Hexagon::J2_jumptpt:
3772         NewOp = Hexagon::J2_jumpt;
3773         break;
3774       case Hexagon::J2_jumpfpt:
3775         NewOp = Hexagon::J2_jumpf;
3776         break;
3777       case Hexagon::J2_jumprtpt:
3778         NewOp = Hexagon::J2_jumprt;
3779         break;
3780       case Hexagon::J2_jumprfpt:
3781         NewOp = Hexagon::J2_jumprf;
3782         break;
3783       }
3784     }
3785     assert(NewOp >= 0 &&
3786            "Couldn't change predicate new instruction to its old form.");
3787   }
3788 
3789   if (isNewValueStore(NewOp)) { // Convert into non-new-value format
3790     NewOp = Hexagon::getNonNVStore(NewOp);
3791     assert(NewOp >= 0 && "Couldn't change new-value store to its old form.");
3792   }
3793 
3794   if (Subtarget.hasV60Ops())
3795     return NewOp;
3796 
3797   // Subtargets prior to V60 didn't support 'taken' forms of predicated jumps.
3798   switch (NewOp) {
3799   case Hexagon::J2_jumpfpt:
3800     return Hexagon::J2_jumpf;
3801   case Hexagon::J2_jumptpt:
3802     return Hexagon::J2_jumpt;
3803   case Hexagon::J2_jumprfpt:
3804     return Hexagon::J2_jumprf;
3805   case Hexagon::J2_jumprtpt:
3806     return Hexagon::J2_jumprt;
3807   }
3808   return NewOp;
3809 }
3810 
3811 // See if instruction could potentially be a duplex candidate.
3812 // If so, return its group. Zero otherwise.
3813 HexagonII::SubInstructionGroup HexagonInstrInfo::getDuplexCandidateGroup(
3814       const MachineInstr &MI) const {
3815   unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
3816   const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
3817 
3818   switch (MI.getOpcode()) {
3819   default:
3820     return HexagonII::HSIG_None;
3821   //
3822   // Group L1:
3823   //
3824   // Rd = memw(Rs+#u4:2)
3825   // Rd = memub(Rs+#u4:0)
3826   case Hexagon::L2_loadri_io:
3827   case Hexagon::dup_L2_loadri_io:
3828     DstReg = MI.getOperand(0).getReg();
3829     SrcReg = MI.getOperand(1).getReg();
3830     // Special case this one from Group L2.
3831     // Rd = memw(r29+#u5:2)
3832     if (isIntRegForSubInst(DstReg)) {
3833       if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
3834           HRI.getStackRegister() == SrcReg &&
3835           MI.getOperand(2).isImm() &&
3836           isShiftedUInt<5,2>(MI.getOperand(2).getImm()))
3837         return HexagonII::HSIG_L2;
3838       // Rd = memw(Rs+#u4:2)
3839       if (isIntRegForSubInst(SrcReg) &&
3840           (MI.getOperand(2).isImm() &&
3841           isShiftedUInt<4,2>(MI.getOperand(2).getImm())))
3842         return HexagonII::HSIG_L1;
3843     }
3844     break;
3845   case Hexagon::L2_loadrub_io:
3846   case Hexagon::dup_L2_loadrub_io:
3847     // Rd = memub(Rs+#u4:0)
3848     DstReg = MI.getOperand(0).getReg();
3849     SrcReg = MI.getOperand(1).getReg();
3850     if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
3851         MI.getOperand(2).isImm() && isUInt<4>(MI.getOperand(2).getImm()))
3852       return HexagonII::HSIG_L1;
3853     break;
3854   //
3855   // Group L2:
3856   //
3857   // Rd = memh/memuh(Rs+#u3:1)
3858   // Rd = memb(Rs+#u3:0)
3859   // Rd = memw(r29+#u5:2) - Handled above.
3860   // Rdd = memd(r29+#u5:3)
3861   // deallocframe
3862   // [if ([!]p0[.new])] dealloc_return
3863   // [if ([!]p0[.new])] jumpr r31
3864   case Hexagon::L2_loadrh_io:
3865   case Hexagon::L2_loadruh_io:
3866   case Hexagon::dup_L2_loadrh_io:
3867   case Hexagon::dup_L2_loadruh_io:
3868     // Rd = memh/memuh(Rs+#u3:1)
3869     DstReg = MI.getOperand(0).getReg();
3870     SrcReg = MI.getOperand(1).getReg();
3871     if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
3872         MI.getOperand(2).isImm() &&
3873         isShiftedUInt<3,1>(MI.getOperand(2).getImm()))
3874       return HexagonII::HSIG_L2;
3875     break;
3876   case Hexagon::L2_loadrb_io:
3877   case Hexagon::dup_L2_loadrb_io:
3878     // Rd = memb(Rs+#u3:0)
3879     DstReg = MI.getOperand(0).getReg();
3880     SrcReg = MI.getOperand(1).getReg();
3881     if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
3882         MI.getOperand(2).isImm() &&
3883         isUInt<3>(MI.getOperand(2).getImm()))
3884       return HexagonII::HSIG_L2;
3885     break;
3886   case Hexagon::L2_loadrd_io:
3887   case Hexagon::dup_L2_loadrd_io:
3888     // Rdd = memd(r29+#u5:3)
3889     DstReg = MI.getOperand(0).getReg();
3890     SrcReg = MI.getOperand(1).getReg();
3891     if (isDblRegForSubInst(DstReg, HRI) &&
3892         Hexagon::IntRegsRegClass.contains(SrcReg) &&
3893         HRI.getStackRegister() == SrcReg &&
3894         MI.getOperand(2).isImm() &&
3895         isShiftedUInt<5,3>(MI.getOperand(2).getImm()))
3896       return HexagonII::HSIG_L2;
3897     break;
3898   // dealloc_return is not documented in Hexagon Manual, but marked
3899   // with A_SUBINSN attribute in iset_v4classic.py.
3900   case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
3901   case Hexagon::RESTORE_DEALLOC_RET_JMP_V4_PIC:
3902   case Hexagon::L4_return:
3903   case Hexagon::L2_deallocframe:
3904   case Hexagon::dup_L2_deallocframe:
3905     return HexagonII::HSIG_L2;
3906   case Hexagon::EH_RETURN_JMPR:
3907   case Hexagon::PS_jmpret:
3908   case Hexagon::SL2_jumpr31:
3909     // jumpr r31
3910     // Actual form JMPR implicit-def %pc, implicit %r31, implicit internal %r0
3911     DstReg = MI.getOperand(0).getReg();
3912     if (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg))
3913       return HexagonII::HSIG_L2;
3914     break;
3915   case Hexagon::PS_jmprett:
3916   case Hexagon::PS_jmpretf:
3917   case Hexagon::PS_jmprettnewpt:
3918   case Hexagon::PS_jmpretfnewpt:
3919   case Hexagon::PS_jmprettnew:
3920   case Hexagon::PS_jmpretfnew:
3921   case Hexagon::SL2_jumpr31_t:
3922   case Hexagon::SL2_jumpr31_f:
3923   case Hexagon::SL2_jumpr31_tnew:
3924   case Hexagon::SL2_jumpr31_fnew:
3925     DstReg = MI.getOperand(1).getReg();
3926     SrcReg = MI.getOperand(0).getReg();
3927     // [if ([!]p0[.new])] jumpr r31
3928     if ((Hexagon::PredRegsRegClass.contains(SrcReg) &&
3929         (Hexagon::P0 == SrcReg)) &&
3930         (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg)))
3931       return HexagonII::HSIG_L2;
3932     break;
3933   case Hexagon::L4_return_t:
3934   case Hexagon::L4_return_f:
3935   case Hexagon::L4_return_tnew_pnt:
3936   case Hexagon::L4_return_fnew_pnt:
3937   case Hexagon::L4_return_tnew_pt:
3938   case Hexagon::L4_return_fnew_pt:
3939     // [if ([!]p0[.new])] dealloc_return
3940     SrcReg = MI.getOperand(0).getReg();
3941     if (Hexagon::PredRegsRegClass.contains(SrcReg) && (Hexagon::P0 == SrcReg))
3942       return HexagonII::HSIG_L2;
3943     break;
3944   //
3945   // Group S1:
3946   //
3947   // memw(Rs+#u4:2) = Rt
3948   // memb(Rs+#u4:0) = Rt
3949   case Hexagon::S2_storeri_io:
3950   case Hexagon::dup_S2_storeri_io:
3951     // Special case this one from Group S2.
3952     // memw(r29+#u5:2) = Rt
3953     Src1Reg = MI.getOperand(0).getReg();
3954     Src2Reg = MI.getOperand(2).getReg();
3955     if (Hexagon::IntRegsRegClass.contains(Src1Reg) &&
3956         isIntRegForSubInst(Src2Reg) &&
3957         HRI.getStackRegister() == Src1Reg && MI.getOperand(1).isImm() &&
3958         isShiftedUInt<5,2>(MI.getOperand(1).getImm()))
3959       return HexagonII::HSIG_S2;
3960     // memw(Rs+#u4:2) = Rt
3961     if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
3962         MI.getOperand(1).isImm() &&
3963         isShiftedUInt<4,2>(MI.getOperand(1).getImm()))
3964       return HexagonII::HSIG_S1;
3965     break;
3966   case Hexagon::S2_storerb_io:
3967   case Hexagon::dup_S2_storerb_io:
3968     // memb(Rs+#u4:0) = Rt
3969     Src1Reg = MI.getOperand(0).getReg();
3970     Src2Reg = MI.getOperand(2).getReg();
3971     if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
3972         MI.getOperand(1).isImm() && isUInt<4>(MI.getOperand(1).getImm()))
3973       return HexagonII::HSIG_S1;
3974     break;
3975   //
3976   // Group S2:
3977   //
3978   // memh(Rs+#u3:1) = Rt
3979   // memw(r29+#u5:2) = Rt
3980   // memd(r29+#s6:3) = Rtt
3981   // memw(Rs+#u4:2) = #U1
3982   // memb(Rs+#u4) = #U1
3983   // allocframe(#u5:3)
3984   case Hexagon::S2_storerh_io:
3985   case Hexagon::dup_S2_storerh_io:
3986     // memh(Rs+#u3:1) = Rt
3987     Src1Reg = MI.getOperand(0).getReg();
3988     Src2Reg = MI.getOperand(2).getReg();
3989     if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
3990         MI.getOperand(1).isImm() &&
3991         isShiftedUInt<3,1>(MI.getOperand(1).getImm()))
3992       return HexagonII::HSIG_S1;
3993     break;
3994   case Hexagon::S2_storerd_io:
3995   case Hexagon::dup_S2_storerd_io:
3996     // memd(r29+#s6:3) = Rtt
3997     Src1Reg = MI.getOperand(0).getReg();
3998     Src2Reg = MI.getOperand(2).getReg();
3999     if (isDblRegForSubInst(Src2Reg, HRI) &&
4000         Hexagon::IntRegsRegClass.contains(Src1Reg) &&
4001         HRI.getStackRegister() == Src1Reg && MI.getOperand(1).isImm() &&
4002         isShiftedInt<6,3>(MI.getOperand(1).getImm()))
4003       return HexagonII::HSIG_S2;
4004     break;
4005   case Hexagon::S4_storeiri_io:
4006   case Hexagon::dup_S4_storeiri_io:
4007     // memw(Rs+#u4:2) = #U1
4008     Src1Reg = MI.getOperand(0).getReg();
4009     if (isIntRegForSubInst(Src1Reg) && MI.getOperand(1).isImm() &&
4010         isShiftedUInt<4,2>(MI.getOperand(1).getImm()) &&
4011         MI.getOperand(2).isImm() && isUInt<1>(MI.getOperand(2).getImm()))
4012       return HexagonII::HSIG_S2;
4013     break;
4014   case Hexagon::S4_storeirb_io:
4015   case Hexagon::dup_S4_storeirb_io:
4016     // memb(Rs+#u4) = #U1
4017     Src1Reg = MI.getOperand(0).getReg();
4018     if (isIntRegForSubInst(Src1Reg) &&
4019         MI.getOperand(1).isImm() && isUInt<4>(MI.getOperand(1).getImm()) &&
4020         MI.getOperand(2).isImm() && isUInt<1>(MI.getOperand(2).getImm()))
4021       return HexagonII::HSIG_S2;
4022     break;
4023   case Hexagon::S2_allocframe:
4024   case Hexagon::dup_S2_allocframe:
4025     if (MI.getOperand(2).isImm() &&
4026         isShiftedUInt<5,3>(MI.getOperand(2).getImm()))
4027       return HexagonII::HSIG_S1;
4028     break;
4029   //
4030   // Group A:
4031   //
4032   // Rx = add(Rx,#s7)
4033   // Rd = Rs
4034   // Rd = #u6
4035   // Rd = #-1
4036   // if ([!]P0[.new]) Rd = #0
4037   // Rd = add(r29,#u6:2)
4038   // Rx = add(Rx,Rs)
4039   // P0 = cmp.eq(Rs,#u2)
4040   // Rdd = combine(#0,Rs)
4041   // Rdd = combine(Rs,#0)
4042   // Rdd = combine(#u2,#U2)
4043   // Rd = add(Rs,#1)
4044   // Rd = add(Rs,#-1)
4045   // Rd = sxth/sxtb/zxtb/zxth(Rs)
4046   // Rd = and(Rs,#1)
4047   case Hexagon::A2_addi:
4048   case Hexagon::dup_A2_addi:
4049     DstReg = MI.getOperand(0).getReg();
4050     SrcReg = MI.getOperand(1).getReg();
4051     if (isIntRegForSubInst(DstReg)) {
4052       // Rd = add(r29,#u6:2)
4053       if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
4054         HRI.getStackRegister() == SrcReg && MI.getOperand(2).isImm() &&
4055         isShiftedUInt<6,2>(MI.getOperand(2).getImm()))
4056         return HexagonII::HSIG_A;
4057       // Rx = add(Rx,#s7)
4058       if ((DstReg == SrcReg) && MI.getOperand(2).isImm() &&
4059           isInt<7>(MI.getOperand(2).getImm()))
4060         return HexagonII::HSIG_A;
4061       // Rd = add(Rs,#1)
4062       // Rd = add(Rs,#-1)
4063       if (isIntRegForSubInst(SrcReg) && MI.getOperand(2).isImm() &&
4064           ((MI.getOperand(2).getImm() == 1) ||
4065           (MI.getOperand(2).getImm() == -1)))
4066         return HexagonII::HSIG_A;
4067     }
4068     break;
4069   case Hexagon::A2_add:
4070   case Hexagon::dup_A2_add:
4071     // Rx = add(Rx,Rs)
4072     DstReg = MI.getOperand(0).getReg();
4073     Src1Reg = MI.getOperand(1).getReg();
4074     Src2Reg = MI.getOperand(2).getReg();
4075     if (isIntRegForSubInst(DstReg) && (DstReg == Src1Reg) &&
4076         isIntRegForSubInst(Src2Reg))
4077       return HexagonII::HSIG_A;
4078     break;
4079   case Hexagon::A2_andir:
4080   case Hexagon::dup_A2_andir:
4081     // Same as zxtb.
4082     // Rd16=and(Rs16,#255)
4083     // Rd16=and(Rs16,#1)
4084     DstReg = MI.getOperand(0).getReg();
4085     SrcReg = MI.getOperand(1).getReg();
4086     if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
4087         MI.getOperand(2).isImm() &&
4088         ((MI.getOperand(2).getImm() == 1) ||
4089         (MI.getOperand(2).getImm() == 255)))
4090       return HexagonII::HSIG_A;
4091     break;
4092   case Hexagon::A2_tfr:
4093   case Hexagon::dup_A2_tfr:
4094     // Rd = Rs
4095     DstReg = MI.getOperand(0).getReg();
4096     SrcReg = MI.getOperand(1).getReg();
4097     if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
4098       return HexagonII::HSIG_A;
4099     break;
4100   case Hexagon::A2_tfrsi:
4101   case Hexagon::dup_A2_tfrsi:
4102     // Rd = #u6
4103     // Do not test for #u6 size since the const is getting extended
4104     // regardless and compound could be formed.
4105     // Rd = #-1
4106     DstReg = MI.getOperand(0).getReg();
4107     if (isIntRegForSubInst(DstReg))
4108       return HexagonII::HSIG_A;
4109     break;
4110   case Hexagon::C2_cmoveit:
4111   case Hexagon::C2_cmovenewit:
4112   case Hexagon::C2_cmoveif:
4113   case Hexagon::C2_cmovenewif:
4114   case Hexagon::dup_C2_cmoveit:
4115   case Hexagon::dup_C2_cmovenewit:
4116   case Hexagon::dup_C2_cmoveif:
4117   case Hexagon::dup_C2_cmovenewif:
4118     // if ([!]P0[.new]) Rd = #0
4119     // Actual form:
4120     // %r16 = C2_cmovenewit internal %p0, 0, implicit undef %r16;
4121     DstReg = MI.getOperand(0).getReg();
4122     SrcReg = MI.getOperand(1).getReg();
4123     if (isIntRegForSubInst(DstReg) &&
4124         Hexagon::PredRegsRegClass.contains(SrcReg) && Hexagon::P0 == SrcReg &&
4125         MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0)
4126       return HexagonII::HSIG_A;
4127     break;
4128   case Hexagon::C2_cmpeqi:
4129   case Hexagon::dup_C2_cmpeqi:
4130     // P0 = cmp.eq(Rs,#u2)
4131     DstReg = MI.getOperand(0).getReg();
4132     SrcReg = MI.getOperand(1).getReg();
4133     if (Hexagon::PredRegsRegClass.contains(DstReg) &&
4134         Hexagon::P0 == DstReg && isIntRegForSubInst(SrcReg) &&
4135         MI.getOperand(2).isImm() && isUInt<2>(MI.getOperand(2).getImm()))
4136       return HexagonII::HSIG_A;
4137     break;
4138   case Hexagon::A2_combineii:
4139   case Hexagon::A4_combineii:
4140   case Hexagon::dup_A2_combineii:
4141   case Hexagon::dup_A4_combineii:
4142     // Rdd = combine(#u2,#U2)
4143     DstReg = MI.getOperand(0).getReg();
4144     if (isDblRegForSubInst(DstReg, HRI) &&
4145         ((MI.getOperand(1).isImm() && isUInt<2>(MI.getOperand(1).getImm())) ||
4146         (MI.getOperand(1).isGlobal() &&
4147         isUInt<2>(MI.getOperand(1).getOffset()))) &&
4148         ((MI.getOperand(2).isImm() && isUInt<2>(MI.getOperand(2).getImm())) ||
4149         (MI.getOperand(2).isGlobal() &&
4150         isUInt<2>(MI.getOperand(2).getOffset()))))
4151       return HexagonII::HSIG_A;
4152     break;
4153   case Hexagon::A4_combineri:
4154   case Hexagon::dup_A4_combineri:
4155     // Rdd = combine(Rs,#0)
4156     // Rdd = combine(Rs,#0)
4157     DstReg = MI.getOperand(0).getReg();
4158     SrcReg = MI.getOperand(1).getReg();
4159     if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
4160         ((MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0) ||
4161         (MI.getOperand(2).isGlobal() && MI.getOperand(2).getOffset() == 0)))
4162       return HexagonII::HSIG_A;
4163     break;
4164   case Hexagon::A4_combineir:
4165   case Hexagon::dup_A4_combineir:
4166     // Rdd = combine(#0,Rs)
4167     DstReg = MI.getOperand(0).getReg();
4168     SrcReg = MI.getOperand(2).getReg();
4169     if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
4170         ((MI.getOperand(1).isImm() && MI.getOperand(1).getImm() == 0) ||
4171         (MI.getOperand(1).isGlobal() && MI.getOperand(1).getOffset() == 0)))
4172       return HexagonII::HSIG_A;
4173     break;
4174   case Hexagon::A2_sxtb:
4175   case Hexagon::A2_sxth:
4176   case Hexagon::A2_zxtb:
4177   case Hexagon::A2_zxth:
4178   case Hexagon::dup_A2_sxtb:
4179   case Hexagon::dup_A2_sxth:
4180   case Hexagon::dup_A2_zxtb:
4181   case Hexagon::dup_A2_zxth:
4182     // Rd = sxth/sxtb/zxtb/zxth(Rs)
4183     DstReg = MI.getOperand(0).getReg();
4184     SrcReg = MI.getOperand(1).getReg();
4185     if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
4186       return HexagonII::HSIG_A;
4187     break;
4188   }
4189 
4190   return HexagonII::HSIG_None;
4191 }
4192 
4193 short HexagonInstrInfo::getEquivalentHWInstr(const MachineInstr &MI) const {
4194   return Hexagon::getRealHWInstr(MI.getOpcode(), Hexagon::InstrType_Real);
4195 }
4196 
4197 unsigned HexagonInstrInfo::getInstrTimingClassLatency(
4198       const InstrItineraryData *ItinData, const MachineInstr &MI) const {
4199   // Default to one cycle for no itinerary. However, an "empty" itinerary may
4200   // still have a MinLatency property, which getStageLatency checks.
4201   if (!ItinData)
4202     return getInstrLatency(ItinData, MI);
4203 
4204   if (MI.isTransient())
4205     return 0;
4206   return ItinData->getStageLatency(MI.getDesc().getSchedClass());
4207 }
4208 
4209 /// getOperandLatency - Compute and return the use operand latency of a given
4210 /// pair of def and use.
4211 /// In most cases, the static scheduling itinerary was enough to determine the
4212 /// operand latency. But it may not be possible for instructions with variable
4213 /// number of defs / uses.
4214 ///
4215 /// This is a raw interface to the itinerary that may be directly overriden by
4216 /// a target. Use computeOperandLatency to get the best estimate of latency.
4217 int HexagonInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
4218                                         const MachineInstr &DefMI,
4219                                         unsigned DefIdx,
4220                                         const MachineInstr &UseMI,
4221                                         unsigned UseIdx) const {
4222   const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
4223 
4224   // Get DefIdx and UseIdx for super registers.
4225   const MachineOperand &DefMO = DefMI.getOperand(DefIdx);
4226 
4227   if (DefMO.isReg() && Register::isPhysicalRegister(DefMO.getReg())) {
4228     if (DefMO.isImplicit()) {
4229       for (MCSuperRegIterator SR(DefMO.getReg(), &HRI); SR.isValid(); ++SR) {
4230         int Idx = DefMI.findRegisterDefOperandIdx(*SR, false, false, &HRI);
4231         if (Idx != -1) {
4232           DefIdx = Idx;
4233           break;
4234         }
4235       }
4236     }
4237 
4238     const MachineOperand &UseMO = UseMI.getOperand(UseIdx);
4239     if (UseMO.isImplicit()) {
4240       for (MCSuperRegIterator SR(UseMO.getReg(), &HRI); SR.isValid(); ++SR) {
4241         int Idx = UseMI.findRegisterUseOperandIdx(*SR, false, &HRI);
4242         if (Idx != -1) {
4243           UseIdx = Idx;
4244           break;
4245         }
4246       }
4247     }
4248   }
4249 
4250   int Latency = TargetInstrInfo::getOperandLatency(ItinData, DefMI, DefIdx,
4251                                                    UseMI, UseIdx);
4252   if (!Latency)
4253     // We should never have 0 cycle latency between two instructions unless
4254     // they can be packetized together. However, this decision can't be made
4255     // here.
4256     Latency = 1;
4257   return Latency;
4258 }
4259 
4260 // inverts the predication logic.
4261 // p -> NotP
4262 // NotP -> P
4263 bool HexagonInstrInfo::getInvertedPredSense(
4264       SmallVectorImpl<MachineOperand> &Cond) const {
4265   if (Cond.empty())
4266     return false;
4267   unsigned Opc = getInvertedPredicatedOpcode(Cond[0].getImm());
4268   Cond[0].setImm(Opc);
4269   return true;
4270 }
4271 
4272 unsigned HexagonInstrInfo::getInvertedPredicatedOpcode(const int Opc) const {
4273   int InvPredOpcode;
4274   InvPredOpcode = isPredicatedTrue(Opc) ? Hexagon::getFalsePredOpcode(Opc)
4275                                         : Hexagon::getTruePredOpcode(Opc);
4276   if (InvPredOpcode >= 0) // Valid instruction with the inverted predicate.
4277     return InvPredOpcode;
4278 
4279   llvm_unreachable("Unexpected predicated instruction");
4280 }
4281 
4282 // Returns the max value that doesn't need to be extended.
4283 int HexagonInstrInfo::getMaxValue(const MachineInstr &MI) const {
4284   const uint64_t F = MI.getDesc().TSFlags;
4285   unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
4286                     & HexagonII::ExtentSignedMask;
4287   unsigned bits =  (F >> HexagonII::ExtentBitsPos)
4288                     & HexagonII::ExtentBitsMask;
4289 
4290   if (isSigned) // if value is signed
4291     return ~(-1U << (bits - 1));
4292   else
4293     return ~(-1U << bits);
4294 }
4295 
4296 
4297 bool HexagonInstrInfo::isAddrModeWithOffset(const MachineInstr &MI) const {
4298   switch (MI.getOpcode()) {
4299   case Hexagon::L2_loadrbgp:
4300   case Hexagon::L2_loadrdgp:
4301   case Hexagon::L2_loadrhgp:
4302   case Hexagon::L2_loadrigp:
4303   case Hexagon::L2_loadrubgp:
4304   case Hexagon::L2_loadruhgp:
4305   case Hexagon::S2_storerbgp:
4306   case Hexagon::S2_storerbnewgp:
4307   case Hexagon::S2_storerhgp:
4308   case Hexagon::S2_storerhnewgp:
4309   case Hexagon::S2_storerigp:
4310   case Hexagon::S2_storerinewgp:
4311   case Hexagon::S2_storerdgp:
4312   case Hexagon::S2_storerfgp:
4313     return true;
4314   }
4315   const uint64_t F = MI.getDesc().TSFlags;
4316   unsigned addrMode =
4317     ((F >> HexagonII::AddrModePos) & HexagonII::AddrModeMask);
4318   // Disallow any base+offset instruction. The assembler does not yet reorder
4319   // based up any zero offset instruction.
4320   return (addrMode == HexagonII::BaseRegOffset ||
4321           addrMode == HexagonII::BaseImmOffset ||
4322           addrMode == HexagonII::BaseLongOffset);
4323 }
4324 
4325 bool HexagonInstrInfo::isPureSlot0(const MachineInstr &MI) const {
4326   // Workaround for the Global Scheduler. Sometimes, it creates
4327   // A4_ext as a Pseudo instruction and calls this function to see if
4328   // it can be added to an existing bundle. Since the instruction doesn't
4329   // belong to any BB yet, we can't use getUnits API.
4330   if (MI.getOpcode() == Hexagon::A4_ext)
4331     return false;
4332 
4333   unsigned FuncUnits = getUnits(MI);
4334   return HexagonFUnits::isSlot0Only(FuncUnits);
4335 }
4336 
4337 bool HexagonInstrInfo::isRestrictNoSlot1Store(const MachineInstr &MI) const {
4338   const uint64_t F = MI.getDesc().TSFlags;
4339   return ((F >> HexagonII::RestrictNoSlot1StorePos) &
4340           HexagonII::RestrictNoSlot1StoreMask);
4341 }
4342 
4343 void HexagonInstrInfo::changeDuplexOpcode(MachineBasicBlock::instr_iterator MII,
4344                                           bool ToBigInstrs) const {
4345   int Opcode = -1;
4346   if (ToBigInstrs) { // To BigCore Instr.
4347     // Check if the instruction can form a Duplex.
4348     if (getDuplexCandidateGroup(*MII))
4349       // Get the opcode marked "dup_*" tag.
4350       Opcode = getDuplexOpcode(*MII, ToBigInstrs);
4351   } else // To TinyCore Instr.
4352     Opcode = getDuplexOpcode(*MII, ToBigInstrs);
4353 
4354   // Change the opcode of the instruction.
4355   if (Opcode >= 0)
4356     MII->setDesc(get(Opcode));
4357 }
4358 
4359 // This function is used to translate instructions to facilitate generating
4360 // Duplexes on TinyCore.
4361 void HexagonInstrInfo::translateInstrsForDup(MachineFunction &MF,
4362                                              bool ToBigInstrs) const {
4363   for (auto &MB : MF)
4364     for (MachineBasicBlock::instr_iterator Instr = MB.instr_begin(),
4365                                            End = MB.instr_end();
4366          Instr != End; ++Instr)
4367       changeDuplexOpcode(Instr, ToBigInstrs);
4368 }
4369 
4370 // This is a specialized form of above function.
4371 void HexagonInstrInfo::translateInstrsForDup(
4372     MachineBasicBlock::instr_iterator MII, bool ToBigInstrs) const {
4373   MachineBasicBlock *MBB = MII->getParent();
4374   while ((MII != MBB->instr_end()) && MII->isInsideBundle()) {
4375     changeDuplexOpcode(MII, ToBigInstrs);
4376     ++MII;
4377   }
4378 }
4379 
4380 unsigned HexagonInstrInfo::getMemAccessSize(const MachineInstr &MI) const {
4381   using namespace HexagonII;
4382 
4383   const uint64_t F = MI.getDesc().TSFlags;
4384   unsigned S = (F >> MemAccessSizePos) & MemAccesSizeMask;
4385   unsigned Size = getMemAccessSizeInBytes(MemAccessSize(S));
4386   if (Size != 0)
4387     return Size;
4388 
4389   // Handle vector access sizes.
4390   const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
4391   switch (S) {
4392     case HexagonII::HVXVectorAccess:
4393       return HRI.getSpillSize(Hexagon::HvxVRRegClass);
4394     default:
4395       llvm_unreachable("Unexpected instruction");
4396   }
4397 }
4398 
4399 // Returns the min value that doesn't need to be extended.
4400 int HexagonInstrInfo::getMinValue(const MachineInstr &MI) const {
4401   const uint64_t F = MI.getDesc().TSFlags;
4402   unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
4403                     & HexagonII::ExtentSignedMask;
4404   unsigned bits =  (F >> HexagonII::ExtentBitsPos)
4405                     & HexagonII::ExtentBitsMask;
4406 
4407   if (isSigned) // if value is signed
4408     return -1U << (bits - 1);
4409   else
4410     return 0;
4411 }
4412 
4413 // Returns opcode of the non-extended equivalent instruction.
4414 short HexagonInstrInfo::getNonExtOpcode(const MachineInstr &MI) const {
4415   // Check if the instruction has a register form that uses register in place
4416   // of the extended operand, if so return that as the non-extended form.
4417   short NonExtOpcode = Hexagon::getRegForm(MI.getOpcode());
4418     if (NonExtOpcode >= 0)
4419       return NonExtOpcode;
4420 
4421   if (MI.getDesc().mayLoad() || MI.getDesc().mayStore()) {
4422     // Check addressing mode and retrieve non-ext equivalent instruction.
4423     switch (getAddrMode(MI)) {
4424     case HexagonII::Absolute:
4425       return Hexagon::changeAddrMode_abs_io(MI.getOpcode());
4426     case HexagonII::BaseImmOffset:
4427       return Hexagon::changeAddrMode_io_rr(MI.getOpcode());
4428     case HexagonII::BaseLongOffset:
4429       return Hexagon::changeAddrMode_ur_rr(MI.getOpcode());
4430 
4431     default:
4432       return -1;
4433     }
4434   }
4435   return -1;
4436 }
4437 
4438 bool HexagonInstrInfo::getPredReg(ArrayRef<MachineOperand> Cond,
4439       unsigned &PredReg, unsigned &PredRegPos, unsigned &PredRegFlags) const {
4440   if (Cond.empty())
4441     return false;
4442   assert(Cond.size() == 2);
4443   if (isNewValueJump(Cond[0].getImm()) || Cond[1].isMBB()) {
4444     LLVM_DEBUG(dbgs() << "No predregs for new-value jumps/endloop");
4445     return false;
4446   }
4447   PredReg = Cond[1].getReg();
4448   PredRegPos = 1;
4449   // See IfConversion.cpp why we add RegState::Implicit | RegState::Undef
4450   PredRegFlags = 0;
4451   if (Cond[1].isImplicit())
4452     PredRegFlags = RegState::Implicit;
4453   if (Cond[1].isUndef())
4454     PredRegFlags |= RegState::Undef;
4455   return true;
4456 }
4457 
4458 short HexagonInstrInfo::getPseudoInstrPair(const MachineInstr &MI) const {
4459   return Hexagon::getRealHWInstr(MI.getOpcode(), Hexagon::InstrType_Pseudo);
4460 }
4461 
4462 short HexagonInstrInfo::getRegForm(const MachineInstr &MI) const {
4463   return Hexagon::getRegForm(MI.getOpcode());
4464 }
4465 
4466 // Return the number of bytes required to encode the instruction.
4467 // Hexagon instructions are fixed length, 4 bytes, unless they
4468 // use a constant extender, which requires another 4 bytes.
4469 // For debug instructions and prolog labels, return 0.
4470 unsigned HexagonInstrInfo::getSize(const MachineInstr &MI) const {
4471   if (MI.isDebugInstr() || MI.isPosition())
4472     return 0;
4473 
4474   unsigned Size = MI.getDesc().getSize();
4475   if (!Size)
4476     // Assume the default insn size in case it cannot be determined
4477     // for whatever reason.
4478     Size = HEXAGON_INSTR_SIZE;
4479 
4480   if (isConstExtended(MI) || isExtended(MI))
4481     Size += HEXAGON_INSTR_SIZE;
4482 
4483   // Try and compute number of instructions in asm.
4484   if (BranchRelaxAsmLarge && MI.getOpcode() == Hexagon::INLINEASM) {
4485     const MachineBasicBlock &MBB = *MI.getParent();
4486     const MachineFunction *MF = MBB.getParent();
4487     const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
4488 
4489     // Count the number of register definitions to find the asm string.
4490     unsigned NumDefs = 0;
4491     for (; MI.getOperand(NumDefs).isReg() && MI.getOperand(NumDefs).isDef();
4492          ++NumDefs)
4493       assert(NumDefs != MI.getNumOperands()-2 && "No asm string?");
4494 
4495     assert(MI.getOperand(NumDefs).isSymbol() && "No asm string?");
4496     // Disassemble the AsmStr and approximate number of instructions.
4497     const char *AsmStr = MI.getOperand(NumDefs).getSymbolName();
4498     Size = getInlineAsmLength(AsmStr, *MAI);
4499   }
4500 
4501   return Size;
4502 }
4503 
4504 uint64_t HexagonInstrInfo::getType(const MachineInstr &MI) const {
4505   const uint64_t F = MI.getDesc().TSFlags;
4506   return (F >> HexagonII::TypePos) & HexagonII::TypeMask;
4507 }
4508 
4509 InstrStage::FuncUnits HexagonInstrInfo::getUnits(const MachineInstr &MI) const {
4510   const InstrItineraryData &II = *Subtarget.getInstrItineraryData();
4511   const InstrStage &IS = *II.beginStage(MI.getDesc().getSchedClass());
4512 
4513   return IS.getUnits();
4514 }
4515 
4516 // Calculate size of the basic block without debug instructions.
4517 unsigned HexagonInstrInfo::nonDbgBBSize(const MachineBasicBlock *BB) const {
4518   return nonDbgMICount(BB->instr_begin(), BB->instr_end());
4519 }
4520 
4521 unsigned HexagonInstrInfo::nonDbgBundleSize(
4522       MachineBasicBlock::const_iterator BundleHead) const {
4523   assert(BundleHead->isBundle() && "Not a bundle header");
4524   auto MII = BundleHead.getInstrIterator();
4525   // Skip the bundle header.
4526   return nonDbgMICount(++MII, getBundleEnd(BundleHead.getInstrIterator()));
4527 }
4528 
4529 /// immediateExtend - Changes the instruction in place to one using an immediate
4530 /// extender.
4531 void HexagonInstrInfo::immediateExtend(MachineInstr &MI) const {
4532   assert((isExtendable(MI)||isConstExtended(MI)) &&
4533                                "Instruction must be extendable");
4534   // Find which operand is extendable.
4535   short ExtOpNum = getCExtOpNum(MI);
4536   MachineOperand &MO = MI.getOperand(ExtOpNum);
4537   // This needs to be something we understand.
4538   assert((MO.isMBB() || MO.isImm()) &&
4539          "Branch with unknown extendable field type");
4540   // Mark given operand as extended.
4541   MO.addTargetFlag(HexagonII::HMOTF_ConstExtended);
4542 }
4543 
4544 bool HexagonInstrInfo::invertAndChangeJumpTarget(
4545       MachineInstr &MI, MachineBasicBlock *NewTarget) const {
4546   LLVM_DEBUG(dbgs() << "\n[invertAndChangeJumpTarget] to "
4547                     << printMBBReference(*NewTarget);
4548              MI.dump(););
4549   assert(MI.isBranch());
4550   unsigned NewOpcode = getInvertedPredicatedOpcode(MI.getOpcode());
4551   int TargetPos = MI.getNumOperands() - 1;
4552   // In general branch target is the last operand,
4553   // but some implicit defs added at the end might change it.
4554   while ((TargetPos > -1) && !MI.getOperand(TargetPos).isMBB())
4555     --TargetPos;
4556   assert((TargetPos >= 0) && MI.getOperand(TargetPos).isMBB());
4557   MI.getOperand(TargetPos).setMBB(NewTarget);
4558   if (EnableBranchPrediction && isPredicatedNew(MI)) {
4559     NewOpcode = reversePrediction(NewOpcode);
4560   }
4561   MI.setDesc(get(NewOpcode));
4562   return true;
4563 }
4564 
4565 void HexagonInstrInfo::genAllInsnTimingClasses(MachineFunction &MF) const {
4566   /* +++ The code below is used to generate complete set of Hexagon Insn +++ */
4567   MachineFunction::iterator A = MF.begin();
4568   MachineBasicBlock &B = *A;
4569   MachineBasicBlock::iterator I = B.begin();
4570   DebugLoc DL = I->getDebugLoc();
4571   MachineInstr *NewMI;
4572 
4573   for (unsigned insn = TargetOpcode::GENERIC_OP_END+1;
4574        insn < Hexagon::INSTRUCTION_LIST_END; ++insn) {
4575     NewMI = BuildMI(B, I, DL, get(insn));
4576     LLVM_DEBUG(dbgs() << "\n"
4577                       << getName(NewMI->getOpcode())
4578                       << "  Class: " << NewMI->getDesc().getSchedClass());
4579     NewMI->eraseFromParent();
4580   }
4581   /* --- The code above is used to generate complete set of Hexagon Insn --- */
4582 }
4583 
4584 // inverts the predication logic.
4585 // p -> NotP
4586 // NotP -> P
4587 bool HexagonInstrInfo::reversePredSense(MachineInstr &MI) const {
4588   LLVM_DEBUG(dbgs() << "\nTrying to reverse pred. sense of:"; MI.dump());
4589   MI.setDesc(get(getInvertedPredicatedOpcode(MI.getOpcode())));
4590   return true;
4591 }
4592 
4593 // Reverse the branch prediction.
4594 unsigned HexagonInstrInfo::reversePrediction(unsigned Opcode) const {
4595   int PredRevOpcode = -1;
4596   if (isPredictedTaken(Opcode))
4597     PredRevOpcode = Hexagon::notTakenBranchPrediction(Opcode);
4598   else
4599     PredRevOpcode = Hexagon::takenBranchPrediction(Opcode);
4600   assert(PredRevOpcode > 0);
4601   return PredRevOpcode;
4602 }
4603 
4604 // TODO: Add more rigorous validation.
4605 bool HexagonInstrInfo::validateBranchCond(const ArrayRef<MachineOperand> &Cond)
4606       const {
4607   return Cond.empty() || (Cond[0].isImm() && (Cond.size() != 1));
4608 }
4609 
4610 void HexagonInstrInfo::
4611 setBundleNoShuf(MachineBasicBlock::instr_iterator MIB) const {
4612   assert(MIB->isBundle());
4613   MachineOperand &Operand = MIB->getOperand(0);
4614   if (Operand.isImm())
4615     Operand.setImm(Operand.getImm() | memShufDisabledMask);
4616   else
4617     MIB->addOperand(MachineOperand::CreateImm(memShufDisabledMask));
4618 }
4619 
4620 bool HexagonInstrInfo::getBundleNoShuf(const MachineInstr &MIB) const {
4621   assert(MIB.isBundle());
4622   const MachineOperand &Operand = MIB.getOperand(0);
4623   return (Operand.isImm() && (Operand.getImm() & memShufDisabledMask) != 0);
4624 }
4625 
4626 // Addressing mode relations.
4627 short HexagonInstrInfo::changeAddrMode_abs_io(short Opc) const {
4628   return Opc >= 0 ? Hexagon::changeAddrMode_abs_io(Opc) : Opc;
4629 }
4630 
4631 short HexagonInstrInfo::changeAddrMode_io_abs(short Opc) const {
4632   return Opc >= 0 ? Hexagon::changeAddrMode_io_abs(Opc) : Opc;
4633 }
4634 
4635 short HexagonInstrInfo::changeAddrMode_io_pi(short Opc) const {
4636   return Opc >= 0 ? Hexagon::changeAddrMode_io_pi(Opc) : Opc;
4637 }
4638 
4639 short HexagonInstrInfo::changeAddrMode_io_rr(short Opc) const {
4640   return Opc >= 0 ? Hexagon::changeAddrMode_io_rr(Opc) : Opc;
4641 }
4642 
4643 short HexagonInstrInfo::changeAddrMode_pi_io(short Opc) const {
4644   return Opc >= 0 ? Hexagon::changeAddrMode_pi_io(Opc) : Opc;
4645 }
4646 
4647 short HexagonInstrInfo::changeAddrMode_rr_io(short Opc) const {
4648   return Opc >= 0 ? Hexagon::changeAddrMode_rr_io(Opc) : Opc;
4649 }
4650 
4651 short HexagonInstrInfo::changeAddrMode_rr_ur(short Opc) const {
4652   return Opc >= 0 ? Hexagon::changeAddrMode_rr_ur(Opc) : Opc;
4653 }
4654 
4655 short HexagonInstrInfo::changeAddrMode_ur_rr(short Opc) const {
4656   return Opc >= 0 ? Hexagon::changeAddrMode_ur_rr(Opc) : Opc;
4657 }
4658