Hexagon: Remove duplicate instructions to handle global/immediate values
[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/Support/MathExtras.h"
27 #define GET_INSTRINFO_CTOR
28 #define GET_INSTRMAP_INFO
29 #include "HexagonGenInstrInfo.inc"
30 #include "HexagonGenDFAPacketizer.inc"
31
32 using namespace llvm;
33
34 ///
35 /// Constants for Hexagon instructions.
36 ///
37 const int Hexagon_MEMW_OFFSET_MAX = 4095;
38 const int Hexagon_MEMW_OFFSET_MIN = -4096;
39 const int Hexagon_MEMD_OFFSET_MAX = 8191;
40 const int Hexagon_MEMD_OFFSET_MIN = -8192;
41 const int Hexagon_MEMH_OFFSET_MAX = 2047;
42 const int Hexagon_MEMH_OFFSET_MIN = -2048;
43 const int Hexagon_MEMB_OFFSET_MAX = 1023;
44 const int Hexagon_MEMB_OFFSET_MIN = -1024;
45 const int Hexagon_ADDI_OFFSET_MAX = 32767;
46 const int Hexagon_ADDI_OFFSET_MIN = -32768;
47 const int Hexagon_MEMD_AUTOINC_MAX = 56;
48 const int Hexagon_MEMD_AUTOINC_MIN = -64;
49 const int Hexagon_MEMW_AUTOINC_MAX = 28;
50 const int Hexagon_MEMW_AUTOINC_MIN = -32;
51 const int Hexagon_MEMH_AUTOINC_MAX = 14;
52 const int Hexagon_MEMH_AUTOINC_MIN = -16;
53 const int Hexagon_MEMB_AUTOINC_MAX = 7;
54 const int Hexagon_MEMB_AUTOINC_MIN = -8;
55
56
57 HexagonInstrInfo::HexagonInstrInfo(HexagonSubtarget &ST)
58   : HexagonGenInstrInfo(Hexagon::ADJCALLSTACKDOWN, Hexagon::ADJCALLSTACKUP),
59     RI(ST, *this), Subtarget(ST) {
60 }
61
62
63 /// isLoadFromStackSlot - If the specified machine instruction is a direct
64 /// load from a stack slot, return the virtual or physical register number of
65 /// the destination along with the FrameIndex of the loaded stack slot.  If
66 /// not, return 0.  This predicate must return 0 if the instruction has
67 /// any side effects other than loading from the stack slot.
68 unsigned HexagonInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
69                                              int &FrameIndex) const {
70
71
72   switch (MI->getOpcode()) {
73   default: break;
74   case Hexagon::LDriw:
75   case Hexagon::LDrid:
76   case Hexagon::LDrih:
77   case Hexagon::LDrib:
78   case Hexagon::LDriub:
79     if (MI->getOperand(2).isFI() &&
80         MI->getOperand(1).isImm() && (MI->getOperand(1).getImm() == 0)) {
81       FrameIndex = MI->getOperand(2).getIndex();
82       return MI->getOperand(0).getReg();
83     }
84     break;
85   }
86   return 0;
87 }
88
89
90 /// isStoreToStackSlot - If the specified machine instruction is a direct
91 /// store to a stack slot, return the virtual or physical register number of
92 /// the source reg along with the FrameIndex of the loaded stack slot.  If
93 /// not, return 0.  This predicate must return 0 if the instruction has
94 /// any side effects other than storing to the stack slot.
95 unsigned HexagonInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
96                                             int &FrameIndex) const {
97   switch (MI->getOpcode()) {
98   default: break;
99   case Hexagon::STriw:
100   case Hexagon::STrid:
101   case Hexagon::STrih:
102   case Hexagon::STrib:
103     if (MI->getOperand(2).isFI() &&
104         MI->getOperand(1).isImm() && (MI->getOperand(1).getImm() == 0)) {
105       FrameIndex = MI->getOperand(0).getIndex();
106       return MI->getOperand(2).getReg();
107     }
108     break;
109   }
110   return 0;
111 }
112
113
114 unsigned
115 HexagonInstrInfo::InsertBranch(MachineBasicBlock &MBB,MachineBasicBlock *TBB,
116                              MachineBasicBlock *FBB,
117                              const SmallVectorImpl<MachineOperand> &Cond,
118                              DebugLoc DL) const{
119
120     int BOpc   = Hexagon::JMP;
121     int BccOpc = Hexagon::JMP_c;
122
123     assert(TBB && "InsertBranch must not be told to insert a fallthrough");
124
125     int regPos = 0;
126     // Check if ReverseBranchCondition has asked to reverse this branch
127     // If we want to reverse the branch an odd number of times, we want
128     // JMP_cNot.
129     if (!Cond.empty() && Cond[0].isImm() && Cond[0].getImm() == 0) {
130       BccOpc = Hexagon::JMP_cNot;
131       regPos = 1;
132     }
133
134     if (FBB == 0) {
135       if (Cond.empty()) {
136         // Due to a bug in TailMerging/CFG Optimization, we need to add a
137         // special case handling of a predicated jump followed by an
138         // unconditional jump. If not, Tail Merging and CFG Optimization go
139         // into an infinite loop.
140         MachineBasicBlock *NewTBB, *NewFBB;
141         SmallVector<MachineOperand, 4> Cond;
142         MachineInstr *Term = MBB.getFirstTerminator();
143         if (isPredicated(Term) && !AnalyzeBranch(MBB, NewTBB, NewFBB, Cond,
144                                                  false)) {
145           MachineBasicBlock *NextBB =
146             llvm::next(MachineFunction::iterator(&MBB));
147           if (NewTBB == NextBB) {
148             ReverseBranchCondition(Cond);
149             RemoveBranch(MBB);
150             return InsertBranch(MBB, TBB, 0, Cond, DL);
151           }
152         }
153         BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
154       } else {
155         BuildMI(&MBB, DL,
156                 get(BccOpc)).addReg(Cond[regPos].getReg()).addMBB(TBB);
157       }
158       return 1;
159     }
160
161     BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[regPos].getReg()).addMBB(TBB);
162     BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
163
164     return 2;
165 }
166
167
168 bool HexagonInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
169                                      MachineBasicBlock *&TBB,
170                                  MachineBasicBlock *&FBB,
171                                  SmallVectorImpl<MachineOperand> &Cond,
172                                  bool AllowModify) const {
173   TBB = NULL;
174   FBB = NULL;
175
176   // If the block has no terminators, it just falls into the block after it.
177   MachineBasicBlock::iterator I = MBB.end();
178   if (I == MBB.begin())
179     return false;
180
181   // A basic block may looks like this:
182   //
183   //  [   insn
184   //     EH_LABEL
185   //      insn
186   //      insn
187   //      insn
188   //     EH_LABEL
189   //      insn     ]
190   //
191   // It has two succs but does not have a terminator
192   // Don't know how to handle it.
193   do {
194     --I;
195     if (I->isEHLabel())
196       return true;
197   } while (I != MBB.begin());
198
199   I = MBB.end();
200   --I;
201
202   while (I->isDebugValue()) {
203     if (I == MBB.begin())
204       return false;
205     --I;
206   }
207   if (!isUnpredicatedTerminator(I))
208     return false;
209
210   // Get the last instruction in the block.
211   MachineInstr *LastInst = I;
212
213   // If there is only one terminator instruction, process it.
214   if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
215     if (LastInst->getOpcode() == Hexagon::JMP) {
216       TBB = LastInst->getOperand(0).getMBB();
217       return false;
218     }
219     if (LastInst->getOpcode() == Hexagon::JMP_c) {
220       // Block ends with fall-through true condbranch.
221       TBB = LastInst->getOperand(1).getMBB();
222       Cond.push_back(LastInst->getOperand(0));
223       return false;
224     }
225     if (LastInst->getOpcode() == Hexagon::JMP_cNot) {
226       // Block ends with fall-through false condbranch.
227       TBB = LastInst->getOperand(1).getMBB();
228       Cond.push_back(MachineOperand::CreateImm(0));
229       Cond.push_back(LastInst->getOperand(0));
230       return false;
231     }
232     // Otherwise, don't know what this is.
233     return true;
234   }
235
236   // Get the instruction before it if it's a terminator.
237   MachineInstr *SecondLastInst = I;
238
239   // If there are three terminators, we don't know what sort of block this is.
240   if (SecondLastInst && I != MBB.begin() &&
241       isUnpredicatedTerminator(--I))
242     return true;
243
244   // If the block ends with Hexagon::BRCOND and Hexagon:JMP, handle it.
245   if (((SecondLastInst->getOpcode() == Hexagon::BRCOND) ||
246       (SecondLastInst->getOpcode() == Hexagon::JMP_c)) &&
247       LastInst->getOpcode() == Hexagon::JMP) {
248     TBB =  SecondLastInst->getOperand(1).getMBB();
249     Cond.push_back(SecondLastInst->getOperand(0));
250     FBB = LastInst->getOperand(0).getMBB();
251     return false;
252   }
253
254   // If the block ends with Hexagon::JMP_cNot and Hexagon:JMP, handle it.
255   if ((SecondLastInst->getOpcode() == Hexagon::JMP_cNot) &&
256       LastInst->getOpcode() == Hexagon::JMP) {
257     TBB =  SecondLastInst->getOperand(1).getMBB();
258     Cond.push_back(MachineOperand::CreateImm(0));
259     Cond.push_back(SecondLastInst->getOperand(0));
260     FBB = LastInst->getOperand(0).getMBB();
261     return false;
262   }
263
264   // If the block ends with two Hexagon:JMPs, handle it.  The second one is not
265   // executed, so remove it.
266   if (SecondLastInst->getOpcode() == Hexagon::JMP &&
267       LastInst->getOpcode() == Hexagon::JMP) {
268     TBB = SecondLastInst->getOperand(0).getMBB();
269     I = LastInst;
270     if (AllowModify)
271       I->eraseFromParent();
272     return false;
273   }
274
275   // Otherwise, can't handle this.
276   return true;
277 }
278
279
280 unsigned HexagonInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
281   int BOpc   = Hexagon::JMP;
282   int BccOpc = Hexagon::JMP_c;
283   int BccOpcNot = Hexagon::JMP_cNot;
284
285   MachineBasicBlock::iterator I = MBB.end();
286   if (I == MBB.begin()) return 0;
287   --I;
288   if (I->getOpcode() != BOpc && I->getOpcode() != BccOpc &&
289       I->getOpcode() != BccOpcNot)
290     return 0;
291
292   // Remove the branch.
293   I->eraseFromParent();
294
295   I = MBB.end();
296
297   if (I == MBB.begin()) return 1;
298   --I;
299   if (I->getOpcode() != BccOpc && I->getOpcode() != BccOpcNot)
300     return 1;
301
302   // Remove the branch.
303   I->eraseFromParent();
304   return 2;
305 }
306
307
308 /// \brief For a comparison instruction, return the source registers in
309 /// \p SrcReg and \p SrcReg2 if having two register operands, and the value it
310 /// compares against in CmpValue. Return true if the comparison instruction
311 /// can be analyzed.
312 bool HexagonInstrInfo::analyzeCompare(const MachineInstr *MI,
313                                       unsigned &SrcReg, unsigned &SrcReg2,
314                                       int &Mask, int &Value) const {
315   unsigned Opc = MI->getOpcode();
316
317   // Set mask and the first source register.
318   switch (Opc) {
319     case Hexagon::CMPEHexagon4rr:
320     case Hexagon::CMPEQri:
321     case Hexagon::CMPEQrr:
322     case Hexagon::CMPGT64rr:
323     case Hexagon::CMPGTU64rr:
324     case Hexagon::CMPGTUri:
325     case Hexagon::CMPGTUrr:
326     case Hexagon::CMPGTri:
327     case Hexagon::CMPGTrr:
328     case Hexagon::CMPLTUrr:
329     case Hexagon::CMPLTrr:
330       SrcReg = MI->getOperand(1).getReg();
331       Mask = ~0;
332       break;
333     case Hexagon::CMPbEQri_V4:
334     case Hexagon::CMPbEQrr_sbsb_V4:
335     case Hexagon::CMPbEQrr_ubub_V4:
336     case Hexagon::CMPbGTUri_V4:
337     case Hexagon::CMPbGTUrr_V4:
338     case Hexagon::CMPbGTrr_V4:
339       SrcReg = MI->getOperand(1).getReg();
340       Mask = 0xFF;
341       break;
342     case Hexagon::CMPhEQri_V4:
343     case Hexagon::CMPhEQrr_shl_V4:
344     case Hexagon::CMPhEQrr_xor_V4:
345     case Hexagon::CMPhGTUri_V4:
346     case Hexagon::CMPhGTUrr_V4:
347     case Hexagon::CMPhGTrr_shl_V4:
348       SrcReg = MI->getOperand(1).getReg();
349       Mask = 0xFFFF;
350       break;
351   }
352
353   // Set the value/second source register.
354   switch (Opc) {
355     case Hexagon::CMPEHexagon4rr:
356     case Hexagon::CMPEQrr:
357     case Hexagon::CMPGT64rr:
358     case Hexagon::CMPGTU64rr:
359     case Hexagon::CMPGTUrr:
360     case Hexagon::CMPGTrr:
361     case Hexagon::CMPbEQrr_sbsb_V4:
362     case Hexagon::CMPbEQrr_ubub_V4:
363     case Hexagon::CMPbGTUrr_V4:
364     case Hexagon::CMPbGTrr_V4:
365     case Hexagon::CMPhEQrr_shl_V4:
366     case Hexagon::CMPhEQrr_xor_V4:
367     case Hexagon::CMPhGTUrr_V4:
368     case Hexagon::CMPhGTrr_shl_V4:
369     case Hexagon::CMPLTUrr:
370     case Hexagon::CMPLTrr:
371       SrcReg2 = MI->getOperand(2).getReg();
372       return true;
373
374     case Hexagon::CMPEQri:
375     case Hexagon::CMPGTUri:
376     case Hexagon::CMPGTri:
377     case Hexagon::CMPbEQri_V4:
378     case Hexagon::CMPbGTUri_V4:
379     case Hexagon::CMPhEQri_V4:
380     case Hexagon::CMPhGTUri_V4:
381       SrcReg2 = 0;
382       Value = MI->getOperand(2).getImm();
383       return true;
384   }
385
386   return false;
387 }
388
389
390 void HexagonInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
391                                  MachineBasicBlock::iterator I, DebugLoc DL,
392                                  unsigned DestReg, unsigned SrcReg,
393                                  bool KillSrc) const {
394   if (Hexagon::IntRegsRegClass.contains(SrcReg, DestReg)) {
395     BuildMI(MBB, I, DL, get(Hexagon::TFR), DestReg).addReg(SrcReg);
396     return;
397   }
398   if (Hexagon::DoubleRegsRegClass.contains(SrcReg, DestReg)) {
399     BuildMI(MBB, I, DL, get(Hexagon::TFR64), DestReg).addReg(SrcReg);
400     return;
401   }
402   if (Hexagon::PredRegsRegClass.contains(SrcReg, DestReg)) {
403     // Map Pd = Ps to Pd = or(Ps, Ps).
404     BuildMI(MBB, I, DL, get(Hexagon::OR_pp),
405             DestReg).addReg(SrcReg).addReg(SrcReg);
406     return;
407   }
408   if (Hexagon::DoubleRegsRegClass.contains(DestReg) &&
409       Hexagon::IntRegsRegClass.contains(SrcReg)) {
410     // We can have an overlap between single and double reg: r1:0 = r0.
411     if(SrcReg == RI.getSubReg(DestReg, Hexagon::subreg_loreg)) {
412         // r1:0 = r0
413         BuildMI(MBB, I, DL, get(Hexagon::TFRI), (RI.getSubReg(DestReg,
414                 Hexagon::subreg_hireg))).addImm(0);
415     } else {
416         // r1:0 = r1 or no overlap.
417         BuildMI(MBB, I, DL, get(Hexagon::TFR), (RI.getSubReg(DestReg,
418                 Hexagon::subreg_loreg))).addReg(SrcReg);
419         BuildMI(MBB, I, DL, get(Hexagon::TFRI), (RI.getSubReg(DestReg,
420                 Hexagon::subreg_hireg))).addImm(0);
421     }
422     return;
423   }
424   if (Hexagon::CRRegsRegClass.contains(DestReg) &&
425       Hexagon::IntRegsRegClass.contains(SrcReg)) {
426     BuildMI(MBB, I, DL, get(Hexagon::TFCR), DestReg).addReg(SrcReg);
427     return;
428   }
429   if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
430       Hexagon::IntRegsRegClass.contains(DestReg)) {
431     BuildMI(MBB, I, DL, get(Hexagon::TFR_RsPd), DestReg).
432       addReg(SrcReg, getKillRegState(KillSrc));
433     return;
434   }
435   if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
436       Hexagon::PredRegsRegClass.contains(DestReg)) {
437     BuildMI(MBB, I, DL, get(Hexagon::TFR_PdRs), DestReg).
438       addReg(SrcReg, getKillRegState(KillSrc));
439     return;
440   }
441
442   llvm_unreachable("Unimplemented");
443 }
444
445
446 void HexagonInstrInfo::
447 storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
448                     unsigned SrcReg, bool isKill, int FI,
449                     const TargetRegisterClass *RC,
450                     const TargetRegisterInfo *TRI) const {
451
452   DebugLoc DL = MBB.findDebugLoc(I);
453   MachineFunction &MF = *MBB.getParent();
454   MachineFrameInfo &MFI = *MF.getFrameInfo();
455   unsigned Align = MFI.getObjectAlignment(FI);
456
457   MachineMemOperand *MMO =
458       MF.getMachineMemOperand(
459                       MachinePointerInfo(PseudoSourceValue::getFixedStack(FI)),
460                       MachineMemOperand::MOStore,
461                       MFI.getObjectSize(FI),
462                       Align);
463
464   if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
465     BuildMI(MBB, I, DL, get(Hexagon::STriw))
466           .addFrameIndex(FI).addImm(0)
467           .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
468   } else if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC)) {
469     BuildMI(MBB, I, DL, get(Hexagon::STrid))
470           .addFrameIndex(FI).addImm(0)
471           .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
472   } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
473     BuildMI(MBB, I, DL, get(Hexagon::STriw_pred))
474           .addFrameIndex(FI).addImm(0)
475           .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
476   } else {
477     llvm_unreachable("Unimplemented");
478   }
479 }
480
481
482 void HexagonInstrInfo::storeRegToAddr(
483                                  MachineFunction &MF, unsigned SrcReg,
484                                  bool isKill,
485                                  SmallVectorImpl<MachineOperand> &Addr,
486                                  const TargetRegisterClass *RC,
487                                  SmallVectorImpl<MachineInstr*> &NewMIs) const
488 {
489   llvm_unreachable("Unimplemented");
490 }
491
492
493 void HexagonInstrInfo::
494 loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
495                      unsigned DestReg, int FI,
496                      const TargetRegisterClass *RC,
497                      const TargetRegisterInfo *TRI) const {
498   DebugLoc DL = MBB.findDebugLoc(I);
499   MachineFunction &MF = *MBB.getParent();
500   MachineFrameInfo &MFI = *MF.getFrameInfo();
501   unsigned Align = MFI.getObjectAlignment(FI);
502
503   MachineMemOperand *MMO =
504       MF.getMachineMemOperand(
505                       MachinePointerInfo(PseudoSourceValue::getFixedStack(FI)),
506                       MachineMemOperand::MOLoad,
507                       MFI.getObjectSize(FI),
508                       Align);
509   if (RC == &Hexagon::IntRegsRegClass) {
510     BuildMI(MBB, I, DL, get(Hexagon::LDriw), DestReg)
511           .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
512   } else if (RC == &Hexagon::DoubleRegsRegClass) {
513     BuildMI(MBB, I, DL, get(Hexagon::LDrid), DestReg)
514           .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
515   } else if (RC == &Hexagon::PredRegsRegClass) {
516     BuildMI(MBB, I, DL, get(Hexagon::LDriw_pred), DestReg)
517           .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
518   } else {
519     llvm_unreachable("Can't store this register to stack slot");
520   }
521 }
522
523
524 void HexagonInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
525                                         SmallVectorImpl<MachineOperand> &Addr,
526                                         const TargetRegisterClass *RC,
527                                  SmallVectorImpl<MachineInstr*> &NewMIs) const {
528   llvm_unreachable("Unimplemented");
529 }
530
531
532 MachineInstr *HexagonInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
533                                                     MachineInstr* MI,
534                                           const SmallVectorImpl<unsigned> &Ops,
535                                                     int FI) const {
536   // Hexagon_TODO: Implement.
537   return(0);
538 }
539
540 MachineInstr*
541 HexagonInstrInfo::emitFrameIndexDebugValue(MachineFunction &MF,
542                                            int FrameIx, uint64_t Offset,
543                                            const MDNode *MDPtr,
544                                            DebugLoc DL) const {
545   MachineInstrBuilder MIB = BuildMI(MF, DL, get(Hexagon::DBG_VALUE))
546     .addImm(0).addImm(Offset).addMetadata(MDPtr);
547   return &*MIB;
548 }
549
550 unsigned HexagonInstrInfo::createVR(MachineFunction* MF, MVT VT) const {
551
552   MachineRegisterInfo &RegInfo = MF->getRegInfo();
553   const TargetRegisterClass *TRC;
554   if (VT == MVT::i1) {
555     TRC = &Hexagon::PredRegsRegClass;
556   } else if (VT == MVT::i32 || VT == MVT::f32) {
557     TRC = &Hexagon::IntRegsRegClass;
558   } else if (VT == MVT::i64 || VT == MVT::f64) {
559     TRC = &Hexagon::DoubleRegsRegClass;
560   } else {
561     llvm_unreachable("Cannot handle this register class");
562   }
563
564   unsigned NewReg = RegInfo.createVirtualRegister(TRC);
565   return NewReg;
566 }
567
568 bool HexagonInstrInfo::isExtendable(const MachineInstr *MI) const {
569   // Constant extenders are allowed only for V4 and above.
570   if (!Subtarget.hasV4TOps())
571     return false;
572
573   const MCInstrDesc &MID = MI->getDesc();
574   const uint64_t F = MID.TSFlags;
575   if ((F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask)
576     return true;
577
578   // TODO: This is largely obsolete now. Will need to be removed
579   // in consecutive patches.
580   switch(MI->getOpcode()) {
581     // TFR_FI Remains a special case.
582     case Hexagon::TFR_FI:
583       return true;
584     default:
585       return false;
586   }
587   return  false;
588 }
589
590 // This returns true in two cases:
591 // - The OP code itself indicates that this is an extended instruction.
592 // - One of MOs has been marked with HMOTF_ConstExtended flag.
593 bool HexagonInstrInfo::isExtended(const MachineInstr *MI) const {
594   // First check if this is permanently extended op code.
595   const uint64_t F = MI->getDesc().TSFlags;
596   if ((F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask)
597     return true;
598   // Use MO operand flags to determine if one of MI's operands
599   // has HMOTF_ConstExtended flag set.
600   for (MachineInstr::const_mop_iterator I = MI->operands_begin(),
601        E = MI->operands_end(); I != E; ++I) {
602     if (I->getTargetFlags() && HexagonII::HMOTF_ConstExtended)
603       return true;
604   }
605   return  false;
606 }
607
608 bool HexagonInstrInfo::isNewValueJump(const MachineInstr *MI) const {
609   switch (MI->getOpcode()) {
610     default: return false;
611     // JMP_EQri
612     case Hexagon::JMP_EQriPt_nv_V4:
613     case Hexagon::JMP_EQriPnt_nv_V4:
614     case Hexagon::JMP_EQriNotPt_nv_V4:
615     case Hexagon::JMP_EQriNotPnt_nv_V4:
616     case Hexagon::JMP_EQriPt_ie_nv_V4:
617     case Hexagon::JMP_EQriPnt_ie_nv_V4:
618     case Hexagon::JMP_EQriNotPt_ie_nv_V4:
619     case Hexagon::JMP_EQriNotPnt_ie_nv_V4:
620
621     // JMP_EQri - with -1
622     case Hexagon::JMP_EQriPtneg_nv_V4:
623     case Hexagon::JMP_EQriPntneg_nv_V4:
624     case Hexagon::JMP_EQriNotPtneg_nv_V4:
625     case Hexagon::JMP_EQriNotPntneg_nv_V4:
626     case Hexagon::JMP_EQriPtneg_ie_nv_V4:
627     case Hexagon::JMP_EQriPntneg_ie_nv_V4:
628     case Hexagon::JMP_EQriNotPtneg_ie_nv_V4:
629     case Hexagon::JMP_EQriNotPntneg_ie_nv_V4:
630
631     // JMP_EQrr
632     case Hexagon::JMP_EQrrPt_nv_V4:
633     case Hexagon::JMP_EQrrPnt_nv_V4:
634     case Hexagon::JMP_EQrrNotPt_nv_V4:
635     case Hexagon::JMP_EQrrNotPnt_nv_V4:
636     case Hexagon::JMP_EQrrPt_ie_nv_V4:
637     case Hexagon::JMP_EQrrPnt_ie_nv_V4:
638     case Hexagon::JMP_EQrrNotPt_ie_nv_V4:
639     case Hexagon::JMP_EQrrNotPnt_ie_nv_V4:
640
641     // JMP_GTri
642     case Hexagon::JMP_GTriPt_nv_V4:
643     case Hexagon::JMP_GTriPnt_nv_V4:
644     case Hexagon::JMP_GTriNotPt_nv_V4:
645     case Hexagon::JMP_GTriNotPnt_nv_V4:
646     case Hexagon::JMP_GTriPt_ie_nv_V4:
647     case Hexagon::JMP_GTriPnt_ie_nv_V4:
648     case Hexagon::JMP_GTriNotPt_ie_nv_V4:
649     case Hexagon::JMP_GTriNotPnt_ie_nv_V4:
650
651     // JMP_GTri - with -1
652     case Hexagon::JMP_GTriPtneg_nv_V4:
653     case Hexagon::JMP_GTriPntneg_nv_V4:
654     case Hexagon::JMP_GTriNotPtneg_nv_V4:
655     case Hexagon::JMP_GTriNotPntneg_nv_V4:
656     case Hexagon::JMP_GTriPtneg_ie_nv_V4:
657     case Hexagon::JMP_GTriPntneg_ie_nv_V4:
658     case Hexagon::JMP_GTriNotPtneg_ie_nv_V4:
659     case Hexagon::JMP_GTriNotPntneg_ie_nv_V4:
660
661     // JMP_GTrr
662     case Hexagon::JMP_GTrrPt_nv_V4:
663     case Hexagon::JMP_GTrrPnt_nv_V4:
664     case Hexagon::JMP_GTrrNotPt_nv_V4:
665     case Hexagon::JMP_GTrrNotPnt_nv_V4:
666     case Hexagon::JMP_GTrrPt_ie_nv_V4:
667     case Hexagon::JMP_GTrrPnt_ie_nv_V4:
668     case Hexagon::JMP_GTrrNotPt_ie_nv_V4:
669     case Hexagon::JMP_GTrrNotPnt_ie_nv_V4:
670
671     // JMP_GTrrdn
672     case Hexagon::JMP_GTrrdnPt_nv_V4:
673     case Hexagon::JMP_GTrrdnPnt_nv_V4:
674     case Hexagon::JMP_GTrrdnNotPt_nv_V4:
675     case Hexagon::JMP_GTrrdnNotPnt_nv_V4:
676     case Hexagon::JMP_GTrrdnPt_ie_nv_V4:
677     case Hexagon::JMP_GTrrdnPnt_ie_nv_V4:
678     case Hexagon::JMP_GTrrdnNotPt_ie_nv_V4:
679     case Hexagon::JMP_GTrrdnNotPnt_ie_nv_V4:
680
681     // JMP_GTUri
682     case Hexagon::JMP_GTUriPt_nv_V4:
683     case Hexagon::JMP_GTUriPnt_nv_V4:
684     case Hexagon::JMP_GTUriNotPt_nv_V4:
685     case Hexagon::JMP_GTUriNotPnt_nv_V4:
686     case Hexagon::JMP_GTUriPt_ie_nv_V4:
687     case Hexagon::JMP_GTUriPnt_ie_nv_V4:
688     case Hexagon::JMP_GTUriNotPt_ie_nv_V4:
689     case Hexagon::JMP_GTUriNotPnt_ie_nv_V4:
690
691     // JMP_GTUrr
692     case Hexagon::JMP_GTUrrPt_nv_V4:
693     case Hexagon::JMP_GTUrrPnt_nv_V4:
694     case Hexagon::JMP_GTUrrNotPt_nv_V4:
695     case Hexagon::JMP_GTUrrNotPnt_nv_V4:
696     case Hexagon::JMP_GTUrrPt_ie_nv_V4:
697     case Hexagon::JMP_GTUrrPnt_ie_nv_V4:
698     case Hexagon::JMP_GTUrrNotPt_ie_nv_V4:
699     case Hexagon::JMP_GTUrrNotPnt_ie_nv_V4:
700
701     // JMP_GTUrrdn
702     case Hexagon::JMP_GTUrrdnPt_nv_V4:
703     case Hexagon::JMP_GTUrrdnPnt_nv_V4:
704     case Hexagon::JMP_GTUrrdnNotPt_nv_V4:
705     case Hexagon::JMP_GTUrrdnNotPnt_nv_V4:
706     case Hexagon::JMP_GTUrrdnPt_ie_nv_V4:
707     case Hexagon::JMP_GTUrrdnPnt_ie_nv_V4:
708     case Hexagon::JMP_GTUrrdnNotPt_ie_nv_V4:
709     case Hexagon::JMP_GTUrrdnNotPnt_ie_nv_V4:
710       return true;
711   }
712 }
713
714 bool HexagonInstrInfo::isNewValueStore(const MachineInstr *MI) const {
715   switch (MI->getOpcode()) {
716     default: return false;
717     // Store Byte
718     case Hexagon::STrib_nv_V4:
719     case Hexagon::STrib_indexed_nv_V4:
720     case Hexagon::STrib_indexed_shl_nv_V4:
721     case Hexagon::STrib_shl_nv_V4:
722     case Hexagon::STb_GP_nv_V4:
723     case Hexagon::POST_STbri_nv_V4:
724     case Hexagon::STrib_cPt_nv_V4:
725     case Hexagon::STrib_cdnPt_nv_V4:
726     case Hexagon::STrib_cNotPt_nv_V4:
727     case Hexagon::STrib_cdnNotPt_nv_V4:
728     case Hexagon::STrib_indexed_cPt_nv_V4:
729     case Hexagon::STrib_indexed_cdnPt_nv_V4:
730     case Hexagon::STrib_indexed_cNotPt_nv_V4:
731     case Hexagon::STrib_indexed_cdnNotPt_nv_V4:
732     case Hexagon::STrib_indexed_shl_cPt_nv_V4:
733     case Hexagon::STrib_indexed_shl_cdnPt_nv_V4:
734     case Hexagon::STrib_indexed_shl_cNotPt_nv_V4:
735     case Hexagon::STrib_indexed_shl_cdnNotPt_nv_V4:
736     case Hexagon::POST_STbri_cPt_nv_V4:
737     case Hexagon::POST_STbri_cdnPt_nv_V4:
738     case Hexagon::POST_STbri_cNotPt_nv_V4:
739     case Hexagon::POST_STbri_cdnNotPt_nv_V4:
740     case Hexagon::STb_GP_cPt_nv_V4:
741     case Hexagon::STb_GP_cNotPt_nv_V4:
742     case Hexagon::STb_GP_cdnPt_nv_V4:
743     case Hexagon::STb_GP_cdnNotPt_nv_V4:
744     case Hexagon::STrib_abs_nv_V4:
745     case Hexagon::STrib_abs_cPt_nv_V4:
746     case Hexagon::STrib_abs_cdnPt_nv_V4:
747     case Hexagon::STrib_abs_cNotPt_nv_V4:
748     case Hexagon::STrib_abs_cdnNotPt_nv_V4:
749
750     // Store Halfword
751     case Hexagon::STrih_nv_V4:
752     case Hexagon::STrih_indexed_nv_V4:
753     case Hexagon::STrih_indexed_shl_nv_V4:
754     case Hexagon::STrih_shl_nv_V4:
755     case Hexagon::STh_GP_nv_V4:
756     case Hexagon::POST_SThri_nv_V4:
757     case Hexagon::STrih_cPt_nv_V4:
758     case Hexagon::STrih_cdnPt_nv_V4:
759     case Hexagon::STrih_cNotPt_nv_V4:
760     case Hexagon::STrih_cdnNotPt_nv_V4:
761     case Hexagon::STrih_indexed_cPt_nv_V4:
762     case Hexagon::STrih_indexed_cdnPt_nv_V4:
763     case Hexagon::STrih_indexed_cNotPt_nv_V4:
764     case Hexagon::STrih_indexed_cdnNotPt_nv_V4:
765     case Hexagon::STrih_indexed_shl_cPt_nv_V4:
766     case Hexagon::STrih_indexed_shl_cdnPt_nv_V4:
767     case Hexagon::STrih_indexed_shl_cNotPt_nv_V4:
768     case Hexagon::STrih_indexed_shl_cdnNotPt_nv_V4:
769     case Hexagon::POST_SThri_cPt_nv_V4:
770     case Hexagon::POST_SThri_cdnPt_nv_V4:
771     case Hexagon::POST_SThri_cNotPt_nv_V4:
772     case Hexagon::POST_SThri_cdnNotPt_nv_V4:
773     case Hexagon::STh_GP_cPt_nv_V4:
774     case Hexagon::STh_GP_cNotPt_nv_V4:
775     case Hexagon::STh_GP_cdnPt_nv_V4:
776     case Hexagon::STh_GP_cdnNotPt_nv_V4:
777     case Hexagon::STrih_abs_nv_V4:
778     case Hexagon::STrih_abs_cPt_nv_V4:
779     case Hexagon::STrih_abs_cdnPt_nv_V4:
780     case Hexagon::STrih_abs_cNotPt_nv_V4:
781     case Hexagon::STrih_abs_cdnNotPt_nv_V4:
782
783     // Store Word
784     case Hexagon::STriw_nv_V4:
785     case Hexagon::STriw_indexed_nv_V4:
786     case Hexagon::STriw_indexed_shl_nv_V4:
787     case Hexagon::STriw_shl_nv_V4:
788     case Hexagon::STw_GP_nv_V4:
789     case Hexagon::POST_STwri_nv_V4:
790     case Hexagon::STriw_cPt_nv_V4:
791     case Hexagon::STriw_cdnPt_nv_V4:
792     case Hexagon::STriw_cNotPt_nv_V4:
793     case Hexagon::STriw_cdnNotPt_nv_V4:
794     case Hexagon::STriw_indexed_cPt_nv_V4:
795     case Hexagon::STriw_indexed_cdnPt_nv_V4:
796     case Hexagon::STriw_indexed_cNotPt_nv_V4:
797     case Hexagon::STriw_indexed_cdnNotPt_nv_V4:
798     case Hexagon::STriw_indexed_shl_cPt_nv_V4:
799     case Hexagon::STriw_indexed_shl_cdnPt_nv_V4:
800     case Hexagon::STriw_indexed_shl_cNotPt_nv_V4:
801     case Hexagon::STriw_indexed_shl_cdnNotPt_nv_V4:
802     case Hexagon::POST_STwri_cPt_nv_V4:
803     case Hexagon::POST_STwri_cdnPt_nv_V4:
804     case Hexagon::POST_STwri_cNotPt_nv_V4:
805     case Hexagon::POST_STwri_cdnNotPt_nv_V4:
806     case Hexagon::STw_GP_cPt_nv_V4:
807     case Hexagon::STw_GP_cNotPt_nv_V4:
808     case Hexagon::STw_GP_cdnPt_nv_V4:
809     case Hexagon::STw_GP_cdnNotPt_nv_V4:
810     case Hexagon::STriw_abs_nv_V4:
811     case Hexagon::STriw_abs_cPt_nv_V4:
812     case Hexagon::STriw_abs_cdnPt_nv_V4:
813     case Hexagon::STriw_abs_cNotPt_nv_V4:
814     case Hexagon::STriw_abs_cdnNotPt_nv_V4:
815       return true;
816   }
817 }
818
819 bool HexagonInstrInfo::isPostIncrement (const MachineInstr* MI) const {
820   switch (MI->getOpcode())
821   {
822     default: return false;
823     // Load Byte
824     case Hexagon::POST_LDrib:
825     case Hexagon::POST_LDrib_cPt:
826     case Hexagon::POST_LDrib_cNotPt:
827     case Hexagon::POST_LDrib_cdnPt_V4:
828     case Hexagon::POST_LDrib_cdnNotPt_V4:
829
830     // Load unsigned byte
831     case Hexagon::POST_LDriub:
832     case Hexagon::POST_LDriub_cPt:
833     case Hexagon::POST_LDriub_cNotPt:
834     case Hexagon::POST_LDriub_cdnPt_V4:
835     case Hexagon::POST_LDriub_cdnNotPt_V4:
836
837     // Load halfword
838     case Hexagon::POST_LDrih:
839     case Hexagon::POST_LDrih_cPt:
840     case Hexagon::POST_LDrih_cNotPt:
841     case Hexagon::POST_LDrih_cdnPt_V4:
842     case Hexagon::POST_LDrih_cdnNotPt_V4:
843
844     // Load unsigned halfword
845     case Hexagon::POST_LDriuh:
846     case Hexagon::POST_LDriuh_cPt:
847     case Hexagon::POST_LDriuh_cNotPt:
848     case Hexagon::POST_LDriuh_cdnPt_V4:
849     case Hexagon::POST_LDriuh_cdnNotPt_V4:
850
851     // Load word
852     case Hexagon::POST_LDriw:
853     case Hexagon::POST_LDriw_cPt:
854     case Hexagon::POST_LDriw_cNotPt:
855     case Hexagon::POST_LDriw_cdnPt_V4:
856     case Hexagon::POST_LDriw_cdnNotPt_V4:
857
858     // Load double word
859     case Hexagon::POST_LDrid:
860     case Hexagon::POST_LDrid_cPt:
861     case Hexagon::POST_LDrid_cNotPt:
862     case Hexagon::POST_LDrid_cdnPt_V4:
863     case Hexagon::POST_LDrid_cdnNotPt_V4:
864
865     // Store byte
866     case Hexagon::POST_STbri:
867     case Hexagon::POST_STbri_cPt:
868     case Hexagon::POST_STbri_cNotPt:
869     case Hexagon::POST_STbri_cdnPt_V4:
870     case Hexagon::POST_STbri_cdnNotPt_V4:
871
872     // Store halfword
873     case Hexagon::POST_SThri:
874     case Hexagon::POST_SThri_cPt:
875     case Hexagon::POST_SThri_cNotPt:
876     case Hexagon::POST_SThri_cdnPt_V4:
877     case Hexagon::POST_SThri_cdnNotPt_V4:
878
879     // Store word
880     case Hexagon::POST_STwri:
881     case Hexagon::POST_STwri_cPt:
882     case Hexagon::POST_STwri_cNotPt:
883     case Hexagon::POST_STwri_cdnPt_V4:
884     case Hexagon::POST_STwri_cdnNotPt_V4:
885
886     // Store double word
887     case Hexagon::POST_STdri:
888     case Hexagon::POST_STdri_cPt:
889     case Hexagon::POST_STdri_cNotPt:
890     case Hexagon::POST_STdri_cdnPt_V4:
891     case Hexagon::POST_STdri_cdnNotPt_V4:
892       return true;
893   }
894 }
895
896 bool HexagonInstrInfo::isNewValueInst(const MachineInstr *MI) const {
897   if (isNewValueJump(MI))
898     return true;
899
900   if (isNewValueStore(MI))
901     return true;
902
903   return false;
904 }
905
906 bool HexagonInstrInfo::isSaveCalleeSavedRegsCall(const MachineInstr *MI) const {
907   return MI->getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4;
908 }
909
910 bool HexagonInstrInfo::isPredicable(MachineInstr *MI) const {
911   bool isPred = MI->getDesc().isPredicable();
912
913   if (!isPred)
914     return false;
915
916   const int Opc = MI->getOpcode();
917
918   switch(Opc) {
919   case Hexagon::TFRI:
920     return isInt<12>(MI->getOperand(1).getImm());
921
922   case Hexagon::STrid:
923   case Hexagon::STrid_indexed:
924     return isShiftedUInt<6,3>(MI->getOperand(1).getImm());
925
926   case Hexagon::STriw:
927   case Hexagon::STriw_indexed:
928   case Hexagon::STriw_nv_V4:
929     return isShiftedUInt<6,2>(MI->getOperand(1).getImm());
930
931   case Hexagon::STrih:
932   case Hexagon::STrih_indexed:
933   case Hexagon::STrih_nv_V4:
934     return isShiftedUInt<6,1>(MI->getOperand(1).getImm());
935
936   case Hexagon::STrib:
937   case Hexagon::STrib_indexed:
938   case Hexagon::STrib_nv_V4:
939     return isUInt<6>(MI->getOperand(1).getImm());
940
941   case Hexagon::LDrid:
942   case Hexagon::LDrid_indexed:
943     return isShiftedUInt<6,3>(MI->getOperand(2).getImm());
944
945   case Hexagon::LDriw:
946   case Hexagon::LDriw_indexed:
947     return isShiftedUInt<6,2>(MI->getOperand(2).getImm());
948
949   case Hexagon::LDrih:
950   case Hexagon::LDriuh:
951   case Hexagon::LDrih_indexed:
952   case Hexagon::LDriuh_indexed:
953     return isShiftedUInt<6,1>(MI->getOperand(2).getImm());
954
955   case Hexagon::LDrib:
956   case Hexagon::LDriub:
957   case Hexagon::LDrib_indexed:
958   case Hexagon::LDriub_indexed:
959     return isUInt<6>(MI->getOperand(2).getImm());
960
961   case Hexagon::POST_LDrid:
962     return isShiftedInt<4,3>(MI->getOperand(3).getImm());
963
964   case Hexagon::POST_LDriw:
965     return isShiftedInt<4,2>(MI->getOperand(3).getImm());
966
967   case Hexagon::POST_LDrih:
968   case Hexagon::POST_LDriuh:
969     return isShiftedInt<4,1>(MI->getOperand(3).getImm());
970
971   case Hexagon::POST_LDrib:
972   case Hexagon::POST_LDriub:
973     return isInt<4>(MI->getOperand(3).getImm());
974
975   case Hexagon::STrib_imm_V4:
976   case Hexagon::STrih_imm_V4:
977   case Hexagon::STriw_imm_V4:
978     return (isUInt<6>(MI->getOperand(1).getImm()) &&
979             isInt<6>(MI->getOperand(2).getImm()));
980
981   case Hexagon::ADD_ri:
982     return isInt<8>(MI->getOperand(2).getImm());
983
984   case Hexagon::ASLH:
985   case Hexagon::ASRH:
986   case Hexagon::SXTB:
987   case Hexagon::SXTH:
988   case Hexagon::ZXTB:
989   case Hexagon::ZXTH:
990     return Subtarget.hasV4TOps();
991
992   case Hexagon::JMPR:
993     return false;
994   }
995
996   return true;
997 }
998
999 // This function performs the following inversiones:
1000 //
1001 //  cPt    ---> cNotPt
1002 //  cNotPt ---> cPt
1003 //
1004 // however, these inversiones are NOT included:
1005 //
1006 //  cdnPt      -X-> cdnNotPt
1007 //  cdnNotPt   -X-> cdnPt
1008 //  cPt_nv     -X-> cNotPt_nv (new value stores)
1009 //  cNotPt_nv  -X-> cPt_nv    (new value stores)
1010 //
1011 // because only the following transformations are allowed:
1012 //
1013 //  cNotPt  ---> cdnNotPt
1014 //  cPt     ---> cdnPt
1015 //  cNotPt  ---> cNotPt_nv
1016 //  cPt     ---> cPt_nv
1017 unsigned HexagonInstrInfo::getInvertedPredicatedOpcode(const int Opc) const {
1018   switch(Opc) {
1019     default: llvm_unreachable("Unexpected predicated instruction");
1020     case Hexagon::TFR_cPt:
1021       return Hexagon::TFR_cNotPt;
1022     case Hexagon::TFR_cNotPt:
1023       return Hexagon::TFR_cPt;
1024
1025     case Hexagon::TFRI_cPt:
1026       return Hexagon::TFRI_cNotPt;
1027     case Hexagon::TFRI_cNotPt:
1028       return Hexagon::TFRI_cPt;
1029
1030     case Hexagon::JMP_c:
1031       return Hexagon::JMP_cNot;
1032     case Hexagon::JMP_cNot:
1033       return Hexagon::JMP_c;
1034
1035     case Hexagon::ADD_ri_cPt:
1036       return Hexagon::ADD_ri_cNotPt;
1037     case Hexagon::ADD_ri_cNotPt:
1038       return Hexagon::ADD_ri_cPt;
1039
1040     case Hexagon::ADD_rr_cPt:
1041       return Hexagon::ADD_rr_cNotPt;
1042     case Hexagon::ADD_rr_cNotPt:
1043       return Hexagon::ADD_rr_cPt;
1044
1045     case Hexagon::XOR_rr_cPt:
1046       return Hexagon::XOR_rr_cNotPt;
1047     case Hexagon::XOR_rr_cNotPt:
1048       return Hexagon::XOR_rr_cPt;
1049
1050     case Hexagon::AND_rr_cPt:
1051       return Hexagon::AND_rr_cNotPt;
1052     case Hexagon::AND_rr_cNotPt:
1053       return Hexagon::AND_rr_cPt;
1054
1055     case Hexagon::OR_rr_cPt:
1056       return Hexagon::OR_rr_cNotPt;
1057     case Hexagon::OR_rr_cNotPt:
1058       return Hexagon::OR_rr_cPt;
1059
1060     case Hexagon::SUB_rr_cPt:
1061       return Hexagon::SUB_rr_cNotPt;
1062     case Hexagon::SUB_rr_cNotPt:
1063       return Hexagon::SUB_rr_cPt;
1064
1065     case Hexagon::COMBINE_rr_cPt:
1066       return Hexagon::COMBINE_rr_cNotPt;
1067     case Hexagon::COMBINE_rr_cNotPt:
1068       return Hexagon::COMBINE_rr_cPt;
1069
1070     case Hexagon::ASLH_cPt_V4:
1071       return Hexagon::ASLH_cNotPt_V4;
1072     case Hexagon::ASLH_cNotPt_V4:
1073       return Hexagon::ASLH_cPt_V4;
1074
1075     case Hexagon::ASRH_cPt_V4:
1076       return Hexagon::ASRH_cNotPt_V4;
1077     case Hexagon::ASRH_cNotPt_V4:
1078       return Hexagon::ASRH_cPt_V4;
1079
1080     case Hexagon::SXTB_cPt_V4:
1081       return Hexagon::SXTB_cNotPt_V4;
1082     case Hexagon::SXTB_cNotPt_V4:
1083       return Hexagon::SXTB_cPt_V4;
1084
1085     case Hexagon::SXTH_cPt_V4:
1086       return Hexagon::SXTH_cNotPt_V4;
1087     case Hexagon::SXTH_cNotPt_V4:
1088       return Hexagon::SXTH_cPt_V4;
1089
1090     case Hexagon::ZXTB_cPt_V4:
1091       return Hexagon::ZXTB_cNotPt_V4;
1092     case Hexagon::ZXTB_cNotPt_V4:
1093       return Hexagon::ZXTB_cPt_V4;
1094
1095     case Hexagon::ZXTH_cPt_V4:
1096       return Hexagon::ZXTH_cNotPt_V4;
1097     case Hexagon::ZXTH_cNotPt_V4:
1098       return Hexagon::ZXTH_cPt_V4;
1099
1100
1101     case Hexagon::JMPR_cPt:
1102       return Hexagon::JMPR_cNotPt;
1103     case Hexagon::JMPR_cNotPt:
1104       return Hexagon::JMPR_cPt;
1105
1106   // V4 indexed+scaled load.
1107     case Hexagon::LDrid_indexed_shl_cPt_V4:
1108       return Hexagon::LDrid_indexed_shl_cNotPt_V4;
1109     case Hexagon::LDrid_indexed_shl_cNotPt_V4:
1110       return Hexagon::LDrid_indexed_shl_cPt_V4;
1111
1112     case Hexagon::LDrib_indexed_shl_cPt_V4:
1113       return Hexagon::LDrib_indexed_shl_cNotPt_V4;
1114     case Hexagon::LDrib_indexed_shl_cNotPt_V4:
1115       return Hexagon::LDrib_indexed_shl_cPt_V4;
1116
1117     case Hexagon::LDriub_indexed_shl_cPt_V4:
1118       return Hexagon::LDriub_indexed_shl_cNotPt_V4;
1119     case Hexagon::LDriub_indexed_shl_cNotPt_V4:
1120       return Hexagon::LDriub_indexed_shl_cPt_V4;
1121
1122     case Hexagon::LDrih_indexed_shl_cPt_V4:
1123       return Hexagon::LDrih_indexed_shl_cNotPt_V4;
1124     case Hexagon::LDrih_indexed_shl_cNotPt_V4:
1125       return Hexagon::LDrih_indexed_shl_cPt_V4;
1126
1127     case Hexagon::LDriuh_indexed_shl_cPt_V4:
1128       return Hexagon::LDriuh_indexed_shl_cNotPt_V4;
1129     case Hexagon::LDriuh_indexed_shl_cNotPt_V4:
1130       return Hexagon::LDriuh_indexed_shl_cPt_V4;
1131
1132     case Hexagon::LDriw_indexed_shl_cPt_V4:
1133       return Hexagon::LDriw_indexed_shl_cNotPt_V4;
1134     case Hexagon::LDriw_indexed_shl_cNotPt_V4:
1135       return Hexagon::LDriw_indexed_shl_cPt_V4;
1136
1137     // Byte.
1138     case Hexagon::POST_STbri_cPt:
1139       return Hexagon::POST_STbri_cNotPt;
1140     case Hexagon::POST_STbri_cNotPt:
1141       return Hexagon::POST_STbri_cPt;
1142
1143     case Hexagon::STrib_cPt:
1144       return Hexagon::STrib_cNotPt;
1145     case Hexagon::STrib_cNotPt:
1146       return Hexagon::STrib_cPt;
1147
1148     case Hexagon::STrib_indexed_cPt:
1149       return Hexagon::STrib_indexed_cNotPt;
1150     case Hexagon::STrib_indexed_cNotPt:
1151       return Hexagon::STrib_indexed_cPt;
1152
1153     case Hexagon::STrib_imm_cPt_V4:
1154       return Hexagon::STrib_imm_cNotPt_V4;
1155     case Hexagon::STrib_imm_cNotPt_V4:
1156       return Hexagon::STrib_imm_cPt_V4;
1157
1158     case Hexagon::STrib_indexed_shl_cPt_V4:
1159       return Hexagon::STrib_indexed_shl_cNotPt_V4;
1160     case Hexagon::STrib_indexed_shl_cNotPt_V4:
1161       return Hexagon::STrib_indexed_shl_cPt_V4;
1162
1163   // Halfword.
1164     case Hexagon::POST_SThri_cPt:
1165       return Hexagon::POST_SThri_cNotPt;
1166     case Hexagon::POST_SThri_cNotPt:
1167       return Hexagon::POST_SThri_cPt;
1168
1169     case Hexagon::STrih_cPt:
1170       return Hexagon::STrih_cNotPt;
1171     case Hexagon::STrih_cNotPt:
1172       return Hexagon::STrih_cPt;
1173
1174     case Hexagon::STrih_indexed_cPt:
1175       return Hexagon::STrih_indexed_cNotPt;
1176     case Hexagon::STrih_indexed_cNotPt:
1177       return Hexagon::STrih_indexed_cPt;
1178
1179     case Hexagon::STrih_imm_cPt_V4:
1180       return Hexagon::STrih_imm_cNotPt_V4;
1181     case Hexagon::STrih_imm_cNotPt_V4:
1182       return Hexagon::STrih_imm_cPt_V4;
1183
1184     case Hexagon::STrih_indexed_shl_cPt_V4:
1185       return Hexagon::STrih_indexed_shl_cNotPt_V4;
1186     case Hexagon::STrih_indexed_shl_cNotPt_V4:
1187       return Hexagon::STrih_indexed_shl_cPt_V4;
1188
1189   // Word.
1190     case Hexagon::POST_STwri_cPt:
1191       return Hexagon::POST_STwri_cNotPt;
1192     case Hexagon::POST_STwri_cNotPt:
1193       return Hexagon::POST_STwri_cPt;
1194
1195     case Hexagon::STriw_cPt:
1196       return Hexagon::STriw_cNotPt;
1197     case Hexagon::STriw_cNotPt:
1198       return Hexagon::STriw_cPt;
1199
1200     case Hexagon::STriw_indexed_cPt:
1201       return Hexagon::STriw_indexed_cNotPt;
1202     case Hexagon::STriw_indexed_cNotPt:
1203       return Hexagon::STriw_indexed_cPt;
1204
1205     case Hexagon::STriw_indexed_shl_cPt_V4:
1206       return Hexagon::STriw_indexed_shl_cNotPt_V4;
1207     case Hexagon::STriw_indexed_shl_cNotPt_V4:
1208       return Hexagon::STriw_indexed_shl_cPt_V4;
1209
1210     case Hexagon::STriw_imm_cPt_V4:
1211       return Hexagon::STriw_imm_cNotPt_V4;
1212     case Hexagon::STriw_imm_cNotPt_V4:
1213       return Hexagon::STriw_imm_cPt_V4;
1214
1215   // Double word.
1216     case Hexagon::POST_STdri_cPt:
1217       return Hexagon::POST_STdri_cNotPt;
1218     case Hexagon::POST_STdri_cNotPt:
1219       return Hexagon::POST_STdri_cPt;
1220
1221     case Hexagon::STrid_cPt:
1222       return Hexagon::STrid_cNotPt;
1223     case Hexagon::STrid_cNotPt:
1224       return Hexagon::STrid_cPt;
1225
1226     case Hexagon::STrid_indexed_cPt:
1227       return Hexagon::STrid_indexed_cNotPt;
1228     case Hexagon::STrid_indexed_cNotPt:
1229       return Hexagon::STrid_indexed_cPt;
1230
1231     case Hexagon::STrid_indexed_shl_cPt_V4:
1232       return Hexagon::STrid_indexed_shl_cNotPt_V4;
1233     case Hexagon::STrid_indexed_shl_cNotPt_V4:
1234       return Hexagon::STrid_indexed_shl_cPt_V4;
1235
1236     // V4 Store to global address.
1237     case Hexagon::STd_GP_cPt_V4:
1238       return Hexagon::STd_GP_cNotPt_V4;
1239     case Hexagon::STd_GP_cNotPt_V4:
1240       return Hexagon::STd_GP_cPt_V4;
1241
1242     case Hexagon::STb_GP_cPt_V4:
1243       return Hexagon::STb_GP_cNotPt_V4;
1244     case Hexagon::STb_GP_cNotPt_V4:
1245       return Hexagon::STb_GP_cPt_V4;
1246
1247     case Hexagon::STh_GP_cPt_V4:
1248       return Hexagon::STh_GP_cNotPt_V4;
1249     case Hexagon::STh_GP_cNotPt_V4:
1250       return Hexagon::STh_GP_cPt_V4;
1251
1252     case Hexagon::STw_GP_cPt_V4:
1253       return Hexagon::STw_GP_cNotPt_V4;
1254     case Hexagon::STw_GP_cNotPt_V4:
1255       return Hexagon::STw_GP_cPt_V4;
1256
1257   // Load.
1258     case Hexagon::LDrid_cPt:
1259       return Hexagon::LDrid_cNotPt;
1260     case Hexagon::LDrid_cNotPt:
1261       return Hexagon::LDrid_cPt;
1262
1263     case Hexagon::LDriw_cPt:
1264       return Hexagon::LDriw_cNotPt;
1265     case Hexagon::LDriw_cNotPt:
1266       return Hexagon::LDriw_cPt;
1267
1268     case Hexagon::LDrih_cPt:
1269       return Hexagon::LDrih_cNotPt;
1270     case Hexagon::LDrih_cNotPt:
1271       return Hexagon::LDrih_cPt;
1272
1273     case Hexagon::LDriuh_cPt:
1274       return Hexagon::LDriuh_cNotPt;
1275     case Hexagon::LDriuh_cNotPt:
1276       return Hexagon::LDriuh_cPt;
1277
1278     case Hexagon::LDrib_cPt:
1279       return Hexagon::LDrib_cNotPt;
1280     case Hexagon::LDrib_cNotPt:
1281       return Hexagon::LDrib_cPt;
1282
1283     case Hexagon::LDriub_cPt:
1284       return Hexagon::LDriub_cNotPt;
1285     case Hexagon::LDriub_cNotPt:
1286       return Hexagon::LDriub_cPt;
1287
1288  // Load Indexed.
1289     case Hexagon::LDrid_indexed_cPt:
1290       return Hexagon::LDrid_indexed_cNotPt;
1291     case Hexagon::LDrid_indexed_cNotPt:
1292       return Hexagon::LDrid_indexed_cPt;
1293
1294     case Hexagon::LDriw_indexed_cPt:
1295       return Hexagon::LDriw_indexed_cNotPt;
1296     case Hexagon::LDriw_indexed_cNotPt:
1297       return Hexagon::LDriw_indexed_cPt;
1298
1299     case Hexagon::LDrih_indexed_cPt:
1300       return Hexagon::LDrih_indexed_cNotPt;
1301     case Hexagon::LDrih_indexed_cNotPt:
1302       return Hexagon::LDrih_indexed_cPt;
1303
1304     case Hexagon::LDriuh_indexed_cPt:
1305       return Hexagon::LDriuh_indexed_cNotPt;
1306     case Hexagon::LDriuh_indexed_cNotPt:
1307       return Hexagon::LDriuh_indexed_cPt;
1308
1309     case Hexagon::LDrib_indexed_cPt:
1310       return Hexagon::LDrib_indexed_cNotPt;
1311     case Hexagon::LDrib_indexed_cNotPt:
1312       return Hexagon::LDrib_indexed_cPt;
1313
1314     case Hexagon::LDriub_indexed_cPt:
1315       return Hexagon::LDriub_indexed_cNotPt;
1316     case Hexagon::LDriub_indexed_cNotPt:
1317       return Hexagon::LDriub_indexed_cPt;
1318
1319   // Post Inc Load.
1320     case Hexagon::POST_LDrid_cPt:
1321       return Hexagon::POST_LDrid_cNotPt;
1322     case Hexagon::POST_LDriw_cNotPt:
1323       return Hexagon::POST_LDriw_cPt;
1324
1325     case Hexagon::POST_LDrih_cPt:
1326       return Hexagon::POST_LDrih_cNotPt;
1327     case Hexagon::POST_LDrih_cNotPt:
1328       return Hexagon::POST_LDrih_cPt;
1329
1330     case Hexagon::POST_LDriuh_cPt:
1331       return Hexagon::POST_LDriuh_cNotPt;
1332     case Hexagon::POST_LDriuh_cNotPt:
1333       return Hexagon::POST_LDriuh_cPt;
1334
1335     case Hexagon::POST_LDrib_cPt:
1336       return Hexagon::POST_LDrib_cNotPt;
1337     case Hexagon::POST_LDrib_cNotPt:
1338       return Hexagon::POST_LDrib_cPt;
1339
1340     case Hexagon::POST_LDriub_cPt:
1341       return Hexagon::POST_LDriub_cNotPt;
1342     case Hexagon::POST_LDriub_cNotPt:
1343       return Hexagon::POST_LDriub_cPt;
1344
1345   // Dealloc_return.
1346     case Hexagon::DEALLOC_RET_cPt_V4:
1347       return Hexagon::DEALLOC_RET_cNotPt_V4;
1348     case Hexagon::DEALLOC_RET_cNotPt_V4:
1349       return Hexagon::DEALLOC_RET_cPt_V4;
1350
1351    // New Value Jump.
1352    // JMPEQ_ri - with -1.
1353     case Hexagon::JMP_EQriPtneg_nv_V4:
1354       return Hexagon::JMP_EQriNotPtneg_nv_V4;
1355     case Hexagon::JMP_EQriNotPtneg_nv_V4:
1356       return Hexagon::JMP_EQriPtneg_nv_V4;
1357
1358     case Hexagon::JMP_EQriPntneg_nv_V4:
1359       return Hexagon::JMP_EQriNotPntneg_nv_V4;
1360     case Hexagon::JMP_EQriNotPntneg_nv_V4:
1361       return Hexagon::JMP_EQriPntneg_nv_V4;
1362
1363    // JMPEQ_ri.
1364      case Hexagon::JMP_EQriPt_nv_V4:
1365       return Hexagon::JMP_EQriNotPt_nv_V4;
1366     case Hexagon::JMP_EQriNotPt_nv_V4:
1367       return Hexagon::JMP_EQriPt_nv_V4;
1368
1369      case Hexagon::JMP_EQriPnt_nv_V4:
1370       return Hexagon::JMP_EQriNotPnt_nv_V4;
1371     case Hexagon::JMP_EQriNotPnt_nv_V4:
1372       return Hexagon::JMP_EQriPnt_nv_V4;
1373
1374    // JMPEQ_rr.
1375      case Hexagon::JMP_EQrrPt_nv_V4:
1376       return Hexagon::JMP_EQrrNotPt_nv_V4;
1377     case Hexagon::JMP_EQrrNotPt_nv_V4:
1378       return Hexagon::JMP_EQrrPt_nv_V4;
1379
1380      case Hexagon::JMP_EQrrPnt_nv_V4:
1381       return Hexagon::JMP_EQrrNotPnt_nv_V4;
1382     case Hexagon::JMP_EQrrNotPnt_nv_V4:
1383       return Hexagon::JMP_EQrrPnt_nv_V4;
1384
1385    // JMPGT_ri - with -1.
1386     case Hexagon::JMP_GTriPtneg_nv_V4:
1387       return Hexagon::JMP_GTriNotPtneg_nv_V4;
1388     case Hexagon::JMP_GTriNotPtneg_nv_V4:
1389       return Hexagon::JMP_GTriPtneg_nv_V4;
1390
1391     case Hexagon::JMP_GTriPntneg_nv_V4:
1392       return Hexagon::JMP_GTriNotPntneg_nv_V4;
1393     case Hexagon::JMP_GTriNotPntneg_nv_V4:
1394       return Hexagon::JMP_GTriPntneg_nv_V4;
1395
1396    // JMPGT_ri.
1397      case Hexagon::JMP_GTriPt_nv_V4:
1398       return Hexagon::JMP_GTriNotPt_nv_V4;
1399     case Hexagon::JMP_GTriNotPt_nv_V4:
1400       return Hexagon::JMP_GTriPt_nv_V4;
1401
1402      case Hexagon::JMP_GTriPnt_nv_V4:
1403       return Hexagon::JMP_GTriNotPnt_nv_V4;
1404     case Hexagon::JMP_GTriNotPnt_nv_V4:
1405       return Hexagon::JMP_GTriPnt_nv_V4;
1406
1407    // JMPGT_rr.
1408      case Hexagon::JMP_GTrrPt_nv_V4:
1409       return Hexagon::JMP_GTrrNotPt_nv_V4;
1410     case Hexagon::JMP_GTrrNotPt_nv_V4:
1411       return Hexagon::JMP_GTrrPt_nv_V4;
1412
1413      case Hexagon::JMP_GTrrPnt_nv_V4:
1414       return Hexagon::JMP_GTrrNotPnt_nv_V4;
1415     case Hexagon::JMP_GTrrNotPnt_nv_V4:
1416       return Hexagon::JMP_GTrrPnt_nv_V4;
1417
1418    // JMPGT_rrdn.
1419      case Hexagon::JMP_GTrrdnPt_nv_V4:
1420       return Hexagon::JMP_GTrrdnNotPt_nv_V4;
1421     case Hexagon::JMP_GTrrdnNotPt_nv_V4:
1422       return Hexagon::JMP_GTrrdnPt_nv_V4;
1423
1424      case Hexagon::JMP_GTrrdnPnt_nv_V4:
1425       return Hexagon::JMP_GTrrdnNotPnt_nv_V4;
1426     case Hexagon::JMP_GTrrdnNotPnt_nv_V4:
1427       return Hexagon::JMP_GTrrdnPnt_nv_V4;
1428
1429    // JMPGTU_ri.
1430      case Hexagon::JMP_GTUriPt_nv_V4:
1431       return Hexagon::JMP_GTUriNotPt_nv_V4;
1432     case Hexagon::JMP_GTUriNotPt_nv_V4:
1433       return Hexagon::JMP_GTUriPt_nv_V4;
1434
1435      case Hexagon::JMP_GTUriPnt_nv_V4:
1436       return Hexagon::JMP_GTUriNotPnt_nv_V4;
1437     case Hexagon::JMP_GTUriNotPnt_nv_V4:
1438       return Hexagon::JMP_GTUriPnt_nv_V4;
1439
1440    // JMPGTU_rr.
1441      case Hexagon::JMP_GTUrrPt_nv_V4:
1442       return Hexagon::JMP_GTUrrNotPt_nv_V4;
1443     case Hexagon::JMP_GTUrrNotPt_nv_V4:
1444       return Hexagon::JMP_GTUrrPt_nv_V4;
1445
1446      case Hexagon::JMP_GTUrrPnt_nv_V4:
1447       return Hexagon::JMP_GTUrrNotPnt_nv_V4;
1448     case Hexagon::JMP_GTUrrNotPnt_nv_V4:
1449       return Hexagon::JMP_GTUrrPnt_nv_V4;
1450
1451    // JMPGTU_rrdn.
1452      case Hexagon::JMP_GTUrrdnPt_nv_V4:
1453       return Hexagon::JMP_GTUrrdnNotPt_nv_V4;
1454     case Hexagon::JMP_GTUrrdnNotPt_nv_V4:
1455       return Hexagon::JMP_GTUrrdnPt_nv_V4;
1456
1457      case Hexagon::JMP_GTUrrdnPnt_nv_V4:
1458       return Hexagon::JMP_GTUrrdnNotPnt_nv_V4;
1459     case Hexagon::JMP_GTUrrdnNotPnt_nv_V4:
1460       return Hexagon::JMP_GTUrrdnPnt_nv_V4;
1461   }
1462 }
1463
1464
1465 int HexagonInstrInfo::
1466 getMatchingCondBranchOpcode(int Opc, bool invertPredicate) const {
1467   enum Hexagon::PredSense inPredSense;
1468   inPredSense = invertPredicate ? Hexagon::PredSense_false :
1469                                   Hexagon::PredSense_true;
1470   int CondOpcode = Hexagon::getPredOpcode(Opc, inPredSense);
1471   if (CondOpcode >= 0) // Valid Conditional opcode/instruction
1472     return CondOpcode;
1473
1474   // This switch case will be removed once all the instructions have been
1475   // modified to use relation maps.
1476   switch(Opc) {
1477   case Hexagon::TFR:
1478     return !invertPredicate ? Hexagon::TFR_cPt :
1479                               Hexagon::TFR_cNotPt;
1480   case Hexagon::TFRI_f:
1481     return !invertPredicate ? Hexagon::TFRI_cPt_f :
1482                               Hexagon::TFRI_cNotPt_f;
1483   case Hexagon::TFRI:
1484     return !invertPredicate ? Hexagon::TFRI_cPt :
1485                               Hexagon::TFRI_cNotPt;
1486   case Hexagon::JMP:
1487     return !invertPredicate ? Hexagon::JMP_c :
1488                               Hexagon::JMP_cNot;
1489   case Hexagon::JMP_EQrrPt_nv_V4:
1490     return !invertPredicate ? Hexagon::JMP_EQrrPt_nv_V4 :
1491                               Hexagon::JMP_EQrrNotPt_nv_V4;
1492   case Hexagon::JMP_EQriPt_nv_V4:
1493     return !invertPredicate ? Hexagon::JMP_EQriPt_nv_V4 :
1494                               Hexagon::JMP_EQriNotPt_nv_V4;
1495   case Hexagon::COMBINE_rr:
1496     return !invertPredicate ? Hexagon::COMBINE_rr_cPt :
1497                               Hexagon::COMBINE_rr_cNotPt;
1498   case Hexagon::ASLH:
1499     return !invertPredicate ? Hexagon::ASLH_cPt_V4 :
1500                               Hexagon::ASLH_cNotPt_V4;
1501   case Hexagon::ASRH:
1502     return !invertPredicate ? Hexagon::ASRH_cPt_V4 :
1503                               Hexagon::ASRH_cNotPt_V4;
1504   case Hexagon::SXTB:
1505     return !invertPredicate ? Hexagon::SXTB_cPt_V4 :
1506                               Hexagon::SXTB_cNotPt_V4;
1507   case Hexagon::SXTH:
1508     return !invertPredicate ? Hexagon::SXTH_cPt_V4 :
1509                               Hexagon::SXTH_cNotPt_V4;
1510   case Hexagon::ZXTB:
1511     return !invertPredicate ? Hexagon::ZXTB_cPt_V4 :
1512                               Hexagon::ZXTB_cNotPt_V4;
1513   case Hexagon::ZXTH:
1514     return !invertPredicate ? Hexagon::ZXTH_cPt_V4 :
1515                               Hexagon::ZXTH_cNotPt_V4;
1516
1517   case Hexagon::JMPR:
1518     return !invertPredicate ? Hexagon::JMPR_cPt :
1519                               Hexagon::JMPR_cNotPt;
1520
1521   // V4 indexed+scaled load.
1522   case Hexagon::LDrid_indexed_shl_V4:
1523     return !invertPredicate ? Hexagon::LDrid_indexed_shl_cPt_V4 :
1524                               Hexagon::LDrid_indexed_shl_cNotPt_V4;
1525   case Hexagon::LDrib_indexed_shl_V4:
1526     return !invertPredicate ? Hexagon::LDrib_indexed_shl_cPt_V4 :
1527                               Hexagon::LDrib_indexed_shl_cNotPt_V4;
1528   case Hexagon::LDriub_indexed_shl_V4:
1529     return !invertPredicate ? Hexagon::LDriub_indexed_shl_cPt_V4 :
1530                               Hexagon::LDriub_indexed_shl_cNotPt_V4;
1531   case Hexagon::LDrih_indexed_shl_V4:
1532     return !invertPredicate ? Hexagon::LDrih_indexed_shl_cPt_V4 :
1533                               Hexagon::LDrih_indexed_shl_cNotPt_V4;
1534   case Hexagon::LDriuh_indexed_shl_V4:
1535     return !invertPredicate ? Hexagon::LDriuh_indexed_shl_cPt_V4 :
1536                               Hexagon::LDriuh_indexed_shl_cNotPt_V4;
1537   case Hexagon::LDriw_indexed_shl_V4:
1538     return !invertPredicate ? Hexagon::LDriw_indexed_shl_cPt_V4 :
1539                               Hexagon::LDriw_indexed_shl_cNotPt_V4;
1540
1541   // V4 Load from global address
1542   case Hexagon::LDd_GP_V4:
1543     return !invertPredicate ? Hexagon::LDd_GP_cPt_V4 :
1544                               Hexagon::LDd_GP_cNotPt_V4;
1545   case Hexagon::LDb_GP_V4:
1546     return !invertPredicate ? Hexagon::LDb_GP_cPt_V4 :
1547                               Hexagon::LDb_GP_cNotPt_V4;
1548   case Hexagon::LDub_GP_V4:
1549     return !invertPredicate ? Hexagon::LDub_GP_cPt_V4 :
1550                               Hexagon::LDub_GP_cNotPt_V4;
1551   case Hexagon::LDh_GP_V4:
1552     return !invertPredicate ? Hexagon::LDh_GP_cPt_V4 :
1553                               Hexagon::LDh_GP_cNotPt_V4;
1554   case Hexagon::LDuh_GP_V4:
1555     return !invertPredicate ? Hexagon::LDuh_GP_cPt_V4 :
1556                               Hexagon::LDuh_GP_cNotPt_V4;
1557   case Hexagon::LDw_GP_V4:
1558     return !invertPredicate ? Hexagon::LDw_GP_cPt_V4 :
1559                               Hexagon::LDw_GP_cNotPt_V4;
1560
1561     // Byte.
1562   case Hexagon::POST_STbri:
1563     return !invertPredicate ? Hexagon::POST_STbri_cPt :
1564                               Hexagon::POST_STbri_cNotPt;
1565   case Hexagon::STrib:
1566     return !invertPredicate ? Hexagon::STrib_cPt :
1567                               Hexagon::STrib_cNotPt;
1568   case Hexagon::STrib_indexed:
1569     return !invertPredicate ? Hexagon::STrib_indexed_cPt :
1570                               Hexagon::STrib_indexed_cNotPt;
1571   case Hexagon::STrib_imm_V4:
1572     return !invertPredicate ? Hexagon::STrib_imm_cPt_V4 :
1573                               Hexagon::STrib_imm_cNotPt_V4;
1574   case Hexagon::STrib_indexed_shl_V4:
1575     return !invertPredicate ? Hexagon::STrib_indexed_shl_cPt_V4 :
1576                               Hexagon::STrib_indexed_shl_cNotPt_V4;
1577   // Halfword.
1578   case Hexagon::POST_SThri:
1579     return !invertPredicate ? Hexagon::POST_SThri_cPt :
1580                               Hexagon::POST_SThri_cNotPt;
1581   case Hexagon::STrih:
1582     return !invertPredicate ? Hexagon::STrih_cPt :
1583                               Hexagon::STrih_cNotPt;
1584   case Hexagon::STrih_indexed:
1585     return !invertPredicate ? Hexagon::STrih_indexed_cPt :
1586                               Hexagon::STrih_indexed_cNotPt;
1587   case Hexagon::STrih_imm_V4:
1588     return !invertPredicate ? Hexagon::STrih_imm_cPt_V4 :
1589                               Hexagon::STrih_imm_cNotPt_V4;
1590   case Hexagon::STrih_indexed_shl_V4:
1591     return !invertPredicate ? Hexagon::STrih_indexed_shl_cPt_V4 :
1592                               Hexagon::STrih_indexed_shl_cNotPt_V4;
1593   // Word.
1594   case Hexagon::POST_STwri:
1595     return !invertPredicate ? Hexagon::POST_STwri_cPt :
1596                               Hexagon::POST_STwri_cNotPt;
1597   case Hexagon::STriw:
1598     return !invertPredicate ? Hexagon::STriw_cPt :
1599                               Hexagon::STriw_cNotPt;
1600   case Hexagon::STriw_indexed:
1601     return !invertPredicate ? Hexagon::STriw_indexed_cPt :
1602                               Hexagon::STriw_indexed_cNotPt;
1603   case Hexagon::STriw_indexed_shl_V4:
1604     return !invertPredicate ? Hexagon::STriw_indexed_shl_cPt_V4 :
1605                               Hexagon::STriw_indexed_shl_cNotPt_V4;
1606   case Hexagon::STriw_imm_V4:
1607     return !invertPredicate ? Hexagon::STriw_imm_cPt_V4 :
1608                               Hexagon::STriw_imm_cNotPt_V4;
1609   // Double word.
1610   case Hexagon::POST_STdri:
1611     return !invertPredicate ? Hexagon::POST_STdri_cPt :
1612                               Hexagon::POST_STdri_cNotPt;
1613   case Hexagon::STrid:
1614     return !invertPredicate ? Hexagon::STrid_cPt :
1615                               Hexagon::STrid_cNotPt;
1616   case Hexagon::STrid_indexed:
1617     return !invertPredicate ? Hexagon::STrid_indexed_cPt :
1618                               Hexagon::STrid_indexed_cNotPt;
1619   case Hexagon::STrid_indexed_shl_V4:
1620     return !invertPredicate ? Hexagon::STrid_indexed_shl_cPt_V4 :
1621                               Hexagon::STrid_indexed_shl_cNotPt_V4;
1622
1623   // V4 Store to global address
1624   case Hexagon::STd_GP_V4:
1625     return !invertPredicate ? Hexagon::STd_GP_cPt_V4 :
1626                               Hexagon::STd_GP_cNotPt_V4;
1627   case Hexagon::STb_GP_V4:
1628     return !invertPredicate ? Hexagon::STb_GP_cPt_V4 :
1629                               Hexagon::STb_GP_cNotPt_V4;
1630   case Hexagon::STh_GP_V4:
1631     return !invertPredicate ? Hexagon::STh_GP_cPt_V4 :
1632                               Hexagon::STh_GP_cNotPt_V4;
1633   case Hexagon::STw_GP_V4:
1634     return !invertPredicate ? Hexagon::STw_GP_cPt_V4 :
1635                               Hexagon::STw_GP_cNotPt_V4;
1636
1637   // Load.
1638   case Hexagon::LDrid:
1639     return !invertPredicate ? Hexagon::LDrid_cPt :
1640                               Hexagon::LDrid_cNotPt;
1641   case Hexagon::LDriw:
1642     return !invertPredicate ? Hexagon::LDriw_cPt :
1643                               Hexagon::LDriw_cNotPt;
1644   case Hexagon::LDrih:
1645     return !invertPredicate ? Hexagon::LDrih_cPt :
1646                               Hexagon::LDrih_cNotPt;
1647   case Hexagon::LDriuh:
1648     return !invertPredicate ? Hexagon::LDriuh_cPt :
1649                               Hexagon::LDriuh_cNotPt;
1650   case Hexagon::LDrib:
1651     return !invertPredicate ? Hexagon::LDrib_cPt :
1652                               Hexagon::LDrib_cNotPt;
1653   case Hexagon::LDriub:
1654     return !invertPredicate ? Hexagon::LDriub_cPt :
1655                               Hexagon::LDriub_cNotPt;
1656  // Load Indexed.
1657   case Hexagon::LDrid_indexed:
1658     return !invertPredicate ? Hexagon::LDrid_indexed_cPt :
1659                               Hexagon::LDrid_indexed_cNotPt;
1660   case Hexagon::LDriw_indexed:
1661     return !invertPredicate ? Hexagon::LDriw_indexed_cPt :
1662                               Hexagon::LDriw_indexed_cNotPt;
1663   case Hexagon::LDrih_indexed:
1664     return !invertPredicate ? Hexagon::LDrih_indexed_cPt :
1665                               Hexagon::LDrih_indexed_cNotPt;
1666   case Hexagon::LDriuh_indexed:
1667     return !invertPredicate ? Hexagon::LDriuh_indexed_cPt :
1668                               Hexagon::LDriuh_indexed_cNotPt;
1669   case Hexagon::LDrib_indexed:
1670     return !invertPredicate ? Hexagon::LDrib_indexed_cPt :
1671                               Hexagon::LDrib_indexed_cNotPt;
1672   case Hexagon::LDriub_indexed:
1673     return !invertPredicate ? Hexagon::LDriub_indexed_cPt :
1674                               Hexagon::LDriub_indexed_cNotPt;
1675   // Post Increment Load.
1676   case Hexagon::POST_LDrid:
1677     return !invertPredicate ? Hexagon::POST_LDrid_cPt :
1678                               Hexagon::POST_LDrid_cNotPt;
1679   case Hexagon::POST_LDriw:
1680     return !invertPredicate ? Hexagon::POST_LDriw_cPt :
1681                               Hexagon::POST_LDriw_cNotPt;
1682   case Hexagon::POST_LDrih:
1683     return !invertPredicate ? Hexagon::POST_LDrih_cPt :
1684                               Hexagon::POST_LDrih_cNotPt;
1685   case Hexagon::POST_LDriuh:
1686     return !invertPredicate ? Hexagon::POST_LDriuh_cPt :
1687                               Hexagon::POST_LDriuh_cNotPt;
1688   case Hexagon::POST_LDrib:
1689     return !invertPredicate ? Hexagon::POST_LDrib_cPt :
1690                               Hexagon::POST_LDrib_cNotPt;
1691   case Hexagon::POST_LDriub:
1692     return !invertPredicate ? Hexagon::POST_LDriub_cPt :
1693                               Hexagon::POST_LDriub_cNotPt;
1694   // DEALLOC_RETURN.
1695   case Hexagon::DEALLOC_RET_V4:
1696     return !invertPredicate ? Hexagon::DEALLOC_RET_cPt_V4 :
1697                               Hexagon::DEALLOC_RET_cNotPt_V4;
1698   }
1699   llvm_unreachable("Unexpected predicable instruction");
1700 }
1701
1702
1703 bool HexagonInstrInfo::
1704 PredicateInstruction(MachineInstr *MI,
1705                      const SmallVectorImpl<MachineOperand> &Cond) const {
1706   int Opc = MI->getOpcode();
1707   assert (isPredicable(MI) && "Expected predicable instruction");
1708   bool invertJump = (!Cond.empty() && Cond[0].isImm() &&
1709                      (Cond[0].getImm() == 0));
1710
1711   // This will change MI's opcode to its predicate version.
1712   // However, its operand list is still the old one, i.e. the
1713   // non-predicate one.
1714   MI->setDesc(get(getMatchingCondBranchOpcode(Opc, invertJump)));
1715
1716   int oper = -1;
1717   unsigned int GAIdx = 0;
1718
1719   // Indicates whether the current MI has a GlobalAddress operand
1720   bool hasGAOpnd = false;
1721   std::vector<MachineOperand> tmpOpnds;
1722
1723   // Indicates whether we need to shift operands to right.
1724   bool needShift = true;
1725
1726   // The predicate is ALWAYS the FIRST input operand !!!
1727   if (MI->getNumOperands() == 0) {
1728     // The non-predicate version of MI does not take any operands,
1729     // i.e. no outs and no ins. In this condition, the predicate
1730     // operand will be directly placed at Operands[0]. No operand
1731     // shift is needed.
1732     // Example: BARRIER
1733     needShift = false;
1734     oper = -1;
1735   }
1736   else if (   MI->getOperand(MI->getNumOperands()-1).isReg()
1737            && MI->getOperand(MI->getNumOperands()-1).isDef()
1738            && !MI->getOperand(MI->getNumOperands()-1).isImplicit()) {
1739     // The non-predicate version of MI does not have any input operands.
1740     // In this condition, we extend the length of Operands[] by one and
1741     // copy the original last operand to the newly allocated slot.
1742     // At this moment, it is just a place holder. Later, we will put
1743     // predicate operand directly into it. No operand shift is needed.
1744     // Example: r0=BARRIER (this is a faked insn used here for illustration)
1745     MI->addOperand(MI->getOperand(MI->getNumOperands()-1));
1746     needShift = false;
1747     oper = MI->getNumOperands() - 2;
1748   }
1749   else {
1750     // We need to right shift all input operands by one. Duplicate the
1751     // last operand into the newly allocated slot.
1752     MI->addOperand(MI->getOperand(MI->getNumOperands()-1));
1753   }
1754
1755   if (needShift)
1756   {
1757     // Operands[ MI->getNumOperands() - 2 ] has been copied into
1758     // Operands[ MI->getNumOperands() - 1 ], so we start from
1759     // Operands[ MI->getNumOperands() - 3 ].
1760     // oper is a signed int.
1761     // It is ok if "MI->getNumOperands()-3" is -3, -2, or -1.
1762     for (oper = MI->getNumOperands() - 3; oper >= 0; --oper)
1763     {
1764       MachineOperand &MO = MI->getOperand(oper);
1765
1766       // Opnd[0] Opnd[1] Opnd[2] Opnd[3] Opnd[4]   Opnd[5]   Opnd[6]   Opnd[7]
1767       // <Def0>  <Def1>  <Use0>  <Use1>  <ImpDef0> <ImpDef1> <ImpUse0> <ImpUse1>
1768       //               /\~
1769       //              /||\~
1770       //               ||
1771       //        Predicate Operand here
1772       if (MO.isReg() && !MO.isUse() && !MO.isImplicit()) {
1773         break;
1774       }
1775       if (MO.isReg()) {
1776         MI->getOperand(oper+1).ChangeToRegister(MO.getReg(), MO.isDef(),
1777                                                 MO.isImplicit(), MO.isKill(),
1778                                                 MO.isDead(), MO.isUndef(),
1779                                                 MO.isDebug());
1780       }
1781       else if (MO.isImm()) {
1782         MI->getOperand(oper+1).ChangeToImmediate(MO.getImm());
1783       }
1784       else if (MO.isGlobal()) {
1785         // MI can not have more than one GlobalAddress operand.
1786         assert(hasGAOpnd == false && "MI can only have one GlobalAddress opnd");
1787
1788         // There is no member function called "ChangeToGlobalAddress" in the
1789         // MachineOperand class (not like "ChangeToRegister" and
1790         // "ChangeToImmediate"). So we have to remove them from Operands[] list
1791         // first, and then add them back after we have inserted the predicate
1792         // operand. tmpOpnds[] is to remember these operands before we remove
1793         // them.
1794         tmpOpnds.push_back(MO);
1795
1796         // Operands[oper] is a GlobalAddress operand;
1797         // Operands[oper+1] has been copied into Operands[oper+2];
1798         hasGAOpnd = true;
1799         GAIdx = oper;
1800         continue;
1801       }
1802       else {
1803         assert(false && "Unexpected operand type");
1804       }
1805     }
1806   }
1807
1808   int regPos = invertJump ? 1 : 0;
1809   MachineOperand PredMO = Cond[regPos];
1810
1811   // [oper] now points to the last explicit Def. Predicate operand must be
1812   // located at [oper+1]. See diagram above.
1813   // This assumes that the predicate is always the first operand,
1814   // i.e. Operands[0+numResults], in the set of inputs
1815   // It is better to have an assert here to check this. But I don't know how
1816   // to write this assert because findFirstPredOperandIdx() would return -1
1817   if (oper < -1) oper = -1;
1818   MI->getOperand(oper+1).ChangeToRegister(PredMO.getReg(), PredMO.isDef(),
1819                                           PredMO.isImplicit(), PredMO.isKill(),
1820                                           PredMO.isDead(), PredMO.isUndef(),
1821                                           PredMO.isDebug());
1822
1823   if (hasGAOpnd)
1824   {
1825     unsigned int i;
1826
1827     // Operands[GAIdx] is the original GlobalAddress operand, which is
1828     // already copied into tmpOpnds[0].
1829     // Operands[GAIdx] now stores a copy of Operands[GAIdx-1]
1830     // Operands[GAIdx+1] has already been copied into Operands[GAIdx+2],
1831     // so we start from [GAIdx+2]
1832     for (i = GAIdx + 2; i < MI->getNumOperands(); ++i)
1833       tmpOpnds.push_back(MI->getOperand(i));
1834
1835     // Remove all operands in range [ (GAIdx+1) ... (MI->getNumOperands()-1) ]
1836     // It is very important that we always remove from the end of Operands[]
1837     // MI->getNumOperands() is at least 2 if program goes to here.
1838     for (i = MI->getNumOperands() - 1; i > GAIdx; --i)
1839       MI->RemoveOperand(i);
1840
1841     for (i = 0; i < tmpOpnds.size(); ++i)
1842       MI->addOperand(tmpOpnds[i]);
1843   }
1844
1845   return true;
1846 }
1847
1848
1849 bool
1850 HexagonInstrInfo::
1851 isProfitableToIfCvt(MachineBasicBlock &MBB,
1852                     unsigned NumCycles,
1853                     unsigned ExtraPredCycles,
1854                     const BranchProbability &Probability) const {
1855   return true;
1856 }
1857
1858
1859 bool
1860 HexagonInstrInfo::
1861 isProfitableToIfCvt(MachineBasicBlock &TMBB,
1862                     unsigned NumTCycles,
1863                     unsigned ExtraTCycles,
1864                     MachineBasicBlock &FMBB,
1865                     unsigned NumFCycles,
1866                     unsigned ExtraFCycles,
1867                     const BranchProbability &Probability) const {
1868   return true;
1869 }
1870
1871
1872 bool HexagonInstrInfo::isPredicated(const MachineInstr *MI) const {
1873   const uint64_t F = MI->getDesc().TSFlags;
1874
1875   return ((F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
1876 }
1877
1878 bool HexagonInstrInfo::isPredicatedNew(const MachineInstr *MI) const {
1879   const uint64_t F = MI->getDesc().TSFlags;
1880
1881   assert(isPredicated(MI));
1882   return ((F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask);
1883 }
1884
1885 bool
1886 HexagonInstrInfo::DefinesPredicate(MachineInstr *MI,
1887                                    std::vector<MachineOperand> &Pred) const {
1888   for (unsigned oper = 0; oper < MI->getNumOperands(); ++oper) {
1889     MachineOperand MO = MI->getOperand(oper);
1890     if (MO.isReg() && MO.isDef()) {
1891       const TargetRegisterClass* RC = RI.getMinimalPhysRegClass(MO.getReg());
1892       if (RC == &Hexagon::PredRegsRegClass) {
1893         Pred.push_back(MO);
1894         return true;
1895       }
1896     }
1897   }
1898   return false;
1899 }
1900
1901
1902 bool
1903 HexagonInstrInfo::
1904 SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
1905                   const SmallVectorImpl<MachineOperand> &Pred2) const {
1906   // TODO: Fix this
1907   return false;
1908 }
1909
1910
1911 //
1912 // We indicate that we want to reverse the branch by
1913 // inserting a 0 at the beginning of the Cond vector.
1914 //
1915 bool HexagonInstrInfo::
1916 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
1917   if (!Cond.empty() && Cond[0].isImm() && Cond[0].getImm() == 0) {
1918     Cond.erase(Cond.begin());
1919   } else {
1920     Cond.insert(Cond.begin(), MachineOperand::CreateImm(0));
1921   }
1922   return false;
1923 }
1924
1925
1926 bool HexagonInstrInfo::
1927 isProfitableToDupForIfCvt(MachineBasicBlock &MBB,unsigned NumInstrs,
1928                           const BranchProbability &Probability) const {
1929   return (NumInstrs <= 4);
1930 }
1931
1932 bool HexagonInstrInfo::isDeallocRet(const MachineInstr *MI) const {
1933   switch (MI->getOpcode()) {
1934   default: return false;
1935   case Hexagon::DEALLOC_RET_V4 :
1936   case Hexagon::DEALLOC_RET_cPt_V4 :
1937   case Hexagon::DEALLOC_RET_cNotPt_V4 :
1938   case Hexagon::DEALLOC_RET_cdnPnt_V4 :
1939   case Hexagon::DEALLOC_RET_cNotdnPnt_V4 :
1940   case Hexagon::DEALLOC_RET_cdnPt_V4 :
1941   case Hexagon::DEALLOC_RET_cNotdnPt_V4 :
1942    return true;
1943   }
1944 }
1945
1946
1947 bool HexagonInstrInfo::
1948 isValidOffset(const int Opcode, const int Offset) const {
1949   // This function is to check whether the "Offset" is in the correct range of
1950   // the given "Opcode". If "Offset" is not in the correct range, "ADD_ri" is
1951   // inserted to calculate the final address. Due to this reason, the function
1952   // assumes that the "Offset" has correct alignment.
1953   // We used to assert if the offset was not properly aligned, however,
1954   // there are cases where a misaligned pointer recast can cause this
1955   // problem, and we need to allow for it. The front end warns of such
1956   // misaligns with respect to load size.
1957
1958   switch(Opcode) {
1959
1960   case Hexagon::LDriw:
1961   case Hexagon::LDriw_indexed:
1962   case Hexagon::LDriw_f:
1963   case Hexagon::STriw_indexed:
1964   case Hexagon::STriw:
1965   case Hexagon::STriw_f:
1966     return (Offset >= Hexagon_MEMW_OFFSET_MIN) &&
1967       (Offset <= Hexagon_MEMW_OFFSET_MAX);
1968
1969   case Hexagon::LDrid:
1970   case Hexagon::LDrid_indexed:
1971   case Hexagon::LDrid_f:
1972   case Hexagon::STrid:
1973   case Hexagon::STrid_indexed:
1974   case Hexagon::STrid_f:
1975     return (Offset >= Hexagon_MEMD_OFFSET_MIN) &&
1976       (Offset <= Hexagon_MEMD_OFFSET_MAX);
1977
1978   case Hexagon::LDrih:
1979   case Hexagon::LDriuh:
1980   case Hexagon::STrih:
1981     return (Offset >= Hexagon_MEMH_OFFSET_MIN) &&
1982       (Offset <= Hexagon_MEMH_OFFSET_MAX);
1983
1984   case Hexagon::LDrib:
1985   case Hexagon::STrib:
1986   case Hexagon::LDriub:
1987     return (Offset >= Hexagon_MEMB_OFFSET_MIN) &&
1988       (Offset <= Hexagon_MEMB_OFFSET_MAX);
1989
1990   case Hexagon::ADD_ri:
1991   case Hexagon::TFR_FI:
1992     return (Offset >= Hexagon_ADDI_OFFSET_MIN) &&
1993       (Offset <= Hexagon_ADDI_OFFSET_MAX);
1994
1995   case Hexagon::MemOPw_ADDi_V4 :
1996   case Hexagon::MemOPw_SUBi_V4 :
1997   case Hexagon::MemOPw_ADDr_V4 :
1998   case Hexagon::MemOPw_SUBr_V4 :
1999   case Hexagon::MemOPw_ANDr_V4 :
2000   case Hexagon::MemOPw_ORr_V4 :
2001     return (0 <= Offset && Offset <= 255);
2002
2003   case Hexagon::MemOPh_ADDi_V4 :
2004   case Hexagon::MemOPh_SUBi_V4 :
2005   case Hexagon::MemOPh_ADDr_V4 :
2006   case Hexagon::MemOPh_SUBr_V4 :
2007   case Hexagon::MemOPh_ANDr_V4 :
2008   case Hexagon::MemOPh_ORr_V4 :
2009     return (0 <= Offset && Offset <= 127);
2010
2011   case Hexagon::MemOPb_ADDi_V4 :
2012   case Hexagon::MemOPb_SUBi_V4 :
2013   case Hexagon::MemOPb_ADDr_V4 :
2014   case Hexagon::MemOPb_SUBr_V4 :
2015   case Hexagon::MemOPb_ANDr_V4 :
2016   case Hexagon::MemOPb_ORr_V4 :
2017     return (0 <= Offset && Offset <= 63);
2018
2019   // LDri_pred and STriw_pred are pseudo operations, so it has to take offset of
2020   // any size. Later pass knows how to handle it.
2021   case Hexagon::STriw_pred:
2022   case Hexagon::LDriw_pred:
2023     return true;
2024
2025   case Hexagon::LOOP0_i:
2026     return isUInt<10>(Offset);
2027
2028   // INLINEASM is very special.
2029   case Hexagon::INLINEASM:
2030     return true;
2031   }
2032
2033   llvm_unreachable("No offset range is defined for this opcode. "
2034                    "Please define it in the above switch statement!");
2035 }
2036
2037
2038 //
2039 // Check if the Offset is a valid auto-inc imm by Load/Store Type.
2040 //
2041 bool HexagonInstrInfo::
2042 isValidAutoIncImm(const EVT VT, const int Offset) const {
2043
2044   if (VT == MVT::i64) {
2045       return (Offset >= Hexagon_MEMD_AUTOINC_MIN &&
2046               Offset <= Hexagon_MEMD_AUTOINC_MAX &&
2047               (Offset & 0x7) == 0);
2048   }
2049   if (VT == MVT::i32) {
2050       return (Offset >= Hexagon_MEMW_AUTOINC_MIN &&
2051               Offset <= Hexagon_MEMW_AUTOINC_MAX &&
2052               (Offset & 0x3) == 0);
2053   }
2054   if (VT == MVT::i16) {
2055       return (Offset >= Hexagon_MEMH_AUTOINC_MIN &&
2056               Offset <= Hexagon_MEMH_AUTOINC_MAX &&
2057               (Offset & 0x1) == 0);
2058   }
2059   if (VT == MVT::i8) {
2060       return (Offset >= Hexagon_MEMB_AUTOINC_MIN &&
2061               Offset <= Hexagon_MEMB_AUTOINC_MAX);
2062   }
2063   llvm_unreachable("Not an auto-inc opc!");
2064 }
2065
2066
2067 bool HexagonInstrInfo::
2068 isMemOp(const MachineInstr *MI) const {
2069   switch (MI->getOpcode())
2070   {
2071     default: return false;
2072     case Hexagon::MemOPw_ADDi_V4 :
2073     case Hexagon::MemOPw_SUBi_V4 :
2074     case Hexagon::MemOPw_ADDr_V4 :
2075     case Hexagon::MemOPw_SUBr_V4 :
2076     case Hexagon::MemOPw_ANDr_V4 :
2077     case Hexagon::MemOPw_ORr_V4 :
2078     case Hexagon::MemOPh_ADDi_V4 :
2079     case Hexagon::MemOPh_SUBi_V4 :
2080     case Hexagon::MemOPh_ADDr_V4 :
2081     case Hexagon::MemOPh_SUBr_V4 :
2082     case Hexagon::MemOPh_ANDr_V4 :
2083     case Hexagon::MemOPh_ORr_V4 :
2084     case Hexagon::MemOPb_ADDi_V4 :
2085     case Hexagon::MemOPb_SUBi_V4 :
2086     case Hexagon::MemOPb_ADDr_V4 :
2087     case Hexagon::MemOPb_SUBr_V4 :
2088     case Hexagon::MemOPb_ANDr_V4 :
2089     case Hexagon::MemOPb_ORr_V4 :
2090     case Hexagon::MemOPb_SETBITi_V4:
2091     case Hexagon::MemOPh_SETBITi_V4:
2092     case Hexagon::MemOPw_SETBITi_V4:
2093     case Hexagon::MemOPb_CLRBITi_V4:
2094     case Hexagon::MemOPh_CLRBITi_V4:
2095     case Hexagon::MemOPw_CLRBITi_V4:
2096     return true;
2097   }
2098   return false;
2099 }
2100
2101
2102 bool HexagonInstrInfo::
2103 isSpillPredRegOp(const MachineInstr *MI) const {
2104   switch (MI->getOpcode()) {
2105     default: return false;
2106     case Hexagon::STriw_pred :
2107     case Hexagon::LDriw_pred :
2108       return true;
2109   }
2110 }
2111
2112 bool HexagonInstrInfo::isNewValueJumpCandidate(const MachineInstr *MI) const {
2113   switch (MI->getOpcode()) {
2114     default: return false;
2115     case Hexagon::CMPEQrr:
2116     case Hexagon::CMPEQri:
2117     case Hexagon::CMPLTrr:
2118     case Hexagon::CMPGTrr:
2119     case Hexagon::CMPGTri:
2120     case Hexagon::CMPLTUrr:
2121     case Hexagon::CMPGTUrr:
2122     case Hexagon::CMPGTUri:
2123     case Hexagon::CMPGEri:
2124     case Hexagon::CMPGEUri:
2125       return true;
2126   }
2127 }
2128
2129 bool HexagonInstrInfo::
2130 isConditionalTransfer (const MachineInstr *MI) const {
2131   switch (MI->getOpcode()) {
2132     default: return false;
2133     case Hexagon::TFR_cPt:
2134     case Hexagon::TFR_cNotPt:
2135     case Hexagon::TFRI_cPt:
2136     case Hexagon::TFRI_cNotPt:
2137     case Hexagon::TFR_cdnPt:
2138     case Hexagon::TFR_cdnNotPt:
2139     case Hexagon::TFRI_cdnPt:
2140     case Hexagon::TFRI_cdnNotPt:
2141       return true;
2142   }
2143 }
2144
2145 bool HexagonInstrInfo::isConditionalALU32 (const MachineInstr* MI) const {
2146   const HexagonRegisterInfo& QRI = getRegisterInfo();
2147   switch (MI->getOpcode())
2148   {
2149     default: return false;
2150     case Hexagon::ADD_ri_cPt:
2151     case Hexagon::ADD_ri_cNotPt:
2152     case Hexagon::ADD_rr_cPt:
2153     case Hexagon::ADD_rr_cNotPt:
2154     case Hexagon::XOR_rr_cPt:
2155     case Hexagon::XOR_rr_cNotPt:
2156     case Hexagon::AND_rr_cPt:
2157     case Hexagon::AND_rr_cNotPt:
2158     case Hexagon::OR_rr_cPt:
2159     case Hexagon::OR_rr_cNotPt:
2160     case Hexagon::SUB_rr_cPt:
2161     case Hexagon::SUB_rr_cNotPt:
2162     case Hexagon::COMBINE_rr_cPt:
2163     case Hexagon::COMBINE_rr_cNotPt:
2164       return true;
2165     case Hexagon::ASLH_cPt_V4:
2166     case Hexagon::ASLH_cNotPt_V4:
2167     case Hexagon::ASRH_cPt_V4:
2168     case Hexagon::ASRH_cNotPt_V4:
2169     case Hexagon::SXTB_cPt_V4:
2170     case Hexagon::SXTB_cNotPt_V4:
2171     case Hexagon::SXTH_cPt_V4:
2172     case Hexagon::SXTH_cNotPt_V4:
2173     case Hexagon::ZXTB_cPt_V4:
2174     case Hexagon::ZXTB_cNotPt_V4:
2175     case Hexagon::ZXTH_cPt_V4:
2176     case Hexagon::ZXTH_cNotPt_V4:
2177       return QRI.Subtarget.hasV4TOps();
2178   }
2179 }
2180
2181 bool HexagonInstrInfo::
2182 isConditionalLoad (const MachineInstr* MI) const {
2183   const HexagonRegisterInfo& QRI = getRegisterInfo();
2184   switch (MI->getOpcode())
2185   {
2186     default: return false;
2187     case Hexagon::LDrid_cPt :
2188     case Hexagon::LDrid_cNotPt :
2189     case Hexagon::LDrid_indexed_cPt :
2190     case Hexagon::LDrid_indexed_cNotPt :
2191     case Hexagon::LDriw_cPt :
2192     case Hexagon::LDriw_cNotPt :
2193     case Hexagon::LDriw_indexed_cPt :
2194     case Hexagon::LDriw_indexed_cNotPt :
2195     case Hexagon::LDrih_cPt :
2196     case Hexagon::LDrih_cNotPt :
2197     case Hexagon::LDrih_indexed_cPt :
2198     case Hexagon::LDrih_indexed_cNotPt :
2199     case Hexagon::LDrib_cPt :
2200     case Hexagon::LDrib_cNotPt :
2201     case Hexagon::LDrib_indexed_cPt :
2202     case Hexagon::LDrib_indexed_cNotPt :
2203     case Hexagon::LDriuh_cPt :
2204     case Hexagon::LDriuh_cNotPt :
2205     case Hexagon::LDriuh_indexed_cPt :
2206     case Hexagon::LDriuh_indexed_cNotPt :
2207     case Hexagon::LDriub_cPt :
2208     case Hexagon::LDriub_cNotPt :
2209     case Hexagon::LDriub_indexed_cPt :
2210     case Hexagon::LDriub_indexed_cNotPt :
2211       return true;
2212     case Hexagon::POST_LDrid_cPt :
2213     case Hexagon::POST_LDrid_cNotPt :
2214     case Hexagon::POST_LDriw_cPt :
2215     case Hexagon::POST_LDriw_cNotPt :
2216     case Hexagon::POST_LDrih_cPt :
2217     case Hexagon::POST_LDrih_cNotPt :
2218     case Hexagon::POST_LDrib_cPt :
2219     case Hexagon::POST_LDrib_cNotPt :
2220     case Hexagon::POST_LDriuh_cPt :
2221     case Hexagon::POST_LDriuh_cNotPt :
2222     case Hexagon::POST_LDriub_cPt :
2223     case Hexagon::POST_LDriub_cNotPt :
2224       return QRI.Subtarget.hasV4TOps();
2225     case Hexagon::LDrid_indexed_shl_cPt_V4 :
2226     case Hexagon::LDrid_indexed_shl_cNotPt_V4 :
2227     case Hexagon::LDrib_indexed_shl_cPt_V4 :
2228     case Hexagon::LDrib_indexed_shl_cNotPt_V4 :
2229     case Hexagon::LDriub_indexed_shl_cPt_V4 :
2230     case Hexagon::LDriub_indexed_shl_cNotPt_V4 :
2231     case Hexagon::LDrih_indexed_shl_cPt_V4 :
2232     case Hexagon::LDrih_indexed_shl_cNotPt_V4 :
2233     case Hexagon::LDriuh_indexed_shl_cPt_V4 :
2234     case Hexagon::LDriuh_indexed_shl_cNotPt_V4 :
2235     case Hexagon::LDriw_indexed_shl_cPt_V4 :
2236     case Hexagon::LDriw_indexed_shl_cNotPt_V4 :
2237       return QRI.Subtarget.hasV4TOps();
2238   }
2239 }
2240
2241 // Returns true if an instruction is a conditional store.
2242 //
2243 // Note: It doesn't include conditional new-value stores as they can't be
2244 // converted to .new predicate.
2245 //
2246 //               p.new NV store [ if(p0.new)memw(R0+#0)=R2.new ]
2247 //                ^           ^
2248 //               /             \ (not OK. it will cause new-value store to be
2249 //              /               X conditional on p0.new while R2 producer is
2250 //             /                 \ on p0)
2251 //            /                   \.
2252 //     p.new store                 p.old NV store
2253 // [if(p0.new)memw(R0+#0)=R2]    [if(p0)memw(R0+#0)=R2.new]
2254 //            ^                  ^
2255 //             \                /
2256 //              \              /
2257 //               \            /
2258 //                 p.old store
2259 //             [if (p0)memw(R0+#0)=R2]
2260 //
2261 // The above diagram shows the steps involoved in the conversion of a predicated
2262 // store instruction to its .new predicated new-value form.
2263 //
2264 // The following set of instructions further explains the scenario where
2265 // conditional new-value store becomes invalid when promoted to .new predicate
2266 // form.
2267 //
2268 // { 1) if (p0) r0 = add(r1, r2)
2269 //   2) p0 = cmp.eq(r3, #0) }
2270 //
2271 //   3) if (p0) memb(r1+#0) = r0  --> this instruction can't be grouped with
2272 // the first two instructions because in instr 1, r0 is conditional on old value
2273 // of p0 but its use in instr 3 is conditional on p0 modified by instr 2 which
2274 // is not valid for new-value stores.
2275 bool HexagonInstrInfo::
2276 isConditionalStore (const MachineInstr* MI) const {
2277   const HexagonRegisterInfo& QRI = getRegisterInfo();
2278   switch (MI->getOpcode())
2279   {
2280     default: return false;
2281     case Hexagon::STrib_imm_cPt_V4 :
2282     case Hexagon::STrib_imm_cNotPt_V4 :
2283     case Hexagon::STrib_indexed_shl_cPt_V4 :
2284     case Hexagon::STrib_indexed_shl_cNotPt_V4 :
2285     case Hexagon::STrib_cPt :
2286     case Hexagon::STrib_cNotPt :
2287     case Hexagon::POST_STbri_cPt :
2288     case Hexagon::POST_STbri_cNotPt :
2289     case Hexagon::STrid_indexed_cPt :
2290     case Hexagon::STrid_indexed_cNotPt :
2291     case Hexagon::STrid_indexed_shl_cPt_V4 :
2292     case Hexagon::POST_STdri_cPt :
2293     case Hexagon::POST_STdri_cNotPt :
2294     case Hexagon::STrih_cPt :
2295     case Hexagon::STrih_cNotPt :
2296     case Hexagon::STrih_indexed_cPt :
2297     case Hexagon::STrih_indexed_cNotPt :
2298     case Hexagon::STrih_imm_cPt_V4 :
2299     case Hexagon::STrih_imm_cNotPt_V4 :
2300     case Hexagon::STrih_indexed_shl_cPt_V4 :
2301     case Hexagon::STrih_indexed_shl_cNotPt_V4 :
2302     case Hexagon::POST_SThri_cPt :
2303     case Hexagon::POST_SThri_cNotPt :
2304     case Hexagon::STriw_cPt :
2305     case Hexagon::STriw_cNotPt :
2306     case Hexagon::STriw_indexed_cPt :
2307     case Hexagon::STriw_indexed_cNotPt :
2308     case Hexagon::STriw_imm_cPt_V4 :
2309     case Hexagon::STriw_imm_cNotPt_V4 :
2310     case Hexagon::STriw_indexed_shl_cPt_V4 :
2311     case Hexagon::STriw_indexed_shl_cNotPt_V4 :
2312     case Hexagon::POST_STwri_cPt :
2313     case Hexagon::POST_STwri_cNotPt :
2314       return QRI.Subtarget.hasV4TOps();
2315
2316     // V4 global address store before promoting to dot new.
2317     case Hexagon::STd_GP_cPt_V4 :
2318     case Hexagon::STd_GP_cNotPt_V4 :
2319     case Hexagon::STb_GP_cPt_V4 :
2320     case Hexagon::STb_GP_cNotPt_V4 :
2321     case Hexagon::STh_GP_cPt_V4 :
2322     case Hexagon::STh_GP_cNotPt_V4 :
2323     case Hexagon::STw_GP_cPt_V4 :
2324     case Hexagon::STw_GP_cNotPt_V4 :
2325       return QRI.Subtarget.hasV4TOps();
2326
2327     // Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
2328     // from the "Conditional Store" list. Because a predicated new value store
2329     // would NOT be promoted to a double dot new store. See diagram below:
2330     // This function returns yes for those stores that are predicated but not
2331     // yet promoted to predicate dot new instructions.
2332     //
2333     //                          +---------------------+
2334     //                    /-----| if (p0) memw(..)=r0 |---------\~
2335     //                   ||     +---------------------+         ||
2336     //          promote  ||       /\       /\                   ||  promote
2337     //                   ||      /||\     /||\                  ||
2338     //                  \||/    demote     ||                  \||/
2339     //                   \/       ||       ||                   \/
2340     //       +-------------------------+   ||   +-------------------------+
2341     //       | if (p0.new) memw(..)=r0 |   ||   | if (p0) memw(..)=r0.new |
2342     //       +-------------------------+   ||   +-------------------------+
2343     //                        ||           ||         ||
2344     //                        ||         demote      \||/
2345     //                      promote        ||         \/ NOT possible
2346     //                        ||           ||         /\~
2347     //                       \||/          ||        /||\~
2348     //                        \/           ||         ||
2349     //                      +-----------------------------+
2350     //                      | if (p0.new) memw(..)=r0.new |
2351     //                      +-----------------------------+
2352     //                           Double Dot New Store
2353     //
2354   }
2355 }
2356
2357 // Returns true, if any one of the operands is a dot new
2358 // insn, whether it is predicated dot new or register dot new.
2359 bool HexagonInstrInfo::isDotNewInst (const MachineInstr* MI) const {
2360   return (isNewValueInst(MI) ||
2361      (isPredicated(MI) && isPredicatedNew(MI)));
2362 }
2363
2364 unsigned HexagonInstrInfo::getAddrMode(const MachineInstr* MI) const {
2365   const uint64_t F = MI->getDesc().TSFlags;
2366
2367   return((F >> HexagonII::AddrModePos) & HexagonII::AddrModeMask);
2368 }
2369
2370 /// immediateExtend - Changes the instruction in place to one using an immediate
2371 /// extender.
2372 void HexagonInstrInfo::immediateExtend(MachineInstr *MI) const {
2373   assert((isExtendable(MI)||isConstExtended(MI)) &&
2374                                "Instruction must be extendable");
2375   // Find which operand is extendable.
2376   short ExtOpNum = getCExtOpNum(MI);
2377   MachineOperand &MO = MI->getOperand(ExtOpNum);
2378   // This needs to be something we understand.
2379   assert((MO.isMBB() || MO.isImm()) &&
2380          "Branch with unknown extendable field type");
2381   // Mark given operand as extended.
2382   MO.addTargetFlag(HexagonII::HMOTF_ConstExtended);
2383 }
2384
2385 DFAPacketizer *HexagonInstrInfo::
2386 CreateTargetScheduleState(const TargetMachine *TM,
2387                            const ScheduleDAG *DAG) const {
2388   const InstrItineraryData *II = TM->getInstrItineraryData();
2389   return TM->getSubtarget<HexagonGenSubtargetInfo>().createDFAPacketizer(II);
2390 }
2391
2392 bool HexagonInstrInfo::isSchedulingBoundary(const MachineInstr *MI,
2393                                             const MachineBasicBlock *MBB,
2394                                             const MachineFunction &MF) const {
2395   // Debug info is never a scheduling boundary. It's necessary to be explicit
2396   // due to the special treatment of IT instructions below, otherwise a
2397   // dbg_value followed by an IT will result in the IT instruction being
2398   // considered a scheduling hazard, which is wrong. It should be the actual
2399   // instruction preceding the dbg_value instruction(s), just like it is
2400   // when debug info is not present.
2401   if (MI->isDebugValue())
2402     return false;
2403
2404   // Terminators and labels can't be scheduled around.
2405   if (MI->getDesc().isTerminator() || MI->isLabel() || MI->isInlineAsm())
2406     return true;
2407
2408   return false;
2409 }
2410
2411 bool HexagonInstrInfo::isConstExtended(MachineInstr *MI) const {
2412
2413   // Constant extenders are allowed only for V4 and above.
2414   if (!Subtarget.hasV4TOps())
2415     return false;
2416
2417   const uint64_t F = MI->getDesc().TSFlags;
2418   unsigned isExtended = (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
2419   if (isExtended) // Instruction must be extended.
2420     return true;
2421
2422   unsigned isExtendable = (F >> HexagonII::ExtendablePos)
2423                           & HexagonII::ExtendableMask;
2424   if (!isExtendable)
2425     return false;
2426
2427   short ExtOpNum = getCExtOpNum(MI);
2428   const MachineOperand &MO = MI->getOperand(ExtOpNum);
2429   // Use MO operand flags to determine if MO
2430   // has the HMOTF_ConstExtended flag set.
2431   if (MO.getTargetFlags() && HexagonII::HMOTF_ConstExtended)
2432     return true;
2433   // If this is a Machine BB address we are talking about, and it is
2434   // not marked as extended, say so.
2435   if (MO.isMBB())
2436     return false;
2437
2438   // We could be using an instruction with an extendable immediate and shoehorn
2439   // a global address into it. If it is a global address it will be constant
2440   // extended. We do this for COMBINE.
2441   // We currently only handle isGlobal() because it is the only kind of
2442   // object we are going to end up with here for now.
2443   // In the future we probably should add isSymbol(), etc.
2444   if (MO.isGlobal() || MO.isSymbol())
2445     return true;
2446
2447   // If the extendable operand is not 'Immediate' type, the instruction should
2448   // have 'isExtended' flag set.
2449   assert(MO.isImm() && "Extendable operand must be Immediate type");
2450
2451   int MinValue = getMinValue(MI);
2452   int MaxValue = getMaxValue(MI);
2453   int ImmValue = MO.getImm();
2454
2455   return (ImmValue < MinValue || ImmValue > MaxValue);
2456 }
2457
2458 // Returns true if a particular operand is extendable for an instruction.
2459 bool HexagonInstrInfo::isOperandExtended(const MachineInstr *MI,
2460                                          unsigned short OperandNum) const {
2461   // Constant extenders are allowed only for V4 and above.
2462   if (!Subtarget.hasV4TOps())
2463     return false;
2464
2465   const uint64_t F = MI->getDesc().TSFlags;
2466
2467   return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask)
2468           == OperandNum;
2469 }
2470
2471 // Returns Operand Index for the constant extended instruction.
2472 unsigned short HexagonInstrInfo::getCExtOpNum(const MachineInstr *MI) const {
2473   const uint64_t F = MI->getDesc().TSFlags;
2474   return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask);
2475 }
2476
2477 // Returns the min value that doesn't need to be extended.
2478 int HexagonInstrInfo::getMinValue(const MachineInstr *MI) const {
2479   const uint64_t F = MI->getDesc().TSFlags;
2480   unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
2481                     & HexagonII::ExtentSignedMask;
2482   unsigned bits =  (F >> HexagonII::ExtentBitsPos)
2483                     & HexagonII::ExtentBitsMask;
2484
2485   if (isSigned) // if value is signed
2486     return -1 << (bits - 1);
2487   else
2488     return 0;
2489 }
2490
2491 // Returns the max value that doesn't need to be extended.
2492 int HexagonInstrInfo::getMaxValue(const MachineInstr *MI) const {
2493   const uint64_t F = MI->getDesc().TSFlags;
2494   unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
2495                     & HexagonII::ExtentSignedMask;
2496   unsigned bits =  (F >> HexagonII::ExtentBitsPos)
2497                     & HexagonII::ExtentBitsMask;
2498
2499   if (isSigned) // if value is signed
2500     return ~(-1 << (bits - 1));
2501   else
2502     return ~(-1 << bits);
2503 }
2504
2505 // Returns true if an instruction can be converted into a non-extended
2506 // equivalent instruction.
2507 bool HexagonInstrInfo::NonExtEquivalentExists (const MachineInstr *MI) const {
2508
2509   short NonExtOpcode;
2510   // Check if the instruction has a register form that uses register in place
2511   // of the extended operand, if so return that as the non-extended form.
2512   if (Hexagon::getRegForm(MI->getOpcode()) >= 0)
2513     return true;
2514
2515   if (MI->getDesc().mayLoad() || MI->getDesc().mayStore()) {
2516     // Check addressing mode and retreive non-ext equivalent instruction.
2517
2518     switch (getAddrMode(MI)) {
2519     case HexagonII::Absolute :
2520       // Load/store with absolute addressing mode can be converted into
2521       // base+offset mode.
2522       NonExtOpcode = Hexagon::getBasedWithImmOffset(MI->getOpcode());
2523       break;
2524     case HexagonII::BaseImmOffset :
2525       // Load/store with base+offset addressing mode can be converted into
2526       // base+register offset addressing mode. However left shift operand should
2527       // be set to 0.
2528       NonExtOpcode = Hexagon::getBaseWithRegOffset(MI->getOpcode());
2529       break;
2530     default:
2531       return false;
2532     }
2533     if (NonExtOpcode < 0)
2534       return false;
2535     return true;
2536   }
2537   return false;
2538 }
2539
2540 // Returns opcode of the non-extended equivalent instruction.
2541 short HexagonInstrInfo::getNonExtOpcode (const MachineInstr *MI) const {
2542
2543   // Check if the instruction has a register form that uses register in place
2544   // of the extended operand, if so return that as the non-extended form.
2545   short NonExtOpcode = Hexagon::getRegForm(MI->getOpcode());
2546     if (NonExtOpcode >= 0)
2547       return NonExtOpcode;
2548
2549   if (MI->getDesc().mayLoad() || MI->getDesc().mayStore()) {
2550     // Check addressing mode and retreive non-ext equivalent instruction.
2551     switch (getAddrMode(MI)) {
2552     case HexagonII::Absolute :
2553       return Hexagon::getBasedWithImmOffset(MI->getOpcode());
2554     case HexagonII::BaseImmOffset :
2555       return Hexagon::getBaseWithRegOffset(MI->getOpcode());
2556     default:
2557       return -1;
2558     }
2559   }
2560   return -1;
2561 }