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