blob: 88bbe76472b2137ec4db442bbdc2e701c7c4f318 [file] [log] [blame]
Colin LeMahieuaf304e52015-02-19 19:00:00 +00001//===- HexagonMCInstrInfo.cpp - Hexagon sub-class of MCInst ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This class extends MCInstrInfo to allow Hexagon specific MCInstr queries
11//
12//===----------------------------------------------------------------------===//
13
Colin LeMahieu68d967d2015-05-29 14:44:13 +000014#include "Hexagon.h"
Colin LeMahieuaf304e52015-02-19 19:00:00 +000015#include "HexagonBaseInfo.h"
Colin LeMahieu68d967d2015-05-29 14:44:13 +000016#include "HexagonMCInstrInfo.h"
Colin LeMahieuaf304e52015-02-19 19:00:00 +000017
18namespace llvm {
Colin LeMahieu68d967d2015-05-29 14:44:13 +000019iterator_range<MCInst::const_iterator>
20HexagonMCInstrInfo::bundleInstructions(MCInst const &MCI) {
21 assert(isBundle(MCI));
22 return iterator_range<MCInst::const_iterator>(
23 MCI.begin() + bundleInstructionsOffset, MCI.end());
24}
25
26size_t HexagonMCInstrInfo::bundleSize(MCInst const &MCI) {
27 if (HexagonMCInstrInfo::isBundle(MCI))
28 return (MCI.size() - bundleInstructionsOffset);
29 else
30 return (1);
Colin LeMahieu1174fea2015-02-19 21:10:50 +000031}
32
Colin LeMahieub6625652015-05-01 21:14:21 +000033HexagonII::MemAccessSize
34HexagonMCInstrInfo::getAccessSize(MCInstrInfo const &MCII, MCInst const &MCI) {
35 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
36
37 return (HexagonII::MemAccessSize((F >> HexagonII::MemAccessSizePos) &
38 HexagonII::MemAccesSizeMask));
39}
40
Colin LeMahieu745c4712015-02-19 19:49:27 +000041unsigned HexagonMCInstrInfo::getBitCount(MCInstrInfo const &MCII,
42 MCInst const &MCI) {
43 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
44 return ((F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask);
45}
46
Colin LeMahieu745c4712015-02-19 19:49:27 +000047// Return constant extended operand number.
48unsigned short HexagonMCInstrInfo::getCExtOpNum(MCInstrInfo const &MCII,
49 MCInst const &MCI) {
50 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
51 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask);
52}
53
Colin LeMahieu1174fea2015-02-19 21:10:50 +000054MCInstrDesc const &HexagonMCInstrInfo::getDesc(MCInstrInfo const &MCII,
55 MCInst const &MCI) {
56 return (MCII.get(MCI.getOpcode()));
57}
58
Colin LeMahieub6625652015-05-01 21:14:21 +000059unsigned HexagonMCInstrInfo::getExtentAlignment(MCInstrInfo const &MCII,
60 MCInst const &MCI) {
61 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
62 return ((F >> HexagonII::ExtentAlignPos) & HexagonII::ExtentAlignMask);
63}
64
65unsigned HexagonMCInstrInfo::getExtentBits(MCInstrInfo const &MCII,
66 MCInst const &MCI) {
67 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
68 return ((F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask);
69}
70
Colin LeMahieuaf304e52015-02-19 19:00:00 +000071// Return the max value that a constant extendable operand can have
72// without being extended.
73int HexagonMCInstrInfo::getMaxValue(MCInstrInfo const &MCII,
74 MCInst const &MCI) {
Colin LeMahieu745c4712015-02-19 19:49:27 +000075 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
Colin LeMahieuaf304e52015-02-19 19:00:00 +000076 unsigned isSigned =
77 (F >> HexagonII::ExtentSignedPos) & HexagonII::ExtentSignedMask;
78 unsigned bits = (F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask;
79
80 if (isSigned) // if value is signed
81 return ~(-1U << (bits - 1));
82 else
83 return ~(-1U << bits);
84}
Colin LeMahieu745c4712015-02-19 19:49:27 +000085
86// Return the min value that a constant extendable operand can have
87// without being extended.
88int HexagonMCInstrInfo::getMinValue(MCInstrInfo const &MCII,
89 MCInst const &MCI) {
90 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
91 unsigned isSigned =
92 (F >> HexagonII::ExtentSignedPos) & HexagonII::ExtentSignedMask;
93 unsigned bits = (F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask;
94
95 if (isSigned) // if value is signed
96 return -1U << (bits - 1);
97 else
98 return 0;
99}
100
Colin LeMahieub6625652015-05-01 21:14:21 +0000101char const *HexagonMCInstrInfo::getName(MCInstrInfo const &MCII,
102 MCInst const &MCI) {
103 return MCII.getName(MCI.getOpcode());
104}
105
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000106unsigned short HexagonMCInstrInfo::getNewValueOp(MCInstrInfo const &MCII,
Colin LeMahieu745c4712015-02-19 19:49:27 +0000107 MCInst const &MCI) {
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000108 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
109 return ((F >> HexagonII::NewValueOpPos) & HexagonII::NewValueOpMask);
110}
111
112MCOperand const &HexagonMCInstrInfo::getNewValueOperand(MCInstrInfo const &MCII,
113 MCInst const &MCI) {
Colin LeMahieu745c4712015-02-19 19:49:27 +0000114 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
115 unsigned const O =
116 (F >> HexagonII::NewValueOpPos) & HexagonII::NewValueOpMask;
117 MCOperand const &MCO = MCI.getOperand(O);
118
119 assert((HexagonMCInstrInfo::isNewValue(MCII, MCI) ||
120 HexagonMCInstrInfo::hasNewValue(MCII, MCI)) &&
121 MCO.isReg());
122 return (MCO);
123}
124
125// Return the Hexagon ISA class for the insn.
126unsigned HexagonMCInstrInfo::getType(MCInstrInfo const &MCII,
127 MCInst const &MCI) {
128 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
129
130 return ((F >> HexagonII::TypePos) & HexagonII::TypeMask);
131}
132
133// Return whether the instruction is a legal new-value producer.
134bool HexagonMCInstrInfo::hasNewValue(MCInstrInfo const &MCII,
135 MCInst const &MCI) {
136 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
137 return ((F >> HexagonII::hasNewValuePos) & HexagonII::hasNewValueMask);
138}
139
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000140bool HexagonMCInstrInfo::isBundle(MCInst const &MCI) {
141 auto Result = Hexagon::BUNDLE == MCI.getOpcode();
142 assert(!Result || (MCI.size() > 0 && MCI.getOperand(0).isImm()));
143 return Result;
144}
145
Colin LeMahieu745c4712015-02-19 19:49:27 +0000146// Return whether the insn is an actual insn.
147bool HexagonMCInstrInfo::isCanon(MCInstrInfo const &MCII, MCInst const &MCI) {
148 return (!HexagonMCInstrInfo::getDesc(MCII, MCI).isPseudo() &&
149 !HexagonMCInstrInfo::isPrefix(MCII, MCI) &&
150 HexagonMCInstrInfo::getType(MCII, MCI) != HexagonII::TypeENDLOOP);
151}
152
153// Return whether the instruction needs to be constant extended.
154// 1) Always return true if the instruction has 'isExtended' flag set.
155//
156// isExtendable:
157// 2) For immediate extended operands, return true only if the value is
158// out-of-range.
159// 3) For global address, always return true.
160
161bool HexagonMCInstrInfo::isConstExtended(MCInstrInfo const &MCII,
162 MCInst const &MCI) {
163 if (HexagonMCInstrInfo::isExtended(MCII, MCI))
164 return true;
165
166 if (!HexagonMCInstrInfo::isExtendable(MCII, MCI))
167 return false;
168
169 short ExtOpNum = HexagonMCInstrInfo::getCExtOpNum(MCII, MCI);
170 int MinValue = HexagonMCInstrInfo::getMinValue(MCII, MCI);
171 int MaxValue = HexagonMCInstrInfo::getMaxValue(MCII, MCI);
172 MCOperand const &MO = MCI.getOperand(ExtOpNum);
173
174 // We could be using an instruction with an extendable immediate and shoehorn
175 // a global address into it. If it is a global address it will be constant
176 // extended. We do this for COMBINE.
177 // We currently only handle isGlobal() because it is the only kind of
178 // object we are going to end up with here for now.
179 // In the future we probably should add isSymbol(), etc.
180 if (MO.isExpr())
181 return true;
182
183 // If the extendable operand is not 'Immediate' type, the instruction should
184 // have 'isExtended' flag set.
185 assert(MO.isImm() && "Extendable operand must be Immediate type");
186
187 int ImmValue = MO.getImm();
188 return (ImmValue < MinValue || ImmValue > MaxValue);
189}
190
191// Return true if the instruction may be extended based on the operand value.
192bool HexagonMCInstrInfo::isExtendable(MCInstrInfo const &MCII,
193 MCInst const &MCI) {
194 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
195 return (F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask;
196}
197
198// Return whether the instruction must be always extended.
199bool HexagonMCInstrInfo::isExtended(MCInstrInfo const &MCII,
200 MCInst const &MCI) {
201 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
202 return (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
203}
204
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000205bool HexagonMCInstrInfo::isImmext(MCInst const &MCI) {
206 auto Op = MCI.getOpcode();
207 return (Op == Hexagon::A4_ext_b || Op == Hexagon::A4_ext_c ||
208 Op == Hexagon::A4_ext_g || Op == Hexagon::A4_ext);
209}
210
211bool HexagonMCInstrInfo::isInnerLoop(MCInst const &MCI) {
212 assert(isBundle(MCI));
213 int64_t Flags = MCI.getOperand(0).getImm();
214 return (Flags & innerLoopMask) != 0;
215}
216
Colin LeMahieu745c4712015-02-19 19:49:27 +0000217// Return whether the insn is a new-value consumer.
218bool HexagonMCInstrInfo::isNewValue(MCInstrInfo const &MCII,
219 MCInst const &MCI) {
220 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
221 return ((F >> HexagonII::NewValuePos) & HexagonII::NewValueMask);
222}
223
224// Return whether the operand can be constant extended.
225bool HexagonMCInstrInfo::isOperandExtended(MCInstrInfo const &MCII,
226 MCInst const &MCI,
227 unsigned short OperandNum) {
228 uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
229 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask) ==
230 OperandNum;
231}
232
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000233bool HexagonMCInstrInfo::isOuterLoop(MCInst const &MCI) {
234 assert(isBundle(MCI));
235 int64_t Flags = MCI.getOperand(0).getImm();
236 return (Flags & outerLoopMask) != 0;
Colin LeMahieu1174fea2015-02-19 21:10:50 +0000237}
238
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000239bool HexagonMCInstrInfo::isPredicated(MCInstrInfo const &MCII,
240 MCInst const &MCI) {
241 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
242 return ((F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
243}
244
245bool HexagonMCInstrInfo::isPredicatedTrue(MCInstrInfo const &MCII,
246 MCInst const &MCI) {
247 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
248 return (
249 !((F >> HexagonII::PredicatedFalsePos) & HexagonII::PredicatedFalseMask));
Colin LeMahieu1174fea2015-02-19 21:10:50 +0000250}
251
Colin LeMahieu745c4712015-02-19 19:49:27 +0000252// Return whether the insn is a prefix.
253bool HexagonMCInstrInfo::isPrefix(MCInstrInfo const &MCII, MCInst const &MCI) {
254 return (HexagonMCInstrInfo::getType(MCII, MCI) == HexagonII::TypePREFIX);
255}
256
257// Return whether the insn is solo, i.e., cannot be in a packet.
258bool HexagonMCInstrInfo::isSolo(MCInstrInfo const &MCII, MCInst const &MCI) {
259 const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
260 return ((F >> HexagonII::SoloPos) & HexagonII::SoloMask);
261}
Colin LeMahieu1174fea2015-02-19 21:10:50 +0000262
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000263void HexagonMCInstrInfo::padEndloop(MCInst &MCB) {
264 MCInst Nop;
265 Nop.setOpcode(Hexagon::A2_nop);
266 assert(isBundle(MCB));
267 while ((HexagonMCInstrInfo::isInnerLoop(MCB) &&
268 (HexagonMCInstrInfo::bundleSize(MCB) < HEXAGON_PACKET_INNER_SIZE)) ||
269 ((HexagonMCInstrInfo::isOuterLoop(MCB) &&
270 (HexagonMCInstrInfo::bundleSize(MCB) < HEXAGON_PACKET_OUTER_SIZE))))
271 MCB.addOperand(MCOperand::createInst(new MCInst(Nop)));
Colin LeMahieu1174fea2015-02-19 21:10:50 +0000272}
273
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000274void HexagonMCInstrInfo::setInnerLoop(MCInst &MCI) {
275 assert(isBundle(MCI));
276 MCOperand &Operand = MCI.getOperand(0);
277 Operand.setImm(Operand.getImm() | innerLoopMask);
Colin LeMahieu1174fea2015-02-19 21:10:50 +0000278}
279
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000280void HexagonMCInstrInfo::setOuterLoop(MCInst &MCI) {
281 assert(isBundle(MCI));
282 MCOperand &Operand = MCI.getOperand(0);
283 Operand.setImm(Operand.getImm() | outerLoopMask);
Colin LeMahieu1174fea2015-02-19 21:10:50 +0000284}
Colin LeMahieuaf304e52015-02-19 19:00:00 +0000285}