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"
17 #include "HexagonBaseInfo.h"
19 #include "llvm/MC/MCSubtargetInfo.h"
22 iterator_range<MCInst::const_iterator>
23 HexagonMCInstrInfo::bundleInstructions(MCInst const &MCI) {
24 assert(isBundle(MCI));
25 return iterator_range<MCInst::const_iterator>(
26 MCI.begin() + bundleInstructionsOffset, MCI.end());
29 size_t HexagonMCInstrInfo::bundleSize(MCInst const &MCI) {
30 if (HexagonMCInstrInfo::isBundle(MCI))
31 return (MCI.size() - bundleInstructionsOffset);
36 HexagonII::MemAccessSize
37 HexagonMCInstrInfo::getAccessSize(MCInstrInfo const &MCII, MCInst const &MCI) {
38 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
40 return (HexagonII::MemAccessSize((F >> HexagonII::MemAccessSizePos) &
41 HexagonII::MemAccesSizeMask));
44 unsigned HexagonMCInstrInfo::getBitCount(MCInstrInfo const &MCII,
46 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
47 return ((F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask);
50 // Return constant extended operand number.
51 unsigned short HexagonMCInstrInfo::getCExtOpNum(MCInstrInfo const &MCII,
53 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
54 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask);
57 MCInstrDesc const &HexagonMCInstrInfo::getDesc(MCInstrInfo const &MCII,
59 return (MCII.get(MCI.getOpcode()));
62 unsigned short HexagonMCInstrInfo::getExtendableOp(MCInstrInfo const &MCII,
64 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
65 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask);
69 HexagonMCInstrInfo::getExtendableOperand(MCInstrInfo const &MCII,
71 unsigned O = HexagonMCInstrInfo::getExtendableOp(MCII, MCI);
72 MCOperand const &MO = MCI.getOperand(O);
74 assert((HexagonMCInstrInfo::isExtendable(MCII, MCI) ||
75 HexagonMCInstrInfo::isExtended(MCII, MCI)) &&
76 (MO.isImm() || MO.isExpr()));
80 unsigned HexagonMCInstrInfo::getExtentAlignment(MCInstrInfo const &MCII,
82 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
83 return ((F >> HexagonII::ExtentAlignPos) & HexagonII::ExtentAlignMask);
86 unsigned HexagonMCInstrInfo::getExtentBits(MCInstrInfo const &MCII,
88 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
89 return ((F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask);
92 // Return the max value that a constant extendable operand can have
93 // without being extended.
94 int HexagonMCInstrInfo::getMaxValue(MCInstrInfo const &MCII,
96 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
98 (F >> HexagonII::ExtentSignedPos) & HexagonII::ExtentSignedMask;
99 unsigned bits = (F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask;
101 if (isSigned) // if value is signed
102 return ~(-1U << (bits - 1));
104 return ~(-1U << bits);
107 // Return the min value that a constant extendable operand can have
108 // without being extended.
109 int HexagonMCInstrInfo::getMinValue(MCInstrInfo const &MCII,
111 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
113 (F >> HexagonII::ExtentSignedPos) & HexagonII::ExtentSignedMask;
114 unsigned bits = (F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask;
116 if (isSigned) // if value is signed
117 return -1U << (bits - 1);
122 char const *HexagonMCInstrInfo::getName(MCInstrInfo const &MCII,
124 return MCII.getName(MCI.getOpcode());
127 unsigned short HexagonMCInstrInfo::getNewValueOp(MCInstrInfo const &MCII,
129 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
130 return ((F >> HexagonII::NewValueOpPos) & HexagonII::NewValueOpMask);
133 MCOperand const &HexagonMCInstrInfo::getNewValueOperand(MCInstrInfo const &MCII,
135 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
137 (F >> HexagonII::NewValueOpPos) & HexagonII::NewValueOpMask;
138 MCOperand const &MCO = MCI.getOperand(O);
140 assert((HexagonMCInstrInfo::isNewValue(MCII, MCI) ||
141 HexagonMCInstrInfo::hasNewValue(MCII, MCI)) &&
146 int HexagonMCInstrInfo::getSubTarget(MCInstrInfo const &MCII,
148 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
150 HexagonII::SubTarget Target = static_cast<HexagonII::SubTarget>(
151 (F >> HexagonII::validSubTargetPos) & HexagonII::validSubTargetMask);
155 return Hexagon::ArchV4;
156 case HexagonII::HasV5SubT:
157 return Hexagon::ArchV5;
161 // Return the Hexagon ISA class for the insn.
162 unsigned HexagonMCInstrInfo::getType(MCInstrInfo const &MCII,
164 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
166 return ((F >> HexagonII::TypePos) & HexagonII::TypeMask);
169 unsigned HexagonMCInstrInfo::getUnits(MCInstrInfo const &MCII,
170 MCSubtargetInfo const &STI,
173 const InstrItinerary *II = STI.getSchedModel().InstrItineraries;
174 int SchedClass = HexagonMCInstrInfo::getDesc(MCII, MCI).getSchedClass();
175 return ((II[SchedClass].FirstStage + HexagonStages)->getUnits());
178 bool HexagonMCInstrInfo::hasImmExt(MCInst const &MCI) {
179 if (!HexagonMCInstrInfo::isBundle(MCI))
182 for (const auto &I : HexagonMCInstrInfo::bundleInstructions(MCI)) {
183 auto MI = I.getInst();
191 // Return whether the instruction is a legal new-value producer.
192 bool HexagonMCInstrInfo::hasNewValue(MCInstrInfo const &MCII,
194 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
195 return ((F >> HexagonII::hasNewValuePos) & HexagonII::hasNewValueMask);
198 MCInst const &HexagonMCInstrInfo::instruction(MCInst const &MCB, size_t Index) {
199 assert(isBundle(MCB));
200 assert(Index < HEXAGON_PACKET_SIZE);
201 return *MCB.getOperand(bundleInstructionsOffset + Index).getInst();
204 bool HexagonMCInstrInfo::isBundle(MCInst const &MCI) {
205 auto Result = Hexagon::BUNDLE == MCI.getOpcode();
206 assert(!Result || (MCI.size() > 0 && MCI.getOperand(0).isImm()));
210 // Return whether the insn is an actual insn.
211 bool HexagonMCInstrInfo::isCanon(MCInstrInfo const &MCII, MCInst const &MCI) {
212 return (!HexagonMCInstrInfo::getDesc(MCII, MCI).isPseudo() &&
213 !HexagonMCInstrInfo::isPrefix(MCII, MCI) &&
214 HexagonMCInstrInfo::getType(MCII, MCI) != HexagonII::TypeENDLOOP);
217 // Return whether the instruction needs to be constant extended.
218 // 1) Always return true if the instruction has 'isExtended' flag set.
221 // 2) For immediate extended operands, return true only if the value is
223 // 3) For global address, always return true.
225 bool HexagonMCInstrInfo::isConstExtended(MCInstrInfo const &MCII,
227 if (HexagonMCInstrInfo::isExtended(MCII, MCI))
230 if (!HexagonMCInstrInfo::isExtendable(MCII, MCI))
233 short ExtOpNum = HexagonMCInstrInfo::getCExtOpNum(MCII, MCI);
234 int MinValue = HexagonMCInstrInfo::getMinValue(MCII, MCI);
235 int MaxValue = HexagonMCInstrInfo::getMaxValue(MCII, MCI);
236 MCOperand const &MO = MCI.getOperand(ExtOpNum);
238 // We could be using an instruction with an extendable immediate and shoehorn
239 // a global address into it. If it is a global address it will be constant
240 // extended. We do this for COMBINE.
241 // We currently only handle isGlobal() because it is the only kind of
242 // object we are going to end up with here for now.
243 // In the future we probably should add isSymbol(), etc.
247 // If the extendable operand is not 'Immediate' type, the instruction should
248 // have 'isExtended' flag set.
249 assert(MO.isImm() && "Extendable operand must be Immediate type");
251 int ImmValue = MO.getImm();
252 return (ImmValue < MinValue || ImmValue > MaxValue);
255 bool HexagonMCInstrInfo::isExtendable(MCInstrInfo const &MCII,
257 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
258 return (F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask;
261 bool HexagonMCInstrInfo::isExtended(MCInstrInfo const &MCII,
263 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
264 return (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
267 bool HexagonMCInstrInfo::isFloat(MCInstrInfo const &MCII, MCInst const &MCI) {
268 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
269 return ((F >> HexagonII::FPPos) & HexagonII::FPMask);
272 bool HexagonMCInstrInfo::isImmext(MCInst const &MCI) {
273 auto Op = MCI.getOpcode();
274 return (Op == Hexagon::A4_ext_b || Op == Hexagon::A4_ext_c ||
275 Op == Hexagon::A4_ext_g || Op == Hexagon::A4_ext);
278 bool HexagonMCInstrInfo::isInnerLoop(MCInst const &MCI) {
279 assert(isBundle(MCI));
280 int64_t Flags = MCI.getOperand(0).getImm();
281 return (Flags & innerLoopMask) != 0;
284 // Return whether the insn is a new-value consumer.
285 bool HexagonMCInstrInfo::isNewValue(MCInstrInfo const &MCII,
287 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
288 return ((F >> HexagonII::NewValuePos) & HexagonII::NewValueMask);
291 // Return whether the operand can be constant extended.
292 bool HexagonMCInstrInfo::isOperandExtended(MCInstrInfo const &MCII,
294 unsigned short OperandNum) {
295 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
296 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask) ==
300 bool HexagonMCInstrInfo::isOuterLoop(MCInst const &MCI) {
301 assert(isBundle(MCI));
302 int64_t Flags = MCI.getOperand(0).getImm();
303 return (Flags & outerLoopMask) != 0;
306 bool HexagonMCInstrInfo::isPredicated(MCInstrInfo const &MCII,
308 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
309 return ((F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
312 bool HexagonMCInstrInfo::isPredicatedTrue(MCInstrInfo const &MCII,
314 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
316 !((F >> HexagonII::PredicatedFalsePos) & HexagonII::PredicatedFalseMask));
319 bool HexagonMCInstrInfo::isPrefix(MCInstrInfo const &MCII, MCInst const &MCI) {
320 return (HexagonMCInstrInfo::getType(MCII, MCI) == HexagonII::TypePREFIX);
323 bool HexagonMCInstrInfo::isSolo(MCInstrInfo const &MCII, MCInst const &MCI) {
324 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
325 return ((F >> HexagonII::SoloPos) & HexagonII::SoloMask);
328 bool HexagonMCInstrInfo::isSoloAX(MCInstrInfo const &MCII, MCInst const &MCI) {
329 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
330 return ((F >> HexagonII::SoloAXPos) & HexagonII::SoloAXMask);
333 bool HexagonMCInstrInfo::isSoloAin1(MCInstrInfo const &MCII,
335 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
336 return ((F >> HexagonII::SoloAin1Pos) & HexagonII::SoloAin1Mask);
339 void HexagonMCInstrInfo::padEndloop(MCInst &MCB) {
341 Nop.setOpcode(Hexagon::A2_nop);
342 assert(isBundle(MCB));
343 while ((HexagonMCInstrInfo::isInnerLoop(MCB) &&
344 (HexagonMCInstrInfo::bundleSize(MCB) < HEXAGON_PACKET_INNER_SIZE)) ||
345 ((HexagonMCInstrInfo::isOuterLoop(MCB) &&
346 (HexagonMCInstrInfo::bundleSize(MCB) < HEXAGON_PACKET_OUTER_SIZE))))
347 MCB.addOperand(MCOperand::createInst(new MCInst(Nop)));
350 bool HexagonMCInstrInfo::prefersSlot3(MCInstrInfo const &MCII,
352 if (HexagonMCInstrInfo::getType(MCII, MCI) == HexagonII::TypeCR)
355 unsigned SchedClass = HexagonMCInstrInfo::getDesc(MCII, MCI).getSchedClass();
356 switch (SchedClass) {
357 case Hexagon::Sched::ALU32_3op_tc_2_SLOT0123:
358 case Hexagon::Sched::ALU64_tc_2_SLOT23:
359 case Hexagon::Sched::ALU64_tc_3x_SLOT23:
360 case Hexagon::Sched::M_tc_2_SLOT23:
361 case Hexagon::Sched::M_tc_3x_SLOT23:
362 case Hexagon::Sched::S_2op_tc_2_SLOT23:
363 case Hexagon::Sched::S_3op_tc_2_SLOT23:
364 case Hexagon::Sched::S_3op_tc_3x_SLOT23:
370 void HexagonMCInstrInfo::setInnerLoop(MCInst &MCI) {
371 assert(isBundle(MCI));
372 MCOperand &Operand = MCI.getOperand(0);
373 Operand.setImm(Operand.getImm() | innerLoopMask);
376 void HexagonMCInstrInfo::setOuterLoop(MCInst &MCI) {
377 assert(isBundle(MCI));
378 MCOperand &Operand = MCI.getOperand(0);
379 Operand.setImm(Operand.getImm() | outerLoopMask);