131d157ae1ac2cd9c787dc3c1d28e64c682803844Jia Liu//===-- HexagonInstrInfo.cpp - Hexagon Instruction Information ------------===//
2b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum//
3b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum//                     The LLVM Compiler Infrastructure
4b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum//
5b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum// This file is distributed under the University of Illinois Open Source
6b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum// License. See LICENSE.TXT for details.
7b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum//
8b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum//===----------------------------------------------------------------------===//
9b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum//
10b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum// This file contains the Hexagon implementation of the TargetInstrInfo class.
11b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum//
12b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum//===----------------------------------------------------------------------===//
13b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
14b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum#include "HexagonInstrInfo.h"
15d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "Hexagon.h"
1679aa3417eb6f58d668aadfedf075240a41d35a26Craig Topper#include "HexagonRegisterInfo.h"
17b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum#include "HexagonSubtarget.h"
18b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum#include "llvm/ADT/STLExtras.h"
19b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum#include "llvm/ADT/SmallVector.h"
20f3fd7ee415ec8a6475a060e29959d04d6158f45fBenjamin Kramer#include "llvm/CodeGen/DFAPacketizer.h"
21b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum#include "llvm/CodeGen/MachineFrameInfo.h"
22d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/CodeGen/MachineInstrBuilder.h"
23b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum#include "llvm/CodeGen/MachineMemOperand.h"
24d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/CodeGen/MachineRegisterInfo.h"
25b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum#include "llvm/CodeGen/PseudoSourceValue.h"
266ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma#include "llvm/Support/Debug.h"
27f3fd7ee415ec8a6475a060e29959d04d6158f45fBenjamin Kramer#include "llvm/Support/MathExtras.h"
28aa35a569751eb79b2a2db31139932e30c6889400Reid Kleckner#include "llvm/Support/raw_ostream.h"
29dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
30dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hinesusing namespace llvm;
31dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
32dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines#define DEBUG_TYPE "hexagon-instrinfo"
33dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
34354362524a72b3fa43a6c09380b7ae3b2380cbbaJuergen Ributzka#define GET_INSTRINFO_CTOR_DTOR
358aa138c12282e8bccac665f2197e817e32c2adcfPranav Bhandarkar#define GET_INSTRMAP_INFO
36b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum#include "HexagonGenInstrInfo.inc"
37ee498d3254b86bceb4f441741e9f442990647ce6Andrew Trick#include "HexagonGenDFAPacketizer.inc"
38b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
39b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum///
40b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum/// Constants for Hexagon instructions.
41b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum///
42b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumconst int Hexagon_MEMW_OFFSET_MAX = 4095;
4326f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pandeconst int Hexagon_MEMW_OFFSET_MIN = -4096;
44b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumconst int Hexagon_MEMD_OFFSET_MAX = 8191;
4526f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pandeconst int Hexagon_MEMD_OFFSET_MIN = -8192;
46b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumconst int Hexagon_MEMH_OFFSET_MAX = 2047;
4726f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pandeconst int Hexagon_MEMH_OFFSET_MIN = -2048;
48b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumconst int Hexagon_MEMB_OFFSET_MAX = 1023;
4926f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pandeconst int Hexagon_MEMB_OFFSET_MIN = -1024;
50b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumconst int Hexagon_ADDI_OFFSET_MAX = 32767;
5126f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pandeconst int Hexagon_ADDI_OFFSET_MIN = -32768;
52b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumconst int Hexagon_MEMD_AUTOINC_MAX = 56;
5326f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pandeconst int Hexagon_MEMD_AUTOINC_MIN = -64;
54b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumconst int Hexagon_MEMW_AUTOINC_MAX = 28;
5526f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pandeconst int Hexagon_MEMW_AUTOINC_MIN = -32;
56b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumconst int Hexagon_MEMH_AUTOINC_MAX = 14;
5726f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pandeconst int Hexagon_MEMH_AUTOINC_MIN = -16;
58b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumconst int Hexagon_MEMB_AUTOINC_MAX = 7;
5926f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pandeconst int Hexagon_MEMB_AUTOINC_MIN = -8;
60b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
61354362524a72b3fa43a6c09380b7ae3b2380cbbaJuergen Ributzka// Pin the vtable to this file.
62354362524a72b3fa43a6c09380b7ae3b2380cbbaJuergen Ributzkavoid HexagonInstrInfo::anchor() {}
63b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
64b4b54153ad760c69a00a08531abef4ed434a5092Tony LinthicumHexagonInstrInfo::HexagonInstrInfo(HexagonSubtarget &ST)
65b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  : HexagonGenInstrInfo(Hexagon::ADJCALLSTACKDOWN, Hexagon::ADJCALLSTACKUP),
6654a56fad36a32f12709da5f96998336f08524be9Bill Wendling    RI(ST), Subtarget(ST) {
67b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum}
68b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
69b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
70b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum/// isLoadFromStackSlot - If the specified machine instruction is a direct
71b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum/// load from a stack slot, return the virtual or physical register number of
72b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum/// the destination along with the FrameIndex of the loaded stack slot.  If
73b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum/// not, return 0.  This predicate must return 0 if the instruction has
74b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum/// any side effects other than loading from the stack slot.
75b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumunsigned HexagonInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
76b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                                             int &FrameIndex) const {
77b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
78b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
79b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  switch (MI->getOpcode()) {
8026f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande  default: break;
81b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::LDriw:
82b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::LDrid:
83b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::LDrih:
84b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::LDrib:
85b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::LDriub:
86b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    if (MI->getOperand(2).isFI() &&
87b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum        MI->getOperand(1).isImm() && (MI->getOperand(1).getImm() == 0)) {
88b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum      FrameIndex = MI->getOperand(2).getIndex();
89b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum      return MI->getOperand(0).getReg();
90b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    }
91b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    break;
92b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  }
93b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  return 0;
94b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum}
95b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
96b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
97b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum/// isStoreToStackSlot - If the specified machine instruction is a direct
98b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum/// store to a stack slot, return the virtual or physical register number of
99b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum/// the source reg along with the FrameIndex of the loaded stack slot.  If
100b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum/// not, return 0.  This predicate must return 0 if the instruction has
101b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum/// any side effects other than storing to the stack slot.
102b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumunsigned HexagonInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
103b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                                            int &FrameIndex) const {
104b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  switch (MI->getOpcode()) {
10526f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande  default: break;
106b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::STriw:
107b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::STrid:
108b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::STrih:
109b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::STrib:
110b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    if (MI->getOperand(2).isFI() &&
111b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum        MI->getOperand(1).isImm() && (MI->getOperand(1).getImm() == 0)) {
1122b38c12643236825a6a49c31ef49da8e62b094c2Sirish Pande      FrameIndex = MI->getOperand(0).getIndex();
1132b38c12643236825a6a49c31ef49da8e62b094c2Sirish Pande      return MI->getOperand(2).getReg();
114b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    }
115b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    break;
116b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  }
117b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  return 0;
118b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum}
119b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
120b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
121b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumunsigned
122b4b54153ad760c69a00a08531abef4ed434a5092Tony LinthicumHexagonInstrInfo::InsertBranch(MachineBasicBlock &MBB,MachineBasicBlock *TBB,
123b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                             MachineBasicBlock *FBB,
124b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                             const SmallVectorImpl<MachineOperand> &Cond,
125b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                             DebugLoc DL) const{
126b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
127b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    int BOpc   = Hexagon::JMP;
1286ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma    int BccOpc = Hexagon::JMP_t;
129b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
130b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    assert(TBB && "InsertBranch must not be told to insert a fallthrough");
131b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
132b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    int regPos = 0;
133b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    // Check if ReverseBranchCondition has asked to reverse this branch
134b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    // If we want to reverse the branch an odd number of times, we want
1356ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma    // JMP_f.
136b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    if (!Cond.empty() && Cond[0].isImm() && Cond[0].getImm() == 0) {
1376ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma      BccOpc = Hexagon::JMP_f;
138b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum      regPos = 1;
139b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    }
140b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
141dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    if (!FBB) {
142b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum      if (Cond.empty()) {
143b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum        // Due to a bug in TailMerging/CFG Optimization, we need to add a
144b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum        // special case handling of a predicated jump followed by an
145b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum        // unconditional jump. If not, Tail Merging and CFG Optimization go
146b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum        // into an infinite loop.
147b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum        MachineBasicBlock *NewTBB, *NewFBB;
148b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum        SmallVector<MachineOperand, 4> Cond;
149b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum        MachineInstr *Term = MBB.getFirstTerminator();
150b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum        if (isPredicated(Term) && !AnalyzeBranch(MBB, NewTBB, NewFBB, Cond,
151b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                                                 false)) {
152b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum          MachineBasicBlock *NextBB =
15336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines            std::next(MachineFunction::iterator(&MBB));
154b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum          if (NewTBB == NextBB) {
155b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum            ReverseBranchCondition(Cond);
156b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum            RemoveBranch(MBB);
157dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines            return InsertBranch(MBB, TBB, nullptr, Cond, DL);
158b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum          }
159b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum        }
160b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum        BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
161b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum      } else {
162b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum        BuildMI(&MBB, DL,
163b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                get(BccOpc)).addReg(Cond[regPos].getReg()).addMBB(TBB);
164b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum      }
165b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum      return 1;
166b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    }
167b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
168b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[regPos].getReg()).addMBB(TBB);
169b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
170b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
171b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    return 2;
172b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum}
173b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
174b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
175b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumbool HexagonInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
176b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                                     MachineBasicBlock *&TBB,
177b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                                 MachineBasicBlock *&FBB,
178b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                                 SmallVectorImpl<MachineOperand> &Cond,
179b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                                 bool AllowModify) const {
180dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  TBB = nullptr;
181dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  FBB = nullptr;
182b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
183b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  // If the block has no terminators, it just falls into the block after it.
1846ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma  MachineBasicBlock::instr_iterator I = MBB.instr_end();
1856ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma  if (I == MBB.instr_begin())
186b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    return false;
187b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
188b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  // A basic block may looks like this:
189b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  //
190b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  //  [   insn
191b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  //     EH_LABEL
192b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  //      insn
193b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  //      insn
194b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  //      insn
195b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  //     EH_LABEL
196b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  //      insn     ]
197b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  //
198b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  // It has two succs but does not have a terminator
199b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  // Don't know how to handle it.
200b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  do {
201b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    --I;
202b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    if (I->isEHLabel())
203b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum      return true;
2046ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma  } while (I != MBB.instr_begin());
205b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
2066ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma  I = MBB.instr_end();
207b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  --I;
208b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
209b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  while (I->isDebugValue()) {
2106ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma    if (I == MBB.instr_begin())
2116ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma      return false;
2126ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma    --I;
2136ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma  }
2146ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma
2156ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma  // Delete the JMP if it's equivalent to a fall-through.
2166ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma  if (AllowModify && I->getOpcode() == Hexagon::JMP &&
2176ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma      MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
2186ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma    DEBUG(dbgs()<< "\nErasing the jump to successor block\n";);
2196ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma    I->eraseFromParent();
2206ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma    I = MBB.instr_end();
2216ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma    if (I == MBB.instr_begin())
222b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum      return false;
223b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    --I;
224b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  }
225b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  if (!isUnpredicatedTerminator(I))
226b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    return false;
227b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
228b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  // Get the last instruction in the block.
229b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  MachineInstr *LastInst = I;
230dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  MachineInstr *SecondLastInst = nullptr;
2316ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma  // Find one more terminator if present.
2326ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma  do {
2336ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma    if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(I)) {
2346ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma      if (!SecondLastInst)
2356ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma        SecondLastInst = I;
2366ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma      else
2376ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma        // This is a third branch.
2386ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma        return true;
2396ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma    }
2406ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma    if (I == MBB.instr_begin())
2416ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma      break;
2426ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma    --I;
2436ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma  } while(I);
2446ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma
2456ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma  int LastOpcode = LastInst->getOpcode();
2466ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma
2476ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma  bool LastOpcodeHasJMP_c = PredOpcodeHasJMP_c(LastOpcode);
2486ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma  bool LastOpcodeHasNot = PredOpcodeHasNot(LastOpcode);
249b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
250b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  // If there is only one terminator instruction, process it.
2516ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma  if (LastInst && !SecondLastInst) {
2526ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma    if (LastOpcode == Hexagon::JMP) {
253b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum      TBB = LastInst->getOperand(0).getMBB();
254b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum      return false;
255b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    }
2566ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma    if (LastOpcode == Hexagon::ENDLOOP0) {
2576ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma      TBB = LastInst->getOperand(0).getMBB();
258b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum      Cond.push_back(LastInst->getOperand(0));
259b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum      return false;
260b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    }
2616ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma    if (LastOpcodeHasJMP_c) {
262b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum      TBB = LastInst->getOperand(1).getMBB();
2636ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma      if (LastOpcodeHasNot) {
2646ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma        Cond.push_back(MachineOperand::CreateImm(0));
2656ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma      }
266b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum      Cond.push_back(LastInst->getOperand(0));
267b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum      return false;
268b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    }
269b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    // Otherwise, don't know what this is.
270b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    return true;
271b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  }
272b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
2736ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma  int SecLastOpcode = SecondLastInst->getOpcode();
274b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
2756ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma  bool SecLastOpcodeHasJMP_c = PredOpcodeHasJMP_c(SecLastOpcode);
2766ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma  bool SecLastOpcodeHasNot = PredOpcodeHasNot(SecLastOpcode);
2776ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma  if (SecLastOpcodeHasJMP_c && (LastOpcode == Hexagon::JMP)) {
278b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    TBB =  SecondLastInst->getOperand(1).getMBB();
2796ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma    if (SecLastOpcodeHasNot)
2806ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma      Cond.push_back(MachineOperand::CreateImm(0));
281b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    Cond.push_back(SecondLastInst->getOperand(0));
282b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    FBB = LastInst->getOperand(0).getMBB();
283b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    return false;
284b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  }
285b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
286b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  // If the block ends with two Hexagon:JMPs, handle it.  The second one is not
287b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  // executed, so remove it.
2886ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma  if (SecLastOpcode == Hexagon::JMP && LastOpcode == Hexagon::JMP) {
289b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    TBB = SecondLastInst->getOperand(0).getMBB();
290b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    I = LastInst;
291b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    if (AllowModify)
292b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum      I->eraseFromParent();
293b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    return false;
294b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  }
295b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
2966ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma  // If the block ends with an ENDLOOP, and JMP, handle it.
2976ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma  if (SecLastOpcode == Hexagon::ENDLOOP0 &&
2986ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma      LastOpcode == Hexagon::JMP) {
2996ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma    TBB = SecondLastInst->getOperand(0).getMBB();
3006ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma    Cond.push_back(SecondLastInst->getOperand(0));
3016ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma    FBB = LastInst->getOperand(0).getMBB();
3026ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma    return false;
3036ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma  }
3046ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma
305b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  // Otherwise, can't handle this.
306b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  return true;
307b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum}
308b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
309b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
310b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumunsigned HexagonInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
311b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  int BOpc   = Hexagon::JMP;
3126ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma  int BccOpc = Hexagon::JMP_t;
3136ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma  int BccOpcNot = Hexagon::JMP_f;
314b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
315b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  MachineBasicBlock::iterator I = MBB.end();
316b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  if (I == MBB.begin()) return 0;
317b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  --I;
318b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  if (I->getOpcode() != BOpc && I->getOpcode() != BccOpc &&
319b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum      I->getOpcode() != BccOpcNot)
320b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    return 0;
321b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
322b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  // Remove the branch.
323b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  I->eraseFromParent();
324b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
325b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  I = MBB.end();
326b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
327b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  if (I == MBB.begin()) return 1;
328b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  --I;
329b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  if (I->getOpcode() != BccOpc && I->getOpcode() != BccOpcNot)
330b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    return 1;
331b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
332b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  // Remove the branch.
333b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  I->eraseFromParent();
334b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  return 2;
335b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum}
336b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
337b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
338ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek/// \brief For a comparison instruction, return the source registers in
339ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek/// \p SrcReg and \p SrcReg2 if having two register operands, and the value it
340ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek/// compares against in CmpValue. Return true if the comparison instruction
341ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek/// can be analyzed.
342ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszekbool HexagonInstrInfo::analyzeCompare(const MachineInstr *MI,
343ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek                                      unsigned &SrcReg, unsigned &SrcReg2,
344ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek                                      int &Mask, int &Value) const {
345ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek  unsigned Opc = MI->getOpcode();
346ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek
347ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek  // Set mask and the first source register.
348ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek  switch (Opc) {
349ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek    case Hexagon::CMPEHexagon4rr:
350ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek    case Hexagon::CMPEQri:
351ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek    case Hexagon::CMPEQrr:
352ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek    case Hexagon::CMPGT64rr:
353ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek    case Hexagon::CMPGTU64rr:
354ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek    case Hexagon::CMPGTUri:
355ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek    case Hexagon::CMPGTUrr:
356ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek    case Hexagon::CMPGTri:
357ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek    case Hexagon::CMPGTrr:
358ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek      SrcReg = MI->getOperand(1).getReg();
359ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek      Mask = ~0;
360ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek      break;
361ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek    case Hexagon::CMPbEQri_V4:
362ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek    case Hexagon::CMPbEQrr_sbsb_V4:
363ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek    case Hexagon::CMPbEQrr_ubub_V4:
364ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek    case Hexagon::CMPbGTUri_V4:
365ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek    case Hexagon::CMPbGTUrr_V4:
366ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek    case Hexagon::CMPbGTrr_V4:
367ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek      SrcReg = MI->getOperand(1).getReg();
368ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek      Mask = 0xFF;
369ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek      break;
370ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek    case Hexagon::CMPhEQri_V4:
371ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek    case Hexagon::CMPhEQrr_shl_V4:
372ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek    case Hexagon::CMPhEQrr_xor_V4:
373ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek    case Hexagon::CMPhGTUri_V4:
374ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek    case Hexagon::CMPhGTUrr_V4:
375ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek    case Hexagon::CMPhGTrr_shl_V4:
376ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek      SrcReg = MI->getOperand(1).getReg();
377ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek      Mask = 0xFFFF;
378ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek      break;
379ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek  }
380ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek
381ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek  // Set the value/second source register.
382ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek  switch (Opc) {
383ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek    case Hexagon::CMPEHexagon4rr:
384ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek    case Hexagon::CMPEQrr:
385ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek    case Hexagon::CMPGT64rr:
386ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek    case Hexagon::CMPGTU64rr:
387ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek    case Hexagon::CMPGTUrr:
388ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek    case Hexagon::CMPGTrr:
389ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek    case Hexagon::CMPbEQrr_sbsb_V4:
390ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek    case Hexagon::CMPbEQrr_ubub_V4:
391ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek    case Hexagon::CMPbGTUrr_V4:
392ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek    case Hexagon::CMPbGTrr_V4:
393ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek    case Hexagon::CMPhEQrr_shl_V4:
394ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek    case Hexagon::CMPhEQrr_xor_V4:
395ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek    case Hexagon::CMPhGTUrr_V4:
396ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek    case Hexagon::CMPhGTrr_shl_V4:
397ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek      SrcReg2 = MI->getOperand(2).getReg();
398ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek      return true;
399ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek
400ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek    case Hexagon::CMPEQri:
401ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek    case Hexagon::CMPGTUri:
402ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek    case Hexagon::CMPGTri:
403ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek    case Hexagon::CMPbEQri_V4:
404ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek    case Hexagon::CMPbGTUri_V4:
405ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek    case Hexagon::CMPhEQri_V4:
406ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek    case Hexagon::CMPhGTUri_V4:
407ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek      SrcReg2 = 0;
408ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek      Value = MI->getOperand(2).getImm();
409ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek      return true;
410ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek  }
411ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek
412ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek  return false;
413ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek}
414ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek
415ce55d91ec97b1227cb4cb94354c89452d787515eKrzysztof Parzyszek
416b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumvoid HexagonInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
417b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                                 MachineBasicBlock::iterator I, DebugLoc DL,
418b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                                 unsigned DestReg, unsigned SrcReg,
419b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                                 bool KillSrc) const {
420b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  if (Hexagon::IntRegsRegClass.contains(SrcReg, DestReg)) {
421b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    BuildMI(MBB, I, DL, get(Hexagon::TFR), DestReg).addReg(SrcReg);
422b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    return;
423b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  }
424b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  if (Hexagon::DoubleRegsRegClass.contains(SrcReg, DestReg)) {
425b53b791efd85e9278baacb29745217dfa2eae775Jyotsna Verma    BuildMI(MBB, I, DL, get(Hexagon::TFR64), DestReg).addReg(SrcReg);
426b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    return;
427b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  }
428b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  if (Hexagon::PredRegsRegClass.contains(SrcReg, DestReg)) {
429b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    // Map Pd = Ps to Pd = or(Ps, Ps).
430b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    BuildMI(MBB, I, DL, get(Hexagon::OR_pp),
431b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum            DestReg).addReg(SrcReg).addReg(SrcReg);
432b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    return;
433b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  }
4342b38c12643236825a6a49c31ef49da8e62b094c2Sirish Pande  if (Hexagon::DoubleRegsRegClass.contains(DestReg) &&
4352b38c12643236825a6a49c31ef49da8e62b094c2Sirish Pande      Hexagon::IntRegsRegClass.contains(SrcReg)) {
436b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    // We can have an overlap between single and double reg: r1:0 = r0.
437b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    if(SrcReg == RI.getSubReg(DestReg, Hexagon::subreg_loreg)) {
438b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum        // r1:0 = r0
439b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum        BuildMI(MBB, I, DL, get(Hexagon::TFRI), (RI.getSubReg(DestReg,
440b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                Hexagon::subreg_hireg))).addImm(0);
441b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    } else {
442b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum        // r1:0 = r1 or no overlap.
443b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum        BuildMI(MBB, I, DL, get(Hexagon::TFR), (RI.getSubReg(DestReg,
444b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                Hexagon::subreg_loreg))).addReg(SrcReg);
445b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum        BuildMI(MBB, I, DL, get(Hexagon::TFRI), (RI.getSubReg(DestReg,
446b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                Hexagon::subreg_hireg))).addImm(0);
447b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    }
448b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    return;
449b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  }
4502b38c12643236825a6a49c31ef49da8e62b094c2Sirish Pande  if (Hexagon::CRRegsRegClass.contains(DestReg) &&
4512b38c12643236825a6a49c31ef49da8e62b094c2Sirish Pande      Hexagon::IntRegsRegClass.contains(SrcReg)) {
452b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    BuildMI(MBB, I, DL, get(Hexagon::TFCR), DestReg).addReg(SrcReg);
453b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    return;
454ab7955b9ce3197215406bc9fc97b22074127d035Sirish Pande  }
455666e0d3bc4a28540c1aa73b24573af70d1d46aeaAnshuman Dasgupta  if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
456666e0d3bc4a28540c1aa73b24573af70d1d46aeaAnshuman Dasgupta      Hexagon::IntRegsRegClass.contains(DestReg)) {
457666e0d3bc4a28540c1aa73b24573af70d1d46aeaAnshuman Dasgupta    BuildMI(MBB, I, DL, get(Hexagon::TFR_RsPd), DestReg).
458666e0d3bc4a28540c1aa73b24573af70d1d46aeaAnshuman Dasgupta      addReg(SrcReg, getKillRegState(KillSrc));
459666e0d3bc4a28540c1aa73b24573af70d1d46aeaAnshuman Dasgupta    return;
460666e0d3bc4a28540c1aa73b24573af70d1d46aeaAnshuman Dasgupta  }
461666e0d3bc4a28540c1aa73b24573af70d1d46aeaAnshuman Dasgupta  if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
462666e0d3bc4a28540c1aa73b24573af70d1d46aeaAnshuman Dasgupta      Hexagon::PredRegsRegClass.contains(DestReg)) {
463666e0d3bc4a28540c1aa73b24573af70d1d46aeaAnshuman Dasgupta    BuildMI(MBB, I, DL, get(Hexagon::TFR_PdRs), DestReg).
464666e0d3bc4a28540c1aa73b24573af70d1d46aeaAnshuman Dasgupta      addReg(SrcReg, getKillRegState(KillSrc));
465666e0d3bc4a28540c1aa73b24573af70d1d46aeaAnshuman Dasgupta    return;
466666e0d3bc4a28540c1aa73b24573af70d1d46aeaAnshuman Dasgupta  }
467ab7955b9ce3197215406bc9fc97b22074127d035Sirish Pande
468ab7955b9ce3197215406bc9fc97b22074127d035Sirish Pande  llvm_unreachable("Unimplemented");
469b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum}
470b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
471b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
472b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumvoid HexagonInstrInfo::
473b4b54153ad760c69a00a08531abef4ed434a5092Tony LinthicumstoreRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
474b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                    unsigned SrcReg, bool isKill, int FI,
475b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                    const TargetRegisterClass *RC,
476b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                    const TargetRegisterInfo *TRI) const {
477b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
478b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  DebugLoc DL = MBB.findDebugLoc(I);
479b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  MachineFunction &MF = *MBB.getParent();
480b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  MachineFrameInfo &MFI = *MF.getFrameInfo();
481b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  unsigned Align = MFI.getObjectAlignment(FI);
482b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
483b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  MachineMemOperand *MMO =
484b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum      MF.getMachineMemOperand(
485b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                      MachinePointerInfo(PseudoSourceValue::getFixedStack(FI)),
486b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                      MachineMemOperand::MOStore,
487b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                      MFI.getObjectSize(FI),
488b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                      Align);
489b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
490420761a0f193e87d08ee1c51b26bba23ab4bac7fCraig Topper  if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
4915262abb2682a4d09cda3563a55f27caffb57466cBrendon Cahoon    BuildMI(MBB, I, DL, get(Hexagon::STriw))
492b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum          .addFrameIndex(FI).addImm(0)
493b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum          .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
494420761a0f193e87d08ee1c51b26bba23ab4bac7fCraig Topper  } else if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC)) {
495b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    BuildMI(MBB, I, DL, get(Hexagon::STrid))
496b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum          .addFrameIndex(FI).addImm(0)
497b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum          .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
498420761a0f193e87d08ee1c51b26bba23ab4bac7fCraig Topper  } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
499b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    BuildMI(MBB, I, DL, get(Hexagon::STriw_pred))
500b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum          .addFrameIndex(FI).addImm(0)
501b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum          .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
502b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  } else {
503bc2198133a1836598b54b943420748e75d5dea94Craig Topper    llvm_unreachable("Unimplemented");
504b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  }
505b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum}
506b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
507b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
508b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumvoid HexagonInstrInfo::storeRegToAddr(
509b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                                 MachineFunction &MF, unsigned SrcReg,
510b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                                 bool isKill,
511b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                                 SmallVectorImpl<MachineOperand> &Addr,
512b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                                 const TargetRegisterClass *RC,
513b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                                 SmallVectorImpl<MachineInstr*> &NewMIs) const
514b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum{
515bc2198133a1836598b54b943420748e75d5dea94Craig Topper  llvm_unreachable("Unimplemented");
516b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum}
517b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
518b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
519b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumvoid HexagonInstrInfo::
520b4b54153ad760c69a00a08531abef4ed434a5092Tony LinthicumloadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
521b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                     unsigned DestReg, int FI,
522b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                     const TargetRegisterClass *RC,
523b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                     const TargetRegisterInfo *TRI) const {
524b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  DebugLoc DL = MBB.findDebugLoc(I);
525b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  MachineFunction &MF = *MBB.getParent();
526b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  MachineFrameInfo &MFI = *MF.getFrameInfo();
527b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  unsigned Align = MFI.getObjectAlignment(FI);
528b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
529b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  MachineMemOperand *MMO =
530b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum      MF.getMachineMemOperand(
531b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                      MachinePointerInfo(PseudoSourceValue::getFixedStack(FI)),
532b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                      MachineMemOperand::MOLoad,
533b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                      MFI.getObjectSize(FI),
534b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                      Align);
535420761a0f193e87d08ee1c51b26bba23ab4bac7fCraig Topper  if (RC == &Hexagon::IntRegsRegClass) {
536b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    BuildMI(MBB, I, DL, get(Hexagon::LDriw), DestReg)
537b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum          .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
538420761a0f193e87d08ee1c51b26bba23ab4bac7fCraig Topper  } else if (RC == &Hexagon::DoubleRegsRegClass) {
539b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    BuildMI(MBB, I, DL, get(Hexagon::LDrid), DestReg)
540b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum          .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
541420761a0f193e87d08ee1c51b26bba23ab4bac7fCraig Topper  } else if (RC == &Hexagon::PredRegsRegClass) {
542b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    BuildMI(MBB, I, DL, get(Hexagon::LDriw_pred), DestReg)
543b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum          .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
544b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  } else {
545bc2198133a1836598b54b943420748e75d5dea94Craig Topper    llvm_unreachable("Can't store this register to stack slot");
546b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  }
547b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum}
548b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
549b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
550b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumvoid HexagonInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
551b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                                        SmallVectorImpl<MachineOperand> &Addr,
552b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                                        const TargetRegisterClass *RC,
553b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                                 SmallVectorImpl<MachineInstr*> &NewMIs) const {
554bc2198133a1836598b54b943420748e75d5dea94Craig Topper  llvm_unreachable("Unimplemented");
555b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum}
556b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
557b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
558b4b54153ad760c69a00a08531abef4ed434a5092Tony LinthicumMachineInstr *HexagonInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
559b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                                                    MachineInstr* MI,
560b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                                          const SmallVectorImpl<unsigned> &Ops,
561b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                                                    int FI) const {
562b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  // Hexagon_TODO: Implement.
563dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  return nullptr;
564b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum}
565b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
566b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumunsigned HexagonInstrInfo::createVR(MachineFunction* MF, MVT VT) const {
567b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
568b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  MachineRegisterInfo &RegInfo = MF->getRegInfo();
569b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  const TargetRegisterClass *TRC;
5707517bbc91ae1c60d3c7df8b11642c7a5bb3d5a71Sirish Pande  if (VT == MVT::i1) {
571420761a0f193e87d08ee1c51b26bba23ab4bac7fCraig Topper    TRC = &Hexagon::PredRegsRegClass;
5727517bbc91ae1c60d3c7df8b11642c7a5bb3d5a71Sirish Pande  } else if (VT == MVT::i32 || VT == MVT::f32) {
573420761a0f193e87d08ee1c51b26bba23ab4bac7fCraig Topper    TRC = &Hexagon::IntRegsRegClass;
5747517bbc91ae1c60d3c7df8b11642c7a5bb3d5a71Sirish Pande  } else if (VT == MVT::i64 || VT == MVT::f64) {
575420761a0f193e87d08ee1c51b26bba23ab4bac7fCraig Topper    TRC = &Hexagon::DoubleRegsRegClass;
5767517bbc91ae1c60d3c7df8b11642c7a5bb3d5a71Sirish Pande  } else {
57727baab62e7d6267d9b18e4665c6bb1b75dae10d4Benjamin Kramer    llvm_unreachable("Cannot handle this register class");
5787517bbc91ae1c60d3c7df8b11642c7a5bb3d5a71Sirish Pande  }
579b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
580b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  unsigned NewReg = RegInfo.createVirtualRegister(TRC);
581b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  return NewReg;
582b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum}
583b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
58426f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pandebool HexagonInstrInfo::isExtendable(const MachineInstr *MI) const {
5859feabc23b39d90f8bf9ab5ac23f5a7f8af71480aJyotsna Verma  // Constant extenders are allowed only for V4 and above.
5869feabc23b39d90f8bf9ab5ac23f5a7f8af71480aJyotsna Verma  if (!Subtarget.hasV4TOps())
5879feabc23b39d90f8bf9ab5ac23f5a7f8af71480aJyotsna Verma    return false;
58826f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande
5899feabc23b39d90f8bf9ab5ac23f5a7f8af71480aJyotsna Verma  const MCInstrDesc &MID = MI->getDesc();
5909feabc23b39d90f8bf9ab5ac23f5a7f8af71480aJyotsna Verma  const uint64_t F = MID.TSFlags;
5919feabc23b39d90f8bf9ab5ac23f5a7f8af71480aJyotsna Verma  if ((F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask)
5929feabc23b39d90f8bf9ab5ac23f5a7f8af71480aJyotsna Verma    return true;
59326f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande
5949feabc23b39d90f8bf9ab5ac23f5a7f8af71480aJyotsna Verma  // TODO: This is largely obsolete now. Will need to be removed
5959feabc23b39d90f8bf9ab5ac23f5a7f8af71480aJyotsna Verma  // in consecutive patches.
5969feabc23b39d90f8bf9ab5ac23f5a7f8af71480aJyotsna Verma  switch(MI->getOpcode()) {
5979feabc23b39d90f8bf9ab5ac23f5a7f8af71480aJyotsna Verma    // TFR_FI Remains a special case.
59826f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    case Hexagon::TFR_FI:
59926f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande      return true;
6009feabc23b39d90f8bf9ab5ac23f5a7f8af71480aJyotsna Verma    default:
6019feabc23b39d90f8bf9ab5ac23f5a7f8af71480aJyotsna Verma      return false;
60226f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande  }
6039feabc23b39d90f8bf9ab5ac23f5a7f8af71480aJyotsna Verma  return  false;
60426f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande}
60526f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande
6069feabc23b39d90f8bf9ab5ac23f5a7f8af71480aJyotsna Verma// This returns true in two cases:
6079feabc23b39d90f8bf9ab5ac23f5a7f8af71480aJyotsna Verma// - The OP code itself indicates that this is an extended instruction.
6089feabc23b39d90f8bf9ab5ac23f5a7f8af71480aJyotsna Verma// - One of MOs has been marked with HMOTF_ConstExtended flag.
60926f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pandebool HexagonInstrInfo::isExtended(const MachineInstr *MI) const {
6109feabc23b39d90f8bf9ab5ac23f5a7f8af71480aJyotsna Verma  // First check if this is permanently extended op code.
6119feabc23b39d90f8bf9ab5ac23f5a7f8af71480aJyotsna Verma  const uint64_t F = MI->getDesc().TSFlags;
6129feabc23b39d90f8bf9ab5ac23f5a7f8af71480aJyotsna Verma  if ((F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask)
6139feabc23b39d90f8bf9ab5ac23f5a7f8af71480aJyotsna Verma    return true;
6149feabc23b39d90f8bf9ab5ac23f5a7f8af71480aJyotsna Verma  // Use MO operand flags to determine if one of MI's operands
6159feabc23b39d90f8bf9ab5ac23f5a7f8af71480aJyotsna Verma  // has HMOTF_ConstExtended flag set.
6169feabc23b39d90f8bf9ab5ac23f5a7f8af71480aJyotsna Verma  for (MachineInstr::const_mop_iterator I = MI->operands_begin(),
6179feabc23b39d90f8bf9ab5ac23f5a7f8af71480aJyotsna Verma       E = MI->operands_end(); I != E; ++I) {
6189feabc23b39d90f8bf9ab5ac23f5a7f8af71480aJyotsna Verma    if (I->getTargetFlags() && HexagonII::HMOTF_ConstExtended)
6197517bbc91ae1c60d3c7df8b11642c7a5bb3d5a71Sirish Pande      return true;
62026f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande  }
6219feabc23b39d90f8bf9ab5ac23f5a7f8af71480aJyotsna Verma  return  false;
62226f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande}
62326f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande
6241a7eab387849feaa3e9c35f145d5b07be54686e3Jyotsna Vermabool HexagonInstrInfo::isBranch (const MachineInstr *MI) const {
6251a7eab387849feaa3e9c35f145d5b07be54686e3Jyotsna Verma  return MI->getDesc().isBranch();
62626f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande}
62726f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande
6289feabc23b39d90f8bf9ab5ac23f5a7f8af71480aJyotsna Vermabool HexagonInstrInfo::isNewValueInst(const MachineInstr *MI) const {
6299feabc23b39d90f8bf9ab5ac23f5a7f8af71480aJyotsna Verma  if (isNewValueJump(MI))
6309feabc23b39d90f8bf9ab5ac23f5a7f8af71480aJyotsna Verma    return true;
6319feabc23b39d90f8bf9ab5ac23f5a7f8af71480aJyotsna Verma
6329feabc23b39d90f8bf9ab5ac23f5a7f8af71480aJyotsna Verma  if (isNewValueStore(MI))
6339feabc23b39d90f8bf9ab5ac23f5a7f8af71480aJyotsna Verma    return true;
6349feabc23b39d90f8bf9ab5ac23f5a7f8af71480aJyotsna Verma
6359feabc23b39d90f8bf9ab5ac23f5a7f8af71480aJyotsna Verma  return false;
6369feabc23b39d90f8bf9ab5ac23f5a7f8af71480aJyotsna Verma}
6379feabc23b39d90f8bf9ab5ac23f5a7f8af71480aJyotsna Verma
63826f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pandebool HexagonInstrInfo::isSaveCalleeSavedRegsCall(const MachineInstr *MI) const {
63926f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande  return MI->getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4;
64026f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande}
641ee498d3254b86bceb4f441741e9f442990647ce6Andrew Trick
642b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumbool HexagonInstrInfo::isPredicable(MachineInstr *MI) const {
643b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  bool isPred = MI->getDesc().isPredicable();
644b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
645b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  if (!isPred)
646b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    return false;
647b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
648b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  const int Opc = MI->getOpcode();
649b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
650b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  switch(Opc) {
651b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::TFRI:
6525262abb2682a4d09cda3563a55f27caffb57466cBrendon Cahoon    return isInt<12>(MI->getOperand(1).getImm());
653b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
654b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::STrid:
655b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::STrid_indexed:
6565262abb2682a4d09cda3563a55f27caffb57466cBrendon Cahoon    return isShiftedUInt<6,3>(MI->getOperand(1).getImm());
657b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
658b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::STriw:
659b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::STriw_indexed:
660b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::STriw_nv_V4:
6615262abb2682a4d09cda3563a55f27caffb57466cBrendon Cahoon    return isShiftedUInt<6,2>(MI->getOperand(1).getImm());
662b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
663b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::STrih:
664b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::STrih_indexed:
665b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::STrih_nv_V4:
6665262abb2682a4d09cda3563a55f27caffb57466cBrendon Cahoon    return isShiftedUInt<6,1>(MI->getOperand(1).getImm());
667b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
668b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::STrib:
669b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::STrib_indexed:
670b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::STrib_nv_V4:
6715262abb2682a4d09cda3563a55f27caffb57466cBrendon Cahoon    return isUInt<6>(MI->getOperand(1).getImm());
672b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
673b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::LDrid:
674b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::LDrid_indexed:
6755262abb2682a4d09cda3563a55f27caffb57466cBrendon Cahoon    return isShiftedUInt<6,3>(MI->getOperand(2).getImm());
676b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
677b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::LDriw:
678b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::LDriw_indexed:
6795262abb2682a4d09cda3563a55f27caffb57466cBrendon Cahoon    return isShiftedUInt<6,2>(MI->getOperand(2).getImm());
680b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
681b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::LDrih:
682b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::LDriuh:
683b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::LDrih_indexed:
684b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::LDriuh_indexed:
6855262abb2682a4d09cda3563a55f27caffb57466cBrendon Cahoon    return isShiftedUInt<6,1>(MI->getOperand(2).getImm());
686b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
687b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::LDrib:
688b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::LDriub:
689b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::LDrib_indexed:
690b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::LDriub_indexed:
6915262abb2682a4d09cda3563a55f27caffb57466cBrendon Cahoon    return isUInt<6>(MI->getOperand(2).getImm());
692b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
693b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::POST_LDrid:
6945262abb2682a4d09cda3563a55f27caffb57466cBrendon Cahoon    return isShiftedInt<4,3>(MI->getOperand(3).getImm());
695b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
696b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::POST_LDriw:
6975262abb2682a4d09cda3563a55f27caffb57466cBrendon Cahoon    return isShiftedInt<4,2>(MI->getOperand(3).getImm());
698b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
699b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::POST_LDrih:
700b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::POST_LDriuh:
7015262abb2682a4d09cda3563a55f27caffb57466cBrendon Cahoon    return isShiftedInt<4,1>(MI->getOperand(3).getImm());
702b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
703b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::POST_LDrib:
704b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::POST_LDriub:
7055262abb2682a4d09cda3563a55f27caffb57466cBrendon Cahoon    return isInt<4>(MI->getOperand(3).getImm());
706b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
707b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::STrib_imm_V4:
708b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::STrih_imm_V4:
709b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::STriw_imm_V4:
7105262abb2682a4d09cda3563a55f27caffb57466cBrendon Cahoon    return (isUInt<6>(MI->getOperand(1).getImm()) &&
7115262abb2682a4d09cda3563a55f27caffb57466cBrendon Cahoon            isInt<6>(MI->getOperand(2).getImm()));
712b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
713b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::ADD_ri:
7145262abb2682a4d09cda3563a55f27caffb57466cBrendon Cahoon    return isInt<8>(MI->getOperand(2).getImm());
715b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
716b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::ASLH:
717b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::ASRH:
718b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::SXTB:
719b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::SXTH:
720b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::ZXTB:
721b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::ZXTH:
7222b38c12643236825a6a49c31ef49da8e62b094c2Sirish Pande    return Subtarget.hasV4TOps();
723b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  }
724b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
725b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  return true;
726b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum}
727b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
7282b38c12643236825a6a49c31ef49da8e62b094c2Sirish Pande// This function performs the following inversiones:
7292b38c12643236825a6a49c31ef49da8e62b094c2Sirish Pande//
7302b38c12643236825a6a49c31ef49da8e62b094c2Sirish Pande//  cPt    ---> cNotPt
7312b38c12643236825a6a49c31ef49da8e62b094c2Sirish Pande//  cNotPt ---> cPt
7322b38c12643236825a6a49c31ef49da8e62b094c2Sirish Pande//
733ab7955b9ce3197215406bc9fc97b22074127d035Sirish Pandeunsigned HexagonInstrInfo::getInvertedPredicatedOpcode(const int Opc) const {
7341a7eab387849feaa3e9c35f145d5b07be54686e3Jyotsna Verma  int InvPredOpcode;
7351a7eab387849feaa3e9c35f145d5b07be54686e3Jyotsna Verma  InvPredOpcode = isPredicatedTrue(Opc) ? Hexagon::getFalsePredOpcode(Opc)
7361a7eab387849feaa3e9c35f145d5b07be54686e3Jyotsna Verma                                        : Hexagon::getTruePredOpcode(Opc);
7371a7eab387849feaa3e9c35f145d5b07be54686e3Jyotsna Verma  if (InvPredOpcode >= 0) // Valid instruction with the inverted predicate.
7381a7eab387849feaa3e9c35f145d5b07be54686e3Jyotsna Verma    return InvPredOpcode;
7391a7eab387849feaa3e9c35f145d5b07be54686e3Jyotsna Verma
740ab7955b9ce3197215406bc9fc97b22074127d035Sirish Pande  switch(Opc) {
74126f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    default: llvm_unreachable("Unexpected predicated instruction");
742ab7955b9ce3197215406bc9fc97b22074127d035Sirish Pande    case Hexagon::COMBINE_rr_cPt:
743ab7955b9ce3197215406bc9fc97b22074127d035Sirish Pande      return Hexagon::COMBINE_rr_cNotPt;
744ab7955b9ce3197215406bc9fc97b22074127d035Sirish Pande    case Hexagon::COMBINE_rr_cNotPt:
745ab7955b9ce3197215406bc9fc97b22074127d035Sirish Pande      return Hexagon::COMBINE_rr_cPt;
746ab7955b9ce3197215406bc9fc97b22074127d035Sirish Pande
747f4f60b10e4ccd14511f5c83f4e83dbcad6740f63Jyotsna Verma      // Dealloc_return.
748ab7955b9ce3197215406bc9fc97b22074127d035Sirish Pande    case Hexagon::DEALLOC_RET_cPt_V4:
749ab7955b9ce3197215406bc9fc97b22074127d035Sirish Pande      return Hexagon::DEALLOC_RET_cNotPt_V4;
750ab7955b9ce3197215406bc9fc97b22074127d035Sirish Pande    case Hexagon::DEALLOC_RET_cNotPt_V4:
751ab7955b9ce3197215406bc9fc97b22074127d035Sirish Pande      return Hexagon::DEALLOC_RET_cPt_V4;
752ab7955b9ce3197215406bc9fc97b22074127d035Sirish Pande  }
753ab7955b9ce3197215406bc9fc97b22074127d035Sirish Pande}
754b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
7551a35b8e2eb165624013d5a2eaf8b673f026999fcJyotsna Verma// New Value Store instructions.
7561a35b8e2eb165624013d5a2eaf8b673f026999fcJyotsna Vermabool HexagonInstrInfo::isNewValueStore(const MachineInstr *MI) const {
7571a35b8e2eb165624013d5a2eaf8b673f026999fcJyotsna Verma  const uint64_t F = MI->getDesc().TSFlags;
7581a35b8e2eb165624013d5a2eaf8b673f026999fcJyotsna Verma
7591a35b8e2eb165624013d5a2eaf8b673f026999fcJyotsna Verma  return ((F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask);
7601a35b8e2eb165624013d5a2eaf8b673f026999fcJyotsna Verma}
7611a35b8e2eb165624013d5a2eaf8b673f026999fcJyotsna Verma
7621a35b8e2eb165624013d5a2eaf8b673f026999fcJyotsna Vermabool HexagonInstrInfo::isNewValueStore(unsigned Opcode) const {
7631a35b8e2eb165624013d5a2eaf8b673f026999fcJyotsna Verma  const uint64_t F = get(Opcode).TSFlags;
7641a35b8e2eb165624013d5a2eaf8b673f026999fcJyotsna Verma
7651a35b8e2eb165624013d5a2eaf8b673f026999fcJyotsna Verma  return ((F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask);
7661a35b8e2eb165624013d5a2eaf8b673f026999fcJyotsna Verma}
767ee498d3254b86bceb4f441741e9f442990647ce6Andrew Trick
768b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumint HexagonInstrInfo::
769b4b54153ad760c69a00a08531abef4ed434a5092Tony LinthicumgetMatchingCondBranchOpcode(int Opc, bool invertPredicate) const {
7708aa138c12282e8bccac665f2197e817e32c2adcfPranav Bhandarkar  enum Hexagon::PredSense inPredSense;
7718aa138c12282e8bccac665f2197e817e32c2adcfPranav Bhandarkar  inPredSense = invertPredicate ? Hexagon::PredSense_false :
7728aa138c12282e8bccac665f2197e817e32c2adcfPranav Bhandarkar                                  Hexagon::PredSense_true;
7738aa138c12282e8bccac665f2197e817e32c2adcfPranav Bhandarkar  int CondOpcode = Hexagon::getPredOpcode(Opc, inPredSense);
7748aa138c12282e8bccac665f2197e817e32c2adcfPranav Bhandarkar  if (CondOpcode >= 0) // Valid Conditional opcode/instruction
7758aa138c12282e8bccac665f2197e817e32c2adcfPranav Bhandarkar    return CondOpcode;
7768aa138c12282e8bccac665f2197e817e32c2adcfPranav Bhandarkar
7778aa138c12282e8bccac665f2197e817e32c2adcfPranav Bhandarkar  // This switch case will be removed once all the instructions have been
7788aa138c12282e8bccac665f2197e817e32c2adcfPranav Bhandarkar  // modified to use relation maps.
779b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  switch(Opc) {
7807517bbc91ae1c60d3c7df8b11642c7a5bb3d5a71Sirish Pande  case Hexagon::TFRI_f:
7817517bbc91ae1c60d3c7df8b11642c7a5bb3d5a71Sirish Pande    return !invertPredicate ? Hexagon::TFRI_cPt_f :
7827517bbc91ae1c60d3c7df8b11642c7a5bb3d5a71Sirish Pande                              Hexagon::TFRI_cNotPt_f;
783b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::COMBINE_rr:
784b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    return !invertPredicate ? Hexagon::COMBINE_rr_cPt :
785b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                              Hexagon::COMBINE_rr_cNotPt;
786f4f60b10e4ccd14511f5c83f4e83dbcad6740f63Jyotsna Verma
787b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  // Word.
788f4f60b10e4ccd14511f5c83f4e83dbcad6740f63Jyotsna Verma  case Hexagon::STriw_f:
789b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    return !invertPredicate ? Hexagon::STriw_cPt :
790b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                              Hexagon::STriw_cNotPt;
791f4f60b10e4ccd14511f5c83f4e83dbcad6740f63Jyotsna Verma  case Hexagon::STriw_indexed_f:
792b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    return !invertPredicate ? Hexagon::STriw_indexed_cPt :
793b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                              Hexagon::STriw_indexed_cNotPt;
794f4f60b10e4ccd14511f5c83f4e83dbcad6740f63Jyotsna Verma
795b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  // DEALLOC_RETURN.
796b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::DEALLOC_RET_V4:
797b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    return !invertPredicate ? Hexagon::DEALLOC_RET_cPt_V4 :
798b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                              Hexagon::DEALLOC_RET_cNotPt_V4;
799b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  }
80027baab62e7d6267d9b18e4665c6bb1b75dae10d4Benjamin Kramer  llvm_unreachable("Unexpected predicable instruction");
801b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum}
802b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
803b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
804b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumbool HexagonInstrInfo::
805b4b54153ad760c69a00a08531abef4ed434a5092Tony LinthicumPredicateInstruction(MachineInstr *MI,
806b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                     const SmallVectorImpl<MachineOperand> &Cond) const {
807b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  int Opc = MI->getOpcode();
808b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  assert (isPredicable(MI) && "Expected predicable instruction");
809b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  bool invertJump = (!Cond.empty() && Cond[0].isImm() &&
810b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                     (Cond[0].getImm() == 0));
8116b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma
8126b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma  // This will change MI's opcode to its predicate version.
8136b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma  // However, its operand list is still the old one, i.e. the
8146b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma  // non-predicate one.
815b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  MI->setDesc(get(getMatchingCondBranchOpcode(Opc, invertJump)));
816b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
8176b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma  int oper = -1;
8186b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma  unsigned int GAIdx = 0;
8196b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma
8206b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma  // Indicates whether the current MI has a GlobalAddress operand
8216b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma  bool hasGAOpnd = false;
8226b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma  std::vector<MachineOperand> tmpOpnds;
8236b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma
8246b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma  // Indicates whether we need to shift operands to right.
8256b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma  bool needShift = true;
8266b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma
8276b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma  // The predicate is ALWAYS the FIRST input operand !!!
8286b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma  if (MI->getNumOperands() == 0) {
8296b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma    // The non-predicate version of MI does not take any operands,
8306b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma    // i.e. no outs and no ins. In this condition, the predicate
8316b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma    // operand will be directly placed at Operands[0]. No operand
8326b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma    // shift is needed.
8336b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma    // Example: BARRIER
8346b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma    needShift = false;
8356b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma    oper = -1;
8366b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma  }
8376b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma  else if (   MI->getOperand(MI->getNumOperands()-1).isReg()
8386b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma           && MI->getOperand(MI->getNumOperands()-1).isDef()
8396b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma           && !MI->getOperand(MI->getNumOperands()-1).isImplicit()) {
8406b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma    // The non-predicate version of MI does not have any input operands.
8416b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma    // In this condition, we extend the length of Operands[] by one and
8426b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma    // copy the original last operand to the newly allocated slot.
8436b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma    // At this moment, it is just a place holder. Later, we will put
8446b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma    // predicate operand directly into it. No operand shift is needed.
8456b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma    // Example: r0=BARRIER (this is a faked insn used here for illustration)
8466b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma    MI->addOperand(MI->getOperand(MI->getNumOperands()-1));
8476b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma    needShift = false;
8486b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma    oper = MI->getNumOperands() - 2;
8496b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma  }
8506b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma  else {
8516b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma    // We need to right shift all input operands by one. Duplicate the
8526b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma    // last operand into the newly allocated slot.
8536b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma    MI->addOperand(MI->getOperand(MI->getNumOperands()-1));
8546b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma  }
8556b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma
8566b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma  if (needShift)
8576b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma  {
8586b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma    // Operands[ MI->getNumOperands() - 2 ] has been copied into
8596b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma    // Operands[ MI->getNumOperands() - 1 ], so we start from
8606b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma    // Operands[ MI->getNumOperands() - 3 ].
8616b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma    // oper is a signed int.
8626b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma    // It is ok if "MI->getNumOperands()-3" is -3, -2, or -1.
8636b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma    for (oper = MI->getNumOperands() - 3; oper >= 0; --oper)
8646b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma    {
8656b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma      MachineOperand &MO = MI->getOperand(oper);
8666b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma
8676b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma      // Opnd[0] Opnd[1] Opnd[2] Opnd[3] Opnd[4]   Opnd[5]   Opnd[6]   Opnd[7]
8686b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma      // <Def0>  <Def1>  <Use0>  <Use1>  <ImpDef0> <ImpDef1> <ImpUse0> <ImpUse1>
8696b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma      //               /\~
8706b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma      //              /||\~
8716b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma      //               ||
8726b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma      //        Predicate Operand here
8736b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma      if (MO.isReg() && !MO.isUse() && !MO.isImplicit()) {
8746b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma        break;
8756b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma      }
8766b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma      if (MO.isReg()) {
8776b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma        MI->getOperand(oper+1).ChangeToRegister(MO.getReg(), MO.isDef(),
8786b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma                                                MO.isImplicit(), MO.isKill(),
8796b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma                                                MO.isDead(), MO.isUndef(),
8806b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma                                                MO.isDebug());
8816b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma      }
8826b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma      else if (MO.isImm()) {
8836b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma        MI->getOperand(oper+1).ChangeToImmediate(MO.getImm());
8846b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma      }
8856b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma      else if (MO.isGlobal()) {
8866b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma        // MI can not have more than one GlobalAddress operand.
8876b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma        assert(hasGAOpnd == false && "MI can only have one GlobalAddress opnd");
8886b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma
8896b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma        // There is no member function called "ChangeToGlobalAddress" in the
8906b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma        // MachineOperand class (not like "ChangeToRegister" and
8916b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma        // "ChangeToImmediate"). So we have to remove them from Operands[] list
8926b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma        // first, and then add them back after we have inserted the predicate
8936b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma        // operand. tmpOpnds[] is to remember these operands before we remove
8946b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma        // them.
8956b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma        tmpOpnds.push_back(MO);
8966b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma
8976b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma        // Operands[oper] is a GlobalAddress operand;
8986b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma        // Operands[oper+1] has been copied into Operands[oper+2];
8996b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma        hasGAOpnd = true;
9006b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma        GAIdx = oper;
9016b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma        continue;
9026b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma      }
9036b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma      else {
9046b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma        assert(false && "Unexpected operand type");
9056b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma      }
906b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    }
907b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  }
908b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
909b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  int regPos = invertJump ? 1 : 0;
910b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  MachineOperand PredMO = Cond[regPos];
9116b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma
9126b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma  // [oper] now points to the last explicit Def. Predicate operand must be
9136b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma  // located at [oper+1]. See diagram above.
9146b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma  // This assumes that the predicate is always the first operand,
9156b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma  // i.e. Operands[0+numResults], in the set of inputs
9166b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma  // It is better to have an assert here to check this. But I don't know how
9176b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma  // to write this assert because findFirstPredOperandIdx() would return -1
9186b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma  if (oper < -1) oper = -1;
919f9759c9f08af056ca6a496d846dd06656b6f1d9bJyotsna Verma
920b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  MI->getOperand(oper+1).ChangeToRegister(PredMO.getReg(), PredMO.isDef(),
921f9759c9f08af056ca6a496d846dd06656b6f1d9bJyotsna Verma                                          PredMO.isImplicit(), false,
922b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                                          PredMO.isDead(), PredMO.isUndef(),
923b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                                          PredMO.isDebug());
924b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
925f9759c9f08af056ca6a496d846dd06656b6f1d9bJyotsna Verma  MachineRegisterInfo &RegInfo = MI->getParent()->getParent()->getRegInfo();
926f9759c9f08af056ca6a496d846dd06656b6f1d9bJyotsna Verma  RegInfo.clearKillFlags(PredMO.getReg());
927f9759c9f08af056ca6a496d846dd06656b6f1d9bJyotsna Verma
9286b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma  if (hasGAOpnd)
9296b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma  {
9306b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma    unsigned int i;
9316b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma
9326b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma    // Operands[GAIdx] is the original GlobalAddress operand, which is
9336b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma    // already copied into tmpOpnds[0].
9346b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma    // Operands[GAIdx] now stores a copy of Operands[GAIdx-1]
9356b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma    // Operands[GAIdx+1] has already been copied into Operands[GAIdx+2],
9366b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma    // so we start from [GAIdx+2]
9376b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma    for (i = GAIdx + 2; i < MI->getNumOperands(); ++i)
9386b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma      tmpOpnds.push_back(MI->getOperand(i));
9396b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma
9406b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma    // Remove all operands in range [ (GAIdx+1) ... (MI->getNumOperands()-1) ]
9416b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma    // It is very important that we always remove from the end of Operands[]
9426b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma    // MI->getNumOperands() is at least 2 if program goes to here.
9436b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma    for (i = MI->getNumOperands() - 1; i > GAIdx; --i)
9446b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma      MI->RemoveOperand(i);
9456b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma
9466b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma    for (i = 0; i < tmpOpnds.size(); ++i)
9476b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma      MI->addOperand(tmpOpnds[i]);
9486b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma  }
9496b8d2026ba0b60a317fa239eacbcaeff5f2270f0Jyotsna Verma
950b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  return true;
951b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum}
952b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
953b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
954b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumbool
955b4b54153ad760c69a00a08531abef4ed434a5092Tony LinthicumHexagonInstrInfo::
956b4b54153ad760c69a00a08531abef4ed434a5092Tony LinthicumisProfitableToIfCvt(MachineBasicBlock &MBB,
957575e90e955064f60ac66230dce6c27653973c149Kay Tiong Khoo                    unsigned NumCycles,
958b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                    unsigned ExtraPredCycles,
959b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                    const BranchProbability &Probability) const {
960b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  return true;
961b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum}
962b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
963b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
964b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumbool
965b4b54153ad760c69a00a08531abef4ed434a5092Tony LinthicumHexagonInstrInfo::
966b4b54153ad760c69a00a08531abef4ed434a5092Tony LinthicumisProfitableToIfCvt(MachineBasicBlock &TMBB,
967b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                    unsigned NumTCycles,
968b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                    unsigned ExtraTCycles,
969b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                    MachineBasicBlock &FMBB,
970b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                    unsigned NumFCycles,
971b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                    unsigned ExtraFCycles,
972b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                    const BranchProbability &Probability) const {
973b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  return true;
974b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum}
975b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
9761a7eab387849feaa3e9c35f145d5b07be54686e3Jyotsna Verma// Returns true if an instruction is predicated irrespective of the predicate
9771a7eab387849feaa3e9c35f145d5b07be54686e3Jyotsna Verma// sense. For example, all of the following will return true.
9781a7eab387849feaa3e9c35f145d5b07be54686e3Jyotsna Verma// if (p0) R1 = add(R2, R3)
9791a7eab387849feaa3e9c35f145d5b07be54686e3Jyotsna Verma// if (!p0) R1 = add(R2, R3)
9801a7eab387849feaa3e9c35f145d5b07be54686e3Jyotsna Verma// if (p0.new) R1 = add(R2, R3)
9811a7eab387849feaa3e9c35f145d5b07be54686e3Jyotsna Verma// if (!p0.new) R1 = add(R2, R3)
982b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumbool HexagonInstrInfo::isPredicated(const MachineInstr *MI) const {
983c635ebdb1a366060fe71ef3028ae888fa5e4130dBrendon Cahoon  const uint64_t F = MI->getDesc().TSFlags;
984b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
985c635ebdb1a366060fe71ef3028ae888fa5e4130dBrendon Cahoon  return ((F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
986b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum}
987b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
9881a7eab387849feaa3e9c35f145d5b07be54686e3Jyotsna Vermabool HexagonInstrInfo::isPredicated(unsigned Opcode) const {
9891a7eab387849feaa3e9c35f145d5b07be54686e3Jyotsna Verma  const uint64_t F = get(Opcode).TSFlags;
9901a7eab387849feaa3e9c35f145d5b07be54686e3Jyotsna Verma
9911a7eab387849feaa3e9c35f145d5b07be54686e3Jyotsna Verma  return ((F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
9921a7eab387849feaa3e9c35f145d5b07be54686e3Jyotsna Verma}
9931a7eab387849feaa3e9c35f145d5b07be54686e3Jyotsna Verma
9941a7eab387849feaa3e9c35f145d5b07be54686e3Jyotsna Vermabool HexagonInstrInfo::isPredicatedTrue(const MachineInstr *MI) const {
9951a7eab387849feaa3e9c35f145d5b07be54686e3Jyotsna Verma  const uint64_t F = MI->getDesc().TSFlags;
9961a7eab387849feaa3e9c35f145d5b07be54686e3Jyotsna Verma
9971a7eab387849feaa3e9c35f145d5b07be54686e3Jyotsna Verma  assert(isPredicated(MI));
9981a7eab387849feaa3e9c35f145d5b07be54686e3Jyotsna Verma  return (!((F >> HexagonII::PredicatedFalsePos) &
9991a7eab387849feaa3e9c35f145d5b07be54686e3Jyotsna Verma            HexagonII::PredicatedFalseMask));
10001a7eab387849feaa3e9c35f145d5b07be54686e3Jyotsna Verma}
10011a7eab387849feaa3e9c35f145d5b07be54686e3Jyotsna Verma
10021a7eab387849feaa3e9c35f145d5b07be54686e3Jyotsna Vermabool HexagonInstrInfo::isPredicatedTrue(unsigned Opcode) const {
10031a7eab387849feaa3e9c35f145d5b07be54686e3Jyotsna Verma  const uint64_t F = get(Opcode).TSFlags;
10041a7eab387849feaa3e9c35f145d5b07be54686e3Jyotsna Verma
10051a7eab387849feaa3e9c35f145d5b07be54686e3Jyotsna Verma  // Make sure that the instruction is predicated.
10061a7eab387849feaa3e9c35f145d5b07be54686e3Jyotsna Verma  assert((F>> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
10071a7eab387849feaa3e9c35f145d5b07be54686e3Jyotsna Verma  return (!((F >> HexagonII::PredicatedFalsePos) &
10081a7eab387849feaa3e9c35f145d5b07be54686e3Jyotsna Verma            HexagonII::PredicatedFalseMask));
10091a7eab387849feaa3e9c35f145d5b07be54686e3Jyotsna Verma}
10101a7eab387849feaa3e9c35f145d5b07be54686e3Jyotsna Verma
1011810848d5b3bc53747722db0d30a21dc168c5503eJyotsna Vermabool HexagonInstrInfo::isPredicatedNew(const MachineInstr *MI) const {
1012810848d5b3bc53747722db0d30a21dc168c5503eJyotsna Verma  const uint64_t F = MI->getDesc().TSFlags;
1013810848d5b3bc53747722db0d30a21dc168c5503eJyotsna Verma
1014810848d5b3bc53747722db0d30a21dc168c5503eJyotsna Verma  assert(isPredicated(MI));
1015810848d5b3bc53747722db0d30a21dc168c5503eJyotsna Verma  return ((F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask);
1016810848d5b3bc53747722db0d30a21dc168c5503eJyotsna Verma}
1017810848d5b3bc53747722db0d30a21dc168c5503eJyotsna Verma
10181a7eab387849feaa3e9c35f145d5b07be54686e3Jyotsna Vermabool HexagonInstrInfo::isPredicatedNew(unsigned Opcode) const {
10191a7eab387849feaa3e9c35f145d5b07be54686e3Jyotsna Verma  const uint64_t F = get(Opcode).TSFlags;
10201a7eab387849feaa3e9c35f145d5b07be54686e3Jyotsna Verma
10211a7eab387849feaa3e9c35f145d5b07be54686e3Jyotsna Verma  assert(isPredicated(Opcode));
10221a7eab387849feaa3e9c35f145d5b07be54686e3Jyotsna Verma  return ((F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask);
10231a7eab387849feaa3e9c35f145d5b07be54686e3Jyotsna Verma}
10241a7eab387849feaa3e9c35f145d5b07be54686e3Jyotsna Verma
1025cdfb55d15bbcdf1533ca26ffcb9c94fa5d225bd0Jyotsna Verma// Returns true, if a ST insn can be promoted to a new-value store.
1026cdfb55d15bbcdf1533ca26ffcb9c94fa5d225bd0Jyotsna Vermabool HexagonInstrInfo::mayBeNewStore(const MachineInstr *MI) const {
1027cdfb55d15bbcdf1533ca26ffcb9c94fa5d225bd0Jyotsna Verma  const HexagonRegisterInfo& QRI = getRegisterInfo();
1028cdfb55d15bbcdf1533ca26ffcb9c94fa5d225bd0Jyotsna Verma  const uint64_t F = MI->getDesc().TSFlags;
1029cdfb55d15bbcdf1533ca26ffcb9c94fa5d225bd0Jyotsna Verma
1030cdfb55d15bbcdf1533ca26ffcb9c94fa5d225bd0Jyotsna Verma  return ((F >> HexagonII::mayNVStorePos) &
1031cdfb55d15bbcdf1533ca26ffcb9c94fa5d225bd0Jyotsna Verma           HexagonII::mayNVStoreMask &
1032cdfb55d15bbcdf1533ca26ffcb9c94fa5d225bd0Jyotsna Verma           QRI.Subtarget.hasV4TOps());
1033cdfb55d15bbcdf1533ca26ffcb9c94fa5d225bd0Jyotsna Verma}
1034cdfb55d15bbcdf1533ca26ffcb9c94fa5d225bd0Jyotsna Verma
1035b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumbool
1036b4b54153ad760c69a00a08531abef4ed434a5092Tony LinthicumHexagonInstrInfo::DefinesPredicate(MachineInstr *MI,
1037b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                                   std::vector<MachineOperand> &Pred) const {
1038b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  for (unsigned oper = 0; oper < MI->getNumOperands(); ++oper) {
1039b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    MachineOperand MO = MI->getOperand(oper);
1040b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    if (MO.isReg() && MO.isDef()) {
1041b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum      const TargetRegisterClass* RC = RI.getMinimalPhysRegClass(MO.getReg());
1042420761a0f193e87d08ee1c51b26bba23ab4bac7fCraig Topper      if (RC == &Hexagon::PredRegsRegClass) {
1043b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum        Pred.push_back(MO);
1044b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum        return true;
1045b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum      }
1046b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    }
1047b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  }
1048b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  return false;
1049b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum}
1050b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
1051b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
1052b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumbool
1053b4b54153ad760c69a00a08531abef4ed434a5092Tony LinthicumHexagonInstrInfo::
1054b4b54153ad760c69a00a08531abef4ed434a5092Tony LinthicumSubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
1055b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                  const SmallVectorImpl<MachineOperand> &Pred2) const {
1056b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  // TODO: Fix this
1057b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  return false;
1058b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum}
1059b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
1060b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
1061b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum//
1062b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum// We indicate that we want to reverse the branch by
1063b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum// inserting a 0 at the beginning of the Cond vector.
1064b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum//
1065b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumbool HexagonInstrInfo::
1066b4b54153ad760c69a00a08531abef4ed434a5092Tony LinthicumReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
1067b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  if (!Cond.empty() && Cond[0].isImm() && Cond[0].getImm() == 0) {
1068b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    Cond.erase(Cond.begin());
1069b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  } else {
1070b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    Cond.insert(Cond.begin(), MachineOperand::CreateImm(0));
1071b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  }
1072b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  return false;
1073b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum}
1074b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
1075b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
1076b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumbool HexagonInstrInfo::
1077b4b54153ad760c69a00a08531abef4ed434a5092Tony LinthicumisProfitableToDupForIfCvt(MachineBasicBlock &MBB,unsigned NumInstrs,
1078b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum                          const BranchProbability &Probability) const {
1079b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  return (NumInstrs <= 4);
1080b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum}
1081b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
1082b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumbool HexagonInstrInfo::isDeallocRet(const MachineInstr *MI) const {
1083b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  switch (MI->getOpcode()) {
108426f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande  default: return false;
1085b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::DEALLOC_RET_V4 :
1086b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::DEALLOC_RET_cPt_V4 :
1087b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::DEALLOC_RET_cNotPt_V4 :
1088b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::DEALLOC_RET_cdnPnt_V4 :
1089b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::DEALLOC_RET_cNotdnPnt_V4 :
1090b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::DEALLOC_RET_cdnPt_V4 :
1091b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::DEALLOC_RET_cNotdnPt_V4 :
1092b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum   return true;
1093b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  }
1094b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum}
1095b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
1096b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
1097b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumbool HexagonInstrInfo::
1098b4b54153ad760c69a00a08531abef4ed434a5092Tony LinthicumisValidOffset(const int Opcode, const int Offset) const {
1099b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  // This function is to check whether the "Offset" is in the correct range of
1100b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  // the given "Opcode". If "Offset" is not in the correct range, "ADD_ri" is
1101b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  // inserted to calculate the final address. Due to this reason, the function
1102b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  // assumes that the "Offset" has correct alignment.
1103cec50e6da221edeef922a5d5f85081ba66b08dabJyotsna Verma  // We used to assert if the offset was not properly aligned, however,
1104cec50e6da221edeef922a5d5f85081ba66b08dabJyotsna Verma  // there are cases where a misaligned pointer recast can cause this
1105cec50e6da221edeef922a5d5f85081ba66b08dabJyotsna Verma  // problem, and we need to allow for it. The front end warns of such
1106cec50e6da221edeef922a5d5f85081ba66b08dabJyotsna Verma  // misaligns with respect to load size.
1107b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
1108b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  switch(Opcode) {
1109b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
1110b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::LDriw:
1111a454ffd02a7ce2f7c8ffebaad1055bf09637d21bJyotsna Verma  case Hexagon::LDriw_indexed:
11127517bbc91ae1c60d3c7df8b11642c7a5bb3d5a71Sirish Pande  case Hexagon::LDriw_f:
1113a454ffd02a7ce2f7c8ffebaad1055bf09637d21bJyotsna Verma  case Hexagon::STriw_indexed:
1114b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::STriw:
11157517bbc91ae1c60d3c7df8b11642c7a5bb3d5a71Sirish Pande  case Hexagon::STriw_f:
1116b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    return (Offset >= Hexagon_MEMW_OFFSET_MIN) &&
1117b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum      (Offset <= Hexagon_MEMW_OFFSET_MAX);
1118b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
1119b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::LDrid:
1120a454ffd02a7ce2f7c8ffebaad1055bf09637d21bJyotsna Verma  case Hexagon::LDrid_indexed:
11217517bbc91ae1c60d3c7df8b11642c7a5bb3d5a71Sirish Pande  case Hexagon::LDrid_f:
1122b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::STrid:
1123a454ffd02a7ce2f7c8ffebaad1055bf09637d21bJyotsna Verma  case Hexagon::STrid_indexed:
11247517bbc91ae1c60d3c7df8b11642c7a5bb3d5a71Sirish Pande  case Hexagon::STrid_f:
1125b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    return (Offset >= Hexagon_MEMD_OFFSET_MIN) &&
1126b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum      (Offset <= Hexagon_MEMD_OFFSET_MAX);
1127b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
1128b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::LDrih:
1129b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::LDriuh:
1130b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::STrih:
1131b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    return (Offset >= Hexagon_MEMH_OFFSET_MIN) &&
1132b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum      (Offset <= Hexagon_MEMH_OFFSET_MAX);
1133b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
1134b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::LDrib:
1135b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::STrib:
1136b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::LDriub:
1137b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    return (Offset >= Hexagon_MEMB_OFFSET_MIN) &&
1138b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum      (Offset <= Hexagon_MEMB_OFFSET_MAX);
1139b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
1140b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::ADD_ri:
1141b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::TFR_FI:
1142b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    return (Offset >= Hexagon_ADDI_OFFSET_MIN) &&
1143b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum      (Offset <= Hexagon_ADDI_OFFSET_MAX);
1144b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
114597e602b574b38d57384f0f877700357531a3d23eJyotsna Verma  case Hexagon::MemOPw_ADDi_V4 :
114697e602b574b38d57384f0f877700357531a3d23eJyotsna Verma  case Hexagon::MemOPw_SUBi_V4 :
114797e602b574b38d57384f0f877700357531a3d23eJyotsna Verma  case Hexagon::MemOPw_ADDr_V4 :
114897e602b574b38d57384f0f877700357531a3d23eJyotsna Verma  case Hexagon::MemOPw_SUBr_V4 :
114997e602b574b38d57384f0f877700357531a3d23eJyotsna Verma  case Hexagon::MemOPw_ANDr_V4 :
115097e602b574b38d57384f0f877700357531a3d23eJyotsna Verma  case Hexagon::MemOPw_ORr_V4 :
1151b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    return (0 <= Offset && Offset <= 255);
1152b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
115397e602b574b38d57384f0f877700357531a3d23eJyotsna Verma  case Hexagon::MemOPh_ADDi_V4 :
115497e602b574b38d57384f0f877700357531a3d23eJyotsna Verma  case Hexagon::MemOPh_SUBi_V4 :
115597e602b574b38d57384f0f877700357531a3d23eJyotsna Verma  case Hexagon::MemOPh_ADDr_V4 :
115697e602b574b38d57384f0f877700357531a3d23eJyotsna Verma  case Hexagon::MemOPh_SUBr_V4 :
115797e602b574b38d57384f0f877700357531a3d23eJyotsna Verma  case Hexagon::MemOPh_ANDr_V4 :
115897e602b574b38d57384f0f877700357531a3d23eJyotsna Verma  case Hexagon::MemOPh_ORr_V4 :
1159b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    return (0 <= Offset && Offset <= 127);
1160b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
116197e602b574b38d57384f0f877700357531a3d23eJyotsna Verma  case Hexagon::MemOPb_ADDi_V4 :
116297e602b574b38d57384f0f877700357531a3d23eJyotsna Verma  case Hexagon::MemOPb_SUBi_V4 :
116397e602b574b38d57384f0f877700357531a3d23eJyotsna Verma  case Hexagon::MemOPb_ADDr_V4 :
116497e602b574b38d57384f0f877700357531a3d23eJyotsna Verma  case Hexagon::MemOPb_SUBr_V4 :
116597e602b574b38d57384f0f877700357531a3d23eJyotsna Verma  case Hexagon::MemOPb_ANDr_V4 :
116697e602b574b38d57384f0f877700357531a3d23eJyotsna Verma  case Hexagon::MemOPb_ORr_V4 :
1167b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    return (0 <= Offset && Offset <= 63);
1168b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
1169b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  // LDri_pred and STriw_pred are pseudo operations, so it has to take offset of
1170b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  // any size. Later pass knows how to handle it.
1171b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::STriw_pred:
1172b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::LDriw_pred:
1173b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    return true;
1174b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
117571490fa946f750fb3afe7228a32d31d401d4c1d8Krzysztof Parzyszek  case Hexagon::LOOP0_i:
117671490fa946f750fb3afe7228a32d31d401d4c1d8Krzysztof Parzyszek    return isUInt<10>(Offset);
117771490fa946f750fb3afe7228a32d31d401d4c1d8Krzysztof Parzyszek
1178b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  // INLINEASM is very special.
1179b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  case Hexagon::INLINEASM:
1180b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    return true;
1181b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  }
1182b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
118327baab62e7d6267d9b18e4665c6bb1b75dae10d4Benjamin Kramer  llvm_unreachable("No offset range is defined for this opcode. "
118427baab62e7d6267d9b18e4665c6bb1b75dae10d4Benjamin Kramer                   "Please define it in the above switch statement!");
1185b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum}
1186b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
1187b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
1188b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum//
1189b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum// Check if the Offset is a valid auto-inc imm by Load/Store Type.
1190b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum//
1191b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumbool HexagonInstrInfo::
1192b4b54153ad760c69a00a08531abef4ed434a5092Tony LinthicumisValidAutoIncImm(const EVT VT, const int Offset) const {
1193b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
1194b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  if (VT == MVT::i64) {
1195b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum      return (Offset >= Hexagon_MEMD_AUTOINC_MIN &&
1196b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum              Offset <= Hexagon_MEMD_AUTOINC_MAX &&
1197b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum              (Offset & 0x7) == 0);
1198b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  }
1199b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  if (VT == MVT::i32) {
1200b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum      return (Offset >= Hexagon_MEMW_AUTOINC_MIN &&
1201b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum              Offset <= Hexagon_MEMW_AUTOINC_MAX &&
1202b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum              (Offset & 0x3) == 0);
1203b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  }
1204b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  if (VT == MVT::i16) {
1205b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum      return (Offset >= Hexagon_MEMH_AUTOINC_MIN &&
1206b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum              Offset <= Hexagon_MEMH_AUTOINC_MAX &&
1207b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum              (Offset & 0x1) == 0);
1208b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  }
1209b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  if (VT == MVT::i8) {
1210b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum      return (Offset >= Hexagon_MEMB_AUTOINC_MIN &&
1211b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum              Offset <= Hexagon_MEMB_AUTOINC_MAX);
1212b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  }
1213bc2198133a1836598b54b943420748e75d5dea94Craig Topper  llvm_unreachable("Not an auto-inc opc!");
1214b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum}
1215b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
1216b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
1217b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumbool HexagonInstrInfo::
1218b4b54153ad760c69a00a08531abef4ed434a5092Tony LinthicumisMemOp(const MachineInstr *MI) const {
12191a35b8e2eb165624013d5a2eaf8b673f026999fcJyotsna Verma//  return MI->getDesc().mayLoad() && MI->getDesc().mayStore();
12201a35b8e2eb165624013d5a2eaf8b673f026999fcJyotsna Verma
1221b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  switch (MI->getOpcode())
1222b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  {
122326f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    default: return false;
122497e602b574b38d57384f0f877700357531a3d23eJyotsna Verma    case Hexagon::MemOPw_ADDi_V4 :
122597e602b574b38d57384f0f877700357531a3d23eJyotsna Verma    case Hexagon::MemOPw_SUBi_V4 :
122697e602b574b38d57384f0f877700357531a3d23eJyotsna Verma    case Hexagon::MemOPw_ADDr_V4 :
122797e602b574b38d57384f0f877700357531a3d23eJyotsna Verma    case Hexagon::MemOPw_SUBr_V4 :
122897e602b574b38d57384f0f877700357531a3d23eJyotsna Verma    case Hexagon::MemOPw_ANDr_V4 :
122997e602b574b38d57384f0f877700357531a3d23eJyotsna Verma    case Hexagon::MemOPw_ORr_V4 :
123097e602b574b38d57384f0f877700357531a3d23eJyotsna Verma    case Hexagon::MemOPh_ADDi_V4 :
123197e602b574b38d57384f0f877700357531a3d23eJyotsna Verma    case Hexagon::MemOPh_SUBi_V4 :
123297e602b574b38d57384f0f877700357531a3d23eJyotsna Verma    case Hexagon::MemOPh_ADDr_V4 :
123397e602b574b38d57384f0f877700357531a3d23eJyotsna Verma    case Hexagon::MemOPh_SUBr_V4 :
123497e602b574b38d57384f0f877700357531a3d23eJyotsna Verma    case Hexagon::MemOPh_ANDr_V4 :
123597e602b574b38d57384f0f877700357531a3d23eJyotsna Verma    case Hexagon::MemOPh_ORr_V4 :
123697e602b574b38d57384f0f877700357531a3d23eJyotsna Verma    case Hexagon::MemOPb_ADDi_V4 :
123797e602b574b38d57384f0f877700357531a3d23eJyotsna Verma    case Hexagon::MemOPb_SUBi_V4 :
123897e602b574b38d57384f0f877700357531a3d23eJyotsna Verma    case Hexagon::MemOPb_ADDr_V4 :
123997e602b574b38d57384f0f877700357531a3d23eJyotsna Verma    case Hexagon::MemOPb_SUBr_V4 :
124097e602b574b38d57384f0f877700357531a3d23eJyotsna Verma    case Hexagon::MemOPb_ANDr_V4 :
124197e602b574b38d57384f0f877700357531a3d23eJyotsna Verma    case Hexagon::MemOPb_ORr_V4 :
124297e602b574b38d57384f0f877700357531a3d23eJyotsna Verma    case Hexagon::MemOPb_SETBITi_V4:
124397e602b574b38d57384f0f877700357531a3d23eJyotsna Verma    case Hexagon::MemOPh_SETBITi_V4:
124497e602b574b38d57384f0f877700357531a3d23eJyotsna Verma    case Hexagon::MemOPw_SETBITi_V4:
124597e602b574b38d57384f0f877700357531a3d23eJyotsna Verma    case Hexagon::MemOPb_CLRBITi_V4:
124697e602b574b38d57384f0f877700357531a3d23eJyotsna Verma    case Hexagon::MemOPh_CLRBITi_V4:
124797e602b574b38d57384f0f877700357531a3d23eJyotsna Verma    case Hexagon::MemOPw_CLRBITi_V4:
124897e602b574b38d57384f0f877700357531a3d23eJyotsna Verma    return true;
1249b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  }
125097e602b574b38d57384f0f877700357531a3d23eJyotsna Verma  return false;
1251b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum}
1252b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
1253b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
1254b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumbool HexagonInstrInfo::
1255b4b54153ad760c69a00a08531abef4ed434a5092Tony LinthicumisSpillPredRegOp(const MachineInstr *MI) const {
125626f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande  switch (MI->getOpcode()) {
125726f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    default: return false;
1258b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::STriw_pred :
1259b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::LDriw_pred :
126026f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande      return true;
12611bfd24851ef35e754d9652551e1a7abb12fe6738Sirish Pande  }
1262b33857040f63a9fdfb0c2a2ca2af67ec12cf9d02Sirish Pande}
1263b33857040f63a9fdfb0c2a2ca2af67ec12cf9d02Sirish Pande
1264b33857040f63a9fdfb0c2a2ca2af67ec12cf9d02Sirish Pandebool HexagonInstrInfo::isNewValueJumpCandidate(const MachineInstr *MI) const {
1265b33857040f63a9fdfb0c2a2ca2af67ec12cf9d02Sirish Pande  switch (MI->getOpcode()) {
12662b38c12643236825a6a49c31ef49da8e62b094c2Sirish Pande    default: return false;
1267b33857040f63a9fdfb0c2a2ca2af67ec12cf9d02Sirish Pande    case Hexagon::CMPEQrr:
1268b33857040f63a9fdfb0c2a2ca2af67ec12cf9d02Sirish Pande    case Hexagon::CMPEQri:
1269b33857040f63a9fdfb0c2a2ca2af67ec12cf9d02Sirish Pande    case Hexagon::CMPGTrr:
1270b33857040f63a9fdfb0c2a2ca2af67ec12cf9d02Sirish Pande    case Hexagon::CMPGTri:
1271b33857040f63a9fdfb0c2a2ca2af67ec12cf9d02Sirish Pande    case Hexagon::CMPGTUrr:
1272b33857040f63a9fdfb0c2a2ca2af67ec12cf9d02Sirish Pande    case Hexagon::CMPGTUri:
1273b33857040f63a9fdfb0c2a2ca2af67ec12cf9d02Sirish Pande      return true;
1274b33857040f63a9fdfb0c2a2ca2af67ec12cf9d02Sirish Pande  }
12751bfd24851ef35e754d9652551e1a7abb12fe6738Sirish Pande}
12761bfd24851ef35e754d9652551e1a7abb12fe6738Sirish Pande
127726f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pandebool HexagonInstrInfo::
127826f61a158b3cce69252c05cc0e79f500d6c3d92eSirish PandeisConditionalTransfer (const MachineInstr *MI) const {
127926f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande  switch (MI->getOpcode()) {
128026f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    default: return false;
128126f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    case Hexagon::TFR_cPt:
128226f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    case Hexagon::TFR_cNotPt:
128326f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    case Hexagon::TFRI_cPt:
128426f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    case Hexagon::TFRI_cNotPt:
128526f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    case Hexagon::TFR_cdnPt:
128626f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    case Hexagon::TFR_cdnNotPt:
128726f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    case Hexagon::TFRI_cdnPt:
128826f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    case Hexagon::TFRI_cdnNotPt:
128926f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande      return true;
129026f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande  }
129126f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande}
1292b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
1293b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumbool HexagonInstrInfo::isConditionalALU32 (const MachineInstr* MI) const {
1294b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  const HexagonRegisterInfo& QRI = getRegisterInfo();
1295b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  switch (MI->getOpcode())
1296b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  {
129726f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    default: return false;
1298b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::ADD_ri_cPt:
1299b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::ADD_ri_cNotPt:
1300b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::ADD_rr_cPt:
1301b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::ADD_rr_cNotPt:
1302b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::XOR_rr_cPt:
1303b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::XOR_rr_cNotPt:
1304b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::AND_rr_cPt:
1305b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::AND_rr_cNotPt:
1306b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::OR_rr_cPt:
1307b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::OR_rr_cNotPt:
1308b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::SUB_rr_cPt:
1309b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::SUB_rr_cNotPt:
1310b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::COMBINE_rr_cPt:
1311b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::COMBINE_rr_cNotPt:
1312b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum      return true;
1313b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::ASLH_cPt_V4:
1314b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::ASLH_cNotPt_V4:
1315b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::ASRH_cPt_V4:
1316b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::ASRH_cNotPt_V4:
1317b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::SXTB_cPt_V4:
1318b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::SXTB_cNotPt_V4:
1319b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::SXTH_cPt_V4:
1320b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::SXTH_cNotPt_V4:
1321b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::ZXTB_cPt_V4:
1322b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::ZXTB_cNotPt_V4:
1323b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::ZXTH_cPt_V4:
1324b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::ZXTH_cNotPt_V4:
132526f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande      return QRI.Subtarget.hasV4TOps();
1326b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  }
1327b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum}
1328b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum
1329b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicumbool HexagonInstrInfo::
1330b4b54153ad760c69a00a08531abef4ed434a5092Tony LinthicumisConditionalLoad (const MachineInstr* MI) const {
1331b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  const HexagonRegisterInfo& QRI = getRegisterInfo();
1332b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  switch (MI->getOpcode())
1333b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum  {
133426f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    default: return false;
1335b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::LDrid_cPt :
1336b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::LDrid_cNotPt :
1337b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::LDrid_indexed_cPt :
1338b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::LDrid_indexed_cNotPt :
1339b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::LDriw_cPt :
1340b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::LDriw_cNotPt :
1341b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::LDriw_indexed_cPt :
1342b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::LDriw_indexed_cNotPt :
1343b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::LDrih_cPt :
1344b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::LDrih_cNotPt :
1345b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::LDrih_indexed_cPt :
1346b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::LDrih_indexed_cNotPt :
1347b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::LDrib_cPt :
1348b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::LDrib_cNotPt :
1349b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::LDrib_indexed_cPt :
1350b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::LDrib_indexed_cNotPt :
1351b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::LDriuh_cPt :
1352b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::LDriuh_cNotPt :
1353b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::LDriuh_indexed_cPt :
1354b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::LDriuh_indexed_cNotPt :
1355b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::LDriub_cPt :
1356b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::LDriub_cNotPt :
1357b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::LDriub_indexed_cPt :
1358b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::LDriub_indexed_cNotPt :
1359b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum      return true;
1360b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::POST_LDrid_cPt :
1361b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::POST_LDrid_cNotPt :
1362b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::POST_LDriw_cPt :
1363b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::POST_LDriw_cNotPt :
1364b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::POST_LDrih_cPt :
1365b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::POST_LDrih_cNotPt :
1366b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::POST_LDrib_cPt :
1367b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::POST_LDrib_cNotPt :
1368b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::POST_LDriuh_cPt :
1369b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::POST_LDriuh_cNotPt :
1370b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::POST_LDriub_cPt :
1371b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::POST_LDriub_cNotPt :
137226f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande      return QRI.Subtarget.hasV4TOps();
1373b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::LDrid_indexed_shl_cPt_V4 :
1374b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::LDrid_indexed_shl_cNotPt_V4 :
1375b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::LDrib_indexed_shl_cPt_V4 :
1376b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::LDrib_indexed_shl_cNotPt_V4 :
1377b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::LDriub_indexed_shl_cPt_V4 :
1378b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::LDriub_indexed_shl_cNotPt_V4 :
1379b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::LDrih_indexed_shl_cPt_V4 :
1380b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::LDrih_indexed_shl_cNotPt_V4 :
1381b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::LDriuh_indexed_shl_cPt_V4 :
1382b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::LDriuh_indexed_shl_cNotPt_V4 :
1383b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::LDriw_indexed_shl_cPt_V4 :
1384b4b54153ad760c69a00a08531abef4ed434a5092Tony Linthicum    case Hexagon::LDriw_indexed_shl_cNotPt_V4 :
138526f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande      return QRI.Subtarget.hasV4TOps();
138626f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande  }
138726f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande}
138826f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande
138926f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande// Returns true if an instruction is a conditional store.
139026f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande//
139126f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande// Note: It doesn't include conditional new-value stores as they can't be
139226f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande// converted to .new predicate.
139326f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande//
139426f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande//               p.new NV store [ if(p0.new)memw(R0+#0)=R2.new ]
139526f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande//                ^           ^
139626f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande//               /             \ (not OK. it will cause new-value store to be
139726f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande//              /               X conditional on p0.new while R2 producer is
139826f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande//             /                 \ on p0)
139926f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande//            /                   \.
140026f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande//     p.new store                 p.old NV store
140126f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande// [if(p0.new)memw(R0+#0)=R2]    [if(p0)memw(R0+#0)=R2.new]
140226f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande//            ^                  ^
140326f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande//             \                /
140426f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande//              \              /
140526f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande//               \            /
140626f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande//                 p.old store
140726f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande//             [if (p0)memw(R0+#0)=R2]
140826f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande//
140926f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande// The above diagram shows the steps involoved in the conversion of a predicated
141026f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande// store instruction to its .new predicated new-value form.
141126f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande//
141226f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande// The following set of instructions further explains the scenario where
141326f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande// conditional new-value store becomes invalid when promoted to .new predicate
141426f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande// form.
141526f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande//
141626f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande// { 1) if (p0) r0 = add(r1, r2)
141726f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande//   2) p0 = cmp.eq(r3, #0) }
141826f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande//
141926f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande//   3) if (p0) memb(r1+#0) = r0  --> this instruction can't be grouped with
142026f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande// the first two instructions because in instr 1, r0 is conditional on old value
142126f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande// of p0 but its use in instr 3 is conditional on p0 modified by instr 2 which
142226f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande// is not valid for new-value stores.
142326f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pandebool HexagonInstrInfo::
142426f61a158b3cce69252c05cc0e79f500d6c3d92eSirish PandeisConditionalStore (const MachineInstr* MI) const {
142526f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande  const HexagonRegisterInfo& QRI = getRegisterInfo();
142626f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande  switch (MI->getOpcode())
142726f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande  {
142826f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    default: return false;
142926f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    case Hexagon::STrib_imm_cPt_V4 :
143026f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    case Hexagon::STrib_imm_cNotPt_V4 :
143126f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    case Hexagon::STrib_indexed_shl_cPt_V4 :
143226f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    case Hexagon::STrib_indexed_shl_cNotPt_V4 :
143326f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    case Hexagon::STrib_cPt :
143426f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    case Hexagon::STrib_cNotPt :
143526f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    case Hexagon::POST_STbri_cPt :
143626f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    case Hexagon::POST_STbri_cNotPt :
143726f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    case Hexagon::STrid_indexed_cPt :
143826f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    case Hexagon::STrid_indexed_cNotPt :
143926f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    case Hexagon::STrid_indexed_shl_cPt_V4 :
144026f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    case Hexagon::POST_STdri_cPt :
144126f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    case Hexagon::POST_STdri_cNotPt :
144226f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    case Hexagon::STrih_cPt :
144326f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    case Hexagon::STrih_cNotPt :
144426f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    case Hexagon::STrih_indexed_cPt :
144526f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    case Hexagon::STrih_indexed_cNotPt :
144626f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    case Hexagon::STrih_imm_cPt_V4 :
144726f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    case Hexagon::STrih_imm_cNotPt_V4 :
144826f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    case Hexagon::STrih_indexed_shl_cPt_V4 :
144926f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    case Hexagon::STrih_indexed_shl_cNotPt_V4 :
145026f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    case Hexagon::POST_SThri_cPt :
145126f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    case Hexagon::POST_SThri_cNotPt :
145226f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    case Hexagon::STriw_cPt :
145326f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    case Hexagon::STriw_cNotPt :
145426f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    case Hexagon::STriw_indexed_cPt :
145526f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    case Hexagon::STriw_indexed_cNotPt :
145626f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    case Hexagon::STriw_imm_cPt_V4 :
145726f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    case Hexagon::STriw_imm_cNotPt_V4 :
145826f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    case Hexagon::STriw_indexed_shl_cPt_V4 :
145926f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    case Hexagon::STriw_indexed_shl_cNotPt_V4 :
146026f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    case Hexagon::POST_STwri_cPt :
146126f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    case Hexagon::POST_STwri_cNotPt :
146226f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande      return QRI.Subtarget.hasV4TOps();
146326f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande
146426f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    // V4 global address store before promoting to dot new.
146526f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    case Hexagon::STd_GP_cPt_V4 :
146626f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    case Hexagon::STd_GP_cNotPt_V4 :
146726f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    case Hexagon::STb_GP_cPt_V4 :
146826f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    case Hexagon::STb_GP_cNotPt_V4 :
146926f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    case Hexagon::STh_GP_cPt_V4 :
147026f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    case Hexagon::STh_GP_cNotPt_V4 :
147126f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    case Hexagon::STw_GP_cPt_V4 :
147226f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    case Hexagon::STw_GP_cNotPt_V4 :
147326f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande      return QRI.Subtarget.hasV4TOps();
147426f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande
147526f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    // Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
147626f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    // from the "Conditional Store" list. Because a predicated new value store
147726f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    // would NOT be promoted to a double dot new store. See diagram below:
147826f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    // This function returns yes for those stores that are predicated but not
147926f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    // yet promoted to predicate dot new instructions.
148026f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    //
148126f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    //                          +---------------------+
148226f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    //                    /-----| if (p0) memw(..)=r0 |---------\~
148326f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    //                   ||     +---------------------+         ||
148426f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    //          promote  ||       /\       /\                   ||  promote
148526f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    //                   ||      /||\     /||\                  ||
148626f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    //                  \||/    demote     ||                  \||/
148726f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    //                   \/       ||       ||                   \/
148826f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    //       +-------------------------+   ||   +-------------------------+
148926f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    //       | if (p0.new) memw(..)=r0 |   ||   | if (p0) memw(..)=r0.new |
149026f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    //       +-------------------------+   ||   +-------------------------+
149126f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    //                        ||           ||         ||
149226f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    //                        ||         demote      \||/
149326f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    //                      promote        ||         \/ NOT possible
149426f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    //                        ||           ||         /\~
149526f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    //                       \||/          ||        /||\~
149626f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    //                        \/           ||         ||
149726f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    //                      +-----------------------------+
149826f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    //                      | if (p0.new) memw(..)=r0.new |
149926f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    //                      +-----------------------------+
150026f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    //                           Double Dot New Store
150126f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande    //
1502d1a87a68064e0b9af3b71b681286954f861bb1b3Sirish Pande  }
1503d1a87a68064e0b9af3b71b681286954f861bb1b3Sirish Pande}
1504d1a87a68064e0b9af3b71b681286954f861bb1b3Sirish Pande
15051a7eab387849feaa3e9c35f145d5b07be54686e3Jyotsna Verma
15061a7eab387849feaa3e9c35f145d5b07be54686e3Jyotsna Vermabool HexagonInstrInfo::isNewValueJump(const MachineInstr *MI) const {
15071a7eab387849feaa3e9c35f145d5b07be54686e3Jyotsna Verma  if (isNewValue(MI) && isBranch(MI))
15081a7eab387849feaa3e9c35f145d5b07be54686e3Jyotsna Verma    return true;
15091a7eab387849feaa3e9c35f145d5b07be54686e3Jyotsna Verma  return false;
15101a7eab387849feaa3e9c35f145d5b07be54686e3Jyotsna Verma}
15111a7eab387849feaa3e9c35f145d5b07be54686e3Jyotsna Verma
151280d81aa8ba923c9f9a953410677ac53c4c2b8318Jyotsna Vermabool HexagonInstrInfo::isPostIncrement (const MachineInstr* MI) const {
151380d81aa8ba923c9f9a953410677ac53c4c2b8318Jyotsna Verma  return (getAddrMode(MI) == HexagonII::PostInc);
151480d81aa8ba923c9f9a953410677ac53c4c2b8318Jyotsna Verma}
151580d81aa8ba923c9f9a953410677ac53c4c2b8318Jyotsna Verma
15161a7eab387849feaa3e9c35f145d5b07be54686e3Jyotsna Vermabool HexagonInstrInfo::isNewValue(const MachineInstr* MI) const {
15171a7eab387849feaa3e9c35f145d5b07be54686e3Jyotsna Verma  const uint64_t F = MI->getDesc().TSFlags;
15181a7eab387849feaa3e9c35f145d5b07be54686e3Jyotsna Verma  return ((F >> HexagonII::NewValuePos) & HexagonII::NewValueMask);
15191a7eab387849feaa3e9c35f145d5b07be54686e3Jyotsna Verma}
15201a7eab387849feaa3e9c35f145d5b07be54686e3Jyotsna Verma
1521810848d5b3bc53747722db0d30a21dc168c5503eJyotsna Verma// Returns true, if any one of the operands is a dot new
1522810848d5b3bc53747722db0d30a21dc168c5503eJyotsna Verma// insn, whether it is predicated dot new or register dot new.
1523810848d5b3bc53747722db0d30a21dc168c5503eJyotsna Vermabool HexagonInstrInfo::isDotNewInst (const MachineInstr* MI) const {
1524810848d5b3bc53747722db0d30a21dc168c5503eJyotsna Verma  return (isNewValueInst(MI) ||
1525810848d5b3bc53747722db0d30a21dc168c5503eJyotsna Verma     (isPredicated(MI) && isPredicatedNew(MI)));
1526810848d5b3bc53747722db0d30a21dc168c5503eJyotsna Verma}
1527810848d5b3bc53747722db0d30a21dc168c5503eJyotsna Verma
1528cdfb55d15bbcdf1533ca26ffcb9c94fa5d225bd0Jyotsna Verma// Returns the most basic instruction for the .new predicated instructions and
1529cdfb55d15bbcdf1533ca26ffcb9c94fa5d225bd0Jyotsna Verma// new-value stores.
1530cdfb55d15bbcdf1533ca26ffcb9c94fa5d225bd0Jyotsna Verma// For example, all of the following instructions will be converted back to the
1531cdfb55d15bbcdf1533ca26ffcb9c94fa5d225bd0Jyotsna Verma// same instruction:
1532cdfb55d15bbcdf1533ca26ffcb9c94fa5d225bd0Jyotsna Verma// 1) if (p0.new) memw(R0+#0) = R1.new  --->
1533cdfb55d15bbcdf1533ca26ffcb9c94fa5d225bd0Jyotsna Verma// 2) if (p0) memw(R0+#0)= R1.new      -------> if (p0) memw(R0+#0) = R1
1534cdfb55d15bbcdf1533ca26ffcb9c94fa5d225bd0Jyotsna Verma// 3) if (p0.new) memw(R0+#0) = R1      --->
1535cdfb55d15bbcdf1533ca26ffcb9c94fa5d225bd0Jyotsna Verma//
1536cdfb55d15bbcdf1533ca26ffcb9c94fa5d225bd0Jyotsna Verma
1537cdfb55d15bbcdf1533ca26ffcb9c94fa5d225bd0Jyotsna Vermaint HexagonInstrInfo::GetDotOldOp(const int opc) const {
1538cdfb55d15bbcdf1533ca26ffcb9c94fa5d225bd0Jyotsna Verma  int NewOp = opc;
1539cdfb55d15bbcdf1533ca26ffcb9c94fa5d225bd0Jyotsna Verma  if (isPredicated(NewOp) && isPredicatedNew(NewOp)) { // Get predicate old form
1540cdfb55d15bbcdf1533ca26ffcb9c94fa5d225bd0Jyotsna Verma    NewOp = Hexagon::getPredOldOpcode(NewOp);
1541cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines    assert(NewOp >= 0 &&
1542cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines           "Couldn't change predicate new instruction to its old form.");
1543cdfb55d15bbcdf1533ca26ffcb9c94fa5d225bd0Jyotsna Verma  }
1544cdfb55d15bbcdf1533ca26ffcb9c94fa5d225bd0Jyotsna Verma
154536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  if (isNewValueStore(NewOp)) { // Convert into non-new-value format
1546cdfb55d15bbcdf1533ca26ffcb9c94fa5d225bd0Jyotsna Verma    NewOp = Hexagon::getNonNVStore(NewOp);
1547cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines    assert(NewOp >= 0 && "Couldn't change new-value store to its old form.");
1548cdfb55d15bbcdf1533ca26ffcb9c94fa5d225bd0Jyotsna Verma  }
1549cdfb55d15bbcdf1533ca26ffcb9c94fa5d225bd0Jyotsna Verma  return NewOp;
1550cdfb55d15bbcdf1533ca26ffcb9c94fa5d225bd0Jyotsna Verma}
1551cdfb55d15bbcdf1533ca26ffcb9c94fa5d225bd0Jyotsna Verma
15521a35b8e2eb165624013d5a2eaf8b673f026999fcJyotsna Verma// Return the new value instruction for a given store.
15531a35b8e2eb165624013d5a2eaf8b673f026999fcJyotsna Vermaint HexagonInstrInfo::GetDotNewOp(const MachineInstr* MI) const {
15541a35b8e2eb165624013d5a2eaf8b673f026999fcJyotsna Verma  int NVOpcode = Hexagon::getNewValueOpcode(MI->getOpcode());
15551a35b8e2eb165624013d5a2eaf8b673f026999fcJyotsna Verma  if (NVOpcode >= 0) // Valid new-value store instruction.
15561a35b8e2eb165624013d5a2eaf8b673f026999fcJyotsna Verma    return NVOpcode;
15571a35b8e2eb165624013d5a2eaf8b673f026999fcJyotsna Verma
15581a35b8e2eb165624013d5a2eaf8b673f026999fcJyotsna Verma  switch (MI->getOpcode()) {
15591a35b8e2eb165624013d5a2eaf8b673f026999fcJyotsna Verma  default: llvm_unreachable("Unknown .new type");
15601a35b8e2eb165624013d5a2eaf8b673f026999fcJyotsna Verma  // store new value byte
15611a35b8e2eb165624013d5a2eaf8b673f026999fcJyotsna Verma  case Hexagon::STrib_shl_V4:
15621a35b8e2eb165624013d5a2eaf8b673f026999fcJyotsna Verma    return Hexagon::STrib_shl_nv_V4;
15631a35b8e2eb165624013d5a2eaf8b673f026999fcJyotsna Verma
15641a35b8e2eb165624013d5a2eaf8b673f026999fcJyotsna Verma  case Hexagon::STrih_shl_V4:
15651a35b8e2eb165624013d5a2eaf8b673f026999fcJyotsna Verma    return Hexagon::STrih_shl_nv_V4;
15661a35b8e2eb165624013d5a2eaf8b673f026999fcJyotsna Verma
15671a35b8e2eb165624013d5a2eaf8b673f026999fcJyotsna Verma  case Hexagon::STriw_f:
15681a35b8e2eb165624013d5a2eaf8b673f026999fcJyotsna Verma    return Hexagon::STriw_nv_V4;
15691a35b8e2eb165624013d5a2eaf8b673f026999fcJyotsna Verma
15701a35b8e2eb165624013d5a2eaf8b673f026999fcJyotsna Verma  case Hexagon::STriw_indexed_f:
15711a35b8e2eb165624013d5a2eaf8b673f026999fcJyotsna Verma    return Hexagon::STriw_indexed_nv_V4;
15721a35b8e2eb165624013d5a2eaf8b673f026999fcJyotsna Verma
15731a35b8e2eb165624013d5a2eaf8b673f026999fcJyotsna Verma  case Hexagon::STriw_shl_V4:
15741a35b8e2eb165624013d5a2eaf8b673f026999fcJyotsna Verma    return Hexagon::STriw_shl_nv_V4;
15751a35b8e2eb165624013d5a2eaf8b673f026999fcJyotsna Verma
15761a35b8e2eb165624013d5a2eaf8b673f026999fcJyotsna Verma  }
15771a35b8e2eb165624013d5a2eaf8b673f026999fcJyotsna Verma  return 0;
15781a35b8e2eb165624013d5a2eaf8b673f026999fcJyotsna Verma}
15791a35b8e2eb165624013d5a2eaf8b673f026999fcJyotsna Verma
158080d81aa8ba923c9f9a953410677ac53c4c2b8318Jyotsna Verma// Return .new predicate version for an instruction.
158180d81aa8ba923c9f9a953410677ac53c4c2b8318Jyotsna Vermaint HexagonInstrInfo::GetDotNewPredOp(MachineInstr *MI,
158280d81aa8ba923c9f9a953410677ac53c4c2b8318Jyotsna Verma                                      const MachineBranchProbabilityInfo
158380d81aa8ba923c9f9a953410677ac53c4c2b8318Jyotsna Verma                                      *MBPI) const {
158480d81aa8ba923c9f9a953410677ac53c4c2b8318Jyotsna Verma
158580d81aa8ba923c9f9a953410677ac53c4c2b8318Jyotsna Verma  int NewOpcode = Hexagon::getPredNewOpcode(MI->getOpcode());
158680d81aa8ba923c9f9a953410677ac53c4c2b8318Jyotsna Verma  if (NewOpcode >= 0) // Valid predicate new instruction
158780d81aa8ba923c9f9a953410677ac53c4c2b8318Jyotsna Verma    return NewOpcode;
158880d81aa8ba923c9f9a953410677ac53c4c2b8318Jyotsna Verma
158980d81aa8ba923c9f9a953410677ac53c4c2b8318Jyotsna Verma  switch (MI->getOpcode()) {
159080d81aa8ba923c9f9a953410677ac53c4c2b8318Jyotsna Verma  default: llvm_unreachable("Unknown .new type");
159180d81aa8ba923c9f9a953410677ac53c4c2b8318Jyotsna Verma  // Condtional Jumps
159280d81aa8ba923c9f9a953410677ac53c4c2b8318Jyotsna Verma  case Hexagon::JMP_t:
159380d81aa8ba923c9f9a953410677ac53c4c2b8318Jyotsna Verma  case Hexagon::JMP_f:
159480d81aa8ba923c9f9a953410677ac53c4c2b8318Jyotsna Verma    return getDotNewPredJumpOp(MI, MBPI);
159580d81aa8ba923c9f9a953410677ac53c4c2b8318Jyotsna Verma
159680d81aa8ba923c9f9a953410677ac53c4c2b8318Jyotsna Verma  case Hexagon::JMPR_t:
159780d81aa8ba923c9f9a953410677ac53c4c2b8318Jyotsna Verma    return Hexagon::JMPR_tnew_tV3;
159880d81aa8ba923c9f9a953410677ac53c4c2b8318Jyotsna Verma
159980d81aa8ba923c9f9a953410677ac53c4c2b8318Jyotsna Verma  case Hexagon::JMPR_f:
160080d81aa8ba923c9f9a953410677ac53c4c2b8318Jyotsna Verma    return Hexagon::JMPR_fnew_tV3;
160180d81aa8ba923c9f9a953410677ac53c4c2b8318Jyotsna Verma
160280d81aa8ba923c9f9a953410677ac53c4c2b8318Jyotsna Verma  case Hexagon::JMPret_t:
160380d81aa8ba923c9f9a953410677ac53c4c2b8318Jyotsna Verma    return Hexagon::JMPret_tnew_tV3;
160480d81aa8ba923c9f9a953410677ac53c4c2b8318Jyotsna Verma
160580d81aa8ba923c9f9a953410677ac53c4c2b8318Jyotsna Verma  case Hexagon::JMPret_f:
160680d81aa8ba923c9f9a953410677ac53c4c2b8318Jyotsna Verma    return Hexagon::JMPret_fnew_tV3;
160780d81aa8ba923c9f9a953410677ac53c4c2b8318Jyotsna Verma
160880d81aa8ba923c9f9a953410677ac53c4c2b8318Jyotsna Verma
160980d81aa8ba923c9f9a953410677ac53c4c2b8318Jyotsna Verma  // Conditional combine
161080d81aa8ba923c9f9a953410677ac53c4c2b8318Jyotsna Verma  case Hexagon::COMBINE_rr_cPt :
161180d81aa8ba923c9f9a953410677ac53c4c2b8318Jyotsna Verma    return Hexagon::COMBINE_rr_cdnPt;
161280d81aa8ba923c9f9a953410677ac53c4c2b8318Jyotsna Verma  case Hexagon::COMBINE_rr_cNotPt :
161380d81aa8ba923c9f9a953410677ac53c4c2b8318Jyotsna Verma    return Hexagon::COMBINE_rr_cdnNotPt;
161480d81aa8ba923c9f9a953410677ac53c4c2b8318Jyotsna Verma  }
161580d81aa8ba923c9f9a953410677ac53c4c2b8318Jyotsna Verma}
161680d81aa8ba923c9f9a953410677ac53c4c2b8318Jyotsna Verma
161780d81aa8ba923c9f9a953410677ac53c4c2b8318Jyotsna Verma
1618ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Vermaunsigned HexagonInstrInfo::getAddrMode(const MachineInstr* MI) const {
1619ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  const uint64_t F = MI->getDesc().TSFlags;
1620ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma
1621ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  return((F >> HexagonII::AddrModePos) & HexagonII::AddrModeMask);
1622ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma}
162326f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande
1624ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma/// immediateExtend - Changes the instruction in place to one using an immediate
1625ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma/// extender.
1626ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Vermavoid HexagonInstrInfo::immediateExtend(MachineInstr *MI) const {
1627ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  assert((isExtendable(MI)||isConstExtended(MI)) &&
1628ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma                               "Instruction must be extendable");
1629ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  // Find which operand is extendable.
1630ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  short ExtOpNum = getCExtOpNum(MI);
1631ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  MachineOperand &MO = MI->getOperand(ExtOpNum);
1632ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  // This needs to be something we understand.
1633ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  assert((MO.isMBB() || MO.isImm()) &&
1634ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma         "Branch with unknown extendable field type");
1635ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  // Mark given operand as extended.
1636ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  MO.addTargetFlag(HexagonII::HMOTF_ConstExtended);
1637ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma}
163826f61a158b3cce69252c05cc0e79f500d6c3d92eSirish Pande
1639ee498d3254b86bceb4f441741e9f442990647ce6Andrew TrickDFAPacketizer *HexagonInstrInfo::
1640ee498d3254b86bceb4f441741e9f442990647ce6Andrew TrickCreateTargetScheduleState(const TargetMachine *TM,
1641ee498d3254b86bceb4f441741e9f442990647ce6Andrew Trick                           const ScheduleDAG *DAG) const {
1642ee498d3254b86bceb4f441741e9f442990647ce6Andrew Trick  const InstrItineraryData *II = TM->getInstrItineraryData();
1643ee498d3254b86bceb4f441741e9f442990647ce6Andrew Trick  return TM->getSubtarget<HexagonGenSubtargetInfo>().createDFAPacketizer(II);
1644ee498d3254b86bceb4f441741e9f442990647ce6Andrew Trick}
1645ee498d3254b86bceb4f441741e9f442990647ce6Andrew Trick
1646ee498d3254b86bceb4f441741e9f442990647ce6Andrew Trickbool HexagonInstrInfo::isSchedulingBoundary(const MachineInstr *MI,
1647ee498d3254b86bceb4f441741e9f442990647ce6Andrew Trick                                            const MachineBasicBlock *MBB,
1648ee498d3254b86bceb4f441741e9f442990647ce6Andrew Trick                                            const MachineFunction &MF) const {
1649ee498d3254b86bceb4f441741e9f442990647ce6Andrew Trick  // Debug info is never a scheduling boundary. It's necessary to be explicit
1650ee498d3254b86bceb4f441741e9f442990647ce6Andrew Trick  // due to the special treatment of IT instructions below, otherwise a
1651ee498d3254b86bceb4f441741e9f442990647ce6Andrew Trick  // dbg_value followed by an IT will result in the IT instruction being
1652ee498d3254b86bceb4f441741e9f442990647ce6Andrew Trick  // considered a scheduling hazard, which is wrong. It should be the actual
1653ee498d3254b86bceb4f441741e9f442990647ce6Andrew Trick  // instruction preceding the dbg_value instruction(s), just like it is
1654ee498d3254b86bceb4f441741e9f442990647ce6Andrew Trick  // when debug info is not present.
1655ee498d3254b86bceb4f441741e9f442990647ce6Andrew Trick  if (MI->isDebugValue())
1656ee498d3254b86bceb4f441741e9f442990647ce6Andrew Trick    return false;
1657ee498d3254b86bceb4f441741e9f442990647ce6Andrew Trick
1658ee498d3254b86bceb4f441741e9f442990647ce6Andrew Trick  // Terminators and labels can't be scheduled around.
165936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  if (MI->getDesc().isTerminator() || MI->isPosition() || MI->isInlineAsm())
1660ee498d3254b86bceb4f441741e9f442990647ce6Andrew Trick    return true;
1661ee498d3254b86bceb4f441741e9f442990647ce6Andrew Trick
1662ee498d3254b86bceb4f441741e9f442990647ce6Andrew Trick  return false;
1663ee498d3254b86bceb4f441741e9f442990647ce6Andrew Trick}
1664ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma
1665ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Vermabool HexagonInstrInfo::isConstExtended(MachineInstr *MI) const {
1666ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma
1667ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  // Constant extenders are allowed only for V4 and above.
1668ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  if (!Subtarget.hasV4TOps())
1669ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma    return false;
1670ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma
1671ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  const uint64_t F = MI->getDesc().TSFlags;
1672ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  unsigned isExtended = (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
1673ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  if (isExtended) // Instruction must be extended.
1674ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma    return true;
1675ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma
1676ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  unsigned isExtendable = (F >> HexagonII::ExtendablePos)
1677ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma                          & HexagonII::ExtendableMask;
1678ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  if (!isExtendable)
1679ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma    return false;
1680ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma
1681ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  short ExtOpNum = getCExtOpNum(MI);
1682ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  const MachineOperand &MO = MI->getOperand(ExtOpNum);
1683ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  // Use MO operand flags to determine if MO
1684ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  // has the HMOTF_ConstExtended flag set.
1685ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  if (MO.getTargetFlags() && HexagonII::HMOTF_ConstExtended)
1686ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma    return true;
1687ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  // If this is a Machine BB address we are talking about, and it is
1688ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  // not marked as extended, say so.
1689ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  if (MO.isMBB())
1690ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma    return false;
1691ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma
1692ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  // We could be using an instruction with an extendable immediate and shoehorn
1693ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  // a global address into it. If it is a global address it will be constant
1694ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  // extended. We do this for COMBINE.
1695ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  // We currently only handle isGlobal() because it is the only kind of
1696ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  // object we are going to end up with here for now.
1697ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  // In the future we probably should add isSymbol(), etc.
1698ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  if (MO.isGlobal() || MO.isSymbol())
1699ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma    return true;
1700ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma
1701ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  // If the extendable operand is not 'Immediate' type, the instruction should
1702ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  // have 'isExtended' flag set.
1703ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  assert(MO.isImm() && "Extendable operand must be Immediate type");
1704ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma
1705ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  int MinValue = getMinValue(MI);
1706ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  int MaxValue = getMaxValue(MI);
1707ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  int ImmValue = MO.getImm();
1708ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma
1709ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  return (ImmValue < MinValue || ImmValue > MaxValue);
1710ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma}
1711ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma
1712f945d09d53a4f2f392b8b51191d37de2f8acd566Jyotsna Verma// Returns the opcode to use when converting MI, which is a conditional jump,
1713f945d09d53a4f2f392b8b51191d37de2f8acd566Jyotsna Verma// into a conditional instruction which uses the .new value of the predicate.
1714f945d09d53a4f2f392b8b51191d37de2f8acd566Jyotsna Verma// We also use branch probabilities to add a hint to the jump.
1715f945d09d53a4f2f392b8b51191d37de2f8acd566Jyotsna Vermaint
1716f945d09d53a4f2f392b8b51191d37de2f8acd566Jyotsna VermaHexagonInstrInfo::getDotNewPredJumpOp(MachineInstr *MI,
1717f945d09d53a4f2f392b8b51191d37de2f8acd566Jyotsna Verma                                  const
1718f945d09d53a4f2f392b8b51191d37de2f8acd566Jyotsna Verma                                  MachineBranchProbabilityInfo *MBPI) const {
1719f945d09d53a4f2f392b8b51191d37de2f8acd566Jyotsna Verma
1720f945d09d53a4f2f392b8b51191d37de2f8acd566Jyotsna Verma  // We assume that block can have at most two successors.
1721f945d09d53a4f2f392b8b51191d37de2f8acd566Jyotsna Verma  bool taken = false;
1722f945d09d53a4f2f392b8b51191d37de2f8acd566Jyotsna Verma  MachineBasicBlock *Src = MI->getParent();
1723f945d09d53a4f2f392b8b51191d37de2f8acd566Jyotsna Verma  MachineOperand *BrTarget = &MI->getOperand(1);
1724f945d09d53a4f2f392b8b51191d37de2f8acd566Jyotsna Verma  MachineBasicBlock *Dst = BrTarget->getMBB();
1725f945d09d53a4f2f392b8b51191d37de2f8acd566Jyotsna Verma
1726f945d09d53a4f2f392b8b51191d37de2f8acd566Jyotsna Verma  const BranchProbability Prediction = MBPI->getEdgeProbability(Src, Dst);
1727f945d09d53a4f2f392b8b51191d37de2f8acd566Jyotsna Verma  if (Prediction >= BranchProbability(1,2))
1728f945d09d53a4f2f392b8b51191d37de2f8acd566Jyotsna Verma    taken = true;
1729f945d09d53a4f2f392b8b51191d37de2f8acd566Jyotsna Verma
1730f945d09d53a4f2f392b8b51191d37de2f8acd566Jyotsna Verma  switch (MI->getOpcode()) {
1731f945d09d53a4f2f392b8b51191d37de2f8acd566Jyotsna Verma  case Hexagon::JMP_t:
1732f945d09d53a4f2f392b8b51191d37de2f8acd566Jyotsna Verma    return taken ? Hexagon::JMP_tnew_t : Hexagon::JMP_tnew_nt;
1733f945d09d53a4f2f392b8b51191d37de2f8acd566Jyotsna Verma  case Hexagon::JMP_f:
1734f945d09d53a4f2f392b8b51191d37de2f8acd566Jyotsna Verma    return taken ? Hexagon::JMP_fnew_t : Hexagon::JMP_fnew_nt;
1735f945d09d53a4f2f392b8b51191d37de2f8acd566Jyotsna Verma
1736f945d09d53a4f2f392b8b51191d37de2f8acd566Jyotsna Verma  default:
1737f945d09d53a4f2f392b8b51191d37de2f8acd566Jyotsna Verma    llvm_unreachable("Unexpected jump instruction.");
1738f945d09d53a4f2f392b8b51191d37de2f8acd566Jyotsna Verma  }
1739f945d09d53a4f2f392b8b51191d37de2f8acd566Jyotsna Verma}
1740ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma// Returns true if a particular operand is extendable for an instruction.
1741ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Vermabool HexagonInstrInfo::isOperandExtended(const MachineInstr *MI,
1742ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma                                         unsigned short OperandNum) const {
1743ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  // Constant extenders are allowed only for V4 and above.
1744ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  if (!Subtarget.hasV4TOps())
1745ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma    return false;
1746ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma
1747ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  const uint64_t F = MI->getDesc().TSFlags;
1748ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma
1749ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask)
1750ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma          == OperandNum;
1751ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma}
1752ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma
1753ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma// Returns Operand Index for the constant extended instruction.
1754ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Vermaunsigned short HexagonInstrInfo::getCExtOpNum(const MachineInstr *MI) const {
1755ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  const uint64_t F = MI->getDesc().TSFlags;
1756ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask);
1757ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma}
1758ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma
1759ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma// Returns the min value that doesn't need to be extended.
1760ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Vermaint HexagonInstrInfo::getMinValue(const MachineInstr *MI) const {
1761ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  const uint64_t F = MI->getDesc().TSFlags;
1762ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
1763ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma                    & HexagonII::ExtentSignedMask;
1764ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  unsigned bits =  (F >> HexagonII::ExtentBitsPos)
1765ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma                    & HexagonII::ExtentBitsMask;
1766ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma
1767ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  if (isSigned) // if value is signed
1768ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma    return -1 << (bits - 1);
1769ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  else
1770ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma    return 0;
1771ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma}
1772ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma
1773ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma// Returns the max value that doesn't need to be extended.
1774ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Vermaint HexagonInstrInfo::getMaxValue(const MachineInstr *MI) const {
1775ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  const uint64_t F = MI->getDesc().TSFlags;
1776ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
1777ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma                    & HexagonII::ExtentSignedMask;
1778ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  unsigned bits =  (F >> HexagonII::ExtentBitsPos)
1779ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma                    & HexagonII::ExtentBitsMask;
1780ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma
1781ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  if (isSigned) // if value is signed
1782ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma    return ~(-1 << (bits - 1));
1783ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  else
1784ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma    return ~(-1 << bits);
1785ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma}
1786ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma
1787ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma// Returns true if an instruction can be converted into a non-extended
1788ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma// equivalent instruction.
1789ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Vermabool HexagonInstrInfo::NonExtEquivalentExists (const MachineInstr *MI) const {
1790ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma
1791ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  short NonExtOpcode;
1792ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  // Check if the instruction has a register form that uses register in place
1793ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  // of the extended operand, if so return that as the non-extended form.
1794ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  if (Hexagon::getRegForm(MI->getOpcode()) >= 0)
1795ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma    return true;
1796ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma
1797ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  if (MI->getDesc().mayLoad() || MI->getDesc().mayStore()) {
179836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    // Check addressing mode and retrieve non-ext equivalent instruction.
1799ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma
1800ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma    switch (getAddrMode(MI)) {
1801ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma    case HexagonII::Absolute :
1802ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma      // Load/store with absolute addressing mode can be converted into
1803ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma      // base+offset mode.
1804ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma      NonExtOpcode = Hexagon::getBasedWithImmOffset(MI->getOpcode());
1805ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma      break;
1806ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma    case HexagonII::BaseImmOffset :
1807ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma      // Load/store with base+offset addressing mode can be converted into
1808ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma      // base+register offset addressing mode. However left shift operand should
1809ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma      // be set to 0.
1810ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma      NonExtOpcode = Hexagon::getBaseWithRegOffset(MI->getOpcode());
1811ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma      break;
1812ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma    default:
1813ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma      return false;
1814ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma    }
1815ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma    if (NonExtOpcode < 0)
1816ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma      return false;
1817ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma    return true;
1818ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  }
1819ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  return false;
1820ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma}
1821ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma
1822ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma// Returns opcode of the non-extended equivalent instruction.
1823ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Vermashort HexagonInstrInfo::getNonExtOpcode (const MachineInstr *MI) const {
1824ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma
1825ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  // Check if the instruction has a register form that uses register in place
1826ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  // of the extended operand, if so return that as the non-extended form.
1827ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  short NonExtOpcode = Hexagon::getRegForm(MI->getOpcode());
1828ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma    if (NonExtOpcode >= 0)
1829ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma      return NonExtOpcode;
1830ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma
1831ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  if (MI->getDesc().mayLoad() || MI->getDesc().mayStore()) {
183236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    // Check addressing mode and retrieve non-ext equivalent instruction.
1833ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma    switch (getAddrMode(MI)) {
1834ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma    case HexagonII::Absolute :
1835ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma      return Hexagon::getBasedWithImmOffset(MI->getOpcode());
1836ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma    case HexagonII::BaseImmOffset :
1837ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma      return Hexagon::getBaseWithRegOffset(MI->getOpcode());
1838ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma    default:
1839ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma      return -1;
1840ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma    }
1841ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  }
1842ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma  return -1;
1843ef94c6c85eed42d16fde51ee4415d27bb281a2caJyotsna Verma}
18446ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma
18456ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Vermabool HexagonInstrInfo::PredOpcodeHasJMP_c(Opcode_t Opcode) const {
18466ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma  return (Opcode == Hexagon::JMP_t) ||
18476ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma         (Opcode == Hexagon::JMP_f) ||
18486ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma         (Opcode == Hexagon::JMP_tnew_t) ||
18496ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma         (Opcode == Hexagon::JMP_fnew_t) ||
18506ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma         (Opcode == Hexagon::JMP_tnew_nt) ||
18516ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma         (Opcode == Hexagon::JMP_fnew_nt);
18526ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma}
18536ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma
18546ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Vermabool HexagonInstrInfo::PredOpcodeHasNot(Opcode_t Opcode) const {
18556ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma  return (Opcode == Hexagon::JMP_f) ||
18566ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma         (Opcode == Hexagon::JMP_fnew_t) ||
18576ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma         (Opcode == Hexagon::JMP_fnew_nt);
18586ea706e40e841c148bc47bcac4dcca5e0f0245ebJyotsna Verma}
1859