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 HexagonMCInstrInfo::getExtentAlignment(MCInstrInfo const &MCII,
61 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
62 return ((F >> HexagonII::ExtentAlignPos) & HexagonII::ExtentAlignMask);
65 unsigned HexagonMCInstrInfo::getExtentBits(MCInstrInfo const &MCII,
67 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
68 return ((F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask);
71 // Return the max value that a constant extendable operand can have
72 // without being extended.
73 int HexagonMCInstrInfo::getMaxValue(MCInstrInfo const &MCII,
75 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
77 (F >> HexagonII::ExtentSignedPos) & HexagonII::ExtentSignedMask;
78 unsigned bits = (F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask;
80 if (isSigned) // if value is signed
81 return ~(-1U << (bits - 1));
83 return ~(-1U << bits);
86 // Return the min value that a constant extendable operand can have
87 // without being extended.
88 int HexagonMCInstrInfo::getMinValue(MCInstrInfo const &MCII,
90 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
92 (F >> HexagonII::ExtentSignedPos) & HexagonII::ExtentSignedMask;
93 unsigned bits = (F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask;
95 if (isSigned) // if value is signed
96 return -1U << (bits - 1);
101 char const *HexagonMCInstrInfo::getName(MCInstrInfo const &MCII,
103 return MCII.getName(MCI.getOpcode());
106 unsigned short HexagonMCInstrInfo::getNewValueOp(MCInstrInfo const &MCII,
108 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
109 return ((F >> HexagonII::NewValueOpPos) & HexagonII::NewValueOpMask);
112 MCOperand const &HexagonMCInstrInfo::getNewValueOperand(MCInstrInfo const &MCII,
114 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
116 (F >> HexagonII::NewValueOpPos) & HexagonII::NewValueOpMask;
117 MCOperand const &MCO = MCI.getOperand(O);
119 assert((HexagonMCInstrInfo::isNewValue(MCII, MCI) ||
120 HexagonMCInstrInfo::hasNewValue(MCII, MCI)) &&
125 // Return the Hexagon ISA class for the insn.
126 unsigned HexagonMCInstrInfo::getType(MCInstrInfo const &MCII,
128 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
130 return ((F >> HexagonII::TypePos) & HexagonII::TypeMask);
133 // Return whether the instruction is a legal new-value producer.
134 bool HexagonMCInstrInfo::hasNewValue(MCInstrInfo const &MCII,
136 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
137 return ((F >> HexagonII::hasNewValuePos) & HexagonII::hasNewValueMask);
140 bool HexagonMCInstrInfo::isBundle(MCInst const &MCI) {
141 auto Result = Hexagon::BUNDLE == MCI.getOpcode();
142 assert(!Result || (MCI.size() > 0 && MCI.getOperand(0).isImm()));
146 // Return whether the insn is an actual insn.
147 bool HexagonMCInstrInfo::isCanon(MCInstrInfo const &MCII, MCInst const &MCI) {
148 return (!HexagonMCInstrInfo::getDesc(MCII, MCI).isPseudo() &&
149 !HexagonMCInstrInfo::isPrefix(MCII, MCI) &&
150 HexagonMCInstrInfo::getType(MCII, MCI) != HexagonII::TypeENDLOOP);
153 // Return whether the instruction needs to be constant extended.
154 // 1) Always return true if the instruction has 'isExtended' flag set.
157 // 2) For immediate extended operands, return true only if the value is
159 // 3) For global address, always return true.
161 bool HexagonMCInstrInfo::isConstExtended(MCInstrInfo const &MCII,
163 if (HexagonMCInstrInfo::isExtended(MCII, MCI))
166 if (!HexagonMCInstrInfo::isExtendable(MCII, MCI))
169 short ExtOpNum = HexagonMCInstrInfo::getCExtOpNum(MCII, MCI);
170 int MinValue = HexagonMCInstrInfo::getMinValue(MCII, MCI);
171 int MaxValue = HexagonMCInstrInfo::getMaxValue(MCII, MCI);
172 MCOperand const &MO = MCI.getOperand(ExtOpNum);
174 // We could be using an instruction with an extendable immediate and shoehorn
175 // a global address into it. If it is a global address it will be constant
176 // extended. We do this for COMBINE.
177 // We currently only handle isGlobal() because it is the only kind of
178 // object we are going to end up with here for now.
179 // In the future we probably should add isSymbol(), etc.
183 // If the extendable operand is not 'Immediate' type, the instruction should
184 // have 'isExtended' flag set.
185 assert(MO.isImm() && "Extendable operand must be Immediate type");
187 int ImmValue = MO.getImm();
188 return (ImmValue < MinValue || ImmValue > MaxValue);
191 // Return true if the instruction may be extended based on the operand value.
192 bool HexagonMCInstrInfo::isExtendable(MCInstrInfo const &MCII,
194 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
195 return (F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask;
198 // Return whether the instruction must be always extended.
199 bool HexagonMCInstrInfo::isExtended(MCInstrInfo const &MCII,
201 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
202 return (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
205 bool HexagonMCInstrInfo::isImmext(MCInst const &MCI) {
206 auto Op = MCI.getOpcode();
207 return (Op == Hexagon::A4_ext_b || Op == Hexagon::A4_ext_c ||
208 Op == Hexagon::A4_ext_g || Op == Hexagon::A4_ext);
211 bool HexagonMCInstrInfo::isInnerLoop(MCInst const &MCI) {
212 assert(isBundle(MCI));
213 int64_t Flags = MCI.getOperand(0).getImm();
214 return (Flags & innerLoopMask) != 0;
217 // Return whether the insn is a new-value consumer.
218 bool HexagonMCInstrInfo::isNewValue(MCInstrInfo const &MCII,
220 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
221 return ((F >> HexagonII::NewValuePos) & HexagonII::NewValueMask);
224 // Return whether the operand can be constant extended.
225 bool HexagonMCInstrInfo::isOperandExtended(MCInstrInfo const &MCII,
227 unsigned short OperandNum) {
228 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
229 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask) ==
233 bool HexagonMCInstrInfo::isOuterLoop(MCInst const &MCI) {
234 assert(isBundle(MCI));
235 int64_t Flags = MCI.getOperand(0).getImm();
236 return (Flags & outerLoopMask) != 0;
239 bool HexagonMCInstrInfo::isPredicated(MCInstrInfo const &MCII,
241 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
242 return ((F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
245 bool HexagonMCInstrInfo::isPredicatedTrue(MCInstrInfo const &MCII,
247 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
249 !((F >> HexagonII::PredicatedFalsePos) & HexagonII::PredicatedFalseMask));
252 // Return whether the insn is a prefix.
253 bool HexagonMCInstrInfo::isPrefix(MCInstrInfo const &MCII, MCInst const &MCI) {
254 return (HexagonMCInstrInfo::getType(MCII, MCI) == HexagonII::TypePREFIX);
257 // Return whether the insn is solo, i.e., cannot be in a packet.
258 bool HexagonMCInstrInfo::isSolo(MCInstrInfo const &MCII, MCInst const &MCI) {
259 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
260 return ((F >> HexagonII::SoloPos) & HexagonII::SoloMask);
263 void HexagonMCInstrInfo::padEndloop(MCInst &MCB) {
265 Nop.setOpcode(Hexagon::A2_nop);
266 assert(isBundle(MCB));
267 while ((HexagonMCInstrInfo::isInnerLoop(MCB) &&
268 (HexagonMCInstrInfo::bundleSize(MCB) < HEXAGON_PACKET_INNER_SIZE)) ||
269 ((HexagonMCInstrInfo::isOuterLoop(MCB) &&
270 (HexagonMCInstrInfo::bundleSize(MCB) < HEXAGON_PACKET_OUTER_SIZE))))
271 MCB.addOperand(MCOperand::createInst(new MCInst(Nop)));
274 void HexagonMCInstrInfo::setInnerLoop(MCInst &MCI) {
275 assert(isBundle(MCI));
276 MCOperand &Operand = MCI.getOperand(0);
277 Operand.setImm(Operand.getImm() | innerLoopMask);
280 void HexagonMCInstrInfo::setOuterLoop(MCInst &MCI) {
281 assert(isBundle(MCI));
282 MCOperand &Operand = MCI.getOperand(0);
283 Operand.setImm(Operand.getImm() | outerLoopMask);