blob: 9b3d11a8fed8416b5609f75ef1d60697537087f6 [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);
Benjamin Kramer986a49b2016-02-16 09:53:47 +0000196 assert(HExpr.getExpr());
Colin LeMahieu98c8e072016-02-15 18:42:07 +0000197 return *HExpr.getExpr();
198}
199
Colin LeMahieu86f218e2015-05-30 18:55:47 +0000200unsigned short HexagonMCInstrInfo::getExtendableOp(MCInstrInfo const &MCII,
201 MCInst const &MCI) {
202 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
203 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask);
204}
205
206MCOperand const &
207HexagonMCInstrInfo::getExtendableOperand(MCInstrInfo const &MCII,
208 MCInst const &MCI) {
209 unsigned O = HexagonMCInstrInfo::getExtendableOp(MCII, MCI);
210 MCOperand const &MO = MCI.getOperand(O);
211
212 assert((HexagonMCInstrInfo::isExtendable(MCII, MCI) ||
213 HexagonMCInstrInfo::isExtended(MCII, MCI)) &&
214 (MO.isImm() || MO.isExpr()));
215 return (MO);
216}
217
Colin LeMahieub6625652015-05-01 21:14:21 +0000218unsigned HexagonMCInstrInfo::getExtentAlignment(MCInstrInfo const &MCII,
219 MCInst const &MCI) {
220 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
221 return ((F >> HexagonII::ExtentAlignPos) & HexagonII::ExtentAlignMask);
222}
223
224unsigned HexagonMCInstrInfo::getExtentBits(MCInstrInfo const &MCII,
225 MCInst const &MCI) {
226 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
227 return ((F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask);
228}
229
Colin LeMahieuaf304e52015-02-19 19:00:00 +0000230// Return the max value that a constant extendable operand can have
231// without being extended.
232int HexagonMCInstrInfo::getMaxValue(MCInstrInfo const &MCII,
233 MCInst const &MCI) {
Colin LeMahieu745c4712015-02-19 19:49:27 +0000234 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
Colin LeMahieuaf304e52015-02-19 19:00:00 +0000235 unsigned isSigned =
236 (F >> HexagonII::ExtentSignedPos) & HexagonII::ExtentSignedMask;
237 unsigned bits = (F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask;
238
239 if (isSigned) // if value is signed
240 return ~(-1U << (bits - 1));
241 else
242 return ~(-1U << bits);
243}
Colin LeMahieu745c4712015-02-19 19:49:27 +0000244
245// Return the min value that a constant extendable operand can have
246// without being extended.
247int HexagonMCInstrInfo::getMinValue(MCInstrInfo const &MCII,
248 MCInst const &MCI) {
249 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
250 unsigned isSigned =
251 (F >> HexagonII::ExtentSignedPos) & HexagonII::ExtentSignedMask;
252 unsigned bits = (F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask;
253
254 if (isSigned) // if value is signed
255 return -1U << (bits - 1);
256 else
257 return 0;
258}
259
Colin LeMahieub6625652015-05-01 21:14:21 +0000260char const *HexagonMCInstrInfo::getName(MCInstrInfo const &MCII,
261 MCInst const &MCI) {
262 return MCII.getName(MCI.getOpcode());
263}
264
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000265unsigned short HexagonMCInstrInfo::getNewValueOp(MCInstrInfo const &MCII,
Colin LeMahieu745c4712015-02-19 19:49:27 +0000266 MCInst const &MCI) {
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000267 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
268 return ((F >> HexagonII::NewValueOpPos) & HexagonII::NewValueOpMask);
269}
270
271MCOperand const &HexagonMCInstrInfo::getNewValueOperand(MCInstrInfo const &MCII,
272 MCInst const &MCI) {
Colin LeMahieu745c4712015-02-19 19:49:27 +0000273 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
274 unsigned const O =
275 (F >> HexagonII::NewValueOpPos) & HexagonII::NewValueOpMask;
276 MCOperand const &MCO = MCI.getOperand(O);
277
278 assert((HexagonMCInstrInfo::isNewValue(MCII, MCI) ||
279 HexagonMCInstrInfo::hasNewValue(MCII, MCI)) &&
280 MCO.isReg());
281 return (MCO);
282}
283
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000284/// Return the new value or the newly produced value.
285unsigned short HexagonMCInstrInfo::getNewValueOp2(MCInstrInfo const &MCII,
286 MCInst const &MCI) {
287 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
288 return ((F >> HexagonII::NewValueOpPos2) & HexagonII::NewValueOpMask2);
289}
290
291MCOperand const &
292HexagonMCInstrInfo::getNewValueOperand2(MCInstrInfo const &MCII,
293 MCInst const &MCI) {
294 unsigned O = HexagonMCInstrInfo::getNewValueOp2(MCII, MCI);
295 MCOperand const &MCO = MCI.getOperand(O);
296
297 assert((HexagonMCInstrInfo::isNewValue(MCII, MCI) ||
298 HexagonMCInstrInfo::hasNewValue2(MCII, MCI)) &&
299 MCO.isReg());
300 return (MCO);
301}
302
Colin LeMahieub23c47b2015-05-31 21:57:09 +0000303int HexagonMCInstrInfo::getSubTarget(MCInstrInfo const &MCII,
304 MCInst const &MCI) {
305 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
306
307 HexagonII::SubTarget Target = static_cast<HexagonII::SubTarget>(
308 (F >> HexagonII::validSubTargetPos) & HexagonII::validSubTargetMask);
309
310 switch (Target) {
311 default:
312 return Hexagon::ArchV4;
313 case HexagonII::HasV5SubT:
314 return Hexagon::ArchV5;
315 }
316}
317
Colin LeMahieu745c4712015-02-19 19:49:27 +0000318// Return the Hexagon ISA class for the insn.
319unsigned HexagonMCInstrInfo::getType(MCInstrInfo const &MCII,
320 MCInst const &MCI) {
321 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
322
323 return ((F >> HexagonII::TypePos) & HexagonII::TypeMask);
324}
325
Colin LeMahieub23c47b2015-05-31 21:57:09 +0000326unsigned HexagonMCInstrInfo::getUnits(MCInstrInfo const &MCII,
327 MCSubtargetInfo const &STI,
328 MCInst const &MCI) {
329
330 const InstrItinerary *II = STI.getSchedModel().InstrItineraries;
331 int SchedClass = HexagonMCInstrInfo::getDesc(MCII, MCI).getSchedClass();
332 return ((II[SchedClass].FirstStage + HexagonStages)->getUnits());
333}
334
335bool HexagonMCInstrInfo::hasImmExt(MCInst const &MCI) {
336 if (!HexagonMCInstrInfo::isBundle(MCI))
337 return false;
338
339 for (const auto &I : HexagonMCInstrInfo::bundleInstructions(MCI)) {
340 auto MI = I.getInst();
341 if (isImmext(*MI))
342 return true;
343 }
344
345 return false;
346}
347
Colin LeMahieube8c4532015-06-05 16:00:11 +0000348bool HexagonMCInstrInfo::hasExtenderForIndex(MCInst const &MCB, size_t Index) {
349 return extenderForIndex(MCB, Index) != nullptr;
350}
351
Colin LeMahieu745c4712015-02-19 19:49:27 +0000352// Return whether the instruction is a legal new-value producer.
353bool HexagonMCInstrInfo::hasNewValue(MCInstrInfo const &MCII,
354 MCInst const &MCI) {
355 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
356 return ((F >> HexagonII::hasNewValuePos) & HexagonII::hasNewValueMask);
357}
358
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000359/// Return whether the insn produces a second value.
360bool HexagonMCInstrInfo::hasNewValue2(MCInstrInfo const &MCII,
361 MCInst const &MCI) {
362 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
363 return ((F >> HexagonII::hasNewValuePos2) & HexagonII::hasNewValueMask2);
364}
365
Colin LeMahieu86f218e2015-05-30 18:55:47 +0000366MCInst const &HexagonMCInstrInfo::instruction(MCInst const &MCB, size_t Index) {
367 assert(isBundle(MCB));
368 assert(Index < HEXAGON_PACKET_SIZE);
369 return *MCB.getOperand(bundleInstructionsOffset + Index).getInst();
370}
371
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000372bool HexagonMCInstrInfo::isBundle(MCInst const &MCI) {
373 auto Result = Hexagon::BUNDLE == MCI.getOpcode();
374 assert(!Result || (MCI.size() > 0 && MCI.getOperand(0).isImm()));
375 return Result;
376}
377
Colin LeMahieu745c4712015-02-19 19:49:27 +0000378// Return whether the insn is an actual insn.
379bool HexagonMCInstrInfo::isCanon(MCInstrInfo const &MCII, MCInst const &MCI) {
380 return (!HexagonMCInstrInfo::getDesc(MCII, MCI).isPseudo() &&
381 !HexagonMCInstrInfo::isPrefix(MCII, MCI) &&
382 HexagonMCInstrInfo::getType(MCII, MCI) != HexagonII::TypeENDLOOP);
383}
384
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000385bool HexagonMCInstrInfo::isCompound(MCInstrInfo const &MCII,
386 MCInst const &MCI) {
387 return (getType(MCII, MCI) == HexagonII::TypeCOMPOUND);
388}
389
Colin LeMahieube8c4532015-06-05 16:00:11 +0000390bool HexagonMCInstrInfo::isDblRegForSubInst(unsigned Reg) {
391 return ((Reg >= Hexagon::D0 && Reg <= Hexagon::D3) ||
392 (Reg >= Hexagon::D8 && Reg <= Hexagon::D11));
393}
394
395bool HexagonMCInstrInfo::isDuplex(MCInstrInfo const &MCII, MCInst const &MCI) {
396 return HexagonII::TypeDUPLEX == HexagonMCInstrInfo::getType(MCII, MCI);
397}
398
Colin LeMahieu745c4712015-02-19 19:49:27 +0000399// Return whether the instruction needs to be constant extended.
400// 1) Always return true if the instruction has 'isExtended' flag set.
401//
402// isExtendable:
403// 2) For immediate extended operands, return true only if the value is
404// out-of-range.
405// 3) For global address, always return true.
406
407bool HexagonMCInstrInfo::isConstExtended(MCInstrInfo const &MCII,
408 MCInst const &MCI) {
409 if (HexagonMCInstrInfo::isExtended(MCII, MCI))
410 return true;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000411 // Branch insns are handled as necessary by relaxation.
412 if ((HexagonMCInstrInfo::getType(MCII, MCI) == HexagonII::TypeJ) ||
413 (HexagonMCInstrInfo::getType(MCII, MCI) == HexagonII::TypeCOMPOUND &&
414 HexagonMCInstrInfo::getDesc(MCII, MCI).isBranch()) ||
415 (HexagonMCInstrInfo::getType(MCII, MCI) == HexagonII::TypeNV &&
416 HexagonMCInstrInfo::getDesc(MCII, MCI).isBranch()))
417 return false;
418 // Otherwise loop instructions and other CR insts are handled by relaxation
419 else if ((HexagonMCInstrInfo::getType(MCII, MCI) == HexagonII::TypeCR) &&
420 (MCI.getOpcode() != Hexagon::C4_addipc))
421 return false;
422 else if (!HexagonMCInstrInfo::isExtendable(MCII, MCI))
Colin LeMahieu745c4712015-02-19 19:49:27 +0000423 return false;
424
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000425 MCOperand const &MO = HexagonMCInstrInfo::getExtendableOperand(MCII, MCI);
Colin LeMahieu745c4712015-02-19 19:49:27 +0000426
427 // We could be using an instruction with an extendable immediate and shoehorn
428 // a global address into it. If it is a global address it will be constant
429 // extended. We do this for COMBINE.
430 // We currently only handle isGlobal() because it is the only kind of
431 // object we are going to end up with here for now.
432 // In the future we probably should add isSymbol(), etc.
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000433 assert(!MO.isImm());
434 int64_t Value;
435 if (!MO.getExpr()->evaluateAsAbsolute(Value))
Colin LeMahieu745c4712015-02-19 19:49:27 +0000436 return true;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000437 int MinValue = HexagonMCInstrInfo::getMinValue(MCII, MCI);
438 int MaxValue = HexagonMCInstrInfo::getMaxValue(MCII, MCI);
439 return (MinValue > Value || Value > MaxValue);
Colin LeMahieu745c4712015-02-19 19:49:27 +0000440}
441
Colin LeMahieu745c4712015-02-19 19:49:27 +0000442bool HexagonMCInstrInfo::isExtendable(MCInstrInfo const &MCII,
443 MCInst const &MCI) {
444 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
445 return (F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask;
446}
447
Colin LeMahieu745c4712015-02-19 19:49:27 +0000448bool HexagonMCInstrInfo::isExtended(MCInstrInfo const &MCII,
449 MCInst const &MCI) {
450 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
451 return (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
452}
453
Colin LeMahieub23c47b2015-05-31 21:57:09 +0000454bool HexagonMCInstrInfo::isFloat(MCInstrInfo const &MCII, MCInst const &MCI) {
455 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
456 return ((F >> HexagonII::FPPos) & HexagonII::FPMask);
457}
458
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000459bool HexagonMCInstrInfo::isImmext(MCInst const &MCI) {
460 auto Op = MCI.getOpcode();
461 return (Op == Hexagon::A4_ext_b || Op == Hexagon::A4_ext_c ||
462 Op == Hexagon::A4_ext_g || Op == Hexagon::A4_ext);
463}
464
465bool HexagonMCInstrInfo::isInnerLoop(MCInst const &MCI) {
466 assert(isBundle(MCI));
467 int64_t Flags = MCI.getOperand(0).getImm();
468 return (Flags & innerLoopMask) != 0;
469}
470
Colin LeMahieube8c4532015-06-05 16:00:11 +0000471bool HexagonMCInstrInfo::isIntReg(unsigned Reg) {
472 return (Reg >= Hexagon::R0 && Reg <= Hexagon::R31);
473}
474
475bool HexagonMCInstrInfo::isIntRegForSubInst(unsigned Reg) {
476 return ((Reg >= Hexagon::R0 && Reg <= Hexagon::R7) ||
477 (Reg >= Hexagon::R16 && Reg <= Hexagon::R23));
478}
479
Colin LeMahieu745c4712015-02-19 19:49:27 +0000480// Return whether the insn is a new-value consumer.
481bool HexagonMCInstrInfo::isNewValue(MCInstrInfo const &MCII,
482 MCInst const &MCI) {
483 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
484 return ((F >> HexagonII::NewValuePos) & HexagonII::NewValueMask);
485}
486
487// Return whether the operand can be constant extended.
488bool HexagonMCInstrInfo::isOperandExtended(MCInstrInfo const &MCII,
489 MCInst const &MCI,
490 unsigned short OperandNum) {
491 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
492 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask) ==
493 OperandNum;
494}
495
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000496bool HexagonMCInstrInfo::isOuterLoop(MCInst const &MCI) {
497 assert(isBundle(MCI));
498 int64_t Flags = MCI.getOperand(0).getImm();
499 return (Flags & outerLoopMask) != 0;
Colin LeMahieu1174fea2015-02-19 21:10:50 +0000500}
501
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000502bool HexagonMCInstrInfo::isPredicated(MCInstrInfo const &MCII,
503 MCInst const &MCI) {
504 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
505 return ((F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
506}
507
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000508bool HexagonMCInstrInfo::isPredicateLate(MCInstrInfo const &MCII,
509 MCInst const &MCI) {
510 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
511 return (F >> HexagonII::PredicateLatePos & HexagonII::PredicateLateMask);
512}
513
514/// Return whether the insn is newly predicated.
515bool HexagonMCInstrInfo::isPredicatedNew(MCInstrInfo const &MCII,
516 MCInst const &MCI) {
517 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
518 return ((F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask);
519}
520
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000521bool HexagonMCInstrInfo::isPredicatedTrue(MCInstrInfo const &MCII,
522 MCInst const &MCI) {
523 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
524 return (
525 !((F >> HexagonII::PredicatedFalsePos) & HexagonII::PredicatedFalseMask));
Colin LeMahieu1174fea2015-02-19 21:10:50 +0000526}
527
Colin LeMahieube8c4532015-06-05 16:00:11 +0000528bool HexagonMCInstrInfo::isPredReg(unsigned Reg) {
529 return (Reg >= Hexagon::P0 && Reg <= Hexagon::P3_0);
530}
531
Colin LeMahieu745c4712015-02-19 19:49:27 +0000532bool HexagonMCInstrInfo::isPrefix(MCInstrInfo const &MCII, MCInst const &MCI) {
533 return (HexagonMCInstrInfo::getType(MCII, MCI) == HexagonII::TypePREFIX);
534}
535
Colin LeMahieu745c4712015-02-19 19:49:27 +0000536bool HexagonMCInstrInfo::isSolo(MCInstrInfo const &MCII, MCInst const &MCI) {
537 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
538 return ((F >> HexagonII::SoloPos) & HexagonII::SoloMask);
539}
Colin LeMahieu1174fea2015-02-19 21:10:50 +0000540
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000541bool HexagonMCInstrInfo::isMemReorderDisabled(MCInst const &MCI) {
542 assert(isBundle(MCI));
543 auto Flags = MCI.getOperand(0).getImm();
544 return (Flags & memReorderDisabledMask) != 0;
545}
546
547bool HexagonMCInstrInfo::isMemStoreReorderEnabled(MCInst const &MCI) {
548 assert(isBundle(MCI));
549 auto Flags = MCI.getOperand(0).getImm();
550 return (Flags & memStoreReorderEnabledMask) != 0;
551}
552
Colin LeMahieub23c47b2015-05-31 21:57:09 +0000553bool HexagonMCInstrInfo::isSoloAX(MCInstrInfo const &MCII, MCInst const &MCI) {
554 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
555 return ((F >> HexagonII::SoloAXPos) & HexagonII::SoloAXMask);
556}
557
558bool HexagonMCInstrInfo::isSoloAin1(MCInstrInfo const &MCII,
559 MCInst const &MCI) {
560 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
561 return ((F >> HexagonII::SoloAin1Pos) & HexagonII::SoloAin1Mask);
562}
563
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000564bool HexagonMCInstrInfo::isVector(MCInstrInfo const &MCII, MCInst const &MCI) {
565 if ((getType(MCII, MCI) <= HexagonII::TypeCVI_LAST) &&
566 (getType(MCII, MCI) >= HexagonII::TypeCVI_FIRST))
567 return true;
568 return false;
569}
570
571int64_t HexagonMCInstrInfo::minConstant(MCInst const &MCI, size_t Index) {
572 auto Sentinal = static_cast<int64_t>(std::numeric_limits<uint32_t>::max())
573 << 8;
574 if (MCI.size() <= Index)
575 return Sentinal;
576 MCOperand const &MCO = MCI.getOperand(Index);
577 if (!MCO.isExpr())
578 return Sentinal;
579 int64_t Value;
580 if (!MCO.getExpr()->evaluateAsAbsolute(Value))
581 return Sentinal;
582 return Value;
583}
Colin LeMahieu98c8e072016-02-15 18:42:07 +0000584
585void HexagonMCInstrInfo::setMustExtend(MCExpr &Expr, bool Val) {
586 HexagonMCExpr &HExpr = *llvm::cast<HexagonMCExpr>(&Expr);
587 HExpr.setMustExtend(Val);
588}
589
590bool HexagonMCInstrInfo::mustExtend(MCExpr const &Expr) {
591 HexagonMCExpr const &HExpr = *llvm::cast<HexagonMCExpr>(&Expr);
592 return HExpr.mustExtend();
593}
594void HexagonMCInstrInfo::setMustNotExtend(MCExpr const &Expr, bool Val) {
595 HexagonMCExpr &HExpr =
596 const_cast<HexagonMCExpr &>(*llvm::cast<HexagonMCExpr>(&Expr));
597 HExpr.setMustNotExtend(Val);
598}
599bool HexagonMCInstrInfo::mustNotExtend(MCExpr const &Expr) {
600 HexagonMCExpr const &HExpr = *llvm::cast<HexagonMCExpr>(&Expr);
601 return HExpr.mustNotExtend();
602}
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000603
Colin LeMahieub3c97272015-11-13 07:58:06 +0000604void HexagonMCInstrInfo::padEndloop(MCContext &Context, MCInst &MCB) {
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000605 MCInst Nop;
606 Nop.setOpcode(Hexagon::A2_nop);
607 assert(isBundle(MCB));
608 while ((HexagonMCInstrInfo::isInnerLoop(MCB) &&
609 (HexagonMCInstrInfo::bundleSize(MCB) < HEXAGON_PACKET_INNER_SIZE)) ||
610 ((HexagonMCInstrInfo::isOuterLoop(MCB) &&
611 (HexagonMCInstrInfo::bundleSize(MCB) < HEXAGON_PACKET_OUTER_SIZE))))
Colin LeMahieub3c97272015-11-13 07:58:06 +0000612 MCB.addOperand(MCOperand::createInst(new (Context) MCInst(Nop)));
Colin LeMahieu1174fea2015-02-19 21:10:50 +0000613}
614
Colin LeMahieub23c47b2015-05-31 21:57:09 +0000615bool HexagonMCInstrInfo::prefersSlot3(MCInstrInfo const &MCII,
616 MCInst const &MCI) {
617 if (HexagonMCInstrInfo::getType(MCII, MCI) == HexagonII::TypeCR)
618 return false;
619
620 unsigned SchedClass = HexagonMCInstrInfo::getDesc(MCII, MCI).getSchedClass();
621 switch (SchedClass) {
622 case Hexagon::Sched::ALU32_3op_tc_2_SLOT0123:
623 case Hexagon::Sched::ALU64_tc_2_SLOT23:
624 case Hexagon::Sched::ALU64_tc_3x_SLOT23:
625 case Hexagon::Sched::M_tc_2_SLOT23:
626 case Hexagon::Sched::M_tc_3x_SLOT23:
627 case Hexagon::Sched::S_2op_tc_2_SLOT23:
628 case Hexagon::Sched::S_3op_tc_2_SLOT23:
629 case Hexagon::Sched::S_3op_tc_3x_SLOT23:
630 return true;
631 }
632 return false;
633}
634
Colin LeMahieube8c4532015-06-05 16:00:11 +0000635void HexagonMCInstrInfo::replaceDuplex(MCContext &Context, MCInst &MCB,
636 DuplexCandidate Candidate) {
637 assert(Candidate.packetIndexI < MCB.size());
638 assert(Candidate.packetIndexJ < MCB.size());
639 assert(isBundle(MCB));
640 MCInst *Duplex =
641 deriveDuplex(Context, Candidate.iClass,
642 *MCB.getOperand(Candidate.packetIndexJ).getInst(),
643 *MCB.getOperand(Candidate.packetIndexI).getInst());
644 assert(Duplex != nullptr);
645 MCB.getOperand(Candidate.packetIndexI).setInst(Duplex);
646 MCB.erase(MCB.begin() + Candidate.packetIndexJ);
647}
648
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000649void HexagonMCInstrInfo::setInnerLoop(MCInst &MCI) {
650 assert(isBundle(MCI));
651 MCOperand &Operand = MCI.getOperand(0);
652 Operand.setImm(Operand.getImm() | innerLoopMask);
Colin LeMahieu1174fea2015-02-19 21:10:50 +0000653}
654
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000655void HexagonMCInstrInfo::setMemReorderDisabled(MCInst &MCI) {
656 assert(isBundle(MCI));
657 MCOperand &Operand = MCI.getOperand(0);
658 Operand.setImm(Operand.getImm() | memReorderDisabledMask);
659 assert(isMemReorderDisabled(MCI));
660}
661
662void HexagonMCInstrInfo::setMemStoreReorderEnabled(MCInst &MCI) {
663 assert(isBundle(MCI));
664 MCOperand &Operand = MCI.getOperand(0);
665 Operand.setImm(Operand.getImm() | memStoreReorderEnabledMask);
666 assert(isMemStoreReorderEnabled(MCI));
667}
668
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000669void HexagonMCInstrInfo::setOuterLoop(MCInst &MCI) {
670 assert(isBundle(MCI));
671 MCOperand &Operand = MCI.getOperand(0);
672 Operand.setImm(Operand.getImm() | outerLoopMask);
Colin LeMahieu1174fea2015-02-19 21:10:50 +0000673}
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000674}