blob: 3af1e14319ee0c8b2074fc1ac32c4c36ecbe4f64 [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 LeMahieu7cd08922015-11-09 04:07:48 +000018#include "HexagonMCChecker.h"
Colin LeMahieub23c47b2015-05-31 21:57:09 +000019
Colin LeMahieube8c4532015-06-05 16:00:11 +000020#include "llvm/MC/MCContext.h"
Colin LeMahieu7cd08922015-11-09 04:07:48 +000021#include "llvm/MC/MCExpr.h"
Colin LeMahieube8c4532015-06-05 16:00:11 +000022#include "llvm/MC/MCInstrInfo.h"
Colin LeMahieub23c47b2015-05-31 21:57:09 +000023#include "llvm/MC/MCSubtargetInfo.h"
Colin LeMahieuaf304e52015-02-19 19:00:00 +000024
25namespace llvm {
Colin LeMahieu7cd08922015-11-09 04:07:48 +000026void HexagonMCInstrInfo::addConstant(MCInst &MI, uint64_t Value,
27 MCContext &Context) {
28 MI.addOperand(MCOperand::createExpr(MCConstantExpr::create(Value, Context)));
29}
30
Benjamin Kramer7c576d82015-11-12 19:30:40 +000031void HexagonMCInstrInfo::addConstExtender(MCContext &Context,
32 MCInstrInfo const &MCII, MCInst &MCB,
Colin LeMahieu7cd08922015-11-09 04:07:48 +000033 MCInst const &MCI) {
34 assert(HexagonMCInstrInfo::isBundle(MCB));
35 MCOperand const &exOp =
36 MCI.getOperand(HexagonMCInstrInfo::getExtendableOp(MCII, MCI));
37
38 // Create the extender.
39 MCInst *XMCI =
Benjamin Kramer7c576d82015-11-12 19:30:40 +000040 new (Context) MCInst(HexagonMCInstrInfo::deriveExtender(MCII, MCI, exOp));
Colin LeMahieu7cd08922015-11-09 04:07:48 +000041
42 MCB.addOperand(MCOperand::createInst(XMCI));
43}
44
Colin LeMahieu68d967d2015-05-29 14:44:13 +000045iterator_range<MCInst::const_iterator>
46HexagonMCInstrInfo::bundleInstructions(MCInst const &MCI) {
47 assert(isBundle(MCI));
48 return iterator_range<MCInst::const_iterator>(
49 MCI.begin() + bundleInstructionsOffset, MCI.end());
50}
51
52size_t HexagonMCInstrInfo::bundleSize(MCInst const &MCI) {
53 if (HexagonMCInstrInfo::isBundle(MCI))
54 return (MCI.size() - bundleInstructionsOffset);
55 else
56 return (1);
Colin LeMahieu1174fea2015-02-19 21:10:50 +000057}
58
Colin LeMahieu7cd08922015-11-09 04:07:48 +000059bool HexagonMCInstrInfo::canonicalizePacket(MCInstrInfo const &MCII,
60 MCSubtargetInfo const &STI,
61 MCContext &Context, MCInst &MCB,
62 HexagonMCChecker *Check) {
63 // Examine the packet and convert pairs of instructions to compound
64 // instructions when possible.
65 if (!HexagonDisableCompound)
66 HexagonMCInstrInfo::tryCompound(MCII, Context, MCB);
67 // Check the bundle for errors.
68 bool CheckOk = Check ? Check->check() : true;
69 if (!CheckOk)
70 return false;
71 HexagonMCShuffle(MCII, STI, MCB);
72 // Examine the packet and convert pairs of instructions to duplex
73 // instructions when possible.
74 MCInst InstBundlePreDuplex = MCInst(MCB);
75 if (!HexagonDisableDuplex) {
76 SmallVector<DuplexCandidate, 8> possibleDuplexes;
77 possibleDuplexes = HexagonMCInstrInfo::getDuplexPossibilties(MCII, MCB);
78 HexagonMCShuffle(MCII, STI, Context, MCB, possibleDuplexes);
79 }
80 // Examines packet and pad the packet, if needed, when an
81 // end-loop is in the bundle.
Colin LeMahieub3c97272015-11-13 07:58:06 +000082 HexagonMCInstrInfo::padEndloop(Context, MCB);
Colin LeMahieu7cd08922015-11-09 04:07:48 +000083 // If compounding and duplexing didn't reduce the size below
84 // 4 or less we have a packet that is too big.
85 if (HexagonMCInstrInfo::bundleSize(MCB) > HEXAGON_PACKET_SIZE)
86 return false;
87 HexagonMCShuffle(MCII, STI, MCB);
88 return true;
89}
90
Colin LeMahieu8ab7e8e2015-11-10 00:02:27 +000091void HexagonMCInstrInfo::clampExtended(MCInstrInfo const &MCII,
92 MCContext &Context, MCInst &MCI) {
Colin LeMahieube99a022015-06-17 03:06:16 +000093 assert(HexagonMCInstrInfo::isExtendable(MCII, MCI) ||
94 HexagonMCInstrInfo::isExtended(MCII, MCI));
95 MCOperand &exOp =
96 MCI.getOperand(HexagonMCInstrInfo::getExtendableOp(MCII, MCI));
97 // If the extended value is a constant, then use it for the extended and
98 // for the extender instructions, masking off the lower 6 bits and
99 // including the assumed bits.
Colin LeMahieu8ab7e8e2015-11-10 00:02:27 +0000100 int64_t Value;
101 if (exOp.getExpr()->evaluateAsAbsolute(Value)) {
Colin LeMahieube99a022015-06-17 03:06:16 +0000102 unsigned Shift = HexagonMCInstrInfo::getExtentAlignment(MCII, MCI);
Colin LeMahieu8ab7e8e2015-11-10 00:02:27 +0000103 exOp.setExpr(MCConstantExpr::create((Value & 0x3f) << Shift, Context));
Colin LeMahieube99a022015-06-17 03:06:16 +0000104 }
105}
106
Colin LeMahieube8c4532015-06-05 16:00:11 +0000107MCInst *HexagonMCInstrInfo::deriveDuplex(MCContext &Context, unsigned iClass,
108 MCInst const &inst0,
109 MCInst const &inst1) {
110 assert((iClass <= 0xf) && "iClass must have range of 0 to 0xf");
111 MCInst *duplexInst = new (Context) MCInst;
112 duplexInst->setOpcode(Hexagon::DuplexIClass0 + iClass);
113
114 MCInst *SubInst0 = new (Context) MCInst(deriveSubInst(inst0));
115 MCInst *SubInst1 = new (Context) MCInst(deriveSubInst(inst1));
116 duplexInst->addOperand(MCOperand::createInst(SubInst0));
117 duplexInst->addOperand(MCOperand::createInst(SubInst1));
118 return duplexInst;
119}
120
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000121MCInst HexagonMCInstrInfo::deriveExtender(MCInstrInfo const &MCII,
122 MCInst const &Inst,
123 MCOperand const &MO) {
124 assert(HexagonMCInstrInfo::isExtendable(MCII, Inst) ||
125 HexagonMCInstrInfo::isExtended(MCII, Inst));
126
127 MCInstrDesc const &Desc = HexagonMCInstrInfo::getDesc(MCII, Inst);
128 MCInst XMI;
129 XMI.setOpcode((Desc.isBranch() || Desc.isCall() ||
130 HexagonMCInstrInfo::getType(MCII, Inst) == HexagonII::TypeCR)
131 ? Hexagon::A4_ext_b
132 : Hexagon::A4_ext);
133 if (MO.isImm())
134 XMI.addOperand(MCOperand::createImm(MO.getImm() & (~0x3f)));
135 else if (MO.isExpr())
136 XMI.addOperand(MCOperand::createExpr(MO.getExpr()));
137 else
138 llvm_unreachable("invalid extendable operand");
139 return XMI;
140}
141
Colin LeMahieube8c4532015-06-05 16:00:11 +0000142MCInst const *HexagonMCInstrInfo::extenderForIndex(MCInst const &MCB,
143 size_t Index) {
144 assert(Index <= bundleSize(MCB));
145 if (Index == 0)
146 return nullptr;
147 MCInst const *Inst =
148 MCB.getOperand(Index + bundleInstructionsOffset - 1).getInst();
149 if (isImmext(*Inst))
150 return Inst;
151 return nullptr;
152}
153
Benjamin Kramer7c576d82015-11-12 19:30:40 +0000154void HexagonMCInstrInfo::extendIfNeeded(MCContext &Context,
155 MCInstrInfo const &MCII, MCInst &MCB,
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000156 MCInst const &MCI, bool MustExtend) {
157 if (isConstExtended(MCII, MCI) || MustExtend)
Benjamin Kramer7c576d82015-11-12 19:30:40 +0000158 addConstExtender(Context, MCII, MCB, MCI);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000159}
160
Colin LeMahieub6625652015-05-01 21:14:21 +0000161HexagonII::MemAccessSize
162HexagonMCInstrInfo::getAccessSize(MCInstrInfo const &MCII, MCInst const &MCI) {
163 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
164
165 return (HexagonII::MemAccessSize((F >> HexagonII::MemAccessSizePos) &
166 HexagonII::MemAccesSizeMask));
167}
168
Colin LeMahieu745c4712015-02-19 19:49:27 +0000169unsigned HexagonMCInstrInfo::getBitCount(MCInstrInfo const &MCII,
170 MCInst const &MCI) {
171 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
172 return ((F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask);
173}
174
Colin LeMahieu745c4712015-02-19 19:49:27 +0000175// Return constant extended operand number.
176unsigned short HexagonMCInstrInfo::getCExtOpNum(MCInstrInfo const &MCII,
177 MCInst const &MCI) {
178 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
179 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask);
180}
181
Colin LeMahieu1174fea2015-02-19 21:10:50 +0000182MCInstrDesc const &HexagonMCInstrInfo::getDesc(MCInstrInfo const &MCII,
183 MCInst const &MCI) {
184 return (MCII.get(MCI.getOpcode()));
185}
186
Colin LeMahieu86f218e2015-05-30 18:55:47 +0000187unsigned short HexagonMCInstrInfo::getExtendableOp(MCInstrInfo const &MCII,
188 MCInst const &MCI) {
189 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
190 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask);
191}
192
193MCOperand const &
194HexagonMCInstrInfo::getExtendableOperand(MCInstrInfo const &MCII,
195 MCInst const &MCI) {
196 unsigned O = HexagonMCInstrInfo::getExtendableOp(MCII, MCI);
197 MCOperand const &MO = MCI.getOperand(O);
198
199 assert((HexagonMCInstrInfo::isExtendable(MCII, MCI) ||
200 HexagonMCInstrInfo::isExtended(MCII, MCI)) &&
201 (MO.isImm() || MO.isExpr()));
202 return (MO);
203}
204
Colin LeMahieub6625652015-05-01 21:14:21 +0000205unsigned HexagonMCInstrInfo::getExtentAlignment(MCInstrInfo const &MCII,
206 MCInst const &MCI) {
207 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
208 return ((F >> HexagonII::ExtentAlignPos) & HexagonII::ExtentAlignMask);
209}
210
211unsigned HexagonMCInstrInfo::getExtentBits(MCInstrInfo const &MCII,
212 MCInst const &MCI) {
213 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
214 return ((F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask);
215}
216
Colin LeMahieuaf304e52015-02-19 19:00:00 +0000217// Return the max value that a constant extendable operand can have
218// without being extended.
219int HexagonMCInstrInfo::getMaxValue(MCInstrInfo const &MCII,
220 MCInst const &MCI) {
Colin LeMahieu745c4712015-02-19 19:49:27 +0000221 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
Colin LeMahieuaf304e52015-02-19 19:00:00 +0000222 unsigned isSigned =
223 (F >> HexagonII::ExtentSignedPos) & HexagonII::ExtentSignedMask;
224 unsigned bits = (F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask;
225
226 if (isSigned) // if value is signed
227 return ~(-1U << (bits - 1));
228 else
229 return ~(-1U << bits);
230}
Colin LeMahieu745c4712015-02-19 19:49:27 +0000231
232// Return the min value that a constant extendable operand can have
233// without being extended.
234int HexagonMCInstrInfo::getMinValue(MCInstrInfo const &MCII,
235 MCInst const &MCI) {
236 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
237 unsigned isSigned =
238 (F >> HexagonII::ExtentSignedPos) & HexagonII::ExtentSignedMask;
239 unsigned bits = (F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask;
240
241 if (isSigned) // if value is signed
242 return -1U << (bits - 1);
243 else
244 return 0;
245}
246
Colin LeMahieub6625652015-05-01 21:14:21 +0000247char const *HexagonMCInstrInfo::getName(MCInstrInfo const &MCII,
248 MCInst const &MCI) {
249 return MCII.getName(MCI.getOpcode());
250}
251
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000252unsigned short HexagonMCInstrInfo::getNewValueOp(MCInstrInfo const &MCII,
Colin LeMahieu745c4712015-02-19 19:49:27 +0000253 MCInst const &MCI) {
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000254 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
255 return ((F >> HexagonII::NewValueOpPos) & HexagonII::NewValueOpMask);
256}
257
258MCOperand const &HexagonMCInstrInfo::getNewValueOperand(MCInstrInfo const &MCII,
259 MCInst const &MCI) {
Colin LeMahieu745c4712015-02-19 19:49:27 +0000260 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
261 unsigned const O =
262 (F >> HexagonII::NewValueOpPos) & HexagonII::NewValueOpMask;
263 MCOperand const &MCO = MCI.getOperand(O);
264
265 assert((HexagonMCInstrInfo::isNewValue(MCII, MCI) ||
266 HexagonMCInstrInfo::hasNewValue(MCII, MCI)) &&
267 MCO.isReg());
268 return (MCO);
269}
270
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000271/// Return the new value or the newly produced value.
272unsigned short HexagonMCInstrInfo::getNewValueOp2(MCInstrInfo const &MCII,
273 MCInst const &MCI) {
274 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
275 return ((F >> HexagonII::NewValueOpPos2) & HexagonII::NewValueOpMask2);
276}
277
278MCOperand const &
279HexagonMCInstrInfo::getNewValueOperand2(MCInstrInfo const &MCII,
280 MCInst const &MCI) {
281 unsigned O = HexagonMCInstrInfo::getNewValueOp2(MCII, MCI);
282 MCOperand const &MCO = MCI.getOperand(O);
283
284 assert((HexagonMCInstrInfo::isNewValue(MCII, MCI) ||
285 HexagonMCInstrInfo::hasNewValue2(MCII, MCI)) &&
286 MCO.isReg());
287 return (MCO);
288}
289
Colin LeMahieub23c47b2015-05-31 21:57:09 +0000290int HexagonMCInstrInfo::getSubTarget(MCInstrInfo const &MCII,
291 MCInst const &MCI) {
292 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
293
294 HexagonII::SubTarget Target = static_cast<HexagonII::SubTarget>(
295 (F >> HexagonII::validSubTargetPos) & HexagonII::validSubTargetMask);
296
297 switch (Target) {
298 default:
299 return Hexagon::ArchV4;
300 case HexagonII::HasV5SubT:
301 return Hexagon::ArchV5;
302 }
303}
304
Colin LeMahieu745c4712015-02-19 19:49:27 +0000305// Return the Hexagon ISA class for the insn.
306unsigned HexagonMCInstrInfo::getType(MCInstrInfo const &MCII,
307 MCInst const &MCI) {
308 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
309
310 return ((F >> HexagonII::TypePos) & HexagonII::TypeMask);
311}
312
Colin LeMahieub23c47b2015-05-31 21:57:09 +0000313unsigned HexagonMCInstrInfo::getUnits(MCInstrInfo const &MCII,
314 MCSubtargetInfo const &STI,
315 MCInst const &MCI) {
316
317 const InstrItinerary *II = STI.getSchedModel().InstrItineraries;
318 int SchedClass = HexagonMCInstrInfo::getDesc(MCII, MCI).getSchedClass();
319 return ((II[SchedClass].FirstStage + HexagonStages)->getUnits());
320}
321
322bool HexagonMCInstrInfo::hasImmExt(MCInst const &MCI) {
323 if (!HexagonMCInstrInfo::isBundle(MCI))
324 return false;
325
326 for (const auto &I : HexagonMCInstrInfo::bundleInstructions(MCI)) {
327 auto MI = I.getInst();
328 if (isImmext(*MI))
329 return true;
330 }
331
332 return false;
333}
334
Colin LeMahieube8c4532015-06-05 16:00:11 +0000335bool HexagonMCInstrInfo::hasExtenderForIndex(MCInst const &MCB, size_t Index) {
336 return extenderForIndex(MCB, Index) != nullptr;
337}
338
Colin LeMahieu745c4712015-02-19 19:49:27 +0000339// Return whether the instruction is a legal new-value producer.
340bool HexagonMCInstrInfo::hasNewValue(MCInstrInfo const &MCII,
341 MCInst const &MCI) {
342 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
343 return ((F >> HexagonII::hasNewValuePos) & HexagonII::hasNewValueMask);
344}
345
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000346/// Return whether the insn produces a second value.
347bool HexagonMCInstrInfo::hasNewValue2(MCInstrInfo const &MCII,
348 MCInst const &MCI) {
349 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
350 return ((F >> HexagonII::hasNewValuePos2) & HexagonII::hasNewValueMask2);
351}
352
Colin LeMahieu86f218e2015-05-30 18:55:47 +0000353MCInst const &HexagonMCInstrInfo::instruction(MCInst const &MCB, size_t Index) {
354 assert(isBundle(MCB));
355 assert(Index < HEXAGON_PACKET_SIZE);
356 return *MCB.getOperand(bundleInstructionsOffset + Index).getInst();
357}
358
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000359bool HexagonMCInstrInfo::isBundle(MCInst const &MCI) {
360 auto Result = Hexagon::BUNDLE == MCI.getOpcode();
361 assert(!Result || (MCI.size() > 0 && MCI.getOperand(0).isImm()));
362 return Result;
363}
364
Colin LeMahieu745c4712015-02-19 19:49:27 +0000365// Return whether the insn is an actual insn.
366bool HexagonMCInstrInfo::isCanon(MCInstrInfo const &MCII, MCInst const &MCI) {
367 return (!HexagonMCInstrInfo::getDesc(MCII, MCI).isPseudo() &&
368 !HexagonMCInstrInfo::isPrefix(MCII, MCI) &&
369 HexagonMCInstrInfo::getType(MCII, MCI) != HexagonII::TypeENDLOOP);
370}
371
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000372bool HexagonMCInstrInfo::isCompound(MCInstrInfo const &MCII,
373 MCInst const &MCI) {
374 return (getType(MCII, MCI) == HexagonII::TypeCOMPOUND);
375}
376
Colin LeMahieube8c4532015-06-05 16:00:11 +0000377bool HexagonMCInstrInfo::isDblRegForSubInst(unsigned Reg) {
378 return ((Reg >= Hexagon::D0 && Reg <= Hexagon::D3) ||
379 (Reg >= Hexagon::D8 && Reg <= Hexagon::D11));
380}
381
382bool HexagonMCInstrInfo::isDuplex(MCInstrInfo const &MCII, MCInst const &MCI) {
383 return HexagonII::TypeDUPLEX == HexagonMCInstrInfo::getType(MCII, MCI);
384}
385
Colin LeMahieu745c4712015-02-19 19:49:27 +0000386// Return whether the instruction needs to be constant extended.
387// 1) Always return true if the instruction has 'isExtended' flag set.
388//
389// isExtendable:
390// 2) For immediate extended operands, return true only if the value is
391// out-of-range.
392// 3) For global address, always return true.
393
394bool HexagonMCInstrInfo::isConstExtended(MCInstrInfo const &MCII,
395 MCInst const &MCI) {
396 if (HexagonMCInstrInfo::isExtended(MCII, MCI))
397 return true;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000398 // Branch insns are handled as necessary by relaxation.
399 if ((HexagonMCInstrInfo::getType(MCII, MCI) == HexagonII::TypeJ) ||
400 (HexagonMCInstrInfo::getType(MCII, MCI) == HexagonII::TypeCOMPOUND &&
401 HexagonMCInstrInfo::getDesc(MCII, MCI).isBranch()) ||
402 (HexagonMCInstrInfo::getType(MCII, MCI) == HexagonII::TypeNV &&
403 HexagonMCInstrInfo::getDesc(MCII, MCI).isBranch()))
404 return false;
405 // Otherwise loop instructions and other CR insts are handled by relaxation
406 else if ((HexagonMCInstrInfo::getType(MCII, MCI) == HexagonII::TypeCR) &&
407 (MCI.getOpcode() != Hexagon::C4_addipc))
408 return false;
409 else if (!HexagonMCInstrInfo::isExtendable(MCII, MCI))
Colin LeMahieu745c4712015-02-19 19:49:27 +0000410 return false;
411
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000412 MCOperand const &MO = HexagonMCInstrInfo::getExtendableOperand(MCII, MCI);
Colin LeMahieu745c4712015-02-19 19:49:27 +0000413
414 // We could be using an instruction with an extendable immediate and shoehorn
415 // a global address into it. If it is a global address it will be constant
416 // extended. We do this for COMBINE.
417 // We currently only handle isGlobal() because it is the only kind of
418 // object we are going to end up with here for now.
419 // In the future we probably should add isSymbol(), etc.
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000420 assert(!MO.isImm());
421 int64_t Value;
422 if (!MO.getExpr()->evaluateAsAbsolute(Value))
Colin LeMahieu745c4712015-02-19 19:49:27 +0000423 return true;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000424 int MinValue = HexagonMCInstrInfo::getMinValue(MCII, MCI);
425 int MaxValue = HexagonMCInstrInfo::getMaxValue(MCII, MCI);
426 return (MinValue > Value || Value > MaxValue);
Colin LeMahieu745c4712015-02-19 19:49:27 +0000427}
428
Colin LeMahieu745c4712015-02-19 19:49:27 +0000429bool HexagonMCInstrInfo::isExtendable(MCInstrInfo const &MCII,
430 MCInst const &MCI) {
431 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
432 return (F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask;
433}
434
Colin LeMahieu745c4712015-02-19 19:49:27 +0000435bool HexagonMCInstrInfo::isExtended(MCInstrInfo const &MCII,
436 MCInst const &MCI) {
437 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
438 return (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
439}
440
Colin LeMahieub23c47b2015-05-31 21:57:09 +0000441bool HexagonMCInstrInfo::isFloat(MCInstrInfo const &MCII, MCInst const &MCI) {
442 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
443 return ((F >> HexagonII::FPPos) & HexagonII::FPMask);
444}
445
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000446bool HexagonMCInstrInfo::isImmext(MCInst const &MCI) {
447 auto Op = MCI.getOpcode();
448 return (Op == Hexagon::A4_ext_b || Op == Hexagon::A4_ext_c ||
449 Op == Hexagon::A4_ext_g || Op == Hexagon::A4_ext);
450}
451
452bool HexagonMCInstrInfo::isInnerLoop(MCInst const &MCI) {
453 assert(isBundle(MCI));
454 int64_t Flags = MCI.getOperand(0).getImm();
455 return (Flags & innerLoopMask) != 0;
456}
457
Colin LeMahieube8c4532015-06-05 16:00:11 +0000458bool HexagonMCInstrInfo::isIntReg(unsigned Reg) {
459 return (Reg >= Hexagon::R0 && Reg <= Hexagon::R31);
460}
461
462bool HexagonMCInstrInfo::isIntRegForSubInst(unsigned Reg) {
463 return ((Reg >= Hexagon::R0 && Reg <= Hexagon::R7) ||
464 (Reg >= Hexagon::R16 && Reg <= Hexagon::R23));
465}
466
Colin LeMahieu745c4712015-02-19 19:49:27 +0000467// Return whether the insn is a new-value consumer.
468bool HexagonMCInstrInfo::isNewValue(MCInstrInfo const &MCII,
469 MCInst const &MCI) {
470 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
471 return ((F >> HexagonII::NewValuePos) & HexagonII::NewValueMask);
472}
473
474// Return whether the operand can be constant extended.
475bool HexagonMCInstrInfo::isOperandExtended(MCInstrInfo const &MCII,
476 MCInst const &MCI,
477 unsigned short OperandNum) {
478 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
479 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask) ==
480 OperandNum;
481}
482
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000483bool HexagonMCInstrInfo::isOuterLoop(MCInst const &MCI) {
484 assert(isBundle(MCI));
485 int64_t Flags = MCI.getOperand(0).getImm();
486 return (Flags & outerLoopMask) != 0;
Colin LeMahieu1174fea2015-02-19 21:10:50 +0000487}
488
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000489bool HexagonMCInstrInfo::isPredicated(MCInstrInfo const &MCII,
490 MCInst const &MCI) {
491 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
492 return ((F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
493}
494
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000495bool HexagonMCInstrInfo::isPredicateLate(MCInstrInfo const &MCII,
496 MCInst const &MCI) {
497 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
498 return (F >> HexagonII::PredicateLatePos & HexagonII::PredicateLateMask);
499}
500
501/// Return whether the insn is newly predicated.
502bool HexagonMCInstrInfo::isPredicatedNew(MCInstrInfo const &MCII,
503 MCInst const &MCI) {
504 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
505 return ((F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask);
506}
507
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000508bool HexagonMCInstrInfo::isPredicatedTrue(MCInstrInfo const &MCII,
509 MCInst const &MCI) {
510 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
511 return (
512 !((F >> HexagonII::PredicatedFalsePos) & HexagonII::PredicatedFalseMask));
Colin LeMahieu1174fea2015-02-19 21:10:50 +0000513}
514
Colin LeMahieube8c4532015-06-05 16:00:11 +0000515bool HexagonMCInstrInfo::isPredReg(unsigned Reg) {
516 return (Reg >= Hexagon::P0 && Reg <= Hexagon::P3_0);
517}
518
Colin LeMahieu745c4712015-02-19 19:49:27 +0000519bool HexagonMCInstrInfo::isPrefix(MCInstrInfo const &MCII, MCInst const &MCI) {
520 return (HexagonMCInstrInfo::getType(MCII, MCI) == HexagonII::TypePREFIX);
521}
522
Colin LeMahieu745c4712015-02-19 19:49:27 +0000523bool HexagonMCInstrInfo::isSolo(MCInstrInfo const &MCII, MCInst const &MCI) {
524 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
525 return ((F >> HexagonII::SoloPos) & HexagonII::SoloMask);
526}
Colin LeMahieu1174fea2015-02-19 21:10:50 +0000527
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000528bool HexagonMCInstrInfo::isMemReorderDisabled(MCInst const &MCI) {
529 assert(isBundle(MCI));
530 auto Flags = MCI.getOperand(0).getImm();
531 return (Flags & memReorderDisabledMask) != 0;
532}
533
534bool HexagonMCInstrInfo::isMemStoreReorderEnabled(MCInst const &MCI) {
535 assert(isBundle(MCI));
536 auto Flags = MCI.getOperand(0).getImm();
537 return (Flags & memStoreReorderEnabledMask) != 0;
538}
539
Colin LeMahieub23c47b2015-05-31 21:57:09 +0000540bool HexagonMCInstrInfo::isSoloAX(MCInstrInfo const &MCII, MCInst const &MCI) {
541 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
542 return ((F >> HexagonII::SoloAXPos) & HexagonII::SoloAXMask);
543}
544
545bool HexagonMCInstrInfo::isSoloAin1(MCInstrInfo const &MCII,
546 MCInst const &MCI) {
547 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
548 return ((F >> HexagonII::SoloAin1Pos) & HexagonII::SoloAin1Mask);
549}
550
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000551bool HexagonMCInstrInfo::isVector(MCInstrInfo const &MCII, MCInst const &MCI) {
552 if ((getType(MCII, MCI) <= HexagonII::TypeCVI_LAST) &&
553 (getType(MCII, MCI) >= HexagonII::TypeCVI_FIRST))
554 return true;
555 return false;
556}
557
558int64_t HexagonMCInstrInfo::minConstant(MCInst const &MCI, size_t Index) {
559 auto Sentinal = static_cast<int64_t>(std::numeric_limits<uint32_t>::max())
560 << 8;
561 if (MCI.size() <= Index)
562 return Sentinal;
563 MCOperand const &MCO = MCI.getOperand(Index);
564 if (!MCO.isExpr())
565 return Sentinal;
566 int64_t Value;
567 if (!MCO.getExpr()->evaluateAsAbsolute(Value))
568 return Sentinal;
569 return Value;
570}
571
Colin LeMahieub3c97272015-11-13 07:58:06 +0000572void HexagonMCInstrInfo::padEndloop(MCContext &Context, MCInst &MCB) {
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000573 MCInst Nop;
574 Nop.setOpcode(Hexagon::A2_nop);
575 assert(isBundle(MCB));
576 while ((HexagonMCInstrInfo::isInnerLoop(MCB) &&
577 (HexagonMCInstrInfo::bundleSize(MCB) < HEXAGON_PACKET_INNER_SIZE)) ||
578 ((HexagonMCInstrInfo::isOuterLoop(MCB) &&
579 (HexagonMCInstrInfo::bundleSize(MCB) < HEXAGON_PACKET_OUTER_SIZE))))
Colin LeMahieub3c97272015-11-13 07:58:06 +0000580 MCB.addOperand(MCOperand::createInst(new (Context) MCInst(Nop)));
Colin LeMahieu1174fea2015-02-19 21:10:50 +0000581}
582
Colin LeMahieub23c47b2015-05-31 21:57:09 +0000583bool HexagonMCInstrInfo::prefersSlot3(MCInstrInfo const &MCII,
584 MCInst const &MCI) {
585 if (HexagonMCInstrInfo::getType(MCII, MCI) == HexagonII::TypeCR)
586 return false;
587
588 unsigned SchedClass = HexagonMCInstrInfo::getDesc(MCII, MCI).getSchedClass();
589 switch (SchedClass) {
590 case Hexagon::Sched::ALU32_3op_tc_2_SLOT0123:
591 case Hexagon::Sched::ALU64_tc_2_SLOT23:
592 case Hexagon::Sched::ALU64_tc_3x_SLOT23:
593 case Hexagon::Sched::M_tc_2_SLOT23:
594 case Hexagon::Sched::M_tc_3x_SLOT23:
595 case Hexagon::Sched::S_2op_tc_2_SLOT23:
596 case Hexagon::Sched::S_3op_tc_2_SLOT23:
597 case Hexagon::Sched::S_3op_tc_3x_SLOT23:
598 return true;
599 }
600 return false;
601}
602
Colin LeMahieube8c4532015-06-05 16:00:11 +0000603void HexagonMCInstrInfo::replaceDuplex(MCContext &Context, MCInst &MCB,
604 DuplexCandidate Candidate) {
605 assert(Candidate.packetIndexI < MCB.size());
606 assert(Candidate.packetIndexJ < MCB.size());
607 assert(isBundle(MCB));
608 MCInst *Duplex =
609 deriveDuplex(Context, Candidate.iClass,
610 *MCB.getOperand(Candidate.packetIndexJ).getInst(),
611 *MCB.getOperand(Candidate.packetIndexI).getInst());
612 assert(Duplex != nullptr);
613 MCB.getOperand(Candidate.packetIndexI).setInst(Duplex);
614 MCB.erase(MCB.begin() + Candidate.packetIndexJ);
615}
616
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000617void HexagonMCInstrInfo::setInnerLoop(MCInst &MCI) {
618 assert(isBundle(MCI));
619 MCOperand &Operand = MCI.getOperand(0);
620 Operand.setImm(Operand.getImm() | innerLoopMask);
Colin LeMahieu1174fea2015-02-19 21:10:50 +0000621}
622
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000623void HexagonMCInstrInfo::setMemReorderDisabled(MCInst &MCI) {
624 assert(isBundle(MCI));
625 MCOperand &Operand = MCI.getOperand(0);
626 Operand.setImm(Operand.getImm() | memReorderDisabledMask);
627 assert(isMemReorderDisabled(MCI));
628}
629
630void HexagonMCInstrInfo::setMemStoreReorderEnabled(MCInst &MCI) {
631 assert(isBundle(MCI));
632 MCOperand &Operand = MCI.getOperand(0);
633 Operand.setImm(Operand.getImm() | memStoreReorderEnabledMask);
634 assert(isMemStoreReorderEnabled(MCI));
635}
636
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000637void HexagonMCInstrInfo::setOuterLoop(MCInst &MCI) {
638 assert(isBundle(MCI));
639 MCOperand &Operand = MCI.getOperand(0);
640 Operand.setImm(Operand.getImm() | outerLoopMask);
Colin LeMahieu1174fea2015-02-19 21:10:50 +0000641}
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000642}