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