1//===- CodeGenIntrinsics.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_BASIC_CODEGENINTRINSICS_H
14#define LLVM_UTILS_TABLEGEN_BASIC_CODEGENINTRINSICS_H
15
16#include "SDNodeProperties.h"
17#include "llvm/ADT/ArrayRef.h"
18#include "llvm/ADT/DenseMap.h"
19#include "llvm/ADT/SmallVector.h"
20#include "llvm/Support/ModRef.h"
21#include <string>
22#include <tuple>
23#include <vector>
24
25namespace llvm {
26class Record;
27class RecordKeeper;
28
29// Global information needed to build intrinsics.
30struct CodeGenIntrinsicContext {
31 explicit CodeGenIntrinsicContext(const RecordKeeper &RC);
32 std::vector<const Record *> DefaultProperties;
33 const Record *NoUnwindAttr;
34};
35
36struct CodeGenIntrinsic {
37 const Record *TheDef; // The actual record defining this intrinsic.
38 std::string Name; // The name of the LLVM function "llvm.bswap.i32"
39 StringRef EnumName; // The name of the enum "bswap_i32"
40 StringRef ClangBuiltinName; // Name of the corresponding GCC builtin, or "".
41 StringRef MSBuiltinName; // Name of the corresponding MS builtin, or "".
42 StringRef TargetPrefix; // Target prefix, e.g. "ppc" for t-s intrinsics.
43 StringRef TargetFeatures; // Target feature expression required, or "".
44
45 /// This structure holds the return values and parameter values of an
46 /// intrinsic. If the number of return values is > 1, then the intrinsic
47 /// implicitly returns a first-class aggregate. The numbering of the types
48 /// starts at 0 with the first return value and continues from there through
49 /// the parameter list. This is useful for "matching" types.
50 struct IntrinsicSignature {
51 /// The MVT::SimpleValueType for each return type. Note that this list is
52 /// only populated when in the context of a target .td file. When building
53 /// Intrinsics.td, this isn't available, because we don't know the target
54 /// pointer size.
55 std::vector<const Record *> RetTys;
56
57 /// The MVT::SimpleValueType for each parameter type. Note that this list is
58 /// only populated when in the context of a target .td file. When building
59 /// Intrinsics.td, this isn't available, because we don't know the target
60 /// pointer size.
61 std::vector<const Record *> ParamTys;
62 };
63
64 IntrinsicSignature IS;
65
66 /// Memory effects of the intrinsic.
67 MemoryEffects ME = MemoryEffects::unknown();
68
69 /// SDPatternOperator Properties applied to the intrinsic.
70 unsigned Properties = 0;
71
72 /// This is set to true if the intrinsic is overloaded by its argument
73 /// types.
74 bool isOverloaded = false;
75
76 /// True if the intrinsic is commutative.
77 bool isCommutative = false;
78
79 /// True if the intrinsic can throw.
80 bool canThrow = false;
81
82 /// True if the intrinsic has side effects that aren't captured by any
83 /// of the other flags.
84 bool hasSideEffects = false;
85
86 // True if the intrinsic is marked as IntrTriviallyScalarizable.
87 bool isTriviallyScalarizable = false;
88
89 /// Function attribute records from Attributes.td, sorted by record ID.
90 SmallVector<const Record *, 8> FnAttributes;
91
92 struct ArgAttribute {
93 const Record *Attr;
94 uint64_t Value;
95 uint64_t Value2;
96
97 ArgAttribute(const Record *Attr, uint64_t V, uint64_t V2)
98 : Attr(Attr), Value(V), Value2(V2) {}
99
100 bool operator<(const ArgAttribute &Other) const;
101 bool operator==(const ArgAttribute &Other) const {
102 return std::tie(args: Attr, args: Value, args: Value2) ==
103 std::tie(args: Other.Attr, args: Other.Value, args: Other.Value2);
104 }
105 };
106
107 struct ImmArgRangeSet {
108 using Range = std::pair<int64_t, int64_t>;
109 using RangeList = SmallVector<Range, 4>;
110
111 unsigned ArgNo;
112 RangeList Ranges;
113 };
114
115 /// Vector of attributes for each argument.
116 SmallVector<SmallVector<ArgAttribute, 0>> ArgumentAttributes;
117
118 /// Range-set constraints for immediate arguments, indexed by argument number.
119 SmallVector<ImmArgRangeSet> ImmArgRangeSets;
120
121 void addArgAttribute(unsigned Idx, const Record *Attr, uint64_t V = 0,
122 uint64_t V2 = 0);
123 void addImmArgRangeSet(unsigned ArgNo, ImmArgRangeSet::RangeList Ranges);
124
125 /// Structure to store pretty print and argument information.
126 struct PrettyPrintArgInfo {
127 unsigned ArgIdx;
128 StringRef ArgName;
129 StringRef FuncName;
130
131 PrettyPrintArgInfo(unsigned Idx, StringRef Name, StringRef Func)
132 : ArgIdx(Idx), ArgName(Name), FuncName(Func) {}
133 };
134
135 /// Vector that stores ArgInfo (ArgIndex, ArgName, FunctionName).
136 SmallVector<PrettyPrintArgInfo> PrettyPrintFunctions;
137
138 /// Default values for parameters. Index = param index.
139 SmallVector<std::optional<uint64_t>> ParamDefaultValues;
140
141 void addPrettyPrintFunction(unsigned ArgIdx, StringRef ArgName,
142 StringRef FuncName);
143
144 void addDefaultArgValue(unsigned ArgIdx, uint64_t Value);
145
146 bool hasProperty(enum SDNP Prop) const { return Properties & (1 << Prop); }
147
148 bool hasFnAttribute(StringRef Name) const;
149
150 bool isConvergent() const { return hasFnAttribute(Name: "Convergent"); }
151
152 /// Goes through all IntrProperties that have IsDefault value set and sets
153 /// the property.
154 void setDefaultProperties(ArrayRef<const Record *> DefaultProperties);
155
156 /// Helper function to set property \p Name to true.
157 void setProperty(const Record *R);
158
159 /// Returns true if the parameter at \p ParamIdx is a pointer type. Returns
160 /// false if the parameter is not a pointer, or \p ParamIdx is greater than
161 /// the size of \p IS.ParamVTs.
162 ///
163 /// Note that this requires that \p IS.ParamVTs is available.
164 bool isParamAPointer(unsigned ParamIdx) const;
165
166 bool isParamImmArg(unsigned ParamIdx) const;
167
168 llvm::IRMemLocation getValueAsIRMemLocation(const Record *R) const;
169
170 CodeGenIntrinsic(const Record *R, const CodeGenIntrinsicContext &Ctx);
171};
172
173class CodeGenIntrinsicTable {
174public:
175 struct TargetSet {
176 StringRef Name;
177 size_t Offset;
178 size_t Count;
179 };
180
181 explicit CodeGenIntrinsicTable(const RecordKeeper &RC);
182
183 bool empty() const { return Intrinsics.empty(); }
184 size_t size() const { return Intrinsics.size(); }
185 auto begin() const { return Intrinsics.begin(); }
186 auto end() const { return Intrinsics.end(); }
187 const CodeGenIntrinsic &operator[](size_t Pos) const {
188 return Intrinsics[Pos];
189 }
190 ArrayRef<CodeGenIntrinsic> operator[](const TargetSet &Set) const {
191 return ArrayRef(&Intrinsics[Set.Offset], Set.Count);
192 }
193 ArrayRef<TargetSet> getTargets() const { return Targets; }
194
195private:
196 void CheckDuplicateIntrinsics() const;
197 void CheckTargetIndependentIntrinsics() const;
198 void CheckOverloadSuffixConflicts() const;
199
200 std::vector<CodeGenIntrinsic> Intrinsics;
201 std::vector<TargetSet> Targets;
202};
203
204// This class builds `CodeGenIntrinsic` on demand for a given Def.
205class CodeGenIntrinsicMap {
206 DenseMap<const Record *, std::unique_ptr<CodeGenIntrinsic>> Map;
207 const CodeGenIntrinsicContext Ctx;
208
209public:
210 explicit CodeGenIntrinsicMap(const RecordKeeper &RC) : Ctx(RC) {}
211 const CodeGenIntrinsic &operator[](const Record *Def);
212};
213
214} // namespace llvm
215
216#endif // LLVM_UTILS_TABLEGEN_BASIC_CODEGENINTRINSICS_H
217