1 //===- HexagonMCInst.cpp - Hexagon sub-class of MCInst --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This class extends MCInst to allow some Hexagon VLIW annotations.
12 //===----------------------------------------------------------------------===//
14 #include "MCTargetDesc/HexagonBaseInfo.h"
15 #include "MCTargetDesc/HexagonMCInst.h"
16 #include "MCTargetDesc/HexagonMCTargetDesc.h"
20 HexagonMCInst::HexagonMCInst() : MCII (createHexagonMCInstrInfo ()) {}
21 HexagonMCInst::HexagonMCInst(MCInstrDesc const &mcid) :
22 MCII (createHexagonMCInstrInfo ()){}
24 void HexagonMCInst::AppendImplicitOperands(MCInst &MCI) {
25 MCI.addOperand(MCOperand::CreateImm(0));
26 MCI.addOperand(MCOperand::CreateInst(nullptr));
29 std::bitset<16> HexagonMCInst::GetImplicitBits(MCInst const &MCI) {
30 SanityCheckImplicitOperands(MCI);
31 std::bitset<16> Bits(MCI.getOperand(MCI.getNumOperands() - 2).getImm());
35 void HexagonMCInst::SetImplicitBits(MCInst &MCI, std::bitset<16> Bits) {
36 SanityCheckImplicitOperands(MCI);
37 MCI.getOperand(MCI.getNumOperands() - 2).setImm(Bits.to_ulong());
40 void HexagonMCInst::setPacketBegin(bool f) {
41 std::bitset<16> Bits(GetImplicitBits(*this));
42 Bits.set(packetBeginIndex, f);
43 SetImplicitBits(*this, Bits);
46 bool HexagonMCInst::isPacketBegin() const {
47 std::bitset<16> Bits(GetImplicitBits(*this));
48 return Bits.test(packetBeginIndex);
51 void HexagonMCInst::setPacketEnd(bool f) {
52 std::bitset<16> Bits(GetImplicitBits(*this));
53 Bits.set(packetEndIndex, f);
54 SetImplicitBits(*this, Bits);
57 bool HexagonMCInst::isPacketEnd() const {
58 std::bitset<16> Bits(GetImplicitBits(*this));
59 return Bits.test(packetEndIndex);
62 void HexagonMCInst::resetPacket() {
63 setPacketBegin(false);
67 MCInstrDesc const &HexagonMCInst::getDesc() const {
68 return (MCII->get(getOpcode()));
71 // Return the Hexagon ISA class for the insn.
72 unsigned HexagonMCInst::getType() const {
73 const uint64_t F = getDesc().TSFlags;
75 return ((F >> HexagonII::TypePos) & HexagonII::TypeMask);
78 // Return whether the insn is an actual insn.
79 bool HexagonMCInst::isCanon() const {
80 return (!getDesc().isPseudo() && !isPrefix() &&
81 getType() != HexagonII::TypeENDLOOP);
84 // Return whether the insn is a prefix.
85 bool HexagonMCInst::isPrefix() const {
86 return (getType() == HexagonII::TypePREFIX);
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);
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);
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);
107 // Return the operand that consumes or produces a new value.
108 const MCOperand &HexagonMCInst::getNewValue() const {
109 const uint64_t F = getDesc().TSFlags;
111 (F >> HexagonII::NewValueOpPos) & HexagonII::NewValueOpMask;
112 const MCOperand &MCO = getOperand(O);
114 assert((isNewValue() || hasNewValue()) && MCO.isReg());
118 // Return whether the instruction needs to be constant extended.
119 // 1) Always return true if the instruction has 'isExtended' flag set.
122 // 2) For immediate extended operands, return true only if the value is
124 // 3) For global address, always return true.
126 bool HexagonMCInst::isConstExtended(void) const {
133 short ExtOpNum = getCExtOpNum();
134 int MinValue = getMinValue();
135 int MaxValue = getMaxValue();
136 const MCOperand &MO = getOperand(ExtOpNum);
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.
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");
151 int ImmValue = MO.getImm();
152 return (ImmValue < MinValue || ImmValue > MaxValue);
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;
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;
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);
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);
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) ==
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;
191 (F >> HexagonII::ExtentSignedPos) & HexagonII::ExtentSignedMask;
192 unsigned bits = (F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask;
194 if (isSigned) // if value is signed
195 return -1U << (bits - 1);
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;
205 (F >> HexagonII::ExtentSignedPos) & HexagonII::ExtentSignedMask;
206 unsigned bits = (F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask;
208 if (isSigned) // if value is signed
209 return ~(-1U << (bits - 1));
211 return ~(-1U << bits);