[Hexagon] Factoring bundle creation in to a utility function.
[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 #include "HexagonMCChecker.h"
19
20 #include "llvm/MC/MCContext.h"
21 #include "llvm/MC/MCExpr.h"
22 #include "llvm/MC/MCInstrInfo.h"
23 #include "llvm/MC/MCSubtargetInfo.h"
24
25 namespace llvm {
26 void HexagonMCInstrInfo::addConstant(MCInst &MI, uint64_t Value,
27                                      MCContext &Context) {
28   MI.addOperand(MCOperand::createExpr(MCConstantExpr::create(Value, Context)));
29 }
30
31 void HexagonMCInstrInfo::addConstExtender(MCContext &Context,
32                                           MCInstrInfo const &MCII, MCInst &MCB,
33                                           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 =
40       new (Context) MCInst(HexagonMCInstrInfo::deriveExtender(MCII, MCI, exOp));
41
42   MCB.addOperand(MCOperand::createInst(XMCI));
43 }
44
45 iterator_range<MCInst::const_iterator>
46 HexagonMCInstrInfo::bundleInstructions(MCInst const &MCI) {
47   assert(isBundle(MCI));
48   return iterator_range<MCInst::const_iterator>(
49       MCI.begin() + bundleInstructionsOffset, MCI.end());
50 }
51
52 size_t HexagonMCInstrInfo::bundleSize(MCInst const &MCI) {
53   if (HexagonMCInstrInfo::isBundle(MCI))
54     return (MCI.size() - bundleInstructionsOffset);
55   else
56     return (1);
57 }
58
59 bool HexagonMCInstrInfo::canonicalizePacket(MCInstrInfo const &MCII,
60                                             MCSubtargetInfo const &STI,
61                                             MCContext &Context, MCInst &MCB,
62                                             HexagonMCChecker *Check) {
63   // Examine the packet and convert pairs of instructions to compound
64   // instructions when possible.
65   if (!HexagonDisableCompound)
66     HexagonMCInstrInfo::tryCompound(MCII, Context, MCB);
67   // Check the bundle for errors.
68   bool CheckOk = Check ? Check->check() : true;
69   if (!CheckOk)
70     return false;
71   HexagonMCShuffle(MCII, STI, MCB);
72   // Examine the packet and convert pairs of instructions to duplex
73   // instructions when possible.
74   MCInst InstBundlePreDuplex = MCInst(MCB);
75   if (!HexagonDisableDuplex) {
76     SmallVector<DuplexCandidate, 8> possibleDuplexes;
77     possibleDuplexes = HexagonMCInstrInfo::getDuplexPossibilties(MCII, MCB);
78     HexagonMCShuffle(MCII, STI, Context, MCB, possibleDuplexes);
79   }
80   // Examines packet and pad the packet, if needed, when an
81   // end-loop is in the bundle.
82   HexagonMCInstrInfo::padEndloop(Context, MCB);
83   // If compounding and duplexing didn't reduce the size below
84   // 4 or less we have a packet that is too big.
85   if (HexagonMCInstrInfo::bundleSize(MCB) > HEXAGON_PACKET_SIZE)
86     return false;
87   HexagonMCShuffle(MCII, STI, MCB);
88   return true;
89 }
90
91 void HexagonMCInstrInfo::clampExtended(MCInstrInfo const &MCII,
92                                        MCContext &Context, MCInst &MCI) {
93   assert(HexagonMCInstrInfo::isExtendable(MCII, MCI) ||
94          HexagonMCInstrInfo::isExtended(MCII, MCI));
95   MCOperand &exOp =
96       MCI.getOperand(HexagonMCInstrInfo::getExtendableOp(MCII, MCI));
97   // If the extended value is a constant, then use it for the extended and
98   // for the extender instructions, masking off the lower 6 bits and
99   // including the assumed bits.
100   int64_t Value;
101   if (exOp.getExpr()->evaluateAsAbsolute(Value)) {
102     unsigned Shift = HexagonMCInstrInfo::getExtentAlignment(MCII, MCI);
103     exOp.setExpr(MCConstantExpr::create((Value & 0x3f) << Shift, Context));
104   }
105 }
106
107 MCInst HexagonMCInstrInfo::createBundle() {
108   MCInst Result;
109   Result.setOpcode(Hexagon::BUNDLE);
110   Result.addOperand(MCOperand::createImm(0));
111   return Result;
112 }
113
114 MCInst *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
128 MCInst 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
149 MCInst 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
161 void HexagonMCInstrInfo::extendIfNeeded(MCContext &Context,
162                                         MCInstrInfo const &MCII, MCInst &MCB,
163                                         MCInst const &MCI, bool MustExtend) {
164   if (isConstExtended(MCII, MCI) || MustExtend)
165     addConstExtender(Context, MCII, MCB, MCI);
166 }
167
168 HexagonII::MemAccessSize
169 HexagonMCInstrInfo::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
176 unsigned 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
182 // Return constant extended operand number.
183 unsigned 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
189 MCInstrDesc const &HexagonMCInstrInfo::getDesc(MCInstrInfo const &MCII,
190                                                MCInst const &MCI) {
191   return (MCII.get(MCI.getOpcode()));
192 }
193
194 unsigned short HexagonMCInstrInfo::getExtendableOp(MCInstrInfo const &MCII,
195                                                    MCInst const &MCI) {
196   const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
197   return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask);
198 }
199
200 MCOperand const &
201 HexagonMCInstrInfo::getExtendableOperand(MCInstrInfo const &MCII,
202                                          MCInst const &MCI) {
203   unsigned O = HexagonMCInstrInfo::getExtendableOp(MCII, MCI);
204   MCOperand const &MO = MCI.getOperand(O);
205
206   assert((HexagonMCInstrInfo::isExtendable(MCII, MCI) ||
207           HexagonMCInstrInfo::isExtended(MCII, MCI)) &&
208          (MO.isImm() || MO.isExpr()));
209   return (MO);
210 }
211
212 unsigned HexagonMCInstrInfo::getExtentAlignment(MCInstrInfo const &MCII,
213                                                 MCInst const &MCI) {
214   const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
215   return ((F >> HexagonII::ExtentAlignPos) & HexagonII::ExtentAlignMask);
216 }
217
218 unsigned HexagonMCInstrInfo::getExtentBits(MCInstrInfo const &MCII,
219                                            MCInst const &MCI) {
220   const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
221   return ((F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask);
222 }
223
224 // Return the max value that a constant extendable operand can have
225 // without being extended.
226 int HexagonMCInstrInfo::getMaxValue(MCInstrInfo const &MCII,
227                                     MCInst const &MCI) {
228   uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
229   unsigned isSigned =
230       (F >> HexagonII::ExtentSignedPos) & HexagonII::ExtentSignedMask;
231   unsigned bits = (F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask;
232
233   if (isSigned) // if value is signed
234     return ~(-1U << (bits - 1));
235   else
236     return ~(-1U << bits);
237 }
238
239 // Return the min value that a constant extendable operand can have
240 // without being extended.
241 int HexagonMCInstrInfo::getMinValue(MCInstrInfo const &MCII,
242                                     MCInst const &MCI) {
243   uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
244   unsigned isSigned =
245       (F >> HexagonII::ExtentSignedPos) & HexagonII::ExtentSignedMask;
246   unsigned bits = (F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask;
247
248   if (isSigned) // if value is signed
249     return -1U << (bits - 1);
250   else
251     return 0;
252 }
253
254 char const *HexagonMCInstrInfo::getName(MCInstrInfo const &MCII,
255                                         MCInst const &MCI) {
256   return MCII.getName(MCI.getOpcode());
257 }
258
259 unsigned short HexagonMCInstrInfo::getNewValueOp(MCInstrInfo const &MCII,
260                                                  MCInst const &MCI) {
261   const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
262   return ((F >> HexagonII::NewValueOpPos) & HexagonII::NewValueOpMask);
263 }
264
265 MCOperand const &HexagonMCInstrInfo::getNewValueOperand(MCInstrInfo const &MCII,
266                                                         MCInst const &MCI) {
267   uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
268   unsigned const O =
269       (F >> HexagonII::NewValueOpPos) & HexagonII::NewValueOpMask;
270   MCOperand const &MCO = MCI.getOperand(O);
271
272   assert((HexagonMCInstrInfo::isNewValue(MCII, MCI) ||
273           HexagonMCInstrInfo::hasNewValue(MCII, MCI)) &&
274          MCO.isReg());
275   return (MCO);
276 }
277
278 /// Return the new value or the newly produced value.
279 unsigned short HexagonMCInstrInfo::getNewValueOp2(MCInstrInfo const &MCII,
280                                                   MCInst const &MCI) {
281   const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
282   return ((F >> HexagonII::NewValueOpPos2) & HexagonII::NewValueOpMask2);
283 }
284
285 MCOperand const &
286 HexagonMCInstrInfo::getNewValueOperand2(MCInstrInfo const &MCII,
287                                         MCInst const &MCI) {
288   unsigned O = HexagonMCInstrInfo::getNewValueOp2(MCII, MCI);
289   MCOperand const &MCO = MCI.getOperand(O);
290
291   assert((HexagonMCInstrInfo::isNewValue(MCII, MCI) ||
292           HexagonMCInstrInfo::hasNewValue2(MCII, MCI)) &&
293          MCO.isReg());
294   return (MCO);
295 }
296
297 int HexagonMCInstrInfo::getSubTarget(MCInstrInfo const &MCII,
298                                      MCInst const &MCI) {
299   const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
300
301   HexagonII::SubTarget Target = static_cast<HexagonII::SubTarget>(
302       (F >> HexagonII::validSubTargetPos) & HexagonII::validSubTargetMask);
303
304   switch (Target) {
305   default:
306     return Hexagon::ArchV4;
307   case HexagonII::HasV5SubT:
308     return Hexagon::ArchV5;
309   }
310 }
311
312 // Return the Hexagon ISA class for the insn.
313 unsigned HexagonMCInstrInfo::getType(MCInstrInfo const &MCII,
314                                      MCInst const &MCI) {
315   const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
316
317   return ((F >> HexagonII::TypePos) & HexagonII::TypeMask);
318 }
319
320 unsigned HexagonMCInstrInfo::getUnits(MCInstrInfo const &MCII,
321                                       MCSubtargetInfo const &STI,
322                                       MCInst const &MCI) {
323
324   const InstrItinerary *II = STI.getSchedModel().InstrItineraries;
325   int SchedClass = HexagonMCInstrInfo::getDesc(MCII, MCI).getSchedClass();
326   return ((II[SchedClass].FirstStage + HexagonStages)->getUnits());
327 }
328
329 bool HexagonMCInstrInfo::hasImmExt(MCInst const &MCI) {
330   if (!HexagonMCInstrInfo::isBundle(MCI))
331     return false;
332
333   for (const auto &I : HexagonMCInstrInfo::bundleInstructions(MCI)) {
334     auto MI = I.getInst();
335     if (isImmext(*MI))
336       return true;
337   }
338
339   return false;
340 }
341
342 bool HexagonMCInstrInfo::hasExtenderForIndex(MCInst const &MCB, size_t Index) {
343   return extenderForIndex(MCB, Index) != nullptr;
344 }
345
346 // Return whether the instruction is a legal new-value producer.
347 bool HexagonMCInstrInfo::hasNewValue(MCInstrInfo const &MCII,
348                                      MCInst const &MCI) {
349   const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
350   return ((F >> HexagonII::hasNewValuePos) & HexagonII::hasNewValueMask);
351 }
352
353 /// Return whether the insn produces a second value.
354 bool HexagonMCInstrInfo::hasNewValue2(MCInstrInfo const &MCII,
355                                       MCInst const &MCI) {
356   const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
357   return ((F >> HexagonII::hasNewValuePos2) & HexagonII::hasNewValueMask2);
358 }
359
360 MCInst const &HexagonMCInstrInfo::instruction(MCInst const &MCB, size_t Index) {
361   assert(isBundle(MCB));
362   assert(Index < HEXAGON_PACKET_SIZE);
363   return *MCB.getOperand(bundleInstructionsOffset + Index).getInst();
364 }
365
366 bool HexagonMCInstrInfo::isBundle(MCInst const &MCI) {
367   auto Result = Hexagon::BUNDLE == MCI.getOpcode();
368   assert(!Result || (MCI.size() > 0 && MCI.getOperand(0).isImm()));
369   return Result;
370 }
371
372 // Return whether the insn is an actual insn.
373 bool HexagonMCInstrInfo::isCanon(MCInstrInfo const &MCII, MCInst const &MCI) {
374   return (!HexagonMCInstrInfo::getDesc(MCII, MCI).isPseudo() &&
375           !HexagonMCInstrInfo::isPrefix(MCII, MCI) &&
376           HexagonMCInstrInfo::getType(MCII, MCI) != HexagonII::TypeENDLOOP);
377 }
378
379 bool HexagonMCInstrInfo::isCompound(MCInstrInfo const &MCII,
380                                     MCInst const &MCI) {
381   return (getType(MCII, MCI) == HexagonII::TypeCOMPOUND);
382 }
383
384 bool HexagonMCInstrInfo::isDblRegForSubInst(unsigned Reg) {
385   return ((Reg >= Hexagon::D0 && Reg <= Hexagon::D3) ||
386           (Reg >= Hexagon::D8 && Reg <= Hexagon::D11));
387 }
388
389 bool HexagonMCInstrInfo::isDuplex(MCInstrInfo const &MCII, MCInst const &MCI) {
390   return HexagonII::TypeDUPLEX == HexagonMCInstrInfo::getType(MCII, MCI);
391 }
392
393 // Return whether the instruction needs to be constant extended.
394 // 1) Always return true if the instruction has 'isExtended' flag set.
395 //
396 // isExtendable:
397 // 2) For immediate extended operands, return true only if the value is
398 //    out-of-range.
399 // 3) For global address, always return true.
400
401 bool HexagonMCInstrInfo::isConstExtended(MCInstrInfo const &MCII,
402                                          MCInst const &MCI) {
403   if (HexagonMCInstrInfo::isExtended(MCII, MCI))
404     return true;
405   // Branch insns are handled as necessary by relaxation.
406   if ((HexagonMCInstrInfo::getType(MCII, MCI) == HexagonII::TypeJ) ||
407       (HexagonMCInstrInfo::getType(MCII, MCI) == HexagonII::TypeCOMPOUND &&
408        HexagonMCInstrInfo::getDesc(MCII, MCI).isBranch()) ||
409       (HexagonMCInstrInfo::getType(MCII, MCI) == HexagonII::TypeNV &&
410        HexagonMCInstrInfo::getDesc(MCII, MCI).isBranch()))
411     return false;
412   // Otherwise loop instructions and other CR insts are handled by relaxation
413   else if ((HexagonMCInstrInfo::getType(MCII, MCI) == HexagonII::TypeCR) &&
414            (MCI.getOpcode() != Hexagon::C4_addipc))
415     return false;
416   else if (!HexagonMCInstrInfo::isExtendable(MCII, MCI))
417     return false;
418
419   MCOperand const &MO = HexagonMCInstrInfo::getExtendableOperand(MCII, MCI);
420
421   // We could be using an instruction with an extendable immediate and shoehorn
422   // a global address into it. If it is a global address it will be constant
423   // extended. We do this for COMBINE.
424   // We currently only handle isGlobal() because it is the only kind of
425   // object we are going to end up with here for now.
426   // In the future we probably should add isSymbol(), etc.
427   assert(!MO.isImm());
428   int64_t Value;
429   if (!MO.getExpr()->evaluateAsAbsolute(Value))
430     return true;
431   int MinValue = HexagonMCInstrInfo::getMinValue(MCII, MCI);
432   int MaxValue = HexagonMCInstrInfo::getMaxValue(MCII, MCI);
433   return (MinValue > Value || Value > MaxValue);
434 }
435
436 bool HexagonMCInstrInfo::isExtendable(MCInstrInfo const &MCII,
437                                       MCInst const &MCI) {
438   uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
439   return (F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask;
440 }
441
442 bool HexagonMCInstrInfo::isExtended(MCInstrInfo const &MCII,
443                                     MCInst const &MCI) {
444   uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
445   return (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
446 }
447
448 bool HexagonMCInstrInfo::isFloat(MCInstrInfo const &MCII, MCInst const &MCI) {
449   const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
450   return ((F >> HexagonII::FPPos) & HexagonII::FPMask);
451 }
452
453 bool HexagonMCInstrInfo::isImmext(MCInst const &MCI) {
454   auto Op = MCI.getOpcode();
455   return (Op == Hexagon::A4_ext_b || Op == Hexagon::A4_ext_c ||
456           Op == Hexagon::A4_ext_g || Op == Hexagon::A4_ext);
457 }
458
459 bool HexagonMCInstrInfo::isInnerLoop(MCInst const &MCI) {
460   assert(isBundle(MCI));
461   int64_t Flags = MCI.getOperand(0).getImm();
462   return (Flags & innerLoopMask) != 0;
463 }
464
465 bool HexagonMCInstrInfo::isIntReg(unsigned Reg) {
466   return (Reg >= Hexagon::R0 && Reg <= Hexagon::R31);
467 }
468
469 bool HexagonMCInstrInfo::isIntRegForSubInst(unsigned Reg) {
470   return ((Reg >= Hexagon::R0 && Reg <= Hexagon::R7) ||
471           (Reg >= Hexagon::R16 && Reg <= Hexagon::R23));
472 }
473
474 // Return whether the insn is a new-value consumer.
475 bool HexagonMCInstrInfo::isNewValue(MCInstrInfo const &MCII,
476                                     MCInst const &MCI) {
477   const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
478   return ((F >> HexagonII::NewValuePos) & HexagonII::NewValueMask);
479 }
480
481 // Return whether the operand can be constant extended.
482 bool HexagonMCInstrInfo::isOperandExtended(MCInstrInfo const &MCII,
483                                            MCInst const &MCI,
484                                            unsigned short OperandNum) {
485   uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
486   return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask) ==
487          OperandNum;
488 }
489
490 bool HexagonMCInstrInfo::isOuterLoop(MCInst const &MCI) {
491   assert(isBundle(MCI));
492   int64_t Flags = MCI.getOperand(0).getImm();
493   return (Flags & outerLoopMask) != 0;
494 }
495
496 bool HexagonMCInstrInfo::isPredicated(MCInstrInfo const &MCII,
497                                       MCInst const &MCI) {
498   const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
499   return ((F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
500 }
501
502 bool HexagonMCInstrInfo::isPredicateLate(MCInstrInfo const &MCII,
503                                          MCInst const &MCI) {
504   const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
505   return (F >> HexagonII::PredicateLatePos & HexagonII::PredicateLateMask);
506 }
507
508 /// Return whether the insn is newly predicated.
509 bool HexagonMCInstrInfo::isPredicatedNew(MCInstrInfo const &MCII,
510                                          MCInst const &MCI) {
511   const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
512   return ((F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask);
513 }
514
515 bool HexagonMCInstrInfo::isPredicatedTrue(MCInstrInfo const &MCII,
516                                           MCInst const &MCI) {
517   const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
518   return (
519       !((F >> HexagonII::PredicatedFalsePos) & HexagonII::PredicatedFalseMask));
520 }
521
522 bool HexagonMCInstrInfo::isPredReg(unsigned Reg) {
523   return (Reg >= Hexagon::P0 && Reg <= Hexagon::P3_0);
524 }
525
526 bool HexagonMCInstrInfo::isPrefix(MCInstrInfo const &MCII, MCInst const &MCI) {
527   return (HexagonMCInstrInfo::getType(MCII, MCI) == HexagonII::TypePREFIX);
528 }
529
530 bool HexagonMCInstrInfo::isSolo(MCInstrInfo const &MCII, MCInst const &MCI) {
531   const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
532   return ((F >> HexagonII::SoloPos) & HexagonII::SoloMask);
533 }
534
535 bool HexagonMCInstrInfo::isMemReorderDisabled(MCInst const &MCI) {
536   assert(isBundle(MCI));
537   auto Flags = MCI.getOperand(0).getImm();
538   return (Flags & memReorderDisabledMask) != 0;
539 }
540
541 bool HexagonMCInstrInfo::isMemStoreReorderEnabled(MCInst const &MCI) {
542   assert(isBundle(MCI));
543   auto Flags = MCI.getOperand(0).getImm();
544   return (Flags & memStoreReorderEnabledMask) != 0;
545 }
546
547 bool HexagonMCInstrInfo::isSoloAX(MCInstrInfo const &MCII, MCInst const &MCI) {
548   const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
549   return ((F >> HexagonII::SoloAXPos) & HexagonII::SoloAXMask);
550 }
551
552 bool HexagonMCInstrInfo::isSoloAin1(MCInstrInfo const &MCII,
553                                     MCInst const &MCI) {
554   const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
555   return ((F >> HexagonII::SoloAin1Pos) & HexagonII::SoloAin1Mask);
556 }
557
558 bool HexagonMCInstrInfo::isVector(MCInstrInfo const &MCII, MCInst const &MCI) {
559   if ((getType(MCII, MCI) <= HexagonII::TypeCVI_LAST) &&
560       (getType(MCII, MCI) >= HexagonII::TypeCVI_FIRST))
561     return true;
562   return false;
563 }
564
565 int64_t HexagonMCInstrInfo::minConstant(MCInst const &MCI, size_t Index) {
566   auto Sentinal = static_cast<int64_t>(std::numeric_limits<uint32_t>::max())
567                   << 8;
568   if (MCI.size() <= Index)
569     return Sentinal;
570   MCOperand const &MCO = MCI.getOperand(Index);
571   if (!MCO.isExpr())
572     return Sentinal;
573   int64_t Value;
574   if (!MCO.getExpr()->evaluateAsAbsolute(Value))
575     return Sentinal;
576   return Value;
577 }
578
579 void HexagonMCInstrInfo::padEndloop(MCContext &Context, MCInst &MCB) {
580   MCInst Nop;
581   Nop.setOpcode(Hexagon::A2_nop);
582   assert(isBundle(MCB));
583   while ((HexagonMCInstrInfo::isInnerLoop(MCB) &&
584           (HexagonMCInstrInfo::bundleSize(MCB) < HEXAGON_PACKET_INNER_SIZE)) ||
585          ((HexagonMCInstrInfo::isOuterLoop(MCB) &&
586            (HexagonMCInstrInfo::bundleSize(MCB) < HEXAGON_PACKET_OUTER_SIZE))))
587     MCB.addOperand(MCOperand::createInst(new (Context) MCInst(Nop)));
588 }
589
590 bool HexagonMCInstrInfo::prefersSlot3(MCInstrInfo const &MCII,
591                                       MCInst const &MCI) {
592   if (HexagonMCInstrInfo::getType(MCII, MCI) == HexagonII::TypeCR)
593     return false;
594
595   unsigned SchedClass = HexagonMCInstrInfo::getDesc(MCII, MCI).getSchedClass();
596   switch (SchedClass) {
597   case Hexagon::Sched::ALU32_3op_tc_2_SLOT0123:
598   case Hexagon::Sched::ALU64_tc_2_SLOT23:
599   case Hexagon::Sched::ALU64_tc_3x_SLOT23:
600   case Hexagon::Sched::M_tc_2_SLOT23:
601   case Hexagon::Sched::M_tc_3x_SLOT23:
602   case Hexagon::Sched::S_2op_tc_2_SLOT23:
603   case Hexagon::Sched::S_3op_tc_2_SLOT23:
604   case Hexagon::Sched::S_3op_tc_3x_SLOT23:
605     return true;
606   }
607   return false;
608 }
609
610 void HexagonMCInstrInfo::replaceDuplex(MCContext &Context, MCInst &MCB,
611                                        DuplexCandidate Candidate) {
612   assert(Candidate.packetIndexI < MCB.size());
613   assert(Candidate.packetIndexJ < MCB.size());
614   assert(isBundle(MCB));
615   MCInst *Duplex =
616       deriveDuplex(Context, Candidate.iClass,
617                    *MCB.getOperand(Candidate.packetIndexJ).getInst(),
618                    *MCB.getOperand(Candidate.packetIndexI).getInst());
619   assert(Duplex != nullptr);
620   MCB.getOperand(Candidate.packetIndexI).setInst(Duplex);
621   MCB.erase(MCB.begin() + Candidate.packetIndexJ);
622 }
623
624 void HexagonMCInstrInfo::setInnerLoop(MCInst &MCI) {
625   assert(isBundle(MCI));
626   MCOperand &Operand = MCI.getOperand(0);
627   Operand.setImm(Operand.getImm() | innerLoopMask);
628 }
629
630 void HexagonMCInstrInfo::setMemReorderDisabled(MCInst &MCI) {
631   assert(isBundle(MCI));
632   MCOperand &Operand = MCI.getOperand(0);
633   Operand.setImm(Operand.getImm() | memReorderDisabledMask);
634   assert(isMemReorderDisabled(MCI));
635 }
636
637 void HexagonMCInstrInfo::setMemStoreReorderEnabled(MCInst &MCI) {
638   assert(isBundle(MCI));
639   MCOperand &Operand = MCI.getOperand(0);
640   Operand.setImm(Operand.getImm() | memStoreReorderEnabledMask);
641   assert(isMemStoreReorderEnabled(MCI));
642 }
643
644 void HexagonMCInstrInfo::setOuterLoop(MCInst &MCI) {
645   assert(isBundle(MCI));
646   MCOperand &Operand = MCI.getOperand(0);
647   Operand.setImm(Operand.getImm() | outerLoopMask);
648 }
649 }