[Hexagon] Adding MC packet shuffler.
[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 "HexagonMCInstrInfo.h"
15
16 #include "Hexagon.h"
17 #include "HexagonBaseInfo.h"
18
19 #include "llvm/MC/MCSubtargetInfo.h"
20
21 namespace llvm {
22 iterator_range<MCInst::const_iterator>
23 HexagonMCInstrInfo::bundleInstructions(MCInst const &MCI) {
24   assert(isBundle(MCI));
25   return iterator_range<MCInst::const_iterator>(
26       MCI.begin() + bundleInstructionsOffset, MCI.end());
27 }
28
29 size_t HexagonMCInstrInfo::bundleSize(MCInst const &MCI) {
30   if (HexagonMCInstrInfo::isBundle(MCI))
31     return (MCI.size() - bundleInstructionsOffset);
32   else
33     return (1);
34 }
35
36 HexagonII::MemAccessSize
37 HexagonMCInstrInfo::getAccessSize(MCInstrInfo const &MCII, MCInst const &MCI) {
38   const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
39
40   return (HexagonII::MemAccessSize((F >> HexagonII::MemAccessSizePos) &
41                                    HexagonII::MemAccesSizeMask));
42 }
43
44 unsigned HexagonMCInstrInfo::getBitCount(MCInstrInfo const &MCII,
45                                          MCInst const &MCI) {
46   uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
47   return ((F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask);
48 }
49
50 // Return constant extended operand number.
51 unsigned short HexagonMCInstrInfo::getCExtOpNum(MCInstrInfo const &MCII,
52                                                 MCInst const &MCI) {
53   const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
54   return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask);
55 }
56
57 MCInstrDesc const &HexagonMCInstrInfo::getDesc(MCInstrInfo const &MCII,
58                                                MCInst const &MCI) {
59   return (MCII.get(MCI.getOpcode()));
60 }
61
62 unsigned short HexagonMCInstrInfo::getExtendableOp(MCInstrInfo const &MCII,
63                                                    MCInst const &MCI) {
64   const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
65   return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask);
66 }
67
68 MCOperand const &
69 HexagonMCInstrInfo::getExtendableOperand(MCInstrInfo const &MCII,
70                                          MCInst const &MCI) {
71   unsigned O = HexagonMCInstrInfo::getExtendableOp(MCII, MCI);
72   MCOperand const &MO = MCI.getOperand(O);
73
74   assert((HexagonMCInstrInfo::isExtendable(MCII, MCI) ||
75           HexagonMCInstrInfo::isExtended(MCII, MCI)) &&
76          (MO.isImm() || MO.isExpr()));
77   return (MO);
78 }
79
80 unsigned HexagonMCInstrInfo::getExtentAlignment(MCInstrInfo const &MCII,
81                                                 MCInst const &MCI) {
82   const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
83   return ((F >> HexagonII::ExtentAlignPos) & HexagonII::ExtentAlignMask);
84 }
85
86 unsigned HexagonMCInstrInfo::getExtentBits(MCInstrInfo const &MCII,
87                                            MCInst const &MCI) {
88   const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
89   return ((F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask);
90 }
91
92 // Return the max value that a constant extendable operand can have
93 // without being extended.
94 int HexagonMCInstrInfo::getMaxValue(MCInstrInfo const &MCII,
95                                     MCInst const &MCI) {
96   uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
97   unsigned isSigned =
98       (F >> HexagonII::ExtentSignedPos) & HexagonII::ExtentSignedMask;
99   unsigned bits = (F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask;
100
101   if (isSigned) // if value is signed
102     return ~(-1U << (bits - 1));
103   else
104     return ~(-1U << bits);
105 }
106
107 // Return the min value that a constant extendable operand can have
108 // without being extended.
109 int HexagonMCInstrInfo::getMinValue(MCInstrInfo const &MCII,
110                                     MCInst const &MCI) {
111   uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
112   unsigned isSigned =
113       (F >> HexagonII::ExtentSignedPos) & HexagonII::ExtentSignedMask;
114   unsigned bits = (F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask;
115
116   if (isSigned) // if value is signed
117     return -1U << (bits - 1);
118   else
119     return 0;
120 }
121
122 char const *HexagonMCInstrInfo::getName(MCInstrInfo const &MCII,
123                                         MCInst const &MCI) {
124   return MCII.getName(MCI.getOpcode());
125 }
126
127 unsigned short HexagonMCInstrInfo::getNewValueOp(MCInstrInfo const &MCII,
128                                                  MCInst const &MCI) {
129   const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
130   return ((F >> HexagonII::NewValueOpPos) & HexagonII::NewValueOpMask);
131 }
132
133 MCOperand const &HexagonMCInstrInfo::getNewValueOperand(MCInstrInfo const &MCII,
134                                                         MCInst const &MCI) {
135   uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
136   unsigned const O =
137       (F >> HexagonII::NewValueOpPos) & HexagonII::NewValueOpMask;
138   MCOperand const &MCO = MCI.getOperand(O);
139
140   assert((HexagonMCInstrInfo::isNewValue(MCII, MCI) ||
141           HexagonMCInstrInfo::hasNewValue(MCII, MCI)) &&
142          MCO.isReg());
143   return (MCO);
144 }
145
146 int HexagonMCInstrInfo::getSubTarget(MCInstrInfo const &MCII,
147                                      MCInst const &MCI) {
148   const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
149
150   HexagonII::SubTarget Target = static_cast<HexagonII::SubTarget>(
151       (F >> HexagonII::validSubTargetPos) & HexagonII::validSubTargetMask);
152
153   switch (Target) {
154   default:
155     return Hexagon::ArchV4;
156   case HexagonII::HasV5SubT:
157     return Hexagon::ArchV5;
158   }
159 }
160
161 // Return the Hexagon ISA class for the insn.
162 unsigned HexagonMCInstrInfo::getType(MCInstrInfo const &MCII,
163                                      MCInst const &MCI) {
164   const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
165
166   return ((F >> HexagonII::TypePos) & HexagonII::TypeMask);
167 }
168
169 unsigned HexagonMCInstrInfo::getUnits(MCInstrInfo const &MCII,
170                                       MCSubtargetInfo const &STI,
171                                       MCInst const &MCI) {
172
173   const InstrItinerary *II = STI.getSchedModel().InstrItineraries;
174   int SchedClass = HexagonMCInstrInfo::getDesc(MCII, MCI).getSchedClass();
175   return ((II[SchedClass].FirstStage + HexagonStages)->getUnits());
176 }
177
178 bool HexagonMCInstrInfo::hasImmExt(MCInst const &MCI) {
179   if (!HexagonMCInstrInfo::isBundle(MCI))
180     return false;
181
182   for (const auto &I : HexagonMCInstrInfo::bundleInstructions(MCI)) {
183     auto MI = I.getInst();
184     if (isImmext(*MI))
185       return true;
186   }
187
188   return false;
189 }
190
191 // Return whether the instruction is a legal new-value producer.
192 bool HexagonMCInstrInfo::hasNewValue(MCInstrInfo const &MCII,
193                                      MCInst const &MCI) {
194   const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
195   return ((F >> HexagonII::hasNewValuePos) & HexagonII::hasNewValueMask);
196 }
197
198 MCInst const &HexagonMCInstrInfo::instruction(MCInst const &MCB, size_t Index) {
199   assert(isBundle(MCB));
200   assert(Index < HEXAGON_PACKET_SIZE);
201   return *MCB.getOperand(bundleInstructionsOffset + Index).getInst();
202 }
203
204 bool HexagonMCInstrInfo::isBundle(MCInst const &MCI) {
205   auto Result = Hexagon::BUNDLE == MCI.getOpcode();
206   assert(!Result || (MCI.size() > 0 && MCI.getOperand(0).isImm()));
207   return Result;
208 }
209
210 // Return whether the insn is an actual insn.
211 bool HexagonMCInstrInfo::isCanon(MCInstrInfo const &MCII, MCInst const &MCI) {
212   return (!HexagonMCInstrInfo::getDesc(MCII, MCI).isPseudo() &&
213           !HexagonMCInstrInfo::isPrefix(MCII, MCI) &&
214           HexagonMCInstrInfo::getType(MCII, MCI) != HexagonII::TypeENDLOOP);
215 }
216
217 // Return whether the instruction needs to be constant extended.
218 // 1) Always return true if the instruction has 'isExtended' flag set.
219 //
220 // isExtendable:
221 // 2) For immediate extended operands, return true only if the value is
222 //    out-of-range.
223 // 3) For global address, always return true.
224
225 bool HexagonMCInstrInfo::isConstExtended(MCInstrInfo const &MCII,
226                                          MCInst const &MCI) {
227   if (HexagonMCInstrInfo::isExtended(MCII, MCI))
228     return true;
229
230   if (!HexagonMCInstrInfo::isExtendable(MCII, MCI))
231     return false;
232
233   short ExtOpNum = HexagonMCInstrInfo::getCExtOpNum(MCII, MCI);
234   int MinValue = HexagonMCInstrInfo::getMinValue(MCII, MCI);
235   int MaxValue = HexagonMCInstrInfo::getMaxValue(MCII, MCI);
236   MCOperand const &MO = MCI.getOperand(ExtOpNum);
237
238   // We could be using an instruction with an extendable immediate and shoehorn
239   // a global address into it. If it is a global address it will be constant
240   // extended. We do this for COMBINE.
241   // We currently only handle isGlobal() because it is the only kind of
242   // object we are going to end up with here for now.
243   // In the future we probably should add isSymbol(), etc.
244   if (MO.isExpr())
245     return true;
246
247   // If the extendable operand is not 'Immediate' type, the instruction should
248   // have 'isExtended' flag set.
249   assert(MO.isImm() && "Extendable operand must be Immediate type");
250
251   int ImmValue = MO.getImm();
252   return (ImmValue < MinValue || ImmValue > MaxValue);
253 }
254
255 bool HexagonMCInstrInfo::isExtendable(MCInstrInfo const &MCII,
256                                       MCInst const &MCI) {
257   uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
258   return (F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask;
259 }
260
261 bool HexagonMCInstrInfo::isExtended(MCInstrInfo const &MCII,
262                                     MCInst const &MCI) {
263   uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
264   return (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
265 }
266
267 bool HexagonMCInstrInfo::isFloat(MCInstrInfo const &MCII, MCInst const &MCI) {
268   const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
269   return ((F >> HexagonII::FPPos) & HexagonII::FPMask);
270 }
271
272 bool HexagonMCInstrInfo::isImmext(MCInst const &MCI) {
273   auto Op = MCI.getOpcode();
274   return (Op == Hexagon::A4_ext_b || Op == Hexagon::A4_ext_c ||
275           Op == Hexagon::A4_ext_g || Op == Hexagon::A4_ext);
276 }
277
278 bool HexagonMCInstrInfo::isInnerLoop(MCInst const &MCI) {
279   assert(isBundle(MCI));
280   int64_t Flags = MCI.getOperand(0).getImm();
281   return (Flags & innerLoopMask) != 0;
282 }
283
284 // Return whether the insn is a new-value consumer.
285 bool HexagonMCInstrInfo::isNewValue(MCInstrInfo const &MCII,
286                                     MCInst const &MCI) {
287   const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
288   return ((F >> HexagonII::NewValuePos) & HexagonII::NewValueMask);
289 }
290
291 // Return whether the operand can be constant extended.
292 bool HexagonMCInstrInfo::isOperandExtended(MCInstrInfo const &MCII,
293                                            MCInst const &MCI,
294                                            unsigned short OperandNum) {
295   uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
296   return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask) ==
297          OperandNum;
298 }
299
300 bool HexagonMCInstrInfo::isOuterLoop(MCInst const &MCI) {
301   assert(isBundle(MCI));
302   int64_t Flags = MCI.getOperand(0).getImm();
303   return (Flags & outerLoopMask) != 0;
304 }
305
306 bool HexagonMCInstrInfo::isPredicated(MCInstrInfo const &MCII,
307                                       MCInst const &MCI) {
308   const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
309   return ((F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
310 }
311
312 bool HexagonMCInstrInfo::isPredicatedTrue(MCInstrInfo const &MCII,
313                                           MCInst const &MCI) {
314   const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
315   return (
316       !((F >> HexagonII::PredicatedFalsePos) & HexagonII::PredicatedFalseMask));
317 }
318
319 bool HexagonMCInstrInfo::isPrefix(MCInstrInfo const &MCII, MCInst const &MCI) {
320   return (HexagonMCInstrInfo::getType(MCII, MCI) == HexagonII::TypePREFIX);
321 }
322
323 bool HexagonMCInstrInfo::isSolo(MCInstrInfo const &MCII, MCInst const &MCI) {
324   const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
325   return ((F >> HexagonII::SoloPos) & HexagonII::SoloMask);
326 }
327
328 bool HexagonMCInstrInfo::isSoloAX(MCInstrInfo const &MCII, MCInst const &MCI) {
329   const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
330   return ((F >> HexagonII::SoloAXPos) & HexagonII::SoloAXMask);
331 }
332
333 bool HexagonMCInstrInfo::isSoloAin1(MCInstrInfo const &MCII,
334                                     MCInst const &MCI) {
335   const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
336   return ((F >> HexagonII::SoloAin1Pos) & HexagonII::SoloAin1Mask);
337 }
338
339 void HexagonMCInstrInfo::padEndloop(MCInst &MCB) {
340   MCInst Nop;
341   Nop.setOpcode(Hexagon::A2_nop);
342   assert(isBundle(MCB));
343   while ((HexagonMCInstrInfo::isInnerLoop(MCB) &&
344           (HexagonMCInstrInfo::bundleSize(MCB) < HEXAGON_PACKET_INNER_SIZE)) ||
345          ((HexagonMCInstrInfo::isOuterLoop(MCB) &&
346            (HexagonMCInstrInfo::bundleSize(MCB) < HEXAGON_PACKET_OUTER_SIZE))))
347     MCB.addOperand(MCOperand::createInst(new MCInst(Nop)));
348 }
349
350 bool HexagonMCInstrInfo::prefersSlot3(MCInstrInfo const &MCII,
351                                       MCInst const &MCI) {
352   if (HexagonMCInstrInfo::getType(MCII, MCI) == HexagonII::TypeCR)
353     return false;
354
355   unsigned SchedClass = HexagonMCInstrInfo::getDesc(MCII, MCI).getSchedClass();
356   switch (SchedClass) {
357   case Hexagon::Sched::ALU32_3op_tc_2_SLOT0123:
358   case Hexagon::Sched::ALU64_tc_2_SLOT23:
359   case Hexagon::Sched::ALU64_tc_3x_SLOT23:
360   case Hexagon::Sched::M_tc_2_SLOT23:
361   case Hexagon::Sched::M_tc_3x_SLOT23:
362   case Hexagon::Sched::S_2op_tc_2_SLOT23:
363   case Hexagon::Sched::S_3op_tc_2_SLOT23:
364   case Hexagon::Sched::S_3op_tc_3x_SLOT23:
365     return true;
366   }
367   return false;
368 }
369
370 void HexagonMCInstrInfo::setInnerLoop(MCInst &MCI) {
371   assert(isBundle(MCI));
372   MCOperand &Operand = MCI.getOperand(0);
373   Operand.setImm(Operand.getImm() | innerLoopMask);
374 }
375
376 void HexagonMCInstrInfo::setOuterLoop(MCInst &MCI) {
377   assert(isBundle(MCI));
378   MCOperand &Operand = MCI.getOperand(0);
379   Operand.setImm(Operand.getImm() | outerLoopMask);
380 }
381 }