clang 19.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/ADT/STLForwardCompat.h"
24#include "llvm/IR/Value.h"
25
26namespace llvm {
27class Type;
28class Value;
29} // namespace llvm
30
31namespace clang {
32class Decl;
33class FunctionDecl;
34class TargetOptions;
35class VarDecl;
36
37namespace CodeGen {
38
39/// Abstract information about a function or function prototype.
41 /// The function prototype of the callee.
42 const FunctionProtoType *CalleeProtoTy;
43 /// The function declaration of the callee.
44 GlobalDecl CalleeDecl;
45
46public:
47 explicit CGCalleeInfo() : CalleeProtoTy(nullptr) {}
48 CGCalleeInfo(const FunctionProtoType *calleeProtoTy, GlobalDecl calleeDecl)
49 : CalleeProtoTy(calleeProtoTy), CalleeDecl(calleeDecl) {}
50 CGCalleeInfo(const FunctionProtoType *calleeProtoTy)
51 : CalleeProtoTy(calleeProtoTy) {}
53 : CalleeProtoTy(nullptr), CalleeDecl(calleeDecl) {}
54
56 return CalleeProtoTy;
57 }
58 const GlobalDecl getCalleeDecl() const { return CalleeDecl; }
59};
60
61/// All available information about a concrete callee.
62class CGCallee {
63 enum class SpecialKind : uintptr_t {
64 Invalid,
65 Builtin,
66 PseudoDestructor,
67 Virtual,
68
70 };
71
72 struct BuiltinInfoStorage {
73 const FunctionDecl *Decl;
74 unsigned ID;
75 };
76 struct PseudoDestructorInfoStorage {
78 };
79 struct VirtualInfoStorage {
80 const CallExpr *CE;
81 GlobalDecl MD;
82 Address Addr;
83 llvm::FunctionType *FTy;
84 };
85
86 SpecialKind KindOrFunctionPointer;
87 union {
89 BuiltinInfoStorage BuiltinInfo;
90 PseudoDestructorInfoStorage PseudoDestructorInfo;
91 VirtualInfoStorage VirtualInfo;
92 };
93
94 explicit CGCallee(SpecialKind kind) : KindOrFunctionPointer(kind) {}
95
96 CGCallee(const FunctionDecl *builtinDecl, unsigned builtinID)
97 : KindOrFunctionPointer(SpecialKind::Builtin) {
98 BuiltinInfo.Decl = builtinDecl;
99 BuiltinInfo.ID = builtinID;
100 }
101
102public:
103 CGCallee() : KindOrFunctionPointer(SpecialKind::Invalid) {}
104
105 /// Construct a callee. Call this constructor directly when this
106 /// isn't a direct call.
107 CGCallee(const CGCalleeInfo &abstractInfo, llvm::Value *functionPtr)
108 : KindOrFunctionPointer(
109 SpecialKind(reinterpret_cast<uintptr_t>(functionPtr))) {
110 AbstractInfo = abstractInfo;
111 assert(functionPtr && "configuring callee without function pointer");
112 assert(functionPtr->getType()->isPointerTy());
113 }
114
115 static CGCallee forBuiltin(unsigned builtinID,
116 const FunctionDecl *builtinDecl) {
117 CGCallee result(SpecialKind::Builtin);
118 result.BuiltinInfo.Decl = builtinDecl;
119 result.BuiltinInfo.ID = builtinID;
120 return result;
121 }
122
124 CGCallee result(SpecialKind::PseudoDestructor);
125 result.PseudoDestructorInfo.Expr = E;
126 return result;
127 }
128
129 static CGCallee forDirect(llvm::Constant *functionPtr,
130 const CGCalleeInfo &abstractInfo = CGCalleeInfo()) {
131 return CGCallee(abstractInfo, functionPtr);
132 }
133
134 static CGCallee forDirect(llvm::FunctionCallee functionPtr,
135 const CGCalleeInfo &abstractInfo = CGCalleeInfo()) {
136 return CGCallee(abstractInfo, functionPtr.getCallee());
137 }
138
139 static CGCallee forVirtual(const CallExpr *CE, GlobalDecl MD, Address Addr,
140 llvm::FunctionType *FTy) {
141 CGCallee result(SpecialKind::Virtual);
142 result.VirtualInfo.CE = CE;
143 result.VirtualInfo.MD = MD;
144 result.VirtualInfo.Addr = Addr;
145 result.VirtualInfo.FTy = FTy;
146 return result;
147 }
148
149 bool isBuiltin() const {
150 return KindOrFunctionPointer == SpecialKind::Builtin;
151 }
153 assert(isBuiltin());
154 return BuiltinInfo.Decl;
155 }
156 unsigned getBuiltinID() const {
157 assert(isBuiltin());
158 return BuiltinInfo.ID;
159 }
160
161 bool isPseudoDestructor() const {
162 return KindOrFunctionPointer == SpecialKind::PseudoDestructor;
163 }
165 assert(isPseudoDestructor());
166 return PseudoDestructorInfo.Expr;
167 }
168
169 bool isOrdinary() const {
170 return uintptr_t(KindOrFunctionPointer) > uintptr_t(SpecialKind::Last);
171 }
173 if (isVirtual())
174 return VirtualInfo.MD;
175 assert(isOrdinary());
176 return AbstractInfo;
177 }
178 llvm::Value *getFunctionPointer() const {
179 assert(isOrdinary());
180 return reinterpret_cast<llvm::Value *>(uintptr_t(KindOrFunctionPointer));
181 }
182 void setFunctionPointer(llvm::Value *functionPtr) {
183 assert(isOrdinary());
184 KindOrFunctionPointer =
185 SpecialKind(reinterpret_cast<uintptr_t>(functionPtr));
186 }
187
188 bool isVirtual() const {
189 return KindOrFunctionPointer == SpecialKind::Virtual;
190 }
192 assert(isVirtual());
193 return VirtualInfo.CE;
194 }
196 assert(isVirtual());
197 return VirtualInfo.MD;
198 }
200 assert(isVirtual());
201 return VirtualInfo.Addr;
202 }
203 llvm::FunctionType *getVirtualFunctionType() const {
204 assert(isVirtual());
205 return VirtualInfo.FTy;
206 }
207
208 /// If this is a delayed callee computation of some sort, prepare
209 /// a concrete callee.
211};
212
213struct CallArg {
214private:
215 union {
217 LValue LV; /// The argument is semantically a load from this l-value.
218 };
219 bool HasLV;
220
221 /// A data-flow flag to make sure getRValue and/or copyInto are not
222 /// called twice for duplicated IR emission.
223 mutable bool IsUsed;
224
225public:
228 : RV(rv), HasLV(false), IsUsed(false), Ty(ty) {}
230 : LV(lv), HasLV(true), IsUsed(false), Ty(ty) {}
231 bool hasLValue() const { return HasLV; }
232 QualType getType() const { return Ty; }
233
234 /// \returns an independent RValue. If the CallArg contains an LValue,
235 /// a temporary copy is returned.
236 RValue getRValue(CodeGenFunction &CGF) const;
237
239 assert(HasLV && !IsUsed);
240 return LV;
241 }
243 assert(!HasLV && !IsUsed);
244 return RV;
245 }
246 void setRValue(RValue _RV) {
247 assert(!HasLV);
248 RV = _RV;
249 }
250
251 bool isAggregate() const { return HasLV || RV.isAggregate(); }
252
253 void copyInto(CodeGenFunction &CGF, Address A) const;
254};
255
256/// CallArgList - Type for representing both the value and type of
257/// arguments in a call.
258class CallArgList : public SmallVector<CallArg, 8> {
259public:
260 CallArgList() = default;
261
262 struct Writeback {
263 /// The original argument. Note that the argument l-value
264 /// is potentially null.
266
267 /// The temporary alloca.
269
270 /// A value to "use" after the writeback, or null.
271 llvm::Value *ToUse;
272 };
273
276
277 /// The "is active" insertion point. This instruction is temporary and
278 /// will be removed after insertion.
279 llvm::Instruction *IsActiveIP;
280 };
281
282 void add(RValue rvalue, QualType type) { push_back(CallArg(rvalue, type)); }
283
285 push_back(CallArg(LV, type));
286 }
287
288 /// Add all the arguments from another CallArgList to this one. After doing
289 /// this, the old CallArgList retains its list of arguments, but must not
290 /// be used to emit a call.
291 void addFrom(const CallArgList &other) {
292 insert(end(), other.begin(), other.end());
293 Writebacks.insert(Writebacks.end(), other.Writebacks.begin(),
294 other.Writebacks.end());
295 CleanupsToDeactivate.insert(CleanupsToDeactivate.end(),
296 other.CleanupsToDeactivate.begin(),
297 other.CleanupsToDeactivate.end());
298 assert(!(StackBase && other.StackBase) && "can't merge stackbases");
299 if (!StackBase)
300 StackBase = other.StackBase;
301 }
302
303 void addWriteback(LValue srcLV, Address temporary, llvm::Value *toUse) {
304 Writeback writeback = {srcLV, temporary, toUse};
305 Writebacks.push_back(writeback);
306 }
307
308 bool hasWritebacks() const { return !Writebacks.empty(); }
309
310 typedef llvm::iterator_range<SmallVectorImpl<Writeback>::const_iterator>
312
314 return writeback_const_range(Writebacks.begin(), Writebacks.end());
315 }
316
318 llvm::Instruction *IsActiveIP) {
319 CallArgCleanup ArgCleanup;
320 ArgCleanup.Cleanup = Cleanup;
321 ArgCleanup.IsActiveIP = IsActiveIP;
322 CleanupsToDeactivate.push_back(ArgCleanup);
323 }
324
326 return CleanupsToDeactivate;
327 }
328
330 llvm::Instruction *getStackBase() const { return StackBase; }
331 void freeArgumentMemory(CodeGenFunction &CGF) const;
332
333 /// Returns if we're using an inalloca struct to pass arguments in
334 /// memory.
335 bool isUsingInAlloca() const { return StackBase; }
336
337private:
338 SmallVector<Writeback, 1> Writebacks;
339
340 /// Deactivate these cleanups immediately before making the call. This
341 /// is used to cleanup objects that are owned by the callee once the call
342 /// occurs.
343 SmallVector<CallArgCleanup, 1> CleanupsToDeactivate;
344
345 /// The stacksave call. It dominates all of the argument evaluation.
346 llvm::CallInst *StackBase = nullptr;
347};
348
349/// FunctionArgList - Type for representing both the decl and type
350/// of parameters to a function. The decl must be either a
351/// ParmVarDecl or ImplicitParamDecl.
352class FunctionArgList : public SmallVector<const VarDecl *, 16> {};
353
354/// ReturnValueSlot - Contains the address where the return value of a
355/// function can be stored, and whether the address is volatile or not.
357 Address Addr = Address::invalid();
358
359 // Return value slot flags
360 LLVM_PREFERRED_TYPE(bool)
361 unsigned IsVolatile : 1;
362 LLVM_PREFERRED_TYPE(bool)
363 unsigned IsUnused : 1;
364 LLVM_PREFERRED_TYPE(bool)
365 unsigned IsExternallyDestructed : 1;
366
367public:
369 : IsVolatile(false), IsUnused(false), IsExternallyDestructed(false) {}
370 ReturnValueSlot(Address Addr, bool IsVolatile, bool IsUnused = false,
371 bool IsExternallyDestructed = false)
372 : Addr(Addr), IsVolatile(IsVolatile), IsUnused(IsUnused),
373 IsExternallyDestructed(IsExternallyDestructed) {}
374
375 bool isNull() const { return !Addr.isValid(); }
376 bool isVolatile() const { return IsVolatile; }
377 Address getValue() const { return Addr; }
378 bool isUnused() const { return IsUnused; }
379 bool isExternallyDestructed() const { return IsExternallyDestructed; }
380 Address getAddress() const { return Addr; }
381};
382
383/// Adds attributes to \p F according to our \p CodeGenOpts and \p LangOpts, as
384/// though we had emitted it ourselves. We remove any attributes on F that
385/// conflict with the attributes we add here.
386///
387/// This is useful for adding attrs to bitcode modules that you want to link
388/// with but don't control, such as CUDA's libdevice. When linking with such
389/// a bitcode library, you might want to set e.g. its functions'
390/// "unsafe-fp-math" attribute to match the attr of the functions you're
391/// codegen'ing. Otherwise, LLVM will interpret the bitcode module's lack of
392/// unsafe-fp-math attrs as tantamount to unsafe-fp-math=false, and then LLVM
393/// will propagate unsafe-fp-math=false up to every transitive caller of a
394/// function in the bitcode library!
395///
396/// With the exception of fast-math attrs, this will only make the attributes
397/// on the function more conservative. But it's unsafe to call this on a
398/// function which relies on particular fast-math attributes for correctness.
399/// It's up to you to ensure that this is safe.
400void mergeDefaultFunctionDefinitionAttributes(llvm::Function &F,
401 const CodeGenOptions &CodeGenOpts,
402 const LangOptions &LangOpts,
403 const TargetOptions &TargetOpts,
404 bool WillInternalize);
405
406enum class FnInfoOpts {
407 None = 0,
408 IsInstanceMethod = 1 << 0,
409 IsChainCall = 1 << 1,
410 IsDelegateCall = 1 << 2,
411};
412
414 return static_cast<FnInfoOpts>(llvm::to_underlying(A) |
415 llvm::to_underlying(B));
416}
417
419 return static_cast<FnInfoOpts>(llvm::to_underlying(A) &
420 llvm::to_underlying(B));
421}
422
424 A = A | B;
425 return A;
426}
427
429 A = A & B;
430 return A;
431}
432
433} // end namespace CodeGen
434} // end namespace clang
435
436#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:2610
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition: Expr.h:2820
CodeGenOptions - Track various options which control how the code is optimized and passed to the back...
Like RawAddress, an abstract representation of an aligned address, but the pointer contained in this ...
Definition: Address.h:111
static Address invalid()
Definition: Address.h:153
bool isValid() const
Definition: Address.h:154
Abstract information about a function or function prototype.
Definition: CGCall.h:40
const GlobalDecl getCalleeDecl() const
Definition: CGCall.h:58
CGCalleeInfo(const FunctionProtoType *calleeProtoTy)
Definition: CGCall.h:50
CGCalleeInfo(const FunctionProtoType *calleeProtoTy, GlobalDecl calleeDecl)
Definition: CGCall.h:48
const FunctionProtoType * getCalleeFunctionProtoType() const
Definition: CGCall.h:55
CGCalleeInfo(GlobalDecl calleeDecl)
Definition: CGCall.h:52
All available information about a concrete callee.
Definition: CGCall.h:62
CGCalleeInfo getAbstractInfo() const
Definition: CGCall.h:172
CGCallee prepareConcreteCallee(CodeGenFunction &CGF) const
If this is a delayed callee computation of some sort, prepare a concrete callee.
Definition: CGCall.cpp:5982
VirtualInfoStorage VirtualInfo
Definition: CGCall.h:91
bool isVirtual() const
Definition: CGCall.h:188
const CXXPseudoDestructorExpr * getPseudoDestructorExpr() const
Definition: CGCall.h:164
bool isOrdinary() const
Definition: CGCall.h:169
Address getThisAddress() const
Definition: CGCall.h:199
const CallExpr * getVirtualCallExpr() const
Definition: CGCall.h:191
BuiltinInfoStorage BuiltinInfo
Definition: CGCall.h:89
bool isPseudoDestructor() const
Definition: CGCall.h:161
llvm::Value * getFunctionPointer() const
Definition: CGCall.h:178
PseudoDestructorInfoStorage PseudoDestructorInfo
Definition: CGCall.h:90
static CGCallee forBuiltin(unsigned builtinID, const FunctionDecl *builtinDecl)
Definition: CGCall.h:115
unsigned getBuiltinID() const
Definition: CGCall.h:156
CGCallee(const CGCalleeInfo &abstractInfo, llvm::Value *functionPtr)
Construct a callee.
Definition: CGCall.h:107
static CGCallee forVirtual(const CallExpr *CE, GlobalDecl MD, Address Addr, llvm::FunctionType *FTy)
Definition: CGCall.h:139
void setFunctionPointer(llvm::Value *functionPtr)
Definition: CGCall.h:182
static CGCallee forDirect(llvm::FunctionCallee functionPtr, const CGCalleeInfo &abstractInfo=CGCalleeInfo())
Definition: CGCall.h:134
static CGCallee forDirect(llvm::Constant *functionPtr, const CGCalleeInfo &abstractInfo=CGCalleeInfo())
Definition: CGCall.h:129
bool isBuiltin() const
Definition: CGCall.h:149
llvm::FunctionType * getVirtualFunctionType() const
Definition: CGCall.h:203
const FunctionDecl * getBuiltinDecl() const
Definition: CGCall.h:152
static CGCallee forPseudoDestructor(const CXXPseudoDestructorExpr *E)
Definition: CGCall.h:123
GlobalDecl getVirtualMethodDecl() const
Definition: CGCall.h:195
CGCalleeInfo AbstractInfo
Definition: CGCall.h:88
CallArgList - Type for representing both the value and type of arguments in a call.
Definition: CGCall.h:258
llvm::Instruction * getStackBase() const
Definition: CGCall.h:330
void addUncopiedAggregate(LValue LV, QualType type)
Definition: CGCall.h:284
llvm::iterator_range< SmallVectorImpl< Writeback >::const_iterator > writeback_const_range
Definition: CGCall.h:311
void addArgCleanupDeactivation(EHScopeStack::stable_iterator Cleanup, llvm::Instruction *IsActiveIP)
Definition: CGCall.h:317
ArrayRef< CallArgCleanup > getCleanupsToDeactivate() const
Definition: CGCall.h:325
bool hasWritebacks() const
Definition: CGCall.h:308
void add(RValue rvalue, QualType type)
Definition: CGCall.h:282
bool isUsingInAlloca() const
Returns if we're using an inalloca struct to pass arguments in memory.
Definition: CGCall.h:335
void allocateArgumentMemory(CodeGenFunction &CGF)
Definition: CGCall.cpp:4355
void freeArgumentMemory(CodeGenFunction &CGF) const
Definition: CGCall.cpp:4362
writeback_const_range writebacks() const
Definition: CGCall.h:313
void addWriteback(LValue srcLV, Address temporary, llvm::Value *toUse)
Definition: CGCall.h:303
void addFrom(const CallArgList &other)
Add all the arguments from another CallArgList to this one.
Definition: CGCall.h:291
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:352
LValue - This represents an lvalue references.
Definition: CGValue.h:181
RValue - This trivial value class is used to represent the result of an expression that is evaluated.
Definition: CGValue.h:41
bool isAggregate() const
Definition: CGValue.h:65
ReturnValueSlot - Contains the address where the return value of a function can be stored,...
Definition: CGCall.h:356
Address getValue() const
Definition: CGCall.h:377
bool isExternallyDestructed() const
Definition: CGCall.h:379
ReturnValueSlot(Address Addr, bool IsVolatile, bool IsUnused=false, bool IsExternallyDestructed=false)
Definition: CGCall.h:370
Address getAddress() const
Definition: CGCall.h:380
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
This represents one expression.
Definition: Expr.h:110
Represents a function declaration or definition.
Definition: Decl.h:1971
Represents a prototype with parameter type info, e.g.
Definition: Type.h:4652
GlobalDecl - represents a global declaration.
Definition: GlobalDecl.h:56
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:461
A (possibly-)qualified type.
Definition: Type.h:940
Options for controlling the target.
Definition: TargetOptions.h:26
@ Decl
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
FnInfoOpts operator|=(FnInfoOpts A, FnInfoOpts B)
Definition: CGCall.h:423
FnInfoOpts operator&(FnInfoOpts A, FnInfoOpts B)
Definition: CGCall.h:418
FnInfoOpts operator&=(FnInfoOpts A, FnInfoOpts B)
Definition: CGCall.h:428
void mergeDefaultFunctionDefinitionAttributes(llvm::Function &F, const CodeGenOptions &CodeGenOpts, const LangOptions &LangOpts, const TargetOptions &TargetOpts, bool WillInternalize)
Adds attributes to F according to our CodeGenOpts and LangOpts, as though we had emitted it ourselves...
Definition: CGCall.cpp:2071
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.
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 true
Definition: stdbool.h:21
#define false
Definition: stdbool.h:22
llvm::Instruction * IsActiveIP
The "is active" insertion point.
Definition: CGCall.h:279
EHScopeStack::stable_iterator Cleanup
Definition: CGCall.h:275
llvm::Value * ToUse
A value to "use" after the writeback, or null.
Definition: CGCall.h:271
LValue Source
The original argument.
Definition: CGCall.h:265
Address Temporary
The temporary alloca.
Definition: CGCall.h:268
LValue getKnownLValue() const
Definition: CGCall.h:238
RValue getKnownRValue() const
Definition: CGCall.h:242
QualType getType() const
Definition: CGCall.h:232
bool isAggregate() const
Definition: CGCall.h:251
CallArg(LValue lv, QualType ty)
Definition: CGCall.h:229
void setRValue(RValue _RV)
Definition: CGCall.h:246
void copyInto(CodeGenFunction &CGF, Address A) const
Definition: CGCall.cpp:4655
CallArg(RValue rv, QualType ty)
Definition: CGCall.h:227
bool hasLValue() const
Definition: CGCall.h:231
RValue getRValue(CodeGenFunction &CGF) const
Definition: CGCall.cpp:4645