1 //===- CodeGenIntrinsic.h - Intrinsic Class Wrapper ------------*- 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 defines a wrapper class for the 'Intrinsic' TableGen class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_UTILS_TABLEGEN_CODEGENINTRINSICS_H
14 #define LLVM_UTILS_TABLEGEN_CODEGENINTRINSICS_H
15 
16 #include "SDNodeProperties.h"
17 #include "llvm/Support/MachineValueType.h"
18 #include <string>
19 #include <vector>
20 
21 namespace llvm {
22 class Record;
23 class RecordKeeper;
24 
25 struct CodeGenIntrinsic {
26   Record *TheDef;             // The actual record defining this intrinsic.
27   std::string Name;           // The name of the LLVM function "llvm.bswap.i32"
28   std::string EnumName;       // The name of the enum "bswap_i32"
29   std::string GCCBuiltinName; // Name of the corresponding GCC builtin, or "".
30   std::string MSBuiltinName;  // Name of the corresponding MS builtin, or "".
31   std::string TargetPrefix;   // Target prefix, e.g. "ppc" for t-s intrinsics.
32 
33   /// This structure holds the return values and parameter values of an
34   /// intrinsic. If the number of return values is > 1, then the intrinsic
35   /// implicitly returns a first-class aggregate. The numbering of the types
36   /// starts at 0 with the first return value and continues from there through
37   /// the parameter list. This is useful for "matching" types.
38   struct IntrinsicSignature {
39     /// The MVT::SimpleValueType for each return type. Note that this list is
40     /// only populated when in the context of a target .td file. When building
41     /// Intrinsics.td, this isn't available, because we don't know the target
42     /// pointer size.
43     std::vector<MVT::SimpleValueType> RetVTs;
44 
45     /// The records for each return type.
46     std::vector<Record *> RetTypeDefs;
47 
48     /// The MVT::SimpleValueType for each parameter type. Note that this list is
49     /// only populated when in the context of a target .td file.  When building
50     /// Intrinsics.td, this isn't available, because we don't know the target
51     /// pointer size.
52     std::vector<MVT::SimpleValueType> ParamVTs;
53 
54     /// The records for each parameter type.
55     std::vector<Record *> ParamTypeDefs;
56   };
57 
58   IntrinsicSignature IS;
59 
60   /// Bit flags describing the type (ref/mod) and location of memory
61   /// accesses that may be performed by the intrinsics. Analogous to
62   /// \c FunctionModRefBehaviour.
63   enum ModRefBits {
64     /// The intrinsic may access memory that is otherwise inaccessible via
65     /// LLVM IR.
66     MR_InaccessibleMem = 1,
67 
68     /// The intrinsic may access memory through pointer arguments.
69     /// LLVM IR.
70     MR_ArgMem = 2,
71 
72     /// The intrinsic may access memory anywhere, i.e. it is not restricted
73     /// to access through pointer arguments.
74     MR_Anywhere = 4 | MR_ArgMem | MR_InaccessibleMem,
75 
76     /// The intrinsic may read memory.
77     MR_Ref = 8,
78 
79     /// The intrinsic may write memory.
80     MR_Mod = 16,
81 
82     /// The intrinsic may both read and write memory.
83     MR_ModRef = MR_Ref | MR_Mod,
84   };
85 
86   /// Memory mod/ref behavior of this intrinsic, corresponding to intrinsic
87   /// properties (IntrReadMem, IntrArgMemOnly, etc.).
88   enum ModRefBehavior {
89     NoMem = 0,
90     ReadArgMem = MR_Ref | MR_ArgMem,
91     ReadInaccessibleMem = MR_Ref | MR_InaccessibleMem,
92     ReadInaccessibleMemOrArgMem = MR_Ref | MR_ArgMem | MR_InaccessibleMem,
93     ReadMem = MR_Ref | MR_Anywhere,
94     WriteArgMem = MR_Mod | MR_ArgMem,
95     WriteInaccessibleMem = MR_Mod | MR_InaccessibleMem,
96     WriteInaccessibleMemOrArgMem = MR_Mod | MR_ArgMem | MR_InaccessibleMem,
97     WriteMem = MR_Mod | MR_Anywhere,
98     ReadWriteArgMem = MR_ModRef | MR_ArgMem,
99     ReadWriteInaccessibleMem = MR_ModRef | MR_InaccessibleMem,
100     ReadWriteInaccessibleMemOrArgMem = MR_ModRef | MR_ArgMem |
101                                        MR_InaccessibleMem,
102     ReadWriteMem = MR_ModRef | MR_Anywhere,
103   };
104   ModRefBehavior ModRef;
105 
106   /// SDPatternOperator Properties applied to the intrinsic.
107   unsigned Properties;
108 
109   /// This is set to true if the intrinsic is overloaded by its argument
110   /// types.
111   bool isOverloaded;
112 
113   /// True if the intrinsic is commutative.
114   bool isCommutative;
115 
116   /// True if the intrinsic can throw.
117   bool canThrow;
118 
119   /// True if the intrinsic is marked as noduplicate.
120   bool isNoDuplicate;
121 
122   /// True if the intrinsic is marked as nomerge.
123   bool isNoMerge;
124 
125   /// True if the intrinsic is no-return.
126   bool isNoReturn;
127 
128   /// True if the intrinsic is no-sync.
129   bool isNoSync;
130 
131   /// True if the intrinsic is no-free.
132   bool isNoFree;
133 
134   /// True if the intrinsic is will-return.
135   bool isWillReturn;
136 
137   /// True if the intrinsic is cold.
138   bool isCold;
139 
140   /// True if the intrinsic is marked as convergent.
141   bool isConvergent;
142 
143   /// True if the intrinsic has side effects that aren't captured by any
144   /// of the other flags.
145   bool hasSideEffects;
146 
147   // True if the intrinsic is marked as speculatable.
148   bool isSpeculatable;
149 
150   enum ArgAttrKind {
151     NoCapture,
152     NoAlias,
153     NoUndef,
154     Returned,
155     ReadOnly,
156     WriteOnly,
157     ReadNone,
158     ImmArg,
159     Alignment
160   };
161 
162   struct ArgAttribute {
163     unsigned Index;
164     ArgAttrKind Kind;
165     uint64_t Value;
166 
167     ArgAttribute(unsigned Idx, ArgAttrKind K, uint64_t V)
168         : Index(Idx), Kind(K), Value(V) {}
169 
170     bool operator<(const ArgAttribute &Other) const {
171       return std::tie(Index, Kind, Value) <
172              std::tie(Other.Index, Other.Kind, Other.Value);
173     }
174   };
175 
176   std::vector<ArgAttribute> ArgumentAttributes;
177 
178   bool hasProperty(enum SDNP Prop) const {
179     return Properties & (1 << Prop);
180   }
181 
182   /// Goes through all IntrProperties that have IsDefault
183   /// value set and sets the property.
184   void setDefaultProperties(Record *R, std::vector<Record *> DefaultProperties);
185 
186   /// Helper function to set property \p Name to true;
187   void setProperty(Record *R);
188 
189   /// Returns true if the parameter at \p ParamIdx is a pointer type. Returns
190   /// false if the parameter is not a pointer, or \p ParamIdx is greater than
191   /// the size of \p IS.ParamVTs.
192   ///
193   /// Note that this requires that \p IS.ParamVTs is available.
194   bool isParamAPointer(unsigned ParamIdx) const;
195 
196   bool isParamImmArg(unsigned ParamIdx) const;
197 
198   CodeGenIntrinsic(Record *R, std::vector<Record *> DefaultProperties);
199 };
200 
201 class CodeGenIntrinsicTable {
202   std::vector<CodeGenIntrinsic> Intrinsics;
203 
204 public:
205   struct TargetSet {
206     std::string Name;
207     size_t Offset;
208     size_t Count;
209   };
210   std::vector<TargetSet> Targets;
211 
212   explicit CodeGenIntrinsicTable(const RecordKeeper &RC);
213   CodeGenIntrinsicTable() = default;
214 
215   bool empty() const { return Intrinsics.empty(); }
216   size_t size() const { return Intrinsics.size(); }
217   CodeGenIntrinsic &operator[](size_t Pos) { return Intrinsics[Pos]; }
218   const CodeGenIntrinsic &operator[](size_t Pos) const {
219     return Intrinsics[Pos];
220   }
221 };
222 }
223 
224 #endif
225