1 //===- HexagonMCInst.cpp - Hexagon sub-class of MCInst --------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This class extends MCInst to allow some Hexagon VLIW annotations.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "HexagonInstrInfo.h"
15 #include "MCTargetDesc/HexagonBaseInfo.h"
16 #include "MCTargetDesc/HexagonMCInst.h"
17 #include "MCTargetDesc/HexagonMCTargetDesc.h"
18 
19 using namespace llvm;
20 
21 std::unique_ptr <MCInstrInfo const> HexagonMCInst::MCII;
22 
HexagonMCInst()23 HexagonMCInst::HexagonMCInst() : MCInst() {}
HexagonMCInst(MCInstrDesc const & mcid)24 HexagonMCInst::HexagonMCInst(MCInstrDesc const &mcid) : MCInst() {}
25 
AppendImplicitOperands(MCInst & MCI)26 void HexagonMCInst::AppendImplicitOperands(MCInst &MCI) {
27   MCI.addOperand(MCOperand::CreateImm(0));
28   MCI.addOperand(MCOperand::CreateInst(nullptr));
29 }
30 
GetImplicitBits(MCInst const & MCI)31 std::bitset<16> HexagonMCInst::GetImplicitBits(MCInst const &MCI) {
32   SanityCheckImplicitOperands(MCI);
33   std::bitset<16> Bits(MCI.getOperand(MCI.getNumOperands() - 2).getImm());
34   return Bits;
35 }
36 
SetImplicitBits(MCInst & MCI,std::bitset<16> Bits)37 void HexagonMCInst::SetImplicitBits(MCInst &MCI, std::bitset<16> Bits) {
38   SanityCheckImplicitOperands(MCI);
39   MCI.getOperand(MCI.getNumOperands() - 2).setImm(Bits.to_ulong());
40 }
41 
setPacketBegin(bool f)42 void HexagonMCInst::setPacketBegin(bool f) {
43   std::bitset<16> Bits(GetImplicitBits(*this));
44   Bits.set(packetBeginIndex, f);
45   SetImplicitBits(*this, Bits);
46 }
47 
isPacketBegin() const48 bool HexagonMCInst::isPacketBegin() const {
49   std::bitset<16> Bits(GetImplicitBits(*this));
50   return Bits.test(packetBeginIndex);
51 }
52 
setPacketEnd(bool f)53 void HexagonMCInst::setPacketEnd(bool f) {
54   std::bitset<16> Bits(GetImplicitBits(*this));
55   Bits.set(packetEndIndex, f);
56   SetImplicitBits(*this, Bits);
57 }
58 
isPacketEnd() const59 bool HexagonMCInst::isPacketEnd() const {
60   std::bitset<16> Bits(GetImplicitBits(*this));
61   return Bits.test(packetEndIndex);
62 }
63 
resetPacket()64 void HexagonMCInst::resetPacket() {
65   setPacketBegin(false);
66   setPacketEnd(false);
67 }
68 
69 // Return the slots used by the insn.
getUnits(const HexagonTargetMachine * TM) const70 unsigned HexagonMCInst::getUnits(const HexagonTargetMachine *TM) const {
71   const HexagonInstrInfo *QII = TM->getSubtargetImpl()->getInstrInfo();
72   const InstrItineraryData *II =
73       TM->getSubtargetImpl()->getInstrItineraryData();
74   const InstrStage *IS =
75       II->beginStage(QII->get(this->getOpcode()).getSchedClass());
76 
77   return (IS->getUnits());
78 }
79 
getDesc() const80 MCInstrDesc const& HexagonMCInst::getDesc() const { return (MCII->get(getOpcode())); }
81 
82 // Return the Hexagon ISA class for the insn.
getType() const83 unsigned HexagonMCInst::getType() const {
84   const uint64_t F = getDesc().TSFlags;
85 
86   return ((F >> HexagonII::TypePos) & HexagonII::TypeMask);
87 }
88 
89 // Return whether the insn is an actual insn.
isCanon() const90 bool HexagonMCInst::isCanon() const {
91   return (!getDesc().isPseudo() && !isPrefix() &&
92           getType() != HexagonII::TypeENDLOOP);
93 }
94 
95 // Return whether the insn is a prefix.
isPrefix() const96 bool HexagonMCInst::isPrefix() const {
97   return (getType() == HexagonII::TypePREFIX);
98 }
99 
100 // Return whether the insn is solo, i.e., cannot be in a packet.
isSolo() const101 bool HexagonMCInst::isSolo() const {
102   const uint64_t F = getDesc().TSFlags;
103   return ((F >> HexagonII::SoloPos) & HexagonII::SoloMask);
104 }
105 
106 // Return whether the insn is a new-value consumer.
isNewValue() const107 bool HexagonMCInst::isNewValue() const {
108   const uint64_t F = getDesc().TSFlags;
109   return ((F >> HexagonII::NewValuePos) & HexagonII::NewValueMask);
110 }
111 
112 // Return whether the instruction is a legal new-value producer.
hasNewValue() const113 bool HexagonMCInst::hasNewValue() const {
114   const uint64_t F = getDesc().TSFlags;
115   return ((F >> HexagonII::hasNewValuePos) & HexagonII::hasNewValueMask);
116 }
117 
118 // Return the operand that consumes or produces a new value.
getNewValue() const119 const MCOperand &HexagonMCInst::getNewValue() const {
120   const uint64_t F = getDesc().TSFlags;
121   const unsigned O =
122       (F >> HexagonII::NewValueOpPos) & HexagonII::NewValueOpMask;
123   const MCOperand &MCO = getOperand(O);
124 
125   assert((isNewValue() || hasNewValue()) && MCO.isReg());
126   return (MCO);
127 }
128 
129 // Return whether the instruction needs to be constant extended.
130 // 1) Always return true if the instruction has 'isExtended' flag set.
131 //
132 // isExtendable:
133 // 2) For immediate extended operands, return true only if the value is
134 //    out-of-range.
135 // 3) For global address, always return true.
136 
isConstExtended(void) const137 bool HexagonMCInst::isConstExtended(void) const {
138   if (isExtended())
139     return true;
140 
141   if (!isExtendable())
142     return false;
143 
144   short ExtOpNum = getCExtOpNum();
145   int MinValue = getMinValue();
146   int MaxValue = getMaxValue();
147   const MCOperand &MO = getOperand(ExtOpNum);
148 
149   // We could be using an instruction with an extendable immediate and shoehorn
150   // a global address into it. If it is a global address it will be constant
151   // extended. We do this for COMBINE.
152   // We currently only handle isGlobal() because it is the only kind of
153   // object we are going to end up with here for now.
154   // In the future we probably should add isSymbol(), etc.
155   if (MO.isExpr())
156     return true;
157 
158   // If the extendable operand is not 'Immediate' type, the instruction should
159   // have 'isExtended' flag set.
160   assert(MO.isImm() && "Extendable operand must be Immediate type");
161 
162   int ImmValue = MO.getImm();
163   return (ImmValue < MinValue || ImmValue > MaxValue);
164 }
165 
166 // Return whether the instruction must be always extended.
isExtended(void) const167 bool HexagonMCInst::isExtended(void) const {
168   const uint64_t F = getDesc().TSFlags;
169   return (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
170 }
171 
172 // Return true if the instruction may be extended based on the operand value.
isExtendable(void) const173 bool HexagonMCInst::isExtendable(void) const {
174   const uint64_t F = getDesc().TSFlags;
175   return (F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask;
176 }
177 
178 // Return number of bits in the constant extended operand.
getBitCount(void) const179 unsigned HexagonMCInst::getBitCount(void) const {
180   const uint64_t F = getDesc().TSFlags;
181   return ((F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask);
182 }
183 
184 // Return constant extended operand number.
getCExtOpNum(void) const185 unsigned short HexagonMCInst::getCExtOpNum(void) const {
186   const uint64_t F = getDesc().TSFlags;
187   return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask);
188 }
189 
190 // Return whether the operand can be constant extended.
isOperandExtended(const unsigned short OperandNum) const191 bool HexagonMCInst::isOperandExtended(const unsigned short OperandNum) const {
192   const uint64_t F = getDesc().TSFlags;
193   return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask) ==
194          OperandNum;
195 }
196 
197 // Return the min value that a constant extendable operand can have
198 // without being extended.
getMinValue(void) const199 int HexagonMCInst::getMinValue(void) const {
200   const uint64_t F = getDesc().TSFlags;
201   unsigned isSigned =
202       (F >> HexagonII::ExtentSignedPos) & HexagonII::ExtentSignedMask;
203   unsigned bits = (F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask;
204 
205   if (isSigned) // if value is signed
206     return -1U << (bits - 1);
207   else
208     return 0;
209 }
210 
211 // Return the max value that a constant extendable operand can have
212 // without being extended.
getMaxValue(void) const213 int HexagonMCInst::getMaxValue(void) const {
214   const uint64_t F = getDesc().TSFlags;
215   unsigned isSigned =
216       (F >> HexagonII::ExtentSignedPos) & HexagonII::ExtentSignedMask;
217   unsigned bits = (F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask;
218 
219   if (isSigned) // if value is signed
220     return ~(-1U << (bits - 1));
221   else
222     return ~(-1U << bits);
223 }
224