clang 24.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 "CGPointerAuthInfo.h"
18#include "CGValue.h"
19#include "EHScopeStack.h"
20#include "clang/AST/ASTFwd.h"
23#include "clang/AST/Type.h"
26#include "llvm/ADT/STLForwardCompat.h"
27#include "llvm/IR/Value.h"
28
29namespace llvm {
30class Type;
31class Value;
32} // namespace llvm
33
34namespace clang {
35class Decl;
36class FunctionDecl;
37class TargetOptions;
38class VarDecl;
39
40namespace CodeGen {
41
42/// Abstract information about a function or function prototype.
44 /// The function type of the callee.
45 const FunctionType *CalleeFunctionTy;
46 /// The function declaration of the callee.
47 GlobalDecl CalleeDecl;
48
49public:
50 explicit CGCalleeInfo() : CalleeFunctionTy(nullptr) {}
51 CGCalleeInfo(const FunctionType *calleeFunctionTy, GlobalDecl calleeDecl)
52 : CalleeFunctionTy(calleeFunctionTy), CalleeDecl(calleeDecl) {}
53 CGCalleeInfo(const FunctionType *calleeFunctionTy)
54 : CalleeFunctionTy(calleeFunctionTy) {}
56 : CalleeFunctionTy(nullptr), CalleeDecl(calleeDecl) {}
57
58 const FunctionType *getCalleeFunctionType() const { return CalleeFunctionTy; }
60 return dyn_cast_or_null<FunctionProtoType>(CalleeFunctionTy);
61 }
62 const GlobalDecl getCalleeDecl() const { return CalleeDecl; }
63};
64
65/// All available information about a concrete callee.
66class CGCallee {
67 enum class SpecialKind : uintptr_t {
68 Invalid,
69 Builtin,
70 PseudoDestructor,
71 Virtual,
72
73 Last = Virtual
74 };
75
76 struct OrdinaryInfoStorage {
77 CGCalleeInfo AbstractInfo;
78 CGPointerAuthInfo PointerAuthInfo;
79 };
80 struct BuiltinInfoStorage {
81 const FunctionDecl *Decl;
82 unsigned ID;
83 };
84 struct PseudoDestructorInfoStorage {
86 };
87 struct VirtualInfoStorage {
88 const CallExpr *CE;
89 GlobalDecl MD;
91 llvm::FunctionType *FTy;
92 };
93
94 SpecialKind KindOrFunctionPointer;
95 union {
96 OrdinaryInfoStorage OrdinaryInfo;
97 BuiltinInfoStorage BuiltinInfo;
98 PseudoDestructorInfoStorage PseudoDestructorInfo;
99 VirtualInfoStorage VirtualInfo;
100 };
101
102 explicit CGCallee(SpecialKind kind) : KindOrFunctionPointer(kind) {}
103
104 CGCallee(const FunctionDecl *builtinDecl, unsigned builtinID)
105 : KindOrFunctionPointer(SpecialKind::Builtin) {
106 BuiltinInfo.Decl = builtinDecl;
107 BuiltinInfo.ID = builtinID;
108 }
109
110public:
111 CGCallee() : KindOrFunctionPointer(SpecialKind::Invalid) {}
112
113 /// Construct a callee. Call this constructor directly when this
114 /// isn't a direct call.
115 CGCallee(const CGCalleeInfo &abstractInfo, llvm::Value *functionPtr,
116 /* FIXME: make parameter pointerAuthInfo mandatory */
117 const CGPointerAuthInfo &pointerAuthInfo = CGPointerAuthInfo())
118 : KindOrFunctionPointer(
119 SpecialKind(reinterpret_cast<uintptr_t>(functionPtr))) {
120 OrdinaryInfo.AbstractInfo = abstractInfo;
121 OrdinaryInfo.PointerAuthInfo = pointerAuthInfo;
122 assert(functionPtr && "configuring callee without function pointer");
123 assert(functionPtr->getType()->isPointerTy());
124 }
125
126 static CGCallee forBuiltin(unsigned builtinID,
127 const FunctionDecl *builtinDecl) {
128 CGCallee result(SpecialKind::Builtin);
129 result.BuiltinInfo.Decl = builtinDecl;
130 result.BuiltinInfo.ID = builtinID;
131 return result;
132 }
133
135 CGCallee result(SpecialKind::PseudoDestructor);
136 result.PseudoDestructorInfo.Expr = E;
137 return result;
138 }
139
140 static CGCallee forDirect(llvm::Constant *functionPtr,
141 const CGCalleeInfo &abstractInfo = CGCalleeInfo()) {
142 return CGCallee(abstractInfo, functionPtr);
143 }
144
145 static CGCallee forDirect(llvm::FunctionCallee functionPtr,
146 const CGCalleeInfo &abstractInfo = CGCalleeInfo()) {
147 return CGCallee(abstractInfo, functionPtr.getCallee());
148 }
149
150 static CGCallee forVirtual(const CallExpr *CE, GlobalDecl MD, Address Addr,
151 llvm::FunctionType *FTy) {
152 CGCallee result(SpecialKind::Virtual);
153 result.VirtualInfo.CE = CE;
154 result.VirtualInfo.MD = MD;
155 result.VirtualInfo.Addr = Addr;
156 result.VirtualInfo.FTy = FTy;
157 return result;
158 }
159
160 bool isBuiltin() const {
161 return KindOrFunctionPointer == SpecialKind::Builtin;
162 }
164 assert(isBuiltin());
165 return BuiltinInfo.Decl;
166 }
167 unsigned getBuiltinID() const {
168 assert(isBuiltin());
169 return BuiltinInfo.ID;
170 }
171
172 bool isPseudoDestructor() const {
173 return KindOrFunctionPointer == SpecialKind::PseudoDestructor;
174 }
176 assert(isPseudoDestructor());
177 return PseudoDestructorInfo.Expr;
178 }
179
180 bool isOrdinary() const {
181 return uintptr_t(KindOrFunctionPointer) > uintptr_t(SpecialKind::Last);
182 }
184 if (isVirtual())
185 return VirtualInfo.MD;
186 assert(isOrdinary());
187 return OrdinaryInfo.AbstractInfo;
188 }
190 assert(isOrdinary());
191 return OrdinaryInfo.PointerAuthInfo;
192 }
193 llvm::Value *getFunctionPointer() const {
194 assert(isOrdinary());
195 return reinterpret_cast<llvm::Value *>(uintptr_t(KindOrFunctionPointer));
196 }
197 void setFunctionPointer(llvm::Value *functionPtr) {
198 assert(isOrdinary());
199 KindOrFunctionPointer =
200 SpecialKind(reinterpret_cast<uintptr_t>(functionPtr));
201 }
203 assert(isOrdinary());
204 OrdinaryInfo.PointerAuthInfo = PointerAuth;
205 }
206
207 bool isVirtual() const {
208 return KindOrFunctionPointer == SpecialKind::Virtual;
209 }
211 assert(isVirtual());
212 return VirtualInfo.CE;
213 }
215 assert(isVirtual());
216 return VirtualInfo.MD;
217 }
219 assert(isVirtual());
220 return VirtualInfo.Addr;
221 }
222 llvm::FunctionType *getVirtualFunctionType() const {
223 assert(isVirtual());
224 return VirtualInfo.FTy;
225 }
226
227 /// If this is a delayed callee computation of some sort, prepare
228 /// a concrete callee.
230};
231
232struct CallArg {
233private:
234 union {
236 LValue LV; /// The argument is semantically a load from this l-value.
237 };
238 bool HasLV;
239
240 /// A data-flow flag to make sure getRValue and/or copyInto are not
241 /// called twice for duplicated IR emission.
242 mutable bool IsUsed;
243
244public:
247 : RV(rv), HasLV(false), IsUsed(false), Ty(ty) {}
249 : LV(lv), HasLV(true), IsUsed(false), Ty(ty) {}
250 bool hasLValue() const { return HasLV; }
251 QualType getType() const { return Ty; }
252
253 /// \returns an independent RValue. If the CallArg contains an LValue,
254 /// a temporary copy is returned.
255 RValue getRValue(CodeGenFunction &CGF) const;
256
258 assert(HasLV && !IsUsed);
259 return LV;
260 }
262 assert(!HasLV && !IsUsed);
263 return RV;
264 }
265 void setRValue(RValue _RV) {
266 assert(!HasLV);
267 RV = _RV;
268 }
269
270 bool isAggregate() const { return HasLV || RV.isAggregate(); }
271
272 void copyInto(CodeGenFunction &CGF, Address A) const;
273};
274
275/// CallArgList - Type for representing both the value and type of
276/// arguments in a call.
277class CallArgList : public SmallVector<CallArg, 8> {
278public:
279 CallArgList() = default;
280
281 struct Writeback {
282 /// The original argument. Note that the argument l-value
283 /// is potentially null.
285
286 /// The temporary alloca.
288
289 /// A value to "use" after the writeback, or null.
290 llvm::Value *ToUse;
291
292 /// An Expression (optional) that performs the writeback with any required
293 /// casting.
295 };
296
299
300 /// The "is active" insertion point. This instruction is temporary and
301 /// will be removed after insertion.
302 llvm::Instruction *IsActiveIP;
303 };
304
305 void add(RValue rvalue, QualType type) { push_back(CallArg(rvalue, type)); }
306
308 push_back(CallArg(LV, type));
309 }
310
311 /// Add all the arguments from another CallArgList to this one. After doing
312 /// this, the old CallArgList retains its list of arguments, but must not
313 /// be used to emit a call.
314 void addFrom(const CallArgList &other) {
315 llvm::append_range(*this, other);
316 llvm::append_range(Writebacks, other.Writebacks);
317 llvm::append_range(CleanupsToDeactivate, other.CleanupsToDeactivate);
318 assert(!(StackBase && other.StackBase) && "can't merge stackbases");
319 if (!StackBase)
320 StackBase = other.StackBase;
321 }
322
323 void addWriteback(LValue srcLV, Address temporary, llvm::Value *toUse,
324 const Expr *writebackExpr = nullptr) {
325 Writeback writeback = {srcLV, temporary, toUse, writebackExpr};
326 Writebacks.push_back(writeback);
327 }
328
329 bool hasWritebacks() const { return !Writebacks.empty(); }
330
331 typedef llvm::iterator_range<SmallVectorImpl<Writeback>::const_iterator>
333
335 return writeback_const_range(Writebacks.begin(), Writebacks.end());
336 }
337
339 llvm::Instruction *IsActiveIP) {
340 CallArgCleanup ArgCleanup;
341 ArgCleanup.Cleanup = Cleanup;
342 ArgCleanup.IsActiveIP = IsActiveIP;
343 CleanupsToDeactivate.push_back(ArgCleanup);
344 }
345
347 return CleanupsToDeactivate;
348 }
349
351 llvm::Instruction *getStackBase() const { return StackBase; }
352 void freeArgumentMemory(CodeGenFunction &CGF) const;
353
354 /// Returns if we're using an inalloca struct to pass arguments in
355 /// memory.
356 bool isUsingInAlloca() const { return StackBase; }
357
358 // Support reversing writebacks for MSVC ABI.
360 std::reverse(Writebacks.begin(), Writebacks.end());
361 }
362
363private:
364 SmallVector<Writeback, 1> Writebacks;
365
366 /// Deactivate these cleanups immediately before making the call. This
367 /// is used to cleanup objects that are owned by the callee once the call
368 /// occurs.
369 SmallVector<CallArgCleanup, 1> CleanupsToDeactivate;
370
371 /// The stacksave call. It dominates all of the argument evaluation.
372 llvm::CallInst *StackBase = nullptr;
373};
374
375/// FunctionArgList - Type for representing both the decl and type
376/// of parameters to a function. The decl must be either a
377/// ParmVarDecl or ImplicitParamDecl.
378class FunctionArgList : public SmallVector<const VarDecl *, 16> {
379 using SmallVector::SmallVector;
380};
381
382/// ReturnValueSlot - Contains the address where the return value of a
383/// function can be stored, and whether the address is volatile or not.
385 Address Addr = Address::invalid();
386
387 // Return value slot flags
388 LLVM_PREFERRED_TYPE(bool)
389 unsigned IsVolatile : 1;
390 LLVM_PREFERRED_TYPE(bool)
391 unsigned IsUnused : 1;
392 LLVM_PREFERRED_TYPE(bool)
393 unsigned IsExternallyDestructed : 1;
394
395public:
397 : IsVolatile(false), IsUnused(false), IsExternallyDestructed(false) {}
398 ReturnValueSlot(Address Addr, bool IsVolatile, bool IsUnused = false,
399 bool IsExternallyDestructed = false)
400 : Addr(Addr), IsVolatile(IsVolatile), IsUnused(IsUnused),
401 IsExternallyDestructed(IsExternallyDestructed) {}
402
403 bool isNull() const { return !Addr.isValid(); }
404 bool isVolatile() const { return IsVolatile; }
405 Address getValue() const { return Addr; }
406 bool isUnused() const { return IsUnused; }
407 bool isExternallyDestructed() const { return IsExternallyDestructed; }
408 Address getAddress() const { return Addr; }
409};
410
411enum class FnInfoOpts {
412 None = 0,
414 IsChainCall = 1 << 1,
417};
418
427
428} // end namespace CodeGen
429} // end namespace clang
430
431#endif
Forward declaration of all AST node types.
Provides LLVM's BitmaskEnum facility to enumeration types declared in namespace clang.
*collection of selector each with an associated kind and an ordered *collection of selectors A selector has a kind
C Language Family Type Representation.
Represents a C++ pseudo-destructor (C++ [expr.pseudo]).
Definition ExprCXX.h:2749
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition Expr.h:2963
Like RawAddress, an abstract representation of an aligned address, but the pointer contained in this ...
Definition Address.h:128
static Address invalid()
Definition Address.h:176
Abstract information about a function or function prototype.
Definition CGCall.h:43
const GlobalDecl getCalleeDecl() const
Definition CGCall.h:62
CGCalleeInfo(const FunctionType *calleeFunctionTy)
Definition CGCall.h:53
const FunctionProtoType * getCalleeFunctionProtoType() const
Definition CGCall.h:59
CGCalleeInfo(GlobalDecl calleeDecl)
Definition CGCall.h:55
const FunctionType * getCalleeFunctionType() const
Definition CGCall.h:58
CGCalleeInfo(const FunctionType *calleeFunctionTy, GlobalDecl calleeDecl)
Definition CGCall.h:51
All available information about a concrete callee.
Definition CGCall.h:66
CGCalleeInfo getAbstractInfo() const
Definition CGCall.h:183
CGCallee prepareConcreteCallee(CodeGenFunction &CGF) const
If this is a delayed callee computation of some sort, prepare a concrete callee.
Definition CGCall.cpp:6846
VirtualInfoStorage VirtualInfo
Definition CGCall.h:99
void setPointerAuthInfo(CGPointerAuthInfo PointerAuth)
Definition CGCall.h:202
bool isVirtual() const
Definition CGCall.h:207
const CXXPseudoDestructorExpr * getPseudoDestructorExpr() const
Definition CGCall.h:175
bool isOrdinary() const
Definition CGCall.h:180
Address getThisAddress() const
Definition CGCall.h:218
const CallExpr * getVirtualCallExpr() const
Definition CGCall.h:210
CGCallee(const CGCalleeInfo &abstractInfo, llvm::Value *functionPtr, const CGPointerAuthInfo &pointerAuthInfo=CGPointerAuthInfo())
Construct a callee.
Definition CGCall.h:115
BuiltinInfoStorage BuiltinInfo
Definition CGCall.h:97
bool isPseudoDestructor() const
Definition CGCall.h:172
llvm::Value * getFunctionPointer() const
Definition CGCall.h:193
PseudoDestructorInfoStorage PseudoDestructorInfo
Definition CGCall.h:98
static CGCallee forBuiltin(unsigned builtinID, const FunctionDecl *builtinDecl)
Definition CGCall.h:126
unsigned getBuiltinID() const
Definition CGCall.h:167
static CGCallee forVirtual(const CallExpr *CE, GlobalDecl MD, Address Addr, llvm::FunctionType *FTy)
Definition CGCall.h:150
void setFunctionPointer(llvm::Value *functionPtr)
Definition CGCall.h:197
static CGCallee forDirect(llvm::FunctionCallee functionPtr, const CGCalleeInfo &abstractInfo=CGCalleeInfo())
Definition CGCall.h:145
static CGCallee forDirect(llvm::Constant *functionPtr, const CGCalleeInfo &abstractInfo=CGCalleeInfo())
Definition CGCall.h:140
bool isBuiltin() const
Definition CGCall.h:160
llvm::FunctionType * getVirtualFunctionType() const
Definition CGCall.h:222
OrdinaryInfoStorage OrdinaryInfo
Definition CGCall.h:96
const FunctionDecl * getBuiltinDecl() const
Definition CGCall.h:163
const CGPointerAuthInfo & getPointerAuthInfo() const
Definition CGCall.h:189
static CGCallee forPseudoDestructor(const CXXPseudoDestructorExpr *E)
Definition CGCall.h:134
GlobalDecl getVirtualMethodDecl() const
Definition CGCall.h:214
void addWriteback(LValue srcLV, Address temporary, llvm::Value *toUse, const Expr *writebackExpr=nullptr)
Definition CGCall.h:323
llvm::Instruction * getStackBase() const
Definition CGCall.h:351
void addUncopiedAggregate(LValue LV, QualType type)
Definition CGCall.h:307
llvm::iterator_range< SmallVectorImpl< Writeback >::const_iterator > writeback_const_range
Definition CGCall.h:332
void addArgCleanupDeactivation(EHScopeStack::stable_iterator Cleanup, llvm::Instruction *IsActiveIP)
Definition CGCall.h:338
ArrayRef< CallArgCleanup > getCleanupsToDeactivate() const
Definition CGCall.h:346
bool hasWritebacks() const
Definition CGCall.h:329
void add(RValue rvalue, QualType type)
Definition CGCall.h:305
bool isUsingInAlloca() const
Returns if we're using an inalloca struct to pass arguments in memory.
Definition CGCall.h:356
void allocateArgumentMemory(CodeGenFunction &CGF)
Definition CGCall.cpp:4948
void freeArgumentMemory(CodeGenFunction &CGF) const
Definition CGCall.cpp:4955
writeback_const_range writebacks() const
Definition CGCall.h:334
void addFrom(const CallArgList &other)
Add all the arguments from another CallArgList to this one.
Definition CGCall.h:314
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
A saved depth on the scope stack.
FunctionArgList - Type for representing both the decl and type of parameters to a function.
Definition CGCall.h:378
LValue - This represents an lvalue references.
Definition CGValue.h:183
RValue - This trivial value class is used to represent the result of an expression that is evaluated.
Definition CGValue.h:42
bool isExternallyDestructed() const
Definition CGCall.h:407
ReturnValueSlot(Address Addr, bool IsVolatile, bool IsUnused=false, bool IsExternallyDestructed=false)
Definition CGCall.h:398
Address getAddress() const
Definition CGCall.h:408
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
This represents one expression.
Definition Expr.h:113
Represents a function declaration or definition.
Definition Decl.h:2058
Represents a prototype with parameter type info, e.g.
Definition TypeBase.h:5421
FunctionType - C99 6.7.5.3 - Function Declarators.
Definition TypeBase.h:4617
GlobalDecl - represents a global declaration.
Definition GlobalDecl.h:60
A (possibly-)qualified type.
Definition TypeBase.h:938
Options for controlling the target.
Represents a variable declaration or definition.
Definition Decl.h:932
@ Decl
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
Definition CGValue.h:146
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
Top level wrappers for InstallAPI frontend operations.
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
Diagnostic wrappers for TextAPI types for error reporting.
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 false
Definition stdbool.h:26
#define true
Definition stdbool.h:25
llvm::Instruction * IsActiveIP
The "is active" insertion point.
Definition CGCall.h:302
EHScopeStack::stable_iterator Cleanup
Definition CGCall.h:298
llvm::Value * ToUse
A value to "use" after the writeback, or null.
Definition CGCall.h:290
LValue Source
The original argument.
Definition CGCall.h:284
Address Temporary
The temporary alloca.
Definition CGCall.h:287
const Expr * WritebackExpr
An Expression (optional) that performs the writeback with any required casting.
Definition CGCall.h:294
LValue getKnownLValue() const
Definition CGCall.h:257
RValue getKnownRValue() const
Definition CGCall.h:261
QualType getType() const
Definition CGCall.h:251
bool isAggregate() const
Definition CGCall.h:270
CallArg(LValue lv, QualType ty)
Definition CGCall.h:248
void setRValue(RValue _RV)
Definition CGCall.h:265
void copyInto(CodeGenFunction &CGF, Address A) const
Definition CGCall.cpp:5239
CallArg(RValue rv, QualType ty)
Definition CGCall.h:246
bool hasLValue() const
Definition CGCall.h:250
RValue getRValue(CodeGenFunction &CGF) const
Definition CGCall.cpp:5229
DisableDebugLocationUpdates(CodeGenFunction &CGF)
Definition CGCall.cpp:6875
DisableDebugLocationUpdates(const DisableDebugLocationUpdates &)=delete
DisableDebugLocationUpdates & operator=(const DisableDebugLocationUpdates &)=delete