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/HexagonMCInstrInfo.h"
17 #include "MCTargetDesc/HexagonMCTargetDesc.h"
21 HexagonMCInst::HexagonMCInst() : MCII (createHexagonMCInstrInfo ()) {}
22 HexagonMCInst::HexagonMCInst(MCInstrDesc const &mcid) :
23 MCII (createHexagonMCInstrInfo ()){}
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 // Return the Hexagon ISA class for the insn.
69 unsigned HexagonMCInst::getType() const {
70 const uint64_t F = HexagonMCInstrInfo::getDesc(*MCII, *this).TSFlags;
72 return ((F >> HexagonII::TypePos) & HexagonII::TypeMask);
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);
81 // Return whether the insn is a prefix.
82 bool HexagonMCInst::isPrefix() const {
83 return (getType() == HexagonII::TypePREFIX);
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);
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);
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);
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;
108 (F >> HexagonII::NewValueOpPos) & HexagonII::NewValueOpMask;
109 const MCOperand &MCO = getOperand(O);
111 assert((isNewValue() || hasNewValue()) && MCO.isReg());
115 // Return whether the instruction needs to be constant extended.
116 // 1) Always return true if the instruction has 'isExtended' flag set.
119 // 2) For immediate extended operands, return true only if the value is
121 // 3) For global address, always return true.
123 bool HexagonMCInst::isConstExtended(void) const {
130 short ExtOpNum = getCExtOpNum();
131 int MinValue = getMinValue();
132 int MaxValue = HexagonMCInstrInfo::getMaxValue(*MCII, *this);
133 const MCOperand &MO = getOperand(ExtOpNum);
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.
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");
148 int ImmValue = MO.getImm();
149 return (ImmValue < MinValue || ImmValue > MaxValue);
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;
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;
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);
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);
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) ==
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;
188 (F >> HexagonII::ExtentSignedPos) & HexagonII::ExtentSignedMask;
189 unsigned bits = (F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask;
191 if (isSigned) // if value is signed
192 return -1U << (bits - 1);