blob: 7b0760e831892e59d3cb136fff3d6569332a1a29 [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 LeMahieub23c47b2015-05-31 21:57:09 +000014#include "HexagonMCInstrInfo.h"
15
Colin LeMahieu68d967d2015-05-29 14:44:13 +000016#include "Hexagon.h"
Colin LeMahieuaf304e52015-02-19 19:00:00 +000017#include "HexagonBaseInfo.h"
Colin LeMahieub23c47b2015-05-31 21:57:09 +000018
19#include "llvm/MC/MCSubtargetInfo.h"
Colin LeMahieuaf304e52015-02-19 19:00:00 +000020
21namespace llvm {
Colin LeMahieu68d967d2015-05-29 14:44:13 +000022iterator_range<MCInst::const_iterator>
23HexagonMCInstrInfo::bundleInstructions(MCInst const &MCI) {
24 assert(isBundle(MCI));
25 return iterator_range<MCInst::const_iterator>(
26 MCI.begin() + bundleInstructionsOffset, MCI.end());
27}
28
29size_t HexagonMCInstrInfo::bundleSize(MCInst const &MCI) {
30 if (HexagonMCInstrInfo::isBundle(MCI))
31 return (MCI.size() - bundleInstructionsOffset);
32 else
33 return (1);
Colin LeMahieu1174fea2015-02-19 21:10:50 +000034}
35
Colin LeMahieub6625652015-05-01 21:14:21 +000036HexagonII::MemAccessSize
37HexagonMCInstrInfo::getAccessSize(MCInstrInfo const &MCII, MCInst const &MCI) {
38 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
39
40 return (HexagonII::MemAccessSize((F >> HexagonII::MemAccessSizePos) &
41 HexagonII::MemAccesSizeMask));
42}
43
Colin LeMahieu745c4712015-02-19 19:49:27 +000044unsigned HexagonMCInstrInfo::getBitCount(MCInstrInfo const &MCII,
45 MCInst const &MCI) {
46 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
47 return ((F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask);
48}
49
Colin LeMahieu745c4712015-02-19 19:49:27 +000050// Return constant extended operand number.
51unsigned short HexagonMCInstrInfo::getCExtOpNum(MCInstrInfo const &MCII,
52 MCInst const &MCI) {
53 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
54 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask);
55}
56
Colin LeMahieu1174fea2015-02-19 21:10:50 +000057MCInstrDesc const &HexagonMCInstrInfo::getDesc(MCInstrInfo const &MCII,
58 MCInst const &MCI) {
59 return (MCII.get(MCI.getOpcode()));
60}
61
Colin LeMahieu86f218e2015-05-30 18:55:47 +000062unsigned short HexagonMCInstrInfo::getExtendableOp(MCInstrInfo const &MCII,
63 MCInst const &MCI) {
64 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
65 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask);
66}
67
68MCOperand const &
69HexagonMCInstrInfo::getExtendableOperand(MCInstrInfo const &MCII,
70 MCInst const &MCI) {
71 unsigned O = HexagonMCInstrInfo::getExtendableOp(MCII, MCI);
72 MCOperand const &MO = MCI.getOperand(O);
73
74 assert((HexagonMCInstrInfo::isExtendable(MCII, MCI) ||
75 HexagonMCInstrInfo::isExtended(MCII, MCI)) &&
76 (MO.isImm() || MO.isExpr()));
77 return (MO);
78}
79
Colin LeMahieub6625652015-05-01 21:14:21 +000080unsigned HexagonMCInstrInfo::getExtentAlignment(MCInstrInfo const &MCII,
81 MCInst const &MCI) {
82 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
83 return ((F >> HexagonII::ExtentAlignPos) & HexagonII::ExtentAlignMask);
84}
85
86unsigned HexagonMCInstrInfo::getExtentBits(MCInstrInfo const &MCII,
87 MCInst const &MCI) {
88 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
89 return ((F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask);
90}
91
Colin LeMahieuaf304e52015-02-19 19:00:00 +000092// Return the max value that a constant extendable operand can have
93// without being extended.
94int HexagonMCInstrInfo::getMaxValue(MCInstrInfo const &MCII,
95 MCInst const &MCI) {
Colin LeMahieu745c4712015-02-19 19:49:27 +000096 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
Colin LeMahieuaf304e52015-02-19 19:00:00 +000097 unsigned isSigned =
98 (F >> HexagonII::ExtentSignedPos) & HexagonII::ExtentSignedMask;
99 unsigned bits = (F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask;
100
101 if (isSigned) // if value is signed
102 return ~(-1U << (bits - 1));
103 else
104 return ~(-1U << bits);
105}
Colin LeMahieu745c4712015-02-19 19:49:27 +0000106
107// Return the min value that a constant extendable operand can have
108// without being extended.
109int HexagonMCInstrInfo::getMinValue(MCInstrInfo const &MCII,
110 MCInst const &MCI) {
111 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
112 unsigned isSigned =
113 (F >> HexagonII::ExtentSignedPos) & HexagonII::ExtentSignedMask;
114 unsigned bits = (F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask;
115
116 if (isSigned) // if value is signed
117 return -1U << (bits - 1);
118 else
119 return 0;
120}
121
Colin LeMahieub6625652015-05-01 21:14:21 +0000122char const *HexagonMCInstrInfo::getName(MCInstrInfo const &MCII,
123 MCInst const &MCI) {
124 return MCII.getName(MCI.getOpcode());
125}
126
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000127unsigned short HexagonMCInstrInfo::getNewValueOp(MCInstrInfo const &MCII,
Colin LeMahieu745c4712015-02-19 19:49:27 +0000128 MCInst const &MCI) {
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000129 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
130 return ((F >> HexagonII::NewValueOpPos) & HexagonII::NewValueOpMask);
131}
132
133MCOperand const &HexagonMCInstrInfo::getNewValueOperand(MCInstrInfo const &MCII,
134 MCInst const &MCI) {
Colin LeMahieu745c4712015-02-19 19:49:27 +0000135 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
136 unsigned const O =
137 (F >> HexagonII::NewValueOpPos) & HexagonII::NewValueOpMask;
138 MCOperand const &MCO = MCI.getOperand(O);
139
140 assert((HexagonMCInstrInfo::isNewValue(MCII, MCI) ||
141 HexagonMCInstrInfo::hasNewValue(MCII, MCI)) &&
142 MCO.isReg());
143 return (MCO);
144}
145
Colin LeMahieub23c47b2015-05-31 21:57:09 +0000146int HexagonMCInstrInfo::getSubTarget(MCInstrInfo const &MCII,
147 MCInst const &MCI) {
148 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
149
150 HexagonII::SubTarget Target = static_cast<HexagonII::SubTarget>(
151 (F >> HexagonII::validSubTargetPos) & HexagonII::validSubTargetMask);
152
153 switch (Target) {
154 default:
155 return Hexagon::ArchV4;
156 case HexagonII::HasV5SubT:
157 return Hexagon::ArchV5;
158 }
159}
160
Colin LeMahieu745c4712015-02-19 19:49:27 +0000161// Return the Hexagon ISA class for the insn.
162unsigned HexagonMCInstrInfo::getType(MCInstrInfo const &MCII,
163 MCInst const &MCI) {
164 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
165
166 return ((F >> HexagonII::TypePos) & HexagonII::TypeMask);
167}
168
Colin LeMahieub23c47b2015-05-31 21:57:09 +0000169unsigned HexagonMCInstrInfo::getUnits(MCInstrInfo const &MCII,
170 MCSubtargetInfo const &STI,
171 MCInst const &MCI) {
172
173 const InstrItinerary *II = STI.getSchedModel().InstrItineraries;
174 int SchedClass = HexagonMCInstrInfo::getDesc(MCII, MCI).getSchedClass();
175 return ((II[SchedClass].FirstStage + HexagonStages)->getUnits());
176}
177
178bool HexagonMCInstrInfo::hasImmExt(MCInst const &MCI) {
179 if (!HexagonMCInstrInfo::isBundle(MCI))
180 return false;
181
182 for (const auto &I : HexagonMCInstrInfo::bundleInstructions(MCI)) {
183 auto MI = I.getInst();
184 if (isImmext(*MI))
185 return true;
186 }
187
188 return false;
189}
190
Colin LeMahieu745c4712015-02-19 19:49:27 +0000191// Return whether the instruction is a legal new-value producer.
192bool HexagonMCInstrInfo::hasNewValue(MCInstrInfo const &MCII,
193 MCInst const &MCI) {
194 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
195 return ((F >> HexagonII::hasNewValuePos) & HexagonII::hasNewValueMask);
196}
197
Colin LeMahieu86f218e2015-05-30 18:55:47 +0000198MCInst 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();
202}
203
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000204bool HexagonMCInstrInfo::isBundle(MCInst const &MCI) {
205 auto Result = Hexagon::BUNDLE == MCI.getOpcode();
206 assert(!Result || (MCI.size() > 0 && MCI.getOperand(0).isImm()));
207 return Result;
208}
209
Colin LeMahieu745c4712015-02-19 19:49:27 +0000210// Return whether the insn is an actual insn.
211bool 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);
215}
216
217// Return whether the instruction needs to be constant extended.
218// 1) Always return true if the instruction has 'isExtended' flag set.
219//
220// isExtendable:
221// 2) For immediate extended operands, return true only if the value is
222// out-of-range.
223// 3) For global address, always return true.
224
225bool HexagonMCInstrInfo::isConstExtended(MCInstrInfo const &MCII,
226 MCInst const &MCI) {
227 if (HexagonMCInstrInfo::isExtended(MCII, MCI))
228 return true;
229
230 if (!HexagonMCInstrInfo::isExtendable(MCII, MCI))
231 return false;
232
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);
237
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.
244 if (MO.isExpr())
245 return true;
246
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");
250
251 int ImmValue = MO.getImm();
252 return (ImmValue < MinValue || ImmValue > MaxValue);
253}
254
Colin LeMahieu745c4712015-02-19 19:49:27 +0000255bool HexagonMCInstrInfo::isExtendable(MCInstrInfo const &MCII,
256 MCInst const &MCI) {
257 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
258 return (F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask;
259}
260
Colin LeMahieu745c4712015-02-19 19:49:27 +0000261bool HexagonMCInstrInfo::isExtended(MCInstrInfo const &MCII,
262 MCInst const &MCI) {
263 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
264 return (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
265}
266
Colin LeMahieub23c47b2015-05-31 21:57:09 +0000267bool 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);
270}
271
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000272bool 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);
276}
277
278bool HexagonMCInstrInfo::isInnerLoop(MCInst const &MCI) {
279 assert(isBundle(MCI));
280 int64_t Flags = MCI.getOperand(0).getImm();
281 return (Flags & innerLoopMask) != 0;
282}
283
Colin LeMahieu745c4712015-02-19 19:49:27 +0000284// Return whether the insn is a new-value consumer.
285bool HexagonMCInstrInfo::isNewValue(MCInstrInfo const &MCII,
286 MCInst const &MCI) {
287 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
288 return ((F >> HexagonII::NewValuePos) & HexagonII::NewValueMask);
289}
290
291// Return whether the operand can be constant extended.
292bool HexagonMCInstrInfo::isOperandExtended(MCInstrInfo const &MCII,
293 MCInst const &MCI,
294 unsigned short OperandNum) {
295 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
296 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask) ==
297 OperandNum;
298}
299
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000300bool HexagonMCInstrInfo::isOuterLoop(MCInst const &MCI) {
301 assert(isBundle(MCI));
302 int64_t Flags = MCI.getOperand(0).getImm();
303 return (Flags & outerLoopMask) != 0;
Colin LeMahieu1174fea2015-02-19 21:10:50 +0000304}
305
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000306bool HexagonMCInstrInfo::isPredicated(MCInstrInfo const &MCII,
307 MCInst const &MCI) {
308 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
309 return ((F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
310}
311
312bool HexagonMCInstrInfo::isPredicatedTrue(MCInstrInfo const &MCII,
313 MCInst const &MCI) {
314 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
315 return (
316 !((F >> HexagonII::PredicatedFalsePos) & HexagonII::PredicatedFalseMask));
Colin LeMahieu1174fea2015-02-19 21:10:50 +0000317}
318
Colin LeMahieu745c4712015-02-19 19:49:27 +0000319bool HexagonMCInstrInfo::isPrefix(MCInstrInfo const &MCII, MCInst const &MCI) {
320 return (HexagonMCInstrInfo::getType(MCII, MCI) == HexagonII::TypePREFIX);
321}
322
Colin LeMahieu745c4712015-02-19 19:49:27 +0000323bool 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);
326}
Colin LeMahieu1174fea2015-02-19 21:10:50 +0000327
Colin LeMahieub23c47b2015-05-31 21:57:09 +0000328bool 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);
331}
332
333bool HexagonMCInstrInfo::isSoloAin1(MCInstrInfo const &MCII,
334 MCInst const &MCI) {
335 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
336 return ((F >> HexagonII::SoloAin1Pos) & HexagonII::SoloAin1Mask);
337}
338
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000339void HexagonMCInstrInfo::padEndloop(MCInst &MCB) {
340 MCInst Nop;
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)));
Colin LeMahieu1174fea2015-02-19 21:10:50 +0000348}
349
Colin LeMahieub23c47b2015-05-31 21:57:09 +0000350bool HexagonMCInstrInfo::prefersSlot3(MCInstrInfo const &MCII,
351 MCInst const &MCI) {
352 if (HexagonMCInstrInfo::getType(MCII, MCI) == HexagonII::TypeCR)
353 return false;
354
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:
365 return true;
366 }
367 return false;
368}
369
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000370void HexagonMCInstrInfo::setInnerLoop(MCInst &MCI) {
371 assert(isBundle(MCI));
372 MCOperand &Operand = MCI.getOperand(0);
373 Operand.setImm(Operand.getImm() | innerLoopMask);
Colin LeMahieu1174fea2015-02-19 21:10:50 +0000374}
375
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000376void HexagonMCInstrInfo::setOuterLoop(MCInst &MCI) {
377 assert(isBundle(MCI));
378 MCOperand &Operand = MCI.getOperand(0);
379 Operand.setImm(Operand.getImm() | outerLoopMask);
Colin LeMahieu1174fea2015-02-19 21:10:50 +0000380}
Colin LeMahieuaf304e52015-02-19 19:00:00 +0000381}