[Hexagon] HVX vector register classes and more isel patterns
[oota-llvm.git] / lib / Target / Hexagon / HexagonInstrInfo.cpp
1 //===-- HexagonInstrInfo.cpp - Hexagon Instruction Information ------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the Hexagon implementation of the TargetInstrInfo class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "HexagonInstrInfo.h"
15 #include "Hexagon.h"
16 #include "HexagonRegisterInfo.h"
17 #include "HexagonSubtarget.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/CodeGen/DFAPacketizer.h"
21 #include "llvm/CodeGen/MachineFrameInfo.h"
22 #include "llvm/CodeGen/MachineInstrBuilder.h"
23 #include "llvm/CodeGen/MachineMemOperand.h"
24 #include "llvm/CodeGen/MachineRegisterInfo.h"
25 #include "llvm/CodeGen/PseudoSourceValue.h"
26 #include "llvm/MC/MCAsmInfo.h"
27 #include "llvm/Support/Debug.h"
28 #include "llvm/Support/MathExtras.h"
29 #include "llvm/Support/raw_ostream.h"
30 #include <cctype>
31
32 using namespace llvm;
33
34 #define DEBUG_TYPE "hexagon-instrinfo"
35
36 #define GET_INSTRINFO_CTOR_DTOR
37 #define GET_INSTRMAP_INFO
38 #include "HexagonGenInstrInfo.inc"
39 #include "HexagonGenDFAPacketizer.inc"
40
41 using namespace llvm;
42
43 static cl::opt<bool> ScheduleInlineAsm("hexagon-sched-inline-asm", cl::Hidden,
44   cl::init(false), cl::desc("Do not consider inline-asm a scheduling/"
45                             "packetization boundary."));
46
47 static cl::opt<bool> EnableBranchPrediction("hexagon-enable-branch-prediction",
48   cl::Hidden, cl::init(true), cl::desc("Enable branch prediction"));
49
50 static cl::opt<bool> EnableTimingClassLatency(
51   "enable-timing-class-latency", cl::Hidden, cl::init(false),
52   cl::desc("Enable timing class latency"));
53
54 static cl::opt<bool> EnableALUForwarding(
55   "enable-alu-forwarding", cl::Hidden, cl::init(true),
56   cl::desc("Enable vec alu forwarding"));
57
58 static cl::opt<bool> EnableACCForwarding(
59   "enable-acc-forwarding", cl::Hidden, cl::init(true),
60   cl::desc("Enable vec acc forwarding"));
61
62 static cl::opt<bool> BranchRelaxAsmLarge("branch-relax-asm-large",
63   cl::init(true), cl::Hidden, cl::ZeroOrMore, cl::desc("branch relax asm"));
64
65 ///
66 /// Constants for Hexagon instructions.
67 ///
68 const int Hexagon_MEMV_OFFSET_MAX_128B = 2047;  // #s7
69 const int Hexagon_MEMV_OFFSET_MIN_128B = -2048; // #s7
70 const int Hexagon_MEMV_OFFSET_MAX = 1023;  // #s6
71 const int Hexagon_MEMV_OFFSET_MIN = -1024; // #s6
72 const int Hexagon_MEMW_OFFSET_MAX = 4095;
73 const int Hexagon_MEMW_OFFSET_MIN = -4096;
74 const int Hexagon_MEMD_OFFSET_MAX = 8191;
75 const int Hexagon_MEMD_OFFSET_MIN = -8192;
76 const int Hexagon_MEMH_OFFSET_MAX = 2047;
77 const int Hexagon_MEMH_OFFSET_MIN = -2048;
78 const int Hexagon_MEMB_OFFSET_MAX = 1023;
79 const int Hexagon_MEMB_OFFSET_MIN = -1024;
80 const int Hexagon_ADDI_OFFSET_MAX = 32767;
81 const int Hexagon_ADDI_OFFSET_MIN = -32768;
82 const int Hexagon_MEMD_AUTOINC_MAX = 56;
83 const int Hexagon_MEMD_AUTOINC_MIN = -64;
84 const int Hexagon_MEMW_AUTOINC_MAX = 28;
85 const int Hexagon_MEMW_AUTOINC_MIN = -32;
86 const int Hexagon_MEMH_AUTOINC_MAX = 14;
87 const int Hexagon_MEMH_AUTOINC_MIN = -16;
88 const int Hexagon_MEMB_AUTOINC_MAX = 7;
89 const int Hexagon_MEMB_AUTOINC_MIN = -8;
90 const int Hexagon_MEMV_AUTOINC_MAX = 192;
91 const int Hexagon_MEMV_AUTOINC_MIN = -256;
92 const int Hexagon_MEMV_AUTOINC_MAX_128B = 384;
93 const int Hexagon_MEMV_AUTOINC_MIN_128B = -512;
94
95 // Pin the vtable to this file.
96 void HexagonInstrInfo::anchor() {}
97
98 HexagonInstrInfo::HexagonInstrInfo(HexagonSubtarget &ST)
99     : HexagonGenInstrInfo(Hexagon::ADJCALLSTACKDOWN, Hexagon::ADJCALLSTACKUP),
100       RI() {}
101
102
103 static bool isIntRegForSubInst(unsigned Reg) {
104   return (Reg >= Hexagon::R0 && Reg <= Hexagon::R7) ||
105          (Reg >= Hexagon::R16 && Reg <= Hexagon::R23);
106 }
107
108
109 static bool isDblRegForSubInst(unsigned Reg, const HexagonRegisterInfo &HRI) {
110   return isIntRegForSubInst(HRI.getSubReg(Reg, Hexagon::subreg_loreg)) &&
111          isIntRegForSubInst(HRI.getSubReg(Reg, Hexagon::subreg_hireg));
112 }
113
114
115 /// Calculate number of instructions excluding the debug instructions.
116 static unsigned nonDbgMICount(MachineBasicBlock::const_instr_iterator MIB,
117                               MachineBasicBlock::const_instr_iterator MIE) {
118   unsigned Count = 0;
119   for (; MIB != MIE; ++MIB) {
120     if (!MIB->isDebugValue())
121       ++Count;
122   }
123   return Count;
124 }
125
126
127 /// Find the hardware loop instruction used to set-up the specified loop.
128 /// On Hexagon, we have two instructions used to set-up the hardware loop
129 /// (LOOP0, LOOP1) with corresponding endloop (ENDLOOP0, ENDLOOP1) instructions
130 /// to indicate the end of a loop.
131 static MachineInstr *findLoopInstr(MachineBasicBlock *BB, int EndLoopOp,
132       SmallPtrSet<MachineBasicBlock *, 8> &Visited) {
133   int LOOPi;
134   int LOOPr;
135   if (EndLoopOp == Hexagon::ENDLOOP0) {
136     LOOPi = Hexagon::J2_loop0i;
137     LOOPr = Hexagon::J2_loop0r;
138   } else { // EndLoopOp == Hexagon::EndLOOP1
139     LOOPi = Hexagon::J2_loop1i;
140     LOOPr = Hexagon::J2_loop1r;
141   }
142
143   // The loop set-up instruction will be in a predecessor block
144   for (MachineBasicBlock::pred_iterator PB = BB->pred_begin(),
145          PE = BB->pred_end(); PB != PE; ++PB) {
146     // If this has been visited, already skip it.
147     if (!Visited.insert(*PB).second)
148       continue;
149     if (*PB == BB)
150       continue;
151     for (MachineBasicBlock::reverse_instr_iterator I = (*PB)->instr_rbegin(),
152            E = (*PB)->instr_rend(); I != E; ++I) {
153       int Opc = I->getOpcode();
154       if (Opc == LOOPi || Opc == LOOPr)
155         return &*I;
156       // We've reached a different loop, which means the loop0 has been removed.
157       if (Opc == EndLoopOp)
158         return 0;
159     }
160     // Check the predecessors for the LOOP instruction.
161     MachineInstr *loop = findLoopInstr(*PB, EndLoopOp, Visited);
162     if (loop)
163       return loop;
164   }
165   return 0;
166 }
167
168
169 /// Gather register def/uses from MI.
170 /// This treats possible (predicated) defs as actually happening ones
171 /// (conservatively).
172 static inline void parseOperands(const MachineInstr *MI,
173       SmallVector<unsigned, 4> &Defs, SmallVector<unsigned, 8> &Uses) {
174   Defs.clear();
175   Uses.clear();
176
177   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
178     const MachineOperand &MO = MI->getOperand(i);
179
180     if (!MO.isReg())
181       continue;
182
183     unsigned Reg = MO.getReg();
184     if (!Reg)
185       continue;
186
187     if (MO.isUse())
188       Uses.push_back(MO.getReg());
189
190     if (MO.isDef())
191       Defs.push_back(MO.getReg());
192   }
193 }
194
195
196 // Position dependent, so check twice for swap.
197 static bool isDuplexPairMatch(unsigned Ga, unsigned Gb) {
198   switch (Ga) {
199   case HexagonII::HSIG_None:
200   default:
201     return false;
202   case HexagonII::HSIG_L1:
203     return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_A);
204   case HexagonII::HSIG_L2:
205     return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
206             Gb == HexagonII::HSIG_A);
207   case HexagonII::HSIG_S1:
208     return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
209             Gb == HexagonII::HSIG_S1 || Gb == HexagonII::HSIG_A);
210   case HexagonII::HSIG_S2:
211     return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
212             Gb == HexagonII::HSIG_S1 || Gb == HexagonII::HSIG_S2 ||
213             Gb == HexagonII::HSIG_A);
214   case HexagonII::HSIG_A:
215     return (Gb == HexagonII::HSIG_A);
216   case HexagonII::HSIG_Compound:
217     return (Gb == HexagonII::HSIG_Compound);
218   }
219   return false;
220 }
221
222
223
224 /// isLoadFromStackSlot - If the specified machine instruction is a direct
225 /// load from a stack slot, return the virtual or physical register number of
226 /// the destination along with the FrameIndex of the loaded stack slot.  If
227 /// not, return 0.  This predicate must return 0 if the instruction has
228 /// any side effects other than loading from the stack slot.
229 unsigned HexagonInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
230                                                int &FrameIndex) const {
231   switch (MI->getOpcode()) {
232   default: break;
233   case Hexagon::L2_loadri_io:
234   case Hexagon::L2_loadrd_io:
235   case Hexagon::L2_loadrh_io:
236   case Hexagon::L2_loadrb_io:
237   case Hexagon::L2_loadrub_io:
238     if (MI->getOperand(2).isFI() &&
239         MI->getOperand(1).isImm() && (MI->getOperand(1).getImm() == 0)) {
240       FrameIndex = MI->getOperand(2).getIndex();
241       return MI->getOperand(0).getReg();
242     }
243     break;
244   }
245   return 0;
246 }
247
248
249 /// isStoreToStackSlot - If the specified machine instruction is a direct
250 /// store to a stack slot, return the virtual or physical register number of
251 /// the source reg along with the FrameIndex of the loaded stack slot.  If
252 /// not, return 0.  This predicate must return 0 if the instruction has
253 /// any side effects other than storing to the stack slot.
254 unsigned HexagonInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
255                                               int &FrameIndex) const {
256   switch (MI->getOpcode()) {
257   default: break;
258   case Hexagon::S2_storeri_io:
259   case Hexagon::S2_storerd_io:
260   case Hexagon::S2_storerh_io:
261   case Hexagon::S2_storerb_io:
262     if (MI->getOperand(2).isFI() &&
263         MI->getOperand(1).isImm() && (MI->getOperand(1).getImm() == 0)) {
264       FrameIndex = MI->getOperand(0).getIndex();
265       return MI->getOperand(2).getReg();
266     }
267     break;
268   }
269   return 0;
270 }
271
272
273 /// This function can analyze one/two way branching only and should (mostly) be
274 /// called by target independent side.
275 /// First entry is always the opcode of the branching instruction, except when
276 /// the Cond vector is supposed to be empty, e.g., when AnalyzeBranch fails, a
277 /// BB with only unconditional jump. Subsequent entries depend upon the opcode,
278 /// e.g. Jump_c p will have
279 /// Cond[0] = Jump_c
280 /// Cond[1] = p
281 /// HW-loop ENDLOOP:
282 /// Cond[0] = ENDLOOP
283 /// Cond[1] = MBB
284 /// New value jump:
285 /// Cond[0] = Hexagon::CMPEQri_f_Jumpnv_t_V4 -- specific opcode
286 /// Cond[1] = R
287 /// Cond[2] = Imm
288 ///
289 bool HexagonInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
290                                      MachineBasicBlock *&TBB,
291                                      MachineBasicBlock *&FBB,
292                                      SmallVectorImpl<MachineOperand> &Cond,
293                                      bool AllowModify) const {
294   TBB = nullptr;
295   FBB = nullptr;
296   Cond.clear();
297
298   // If the block has no terminators, it just falls into the block after it.
299   MachineBasicBlock::instr_iterator I = MBB.instr_end();
300   if (I == MBB.instr_begin())
301     return false;
302
303   // A basic block may looks like this:
304   //
305   //  [   insn
306   //     EH_LABEL
307   //      insn
308   //      insn
309   //      insn
310   //     EH_LABEL
311   //      insn     ]
312   //
313   // It has two succs but does not have a terminator
314   // Don't know how to handle it.
315   do {
316     --I;
317     if (I->isEHLabel())
318       // Don't analyze EH branches.
319       return true;
320   } while (I != MBB.instr_begin());
321
322   I = MBB.instr_end();
323   --I;
324
325   while (I->isDebugValue()) {
326     if (I == MBB.instr_begin())
327       return false;
328     --I;
329   }
330
331   bool JumpToBlock = I->getOpcode() == Hexagon::J2_jump &&
332                      I->getOperand(0).isMBB();
333   // Delete the J2_jump if it's equivalent to a fall-through.
334   if (AllowModify && JumpToBlock &&
335       MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
336     DEBUG(dbgs()<< "\nErasing the jump to successor block\n";);
337     I->eraseFromParent();
338     I = MBB.instr_end();
339     if (I == MBB.instr_begin())
340       return false;
341     --I;
342   }
343   if (!isUnpredicatedTerminator(&*I))
344     return false;
345
346   // Get the last instruction in the block.
347   MachineInstr *LastInst = &*I;
348   MachineInstr *SecondLastInst = nullptr;
349   // Find one more terminator if present.
350   for (;;) {
351     if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(&*I)) {
352       if (!SecondLastInst)
353         SecondLastInst = &*I;
354       else
355         // This is a third branch.
356         return true;
357     }
358     if (I == MBB.instr_begin())
359       break;
360     --I;
361   }
362
363   int LastOpcode = LastInst->getOpcode();
364   int SecLastOpcode = SecondLastInst ? SecondLastInst->getOpcode() : 0;
365   // If the branch target is not a basic block, it could be a tail call.
366   // (It is, if the target is a function.)
367   if (LastOpcode == Hexagon::J2_jump && !LastInst->getOperand(0).isMBB())
368     return true;
369   if (SecLastOpcode == Hexagon::J2_jump &&
370       !SecondLastInst->getOperand(0).isMBB())
371     return true;
372
373   bool LastOpcodeHasJMP_c = PredOpcodeHasJMP_c(LastOpcode);
374   bool LastOpcodeHasNVJump = isNewValueJump(LastInst);
375
376   // If there is only one terminator instruction, process it.
377   if (LastInst && !SecondLastInst) {
378     if (LastOpcode == Hexagon::J2_jump) {
379       TBB = LastInst->getOperand(0).getMBB();
380       return false;
381     }
382     if (isEndLoopN(LastOpcode)) {
383       TBB = LastInst->getOperand(0).getMBB();
384       Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
385       Cond.push_back(LastInst->getOperand(0));
386       return false;
387     }
388     if (LastOpcodeHasJMP_c) {
389       TBB = LastInst->getOperand(1).getMBB();
390       Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
391       Cond.push_back(LastInst->getOperand(0));
392       return false;
393     }
394     // Only supporting rr/ri versions of new-value jumps.
395     if (LastOpcodeHasNVJump && (LastInst->getNumExplicitOperands() == 3)) {
396       TBB = LastInst->getOperand(2).getMBB();
397       Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
398       Cond.push_back(LastInst->getOperand(0));
399       Cond.push_back(LastInst->getOperand(1));
400       return false;
401     }
402     DEBUG(dbgs() << "\nCant analyze BB#" << MBB.getNumber()
403                  << " with one jump\n";);
404     // Otherwise, don't know what this is.
405     return true;
406   }
407
408   bool SecLastOpcodeHasJMP_c = PredOpcodeHasJMP_c(SecLastOpcode);
409   bool SecLastOpcodeHasNVJump = isNewValueJump(SecondLastInst);
410   if (SecLastOpcodeHasJMP_c && (LastOpcode == Hexagon::J2_jump)) {
411     TBB =  SecondLastInst->getOperand(1).getMBB();
412     Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
413     Cond.push_back(SecondLastInst->getOperand(0));
414     FBB = LastInst->getOperand(0).getMBB();
415     return false;
416   }
417
418   // Only supporting rr/ri versions of new-value jumps.
419   if (SecLastOpcodeHasNVJump &&
420       (SecondLastInst->getNumExplicitOperands() == 3) &&
421       (LastOpcode == Hexagon::J2_jump)) {
422     TBB = SecondLastInst->getOperand(2).getMBB();
423     Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
424     Cond.push_back(SecondLastInst->getOperand(0));
425     Cond.push_back(SecondLastInst->getOperand(1));
426     FBB = LastInst->getOperand(0).getMBB();
427     return false;
428   }
429
430   // If the block ends with two Hexagon:JMPs, handle it.  The second one is not
431   // executed, so remove it.
432   if (SecLastOpcode == Hexagon::J2_jump && LastOpcode == Hexagon::J2_jump) {
433     TBB = SecondLastInst->getOperand(0).getMBB();
434     I = LastInst->getIterator();
435     if (AllowModify)
436       I->eraseFromParent();
437     return false;
438   }
439
440   // If the block ends with an ENDLOOP, and J2_jump, handle it.
441   if (isEndLoopN(SecLastOpcode) && LastOpcode == Hexagon::J2_jump) {
442     TBB = SecondLastInst->getOperand(0).getMBB();
443     Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
444     Cond.push_back(SecondLastInst->getOperand(0));
445     FBB = LastInst->getOperand(0).getMBB();
446     return false;
447   }
448   DEBUG(dbgs() << "\nCant analyze BB#" << MBB.getNumber()
449                << " with two jumps";);
450   // Otherwise, can't handle this.
451   return true;
452 }
453
454
455 unsigned HexagonInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
456   DEBUG(dbgs() << "\nRemoving branches out of BB#" << MBB.getNumber());
457   MachineBasicBlock::iterator I = MBB.end();
458   unsigned Count = 0;
459   while (I != MBB.begin()) {
460     --I;
461     if (I->isDebugValue())
462       continue;
463     // Only removing branches from end of MBB.
464     if (!I->isBranch())
465       return Count;
466     if (Count && (I->getOpcode() == Hexagon::J2_jump))
467       llvm_unreachable("Malformed basic block: unconditional branch not last");
468     MBB.erase(&MBB.back());
469     I = MBB.end();
470     ++Count;
471   }
472   return Count;
473 }
474
475
476 unsigned HexagonInstrInfo::InsertBranch(MachineBasicBlock &MBB,
477       MachineBasicBlock *TBB, MachineBasicBlock *FBB,
478       ArrayRef<MachineOperand> Cond, DebugLoc DL) const {
479   unsigned BOpc   = Hexagon::J2_jump;
480   unsigned BccOpc = Hexagon::J2_jumpt;
481   assert(validateBranchCond(Cond) && "Invalid branching condition");
482   assert(TBB && "InsertBranch must not be told to insert a fallthrough");
483
484   // Check if ReverseBranchCondition has asked to reverse this branch
485   // If we want to reverse the branch an odd number of times, we want
486   // J2_jumpf.
487   if (!Cond.empty() && Cond[0].isImm())
488     BccOpc = Cond[0].getImm();
489
490   if (!FBB) {
491     if (Cond.empty()) {
492       // Due to a bug in TailMerging/CFG Optimization, we need to add a
493       // special case handling of a predicated jump followed by an
494       // unconditional jump. If not, Tail Merging and CFG Optimization go
495       // into an infinite loop.
496       MachineBasicBlock *NewTBB, *NewFBB;
497       SmallVector<MachineOperand, 4> Cond;
498       MachineInstr *Term = MBB.getFirstTerminator();
499       if (Term != MBB.end() && isPredicated(Term) &&
500           !AnalyzeBranch(MBB, NewTBB, NewFBB, Cond, false)) {
501         MachineBasicBlock *NextBB = &*++MBB.getIterator();
502         if (NewTBB == NextBB) {
503           ReverseBranchCondition(Cond);
504           RemoveBranch(MBB);
505           return InsertBranch(MBB, TBB, nullptr, Cond, DL);
506         }
507       }
508       BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
509     } else if (isEndLoopN(Cond[0].getImm())) {
510       int EndLoopOp = Cond[0].getImm();
511       assert(Cond[1].isMBB());
512       // Since we're adding an ENDLOOP, there better be a LOOP instruction.
513       // Check for it, and change the BB target if needed.
514       SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
515       MachineInstr *Loop = findLoopInstr(TBB, EndLoopOp, VisitedBBs);
516       assert(Loop != 0 && "Inserting an ENDLOOP without a LOOP");
517       Loop->getOperand(0).setMBB(TBB);
518       // Add the ENDLOOP after the finding the LOOP0.
519       BuildMI(&MBB, DL, get(EndLoopOp)).addMBB(TBB);
520     } else if (isNewValueJump(Cond[0].getImm())) {
521       assert((Cond.size() == 3) && "Only supporting rr/ri version of nvjump");
522       // New value jump
523       // (ins IntRegs:$src1, IntRegs:$src2, brtarget:$offset)
524       // (ins IntRegs:$src1, u5Imm:$src2, brtarget:$offset)
525       unsigned Flags1 = getUndefRegState(Cond[1].isUndef());
526       DEBUG(dbgs() << "\nInserting NVJump for BB#" << MBB.getNumber(););
527       if (Cond[2].isReg()) {
528         unsigned Flags2 = getUndefRegState(Cond[2].isUndef());
529         BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[1].getReg(), Flags1).
530           addReg(Cond[2].getReg(), Flags2).addMBB(TBB);
531       } else if(Cond[2].isImm()) {
532         BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[1].getReg(), Flags1).
533           addImm(Cond[2].getImm()).addMBB(TBB);
534       } else
535         llvm_unreachable("Invalid condition for branching");
536     } else {
537       assert((Cond.size() == 2) && "Malformed cond vector");
538       const MachineOperand &RO = Cond[1];
539       unsigned Flags = getUndefRegState(RO.isUndef());
540       BuildMI(&MBB, DL, get(BccOpc)).addReg(RO.getReg(), Flags).addMBB(TBB);
541     }
542     return 1;
543   }
544   assert((!Cond.empty()) &&
545          "Cond. cannot be empty when multiple branchings are required");
546   assert((!isNewValueJump(Cond[0].getImm())) &&
547          "NV-jump cannot be inserted with another branch");
548   // Special case for hardware loops.  The condition is a basic block.
549   if (isEndLoopN(Cond[0].getImm())) {
550     int EndLoopOp = Cond[0].getImm();
551     assert(Cond[1].isMBB());
552     // Since we're adding an ENDLOOP, there better be a LOOP instruction.
553     // Check for it, and change the BB target if needed.
554     SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
555     MachineInstr *Loop = findLoopInstr(TBB, EndLoopOp, VisitedBBs);
556     assert(Loop != 0 && "Inserting an ENDLOOP without a LOOP");
557     Loop->getOperand(0).setMBB(TBB);
558     // Add the ENDLOOP after the finding the LOOP0.
559     BuildMI(&MBB, DL, get(EndLoopOp)).addMBB(TBB);
560   } else {
561     const MachineOperand &RO = Cond[1];
562     unsigned Flags = getUndefRegState(RO.isUndef());
563     BuildMI(&MBB, DL, get(BccOpc)).addReg(RO.getReg(), Flags).addMBB(TBB);
564   }
565   BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
566
567   return 2;
568 }
569
570
571 bool HexagonInstrInfo::isProfitableToIfCvt(MachineBasicBlock &MBB,
572       unsigned NumCycles, unsigned ExtraPredCycles,
573       BranchProbability Probability) const {
574   return nonDbgBBSize(&MBB) <= 3;
575 }
576
577
578 bool HexagonInstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB,
579       unsigned NumTCycles, unsigned ExtraTCycles, MachineBasicBlock &FMBB,
580       unsigned NumFCycles, unsigned ExtraFCycles, BranchProbability Probability)
581       const {
582   return nonDbgBBSize(&TMBB) <= 3 && nonDbgBBSize(&FMBB) <= 3;
583 }
584
585
586 bool HexagonInstrInfo::isProfitableToDupForIfCvt(MachineBasicBlock &MBB,
587       unsigned NumInstrs, BranchProbability Probability) const {
588   return NumInstrs <= 4;
589 }
590
591
592 void HexagonInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
593       MachineBasicBlock::iterator I, DebugLoc DL, unsigned DestReg,
594       unsigned SrcReg, bool KillSrc) const {
595   auto &HRI = getRegisterInfo();
596   if (Hexagon::IntRegsRegClass.contains(SrcReg, DestReg)) {
597     BuildMI(MBB, I, DL, get(Hexagon::A2_tfr), DestReg).addReg(SrcReg);
598     return;
599   }
600   if (Hexagon::DoubleRegsRegClass.contains(SrcReg, DestReg)) {
601     BuildMI(MBB, I, DL, get(Hexagon::A2_tfrp), DestReg).addReg(SrcReg);
602     return;
603   }
604   if (Hexagon::PredRegsRegClass.contains(SrcReg, DestReg)) {
605     // Map Pd = Ps to Pd = or(Ps, Ps).
606     BuildMI(MBB, I, DL, get(Hexagon::C2_or),
607             DestReg).addReg(SrcReg).addReg(SrcReg);
608     return;
609   }
610   if (Hexagon::DoubleRegsRegClass.contains(DestReg) &&
611       Hexagon::IntRegsRegClass.contains(SrcReg)) {
612     // We can have an overlap between single and double reg: r1:0 = r0.
613     if(SrcReg == RI.getSubReg(DestReg, Hexagon::subreg_loreg)) {
614         // r1:0 = r0
615         BuildMI(MBB, I, DL, get(Hexagon::A2_tfrsi), (RI.getSubReg(DestReg,
616                 Hexagon::subreg_hireg))).addImm(0);
617     } else {
618         // r1:0 = r1 or no overlap.
619         BuildMI(MBB, I, DL, get(Hexagon::A2_tfr), (RI.getSubReg(DestReg,
620                 Hexagon::subreg_loreg))).addReg(SrcReg);
621         BuildMI(MBB, I, DL, get(Hexagon::A2_tfrsi), (RI.getSubReg(DestReg,
622                 Hexagon::subreg_hireg))).addImm(0);
623     }
624     return;
625   }
626   if (Hexagon::CtrRegsRegClass.contains(DestReg) &&
627       Hexagon::IntRegsRegClass.contains(SrcReg)) {
628     BuildMI(MBB, I, DL, get(Hexagon::A2_tfrrcr), DestReg).addReg(SrcReg);
629     return;
630   }
631   if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
632       Hexagon::IntRegsRegClass.contains(DestReg)) {
633     BuildMI(MBB, I, DL, get(Hexagon::C2_tfrpr), DestReg).
634       addReg(SrcReg, getKillRegState(KillSrc));
635     return;
636   }
637   if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
638       Hexagon::PredRegsRegClass.contains(DestReg)) {
639     BuildMI(MBB, I, DL, get(Hexagon::C2_tfrrp), DestReg).
640       addReg(SrcReg, getKillRegState(KillSrc));
641     return;
642   }
643   if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
644       Hexagon::IntRegsRegClass.contains(DestReg)) {
645     BuildMI(MBB, I, DL, get(Hexagon::C2_tfrpr), DestReg).
646       addReg(SrcReg, getKillRegState(KillSrc));
647     return;
648   }
649   if (Hexagon::VectorRegsRegClass.contains(SrcReg, DestReg)) {
650     BuildMI(MBB, I, DL, get(Hexagon::V6_vassign), DestReg).
651       addReg(SrcReg, getKillRegState(KillSrc));
652     return;
653   }
654   if (Hexagon::VecDblRegsRegClass.contains(SrcReg, DestReg)) {
655     BuildMI(MBB, I, DL, get(Hexagon::V6_vcombine), DestReg).
656       addReg(HRI.getSubReg(SrcReg, Hexagon::subreg_hireg),
657              getKillRegState(KillSrc)).
658       addReg(HRI.getSubReg(SrcReg, Hexagon::subreg_loreg),
659              getKillRegState(KillSrc));
660     return;
661   }
662   if (Hexagon::VecPredRegsRegClass.contains(SrcReg, DestReg)) {
663     BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and), DestReg).
664       addReg(SrcReg).
665       addReg(SrcReg, getKillRegState(KillSrc));
666     return;
667   }
668   if (Hexagon::VecPredRegsRegClass.contains(SrcReg) &&
669     Hexagon::VectorRegsRegClass.contains(DestReg)) {
670     llvm_unreachable("Unimplemented pred to vec");
671     return;
672   }
673   if (Hexagon::VecPredRegsRegClass.contains(DestReg) &&
674       Hexagon::VectorRegsRegClass.contains(SrcReg)) {
675     llvm_unreachable("Unimplemented vec to pred");
676     return;
677   }
678   if (Hexagon::VecPredRegs128BRegClass.contains(SrcReg, DestReg)) {
679     BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and),
680       HRI.getSubReg(DestReg, Hexagon::subreg_hireg)).
681       addReg(HRI.getSubReg(SrcReg, Hexagon::subreg_hireg),
682              getKillRegState(KillSrc));
683     BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and),
684       HRI.getSubReg(DestReg, Hexagon::subreg_loreg)).
685       addReg(HRI.getSubReg(SrcReg, Hexagon::subreg_loreg),
686              getKillRegState(KillSrc));
687     return;
688   }
689
690 #ifndef NDEBUG
691   // Show the invalid registers to ease debugging.
692   dbgs() << "Invalid registers for copy in BB#" << MBB.getNumber()
693          << ": " << PrintReg(DestReg, &HRI)
694          << " = " << PrintReg(SrcReg, &HRI) << '\n';
695 #endif
696   llvm_unreachable("Unimplemented");
697 }
698
699
700 void HexagonInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
701       MachineBasicBlock::iterator I, unsigned SrcReg, bool isKill, int FI,
702       const TargetRegisterClass *RC, const TargetRegisterInfo *TRI) const {
703   DebugLoc DL = MBB.findDebugLoc(I);
704   MachineFunction &MF = *MBB.getParent();
705   MachineFrameInfo &MFI = *MF.getFrameInfo();
706   unsigned Align = MFI.getObjectAlignment(FI);
707
708   MachineMemOperand *MMO = MF.getMachineMemOperand(
709       MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOStore,
710       MFI.getObjectSize(FI), Align);
711
712   if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
713     BuildMI(MBB, I, DL, get(Hexagon::S2_storeri_io))
714           .addFrameIndex(FI).addImm(0)
715           .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
716   } else if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC)) {
717     BuildMI(MBB, I, DL, get(Hexagon::S2_storerd_io))
718           .addFrameIndex(FI).addImm(0)
719           .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
720   } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
721     BuildMI(MBB, I, DL, get(Hexagon::STriw_pred))
722           .addFrameIndex(FI).addImm(0)
723           .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
724   } else {
725     llvm_unreachable("Unimplemented");
726   }
727 }
728
729
730 void HexagonInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
731       MachineBasicBlock::iterator I, unsigned DestReg, int FI,
732       const TargetRegisterClass *RC, const TargetRegisterInfo *TRI) const {
733   DebugLoc DL = MBB.findDebugLoc(I);
734   MachineFunction &MF = *MBB.getParent();
735   MachineFrameInfo &MFI = *MF.getFrameInfo();
736   unsigned Align = MFI.getObjectAlignment(FI);
737
738   MachineMemOperand *MMO = MF.getMachineMemOperand(
739       MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOLoad,
740       MFI.getObjectSize(FI), Align);
741   if (RC == &Hexagon::IntRegsRegClass) {
742     BuildMI(MBB, I, DL, get(Hexagon::L2_loadri_io), DestReg)
743           .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
744   } else if (RC == &Hexagon::DoubleRegsRegClass) {
745     BuildMI(MBB, I, DL, get(Hexagon::L2_loadrd_io), DestReg)
746           .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
747   } else if (RC == &Hexagon::PredRegsRegClass) {
748     BuildMI(MBB, I, DL, get(Hexagon::LDriw_pred), DestReg)
749           .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
750   } else {
751     llvm_unreachable("Can't store this register to stack slot");
752   }
753 }
754
755
756 /// expandPostRAPseudo - This function is called for all pseudo instructions
757 /// that remain after register allocation. Many pseudo instructions are
758 /// created to help register allocation. This is the place to convert them
759 /// into real instructions. The target can edit MI in place, or it can insert
760 /// new instructions and erase MI. The function should return true if
761 /// anything was changed.
762 bool HexagonInstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI)
763       const {
764   const HexagonRegisterInfo &HRI = getRegisterInfo();
765   MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
766   MachineBasicBlock &MBB = *MI->getParent();
767   DebugLoc DL = MI->getDebugLoc();
768   unsigned Opc = MI->getOpcode();
769   const unsigned VecOffset = 1;
770   bool Is128B = false;
771
772   switch (Opc) {
773     case Hexagon::ALIGNA:
774       BuildMI(MBB, MI, DL, get(Hexagon::A2_andir), MI->getOperand(0).getReg())
775           .addReg(HRI.getFrameRegister())
776           .addImm(-MI->getOperand(1).getImm());
777       MBB.erase(MI);
778       return true;
779     case Hexagon::STrivv_indexed_128B:
780       Is128B = true;
781     case Hexagon::STrivv_indexed: {
782       unsigned SrcReg = MI->getOperand(2).getReg();
783       unsigned SrcSubHi = HRI.getSubReg(SrcReg, Hexagon::subreg_hireg);
784       unsigned SrcSubLo = HRI.getSubReg(SrcReg, Hexagon::subreg_loreg);
785       unsigned NewOpcd = Is128B ? Hexagon::V6_vS32b_ai_128B
786                                 : Hexagon::V6_vS32b_ai;
787       unsigned Offset = Is128B ? VecOffset << 7 : VecOffset << 6;
788       MachineInstr *MI1New = BuildMI(MBB, MI, DL, get(NewOpcd))
789           .addOperand(MI->getOperand(0))
790           .addImm(MI->getOperand(1).getImm())
791           .addReg(SrcSubLo)
792           .setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
793       MI1New->getOperand(0).setIsKill(false);
794       BuildMI(MBB, MI, DL, get(NewOpcd))
795         .addOperand(MI->getOperand(0))
796         // The Vectors are indexed in multiples of vector size.
797         .addImm(MI->getOperand(1).getImm()+Offset)
798         .addReg(SrcSubHi)
799         .setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
800       MBB.erase(MI);
801       return true;
802     }
803     case Hexagon::LDrivv_pseudo_V6_128B:
804     case Hexagon::LDrivv_indexed_128B:
805       Is128B = true;
806     case Hexagon::LDrivv_pseudo_V6:
807     case Hexagon::LDrivv_indexed: {
808       unsigned NewOpcd = Is128B ? Hexagon::V6_vL32b_ai_128B
809                                 : Hexagon::V6_vL32b_ai;
810       unsigned DstReg = MI->getOperand(0).getReg();
811       unsigned Offset = Is128B ? VecOffset << 7 : VecOffset << 6;
812       MachineInstr *MI1New =
813           BuildMI(MBB, MI, DL, get(NewOpcd),
814                   HRI.getSubReg(DstReg, Hexagon::subreg_loreg))
815               .addOperand(MI->getOperand(1))
816               .addImm(MI->getOperand(2).getImm());
817       MI1New->getOperand(1).setIsKill(false);
818       BuildMI(MBB, MI, DL, get(NewOpcd),
819               HRI.getSubReg(DstReg, Hexagon::subreg_hireg))
820           .addOperand(MI->getOperand(1))
821           // The Vectors are indexed in multiples of vector size.
822           .addImm(MI->getOperand(2).getImm() + Offset)
823           .setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
824       MBB.erase(MI);
825       return true;
826     }
827     case Hexagon::LDriv_pseudo_V6_128B:
828       Is128B = true;
829     case Hexagon::LDriv_pseudo_V6: {
830       unsigned DstReg = MI->getOperand(0).getReg();
831       unsigned NewOpc = Is128B ? Hexagon::V6_vL32b_ai_128B
832                                : Hexagon::V6_vL32b_ai;
833       int32_t Off = MI->getOperand(2).getImm();
834       int32_t Idx = Off;
835       BuildMI(MBB, MI, DL, get(NewOpc), DstReg)
836         .addOperand(MI->getOperand(1))
837         .addImm(Idx)
838         .setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
839       MBB.erase(MI);
840       return true;
841     }
842     case Hexagon::STriv_pseudo_V6_128B:
843       Is128B = true;
844     case Hexagon::STriv_pseudo_V6: {
845       unsigned NewOpc = Is128B ? Hexagon::V6_vS32b_ai_128B
846                                : Hexagon::V6_vS32b_ai;
847       int32_t Off = MI->getOperand(1).getImm();
848       int32_t Idx = Is128B ? (Off >> 7) : (Off >> 6);
849       BuildMI(MBB, MI, DL, get(NewOpc))
850         .addOperand(MI->getOperand(0))
851         .addImm(Idx)
852         .addOperand(MI->getOperand(2))
853         .setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
854       MBB.erase(MI);
855       return true;
856     }
857     case Hexagon::TFR_PdTrue: {
858       unsigned Reg = MI->getOperand(0).getReg();
859       BuildMI(MBB, MI, DL, get(Hexagon::C2_orn), Reg)
860         .addReg(Reg, RegState::Undef)
861         .addReg(Reg, RegState::Undef);
862       MBB.erase(MI);
863       return true;
864     }
865     case Hexagon::TFR_PdFalse: {
866       unsigned Reg = MI->getOperand(0).getReg();
867       BuildMI(MBB, MI, DL, get(Hexagon::C2_andn), Reg)
868         .addReg(Reg, RegState::Undef)
869         .addReg(Reg, RegState::Undef);
870       MBB.erase(MI);
871       return true;
872     }
873     case Hexagon::VMULW: {
874       // Expand a 64-bit vector multiply into 2 32-bit scalar multiplies.
875       unsigned DstReg = MI->getOperand(0).getReg();
876       unsigned Src1Reg = MI->getOperand(1).getReg();
877       unsigned Src2Reg = MI->getOperand(2).getReg();
878       unsigned Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::subreg_hireg);
879       unsigned Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::subreg_loreg);
880       unsigned Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::subreg_hireg);
881       unsigned Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::subreg_loreg);
882       BuildMI(MBB, MI, MI->getDebugLoc(), get(Hexagon::M2_mpyi),
883               HRI.getSubReg(DstReg, Hexagon::subreg_hireg)).addReg(Src1SubHi)
884           .addReg(Src2SubHi);
885       BuildMI(MBB, MI, MI->getDebugLoc(), get(Hexagon::M2_mpyi),
886               HRI.getSubReg(DstReg, Hexagon::subreg_loreg)).addReg(Src1SubLo)
887           .addReg(Src2SubLo);
888       MBB.erase(MI);
889       MRI.clearKillFlags(Src1SubHi);
890       MRI.clearKillFlags(Src1SubLo);
891       MRI.clearKillFlags(Src2SubHi);
892       MRI.clearKillFlags(Src2SubLo);
893       return true;
894     }
895     case Hexagon::VMULW_ACC: {
896       // Expand 64-bit vector multiply with addition into 2 scalar multiplies.
897       unsigned DstReg = MI->getOperand(0).getReg();
898       unsigned Src1Reg = MI->getOperand(1).getReg();
899       unsigned Src2Reg = MI->getOperand(2).getReg();
900       unsigned Src3Reg = MI->getOperand(3).getReg();
901       unsigned Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::subreg_hireg);
902       unsigned Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::subreg_loreg);
903       unsigned Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::subreg_hireg);
904       unsigned Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::subreg_loreg);
905       unsigned Src3SubHi = HRI.getSubReg(Src3Reg, Hexagon::subreg_hireg);
906       unsigned Src3SubLo = HRI.getSubReg(Src3Reg, Hexagon::subreg_loreg);
907       BuildMI(MBB, MI, MI->getDebugLoc(), get(Hexagon::M2_maci),
908               HRI.getSubReg(DstReg, Hexagon::subreg_hireg)).addReg(Src1SubHi)
909           .addReg(Src2SubHi).addReg(Src3SubHi);
910       BuildMI(MBB, MI, MI->getDebugLoc(), get(Hexagon::M2_maci),
911               HRI.getSubReg(DstReg, Hexagon::subreg_loreg)).addReg(Src1SubLo)
912           .addReg(Src2SubLo).addReg(Src3SubLo);
913       MBB.erase(MI);
914       MRI.clearKillFlags(Src1SubHi);
915       MRI.clearKillFlags(Src1SubLo);
916       MRI.clearKillFlags(Src2SubHi);
917       MRI.clearKillFlags(Src2SubLo);
918       MRI.clearKillFlags(Src3SubHi);
919       MRI.clearKillFlags(Src3SubLo);
920       return true;
921     }
922     case Hexagon::MUX64_rr: {
923       const MachineOperand &Op0 = MI->getOperand(0);
924       const MachineOperand &Op1 = MI->getOperand(1);
925       const MachineOperand &Op2 = MI->getOperand(2);
926       const MachineOperand &Op3 = MI->getOperand(3);
927       unsigned Rd = Op0.getReg();
928       unsigned Pu = Op1.getReg();
929       unsigned Rs = Op2.getReg();
930       unsigned Rt = Op3.getReg();
931       DebugLoc DL = MI->getDebugLoc();
932       unsigned K1 = getKillRegState(Op1.isKill());
933       unsigned K2 = getKillRegState(Op2.isKill());
934       unsigned K3 = getKillRegState(Op3.isKill());
935       if (Rd != Rs)
936         BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpt), Rd)
937           .addReg(Pu, (Rd == Rt) ? K1 : 0)
938           .addReg(Rs, K2);
939       if (Rd != Rt)
940         BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpf), Rd)
941           .addReg(Pu, K1)
942           .addReg(Rt, K3);
943       MBB.erase(MI);
944       return true;
945     }
946     case Hexagon::TCRETURNi:
947       MI->setDesc(get(Hexagon::J2_jump));
948       return true;
949     case Hexagon::TCRETURNr:
950       MI->setDesc(get(Hexagon::J2_jumpr));
951       return true;
952     case Hexagon::TFRI_f:
953     case Hexagon::TFRI_cPt_f:
954     case Hexagon::TFRI_cNotPt_f: {
955       unsigned Opx = (Opc == Hexagon::TFRI_f) ? 1 : 2;
956       APFloat FVal = MI->getOperand(Opx).getFPImm()->getValueAPF();
957       APInt IVal = FVal.bitcastToAPInt();
958       MI->RemoveOperand(Opx);
959       unsigned NewOpc = (Opc == Hexagon::TFRI_f)     ? Hexagon::A2_tfrsi   :
960                         (Opc == Hexagon::TFRI_cPt_f) ? Hexagon::C2_cmoveit :
961                                                        Hexagon::C2_cmoveif;
962       MI->setDesc(get(NewOpc));
963       MI->addOperand(MachineOperand::CreateImm(IVal.getZExtValue()));
964       return true;
965     }
966   }
967
968   return false;
969 }
970
971
972 // We indicate that we want to reverse the branch by
973 // inserting the reversed branching opcode.
974 bool HexagonInstrInfo::ReverseBranchCondition(
975       SmallVectorImpl<MachineOperand> &Cond) const {
976   if (Cond.empty())
977     return true;
978   assert(Cond[0].isImm() && "First entry in the cond vector not imm-val");
979   unsigned opcode = Cond[0].getImm();
980   //unsigned temp;
981   assert(get(opcode).isBranch() && "Should be a branching condition.");
982   if (isEndLoopN(opcode))
983     return true;
984   unsigned NewOpcode = getInvertedPredicatedOpcode(opcode);
985   Cond[0].setImm(NewOpcode);
986   return false;
987 }
988
989
990 void HexagonInstrInfo::insertNoop(MachineBasicBlock &MBB,
991       MachineBasicBlock::iterator MI) const {
992   DebugLoc DL;
993   BuildMI(MBB, MI, DL, get(Hexagon::A2_nop));
994 }
995
996
997 // Returns true if an instruction is predicated irrespective of the predicate
998 // sense. For example, all of the following will return true.
999 // if (p0) R1 = add(R2, R3)
1000 // if (!p0) R1 = add(R2, R3)
1001 // if (p0.new) R1 = add(R2, R3)
1002 // if (!p0.new) R1 = add(R2, R3)
1003 // Note: New-value stores are not included here as in the current
1004 // implementation, we don't need to check their predicate sense.
1005 bool HexagonInstrInfo::isPredicated(const MachineInstr *MI) const {
1006   const uint64_t F = MI->getDesc().TSFlags;
1007   return (F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask;
1008 }
1009
1010
1011 bool HexagonInstrInfo::PredicateInstruction(MachineInstr *MI,
1012       ArrayRef<MachineOperand> Cond) const {
1013   if (Cond.empty() || isNewValueJump(Cond[0].getImm()) ||
1014       isEndLoopN(Cond[0].getImm())) {
1015     DEBUG(dbgs() << "\nCannot predicate:"; MI->dump(););
1016     return false;
1017   }
1018   int Opc = MI->getOpcode();
1019   assert (isPredicable(MI) && "Expected predicable instruction");
1020   bool invertJump = predOpcodeHasNot(Cond);
1021
1022   // We have to predicate MI "in place", i.e. after this function returns,
1023   // MI will need to be transformed into a predicated form. To avoid com-
1024   // plicated manipulations with the operands (handling tied operands,
1025   // etc.), build a new temporary instruction, then overwrite MI with it.
1026
1027   MachineBasicBlock &B = *MI->getParent();
1028   DebugLoc DL = MI->getDebugLoc();
1029   unsigned PredOpc = getCondOpcode(Opc, invertJump);
1030   MachineInstrBuilder T = BuildMI(B, MI, DL, get(PredOpc));
1031   unsigned NOp = 0, NumOps = MI->getNumOperands();
1032   while (NOp < NumOps) {
1033     MachineOperand &Op = MI->getOperand(NOp);
1034     if (!Op.isReg() || !Op.isDef() || Op.isImplicit())
1035       break;
1036     T.addOperand(Op);
1037     NOp++;
1038   }
1039
1040   unsigned PredReg, PredRegPos, PredRegFlags;
1041   bool GotPredReg = getPredReg(Cond, PredReg, PredRegPos, PredRegFlags);
1042   (void)GotPredReg;
1043   assert(GotPredReg);
1044   T.addReg(PredReg, PredRegFlags);
1045   while (NOp < NumOps)
1046     T.addOperand(MI->getOperand(NOp++));
1047
1048   MI->setDesc(get(PredOpc));
1049   while (unsigned n = MI->getNumOperands())
1050     MI->RemoveOperand(n-1);
1051   for (unsigned i = 0, n = T->getNumOperands(); i < n; ++i)
1052     MI->addOperand(T->getOperand(i));
1053
1054   MachineBasicBlock::instr_iterator TI = T->getIterator();
1055   B.erase(TI);
1056
1057   MachineRegisterInfo &MRI = B.getParent()->getRegInfo();
1058   MRI.clearKillFlags(PredReg);
1059   return true;
1060 }
1061
1062
1063 bool HexagonInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
1064       ArrayRef<MachineOperand> Pred2) const {
1065   // TODO: Fix this
1066   return false;
1067 }
1068
1069
1070 bool HexagonInstrInfo::DefinesPredicate(MachineInstr *MI,
1071                                    std::vector<MachineOperand> &Pred) const {
1072   auto &HRI = getRegisterInfo();
1073   for (unsigned oper = 0; oper < MI->getNumOperands(); ++oper) {
1074     MachineOperand MO = MI->getOperand(oper);
1075     if (MO.isReg() && MO.isDef()) {
1076       const TargetRegisterClass* RC = HRI.getMinimalPhysRegClass(MO.getReg());
1077       if (RC == &Hexagon::PredRegsRegClass) {
1078         Pred.push_back(MO);
1079         return true;
1080       }
1081     }
1082   }
1083   return false;
1084 }
1085
1086 bool HexagonInstrInfo::isPredicable(MachineInstr *MI) const {
1087   bool isPred = MI->getDesc().isPredicable();
1088
1089   if (!isPred)
1090     return false;
1091
1092   const int Opc = MI->getOpcode();
1093   int NumOperands = MI->getNumOperands();
1094
1095   // Keep a flag for upto 4 operands in the instructions, to indicate if
1096   // that operand has been constant extended.
1097   bool OpCExtended[4];
1098   if (NumOperands > 4)
1099     NumOperands = 4;
1100
1101   for (int i = 0; i < NumOperands; i++)
1102     OpCExtended[i] = (isOperandExtended(MI, i) && isConstExtended(MI));
1103
1104   switch(Opc) {
1105   case Hexagon::A2_tfrsi:
1106     return (isOperandExtended(MI, 1) && isConstExtended(MI)) ||
1107            isInt<12>(MI->getOperand(1).getImm());
1108
1109   case Hexagon::S2_storerd_io:
1110     return isShiftedUInt<6,3>(MI->getOperand(1).getImm());
1111
1112   case Hexagon::S2_storeri_io:
1113   case Hexagon::S2_storerinew_io:
1114     return isShiftedUInt<6,2>(MI->getOperand(1).getImm());
1115
1116   case Hexagon::S2_storerh_io:
1117   case Hexagon::S2_storerhnew_io:
1118     return isShiftedUInt<6,1>(MI->getOperand(1).getImm());
1119
1120   case Hexagon::S2_storerb_io:
1121   case Hexagon::S2_storerbnew_io:
1122     return isUInt<6>(MI->getOperand(1).getImm());
1123
1124   case Hexagon::L2_loadrd_io:
1125     return isShiftedUInt<6,3>(MI->getOperand(2).getImm());
1126
1127   case Hexagon::L2_loadri_io:
1128     return isShiftedUInt<6,2>(MI->getOperand(2).getImm());
1129
1130   case Hexagon::L2_loadrh_io:
1131   case Hexagon::L2_loadruh_io:
1132     return isShiftedUInt<6,1>(MI->getOperand(2).getImm());
1133
1134   case Hexagon::L2_loadrb_io:
1135   case Hexagon::L2_loadrub_io:
1136     return isUInt<6>(MI->getOperand(2).getImm());
1137
1138   case Hexagon::L2_loadrd_pi:
1139     return isShiftedInt<4,3>(MI->getOperand(3).getImm());
1140
1141   case Hexagon::L2_loadri_pi:
1142     return isShiftedInt<4,2>(MI->getOperand(3).getImm());
1143
1144   case Hexagon::L2_loadrh_pi:
1145   case Hexagon::L2_loadruh_pi:
1146     return isShiftedInt<4,1>(MI->getOperand(3).getImm());
1147
1148   case Hexagon::L2_loadrb_pi:
1149   case Hexagon::L2_loadrub_pi:
1150     return isInt<4>(MI->getOperand(3).getImm());
1151
1152   case Hexagon::S4_storeirb_io:
1153   case Hexagon::S4_storeirh_io:
1154   case Hexagon::S4_storeiri_io:
1155     return (OpCExtended[1] || isUInt<6>(MI->getOperand(1).getImm())) &&
1156            (OpCExtended[2] || isInt<6>(MI->getOperand(2).getImm()));
1157
1158   case Hexagon::A2_addi:
1159     return isInt<8>(MI->getOperand(2).getImm());
1160
1161   case Hexagon::A2_aslh:
1162   case Hexagon::A2_asrh:
1163   case Hexagon::A2_sxtb:
1164   case Hexagon::A2_sxth:
1165   case Hexagon::A2_zxtb:
1166   case Hexagon::A2_zxth:
1167     return true;
1168   }
1169
1170   return true;
1171 }
1172
1173
1174 bool HexagonInstrInfo::isSchedulingBoundary(const MachineInstr *MI,
1175       const MachineBasicBlock *MBB, const MachineFunction &MF) const {
1176   // Debug info is never a scheduling boundary. It's necessary to be explicit
1177   // due to the special treatment of IT instructions below, otherwise a
1178   // dbg_value followed by an IT will result in the IT instruction being
1179   // considered a scheduling hazard, which is wrong. It should be the actual
1180   // instruction preceding the dbg_value instruction(s), just like it is
1181   // when debug info is not present.
1182   if (MI->isDebugValue())
1183     return false;
1184
1185   // Throwing call is a boundary.
1186   if (MI->isCall()) {
1187     // If any of the block's successors is a landing pad, this could be a
1188     // throwing call.
1189     for (auto I : MBB->successors())
1190       if (I->isEHPad())
1191         return true;
1192   }
1193
1194   // Don't mess around with no return calls.
1195   if (MI->getOpcode() == Hexagon::CALLv3nr)
1196     return true;
1197
1198   // Terminators and labels can't be scheduled around.
1199   if (MI->getDesc().isTerminator() || MI->isPosition())
1200     return true;
1201
1202   if (MI->isInlineAsm() && !ScheduleInlineAsm)
1203       return true;
1204
1205   return false;
1206 }
1207
1208
1209 /// Measure the specified inline asm to determine an approximation of its
1210 /// length.
1211 /// Comments (which run till the next SeparatorString or newline) do not
1212 /// count as an instruction.
1213 /// Any other non-whitespace text is considered an instruction, with
1214 /// multiple instructions separated by SeparatorString or newlines.
1215 /// Variable-length instructions are not handled here; this function
1216 /// may be overloaded in the target code to do that.
1217 /// Hexagon counts the number of ##'s and adjust for that many
1218 /// constant exenders.
1219 unsigned HexagonInstrInfo::getInlineAsmLength(const char *Str,
1220       const MCAsmInfo &MAI) const {
1221   StringRef AStr(Str);
1222   // Count the number of instructions in the asm.
1223   bool atInsnStart = true;
1224   unsigned Length = 0;
1225   for (; *Str; ++Str) {
1226     if (*Str == '\n' || strncmp(Str, MAI.getSeparatorString(),
1227                                 strlen(MAI.getSeparatorString())) == 0)
1228       atInsnStart = true;
1229     if (atInsnStart && !std::isspace(static_cast<unsigned char>(*Str))) {
1230       Length += MAI.getMaxInstLength();
1231       atInsnStart = false;
1232     }
1233     if (atInsnStart && strncmp(Str, MAI.getCommentString(),
1234                                strlen(MAI.getCommentString())) == 0)
1235       atInsnStart = false;
1236   }
1237
1238   // Add to size number of constant extenders seen * 4.
1239   StringRef Occ("##");
1240   Length += AStr.count(Occ)*4;
1241   return Length;
1242 }
1243
1244
1245 ScheduleHazardRecognizer*
1246 HexagonInstrInfo::CreateTargetPostRAHazardRecognizer(
1247       const InstrItineraryData *II, const ScheduleDAG *DAG) const {
1248   return TargetInstrInfo::CreateTargetPostRAHazardRecognizer(II, DAG);
1249 }
1250
1251
1252 /// \brief For a comparison instruction, return the source registers in
1253 /// \p SrcReg and \p SrcReg2 if having two register operands, and the value it
1254 /// compares against in CmpValue. Return true if the comparison instruction
1255 /// can be analyzed.
1256 bool HexagonInstrInfo::analyzeCompare(const MachineInstr *MI,
1257       unsigned &SrcReg, unsigned &SrcReg2, int &Mask, int &Value) const {
1258   unsigned Opc = MI->getOpcode();
1259
1260   // Set mask and the first source register.
1261   switch (Opc) {
1262     case Hexagon::C2_cmpeq:
1263     case Hexagon::C2_cmpeqp:
1264     case Hexagon::C2_cmpgt:
1265     case Hexagon::C2_cmpgtp:
1266     case Hexagon::C2_cmpgtu:
1267     case Hexagon::C2_cmpgtup:
1268     case Hexagon::C4_cmpneq:
1269     case Hexagon::C4_cmplte:
1270     case Hexagon::C4_cmplteu:
1271     case Hexagon::C2_cmpeqi:
1272     case Hexagon::C2_cmpgti:
1273     case Hexagon::C2_cmpgtui:
1274     case Hexagon::C4_cmpneqi:
1275     case Hexagon::C4_cmplteui:
1276     case Hexagon::C4_cmpltei:
1277       SrcReg = MI->getOperand(1).getReg();
1278       Mask = ~0;
1279       break;
1280     case Hexagon::A4_cmpbeq:
1281     case Hexagon::A4_cmpbgt:
1282     case Hexagon::A4_cmpbgtu:
1283     case Hexagon::A4_cmpbeqi:
1284     case Hexagon::A4_cmpbgti:
1285     case Hexagon::A4_cmpbgtui:
1286       SrcReg = MI->getOperand(1).getReg();
1287       Mask = 0xFF;
1288       break;
1289     case Hexagon::A4_cmpheq:
1290     case Hexagon::A4_cmphgt:
1291     case Hexagon::A4_cmphgtu:
1292     case Hexagon::A4_cmpheqi:
1293     case Hexagon::A4_cmphgti:
1294     case Hexagon::A4_cmphgtui:
1295       SrcReg = MI->getOperand(1).getReg();
1296       Mask = 0xFFFF;
1297       break;
1298   }
1299
1300   // Set the value/second source register.
1301   switch (Opc) {
1302     case Hexagon::C2_cmpeq:
1303     case Hexagon::C2_cmpeqp:
1304     case Hexagon::C2_cmpgt:
1305     case Hexagon::C2_cmpgtp:
1306     case Hexagon::C2_cmpgtu:
1307     case Hexagon::C2_cmpgtup:
1308     case Hexagon::A4_cmpbeq:
1309     case Hexagon::A4_cmpbgt:
1310     case Hexagon::A4_cmpbgtu:
1311     case Hexagon::A4_cmpheq:
1312     case Hexagon::A4_cmphgt:
1313     case Hexagon::A4_cmphgtu:
1314     case Hexagon::C4_cmpneq:
1315     case Hexagon::C4_cmplte:
1316     case Hexagon::C4_cmplteu:
1317       SrcReg2 = MI->getOperand(2).getReg();
1318       return true;
1319
1320     case Hexagon::C2_cmpeqi:
1321     case Hexagon::C2_cmpgtui:
1322     case Hexagon::C2_cmpgti:
1323     case Hexagon::C4_cmpneqi:
1324     case Hexagon::C4_cmplteui:
1325     case Hexagon::C4_cmpltei:
1326     case Hexagon::A4_cmpbeqi:
1327     case Hexagon::A4_cmpbgti:
1328     case Hexagon::A4_cmpbgtui:
1329     case Hexagon::A4_cmpheqi:
1330     case Hexagon::A4_cmphgti:
1331     case Hexagon::A4_cmphgtui:
1332       SrcReg2 = 0;
1333       Value = MI->getOperand(2).getImm();
1334       return true;
1335   }
1336
1337   return false;
1338 }
1339
1340
1341 unsigned HexagonInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
1342       const MachineInstr *MI, unsigned *PredCost) const {
1343   return getInstrTimingClassLatency(ItinData, MI);
1344 }
1345
1346
1347 DFAPacketizer *HexagonInstrInfo::CreateTargetScheduleState(
1348     const TargetSubtargetInfo &STI) const {
1349   const InstrItineraryData *II = STI.getInstrItineraryData();
1350   return static_cast<const HexagonSubtarget&>(STI).createDFAPacketizer(II);
1351 }
1352
1353
1354 // Inspired by this pair:
1355 //  %R13<def> = L2_loadri_io %R29, 136; mem:LD4[FixedStack0]
1356 //  S2_storeri_io %R29, 132, %R1<kill>; flags:  mem:ST4[FixedStack1]
1357 // Currently AA considers the addresses in these instructions to be aliasing.
1358 bool HexagonInstrInfo::areMemAccessesTriviallyDisjoint(MachineInstr *MIa,
1359       MachineInstr *MIb, AliasAnalysis *AA) const {
1360   int OffsetA = 0, OffsetB = 0;
1361   unsigned SizeA = 0, SizeB = 0;
1362
1363   if (MIa->hasUnmodeledSideEffects() || MIb->hasUnmodeledSideEffects() ||
1364       MIa->hasOrderedMemoryRef() || MIa->hasOrderedMemoryRef())
1365     return false;
1366
1367   // Instructions that are pure loads, not loads and stores like memops are not
1368   // dependent.
1369   if (MIa->mayLoad() && !isMemOp(MIa) && MIb->mayLoad() && !isMemOp(MIb))
1370     return true;
1371
1372   // Get base, offset, and access size in MIa.
1373   unsigned BaseRegA = getBaseAndOffset(MIa, OffsetA, SizeA);
1374   if (!BaseRegA || !SizeA)
1375     return false;
1376
1377   // Get base, offset, and access size in MIb.
1378   unsigned BaseRegB = getBaseAndOffset(MIb, OffsetB, SizeB);
1379   if (!BaseRegB || !SizeB)
1380     return false;
1381
1382   if (BaseRegA != BaseRegB)
1383     return false;
1384
1385   // This is a mem access with the same base register and known offsets from it.
1386   // Reason about it.
1387   if (OffsetA > OffsetB) {
1388     uint64_t offDiff = (uint64_t)((int64_t)OffsetA - (int64_t)OffsetB);
1389     return (SizeB <= offDiff);
1390   } else if (OffsetA < OffsetB) {
1391     uint64_t offDiff = (uint64_t)((int64_t)OffsetB - (int64_t)OffsetA);
1392     return (SizeA <= offDiff);
1393   }
1394
1395   return false;
1396 }
1397
1398
1399 unsigned HexagonInstrInfo::createVR(MachineFunction* MF, MVT VT) const {
1400   MachineRegisterInfo &MRI = MF->getRegInfo();
1401   const TargetRegisterClass *TRC;
1402   if (VT == MVT::i1) {
1403     TRC = &Hexagon::PredRegsRegClass;
1404   } else if (VT == MVT::i32 || VT == MVT::f32) {
1405     TRC = &Hexagon::IntRegsRegClass;
1406   } else if (VT == MVT::i64 || VT == MVT::f64) {
1407     TRC = &Hexagon::DoubleRegsRegClass;
1408   } else {
1409     llvm_unreachable("Cannot handle this register class");
1410   }
1411
1412   unsigned NewReg = MRI.createVirtualRegister(TRC);
1413   return NewReg;
1414 }
1415
1416
1417 bool HexagonInstrInfo::isAbsoluteSet(const MachineInstr* MI) const {
1418   return (getAddrMode(MI) == HexagonII::AbsoluteSet);
1419 }
1420
1421
1422 bool HexagonInstrInfo::isAccumulator(const MachineInstr *MI) const {
1423   const uint64_t F = MI->getDesc().TSFlags;
1424   return((F >> HexagonII::AccumulatorPos) & HexagonII::AccumulatorMask);
1425 }
1426
1427
1428 bool HexagonInstrInfo::isComplex(const MachineInstr *MI) const {
1429   const MachineFunction *MF = MI->getParent()->getParent();
1430   const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
1431   const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
1432
1433   if (!(isTC1(MI))
1434       && !(QII->isTC2Early(MI))
1435       && !(MI->getDesc().mayLoad())
1436       && !(MI->getDesc().mayStore())
1437       && (MI->getDesc().getOpcode() != Hexagon::S2_allocframe)
1438       && (MI->getDesc().getOpcode() != Hexagon::L2_deallocframe)
1439       && !(QII->isMemOp(MI))
1440       && !(MI->isBranch())
1441       && !(MI->isReturn())
1442       && !MI->isCall())
1443     return true;
1444
1445   return false;
1446 }
1447
1448
1449 // Return true if the the instruction is a compund branch instruction.
1450 bool HexagonInstrInfo::isCompoundBranchInstr(const MachineInstr *MI) const {
1451   return (getType(MI) == HexagonII::TypeCOMPOUND && MI->isBranch());
1452 }
1453
1454
1455 bool HexagonInstrInfo::isCondInst(const MachineInstr *MI) const {
1456   return (MI->isBranch() && isPredicated(MI)) ||
1457          isConditionalTransfer(MI) ||
1458          isConditionalALU32(MI)    ||
1459          isConditionalLoad(MI)     ||
1460          // Predicated stores which don't have a .new on any operands.
1461          (MI->mayStore() && isPredicated(MI) && !isNewValueStore(MI) &&
1462           !isPredicatedNew(MI));
1463 }
1464
1465
1466 bool HexagonInstrInfo::isConditionalALU32(const MachineInstr* MI) const {
1467   switch (MI->getOpcode()) {
1468     case Hexagon::A2_paddf:
1469     case Hexagon::A2_paddfnew:
1470     case Hexagon::A2_paddif:
1471     case Hexagon::A2_paddifnew:
1472     case Hexagon::A2_paddit:
1473     case Hexagon::A2_padditnew:
1474     case Hexagon::A2_paddt:
1475     case Hexagon::A2_paddtnew:
1476     case Hexagon::A2_pandf:
1477     case Hexagon::A2_pandfnew:
1478     case Hexagon::A2_pandt:
1479     case Hexagon::A2_pandtnew:
1480     case Hexagon::A2_porf:
1481     case Hexagon::A2_porfnew:
1482     case Hexagon::A2_port:
1483     case Hexagon::A2_portnew:
1484     case Hexagon::A2_psubf:
1485     case Hexagon::A2_psubfnew:
1486     case Hexagon::A2_psubt:
1487     case Hexagon::A2_psubtnew:
1488     case Hexagon::A2_pxorf:
1489     case Hexagon::A2_pxorfnew:
1490     case Hexagon::A2_pxort:
1491     case Hexagon::A2_pxortnew:
1492     case Hexagon::A4_paslhf:
1493     case Hexagon::A4_paslhfnew:
1494     case Hexagon::A4_paslht:
1495     case Hexagon::A4_paslhtnew:
1496     case Hexagon::A4_pasrhf:
1497     case Hexagon::A4_pasrhfnew:
1498     case Hexagon::A4_pasrht:
1499     case Hexagon::A4_pasrhtnew:
1500     case Hexagon::A4_psxtbf:
1501     case Hexagon::A4_psxtbfnew:
1502     case Hexagon::A4_psxtbt:
1503     case Hexagon::A4_psxtbtnew:
1504     case Hexagon::A4_psxthf:
1505     case Hexagon::A4_psxthfnew:
1506     case Hexagon::A4_psxtht:
1507     case Hexagon::A4_psxthtnew:
1508     case Hexagon::A4_pzxtbf:
1509     case Hexagon::A4_pzxtbfnew:
1510     case Hexagon::A4_pzxtbt:
1511     case Hexagon::A4_pzxtbtnew:
1512     case Hexagon::A4_pzxthf:
1513     case Hexagon::A4_pzxthfnew:
1514     case Hexagon::A4_pzxtht:
1515     case Hexagon::A4_pzxthtnew:
1516     case Hexagon::C2_ccombinewf:
1517     case Hexagon::C2_ccombinewt:
1518       return true;
1519   }
1520   return false;
1521 }
1522
1523
1524 // FIXME - Function name and it's functionality don't match.
1525 // It should be renamed to hasPredNewOpcode()
1526 bool HexagonInstrInfo::isConditionalLoad(const MachineInstr* MI) const {
1527   if (!MI->getDesc().mayLoad() || !isPredicated(MI))
1528     return false;
1529
1530   int PNewOpcode = Hexagon::getPredNewOpcode(MI->getOpcode());
1531   // Instruction with valid predicated-new opcode can be promoted to .new.
1532   return PNewOpcode >= 0;
1533 }
1534
1535
1536 // Returns true if an instruction is a conditional store.
1537 //
1538 // Note: It doesn't include conditional new-value stores as they can't be
1539 // converted to .new predicate.
1540 bool HexagonInstrInfo::isConditionalStore(const MachineInstr* MI) const {
1541   switch (MI->getOpcode()) {
1542     default: return false;
1543     case Hexagon::S4_storeirbt_io:
1544     case Hexagon::S4_storeirbf_io:
1545     case Hexagon::S4_pstorerbt_rr:
1546     case Hexagon::S4_pstorerbf_rr:
1547     case Hexagon::S2_pstorerbt_io:
1548     case Hexagon::S2_pstorerbf_io:
1549     case Hexagon::S2_pstorerbt_pi:
1550     case Hexagon::S2_pstorerbf_pi:
1551     case Hexagon::S2_pstorerdt_io:
1552     case Hexagon::S2_pstorerdf_io:
1553     case Hexagon::S4_pstorerdt_rr:
1554     case Hexagon::S4_pstorerdf_rr:
1555     case Hexagon::S2_pstorerdt_pi:
1556     case Hexagon::S2_pstorerdf_pi:
1557     case Hexagon::S2_pstorerht_io:
1558     case Hexagon::S2_pstorerhf_io:
1559     case Hexagon::S4_storeirht_io:
1560     case Hexagon::S4_storeirhf_io:
1561     case Hexagon::S4_pstorerht_rr:
1562     case Hexagon::S4_pstorerhf_rr:
1563     case Hexagon::S2_pstorerht_pi:
1564     case Hexagon::S2_pstorerhf_pi:
1565     case Hexagon::S2_pstorerit_io:
1566     case Hexagon::S2_pstorerif_io:
1567     case Hexagon::S4_storeirit_io:
1568     case Hexagon::S4_storeirif_io:
1569     case Hexagon::S4_pstorerit_rr:
1570     case Hexagon::S4_pstorerif_rr:
1571     case Hexagon::S2_pstorerit_pi:
1572     case Hexagon::S2_pstorerif_pi:
1573
1574     // V4 global address store before promoting to dot new.
1575     case Hexagon::S4_pstorerdt_abs:
1576     case Hexagon::S4_pstorerdf_abs:
1577     case Hexagon::S4_pstorerbt_abs:
1578     case Hexagon::S4_pstorerbf_abs:
1579     case Hexagon::S4_pstorerht_abs:
1580     case Hexagon::S4_pstorerhf_abs:
1581     case Hexagon::S4_pstorerit_abs:
1582     case Hexagon::S4_pstorerif_abs:
1583       return true;
1584
1585     // Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
1586     // from the "Conditional Store" list. Because a predicated new value store
1587     // would NOT be promoted to a double dot new store.
1588     // This function returns yes for those stores that are predicated but not
1589     // yet promoted to predicate dot new instructions.
1590   }
1591 }
1592
1593
1594 bool HexagonInstrInfo::isConditionalTransfer(const MachineInstr *MI) const {
1595   switch (MI->getOpcode()) {
1596     case Hexagon::A2_tfrt:
1597     case Hexagon::A2_tfrf:
1598     case Hexagon::C2_cmoveit:
1599     case Hexagon::C2_cmoveif:
1600     case Hexagon::A2_tfrtnew:
1601     case Hexagon::A2_tfrfnew:
1602     case Hexagon::C2_cmovenewit:
1603     case Hexagon::C2_cmovenewif:
1604     case Hexagon::A2_tfrpt:
1605     case Hexagon::A2_tfrpf:
1606       return true;
1607
1608     default:
1609       return false;
1610   }
1611   return false;
1612 }
1613
1614
1615 // TODO: In order to have isExtendable for fpimm/f32Ext, we need to handle
1616 // isFPImm and later getFPImm as well.
1617 bool HexagonInstrInfo::isConstExtended(const MachineInstr *MI) const {
1618   const uint64_t F = MI->getDesc().TSFlags;
1619   unsigned isExtended = (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
1620   if (isExtended) // Instruction must be extended.
1621     return true;
1622
1623   unsigned isExtendable =
1624     (F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask;
1625   if (!isExtendable)
1626     return false;
1627
1628   if (MI->isCall())
1629     return false;
1630
1631   short ExtOpNum = getCExtOpNum(MI);
1632   const MachineOperand &MO = MI->getOperand(ExtOpNum);
1633   // Use MO operand flags to determine if MO
1634   // has the HMOTF_ConstExtended flag set.
1635   if (MO.getTargetFlags() && HexagonII::HMOTF_ConstExtended)
1636     return true;
1637   // If this is a Machine BB address we are talking about, and it is
1638   // not marked as extended, say so.
1639   if (MO.isMBB())
1640     return false;
1641
1642   // We could be using an instruction with an extendable immediate and shoehorn
1643   // a global address into it. If it is a global address it will be constant
1644   // extended. We do this for COMBINE.
1645   // We currently only handle isGlobal() because it is the only kind of
1646   // object we are going to end up with here for now.
1647   // In the future we probably should add isSymbol(), etc.
1648   if (MO.isGlobal() || MO.isSymbol() || MO.isBlockAddress() ||
1649       MO.isJTI() || MO.isCPI())
1650     return true;
1651
1652   // If the extendable operand is not 'Immediate' type, the instruction should
1653   // have 'isExtended' flag set.
1654   assert(MO.isImm() && "Extendable operand must be Immediate type");
1655
1656   int MinValue = getMinValue(MI);
1657   int MaxValue = getMaxValue(MI);
1658   int ImmValue = MO.getImm();
1659
1660   return (ImmValue < MinValue || ImmValue > MaxValue);
1661 }
1662
1663
1664 bool HexagonInstrInfo::isDeallocRet(const MachineInstr *MI) const {
1665   switch (MI->getOpcode()) {
1666   case Hexagon::L4_return :
1667   case Hexagon::L4_return_t :
1668   case Hexagon::L4_return_f :
1669   case Hexagon::L4_return_tnew_pnt :
1670   case Hexagon::L4_return_fnew_pnt :
1671   case Hexagon::L4_return_tnew_pt :
1672   case Hexagon::L4_return_fnew_pt :
1673    return true;
1674   }
1675   return false;
1676 }
1677
1678
1679 // Return true when ConsMI uses a register defined by ProdMI.
1680 bool HexagonInstrInfo::isDependent(const MachineInstr *ProdMI,
1681       const MachineInstr *ConsMI) const {
1682   const MCInstrDesc &ProdMCID = ProdMI->getDesc();
1683   if (!ProdMCID.getNumDefs())
1684     return false;
1685
1686   auto &HRI = getRegisterInfo();
1687
1688   SmallVector<unsigned, 4> DefsA;
1689   SmallVector<unsigned, 4> DefsB;
1690   SmallVector<unsigned, 8> UsesA;
1691   SmallVector<unsigned, 8> UsesB;
1692
1693   parseOperands(ProdMI, DefsA, UsesA);
1694   parseOperands(ConsMI, DefsB, UsesB);
1695
1696   for (auto &RegA : DefsA)
1697     for (auto &RegB : UsesB) {
1698       // True data dependency.
1699       if (RegA == RegB)
1700         return true;
1701
1702       if (Hexagon::DoubleRegsRegClass.contains(RegA))
1703         for (MCSubRegIterator SubRegs(RegA, &HRI); SubRegs.isValid(); ++SubRegs)
1704           if (RegB == *SubRegs)
1705             return true;
1706
1707       if (Hexagon::DoubleRegsRegClass.contains(RegB))
1708         for (MCSubRegIterator SubRegs(RegB, &HRI); SubRegs.isValid(); ++SubRegs)
1709           if (RegA == *SubRegs)
1710             return true;
1711     }
1712
1713   return false;
1714 }
1715
1716
1717 // Returns true if the instruction is alread a .cur.
1718 bool HexagonInstrInfo::isDotCurInst(const MachineInstr* MI) const {
1719   switch (MI->getOpcode()) {
1720   case Hexagon::V6_vL32b_cur_pi:
1721   case Hexagon::V6_vL32b_cur_ai:
1722   case Hexagon::V6_vL32b_cur_pi_128B:
1723   case Hexagon::V6_vL32b_cur_ai_128B:
1724     return true;
1725   }
1726   return false;
1727 }
1728
1729
1730 // Returns true, if any one of the operands is a dot new
1731 // insn, whether it is predicated dot new or register dot new.
1732 bool HexagonInstrInfo::isDotNewInst(const MachineInstr* MI) const {
1733   if (isNewValueInst(MI) ||
1734      (isPredicated(MI) && isPredicatedNew(MI)))
1735     return true;
1736
1737   return false;
1738 }
1739
1740
1741 /// Symmetrical. See if these two instructions are fit for duplex pair.
1742 bool HexagonInstrInfo::isDuplexPair(const MachineInstr *MIa,
1743       const MachineInstr *MIb) const {
1744   HexagonII::SubInstructionGroup MIaG = getDuplexCandidateGroup(MIa);
1745   HexagonII::SubInstructionGroup MIbG = getDuplexCandidateGroup(MIb);
1746   return (isDuplexPairMatch(MIaG, MIbG) || isDuplexPairMatch(MIbG, MIaG));
1747 }
1748
1749
1750 bool HexagonInstrInfo::isEarlySourceInstr(MachineInstr *MI) const {
1751   if (!MI)
1752     return false;
1753
1754   if (MI->mayLoad() || MI->mayStore() || MI->isCompare())
1755     return true;
1756
1757   // Multiply
1758   unsigned SchedClass = MI->getDesc().getSchedClass();
1759   if (SchedClass == Hexagon::Sched::M_tc_3or4x_SLOT23)
1760     return true;
1761   return false;
1762 }
1763
1764
1765 bool HexagonInstrInfo::isEndLoopN(unsigned Opcode) const {
1766   return (Opcode == Hexagon::ENDLOOP0 ||
1767           Opcode == Hexagon::ENDLOOP1);
1768 }
1769
1770
1771 bool HexagonInstrInfo::isExpr(unsigned OpType) const {
1772   switch(OpType) {
1773   case MachineOperand::MO_MachineBasicBlock:
1774   case MachineOperand::MO_GlobalAddress:
1775   case MachineOperand::MO_ExternalSymbol:
1776   case MachineOperand::MO_JumpTableIndex:
1777   case MachineOperand::MO_ConstantPoolIndex:
1778   case MachineOperand::MO_BlockAddress:
1779     return true;
1780   default:
1781     return false;
1782   }
1783 }
1784
1785
1786 bool HexagonInstrInfo::isExtendable(const MachineInstr *MI) const {
1787   const MCInstrDesc &MID = MI->getDesc();
1788   const uint64_t F = MID.TSFlags;
1789   if ((F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask)
1790     return true;
1791
1792   // TODO: This is largely obsolete now. Will need to be removed
1793   // in consecutive patches.
1794   switch(MI->getOpcode()) {
1795     // TFR_FI Remains a special case.
1796     case Hexagon::TFR_FI:
1797       return true;
1798     default:
1799       return false;
1800   }
1801   return  false;
1802 }
1803
1804
1805 // This returns true in two cases:
1806 // - The OP code itself indicates that this is an extended instruction.
1807 // - One of MOs has been marked with HMOTF_ConstExtended flag.
1808 bool HexagonInstrInfo::isExtended(const MachineInstr *MI) const {
1809   // First check if this is permanently extended op code.
1810   const uint64_t F = MI->getDesc().TSFlags;
1811   if ((F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask)
1812     return true;
1813   // Use MO operand flags to determine if one of MI's operands
1814   // has HMOTF_ConstExtended flag set.
1815   for (MachineInstr::const_mop_iterator I = MI->operands_begin(),
1816        E = MI->operands_end(); I != E; ++I) {
1817     if (I->getTargetFlags() && HexagonII::HMOTF_ConstExtended)
1818       return true;
1819   }
1820   return  false;
1821 }
1822
1823
1824 bool HexagonInstrInfo::isFloat(MachineInstr *MI) const {
1825   unsigned Opcode = MI->getOpcode();
1826   const uint64_t F = get(Opcode).TSFlags;
1827   return (F >> HexagonII::FPPos) & HexagonII::FPMask;
1828 }
1829
1830
1831 bool HexagonInstrInfo::isIndirectCall(const MachineInstr *MI) const {
1832   switch (MI->getOpcode()) {
1833   case Hexagon::J2_callr :
1834   case Hexagon::J2_callrf :
1835   case Hexagon::J2_callrt :
1836     return true;
1837   }
1838   return false;
1839 }
1840
1841
1842 bool HexagonInstrInfo::isIndirectL4Return(const MachineInstr *MI) const {
1843   switch (MI->getOpcode()) {
1844   case Hexagon::L4_return :
1845   case Hexagon::L4_return_t :
1846   case Hexagon::L4_return_f :
1847   case Hexagon::L4_return_fnew_pnt :
1848   case Hexagon::L4_return_fnew_pt :
1849   case Hexagon::L4_return_tnew_pnt :
1850   case Hexagon::L4_return_tnew_pt :
1851     return true;
1852   }
1853   return false;
1854 }
1855
1856
1857 bool HexagonInstrInfo::isJumpR(const MachineInstr *MI) const {
1858   switch (MI->getOpcode()) {
1859   case Hexagon::J2_jumpr :
1860   case Hexagon::J2_jumprt :
1861   case Hexagon::J2_jumprf :
1862   case Hexagon::J2_jumprtnewpt :
1863   case Hexagon::J2_jumprfnewpt  :
1864   case Hexagon::J2_jumprtnew :
1865   case Hexagon::J2_jumprfnew :
1866     return true;
1867   }
1868   return false;
1869 }
1870
1871
1872 // Return true if a given MI can accomodate given offset.
1873 // Use abs estimate as oppose to the exact number.
1874 // TODO: This will need to be changed to use MC level
1875 // definition of instruction extendable field size.
1876 bool HexagonInstrInfo::isJumpWithinBranchRange(const MachineInstr *MI,
1877       unsigned offset) const {
1878   // This selection of jump instructions matches to that what
1879   // AnalyzeBranch can parse, plus NVJ.
1880   if (isNewValueJump(MI)) // r9:2
1881     return isInt<11>(offset);
1882
1883   switch (MI->getOpcode()) {
1884   // Still missing Jump to address condition on register value.
1885   default:
1886     return false;
1887   case Hexagon::J2_jump: // bits<24> dst; // r22:2
1888   case Hexagon::J2_call:
1889   case Hexagon::CALLv3nr:
1890     return isInt<24>(offset);
1891   case Hexagon::J2_jumpt: //bits<17> dst; // r15:2
1892   case Hexagon::J2_jumpf:
1893   case Hexagon::J2_jumptnew:
1894   case Hexagon::J2_jumptnewpt:
1895   case Hexagon::J2_jumpfnew:
1896   case Hexagon::J2_jumpfnewpt:
1897   case Hexagon::J2_callt:
1898   case Hexagon::J2_callf:
1899     return isInt<17>(offset);
1900   case Hexagon::J2_loop0i:
1901   case Hexagon::J2_loop0iext:
1902   case Hexagon::J2_loop0r:
1903   case Hexagon::J2_loop0rext:
1904   case Hexagon::J2_loop1i:
1905   case Hexagon::J2_loop1iext:
1906   case Hexagon::J2_loop1r:
1907   case Hexagon::J2_loop1rext:
1908     return isInt<9>(offset);
1909   // TODO: Add all the compound branches here. Can we do this in Relation model?
1910   case Hexagon::J4_cmpeqi_tp0_jump_nt:
1911   case Hexagon::J4_cmpeqi_tp1_jump_nt:
1912     return isInt<11>(offset);
1913   }
1914 }
1915
1916
1917 bool HexagonInstrInfo::isLateInstrFeedsEarlyInstr(MachineInstr *LRMI,
1918       MachineInstr *ESMI) const {
1919   if (!LRMI || !ESMI)
1920     return false;
1921
1922   bool isLate = isLateResultInstr(LRMI);
1923   bool isEarly = isEarlySourceInstr(ESMI);
1924
1925   DEBUG(dbgs() << "V60" <<  (isLate ? "-LR  " : " --  "));
1926   DEBUG(LRMI->dump());
1927   DEBUG(dbgs() << "V60" <<  (isEarly ? "-ES  " : " --  "));
1928   DEBUG(ESMI->dump());
1929
1930   if (isLate && isEarly) {
1931     DEBUG(dbgs() << "++Is Late Result feeding Early Source\n");
1932     return true;
1933   }
1934
1935   return false;
1936 }
1937
1938
1939 bool HexagonInstrInfo::isLateResultInstr(MachineInstr *MI) const {
1940   if (!MI)
1941     return false;
1942
1943   switch (MI->getOpcode()) {
1944   case TargetOpcode::EXTRACT_SUBREG:
1945   case TargetOpcode::INSERT_SUBREG:
1946   case TargetOpcode::SUBREG_TO_REG:
1947   case TargetOpcode::REG_SEQUENCE:
1948   case TargetOpcode::IMPLICIT_DEF:
1949   case TargetOpcode::COPY:
1950   case TargetOpcode::INLINEASM:
1951   case TargetOpcode::PHI:
1952     return false;
1953   default:
1954     break;
1955   }
1956
1957   unsigned SchedClass = MI->getDesc().getSchedClass();
1958
1959   switch (SchedClass) {
1960   case Hexagon::Sched::ALU32_2op_tc_1_SLOT0123:
1961   case Hexagon::Sched::ALU32_3op_tc_1_SLOT0123:
1962   case Hexagon::Sched::ALU32_ADDI_tc_1_SLOT0123:
1963   case Hexagon::Sched::ALU64_tc_1_SLOT23:
1964   case Hexagon::Sched::EXTENDER_tc_1_SLOT0123:
1965   case Hexagon::Sched::S_2op_tc_1_SLOT23:
1966   case Hexagon::Sched::S_3op_tc_1_SLOT23:
1967   case Hexagon::Sched::V2LDST_tc_ld_SLOT01:
1968   case Hexagon::Sched::V2LDST_tc_st_SLOT0:
1969   case Hexagon::Sched::V2LDST_tc_st_SLOT01:
1970   case Hexagon::Sched::V4LDST_tc_ld_SLOT01:
1971   case Hexagon::Sched::V4LDST_tc_st_SLOT0:
1972   case Hexagon::Sched::V4LDST_tc_st_SLOT01:
1973     return false;
1974   }
1975   return true;
1976 }
1977
1978
1979 bool HexagonInstrInfo::isLateSourceInstr(const MachineInstr *MI) const {
1980   if (!MI)
1981     return false;
1982
1983   // Instructions with iclass A_CVI_VX and attribute A_CVI_LATE uses a multiply
1984   // resource, but all operands can be received late like an ALU instruction.
1985   return MI->getDesc().getSchedClass() == Hexagon::Sched::CVI_VX_LATE;
1986 }
1987
1988
1989 bool HexagonInstrInfo::isLoopN(unsigned Opcode) const {
1990   return (Opcode == Hexagon::J2_loop0i ||
1991           Opcode == Hexagon::J2_loop0r ||
1992           Opcode == Hexagon::J2_loop0iext ||
1993           Opcode == Hexagon::J2_loop0rext ||
1994           Opcode == Hexagon::J2_loop1i ||
1995           Opcode == Hexagon::J2_loop1r ||
1996           Opcode == Hexagon::J2_loop1iext ||
1997           Opcode == Hexagon::J2_loop1rext);
1998 }
1999
2000
2001 bool HexagonInstrInfo::isMemOp(const MachineInstr *MI) const {
2002   switch (MI->getOpcode()) {
2003     default: return false;
2004     case Hexagon::L4_iadd_memopw_io :
2005     case Hexagon::L4_isub_memopw_io :
2006     case Hexagon::L4_add_memopw_io :
2007     case Hexagon::L4_sub_memopw_io :
2008     case Hexagon::L4_and_memopw_io :
2009     case Hexagon::L4_or_memopw_io :
2010     case Hexagon::L4_iadd_memoph_io :
2011     case Hexagon::L4_isub_memoph_io :
2012     case Hexagon::L4_add_memoph_io :
2013     case Hexagon::L4_sub_memoph_io :
2014     case Hexagon::L4_and_memoph_io :
2015     case Hexagon::L4_or_memoph_io :
2016     case Hexagon::L4_iadd_memopb_io :
2017     case Hexagon::L4_isub_memopb_io :
2018     case Hexagon::L4_add_memopb_io :
2019     case Hexagon::L4_sub_memopb_io :
2020     case Hexagon::L4_and_memopb_io :
2021     case Hexagon::L4_or_memopb_io :
2022     case Hexagon::L4_ior_memopb_io:
2023     case Hexagon::L4_ior_memoph_io:
2024     case Hexagon::L4_ior_memopw_io:
2025     case Hexagon::L4_iand_memopb_io:
2026     case Hexagon::L4_iand_memoph_io:
2027     case Hexagon::L4_iand_memopw_io:
2028     return true;
2029   }
2030   return false;
2031 }
2032
2033
2034 bool HexagonInstrInfo::isNewValue(const MachineInstr* MI) const {
2035   const uint64_t F = MI->getDesc().TSFlags;
2036   return (F >> HexagonII::NewValuePos) & HexagonII::NewValueMask;
2037 }
2038
2039
2040 bool HexagonInstrInfo::isNewValue(unsigned Opcode) const {
2041   const uint64_t F = get(Opcode).TSFlags;
2042   return (F >> HexagonII::NewValuePos) & HexagonII::NewValueMask;
2043 }
2044
2045
2046 bool HexagonInstrInfo::isNewValueInst(const MachineInstr *MI) const {
2047   return isNewValueJump(MI) || isNewValueStore(MI);
2048 }
2049
2050
2051 bool HexagonInstrInfo::isNewValueJump(const MachineInstr *MI) const {
2052   return isNewValue(MI) && MI->isBranch();
2053 }
2054
2055
2056 bool HexagonInstrInfo::isNewValueJump(unsigned Opcode) const {
2057   return isNewValue(Opcode) && get(Opcode).isBranch() && isPredicated(Opcode);
2058 }
2059
2060
2061 bool HexagonInstrInfo::isNewValueStore(const MachineInstr *MI) const {
2062   const uint64_t F = MI->getDesc().TSFlags;
2063   return (F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask;
2064 }
2065
2066
2067 bool HexagonInstrInfo::isNewValueStore(unsigned Opcode) const {
2068   const uint64_t F = get(Opcode).TSFlags;
2069   return (F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask;
2070 }
2071
2072
2073 // Returns true if a particular operand is extendable for an instruction.
2074 bool HexagonInstrInfo::isOperandExtended(const MachineInstr *MI,
2075     unsigned OperandNum) const {
2076   const uint64_t F = MI->getDesc().TSFlags;
2077   return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask)
2078           == OperandNum;
2079 }
2080
2081
2082 bool HexagonInstrInfo::isPostIncrement(const MachineInstr* MI) const {
2083   return getAddrMode(MI) == HexagonII::PostInc;
2084 }
2085
2086
2087 bool HexagonInstrInfo::isPredicatedNew(const MachineInstr *MI) const {
2088   const uint64_t F = MI->getDesc().TSFlags;
2089   assert(isPredicated(MI));
2090   return (F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask;
2091 }
2092
2093
2094 bool HexagonInstrInfo::isPredicatedNew(unsigned Opcode) const {
2095   const uint64_t F = get(Opcode).TSFlags;
2096   assert(isPredicated(Opcode));
2097   return (F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask;
2098 }
2099
2100
2101 bool HexagonInstrInfo::isPredicatedTrue(const MachineInstr *MI) const {
2102   const uint64_t F = MI->getDesc().TSFlags;
2103   return !((F >> HexagonII::PredicatedFalsePos) &
2104            HexagonII::PredicatedFalseMask);
2105 }
2106
2107
2108 bool HexagonInstrInfo::isPredicatedTrue(unsigned Opcode) const {
2109   const uint64_t F = get(Opcode).TSFlags;
2110   // Make sure that the instruction is predicated.
2111   assert((F>> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
2112   return !((F >> HexagonII::PredicatedFalsePos) &
2113            HexagonII::PredicatedFalseMask);
2114 }
2115
2116
2117 bool HexagonInstrInfo::isPredicated(unsigned Opcode) const {
2118   const uint64_t F = get(Opcode).TSFlags;
2119   return (F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask;
2120 }
2121
2122
2123 bool HexagonInstrInfo::isPredicateLate(unsigned Opcode) const {
2124   const uint64_t F = get(Opcode).TSFlags;
2125   return ~(F >> HexagonII::PredicateLatePos) & HexagonII::PredicateLateMask;
2126 }
2127
2128
2129 bool HexagonInstrInfo::isPredictedTaken(unsigned Opcode) const {
2130   const uint64_t F = get(Opcode).TSFlags;
2131   assert(get(Opcode).isBranch() &&
2132          (isPredicatedNew(Opcode) || isNewValue(Opcode)));
2133   return (F >> HexagonII::TakenPos) & HexagonII::TakenMask;
2134 }
2135
2136
2137 bool HexagonInstrInfo::isSaveCalleeSavedRegsCall(const MachineInstr *MI) const {
2138   return MI->getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4 ||
2139          MI->getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_EXT;
2140 }
2141
2142
2143 bool HexagonInstrInfo::isSolo(const MachineInstr* MI) const {
2144   const uint64_t F = MI->getDesc().TSFlags;
2145   return (F >> HexagonII::SoloPos) & HexagonII::SoloMask;
2146 }
2147
2148
2149 bool HexagonInstrInfo::isSpillPredRegOp(const MachineInstr *MI) const {
2150   switch (MI->getOpcode()) {
2151   case Hexagon::STriw_pred :
2152   case Hexagon::LDriw_pred :
2153     return true;
2154   default:
2155     return false;
2156   }
2157 }
2158
2159
2160 // Returns true when SU has a timing class TC1.
2161 bool HexagonInstrInfo::isTC1(const MachineInstr *MI) const {
2162   unsigned SchedClass = MI->getDesc().getSchedClass();
2163   switch (SchedClass) {
2164   case Hexagon::Sched::ALU32_2op_tc_1_SLOT0123:
2165   case Hexagon::Sched::ALU32_3op_tc_1_SLOT0123:
2166   case Hexagon::Sched::ALU32_ADDI_tc_1_SLOT0123:
2167   case Hexagon::Sched::ALU64_tc_1_SLOT23:
2168   case Hexagon::Sched::EXTENDER_tc_1_SLOT0123:
2169   //case Hexagon::Sched::M_tc_1_SLOT23:
2170   case Hexagon::Sched::S_2op_tc_1_SLOT23:
2171   case Hexagon::Sched::S_3op_tc_1_SLOT23:
2172     return true;
2173
2174   default:
2175     return false;
2176   }
2177 }
2178
2179
2180 bool HexagonInstrInfo::isTC2(const MachineInstr *MI) const {
2181   unsigned SchedClass = MI->getDesc().getSchedClass();
2182   switch (SchedClass) {
2183   case Hexagon::Sched::ALU32_3op_tc_2_SLOT0123:
2184   case Hexagon::Sched::ALU64_tc_2_SLOT23:
2185   case Hexagon::Sched::CR_tc_2_SLOT3:
2186   case Hexagon::Sched::M_tc_2_SLOT23:
2187   case Hexagon::Sched::S_2op_tc_2_SLOT23:
2188   case Hexagon::Sched::S_3op_tc_2_SLOT23:
2189     return true;
2190
2191   default:
2192     return false;
2193   }
2194 }
2195
2196
2197 bool HexagonInstrInfo::isTC2Early(const MachineInstr *MI) const {
2198   unsigned SchedClass = MI->getDesc().getSchedClass();
2199   switch (SchedClass) {
2200   case Hexagon::Sched::ALU32_2op_tc_2early_SLOT0123:
2201   case Hexagon::Sched::ALU32_3op_tc_2early_SLOT0123:
2202   case Hexagon::Sched::ALU64_tc_2early_SLOT23:
2203   case Hexagon::Sched::CR_tc_2early_SLOT23:
2204   case Hexagon::Sched::CR_tc_2early_SLOT3:
2205   case Hexagon::Sched::J_tc_2early_SLOT0123:
2206   case Hexagon::Sched::J_tc_2early_SLOT2:
2207   case Hexagon::Sched::J_tc_2early_SLOT23:
2208   case Hexagon::Sched::S_2op_tc_2early_SLOT23:
2209   case Hexagon::Sched::S_3op_tc_2early_SLOT23:
2210     return true;
2211
2212   default:
2213     return false;
2214   }
2215 }
2216
2217
2218 bool HexagonInstrInfo::isTC4x(const MachineInstr *MI) const {
2219   if (!MI)
2220     return false;
2221
2222   unsigned SchedClass = MI->getDesc().getSchedClass();
2223   return SchedClass == Hexagon::Sched::M_tc_3or4x_SLOT23;
2224 }
2225
2226
2227 bool HexagonInstrInfo::isV60VectorInstruction(const MachineInstr *MI) const {
2228   if (!MI)
2229     return false;
2230
2231   const uint64_t V = getType(MI);
2232   return HexagonII::TypeCVI_FIRST <= V && V <= HexagonII::TypeCVI_LAST;
2233 }
2234
2235
2236 // Check if the Offset is a valid auto-inc imm by Load/Store Type.
2237 //
2238 bool HexagonInstrInfo::isValidAutoIncImm(const EVT VT, const int Offset) const {
2239   if (VT == MVT::v16i32 || VT == MVT::v8i64 ||
2240       VT == MVT::v32i16 || VT == MVT::v64i8) {
2241       return (Offset >= Hexagon_MEMV_AUTOINC_MIN &&
2242               Offset <= Hexagon_MEMV_AUTOINC_MAX &&
2243               (Offset & 0x3f) == 0);
2244   }
2245   // 128B
2246   if (VT == MVT::v32i32 || VT == MVT::v16i64 ||
2247       VT == MVT::v64i16 || VT == MVT::v128i8) {
2248       return (Offset >= Hexagon_MEMV_AUTOINC_MIN_128B &&
2249               Offset <= Hexagon_MEMV_AUTOINC_MAX_128B &&
2250               (Offset & 0x7f) == 0);
2251   }
2252   if (VT == MVT::i64) {
2253       return (Offset >= Hexagon_MEMD_AUTOINC_MIN &&
2254               Offset <= Hexagon_MEMD_AUTOINC_MAX &&
2255               (Offset & 0x7) == 0);
2256   }
2257   if (VT == MVT::i32) {
2258       return (Offset >= Hexagon_MEMW_AUTOINC_MIN &&
2259               Offset <= Hexagon_MEMW_AUTOINC_MAX &&
2260               (Offset & 0x3) == 0);
2261   }
2262   if (VT == MVT::i16) {
2263       return (Offset >= Hexagon_MEMH_AUTOINC_MIN &&
2264               Offset <= Hexagon_MEMH_AUTOINC_MAX &&
2265               (Offset & 0x1) == 0);
2266   }
2267   if (VT == MVT::i8) {
2268       return (Offset >= Hexagon_MEMB_AUTOINC_MIN &&
2269               Offset <= Hexagon_MEMB_AUTOINC_MAX);
2270   }
2271   llvm_unreachable("Not an auto-inc opc!");
2272 }
2273
2274
2275 bool HexagonInstrInfo::isValidOffset(unsigned Opcode, int Offset,
2276       bool Extend) const {
2277   // This function is to check whether the "Offset" is in the correct range of
2278   // the given "Opcode". If "Offset" is not in the correct range, "A2_addi" is
2279   // inserted to calculate the final address. Due to this reason, the function
2280   // assumes that the "Offset" has correct alignment.
2281   // We used to assert if the offset was not properly aligned, however,
2282   // there are cases where a misaligned pointer recast can cause this
2283   // problem, and we need to allow for it. The front end warns of such
2284   // misaligns with respect to load size.
2285
2286   switch (Opcode) {
2287   case Hexagon::STriq_pred_V6:
2288   case Hexagon::STriq_pred_vec_V6:
2289   case Hexagon::STriv_pseudo_V6:
2290   case Hexagon::STrivv_pseudo_V6:
2291   case Hexagon::LDriq_pred_V6:
2292   case Hexagon::LDriq_pred_vec_V6:
2293   case Hexagon::LDriv_pseudo_V6:
2294   case Hexagon::LDrivv_pseudo_V6:
2295   case Hexagon::LDrivv_indexed:
2296   case Hexagon::STrivv_indexed:
2297   case Hexagon::V6_vL32b_ai:
2298   case Hexagon::V6_vS32b_ai:
2299   case Hexagon::V6_vL32Ub_ai:
2300   case Hexagon::V6_vS32Ub_ai:
2301     return (Offset >= Hexagon_MEMV_OFFSET_MIN) &&
2302       (Offset <= Hexagon_MEMV_OFFSET_MAX);
2303
2304   case Hexagon::STriq_pred_V6_128B:
2305   case Hexagon::STriq_pred_vec_V6_128B:
2306   case Hexagon::STriv_pseudo_V6_128B:
2307   case Hexagon::STrivv_pseudo_V6_128B:
2308   case Hexagon::LDriq_pred_V6_128B:
2309   case Hexagon::LDriq_pred_vec_V6_128B:
2310   case Hexagon::LDriv_pseudo_V6_128B:
2311   case Hexagon::LDrivv_pseudo_V6_128B:
2312   case Hexagon::LDrivv_indexed_128B:
2313   case Hexagon::STrivv_indexed_128B:
2314   case Hexagon::V6_vL32b_ai_128B:
2315   case Hexagon::V6_vS32b_ai_128B:
2316   case Hexagon::V6_vL32Ub_ai_128B:
2317   case Hexagon::V6_vS32Ub_ai_128B:
2318     return (Offset >= Hexagon_MEMV_OFFSET_MIN_128B) &&
2319       (Offset <= Hexagon_MEMV_OFFSET_MAX_128B);
2320
2321   case Hexagon::J2_loop0i:
2322   case Hexagon::J2_loop1i:
2323     return isUInt<10>(Offset);
2324   }
2325
2326   if (Extend)
2327     return true;
2328
2329   switch (Opcode) {
2330   case Hexagon::L2_loadri_io:
2331   case Hexagon::S2_storeri_io:
2332     return (Offset >= Hexagon_MEMW_OFFSET_MIN) &&
2333       (Offset <= Hexagon_MEMW_OFFSET_MAX);
2334
2335   case Hexagon::L2_loadrd_io:
2336   case Hexagon::S2_storerd_io:
2337     return (Offset >= Hexagon_MEMD_OFFSET_MIN) &&
2338       (Offset <= Hexagon_MEMD_OFFSET_MAX);
2339
2340   case Hexagon::L2_loadrh_io:
2341   case Hexagon::L2_loadruh_io:
2342   case Hexagon::S2_storerh_io:
2343     return (Offset >= Hexagon_MEMH_OFFSET_MIN) &&
2344       (Offset <= Hexagon_MEMH_OFFSET_MAX);
2345
2346   case Hexagon::L2_loadrb_io:
2347   case Hexagon::L2_loadrub_io:
2348   case Hexagon::S2_storerb_io:
2349     return (Offset >= Hexagon_MEMB_OFFSET_MIN) &&
2350       (Offset <= Hexagon_MEMB_OFFSET_MAX);
2351
2352   case Hexagon::A2_addi:
2353     return (Offset >= Hexagon_ADDI_OFFSET_MIN) &&
2354       (Offset <= Hexagon_ADDI_OFFSET_MAX);
2355
2356   case Hexagon::L4_iadd_memopw_io :
2357   case Hexagon::L4_isub_memopw_io :
2358   case Hexagon::L4_add_memopw_io :
2359   case Hexagon::L4_sub_memopw_io :
2360   case Hexagon::L4_and_memopw_io :
2361   case Hexagon::L4_or_memopw_io :
2362     return (0 <= Offset && Offset <= 255);
2363
2364   case Hexagon::L4_iadd_memoph_io :
2365   case Hexagon::L4_isub_memoph_io :
2366   case Hexagon::L4_add_memoph_io :
2367   case Hexagon::L4_sub_memoph_io :
2368   case Hexagon::L4_and_memoph_io :
2369   case Hexagon::L4_or_memoph_io :
2370     return (0 <= Offset && Offset <= 127);
2371
2372   case Hexagon::L4_iadd_memopb_io :
2373   case Hexagon::L4_isub_memopb_io :
2374   case Hexagon::L4_add_memopb_io :
2375   case Hexagon::L4_sub_memopb_io :
2376   case Hexagon::L4_and_memopb_io :
2377   case Hexagon::L4_or_memopb_io :
2378     return (0 <= Offset && Offset <= 63);
2379
2380   // LDri_pred and STriw_pred are pseudo operations, so it has to take offset of
2381   // any size. Later pass knows how to handle it.
2382   case Hexagon::STriw_pred:
2383   case Hexagon::LDriw_pred:
2384     return true;
2385
2386   case Hexagon::TFR_FI:
2387   case Hexagon::TFR_FIA:
2388   case Hexagon::INLINEASM:
2389     return true;
2390
2391   case Hexagon::L2_ploadrbt_io:
2392   case Hexagon::L2_ploadrbf_io:
2393   case Hexagon::L2_ploadrubt_io:
2394   case Hexagon::L2_ploadrubf_io:
2395   case Hexagon::S2_pstorerbt_io:
2396   case Hexagon::S2_pstorerbf_io:
2397   case Hexagon::S4_storeirb_io:
2398   case Hexagon::S4_storeirbt_io:
2399   case Hexagon::S4_storeirbf_io:
2400     return isUInt<6>(Offset);
2401
2402   case Hexagon::L2_ploadrht_io:
2403   case Hexagon::L2_ploadrhf_io:
2404   case Hexagon::L2_ploadruht_io:
2405   case Hexagon::L2_ploadruhf_io:
2406   case Hexagon::S2_pstorerht_io:
2407   case Hexagon::S2_pstorerhf_io:
2408   case Hexagon::S4_storeirh_io:
2409   case Hexagon::S4_storeirht_io:
2410   case Hexagon::S4_storeirhf_io:
2411     return isShiftedUInt<6,1>(Offset);
2412
2413   case Hexagon::L2_ploadrit_io:
2414   case Hexagon::L2_ploadrif_io:
2415   case Hexagon::S2_pstorerit_io:
2416   case Hexagon::S2_pstorerif_io:
2417   case Hexagon::S4_storeiri_io:
2418   case Hexagon::S4_storeirit_io:
2419   case Hexagon::S4_storeirif_io:
2420     return isShiftedUInt<6,2>(Offset);
2421
2422   case Hexagon::L2_ploadrdt_io:
2423   case Hexagon::L2_ploadrdf_io:
2424   case Hexagon::S2_pstorerdt_io:
2425   case Hexagon::S2_pstorerdf_io:
2426     return isShiftedUInt<6,3>(Offset);
2427   } // switch
2428
2429   llvm_unreachable("No offset range is defined for this opcode. "
2430                    "Please define it in the above switch statement!");
2431 }
2432
2433
2434 bool HexagonInstrInfo::isVecAcc(const MachineInstr *MI) const {
2435   return MI && isV60VectorInstruction(MI) && isAccumulator(MI);
2436 }
2437
2438
2439 bool HexagonInstrInfo::isVecALU(const MachineInstr *MI) const {
2440   if (!MI)
2441     return false;
2442   const uint64_t F = get(MI->getOpcode()).TSFlags;
2443   const uint64_t V = ((F >> HexagonII::TypePos) & HexagonII::TypeMask);
2444   return
2445     V == HexagonII::TypeCVI_VA         ||
2446     V == HexagonII::TypeCVI_VA_DV;
2447 }
2448
2449
2450 bool HexagonInstrInfo::isVecUsableNextPacket(const MachineInstr *ProdMI,
2451       const MachineInstr *ConsMI) const {
2452   if (EnableACCForwarding && isVecAcc(ProdMI) && isVecAcc(ConsMI))
2453     return true;
2454
2455   if (EnableALUForwarding && (isVecALU(ConsMI) || isLateSourceInstr(ConsMI)))
2456     return true;
2457
2458   if (mayBeNewStore(ConsMI))
2459     return true;
2460
2461   return false;
2462 }
2463
2464
2465 bool HexagonInstrInfo::hasEHLabel(const MachineBasicBlock *B) const {
2466   for (auto &I : *B)
2467     if (I.isEHLabel())
2468       return true;
2469   return false;
2470 }
2471
2472
2473 // Returns true if an instruction can be converted into a non-extended
2474 // equivalent instruction.
2475 bool HexagonInstrInfo::hasNonExtEquivalent(const MachineInstr *MI) const {
2476   short NonExtOpcode;
2477   // Check if the instruction has a register form that uses register in place
2478   // of the extended operand, if so return that as the non-extended form.
2479   if (Hexagon::getRegForm(MI->getOpcode()) >= 0)
2480     return true;
2481
2482   if (MI->getDesc().mayLoad() || MI->getDesc().mayStore()) {
2483     // Check addressing mode and retrieve non-ext equivalent instruction.
2484
2485     switch (getAddrMode(MI)) {
2486     case HexagonII::Absolute :
2487       // Load/store with absolute addressing mode can be converted into
2488       // base+offset mode.
2489       NonExtOpcode = Hexagon::getBaseWithImmOffset(MI->getOpcode());
2490       break;
2491     case HexagonII::BaseImmOffset :
2492       // Load/store with base+offset addressing mode can be converted into
2493       // base+register offset addressing mode. However left shift operand should
2494       // be set to 0.
2495       NonExtOpcode = Hexagon::getBaseWithRegOffset(MI->getOpcode());
2496       break;
2497     case HexagonII::BaseLongOffset:
2498       NonExtOpcode = Hexagon::getRegShlForm(MI->getOpcode());
2499       break;
2500     default:
2501       return false;
2502     }
2503     if (NonExtOpcode < 0)
2504       return false;
2505     return true;
2506   }
2507   return false;
2508 }
2509
2510
2511 bool HexagonInstrInfo::hasPseudoInstrPair(MachineInstr *MI) const {
2512   return Hexagon::getRealHWInstr(MI->getOpcode(),
2513                                  Hexagon::InstrType_Pseudo) >= 0;
2514 }
2515
2516
2517 bool HexagonInstrInfo::hasUncondBranch(const MachineBasicBlock *B)
2518       const {
2519   MachineBasicBlock::const_iterator I = B->getFirstTerminator(), E = B->end();
2520   while (I != E) {
2521     if (I->isBarrier())
2522       return true;
2523     ++I;
2524   }
2525   return false;
2526 }
2527
2528
2529 // Returns true, if a LD insn can be promoted to a cur load.
2530 bool HexagonInstrInfo::mayBeCurLoad(const MachineInstr *MI) const {
2531   auto &HST = MI->getParent()->getParent()->getSubtarget<HexagonSubtarget>();
2532   const uint64_t F = MI->getDesc().TSFlags;
2533   return ((F >> HexagonII::mayCVLoadPos) & HexagonII::mayCVLoadMask) &&
2534          HST.hasV60TOps();
2535 }
2536
2537
2538 // Returns true, if a ST insn can be promoted to a new-value store.
2539 bool HexagonInstrInfo::mayBeNewStore(const MachineInstr *MI) const {
2540   const uint64_t F = MI->getDesc().TSFlags;
2541   return (F >> HexagonII::mayNVStorePos) & HexagonII::mayNVStoreMask;
2542 }
2543
2544
2545 bool HexagonInstrInfo::producesStall(const MachineInstr *ProdMI,
2546       const MachineInstr *ConsMI) const {
2547   // There is no stall when ProdMI is not a V60 vector.
2548   if (!isV60VectorInstruction(ProdMI))
2549     return false;
2550
2551   // There is no stall when ProdMI and ConsMI are not dependent.
2552   if (!isDependent(ProdMI, ConsMI))
2553     return false;
2554
2555   // When Forward Scheduling is enabled, there is no stall if ProdMI and ConsMI
2556   // are scheduled in consecutive packets.
2557   if (isVecUsableNextPacket(ProdMI, ConsMI))
2558     return false;
2559
2560   return true;
2561 }
2562
2563
2564 bool HexagonInstrInfo::producesStall(const MachineInstr *MI,
2565       MachineBasicBlock::const_instr_iterator BII) const {
2566   // There is no stall when I is not a V60 vector.
2567   if (!isV60VectorInstruction(MI))
2568     return false;
2569
2570   MachineBasicBlock::const_instr_iterator MII = BII;
2571   MachineBasicBlock::const_instr_iterator MIE = MII->getParent()->instr_end();
2572
2573   if (!(*MII).isBundle()) {
2574     const MachineInstr *J = &*MII;
2575     if (!isV60VectorInstruction(J))
2576       return false;
2577     else if (isVecUsableNextPacket(J, MI))
2578       return false;
2579     return true;
2580   }
2581
2582   for (++MII; MII != MIE && MII->isInsideBundle(); ++MII) {
2583     const MachineInstr *J = &*MII;
2584     if (producesStall(J, MI))
2585       return true;
2586   }
2587   return false;
2588 }
2589
2590
2591 bool HexagonInstrInfo::predCanBeUsedAsDotNew(MachineInstr *MI,
2592       unsigned PredReg) const {
2593   for (unsigned opNum = 0; opNum < MI->getNumOperands(); opNum++) {
2594     MachineOperand &MO = MI->getOperand(opNum);
2595     if (MO.isReg() && MO.isDef() && MO.isImplicit() && (MO.getReg() == PredReg))
2596       return false; // Predicate register must be explicitly defined.
2597   }
2598
2599   // Hexagon Programmer's Reference says that decbin, memw_locked, and
2600   // memd_locked cannot be used as .new as well,
2601   // but we don't seem to have these instructions defined.
2602   return MI->getOpcode() != Hexagon::A4_tlbmatch;
2603 }
2604
2605
2606 bool HexagonInstrInfo::PredOpcodeHasJMP_c(unsigned Opcode) const {
2607   return (Opcode == Hexagon::J2_jumpt)      ||
2608          (Opcode == Hexagon::J2_jumpf)      ||
2609          (Opcode == Hexagon::J2_jumptnew)   ||
2610          (Opcode == Hexagon::J2_jumpfnew)   ||
2611          (Opcode == Hexagon::J2_jumptnewpt) ||
2612          (Opcode == Hexagon::J2_jumpfnewpt);
2613 }
2614
2615
2616 bool HexagonInstrInfo::predOpcodeHasNot(ArrayRef<MachineOperand> Cond) const {
2617   if (Cond.empty() || !isPredicated(Cond[0].getImm()))
2618     return false;
2619   return !isPredicatedTrue(Cond[0].getImm());
2620 }
2621
2622
2623 unsigned HexagonInstrInfo::getAddrMode(const MachineInstr* MI) const {
2624   const uint64_t F = MI->getDesc().TSFlags;
2625   return (F >> HexagonII::AddrModePos) & HexagonII::AddrModeMask;
2626 }
2627
2628
2629 // Returns the base register in a memory access (load/store). The offset is
2630 // returned in Offset and the access size is returned in AccessSize.
2631 unsigned HexagonInstrInfo::getBaseAndOffset(const MachineInstr *MI,
2632       int &Offset, unsigned &AccessSize) const {
2633   // Return if it is not a base+offset type instruction or a MemOp.
2634   if (getAddrMode(MI) != HexagonII::BaseImmOffset &&
2635       getAddrMode(MI) != HexagonII::BaseLongOffset &&
2636       !isMemOp(MI) && !isPostIncrement(MI))
2637     return 0;
2638
2639   // Since it is a memory access instruction, getMemAccessSize() should never
2640   // return 0.
2641   assert (getMemAccessSize(MI) &&
2642           "BaseImmOffset or BaseLongOffset or MemOp without accessSize");
2643
2644   // Return Values of getMemAccessSize() are
2645   // 0 - Checked in the assert above.
2646   // 1, 2, 3, 4 & 7, 8 - The statement below is correct for all these.
2647   // MemAccessSize is represented as 1+log2(N) where N is size in bits.
2648   AccessSize = (1U << (getMemAccessSize(MI) - 1));
2649
2650   unsigned basePos = 0, offsetPos = 0;
2651   if (!getBaseAndOffsetPosition(MI, basePos, offsetPos))
2652     return 0;
2653
2654   // Post increment updates its EA after the mem access,
2655   // so we need to treat its offset as zero.
2656   if (isPostIncrement(MI))
2657     Offset = 0;
2658   else {
2659     Offset = MI->getOperand(offsetPos).getImm();
2660   }
2661
2662   return MI->getOperand(basePos).getReg();
2663 }
2664
2665
2666 /// Return the position of the base and offset operands for this instruction.
2667 bool HexagonInstrInfo::getBaseAndOffsetPosition(const MachineInstr *MI,
2668       unsigned &BasePos, unsigned &OffsetPos) const {
2669   // Deal with memops first.
2670   if (isMemOp(MI)) {
2671     assert (MI->getOperand(0).isReg() && MI->getOperand(1).isImm() &&
2672             "Bad Memop.");
2673     BasePos = 0;
2674     OffsetPos = 1;
2675   } else if (MI->mayStore()) {
2676     BasePos = 0;
2677     OffsetPos = 1;
2678   } else if (MI->mayLoad()) {
2679     BasePos = 1;
2680     OffsetPos = 2;
2681   } else
2682     return false;
2683
2684   if (isPredicated(MI)) {
2685     BasePos++;
2686     OffsetPos++;
2687   }
2688   if (isPostIncrement(MI)) {
2689     BasePos++;
2690     OffsetPos++;
2691   }
2692
2693   if (!MI->getOperand(BasePos).isReg() || !MI->getOperand(OffsetPos).isImm())
2694     return false;
2695
2696   return true;
2697 }
2698
2699
2700 // Inserts branching instructions in reverse order of their occurence.
2701 // e.g. jump_t t1 (i1)
2702 // jump t2        (i2)
2703 // Jumpers = {i2, i1}
2704 SmallVector<MachineInstr*, 2> HexagonInstrInfo::getBranchingInstrs(
2705       MachineBasicBlock& MBB) const {
2706   SmallVector<MachineInstr*, 2> Jumpers;
2707   // If the block has no terminators, it just falls into the block after it.
2708   MachineBasicBlock::instr_iterator I = MBB.instr_end();
2709   if (I == MBB.instr_begin())
2710     return Jumpers;
2711
2712   // A basic block may looks like this:
2713   //
2714   //  [   insn
2715   //     EH_LABEL
2716   //      insn
2717   //      insn
2718   //      insn
2719   //     EH_LABEL
2720   //      insn     ]
2721   //
2722   // It has two succs but does not have a terminator
2723   // Don't know how to handle it.
2724   do {
2725     --I;
2726     if (I->isEHLabel())
2727       return Jumpers;
2728   } while (I != MBB.instr_begin());
2729
2730   I = MBB.instr_end();
2731   --I;
2732
2733   while (I->isDebugValue()) {
2734     if (I == MBB.instr_begin())
2735       return Jumpers;
2736     --I;
2737   }
2738   if (!isUnpredicatedTerminator(&*I))
2739     return Jumpers;
2740
2741   // Get the last instruction in the block.
2742   MachineInstr *LastInst = &*I;
2743   Jumpers.push_back(LastInst);
2744   MachineInstr *SecondLastInst = nullptr;
2745   // Find one more terminator if present.
2746   do {
2747     if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(&*I)) {
2748       if (!SecondLastInst) {
2749         SecondLastInst = &*I;
2750         Jumpers.push_back(SecondLastInst);
2751       } else // This is a third branch.
2752         return Jumpers;
2753     }
2754     if (I == MBB.instr_begin())
2755       break;
2756     --I;
2757   } while (true);
2758   return Jumpers;
2759 }
2760
2761
2762 // Returns Operand Index for the constant extended instruction.
2763 unsigned HexagonInstrInfo::getCExtOpNum(const MachineInstr *MI) const {
2764   const uint64_t F = MI->getDesc().TSFlags;
2765   return (F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask;
2766 }
2767
2768 // See if instruction could potentially be a duplex candidate.
2769 // If so, return its group. Zero otherwise.
2770 HexagonII::CompoundGroup HexagonInstrInfo::getCompoundCandidateGroup(
2771       const MachineInstr *MI) const {
2772   unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
2773
2774   switch (MI->getOpcode()) {
2775   default:
2776     return HexagonII::HCG_None;
2777   //
2778   // Compound pairs.
2779   // "p0=cmp.eq(Rs16,Rt16); if (p0.new) jump:nt #r9:2"
2780   // "Rd16=#U6 ; jump #r9:2"
2781   // "Rd16=Rs16 ; jump #r9:2"
2782   //
2783   case Hexagon::C2_cmpeq:
2784   case Hexagon::C2_cmpgt:
2785   case Hexagon::C2_cmpgtu:
2786     DstReg = MI->getOperand(0).getReg();
2787     Src1Reg = MI->getOperand(1).getReg();
2788     Src2Reg = MI->getOperand(2).getReg();
2789     if (Hexagon::PredRegsRegClass.contains(DstReg) &&
2790         (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
2791         isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg))
2792       return HexagonII::HCG_A;
2793     break;
2794   case Hexagon::C2_cmpeqi:
2795   case Hexagon::C2_cmpgti:
2796   case Hexagon::C2_cmpgtui:
2797     // P0 = cmp.eq(Rs,#u2)
2798     DstReg = MI->getOperand(0).getReg();
2799     SrcReg = MI->getOperand(1).getReg();
2800     if (Hexagon::PredRegsRegClass.contains(DstReg) &&
2801         (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
2802         isIntRegForSubInst(SrcReg) && MI->getOperand(2).isImm() &&
2803         ((isUInt<5>(MI->getOperand(2).getImm())) ||
2804          (MI->getOperand(2).getImm() == -1)))
2805       return HexagonII::HCG_A;
2806     break;
2807   case Hexagon::A2_tfr:
2808     // Rd = Rs
2809     DstReg = MI->getOperand(0).getReg();
2810     SrcReg = MI->getOperand(1).getReg();
2811     if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
2812       return HexagonII::HCG_A;
2813     break;
2814   case Hexagon::A2_tfrsi:
2815     // Rd = #u6
2816     // Do not test for #u6 size since the const is getting extended
2817     // regardless and compound could be formed.
2818     DstReg = MI->getOperand(0).getReg();
2819     if (isIntRegForSubInst(DstReg))
2820       return HexagonII::HCG_A;
2821     break;
2822   case Hexagon::S2_tstbit_i:
2823     DstReg = MI->getOperand(0).getReg();
2824     Src1Reg = MI->getOperand(1).getReg();
2825     if (Hexagon::PredRegsRegClass.contains(DstReg) &&
2826         (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
2827         MI->getOperand(2).isImm() &&
2828         isIntRegForSubInst(Src1Reg) && (MI->getOperand(2).getImm() == 0))
2829       return HexagonII::HCG_A;
2830     break;
2831   // The fact that .new form is used pretty much guarantees
2832   // that predicate register will match. Nevertheless,
2833   // there could be some false positives without additional
2834   // checking.
2835   case Hexagon::J2_jumptnew:
2836   case Hexagon::J2_jumpfnew:
2837   case Hexagon::J2_jumptnewpt:
2838   case Hexagon::J2_jumpfnewpt:
2839     Src1Reg = MI->getOperand(0).getReg();
2840     if (Hexagon::PredRegsRegClass.contains(Src1Reg) &&
2841         (Hexagon::P0 == Src1Reg || Hexagon::P1 == Src1Reg))
2842       return HexagonII::HCG_B;
2843     break;
2844   // Transfer and jump:
2845   // Rd=#U6 ; jump #r9:2
2846   // Rd=Rs ; jump #r9:2
2847   // Do not test for jump range here.
2848   case Hexagon::J2_jump:
2849   case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
2850     return HexagonII::HCG_C;
2851     break;
2852   }
2853
2854   return HexagonII::HCG_None;
2855 }
2856
2857
2858 // Returns -1 when there is no opcode found.
2859 unsigned HexagonInstrInfo::getCompoundOpcode(const MachineInstr *GA,
2860       const MachineInstr *GB) const {
2861   assert(getCompoundCandidateGroup(GA) == HexagonII::HCG_A);
2862   assert(getCompoundCandidateGroup(GB) == HexagonII::HCG_B);
2863   if ((GA->getOpcode() != Hexagon::C2_cmpeqi) ||
2864       (GB->getOpcode() != Hexagon::J2_jumptnew))
2865     return -1;
2866   unsigned DestReg = GA->getOperand(0).getReg();
2867   if (!GB->readsRegister(DestReg))
2868     return -1;
2869   if (DestReg == Hexagon::P0)
2870     return Hexagon::J4_cmpeqi_tp0_jump_nt;
2871   if (DestReg == Hexagon::P1)
2872     return Hexagon::J4_cmpeqi_tp1_jump_nt;
2873   return -1;
2874 }
2875
2876
2877 int HexagonInstrInfo::getCondOpcode(int Opc, bool invertPredicate) const {
2878   enum Hexagon::PredSense inPredSense;
2879   inPredSense = invertPredicate ? Hexagon::PredSense_false :
2880                                   Hexagon::PredSense_true;
2881   int CondOpcode = Hexagon::getPredOpcode(Opc, inPredSense);
2882   if (CondOpcode >= 0) // Valid Conditional opcode/instruction
2883     return CondOpcode;
2884
2885   // This switch case will be removed once all the instructions have been
2886   // modified to use relation maps.
2887   switch(Opc) {
2888   case Hexagon::TFRI_f:
2889     return !invertPredicate ? Hexagon::TFRI_cPt_f :
2890                               Hexagon::TFRI_cNotPt_f;
2891   }
2892
2893   llvm_unreachable("Unexpected predicable instruction");
2894 }
2895
2896
2897 // Return the cur value instruction for a given store.
2898 int HexagonInstrInfo::getDotCurOp(const MachineInstr* MI) const {
2899   switch (MI->getOpcode()) {
2900   default: llvm_unreachable("Unknown .cur type");
2901   case Hexagon::V6_vL32b_pi:
2902     return Hexagon::V6_vL32b_cur_pi;
2903   case Hexagon::V6_vL32b_ai:
2904     return Hexagon::V6_vL32b_cur_ai;
2905   //128B
2906   case Hexagon::V6_vL32b_pi_128B:
2907     return Hexagon::V6_vL32b_cur_pi_128B;
2908   case Hexagon::V6_vL32b_ai_128B:
2909     return Hexagon::V6_vL32b_cur_ai_128B;
2910   }
2911   return 0;
2912 }
2913
2914
2915
2916 // The diagram below shows the steps involved in the conversion of a predicated
2917 // store instruction to its .new predicated new-value form.
2918 //
2919 //               p.new NV store [ if(p0.new)memw(R0+#0)=R2.new ]
2920 //                ^           ^
2921 //               /             \ (not OK. it will cause new-value store to be
2922 //              /               X conditional on p0.new while R2 producer is
2923 //             /                 \ on p0)
2924 //            /                   \.
2925 //     p.new store                 p.old NV store
2926 // [if(p0.new)memw(R0+#0)=R2]    [if(p0)memw(R0+#0)=R2.new]
2927 //            ^                  ^
2928 //             \                /
2929 //              \              /
2930 //               \            /
2931 //                 p.old store
2932 //             [if (p0)memw(R0+#0)=R2]
2933 //
2934 //
2935 // The following set of instructions further explains the scenario where
2936 // conditional new-value store becomes invalid when promoted to .new predicate
2937 // form.
2938 //
2939 // { 1) if (p0) r0 = add(r1, r2)
2940 //   2) p0 = cmp.eq(r3, #0) }
2941 //
2942 //   3) if (p0) memb(r1+#0) = r0  --> this instruction can't be grouped with
2943 // the first two instructions because in instr 1, r0 is conditional on old value
2944 // of p0 but its use in instr 3 is conditional on p0 modified by instr 2 which
2945 // is not valid for new-value stores.
2946 // Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
2947 // from the "Conditional Store" list. Because a predicated new value store
2948 // would NOT be promoted to a double dot new store. See diagram below:
2949 // This function returns yes for those stores that are predicated but not
2950 // yet promoted to predicate dot new instructions.
2951 //
2952 //                          +---------------------+
2953 //                    /-----| if (p0) memw(..)=r0 |---------\~
2954 //                   ||     +---------------------+         ||
2955 //          promote  ||       /\       /\                   ||  promote
2956 //                   ||      /||\     /||\                  ||
2957 //                  \||/    demote     ||                  \||/
2958 //                   \/       ||       ||                   \/
2959 //       +-------------------------+   ||   +-------------------------+
2960 //       | if (p0.new) memw(..)=r0 |   ||   | if (p0) memw(..)=r0.new |
2961 //       +-------------------------+   ||   +-------------------------+
2962 //                        ||           ||         ||
2963 //                        ||         demote      \||/
2964 //                      promote        ||         \/ NOT possible
2965 //                        ||           ||         /\~
2966 //                       \||/          ||        /||\~
2967 //                        \/           ||         ||
2968 //                      +-----------------------------+
2969 //                      | if (p0.new) memw(..)=r0.new |
2970 //                      +-----------------------------+
2971 //                           Double Dot New Store
2972 //
2973 // Returns the most basic instruction for the .new predicated instructions and
2974 // new-value stores.
2975 // For example, all of the following instructions will be converted back to the
2976 // same instruction:
2977 // 1) if (p0.new) memw(R0+#0) = R1.new  --->
2978 // 2) if (p0) memw(R0+#0)= R1.new      -------> if (p0) memw(R0+#0) = R1
2979 // 3) if (p0.new) memw(R0+#0) = R1      --->
2980 //
2981 // To understand the translation of instruction 1 to its original form, consider
2982 // a packet with 3 instructions.
2983 // { p0 = cmp.eq(R0,R1)
2984 //   if (p0.new) R2 = add(R3, R4)
2985 //   R5 = add (R3, R1)
2986 // }
2987 // if (p0) memw(R5+#0) = R2 <--- trying to include it in the previous packet
2988 //
2989 // This instruction can be part of the previous packet only if both p0 and R2
2990 // are promoted to .new values. This promotion happens in steps, first
2991 // predicate register is promoted to .new and in the next iteration R2 is
2992 // promoted. Therefore, in case of dependence check failure (due to R5) during
2993 // next iteration, it should be converted back to its most basic form.
2994
2995
2996 // Return the new value instruction for a given store.
2997 int HexagonInstrInfo::getDotNewOp(const MachineInstr* MI) const {
2998   int NVOpcode = Hexagon::getNewValueOpcode(MI->getOpcode());
2999   if (NVOpcode >= 0) // Valid new-value store instruction.
3000     return NVOpcode;
3001
3002   switch (MI->getOpcode()) {
3003   default: llvm_unreachable("Unknown .new type");
3004   case Hexagon::S4_storerb_ur:
3005     return Hexagon::S4_storerbnew_ur;
3006
3007   case Hexagon::S2_storerb_pci:
3008     return Hexagon::S2_storerb_pci;
3009
3010   case Hexagon::S2_storeri_pci:
3011     return Hexagon::S2_storeri_pci;
3012
3013   case Hexagon::S2_storerh_pci:
3014     return Hexagon::S2_storerh_pci;
3015
3016   case Hexagon::S2_storerd_pci:
3017     return Hexagon::S2_storerd_pci;
3018
3019   case Hexagon::S2_storerf_pci:
3020     return Hexagon::S2_storerf_pci;
3021
3022   case Hexagon::V6_vS32b_ai:
3023     return Hexagon::V6_vS32b_new_ai;
3024
3025   case Hexagon::V6_vS32b_pi:
3026     return Hexagon::V6_vS32b_new_pi;
3027
3028   // 128B
3029   case Hexagon::V6_vS32b_ai_128B:
3030     return Hexagon::V6_vS32b_new_ai_128B;
3031
3032   case Hexagon::V6_vS32b_pi_128B:
3033     return Hexagon::V6_vS32b_new_pi_128B;
3034   }
3035   return 0;
3036 }
3037
3038 // Returns the opcode to use when converting MI, which is a conditional jump,
3039 // into a conditional instruction which uses the .new value of the predicate.
3040 // We also use branch probabilities to add a hint to the jump.
3041 int HexagonInstrInfo::getDotNewPredJumpOp(MachineInstr *MI,
3042       const MachineBranchProbabilityInfo *MBPI) const {
3043   // We assume that block can have at most two successors.
3044   bool taken = false;
3045   MachineBasicBlock *Src = MI->getParent();
3046   MachineOperand *BrTarget = &MI->getOperand(1);
3047   MachineBasicBlock *Dst = BrTarget->getMBB();
3048
3049   const BranchProbability Prediction = MBPI->getEdgeProbability(Src, Dst);
3050   if (Prediction >= BranchProbability(1,2))
3051     taken = true;
3052
3053   switch (MI->getOpcode()) {
3054   case Hexagon::J2_jumpt:
3055     return taken ? Hexagon::J2_jumptnewpt : Hexagon::J2_jumptnew;
3056   case Hexagon::J2_jumpf:
3057     return taken ? Hexagon::J2_jumpfnewpt : Hexagon::J2_jumpfnew;
3058
3059   default:
3060     llvm_unreachable("Unexpected jump instruction.");
3061   }
3062 }
3063
3064
3065 // Return .new predicate version for an instruction.
3066 int HexagonInstrInfo::getDotNewPredOp(MachineInstr *MI,
3067       const MachineBranchProbabilityInfo *MBPI) const {
3068   int NewOpcode = Hexagon::getPredNewOpcode(MI->getOpcode());
3069   if (NewOpcode >= 0) // Valid predicate new instruction
3070     return NewOpcode;
3071
3072   switch (MI->getOpcode()) {
3073   // Condtional Jumps
3074   case Hexagon::J2_jumpt:
3075   case Hexagon::J2_jumpf:
3076     return getDotNewPredJumpOp(MI, MBPI);
3077
3078   default:
3079     assert(0 && "Unknown .new type");
3080   }
3081   return 0;
3082 }
3083
3084
3085 int HexagonInstrInfo::getDotOldOp(const int opc) const {
3086   int NewOp = opc;
3087   if (isPredicated(NewOp) && isPredicatedNew(NewOp)) { // Get predicate old form
3088     NewOp = Hexagon::getPredOldOpcode(NewOp);
3089     assert(NewOp >= 0 &&
3090            "Couldn't change predicate new instruction to its old form.");
3091   }
3092
3093   if (isNewValueStore(NewOp)) { // Convert into non-new-value format
3094     NewOp = Hexagon::getNonNVStore(NewOp);
3095     assert(NewOp >= 0 && "Couldn't change new-value store to its old form.");
3096   }
3097   return NewOp;
3098 }
3099
3100
3101 // See if instruction could potentially be a duplex candidate.
3102 // If so, return its group. Zero otherwise.
3103 HexagonII::SubInstructionGroup HexagonInstrInfo::getDuplexCandidateGroup(
3104       const MachineInstr *MI) const {
3105   unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
3106   auto &HRI = getRegisterInfo();
3107
3108   switch (MI->getOpcode()) {
3109   default:
3110     return HexagonII::HSIG_None;
3111   //
3112   // Group L1:
3113   //
3114   // Rd = memw(Rs+#u4:2)
3115   // Rd = memub(Rs+#u4:0)
3116   case Hexagon::L2_loadri_io:
3117     DstReg = MI->getOperand(0).getReg();
3118     SrcReg = MI->getOperand(1).getReg();
3119     // Special case this one from Group L2.
3120     // Rd = memw(r29+#u5:2)
3121     if (isIntRegForSubInst(DstReg)) {
3122       if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
3123           HRI.getStackRegister() == SrcReg &&
3124           MI->getOperand(2).isImm() &&
3125           isShiftedUInt<5,2>(MI->getOperand(2).getImm()))
3126         return HexagonII::HSIG_L2;
3127       // Rd = memw(Rs+#u4:2)
3128       if (isIntRegForSubInst(SrcReg) &&
3129           (MI->getOperand(2).isImm() &&
3130           isShiftedUInt<4,2>(MI->getOperand(2).getImm())))
3131         return HexagonII::HSIG_L1;
3132     }
3133     break;
3134   case Hexagon::L2_loadrub_io:
3135     // Rd = memub(Rs+#u4:0)
3136     DstReg = MI->getOperand(0).getReg();
3137     SrcReg = MI->getOperand(1).getReg();
3138     if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
3139         MI->getOperand(2).isImm() && isUInt<4>(MI->getOperand(2).getImm()))
3140       return HexagonII::HSIG_L1;
3141     break;
3142   //
3143   // Group L2:
3144   //
3145   // Rd = memh/memuh(Rs+#u3:1)
3146   // Rd = memb(Rs+#u3:0)
3147   // Rd = memw(r29+#u5:2) - Handled above.
3148   // Rdd = memd(r29+#u5:3)
3149   // deallocframe
3150   // [if ([!]p0[.new])] dealloc_return
3151   // [if ([!]p0[.new])] jumpr r31
3152   case Hexagon::L2_loadrh_io:
3153   case Hexagon::L2_loadruh_io:
3154     // Rd = memh/memuh(Rs+#u3:1)
3155     DstReg = MI->getOperand(0).getReg();
3156     SrcReg = MI->getOperand(1).getReg();
3157     if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
3158         MI->getOperand(2).isImm() &&
3159         isShiftedUInt<3,1>(MI->getOperand(2).getImm()))
3160       return HexagonII::HSIG_L2;
3161     break;
3162   case Hexagon::L2_loadrb_io:
3163     // Rd = memb(Rs+#u3:0)
3164     DstReg = MI->getOperand(0).getReg();
3165     SrcReg = MI->getOperand(1).getReg();
3166     if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
3167         MI->getOperand(2).isImm() &&
3168         isUInt<3>(MI->getOperand(2).getImm()))
3169       return HexagonII::HSIG_L2;
3170     break;
3171   case Hexagon::L2_loadrd_io:
3172     // Rdd = memd(r29+#u5:3)
3173     DstReg = MI->getOperand(0).getReg();
3174     SrcReg = MI->getOperand(1).getReg();
3175     if (isDblRegForSubInst(DstReg, HRI) &&
3176         Hexagon::IntRegsRegClass.contains(SrcReg) &&
3177         HRI.getStackRegister() == SrcReg &&
3178         MI->getOperand(2).isImm() &&
3179         isShiftedUInt<5,3>(MI->getOperand(2).getImm()))
3180       return HexagonII::HSIG_L2;
3181     break;
3182   // dealloc_return is not documented in Hexagon Manual, but marked
3183   // with A_SUBINSN attribute in iset_v4classic.py.
3184   case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
3185   case Hexagon::L4_return:
3186   case Hexagon::L2_deallocframe:
3187     return HexagonII::HSIG_L2;
3188   case Hexagon::EH_RETURN_JMPR:
3189   case Hexagon::JMPret :
3190     // jumpr r31
3191     // Actual form JMPR %PC<imp-def>, %R31<imp-use>, %R0<imp-use,internal>.
3192     DstReg = MI->getOperand(0).getReg();
3193     if (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg))
3194       return HexagonII::HSIG_L2;
3195     break;
3196   case Hexagon::JMPrett:
3197   case Hexagon::JMPretf:
3198   case Hexagon::JMPrettnewpt:
3199   case Hexagon::JMPretfnewpt :
3200   case Hexagon::JMPrettnew :
3201   case Hexagon::JMPretfnew :
3202     DstReg = MI->getOperand(1).getReg();
3203     SrcReg = MI->getOperand(0).getReg();
3204     // [if ([!]p0[.new])] jumpr r31
3205     if ((Hexagon::PredRegsRegClass.contains(SrcReg) &&
3206         (Hexagon::P0 == SrcReg)) &&
3207         (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg)))
3208       return HexagonII::HSIG_L2;
3209      break;
3210   case Hexagon::L4_return_t :
3211   case Hexagon::L4_return_f :
3212   case Hexagon::L4_return_tnew_pnt :
3213   case Hexagon::L4_return_fnew_pnt :
3214   case Hexagon::L4_return_tnew_pt :
3215   case Hexagon::L4_return_fnew_pt :
3216     // [if ([!]p0[.new])] dealloc_return
3217     SrcReg = MI->getOperand(0).getReg();
3218     if (Hexagon::PredRegsRegClass.contains(SrcReg) && (Hexagon::P0 == SrcReg))
3219       return HexagonII::HSIG_L2;
3220     break;
3221   //
3222   // Group S1:
3223   //
3224   // memw(Rs+#u4:2) = Rt
3225   // memb(Rs+#u4:0) = Rt
3226   case Hexagon::S2_storeri_io:
3227     // Special case this one from Group S2.
3228     // memw(r29+#u5:2) = Rt
3229     Src1Reg = MI->getOperand(0).getReg();
3230     Src2Reg = MI->getOperand(2).getReg();
3231     if (Hexagon::IntRegsRegClass.contains(Src1Reg) &&
3232         isIntRegForSubInst(Src2Reg) &&
3233         HRI.getStackRegister() == Src1Reg && MI->getOperand(1).isImm() &&
3234         isShiftedUInt<5,2>(MI->getOperand(1).getImm()))
3235       return HexagonII::HSIG_S2;
3236     // memw(Rs+#u4:2) = Rt
3237     if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
3238         MI->getOperand(1).isImm() &&
3239         isShiftedUInt<4,2>(MI->getOperand(1).getImm()))
3240       return HexagonII::HSIG_S1;
3241     break;
3242   case Hexagon::S2_storerb_io:
3243     // memb(Rs+#u4:0) = Rt
3244     Src1Reg = MI->getOperand(0).getReg();
3245     Src2Reg = MI->getOperand(2).getReg();
3246     if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
3247         MI->getOperand(1).isImm() && isUInt<4>(MI->getOperand(1).getImm()))
3248       return HexagonII::HSIG_S1;
3249     break;
3250   //
3251   // Group S2:
3252   //
3253   // memh(Rs+#u3:1) = Rt
3254   // memw(r29+#u5:2) = Rt
3255   // memd(r29+#s6:3) = Rtt
3256   // memw(Rs+#u4:2) = #U1
3257   // memb(Rs+#u4) = #U1
3258   // allocframe(#u5:3)
3259   case Hexagon::S2_storerh_io:
3260     // memh(Rs+#u3:1) = Rt
3261     Src1Reg = MI->getOperand(0).getReg();
3262     Src2Reg = MI->getOperand(2).getReg();
3263     if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
3264         MI->getOperand(1).isImm() &&
3265         isShiftedUInt<3,1>(MI->getOperand(1).getImm()))
3266       return HexagonII::HSIG_S1;
3267     break;
3268   case Hexagon::S2_storerd_io:
3269     // memd(r29+#s6:3) = Rtt
3270     Src1Reg = MI->getOperand(0).getReg();
3271     Src2Reg = MI->getOperand(2).getReg();
3272     if (isDblRegForSubInst(Src2Reg, HRI) &&
3273         Hexagon::IntRegsRegClass.contains(Src1Reg) &&
3274         HRI.getStackRegister() == Src1Reg && MI->getOperand(1).isImm() &&
3275         isShiftedInt<6,3>(MI->getOperand(1).getImm()))
3276       return HexagonII::HSIG_S2;
3277     break;
3278   case Hexagon::S4_storeiri_io:
3279     // memw(Rs+#u4:2) = #U1
3280     Src1Reg = MI->getOperand(0).getReg();
3281     if (isIntRegForSubInst(Src1Reg) && MI->getOperand(1).isImm() &&
3282         isShiftedUInt<4,2>(MI->getOperand(1).getImm()) &&
3283         MI->getOperand(2).isImm() && isUInt<1>(MI->getOperand(2).getImm()))
3284       return HexagonII::HSIG_S2;
3285     break;
3286   case Hexagon::S4_storeirb_io:
3287     // memb(Rs+#u4) = #U1
3288     Src1Reg = MI->getOperand(0).getReg();
3289     if (isIntRegForSubInst(Src1Reg) && MI->getOperand(1).isImm() &&
3290         isUInt<4>(MI->getOperand(1).getImm()) && MI->getOperand(2).isImm() &&
3291         MI->getOperand(2).isImm() && isUInt<1>(MI->getOperand(2).getImm()))
3292       return HexagonII::HSIG_S2;
3293     break;
3294   case Hexagon::S2_allocframe:
3295     if (MI->getOperand(0).isImm() &&
3296         isShiftedUInt<5,3>(MI->getOperand(0).getImm()))
3297       return HexagonII::HSIG_S1;
3298     break;
3299   //
3300   // Group A:
3301   //
3302   // Rx = add(Rx,#s7)
3303   // Rd = Rs
3304   // Rd = #u6
3305   // Rd = #-1
3306   // if ([!]P0[.new]) Rd = #0
3307   // Rd = add(r29,#u6:2)
3308   // Rx = add(Rx,Rs)
3309   // P0 = cmp.eq(Rs,#u2)
3310   // Rdd = combine(#0,Rs)
3311   // Rdd = combine(Rs,#0)
3312   // Rdd = combine(#u2,#U2)
3313   // Rd = add(Rs,#1)
3314   // Rd = add(Rs,#-1)
3315   // Rd = sxth/sxtb/zxtb/zxth(Rs)
3316   // Rd = and(Rs,#1)
3317   case Hexagon::A2_addi:
3318     DstReg = MI->getOperand(0).getReg();
3319     SrcReg = MI->getOperand(1).getReg();
3320     if (isIntRegForSubInst(DstReg)) {
3321       // Rd = add(r29,#u6:2)
3322       if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
3323         HRI.getStackRegister() == SrcReg && MI->getOperand(2).isImm() &&
3324         isShiftedUInt<6,2>(MI->getOperand(2).getImm()))
3325         return HexagonII::HSIG_A;
3326       // Rx = add(Rx,#s7)
3327       if ((DstReg == SrcReg) && MI->getOperand(2).isImm() &&
3328           isInt<7>(MI->getOperand(2).getImm()))
3329         return HexagonII::HSIG_A;
3330       // Rd = add(Rs,#1)
3331       // Rd = add(Rs,#-1)
3332       if (isIntRegForSubInst(SrcReg) && MI->getOperand(2).isImm() &&
3333           ((MI->getOperand(2).getImm() == 1) ||
3334           (MI->getOperand(2).getImm() == -1)))
3335         return HexagonII::HSIG_A;
3336     }
3337     break;
3338   case Hexagon::A2_add:
3339     // Rx = add(Rx,Rs)
3340     DstReg = MI->getOperand(0).getReg();
3341     Src1Reg = MI->getOperand(1).getReg();
3342     Src2Reg = MI->getOperand(2).getReg();
3343     if (isIntRegForSubInst(DstReg) && (DstReg == Src1Reg) &&
3344         isIntRegForSubInst(Src2Reg))
3345       return HexagonII::HSIG_A;
3346     break;
3347   case Hexagon::A2_andir:
3348     // Same as zxtb.
3349     // Rd16=and(Rs16,#255)
3350     // Rd16=and(Rs16,#1)
3351     DstReg = MI->getOperand(0).getReg();
3352     SrcReg = MI->getOperand(1).getReg();
3353     if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
3354         MI->getOperand(2).isImm() &&
3355         ((MI->getOperand(2).getImm() == 1) ||
3356         (MI->getOperand(2).getImm() == 255)))
3357       return HexagonII::HSIG_A;
3358     break;
3359   case Hexagon::A2_tfr:
3360     // Rd = Rs
3361     DstReg = MI->getOperand(0).getReg();
3362     SrcReg = MI->getOperand(1).getReg();
3363     if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3364       return HexagonII::HSIG_A;
3365     break;
3366   case Hexagon::A2_tfrsi:
3367     // Rd = #u6
3368     // Do not test for #u6 size since the const is getting extended
3369     // regardless and compound could be formed.
3370     // Rd = #-1
3371     DstReg = MI->getOperand(0).getReg();
3372     if (isIntRegForSubInst(DstReg))
3373       return HexagonII::HSIG_A;
3374     break;
3375   case Hexagon::C2_cmoveit:
3376   case Hexagon::C2_cmovenewit:
3377   case Hexagon::C2_cmoveif:
3378   case Hexagon::C2_cmovenewif:
3379     // if ([!]P0[.new]) Rd = #0
3380     // Actual form:
3381     // %R16<def> = C2_cmovenewit %P0<internal>, 0, %R16<imp-use,undef>;
3382     DstReg = MI->getOperand(0).getReg();
3383     SrcReg = MI->getOperand(1).getReg();
3384     if (isIntRegForSubInst(DstReg) &&
3385         Hexagon::PredRegsRegClass.contains(SrcReg) && Hexagon::P0 == SrcReg &&
3386         MI->getOperand(2).isImm() && MI->getOperand(2).getImm() == 0)
3387       return HexagonII::HSIG_A;
3388     break;
3389   case Hexagon::C2_cmpeqi:
3390     // P0 = cmp.eq(Rs,#u2)
3391     DstReg = MI->getOperand(0).getReg();
3392     SrcReg = MI->getOperand(1).getReg();
3393     if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3394         Hexagon::P0 == DstReg && isIntRegForSubInst(SrcReg) &&
3395         MI->getOperand(2).isImm() && isUInt<2>(MI->getOperand(2).getImm()))
3396       return HexagonII::HSIG_A;
3397     break;
3398   case Hexagon::A2_combineii:
3399   case Hexagon::A4_combineii:
3400     // Rdd = combine(#u2,#U2)
3401     DstReg = MI->getOperand(0).getReg();
3402     if (isDblRegForSubInst(DstReg, HRI) &&
3403         ((MI->getOperand(1).isImm() && isUInt<2>(MI->getOperand(1).getImm())) ||
3404         (MI->getOperand(1).isGlobal() &&
3405         isUInt<2>(MI->getOperand(1).getOffset()))) &&
3406         ((MI->getOperand(2).isImm() && isUInt<2>(MI->getOperand(2).getImm())) ||
3407         (MI->getOperand(2).isGlobal() &&
3408         isUInt<2>(MI->getOperand(2).getOffset()))))
3409       return HexagonII::HSIG_A;
3410     break;
3411   case Hexagon::A4_combineri:
3412     // Rdd = combine(Rs,#0)
3413     DstReg = MI->getOperand(0).getReg();
3414     SrcReg = MI->getOperand(1).getReg();
3415     if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
3416         ((MI->getOperand(2).isImm() && MI->getOperand(2).getImm() == 0) ||
3417         (MI->getOperand(2).isGlobal() && MI->getOperand(2).getOffset() == 0)))
3418       return HexagonII::HSIG_A;
3419     break;
3420   case Hexagon::A4_combineir:
3421     // Rdd = combine(#0,Rs)
3422     DstReg = MI->getOperand(0).getReg();
3423     SrcReg = MI->getOperand(2).getReg();
3424     if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
3425         ((MI->getOperand(1).isImm() && MI->getOperand(1).getImm() == 0) ||
3426         (MI->getOperand(1).isGlobal() && MI->getOperand(1).getOffset() == 0)))
3427       return HexagonII::HSIG_A;
3428     break;
3429   case Hexagon::A2_sxtb:
3430   case Hexagon::A2_sxth:
3431   case Hexagon::A2_zxtb:
3432   case Hexagon::A2_zxth:
3433     // Rd = sxth/sxtb/zxtb/zxth(Rs)
3434     DstReg = MI->getOperand(0).getReg();
3435     SrcReg = MI->getOperand(1).getReg();
3436     if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3437       return HexagonII::HSIG_A;
3438     break;
3439   }
3440
3441   return HexagonII::HSIG_None;
3442 }
3443
3444
3445 short HexagonInstrInfo::getEquivalentHWInstr(MachineInstr *MI) const {
3446   return Hexagon::getRealHWInstr(MI->getOpcode(), Hexagon::InstrType_Real);
3447 }
3448
3449
3450 // Return first non-debug instruction in the basic block.
3451 MachineInstr *HexagonInstrInfo::getFirstNonDbgInst(MachineBasicBlock *BB)
3452       const {
3453   for (auto MII = BB->instr_begin(), End = BB->instr_end(); MII != End; MII++) {
3454     MachineInstr *MI = &*MII;
3455     if (MI->isDebugValue())
3456       continue;
3457     return MI;
3458   }
3459   return nullptr;
3460 }
3461
3462
3463 unsigned HexagonInstrInfo::getInstrTimingClassLatency(
3464       const InstrItineraryData *ItinData, const MachineInstr *MI) const {
3465   // Default to one cycle for no itinerary. However, an "empty" itinerary may
3466   // still have a MinLatency property, which getStageLatency checks.
3467   if (!ItinData)
3468     return getInstrLatency(ItinData, MI);
3469
3470   // Get the latency embedded in the itinerary. If we're not using timing class
3471   // latencies or if we using BSB scheduling, then restrict the maximum latency
3472   // to 1 (that is, either 0 or 1).
3473   if (MI->isTransient())
3474     return 0;
3475   unsigned Latency = ItinData->getStageLatency(MI->getDesc().getSchedClass());
3476   if (!EnableTimingClassLatency ||
3477       MI->getParent()->getParent()->getSubtarget<HexagonSubtarget>().
3478       useBSBScheduling())
3479     if (Latency > 1)
3480       Latency = 1;
3481   return Latency;
3482 }
3483
3484
3485 // inverts the predication logic.
3486 // p -> NotP
3487 // NotP -> P
3488 bool HexagonInstrInfo::getInvertedPredSense(
3489       SmallVectorImpl<MachineOperand> &Cond) const {
3490   if (Cond.empty())
3491     return false;
3492   unsigned Opc = getInvertedPredicatedOpcode(Cond[0].getImm());
3493   Cond[0].setImm(Opc);
3494   return true;
3495 }
3496
3497
3498 unsigned HexagonInstrInfo::getInvertedPredicatedOpcode(const int Opc) const {
3499   int InvPredOpcode;
3500   InvPredOpcode = isPredicatedTrue(Opc) ? Hexagon::getFalsePredOpcode(Opc)
3501                                         : Hexagon::getTruePredOpcode(Opc);
3502   if (InvPredOpcode >= 0) // Valid instruction with the inverted predicate.
3503     return InvPredOpcode;
3504
3505   llvm_unreachable("Unexpected predicated instruction");
3506 }
3507
3508
3509 // Returns the max value that doesn't need to be extended.
3510 int HexagonInstrInfo::getMaxValue(const MachineInstr *MI) const {
3511   const uint64_t F = MI->getDesc().TSFlags;
3512   unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
3513                     & HexagonII::ExtentSignedMask;
3514   unsigned bits =  (F >> HexagonII::ExtentBitsPos)
3515                     & HexagonII::ExtentBitsMask;
3516
3517   if (isSigned) // if value is signed
3518     return ~(-1U << (bits - 1));
3519   else
3520     return ~(-1U << bits);
3521 }
3522
3523
3524 unsigned HexagonInstrInfo::getMemAccessSize(const MachineInstr* MI) const {
3525   const uint64_t F = MI->getDesc().TSFlags;
3526   return (F >> HexagonII::MemAccessSizePos) & HexagonII::MemAccesSizeMask;
3527 }
3528
3529
3530 // Returns the min value that doesn't need to be extended.
3531 int HexagonInstrInfo::getMinValue(const MachineInstr *MI) const {
3532   const uint64_t F = MI->getDesc().TSFlags;
3533   unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
3534                     & HexagonII::ExtentSignedMask;
3535   unsigned bits =  (F >> HexagonII::ExtentBitsPos)
3536                     & HexagonII::ExtentBitsMask;
3537
3538   if (isSigned) // if value is signed
3539     return -1U << (bits - 1);
3540   else
3541     return 0;
3542 }
3543
3544
3545 // Returns opcode of the non-extended equivalent instruction.
3546 short HexagonInstrInfo::getNonExtOpcode(const MachineInstr *MI) const {
3547   // Check if the instruction has a register form that uses register in place
3548   // of the extended operand, if so return that as the non-extended form.
3549   short NonExtOpcode = Hexagon::getRegForm(MI->getOpcode());
3550     if (NonExtOpcode >= 0)
3551       return NonExtOpcode;
3552
3553   if (MI->getDesc().mayLoad() || MI->getDesc().mayStore()) {
3554     // Check addressing mode and retrieve non-ext equivalent instruction.
3555     switch (getAddrMode(MI)) {
3556     case HexagonII::Absolute :
3557       return Hexagon::getBaseWithImmOffset(MI->getOpcode());
3558     case HexagonII::BaseImmOffset :
3559       return Hexagon::getBaseWithRegOffset(MI->getOpcode());
3560     case HexagonII::BaseLongOffset:
3561       return Hexagon::getRegShlForm(MI->getOpcode());
3562
3563     default:
3564       return -1;
3565     }
3566   }
3567   return -1;
3568 }
3569
3570
3571 bool HexagonInstrInfo::getPredReg(ArrayRef<MachineOperand> Cond,
3572       unsigned &PredReg, unsigned &PredRegPos, unsigned &PredRegFlags) const {
3573   if (Cond.empty())
3574     return false;
3575   assert(Cond.size() == 2);
3576   if (isNewValueJump(Cond[0].getImm()) || Cond[1].isMBB()) {
3577      DEBUG(dbgs() << "No predregs for new-value jumps/endloop");
3578      return false;
3579   }
3580   PredReg = Cond[1].getReg();
3581   PredRegPos = 1;
3582   // See IfConversion.cpp why we add RegState::Implicit | RegState::Undef
3583   PredRegFlags = 0;
3584   if (Cond[1].isImplicit())
3585     PredRegFlags = RegState::Implicit;
3586   if (Cond[1].isUndef())
3587     PredRegFlags |= RegState::Undef;
3588   return true;
3589 }
3590
3591
3592 short HexagonInstrInfo::getPseudoInstrPair(MachineInstr *MI) const {
3593   return Hexagon::getRealHWInstr(MI->getOpcode(), Hexagon::InstrType_Pseudo);
3594 }
3595
3596
3597 short HexagonInstrInfo::getRegForm(const MachineInstr *MI) const {
3598   return Hexagon::getRegForm(MI->getOpcode());
3599 }
3600
3601
3602 // Return the number of bytes required to encode the instruction.
3603 // Hexagon instructions are fixed length, 4 bytes, unless they
3604 // use a constant extender, which requires another 4 bytes.
3605 // For debug instructions and prolog labels, return 0.
3606 unsigned HexagonInstrInfo::getSize(const MachineInstr *MI) const {
3607   if (MI->isDebugValue() || MI->isPosition())
3608     return 0;
3609
3610   unsigned Size = MI->getDesc().getSize();
3611   if (!Size)
3612     // Assume the default insn size in case it cannot be determined
3613     // for whatever reason.
3614     Size = HEXAGON_INSTR_SIZE;
3615
3616   if (isConstExtended(MI) || isExtended(MI))
3617     Size += HEXAGON_INSTR_SIZE;
3618
3619   // Try and compute number of instructions in asm.
3620   if (BranchRelaxAsmLarge && MI->getOpcode() == Hexagon::INLINEASM) {
3621     const MachineBasicBlock &MBB = *MI->getParent();
3622     const MachineFunction *MF = MBB.getParent();
3623     const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
3624
3625     // Count the number of register definitions to find the asm string.
3626     unsigned NumDefs = 0;
3627     for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
3628          ++NumDefs)
3629       assert(NumDefs != MI->getNumOperands()-2 && "No asm string?");
3630
3631     assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
3632     // Disassemble the AsmStr and approximate number of instructions.
3633     const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
3634     Size = getInlineAsmLength(AsmStr, *MAI);
3635   }
3636
3637   return Size;
3638 }
3639
3640
3641 uint64_t HexagonInstrInfo::getType(const MachineInstr* MI) const {
3642   const uint64_t F = MI->getDesc().TSFlags;
3643   return (F >> HexagonII::TypePos) & HexagonII::TypeMask;
3644 }
3645
3646
3647 unsigned HexagonInstrInfo::getUnits(const MachineInstr* MI) const {
3648   const TargetSubtargetInfo &ST = MI->getParent()->getParent()->getSubtarget();
3649   const InstrItineraryData &II = *ST.getInstrItineraryData();
3650   const InstrStage &IS = *II.beginStage(MI->getDesc().getSchedClass());
3651
3652   return IS.getUnits();
3653 }
3654
3655
3656 unsigned HexagonInstrInfo::getValidSubTargets(const unsigned Opcode) const {
3657   const uint64_t F = get(Opcode).TSFlags;
3658   return (F >> HexagonII::validSubTargetPos) & HexagonII::validSubTargetMask;
3659 }
3660
3661
3662 // Calculate size of the basic block without debug instructions.
3663 unsigned HexagonInstrInfo::nonDbgBBSize(const MachineBasicBlock *BB) const {
3664   return nonDbgMICount(BB->instr_begin(), BB->instr_end());
3665 }
3666
3667
3668 unsigned HexagonInstrInfo::nonDbgBundleSize(
3669       MachineBasicBlock::const_iterator BundleHead) const {
3670   assert(BundleHead->isBundle() && "Not a bundle header");
3671   auto MII = BundleHead.getInstrIterator();
3672   // Skip the bundle header.
3673   return nonDbgMICount(++MII, getBundleEnd(BundleHead));
3674 }
3675
3676
3677 /// immediateExtend - Changes the instruction in place to one using an immediate
3678 /// extender.
3679 void HexagonInstrInfo::immediateExtend(MachineInstr *MI) const {
3680   assert((isExtendable(MI)||isConstExtended(MI)) &&
3681                                "Instruction must be extendable");
3682   // Find which operand is extendable.
3683   short ExtOpNum = getCExtOpNum(MI);
3684   MachineOperand &MO = MI->getOperand(ExtOpNum);
3685   // This needs to be something we understand.
3686   assert((MO.isMBB() || MO.isImm()) &&
3687          "Branch with unknown extendable field type");
3688   // Mark given operand as extended.
3689   MO.addTargetFlag(HexagonII::HMOTF_ConstExtended);
3690 }
3691
3692
3693 bool HexagonInstrInfo::invertAndChangeJumpTarget(
3694       MachineInstr* MI, MachineBasicBlock* NewTarget) const {
3695   DEBUG(dbgs() << "\n[invertAndChangeJumpTarget] to BB#"
3696                << NewTarget->getNumber(); MI->dump(););
3697   assert(MI->isBranch());
3698   unsigned NewOpcode = getInvertedPredicatedOpcode(MI->getOpcode());
3699   int TargetPos = MI->getNumOperands() - 1;
3700   // In general branch target is the last operand,
3701   // but some implicit defs added at the end might change it.
3702   while ((TargetPos > -1) && !MI->getOperand(TargetPos).isMBB())
3703     --TargetPos;
3704   assert((TargetPos >= 0) && MI->getOperand(TargetPos).isMBB());
3705   MI->getOperand(TargetPos).setMBB(NewTarget);
3706   if (EnableBranchPrediction && isPredicatedNew(MI)) {
3707     NewOpcode = reversePrediction(NewOpcode);
3708   }
3709   MI->setDesc(get(NewOpcode));
3710   return true;
3711 }
3712
3713
3714 void HexagonInstrInfo::genAllInsnTimingClasses(MachineFunction &MF) const {
3715   /* +++ The code below is used to generate complete set of Hexagon Insn +++ */
3716   MachineFunction::iterator A = MF.begin();
3717   MachineBasicBlock &B = *A;
3718   MachineBasicBlock::iterator I = B.begin();
3719   MachineInstr *MI = &*I;
3720   DebugLoc DL = MI->getDebugLoc();
3721   MachineInstr *NewMI;
3722
3723   for (unsigned insn = TargetOpcode::GENERIC_OP_END+1;
3724        insn < Hexagon::INSTRUCTION_LIST_END; ++insn) {
3725     NewMI = BuildMI(B, MI, DL, get(insn));
3726     DEBUG(dbgs() << "\n" << getName(NewMI->getOpcode()) <<
3727           "  Class: " << NewMI->getDesc().getSchedClass());
3728     NewMI->eraseFromParent();
3729   }
3730   /* --- The code above is used to generate complete set of Hexagon Insn --- */
3731 }
3732
3733
3734 // inverts the predication logic.
3735 // p -> NotP
3736 // NotP -> P
3737 bool HexagonInstrInfo::reversePredSense(MachineInstr* MI) const {
3738   DEBUG(dbgs() << "\nTrying to reverse pred. sense of:"; MI->dump());
3739   MI->setDesc(get(getInvertedPredicatedOpcode(MI->getOpcode())));
3740   return true;
3741 }
3742
3743
3744 // Reverse the branch prediction.
3745 unsigned HexagonInstrInfo::reversePrediction(unsigned Opcode) const {
3746   int PredRevOpcode = -1;
3747   if (isPredictedTaken(Opcode))
3748     PredRevOpcode = Hexagon::notTakenBranchPrediction(Opcode);
3749   else
3750     PredRevOpcode = Hexagon::takenBranchPrediction(Opcode);
3751   assert(PredRevOpcode > 0);
3752   return PredRevOpcode;
3753 }
3754
3755
3756 // TODO: Add more rigorous validation.
3757 bool HexagonInstrInfo::validateBranchCond(const ArrayRef<MachineOperand> &Cond)
3758       const {
3759   return Cond.empty() || (Cond[0].isImm() && (Cond.size() != 1));
3760 }
3761