blob: 1effad6c2aaca98b3f42f274e484b2c7c8674b1b [file] [log] [blame]
Colin LeMahieuaf304e52015-02-19 19:00:00 +00001//===- HexagonMCInstrInfo.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 MCInstrInfo to allow Hexagon specific MCInstr queries
11//
12//===----------------------------------------------------------------------===//
13
14#include "HexagonMCInstrInfo.h"
15#include "HexagonBaseInfo.h"
Colin LeMahieuaf304e52015-02-19 19:00:00 +000016
17namespace llvm {
Colin LeMahieu1174fea2015-02-19 21:10:50 +000018void HexagonMCInstrInfo::AppendImplicitOperands(MCInst &MCI) {
19 MCI.addOperand(MCOperand::CreateImm(0));
20 MCI.addOperand(MCOperand::CreateInst(nullptr));
21}
22
Colin LeMahieub6625652015-05-01 21:14:21 +000023HexagonII::MemAccessSize
24HexagonMCInstrInfo::getAccessSize(MCInstrInfo const &MCII, MCInst const &MCI) {
25 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
26
27 return (HexagonII::MemAccessSize((F >> HexagonII::MemAccessSizePos) &
28 HexagonII::MemAccesSizeMask));
29}
30
Colin LeMahieu745c4712015-02-19 19:49:27 +000031unsigned HexagonMCInstrInfo::getBitCount(MCInstrInfo const &MCII,
32 MCInst const &MCI) {
33 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
34 return ((F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask);
35}
36
Colin LeMahieu745c4712015-02-19 19:49:27 +000037// Return constant extended operand number.
38unsigned short HexagonMCInstrInfo::getCExtOpNum(MCInstrInfo const &MCII,
39 MCInst const &MCI) {
40 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
41 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask);
42}
43
Colin LeMahieu1174fea2015-02-19 21:10:50 +000044MCInstrDesc const &HexagonMCInstrInfo::getDesc(MCInstrInfo const &MCII,
45 MCInst const &MCI) {
46 return (MCII.get(MCI.getOpcode()));
47}
48
Colin LeMahieub6625652015-05-01 21:14:21 +000049unsigned HexagonMCInstrInfo::getExtentAlignment(MCInstrInfo const &MCII,
50 MCInst const &MCI) {
51 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
52 return ((F >> HexagonII::ExtentAlignPos) & HexagonII::ExtentAlignMask);
53}
54
55unsigned HexagonMCInstrInfo::getExtentBits(MCInstrInfo const &MCII,
56 MCInst const &MCI) {
57 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
58 return ((F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask);
59}
60
Colin LeMahieu1174fea2015-02-19 21:10:50 +000061std::bitset<16> HexagonMCInstrInfo::GetImplicitBits(MCInst const &MCI) {
62 SanityCheckImplicitOperands(MCI);
63 std::bitset<16> Bits(MCI.getOperand(MCI.getNumOperands() - 2).getImm());
64 return Bits;
65}
66
Colin LeMahieuaf304e52015-02-19 19:00:00 +000067// Return the max value that a constant extendable operand can have
68// without being extended.
69int HexagonMCInstrInfo::getMaxValue(MCInstrInfo const &MCII,
70 MCInst const &MCI) {
Colin LeMahieu745c4712015-02-19 19:49:27 +000071 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
Colin LeMahieuaf304e52015-02-19 19:00:00 +000072 unsigned isSigned =
73 (F >> HexagonII::ExtentSignedPos) & HexagonII::ExtentSignedMask;
74 unsigned bits = (F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask;
75
76 if (isSigned) // if value is signed
77 return ~(-1U << (bits - 1));
78 else
79 return ~(-1U << bits);
80}
Colin LeMahieu745c4712015-02-19 19:49:27 +000081
82// Return the min value that a constant extendable operand can have
83// without being extended.
84int HexagonMCInstrInfo::getMinValue(MCInstrInfo const &MCII,
85 MCInst const &MCI) {
86 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
87 unsigned isSigned =
88 (F >> HexagonII::ExtentSignedPos) & HexagonII::ExtentSignedMask;
89 unsigned bits = (F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask;
90
91 if (isSigned) // if value is signed
92 return -1U << (bits - 1);
93 else
94 return 0;
95}
96
Colin LeMahieub6625652015-05-01 21:14:21 +000097char const *HexagonMCInstrInfo::getName(MCInstrInfo const &MCII,
98 MCInst const &MCI) {
99 return MCII.getName(MCI.getOpcode());
100}
101
Colin LeMahieu745c4712015-02-19 19:49:27 +0000102// Return the operand that consumes or produces a new value.
103MCOperand const &HexagonMCInstrInfo::getNewValue(MCInstrInfo const &MCII,
104 MCInst const &MCI) {
105 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
106 unsigned const O =
107 (F >> HexagonII::NewValueOpPos) & HexagonII::NewValueOpMask;
108 MCOperand const &MCO = MCI.getOperand(O);
109
110 assert((HexagonMCInstrInfo::isNewValue(MCII, MCI) ||
111 HexagonMCInstrInfo::hasNewValue(MCII, MCI)) &&
112 MCO.isReg());
113 return (MCO);
114}
115
116// Return the Hexagon ISA class for the insn.
117unsigned HexagonMCInstrInfo::getType(MCInstrInfo const &MCII,
118 MCInst const &MCI) {
119 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
120
121 return ((F >> HexagonII::TypePos) & HexagonII::TypeMask);
122}
123
124// Return whether the instruction is a legal new-value producer.
125bool HexagonMCInstrInfo::hasNewValue(MCInstrInfo const &MCII,
126 MCInst const &MCI) {
127 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
128 return ((F >> HexagonII::hasNewValuePos) & HexagonII::hasNewValueMask);
129}
130
131// Return whether the insn is an actual insn.
132bool HexagonMCInstrInfo::isCanon(MCInstrInfo const &MCII, MCInst const &MCI) {
133 return (!HexagonMCInstrInfo::getDesc(MCII, MCI).isPseudo() &&
134 !HexagonMCInstrInfo::isPrefix(MCII, MCI) &&
135 HexagonMCInstrInfo::getType(MCII, MCI) != HexagonII::TypeENDLOOP);
136}
137
138// Return whether the instruction needs to be constant extended.
139// 1) Always return true if the instruction has 'isExtended' flag set.
140//
141// isExtendable:
142// 2) For immediate extended operands, return true only if the value is
143// out-of-range.
144// 3) For global address, always return true.
145
146bool HexagonMCInstrInfo::isConstExtended(MCInstrInfo const &MCII,
147 MCInst const &MCI) {
148 if (HexagonMCInstrInfo::isExtended(MCII, MCI))
149 return true;
150
151 if (!HexagonMCInstrInfo::isExtendable(MCII, MCI))
152 return false;
153
154 short ExtOpNum = HexagonMCInstrInfo::getCExtOpNum(MCII, MCI);
155 int MinValue = HexagonMCInstrInfo::getMinValue(MCII, MCI);
156 int MaxValue = HexagonMCInstrInfo::getMaxValue(MCII, MCI);
157 MCOperand const &MO = MCI.getOperand(ExtOpNum);
158
159 // We could be using an instruction with an extendable immediate and shoehorn
160 // a global address into it. If it is a global address it will be constant
161 // extended. We do this for COMBINE.
162 // We currently only handle isGlobal() because it is the only kind of
163 // object we are going to end up with here for now.
164 // In the future we probably should add isSymbol(), etc.
165 if (MO.isExpr())
166 return true;
167
168 // If the extendable operand is not 'Immediate' type, the instruction should
169 // have 'isExtended' flag set.
170 assert(MO.isImm() && "Extendable operand must be Immediate type");
171
172 int ImmValue = MO.getImm();
173 return (ImmValue < MinValue || ImmValue > MaxValue);
174}
175
176// Return true if the instruction may be extended based on the operand value.
177bool HexagonMCInstrInfo::isExtendable(MCInstrInfo const &MCII,
178 MCInst const &MCI) {
179 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
180 return (F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask;
181}
182
183// Return whether the instruction must be always extended.
184bool HexagonMCInstrInfo::isExtended(MCInstrInfo const &MCII,
185 MCInst const &MCI) {
186 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
187 return (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
188}
189
190// Return whether the insn is a new-value consumer.
191bool HexagonMCInstrInfo::isNewValue(MCInstrInfo const &MCII,
192 MCInst const &MCI) {
193 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
194 return ((F >> HexagonII::NewValuePos) & HexagonII::NewValueMask);
195}
196
197// Return whether the operand can be constant extended.
198bool HexagonMCInstrInfo::isOperandExtended(MCInstrInfo const &MCII,
199 MCInst const &MCI,
200 unsigned short OperandNum) {
201 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
202 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask) ==
203 OperandNum;
204}
205
Colin LeMahieu1174fea2015-02-19 21:10:50 +0000206bool HexagonMCInstrInfo::isPacketBegin(MCInst const &MCI) {
207 std::bitset<16> Bits(GetImplicitBits(MCI));
208 return Bits.test(packetBeginIndex);
209}
210
211bool HexagonMCInstrInfo::isPacketEnd(MCInst const &MCI) {
212 std::bitset<16> Bits(GetImplicitBits(MCI));
213 return Bits.test(packetEndIndex);
214}
215
Colin LeMahieu745c4712015-02-19 19:49:27 +0000216// Return whether the insn is a prefix.
217bool HexagonMCInstrInfo::isPrefix(MCInstrInfo const &MCII, MCInst const &MCI) {
218 return (HexagonMCInstrInfo::getType(MCII, MCI) == HexagonII::TypePREFIX);
219}
220
221// Return whether the insn is solo, i.e., cannot be in a packet.
222bool HexagonMCInstrInfo::isSolo(MCInstrInfo const &MCII, MCInst const &MCI) {
223 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
224 return ((F >> HexagonII::SoloPos) & HexagonII::SoloMask);
225}
Colin LeMahieu1174fea2015-02-19 21:10:50 +0000226
227void HexagonMCInstrInfo::resetPacket(MCInst &MCI) {
228 setPacketBegin(MCI, false);
229 setPacketEnd(MCI, false);
230}
231
232void HexagonMCInstrInfo::SetImplicitBits(MCInst &MCI, std::bitset<16> Bits) {
233 SanityCheckImplicitOperands(MCI);
234 MCI.getOperand(MCI.getNumOperands() - 2).setImm(Bits.to_ulong());
235}
236
237void HexagonMCInstrInfo::setPacketBegin(MCInst &MCI, bool f) {
238 std::bitset<16> Bits(GetImplicitBits(MCI));
239 Bits.set(packetBeginIndex, f);
240 SetImplicitBits(MCI, Bits);
241}
242
243void HexagonMCInstrInfo::setPacketEnd(MCInst &MCI, bool f) {
244 std::bitset<16> Bits(GetImplicitBits(MCI));
245 Bits.set(packetEndIndex, f);
246 SetImplicitBits(MCI, Bits);
247}
Colin LeMahieuaf304e52015-02-19 19:00:00 +0000248}