33e7c81904f02642f98693f5c356bdbce72c222e
[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 #include "HexagonBaseInfo.h"
16
17 namespace llvm {
18 void HexagonMCInstrInfo::AppendImplicitOperands(MCInst &MCI) {
19   MCI.addOperand(MCOperand::CreateImm(0));
20   MCI.addOperand(MCOperand::CreateInst(nullptr));
21 }
22
23 unsigned HexagonMCInstrInfo::getBitCount(MCInstrInfo const &MCII,
24                                          MCInst const &MCI) {
25   uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
26   return ((F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask);
27 }
28
29 // Return constant extended operand number.
30 unsigned short HexagonMCInstrInfo::getCExtOpNum(MCInstrInfo const &MCII,
31                                                 MCInst const &MCI) {
32   const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
33   return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask);
34 }
35
36 MCInstrDesc const &HexagonMCInstrInfo::getDesc(MCInstrInfo const &MCII,
37                                                MCInst const &MCI) {
38   return (MCII.get(MCI.getOpcode()));
39 }
40
41 std::bitset<16> HexagonMCInstrInfo::GetImplicitBits(MCInst const &MCI) {
42   SanityCheckImplicitOperands(MCI);
43   std::bitset<16> Bits(MCI.getOperand(MCI.getNumOperands() - 2).getImm());
44   return Bits;
45 }
46
47 // Return the max value that a constant extendable operand can have
48 // without being extended.
49 int HexagonMCInstrInfo::getMaxValue(MCInstrInfo const &MCII,
50                                     MCInst const &MCI) {
51   uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
52   unsigned isSigned =
53       (F >> HexagonII::ExtentSignedPos) & HexagonII::ExtentSignedMask;
54   unsigned bits = (F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask;
55
56   if (isSigned) // if value is signed
57     return ~(-1U << (bits - 1));
58   else
59     return ~(-1U << bits);
60 }
61
62 // Return the min value that a constant extendable operand can have
63 // without being extended.
64 int HexagonMCInstrInfo::getMinValue(MCInstrInfo const &MCII,
65                                     MCInst const &MCI) {
66   uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
67   unsigned isSigned =
68       (F >> HexagonII::ExtentSignedPos) & HexagonII::ExtentSignedMask;
69   unsigned bits = (F >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask;
70
71   if (isSigned) // if value is signed
72     return -1U << (bits - 1);
73   else
74     return 0;
75 }
76
77 // Return the operand that consumes or produces a new value.
78 MCOperand const &HexagonMCInstrInfo::getNewValue(MCInstrInfo const &MCII,
79                                                  MCInst const &MCI) {
80   uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
81   unsigned const O =
82       (F >> HexagonII::NewValueOpPos) & HexagonII::NewValueOpMask;
83   MCOperand const &MCO = MCI.getOperand(O);
84
85   assert((HexagonMCInstrInfo::isNewValue(MCII, MCI) ||
86           HexagonMCInstrInfo::hasNewValue(MCII, MCI)) &&
87          MCO.isReg());
88   return (MCO);
89 }
90
91 // Return the Hexagon ISA class for the insn.
92 unsigned HexagonMCInstrInfo::getType(MCInstrInfo const &MCII,
93                                      MCInst const &MCI) {
94   const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
95
96   return ((F >> HexagonII::TypePos) & HexagonII::TypeMask);
97 }
98
99 // Return whether the instruction is a legal new-value producer.
100 bool HexagonMCInstrInfo::hasNewValue(MCInstrInfo const &MCII,
101                                      MCInst const &MCI) {
102   const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
103   return ((F >> HexagonII::hasNewValuePos) & HexagonII::hasNewValueMask);
104 }
105
106 // Return whether the insn is an actual insn.
107 bool HexagonMCInstrInfo::isCanon(MCInstrInfo const &MCII, MCInst const &MCI) {
108   return (!HexagonMCInstrInfo::getDesc(MCII, MCI).isPseudo() &&
109           !HexagonMCInstrInfo::isPrefix(MCII, MCI) &&
110           HexagonMCInstrInfo::getType(MCII, MCI) != HexagonII::TypeENDLOOP);
111 }
112
113 // Return whether the instruction needs to be constant extended.
114 // 1) Always return true if the instruction has 'isExtended' flag set.
115 //
116 // isExtendable:
117 // 2) For immediate extended operands, return true only if the value is
118 //    out-of-range.
119 // 3) For global address, always return true.
120
121 bool HexagonMCInstrInfo::isConstExtended(MCInstrInfo const &MCII,
122                                          MCInst const &MCI) {
123   if (HexagonMCInstrInfo::isExtended(MCII, MCI))
124     return true;
125
126   if (!HexagonMCInstrInfo::isExtendable(MCII, MCI))
127     return false;
128
129   short ExtOpNum = HexagonMCInstrInfo::getCExtOpNum(MCII, MCI);
130   int MinValue = HexagonMCInstrInfo::getMinValue(MCII, MCI);
131   int MaxValue = HexagonMCInstrInfo::getMaxValue(MCII, MCI);
132   MCOperand const &MO = MCI.getOperand(ExtOpNum);
133
134   // We could be using an instruction with an extendable immediate and shoehorn
135   // a global address into it. If it is a global address it will be constant
136   // extended. We do this for COMBINE.
137   // We currently only handle isGlobal() because it is the only kind of
138   // object we are going to end up with here for now.
139   // In the future we probably should add isSymbol(), etc.
140   if (MO.isExpr())
141     return true;
142
143   // If the extendable operand is not 'Immediate' type, the instruction should
144   // have 'isExtended' flag set.
145   assert(MO.isImm() && "Extendable operand must be Immediate type");
146
147   int ImmValue = MO.getImm();
148   return (ImmValue < MinValue || ImmValue > MaxValue);
149 }
150
151 // Return true if the instruction may be extended based on the operand value.
152 bool HexagonMCInstrInfo::isExtendable(MCInstrInfo const &MCII,
153                                       MCInst const &MCI) {
154   uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
155   return (F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask;
156 }
157
158 // Return whether the instruction must be always extended.
159 bool HexagonMCInstrInfo::isExtended(MCInstrInfo const &MCII,
160                                     MCInst const &MCI) {
161   uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
162   return (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
163 }
164
165 // Return whether the insn is a new-value consumer.
166 bool HexagonMCInstrInfo::isNewValue(MCInstrInfo const &MCII,
167                                     MCInst const &MCI) {
168   const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
169   return ((F >> HexagonII::NewValuePos) & HexagonII::NewValueMask);
170 }
171
172 // Return whether the operand can be constant extended.
173 bool HexagonMCInstrInfo::isOperandExtended(MCInstrInfo const &MCII,
174                                            MCInst const &MCI,
175                                            unsigned short OperandNum) {
176   uint64_t const F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
177   return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask) ==
178          OperandNum;
179 }
180
181 bool HexagonMCInstrInfo::isPacketBegin(MCInst const &MCI) {
182   std::bitset<16> Bits(GetImplicitBits(MCI));
183   return Bits.test(packetBeginIndex);
184 }
185
186 bool HexagonMCInstrInfo::isPacketEnd(MCInst const &MCI) {
187   std::bitset<16> Bits(GetImplicitBits(MCI));
188   return Bits.test(packetEndIndex);
189 }
190
191 // Return whether the insn is a prefix.
192 bool HexagonMCInstrInfo::isPrefix(MCInstrInfo const &MCII, MCInst const &MCI) {
193   return (HexagonMCInstrInfo::getType(MCII, MCI) == HexagonII::TypePREFIX);
194 }
195
196 // Return whether the insn is solo, i.e., cannot be in a packet.
197 bool HexagonMCInstrInfo::isSolo(MCInstrInfo const &MCII, MCInst const &MCI) {
198   const uint64_t F = HexagonMCInstrInfo::getDesc(MCII, MCI).TSFlags;
199   return ((F >> HexagonII::SoloPos) & HexagonII::SoloMask);
200 }
201
202 void HexagonMCInstrInfo::resetPacket(MCInst &MCI) {
203   setPacketBegin(MCI, false);
204   setPacketEnd(MCI, false);
205 }
206
207 void HexagonMCInstrInfo::SetImplicitBits(MCInst &MCI, std::bitset<16> Bits) {
208   SanityCheckImplicitOperands(MCI);
209   MCI.getOperand(MCI.getNumOperands() - 2).setImm(Bits.to_ulong());
210 }
211
212 void HexagonMCInstrInfo::setPacketBegin(MCInst &MCI, bool f) {
213   std::bitset<16> Bits(GetImplicitBits(MCI));
214   Bits.set(packetBeginIndex, f);
215   SetImplicitBits(MCI, Bits);
216 }
217
218 void HexagonMCInstrInfo::setPacketEnd(MCInst &MCI, bool f) {
219   std::bitset<16> Bits(GetImplicitBits(MCI));
220   Bits.set(packetEndIndex, f);
221   SetImplicitBits(MCI, Bits);
222 }
223 }