[Hexagon] Creating HexagonMCInstrInfo namespace as landing zone for static functions...
[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/HexagonMCInstrInfo.h"
17 #include "MCTargetDesc/HexagonMCTargetDesc.h"
18
19 using namespace llvm;
20
21 HexagonMCInst::HexagonMCInst() : MCII (createHexagonMCInstrInfo ()) {}
22 HexagonMCInst::HexagonMCInst(MCInstrDesc const &mcid) :
23   MCII (createHexagonMCInstrInfo ()){}
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 // Return the Hexagon ISA class for the insn.
69 unsigned HexagonMCInst::getType() const {
70   const uint64_t F = HexagonMCInstrInfo::getDesc(*MCII, *this).TSFlags;
71
72   return ((F >> HexagonII::TypePos) & HexagonII::TypeMask);
73 }
74
75 // Return whether the insn is an actual insn.
76 bool HexagonMCInst::isCanon() const {
77   return (!HexagonMCInstrInfo::getDesc(*MCII, *this).isPseudo() &&
78           !isPrefix() && getType() != HexagonII::TypeENDLOOP);
79 }
80
81 // Return whether the insn is a prefix.
82 bool HexagonMCInst::isPrefix() const {
83   return (getType() == HexagonII::TypePREFIX);
84 }
85
86 // Return whether the insn is solo, i.e., cannot be in a packet.
87 bool HexagonMCInst::isSolo() const {
88   const uint64_t F = HexagonMCInstrInfo::getDesc(*MCII, *this).TSFlags;
89   return ((F >> HexagonII::SoloPos) & HexagonII::SoloMask);
90 }
91
92 // Return whether the insn is a new-value consumer.
93 bool HexagonMCInst::isNewValue() const {
94   const uint64_t F = HexagonMCInstrInfo::getDesc(*MCII, *this).TSFlags;
95   return ((F >> HexagonII::NewValuePos) & HexagonII::NewValueMask);
96 }
97
98 // Return whether the instruction is a legal new-value producer.
99 bool HexagonMCInst::hasNewValue() const {
100   const uint64_t F = HexagonMCInstrInfo::getDesc(*MCII, *this).TSFlags;
101   return ((F >> HexagonII::hasNewValuePos) & HexagonII::hasNewValueMask);
102 }
103
104 // Return the operand that consumes or produces a new value.
105 const MCOperand &HexagonMCInst::getNewValue() const {
106   const uint64_t F = HexagonMCInstrInfo::getDesc(*MCII, *this).TSFlags;
107   const unsigned O =
108       (F >> HexagonII::NewValueOpPos) & HexagonII::NewValueOpMask;
109   const MCOperand &MCO = getOperand(O);
110
111   assert((isNewValue() || hasNewValue()) && MCO.isReg());
112   return (MCO);
113 }
114
115 // Return whether the instruction needs to be constant extended.
116 // 1) Always return true if the instruction has 'isExtended' flag set.
117 //
118 // isExtendable:
119 // 2) For immediate extended operands, return true only if the value is
120 //    out-of-range.
121 // 3) For global address, always return true.
122
123 bool HexagonMCInst::isConstExtended(void) const {
124   if (isExtended())
125     return true;
126
127   if (!isExtendable())
128     return false;
129
130   short ExtOpNum = getCExtOpNum();
131   int MinValue = getMinValue();
132   int MaxValue = HexagonMCInstrInfo::getMaxValue(*MCII, *this);
133   const MCOperand &MO = getOperand(ExtOpNum);
134
135   // We could be using an instruction with an extendable immediate and shoehorn
136   // a global address into it. If it is a global address it will be constant
137   // extended. We do this for COMBINE.
138   // We currently only handle isGlobal() because it is the only kind of
139   // object we are going to end up with here for now.
140   // In the future we probably should add isSymbol(), etc.
141   if (MO.isExpr())
142     return true;
143
144   // If the extendable operand is not 'Immediate' type, the instruction should
145   // have 'isExtended' flag set.
146   assert(MO.isImm() && "Extendable operand must be Immediate type");
147
148   int ImmValue = MO.getImm();
149   return (ImmValue < MinValue || ImmValue > MaxValue);
150 }
151
152 // Return whether the instruction must be always extended.
153 bool HexagonMCInst::isExtended(void) const {
154   const uint64_t F = HexagonMCInstrInfo::getDesc(*MCII, *this).TSFlags;
155   return (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
156 }
157
158 // Return true if the instruction may be extended based on the operand value.
159 bool HexagonMCInst::isExtendable(void) const {
160   const uint64_t F = HexagonMCInstrInfo::getDesc(*MCII, *this).TSFlags;
161   return (F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask;
162 }
163
164 // Return number of bits in the constant extended operand.
165 unsigned HexagonMCInst::getBitCount(void) const {
166   const uint64_t F = HexagonMCInstrInfo::getDesc(*MCII, *this).TSFlags;
167   return ((F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask);
168 }
169
170 // Return constant extended operand number.
171 unsigned short HexagonMCInst::getCExtOpNum(void) const {
172   const uint64_t F = HexagonMCInstrInfo::getDesc(*MCII, *this).TSFlags;
173   return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask);
174 }
175
176 // Return whether the operand can be constant extended.
177 bool HexagonMCInst::isOperandExtended(const unsigned short OperandNum) const {
178   const uint64_t F = HexagonMCInstrInfo::getDesc(*MCII, *this).TSFlags;
179   return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask) ==
180          OperandNum;
181 }
182
183 // Return the min value that a constant extendable operand can have
184 // without being extended.
185 int HexagonMCInst::getMinValue(void) const {
186   const uint64_t F = HexagonMCInstrInfo::getDesc(*MCII, *this).TSFlags;
187   unsigned isSigned =
188       (F >> HexagonII::ExtentSignedPos) & HexagonII::ExtentSignedMask;
189   unsigned bits = (F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask;
190
191   if (isSigned) // if value is signed
192     return -1U << (bits - 1);
193   else
194     return 0;
195 }