clang 24.0.0git
CIRGenCall.h
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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// These classes wrap the information about a call or function
10// definition used to handle ABI compliancy.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef CLANG_LIB_CODEGEN_CIRGENCALL_H
15#define CLANG_LIB_CODEGEN_CIRGENCALL_H
16
17#include "CIRGenValue.h"
18#include "mlir/IR/Operation.h"
20#include "llvm/ADT/SmallVector.h"
21
22namespace clang::CIRGen {
23
24class CIRGenFunction;
25
26/// Abstract information about a function or function prototype.
28 const clang::FunctionProtoType *calleeProtoTy;
29 clang::GlobalDecl calleeDecl;
30
31public:
32 explicit CIRGenCalleeInfo() : calleeProtoTy(nullptr), calleeDecl() {}
34 clang::GlobalDecl calleeDecl)
35 : calleeProtoTy(calleeProtoTy), calleeDecl(calleeDecl) {}
37 : calleeProtoTy(calleeProtoTy) {}
39 : calleeProtoTy(nullptr), calleeDecl(calleeDecl) {}
40
42 return calleeProtoTy;
43 }
44 clang::GlobalDecl getCalleeDecl() const { return calleeDecl; }
45};
46
47class CIRGenCallee {
48 enum class SpecialKind : uintptr_t {
49 Invalid,
50 Builtin,
51 PseudoDestructor,
52 Virtual,
53
54 Last = Virtual
55 };
56
57 struct BuiltinInfoStorage {
59 unsigned id;
60 };
61 struct PseudoDestructorInfoStorage {
63 };
64 struct VirtualInfoStorage {
65 const clang::CallExpr *ce;
67 Address addr;
68 cir::FuncType fTy;
69 };
70
71 SpecialKind kindOrFunctionPtr;
72
73 union {
75 BuiltinInfoStorage builtinInfo;
76 PseudoDestructorInfoStorage pseudoDestructorInfo;
77 VirtualInfoStorage virtualInfo;
78 };
79
80 explicit CIRGenCallee(SpecialKind kind) : kindOrFunctionPtr(kind) {}
81
82public:
83 CIRGenCallee() : kindOrFunctionPtr(SpecialKind::Invalid) {}
84
85 CIRGenCallee(const CIRGenCalleeInfo &abstractInfo, mlir::Operation *funcPtr)
86 : kindOrFunctionPtr(SpecialKind(reinterpret_cast<uintptr_t>(funcPtr))),
88 assert(funcPtr && "configuring callee without function pointer");
89 }
90
91 static CIRGenCallee
92 forDirect(mlir::Operation *funcPtr,
94 return CIRGenCallee(abstractInfo, funcPtr);
95 }
96
97 bool isInvalid() const { return kindOrFunctionPtr == SpecialKind::Invalid; }
98
99 bool isBuiltin() const { return kindOrFunctionPtr == SpecialKind::Builtin; }
100
102 assert(isBuiltin());
103 return builtinInfo.decl;
104 }
105 unsigned getBuiltinID() const {
106 assert(isBuiltin());
107 return builtinInfo.id;
108 }
109
110 static CIRGenCallee forBuiltin(unsigned builtinID,
111 const clang::FunctionDecl *builtinDecl) {
112 CIRGenCallee result(SpecialKind::Builtin);
113 result.builtinInfo.decl = builtinDecl;
114 result.builtinInfo.id = builtinID;
115 return result;
116 }
117
118 static CIRGenCallee
120 CIRGenCallee result(SpecialKind::PseudoDestructor);
121 result.pseudoDestructorInfo.expr = expr;
122 return result;
123 }
124
125 bool isPseudoDestructor() const {
126 return kindOrFunctionPtr == SpecialKind::PseudoDestructor;
127 }
128
130 assert(isPseudoDestructor());
131 return pseudoDestructorInfo.expr;
132 }
133
134 bool isOrdinary() const {
135 return uintptr_t(kindOrFunctionPtr) > uintptr_t(SpecialKind::Last);
136 }
137
138 /// If this is a delayed callee computation of some sort, prepare a concrete
139 /// callee
141
143 if (isVirtual())
144 return virtualInfo.md;
145 assert(isOrdinary());
146 return abstractInfo;
147 }
148
149 mlir::Operation *getFunctionPointer() const {
150 assert(isOrdinary());
151 return reinterpret_cast<mlir::Operation *>(kindOrFunctionPtr);
152 }
153
154 bool isVirtual() const { return kindOrFunctionPtr == SpecialKind::Virtual; }
155
156 static CIRGenCallee forVirtual(const clang::CallExpr *ce,
157 clang::GlobalDecl md, Address addr,
158 cir::FuncType fTy) {
159 CIRGenCallee result(SpecialKind::Virtual);
160 result.virtualInfo.ce = ce;
161 result.virtualInfo.md = md;
162 result.virtualInfo.addr = addr;
163 result.virtualInfo.fTy = fTy;
164 return result;
165 }
166
168 assert(isVirtual());
169 return virtualInfo.ce;
170 }
171
173 assert(isVirtual());
174 return virtualInfo.md;
175 }
176
178 assert(isVirtual());
179 return virtualInfo.addr;
180 }
181
182 cir::FuncType getVirtualFunctionType() const {
183 assert(isVirtual());
184 return virtualInfo.fTy;
185 }
186
187 void setFunctionPointer(mlir::Operation *functionPtr) {
188 assert(isOrdinary());
189 kindOrFunctionPtr = SpecialKind(reinterpret_cast<uintptr_t>(functionPtr));
190 }
191};
192
193/// Type for representing both the decl and type of parameters to a function.
194/// The decl must be either a ParmVarDecl or ImplicitParamDecl.
195class FunctionArgList : public llvm::SmallVector<const clang::VarDecl *, 16> {};
196
197struct CallArg {
198private:
199 union {
201 LValue lv; // This argument is semantically a load from this l-value
202 };
203 bool hasLV;
204
205 /// A data-flow flag to make sure getRValue and/or copyInto are not
206 /// called twice for duplicated IR emission.
207 mutable bool isUsed;
208
209public:
211
213 : rv(rv), hasLV(false), isUsed(false), ty(ty) {}
214
216 : lv(lv), hasLV(true), isUsed(false), ty(ty) {}
217
218 bool hasLValue() const { return hasLV; }
219
220 /// \returns an independent RValue. If the CallArg contains an LValue,
221 /// a temporary copy is returned.
222 RValue getRValue(CIRGenFunction &cgf, mlir::Location loc) const;
223
225 assert(hasLV && !isUsed);
226 return lv;
227 }
228
230 assert(!hasLV && !isUsed);
231 return rv;
232 }
233
234 bool isAggregate() const { return hasLV || rv.isAggregate(); }
235
236 void copyInto(CIRGenFunction &cgf, Address addr, mlir::Location loc) const;
237};
238
239class CallArgList : public llvm::SmallVector<CallArg, 8> {
240public:
241 void add(RValue rvalue, clang::QualType type) { emplace_back(rvalue, type); }
242
244 emplace_back(lvalue, type);
245 }
246
247 /// Add all the arguments from another CallArgList to this one. After doing
248 /// this, the old CallArgList retains its list of arguments, but must not
249 /// be used to emit a call.
250 void addFrom(const CallArgList &other) {
251 insert(end(), other.begin(), other.end());
252 // Classic codegen has handling for these here. We may not need it here for
253 // CIR, but if not we should implement equivalent handling in lowering.
257 }
258};
259
260/// Contains the address where the return value of a function can be stored, and
261/// whether the address is volatile or not.
263 Address addr = Address::invalid();
264
265public:
266 ReturnValueSlot() = default;
267 ReturnValueSlot(Address addr) : addr(addr) {}
268
269 bool isNull() const { return !addr.isValid(); }
270 Address getValue() const { return addr; }
271};
272
273} // namespace clang::CIRGen
274
275#endif // CLANG_LIB_CODEGEN_CIRGENCALL_H
*collection of selector each with an associated kind and an ordered *collection of selectors A selector has a kind
static Address invalid()
Definition Address.h:76
Abstract information about a function or function prototype.
Definition CIRGenCall.h:27
CIRGenCalleeInfo(clang::GlobalDecl calleeDecl)
Definition CIRGenCall.h:38
clang::GlobalDecl getCalleeDecl() const
Definition CIRGenCall.h:44
CIRGenCalleeInfo(const clang::FunctionProtoType *calleeProtoTy, clang::GlobalDecl calleeDecl)
Definition CIRGenCall.h:33
const clang::FunctionProtoType * getCalleeFunctionProtoType() const
Definition CIRGenCall.h:41
CIRGenCalleeInfo(const clang::FunctionProtoType *calleeProtoTy)
Definition CIRGenCall.h:36
bool isPseudoDestructor() const
Definition CIRGenCall.h:125
void setFunctionPointer(mlir::Operation *functionPtr)
Definition CIRGenCall.h:187
const clang::FunctionDecl * getBuiltinDecl() const
Definition CIRGenCall.h:101
const CXXPseudoDestructorExpr * getPseudoDestructorExpr() const
Definition CIRGenCall.h:129
static CIRGenCallee forDirect(mlir::Operation *funcPtr, const CIRGenCalleeInfo &abstractInfo=CIRGenCalleeInfo())
Definition CIRGenCall.h:92
unsigned getBuiltinID() const
Definition CIRGenCall.h:105
VirtualInfoStorage virtualInfo
Definition CIRGenCall.h:77
CIRGenCalleeInfo getAbstractInfo() const
Definition CIRGenCall.h:142
clang::GlobalDecl getVirtualMethodDecl() const
Definition CIRGenCall.h:172
BuiltinInfoStorage builtinInfo
Definition CIRGenCall.h:75
CIRGenCallee prepareConcreteCallee(CIRGenFunction &cgf) const
If this is a delayed callee computation of some sort, prepare a concrete callee.
Address getThisAddress() const
Definition CIRGenCall.h:177
static CIRGenCallee forBuiltin(unsigned builtinID, const clang::FunctionDecl *builtinDecl)
Definition CIRGenCall.h:110
cir::FuncType getVirtualFunctionType() const
Definition CIRGenCall.h:182
CIRGenCallee(const CIRGenCalleeInfo &abstractInfo, mlir::Operation *funcPtr)
Definition CIRGenCall.h:85
static CIRGenCallee forVirtual(const clang::CallExpr *ce, clang::GlobalDecl md, Address addr, cir::FuncType fTy)
Definition CIRGenCall.h:156
CIRGenCalleeInfo abstractInfo
Definition CIRGenCall.h:74
const clang::CallExpr * getVirtualCallExpr() const
Definition CIRGenCall.h:167
mlir::Operation * getFunctionPointer() const
Definition CIRGenCall.h:149
PseudoDestructorInfoStorage pseudoDestructorInfo
Definition CIRGenCall.h:76
static CIRGenCallee forPseudoDestructor(const clang::CXXPseudoDestructorExpr *expr)
Definition CIRGenCall.h:119
void addFrom(const CallArgList &other)
Add all the arguments from another CallArgList to this one.
Definition CIRGenCall.h:250
void addUncopiedAggregate(LValue lvalue, clang::QualType type)
Definition CIRGenCall.h:243
void add(RValue rvalue, clang::QualType type)
Definition CIRGenCall.h:241
Type for representing both the decl and type of parameters to a function.
Definition CIRGenCall.h:195
This trivial value class is used to represent the result of an expression that is evaluated.
Definition CIRGenValue.h:33
Represents a C++ pseudo-destructor (C++ [expr.pseudo]).
Definition ExprCXX.h:2750
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition Expr.h:3012
Represents a function declaration or definition.
Definition Decl.h:2071
Represents a prototype with parameter type info, e.g.
Definition TypeBase.h:5548
GlobalDecl - represents a global declaration.
Definition GlobalDecl.h:60
A (possibly-)qualified type.
Definition TypeBase.h:938
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
const internal::VariadicAllOfMatcher< Decl > decl
Matches declarations.
const internal::VariadicDynCastAllOfMatcher< Stmt, Expr > expr
Matches expressions.
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
__UINTPTR_TYPE__ uintptr_t
An unsigned integer type with the property that any valid pointer to void can be converted to this ty...
#define false
Definition stdbool.h:26
#define true
Definition stdbool.h:25
static bool cleanupsToDeactivate()
clang::QualType ty
Definition CIRGenCall.h:210
bool isAggregate() const
Definition CIRGenCall.h:234
CallArg(RValue rv, clang::QualType ty)
Definition CIRGenCall.h:212
CallArg(LValue lv, clang::QualType ty)
Definition CIRGenCall.h:215
RValue getRValue(CIRGenFunction &cgf, mlir::Location loc) const
bool hasLValue() const
Definition CIRGenCall.h:218
RValue getKnownRValue() const
Definition CIRGenCall.h:229
void copyInto(CIRGenFunction &cgf, Address addr, mlir::Location loc) const
LValue getKnownLValue() const
Definition CIRGenCall.h:224