blob: eed635faeacf4161beacb0501dff86c3fe5fc4ca [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));
Craig Topper15576e12015-12-06 05:08:07 +000048 return make_range(MCI.begin() + bundleInstructionsOffset, MCI.end());
Colin LeMahieu68d967d2015-05-29 14:44:13 +000049}
50
51size_t HexagonMCInstrInfo::bundleSize(MCInst const &MCI) {
52 if (HexagonMCInstrInfo::isBundle(MCI))
53 return (MCI.size() - bundleInstructionsOffset);
54 else
55 return (1);
Colin LeMahieu1174fea2015-02-19 21:10:50 +000056}
57
Colin LeMahieu7cd08922015-11-09 04:07:48 +000058bool HexagonMCInstrInfo::canonicalizePacket(MCInstrInfo const &MCII,
59 MCSubtargetInfo const &STI,
60 MCContext &Context, MCInst &MCB,
61 HexagonMCChecker *Check) {
62 // Examine the packet and convert pairs of instructions to compound
63 // instructions when possible.
64 if (!HexagonDisableCompound)
65 HexagonMCInstrInfo::tryCompound(MCII, Context, MCB);
66 // Check the bundle for errors.
67 bool CheckOk = Check ? Check->check() : true;
68 if (!CheckOk)
69 return false;
70 HexagonMCShuffle(MCII, STI, MCB);
71 // Examine the packet and convert pairs of instructions to duplex
72 // instructions when possible.
73 MCInst InstBundlePreDuplex = MCInst(MCB);
74 if (!HexagonDisableDuplex) {
75 SmallVector<DuplexCandidate, 8> possibleDuplexes;
76 possibleDuplexes = HexagonMCInstrInfo::getDuplexPossibilties(MCII, MCB);
77 HexagonMCShuffle(MCII, STI, Context, MCB, possibleDuplexes);
78 }
79 // Examines packet and pad the packet, if needed, when an
80 // end-loop is in the bundle.
Colin LeMahieub3c97272015-11-13 07:58:06 +000081 HexagonMCInstrInfo::padEndloop(Context, MCB);
Colin LeMahieu7cd08922015-11-09 04:07:48 +000082 // If compounding and duplexing didn't reduce the size below
83 // 4 or less we have a packet that is too big.
84 if (HexagonMCInstrInfo::bundleSize(MCB) > HEXAGON_PACKET_SIZE)
85 return false;
86 HexagonMCShuffle(MCII, STI, MCB);
87 return true;
88}
89
Colin LeMahieu8ab7e8e2015-11-10 00:02:27 +000090void HexagonMCInstrInfo::clampExtended(MCInstrInfo const &MCII,
91 MCContext &Context, MCInst &MCI) {
Colin LeMahieube99a022015-06-17 03:06:16 +000092 assert(HexagonMCInstrInfo::isExtendable(MCII, MCI) ||
93 HexagonMCInstrInfo::isExtended(MCII, MCI));
94 MCOperand &exOp =
95 MCI.getOperand(HexagonMCInstrInfo::getExtendableOp(MCII, MCI));
96 // If the extended value is a constant, then use it for the extended and
97 // for the extender instructions, masking off the lower 6 bits and
98 // including the assumed bits.
Colin LeMahieu8ab7e8e2015-11-10 00:02:27 +000099 int64_t Value;
100 if (exOp.getExpr()->evaluateAsAbsolute(Value)) {
Colin LeMahieube99a022015-06-17 03:06:16 +0000101 unsigned Shift = HexagonMCInstrInfo::getExtentAlignment(MCII, MCI);
Colin LeMahieuc7b21242016-02-15 18:47:55 +0000102 exOp.setExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +0000103 MCConstantExpr::create((Value & 0x3f) << Shift, Context), Context));
Colin LeMahieube99a022015-06-17 03:06:16 +0000104 }
105}
106
Colin LeMahieuf0af6e52015-11-13 17:42:46 +0000107MCInst HexagonMCInstrInfo::createBundle() {
108 MCInst Result;
109 Result.setOpcode(Hexagon::BUNDLE);
110 Result.addOperand(MCOperand::createImm(0));
111 return Result;
112}
113
Colin LeMahieube8c4532015-06-05 16:00:11 +0000114MCInst *HexagonMCInstrInfo::deriveDuplex(MCContext &Context, unsigned iClass,
115 MCInst const &inst0,
116 MCInst const &inst1) {
117 assert((iClass <= 0xf) && "iClass must have range of 0 to 0xf");
118 MCInst *duplexInst = new (Context) MCInst;
119 duplexInst->setOpcode(Hexagon::DuplexIClass0 + iClass);
120
121 MCInst *SubInst0 = new (Context) MCInst(deriveSubInst(inst0));
122 MCInst *SubInst1 = new (Context) MCInst(deriveSubInst(inst1));
123 duplexInst->addOperand(MCOperand::createInst(SubInst0));
124 duplexInst->addOperand(MCOperand::createInst(SubInst1));
125 return duplexInst;
126}
127
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000128MCInst HexagonMCInstrInfo::deriveExtender(MCInstrInfo const &MCII,
129 MCInst const &Inst,
130 MCOperand const &MO) {
131 assert(HexagonMCInstrInfo::isExtendable(MCII, Inst) ||
132 HexagonMCInstrInfo::isExtended(MCII, Inst));
133
134 MCInstrDesc const &Desc = HexagonMCInstrInfo::getDesc(MCII, Inst);
135 MCInst XMI;
136 XMI.setOpcode((Desc.isBranch() || Desc.isCall() ||
137 HexagonMCInstrInfo::getType(MCII, Inst) == HexagonII::TypeCR)
138 ? Hexagon::A4_ext_b
139 : Hexagon::A4_ext);
140 if (MO.isImm())
141 XMI.addOperand(MCOperand::createImm(MO.getImm() & (~0x3f)));
142 else if (MO.isExpr())
143 XMI.addOperand(MCOperand::createExpr(MO.getExpr()));
144 else
145 llvm_unreachable("invalid extendable operand");
146 return XMI;
147}
148
Colin LeMahieube8c4532015-06-05 16:00:11 +0000149MCInst const *HexagonMCInstrInfo::extenderForIndex(MCInst const &MCB,
150 size_t Index) {
151 assert(Index <= bundleSize(MCB));
152 if (Index == 0)
153 return nullptr;
154 MCInst const *Inst =
155 MCB.getOperand(Index + bundleInstructionsOffset - 1).getInst();
156 if (isImmext(*Inst))
157 return Inst;
158 return nullptr;
159}
160
Benjamin Kramer7c576d82015-11-12 19:30:40 +0000161void HexagonMCInstrInfo::extendIfNeeded(MCContext &Context,
162 MCInstrInfo const &MCII, MCInst &MCB,
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000163 MCInst const &MCI, bool MustExtend) {
164 if (isConstExtended(MCII, MCI) || MustExtend)
Benjamin Kramer7c576d82015-11-12 19:30:40 +0000165 addConstExtender(Context, MCII, MCB, MCI);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000166}
167
Colin LeMahieub6625652015-05-01 21:14:21 +0000168HexagonII::MemAccessSize
169HexagonMCInstrInfo::getAccessSize(MCInstrInfo const &MCII, MCInst const &MCI) {
170 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
171
172 return (HexagonII::MemAccessSize((F >> HexagonII::MemAccessSizePos) &
173 HexagonII::MemAccesSizeMask));
174}
175
Colin LeMahieu745c4712015-02-19 19:49:27 +0000176unsigned HexagonMCInstrInfo::getBitCount(MCInstrInfo const &MCII,
177 MCInst const &MCI) {
178 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
179 return ((F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask);
180}
181
Colin LeMahieu745c4712015-02-19 19:49:27 +0000182// Return constant extended operand number.
183unsigned short HexagonMCInstrInfo::getCExtOpNum(MCInstrInfo const &MCII,
184 MCInst const &MCI) {
185 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
186 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask);
187}
188
Colin LeMahieu1174fea2015-02-19 21:10:50 +0000189MCInstrDesc const &HexagonMCInstrInfo::getDesc(MCInstrInfo const &MCII,
190 MCInst const &MCI) {
191 return (MCII.get(MCI.getOpcode()));
192}
193
Colin LeMahieu98c8e072016-02-15 18:42:07 +0000194MCExpr const &HexagonMCInstrInfo::getExpr(MCExpr const &Expr) {
195 HexagonMCExpr const &HExpr = *llvm::cast<HexagonMCExpr>(&Expr);
196 return *HExpr.getExpr();
197}
198
Colin LeMahieu86f218e2015-05-30 18:55:47 +0000199unsigned short HexagonMCInstrInfo::getExtendableOp(MCInstrInfo const &MCII,
200 MCInst const &MCI) {
201 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
202 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask);
203}
204
205MCOperand const &
206HexagonMCInstrInfo::getExtendableOperand(MCInstrInfo const &MCII,
207 MCInst const &MCI) {
208 unsigned O = HexagonMCInstrInfo::getExtendableOp(MCII, MCI);
209 MCOperand const &MO = MCI.getOperand(O);
210
211 assert((HexagonMCInstrInfo::isExtendable(MCII, MCI) ||
212 HexagonMCInstrInfo::isExtended(MCII, MCI)) &&
213 (MO.isImm() || MO.isExpr()));
214 return (MO);
215}
216
Colin LeMahieub6625652015-05-01 21:14:21 +0000217unsigned HexagonMCInstrInfo::getExtentAlignment(MCInstrInfo const &MCII,
218 MCInst const &MCI) {
219 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
220 return ((F >> HexagonII::ExtentAlignPos) & HexagonII::ExtentAlignMask);
221}
222
223unsigned HexagonMCInstrInfo::getExtentBits(MCInstrInfo const &MCII,
224 MCInst const &MCI) {
225 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
226 return ((F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask);
227}
228
Colin LeMahieuaf304e52015-02-19 19:00:00 +0000229// Return the max value that a constant extendable operand can have
230// without being extended.
231int HexagonMCInstrInfo::getMaxValue(MCInstrInfo const &MCII,
232 MCInst const &MCI) {
Colin LeMahieu745c4712015-02-19 19:49:27 +0000233 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
Colin LeMahieuaf304e52015-02-19 19:00:00 +0000234 unsigned isSigned =
235 (F >> HexagonII::ExtentSignedPos) & HexagonII::ExtentSignedMask;
236 unsigned bits = (F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask;
237
238 if (isSigned) // if value is signed
239 return ~(-1U << (bits - 1));
240 else
241 return ~(-1U << bits);
242}
Colin LeMahieu745c4712015-02-19 19:49:27 +0000243
244// Return the min value that a constant extendable operand can have
245// without being extended.
246int HexagonMCInstrInfo::getMinValue(MCInstrInfo const &MCII,
247 MCInst const &MCI) {
248 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
249 unsigned isSigned =
250 (F >> HexagonII::ExtentSignedPos) & HexagonII::ExtentSignedMask;
251 unsigned bits = (F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask;
252
253 if (isSigned) // if value is signed
254 return -1U << (bits - 1);
255 else
256 return 0;
257}
258
Colin LeMahieub6625652015-05-01 21:14:21 +0000259char const *HexagonMCInstrInfo::getName(MCInstrInfo const &MCII,
260 MCInst const &MCI) {
261 return MCII.getName(MCI.getOpcode());
262}
263
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000264unsigned short HexagonMCInstrInfo::getNewValueOp(MCInstrInfo const &MCII,
Colin LeMahieu745c4712015-02-19 19:49:27 +0000265 MCInst const &MCI) {
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000266 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
267 return ((F >> HexagonII::NewValueOpPos) & HexagonII::NewValueOpMask);
268}
269
270MCOperand const &HexagonMCInstrInfo::getNewValueOperand(MCInstrInfo const &MCII,
271 MCInst const &MCI) {
Colin LeMahieu745c4712015-02-19 19:49:27 +0000272 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
273 unsigned const O =
274 (F >> HexagonII::NewValueOpPos) & HexagonII::NewValueOpMask;
275 MCOperand const &MCO = MCI.getOperand(O);
276
277 assert((HexagonMCInstrInfo::isNewValue(MCII, MCI) ||
278 HexagonMCInstrInfo::hasNewValue(MCII, MCI)) &&
279 MCO.isReg());
280 return (MCO);
281}
282
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000283/// Return the new value or the newly produced value.
284unsigned short HexagonMCInstrInfo::getNewValueOp2(MCInstrInfo const &MCII,
285 MCInst const &MCI) {
286 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
287 return ((F >> HexagonII::NewValueOpPos2) & HexagonII::NewValueOpMask2);
288}
289
290MCOperand const &
291HexagonMCInstrInfo::getNewValueOperand2(MCInstrInfo const &MCII,
292 MCInst const &MCI) {
293 unsigned O = HexagonMCInstrInfo::getNewValueOp2(MCII, MCI);
294 MCOperand const &MCO = MCI.getOperand(O);
295
296 assert((HexagonMCInstrInfo::isNewValue(MCII, MCI) ||
297 HexagonMCInstrInfo::hasNewValue2(MCII, MCI)) &&
298 MCO.isReg());
299 return (MCO);
300}
301
Colin LeMahieub23c47b2015-05-31 21:57:09 +0000302int HexagonMCInstrInfo::getSubTarget(MCInstrInfo const &MCII,
303 MCInst const &MCI) {
304 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
305
306 HexagonII::SubTarget Target = static_cast<HexagonII::SubTarget>(
307 (F >> HexagonII::validSubTargetPos) & HexagonII::validSubTargetMask);
308
309 switch (Target) {
310 default:
311 return Hexagon::ArchV4;
312 case HexagonII::HasV5SubT:
313 return Hexagon::ArchV5;
314 }
315}
316
Colin LeMahieu745c4712015-02-19 19:49:27 +0000317// Return the Hexagon ISA class for the insn.
318unsigned HexagonMCInstrInfo::getType(MCInstrInfo const &MCII,
319 MCInst const &MCI) {
320 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
321
322 return ((F >> HexagonII::TypePos) & HexagonII::TypeMask);
323}
324
Colin LeMahieub23c47b2015-05-31 21:57:09 +0000325unsigned HexagonMCInstrInfo::getUnits(MCInstrInfo const &MCII,
326 MCSubtargetInfo const &STI,
327 MCInst const &MCI) {
328
329 const InstrItinerary *II = STI.getSchedModel().InstrItineraries;
330 int SchedClass = HexagonMCInstrInfo::getDesc(MCII, MCI).getSchedClass();
331 return ((II[SchedClass].FirstStage + HexagonStages)->getUnits());
332}
333
334bool HexagonMCInstrInfo::hasImmExt(MCInst const &MCI) {
335 if (!HexagonMCInstrInfo::isBundle(MCI))
336 return false;
337
338 for (const auto &I : HexagonMCInstrInfo::bundleInstructions(MCI)) {
339 auto MI = I.getInst();
340 if (isImmext(*MI))
341 return true;
342 }
343
344 return false;
345}
346
Colin LeMahieube8c4532015-06-05 16:00:11 +0000347bool HexagonMCInstrInfo::hasExtenderForIndex(MCInst const &MCB, size_t Index) {
348 return extenderForIndex(MCB, Index) != nullptr;
349}
350
Colin LeMahieu745c4712015-02-19 19:49:27 +0000351// Return whether the instruction is a legal new-value producer.
352bool HexagonMCInstrInfo::hasNewValue(MCInstrInfo const &MCII,
353 MCInst const &MCI) {
354 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
355 return ((F >> HexagonII::hasNewValuePos) & HexagonII::hasNewValueMask);
356}
357
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000358/// Return whether the insn produces a second value.
359bool HexagonMCInstrInfo::hasNewValue2(MCInstrInfo const &MCII,
360 MCInst const &MCI) {
361 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
362 return ((F >> HexagonII::hasNewValuePos2) & HexagonII::hasNewValueMask2);
363}
364
Colin LeMahieu86f218e2015-05-30 18:55:47 +0000365MCInst const &HexagonMCInstrInfo::instruction(MCInst const &MCB, size_t Index) {
366 assert(isBundle(MCB));
367 assert(Index < HEXAGON_PACKET_SIZE);
368 return *MCB.getOperand(bundleInstructionsOffset + Index).getInst();
369}
370
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000371bool HexagonMCInstrInfo::isBundle(MCInst const &MCI) {
372 auto Result = Hexagon::BUNDLE == MCI.getOpcode();
373 assert(!Result || (MCI.size() > 0 && MCI.getOperand(0).isImm()));
374 return Result;
375}
376
Colin LeMahieu745c4712015-02-19 19:49:27 +0000377// Return whether the insn is an actual insn.
378bool HexagonMCInstrInfo::isCanon(MCInstrInfo const &MCII, MCInst const &MCI) {
379 return (!HexagonMCInstrInfo::getDesc(MCII, MCI).isPseudo() &&
380 !HexagonMCInstrInfo::isPrefix(MCII, MCI) &&
381 HexagonMCInstrInfo::getType(MCII, MCI) != HexagonII::TypeENDLOOP);
382}
383
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000384bool HexagonMCInstrInfo::isCompound(MCInstrInfo const &MCII,
385 MCInst const &MCI) {
386 return (getType(MCII, MCI) == HexagonII::TypeCOMPOUND);
387}
388
Colin LeMahieube8c4532015-06-05 16:00:11 +0000389bool HexagonMCInstrInfo::isDblRegForSubInst(unsigned Reg) {
390 return ((Reg >= Hexagon::D0 && Reg <= Hexagon::D3) ||
391 (Reg >= Hexagon::D8 && Reg <= Hexagon::D11));
392}
393
394bool HexagonMCInstrInfo::isDuplex(MCInstrInfo const &MCII, MCInst const &MCI) {
395 return HexagonII::TypeDUPLEX == HexagonMCInstrInfo::getType(MCII, MCI);
396}
397
Colin LeMahieu745c4712015-02-19 19:49:27 +0000398// Return whether the instruction needs to be constant extended.
399// 1) Always return true if the instruction has 'isExtended' flag set.
400//
401// isExtendable:
402// 2) For immediate extended operands, return true only if the value is
403// out-of-range.
404// 3) For global address, always return true.
405
406bool HexagonMCInstrInfo::isConstExtended(MCInstrInfo const &MCII,
407 MCInst const &MCI) {
408 if (HexagonMCInstrInfo::isExtended(MCII, MCI))
409 return true;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000410 // Branch insns are handled as necessary by relaxation.
411 if ((HexagonMCInstrInfo::getType(MCII, MCI) == HexagonII::TypeJ) ||
412 (HexagonMCInstrInfo::getType(MCII, MCI) == HexagonII::TypeCOMPOUND &&
413 HexagonMCInstrInfo::getDesc(MCII, MCI).isBranch()) ||
414 (HexagonMCInstrInfo::getType(MCII, MCI) == HexagonII::TypeNV &&
415 HexagonMCInstrInfo::getDesc(MCII, MCI).isBranch()))
416 return false;
417 // Otherwise loop instructions and other CR insts are handled by relaxation
418 else if ((HexagonMCInstrInfo::getType(MCII, MCI) == HexagonII::TypeCR) &&
419 (MCI.getOpcode() != Hexagon::C4_addipc))
420 return false;
421 else if (!HexagonMCInstrInfo::isExtendable(MCII, MCI))
Colin LeMahieu745c4712015-02-19 19:49:27 +0000422 return false;
423
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000424 MCOperand const &MO = HexagonMCInstrInfo::getExtendableOperand(MCII, MCI);
Colin LeMahieu745c4712015-02-19 19:49:27 +0000425
426 // We could be using an instruction with an extendable immediate and shoehorn
427 // a global address into it. If it is a global address it will be constant
428 // extended. We do this for COMBINE.
429 // We currently only handle isGlobal() because it is the only kind of
430 // object we are going to end up with here for now.
431 // In the future we probably should add isSymbol(), etc.
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000432 assert(!MO.isImm());
433 int64_t Value;
434 if (!MO.getExpr()->evaluateAsAbsolute(Value))
Colin LeMahieu745c4712015-02-19 19:49:27 +0000435 return true;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000436 int MinValue = HexagonMCInstrInfo::getMinValue(MCII, MCI);
437 int MaxValue = HexagonMCInstrInfo::getMaxValue(MCII, MCI);
438 return (MinValue > Value || Value > MaxValue);
Colin LeMahieu745c4712015-02-19 19:49:27 +0000439}
440
Colin LeMahieu745c4712015-02-19 19:49:27 +0000441bool HexagonMCInstrInfo::isExtendable(MCInstrInfo const &MCII,
442 MCInst const &MCI) {
443 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
444 return (F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask;
445}
446
Colin LeMahieu745c4712015-02-19 19:49:27 +0000447bool HexagonMCInstrInfo::isExtended(MCInstrInfo const &MCII,
448 MCInst const &MCI) {
449 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
450 return (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
451}
452
Colin LeMahieub23c47b2015-05-31 21:57:09 +0000453bool HexagonMCInstrInfo::isFloat(MCInstrInfo const &MCII, MCInst const &MCI) {
454 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
455 return ((F >> HexagonII::FPPos) & HexagonII::FPMask);
456}
457
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000458bool HexagonMCInstrInfo::isImmext(MCInst const &MCI) {
459 auto Op = MCI.getOpcode();
460 return (Op == Hexagon::A4_ext_b || Op == Hexagon::A4_ext_c ||
461 Op == Hexagon::A4_ext_g || Op == Hexagon::A4_ext);
462}
463
464bool HexagonMCInstrInfo::isInnerLoop(MCInst const &MCI) {
465 assert(isBundle(MCI));
466 int64_t Flags = MCI.getOperand(0).getImm();
467 return (Flags & innerLoopMask) != 0;
468}
469
Colin LeMahieube8c4532015-06-05 16:00:11 +0000470bool HexagonMCInstrInfo::isIntReg(unsigned Reg) {
471 return (Reg >= Hexagon::R0 && Reg <= Hexagon::R31);
472}
473
474bool HexagonMCInstrInfo::isIntRegForSubInst(unsigned Reg) {
475 return ((Reg >= Hexagon::R0 && Reg <= Hexagon::R7) ||
476 (Reg >= Hexagon::R16 && Reg <= Hexagon::R23));
477}
478
Colin LeMahieu745c4712015-02-19 19:49:27 +0000479// Return whether the insn is a new-value consumer.
480bool HexagonMCInstrInfo::isNewValue(MCInstrInfo const &MCII,
481 MCInst const &MCI) {
482 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
483 return ((F >> HexagonII::NewValuePos) & HexagonII::NewValueMask);
484}
485
486// Return whether the operand can be constant extended.
487bool HexagonMCInstrInfo::isOperandExtended(MCInstrInfo const &MCII,
488 MCInst const &MCI,
489 unsigned short OperandNum) {
490 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
491 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask) ==
492 OperandNum;
493}
494
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000495bool HexagonMCInstrInfo::isOuterLoop(MCInst const &MCI) {
496 assert(isBundle(MCI));
497 int64_t Flags = MCI.getOperand(0).getImm();
498 return (Flags & outerLoopMask) != 0;
Colin LeMahieu1174fea2015-02-19 21:10:50 +0000499}
500
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000501bool HexagonMCInstrInfo::isPredicated(MCInstrInfo const &MCII,
502 MCInst const &MCI) {
503 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
504 return ((F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
505}
506
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000507bool HexagonMCInstrInfo::isPredicateLate(MCInstrInfo const &MCII,
508 MCInst const &MCI) {
509 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
510 return (F >> HexagonII::PredicateLatePos & HexagonII::PredicateLateMask);
511}
512
513/// Return whether the insn is newly predicated.
514bool HexagonMCInstrInfo::isPredicatedNew(MCInstrInfo const &MCII,
515 MCInst const &MCI) {
516 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
517 return ((F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask);
518}
519
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000520bool HexagonMCInstrInfo::isPredicatedTrue(MCInstrInfo const &MCII,
521 MCInst const &MCI) {
522 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
523 return (
524 !((F >> HexagonII::PredicatedFalsePos) & HexagonII::PredicatedFalseMask));
Colin LeMahieu1174fea2015-02-19 21:10:50 +0000525}
526
Colin LeMahieube8c4532015-06-05 16:00:11 +0000527bool HexagonMCInstrInfo::isPredReg(unsigned Reg) {
528 return (Reg >= Hexagon::P0 && Reg <= Hexagon::P3_0);
529}
530
Colin LeMahieu745c4712015-02-19 19:49:27 +0000531bool HexagonMCInstrInfo::isPrefix(MCInstrInfo const &MCII, MCInst const &MCI) {
532 return (HexagonMCInstrInfo::getType(MCII, MCI) == HexagonII::TypePREFIX);
533}
534
Colin LeMahieu745c4712015-02-19 19:49:27 +0000535bool HexagonMCInstrInfo::isSolo(MCInstrInfo const &MCII, MCInst const &MCI) {
536 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
537 return ((F >> HexagonII::SoloPos) & HexagonII::SoloMask);
538}
Colin LeMahieu1174fea2015-02-19 21:10:50 +0000539
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000540bool HexagonMCInstrInfo::isMemReorderDisabled(MCInst const &MCI) {
541 assert(isBundle(MCI));
542 auto Flags = MCI.getOperand(0).getImm();
543 return (Flags & memReorderDisabledMask) != 0;
544}
545
546bool HexagonMCInstrInfo::isMemStoreReorderEnabled(MCInst const &MCI) {
547 assert(isBundle(MCI));
548 auto Flags = MCI.getOperand(0).getImm();
549 return (Flags & memStoreReorderEnabledMask) != 0;
550}
551
Colin LeMahieub23c47b2015-05-31 21:57:09 +0000552bool HexagonMCInstrInfo::isSoloAX(MCInstrInfo const &MCII, MCInst const &MCI) {
553 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
554 return ((F >> HexagonII::SoloAXPos) & HexagonII::SoloAXMask);
555}
556
557bool HexagonMCInstrInfo::isSoloAin1(MCInstrInfo const &MCII,
558 MCInst const &MCI) {
559 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
560 return ((F >> HexagonII::SoloAin1Pos) & HexagonII::SoloAin1Mask);
561}
562
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000563bool HexagonMCInstrInfo::isVector(MCInstrInfo const &MCII, MCInst const &MCI) {
564 if ((getType(MCII, MCI) <= HexagonII::TypeCVI_LAST) &&
565 (getType(MCII, MCI) >= HexagonII::TypeCVI_FIRST))
566 return true;
567 return false;
568}
569
570int64_t HexagonMCInstrInfo::minConstant(MCInst const &MCI, size_t Index) {
571 auto Sentinal = static_cast<int64_t>(std::numeric_limits<uint32_t>::max())
572 << 8;
573 if (MCI.size() <= Index)
574 return Sentinal;
575 MCOperand const &MCO = MCI.getOperand(Index);
576 if (!MCO.isExpr())
577 return Sentinal;
578 int64_t Value;
579 if (!MCO.getExpr()->evaluateAsAbsolute(Value))
580 return Sentinal;
581 return Value;
582}
Colin LeMahieu98c8e072016-02-15 18:42:07 +0000583
584void HexagonMCInstrInfo::setMustExtend(MCExpr &Expr, bool Val) {
585 HexagonMCExpr &HExpr = *llvm::cast<HexagonMCExpr>(&Expr);
586 HExpr.setMustExtend(Val);
587}
588
589bool HexagonMCInstrInfo::mustExtend(MCExpr const &Expr) {
590 HexagonMCExpr const &HExpr = *llvm::cast<HexagonMCExpr>(&Expr);
591 return HExpr.mustExtend();
592}
593void HexagonMCInstrInfo::setMustNotExtend(MCExpr const &Expr, bool Val) {
594 HexagonMCExpr &HExpr =
595 const_cast<HexagonMCExpr &>(*llvm::cast<HexagonMCExpr>(&Expr));
596 HExpr.setMustNotExtend(Val);
597}
598bool HexagonMCInstrInfo::mustNotExtend(MCExpr const &Expr) {
599 HexagonMCExpr const &HExpr = *llvm::cast<HexagonMCExpr>(&Expr);
600 return HExpr.mustNotExtend();
601}
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000602
Colin LeMahieub3c97272015-11-13 07:58:06 +0000603void HexagonMCInstrInfo::padEndloop(MCContext &Context, MCInst &MCB) {
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000604 MCInst Nop;
605 Nop.setOpcode(Hexagon::A2_nop);
606 assert(isBundle(MCB));
607 while ((HexagonMCInstrInfo::isInnerLoop(MCB) &&
608 (HexagonMCInstrInfo::bundleSize(MCB) < HEXAGON_PACKET_INNER_SIZE)) ||
609 ((HexagonMCInstrInfo::isOuterLoop(MCB) &&
610 (HexagonMCInstrInfo::bundleSize(MCB) < HEXAGON_PACKET_OUTER_SIZE))))
Colin LeMahieub3c97272015-11-13 07:58:06 +0000611 MCB.addOperand(MCOperand::createInst(new (Context) MCInst(Nop)));
Colin LeMahieu1174fea2015-02-19 21:10:50 +0000612}
613
Colin LeMahieub23c47b2015-05-31 21:57:09 +0000614bool HexagonMCInstrInfo::prefersSlot3(MCInstrInfo const &MCII,
615 MCInst const &MCI) {
616 if (HexagonMCInstrInfo::getType(MCII, MCI) == HexagonII::TypeCR)
617 return false;
618
619 unsigned SchedClass = HexagonMCInstrInfo::getDesc(MCII, MCI).getSchedClass();
620 switch (SchedClass) {
621 case Hexagon::Sched::ALU32_3op_tc_2_SLOT0123:
622 case Hexagon::Sched::ALU64_tc_2_SLOT23:
623 case Hexagon::Sched::ALU64_tc_3x_SLOT23:
624 case Hexagon::Sched::M_tc_2_SLOT23:
625 case Hexagon::Sched::M_tc_3x_SLOT23:
626 case Hexagon::Sched::S_2op_tc_2_SLOT23:
627 case Hexagon::Sched::S_3op_tc_2_SLOT23:
628 case Hexagon::Sched::S_3op_tc_3x_SLOT23:
629 return true;
630 }
631 return false;
632}
633
Colin LeMahieube8c4532015-06-05 16:00:11 +0000634void HexagonMCInstrInfo::replaceDuplex(MCContext &Context, MCInst &MCB,
635 DuplexCandidate Candidate) {
636 assert(Candidate.packetIndexI < MCB.size());
637 assert(Candidate.packetIndexJ < MCB.size());
638 assert(isBundle(MCB));
639 MCInst *Duplex =
640 deriveDuplex(Context, Candidate.iClass,
641 *MCB.getOperand(Candidate.packetIndexJ).getInst(),
642 *MCB.getOperand(Candidate.packetIndexI).getInst());
643 assert(Duplex != nullptr);
644 MCB.getOperand(Candidate.packetIndexI).setInst(Duplex);
645 MCB.erase(MCB.begin() + Candidate.packetIndexJ);
646}
647
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000648void HexagonMCInstrInfo::setInnerLoop(MCInst &MCI) {
649 assert(isBundle(MCI));
650 MCOperand &Operand = MCI.getOperand(0);
651 Operand.setImm(Operand.getImm() | innerLoopMask);
Colin LeMahieu1174fea2015-02-19 21:10:50 +0000652}
653
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000654void HexagonMCInstrInfo::setMemReorderDisabled(MCInst &MCI) {
655 assert(isBundle(MCI));
656 MCOperand &Operand = MCI.getOperand(0);
657 Operand.setImm(Operand.getImm() | memReorderDisabledMask);
658 assert(isMemReorderDisabled(MCI));
659}
660
661void HexagonMCInstrInfo::setMemStoreReorderEnabled(MCInst &MCI) {
662 assert(isBundle(MCI));
663 MCOperand &Operand = MCI.getOperand(0);
664 Operand.setImm(Operand.getImm() | memStoreReorderEnabledMask);
665 assert(isMemStoreReorderEnabled(MCI));
666}
667
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000668void HexagonMCInstrInfo::setOuterLoop(MCInst &MCI) {
669 assert(isBundle(MCI));
670 MCOperand &Operand = MCI.getOperand(0);
671 Operand.setImm(Operand.getImm() | outerLoopMask);
Colin LeMahieu1174fea2015-02-19 21:10:50 +0000672}
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000673}