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 //===----------------------------------------------------------------------===//
15 #include "HexagonBaseInfo.h"
16 #include "HexagonMCInstrInfo.h"
19 iterator_range<MCInst::const_iterator>
20 HexagonMCInstrInfo::bundleInstructions(MCInst const &MCI) {
21 assert(isBundle(MCI));
22 return iterator_range<MCInst::const_iterator>(
23 MCI.begin() + bundleInstructionsOffset, MCI.end());
26 size_t HexagonMCInstrInfo::bundleSize(MCInst const &MCI) {
27 if (HexagonMCInstrInfo::isBundle(MCI))
28 return (MCI.size() - bundleInstructionsOffset);
33 HexagonII::MemAccessSize
34 HexagonMCInstrInfo::getAccessSize(MCInstrInfo const &MCII, MCInst const &MCI) {
35 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
37 return (HexagonII::MemAccessSize((F >> HexagonII::MemAccessSizePos) &
38 HexagonII::MemAccesSizeMask));
41 unsigned HexagonMCInstrInfo::getBitCount(MCInstrInfo const &MCII,
43 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
44 return ((F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask);
47 // Return constant extended operand number.
48 unsigned short HexagonMCInstrInfo::getCExtOpNum(MCInstrInfo const &MCII,
50 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
51 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask);
54 MCInstrDesc const &HexagonMCInstrInfo::getDesc(MCInstrInfo const &MCII,
56 return (MCII.get(MCI.getOpcode()));
59 unsigned short HexagonMCInstrInfo::getExtendableOp(MCInstrInfo const &MCII,
61 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
62 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask);
66 HexagonMCInstrInfo::getExtendableOperand(MCInstrInfo const &MCII,
68 unsigned O = HexagonMCInstrInfo::getExtendableOp(MCII, MCI);
69 MCOperand const &MO = MCI.getOperand(O);
71 assert((HexagonMCInstrInfo::isExtendable(MCII, MCI) ||
72 HexagonMCInstrInfo::isExtended(MCII, MCI)) &&
73 (MO.isImm() || MO.isExpr()));
77 unsigned HexagonMCInstrInfo::getExtentAlignment(MCInstrInfo const &MCII,
79 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
80 return ((F >> HexagonII::ExtentAlignPos) & HexagonII::ExtentAlignMask);
83 unsigned HexagonMCInstrInfo::getExtentBits(MCInstrInfo const &MCII,
85 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
86 return ((F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask);
89 // Return the max value that a constant extendable operand can have
90 // without being extended.
91 int HexagonMCInstrInfo::getMaxValue(MCInstrInfo const &MCII,
93 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
95 (F >> HexagonII::ExtentSignedPos) & HexagonII::ExtentSignedMask;
96 unsigned bits = (F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask;
98 if (isSigned) // if value is signed
99 return ~(-1U << (bits - 1));
101 return ~(-1U << bits);
104 // Return the min value that a constant extendable operand can have
105 // without being extended.
106 int HexagonMCInstrInfo::getMinValue(MCInstrInfo const &MCII,
108 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
110 (F >> HexagonII::ExtentSignedPos) & HexagonII::ExtentSignedMask;
111 unsigned bits = (F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask;
113 if (isSigned) // if value is signed
114 return -1U << (bits - 1);
119 char const *HexagonMCInstrInfo::getName(MCInstrInfo const &MCII,
121 return MCII.getName(MCI.getOpcode());
124 unsigned short HexagonMCInstrInfo::getNewValueOp(MCInstrInfo const &MCII,
126 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
127 return ((F >> HexagonII::NewValueOpPos) & HexagonII::NewValueOpMask);
130 MCOperand const &HexagonMCInstrInfo::getNewValueOperand(MCInstrInfo const &MCII,
132 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
134 (F >> HexagonII::NewValueOpPos) & HexagonII::NewValueOpMask;
135 MCOperand const &MCO = MCI.getOperand(O);
137 assert((HexagonMCInstrInfo::isNewValue(MCII, MCI) ||
138 HexagonMCInstrInfo::hasNewValue(MCII, MCI)) &&
143 // Return the Hexagon ISA class for the insn.
144 unsigned HexagonMCInstrInfo::getType(MCInstrInfo const &MCII,
146 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
148 return ((F >> HexagonII::TypePos) & HexagonII::TypeMask);
151 // Return whether the instruction is a legal new-value producer.
152 bool HexagonMCInstrInfo::hasNewValue(MCInstrInfo const &MCII,
154 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
155 return ((F >> HexagonII::hasNewValuePos) & HexagonII::hasNewValueMask);
158 MCInst const &HexagonMCInstrInfo::instruction(MCInst const &MCB, size_t Index) {
159 assert(isBundle(MCB));
160 assert(Index < HEXAGON_PACKET_SIZE);
161 return *MCB.getOperand(bundleInstructionsOffset + Index).getInst();
164 bool HexagonMCInstrInfo::isBundle(MCInst const &MCI) {
165 auto Result = Hexagon::BUNDLE == MCI.getOpcode();
166 assert(!Result || (MCI.size() > 0 && MCI.getOperand(0).isImm()));
170 // Return whether the insn is an actual insn.
171 bool HexagonMCInstrInfo::isCanon(MCInstrInfo const &MCII, MCInst const &MCI) {
172 return (!HexagonMCInstrInfo::getDesc(MCII, MCI).isPseudo() &&
173 !HexagonMCInstrInfo::isPrefix(MCII, MCI) &&
174 HexagonMCInstrInfo::getType(MCII, MCI) != HexagonII::TypeENDLOOP);
177 // Return whether the instruction needs to be constant extended.
178 // 1) Always return true if the instruction has 'isExtended' flag set.
181 // 2) For immediate extended operands, return true only if the value is
183 // 3) For global address, always return true.
185 bool HexagonMCInstrInfo::isConstExtended(MCInstrInfo const &MCII,
187 if (HexagonMCInstrInfo::isExtended(MCII, MCI))
190 if (!HexagonMCInstrInfo::isExtendable(MCII, MCI))
193 short ExtOpNum = HexagonMCInstrInfo::getCExtOpNum(MCII, MCI);
194 int MinValue = HexagonMCInstrInfo::getMinValue(MCII, MCI);
195 int MaxValue = HexagonMCInstrInfo::getMaxValue(MCII, MCI);
196 MCOperand const &MO = MCI.getOperand(ExtOpNum);
198 // We could be using an instruction with an extendable immediate and shoehorn
199 // a global address into it. If it is a global address it will be constant
200 // extended. We do this for COMBINE.
201 // We currently only handle isGlobal() because it is the only kind of
202 // object we are going to end up with here for now.
203 // In the future we probably should add isSymbol(), etc.
207 // If the extendable operand is not 'Immediate' type, the instruction should
208 // have 'isExtended' flag set.
209 assert(MO.isImm() && "Extendable operand must be Immediate type");
211 int ImmValue = MO.getImm();
212 return (ImmValue < MinValue || ImmValue > MaxValue);
215 // Return true if the instruction may be extended based on the operand value.
216 bool HexagonMCInstrInfo::isExtendable(MCInstrInfo const &MCII,
218 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
219 return (F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask;
222 // Return whether the instruction must be always extended.
223 bool HexagonMCInstrInfo::isExtended(MCInstrInfo const &MCII,
225 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
226 return (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
229 bool HexagonMCInstrInfo::isImmext(MCInst const &MCI) {
230 auto Op = MCI.getOpcode();
231 return (Op == Hexagon::A4_ext_b || Op == Hexagon::A4_ext_c ||
232 Op == Hexagon::A4_ext_g || Op == Hexagon::A4_ext);
235 bool HexagonMCInstrInfo::isInnerLoop(MCInst const &MCI) {
236 assert(isBundle(MCI));
237 int64_t Flags = MCI.getOperand(0).getImm();
238 return (Flags & innerLoopMask) != 0;
241 // Return whether the insn is a new-value consumer.
242 bool HexagonMCInstrInfo::isNewValue(MCInstrInfo const &MCII,
244 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
245 return ((F >> HexagonII::NewValuePos) & HexagonII::NewValueMask);
248 // Return whether the operand can be constant extended.
249 bool HexagonMCInstrInfo::isOperandExtended(MCInstrInfo const &MCII,
251 unsigned short OperandNum) {
252 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
253 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask) ==
257 bool HexagonMCInstrInfo::isOuterLoop(MCInst const &MCI) {
258 assert(isBundle(MCI));
259 int64_t Flags = MCI.getOperand(0).getImm();
260 return (Flags & outerLoopMask) != 0;
263 bool HexagonMCInstrInfo::isPredicated(MCInstrInfo const &MCII,
265 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
266 return ((F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
269 bool HexagonMCInstrInfo::isPredicatedTrue(MCInstrInfo const &MCII,
271 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
273 !((F >> HexagonII::PredicatedFalsePos) & HexagonII::PredicatedFalseMask));
276 // Return whether the insn is a prefix.
277 bool HexagonMCInstrInfo::isPrefix(MCInstrInfo const &MCII, MCInst const &MCI) {
278 return (HexagonMCInstrInfo::getType(MCII, MCI) == HexagonII::TypePREFIX);
281 // Return whether the insn is solo, i.e., cannot be in a packet.
282 bool HexagonMCInstrInfo::isSolo(MCInstrInfo const &MCII, MCInst const &MCI) {
283 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
284 return ((F >> HexagonII::SoloPos) & HexagonII::SoloMask);
287 void HexagonMCInstrInfo::padEndloop(MCInst &MCB) {
289 Nop.setOpcode(Hexagon::A2_nop);
290 assert(isBundle(MCB));
291 while ((HexagonMCInstrInfo::isInnerLoop(MCB) &&
292 (HexagonMCInstrInfo::bundleSize(MCB) < HEXAGON_PACKET_INNER_SIZE)) ||
293 ((HexagonMCInstrInfo::isOuterLoop(MCB) &&
294 (HexagonMCInstrInfo::bundleSize(MCB) < HEXAGON_PACKET_OUTER_SIZE))))
295 MCB.addOperand(MCOperand::createInst(new MCInst(Nop)));
298 void HexagonMCInstrInfo::setInnerLoop(MCInst &MCI) {
299 assert(isBundle(MCI));
300 MCOperand &Operand = MCI.getOperand(0);
301 Operand.setImm(Operand.getImm() | innerLoopMask);
304 void HexagonMCInstrInfo::setOuterLoop(MCInst &MCI) {
305 assert(isBundle(MCI));
306 MCOperand &Operand = MCI.getOperand(0);
307 Operand.setImm(Operand.getImm() | outerLoopMask);