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