clang 17.0.0git
CGCall.h
Go to the documentation of this file.
1//===----- CGCall.h - Encapsulate calling convention details ----*- 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// These classes wrap the information about a call or function
10// definition used to handle ABI compliancy.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_LIB_CODEGEN_CGCALL_H
15#define LLVM_CLANG_LIB_CODEGEN_CGCALL_H
16
17#include "CGValue.h"
18#include "EHScopeStack.h"
19#include "clang/AST/ASTFwd.h"
22#include "clang/AST/Type.h"
23#include "llvm/IR/Value.h"
24
25namespace llvm {
26class Type;
27class Value;
28} // namespace llvm
29
30namespace clang {
31class Decl;
32class FunctionDecl;
33class VarDecl;
34
35namespace CodeGen {
36
37/// Abstract information about a function or function prototype.
39 /// The function prototype of the callee.
40 const FunctionProtoType *CalleeProtoTy;
41 /// The function declaration of the callee.
42 GlobalDecl CalleeDecl;
43
44public:
45 explicit CGCalleeInfo() : CalleeProtoTy(nullptr) {}
46 CGCalleeInfo(const FunctionProtoType *calleeProtoTy, GlobalDecl calleeDecl)
47 : CalleeProtoTy(calleeProtoTy), CalleeDecl(calleeDecl) {}
48 CGCalleeInfo(const FunctionProtoType *calleeProtoTy)
49 : CalleeProtoTy(calleeProtoTy) {}
51 : CalleeProtoTy(nullptr), CalleeDecl(calleeDecl) {}
52
54 return CalleeProtoTy;
55 }
56 const GlobalDecl getCalleeDecl() const { return CalleeDecl; }
57};
58
59/// All available information about a concrete callee.
60class CGCallee {
61 enum class SpecialKind : uintptr_t {
62 Invalid,
63 Builtin,
64 PseudoDestructor,
65 Virtual,
66
68 };
69
70 struct BuiltinInfoStorage {
71 const FunctionDecl *Decl;
72 unsigned ID;
73 };
74 struct PseudoDestructorInfoStorage {
76 };
77 struct VirtualInfoStorage {
78 const CallExpr *CE;
79 GlobalDecl MD;
80 Address Addr;
81 llvm::FunctionType *FTy;
82 };
83
84 SpecialKind KindOrFunctionPointer;
85 union {
87 BuiltinInfoStorage BuiltinInfo;
88 PseudoDestructorInfoStorage PseudoDestructorInfo;
89 VirtualInfoStorage VirtualInfo;
90 };
91
92 explicit CGCallee(SpecialKind kind) : KindOrFunctionPointer(kind) {}
93
94 CGCallee(const FunctionDecl *builtinDecl, unsigned builtinID)
95 : KindOrFunctionPointer(SpecialKind::Builtin) {
96 BuiltinInfo.Decl = builtinDecl;
97 BuiltinInfo.ID = builtinID;
98 }
99
100public:
101 CGCallee() : KindOrFunctionPointer(SpecialKind::Invalid) {}
102
103 /// Construct a callee. Call this constructor directly when this
104 /// isn't a direct call.
105 CGCallee(const CGCalleeInfo &abstractInfo, llvm::Value *functionPtr)
106 : KindOrFunctionPointer(
107 SpecialKind(reinterpret_cast<uintptr_t>(functionPtr))) {
108 AbstractInfo = abstractInfo;
109 assert(functionPtr && "configuring callee without function pointer");
110 assert(functionPtr->getType()->isPointerTy());
111 assert(functionPtr->getType()->isOpaquePointerTy() ||
112 functionPtr->getType()->getNonOpaquePointerElementType()
113 ->isFunctionTy());
114 }
115
116 static CGCallee forBuiltin(unsigned builtinID,
117 const FunctionDecl *builtinDecl) {
118 CGCallee result(SpecialKind::Builtin);
119 result.BuiltinInfo.Decl = builtinDecl;
120 result.BuiltinInfo.ID = builtinID;
121 return result;
122 }
123
125 CGCallee result(SpecialKind::PseudoDestructor);
126 result.PseudoDestructorInfo.Expr = E;
127 return result;
128 }
129
130 static CGCallee forDirect(llvm::Constant *functionPtr,
131 const CGCalleeInfo &abstractInfo = CGCalleeInfo()) {
132 return CGCallee(abstractInfo, functionPtr);
133 }
134
135 static CGCallee forDirect(llvm::FunctionCallee functionPtr,
136 const CGCalleeInfo &abstractInfo = CGCalleeInfo()) {
137 return CGCallee(abstractInfo, functionPtr.getCallee());
138 }
139
140 static CGCallee forVirtual(const CallExpr *CE, GlobalDecl MD, Address Addr,
141 llvm::FunctionType *FTy) {
142 CGCallee result(SpecialKind::Virtual);
143 result.VirtualInfo.CE = CE;
144 result.VirtualInfo.MD = MD;
145 result.VirtualInfo.Addr = Addr;
146 result.VirtualInfo.FTy = FTy;
147 return result;
148 }
149
150 bool isBuiltin() const {
151 return KindOrFunctionPointer == SpecialKind::Builtin;
152 }
154 assert(isBuiltin());
155 return BuiltinInfo.Decl;
156 }
157 unsigned getBuiltinID() const {
158 assert(isBuiltin());
159 return BuiltinInfo.ID;
160 }
161
162 bool isPseudoDestructor() const {
163 return KindOrFunctionPointer == SpecialKind::PseudoDestructor;
164 }
166 assert(isPseudoDestructor());
167 return PseudoDestructorInfo.Expr;
168 }
169
170 bool isOrdinary() const {
171 return uintptr_t(KindOrFunctionPointer) > uintptr_t(SpecialKind::Last);
172 }
174 if (isVirtual())
175 return VirtualInfo.MD;
176 assert(isOrdinary());
177 return AbstractInfo;
178 }
179 llvm::Value *getFunctionPointer() const {
180 assert(isOrdinary());
181 return reinterpret_cast<llvm::Value *>(uintptr_t(KindOrFunctionPointer));
182 }
183 void setFunctionPointer(llvm::Value *functionPtr) {
184 assert(isOrdinary());
185 KindOrFunctionPointer =
186 SpecialKind(reinterpret_cast<uintptr_t>(functionPtr));
187 }
188
189 bool isVirtual() const {
190 return KindOrFunctionPointer == SpecialKind::Virtual;
191 }
193 assert(isVirtual());
194 return VirtualInfo.CE;
195 }
197 assert(isVirtual());
198 return VirtualInfo.MD;
199 }
201 assert(isVirtual());
202 return VirtualInfo.Addr;
203 }
204 llvm::FunctionType *getVirtualFunctionType() const {
205 assert(isVirtual());
206 return VirtualInfo.FTy;
207 }
208
209 /// If this is a delayed callee computation of some sort, prepare
210 /// a concrete callee.
212};
213
214struct CallArg {
215private:
216 union {
218 LValue LV; /// The argument is semantically a load from this l-value.
219 };
220 bool HasLV;
221
222 /// A data-flow flag to make sure getRValue and/or copyInto are not
223 /// called twice for duplicated IR emission.
224 mutable bool IsUsed;
225
226public:
229 : RV(rv), HasLV(false), IsUsed(false), Ty(ty) {}
231 : LV(lv), HasLV(true), IsUsed(false), Ty(ty) {}
232 bool hasLValue() const { return HasLV; }
233 QualType getType() const { return Ty; }
234
235 /// \returns an independent RValue. If the CallArg contains an LValue,
236 /// a temporary copy is returned.
237 RValue getRValue(CodeGenFunction &CGF) const;
238
240 assert(HasLV && !IsUsed);
241 return LV;
242 }
244 assert(!HasLV && !IsUsed);
245 return RV;
246 }
247 void setRValue(RValue _RV) {
248 assert(!HasLV);
249 RV = _RV;
250 }
251
252 bool isAggregate() const { return HasLV || RV.isAggregate(); }
253
254 void copyInto(CodeGenFunction &CGF, Address A) const;
255};
256
257/// CallArgList - Type for representing both the value and type of
258/// arguments in a call.
259class CallArgList : public SmallVector<CallArg, 8> {
260public:
261 CallArgList() : StackBase(nullptr) {}
262
263 struct Writeback {
264 /// The original argument. Note that the argument l-value
265 /// is potentially null.
267
268 /// The temporary alloca.
270
271 /// A value to "use" after the writeback, or null.
272 llvm::Value *ToUse;
273 };
274
277
278 /// The "is active" insertion point. This instruction is temporary and
279 /// will be removed after insertion.
280 llvm::Instruction *IsActiveIP;
281 };
282
283 void add(RValue rvalue, QualType type) { push_back(CallArg(rvalue, type)); }
284
286 push_back(CallArg(LV, type));
287 }
288
289 /// Add all the arguments from another CallArgList to this one. After doing
290 /// this, the old CallArgList retains its list of arguments, but must not
291 /// be used to emit a call.
292 void addFrom(const CallArgList &other) {
293 insert(end(), other.begin(), other.end());
294 Writebacks.insert(Writebacks.end(), other.Writebacks.begin(),
295 other.Writebacks.end());
296 CleanupsToDeactivate.insert(CleanupsToDeactivate.end(),
297 other.CleanupsToDeactivate.begin(),
298 other.CleanupsToDeactivate.end());
299 assert(!(StackBase && other.StackBase) && "can't merge stackbases");
300 if (!StackBase)
301 StackBase = other.StackBase;
302 }
303
304 void addWriteback(LValue srcLV, Address temporary, llvm::Value *toUse) {
305 Writeback writeback = {srcLV, temporary, toUse};
306 Writebacks.push_back(writeback);
307 }
308
309 bool hasWritebacks() const { return !Writebacks.empty(); }
310
311 typedef llvm::iterator_range<SmallVectorImpl<Writeback>::const_iterator>
313
315 return writeback_const_range(Writebacks.begin(), Writebacks.end());
316 }
317
319 llvm::Instruction *IsActiveIP) {
320 CallArgCleanup ArgCleanup;
321 ArgCleanup.Cleanup = Cleanup;
322 ArgCleanup.IsActiveIP = IsActiveIP;
323 CleanupsToDeactivate.push_back(ArgCleanup);
324 }
325
327 return CleanupsToDeactivate;
328 }
329
331 llvm::Instruction *getStackBase() const { return StackBase; }
332 void freeArgumentMemory(CodeGenFunction &CGF) const;
333
334 /// Returns if we're using an inalloca struct to pass arguments in
335 /// memory.
336 bool isUsingInAlloca() const { return StackBase; }
337
338private:
339 SmallVector<Writeback, 1> Writebacks;
340
341 /// Deactivate these cleanups immediately before making the call. This
342 /// is used to cleanup objects that are owned by the callee once the call
343 /// occurs.
344 SmallVector<CallArgCleanup, 1> CleanupsToDeactivate;
345
346 /// The stacksave call. It dominates all of the argument evaluation.
347 llvm::CallInst *StackBase;
348};
349
350/// FunctionArgList - Type for representing both the decl and type
351/// of parameters to a function. The decl must be either a
352/// ParmVarDecl or ImplicitParamDecl.
353class FunctionArgList : public SmallVector<const VarDecl *, 16> {};
354
355/// ReturnValueSlot - Contains the address where the return value of a
356/// function can be stored, and whether the address is volatile or not.
358 Address Addr = Address::invalid();
359
360 // Return value slot flags
361 unsigned IsVolatile : 1;
362 unsigned IsUnused : 1;
363 unsigned IsExternallyDestructed : 1;
364
365public:
367 : IsVolatile(false), IsUnused(false), IsExternallyDestructed(false) {}
368 ReturnValueSlot(Address Addr, bool IsVolatile, bool IsUnused = false,
369 bool IsExternallyDestructed = false)
370 : Addr(Addr), IsVolatile(IsVolatile), IsUnused(IsUnused),
371 IsExternallyDestructed(IsExternallyDestructed) {}
372
373 bool isNull() const { return !Addr.isValid(); }
374 bool isVolatile() const { return IsVolatile; }
375 Address getValue() const { return Addr; }
376 bool isUnused() const { return IsUnused; }
377 bool isExternallyDestructed() const { return IsExternallyDestructed; }
378};
379
380} // end namespace CodeGen
381} // end namespace clang
382
383#endif
Forward declaration of all AST node types.
MatchType Type
C Language Family Type Representation.
Represents a C++ pseudo-destructor (C++ [expr.pseudo]).
Definition: ExprCXX.h:2592
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition: Expr.h:2812
An aligned address.
Definition: Address.h:29
static Address invalid()
Definition: Address.h:49
bool isValid() const
Definition: Address.h:50
Abstract information about a function or function prototype.
Definition: CGCall.h:38
const GlobalDecl getCalleeDecl() const
Definition: CGCall.h:56
CGCalleeInfo(const FunctionProtoType *calleeProtoTy)
Definition: CGCall.h:48
CGCalleeInfo(const FunctionProtoType *calleeProtoTy, GlobalDecl calleeDecl)
Definition: CGCall.h:46
const FunctionProtoType * getCalleeFunctionProtoType() const
Definition: CGCall.h:53
CGCalleeInfo(GlobalDecl calleeDecl)
Definition: CGCall.h:50
All available information about a concrete callee.
Definition: CGCall.h:60
CGCalleeInfo getAbstractInfo() const
Definition: CGCall.h:173
CGCallee prepareConcreteCallee(CodeGenFunction &CGF) const
If this is a delayed callee computation of some sort, prepare a concrete callee.
Definition: CGCall.cpp:5712
VirtualInfoStorage VirtualInfo
Definition: CGCall.h:89
bool isVirtual() const
Definition: CGCall.h:189
const CXXPseudoDestructorExpr * getPseudoDestructorExpr() const
Definition: CGCall.h:165
bool isOrdinary() const
Definition: CGCall.h:170
Address getThisAddress() const
Definition: CGCall.h:200
const CallExpr * getVirtualCallExpr() const
Definition: CGCall.h:192
BuiltinInfoStorage BuiltinInfo
Definition: CGCall.h:87
bool isPseudoDestructor() const
Definition: CGCall.h:162
llvm::Value * getFunctionPointer() const
Definition: CGCall.h:179
PseudoDestructorInfoStorage PseudoDestructorInfo
Definition: CGCall.h:88
static CGCallee forBuiltin(unsigned builtinID, const FunctionDecl *builtinDecl)
Definition: CGCall.h:116
unsigned getBuiltinID() const
Definition: CGCall.h:157
CGCallee(const CGCalleeInfo &abstractInfo, llvm::Value *functionPtr)
Construct a callee.
Definition: CGCall.h:105
static CGCallee forVirtual(const CallExpr *CE, GlobalDecl MD, Address Addr, llvm::FunctionType *FTy)
Definition: CGCall.h:140
void setFunctionPointer(llvm::Value *functionPtr)
Definition: CGCall.h:183
static CGCallee forDirect(llvm::FunctionCallee functionPtr, const CGCalleeInfo &abstractInfo=CGCalleeInfo())
Definition: CGCall.h:135
static CGCallee forDirect(llvm::Constant *functionPtr, const CGCalleeInfo &abstractInfo=CGCalleeInfo())
Definition: CGCall.h:130
bool isBuiltin() const
Definition: CGCall.h:150
llvm::FunctionType * getVirtualFunctionType() const
Definition: CGCall.h:204
const FunctionDecl * getBuiltinDecl() const
Definition: CGCall.h:153
static CGCallee forPseudoDestructor(const CXXPseudoDestructorExpr *E)
Definition: CGCall.h:124
GlobalDecl getVirtualMethodDecl() const
Definition: CGCall.h:196
CGCalleeInfo AbstractInfo
Definition: CGCall.h:86
CallArgList - Type for representing both the value and type of arguments in a call.
Definition: CGCall.h:259
llvm::Instruction * getStackBase() const
Definition: CGCall.h:331
void addUncopiedAggregate(LValue LV, QualType type)
Definition: CGCall.h:285
llvm::iterator_range< SmallVectorImpl< Writeback >::const_iterator > writeback_const_range
Definition: CGCall.h:312
void addArgCleanupDeactivation(EHScopeStack::stable_iterator Cleanup, llvm::Instruction *IsActiveIP)
Definition: CGCall.h:318
ArrayRef< CallArgCleanup > getCleanupsToDeactivate() const
Definition: CGCall.h:326
bool hasWritebacks() const
Definition: CGCall.h:309
void add(RValue rvalue, QualType type)
Definition: CGCall.h:283
bool isUsingInAlloca() const
Returns if we're using an inalloca struct to pass arguments in memory.
Definition: CGCall.h:336
void allocateArgumentMemory(CodeGenFunction &CGF)
Definition: CGCall.cpp:4131
void freeArgumentMemory(CodeGenFunction &CGF) const
Definition: CGCall.cpp:4139
writeback_const_range writebacks() const
Definition: CGCall.h:314
void addWriteback(LValue srcLV, Address temporary, llvm::Value *toUse)
Definition: CGCall.h:304
void addFrom(const CallArgList &other)
Add all the arguments from another CallArgList to this one.
Definition: CGCall.h:292
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
A saved depth on the scope stack.
Definition: EHScopeStack.h:101
FunctionArgList - Type for representing both the decl and type of parameters to a function.
Definition: CGCall.h:353
LValue - This represents an lvalue references.
Definition: CGValue.h:171
RValue - This trivial value class is used to represent the result of an expression that is evaluated.
Definition: CGValue.h:39
bool isAggregate() const
Definition: CGValue.h:56
ReturnValueSlot - Contains the address where the return value of a function can be stored,...
Definition: CGCall.h:357
Address getValue() const
Definition: CGCall.h:375
bool isExternallyDestructed() const
Definition: CGCall.h:377
ReturnValueSlot(Address Addr, bool IsVolatile, bool IsUnused=false, bool IsExternallyDestructed=false)
Definition: CGCall.h:368
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:83
This represents one expression.
Definition: Expr.h:110
Represents a function declaration or definition.
Definition: Decl.h:1917
Represents a prototype with parameter type info, e.g.
Definition: Type.h:4041
GlobalDecl - represents a global declaration.
Definition: GlobalDecl.h:56
A (possibly-)qualified type.
Definition: Type.h:736
@ Decl
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
YAML serialization mapping.
Definition: Dominators.h:30
__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:21
#define false
Definition: stdbool.h:22
llvm::Instruction * IsActiveIP
The "is active" insertion point.
Definition: CGCall.h:280
EHScopeStack::stable_iterator Cleanup
Definition: CGCall.h:276
llvm::Value * ToUse
A value to "use" after the writeback, or null.
Definition: CGCall.h:272
LValue Source
The original argument.
Definition: CGCall.h:266
Address Temporary
The temporary alloca.
Definition: CGCall.h:269
LValue getKnownLValue() const
Definition: CGCall.h:239
RValue getKnownRValue() const
Definition: CGCall.h:243
QualType getType() const
Definition: CGCall.h:233
bool isAggregate() const
Definition: CGCall.h:252
CallArg(LValue lv, QualType ty)
Definition: CGCall.h:230
void setRValue(RValue _RV)
Definition: CGCall.h:247
void copyInto(CodeGenFunction &CGF, Address A) const
Definition: CGCall.cpp:4422
CallArg(RValue rv, QualType ty)
Definition: CGCall.h:228
bool hasLValue() const
Definition: CGCall.h:232
RValue getRValue(CodeGenFunction &CGF) const
Definition: CGCall.cpp:4412