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 std::unique_ptr <MCInstrInfo const> HexagonMCInst::MCII;
22 HexagonMCInst::HexagonMCInst() : MCInst() {}
23 HexagonMCInst::HexagonMCInst(MCInstrDesc const &mcid) : MCInst() {}
25 void HexagonMCInst::AppendImplicitOperands(MCInst &MCI) {
26 MCI.addOperand(MCOperand::CreateImm(0));
27 MCI.addOperand(MCOperand::CreateInst(nullptr));
30 std::bitset<16> HexagonMCInst::GetImplicitBits(MCInst const &MCI) {
31 SanityCheckImplicitOperands(MCI);
32 std::bitset<16> Bits(MCI.getOperand(MCI.getNumOperands() - 2).getImm());
36 void HexagonMCInst::SetImplicitBits(MCInst &MCI, std::bitset<16> Bits) {
37 SanityCheckImplicitOperands(MCI);
38 MCI.getOperand(MCI.getNumOperands() - 2).setImm(Bits.to_ulong());
41 void HexagonMCInst::setPacketBegin(bool f) {
42 std::bitset<16> Bits(GetImplicitBits(*this));
43 Bits.set(packetBeginIndex, f);
44 SetImplicitBits(*this, Bits);
47 bool HexagonMCInst::isPacketBegin() const {
48 std::bitset<16> Bits(GetImplicitBits(*this));
49 return Bits.test(packetBeginIndex);
52 void HexagonMCInst::setPacketEnd(bool f) {
53 std::bitset<16> Bits(GetImplicitBits(*this));
54 Bits.set(packetEndIndex, f);
55 SetImplicitBits(*this, Bits);
58 bool HexagonMCInst::isPacketEnd() const {
59 std::bitset<16> Bits(GetImplicitBits(*this));
60 return Bits.test(packetEndIndex);
63 void HexagonMCInst::resetPacket() {
64 setPacketBegin(false);
68 MCInstrDesc const &HexagonMCInst::getDesc() const {
69 return (MCII->get(getOpcode()));
72 MCInstrDesc const &HexagonMCInst::getDesc() const {
73 return (MCII->get(getOpcode()));
76 // Return the Hexagon ISA class for the insn.
77 unsigned HexagonMCInst::getType() const {
78 const uint64_t F = getDesc().TSFlags;
80 return ((F >> HexagonII::TypePos) & HexagonII::TypeMask);
83 // Return whether the insn is an actual insn.
84 bool HexagonMCInst::isCanon() const {
85 return (!getDesc().isPseudo() && !isPrefix() &&
86 getType() != HexagonII::TypeENDLOOP);
89 // Return whether the insn is a prefix.
90 bool HexagonMCInst::isPrefix() const {
91 return (getType() == HexagonII::TypePREFIX);
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);
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);
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);
112 // Return the operand that consumes or produces a new value.
113 const MCOperand &HexagonMCInst::getNewValue() const {
114 const uint64_t F = getDesc().TSFlags;
116 (F >> HexagonII::NewValueOpPos) & HexagonII::NewValueOpMask;
117 const MCOperand &MCO = getOperand(O);
119 assert((isNewValue() || hasNewValue()) && MCO.isReg());
123 // Return whether the instruction needs to be constant extended.
124 // 1) Always return true if the instruction has 'isExtended' flag set.
127 // 2) For immediate extended operands, return true only if the value is
129 // 3) For global address, always return true.
131 bool HexagonMCInst::isConstExtended(void) const {
138 short ExtOpNum = getCExtOpNum();
139 int MinValue = getMinValue();
140 int MaxValue = getMaxValue();
141 const MCOperand &MO = getOperand(ExtOpNum);
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.
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");
156 int ImmValue = MO.getImm();
157 return (ImmValue < MinValue || ImmValue > MaxValue);
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;
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;
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);
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);
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) ==
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;
196 (F >> HexagonII::ExtentSignedPos) & HexagonII::ExtentSignedMask;
197 unsigned bits = (F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask;
199 if (isSigned) // if value is signed
200 return -1U << (bits - 1);
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;
210 (F >> HexagonII::ExtentSignedPos) & HexagonII::ExtentSignedMask;
211 unsigned bits = (F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask;
213 if (isSigned) // if value is signed
214 return ~(-1U << (bits - 1));
216 return ~(-1U << bits);