[Hexagon] Disassembling, printing, and emitting instructions a whole-bundle at a...
[oota-llvm.git] / lib / Target / Hexagon / MCTargetDesc / HexagonMCInstrInfo.cpp
1 //===- 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
14 #include "Hexagon.h"
15 #include "HexagonBaseInfo.h"
16 #include "HexagonMCInstrInfo.h"
17
18 namespace llvm {
19 iterator_range<MCInst::const_iterator>
20 HexagonMCInstrInfo::bundleInstructions(MCInst const &MCI) {
21   assert(isBundle(MCI));
22   return iterator_range<MCInst::const_iterator>(
23       MCI.begin() + bundleInstructionsOffset, MCI.end());
24 }
25
26 size_t HexagonMCInstrInfo::bundleSize(MCInst const &MCI) {
27   if (HexagonMCInstrInfo::isBundle(MCI))
28     return (MCI.size() - bundleInstructionsOffset);
29   else
30     return (1);
31 }
32
33 HexagonII::MemAccessSize
34 HexagonMCInstrInfo::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
41 unsigned 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
47 // Return constant extended operand number.
48 unsigned 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
54 MCInstrDesc const &HexagonMCInstrInfo::getDesc(MCInstrInfo const &MCII,
55                                                MCInst const &MCI) {
56   return (MCII.get(MCI.getOpcode()));
57 }
58
59 unsigned 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
65 unsigned 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
71 // Return the max value that a constant extendable operand can have
72 // without being extended.
73 int HexagonMCInstrInfo::getMaxValue(MCInstrInfo const &MCII,
74                                     MCInst const &MCI) {
75   uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
76   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 }
85
86 // Return the min value that a constant extendable operand can have
87 // without being extended.
88 int 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
101 char const *HexagonMCInstrInfo::getName(MCInstrInfo const &MCII,
102                                         MCInst const &MCI) {
103   return MCII.getName(MCI.getOpcode());
104 }
105
106 unsigned short HexagonMCInstrInfo::getNewValueOp(MCInstrInfo const &MCII,
107                                                  MCInst const &MCI) {
108   const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
109   return ((F >> HexagonII::NewValueOpPos) & HexagonII::NewValueOpMask);
110 }
111
112 MCOperand const &HexagonMCInstrInfo::getNewValueOperand(MCInstrInfo const &MCII,
113                                                         MCInst const &MCI) {
114   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.
126 unsigned 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.
134 bool 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
140 bool 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
146 // Return whether the insn is an actual insn.
147 bool 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
161 bool 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.
192 bool 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.
199 bool 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
205 bool 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
211 bool HexagonMCInstrInfo::isInnerLoop(MCInst const &MCI) {
212   assert(isBundle(MCI));
213   int64_t Flags = MCI.getOperand(0).getImm();
214   return (Flags & innerLoopMask) != 0;
215 }
216
217 // Return whether the insn is a new-value consumer.
218 bool 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.
225 bool 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
233 bool HexagonMCInstrInfo::isOuterLoop(MCInst const &MCI) {
234   assert(isBundle(MCI));
235   int64_t Flags = MCI.getOperand(0).getImm();
236   return (Flags & outerLoopMask) != 0;
237 }
238
239 bool 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
245 bool 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));
250 }
251
252 // Return whether the insn is a prefix.
253 bool 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.
258 bool 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 }
262
263 void 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)));
272 }
273
274 void HexagonMCInstrInfo::setInnerLoop(MCInst &MCI) {
275   assert(isBundle(MCI));
276   MCOperand &Operand = MCI.getOperand(0);
277   Operand.setImm(Operand.getImm() | innerLoopMask);
278 }
279
280 void HexagonMCInstrInfo::setOuterLoop(MCInst &MCI) {
281   assert(isBundle(MCI));
282   MCOperand &Operand = MCI.getOperand(0);
283   Operand.setImm(Operand.getImm() | outerLoopMask);
284 }
285 }