2a9535025ed89cf44aa9a6f5920f279a81cab479
[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
541 unsigned HexagonInstrInfo::createVR(MachineFunction* MF, MVT VT) const {
542
543   MachineRegisterInfo &RegInfo = MF->getRegInfo();
544   const TargetRegisterClass *TRC;
545   if (VT == MVT::i1) {
546     TRC = &Hexagon::PredRegsRegClass;
547   } else if (VT == MVT::i32 || VT == MVT::f32) {
548     TRC = &Hexagon::IntRegsRegClass;
549   } else if (VT == MVT::i64 || VT == MVT::f64) {
550     TRC = &Hexagon::DoubleRegsRegClass;
551   } else {
552     llvm_unreachable("Cannot handle this register class");
553   }
554
555   unsigned NewReg = RegInfo.createVirtualRegister(TRC);
556   return NewReg;
557 }
558
559 bool HexagonInstrInfo::isExtendable(const MachineInstr *MI) const {
560   switch(MI->getOpcode()) {
561     default: return false;
562     // JMP_EQri
563     case Hexagon::JMP_EQriPt_nv_V4:
564     case Hexagon::JMP_EQriPnt_nv_V4:
565     case Hexagon::JMP_EQriNotPt_nv_V4:
566     case Hexagon::JMP_EQriNotPnt_nv_V4:
567
568     // JMP_EQri - with -1
569     case Hexagon::JMP_EQriPtneg_nv_V4:
570     case Hexagon::JMP_EQriPntneg_nv_V4:
571     case Hexagon::JMP_EQriNotPtneg_nv_V4:
572     case Hexagon::JMP_EQriNotPntneg_nv_V4:
573
574     // JMP_EQrr
575     case Hexagon::JMP_EQrrPt_nv_V4:
576     case Hexagon::JMP_EQrrPnt_nv_V4:
577     case Hexagon::JMP_EQrrNotPt_nv_V4:
578     case Hexagon::JMP_EQrrNotPnt_nv_V4:
579
580     // JMP_GTri
581     case Hexagon::JMP_GTriPt_nv_V4:
582     case Hexagon::JMP_GTriPnt_nv_V4:
583     case Hexagon::JMP_GTriNotPt_nv_V4:
584     case Hexagon::JMP_GTriNotPnt_nv_V4:
585
586     // JMP_GTri - with -1
587     case Hexagon::JMP_GTriPtneg_nv_V4:
588     case Hexagon::JMP_GTriPntneg_nv_V4:
589     case Hexagon::JMP_GTriNotPtneg_nv_V4:
590     case Hexagon::JMP_GTriNotPntneg_nv_V4:
591
592     // JMP_GTrr
593     case Hexagon::JMP_GTrrPt_nv_V4:
594     case Hexagon::JMP_GTrrPnt_nv_V4:
595     case Hexagon::JMP_GTrrNotPt_nv_V4:
596     case Hexagon::JMP_GTrrNotPnt_nv_V4:
597
598     // JMP_GTrrdn
599     case Hexagon::JMP_GTrrdnPt_nv_V4:
600     case Hexagon::JMP_GTrrdnPnt_nv_V4:
601     case Hexagon::JMP_GTrrdnNotPt_nv_V4:
602     case Hexagon::JMP_GTrrdnNotPnt_nv_V4:
603
604     // JMP_GTUri
605     case Hexagon::JMP_GTUriPt_nv_V4:
606     case Hexagon::JMP_GTUriPnt_nv_V4:
607     case Hexagon::JMP_GTUriNotPt_nv_V4:
608     case Hexagon::JMP_GTUriNotPnt_nv_V4:
609
610     // JMP_GTUrr
611     case Hexagon::JMP_GTUrrPt_nv_V4:
612     case Hexagon::JMP_GTUrrPnt_nv_V4:
613     case Hexagon::JMP_GTUrrNotPt_nv_V4:
614     case Hexagon::JMP_GTUrrNotPnt_nv_V4:
615
616     // JMP_GTUrrdn
617     case Hexagon::JMP_GTUrrdnPt_nv_V4:
618     case Hexagon::JMP_GTUrrdnPnt_nv_V4:
619     case Hexagon::JMP_GTUrrdnNotPt_nv_V4:
620     case Hexagon::JMP_GTUrrdnNotPnt_nv_V4:
621
622     // TFR_FI
623     case Hexagon::TFR_FI:
624       return true;
625   }
626 }
627
628 bool HexagonInstrInfo::isExtended(const MachineInstr *MI) const {
629   switch(MI->getOpcode()) {
630     default: return false;
631     // JMP_EQri
632     case Hexagon::JMP_EQriPt_ie_nv_V4:
633     case Hexagon::JMP_EQriPnt_ie_nv_V4:
634     case Hexagon::JMP_EQriNotPt_ie_nv_V4:
635     case Hexagon::JMP_EQriNotPnt_ie_nv_V4:
636
637     // JMP_EQri - with -1
638     case Hexagon::JMP_EQriPtneg_ie_nv_V4:
639     case Hexagon::JMP_EQriPntneg_ie_nv_V4:
640     case Hexagon::JMP_EQriNotPtneg_ie_nv_V4:
641     case Hexagon::JMP_EQriNotPntneg_ie_nv_V4:
642
643     // JMP_EQrr
644     case Hexagon::JMP_EQrrPt_ie_nv_V4:
645     case Hexagon::JMP_EQrrPnt_ie_nv_V4:
646     case Hexagon::JMP_EQrrNotPt_ie_nv_V4:
647     case Hexagon::JMP_EQrrNotPnt_ie_nv_V4:
648
649     // JMP_GTri
650     case Hexagon::JMP_GTriPt_ie_nv_V4:
651     case Hexagon::JMP_GTriPnt_ie_nv_V4:
652     case Hexagon::JMP_GTriNotPt_ie_nv_V4:
653     case Hexagon::JMP_GTriNotPnt_ie_nv_V4:
654
655     // JMP_GTri - with -1
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_ie_nv_V4:
663     case Hexagon::JMP_GTrrPnt_ie_nv_V4:
664     case Hexagon::JMP_GTrrNotPt_ie_nv_V4:
665     case Hexagon::JMP_GTrrNotPnt_ie_nv_V4:
666
667     // JMP_GTrrdn
668     case Hexagon::JMP_GTrrdnPt_ie_nv_V4:
669     case Hexagon::JMP_GTrrdnPnt_ie_nv_V4:
670     case Hexagon::JMP_GTrrdnNotPt_ie_nv_V4:
671     case Hexagon::JMP_GTrrdnNotPnt_ie_nv_V4:
672
673     // JMP_GTUri
674     case Hexagon::JMP_GTUriPt_ie_nv_V4:
675     case Hexagon::JMP_GTUriPnt_ie_nv_V4:
676     case Hexagon::JMP_GTUriNotPt_ie_nv_V4:
677     case Hexagon::JMP_GTUriNotPnt_ie_nv_V4:
678
679     // JMP_GTUrr
680     case Hexagon::JMP_GTUrrPt_ie_nv_V4:
681     case Hexagon::JMP_GTUrrPnt_ie_nv_V4:
682     case Hexagon::JMP_GTUrrNotPt_ie_nv_V4:
683     case Hexagon::JMP_GTUrrNotPnt_ie_nv_V4:
684
685     // JMP_GTUrrdn
686     case Hexagon::JMP_GTUrrdnPt_ie_nv_V4:
687     case Hexagon::JMP_GTUrrdnPnt_ie_nv_V4:
688     case Hexagon::JMP_GTUrrdnNotPt_ie_nv_V4:
689     case Hexagon::JMP_GTUrrdnNotPnt_ie_nv_V4:
690
691     // V4 absolute set addressing.
692     case Hexagon::LDrid_abs_setimm_V4:
693     case Hexagon::LDriw_abs_setimm_V4:
694     case Hexagon::LDrih_abs_setimm_V4:
695     case Hexagon::LDrib_abs_setimm_V4:
696     case Hexagon::LDriuh_abs_setimm_V4:
697     case Hexagon::LDriub_abs_setimm_V4:
698
699     case Hexagon::STrid_abs_setimm_V4:
700     case Hexagon::STrib_abs_setimm_V4:
701     case Hexagon::STrih_abs_setimm_V4:
702     case Hexagon::STriw_abs_setimm_V4:
703
704     // V4 global address load.
705     case Hexagon::LDd_GP_cPt_V4 :
706     case Hexagon::LDd_GP_cNotPt_V4 :
707     case Hexagon::LDd_GP_cdnPt_V4 :
708     case Hexagon::LDd_GP_cdnNotPt_V4 :
709     case Hexagon::LDb_GP_cPt_V4 :
710     case Hexagon::LDb_GP_cNotPt_V4 :
711     case Hexagon::LDb_GP_cdnPt_V4 :
712     case Hexagon::LDb_GP_cdnNotPt_V4 :
713     case Hexagon::LDub_GP_cPt_V4 :
714     case Hexagon::LDub_GP_cNotPt_V4 :
715     case Hexagon::LDub_GP_cdnPt_V4 :
716     case Hexagon::LDub_GP_cdnNotPt_V4 :
717     case Hexagon::LDh_GP_cPt_V4 :
718     case Hexagon::LDh_GP_cNotPt_V4 :
719     case Hexagon::LDh_GP_cdnPt_V4 :
720     case Hexagon::LDh_GP_cdnNotPt_V4 :
721     case Hexagon::LDuh_GP_cPt_V4 :
722     case Hexagon::LDuh_GP_cNotPt_V4 :
723     case Hexagon::LDuh_GP_cdnPt_V4 :
724     case Hexagon::LDuh_GP_cdnNotPt_V4 :
725     case Hexagon::LDw_GP_cPt_V4 :
726     case Hexagon::LDw_GP_cNotPt_V4 :
727     case Hexagon::LDw_GP_cdnPt_V4 :
728     case Hexagon::LDw_GP_cdnNotPt_V4 :
729
730     // V4 global address store.
731     case Hexagon::STd_GP_cPt_V4 :
732     case Hexagon::STd_GP_cNotPt_V4 :
733     case Hexagon::STd_GP_cdnPt_V4 :
734     case Hexagon::STd_GP_cdnNotPt_V4 :
735     case Hexagon::STb_GP_cPt_V4 :
736     case Hexagon::STb_GP_cNotPt_V4 :
737     case Hexagon::STb_GP_cdnPt_V4 :
738     case Hexagon::STb_GP_cdnNotPt_V4 :
739     case Hexagon::STh_GP_cPt_V4 :
740     case Hexagon::STh_GP_cNotPt_V4 :
741     case Hexagon::STh_GP_cdnPt_V4 :
742     case Hexagon::STh_GP_cdnNotPt_V4 :
743     case Hexagon::STw_GP_cPt_V4 :
744     case Hexagon::STw_GP_cNotPt_V4 :
745     case Hexagon::STw_GP_cdnPt_V4 :
746     case Hexagon::STw_GP_cdnNotPt_V4 :
747
748     // V4 predicated global address new value store.
749     case Hexagon::STb_GP_cPt_nv_V4 :
750     case Hexagon::STb_GP_cNotPt_nv_V4 :
751     case Hexagon::STb_GP_cdnPt_nv_V4 :
752     case Hexagon::STb_GP_cdnNotPt_nv_V4 :
753     case Hexagon::STh_GP_cPt_nv_V4 :
754     case Hexagon::STh_GP_cNotPt_nv_V4 :
755     case Hexagon::STh_GP_cdnPt_nv_V4 :
756     case Hexagon::STh_GP_cdnNotPt_nv_V4 :
757     case Hexagon::STw_GP_cPt_nv_V4 :
758     case Hexagon::STw_GP_cNotPt_nv_V4 :
759     case Hexagon::STw_GP_cdnPt_nv_V4 :
760     case Hexagon::STw_GP_cdnNotPt_nv_V4 :
761
762     // TFR_FI
763     case Hexagon::TFR_FI_immext_V4:
764
765     // TFRI_F
766     case Hexagon::TFRI_f:
767     case Hexagon::TFRI_cPt_f:
768     case Hexagon::TFRI_cNotPt_f:
769     case Hexagon::CONST64_Float_Real:
770       return true;
771   }
772 }
773
774 bool HexagonInstrInfo::isNewValueJump(const MachineInstr *MI) const {
775   switch (MI->getOpcode()) {
776     default: return false;
777     // JMP_EQri
778     case Hexagon::JMP_EQriPt_nv_V4:
779     case Hexagon::JMP_EQriPnt_nv_V4:
780     case Hexagon::JMP_EQriNotPt_nv_V4:
781     case Hexagon::JMP_EQriNotPnt_nv_V4:
782     case Hexagon::JMP_EQriPt_ie_nv_V4:
783     case Hexagon::JMP_EQriPnt_ie_nv_V4:
784     case Hexagon::JMP_EQriNotPt_ie_nv_V4:
785     case Hexagon::JMP_EQriNotPnt_ie_nv_V4:
786
787     // JMP_EQri - with -1
788     case Hexagon::JMP_EQriPtneg_nv_V4:
789     case Hexagon::JMP_EQriPntneg_nv_V4:
790     case Hexagon::JMP_EQriNotPtneg_nv_V4:
791     case Hexagon::JMP_EQriNotPntneg_nv_V4:
792     case Hexagon::JMP_EQriPtneg_ie_nv_V4:
793     case Hexagon::JMP_EQriPntneg_ie_nv_V4:
794     case Hexagon::JMP_EQriNotPtneg_ie_nv_V4:
795     case Hexagon::JMP_EQriNotPntneg_ie_nv_V4:
796
797     // JMP_EQrr
798     case Hexagon::JMP_EQrrPt_nv_V4:
799     case Hexagon::JMP_EQrrPnt_nv_V4:
800     case Hexagon::JMP_EQrrNotPt_nv_V4:
801     case Hexagon::JMP_EQrrNotPnt_nv_V4:
802     case Hexagon::JMP_EQrrPt_ie_nv_V4:
803     case Hexagon::JMP_EQrrPnt_ie_nv_V4:
804     case Hexagon::JMP_EQrrNotPt_ie_nv_V4:
805     case Hexagon::JMP_EQrrNotPnt_ie_nv_V4:
806
807     // JMP_GTri
808     case Hexagon::JMP_GTriPt_nv_V4:
809     case Hexagon::JMP_GTriPnt_nv_V4:
810     case Hexagon::JMP_GTriNotPt_nv_V4:
811     case Hexagon::JMP_GTriNotPnt_nv_V4:
812     case Hexagon::JMP_GTriPt_ie_nv_V4:
813     case Hexagon::JMP_GTriPnt_ie_nv_V4:
814     case Hexagon::JMP_GTriNotPt_ie_nv_V4:
815     case Hexagon::JMP_GTriNotPnt_ie_nv_V4:
816
817     // JMP_GTri - with -1
818     case Hexagon::JMP_GTriPtneg_nv_V4:
819     case Hexagon::JMP_GTriPntneg_nv_V4:
820     case Hexagon::JMP_GTriNotPtneg_nv_V4:
821     case Hexagon::JMP_GTriNotPntneg_nv_V4:
822     case Hexagon::JMP_GTriPtneg_ie_nv_V4:
823     case Hexagon::JMP_GTriPntneg_ie_nv_V4:
824     case Hexagon::JMP_GTriNotPtneg_ie_nv_V4:
825     case Hexagon::JMP_GTriNotPntneg_ie_nv_V4:
826
827     // JMP_GTrr
828     case Hexagon::JMP_GTrrPt_nv_V4:
829     case Hexagon::JMP_GTrrPnt_nv_V4:
830     case Hexagon::JMP_GTrrNotPt_nv_V4:
831     case Hexagon::JMP_GTrrNotPnt_nv_V4:
832     case Hexagon::JMP_GTrrPt_ie_nv_V4:
833     case Hexagon::JMP_GTrrPnt_ie_nv_V4:
834     case Hexagon::JMP_GTrrNotPt_ie_nv_V4:
835     case Hexagon::JMP_GTrrNotPnt_ie_nv_V4:
836
837     // JMP_GTrrdn
838     case Hexagon::JMP_GTrrdnPt_nv_V4:
839     case Hexagon::JMP_GTrrdnPnt_nv_V4:
840     case Hexagon::JMP_GTrrdnNotPt_nv_V4:
841     case Hexagon::JMP_GTrrdnNotPnt_nv_V4:
842     case Hexagon::JMP_GTrrdnPt_ie_nv_V4:
843     case Hexagon::JMP_GTrrdnPnt_ie_nv_V4:
844     case Hexagon::JMP_GTrrdnNotPt_ie_nv_V4:
845     case Hexagon::JMP_GTrrdnNotPnt_ie_nv_V4:
846
847     // JMP_GTUri
848     case Hexagon::JMP_GTUriPt_nv_V4:
849     case Hexagon::JMP_GTUriPnt_nv_V4:
850     case Hexagon::JMP_GTUriNotPt_nv_V4:
851     case Hexagon::JMP_GTUriNotPnt_nv_V4:
852     case Hexagon::JMP_GTUriPt_ie_nv_V4:
853     case Hexagon::JMP_GTUriPnt_ie_nv_V4:
854     case Hexagon::JMP_GTUriNotPt_ie_nv_V4:
855     case Hexagon::JMP_GTUriNotPnt_ie_nv_V4:
856
857     // JMP_GTUrr
858     case Hexagon::JMP_GTUrrPt_nv_V4:
859     case Hexagon::JMP_GTUrrPnt_nv_V4:
860     case Hexagon::JMP_GTUrrNotPt_nv_V4:
861     case Hexagon::JMP_GTUrrNotPnt_nv_V4:
862     case Hexagon::JMP_GTUrrPt_ie_nv_V4:
863     case Hexagon::JMP_GTUrrPnt_ie_nv_V4:
864     case Hexagon::JMP_GTUrrNotPt_ie_nv_V4:
865     case Hexagon::JMP_GTUrrNotPnt_ie_nv_V4:
866
867     // JMP_GTUrrdn
868     case Hexagon::JMP_GTUrrdnPt_nv_V4:
869     case Hexagon::JMP_GTUrrdnPnt_nv_V4:
870     case Hexagon::JMP_GTUrrdnNotPt_nv_V4:
871     case Hexagon::JMP_GTUrrdnNotPnt_nv_V4:
872     case Hexagon::JMP_GTUrrdnPt_ie_nv_V4:
873     case Hexagon::JMP_GTUrrdnPnt_ie_nv_V4:
874     case Hexagon::JMP_GTUrrdnNotPt_ie_nv_V4:
875     case Hexagon::JMP_GTUrrdnNotPnt_ie_nv_V4:
876       return true;
877   }
878 }
879
880 unsigned HexagonInstrInfo::getImmExtForm(const MachineInstr* MI) const {
881   switch(MI->getOpcode()) {
882     default: llvm_unreachable("Unknown type of instruction.");
883     // JMP_EQri
884     case Hexagon::JMP_EQriPt_nv_V4:
885       return Hexagon::JMP_EQriPt_ie_nv_V4;
886     case Hexagon::JMP_EQriNotPt_nv_V4:
887       return Hexagon::JMP_EQriNotPt_ie_nv_V4;
888     case Hexagon::JMP_EQriPnt_nv_V4:
889       return Hexagon::JMP_EQriPnt_ie_nv_V4;
890     case Hexagon::JMP_EQriNotPnt_nv_V4:
891       return Hexagon::JMP_EQriNotPnt_ie_nv_V4;
892
893     // JMP_EQri -- with -1
894     case Hexagon::JMP_EQriPtneg_nv_V4:
895       return Hexagon::JMP_EQriPtneg_ie_nv_V4;
896     case Hexagon::JMP_EQriNotPtneg_nv_V4:
897       return Hexagon::JMP_EQriNotPtneg_ie_nv_V4;
898     case Hexagon::JMP_EQriPntneg_nv_V4:
899       return Hexagon::JMP_EQriPntneg_ie_nv_V4;
900     case Hexagon::JMP_EQriNotPntneg_nv_V4:
901       return Hexagon::JMP_EQriNotPntneg_ie_nv_V4;
902
903     // JMP_EQrr
904     case Hexagon::JMP_EQrrPt_nv_V4:
905       return Hexagon::JMP_EQrrPt_ie_nv_V4;
906     case Hexagon::JMP_EQrrNotPt_nv_V4:
907       return Hexagon::JMP_EQrrNotPt_ie_nv_V4;
908     case Hexagon::JMP_EQrrPnt_nv_V4:
909       return Hexagon::JMP_EQrrPnt_ie_nv_V4;
910     case Hexagon::JMP_EQrrNotPnt_nv_V4:
911       return Hexagon::JMP_EQrrNotPnt_ie_nv_V4;
912
913     // JMP_GTri
914     case Hexagon::JMP_GTriPt_nv_V4:
915       return Hexagon::JMP_GTriPt_ie_nv_V4;
916     case Hexagon::JMP_GTriNotPt_nv_V4:
917       return Hexagon::JMP_GTriNotPt_ie_nv_V4;
918     case Hexagon::JMP_GTriPnt_nv_V4:
919       return Hexagon::JMP_GTriPnt_ie_nv_V4;
920     case Hexagon::JMP_GTriNotPnt_nv_V4:
921       return Hexagon::JMP_GTriNotPnt_ie_nv_V4;
922
923     // JMP_GTri -- with -1
924     case Hexagon::JMP_GTriPtneg_nv_V4:
925       return Hexagon::JMP_GTriPtneg_ie_nv_V4;
926     case Hexagon::JMP_GTriNotPtneg_nv_V4:
927       return Hexagon::JMP_GTriNotPtneg_ie_nv_V4;
928     case Hexagon::JMP_GTriPntneg_nv_V4:
929       return Hexagon::JMP_GTriPntneg_ie_nv_V4;
930     case Hexagon::JMP_GTriNotPntneg_nv_V4:
931       return Hexagon::JMP_GTriNotPntneg_ie_nv_V4;
932
933     // JMP_GTrr
934     case Hexagon::JMP_GTrrPt_nv_V4:
935       return Hexagon::JMP_GTrrPt_ie_nv_V4;
936     case Hexagon::JMP_GTrrNotPt_nv_V4:
937       return Hexagon::JMP_GTrrNotPt_ie_nv_V4;
938     case Hexagon::JMP_GTrrPnt_nv_V4:
939       return Hexagon::JMP_GTrrPnt_ie_nv_V4;
940     case Hexagon::JMP_GTrrNotPnt_nv_V4:
941       return Hexagon::JMP_GTrrNotPnt_ie_nv_V4;
942
943     // JMP_GTrrdn
944     case Hexagon::JMP_GTrrdnPt_nv_V4:
945       return Hexagon::JMP_GTrrdnPt_ie_nv_V4;
946     case Hexagon::JMP_GTrrdnNotPt_nv_V4:
947       return Hexagon::JMP_GTrrdnNotPt_ie_nv_V4;
948     case Hexagon::JMP_GTrrdnPnt_nv_V4:
949       return Hexagon::JMP_GTrrdnPnt_ie_nv_V4;
950     case Hexagon::JMP_GTrrdnNotPnt_nv_V4:
951       return Hexagon::JMP_GTrrdnNotPnt_ie_nv_V4;
952
953     // JMP_GTUri
954     case Hexagon::JMP_GTUriPt_nv_V4:
955       return Hexagon::JMP_GTUriPt_ie_nv_V4;
956     case Hexagon::JMP_GTUriNotPt_nv_V4:
957       return Hexagon::JMP_GTUriNotPt_ie_nv_V4;
958     case Hexagon::JMP_GTUriPnt_nv_V4:
959       return Hexagon::JMP_GTUriPnt_ie_nv_V4;
960     case Hexagon::JMP_GTUriNotPnt_nv_V4:
961       return Hexagon::JMP_GTUriNotPnt_ie_nv_V4;
962
963     // JMP_GTUrr
964     case Hexagon::JMP_GTUrrPt_nv_V4:
965       return Hexagon::JMP_GTUrrPt_ie_nv_V4;
966     case Hexagon::JMP_GTUrrNotPt_nv_V4:
967       return Hexagon::JMP_GTUrrNotPt_ie_nv_V4;
968     case Hexagon::JMP_GTUrrPnt_nv_V4:
969       return Hexagon::JMP_GTUrrPnt_ie_nv_V4;
970     case Hexagon::JMP_GTUrrNotPnt_nv_V4:
971       return Hexagon::JMP_GTUrrNotPnt_ie_nv_V4;
972
973     // JMP_GTUrrdn
974     case Hexagon::JMP_GTUrrdnPt_nv_V4:
975       return Hexagon::JMP_GTUrrdnPt_ie_nv_V4;
976     case Hexagon::JMP_GTUrrdnNotPt_nv_V4:
977       return Hexagon::JMP_GTUrrdnNotPt_ie_nv_V4;
978     case Hexagon::JMP_GTUrrdnPnt_nv_V4:
979       return Hexagon::JMP_GTUrrdnPnt_ie_nv_V4;
980     case Hexagon::JMP_GTUrrdnNotPnt_nv_V4:
981       return Hexagon::JMP_GTUrrdnNotPnt_ie_nv_V4;
982
983     case Hexagon::TFR_FI:
984         return Hexagon::TFR_FI_immext_V4;
985
986     case Hexagon::MEMw_ADDi_indexed_MEM_V4 :
987     case Hexagon::MEMw_SUBi_indexed_MEM_V4 :
988     case Hexagon::MEMw_ADDr_indexed_MEM_V4 :
989     case Hexagon::MEMw_SUBr_indexed_MEM_V4 :
990     case Hexagon::MEMw_ANDr_indexed_MEM_V4 :
991     case Hexagon::MEMw_ORr_indexed_MEM_V4 :
992     case Hexagon::MEMw_ADDi_MEM_V4 :
993     case Hexagon::MEMw_SUBi_MEM_V4 :
994     case Hexagon::MEMw_ADDr_MEM_V4 :
995     case Hexagon::MEMw_SUBr_MEM_V4 :
996     case Hexagon::MEMw_ANDr_MEM_V4 :
997     case Hexagon::MEMw_ORr_MEM_V4 :
998     case Hexagon::MEMh_ADDi_indexed_MEM_V4 :
999     case Hexagon::MEMh_SUBi_indexed_MEM_V4 :
1000     case Hexagon::MEMh_ADDr_indexed_MEM_V4 :
1001     case Hexagon::MEMh_SUBr_indexed_MEM_V4 :
1002     case Hexagon::MEMh_ANDr_indexed_MEM_V4 :
1003     case Hexagon::MEMh_ORr_indexed_MEM_V4 :
1004     case Hexagon::MEMh_ADDi_MEM_V4 :
1005     case Hexagon::MEMh_SUBi_MEM_V4 :
1006     case Hexagon::MEMh_ADDr_MEM_V4 :
1007     case Hexagon::MEMh_SUBr_MEM_V4 :
1008     case Hexagon::MEMh_ANDr_MEM_V4 :
1009     case Hexagon::MEMh_ORr_MEM_V4 :
1010     case Hexagon::MEMb_ADDi_indexed_MEM_V4 :
1011     case Hexagon::MEMb_SUBi_indexed_MEM_V4 :
1012     case Hexagon::MEMb_ADDr_indexed_MEM_V4 :
1013     case Hexagon::MEMb_SUBr_indexed_MEM_V4 :
1014     case Hexagon::MEMb_ANDr_indexed_MEM_V4 :
1015     case Hexagon::MEMb_ORr_indexed_MEM_V4 :
1016     case Hexagon::MEMb_ADDi_MEM_V4 :
1017     case Hexagon::MEMb_SUBi_MEM_V4 :
1018     case Hexagon::MEMb_ADDr_MEM_V4 :
1019     case Hexagon::MEMb_SUBr_MEM_V4 :
1020     case Hexagon::MEMb_ANDr_MEM_V4 :
1021     case Hexagon::MEMb_ORr_MEM_V4 :
1022       llvm_unreachable("Needs implementing.");
1023   }
1024 }
1025
1026 unsigned HexagonInstrInfo::getNormalBranchForm(const MachineInstr* MI) const {
1027   switch(MI->getOpcode()) {
1028     default: llvm_unreachable("Unknown type of jump instruction.");
1029     // JMP_EQri
1030     case Hexagon::JMP_EQriPt_ie_nv_V4:
1031       return Hexagon::JMP_EQriPt_nv_V4;
1032     case Hexagon::JMP_EQriNotPt_ie_nv_V4:
1033       return Hexagon::JMP_EQriNotPt_nv_V4;
1034     case Hexagon::JMP_EQriPnt_ie_nv_V4:
1035       return Hexagon::JMP_EQriPnt_nv_V4;
1036     case Hexagon::JMP_EQriNotPnt_ie_nv_V4:
1037       return Hexagon::JMP_EQriNotPnt_nv_V4;
1038
1039     // JMP_EQri -- with -1
1040     case Hexagon::JMP_EQriPtneg_ie_nv_V4:
1041       return Hexagon::JMP_EQriPtneg_nv_V4;
1042     case Hexagon::JMP_EQriNotPtneg_ie_nv_V4:
1043       return Hexagon::JMP_EQriNotPtneg_nv_V4;
1044     case Hexagon::JMP_EQriPntneg_ie_nv_V4:
1045       return Hexagon::JMP_EQriPntneg_nv_V4;
1046     case Hexagon::JMP_EQriNotPntneg_ie_nv_V4:
1047       return Hexagon::JMP_EQriNotPntneg_nv_V4;
1048
1049     // JMP_EQrr
1050     case Hexagon::JMP_EQrrPt_ie_nv_V4:
1051       return Hexagon::JMP_EQrrPt_nv_V4;
1052     case Hexagon::JMP_EQrrNotPt_ie_nv_V4:
1053       return Hexagon::JMP_EQrrNotPt_nv_V4;
1054     case Hexagon::JMP_EQrrPnt_ie_nv_V4:
1055       return Hexagon::JMP_EQrrPnt_nv_V4;
1056     case Hexagon::JMP_EQrrNotPnt_ie_nv_V4:
1057       return Hexagon::JMP_EQrrNotPnt_nv_V4;
1058
1059     // JMP_GTri
1060     case Hexagon::JMP_GTriPt_ie_nv_V4:
1061       return Hexagon::JMP_GTriPt_nv_V4;
1062     case Hexagon::JMP_GTriNotPt_ie_nv_V4:
1063       return Hexagon::JMP_GTriNotPt_nv_V4;
1064     case Hexagon::JMP_GTriPnt_ie_nv_V4:
1065       return Hexagon::JMP_GTriPnt_nv_V4;
1066     case Hexagon::JMP_GTriNotPnt_ie_nv_V4:
1067       return Hexagon::JMP_GTriNotPnt_nv_V4;
1068
1069     // JMP_GTri -- with -1
1070     case Hexagon::JMP_GTriPtneg_ie_nv_V4:
1071       return Hexagon::JMP_GTriPtneg_nv_V4;
1072     case Hexagon::JMP_GTriNotPtneg_ie_nv_V4:
1073       return Hexagon::JMP_GTriNotPtneg_nv_V4;
1074     case Hexagon::JMP_GTriPntneg_ie_nv_V4:
1075       return Hexagon::JMP_GTriPntneg_nv_V4;
1076     case Hexagon::JMP_GTriNotPntneg_ie_nv_V4:
1077       return Hexagon::JMP_GTriNotPntneg_nv_V4;
1078
1079     // JMP_GTrr
1080     case Hexagon::JMP_GTrrPt_ie_nv_V4:
1081       return Hexagon::JMP_GTrrPt_nv_V4;
1082     case Hexagon::JMP_GTrrNotPt_ie_nv_V4:
1083       return Hexagon::JMP_GTrrNotPt_nv_V4;
1084     case Hexagon::JMP_GTrrPnt_ie_nv_V4:
1085       return Hexagon::JMP_GTrrPnt_nv_V4;
1086     case Hexagon::JMP_GTrrNotPnt_ie_nv_V4:
1087       return Hexagon::JMP_GTrrNotPnt_nv_V4;
1088
1089     // JMP_GTrrdn
1090     case Hexagon::JMP_GTrrdnPt_ie_nv_V4:
1091       return Hexagon::JMP_GTrrdnPt_nv_V4;
1092     case Hexagon::JMP_GTrrdnNotPt_ie_nv_V4:
1093       return Hexagon::JMP_GTrrdnNotPt_nv_V4;
1094     case Hexagon::JMP_GTrrdnPnt_ie_nv_V4:
1095       return Hexagon::JMP_GTrrdnPnt_nv_V4;
1096     case Hexagon::JMP_GTrrdnNotPnt_ie_nv_V4:
1097       return Hexagon::JMP_GTrrdnNotPnt_nv_V4;
1098
1099     // JMP_GTUri
1100     case Hexagon::JMP_GTUriPt_ie_nv_V4:
1101       return Hexagon::JMP_GTUriPt_nv_V4;
1102     case Hexagon::JMP_GTUriNotPt_ie_nv_V4:
1103       return Hexagon::JMP_GTUriNotPt_nv_V4;
1104     case Hexagon::JMP_GTUriPnt_ie_nv_V4:
1105       return Hexagon::JMP_GTUriPnt_nv_V4;
1106     case Hexagon::JMP_GTUriNotPnt_ie_nv_V4:
1107       return Hexagon::JMP_GTUriNotPnt_nv_V4;
1108
1109     // JMP_GTUrr
1110     case Hexagon::JMP_GTUrrPt_ie_nv_V4:
1111       return Hexagon::JMP_GTUrrPt_nv_V4;
1112     case Hexagon::JMP_GTUrrNotPt_ie_nv_V4:
1113       return Hexagon::JMP_GTUrrNotPt_nv_V4;
1114     case Hexagon::JMP_GTUrrPnt_ie_nv_V4:
1115       return Hexagon::JMP_GTUrrPnt_nv_V4;
1116     case Hexagon::JMP_GTUrrNotPnt_ie_nv_V4:
1117       return Hexagon::JMP_GTUrrNotPnt_nv_V4;
1118
1119     // JMP_GTUrrdn
1120     case Hexagon::JMP_GTUrrdnPt_ie_nv_V4:
1121       return Hexagon::JMP_GTUrrdnPt_nv_V4;
1122     case Hexagon::JMP_GTUrrdnNotPt_ie_nv_V4:
1123       return Hexagon::JMP_GTUrrdnNotPt_nv_V4;
1124     case Hexagon::JMP_GTUrrdnPnt_ie_nv_V4:
1125       return Hexagon::JMP_GTUrrdnPnt_nv_V4;
1126     case Hexagon::JMP_GTUrrdnNotPnt_ie_nv_V4:
1127       return Hexagon::JMP_GTUrrdnNotPnt_nv_V4;
1128   }
1129 }
1130
1131
1132 bool HexagonInstrInfo::isNewValueStore(const MachineInstr *MI) const {
1133   switch (MI->getOpcode()) {
1134     default: return false;
1135     // Store Byte
1136     case Hexagon::STrib_nv_V4:
1137     case Hexagon::STrib_indexed_nv_V4:
1138     case Hexagon::STrib_indexed_shl_nv_V4:
1139     case Hexagon::STrib_shl_nv_V4:
1140     case Hexagon::STb_GP_nv_V4:
1141     case Hexagon::POST_STbri_nv_V4:
1142     case Hexagon::STrib_cPt_nv_V4:
1143     case Hexagon::STrib_cdnPt_nv_V4:
1144     case Hexagon::STrib_cNotPt_nv_V4:
1145     case Hexagon::STrib_cdnNotPt_nv_V4:
1146     case Hexagon::STrib_indexed_cPt_nv_V4:
1147     case Hexagon::STrib_indexed_cdnPt_nv_V4:
1148     case Hexagon::STrib_indexed_cNotPt_nv_V4:
1149     case Hexagon::STrib_indexed_cdnNotPt_nv_V4:
1150     case Hexagon::STrib_indexed_shl_cPt_nv_V4:
1151     case Hexagon::STrib_indexed_shl_cdnPt_nv_V4:
1152     case Hexagon::STrib_indexed_shl_cNotPt_nv_V4:
1153     case Hexagon::STrib_indexed_shl_cdnNotPt_nv_V4:
1154     case Hexagon::POST_STbri_cPt_nv_V4:
1155     case Hexagon::POST_STbri_cdnPt_nv_V4:
1156     case Hexagon::POST_STbri_cNotPt_nv_V4:
1157     case Hexagon::POST_STbri_cdnNotPt_nv_V4:
1158     case Hexagon::STb_GP_cPt_nv_V4:
1159     case Hexagon::STb_GP_cNotPt_nv_V4:
1160     case Hexagon::STb_GP_cdnPt_nv_V4:
1161     case Hexagon::STb_GP_cdnNotPt_nv_V4:
1162     case Hexagon::STrib_abs_nv_V4:
1163     case Hexagon::STrib_abs_cPt_nv_V4:
1164     case Hexagon::STrib_abs_cdnPt_nv_V4:
1165     case Hexagon::STrib_abs_cNotPt_nv_V4:
1166     case Hexagon::STrib_abs_cdnNotPt_nv_V4:
1167     case Hexagon::STrib_imm_abs_nv_V4:
1168     case Hexagon::STrib_imm_abs_cPt_nv_V4:
1169     case Hexagon::STrib_imm_abs_cdnPt_nv_V4:
1170     case Hexagon::STrib_imm_abs_cNotPt_nv_V4:
1171     case Hexagon::STrib_imm_abs_cdnNotPt_nv_V4:
1172
1173     // Store Halfword
1174     case Hexagon::STrih_nv_V4:
1175     case Hexagon::STrih_indexed_nv_V4:
1176     case Hexagon::STrih_indexed_shl_nv_V4:
1177     case Hexagon::STrih_shl_nv_V4:
1178     case Hexagon::STh_GP_nv_V4:
1179     case Hexagon::POST_SThri_nv_V4:
1180     case Hexagon::STrih_cPt_nv_V4:
1181     case Hexagon::STrih_cdnPt_nv_V4:
1182     case Hexagon::STrih_cNotPt_nv_V4:
1183     case Hexagon::STrih_cdnNotPt_nv_V4:
1184     case Hexagon::STrih_indexed_cPt_nv_V4:
1185     case Hexagon::STrih_indexed_cdnPt_nv_V4:
1186     case Hexagon::STrih_indexed_cNotPt_nv_V4:
1187     case Hexagon::STrih_indexed_cdnNotPt_nv_V4:
1188     case Hexagon::STrih_indexed_shl_cPt_nv_V4:
1189     case Hexagon::STrih_indexed_shl_cdnPt_nv_V4:
1190     case Hexagon::STrih_indexed_shl_cNotPt_nv_V4:
1191     case Hexagon::STrih_indexed_shl_cdnNotPt_nv_V4:
1192     case Hexagon::POST_SThri_cPt_nv_V4:
1193     case Hexagon::POST_SThri_cdnPt_nv_V4:
1194     case Hexagon::POST_SThri_cNotPt_nv_V4:
1195     case Hexagon::POST_SThri_cdnNotPt_nv_V4:
1196     case Hexagon::STh_GP_cPt_nv_V4:
1197     case Hexagon::STh_GP_cNotPt_nv_V4:
1198     case Hexagon::STh_GP_cdnPt_nv_V4:
1199     case Hexagon::STh_GP_cdnNotPt_nv_V4:
1200     case Hexagon::STrih_abs_nv_V4:
1201     case Hexagon::STrih_abs_cPt_nv_V4:
1202     case Hexagon::STrih_abs_cdnPt_nv_V4:
1203     case Hexagon::STrih_abs_cNotPt_nv_V4:
1204     case Hexagon::STrih_abs_cdnNotPt_nv_V4:
1205     case Hexagon::STrih_imm_abs_nv_V4:
1206     case Hexagon::STrih_imm_abs_cPt_nv_V4:
1207     case Hexagon::STrih_imm_abs_cdnPt_nv_V4:
1208     case Hexagon::STrih_imm_abs_cNotPt_nv_V4:
1209     case Hexagon::STrih_imm_abs_cdnNotPt_nv_V4:
1210
1211     // Store Word
1212     case Hexagon::STriw_nv_V4:
1213     case Hexagon::STriw_indexed_nv_V4:
1214     case Hexagon::STriw_indexed_shl_nv_V4:
1215     case Hexagon::STriw_shl_nv_V4:
1216     case Hexagon::STw_GP_nv_V4:
1217     case Hexagon::POST_STwri_nv_V4:
1218     case Hexagon::STriw_cPt_nv_V4:
1219     case Hexagon::STriw_cdnPt_nv_V4:
1220     case Hexagon::STriw_cNotPt_nv_V4:
1221     case Hexagon::STriw_cdnNotPt_nv_V4:
1222     case Hexagon::STriw_indexed_cPt_nv_V4:
1223     case Hexagon::STriw_indexed_cdnPt_nv_V4:
1224     case Hexagon::STriw_indexed_cNotPt_nv_V4:
1225     case Hexagon::STriw_indexed_cdnNotPt_nv_V4:
1226     case Hexagon::STriw_indexed_shl_cPt_nv_V4:
1227     case Hexagon::STriw_indexed_shl_cdnPt_nv_V4:
1228     case Hexagon::STriw_indexed_shl_cNotPt_nv_V4:
1229     case Hexagon::STriw_indexed_shl_cdnNotPt_nv_V4:
1230     case Hexagon::POST_STwri_cPt_nv_V4:
1231     case Hexagon::POST_STwri_cdnPt_nv_V4:
1232     case Hexagon::POST_STwri_cNotPt_nv_V4:
1233     case Hexagon::POST_STwri_cdnNotPt_nv_V4:
1234     case Hexagon::STw_GP_cPt_nv_V4:
1235     case Hexagon::STw_GP_cNotPt_nv_V4:
1236     case Hexagon::STw_GP_cdnPt_nv_V4:
1237     case Hexagon::STw_GP_cdnNotPt_nv_V4:
1238     case Hexagon::STriw_abs_nv_V4:
1239     case Hexagon::STriw_abs_cPt_nv_V4:
1240     case Hexagon::STriw_abs_cdnPt_nv_V4:
1241     case Hexagon::STriw_abs_cNotPt_nv_V4:
1242     case Hexagon::STriw_abs_cdnNotPt_nv_V4:
1243     case Hexagon::STriw_imm_abs_nv_V4:
1244     case Hexagon::STriw_imm_abs_cPt_nv_V4:
1245     case Hexagon::STriw_imm_abs_cdnPt_nv_V4:
1246     case Hexagon::STriw_imm_abs_cNotPt_nv_V4:
1247     case Hexagon::STriw_imm_abs_cdnNotPt_nv_V4:
1248       return true;
1249   }
1250 }
1251
1252 bool HexagonInstrInfo::isPostIncrement (const MachineInstr* MI) const {
1253   switch (MI->getOpcode())
1254   {
1255     default: return false;
1256     // Load Byte
1257     case Hexagon::POST_LDrib:
1258     case Hexagon::POST_LDrib_cPt:
1259     case Hexagon::POST_LDrib_cNotPt:
1260     case Hexagon::POST_LDrib_cdnPt_V4:
1261     case Hexagon::POST_LDrib_cdnNotPt_V4:
1262
1263     // Load unsigned byte
1264     case Hexagon::POST_LDriub:
1265     case Hexagon::POST_LDriub_cPt:
1266     case Hexagon::POST_LDriub_cNotPt:
1267     case Hexagon::POST_LDriub_cdnPt_V4:
1268     case Hexagon::POST_LDriub_cdnNotPt_V4:
1269
1270     // Load halfword
1271     case Hexagon::POST_LDrih:
1272     case Hexagon::POST_LDrih_cPt:
1273     case Hexagon::POST_LDrih_cNotPt:
1274     case Hexagon::POST_LDrih_cdnPt_V4:
1275     case Hexagon::POST_LDrih_cdnNotPt_V4:
1276
1277     // Load unsigned halfword
1278     case Hexagon::POST_LDriuh:
1279     case Hexagon::POST_LDriuh_cPt:
1280     case Hexagon::POST_LDriuh_cNotPt:
1281     case Hexagon::POST_LDriuh_cdnPt_V4:
1282     case Hexagon::POST_LDriuh_cdnNotPt_V4:
1283
1284     // Load word
1285     case Hexagon::POST_LDriw:
1286     case Hexagon::POST_LDriw_cPt:
1287     case Hexagon::POST_LDriw_cNotPt:
1288     case Hexagon::POST_LDriw_cdnPt_V4:
1289     case Hexagon::POST_LDriw_cdnNotPt_V4:
1290
1291     // Load double word
1292     case Hexagon::POST_LDrid:
1293     case Hexagon::POST_LDrid_cPt:
1294     case Hexagon::POST_LDrid_cNotPt:
1295     case Hexagon::POST_LDrid_cdnPt_V4:
1296     case Hexagon::POST_LDrid_cdnNotPt_V4:
1297
1298     // Store byte
1299     case Hexagon::POST_STbri:
1300     case Hexagon::POST_STbri_cPt:
1301     case Hexagon::POST_STbri_cNotPt:
1302     case Hexagon::POST_STbri_cdnPt_V4:
1303     case Hexagon::POST_STbri_cdnNotPt_V4:
1304
1305     // Store halfword
1306     case Hexagon::POST_SThri:
1307     case Hexagon::POST_SThri_cPt:
1308     case Hexagon::POST_SThri_cNotPt:
1309     case Hexagon::POST_SThri_cdnPt_V4:
1310     case Hexagon::POST_SThri_cdnNotPt_V4:
1311
1312     // Store word
1313     case Hexagon::POST_STwri:
1314     case Hexagon::POST_STwri_cPt:
1315     case Hexagon::POST_STwri_cNotPt:
1316     case Hexagon::POST_STwri_cdnPt_V4:
1317     case Hexagon::POST_STwri_cdnNotPt_V4:
1318
1319     // Store double word
1320     case Hexagon::POST_STdri:
1321     case Hexagon::POST_STdri_cPt:
1322     case Hexagon::POST_STdri_cNotPt:
1323     case Hexagon::POST_STdri_cdnPt_V4:
1324     case Hexagon::POST_STdri_cdnNotPt_V4:
1325       return true;
1326   }
1327 }
1328
1329 bool HexagonInstrInfo::isSaveCalleeSavedRegsCall(const MachineInstr *MI) const {
1330   return MI->getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4;
1331 }
1332
1333 bool HexagonInstrInfo::isPredicable(MachineInstr *MI) const {
1334   bool isPred = MI->getDesc().isPredicable();
1335
1336   if (!isPred)
1337     return false;
1338
1339   const int Opc = MI->getOpcode();
1340
1341   switch(Opc) {
1342   case Hexagon::TFRI:
1343     return isInt<12>(MI->getOperand(1).getImm());
1344
1345   case Hexagon::STrid:
1346   case Hexagon::STrid_indexed:
1347     return isShiftedUInt<6,3>(MI->getOperand(1).getImm());
1348
1349   case Hexagon::STriw:
1350   case Hexagon::STriw_indexed:
1351   case Hexagon::STriw_nv_V4:
1352     return isShiftedUInt<6,2>(MI->getOperand(1).getImm());
1353
1354   case Hexagon::STrih:
1355   case Hexagon::STrih_indexed:
1356   case Hexagon::STrih_nv_V4:
1357     return isShiftedUInt<6,1>(MI->getOperand(1).getImm());
1358
1359   case Hexagon::STrib:
1360   case Hexagon::STrib_indexed:
1361   case Hexagon::STrib_nv_V4:
1362     return isUInt<6>(MI->getOperand(1).getImm());
1363
1364   case Hexagon::LDrid:
1365   case Hexagon::LDrid_indexed:
1366     return isShiftedUInt<6,3>(MI->getOperand(2).getImm());
1367
1368   case Hexagon::LDriw:
1369   case Hexagon::LDriw_indexed:
1370     return isShiftedUInt<6,2>(MI->getOperand(2).getImm());
1371
1372   case Hexagon::LDrih:
1373   case Hexagon::LDriuh:
1374   case Hexagon::LDrih_indexed:
1375   case Hexagon::LDriuh_indexed:
1376     return isShiftedUInt<6,1>(MI->getOperand(2).getImm());
1377
1378   case Hexagon::LDrib:
1379   case Hexagon::LDriub:
1380   case Hexagon::LDrib_indexed:
1381   case Hexagon::LDriub_indexed:
1382     return isUInt<6>(MI->getOperand(2).getImm());
1383
1384   case Hexagon::POST_LDrid:
1385     return isShiftedInt<4,3>(MI->getOperand(3).getImm());
1386
1387   case Hexagon::POST_LDriw:
1388     return isShiftedInt<4,2>(MI->getOperand(3).getImm());
1389
1390   case Hexagon::POST_LDrih:
1391   case Hexagon::POST_LDriuh:
1392     return isShiftedInt<4,1>(MI->getOperand(3).getImm());
1393
1394   case Hexagon::POST_LDrib:
1395   case Hexagon::POST_LDriub:
1396     return isInt<4>(MI->getOperand(3).getImm());
1397
1398   case Hexagon::STrib_imm_V4:
1399   case Hexagon::STrih_imm_V4:
1400   case Hexagon::STriw_imm_V4:
1401     return (isUInt<6>(MI->getOperand(1).getImm()) &&
1402             isInt<6>(MI->getOperand(2).getImm()));
1403
1404   case Hexagon::ADD_ri:
1405     return isInt<8>(MI->getOperand(2).getImm());
1406
1407   case Hexagon::ASLH:
1408   case Hexagon::ASRH:
1409   case Hexagon::SXTB:
1410   case Hexagon::SXTH:
1411   case Hexagon::ZXTB:
1412   case Hexagon::ZXTH:
1413     return Subtarget.hasV4TOps();
1414
1415   case Hexagon::JMPR:
1416     return false;
1417   }
1418
1419   return true;
1420 }
1421
1422 // This function performs the following inversiones:
1423 //
1424 //  cPt    ---> cNotPt
1425 //  cNotPt ---> cPt
1426 //
1427 // however, these inversiones are NOT included:
1428 //
1429 //  cdnPt      -X-> cdnNotPt
1430 //  cdnNotPt   -X-> cdnPt
1431 //  cPt_nv     -X-> cNotPt_nv (new value stores)
1432 //  cNotPt_nv  -X-> cPt_nv    (new value stores)
1433 //
1434 // because only the following transformations are allowed:
1435 //
1436 //  cNotPt  ---> cdnNotPt
1437 //  cPt     ---> cdnPt
1438 //  cNotPt  ---> cNotPt_nv
1439 //  cPt     ---> cPt_nv
1440 unsigned HexagonInstrInfo::getInvertedPredicatedOpcode(const int Opc) const {
1441   switch(Opc) {
1442     default: llvm_unreachable("Unexpected predicated instruction");
1443     case Hexagon::TFR_cPt:
1444       return Hexagon::TFR_cNotPt;
1445     case Hexagon::TFR_cNotPt:
1446       return Hexagon::TFR_cPt;
1447
1448     case Hexagon::TFRI_cPt:
1449       return Hexagon::TFRI_cNotPt;
1450     case Hexagon::TFRI_cNotPt:
1451       return Hexagon::TFRI_cPt;
1452
1453     case Hexagon::JMP_c:
1454       return Hexagon::JMP_cNot;
1455     case Hexagon::JMP_cNot:
1456       return Hexagon::JMP_c;
1457
1458     case Hexagon::ADD_ri_cPt:
1459       return Hexagon::ADD_ri_cNotPt;
1460     case Hexagon::ADD_ri_cNotPt:
1461       return Hexagon::ADD_ri_cPt;
1462
1463     case Hexagon::ADD_rr_cPt:
1464       return Hexagon::ADD_rr_cNotPt;
1465     case Hexagon::ADD_rr_cNotPt:
1466       return Hexagon::ADD_rr_cPt;
1467
1468     case Hexagon::XOR_rr_cPt:
1469       return Hexagon::XOR_rr_cNotPt;
1470     case Hexagon::XOR_rr_cNotPt:
1471       return Hexagon::XOR_rr_cPt;
1472
1473     case Hexagon::AND_rr_cPt:
1474       return Hexagon::AND_rr_cNotPt;
1475     case Hexagon::AND_rr_cNotPt:
1476       return Hexagon::AND_rr_cPt;
1477
1478     case Hexagon::OR_rr_cPt:
1479       return Hexagon::OR_rr_cNotPt;
1480     case Hexagon::OR_rr_cNotPt:
1481       return Hexagon::OR_rr_cPt;
1482
1483     case Hexagon::SUB_rr_cPt:
1484       return Hexagon::SUB_rr_cNotPt;
1485     case Hexagon::SUB_rr_cNotPt:
1486       return Hexagon::SUB_rr_cPt;
1487
1488     case Hexagon::COMBINE_rr_cPt:
1489       return Hexagon::COMBINE_rr_cNotPt;
1490     case Hexagon::COMBINE_rr_cNotPt:
1491       return Hexagon::COMBINE_rr_cPt;
1492
1493     case Hexagon::ASLH_cPt_V4:
1494       return Hexagon::ASLH_cNotPt_V4;
1495     case Hexagon::ASLH_cNotPt_V4:
1496       return Hexagon::ASLH_cPt_V4;
1497
1498     case Hexagon::ASRH_cPt_V4:
1499       return Hexagon::ASRH_cNotPt_V4;
1500     case Hexagon::ASRH_cNotPt_V4:
1501       return Hexagon::ASRH_cPt_V4;
1502
1503     case Hexagon::SXTB_cPt_V4:
1504       return Hexagon::SXTB_cNotPt_V4;
1505     case Hexagon::SXTB_cNotPt_V4:
1506       return Hexagon::SXTB_cPt_V4;
1507
1508     case Hexagon::SXTH_cPt_V4:
1509       return Hexagon::SXTH_cNotPt_V4;
1510     case Hexagon::SXTH_cNotPt_V4:
1511       return Hexagon::SXTH_cPt_V4;
1512
1513     case Hexagon::ZXTB_cPt_V4:
1514       return Hexagon::ZXTB_cNotPt_V4;
1515     case Hexagon::ZXTB_cNotPt_V4:
1516       return Hexagon::ZXTB_cPt_V4;
1517
1518     case Hexagon::ZXTH_cPt_V4:
1519       return Hexagon::ZXTH_cNotPt_V4;
1520     case Hexagon::ZXTH_cNotPt_V4:
1521       return Hexagon::ZXTH_cPt_V4;
1522
1523
1524     case Hexagon::JMPR_cPt:
1525       return Hexagon::JMPR_cNotPt;
1526     case Hexagon::JMPR_cNotPt:
1527       return Hexagon::JMPR_cPt;
1528
1529   // V4 indexed+scaled load.
1530     case Hexagon::LDrid_indexed_shl_cPt_V4:
1531       return Hexagon::LDrid_indexed_shl_cNotPt_V4;
1532     case Hexagon::LDrid_indexed_shl_cNotPt_V4:
1533       return Hexagon::LDrid_indexed_shl_cPt_V4;
1534
1535     case Hexagon::LDrib_indexed_shl_cPt_V4:
1536       return Hexagon::LDrib_indexed_shl_cNotPt_V4;
1537     case Hexagon::LDrib_indexed_shl_cNotPt_V4:
1538       return Hexagon::LDrib_indexed_shl_cPt_V4;
1539
1540     case Hexagon::LDriub_indexed_shl_cPt_V4:
1541       return Hexagon::LDriub_indexed_shl_cNotPt_V4;
1542     case Hexagon::LDriub_indexed_shl_cNotPt_V4:
1543       return Hexagon::LDriub_indexed_shl_cPt_V4;
1544
1545     case Hexagon::LDrih_indexed_shl_cPt_V4:
1546       return Hexagon::LDrih_indexed_shl_cNotPt_V4;
1547     case Hexagon::LDrih_indexed_shl_cNotPt_V4:
1548       return Hexagon::LDrih_indexed_shl_cPt_V4;
1549
1550     case Hexagon::LDriuh_indexed_shl_cPt_V4:
1551       return Hexagon::LDriuh_indexed_shl_cNotPt_V4;
1552     case Hexagon::LDriuh_indexed_shl_cNotPt_V4:
1553       return Hexagon::LDriuh_indexed_shl_cPt_V4;
1554
1555     case Hexagon::LDriw_indexed_shl_cPt_V4:
1556       return Hexagon::LDriw_indexed_shl_cNotPt_V4;
1557     case Hexagon::LDriw_indexed_shl_cNotPt_V4:
1558       return Hexagon::LDriw_indexed_shl_cPt_V4;
1559
1560     // Byte.
1561     case Hexagon::POST_STbri_cPt:
1562       return Hexagon::POST_STbri_cNotPt;
1563     case Hexagon::POST_STbri_cNotPt:
1564       return Hexagon::POST_STbri_cPt;
1565
1566     case Hexagon::STrib_cPt:
1567       return Hexagon::STrib_cNotPt;
1568     case Hexagon::STrib_cNotPt:
1569       return Hexagon::STrib_cPt;
1570
1571     case Hexagon::STrib_indexed_cPt:
1572       return Hexagon::STrib_indexed_cNotPt;
1573     case Hexagon::STrib_indexed_cNotPt:
1574       return Hexagon::STrib_indexed_cPt;
1575
1576     case Hexagon::STrib_imm_cPt_V4:
1577       return Hexagon::STrib_imm_cNotPt_V4;
1578     case Hexagon::STrib_imm_cNotPt_V4:
1579       return Hexagon::STrib_imm_cPt_V4;
1580
1581     case Hexagon::STrib_indexed_shl_cPt_V4:
1582       return Hexagon::STrib_indexed_shl_cNotPt_V4;
1583     case Hexagon::STrib_indexed_shl_cNotPt_V4:
1584       return Hexagon::STrib_indexed_shl_cPt_V4;
1585
1586   // Halfword.
1587     case Hexagon::POST_SThri_cPt:
1588       return Hexagon::POST_SThri_cNotPt;
1589     case Hexagon::POST_SThri_cNotPt:
1590       return Hexagon::POST_SThri_cPt;
1591
1592     case Hexagon::STrih_cPt:
1593       return Hexagon::STrih_cNotPt;
1594     case Hexagon::STrih_cNotPt:
1595       return Hexagon::STrih_cPt;
1596
1597     case Hexagon::STrih_indexed_cPt:
1598       return Hexagon::STrih_indexed_cNotPt;
1599     case Hexagon::STrih_indexed_cNotPt:
1600       return Hexagon::STrih_indexed_cPt;
1601
1602     case Hexagon::STrih_imm_cPt_V4:
1603       return Hexagon::STrih_imm_cNotPt_V4;
1604     case Hexagon::STrih_imm_cNotPt_V4:
1605       return Hexagon::STrih_imm_cPt_V4;
1606
1607     case Hexagon::STrih_indexed_shl_cPt_V4:
1608       return Hexagon::STrih_indexed_shl_cNotPt_V4;
1609     case Hexagon::STrih_indexed_shl_cNotPt_V4:
1610       return Hexagon::STrih_indexed_shl_cPt_V4;
1611
1612   // Word.
1613     case Hexagon::POST_STwri_cPt:
1614       return Hexagon::POST_STwri_cNotPt;
1615     case Hexagon::POST_STwri_cNotPt:
1616       return Hexagon::POST_STwri_cPt;
1617
1618     case Hexagon::STriw_cPt:
1619       return Hexagon::STriw_cNotPt;
1620     case Hexagon::STriw_cNotPt:
1621       return Hexagon::STriw_cPt;
1622
1623     case Hexagon::STriw_indexed_cPt:
1624       return Hexagon::STriw_indexed_cNotPt;
1625     case Hexagon::STriw_indexed_cNotPt:
1626       return Hexagon::STriw_indexed_cPt;
1627
1628     case Hexagon::STriw_indexed_shl_cPt_V4:
1629       return Hexagon::STriw_indexed_shl_cNotPt_V4;
1630     case Hexagon::STriw_indexed_shl_cNotPt_V4:
1631       return Hexagon::STriw_indexed_shl_cPt_V4;
1632
1633     case Hexagon::STriw_imm_cPt_V4:
1634       return Hexagon::STriw_imm_cNotPt_V4;
1635     case Hexagon::STriw_imm_cNotPt_V4:
1636       return Hexagon::STriw_imm_cPt_V4;
1637
1638   // Double word.
1639     case Hexagon::POST_STdri_cPt:
1640       return Hexagon::POST_STdri_cNotPt;
1641     case Hexagon::POST_STdri_cNotPt:
1642       return Hexagon::POST_STdri_cPt;
1643
1644     case Hexagon::STrid_cPt:
1645       return Hexagon::STrid_cNotPt;
1646     case Hexagon::STrid_cNotPt:
1647       return Hexagon::STrid_cPt;
1648
1649     case Hexagon::STrid_indexed_cPt:
1650       return Hexagon::STrid_indexed_cNotPt;
1651     case Hexagon::STrid_indexed_cNotPt:
1652       return Hexagon::STrid_indexed_cPt;
1653
1654     case Hexagon::STrid_indexed_shl_cPt_V4:
1655       return Hexagon::STrid_indexed_shl_cNotPt_V4;
1656     case Hexagon::STrid_indexed_shl_cNotPt_V4:
1657       return Hexagon::STrid_indexed_shl_cPt_V4;
1658
1659     // V4 Store to global address.
1660     case Hexagon::STd_GP_cPt_V4:
1661       return Hexagon::STd_GP_cNotPt_V4;
1662     case Hexagon::STd_GP_cNotPt_V4:
1663       return Hexagon::STd_GP_cPt_V4;
1664
1665     case Hexagon::STb_GP_cPt_V4:
1666       return Hexagon::STb_GP_cNotPt_V4;
1667     case Hexagon::STb_GP_cNotPt_V4:
1668       return Hexagon::STb_GP_cPt_V4;
1669
1670     case Hexagon::STh_GP_cPt_V4:
1671       return Hexagon::STh_GP_cNotPt_V4;
1672     case Hexagon::STh_GP_cNotPt_V4:
1673       return Hexagon::STh_GP_cPt_V4;
1674
1675     case Hexagon::STw_GP_cPt_V4:
1676       return Hexagon::STw_GP_cNotPt_V4;
1677     case Hexagon::STw_GP_cNotPt_V4:
1678       return Hexagon::STw_GP_cPt_V4;
1679
1680   // Load.
1681     case Hexagon::LDrid_cPt:
1682       return Hexagon::LDrid_cNotPt;
1683     case Hexagon::LDrid_cNotPt:
1684       return Hexagon::LDrid_cPt;
1685
1686     case Hexagon::LDriw_cPt:
1687       return Hexagon::LDriw_cNotPt;
1688     case Hexagon::LDriw_cNotPt:
1689       return Hexagon::LDriw_cPt;
1690
1691     case Hexagon::LDrih_cPt:
1692       return Hexagon::LDrih_cNotPt;
1693     case Hexagon::LDrih_cNotPt:
1694       return Hexagon::LDrih_cPt;
1695
1696     case Hexagon::LDriuh_cPt:
1697       return Hexagon::LDriuh_cNotPt;
1698     case Hexagon::LDriuh_cNotPt:
1699       return Hexagon::LDriuh_cPt;
1700
1701     case Hexagon::LDrib_cPt:
1702       return Hexagon::LDrib_cNotPt;
1703     case Hexagon::LDrib_cNotPt:
1704       return Hexagon::LDrib_cPt;
1705
1706     case Hexagon::LDriub_cPt:
1707       return Hexagon::LDriub_cNotPt;
1708     case Hexagon::LDriub_cNotPt:
1709       return Hexagon::LDriub_cPt;
1710
1711  // Load Indexed.
1712     case Hexagon::LDrid_indexed_cPt:
1713       return Hexagon::LDrid_indexed_cNotPt;
1714     case Hexagon::LDrid_indexed_cNotPt:
1715       return Hexagon::LDrid_indexed_cPt;
1716
1717     case Hexagon::LDriw_indexed_cPt:
1718       return Hexagon::LDriw_indexed_cNotPt;
1719     case Hexagon::LDriw_indexed_cNotPt:
1720       return Hexagon::LDriw_indexed_cPt;
1721
1722     case Hexagon::LDrih_indexed_cPt:
1723       return Hexagon::LDrih_indexed_cNotPt;
1724     case Hexagon::LDrih_indexed_cNotPt:
1725       return Hexagon::LDrih_indexed_cPt;
1726
1727     case Hexagon::LDriuh_indexed_cPt:
1728       return Hexagon::LDriuh_indexed_cNotPt;
1729     case Hexagon::LDriuh_indexed_cNotPt:
1730       return Hexagon::LDriuh_indexed_cPt;
1731
1732     case Hexagon::LDrib_indexed_cPt:
1733       return Hexagon::LDrib_indexed_cNotPt;
1734     case Hexagon::LDrib_indexed_cNotPt:
1735       return Hexagon::LDrib_indexed_cPt;
1736
1737     case Hexagon::LDriub_indexed_cPt:
1738       return Hexagon::LDriub_indexed_cNotPt;
1739     case Hexagon::LDriub_indexed_cNotPt:
1740       return Hexagon::LDriub_indexed_cPt;
1741
1742   // Post Inc Load.
1743     case Hexagon::POST_LDrid_cPt:
1744       return Hexagon::POST_LDrid_cNotPt;
1745     case Hexagon::POST_LDriw_cNotPt:
1746       return Hexagon::POST_LDriw_cPt;
1747
1748     case Hexagon::POST_LDrih_cPt:
1749       return Hexagon::POST_LDrih_cNotPt;
1750     case Hexagon::POST_LDrih_cNotPt:
1751       return Hexagon::POST_LDrih_cPt;
1752
1753     case Hexagon::POST_LDriuh_cPt:
1754       return Hexagon::POST_LDriuh_cNotPt;
1755     case Hexagon::POST_LDriuh_cNotPt:
1756       return Hexagon::POST_LDriuh_cPt;
1757
1758     case Hexagon::POST_LDrib_cPt:
1759       return Hexagon::POST_LDrib_cNotPt;
1760     case Hexagon::POST_LDrib_cNotPt:
1761       return Hexagon::POST_LDrib_cPt;
1762
1763     case Hexagon::POST_LDriub_cPt:
1764       return Hexagon::POST_LDriub_cNotPt;
1765     case Hexagon::POST_LDriub_cNotPt:
1766       return Hexagon::POST_LDriub_cPt;
1767
1768   // Dealloc_return.
1769     case Hexagon::DEALLOC_RET_cPt_V4:
1770       return Hexagon::DEALLOC_RET_cNotPt_V4;
1771     case Hexagon::DEALLOC_RET_cNotPt_V4:
1772       return Hexagon::DEALLOC_RET_cPt_V4;
1773
1774    // New Value Jump.
1775    // JMPEQ_ri - with -1.
1776     case Hexagon::JMP_EQriPtneg_nv_V4:
1777       return Hexagon::JMP_EQriNotPtneg_nv_V4;
1778     case Hexagon::JMP_EQriNotPtneg_nv_V4:
1779       return Hexagon::JMP_EQriPtneg_nv_V4;
1780
1781     case Hexagon::JMP_EQriPntneg_nv_V4:
1782       return Hexagon::JMP_EQriNotPntneg_nv_V4;
1783     case Hexagon::JMP_EQriNotPntneg_nv_V4:
1784       return Hexagon::JMP_EQriPntneg_nv_V4;
1785
1786    // JMPEQ_ri.
1787      case Hexagon::JMP_EQriPt_nv_V4:
1788       return Hexagon::JMP_EQriNotPt_nv_V4;
1789     case Hexagon::JMP_EQriNotPt_nv_V4:
1790       return Hexagon::JMP_EQriPt_nv_V4;
1791
1792      case Hexagon::JMP_EQriPnt_nv_V4:
1793       return Hexagon::JMP_EQriNotPnt_nv_V4;
1794     case Hexagon::JMP_EQriNotPnt_nv_V4:
1795       return Hexagon::JMP_EQriPnt_nv_V4;
1796
1797    // JMPEQ_rr.
1798      case Hexagon::JMP_EQrrPt_nv_V4:
1799       return Hexagon::JMP_EQrrNotPt_nv_V4;
1800     case Hexagon::JMP_EQrrNotPt_nv_V4:
1801       return Hexagon::JMP_EQrrPt_nv_V4;
1802
1803      case Hexagon::JMP_EQrrPnt_nv_V4:
1804       return Hexagon::JMP_EQrrNotPnt_nv_V4;
1805     case Hexagon::JMP_EQrrNotPnt_nv_V4:
1806       return Hexagon::JMP_EQrrPnt_nv_V4;
1807
1808    // JMPGT_ri - with -1.
1809     case Hexagon::JMP_GTriPtneg_nv_V4:
1810       return Hexagon::JMP_GTriNotPtneg_nv_V4;
1811     case Hexagon::JMP_GTriNotPtneg_nv_V4:
1812       return Hexagon::JMP_GTriPtneg_nv_V4;
1813
1814     case Hexagon::JMP_GTriPntneg_nv_V4:
1815       return Hexagon::JMP_GTriNotPntneg_nv_V4;
1816     case Hexagon::JMP_GTriNotPntneg_nv_V4:
1817       return Hexagon::JMP_GTriPntneg_nv_V4;
1818
1819    // JMPGT_ri.
1820      case Hexagon::JMP_GTriPt_nv_V4:
1821       return Hexagon::JMP_GTriNotPt_nv_V4;
1822     case Hexagon::JMP_GTriNotPt_nv_V4:
1823       return Hexagon::JMP_GTriPt_nv_V4;
1824
1825      case Hexagon::JMP_GTriPnt_nv_V4:
1826       return Hexagon::JMP_GTriNotPnt_nv_V4;
1827     case Hexagon::JMP_GTriNotPnt_nv_V4:
1828       return Hexagon::JMP_GTriPnt_nv_V4;
1829
1830    // JMPGT_rr.
1831      case Hexagon::JMP_GTrrPt_nv_V4:
1832       return Hexagon::JMP_GTrrNotPt_nv_V4;
1833     case Hexagon::JMP_GTrrNotPt_nv_V4:
1834       return Hexagon::JMP_GTrrPt_nv_V4;
1835
1836      case Hexagon::JMP_GTrrPnt_nv_V4:
1837       return Hexagon::JMP_GTrrNotPnt_nv_V4;
1838     case Hexagon::JMP_GTrrNotPnt_nv_V4:
1839       return Hexagon::JMP_GTrrPnt_nv_V4;
1840
1841    // JMPGT_rrdn.
1842      case Hexagon::JMP_GTrrdnPt_nv_V4:
1843       return Hexagon::JMP_GTrrdnNotPt_nv_V4;
1844     case Hexagon::JMP_GTrrdnNotPt_nv_V4:
1845       return Hexagon::JMP_GTrrdnPt_nv_V4;
1846
1847      case Hexagon::JMP_GTrrdnPnt_nv_V4:
1848       return Hexagon::JMP_GTrrdnNotPnt_nv_V4;
1849     case Hexagon::JMP_GTrrdnNotPnt_nv_V4:
1850       return Hexagon::JMP_GTrrdnPnt_nv_V4;
1851
1852    // JMPGTU_ri.
1853      case Hexagon::JMP_GTUriPt_nv_V4:
1854       return Hexagon::JMP_GTUriNotPt_nv_V4;
1855     case Hexagon::JMP_GTUriNotPt_nv_V4:
1856       return Hexagon::JMP_GTUriPt_nv_V4;
1857
1858      case Hexagon::JMP_GTUriPnt_nv_V4:
1859       return Hexagon::JMP_GTUriNotPnt_nv_V4;
1860     case Hexagon::JMP_GTUriNotPnt_nv_V4:
1861       return Hexagon::JMP_GTUriPnt_nv_V4;
1862
1863    // JMPGTU_rr.
1864      case Hexagon::JMP_GTUrrPt_nv_V4:
1865       return Hexagon::JMP_GTUrrNotPt_nv_V4;
1866     case Hexagon::JMP_GTUrrNotPt_nv_V4:
1867       return Hexagon::JMP_GTUrrPt_nv_V4;
1868
1869      case Hexagon::JMP_GTUrrPnt_nv_V4:
1870       return Hexagon::JMP_GTUrrNotPnt_nv_V4;
1871     case Hexagon::JMP_GTUrrNotPnt_nv_V4:
1872       return Hexagon::JMP_GTUrrPnt_nv_V4;
1873
1874    // JMPGTU_rrdn.
1875      case Hexagon::JMP_GTUrrdnPt_nv_V4:
1876       return Hexagon::JMP_GTUrrdnNotPt_nv_V4;
1877     case Hexagon::JMP_GTUrrdnNotPt_nv_V4:
1878       return Hexagon::JMP_GTUrrdnPt_nv_V4;
1879
1880      case Hexagon::JMP_GTUrrdnPnt_nv_V4:
1881       return Hexagon::JMP_GTUrrdnNotPnt_nv_V4;
1882     case Hexagon::JMP_GTUrrdnNotPnt_nv_V4:
1883       return Hexagon::JMP_GTUrrdnPnt_nv_V4;
1884   }
1885 }
1886
1887
1888 int HexagonInstrInfo::
1889 getMatchingCondBranchOpcode(int Opc, bool invertPredicate) const {
1890   enum Hexagon::PredSense inPredSense;
1891   inPredSense = invertPredicate ? Hexagon::PredSense_false :
1892                                   Hexagon::PredSense_true;
1893   int CondOpcode = Hexagon::getPredOpcode(Opc, inPredSense);
1894   if (CondOpcode >= 0) // Valid Conditional opcode/instruction
1895     return CondOpcode;
1896
1897   // This switch case will be removed once all the instructions have been
1898   // modified to use relation maps.
1899   switch(Opc) {
1900   case Hexagon::TFR:
1901     return !invertPredicate ? Hexagon::TFR_cPt :
1902                               Hexagon::TFR_cNotPt;
1903   case Hexagon::TFRI_f:
1904     return !invertPredicate ? Hexagon::TFRI_cPt_f :
1905                               Hexagon::TFRI_cNotPt_f;
1906   case Hexagon::TFRI:
1907     return !invertPredicate ? Hexagon::TFRI_cPt :
1908                               Hexagon::TFRI_cNotPt;
1909   case Hexagon::JMP:
1910     return !invertPredicate ? Hexagon::JMP_c :
1911                               Hexagon::JMP_cNot;
1912   case Hexagon::JMP_EQrrPt_nv_V4:
1913     return !invertPredicate ? Hexagon::JMP_EQrrPt_nv_V4 :
1914                               Hexagon::JMP_EQrrNotPt_nv_V4;
1915   case Hexagon::JMP_EQriPt_nv_V4:
1916     return !invertPredicate ? Hexagon::JMP_EQriPt_nv_V4 :
1917                               Hexagon::JMP_EQriNotPt_nv_V4;
1918   case Hexagon::COMBINE_rr:
1919     return !invertPredicate ? Hexagon::COMBINE_rr_cPt :
1920                               Hexagon::COMBINE_rr_cNotPt;
1921   case Hexagon::ASLH:
1922     return !invertPredicate ? Hexagon::ASLH_cPt_V4 :
1923                               Hexagon::ASLH_cNotPt_V4;
1924   case Hexagon::ASRH:
1925     return !invertPredicate ? Hexagon::ASRH_cPt_V4 :
1926                               Hexagon::ASRH_cNotPt_V4;
1927   case Hexagon::SXTB:
1928     return !invertPredicate ? Hexagon::SXTB_cPt_V4 :
1929                               Hexagon::SXTB_cNotPt_V4;
1930   case Hexagon::SXTH:
1931     return !invertPredicate ? Hexagon::SXTH_cPt_V4 :
1932                               Hexagon::SXTH_cNotPt_V4;
1933   case Hexagon::ZXTB:
1934     return !invertPredicate ? Hexagon::ZXTB_cPt_V4 :
1935                               Hexagon::ZXTB_cNotPt_V4;
1936   case Hexagon::ZXTH:
1937     return !invertPredicate ? Hexagon::ZXTH_cPt_V4 :
1938                               Hexagon::ZXTH_cNotPt_V4;
1939
1940   case Hexagon::JMPR:
1941     return !invertPredicate ? Hexagon::JMPR_cPt :
1942                               Hexagon::JMPR_cNotPt;
1943
1944   // V4 indexed+scaled load.
1945   case Hexagon::LDrid_indexed_shl_V4:
1946     return !invertPredicate ? Hexagon::LDrid_indexed_shl_cPt_V4 :
1947                               Hexagon::LDrid_indexed_shl_cNotPt_V4;
1948   case Hexagon::LDrib_indexed_shl_V4:
1949     return !invertPredicate ? Hexagon::LDrib_indexed_shl_cPt_V4 :
1950                               Hexagon::LDrib_indexed_shl_cNotPt_V4;
1951   case Hexagon::LDriub_indexed_shl_V4:
1952     return !invertPredicate ? Hexagon::LDriub_indexed_shl_cPt_V4 :
1953                               Hexagon::LDriub_indexed_shl_cNotPt_V4;
1954   case Hexagon::LDrih_indexed_shl_V4:
1955     return !invertPredicate ? Hexagon::LDrih_indexed_shl_cPt_V4 :
1956                               Hexagon::LDrih_indexed_shl_cNotPt_V4;
1957   case Hexagon::LDriuh_indexed_shl_V4:
1958     return !invertPredicate ? Hexagon::LDriuh_indexed_shl_cPt_V4 :
1959                               Hexagon::LDriuh_indexed_shl_cNotPt_V4;
1960   case Hexagon::LDriw_indexed_shl_V4:
1961     return !invertPredicate ? Hexagon::LDriw_indexed_shl_cPt_V4 :
1962                               Hexagon::LDriw_indexed_shl_cNotPt_V4;
1963
1964   // V4 Load from global address
1965   case Hexagon::LDd_GP_V4:
1966     return !invertPredicate ? Hexagon::LDd_GP_cPt_V4 :
1967                               Hexagon::LDd_GP_cNotPt_V4;
1968   case Hexagon::LDb_GP_V4:
1969     return !invertPredicate ? Hexagon::LDb_GP_cPt_V4 :
1970                               Hexagon::LDb_GP_cNotPt_V4;
1971   case Hexagon::LDub_GP_V4:
1972     return !invertPredicate ? Hexagon::LDub_GP_cPt_V4 :
1973                               Hexagon::LDub_GP_cNotPt_V4;
1974   case Hexagon::LDh_GP_V4:
1975     return !invertPredicate ? Hexagon::LDh_GP_cPt_V4 :
1976                               Hexagon::LDh_GP_cNotPt_V4;
1977   case Hexagon::LDuh_GP_V4:
1978     return !invertPredicate ? Hexagon::LDuh_GP_cPt_V4 :
1979                               Hexagon::LDuh_GP_cNotPt_V4;
1980   case Hexagon::LDw_GP_V4:
1981     return !invertPredicate ? Hexagon::LDw_GP_cPt_V4 :
1982                               Hexagon::LDw_GP_cNotPt_V4;
1983
1984     // Byte.
1985   case Hexagon::POST_STbri:
1986     return !invertPredicate ? Hexagon::POST_STbri_cPt :
1987                               Hexagon::POST_STbri_cNotPt;
1988   case Hexagon::STrib:
1989     return !invertPredicate ? Hexagon::STrib_cPt :
1990                               Hexagon::STrib_cNotPt;
1991   case Hexagon::STrib_indexed:
1992     return !invertPredicate ? Hexagon::STrib_indexed_cPt :
1993                               Hexagon::STrib_indexed_cNotPt;
1994   case Hexagon::STrib_imm_V4:
1995     return !invertPredicate ? Hexagon::STrib_imm_cPt_V4 :
1996                               Hexagon::STrib_imm_cNotPt_V4;
1997   case Hexagon::STrib_indexed_shl_V4:
1998     return !invertPredicate ? Hexagon::STrib_indexed_shl_cPt_V4 :
1999                               Hexagon::STrib_indexed_shl_cNotPt_V4;
2000   // Halfword.
2001   case Hexagon::POST_SThri:
2002     return !invertPredicate ? Hexagon::POST_SThri_cPt :
2003                               Hexagon::POST_SThri_cNotPt;
2004   case Hexagon::STrih:
2005     return !invertPredicate ? Hexagon::STrih_cPt :
2006                               Hexagon::STrih_cNotPt;
2007   case Hexagon::STrih_indexed:
2008     return !invertPredicate ? Hexagon::STrih_indexed_cPt :
2009                               Hexagon::STrih_indexed_cNotPt;
2010   case Hexagon::STrih_imm_V4:
2011     return !invertPredicate ? Hexagon::STrih_imm_cPt_V4 :
2012                               Hexagon::STrih_imm_cNotPt_V4;
2013   case Hexagon::STrih_indexed_shl_V4:
2014     return !invertPredicate ? Hexagon::STrih_indexed_shl_cPt_V4 :
2015                               Hexagon::STrih_indexed_shl_cNotPt_V4;
2016   // Word.
2017   case Hexagon::POST_STwri:
2018     return !invertPredicate ? Hexagon::POST_STwri_cPt :
2019                               Hexagon::POST_STwri_cNotPt;
2020   case Hexagon::STriw:
2021     return !invertPredicate ? Hexagon::STriw_cPt :
2022                               Hexagon::STriw_cNotPt;
2023   case Hexagon::STriw_indexed:
2024     return !invertPredicate ? Hexagon::STriw_indexed_cPt :
2025                               Hexagon::STriw_indexed_cNotPt;
2026   case Hexagon::STriw_indexed_shl_V4:
2027     return !invertPredicate ? Hexagon::STriw_indexed_shl_cPt_V4 :
2028                               Hexagon::STriw_indexed_shl_cNotPt_V4;
2029   case Hexagon::STriw_imm_V4:
2030     return !invertPredicate ? Hexagon::STriw_imm_cPt_V4 :
2031                               Hexagon::STriw_imm_cNotPt_V4;
2032   // Double word.
2033   case Hexagon::POST_STdri:
2034     return !invertPredicate ? Hexagon::POST_STdri_cPt :
2035                               Hexagon::POST_STdri_cNotPt;
2036   case Hexagon::STrid:
2037     return !invertPredicate ? Hexagon::STrid_cPt :
2038                               Hexagon::STrid_cNotPt;
2039   case Hexagon::STrid_indexed:
2040     return !invertPredicate ? Hexagon::STrid_indexed_cPt :
2041                               Hexagon::STrid_indexed_cNotPt;
2042   case Hexagon::STrid_indexed_shl_V4:
2043     return !invertPredicate ? Hexagon::STrid_indexed_shl_cPt_V4 :
2044                               Hexagon::STrid_indexed_shl_cNotPt_V4;
2045
2046   // V4 Store to global address
2047   case Hexagon::STd_GP_V4:
2048     return !invertPredicate ? Hexagon::STd_GP_cPt_V4 :
2049                               Hexagon::STd_GP_cNotPt_V4;
2050   case Hexagon::STb_GP_V4:
2051     return !invertPredicate ? Hexagon::STb_GP_cPt_V4 :
2052                               Hexagon::STb_GP_cNotPt_V4;
2053   case Hexagon::STh_GP_V4:
2054     return !invertPredicate ? Hexagon::STh_GP_cPt_V4 :
2055                               Hexagon::STh_GP_cNotPt_V4;
2056   case Hexagon::STw_GP_V4:
2057     return !invertPredicate ? Hexagon::STw_GP_cPt_V4 :
2058                               Hexagon::STw_GP_cNotPt_V4;
2059
2060   // Load.
2061   case Hexagon::LDrid:
2062     return !invertPredicate ? Hexagon::LDrid_cPt :
2063                               Hexagon::LDrid_cNotPt;
2064   case Hexagon::LDriw:
2065     return !invertPredicate ? Hexagon::LDriw_cPt :
2066                               Hexagon::LDriw_cNotPt;
2067   case Hexagon::LDrih:
2068     return !invertPredicate ? Hexagon::LDrih_cPt :
2069                               Hexagon::LDrih_cNotPt;
2070   case Hexagon::LDriuh:
2071     return !invertPredicate ? Hexagon::LDriuh_cPt :
2072                               Hexagon::LDriuh_cNotPt;
2073   case Hexagon::LDrib:
2074     return !invertPredicate ? Hexagon::LDrib_cPt :
2075                               Hexagon::LDrib_cNotPt;
2076   case Hexagon::LDriub:
2077     return !invertPredicate ? Hexagon::LDriub_cPt :
2078                               Hexagon::LDriub_cNotPt;
2079  // Load Indexed.
2080   case Hexagon::LDrid_indexed:
2081     return !invertPredicate ? Hexagon::LDrid_indexed_cPt :
2082                               Hexagon::LDrid_indexed_cNotPt;
2083   case Hexagon::LDriw_indexed:
2084     return !invertPredicate ? Hexagon::LDriw_indexed_cPt :
2085                               Hexagon::LDriw_indexed_cNotPt;
2086   case Hexagon::LDrih_indexed:
2087     return !invertPredicate ? Hexagon::LDrih_indexed_cPt :
2088                               Hexagon::LDrih_indexed_cNotPt;
2089   case Hexagon::LDriuh_indexed:
2090     return !invertPredicate ? Hexagon::LDriuh_indexed_cPt :
2091                               Hexagon::LDriuh_indexed_cNotPt;
2092   case Hexagon::LDrib_indexed:
2093     return !invertPredicate ? Hexagon::LDrib_indexed_cPt :
2094                               Hexagon::LDrib_indexed_cNotPt;
2095   case Hexagon::LDriub_indexed:
2096     return !invertPredicate ? Hexagon::LDriub_indexed_cPt :
2097                               Hexagon::LDriub_indexed_cNotPt;
2098   // Post Increment Load.
2099   case Hexagon::POST_LDrid:
2100     return !invertPredicate ? Hexagon::POST_LDrid_cPt :
2101                               Hexagon::POST_LDrid_cNotPt;
2102   case Hexagon::POST_LDriw:
2103     return !invertPredicate ? Hexagon::POST_LDriw_cPt :
2104                               Hexagon::POST_LDriw_cNotPt;
2105   case Hexagon::POST_LDrih:
2106     return !invertPredicate ? Hexagon::POST_LDrih_cPt :
2107                               Hexagon::POST_LDrih_cNotPt;
2108   case Hexagon::POST_LDriuh:
2109     return !invertPredicate ? Hexagon::POST_LDriuh_cPt :
2110                               Hexagon::POST_LDriuh_cNotPt;
2111   case Hexagon::POST_LDrib:
2112     return !invertPredicate ? Hexagon::POST_LDrib_cPt :
2113                               Hexagon::POST_LDrib_cNotPt;
2114   case Hexagon::POST_LDriub:
2115     return !invertPredicate ? Hexagon::POST_LDriub_cPt :
2116                               Hexagon::POST_LDriub_cNotPt;
2117   // DEALLOC_RETURN.
2118   case Hexagon::DEALLOC_RET_V4:
2119     return !invertPredicate ? Hexagon::DEALLOC_RET_cPt_V4 :
2120                               Hexagon::DEALLOC_RET_cNotPt_V4;
2121   }
2122   llvm_unreachable("Unexpected predicable instruction");
2123 }
2124
2125
2126 bool HexagonInstrInfo::
2127 PredicateInstruction(MachineInstr *MI,
2128                      const SmallVectorImpl<MachineOperand> &Cond) const {
2129   int Opc = MI->getOpcode();
2130   assert (isPredicable(MI) && "Expected predicable instruction");
2131   bool invertJump = (!Cond.empty() && Cond[0].isImm() &&
2132                      (Cond[0].getImm() == 0));
2133
2134   // This will change MI's opcode to its predicate version.
2135   // However, its operand list is still the old one, i.e. the
2136   // non-predicate one.
2137   MI->setDesc(get(getMatchingCondBranchOpcode(Opc, invertJump)));
2138
2139   int oper = -1;
2140   unsigned int GAIdx = 0;
2141
2142   // Indicates whether the current MI has a GlobalAddress operand
2143   bool hasGAOpnd = false;
2144   std::vector<MachineOperand> tmpOpnds;
2145
2146   // Indicates whether we need to shift operands to right.
2147   bool needShift = true;
2148
2149   // The predicate is ALWAYS the FIRST input operand !!!
2150   if (MI->getNumOperands() == 0) {
2151     // The non-predicate version of MI does not take any operands,
2152     // i.e. no outs and no ins. In this condition, the predicate
2153     // operand will be directly placed at Operands[0]. No operand
2154     // shift is needed.
2155     // Example: BARRIER
2156     needShift = false;
2157     oper = -1;
2158   }
2159   else if (   MI->getOperand(MI->getNumOperands()-1).isReg()
2160            && MI->getOperand(MI->getNumOperands()-1).isDef()
2161            && !MI->getOperand(MI->getNumOperands()-1).isImplicit()) {
2162     // The non-predicate version of MI does not have any input operands.
2163     // In this condition, we extend the length of Operands[] by one and
2164     // copy the original last operand to the newly allocated slot.
2165     // At this moment, it is just a place holder. Later, we will put
2166     // predicate operand directly into it. No operand shift is needed.
2167     // Example: r0=BARRIER (this is a faked insn used here for illustration)
2168     MI->addOperand(MI->getOperand(MI->getNumOperands()-1));
2169     needShift = false;
2170     oper = MI->getNumOperands() - 2;
2171   }
2172   else {
2173     // We need to right shift all input operands by one. Duplicate the
2174     // last operand into the newly allocated slot.
2175     MI->addOperand(MI->getOperand(MI->getNumOperands()-1));
2176   }
2177
2178   if (needShift)
2179   {
2180     // Operands[ MI->getNumOperands() - 2 ] has been copied into
2181     // Operands[ MI->getNumOperands() - 1 ], so we start from
2182     // Operands[ MI->getNumOperands() - 3 ].
2183     // oper is a signed int.
2184     // It is ok if "MI->getNumOperands()-3" is -3, -2, or -1.
2185     for (oper = MI->getNumOperands() - 3; oper >= 0; --oper)
2186     {
2187       MachineOperand &MO = MI->getOperand(oper);
2188
2189       // Opnd[0] Opnd[1] Opnd[2] Opnd[3] Opnd[4]   Opnd[5]   Opnd[6]   Opnd[7]
2190       // <Def0>  <Def1>  <Use0>  <Use1>  <ImpDef0> <ImpDef1> <ImpUse0> <ImpUse1>
2191       //               /\~
2192       //              /||\~
2193       //               ||
2194       //        Predicate Operand here
2195       if (MO.isReg() && !MO.isUse() && !MO.isImplicit()) {
2196         break;
2197       }
2198       if (MO.isReg()) {
2199         MI->getOperand(oper+1).ChangeToRegister(MO.getReg(), MO.isDef(),
2200                                                 MO.isImplicit(), MO.isKill(),
2201                                                 MO.isDead(), MO.isUndef(),
2202                                                 MO.isDebug());
2203       }
2204       else if (MO.isImm()) {
2205         MI->getOperand(oper+1).ChangeToImmediate(MO.getImm());
2206       }
2207       else if (MO.isGlobal()) {
2208         // MI can not have more than one GlobalAddress operand.
2209         assert(hasGAOpnd == false && "MI can only have one GlobalAddress opnd");
2210
2211         // There is no member function called "ChangeToGlobalAddress" in the
2212         // MachineOperand class (not like "ChangeToRegister" and
2213         // "ChangeToImmediate"). So we have to remove them from Operands[] list
2214         // first, and then add them back after we have inserted the predicate
2215         // operand. tmpOpnds[] is to remember these operands before we remove
2216         // them.
2217         tmpOpnds.push_back(MO);
2218
2219         // Operands[oper] is a GlobalAddress operand;
2220         // Operands[oper+1] has been copied into Operands[oper+2];
2221         hasGAOpnd = true;
2222         GAIdx = oper;
2223         continue;
2224       }
2225       else {
2226         assert(false && "Unexpected operand type");
2227       }
2228     }
2229   }
2230
2231   int regPos = invertJump ? 1 : 0;
2232   MachineOperand PredMO = Cond[regPos];
2233
2234   // [oper] now points to the last explicit Def. Predicate operand must be
2235   // located at [oper+1]. See diagram above.
2236   // This assumes that the predicate is always the first operand,
2237   // i.e. Operands[0+numResults], in the set of inputs
2238   // It is better to have an assert here to check this. But I don't know how
2239   // to write this assert because findFirstPredOperandIdx() would return -1
2240   if (oper < -1) oper = -1;
2241   MI->getOperand(oper+1).ChangeToRegister(PredMO.getReg(), PredMO.isDef(),
2242                                           PredMO.isImplicit(), PredMO.isKill(),
2243                                           PredMO.isDead(), PredMO.isUndef(),
2244                                           PredMO.isDebug());
2245
2246   if (hasGAOpnd)
2247   {
2248     unsigned int i;
2249
2250     // Operands[GAIdx] is the original GlobalAddress operand, which is
2251     // already copied into tmpOpnds[0].
2252     // Operands[GAIdx] now stores a copy of Operands[GAIdx-1]
2253     // Operands[GAIdx+1] has already been copied into Operands[GAIdx+2],
2254     // so we start from [GAIdx+2]
2255     for (i = GAIdx + 2; i < MI->getNumOperands(); ++i)
2256       tmpOpnds.push_back(MI->getOperand(i));
2257
2258     // Remove all operands in range [ (GAIdx+1) ... (MI->getNumOperands()-1) ]
2259     // It is very important that we always remove from the end of Operands[]
2260     // MI->getNumOperands() is at least 2 if program goes to here.
2261     for (i = MI->getNumOperands() - 1; i > GAIdx; --i)
2262       MI->RemoveOperand(i);
2263
2264     for (i = 0; i < tmpOpnds.size(); ++i)
2265       MI->addOperand(tmpOpnds[i]);
2266   }
2267
2268   return true;
2269 }
2270
2271
2272 bool
2273 HexagonInstrInfo::
2274 isProfitableToIfCvt(MachineBasicBlock &MBB,
2275                     unsigned NumCycles,
2276                     unsigned ExtraPredCycles,
2277                     const BranchProbability &Probability) const {
2278   return true;
2279 }
2280
2281
2282 bool
2283 HexagonInstrInfo::
2284 isProfitableToIfCvt(MachineBasicBlock &TMBB,
2285                     unsigned NumTCycles,
2286                     unsigned ExtraTCycles,
2287                     MachineBasicBlock &FMBB,
2288                     unsigned NumFCycles,
2289                     unsigned ExtraFCycles,
2290                     const BranchProbability &Probability) const {
2291   return true;
2292 }
2293
2294
2295 bool HexagonInstrInfo::isPredicated(const MachineInstr *MI) const {
2296   const uint64_t F = MI->getDesc().TSFlags;
2297
2298   return ((F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
2299 }
2300
2301 bool
2302 HexagonInstrInfo::DefinesPredicate(MachineInstr *MI,
2303                                    std::vector<MachineOperand> &Pred) const {
2304   for (unsigned oper = 0; oper < MI->getNumOperands(); ++oper) {
2305     MachineOperand MO = MI->getOperand(oper);
2306     if (MO.isReg() && MO.isDef()) {
2307       const TargetRegisterClass* RC = RI.getMinimalPhysRegClass(MO.getReg());
2308       if (RC == &Hexagon::PredRegsRegClass) {
2309         Pred.push_back(MO);
2310         return true;
2311       }
2312     }
2313   }
2314   return false;
2315 }
2316
2317
2318 bool
2319 HexagonInstrInfo::
2320 SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
2321                   const SmallVectorImpl<MachineOperand> &Pred2) const {
2322   // TODO: Fix this
2323   return false;
2324 }
2325
2326
2327 //
2328 // We indicate that we want to reverse the branch by
2329 // inserting a 0 at the beginning of the Cond vector.
2330 //
2331 bool HexagonInstrInfo::
2332 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
2333   if (!Cond.empty() && Cond[0].isImm() && Cond[0].getImm() == 0) {
2334     Cond.erase(Cond.begin());
2335   } else {
2336     Cond.insert(Cond.begin(), MachineOperand::CreateImm(0));
2337   }
2338   return false;
2339 }
2340
2341
2342 bool HexagonInstrInfo::
2343 isProfitableToDupForIfCvt(MachineBasicBlock &MBB,unsigned NumInstrs,
2344                           const BranchProbability &Probability) const {
2345   return (NumInstrs <= 4);
2346 }
2347
2348 bool HexagonInstrInfo::isDeallocRet(const MachineInstr *MI) const {
2349   switch (MI->getOpcode()) {
2350   default: return false;
2351   case Hexagon::DEALLOC_RET_V4 :
2352   case Hexagon::DEALLOC_RET_cPt_V4 :
2353   case Hexagon::DEALLOC_RET_cNotPt_V4 :
2354   case Hexagon::DEALLOC_RET_cdnPnt_V4 :
2355   case Hexagon::DEALLOC_RET_cNotdnPnt_V4 :
2356   case Hexagon::DEALLOC_RET_cdnPt_V4 :
2357   case Hexagon::DEALLOC_RET_cNotdnPt_V4 :
2358    return true;
2359   }
2360 }
2361
2362
2363 bool HexagonInstrInfo::
2364 isValidOffset(const int Opcode, const int Offset) const {
2365   // This function is to check whether the "Offset" is in the correct range of
2366   // the given "Opcode". If "Offset" is not in the correct range, "ADD_ri" is
2367   // inserted to calculate the final address. Due to this reason, the function
2368   // assumes that the "Offset" has correct alignment.
2369
2370   switch(Opcode) {
2371
2372   case Hexagon::LDriw:
2373   case Hexagon::LDriw_indexed:
2374   case Hexagon::LDriw_f:
2375   case Hexagon::STriw_indexed:
2376   case Hexagon::STriw:
2377   case Hexagon::STriw_f:
2378     assert((Offset % 4 == 0) && "Offset has incorrect alignment");
2379     return (Offset >= Hexagon_MEMW_OFFSET_MIN) &&
2380       (Offset <= Hexagon_MEMW_OFFSET_MAX);
2381
2382   case Hexagon::LDrid:
2383   case Hexagon::LDrid_indexed:
2384   case Hexagon::LDrid_f:
2385   case Hexagon::STrid:
2386   case Hexagon::STrid_indexed:
2387   case Hexagon::STrid_f:
2388     assert((Offset % 8 == 0) && "Offset has incorrect alignment");
2389     return (Offset >= Hexagon_MEMD_OFFSET_MIN) &&
2390       (Offset <= Hexagon_MEMD_OFFSET_MAX);
2391
2392   case Hexagon::LDrih:
2393   case Hexagon::LDriuh:
2394   case Hexagon::STrih:
2395     assert((Offset % 2 == 0) && "Offset has incorrect alignment");
2396     return (Offset >= Hexagon_MEMH_OFFSET_MIN) &&
2397       (Offset <= Hexagon_MEMH_OFFSET_MAX);
2398
2399   case Hexagon::LDrib:
2400   case Hexagon::STrib:
2401   case Hexagon::LDriub:
2402     return (Offset >= Hexagon_MEMB_OFFSET_MIN) &&
2403       (Offset <= Hexagon_MEMB_OFFSET_MAX);
2404
2405   case Hexagon::ADD_ri:
2406   case Hexagon::TFR_FI:
2407     return (Offset >= Hexagon_ADDI_OFFSET_MIN) &&
2408       (Offset <= Hexagon_ADDI_OFFSET_MAX);
2409
2410   case Hexagon::MEMw_ADDi_indexed_MEM_V4 :
2411   case Hexagon::MEMw_SUBi_indexed_MEM_V4 :
2412   case Hexagon::MEMw_ADDr_indexed_MEM_V4 :
2413   case Hexagon::MEMw_SUBr_indexed_MEM_V4 :
2414   case Hexagon::MEMw_ANDr_indexed_MEM_V4 :
2415   case Hexagon::MEMw_ORr_indexed_MEM_V4 :
2416   case Hexagon::MEMw_ADDi_MEM_V4 :
2417   case Hexagon::MEMw_SUBi_MEM_V4 :
2418   case Hexagon::MEMw_ADDr_MEM_V4 :
2419   case Hexagon::MEMw_SUBr_MEM_V4 :
2420   case Hexagon::MEMw_ANDr_MEM_V4 :
2421   case Hexagon::MEMw_ORr_MEM_V4 :
2422     assert ((Offset % 4) == 0 && "MEMOPw offset is not aligned correctly." );
2423     return (0 <= Offset && Offset <= 255);
2424
2425   case Hexagon::MEMh_ADDi_indexed_MEM_V4 :
2426   case Hexagon::MEMh_SUBi_indexed_MEM_V4 :
2427   case Hexagon::MEMh_ADDr_indexed_MEM_V4 :
2428   case Hexagon::MEMh_SUBr_indexed_MEM_V4 :
2429   case Hexagon::MEMh_ANDr_indexed_MEM_V4 :
2430   case Hexagon::MEMh_ORr_indexed_MEM_V4 :
2431   case Hexagon::MEMh_ADDi_MEM_V4 :
2432   case Hexagon::MEMh_SUBi_MEM_V4 :
2433   case Hexagon::MEMh_ADDr_MEM_V4 :
2434   case Hexagon::MEMh_SUBr_MEM_V4 :
2435   case Hexagon::MEMh_ANDr_MEM_V4 :
2436   case Hexagon::MEMh_ORr_MEM_V4 :
2437     assert ((Offset % 2) == 0 && "MEMOPh offset is not aligned correctly." );
2438     return (0 <= Offset && Offset <= 127);
2439
2440   case Hexagon::MEMb_ADDi_indexed_MEM_V4 :
2441   case Hexagon::MEMb_SUBi_indexed_MEM_V4 :
2442   case Hexagon::MEMb_ADDr_indexed_MEM_V4 :
2443   case Hexagon::MEMb_SUBr_indexed_MEM_V4 :
2444   case Hexagon::MEMb_ANDr_indexed_MEM_V4 :
2445   case Hexagon::MEMb_ORr_indexed_MEM_V4 :
2446   case Hexagon::MEMb_ADDi_MEM_V4 :
2447   case Hexagon::MEMb_SUBi_MEM_V4 :
2448   case Hexagon::MEMb_ADDr_MEM_V4 :
2449   case Hexagon::MEMb_SUBr_MEM_V4 :
2450   case Hexagon::MEMb_ANDr_MEM_V4 :
2451   case Hexagon::MEMb_ORr_MEM_V4 :
2452     return (0 <= Offset && Offset <= 63);
2453
2454   // LDri_pred and STriw_pred are pseudo operations, so it has to take offset of
2455   // any size. Later pass knows how to handle it.
2456   case Hexagon::STriw_pred:
2457   case Hexagon::LDriw_pred:
2458     return true;
2459
2460   case Hexagon::LOOP0_i:
2461     return isUInt<10>(Offset);
2462
2463   // INLINEASM is very special.
2464   case Hexagon::INLINEASM:
2465     return true;
2466   }
2467
2468   llvm_unreachable("No offset range is defined for this opcode. "
2469                    "Please define it in the above switch statement!");
2470 }
2471
2472
2473 //
2474 // Check if the Offset is a valid auto-inc imm by Load/Store Type.
2475 //
2476 bool HexagonInstrInfo::
2477 isValidAutoIncImm(const EVT VT, const int Offset) const {
2478
2479   if (VT == MVT::i64) {
2480       return (Offset >= Hexagon_MEMD_AUTOINC_MIN &&
2481               Offset <= Hexagon_MEMD_AUTOINC_MAX &&
2482               (Offset & 0x7) == 0);
2483   }
2484   if (VT == MVT::i32) {
2485       return (Offset >= Hexagon_MEMW_AUTOINC_MIN &&
2486               Offset <= Hexagon_MEMW_AUTOINC_MAX &&
2487               (Offset & 0x3) == 0);
2488   }
2489   if (VT == MVT::i16) {
2490       return (Offset >= Hexagon_MEMH_AUTOINC_MIN &&
2491               Offset <= Hexagon_MEMH_AUTOINC_MAX &&
2492               (Offset & 0x1) == 0);
2493   }
2494   if (VT == MVT::i8) {
2495       return (Offset >= Hexagon_MEMB_AUTOINC_MIN &&
2496               Offset <= Hexagon_MEMB_AUTOINC_MAX);
2497   }
2498   llvm_unreachable("Not an auto-inc opc!");
2499 }
2500
2501
2502 bool HexagonInstrInfo::
2503 isMemOp(const MachineInstr *MI) const {
2504   switch (MI->getOpcode())
2505   {
2506     default: return false;
2507     case Hexagon::MEMw_ADDi_indexed_MEM_V4 :
2508     case Hexagon::MEMw_SUBi_indexed_MEM_V4 :
2509     case Hexagon::MEMw_ADDr_indexed_MEM_V4 :
2510     case Hexagon::MEMw_SUBr_indexed_MEM_V4 :
2511     case Hexagon::MEMw_ANDr_indexed_MEM_V4 :
2512     case Hexagon::MEMw_ORr_indexed_MEM_V4 :
2513     case Hexagon::MEMw_ADDi_MEM_V4 :
2514     case Hexagon::MEMw_SUBi_MEM_V4 :
2515     case Hexagon::MEMw_ADDr_MEM_V4 :
2516     case Hexagon::MEMw_SUBr_MEM_V4 :
2517     case Hexagon::MEMw_ANDr_MEM_V4 :
2518     case Hexagon::MEMw_ORr_MEM_V4 :
2519     case Hexagon::MEMh_ADDi_indexed_MEM_V4 :
2520     case Hexagon::MEMh_SUBi_indexed_MEM_V4 :
2521     case Hexagon::MEMh_ADDr_indexed_MEM_V4 :
2522     case Hexagon::MEMh_SUBr_indexed_MEM_V4 :
2523     case Hexagon::MEMh_ANDr_indexed_MEM_V4 :
2524     case Hexagon::MEMh_ORr_indexed_MEM_V4 :
2525     case Hexagon::MEMh_ADDi_MEM_V4 :
2526     case Hexagon::MEMh_SUBi_MEM_V4 :
2527     case Hexagon::MEMh_ADDr_MEM_V4 :
2528     case Hexagon::MEMh_SUBr_MEM_V4 :
2529     case Hexagon::MEMh_ANDr_MEM_V4 :
2530     case Hexagon::MEMh_ORr_MEM_V4 :
2531     case Hexagon::MEMb_ADDi_indexed_MEM_V4 :
2532     case Hexagon::MEMb_SUBi_indexed_MEM_V4 :
2533     case Hexagon::MEMb_ADDr_indexed_MEM_V4 :
2534     case Hexagon::MEMb_SUBr_indexed_MEM_V4 :
2535     case Hexagon::MEMb_ANDr_indexed_MEM_V4 :
2536     case Hexagon::MEMb_ORr_indexed_MEM_V4 :
2537     case Hexagon::MEMb_ADDi_MEM_V4 :
2538     case Hexagon::MEMb_SUBi_MEM_V4 :
2539     case Hexagon::MEMb_ADDr_MEM_V4 :
2540     case Hexagon::MEMb_SUBr_MEM_V4 :
2541     case Hexagon::MEMb_ANDr_MEM_V4 :
2542     case Hexagon::MEMb_ORr_MEM_V4 :
2543       return true;
2544   }
2545 }
2546
2547
2548 bool HexagonInstrInfo::
2549 isSpillPredRegOp(const MachineInstr *MI) const {
2550   switch (MI->getOpcode()) {
2551     default: return false;
2552     case Hexagon::STriw_pred :
2553     case Hexagon::LDriw_pred :
2554       return true;
2555   }
2556 }
2557
2558 bool HexagonInstrInfo::isNewValueJumpCandidate(const MachineInstr *MI) const {
2559   switch (MI->getOpcode()) {
2560     default: return false;
2561     case Hexagon::CMPEQrr:
2562     case Hexagon::CMPEQri:
2563     case Hexagon::CMPLTrr:
2564     case Hexagon::CMPGTrr:
2565     case Hexagon::CMPGTri:
2566     case Hexagon::CMPLTUrr:
2567     case Hexagon::CMPGTUrr:
2568     case Hexagon::CMPGTUri:
2569     case Hexagon::CMPGEri:
2570     case Hexagon::CMPGEUri:
2571       return true;
2572   }
2573 }
2574
2575 bool HexagonInstrInfo::
2576 isConditionalTransfer (const MachineInstr *MI) const {
2577   switch (MI->getOpcode()) {
2578     default: return false;
2579     case Hexagon::TFR_cPt:
2580     case Hexagon::TFR_cNotPt:
2581     case Hexagon::TFRI_cPt:
2582     case Hexagon::TFRI_cNotPt:
2583     case Hexagon::TFR_cdnPt:
2584     case Hexagon::TFR_cdnNotPt:
2585     case Hexagon::TFRI_cdnPt:
2586     case Hexagon::TFRI_cdnNotPt:
2587       return true;
2588   }
2589 }
2590
2591 bool HexagonInstrInfo::isConditionalALU32 (const MachineInstr* MI) const {
2592   const HexagonRegisterInfo& QRI = getRegisterInfo();
2593   switch (MI->getOpcode())
2594   {
2595     default: return false;
2596     case Hexagon::ADD_ri_cPt:
2597     case Hexagon::ADD_ri_cNotPt:
2598     case Hexagon::ADD_rr_cPt:
2599     case Hexagon::ADD_rr_cNotPt:
2600     case Hexagon::XOR_rr_cPt:
2601     case Hexagon::XOR_rr_cNotPt:
2602     case Hexagon::AND_rr_cPt:
2603     case Hexagon::AND_rr_cNotPt:
2604     case Hexagon::OR_rr_cPt:
2605     case Hexagon::OR_rr_cNotPt:
2606     case Hexagon::SUB_rr_cPt:
2607     case Hexagon::SUB_rr_cNotPt:
2608     case Hexagon::COMBINE_rr_cPt:
2609     case Hexagon::COMBINE_rr_cNotPt:
2610       return true;
2611     case Hexagon::ASLH_cPt_V4:
2612     case Hexagon::ASLH_cNotPt_V4:
2613     case Hexagon::ASRH_cPt_V4:
2614     case Hexagon::ASRH_cNotPt_V4:
2615     case Hexagon::SXTB_cPt_V4:
2616     case Hexagon::SXTB_cNotPt_V4:
2617     case Hexagon::SXTH_cPt_V4:
2618     case Hexagon::SXTH_cNotPt_V4:
2619     case Hexagon::ZXTB_cPt_V4:
2620     case Hexagon::ZXTB_cNotPt_V4:
2621     case Hexagon::ZXTH_cPt_V4:
2622     case Hexagon::ZXTH_cNotPt_V4:
2623       return QRI.Subtarget.hasV4TOps();
2624   }
2625 }
2626
2627 bool HexagonInstrInfo::
2628 isConditionalLoad (const MachineInstr* MI) const {
2629   const HexagonRegisterInfo& QRI = getRegisterInfo();
2630   switch (MI->getOpcode())
2631   {
2632     default: return false;
2633     case Hexagon::LDrid_cPt :
2634     case Hexagon::LDrid_cNotPt :
2635     case Hexagon::LDrid_indexed_cPt :
2636     case Hexagon::LDrid_indexed_cNotPt :
2637     case Hexagon::LDriw_cPt :
2638     case Hexagon::LDriw_cNotPt :
2639     case Hexagon::LDriw_indexed_cPt :
2640     case Hexagon::LDriw_indexed_cNotPt :
2641     case Hexagon::LDrih_cPt :
2642     case Hexagon::LDrih_cNotPt :
2643     case Hexagon::LDrih_indexed_cPt :
2644     case Hexagon::LDrih_indexed_cNotPt :
2645     case Hexagon::LDrib_cPt :
2646     case Hexagon::LDrib_cNotPt :
2647     case Hexagon::LDrib_indexed_cPt :
2648     case Hexagon::LDrib_indexed_cNotPt :
2649     case Hexagon::LDriuh_cPt :
2650     case Hexagon::LDriuh_cNotPt :
2651     case Hexagon::LDriuh_indexed_cPt :
2652     case Hexagon::LDriuh_indexed_cNotPt :
2653     case Hexagon::LDriub_cPt :
2654     case Hexagon::LDriub_cNotPt :
2655     case Hexagon::LDriub_indexed_cPt :
2656     case Hexagon::LDriub_indexed_cNotPt :
2657       return true;
2658     case Hexagon::POST_LDrid_cPt :
2659     case Hexagon::POST_LDrid_cNotPt :
2660     case Hexagon::POST_LDriw_cPt :
2661     case Hexagon::POST_LDriw_cNotPt :
2662     case Hexagon::POST_LDrih_cPt :
2663     case Hexagon::POST_LDrih_cNotPt :
2664     case Hexagon::POST_LDrib_cPt :
2665     case Hexagon::POST_LDrib_cNotPt :
2666     case Hexagon::POST_LDriuh_cPt :
2667     case Hexagon::POST_LDriuh_cNotPt :
2668     case Hexagon::POST_LDriub_cPt :
2669     case Hexagon::POST_LDriub_cNotPt :
2670       return QRI.Subtarget.hasV4TOps();
2671     case Hexagon::LDrid_indexed_shl_cPt_V4 :
2672     case Hexagon::LDrid_indexed_shl_cNotPt_V4 :
2673     case Hexagon::LDrib_indexed_shl_cPt_V4 :
2674     case Hexagon::LDrib_indexed_shl_cNotPt_V4 :
2675     case Hexagon::LDriub_indexed_shl_cPt_V4 :
2676     case Hexagon::LDriub_indexed_shl_cNotPt_V4 :
2677     case Hexagon::LDrih_indexed_shl_cPt_V4 :
2678     case Hexagon::LDrih_indexed_shl_cNotPt_V4 :
2679     case Hexagon::LDriuh_indexed_shl_cPt_V4 :
2680     case Hexagon::LDriuh_indexed_shl_cNotPt_V4 :
2681     case Hexagon::LDriw_indexed_shl_cPt_V4 :
2682     case Hexagon::LDriw_indexed_shl_cNotPt_V4 :
2683       return QRI.Subtarget.hasV4TOps();
2684   }
2685 }
2686
2687 // Returns true if an instruction is a conditional store.
2688 //
2689 // Note: It doesn't include conditional new-value stores as they can't be
2690 // converted to .new predicate.
2691 //
2692 //               p.new NV store [ if(p0.new)memw(R0+#0)=R2.new ]
2693 //                ^           ^
2694 //               /             \ (not OK. it will cause new-value store to be
2695 //              /               X conditional on p0.new while R2 producer is
2696 //             /                 \ on p0)
2697 //            /                   \.
2698 //     p.new store                 p.old NV store
2699 // [if(p0.new)memw(R0+#0)=R2]    [if(p0)memw(R0+#0)=R2.new]
2700 //            ^                  ^
2701 //             \                /
2702 //              \              /
2703 //               \            /
2704 //                 p.old store
2705 //             [if (p0)memw(R0+#0)=R2]
2706 //
2707 // The above diagram shows the steps involoved in the conversion of a predicated
2708 // store instruction to its .new predicated new-value form.
2709 //
2710 // The following set of instructions further explains the scenario where
2711 // conditional new-value store becomes invalid when promoted to .new predicate
2712 // form.
2713 //
2714 // { 1) if (p0) r0 = add(r1, r2)
2715 //   2) p0 = cmp.eq(r3, #0) }
2716 //
2717 //   3) if (p0) memb(r1+#0) = r0  --> this instruction can't be grouped with
2718 // the first two instructions because in instr 1, r0 is conditional on old value
2719 // of p0 but its use in instr 3 is conditional on p0 modified by instr 2 which
2720 // is not valid for new-value stores.
2721 bool HexagonInstrInfo::
2722 isConditionalStore (const MachineInstr* MI) const {
2723   const HexagonRegisterInfo& QRI = getRegisterInfo();
2724   switch (MI->getOpcode())
2725   {
2726     default: return false;
2727     case Hexagon::STrib_imm_cPt_V4 :
2728     case Hexagon::STrib_imm_cNotPt_V4 :
2729     case Hexagon::STrib_indexed_shl_cPt_V4 :
2730     case Hexagon::STrib_indexed_shl_cNotPt_V4 :
2731     case Hexagon::STrib_cPt :
2732     case Hexagon::STrib_cNotPt :
2733     case Hexagon::POST_STbri_cPt :
2734     case Hexagon::POST_STbri_cNotPt :
2735     case Hexagon::STrid_indexed_cPt :
2736     case Hexagon::STrid_indexed_cNotPt :
2737     case Hexagon::STrid_indexed_shl_cPt_V4 :
2738     case Hexagon::POST_STdri_cPt :
2739     case Hexagon::POST_STdri_cNotPt :
2740     case Hexagon::STrih_cPt :
2741     case Hexagon::STrih_cNotPt :
2742     case Hexagon::STrih_indexed_cPt :
2743     case Hexagon::STrih_indexed_cNotPt :
2744     case Hexagon::STrih_imm_cPt_V4 :
2745     case Hexagon::STrih_imm_cNotPt_V4 :
2746     case Hexagon::STrih_indexed_shl_cPt_V4 :
2747     case Hexagon::STrih_indexed_shl_cNotPt_V4 :
2748     case Hexagon::POST_SThri_cPt :
2749     case Hexagon::POST_SThri_cNotPt :
2750     case Hexagon::STriw_cPt :
2751     case Hexagon::STriw_cNotPt :
2752     case Hexagon::STriw_indexed_cPt :
2753     case Hexagon::STriw_indexed_cNotPt :
2754     case Hexagon::STriw_imm_cPt_V4 :
2755     case Hexagon::STriw_imm_cNotPt_V4 :
2756     case Hexagon::STriw_indexed_shl_cPt_V4 :
2757     case Hexagon::STriw_indexed_shl_cNotPt_V4 :
2758     case Hexagon::POST_STwri_cPt :
2759     case Hexagon::POST_STwri_cNotPt :
2760       return QRI.Subtarget.hasV4TOps();
2761
2762     // V4 global address store before promoting to dot new.
2763     case Hexagon::STd_GP_cPt_V4 :
2764     case Hexagon::STd_GP_cNotPt_V4 :
2765     case Hexagon::STb_GP_cPt_V4 :
2766     case Hexagon::STb_GP_cNotPt_V4 :
2767     case Hexagon::STh_GP_cPt_V4 :
2768     case Hexagon::STh_GP_cNotPt_V4 :
2769     case Hexagon::STw_GP_cPt_V4 :
2770     case Hexagon::STw_GP_cNotPt_V4 :
2771       return QRI.Subtarget.hasV4TOps();
2772
2773     // Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
2774     // from the "Conditional Store" list. Because a predicated new value store
2775     // would NOT be promoted to a double dot new store. See diagram below:
2776     // This function returns yes for those stores that are predicated but not
2777     // yet promoted to predicate dot new instructions.
2778     //
2779     //                          +---------------------+
2780     //                    /-----| if (p0) memw(..)=r0 |---------\~
2781     //                   ||     +---------------------+         ||
2782     //          promote  ||       /\       /\                   ||  promote
2783     //                   ||      /||\     /||\                  ||
2784     //                  \||/    demote     ||                  \||/
2785     //                   \/       ||       ||                   \/
2786     //       +-------------------------+   ||   +-------------------------+
2787     //       | if (p0.new) memw(..)=r0 |   ||   | if (p0) memw(..)=r0.new |
2788     //       +-------------------------+   ||   +-------------------------+
2789     //                        ||           ||         ||
2790     //                        ||         demote      \||/
2791     //                      promote        ||         \/ NOT possible
2792     //                        ||           ||         /\~
2793     //                       \||/          ||        /||\~
2794     //                        \/           ||         ||
2795     //                      +-----------------------------+
2796     //                      | if (p0.new) memw(..)=r0.new |
2797     //                      +-----------------------------+
2798     //                           Double Dot New Store
2799     //
2800   }
2801 }
2802
2803 unsigned HexagonInstrInfo::getAddrMode(const MachineInstr* MI) const {
2804   const uint64_t F = MI->getDesc().TSFlags;
2805
2806   return((F >> HexagonII::AddrModePos) & HexagonII::AddrModeMask);
2807 }
2808
2809 /// immediateExtend - Changes the instruction in place to one using an immediate
2810 /// extender.
2811 void HexagonInstrInfo::immediateExtend(MachineInstr *MI) const {
2812   assert((isExtendable(MI)||isConstExtended(MI)) &&
2813                                "Instruction must be extendable");
2814   // Find which operand is extendable.
2815   short ExtOpNum = getCExtOpNum(MI);
2816   MachineOperand &MO = MI->getOperand(ExtOpNum);
2817   // This needs to be something we understand.
2818   assert((MO.isMBB() || MO.isImm()) &&
2819          "Branch with unknown extendable field type");
2820   // Mark given operand as extended.
2821   MO.addTargetFlag(HexagonII::HMOTF_ConstExtended);
2822 }
2823
2824 DFAPacketizer *HexagonInstrInfo::
2825 CreateTargetScheduleState(const TargetMachine *TM,
2826                            const ScheduleDAG *DAG) const {
2827   const InstrItineraryData *II = TM->getInstrItineraryData();
2828   return TM->getSubtarget<HexagonGenSubtargetInfo>().createDFAPacketizer(II);
2829 }
2830
2831 bool HexagonInstrInfo::isSchedulingBoundary(const MachineInstr *MI,
2832                                             const MachineBasicBlock *MBB,
2833                                             const MachineFunction &MF) const {
2834   // Debug info is never a scheduling boundary. It's necessary to be explicit
2835   // due to the special treatment of IT instructions below, otherwise a
2836   // dbg_value followed by an IT will result in the IT instruction being
2837   // considered a scheduling hazard, which is wrong. It should be the actual
2838   // instruction preceding the dbg_value instruction(s), just like it is
2839   // when debug info is not present.
2840   if (MI->isDebugValue())
2841     return false;
2842
2843   // Terminators and labels can't be scheduled around.
2844   if (MI->getDesc().isTerminator() || MI->isLabel() || MI->isInlineAsm())
2845     return true;
2846
2847   return false;
2848 }
2849
2850 bool HexagonInstrInfo::isConstExtended(MachineInstr *MI) const {
2851
2852   // Constant extenders are allowed only for V4 and above.
2853   if (!Subtarget.hasV4TOps())
2854     return false;
2855
2856   const uint64_t F = MI->getDesc().TSFlags;
2857   unsigned isExtended = (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
2858   if (isExtended) // Instruction must be extended.
2859     return true;
2860
2861   unsigned isExtendable = (F >> HexagonII::ExtendablePos)
2862                           & HexagonII::ExtendableMask;
2863   if (!isExtendable)
2864     return false;
2865
2866   short ExtOpNum = getCExtOpNum(MI);
2867   const MachineOperand &MO = MI->getOperand(ExtOpNum);
2868   // Use MO operand flags to determine if MO
2869   // has the HMOTF_ConstExtended flag set.
2870   if (MO.getTargetFlags() && HexagonII::HMOTF_ConstExtended)
2871     return true;
2872   // If this is a Machine BB address we are talking about, and it is
2873   // not marked as extended, say so.
2874   if (MO.isMBB())
2875     return false;
2876
2877   // We could be using an instruction with an extendable immediate and shoehorn
2878   // a global address into it. If it is a global address it will be constant
2879   // extended. We do this for COMBINE.
2880   // We currently only handle isGlobal() because it is the only kind of
2881   // object we are going to end up with here for now.
2882   // In the future we probably should add isSymbol(), etc.
2883   if (MO.isGlobal() || MO.isSymbol())
2884     return true;
2885
2886   // If the extendable operand is not 'Immediate' type, the instruction should
2887   // have 'isExtended' flag set.
2888   assert(MO.isImm() && "Extendable operand must be Immediate type");
2889
2890   int MinValue = getMinValue(MI);
2891   int MaxValue = getMaxValue(MI);
2892   int ImmValue = MO.getImm();
2893
2894   return (ImmValue < MinValue || ImmValue > MaxValue);
2895 }
2896
2897 // Returns true if a particular operand is extendable for an instruction.
2898 bool HexagonInstrInfo::isOperandExtended(const MachineInstr *MI,
2899                                          unsigned short OperandNum) const {
2900   // Constant extenders are allowed only for V4 and above.
2901   if (!Subtarget.hasV4TOps())
2902     return false;
2903
2904   const uint64_t F = MI->getDesc().TSFlags;
2905
2906   return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask)
2907           == OperandNum;
2908 }
2909
2910 // Returns Operand Index for the constant extended instruction.
2911 unsigned short HexagonInstrInfo::getCExtOpNum(const MachineInstr *MI) const {
2912   const uint64_t F = MI->getDesc().TSFlags;
2913   return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask);
2914 }
2915
2916 // Returns the min value that doesn't need to be extended.
2917 int HexagonInstrInfo::getMinValue(const MachineInstr *MI) const {
2918   const uint64_t F = MI->getDesc().TSFlags;
2919   unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
2920                     & HexagonII::ExtentSignedMask;
2921   unsigned bits =  (F >> HexagonII::ExtentBitsPos)
2922                     & HexagonII::ExtentBitsMask;
2923
2924   if (isSigned) // if value is signed
2925     return -1 << (bits - 1);
2926   else
2927     return 0;
2928 }
2929
2930 // Returns the max value that doesn't need to be extended.
2931 int HexagonInstrInfo::getMaxValue(const MachineInstr *MI) const {
2932   const uint64_t F = MI->getDesc().TSFlags;
2933   unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
2934                     & HexagonII::ExtentSignedMask;
2935   unsigned bits =  (F >> HexagonII::ExtentBitsPos)
2936                     & HexagonII::ExtentBitsMask;
2937
2938   if (isSigned) // if value is signed
2939     return ~(-1 << (bits - 1));
2940   else
2941     return ~(-1 << bits);
2942 }
2943
2944 // Returns true if an instruction can be converted into a non-extended
2945 // equivalent instruction.
2946 bool HexagonInstrInfo::NonExtEquivalentExists (const MachineInstr *MI) const {
2947
2948   short NonExtOpcode;
2949   // Check if the instruction has a register form that uses register in place
2950   // of the extended operand, if so return that as the non-extended form.
2951   if (Hexagon::getRegForm(MI->getOpcode()) >= 0)
2952     return true;
2953
2954   if (MI->getDesc().mayLoad() || MI->getDesc().mayStore()) {
2955     // Check addressing mode and retreive non-ext equivalent instruction.
2956
2957     switch (getAddrMode(MI)) {
2958     case HexagonII::Absolute :
2959       // Load/store with absolute addressing mode can be converted into
2960       // base+offset mode.
2961       NonExtOpcode = Hexagon::getBasedWithImmOffset(MI->getOpcode());
2962       break;
2963     case HexagonII::BaseImmOffset :
2964       // Load/store with base+offset addressing mode can be converted into
2965       // base+register offset addressing mode. However left shift operand should
2966       // be set to 0.
2967       NonExtOpcode = Hexagon::getBaseWithRegOffset(MI->getOpcode());
2968       break;
2969     default:
2970       return false;
2971     }
2972     if (NonExtOpcode < 0)
2973       return false;
2974     return true;
2975   }
2976   return false;
2977 }
2978
2979 // Returns opcode of the non-extended equivalent instruction.
2980 short HexagonInstrInfo::getNonExtOpcode (const MachineInstr *MI) const {
2981
2982   // Check if the instruction has a register form that uses register in place
2983   // of the extended operand, if so return that as the non-extended form.
2984   short NonExtOpcode = Hexagon::getRegForm(MI->getOpcode());
2985     if (NonExtOpcode >= 0)
2986       return NonExtOpcode;
2987
2988   if (MI->getDesc().mayLoad() || MI->getDesc().mayStore()) {
2989     // Check addressing mode and retreive non-ext equivalent instruction.
2990     switch (getAddrMode(MI)) {
2991     case HexagonII::Absolute :
2992       return Hexagon::getBasedWithImmOffset(MI->getOpcode());
2993     case HexagonII::BaseImmOffset :
2994       return Hexagon::getBaseWithRegOffset(MI->getOpcode());
2995     default:
2996       return -1;
2997     }
2998   }
2999   return -1;
3000 }