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 unsigned HexagonMCInstrInfo::getBitCount(MCInstrInfo const &MCII,
25 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
26 return ((F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask);
29 // Return constant extended operand number.
30 unsigned short HexagonMCInstrInfo::getCExtOpNum(MCInstrInfo const &MCII,
32 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
33 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask);
36 MCInstrDesc const &HexagonMCInstrInfo::getDesc(MCInstrInfo const &MCII,
38 return (MCII.get(MCI.getOpcode()));
41 std::bitset<16> HexagonMCInstrInfo::GetImplicitBits(MCInst const &MCI) {
42 SanityCheckImplicitOperands(MCI);
43 std::bitset<16> Bits(MCI.getOperand(MCI.getNumOperands() - 2).getImm());
47 // Return the max value that a constant extendable operand can have
48 // without being extended.
49 int HexagonMCInstrInfo::getMaxValue(MCInstrInfo const &MCII,
51 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
53 (F >> HexagonII::ExtentSignedPos) & HexagonII::ExtentSignedMask;
54 unsigned bits = (F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask;
56 if (isSigned) // if value is signed
57 return ~(-1U << (bits - 1));
59 return ~(-1U << bits);
62 // Return the min value that a constant extendable operand can have
63 // without being extended.
64 int HexagonMCInstrInfo::getMinValue(MCInstrInfo const &MCII,
66 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
68 (F >> HexagonII::ExtentSignedPos) & HexagonII::ExtentSignedMask;
69 unsigned bits = (F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask;
71 if (isSigned) // if value is signed
72 return -1U << (bits - 1);
77 // Return the operand that consumes or produces a new value.
78 MCOperand const &HexagonMCInstrInfo::getNewValue(MCInstrInfo const &MCII,
80 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
82 (F >> HexagonII::NewValueOpPos) & HexagonII::NewValueOpMask;
83 MCOperand const &MCO = MCI.getOperand(O);
85 assert((HexagonMCInstrInfo::isNewValue(MCII, MCI) ||
86 HexagonMCInstrInfo::hasNewValue(MCII, MCI)) &&
91 // Return the Hexagon ISA class for the insn.
92 unsigned HexagonMCInstrInfo::getType(MCInstrInfo const &MCII,
94 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
96 return ((F >> HexagonII::TypePos) & HexagonII::TypeMask);
99 // Return whether the instruction is a legal new-value producer.
100 bool HexagonMCInstrInfo::hasNewValue(MCInstrInfo const &MCII,
102 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
103 return ((F >> HexagonII::hasNewValuePos) & HexagonII::hasNewValueMask);
106 // Return whether the insn is an actual insn.
107 bool HexagonMCInstrInfo::isCanon(MCInstrInfo const &MCII, MCInst const &MCI) {
108 return (!HexagonMCInstrInfo::getDesc(MCII, MCI).isPseudo() &&
109 !HexagonMCInstrInfo::isPrefix(MCII, MCI) &&
110 HexagonMCInstrInfo::getType(MCII, MCI) != HexagonII::TypeENDLOOP);
113 // Return whether the instruction needs to be constant extended.
114 // 1) Always return true if the instruction has 'isExtended' flag set.
117 // 2) For immediate extended operands, return true only if the value is
119 // 3) For global address, always return true.
121 bool HexagonMCInstrInfo::isConstExtended(MCInstrInfo const &MCII,
123 if (HexagonMCInstrInfo::isExtended(MCII, MCI))
126 if (!HexagonMCInstrInfo::isExtendable(MCII, MCI))
129 short ExtOpNum = HexagonMCInstrInfo::getCExtOpNum(MCII, MCI);
130 int MinValue = HexagonMCInstrInfo::getMinValue(MCII, MCI);
131 int MaxValue = HexagonMCInstrInfo::getMaxValue(MCII, MCI);
132 MCOperand const &MO = MCI.getOperand(ExtOpNum);
134 // We could be using an instruction with an extendable immediate and shoehorn
135 // a global address into it. If it is a global address it will be constant
136 // extended. We do this for COMBINE.
137 // We currently only handle isGlobal() because it is the only kind of
138 // object we are going to end up with here for now.
139 // In the future we probably should add isSymbol(), etc.
143 // If the extendable operand is not 'Immediate' type, the instruction should
144 // have 'isExtended' flag set.
145 assert(MO.isImm() && "Extendable operand must be Immediate type");
147 int ImmValue = MO.getImm();
148 return (ImmValue < MinValue || ImmValue > MaxValue);
151 // Return true if the instruction may be extended based on the operand value.
152 bool HexagonMCInstrInfo::isExtendable(MCInstrInfo const &MCII,
154 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
155 return (F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask;
158 // Return whether the instruction must be always extended.
159 bool HexagonMCInstrInfo::isExtended(MCInstrInfo const &MCII,
161 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
162 return (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
165 // Return whether the insn is a new-value consumer.
166 bool HexagonMCInstrInfo::isNewValue(MCInstrInfo const &MCII,
168 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
169 return ((F >> HexagonII::NewValuePos) & HexagonII::NewValueMask);
172 // Return whether the operand can be constant extended.
173 bool HexagonMCInstrInfo::isOperandExtended(MCInstrInfo const &MCII,
175 unsigned short OperandNum) {
176 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
177 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask) ==
181 bool HexagonMCInstrInfo::isPacketBegin(MCInst const &MCI) {
182 std::bitset<16> Bits(GetImplicitBits(MCI));
183 return Bits.test(packetBeginIndex);
186 bool HexagonMCInstrInfo::isPacketEnd(MCInst const &MCI) {
187 std::bitset<16> Bits(GetImplicitBits(MCI));
188 return Bits.test(packetEndIndex);
191 // Return whether the insn is a prefix.
192 bool HexagonMCInstrInfo::isPrefix(MCInstrInfo const &MCII, MCInst const &MCI) {
193 return (HexagonMCInstrInfo::getType(MCII, MCI) == HexagonII::TypePREFIX);
196 // Return whether the insn is solo, i.e., cannot be in a packet.
197 bool HexagonMCInstrInfo::isSolo(MCInstrInfo const &MCII, MCInst const &MCI) {
198 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
199 return ((F >> HexagonII::SoloPos) & HexagonII::SoloMask);
202 void HexagonMCInstrInfo::resetPacket(MCInst &MCI) {
203 setPacketBegin(MCI, false);
204 setPacketEnd(MCI, false);
207 void HexagonMCInstrInfo::SetImplicitBits(MCInst &MCI, std::bitset<16> Bits) {
208 SanityCheckImplicitOperands(MCI);
209 MCI.getOperand(MCI.getNumOperands() - 2).setImm(Bits.to_ulong());
212 void HexagonMCInstrInfo::setPacketBegin(MCInst &MCI, bool f) {
213 std::bitset<16> Bits(GetImplicitBits(MCI));
214 Bits.set(packetBeginIndex, f);
215 SetImplicitBits(MCI, Bits);
218 void HexagonMCInstrInfo::setPacketEnd(MCInst &MCI, bool f) {
219 std::bitset<16> Bits(GetImplicitBits(MCI));
220 Bits.set(packetEndIndex, f);
221 SetImplicitBits(MCI, Bits);