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