[Hexagon] Removing static variable holding MCInstrInfo.
[oota-llvm.git] / lib / Target / Hexagon / MCTargetDesc / HexagonMCInst.cpp
1 //===- HexagonMCInst.cpp - Hexagon sub-class of MCInst --------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This class extends MCInst to allow some Hexagon VLIW annotations.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "MCTargetDesc/HexagonBaseInfo.h"
15 #include "MCTargetDesc/HexagonMCInst.h"
16 #include "MCTargetDesc/HexagonMCTargetDesc.h"
17
18 using namespace llvm;
19
20 HexagonMCInst::HexagonMCInst() : MCII (createHexagonMCInstrInfo ()) {}
21 HexagonMCInst::HexagonMCInst(MCInstrDesc const &mcid) :
22   MCII (createHexagonMCInstrInfo ()){}
23
24 void HexagonMCInst::AppendImplicitOperands(MCInst &MCI) {
25   MCI.addOperand(MCOperand::CreateImm(0));
26   MCI.addOperand(MCOperand::CreateInst(nullptr));
27 }
28
29 std::bitset<16> HexagonMCInst::GetImplicitBits(MCInst const &MCI) {
30   SanityCheckImplicitOperands(MCI);
31   std::bitset<16> Bits(MCI.getOperand(MCI.getNumOperands() - 2).getImm());
32   return Bits;
33 }
34
35 void HexagonMCInst::SetImplicitBits(MCInst &MCI, std::bitset<16> Bits) {
36   SanityCheckImplicitOperands(MCI);
37   MCI.getOperand(MCI.getNumOperands() - 2).setImm(Bits.to_ulong());
38 }
39
40 void HexagonMCInst::setPacketBegin(bool f) {
41   std::bitset<16> Bits(GetImplicitBits(*this));
42   Bits.set(packetBeginIndex, f);
43   SetImplicitBits(*this, Bits);
44 }
45
46 bool HexagonMCInst::isPacketBegin() const {
47   std::bitset<16> Bits(GetImplicitBits(*this));
48   return Bits.test(packetBeginIndex);
49 }
50
51 void HexagonMCInst::setPacketEnd(bool f) {
52   std::bitset<16> Bits(GetImplicitBits(*this));
53   Bits.set(packetEndIndex, f);
54   SetImplicitBits(*this, Bits);
55 }
56
57 bool HexagonMCInst::isPacketEnd() const {
58   std::bitset<16> Bits(GetImplicitBits(*this));
59   return Bits.test(packetEndIndex);
60 }
61
62 void HexagonMCInst::resetPacket() {
63   setPacketBegin(false);
64   setPacketEnd(false);
65 }
66
67 MCInstrDesc const &HexagonMCInst::getDesc() const {
68   return (MCII->get(getOpcode()));
69 }
70
71 // Return the Hexagon ISA class for the insn.
72 unsigned HexagonMCInst::getType() const {
73   const uint64_t F = getDesc().TSFlags;
74
75   return ((F >> HexagonII::TypePos) & HexagonII::TypeMask);
76 }
77
78 // Return whether the insn is an actual insn.
79 bool HexagonMCInst::isCanon() const {
80   return (!getDesc().isPseudo() && !isPrefix() &&
81           getType() != HexagonII::TypeENDLOOP);
82 }
83
84 // Return whether the insn is a prefix.
85 bool HexagonMCInst::isPrefix() const {
86   return (getType() == HexagonII::TypePREFIX);
87 }
88
89 // Return whether the insn is solo, i.e., cannot be in a packet.
90 bool HexagonMCInst::isSolo() const {
91   const uint64_t F = getDesc().TSFlags;
92   return ((F >> HexagonII::SoloPos) & HexagonII::SoloMask);
93 }
94
95 // Return whether the insn is a new-value consumer.
96 bool HexagonMCInst::isNewValue() const {
97   const uint64_t F = getDesc().TSFlags;
98   return ((F >> HexagonII::NewValuePos) & HexagonII::NewValueMask);
99 }
100
101 // Return whether the instruction is a legal new-value producer.
102 bool HexagonMCInst::hasNewValue() const {
103   const uint64_t F = getDesc().TSFlags;
104   return ((F >> HexagonII::hasNewValuePos) & HexagonII::hasNewValueMask);
105 }
106
107 // Return the operand that consumes or produces a new value.
108 const MCOperand &HexagonMCInst::getNewValue() const {
109   const uint64_t F = getDesc().TSFlags;
110   const unsigned O =
111       (F >> HexagonII::NewValueOpPos) & HexagonII::NewValueOpMask;
112   const MCOperand &MCO = getOperand(O);
113
114   assert((isNewValue() || hasNewValue()) && MCO.isReg());
115   return (MCO);
116 }
117
118 // Return whether the instruction needs to be constant extended.
119 // 1) Always return true if the instruction has 'isExtended' flag set.
120 //
121 // isExtendable:
122 // 2) For immediate extended operands, return true only if the value is
123 //    out-of-range.
124 // 3) For global address, always return true.
125
126 bool HexagonMCInst::isConstExtended(void) const {
127   if (isExtended())
128     return true;
129
130   if (!isExtendable())
131     return false;
132
133   short ExtOpNum = getCExtOpNum();
134   int MinValue = getMinValue();
135   int MaxValue = getMaxValue();
136   const MCOperand &MO = getOperand(ExtOpNum);
137
138   // We could be using an instruction with an extendable immediate and shoehorn
139   // a global address into it. If it is a global address it will be constant
140   // extended. We do this for COMBINE.
141   // We currently only handle isGlobal() because it is the only kind of
142   // object we are going to end up with here for now.
143   // In the future we probably should add isSymbol(), etc.
144   if (MO.isExpr())
145     return true;
146
147   // If the extendable operand is not 'Immediate' type, the instruction should
148   // have 'isExtended' flag set.
149   assert(MO.isImm() && "Extendable operand must be Immediate type");
150
151   int ImmValue = MO.getImm();
152   return (ImmValue < MinValue || ImmValue > MaxValue);
153 }
154
155 // Return whether the instruction must be always extended.
156 bool HexagonMCInst::isExtended(void) const {
157   const uint64_t F = getDesc().TSFlags;
158   return (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
159 }
160
161 // Return true if the instruction may be extended based on the operand value.
162 bool HexagonMCInst::isExtendable(void) const {
163   const uint64_t F = getDesc().TSFlags;
164   return (F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask;
165 }
166
167 // Return number of bits in the constant extended operand.
168 unsigned HexagonMCInst::getBitCount(void) const {
169   const uint64_t F = getDesc().TSFlags;
170   return ((F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask);
171 }
172
173 // Return constant extended operand number.
174 unsigned short HexagonMCInst::getCExtOpNum(void) const {
175   const uint64_t F = getDesc().TSFlags;
176   return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask);
177 }
178
179 // Return whether the operand can be constant extended.
180 bool HexagonMCInst::isOperandExtended(const unsigned short OperandNum) const {
181   const uint64_t F = getDesc().TSFlags;
182   return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask) ==
183          OperandNum;
184 }
185
186 // Return the min value that a constant extendable operand can have
187 // without being extended.
188 int HexagonMCInst::getMinValue(void) const {
189   const uint64_t F = getDesc().TSFlags;
190   unsigned isSigned =
191       (F >> HexagonII::ExtentSignedPos) & HexagonII::ExtentSignedMask;
192   unsigned bits = (F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask;
193
194   if (isSigned) // if value is signed
195     return -1U << (bits - 1);
196   else
197     return 0;
198 }
199
200 // Return the max value that a constant extendable operand can have
201 // without being extended.
202 int HexagonMCInst::getMaxValue(void) const {
203   const uint64_t F = getDesc().TSFlags;
204   unsigned isSigned =
205       (F >> HexagonII::ExtentSignedPos) & HexagonII::ExtentSignedMask;
206   unsigned bits = (F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask;
207
208   if (isSigned) // if value is signed
209     return ~(-1U << (bits - 1));
210   else
211     return ~(-1U << bits);
212 }