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