1 //===- HexagonMCInstrInfo.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 MCInstrInfo to allow Hexagon specific MCInstr queries
12 //===----------------------------------------------------------------------===//
14 #include "HexagonMCInstrInfo.h"
15 #include "HexagonBaseInfo.h"
18 void HexagonMCInstrInfo::AppendImplicitOperands(MCInst &MCI) {
19 MCI.addOperand(MCOperand::CreateImm(0));
20 MCI.addOperand(MCOperand::CreateInst(nullptr));
23 HexagonII::MemAccessSize
24 HexagonMCInstrInfo::getAccessSize(MCInstrInfo const &MCII, MCInst const &MCI) {
25 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
27 return (HexagonII::MemAccessSize((F >> HexagonII::MemAccessSizePos) &
28 HexagonII::MemAccesSizeMask));
31 unsigned HexagonMCInstrInfo::getBitCount(MCInstrInfo const &MCII,
33 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
34 return ((F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask);
37 // Return constant extended operand number.
38 unsigned short HexagonMCInstrInfo::getCExtOpNum(MCInstrInfo const &MCII,
40 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
41 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask);
44 MCInstrDesc const &HexagonMCInstrInfo::getDesc(MCInstrInfo const &MCII,
46 return (MCII.get(MCI.getOpcode()));
49 unsigned HexagonMCInstrInfo::getExtentAlignment(MCInstrInfo const &MCII,
51 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
52 return ((F >> HexagonII::ExtentAlignPos) & HexagonII::ExtentAlignMask);
55 unsigned HexagonMCInstrInfo::getExtentBits(MCInstrInfo const &MCII,
57 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
58 return ((F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask);
61 std::bitset<16> HexagonMCInstrInfo::GetImplicitBits(MCInst const &MCI) {
62 SanityCheckImplicitOperands(MCI);
63 std::bitset<16> Bits(MCI.getOperand(MCI.getNumOperands() - 2).getImm());
67 // Return the max value that a constant extendable operand can have
68 // without being extended.
69 int HexagonMCInstrInfo::getMaxValue(MCInstrInfo const &MCII,
71 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
73 (F >> HexagonII::ExtentSignedPos) & HexagonII::ExtentSignedMask;
74 unsigned bits = (F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask;
76 if (isSigned) // if value is signed
77 return ~(-1U << (bits - 1));
79 return ~(-1U << bits);
82 // Return the min value that a constant extendable operand can have
83 // without being extended.
84 int HexagonMCInstrInfo::getMinValue(MCInstrInfo const &MCII,
86 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
88 (F >> HexagonII::ExtentSignedPos) & HexagonII::ExtentSignedMask;
89 unsigned bits = (F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask;
91 if (isSigned) // if value is signed
92 return -1U << (bits - 1);
97 char const *HexagonMCInstrInfo::getName(MCInstrInfo const &MCII,
99 return MCII.getName(MCI.getOpcode());
102 // Return the operand that consumes or produces a new value.
103 MCOperand const &HexagonMCInstrInfo::getNewValue(MCInstrInfo const &MCII,
105 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
107 (F >> HexagonII::NewValueOpPos) & HexagonII::NewValueOpMask;
108 MCOperand const &MCO = MCI.getOperand(O);
110 assert((HexagonMCInstrInfo::isNewValue(MCII, MCI) ||
111 HexagonMCInstrInfo::hasNewValue(MCII, MCI)) &&
116 // Return the Hexagon ISA class for the insn.
117 unsigned HexagonMCInstrInfo::getType(MCInstrInfo const &MCII,
119 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
121 return ((F >> HexagonII::TypePos) & HexagonII::TypeMask);
124 // Return whether the instruction is a legal new-value producer.
125 bool HexagonMCInstrInfo::hasNewValue(MCInstrInfo const &MCII,
127 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
128 return ((F >> HexagonII::hasNewValuePos) & HexagonII::hasNewValueMask);
131 // Return whether the insn is an actual insn.
132 bool HexagonMCInstrInfo::isCanon(MCInstrInfo const &MCII, MCInst const &MCI) {
133 return (!HexagonMCInstrInfo::getDesc(MCII, MCI).isPseudo() &&
134 !HexagonMCInstrInfo::isPrefix(MCII, MCI) &&
135 HexagonMCInstrInfo::getType(MCII, MCI) != HexagonII::TypeENDLOOP);
138 // Return whether the instruction needs to be constant extended.
139 // 1) Always return true if the instruction has 'isExtended' flag set.
142 // 2) For immediate extended operands, return true only if the value is
144 // 3) For global address, always return true.
146 bool HexagonMCInstrInfo::isConstExtended(MCInstrInfo const &MCII,
148 if (HexagonMCInstrInfo::isExtended(MCII, MCI))
151 if (!HexagonMCInstrInfo::isExtendable(MCII, MCI))
154 short ExtOpNum = HexagonMCInstrInfo::getCExtOpNum(MCII, MCI);
155 int MinValue = HexagonMCInstrInfo::getMinValue(MCII, MCI);
156 int MaxValue = HexagonMCInstrInfo::getMaxValue(MCII, MCI);
157 MCOperand const &MO = MCI.getOperand(ExtOpNum);
159 // We could be using an instruction with an extendable immediate and shoehorn
160 // a global address into it. If it is a global address it will be constant
161 // extended. We do this for COMBINE.
162 // We currently only handle isGlobal() because it is the only kind of
163 // object we are going to end up with here for now.
164 // In the future we probably should add isSymbol(), etc.
168 // If the extendable operand is not 'Immediate' type, the instruction should
169 // have 'isExtended' flag set.
170 assert(MO.isImm() && "Extendable operand must be Immediate type");
172 int ImmValue = MO.getImm();
173 return (ImmValue < MinValue || ImmValue > MaxValue);
176 // Return true if the instruction may be extended based on the operand value.
177 bool HexagonMCInstrInfo::isExtendable(MCInstrInfo const &MCII,
179 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
180 return (F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask;
183 // Return whether the instruction must be always extended.
184 bool HexagonMCInstrInfo::isExtended(MCInstrInfo const &MCII,
186 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
187 return (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
190 // Return whether the insn is a new-value consumer.
191 bool HexagonMCInstrInfo::isNewValue(MCInstrInfo const &MCII,
193 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
194 return ((F >> HexagonII::NewValuePos) & HexagonII::NewValueMask);
197 // Return whether the operand can be constant extended.
198 bool HexagonMCInstrInfo::isOperandExtended(MCInstrInfo const &MCII,
200 unsigned short OperandNum) {
201 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
202 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask) ==
206 bool HexagonMCInstrInfo::isPacketBegin(MCInst const &MCI) {
207 std::bitset<16> Bits(GetImplicitBits(MCI));
208 return Bits.test(packetBeginIndex);
211 bool HexagonMCInstrInfo::isPacketEnd(MCInst const &MCI) {
212 std::bitset<16> Bits(GetImplicitBits(MCI));
213 return Bits.test(packetEndIndex);
216 // Return whether the insn is a prefix.
217 bool HexagonMCInstrInfo::isPrefix(MCInstrInfo const &MCII, MCInst const &MCI) {
218 return (HexagonMCInstrInfo::getType(MCII, MCI) == HexagonII::TypePREFIX);
221 // Return whether the insn is solo, i.e., cannot be in a packet.
222 bool HexagonMCInstrInfo::isSolo(MCInstrInfo const &MCII, MCInst const &MCI) {
223 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
224 return ((F >> HexagonII::SoloPos) & HexagonII::SoloMask);
227 void HexagonMCInstrInfo::resetPacket(MCInst &MCI) {
228 setPacketBegin(MCI, false);
229 setPacketEnd(MCI, false);
232 void HexagonMCInstrInfo::SetImplicitBits(MCInst &MCI, std::bitset<16> Bits) {
233 SanityCheckImplicitOperands(MCI);
234 MCI.getOperand(MCI.getNumOperands() - 2).setImm(Bits.to_ulong());
237 void HexagonMCInstrInfo::setPacketBegin(MCInst &MCI, bool f) {
238 std::bitset<16> Bits(GetImplicitBits(MCI));
239 Bits.set(packetBeginIndex, f);
240 SetImplicitBits(MCI, Bits);
243 void HexagonMCInstrInfo::setPacketEnd(MCInst &MCI, bool f) {
244 std::bitset<16> Bits(GetImplicitBits(MCI));
245 Bits.set(packetEndIndex, f);
246 SetImplicitBits(MCI, Bits);