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 ClangBuiltinName; // 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-callback.
129   bool isNoCallback;
130 
131   /// True if the intrinsic is no-sync.
132   bool isNoSync;
133 
134   /// True if the intrinsic is no-free.
135   bool isNoFree;
136 
137   /// True if the intrinsic is will-return.
138   bool isWillReturn;
139 
140   /// True if the intrinsic is cold.
141   bool isCold;
142 
143   /// True if the intrinsic is marked as convergent.
144   bool isConvergent;
145 
146   /// True if the intrinsic has side effects that aren't captured by any
147   /// of the other flags.
148   bool hasSideEffects;
149 
150   // True if the intrinsic is marked as speculatable.
151   bool isSpeculatable;
152 
153   enum ArgAttrKind {
154     NoCapture,
155     NoAlias,
156     NoUndef,
157     Returned,
158     ReadOnly,
159     WriteOnly,
160     ReadNone,
161     ImmArg,
162     Alignment
163   };
164 
165   struct ArgAttribute {
166     unsigned Index;
167     ArgAttrKind Kind;
168     uint64_t Value;
169 
170     ArgAttribute(unsigned Idx, ArgAttrKind K, uint64_t V)
171         : Index(Idx), Kind(K), Value(V) {}
172 
173     bool operator<(const ArgAttribute &Other) const {
174       return std::tie(Index, Kind, Value) <
175              std::tie(Other.Index, Other.Kind, Other.Value);
176     }
177   };
178 
179   std::vector<ArgAttribute> ArgumentAttributes;
180 
181   bool hasProperty(enum SDNP Prop) const {
182     return Properties & (1 << Prop);
183   }
184 
185   /// Goes through all IntrProperties that have IsDefault
186   /// value set and sets the property.
187   void setDefaultProperties(Record *R, std::vector<Record *> DefaultProperties);
188 
189   /// Helper function to set property \p Name to true;
190   void setProperty(Record *R);
191 
192   /// Returns true if the parameter at \p ParamIdx is a pointer type. Returns
193   /// false if the parameter is not a pointer, or \p ParamIdx is greater than
194   /// the size of \p IS.ParamVTs.
195   ///
196   /// Note that this requires that \p IS.ParamVTs is available.
197   bool isParamAPointer(unsigned ParamIdx) const;
198 
199   bool isParamImmArg(unsigned ParamIdx) const;
200 
201   CodeGenIntrinsic(Record *R, std::vector<Record *> DefaultProperties);
202 };
203 
204 class CodeGenIntrinsicTable {
205   std::vector<CodeGenIntrinsic> Intrinsics;
206 
207 public:
208   struct TargetSet {
209     std::string Name;
210     size_t Offset;
211     size_t Count;
212   };
213   std::vector<TargetSet> Targets;
214 
215   explicit CodeGenIntrinsicTable(const RecordKeeper &RC);
216   CodeGenIntrinsicTable() = default;
217 
218   bool empty() const { return Intrinsics.empty(); }
219   size_t size() const { return Intrinsics.size(); }
220   CodeGenIntrinsic &operator[](size_t Pos) { return Intrinsics[Pos]; }
221   const CodeGenIntrinsic &operator[](size_t Pos) const {
222     return Intrinsics[Pos];
223   }
224 };
225 }
226 
227 #endif
228