blob: 5ea00d36e5a84262a0d1ee8ed0958b5ab266c93a [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
Colin LeMahieu68d967d2015-05-29 14:44:13 +000014#include "Hexagon.h"
Colin LeMahieuaf304e52015-02-19 19:00:00 +000015#include "HexagonBaseInfo.h"
Colin LeMahieu68d967d2015-05-29 14:44:13 +000016#include "HexagonMCInstrInfo.h"
Colin LeMahieuaf304e52015-02-19 19:00:00 +000017
18namespace llvm {
Colin LeMahieu68d967d2015-05-29 14:44:13 +000019iterator_range<MCInst::const_iterator>
20HexagonMCInstrInfo::bundleInstructions(MCInst const &MCI) {
21 assert(isBundle(MCI));
22 return iterator_range<MCInst::const_iterator>(
23 MCI.begin() + bundleInstructionsOffset, MCI.end());
24}
25
26size_t HexagonMCInstrInfo::bundleSize(MCInst const &MCI) {
27 if (HexagonMCInstrInfo::isBundle(MCI))
28 return (MCI.size() - bundleInstructionsOffset);
29 else
30 return (1);
Colin LeMahieu1174fea2015-02-19 21:10:50 +000031}
32
Colin LeMahieub6625652015-05-01 21:14:21 +000033HexagonII::MemAccessSize
34HexagonMCInstrInfo::getAccessSize(MCInstrInfo const &MCII, MCInst const &MCI) {
35 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
36
37 return (HexagonII::MemAccessSize((F >> HexagonII::MemAccessSizePos) &
38 HexagonII::MemAccesSizeMask));
39}
40
Colin LeMahieu745c4712015-02-19 19:49:27 +000041unsigned HexagonMCInstrInfo::getBitCount(MCInstrInfo const &MCII,
42 MCInst const &MCI) {
43 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
44 return ((F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask);
45}
46
Colin LeMahieu745c4712015-02-19 19:49:27 +000047// Return constant extended operand number.
48unsigned short HexagonMCInstrInfo::getCExtOpNum(MCInstrInfo const &MCII,
49 MCInst const &MCI) {
50 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
51 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask);
52}
53
Colin LeMahieu1174fea2015-02-19 21:10:50 +000054MCInstrDesc const &HexagonMCInstrInfo::getDesc(MCInstrInfo const &MCII,
55 MCInst const &MCI) {
56 return (MCII.get(MCI.getOpcode()));
57}
58
Colin LeMahieu86f218e2015-05-30 18:55:47 +000059unsigned short HexagonMCInstrInfo::getExtendableOp(MCInstrInfo const &MCII,
60 MCInst const &MCI) {
61 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
62 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask);
63}
64
65MCOperand const &
66HexagonMCInstrInfo::getExtendableOperand(MCInstrInfo const &MCII,
67 MCInst const &MCI) {
68 unsigned O = HexagonMCInstrInfo::getExtendableOp(MCII, MCI);
69 MCOperand const &MO = MCI.getOperand(O);
70
71 assert((HexagonMCInstrInfo::isExtendable(MCII, MCI) ||
72 HexagonMCInstrInfo::isExtended(MCII, MCI)) &&
73 (MO.isImm() || MO.isExpr()));
74 return (MO);
75}
76
Colin LeMahieub6625652015-05-01 21:14:21 +000077unsigned HexagonMCInstrInfo::getExtentAlignment(MCInstrInfo const &MCII,
78 MCInst const &MCI) {
79 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
80 return ((F >> HexagonII::ExtentAlignPos) & HexagonII::ExtentAlignMask);
81}
82
83unsigned HexagonMCInstrInfo::getExtentBits(MCInstrInfo const &MCII,
84 MCInst const &MCI) {
85 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
86 return ((F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask);
87}
88
Colin LeMahieuaf304e52015-02-19 19:00:00 +000089// Return the max value that a constant extendable operand can have
90// without being extended.
91int HexagonMCInstrInfo::getMaxValue(MCInstrInfo const &MCII,
92 MCInst const &MCI) {
Colin LeMahieu745c4712015-02-19 19:49:27 +000093 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
Colin LeMahieuaf304e52015-02-19 19:00:00 +000094 unsigned isSigned =
95 (F >> HexagonII::ExtentSignedPos) & HexagonII::ExtentSignedMask;
96 unsigned bits = (F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask;
97
98 if (isSigned) // if value is signed
99 return ~(-1U << (bits - 1));
100 else
101 return ~(-1U << bits);
102}
Colin LeMahieu745c4712015-02-19 19:49:27 +0000103
104// Return the min value that a constant extendable operand can have
105// without being extended.
106int HexagonMCInstrInfo::getMinValue(MCInstrInfo const &MCII,
107 MCInst const &MCI) {
108 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
109 unsigned isSigned =
110 (F >> HexagonII::ExtentSignedPos) & HexagonII::ExtentSignedMask;
111 unsigned bits = (F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask;
112
113 if (isSigned) // if value is signed
114 return -1U << (bits - 1);
115 else
116 return 0;
117}
118
Colin LeMahieub6625652015-05-01 21:14:21 +0000119char const *HexagonMCInstrInfo::getName(MCInstrInfo const &MCII,
120 MCInst const &MCI) {
121 return MCII.getName(MCI.getOpcode());
122}
123
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000124unsigned short HexagonMCInstrInfo::getNewValueOp(MCInstrInfo const &MCII,
Colin LeMahieu745c4712015-02-19 19:49:27 +0000125 MCInst const &MCI) {
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000126 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
127 return ((F >> HexagonII::NewValueOpPos) & HexagonII::NewValueOpMask);
128}
129
130MCOperand const &HexagonMCInstrInfo::getNewValueOperand(MCInstrInfo const &MCII,
131 MCInst const &MCI) {
Colin LeMahieu745c4712015-02-19 19:49:27 +0000132 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
133 unsigned const O =
134 (F >> HexagonII::NewValueOpPos) & HexagonII::NewValueOpMask;
135 MCOperand const &MCO = MCI.getOperand(O);
136
137 assert((HexagonMCInstrInfo::isNewValue(MCII, MCI) ||
138 HexagonMCInstrInfo::hasNewValue(MCII, MCI)) &&
139 MCO.isReg());
140 return (MCO);
141}
142
143// Return the Hexagon ISA class for the insn.
144unsigned HexagonMCInstrInfo::getType(MCInstrInfo const &MCII,
145 MCInst const &MCI) {
146 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
147
148 return ((F >> HexagonII::TypePos) & HexagonII::TypeMask);
149}
150
151// Return whether the instruction is a legal new-value producer.
152bool HexagonMCInstrInfo::hasNewValue(MCInstrInfo const &MCII,
153 MCInst const &MCI) {
154 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
155 return ((F >> HexagonII::hasNewValuePos) & HexagonII::hasNewValueMask);
156}
157
Colin LeMahieu86f218e2015-05-30 18:55:47 +0000158MCInst const &HexagonMCInstrInfo::instruction(MCInst const &MCB, size_t Index) {
159 assert(isBundle(MCB));
160 assert(Index < HEXAGON_PACKET_SIZE);
161 return *MCB.getOperand(bundleInstructionsOffset + Index).getInst();
162}
163
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000164bool HexagonMCInstrInfo::isBundle(MCInst const &MCI) {
165 auto Result = Hexagon::BUNDLE == MCI.getOpcode();
166 assert(!Result || (MCI.size() > 0 && MCI.getOperand(0).isImm()));
167 return Result;
168}
169
Colin LeMahieu745c4712015-02-19 19:49:27 +0000170// Return whether the insn is an actual insn.
171bool HexagonMCInstrInfo::isCanon(MCInstrInfo const &MCII, MCInst const &MCI) {
172 return (!HexagonMCInstrInfo::getDesc(MCII, MCI).isPseudo() &&
173 !HexagonMCInstrInfo::isPrefix(MCII, MCI) &&
174 HexagonMCInstrInfo::getType(MCII, MCI) != HexagonII::TypeENDLOOP);
175}
176
177// Return whether the instruction needs to be constant extended.
178// 1) Always return true if the instruction has 'isExtended' flag set.
179//
180// isExtendable:
181// 2) For immediate extended operands, return true only if the value is
182// out-of-range.
183// 3) For global address, always return true.
184
185bool HexagonMCInstrInfo::isConstExtended(MCInstrInfo const &MCII,
186 MCInst const &MCI) {
187 if (HexagonMCInstrInfo::isExtended(MCII, MCI))
188 return true;
189
190 if (!HexagonMCInstrInfo::isExtendable(MCII, MCI))
191 return false;
192
193 short ExtOpNum = HexagonMCInstrInfo::getCExtOpNum(MCII, MCI);
194 int MinValue = HexagonMCInstrInfo::getMinValue(MCII, MCI);
195 int MaxValue = HexagonMCInstrInfo::getMaxValue(MCII, MCI);
196 MCOperand const &MO = MCI.getOperand(ExtOpNum);
197
198 // We could be using an instruction with an extendable immediate and shoehorn
199 // a global address into it. If it is a global address it will be constant
200 // extended. We do this for COMBINE.
201 // We currently only handle isGlobal() because it is the only kind of
202 // object we are going to end up with here for now.
203 // In the future we probably should add isSymbol(), etc.
204 if (MO.isExpr())
205 return true;
206
207 // If the extendable operand is not 'Immediate' type, the instruction should
208 // have 'isExtended' flag set.
209 assert(MO.isImm() && "Extendable operand must be Immediate type");
210
211 int ImmValue = MO.getImm();
212 return (ImmValue < MinValue || ImmValue > MaxValue);
213}
214
215// Return true if the instruction may be extended based on the operand value.
216bool HexagonMCInstrInfo::isExtendable(MCInstrInfo const &MCII,
217 MCInst const &MCI) {
218 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
219 return (F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask;
220}
221
222// Return whether the instruction must be always extended.
223bool HexagonMCInstrInfo::isExtended(MCInstrInfo const &MCII,
224 MCInst const &MCI) {
225 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
226 return (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
227}
228
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000229bool HexagonMCInstrInfo::isImmext(MCInst const &MCI) {
230 auto Op = MCI.getOpcode();
231 return (Op == Hexagon::A4_ext_b || Op == Hexagon::A4_ext_c ||
232 Op == Hexagon::A4_ext_g || Op == Hexagon::A4_ext);
233}
234
235bool HexagonMCInstrInfo::isInnerLoop(MCInst const &MCI) {
236 assert(isBundle(MCI));
237 int64_t Flags = MCI.getOperand(0).getImm();
238 return (Flags & innerLoopMask) != 0;
239}
240
Colin LeMahieu745c4712015-02-19 19:49:27 +0000241// Return whether the insn is a new-value consumer.
242bool HexagonMCInstrInfo::isNewValue(MCInstrInfo const &MCII,
243 MCInst const &MCI) {
244 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
245 return ((F >> HexagonII::NewValuePos) & HexagonII::NewValueMask);
246}
247
248// Return whether the operand can be constant extended.
249bool HexagonMCInstrInfo::isOperandExtended(MCInstrInfo const &MCII,
250 MCInst const &MCI,
251 unsigned short OperandNum) {
252 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
253 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask) ==
254 OperandNum;
255}
256
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000257bool HexagonMCInstrInfo::isOuterLoop(MCInst const &MCI) {
258 assert(isBundle(MCI));
259 int64_t Flags = MCI.getOperand(0).getImm();
260 return (Flags & outerLoopMask) != 0;
Colin LeMahieu1174fea2015-02-19 21:10:50 +0000261}
262
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000263bool HexagonMCInstrInfo::isPredicated(MCInstrInfo const &MCII,
264 MCInst const &MCI) {
265 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
266 return ((F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
267}
268
269bool HexagonMCInstrInfo::isPredicatedTrue(MCInstrInfo const &MCII,
270 MCInst const &MCI) {
271 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
272 return (
273 !((F >> HexagonII::PredicatedFalsePos) & HexagonII::PredicatedFalseMask));
Colin LeMahieu1174fea2015-02-19 21:10:50 +0000274}
275
Colin LeMahieu745c4712015-02-19 19:49:27 +0000276// Return whether the insn is a prefix.
277bool HexagonMCInstrInfo::isPrefix(MCInstrInfo const &MCII, MCInst const &MCI) {
278 return (HexagonMCInstrInfo::getType(MCII, MCI) == HexagonII::TypePREFIX);
279}
280
281// Return whether the insn is solo, i.e., cannot be in a packet.
282bool HexagonMCInstrInfo::isSolo(MCInstrInfo const &MCII, MCInst const &MCI) {
283 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
284 return ((F >> HexagonII::SoloPos) & HexagonII::SoloMask);
285}
Colin LeMahieu1174fea2015-02-19 21:10:50 +0000286
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000287void HexagonMCInstrInfo::padEndloop(MCInst &MCB) {
288 MCInst Nop;
289 Nop.setOpcode(Hexagon::A2_nop);
290 assert(isBundle(MCB));
291 while ((HexagonMCInstrInfo::isInnerLoop(MCB) &&
292 (HexagonMCInstrInfo::bundleSize(MCB) < HEXAGON_PACKET_INNER_SIZE)) ||
293 ((HexagonMCInstrInfo::isOuterLoop(MCB) &&
294 (HexagonMCInstrInfo::bundleSize(MCB) < HEXAGON_PACKET_OUTER_SIZE))))
295 MCB.addOperand(MCOperand::createInst(new MCInst(Nop)));
Colin LeMahieu1174fea2015-02-19 21:10:50 +0000296}
297
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000298void HexagonMCInstrInfo::setInnerLoop(MCInst &MCI) {
299 assert(isBundle(MCI));
300 MCOperand &Operand = MCI.getOperand(0);
301 Operand.setImm(Operand.getImm() | innerLoopMask);
Colin LeMahieu1174fea2015-02-19 21:10:50 +0000302}
303
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000304void HexagonMCInstrInfo::setOuterLoop(MCInst &MCI) {
305 assert(isBundle(MCI));
306 MCOperand &Operand = MCI.getOperand(0);
307 Operand.setImm(Operand.getImm() | outerLoopMask);
Colin LeMahieu1174fea2015-02-19 21:10:50 +0000308}
Colin LeMahieuaf304e52015-02-19 19:00:00 +0000309}