1//===- HexagonMCInst.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 MCInst to allow some Hexagon VLIW annotations.
11//
12//===----------------------------------------------------------------------===//
13
14#include "HexagonInstrInfo.h"
15#include "MCTargetDesc/HexagonBaseInfo.h"
16#include "MCTargetDesc/HexagonMCInst.h"
17#include "MCTargetDesc/HexagonMCTargetDesc.h"
18
19using namespace llvm;
20
21// Return the slots used by the insn.
22unsigned HexagonMCInst::getUnits(const HexagonTargetMachine* TM) const {
23  const HexagonInstrInfo* QII = TM->getInstrInfo();
24  const InstrItineraryData* II = TM->getInstrItineraryData();
25  const InstrStage*
26    IS = II->beginStage(QII->get(this->getOpcode()).getSchedClass());
27
28  return (IS->getUnits());
29}
30
31// Return the Hexagon ISA class for the insn.
32unsigned HexagonMCInst::getType() const {
33  const uint64_t F = MCID->TSFlags;
34
35  return ((F >> HexagonII::TypePos) & HexagonII::TypeMask);
36}
37
38// Return whether the insn is an actual insn.
39bool HexagonMCInst::isCanon() const {
40  return (!MCID->isPseudo() &&
41          !isPrefix() &&
42          getType() != HexagonII::TypeENDLOOP);
43}
44
45// Return whether the insn is a prefix.
46bool HexagonMCInst::isPrefix() const {
47  return (getType() == HexagonII::TypePREFIX);
48}
49
50// Return whether the insn is solo, i.e., cannot be in a packet.
51bool HexagonMCInst::isSolo() const {
52  const uint64_t F = MCID->TSFlags;
53  return ((F >> HexagonII::SoloPos) & HexagonII::SoloMask);
54}
55
56// Return whether the insn is a new-value consumer.
57bool HexagonMCInst::isNewValue() const {
58  const uint64_t F = MCID->TSFlags;
59  return ((F >> HexagonII::NewValuePos) & HexagonII::NewValueMask);
60}
61
62// Return whether the instruction is a legal new-value producer.
63bool HexagonMCInst::hasNewValue() const {
64  const uint64_t F = MCID->TSFlags;
65  return ((F >> HexagonII::hasNewValuePos) & HexagonII::hasNewValueMask);
66}
67
68// Return the operand that consumes or produces a new value.
69const MCOperand& HexagonMCInst::getNewValue() const {
70  const uint64_t F = MCID->TSFlags;
71  const unsigned O = (F >> HexagonII::NewValueOpPos) &
72                     HexagonII::NewValueOpMask;
73  const MCOperand& MCO = getOperand(O);
74
75  assert ((isNewValue() || hasNewValue()) && MCO.isReg());
76  return (MCO);
77}
78
79// Return whether the instruction needs to be constant extended.
80// 1) Always return true if the instruction has 'isExtended' flag set.
81//
82// isExtendable:
83// 2) For immediate extended operands, return true only if the value is
84//    out-of-range.
85// 3) For global address, always return true.
86
87bool HexagonMCInst::isConstExtended(void) const {
88  if (isExtended())
89    return true;
90
91  if (!isExtendable())
92    return false;
93
94  short ExtOpNum = getCExtOpNum();
95  int MinValue   = getMinValue();
96  int MaxValue   = getMaxValue();
97  const MCOperand& MO = getOperand(ExtOpNum);
98
99  // We could be using an instruction with an extendable immediate and shoehorn
100  // a global address into it. If it is a global address it will be constant
101  // extended. We do this for COMBINE.
102  // We currently only handle isGlobal() because it is the only kind of
103  // object we are going to end up with here for now.
104  // In the future we probably should add isSymbol(), etc.
105  if (MO.isExpr())
106    return true;
107
108  // If the extendable operand is not 'Immediate' type, the instruction should
109  // have 'isExtended' flag set.
110  assert(MO.isImm() && "Extendable operand must be Immediate type");
111
112  int ImmValue = MO.getImm();
113  return (ImmValue < MinValue || ImmValue > MaxValue);
114}
115
116// Return whether the instruction must be always extended.
117bool HexagonMCInst::isExtended(void) const {
118  const uint64_t F = MCID->TSFlags;
119  return (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
120}
121
122// Return true if the instruction may be extended based on the operand value.
123bool HexagonMCInst::isExtendable(void) const {
124  const uint64_t F = MCID->TSFlags;
125  return (F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask;
126}
127
128// Return number of bits in the constant extended operand.
129unsigned HexagonMCInst::getBitCount(void) const {
130  const uint64_t F = MCID->TSFlags;
131  return ((F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask);
132}
133
134// Return constant extended operand number.
135unsigned short HexagonMCInst::getCExtOpNum(void) const {
136  const uint64_t F = MCID->TSFlags;
137  return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask);
138}
139
140// Return whether the operand can be constant extended.
141bool HexagonMCInst::isOperandExtended(const unsigned short OperandNum) const {
142  const uint64_t F = MCID->TSFlags;
143  return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask)
144          == OperandNum;
145}
146
147// Return the min value that a constant extendable operand can have
148// without being extended.
149int HexagonMCInst::getMinValue(void) const {
150  const uint64_t F = MCID->TSFlags;
151  unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
152                    & HexagonII::ExtentSignedMask;
153  unsigned bits =  (F >> HexagonII::ExtentBitsPos)
154                    & HexagonII::ExtentBitsMask;
155
156  if (isSigned) // if value is signed
157    return -1 << (bits - 1);
158  else
159    return 0;
160}
161
162// Return the max value that a constant extendable operand can have
163// without being extended.
164int HexagonMCInst::getMaxValue(void) const {
165  const uint64_t F = MCID->TSFlags;
166  unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
167                    & HexagonII::ExtentSignedMask;
168  unsigned bits =  (F >> HexagonII::ExtentBitsPos)
169                    & HexagonII::ExtentBitsMask;
170
171  if (isSigned) // if value is signed
172    return ~(-1 << (bits - 1));
173  else
174    return ~(-1 << bits);
175}
176