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