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