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 "HexagonInstrInfo.h"
15 #include "MCTargetDesc/HexagonBaseInfo.h"
16 #include "MCTargetDesc/HexagonMCInst.h"
17 #include "MCTargetDesc/HexagonMCTargetDesc.h"
21 std::unique_ptr <MCInstrInfo const> HexagonMCInst::MCII;
23 HexagonMCInst::HexagonMCInst() : MCInst() {}
24 HexagonMCInst::HexagonMCInst(MCInstrDesc const &mcid) : MCInst() {}
26 void HexagonMCInst::AppendImplicitOperands(MCInst &MCI) {
27 MCI.addOperand(MCOperand::CreateImm(0));
28 MCI.addOperand(MCOperand::CreateInst(nullptr));
31 std::bitset<16> HexagonMCInst::GetImplicitBits(MCInst const &MCI) {
32 SanityCheckImplicitOperands(MCI);
33 std::bitset<16> Bits(MCI.getOperand(MCI.getNumOperands() - 2).getImm());
37 void HexagonMCInst::SetImplicitBits(MCInst &MCI, std::bitset<16> Bits) {
38 SanityCheckImplicitOperands(MCI);
39 MCI.getOperand(MCI.getNumOperands() - 2).setImm(Bits.to_ulong());
42 void HexagonMCInst::setPacketBegin(bool f) {
43 std::bitset<16> Bits(GetImplicitBits(*this));
44 Bits.set(packetBeginIndex, f);
45 SetImplicitBits(*this, Bits);
48 bool HexagonMCInst::isPacketBegin() const {
49 std::bitset<16> Bits(GetImplicitBits(*this));
50 return Bits.test(packetBeginIndex);
53 void HexagonMCInst::setPacketEnd(bool f) {
54 std::bitset<16> Bits(GetImplicitBits(*this));
55 Bits.set(packetEndIndex, f);
56 SetImplicitBits(*this, Bits);
59 bool HexagonMCInst::isPacketEnd() const {
60 std::bitset<16> Bits(GetImplicitBits(*this));
61 return Bits.test(packetEndIndex);
64 void HexagonMCInst::resetPacket() {
65 setPacketBegin(false);
69 // Return the slots used by the insn.
70 unsigned HexagonMCInst::getUnits(const HexagonTargetMachine *TM) const {
71 const HexagonInstrInfo *QII = TM->getSubtargetImpl()->getInstrInfo();
72 const InstrItineraryData *II =
73 TM->getSubtargetImpl()->getInstrItineraryData();
74 const InstrStage *IS =
75 II->beginStage(QII->get(this->getOpcode()).getSchedClass());
77 return (IS->getUnits());
80 MCInstrDesc const& HexagonMCInst::getDesc() const { return (MCII->get(getOpcode())); }
82 // Return the Hexagon ISA class for the insn.
83 unsigned HexagonMCInst::getType() const {
84 const uint64_t F = getDesc().TSFlags;
86 return ((F >> HexagonII::TypePos) & HexagonII::TypeMask);
89 // Return whether the insn is an actual insn.
90 bool HexagonMCInst::isCanon() const {
91 return (!getDesc().isPseudo() && !isPrefix() &&
92 getType() != HexagonII::TypeENDLOOP);
95 // Return whether the insn is a prefix.
96 bool HexagonMCInst::isPrefix() const {
97 return (getType() == HexagonII::TypePREFIX);
100 // Return whether the insn is solo, i.e., cannot be in a packet.
101 bool HexagonMCInst::isSolo() const {
102 const uint64_t F = getDesc().TSFlags;
103 return ((F >> HexagonII::SoloPos) & HexagonII::SoloMask);
106 // Return whether the insn is a new-value consumer.
107 bool HexagonMCInst::isNewValue() const {
108 const uint64_t F = getDesc().TSFlags;
109 return ((F >> HexagonII::NewValuePos) & HexagonII::NewValueMask);
112 // Return whether the instruction is a legal new-value producer.
113 bool HexagonMCInst::hasNewValue() const {
114 const uint64_t F = getDesc().TSFlags;
115 return ((F >> HexagonII::hasNewValuePos) & HexagonII::hasNewValueMask);
118 // Return the operand that consumes or produces a new value.
119 const MCOperand &HexagonMCInst::getNewValue() const {
120 const uint64_t F = getDesc().TSFlags;
122 (F >> HexagonII::NewValueOpPos) & HexagonII::NewValueOpMask;
123 const MCOperand &MCO = getOperand(O);
125 assert((isNewValue() || hasNewValue()) && MCO.isReg());
129 // Return whether the instruction needs to be constant extended.
130 // 1) Always return true if the instruction has 'isExtended' flag set.
133 // 2) For immediate extended operands, return true only if the value is
135 // 3) For global address, always return true.
137 bool HexagonMCInst::isConstExtended(void) const {
144 short ExtOpNum = getCExtOpNum();
145 int MinValue = getMinValue();
146 int MaxValue = getMaxValue();
147 const MCOperand &MO = getOperand(ExtOpNum);
149 // We could be using an instruction with an extendable immediate and shoehorn
150 // a global address into it. If it is a global address it will be constant
151 // extended. We do this for COMBINE.
152 // We currently only handle isGlobal() because it is the only kind of
153 // object we are going to end up with here for now.
154 // In the future we probably should add isSymbol(), etc.
158 // If the extendable operand is not 'Immediate' type, the instruction should
159 // have 'isExtended' flag set.
160 assert(MO.isImm() && "Extendable operand must be Immediate type");
162 int ImmValue = MO.getImm();
163 return (ImmValue < MinValue || ImmValue > MaxValue);
166 // Return whether the instruction must be always extended.
167 bool HexagonMCInst::isExtended(void) const {
168 const uint64_t F = getDesc().TSFlags;
169 return (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
172 // Return true if the instruction may be extended based on the operand value.
173 bool HexagonMCInst::isExtendable(void) const {
174 const uint64_t F = getDesc().TSFlags;
175 return (F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask;
178 // Return number of bits in the constant extended operand.
179 unsigned HexagonMCInst::getBitCount(void) const {
180 const uint64_t F = getDesc().TSFlags;
181 return ((F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask);
184 // Return constant extended operand number.
185 unsigned short HexagonMCInst::getCExtOpNum(void) const {
186 const uint64_t F = getDesc().TSFlags;
187 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask);
190 // Return whether the operand can be constant extended.
191 bool HexagonMCInst::isOperandExtended(const unsigned short OperandNum) const {
192 const uint64_t F = getDesc().TSFlags;
193 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask) ==
197 // Return the min value that a constant extendable operand can have
198 // without being extended.
199 int HexagonMCInst::getMinValue(void) const {
200 const uint64_t F = getDesc().TSFlags;
202 (F >> HexagonII::ExtentSignedPos) & HexagonII::ExtentSignedMask;
203 unsigned bits = (F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask;
205 if (isSigned) // if value is signed
206 return -1U << (bits - 1);
211 // Return the max value that a constant extendable operand can have
212 // without being extended.
213 int HexagonMCInst::getMaxValue(void) const {
214 const uint64_t F = getDesc().TSFlags;
216 (F >> HexagonII::ExtentSignedPos) & HexagonII::ExtentSignedMask;
217 unsigned bits = (F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask;
219 if (isSigned) // if value is signed
220 return ~(-1U << (bits - 1));
222 return ~(-1U << bits);