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"
16 #include "llvm/MC/MCInstrInfo.h"
19 unsigned HexagonMCInstrInfo::getBitCount(MCInstrInfo const &MCII,
21 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
22 return ((F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask);
25 MCInstrDesc const &HexagonMCInstrInfo::getDesc(MCInstrInfo const &MCII,
27 return (MCII.get(MCI.getOpcode()));
30 // Return constant extended operand number.
31 unsigned short HexagonMCInstrInfo::getCExtOpNum(MCInstrInfo const &MCII,
33 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
34 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask);
37 // Return the max value that a constant extendable operand can have
38 // without being extended.
39 int HexagonMCInstrInfo::getMaxValue(MCInstrInfo const &MCII,
41 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
43 (F >> HexagonII::ExtentSignedPos) & HexagonII::ExtentSignedMask;
44 unsigned bits = (F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask;
46 if (isSigned) // if value is signed
47 return ~(-1U << (bits - 1));
49 return ~(-1U << bits);
52 // Return the min value that a constant extendable operand can have
53 // without being extended.
54 int HexagonMCInstrInfo::getMinValue(MCInstrInfo const &MCII,
56 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
58 (F >> HexagonII::ExtentSignedPos) & HexagonII::ExtentSignedMask;
59 unsigned bits = (F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask;
61 if (isSigned) // if value is signed
62 return -1U << (bits - 1);
67 // Return the operand that consumes or produces a new value.
68 MCOperand const &HexagonMCInstrInfo::getNewValue(MCInstrInfo const &MCII,
70 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
72 (F >> HexagonII::NewValueOpPos) & HexagonII::NewValueOpMask;
73 MCOperand const &MCO = MCI.getOperand(O);
75 assert((HexagonMCInstrInfo::isNewValue(MCII, MCI) ||
76 HexagonMCInstrInfo::hasNewValue(MCII, MCI)) &&
81 // Return the Hexagon ISA class for the insn.
82 unsigned HexagonMCInstrInfo::getType(MCInstrInfo const &MCII,
84 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
86 return ((F >> HexagonII::TypePos) & HexagonII::TypeMask);
89 // Return whether the instruction is a legal new-value producer.
90 bool HexagonMCInstrInfo::hasNewValue(MCInstrInfo const &MCII,
92 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
93 return ((F >> HexagonII::hasNewValuePos) & HexagonII::hasNewValueMask);
96 // Return whether the insn is an actual insn.
97 bool HexagonMCInstrInfo::isCanon(MCInstrInfo const &MCII, MCInst const &MCI) {
98 return (!HexagonMCInstrInfo::getDesc(MCII, MCI).isPseudo() &&
99 !HexagonMCInstrInfo::isPrefix(MCII, MCI) &&
100 HexagonMCInstrInfo::getType(MCII, MCI) != HexagonII::TypeENDLOOP);
103 // Return whether the instruction needs to be constant extended.
104 // 1) Always return true if the instruction has 'isExtended' flag set.
107 // 2) For immediate extended operands, return true only if the value is
109 // 3) For global address, always return true.
111 bool HexagonMCInstrInfo::isConstExtended(MCInstrInfo const &MCII,
113 if (HexagonMCInstrInfo::isExtended(MCII, MCI))
116 if (!HexagonMCInstrInfo::isExtendable(MCII, MCI))
119 short ExtOpNum = HexagonMCInstrInfo::getCExtOpNum(MCII, MCI);
120 int MinValue = HexagonMCInstrInfo::getMinValue(MCII, MCI);
121 int MaxValue = HexagonMCInstrInfo::getMaxValue(MCII, MCI);
122 MCOperand const &MO = MCI.getOperand(ExtOpNum);
124 // We could be using an instruction with an extendable immediate and shoehorn
125 // a global address into it. If it is a global address it will be constant
126 // extended. We do this for COMBINE.
127 // We currently only handle isGlobal() because it is the only kind of
128 // object we are going to end up with here for now.
129 // In the future we probably should add isSymbol(), etc.
133 // If the extendable operand is not 'Immediate' type, the instruction should
134 // have 'isExtended' flag set.
135 assert(MO.isImm() && "Extendable operand must be Immediate type");
137 int ImmValue = MO.getImm();
138 return (ImmValue < MinValue || ImmValue > MaxValue);
141 // Return true if the instruction may be extended based on the operand value.
142 bool HexagonMCInstrInfo::isExtendable(MCInstrInfo const &MCII,
144 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
145 return (F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask;
148 // Return whether the instruction must be always extended.
149 bool HexagonMCInstrInfo::isExtended(MCInstrInfo const &MCII,
151 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
152 return (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
155 // Return whether the insn is a new-value consumer.
156 bool HexagonMCInstrInfo::isNewValue(MCInstrInfo const &MCII,
158 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
159 return ((F >> HexagonII::NewValuePos) & HexagonII::NewValueMask);
162 // Return whether the operand can be constant extended.
163 bool HexagonMCInstrInfo::isOperandExtended(MCInstrInfo const &MCII,
165 unsigned short OperandNum) {
166 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
167 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask) ==
171 // Return whether the insn is a prefix.
172 bool HexagonMCInstrInfo::isPrefix(MCInstrInfo const &MCII, MCInst const &MCI) {
173 return (HexagonMCInstrInfo::getType(MCII, MCI) == HexagonII::TypePREFIX);
176 // Return whether the insn is solo, i.e., cannot be in a packet.
177 bool HexagonMCInstrInfo::isSolo(MCInstrInfo const &MCII, MCInst const &MCI) {
178 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
179 return ((F >> HexagonII::SoloPos) & HexagonII::SoloMask);