[Hexagon] Removing old versions of cmph and updating references.
[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/Debug.h"
27 #include "llvm/Support/MathExtras.h"
28 #include "llvm/Support/raw_ostream.h"
29
30 using namespace llvm;
31
32 #define DEBUG_TYPE "hexagon-instrinfo"
33
34 #define GET_INSTRINFO_CTOR_DTOR
35 #define GET_INSTRMAP_INFO
36 #include "HexagonGenInstrInfo.inc"
37 #include "HexagonGenDFAPacketizer.inc"
38
39 ///
40 /// Constants for Hexagon instructions.
41 ///
42 const int Hexagon_MEMW_OFFSET_MAX = 4095;
43 const int Hexagon_MEMW_OFFSET_MIN = -4096;
44 const int Hexagon_MEMD_OFFSET_MAX = 8191;
45 const int Hexagon_MEMD_OFFSET_MIN = -8192;
46 const int Hexagon_MEMH_OFFSET_MAX = 2047;
47 const int Hexagon_MEMH_OFFSET_MIN = -2048;
48 const int Hexagon_MEMB_OFFSET_MAX = 1023;
49 const int Hexagon_MEMB_OFFSET_MIN = -1024;
50 const int Hexagon_ADDI_OFFSET_MAX = 32767;
51 const int Hexagon_ADDI_OFFSET_MIN = -32768;
52 const int Hexagon_MEMD_AUTOINC_MAX = 56;
53 const int Hexagon_MEMD_AUTOINC_MIN = -64;
54 const int Hexagon_MEMW_AUTOINC_MAX = 28;
55 const int Hexagon_MEMW_AUTOINC_MIN = -32;
56 const int Hexagon_MEMH_AUTOINC_MAX = 14;
57 const int Hexagon_MEMH_AUTOINC_MIN = -16;
58 const int Hexagon_MEMB_AUTOINC_MAX = 7;
59 const int Hexagon_MEMB_AUTOINC_MIN = -8;
60
61 // Pin the vtable to this file.
62 void HexagonInstrInfo::anchor() {}
63
64 HexagonInstrInfo::HexagonInstrInfo(HexagonSubtarget &ST)
65   : HexagonGenInstrInfo(Hexagon::ADJCALLSTACKDOWN, Hexagon::ADJCALLSTACKUP),
66     RI(ST), Subtarget(ST) {
67 }
68
69
70 /// isLoadFromStackSlot - If the specified machine instruction is a direct
71 /// load from a stack slot, return the virtual or physical register number of
72 /// the destination along with the FrameIndex of the loaded stack slot.  If
73 /// not, return 0.  This predicate must return 0 if the instruction has
74 /// any side effects other than loading from the stack slot.
75 unsigned HexagonInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
76                                              int &FrameIndex) const {
77
78
79   switch (MI->getOpcode()) {
80   default: break;
81   case Hexagon::L2_loadri_io:
82   case Hexagon::L2_loadrd_io:
83   case Hexagon::L2_loadrh_io:
84   case Hexagon::L2_loadrb_io:
85   case Hexagon::L2_loadrub_io:
86     if (MI->getOperand(2).isFI() &&
87         MI->getOperand(1).isImm() && (MI->getOperand(1).getImm() == 0)) {
88       FrameIndex = MI->getOperand(2).getIndex();
89       return MI->getOperand(0).getReg();
90     }
91     break;
92   }
93   return 0;
94 }
95
96
97 /// isStoreToStackSlot - If the specified machine instruction is a direct
98 /// store to a stack slot, return the virtual or physical register number of
99 /// the source reg along with the FrameIndex of the loaded stack slot.  If
100 /// not, return 0.  This predicate must return 0 if the instruction has
101 /// any side effects other than storing to the stack slot.
102 unsigned HexagonInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
103                                             int &FrameIndex) const {
104   switch (MI->getOpcode()) {
105   default: break;
106   case Hexagon::S2_storeri_io:
107   case Hexagon::S2_storerd_io:
108   case Hexagon::S2_storerh_io:
109   case Hexagon::S2_storerb_io:
110     if (MI->getOperand(2).isFI() &&
111         MI->getOperand(1).isImm() && (MI->getOperand(1).getImm() == 0)) {
112       FrameIndex = MI->getOperand(0).getIndex();
113       return MI->getOperand(2).getReg();
114     }
115     break;
116   }
117   return 0;
118 }
119
120
121 unsigned
122 HexagonInstrInfo::InsertBranch(MachineBasicBlock &MBB,MachineBasicBlock *TBB,
123                              MachineBasicBlock *FBB,
124                              const SmallVectorImpl<MachineOperand> &Cond,
125                              DebugLoc DL) const{
126
127     int BOpc   = Hexagon::J2_jump;
128     int BccOpc = Hexagon::J2_jumpt;
129
130     assert(TBB && "InsertBranch must not be told to insert a fallthrough");
131
132     int regPos = 0;
133     // Check if ReverseBranchCondition has asked to reverse this branch
134     // If we want to reverse the branch an odd number of times, we want
135     // JMP_f.
136     if (!Cond.empty() && Cond[0].isImm() && Cond[0].getImm() == 0) {
137       BccOpc = Hexagon::J2_jumpf;
138       regPos = 1;
139     }
140
141     if (!FBB) {
142       if (Cond.empty()) {
143         // Due to a bug in TailMerging/CFG Optimization, we need to add a
144         // special case handling of a predicated jump followed by an
145         // unconditional jump. If not, Tail Merging and CFG Optimization go
146         // into an infinite loop.
147         MachineBasicBlock *NewTBB, *NewFBB;
148         SmallVector<MachineOperand, 4> Cond;
149         MachineInstr *Term = MBB.getFirstTerminator();
150         if (isPredicated(Term) && !AnalyzeBranch(MBB, NewTBB, NewFBB, Cond,
151                                                  false)) {
152           MachineBasicBlock *NextBB =
153             std::next(MachineFunction::iterator(&MBB));
154           if (NewTBB == NextBB) {
155             ReverseBranchCondition(Cond);
156             RemoveBranch(MBB);
157             return InsertBranch(MBB, TBB, nullptr, Cond, DL);
158           }
159         }
160         BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
161       } else {
162         BuildMI(&MBB, DL,
163                 get(BccOpc)).addReg(Cond[regPos].getReg()).addMBB(TBB);
164       }
165       return 1;
166     }
167
168     BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[regPos].getReg()).addMBB(TBB);
169     BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
170
171     return 2;
172 }
173
174
175 bool HexagonInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
176                                      MachineBasicBlock *&TBB,
177                                  MachineBasicBlock *&FBB,
178                                  SmallVectorImpl<MachineOperand> &Cond,
179                                  bool AllowModify) const {
180   TBB = nullptr;
181   FBB = nullptr;
182
183   // If the block has no terminators, it just falls into the block after it.
184   MachineBasicBlock::instr_iterator I = MBB.instr_end();
185   if (I == MBB.instr_begin())
186     return false;
187
188   // A basic block may looks like this:
189   //
190   //  [   insn
191   //     EH_LABEL
192   //      insn
193   //      insn
194   //      insn
195   //     EH_LABEL
196   //      insn     ]
197   //
198   // It has two succs but does not have a terminator
199   // Don't know how to handle it.
200   do {
201     --I;
202     if (I->isEHLabel())
203       return true;
204   } while (I != MBB.instr_begin());
205
206   I = MBB.instr_end();
207   --I;
208
209   while (I->isDebugValue()) {
210     if (I == MBB.instr_begin())
211       return false;
212     --I;
213   }
214
215   // Delete the JMP if it's equivalent to a fall-through.
216   if (AllowModify && I->getOpcode() == Hexagon::J2_jump &&
217       MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
218     DEBUG(dbgs()<< "\nErasing the jump to successor block\n";);
219     I->eraseFromParent();
220     I = MBB.instr_end();
221     if (I == MBB.instr_begin())
222       return false;
223     --I;
224   }
225   if (!isUnpredicatedTerminator(I))
226     return false;
227
228   // Get the last instruction in the block.
229   MachineInstr *LastInst = I;
230   MachineInstr *SecondLastInst = nullptr;
231   // Find one more terminator if present.
232   do {
233     if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(I)) {
234       if (!SecondLastInst)
235         SecondLastInst = I;
236       else
237         // This is a third branch.
238         return true;
239     }
240     if (I == MBB.instr_begin())
241       break;
242     --I;
243   } while(I);
244
245   int LastOpcode = LastInst->getOpcode();
246
247   bool LastOpcodeHasJMP_c = PredOpcodeHasJMP_c(LastOpcode);
248   bool LastOpcodeHasNot = PredOpcodeHasNot(LastOpcode);
249
250   // If there is only one terminator instruction, process it.
251   if (LastInst && !SecondLastInst) {
252     if (LastOpcode == Hexagon::J2_jump) {
253       TBB = LastInst->getOperand(0).getMBB();
254       return false;
255     }
256     if (LastOpcode == Hexagon::ENDLOOP0) {
257       TBB = LastInst->getOperand(0).getMBB();
258       Cond.push_back(LastInst->getOperand(0));
259       return false;
260     }
261     if (LastOpcodeHasJMP_c) {
262       TBB = LastInst->getOperand(1).getMBB();
263       if (LastOpcodeHasNot) {
264         Cond.push_back(MachineOperand::CreateImm(0));
265       }
266       Cond.push_back(LastInst->getOperand(0));
267       return false;
268     }
269     // Otherwise, don't know what this is.
270     return true;
271   }
272
273   int SecLastOpcode = SecondLastInst->getOpcode();
274
275   bool SecLastOpcodeHasJMP_c = PredOpcodeHasJMP_c(SecLastOpcode);
276   bool SecLastOpcodeHasNot = PredOpcodeHasNot(SecLastOpcode);
277   if (SecLastOpcodeHasJMP_c && (LastOpcode == Hexagon::J2_jump)) {
278     TBB =  SecondLastInst->getOperand(1).getMBB();
279     if (SecLastOpcodeHasNot)
280       Cond.push_back(MachineOperand::CreateImm(0));
281     Cond.push_back(SecondLastInst->getOperand(0));
282     FBB = LastInst->getOperand(0).getMBB();
283     return false;
284   }
285
286   // If the block ends with two Hexagon:JMPs, handle it.  The second one is not
287   // executed, so remove it.
288   if (SecLastOpcode == Hexagon::J2_jump && LastOpcode == Hexagon::J2_jump) {
289     TBB = SecondLastInst->getOperand(0).getMBB();
290     I = LastInst;
291     if (AllowModify)
292       I->eraseFromParent();
293     return false;
294   }
295
296   // If the block ends with an ENDLOOP, and JMP, handle it.
297   if (SecLastOpcode == Hexagon::ENDLOOP0 &&
298       LastOpcode == Hexagon::J2_jump) {
299     TBB = SecondLastInst->getOperand(0).getMBB();
300     Cond.push_back(SecondLastInst->getOperand(0));
301     FBB = LastInst->getOperand(0).getMBB();
302     return false;
303   }
304
305   // Otherwise, can't handle this.
306   return true;
307 }
308
309
310 unsigned HexagonInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
311   int BOpc   = Hexagon::J2_jump;
312   int BccOpc = Hexagon::J2_jumpt;
313   int BccOpcNot = Hexagon::J2_jumpf;
314
315   MachineBasicBlock::iterator I = MBB.end();
316   if (I == MBB.begin()) return 0;
317   --I;
318   if (I->getOpcode() != BOpc && I->getOpcode() != BccOpc &&
319       I->getOpcode() != BccOpcNot)
320     return 0;
321
322   // Remove the branch.
323   I->eraseFromParent();
324
325   I = MBB.end();
326
327   if (I == MBB.begin()) return 1;
328   --I;
329   if (I->getOpcode() != BccOpc && I->getOpcode() != BccOpcNot)
330     return 1;
331
332   // Remove the branch.
333   I->eraseFromParent();
334   return 2;
335 }
336
337
338 /// \brief For a comparison instruction, return the source registers in
339 /// \p SrcReg and \p SrcReg2 if having two register operands, and the value it
340 /// compares against in CmpValue. Return true if the comparison instruction
341 /// can be analyzed.
342 bool HexagonInstrInfo::analyzeCompare(const MachineInstr *MI,
343                                       unsigned &SrcReg, unsigned &SrcReg2,
344                                       int &Mask, int &Value) const {
345   unsigned Opc = MI->getOpcode();
346
347   // Set mask and the first source register.
348   switch (Opc) {
349     case Hexagon::C2_cmpeqp:
350     case Hexagon::C2_cmpeqi:
351     case Hexagon::C2_cmpeq:
352     case Hexagon::C2_cmpgtp:
353     case Hexagon::C2_cmpgtup:
354     case Hexagon::C2_cmpgtui:
355     case Hexagon::C2_cmpgtu:
356     case Hexagon::C2_cmpgti:
357     case Hexagon::C2_cmpgt:
358       SrcReg = MI->getOperand(1).getReg();
359       Mask = ~0;
360       break;
361     case Hexagon::A4_cmpbeqi:
362     case Hexagon::A4_cmpbeq:
363     case Hexagon::A4_cmpbgtui:
364     case Hexagon::A4_cmpbgtu:
365     case Hexagon::A4_cmpbgt:
366       SrcReg = MI->getOperand(1).getReg();
367       Mask = 0xFF;
368       break;
369     case Hexagon::A4_cmpheqi:
370     case Hexagon::A4_cmpheq:
371     case Hexagon::A4_cmphgtui:
372     case Hexagon::A4_cmphgtu:
373     case Hexagon::A4_cmphgt:
374       SrcReg = MI->getOperand(1).getReg();
375       Mask = 0xFFFF;
376       break;
377   }
378
379   // Set the value/second source register.
380   switch (Opc) {
381     case Hexagon::C2_cmpeqp:
382     case Hexagon::C2_cmpeq:
383     case Hexagon::C2_cmpgtp:
384     case Hexagon::C2_cmpgtup:
385     case Hexagon::C2_cmpgtu:
386     case Hexagon::C2_cmpgt:
387     case Hexagon::A4_cmpbeq:
388     case Hexagon::A4_cmpbgtu:
389     case Hexagon::A4_cmpbgt:
390     case Hexagon::A4_cmpheq:
391     case Hexagon::A4_cmphgtu:
392     case Hexagon::A4_cmphgt:
393       SrcReg2 = MI->getOperand(2).getReg();
394       return true;
395
396     case Hexagon::C2_cmpeqi:
397     case Hexagon::C2_cmpgtui:
398     case Hexagon::C2_cmpgti:
399     case Hexagon::A4_cmpbeqi:
400     case Hexagon::A4_cmpbgtui:
401     case Hexagon::A4_cmpheqi:
402     case Hexagon::A4_cmphgtui:
403       SrcReg2 = 0;
404       Value = MI->getOperand(2).getImm();
405       return true;
406   }
407
408   return false;
409 }
410
411
412 void HexagonInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
413                                  MachineBasicBlock::iterator I, DebugLoc DL,
414                                  unsigned DestReg, unsigned SrcReg,
415                                  bool KillSrc) const {
416   if (Hexagon::IntRegsRegClass.contains(SrcReg, DestReg)) {
417     BuildMI(MBB, I, DL, get(Hexagon::A2_tfr), DestReg).addReg(SrcReg);
418     return;
419   }
420   if (Hexagon::DoubleRegsRegClass.contains(SrcReg, DestReg)) {
421     BuildMI(MBB, I, DL, get(Hexagon::A2_tfrp), DestReg).addReg(SrcReg);
422     return;
423   }
424   if (Hexagon::PredRegsRegClass.contains(SrcReg, DestReg)) {
425     // Map Pd = Ps to Pd = or(Ps, Ps).
426     BuildMI(MBB, I, DL, get(Hexagon::C2_or),
427             DestReg).addReg(SrcReg).addReg(SrcReg);
428     return;
429   }
430   if (Hexagon::DoubleRegsRegClass.contains(DestReg) &&
431       Hexagon::IntRegsRegClass.contains(SrcReg)) {
432     // We can have an overlap between single and double reg: r1:0 = r0.
433     if(SrcReg == RI.getSubReg(DestReg, Hexagon::subreg_loreg)) {
434         // r1:0 = r0
435         BuildMI(MBB, I, DL, get(Hexagon::A2_tfrsi), (RI.getSubReg(DestReg,
436                 Hexagon::subreg_hireg))).addImm(0);
437     } else {
438         // r1:0 = r1 or no overlap.
439         BuildMI(MBB, I, DL, get(Hexagon::A2_tfr), (RI.getSubReg(DestReg,
440                 Hexagon::subreg_loreg))).addReg(SrcReg);
441         BuildMI(MBB, I, DL, get(Hexagon::A2_tfrsi), (RI.getSubReg(DestReg,
442                 Hexagon::subreg_hireg))).addImm(0);
443     }
444     return;
445   }
446   if (Hexagon::CtrRegsRegClass.contains(DestReg) &&
447       Hexagon::IntRegsRegClass.contains(SrcReg)) {
448     BuildMI(MBB, I, DL, get(Hexagon::A2_tfrrcr), DestReg).addReg(SrcReg);
449     return;
450   }
451   if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
452       Hexagon::IntRegsRegClass.contains(DestReg)) {
453     BuildMI(MBB, I, DL, get(Hexagon::C2_tfrpr), DestReg).
454       addReg(SrcReg, getKillRegState(KillSrc));
455     return;
456   }
457   if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
458       Hexagon::PredRegsRegClass.contains(DestReg)) {
459     BuildMI(MBB, I, DL, get(Hexagon::C2_tfrrp), DestReg).
460       addReg(SrcReg, getKillRegState(KillSrc));
461     return;
462   }
463
464   llvm_unreachable("Unimplemented");
465 }
466
467
468 void HexagonInstrInfo::
469 storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
470                     unsigned SrcReg, bool isKill, int FI,
471                     const TargetRegisterClass *RC,
472                     const TargetRegisterInfo *TRI) const {
473
474   DebugLoc DL = MBB.findDebugLoc(I);
475   MachineFunction &MF = *MBB.getParent();
476   MachineFrameInfo &MFI = *MF.getFrameInfo();
477   unsigned Align = MFI.getObjectAlignment(FI);
478
479   MachineMemOperand *MMO =
480       MF.getMachineMemOperand(
481                       MachinePointerInfo(PseudoSourceValue::getFixedStack(FI)),
482                       MachineMemOperand::MOStore,
483                       MFI.getObjectSize(FI),
484                       Align);
485
486   if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
487     BuildMI(MBB, I, DL, get(Hexagon::S2_storeri_io))
488           .addFrameIndex(FI).addImm(0)
489           .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
490   } else if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC)) {
491     BuildMI(MBB, I, DL, get(Hexagon::S2_storerd_io))
492           .addFrameIndex(FI).addImm(0)
493           .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
494   } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
495     BuildMI(MBB, I, DL, get(Hexagon::STriw_pred))
496           .addFrameIndex(FI).addImm(0)
497           .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
498   } else {
499     llvm_unreachable("Unimplemented");
500   }
501 }
502
503
504 void HexagonInstrInfo::storeRegToAddr(
505                                  MachineFunction &MF, unsigned SrcReg,
506                                  bool isKill,
507                                  SmallVectorImpl<MachineOperand> &Addr,
508                                  const TargetRegisterClass *RC,
509                                  SmallVectorImpl<MachineInstr*> &NewMIs) const
510 {
511   llvm_unreachable("Unimplemented");
512 }
513
514
515 void HexagonInstrInfo::
516 loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
517                      unsigned DestReg, int FI,
518                      const TargetRegisterClass *RC,
519                      const TargetRegisterInfo *TRI) const {
520   DebugLoc DL = MBB.findDebugLoc(I);
521   MachineFunction &MF = *MBB.getParent();
522   MachineFrameInfo &MFI = *MF.getFrameInfo();
523   unsigned Align = MFI.getObjectAlignment(FI);
524
525   MachineMemOperand *MMO =
526       MF.getMachineMemOperand(
527                       MachinePointerInfo(PseudoSourceValue::getFixedStack(FI)),
528                       MachineMemOperand::MOLoad,
529                       MFI.getObjectSize(FI),
530                       Align);
531   if (RC == &Hexagon::IntRegsRegClass) {
532     BuildMI(MBB, I, DL, get(Hexagon::L2_loadri_io), DestReg)
533           .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
534   } else if (RC == &Hexagon::DoubleRegsRegClass) {
535     BuildMI(MBB, I, DL, get(Hexagon::L2_loadrd_io), DestReg)
536           .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
537   } else if (RC == &Hexagon::PredRegsRegClass) {
538     BuildMI(MBB, I, DL, get(Hexagon::LDriw_pred), DestReg)
539           .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
540   } else {
541     llvm_unreachable("Can't store this register to stack slot");
542   }
543 }
544
545
546 void HexagonInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
547                                         SmallVectorImpl<MachineOperand> &Addr,
548                                         const TargetRegisterClass *RC,
549                                  SmallVectorImpl<MachineInstr*> &NewMIs) const {
550   llvm_unreachable("Unimplemented");
551 }
552
553
554 MachineInstr *HexagonInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
555                                                     MachineInstr* MI,
556                                           const SmallVectorImpl<unsigned> &Ops,
557                                                     int FI) const {
558   // Hexagon_TODO: Implement.
559   return nullptr;
560 }
561
562 unsigned HexagonInstrInfo::createVR(MachineFunction* MF, MVT VT) const {
563
564   MachineRegisterInfo &RegInfo = MF->getRegInfo();
565   const TargetRegisterClass *TRC;
566   if (VT == MVT::i1) {
567     TRC = &Hexagon::PredRegsRegClass;
568   } else if (VT == MVT::i32 || VT == MVT::f32) {
569     TRC = &Hexagon::IntRegsRegClass;
570   } else if (VT == MVT::i64 || VT == MVT::f64) {
571     TRC = &Hexagon::DoubleRegsRegClass;
572   } else {
573     llvm_unreachable("Cannot handle this register class");
574   }
575
576   unsigned NewReg = RegInfo.createVirtualRegister(TRC);
577   return NewReg;
578 }
579
580 bool HexagonInstrInfo::isExtendable(const MachineInstr *MI) const {
581   // Constant extenders are allowed only for V4 and above.
582   if (!Subtarget.hasV4TOps())
583     return false;
584
585   const MCInstrDesc &MID = MI->getDesc();
586   const uint64_t F = MID.TSFlags;
587   if ((F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask)
588     return true;
589
590   // TODO: This is largely obsolete now. Will need to be removed
591   // in consecutive patches.
592   switch(MI->getOpcode()) {
593     // TFR_FI Remains a special case.
594     case Hexagon::TFR_FI:
595       return true;
596     default:
597       return false;
598   }
599   return  false;
600 }
601
602 // This returns true in two cases:
603 // - The OP code itself indicates that this is an extended instruction.
604 // - One of MOs has been marked with HMOTF_ConstExtended flag.
605 bool HexagonInstrInfo::isExtended(const MachineInstr *MI) const {
606   // First check if this is permanently extended op code.
607   const uint64_t F = MI->getDesc().TSFlags;
608   if ((F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask)
609     return true;
610   // Use MO operand flags to determine if one of MI's operands
611   // has HMOTF_ConstExtended flag set.
612   for (MachineInstr::const_mop_iterator I = MI->operands_begin(),
613        E = MI->operands_end(); I != E; ++I) {
614     if (I->getTargetFlags() && HexagonII::HMOTF_ConstExtended)
615       return true;
616   }
617   return  false;
618 }
619
620 bool HexagonInstrInfo::isBranch (const MachineInstr *MI) const {
621   return MI->getDesc().isBranch();
622 }
623
624 bool HexagonInstrInfo::isNewValueInst(const MachineInstr *MI) const {
625   if (isNewValueJump(MI))
626     return true;
627
628   if (isNewValueStore(MI))
629     return true;
630
631   return false;
632 }
633
634 bool HexagonInstrInfo::isSaveCalleeSavedRegsCall(const MachineInstr *MI) const {
635   return MI->getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4;
636 }
637
638 bool HexagonInstrInfo::isPredicable(MachineInstr *MI) const {
639   bool isPred = MI->getDesc().isPredicable();
640
641   if (!isPred)
642     return false;
643
644   const int Opc = MI->getOpcode();
645
646   switch(Opc) {
647   case Hexagon::A2_tfrsi:
648     return isInt<12>(MI->getOperand(1).getImm());
649
650   case Hexagon::S2_storerd_io:
651     return isShiftedUInt<6,3>(MI->getOperand(1).getImm());
652
653   case Hexagon::S2_storeri_io:
654   case Hexagon::S2_storerinew_io:
655     return isShiftedUInt<6,2>(MI->getOperand(1).getImm());
656
657   case Hexagon::S2_storerh_io:
658   case Hexagon::S2_storerhnew_io:
659     return isShiftedUInt<6,1>(MI->getOperand(1).getImm());
660
661   case Hexagon::S2_storerb_io:
662   case Hexagon::S2_storerbnew_io:
663     return isUInt<6>(MI->getOperand(1).getImm());
664
665   case Hexagon::L2_loadrd_io:
666     return isShiftedUInt<6,3>(MI->getOperand(2).getImm());
667
668   case Hexagon::L2_loadri_io:
669     return isShiftedUInt<6,2>(MI->getOperand(2).getImm());
670
671   case Hexagon::L2_loadrh_io:
672   case Hexagon::L2_loadruh_io:
673     return isShiftedUInt<6,1>(MI->getOperand(2).getImm());
674
675   case Hexagon::L2_loadrb_io:
676   case Hexagon::L2_loadrub_io:
677     return isUInt<6>(MI->getOperand(2).getImm());
678
679   case Hexagon::L2_loadrd_pi:
680     return isShiftedInt<4,3>(MI->getOperand(3).getImm());
681
682   case Hexagon::L2_loadri_pi:
683     return isShiftedInt<4,2>(MI->getOperand(3).getImm());
684
685   case Hexagon::L2_loadrh_pi:
686   case Hexagon::L2_loadruh_pi:
687     return isShiftedInt<4,1>(MI->getOperand(3).getImm());
688
689   case Hexagon::L2_loadrb_pi:
690   case Hexagon::L2_loadrub_pi:
691     return isInt<4>(MI->getOperand(3).getImm());
692
693   case Hexagon::S4_storeirb_io:
694   case Hexagon::S4_storeirh_io:
695   case Hexagon::S4_storeiri_io:
696     return (isUInt<6>(MI->getOperand(1).getImm()) &&
697             isInt<6>(MI->getOperand(2).getImm()));
698
699   case Hexagon::ADD_ri:
700     return isInt<8>(MI->getOperand(2).getImm());
701
702   case Hexagon::A2_aslh:
703   case Hexagon::A2_asrh:
704   case Hexagon::A2_sxtb:
705   case Hexagon::A2_sxth:
706   case Hexagon::A2_zxtb:
707   case Hexagon::A2_zxth:
708     return Subtarget.hasV4TOps();
709   }
710
711   return true;
712 }
713
714 // This function performs the following inversiones:
715 //
716 //  cPt    ---> cNotPt
717 //  cNotPt ---> cPt
718 //
719 unsigned HexagonInstrInfo::getInvertedPredicatedOpcode(const int Opc) const {
720   int InvPredOpcode;
721   InvPredOpcode = isPredicatedTrue(Opc) ? Hexagon::getFalsePredOpcode(Opc)
722                                         : Hexagon::getTruePredOpcode(Opc);
723   if (InvPredOpcode >= 0) // Valid instruction with the inverted predicate.
724     return InvPredOpcode;
725
726   switch(Opc) {
727     default: llvm_unreachable("Unexpected predicated instruction");
728     case Hexagon::C2_ccombinewt:
729       return Hexagon::C2_ccombinewf;
730     case Hexagon::C2_ccombinewf:
731       return Hexagon::C2_ccombinewt;
732
733       // Dealloc_return.
734     case Hexagon::L4_return_t:
735       return Hexagon::L4_return_f;
736     case Hexagon::L4_return_f:
737       return Hexagon::L4_return_t;
738   }
739 }
740
741 // New Value Store instructions.
742 bool HexagonInstrInfo::isNewValueStore(const MachineInstr *MI) const {
743   const uint64_t F = MI->getDesc().TSFlags;
744
745   return ((F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask);
746 }
747
748 bool HexagonInstrInfo::isNewValueStore(unsigned Opcode) const {
749   const uint64_t F = get(Opcode).TSFlags;
750
751   return ((F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask);
752 }
753
754 int HexagonInstrInfo::
755 getMatchingCondBranchOpcode(int Opc, bool invertPredicate) const {
756   enum Hexagon::PredSense inPredSense;
757   inPredSense = invertPredicate ? Hexagon::PredSense_false :
758                                   Hexagon::PredSense_true;
759   int CondOpcode = Hexagon::getPredOpcode(Opc, inPredSense);
760   if (CondOpcode >= 0) // Valid Conditional opcode/instruction
761     return CondOpcode;
762
763   // This switch case will be removed once all the instructions have been
764   // modified to use relation maps.
765   switch(Opc) {
766   case Hexagon::TFRI_f:
767     return !invertPredicate ? Hexagon::TFRI_cPt_f :
768                               Hexagon::TFRI_cNotPt_f;
769   case Hexagon::A2_combinew:
770     return !invertPredicate ? Hexagon::C2_ccombinewt :
771                               Hexagon::C2_ccombinewf;
772
773   // Word.
774   case Hexagon::STriw_f:
775     return !invertPredicate ? Hexagon::S2_pstorerit_io:
776                               Hexagon::S2_pstorerif_io;
777   case Hexagon::STriw_indexed_f:
778     return !invertPredicate ? Hexagon::S2_pstorerit_io:
779                               Hexagon::S2_pstorerif_io;
780
781   // DEALLOC_RETURN.
782   case Hexagon::L4_return:
783     return !invertPredicate ? Hexagon::L4_return_t:
784                               Hexagon::L4_return_f;
785   }
786   llvm_unreachable("Unexpected predicable instruction");
787 }
788
789
790 bool HexagonInstrInfo::
791 PredicateInstruction(MachineInstr *MI,
792                      const SmallVectorImpl<MachineOperand> &Cond) const {
793   int Opc = MI->getOpcode();
794   assert (isPredicable(MI) && "Expected predicable instruction");
795   bool invertJump = (!Cond.empty() && Cond[0].isImm() &&
796                      (Cond[0].getImm() == 0));
797
798   // This will change MI's opcode to its predicate version.
799   // However, its operand list is still the old one, i.e. the
800   // non-predicate one.
801   MI->setDesc(get(getMatchingCondBranchOpcode(Opc, invertJump)));
802
803   int oper = -1;
804   unsigned int GAIdx = 0;
805
806   // Indicates whether the current MI has a GlobalAddress operand
807   bool hasGAOpnd = false;
808   std::vector<MachineOperand> tmpOpnds;
809
810   // Indicates whether we need to shift operands to right.
811   bool needShift = true;
812
813   // The predicate is ALWAYS the FIRST input operand !!!
814   if (MI->getNumOperands() == 0) {
815     // The non-predicate version of MI does not take any operands,
816     // i.e. no outs and no ins. In this condition, the predicate
817     // operand will be directly placed at Operands[0]. No operand
818     // shift is needed.
819     // Example: BARRIER
820     needShift = false;
821     oper = -1;
822   }
823   else if (   MI->getOperand(MI->getNumOperands()-1).isReg()
824            && MI->getOperand(MI->getNumOperands()-1).isDef()
825            && !MI->getOperand(MI->getNumOperands()-1).isImplicit()) {
826     // The non-predicate version of MI does not have any input operands.
827     // In this condition, we extend the length of Operands[] by one and
828     // copy the original last operand to the newly allocated slot.
829     // At this moment, it is just a place holder. Later, we will put
830     // predicate operand directly into it. No operand shift is needed.
831     // Example: r0=BARRIER (this is a faked insn used here for illustration)
832     MI->addOperand(MI->getOperand(MI->getNumOperands()-1));
833     needShift = false;
834     oper = MI->getNumOperands() - 2;
835   }
836   else {
837     // We need to right shift all input operands by one. Duplicate the
838     // last operand into the newly allocated slot.
839     MI->addOperand(MI->getOperand(MI->getNumOperands()-1));
840   }
841
842   if (needShift)
843   {
844     // Operands[ MI->getNumOperands() - 2 ] has been copied into
845     // Operands[ MI->getNumOperands() - 1 ], so we start from
846     // Operands[ MI->getNumOperands() - 3 ].
847     // oper is a signed int.
848     // It is ok if "MI->getNumOperands()-3" is -3, -2, or -1.
849     for (oper = MI->getNumOperands() - 3; oper >= 0; --oper)
850     {
851       MachineOperand &MO = MI->getOperand(oper);
852
853       // Opnd[0] Opnd[1] Opnd[2] Opnd[3] Opnd[4]   Opnd[5]   Opnd[6]   Opnd[7]
854       // <Def0>  <Def1>  <Use0>  <Use1>  <ImpDef0> <ImpDef1> <ImpUse0> <ImpUse1>
855       //               /\~
856       //              /||\~
857       //               ||
858       //        Predicate Operand here
859       if (MO.isReg() && !MO.isUse() && !MO.isImplicit()) {
860         break;
861       }
862       if (MO.isReg()) {
863         MI->getOperand(oper+1).ChangeToRegister(MO.getReg(), MO.isDef(),
864                                                 MO.isImplicit(), MO.isKill(),
865                                                 MO.isDead(), MO.isUndef(),
866                                                 MO.isDebug());
867       }
868       else if (MO.isImm()) {
869         MI->getOperand(oper+1).ChangeToImmediate(MO.getImm());
870       }
871       else if (MO.isGlobal()) {
872         // MI can not have more than one GlobalAddress operand.
873         assert(hasGAOpnd == false && "MI can only have one GlobalAddress opnd");
874
875         // There is no member function called "ChangeToGlobalAddress" in the
876         // MachineOperand class (not like "ChangeToRegister" and
877         // "ChangeToImmediate"). So we have to remove them from Operands[] list
878         // first, and then add them back after we have inserted the predicate
879         // operand. tmpOpnds[] is to remember these operands before we remove
880         // them.
881         tmpOpnds.push_back(MO);
882
883         // Operands[oper] is a GlobalAddress operand;
884         // Operands[oper+1] has been copied into Operands[oper+2];
885         hasGAOpnd = true;
886         GAIdx = oper;
887         continue;
888       }
889       else {
890         llvm_unreachable("Unexpected operand type");
891       }
892     }
893   }
894
895   int regPos = invertJump ? 1 : 0;
896   MachineOperand PredMO = Cond[regPos];
897
898   // [oper] now points to the last explicit Def. Predicate operand must be
899   // located at [oper+1]. See diagram above.
900   // This assumes that the predicate is always the first operand,
901   // i.e. Operands[0+numResults], in the set of inputs
902   // It is better to have an assert here to check this. But I don't know how
903   // to write this assert because findFirstPredOperandIdx() would return -1
904   if (oper < -1) oper = -1;
905
906   MI->getOperand(oper+1).ChangeToRegister(PredMO.getReg(), PredMO.isDef(),
907                                           PredMO.isImplicit(), false,
908                                           PredMO.isDead(), PredMO.isUndef(),
909                                           PredMO.isDebug());
910
911   MachineRegisterInfo &RegInfo = MI->getParent()->getParent()->getRegInfo();
912   RegInfo.clearKillFlags(PredMO.getReg());
913
914   if (hasGAOpnd)
915   {
916     unsigned int i;
917
918     // Operands[GAIdx] is the original GlobalAddress operand, which is
919     // already copied into tmpOpnds[0].
920     // Operands[GAIdx] now stores a copy of Operands[GAIdx-1]
921     // Operands[GAIdx+1] has already been copied into Operands[GAIdx+2],
922     // so we start from [GAIdx+2]
923     for (i = GAIdx + 2; i < MI->getNumOperands(); ++i)
924       tmpOpnds.push_back(MI->getOperand(i));
925
926     // Remove all operands in range [ (GAIdx+1) ... (MI->getNumOperands()-1) ]
927     // It is very important that we always remove from the end of Operands[]
928     // MI->getNumOperands() is at least 2 if program goes to here.
929     for (i = MI->getNumOperands() - 1; i > GAIdx; --i)
930       MI->RemoveOperand(i);
931
932     for (i = 0; i < tmpOpnds.size(); ++i)
933       MI->addOperand(tmpOpnds[i]);
934   }
935
936   return true;
937 }
938
939
940 bool
941 HexagonInstrInfo::
942 isProfitableToIfCvt(MachineBasicBlock &MBB,
943                     unsigned NumCycles,
944                     unsigned ExtraPredCycles,
945                     const BranchProbability &Probability) const {
946   return true;
947 }
948
949
950 bool
951 HexagonInstrInfo::
952 isProfitableToIfCvt(MachineBasicBlock &TMBB,
953                     unsigned NumTCycles,
954                     unsigned ExtraTCycles,
955                     MachineBasicBlock &FMBB,
956                     unsigned NumFCycles,
957                     unsigned ExtraFCycles,
958                     const BranchProbability &Probability) const {
959   return true;
960 }
961
962 // Returns true if an instruction is predicated irrespective of the predicate
963 // sense. For example, all of the following will return true.
964 // if (p0) R1 = add(R2, R3)
965 // if (!p0) R1 = add(R2, R3)
966 // if (p0.new) R1 = add(R2, R3)
967 // if (!p0.new) R1 = add(R2, R3)
968 bool HexagonInstrInfo::isPredicated(const MachineInstr *MI) const {
969   const uint64_t F = MI->getDesc().TSFlags;
970
971   return ((F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
972 }
973
974 bool HexagonInstrInfo::isPredicated(unsigned Opcode) const {
975   const uint64_t F = get(Opcode).TSFlags;
976
977   return ((F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
978 }
979
980 bool HexagonInstrInfo::isPredicatedTrue(const MachineInstr *MI) const {
981   const uint64_t F = MI->getDesc().TSFlags;
982
983   assert(isPredicated(MI));
984   return (!((F >> HexagonII::PredicatedFalsePos) &
985             HexagonII::PredicatedFalseMask));
986 }
987
988 bool HexagonInstrInfo::isPredicatedTrue(unsigned Opcode) const {
989   const uint64_t F = get(Opcode).TSFlags;
990
991   // Make sure that the instruction is predicated.
992   assert((F>> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
993   return (!((F >> HexagonII::PredicatedFalsePos) &
994             HexagonII::PredicatedFalseMask));
995 }
996
997 bool HexagonInstrInfo::isPredicatedNew(const MachineInstr *MI) const {
998   const uint64_t F = MI->getDesc().TSFlags;
999
1000   assert(isPredicated(MI));
1001   return ((F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask);
1002 }
1003
1004 bool HexagonInstrInfo::isPredicatedNew(unsigned Opcode) const {
1005   const uint64_t F = get(Opcode).TSFlags;
1006
1007   assert(isPredicated(Opcode));
1008   return ((F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask);
1009 }
1010
1011 // Returns true, if a ST insn can be promoted to a new-value store.
1012 bool HexagonInstrInfo::mayBeNewStore(const MachineInstr *MI) const {
1013   const HexagonRegisterInfo& QRI = getRegisterInfo();
1014   const uint64_t F = MI->getDesc().TSFlags;
1015
1016   return ((F >> HexagonII::mayNVStorePos) &
1017            HexagonII::mayNVStoreMask &
1018            QRI.Subtarget.hasV4TOps());
1019 }
1020
1021 bool
1022 HexagonInstrInfo::DefinesPredicate(MachineInstr *MI,
1023                                    std::vector<MachineOperand> &Pred) const {
1024   for (unsigned oper = 0; oper < MI->getNumOperands(); ++oper) {
1025     MachineOperand MO = MI->getOperand(oper);
1026     if (MO.isReg() && MO.isDef()) {
1027       const TargetRegisterClass* RC = RI.getMinimalPhysRegClass(MO.getReg());
1028       if (RC == &Hexagon::PredRegsRegClass) {
1029         Pred.push_back(MO);
1030         return true;
1031       }
1032     }
1033   }
1034   return false;
1035 }
1036
1037
1038 bool
1039 HexagonInstrInfo::
1040 SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
1041                   const SmallVectorImpl<MachineOperand> &Pred2) const {
1042   // TODO: Fix this
1043   return false;
1044 }
1045
1046
1047 //
1048 // We indicate that we want to reverse the branch by
1049 // inserting a 0 at the beginning of the Cond vector.
1050 //
1051 bool HexagonInstrInfo::
1052 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
1053   if (!Cond.empty() && Cond[0].isImm() && Cond[0].getImm() == 0) {
1054     Cond.erase(Cond.begin());
1055   } else {
1056     Cond.insert(Cond.begin(), MachineOperand::CreateImm(0));
1057   }
1058   return false;
1059 }
1060
1061
1062 bool HexagonInstrInfo::
1063 isProfitableToDupForIfCvt(MachineBasicBlock &MBB,unsigned NumInstrs,
1064                           const BranchProbability &Probability) const {
1065   return (NumInstrs <= 4);
1066 }
1067
1068 bool HexagonInstrInfo::isDeallocRet(const MachineInstr *MI) const {
1069   switch (MI->getOpcode()) {
1070   default: return false;
1071   case Hexagon::L4_return:
1072   case Hexagon::L4_return_t:
1073   case Hexagon::L4_return_f:
1074   case Hexagon::L4_return_tnew_pnt:
1075   case Hexagon::L4_return_fnew_pnt:
1076   case Hexagon::L4_return_tnew_pt:
1077   case Hexagon::L4_return_fnew_pt:
1078    return true;
1079   }
1080 }
1081
1082
1083 bool HexagonInstrInfo::
1084 isValidOffset(const int Opcode, const int Offset) const {
1085   // This function is to check whether the "Offset" is in the correct range of
1086   // the given "Opcode". If "Offset" is not in the correct range, "ADD_ri" is
1087   // inserted to calculate the final address. Due to this reason, the function
1088   // assumes that the "Offset" has correct alignment.
1089   // We used to assert if the offset was not properly aligned, however,
1090   // there are cases where a misaligned pointer recast can cause this
1091   // problem, and we need to allow for it. The front end warns of such
1092   // misaligns with respect to load size.
1093
1094   switch(Opcode) {
1095
1096   case Hexagon::L2_loadri_io:
1097   case Hexagon::LDriw_f:
1098   case Hexagon::S2_storeri_io:
1099   case Hexagon::STriw_f:
1100     return (Offset >= Hexagon_MEMW_OFFSET_MIN) &&
1101       (Offset <= Hexagon_MEMW_OFFSET_MAX);
1102
1103   case Hexagon::L2_loadrd_io:
1104   case Hexagon::LDrid_f:
1105   case Hexagon::S2_storerd_io:
1106   case Hexagon::STrid_f:
1107     return (Offset >= Hexagon_MEMD_OFFSET_MIN) &&
1108       (Offset <= Hexagon_MEMD_OFFSET_MAX);
1109
1110   case Hexagon::L2_loadrh_io:
1111   case Hexagon::L2_loadruh_io:
1112   case Hexagon::S2_storerh_io:
1113     return (Offset >= Hexagon_MEMH_OFFSET_MIN) &&
1114       (Offset <= Hexagon_MEMH_OFFSET_MAX);
1115
1116   case Hexagon::L2_loadrb_io:
1117   case Hexagon::S2_storerb_io:
1118   case Hexagon::L2_loadrub_io:
1119     return (Offset >= Hexagon_MEMB_OFFSET_MIN) &&
1120       (Offset <= Hexagon_MEMB_OFFSET_MAX);
1121
1122   case Hexagon::ADD_ri:
1123   case Hexagon::TFR_FI:
1124     return (Offset >= Hexagon_ADDI_OFFSET_MIN) &&
1125       (Offset <= Hexagon_ADDI_OFFSET_MAX);
1126
1127   case Hexagon::L4_iadd_memopw_io:
1128   case Hexagon::L4_isub_memopw_io:
1129   case Hexagon::L4_add_memopw_io:
1130   case Hexagon::L4_sub_memopw_io:
1131   case Hexagon::L4_and_memopw_io:
1132   case Hexagon::L4_or_memopw_io:
1133     return (0 <= Offset && Offset <= 255);
1134
1135   case Hexagon::L4_iadd_memoph_io:
1136   case Hexagon::L4_isub_memoph_io:
1137   case Hexagon::L4_add_memoph_io:
1138   case Hexagon::L4_sub_memoph_io:
1139   case Hexagon::L4_and_memoph_io:
1140   case Hexagon::L4_or_memoph_io:
1141     return (0 <= Offset && Offset <= 127);
1142
1143   case Hexagon::L4_iadd_memopb_io:
1144   case Hexagon::L4_isub_memopb_io:
1145   case Hexagon::L4_add_memopb_io:
1146   case Hexagon::L4_sub_memopb_io:
1147   case Hexagon::L4_and_memopb_io:
1148   case Hexagon::L4_or_memopb_io:
1149     return (0 <= Offset && Offset <= 63);
1150
1151   // LDri_pred and STriw_pred are pseudo operations, so it has to take offset of
1152   // any size. Later pass knows how to handle it.
1153   case Hexagon::STriw_pred:
1154   case Hexagon::LDriw_pred:
1155     return true;
1156
1157   case Hexagon::J2_loop0i:
1158     return isUInt<10>(Offset);
1159
1160   // INLINEASM is very special.
1161   case Hexagon::INLINEASM:
1162     return true;
1163   }
1164
1165   llvm_unreachable("No offset range is defined for this opcode. "
1166                    "Please define it in the above switch statement!");
1167 }
1168
1169
1170 //
1171 // Check if the Offset is a valid auto-inc imm by Load/Store Type.
1172 //
1173 bool HexagonInstrInfo::
1174 isValidAutoIncImm(const EVT VT, const int Offset) const {
1175
1176   if (VT == MVT::i64) {
1177       return (Offset >= Hexagon_MEMD_AUTOINC_MIN &&
1178               Offset <= Hexagon_MEMD_AUTOINC_MAX &&
1179               (Offset & 0x7) == 0);
1180   }
1181   if (VT == MVT::i32) {
1182       return (Offset >= Hexagon_MEMW_AUTOINC_MIN &&
1183               Offset <= Hexagon_MEMW_AUTOINC_MAX &&
1184               (Offset & 0x3) == 0);
1185   }
1186   if (VT == MVT::i16) {
1187       return (Offset >= Hexagon_MEMH_AUTOINC_MIN &&
1188               Offset <= Hexagon_MEMH_AUTOINC_MAX &&
1189               (Offset & 0x1) == 0);
1190   }
1191   if (VT == MVT::i8) {
1192       return (Offset >= Hexagon_MEMB_AUTOINC_MIN &&
1193               Offset <= Hexagon_MEMB_AUTOINC_MAX);
1194   }
1195   llvm_unreachable("Not an auto-inc opc!");
1196 }
1197
1198
1199 bool HexagonInstrInfo::
1200 isMemOp(const MachineInstr *MI) const {
1201 //  return MI->getDesc().mayLoad() && MI->getDesc().mayStore();
1202
1203   switch (MI->getOpcode())
1204   {
1205   default: return false;
1206   case Hexagon::L4_iadd_memopw_io:
1207   case Hexagon::L4_isub_memopw_io:
1208   case Hexagon::L4_add_memopw_io:
1209   case Hexagon::L4_sub_memopw_io:
1210   case Hexagon::L4_and_memopw_io:
1211   case Hexagon::L4_or_memopw_io:
1212   case Hexagon::L4_iadd_memoph_io:
1213   case Hexagon::L4_isub_memoph_io:
1214   case Hexagon::L4_add_memoph_io:
1215   case Hexagon::L4_sub_memoph_io:
1216   case Hexagon::L4_and_memoph_io:
1217   case Hexagon::L4_or_memoph_io:
1218   case Hexagon::L4_iadd_memopb_io:
1219   case Hexagon::L4_isub_memopb_io:
1220   case Hexagon::L4_add_memopb_io:
1221   case Hexagon::L4_sub_memopb_io:
1222   case Hexagon::L4_and_memopb_io:
1223   case Hexagon::L4_or_memopb_io:
1224   case Hexagon::L4_ior_memopb_io:
1225   case Hexagon::L4_ior_memoph_io:
1226   case Hexagon::L4_ior_memopw_io:
1227   case Hexagon::L4_iand_memopb_io:
1228   case Hexagon::L4_iand_memoph_io:
1229   case Hexagon::L4_iand_memopw_io:
1230     return true;
1231   }
1232   return false;
1233 }
1234
1235
1236 bool HexagonInstrInfo::
1237 isSpillPredRegOp(const MachineInstr *MI) const {
1238   switch (MI->getOpcode()) {
1239     default: return false;
1240     case Hexagon::STriw_pred :
1241     case Hexagon::LDriw_pred :
1242       return true;
1243   }
1244 }
1245
1246 bool HexagonInstrInfo::isNewValueJumpCandidate(const MachineInstr *MI) const {
1247   switch (MI->getOpcode()) {
1248     default: return false;
1249     case Hexagon::C2_cmpeq:
1250     case Hexagon::C2_cmpeqi:
1251     case Hexagon::C2_cmpgt:
1252     case Hexagon::C2_cmpgti:
1253     case Hexagon::C2_cmpgtu:
1254     case Hexagon::C2_cmpgtui:
1255       return true;
1256   }
1257 }
1258
1259 bool HexagonInstrInfo::
1260 isConditionalTransfer (const MachineInstr *MI) const {
1261   switch (MI->getOpcode()) {
1262     default: return false;
1263     case Hexagon::A2_tfrt:
1264     case Hexagon::A2_tfrf:
1265     case Hexagon::C2_cmoveit:
1266     case Hexagon::C2_cmoveif:
1267     case Hexagon::A2_tfrtnew:
1268     case Hexagon::A2_tfrfnew:
1269     case Hexagon::C2_cmovenewit:
1270     case Hexagon::C2_cmovenewif:
1271       return true;
1272   }
1273 }
1274
1275 bool HexagonInstrInfo::isConditionalALU32 (const MachineInstr* MI) const {
1276   switch (MI->getOpcode())
1277   {
1278     default: return false;
1279     case Hexagon::A2_paddf:
1280     case Hexagon::A2_paddfnew:
1281     case Hexagon::A2_paddt:
1282     case Hexagon::A2_paddtnew:
1283     case Hexagon::A2_pandf:
1284     case Hexagon::A2_pandfnew:
1285     case Hexagon::A2_pandt:
1286     case Hexagon::A2_pandtnew:
1287     case Hexagon::A4_paslhf:
1288     case Hexagon::A4_paslhfnew:
1289     case Hexagon::A4_paslht:
1290     case Hexagon::A4_paslhtnew:
1291     case Hexagon::A4_pasrhf:
1292     case Hexagon::A4_pasrhfnew:
1293     case Hexagon::A4_pasrht:
1294     case Hexagon::A4_pasrhtnew:
1295     case Hexagon::A2_porf:
1296     case Hexagon::A2_porfnew:
1297     case Hexagon::A2_port:
1298     case Hexagon::A2_portnew:
1299     case Hexagon::A2_psubf:
1300     case Hexagon::A2_psubfnew:
1301     case Hexagon::A2_psubt:
1302     case Hexagon::A2_psubtnew:
1303     case Hexagon::A2_pxorf:
1304     case Hexagon::A2_pxorfnew:
1305     case Hexagon::A2_pxort:
1306     case Hexagon::A2_pxortnew:
1307     case Hexagon::A4_psxthf:
1308     case Hexagon::A4_psxthfnew:
1309     case Hexagon::A4_psxtht:
1310     case Hexagon::A4_psxthtnew:
1311     case Hexagon::A4_psxtbf:
1312     case Hexagon::A4_psxtbfnew:
1313     case Hexagon::A4_psxtbt:
1314     case Hexagon::A4_psxtbtnew:
1315     case Hexagon::A4_pzxtbf:
1316     case Hexagon::A4_pzxtbfnew:
1317     case Hexagon::A4_pzxtbt:
1318     case Hexagon::A4_pzxtbtnew:
1319     case Hexagon::A4_pzxthf:
1320     case Hexagon::A4_pzxthfnew:
1321     case Hexagon::A4_pzxtht:
1322     case Hexagon::A4_pzxthtnew:
1323     case Hexagon::ADD_ri_cPt:
1324     case Hexagon::ADD_ri_cNotPt:
1325     case Hexagon::C2_ccombinewt:
1326     case Hexagon::C2_ccombinewf:
1327       return true;
1328   }
1329 }
1330
1331 bool HexagonInstrInfo::
1332 isConditionalLoad (const MachineInstr* MI) const {
1333   const HexagonRegisterInfo& QRI = getRegisterInfo();
1334   switch (MI->getOpcode())
1335   {
1336     default: return false;
1337     case Hexagon::L2_ploadrdt_io :
1338     case Hexagon::L2_ploadrdf_io:
1339     case Hexagon::L2_ploadrit_io:
1340     case Hexagon::L2_ploadrif_io:
1341     case Hexagon::L2_ploadrht_io:
1342     case Hexagon::L2_ploadrhf_io:
1343     case Hexagon::L2_ploadrbt_io:
1344     case Hexagon::L2_ploadrbf_io:
1345     case Hexagon::L2_ploadruht_io:
1346     case Hexagon::L2_ploadruhf_io:
1347     case Hexagon::L2_ploadrubt_io:
1348     case Hexagon::L2_ploadrubf_io:
1349       return true;
1350     case Hexagon::L2_ploadrdt_pi:
1351     case Hexagon::L2_ploadrdf_pi:
1352     case Hexagon::L2_ploadrit_pi:
1353     case Hexagon::L2_ploadrif_pi:
1354     case Hexagon::L2_ploadrht_pi:
1355     case Hexagon::L2_ploadrhf_pi:
1356     case Hexagon::L2_ploadrbt_pi:
1357     case Hexagon::L2_ploadrbf_pi:
1358     case Hexagon::L2_ploadruht_pi:
1359     case Hexagon::L2_ploadruhf_pi:
1360     case Hexagon::L2_ploadrubt_pi:
1361     case Hexagon::L2_ploadrubf_pi:
1362       return QRI.Subtarget.hasV4TOps();
1363     case Hexagon::L4_ploadrdt_rr:
1364     case Hexagon::L4_ploadrdf_rr:
1365     case Hexagon::L4_ploadrbt_rr:
1366     case Hexagon::L4_ploadrbf_rr:
1367     case Hexagon::L4_ploadrubt_rr:
1368     case Hexagon::L4_ploadrubf_rr:
1369     case Hexagon::L4_ploadrht_rr:
1370     case Hexagon::L4_ploadrhf_rr:
1371     case Hexagon::L4_ploadruht_rr:
1372     case Hexagon::L4_ploadruhf_rr:
1373     case Hexagon::L4_ploadrit_rr:
1374     case Hexagon::L4_ploadrif_rr:
1375       return QRI.Subtarget.hasV4TOps();
1376   }
1377 }
1378
1379 // Returns true if an instruction is a conditional store.
1380 //
1381 // Note: It doesn't include conditional new-value stores as they can't be
1382 // converted to .new predicate.
1383 //
1384 //               p.new NV store [ if(p0.new)memw(R0+#0)=R2.new ]
1385 //                ^           ^
1386 //               /             \ (not OK. it will cause new-value store to be
1387 //              /               X conditional on p0.new while R2 producer is
1388 //             /                 \ on p0)
1389 //            /                   \.
1390 //     p.new store                 p.old NV store
1391 // [if(p0.new)memw(R0+#0)=R2]    [if(p0)memw(R0+#0)=R2.new]
1392 //            ^                  ^
1393 //             \                /
1394 //              \              /
1395 //               \            /
1396 //                 p.old store
1397 //             [if (p0)memw(R0+#0)=R2]
1398 //
1399 // The above diagram shows the steps involoved in the conversion of a predicated
1400 // store instruction to its .new predicated new-value form.
1401 //
1402 // The following set of instructions further explains the scenario where
1403 // conditional new-value store becomes invalid when promoted to .new predicate
1404 // form.
1405 //
1406 // { 1) if (p0) r0 = add(r1, r2)
1407 //   2) p0 = cmp.eq(r3, #0) }
1408 //
1409 //   3) if (p0) memb(r1+#0) = r0  --> this instruction can't be grouped with
1410 // the first two instructions because in instr 1, r0 is conditional on old value
1411 // of p0 but its use in instr 3 is conditional on p0 modified by instr 2 which
1412 // is not valid for new-value stores.
1413 bool HexagonInstrInfo::
1414 isConditionalStore (const MachineInstr* MI) const {
1415   const HexagonRegisterInfo& QRI = getRegisterInfo();
1416   switch (MI->getOpcode())
1417   {
1418     default: return false;
1419     case Hexagon::S4_storeirbt_io:
1420     case Hexagon::S4_storeirbf_io:
1421     case Hexagon::S4_pstorerbt_rr:
1422     case Hexagon::S4_pstorerbf_rr:
1423     case Hexagon::S2_pstorerbt_io:
1424     case Hexagon::S2_pstorerbf_io:
1425     case Hexagon::S2_pstorerbt_pi:
1426     case Hexagon::S2_pstorerbf_pi:
1427     case Hexagon::S2_pstorerdt_io:
1428     case Hexagon::S2_pstorerdf_io:
1429     case Hexagon::S4_pstorerdt_rr:
1430     case Hexagon::S4_pstorerdf_rr:
1431     case Hexagon::S2_pstorerdt_pi:
1432     case Hexagon::S2_pstorerdf_pi:
1433     case Hexagon::S2_pstorerht_io:
1434     case Hexagon::S2_pstorerhf_io:
1435     case Hexagon::S4_storeirht_io:
1436     case Hexagon::S4_storeirhf_io:
1437     case Hexagon::S4_pstorerht_rr:
1438     case Hexagon::S4_pstorerhf_rr:
1439     case Hexagon::S2_pstorerht_pi:
1440     case Hexagon::S2_pstorerhf_pi:
1441     case Hexagon::S2_pstorerit_io:
1442     case Hexagon::S2_pstorerif_io:
1443     case Hexagon::S4_storeirit_io:
1444     case Hexagon::S4_storeirif_io:
1445     case Hexagon::S4_pstorerit_rr:
1446     case Hexagon::S4_pstorerif_rr:
1447     case Hexagon::S2_pstorerit_pi:
1448     case Hexagon::S2_pstorerif_pi:
1449       return QRI.Subtarget.hasV4TOps();
1450
1451     // V4 global address store before promoting to dot new.
1452     case Hexagon::S4_pstorerdt_abs:
1453     case Hexagon::S4_pstorerdf_abs:
1454     case Hexagon::S4_pstorerbt_abs:
1455     case Hexagon::S4_pstorerbf_abs:
1456     case Hexagon::S4_pstorerht_abs:
1457     case Hexagon::S4_pstorerhf_abs:
1458     case Hexagon::S4_pstorerit_abs:
1459     case Hexagon::S4_pstorerif_abs:
1460       return QRI.Subtarget.hasV4TOps();
1461
1462     // Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
1463     // from the "Conditional Store" list. Because a predicated new value store
1464     // would NOT be promoted to a double dot new store. See diagram below:
1465     // This function returns yes for those stores that are predicated but not
1466     // yet promoted to predicate dot new instructions.
1467     //
1468     //                          +---------------------+
1469     //                    /-----| if (p0) memw(..)=r0 |---------\~
1470     //                   ||     +---------------------+         ||
1471     //          promote  ||       /\       /\                   ||  promote
1472     //                   ||      /||\     /||\                  ||
1473     //                  \||/    demote     ||                  \||/
1474     //                   \/       ||       ||                   \/
1475     //       +-------------------------+   ||   +-------------------------+
1476     //       | if (p0.new) memw(..)=r0 |   ||   | if (p0) memw(..)=r0.new |
1477     //       +-------------------------+   ||   +-------------------------+
1478     //                        ||           ||         ||
1479     //                        ||         demote      \||/
1480     //                      promote        ||         \/ NOT possible
1481     //                        ||           ||         /\~
1482     //                       \||/          ||        /||\~
1483     //                        \/           ||         ||
1484     //                      +-----------------------------+
1485     //                      | if (p0.new) memw(..)=r0.new |
1486     //                      +-----------------------------+
1487     //                           Double Dot New Store
1488     //
1489   }
1490 }
1491
1492
1493 bool HexagonInstrInfo::isNewValueJump(const MachineInstr *MI) const {
1494   if (isNewValue(MI) && isBranch(MI))
1495     return true;
1496   return false;
1497 }
1498
1499 bool HexagonInstrInfo::isPostIncrement (const MachineInstr* MI) const {
1500   return (getAddrMode(MI) == HexagonII::PostInc);
1501 }
1502
1503 bool HexagonInstrInfo::isNewValue(const MachineInstr* MI) const {
1504   const uint64_t F = MI->getDesc().TSFlags;
1505   return ((F >> HexagonII::NewValuePos) & HexagonII::NewValueMask);
1506 }
1507
1508 // Returns true, if any one of the operands is a dot new
1509 // insn, whether it is predicated dot new or register dot new.
1510 bool HexagonInstrInfo::isDotNewInst (const MachineInstr* MI) const {
1511   return (isNewValueInst(MI) ||
1512      (isPredicated(MI) && isPredicatedNew(MI)));
1513 }
1514
1515 // Returns the most basic instruction for the .new predicated instructions and
1516 // new-value stores.
1517 // For example, all of the following instructions will be converted back to the
1518 // same instruction:
1519 // 1) if (p0.new) memw(R0+#0) = R1.new  --->
1520 // 2) if (p0) memw(R0+#0)= R1.new      -------> if (p0) memw(R0+#0) = R1
1521 // 3) if (p0.new) memw(R0+#0) = R1      --->
1522 //
1523
1524 int HexagonInstrInfo::GetDotOldOp(const int opc) const {
1525   int NewOp = opc;
1526   if (isPredicated(NewOp) && isPredicatedNew(NewOp)) { // Get predicate old form
1527     NewOp = Hexagon::getPredOldOpcode(NewOp);
1528     assert(NewOp >= 0 &&
1529            "Couldn't change predicate new instruction to its old form.");
1530   }
1531
1532   if (isNewValueStore(NewOp)) { // Convert into non-new-value format
1533     NewOp = Hexagon::getNonNVStore(NewOp);
1534     assert(NewOp >= 0 && "Couldn't change new-value store to its old form.");
1535   }
1536   return NewOp;
1537 }
1538
1539 // Return the new value instruction for a given store.
1540 int HexagonInstrInfo::GetDotNewOp(const MachineInstr* MI) const {
1541   int NVOpcode = Hexagon::getNewValueOpcode(MI->getOpcode());
1542   if (NVOpcode >= 0) // Valid new-value store instruction.
1543     return NVOpcode;
1544
1545   switch (MI->getOpcode()) {
1546   default: llvm_unreachable("Unknown .new type");
1547   // store new value byte
1548   case Hexagon::STrib_shl_V4:
1549     return Hexagon::STrib_shl_nv_V4;
1550
1551   case Hexagon::STrih_shl_V4:
1552     return Hexagon::STrih_shl_nv_V4;
1553
1554   case Hexagon::STriw_f:
1555     return Hexagon::S2_storerinew_io;
1556
1557   case Hexagon::STriw_indexed_f:
1558     return Hexagon::S4_storerinew_rr;
1559
1560   case Hexagon::STriw_shl_V4:
1561     return Hexagon::STriw_shl_nv_V4;
1562
1563   }
1564   return 0;
1565 }
1566
1567 // Return .new predicate version for an instruction.
1568 int HexagonInstrInfo::GetDotNewPredOp(MachineInstr *MI,
1569                                       const MachineBranchProbabilityInfo
1570                                       *MBPI) const {
1571
1572   int NewOpcode = Hexagon::getPredNewOpcode(MI->getOpcode());
1573   if (NewOpcode >= 0) // Valid predicate new instruction
1574     return NewOpcode;
1575
1576   switch (MI->getOpcode()) {
1577   default: llvm_unreachable("Unknown .new type");
1578   // Condtional Jumps
1579   case Hexagon::J2_jumpt:
1580   case Hexagon::J2_jumpf:
1581     return getDotNewPredJumpOp(MI, MBPI);
1582
1583   case Hexagon::J2_jumprt:
1584     return Hexagon::J2_jumptnewpt;
1585
1586   case Hexagon::J2_jumprf:
1587     return Hexagon::J2_jumprfnewpt;
1588
1589   case Hexagon::JMPrett:
1590     return Hexagon::J2_jumprtnewpt;
1591
1592   case Hexagon::JMPretf:
1593     return Hexagon::J2_jumprfnewpt;
1594
1595
1596   // Conditional combine
1597   case Hexagon::C2_ccombinewt:
1598     return Hexagon::C2_ccombinewnewt;
1599   case Hexagon::C2_ccombinewf:
1600     return Hexagon::C2_ccombinewnewf;
1601   }
1602 }
1603
1604
1605 unsigned HexagonInstrInfo::getAddrMode(const MachineInstr* MI) const {
1606   const uint64_t F = MI->getDesc().TSFlags;
1607
1608   return((F >> HexagonII::AddrModePos) & HexagonII::AddrModeMask);
1609 }
1610
1611 /// immediateExtend - Changes the instruction in place to one using an immediate
1612 /// extender.
1613 void HexagonInstrInfo::immediateExtend(MachineInstr *MI) const {
1614   assert((isExtendable(MI)||isConstExtended(MI)) &&
1615                                "Instruction must be extendable");
1616   // Find which operand is extendable.
1617   short ExtOpNum = getCExtOpNum(MI);
1618   MachineOperand &MO = MI->getOperand(ExtOpNum);
1619   // This needs to be something we understand.
1620   assert((MO.isMBB() || MO.isImm()) &&
1621          "Branch with unknown extendable field type");
1622   // Mark given operand as extended.
1623   MO.addTargetFlag(HexagonII::HMOTF_ConstExtended);
1624 }
1625
1626 DFAPacketizer *HexagonInstrInfo::CreateTargetScheduleState(
1627     const TargetSubtargetInfo &STI) const {
1628   const InstrItineraryData *II = STI.getInstrItineraryData();
1629   return static_cast<const HexagonSubtarget &>(STI).createDFAPacketizer(II);
1630 }
1631
1632 bool HexagonInstrInfo::isSchedulingBoundary(const MachineInstr *MI,
1633                                             const MachineBasicBlock *MBB,
1634                                             const MachineFunction &MF) const {
1635   // Debug info is never a scheduling boundary. It's necessary to be explicit
1636   // due to the special treatment of IT instructions below, otherwise a
1637   // dbg_value followed by an IT will result in the IT instruction being
1638   // considered a scheduling hazard, which is wrong. It should be the actual
1639   // instruction preceding the dbg_value instruction(s), just like it is
1640   // when debug info is not present.
1641   if (MI->isDebugValue())
1642     return false;
1643
1644   // Terminators and labels can't be scheduled around.
1645   if (MI->getDesc().isTerminator() || MI->isPosition() || MI->isInlineAsm())
1646     return true;
1647
1648   return false;
1649 }
1650
1651 bool HexagonInstrInfo::isConstExtended(MachineInstr *MI) const {
1652
1653   // Constant extenders are allowed only for V4 and above.
1654   if (!Subtarget.hasV4TOps())
1655     return false;
1656
1657   const uint64_t F = MI->getDesc().TSFlags;
1658   unsigned isExtended = (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
1659   if (isExtended) // Instruction must be extended.
1660     return true;
1661
1662   unsigned isExtendable = (F >> HexagonII::ExtendablePos)
1663                           & HexagonII::ExtendableMask;
1664   if (!isExtendable)
1665     return false;
1666
1667   short ExtOpNum = getCExtOpNum(MI);
1668   const MachineOperand &MO = MI->getOperand(ExtOpNum);
1669   // Use MO operand flags to determine if MO
1670   // has the HMOTF_ConstExtended flag set.
1671   if (MO.getTargetFlags() && HexagonII::HMOTF_ConstExtended)
1672     return true;
1673   // If this is a Machine BB address we are talking about, and it is
1674   // not marked as extended, say so.
1675   if (MO.isMBB())
1676     return false;
1677
1678   // We could be using an instruction with an extendable immediate and shoehorn
1679   // a global address into it. If it is a global address it will be constant
1680   // extended. We do this for COMBINE.
1681   // We currently only handle isGlobal() because it is the only kind of
1682   // object we are going to end up with here for now.
1683   // In the future we probably should add isSymbol(), etc.
1684   if (MO.isGlobal() || MO.isSymbol())
1685     return true;
1686
1687   // If the extendable operand is not 'Immediate' type, the instruction should
1688   // have 'isExtended' flag set.
1689   assert(MO.isImm() && "Extendable operand must be Immediate type");
1690
1691   int MinValue = getMinValue(MI);
1692   int MaxValue = getMaxValue(MI);
1693   int ImmValue = MO.getImm();
1694
1695   return (ImmValue < MinValue || ImmValue > MaxValue);
1696 }
1697
1698 // Returns the opcode to use when converting MI, which is a conditional jump,
1699 // into a conditional instruction which uses the .new value of the predicate.
1700 // We also use branch probabilities to add a hint to the jump.
1701 int
1702 HexagonInstrInfo::getDotNewPredJumpOp(MachineInstr *MI,
1703                                   const
1704                                   MachineBranchProbabilityInfo *MBPI) const {
1705
1706   // We assume that block can have at most two successors.
1707   bool taken = false;
1708   MachineBasicBlock *Src = MI->getParent();
1709   MachineOperand *BrTarget = &MI->getOperand(1);
1710   MachineBasicBlock *Dst = BrTarget->getMBB();
1711
1712   const BranchProbability Prediction = MBPI->getEdgeProbability(Src, Dst);
1713   if (Prediction >= BranchProbability(1,2))
1714     taken = true;
1715
1716   switch (MI->getOpcode()) {
1717   case Hexagon::J2_jumpt:
1718     return taken ? Hexagon::J2_jumptnewpt : Hexagon::J2_jumptnew;
1719   case Hexagon::J2_jumpf:
1720     return taken ? Hexagon::J2_jumpfnewpt : Hexagon::J2_jumpfnew;
1721
1722   default:
1723     llvm_unreachable("Unexpected jump instruction.");
1724   }
1725 }
1726 // Returns true if a particular operand is extendable for an instruction.
1727 bool HexagonInstrInfo::isOperandExtended(const MachineInstr *MI,
1728                                          unsigned short OperandNum) const {
1729   // Constant extenders are allowed only for V4 and above.
1730   if (!Subtarget.hasV4TOps())
1731     return false;
1732
1733   const uint64_t F = MI->getDesc().TSFlags;
1734
1735   return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask)
1736           == OperandNum;
1737 }
1738
1739 // Returns Operand Index for the constant extended instruction.
1740 unsigned short HexagonInstrInfo::getCExtOpNum(const MachineInstr *MI) const {
1741   const uint64_t F = MI->getDesc().TSFlags;
1742   return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask);
1743 }
1744
1745 // Returns the min value that doesn't need to be extended.
1746 int HexagonInstrInfo::getMinValue(const MachineInstr *MI) const {
1747   const uint64_t F = MI->getDesc().TSFlags;
1748   unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
1749                     & HexagonII::ExtentSignedMask;
1750   unsigned bits =  (F >> HexagonII::ExtentBitsPos)
1751                     & HexagonII::ExtentBitsMask;
1752
1753   if (isSigned) // if value is signed
1754     return -1U << (bits - 1);
1755   else
1756     return 0;
1757 }
1758
1759 // Returns the max value that doesn't need to be extended.
1760 int HexagonInstrInfo::getMaxValue(const MachineInstr *MI) const {
1761   const uint64_t F = MI->getDesc().TSFlags;
1762   unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
1763                     & HexagonII::ExtentSignedMask;
1764   unsigned bits =  (F >> HexagonII::ExtentBitsPos)
1765                     & HexagonII::ExtentBitsMask;
1766
1767   if (isSigned) // if value is signed
1768     return ~(-1U << (bits - 1));
1769   else
1770     return ~(-1U << bits);
1771 }
1772
1773 // Returns true if an instruction can be converted into a non-extended
1774 // equivalent instruction.
1775 bool HexagonInstrInfo::NonExtEquivalentExists (const MachineInstr *MI) const {
1776
1777   short NonExtOpcode;
1778   // Check if the instruction has a register form that uses register in place
1779   // of the extended operand, if so return that as the non-extended form.
1780   if (Hexagon::getRegForm(MI->getOpcode()) >= 0)
1781     return true;
1782
1783   if (MI->getDesc().mayLoad() || MI->getDesc().mayStore()) {
1784     // Check addressing mode and retrieve non-ext equivalent instruction.
1785
1786     switch (getAddrMode(MI)) {
1787     case HexagonII::Absolute :
1788       // Load/store with absolute addressing mode can be converted into
1789       // base+offset mode.
1790       NonExtOpcode = Hexagon::getBasedWithImmOffset(MI->getOpcode());
1791       break;
1792     case HexagonII::BaseImmOffset :
1793       // Load/store with base+offset addressing mode can be converted into
1794       // base+register offset addressing mode. However left shift operand should
1795       // be set to 0.
1796       NonExtOpcode = Hexagon::getBaseWithRegOffset(MI->getOpcode());
1797       break;
1798     default:
1799       return false;
1800     }
1801     if (NonExtOpcode < 0)
1802       return false;
1803     return true;
1804   }
1805   return false;
1806 }
1807
1808 // Returns opcode of the non-extended equivalent instruction.
1809 short HexagonInstrInfo::getNonExtOpcode (const MachineInstr *MI) const {
1810
1811   // Check if the instruction has a register form that uses register in place
1812   // of the extended operand, if so return that as the non-extended form.
1813   short NonExtOpcode = Hexagon::getRegForm(MI->getOpcode());
1814     if (NonExtOpcode >= 0)
1815       return NonExtOpcode;
1816
1817   if (MI->getDesc().mayLoad() || MI->getDesc().mayStore()) {
1818     // Check addressing mode and retrieve non-ext equivalent instruction.
1819     switch (getAddrMode(MI)) {
1820     case HexagonII::Absolute :
1821       return Hexagon::getBasedWithImmOffset(MI->getOpcode());
1822     case HexagonII::BaseImmOffset :
1823       return Hexagon::getBaseWithRegOffset(MI->getOpcode());
1824     default:
1825       return -1;
1826     }
1827   }
1828   return -1;
1829 }
1830
1831 bool HexagonInstrInfo::PredOpcodeHasJMP_c(Opcode_t Opcode) const {
1832   return (Opcode == Hexagon::J2_jumpt) ||
1833          (Opcode == Hexagon::J2_jumpf) ||
1834          (Opcode == Hexagon::J2_jumptnewpt) ||
1835          (Opcode == Hexagon::J2_jumpfnewpt) ||
1836          (Opcode == Hexagon::J2_jumpt) ||
1837          (Opcode == Hexagon::J2_jumpf);
1838 }
1839
1840 bool HexagonInstrInfo::PredOpcodeHasNot(Opcode_t Opcode) const {
1841   return (Opcode == Hexagon::J2_jumpf) ||
1842          (Opcode == Hexagon::J2_jumpfnewpt) ||
1843          (Opcode == Hexagon::J2_jumpfnew);
1844 }