1 //===-- ARMBaseInfo.h - Top level definitions for ARM ---*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains small standalone helper functions and enum definitions for
10 // the ARM target useful for the compiler back-end and the MC libraries.
11 // As such, it deliberately does not include references to LLVM core
12 // code gen types, passes, etc..
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_LIB_TARGET_ARM_UTILS_ARMBASEINFO_H
17 #define LLVM_LIB_TARGET_ARM_UTILS_ARMBASEINFO_H
18 
19 #include "llvm/ADT/StringSwitch.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/MC/SubtargetFeature.h"
22 #include "MCTargetDesc/ARMMCTargetDesc.h"
23 
24 namespace llvm {
25 
26 // Enums corresponding to ARM condition codes
27 namespace ARMCC {
28 // The CondCodes constants map directly to the 4-bit encoding of the
29 // condition field for predicated instructions.
30 enum CondCodes { // Meaning (integer)          Meaning (floating-point)
31   EQ,            // Equal                      Equal
32   NE,            // Not equal                  Not equal, or unordered
33   HS,            // Carry set                  >, ==, or unordered
34   LO,            // Carry clear                Less than
35   MI,            // Minus, negative            Less than
36   PL,            // Plus, positive or zero     >, ==, or unordered
37   VS,            // Overflow                   Unordered
38   VC,            // No overflow                Not unordered
39   HI,            // Unsigned higher            Greater than, or unordered
40   LS,            // Unsigned lower or same     Less than or equal
41   GE,            // Greater than or equal      Greater than or equal
42   LT,            // Less than                  Less than, or unordered
43   GT,            // Greater than               Greater than
44   LE,            // Less than or equal         <, ==, or unordered
45   AL             // Always (unconditional)     Always (unconditional)
46 };
47 
48 inline static CondCodes getOppositeCondition(CondCodes CC) {
49   switch (CC) {
50   default: llvm_unreachable("Unknown condition code");
51   case EQ: return NE;
52   case NE: return EQ;
53   case HS: return LO;
54   case LO: return HS;
55   case MI: return PL;
56   case PL: return MI;
57   case VS: return VC;
58   case VC: return VS;
59   case HI: return LS;
60   case LS: return HI;
61   case GE: return LT;
62   case LT: return GE;
63   case GT: return LE;
64   case LE: return GT;
65   }
66 }
67 
68 /// getSwappedCondition - assume the flags are set by MI(a,b), return
69 /// the condition code if we modify the instructions such that flags are
70 /// set by MI(b,a).
71 inline static ARMCC::CondCodes getSwappedCondition(ARMCC::CondCodes CC) {
72   switch (CC) {
73   default: return ARMCC::AL;
74   case ARMCC::EQ: return ARMCC::EQ;
75   case ARMCC::NE: return ARMCC::NE;
76   case ARMCC::HS: return ARMCC::LS;
77   case ARMCC::LO: return ARMCC::HI;
78   case ARMCC::HI: return ARMCC::LO;
79   case ARMCC::LS: return ARMCC::HS;
80   case ARMCC::GE: return ARMCC::LE;
81   case ARMCC::LT: return ARMCC::GT;
82   case ARMCC::GT: return ARMCC::LT;
83   case ARMCC::LE: return ARMCC::GE;
84   }
85 }
86 } // end namespace ARMCC
87 
88 namespace ARMVCC {
89   enum VPTCodes {
90     None = 0,
91     Then,
92     Else
93   };
94 } // namespace ARMVCC
95 
96 namespace ARM {
97   /// Mask values for IT and VPT Blocks, to be used by MCOperands.
98   /// Note that this is different from the "real" encoding used by the
99   /// instructions. In this encoding, the lowest set bit indicates the end of
100   /// the encoding, and above that, "1" indicates an else, while "0" indicates
101   /// a then.
102   ///   Tx = x100
103   ///   Txy = xy10
104   ///   Txyz = xyz1
105   enum class PredBlockMask {
106     T = 0b1000,
107     TT = 0b0100,
108     TE = 0b1100,
109     TTT = 0b0010,
110     TTE = 0b0110,
111     TEE = 0b1110,
112     TET = 0b1010,
113     TTTT = 0b0001,
114     TTTE = 0b0011,
115     TTEE = 0b0111,
116     TTET = 0b0101,
117     TEEE = 0b1111,
118     TEET = 0b1101,
119     TETT = 0b1001,
120     TETE = 0b1011
121   };
122 } // namespace ARM
123 
124 inline static const char *ARMVPTPredToString(ARMVCC::VPTCodes CC) {
125   switch (CC) {
126   case ARMVCC::None:  return "none";
127   case ARMVCC::Then:  return "t";
128   case ARMVCC::Else:  return "e";
129   }
130   llvm_unreachable("Unknown VPT code");
131 }
132 
133 inline static unsigned ARMVectorCondCodeFromString(StringRef CC) {
134   return StringSwitch<unsigned>(CC.lower())
135     .Case("t", ARMVCC::Then)
136     .Case("e", ARMVCC::Else)
137     .Default(~0U);
138 }
139 
140 inline static const char *ARMCondCodeToString(ARMCC::CondCodes CC) {
141   switch (CC) {
142   case ARMCC::EQ:  return "eq";
143   case ARMCC::NE:  return "ne";
144   case ARMCC::HS:  return "hs";
145   case ARMCC::LO:  return "lo";
146   case ARMCC::MI:  return "mi";
147   case ARMCC::PL:  return "pl";
148   case ARMCC::VS:  return "vs";
149   case ARMCC::VC:  return "vc";
150   case ARMCC::HI:  return "hi";
151   case ARMCC::LS:  return "ls";
152   case ARMCC::GE:  return "ge";
153   case ARMCC::LT:  return "lt";
154   case ARMCC::GT:  return "gt";
155   case ARMCC::LE:  return "le";
156   case ARMCC::AL:  return "al";
157   }
158   llvm_unreachable("Unknown condition code");
159 }
160 
161 inline static unsigned ARMCondCodeFromString(StringRef CC) {
162   return StringSwitch<unsigned>(CC.lower())
163     .Case("eq", ARMCC::EQ)
164     .Case("ne", ARMCC::NE)
165     .Case("hs", ARMCC::HS)
166     .Case("cs", ARMCC::HS)
167     .Case("lo", ARMCC::LO)
168     .Case("cc", ARMCC::LO)
169     .Case("mi", ARMCC::MI)
170     .Case("pl", ARMCC::PL)
171     .Case("vs", ARMCC::VS)
172     .Case("vc", ARMCC::VC)
173     .Case("hi", ARMCC::HI)
174     .Case("ls", ARMCC::LS)
175     .Case("ge", ARMCC::GE)
176     .Case("lt", ARMCC::LT)
177     .Case("gt", ARMCC::GT)
178     .Case("le", ARMCC::LE)
179     .Case("al", ARMCC::AL)
180     .Default(~0U);
181 }
182 
183 // System Registers
184 namespace ARMSysReg {
185   struct MClassSysReg {
186     const char *Name;
187     uint16_t M1Encoding12;
188     uint16_t M2M3Encoding8;
189     uint16_t Encoding;
190     FeatureBitset FeaturesRequired;
191 
192     // return true if FeaturesRequired are all present in ActiveFeatures
193     bool hasRequiredFeatures(FeatureBitset ActiveFeatures) const {
194       return (FeaturesRequired & ActiveFeatures) == FeaturesRequired;
195     }
196 
197     // returns true if TestFeatures are all present in FeaturesRequired
198     bool isInRequiredFeatures(FeatureBitset TestFeatures) const {
199       return (FeaturesRequired & TestFeatures) == TestFeatures;
200     }
201   };
202 
203   #define GET_MCLASSSYSREG_DECL
204   #include "ARMGenSystemRegister.inc"
205 
206   // lookup system register using 12-bit SYSm value.
207   // Note: the search is uniqued using M1 mask
208   const MClassSysReg *lookupMClassSysRegBy12bitSYSmValue(unsigned SYSm);
209 
210   // returns APSR with _<bits> qualifier.
211   // Note: ARMv7-M deprecates using MSR APSR without a _<bits> qualifier
212   const MClassSysReg *lookupMClassSysRegAPSRNonDeprecated(unsigned SYSm);
213 
214   // lookup system registers using 8-bit SYSm value
215   const MClassSysReg *lookupMClassSysRegBy8bitSYSmValue(unsigned SYSm);
216 
217 } // end namespace ARMSysReg
218 
219 // Banked Registers
220 namespace ARMBankedReg {
221   struct BankedReg {
222     const char *Name;
223     uint16_t Encoding;
224   };
225   #define GET_BANKEDREG_DECL
226   #include "ARMGenSystemRegister.inc"
227 } // end namespace ARMBankedReg
228 
229 } // end namespace llvm
230 
231 #endif // LLVM_LIB_TARGET_ARM_UTILS_ARMBASEINFO_H
232