clang 23.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 isBuiltin() const { return kindOrFunctionPtr == SpecialKind::Builtin; }
98
100 assert(isBuiltin());
101 return builtinInfo.decl;
102 }
103 unsigned getBuiltinID() const {
104 assert(isBuiltin());
105 return builtinInfo.id;
106 }
107
108 static CIRGenCallee forBuiltin(unsigned builtinID,
109 const clang::FunctionDecl *builtinDecl) {
110 CIRGenCallee result(SpecialKind::Builtin);
111 result.builtinInfo.decl = builtinDecl;
112 result.builtinInfo.id = builtinID;
113 return result;
114 }
115
116 static CIRGenCallee
118 CIRGenCallee result(SpecialKind::PseudoDestructor);
119 result.pseudoDestructorInfo.expr = expr;
120 return result;
121 }
122
123 bool isPseudoDestructor() const {
124 return kindOrFunctionPtr == SpecialKind::PseudoDestructor;
125 }
126
128 assert(isPseudoDestructor());
129 return pseudoDestructorInfo.expr;
130 }
131
132 bool isOrdinary() const {
133 return uintptr_t(kindOrFunctionPtr) > uintptr_t(SpecialKind::Last);
134 }
135
136 /// If this is a delayed callee computation of some sort, prepare a concrete
137 /// callee
139
141 if (isVirtual())
142 return virtualInfo.md;
143 assert(isOrdinary());
144 return abstractInfo;
145 }
146
147 mlir::Operation *getFunctionPointer() const {
148 assert(isOrdinary());
149 return reinterpret_cast<mlir::Operation *>(kindOrFunctionPtr);
150 }
151
152 bool isVirtual() const { return kindOrFunctionPtr == SpecialKind::Virtual; }
153
154 static CIRGenCallee forVirtual(const clang::CallExpr *ce,
155 clang::GlobalDecl md, Address addr,
156 cir::FuncType fTy) {
157 CIRGenCallee result(SpecialKind::Virtual);
158 result.virtualInfo.ce = ce;
159 result.virtualInfo.md = md;
160 result.virtualInfo.addr = addr;
161 result.virtualInfo.fTy = fTy;
162 return result;
163 }
164
166 assert(isVirtual());
167 return virtualInfo.ce;
168 }
169
171 assert(isVirtual());
172 return virtualInfo.md;
173 }
174
176 assert(isVirtual());
177 return virtualInfo.addr;
178 }
179
180 cir::FuncType getVirtualFunctionType() const {
181 assert(isVirtual());
182 return virtualInfo.fTy;
183 }
184
185 void setFunctionPointer(mlir::Operation *functionPtr) {
186 assert(isOrdinary());
187 kindOrFunctionPtr = SpecialKind(reinterpret_cast<uintptr_t>(functionPtr));
188 }
189};
190
191/// Type for representing both the decl and type of parameters to a function.
192/// The decl must be either a ParmVarDecl or ImplicitParamDecl.
193class FunctionArgList : public llvm::SmallVector<const clang::VarDecl *, 16> {};
194
195struct CallArg {
196private:
197 union {
199 LValue lv; // This argument is semantically a load from this l-value
200 };
201 bool hasLV;
202
203 /// A data-flow flag to make sure getRValue and/or copyInto are not
204 /// called twice for duplicated IR emission.
205 [[maybe_unused]] mutable bool isUsed;
206
207public:
209
211 : rv(rv), hasLV(false), isUsed(false), ty(ty) {}
212
214 : lv(lv), hasLV(true), isUsed(false), ty(ty) {}
215
216 bool hasLValue() const { return hasLV; }
217
219 assert(hasLV && !isUsed);
220 return lv;
221 }
222
224 assert(!hasLV && !isUsed);
225 return rv;
226 }
227
228 bool isAggregate() const { return hasLV || rv.isAggregate(); }
229
230 void copyInto(CIRGenFunction &cgf, Address addr, mlir::Location loc) const;
231};
232
233class CallArgList : public llvm::SmallVector<CallArg, 8> {
234public:
235 void add(RValue rvalue, clang::QualType type) { emplace_back(rvalue, type); }
236
238 emplace_back(lvalue, type);
239 }
240
241 /// Add all the arguments from another CallArgList to this one. After doing
242 /// this, the old CallArgList retains its list of arguments, but must not
243 /// be used to emit a call.
244 void addFrom(const CallArgList &other) {
245 insert(end(), other.begin(), other.end());
246 // Classic codegen has handling for these here. We may not need it here for
247 // CIR, but if not we should implement equivalent handling in lowering.
251 }
252};
253
254/// Contains the address where the return value of a function can be stored, and
255/// whether the address is volatile or not.
257 Address addr = Address::invalid();
258
259public:
260 ReturnValueSlot() = default;
261 ReturnValueSlot(Address addr) : addr(addr) {}
262
263 bool isNull() const { return !addr.isValid(); }
264 Address getValue() const { return addr; }
265};
266
267} // namespace clang::CIRGen
268
269#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:73
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:123
void setFunctionPointer(mlir::Operation *functionPtr)
Definition CIRGenCall.h:185
const clang::FunctionDecl * getBuiltinDecl() const
Definition CIRGenCall.h:99
const CXXPseudoDestructorExpr * getPseudoDestructorExpr() const
Definition CIRGenCall.h:127
static CIRGenCallee forDirect(mlir::Operation *funcPtr, const CIRGenCalleeInfo &abstractInfo=CIRGenCalleeInfo())
Definition CIRGenCall.h:92
unsigned getBuiltinID() const
Definition CIRGenCall.h:103
VirtualInfoStorage virtualInfo
Definition CIRGenCall.h:77
CIRGenCalleeInfo getAbstractInfo() const
Definition CIRGenCall.h:140
clang::GlobalDecl getVirtualMethodDecl() const
Definition CIRGenCall.h:170
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:175
static CIRGenCallee forBuiltin(unsigned builtinID, const clang::FunctionDecl *builtinDecl)
Definition CIRGenCall.h:108
cir::FuncType getVirtualFunctionType() const
Definition CIRGenCall.h:180
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:154
CIRGenCalleeInfo abstractInfo
Definition CIRGenCall.h:74
const clang::CallExpr * getVirtualCallExpr() const
Definition CIRGenCall.h:165
mlir::Operation * getFunctionPointer() const
Definition CIRGenCall.h:147
PseudoDestructorInfoStorage pseudoDestructorInfo
Definition CIRGenCall.h:76
static CIRGenCallee forPseudoDestructor(const clang::CXXPseudoDestructorExpr *expr)
Definition CIRGenCall.h:117
void addFrom(const CallArgList &other)
Add all the arguments from another CallArgList to this one.
Definition CIRGenCall.h:244
void addUncopiedAggregate(LValue lvalue, clang::QualType type)
Definition CIRGenCall.h:237
void add(RValue rvalue, clang::QualType type)
Definition CIRGenCall.h:235
Type for representing both the decl and type of parameters to a function.
Definition CIRGenCall.h:193
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:2745
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition Expr.h:2943
Represents a function declaration or definition.
Definition Decl.h:2000
Represents a prototype with parameter type info, e.g.
Definition TypeBase.h:5269
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.
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:208
bool isAggregate() const
Definition CIRGenCall.h:228
CallArg(RValue rv, clang::QualType ty)
Definition CIRGenCall.h:210
CallArg(LValue lv, clang::QualType ty)
Definition CIRGenCall.h:213
bool hasLValue() const
Definition CIRGenCall.h:216
RValue getKnownRValue() const
Definition CIRGenCall.h:223
void copyInto(CIRGenFunction &cgf, Address addr, mlir::Location loc) const
LValue getKnownLValue() const
Definition CIRGenCall.h:218