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