1//===------------------------------------------------------------*- 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#ifndef LLVM_UTILS_TABLEGEN_COMMON_RUNTIMELIBCALLS_H
10#define LLVM_UTILS_TABLEGEN_COMMON_RUNTIMELIBCALLS_H
11
12#include "llvm/ADT/StringRef.h"
13#include "llvm/Support/raw_ostream.h"
14#include "llvm/TableGen/Record.h"
15#include "llvm/TableGen/SetTheory.h"
16
17namespace llvm {
18
19class AvailabilityPredicate {
20 const Record *TheDef;
21 std::string PredicateString;
22
23public:
24 AvailabilityPredicate(const Record *Def) : TheDef(Def) {
25 if (!TheDef)
26 return;
27 // An unset CondDag means the libcall is always available.
28 if (const RecordVal *RV = TheDef->getValue(Name: "CondDag")) {
29 if (const auto *Dag = dyn_cast_or_null<DagInit>(Val: RV->getValue());
30 Dag && !isa<UnsetInit>(Val: Dag->getOperator()))
31 PredicateString = lowerCondDag(Owner: TheDef, Val: Dag);
32 }
33 }
34
35 const Record *getDef() const { return TheDef; }
36
37 bool isAlwaysAvailable() const { return PredicateString.empty(); }
38
39 void emitIf(raw_ostream &OS) const {
40 OS << "if (" << PredicateString << ") {\n";
41 }
42
43 void emitEndIf(raw_ostream &OS) const { OS << "}\n"; }
44
45 void emitTableVariableNameSuffix(raw_ostream &OS) const {
46 if (TheDef)
47 OS << '_' << TheDef->getName();
48 }
49
50private:
51 // Lower a (all_of/any_of/not <LibcallPredicate>...) dag to a C++ boolean
52 // expression.
53 static std::string lowerCondDag(const Record *Owner, const Init *Val,
54 bool ParenIfBinOp = false);
55};
56
57class RuntimeLibcalls;
58class RuntimeLibcallImpl;
59
60/// Used to apply predicates to nested sets of libcalls.
61struct LibcallPredicateExpander : SetTheory::Expander {
62 const RuntimeLibcalls &Libcalls;
63 DenseMap<const RuntimeLibcallImpl *,
64 std::pair<std::vector<const Record *>, const Record *>> &Func2Preds;
65
66 LibcallPredicateExpander(
67 const RuntimeLibcalls &Libcalls,
68 DenseMap<const RuntimeLibcallImpl *,
69 std::pair<std::vector<const Record *>, const Record *>>
70 &Func2Preds)
71 : Libcalls(Libcalls), Func2Preds(Func2Preds) {}
72
73 void expand(SetTheory &ST, const Record *Def,
74 SetTheory::RecSet &Elts) override;
75};
76
77class RuntimeLibcall {
78 const Record *TheDef = nullptr;
79 const size_t EnumVal;
80
81public:
82 RuntimeLibcall() = delete;
83 RuntimeLibcall(const Record *Def, size_t EnumVal)
84 : TheDef(Def), EnumVal(EnumVal) {
85 assert(Def);
86 }
87
88 ~RuntimeLibcall() { assert(TheDef); }
89
90 const Record *getDef() const { return TheDef; }
91
92 StringRef getName() const { return TheDef->getName(); }
93
94 size_t getEnumVal() const { return EnumVal; }
95
96 void emitEnumEntry(raw_ostream &OS) const {
97 OS << "RTLIB::" << TheDef->getValueAsString(FieldName: "Name");
98 }
99};
100
101class RuntimeLibcallImpl {
102 const Record *TheDef;
103 const RuntimeLibcall *Provides = nullptr;
104 const RuntimeLibcall *ProvidesF80 = nullptr;
105 const RuntimeLibcall *ProvidesF128 = nullptr;
106 const RuntimeLibcall *ProvidesPPCF128 = nullptr;
107 const size_t EnumVal;
108
109public:
110 RuntimeLibcallImpl(
111 const Record *Def,
112 const DenseMap<const Record *, const RuntimeLibcall *> &ProvideMap,
113 size_t EnumVal)
114 : TheDef(Def), EnumVal(EnumVal) {
115 if (const Record *ProvidesDef = Def->getValueAsOptionalDef(FieldName: "Provides"))
116 Provides = ProvideMap.lookup(Val: ProvidesDef);
117
118 if (Def->isSubClassOf(Name: "LongDoubleLibcallImpl")) {
119 ProvidesF80 = ProvideMap.lookup(Val: Def->getValueAsDef(FieldName: "ProvidesF80"));
120 ProvidesF128 = ProvideMap.lookup(Val: Def->getValueAsDef(FieldName: "ProvidesF128"));
121 ProvidesPPCF128 =
122 ProvideMap.lookup(Val: Def->getValueAsDef(FieldName: "ProvidesPPCF128"));
123 }
124 }
125
126 ~RuntimeLibcallImpl() = default;
127
128 const Record *getDef() const { return TheDef; }
129
130 StringRef getName() const { return TheDef->getName(); }
131
132 size_t getEnumVal() const { return EnumVal; }
133
134 const RuntimeLibcall *getProvides() const { return Provides; }
135
136 bool isLongDouble() const { return ProvidesF80 != nullptr; }
137
138 const RuntimeLibcall *getProvidesF80() const { return ProvidesF80; }
139 const RuntimeLibcall *getProvidesF128() const { return ProvidesF128; }
140 const RuntimeLibcall *getProvidesPPCF128() const { return ProvidesPPCF128; }
141
142 StringRef getLibcallFuncName() const {
143 return TheDef->getValueAsString(FieldName: "LibCallFuncName");
144 }
145
146 const Record *getCallingConv() const {
147 return TheDef->getValueAsOptionalDef(FieldName: "CallingConv");
148 }
149
150 void emitQuotedLibcallFuncName(raw_ostream &OS) const {
151 OS << '\"' << getLibcallFuncName() << '\"';
152 }
153
154 void emitEnumEntry(raw_ostream &OS) const {
155 OS << "RTLIB::impl_" << this->getName();
156 }
157
158 void emitSetImplCall(raw_ostream &OS) const {
159 OS << "setLibcallImpl(";
160 Provides->emitEnumEntry(OS);
161 OS << ", ";
162 emitEnumEntry(OS);
163 OS << "); // " << getLibcallFuncName() << '\n';
164 }
165
166 void emitTableEntry(raw_ostream &OS) const {
167 OS << '{';
168 Provides->emitEnumEntry(OS);
169 OS << ", ";
170 emitEnumEntry(OS);
171 OS << "}, // " << getLibcallFuncName() << '\n';
172 }
173
174 void emitSetCallingConv(raw_ostream &OS) const {}
175};
176
177struct LibcallsWithCC {
178 std::vector<const RuntimeLibcallImpl *> LibcallImpls;
179 const Record *CallingConv = nullptr;
180};
181
182class RuntimeLibcalls {
183private:
184 DenseMap<const Record *, const RuntimeLibcall *> Def2RuntimeLibcall;
185 DenseMap<const Record *, const RuntimeLibcallImpl *> Def2RuntimeLibcallImpl;
186
187 std::vector<RuntimeLibcall> RuntimeLibcallDefList;
188 std::vector<RuntimeLibcallImpl> RuntimeLibcallImplDefList;
189
190 std::vector<const Record *> FuncArgTypeList;
191
192public:
193 RuntimeLibcalls(const RecordKeeper &Records);
194
195 ArrayRef<RuntimeLibcall> getRuntimeLibcallDefList() const {
196 return RuntimeLibcallDefList;
197 }
198
199 ArrayRef<RuntimeLibcallImpl> getRuntimeLibcallImplDefList() const {
200 return RuntimeLibcallImplDefList;
201 }
202
203 const RuntimeLibcall *getRuntimeLibcall(const Record *Def) const {
204 return Def2RuntimeLibcall.lookup(Val: Def);
205 }
206
207 const RuntimeLibcallImpl *getRuntimeLibcallImpl(const Record *Def) const {
208 return Def2RuntimeLibcallImpl.lookup(Val: Def);
209 }
210
211 ArrayRef<const Record *> getFuncArgTypeList() const {
212 return FuncArgTypeList;
213 }
214};
215
216} // namespace llvm
217
218#endif // LLVM_UTILS_TABLEGEN_COMMON_RUNTIMELIBCALLS_H
219