clang  7.0.0svn
CGCall.h
Go to the documentation of this file.
1 //===----- CGCall.h - Encapsulate calling convention details ----*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // These classes wrap the information about a call or function
11 // definition used to handle ABI compliancy.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_CLANG_LIB_CODEGEN_CGCALL_H
16 #define LLVM_CLANG_LIB_CODEGEN_CGCALL_H
17 
18 #include "CGValue.h"
19 #include "EHScopeStack.h"
21 #include "clang/AST/GlobalDecl.h"
22 #include "clang/AST/Type.h"
23 #include "llvm/IR/Value.h"
24 
25 // FIXME: Restructure so we don't have to expose so much stuff.
26 #include "ABIInfo.h"
27 
28 namespace llvm {
29 class AttributeList;
30 class Function;
31 class Type;
32 class Value;
33 }
34 
35 namespace clang {
36  class ASTContext;
37  class Decl;
38  class FunctionDecl;
39  class ObjCMethodDecl;
40  class VarDecl;
41 
42 namespace CodeGen {
43 
44 /// Abstract information about a function or function prototype.
45 class CGCalleeInfo {
46  /// The function prototype of the callee.
47  const FunctionProtoType *CalleeProtoTy;
48  /// The function declaration of the callee.
49  const Decl *CalleeDecl;
50 
51 public:
52  explicit CGCalleeInfo() : CalleeProtoTy(nullptr), CalleeDecl(nullptr) {}
53  CGCalleeInfo(const FunctionProtoType *calleeProtoTy, const Decl *calleeDecl)
54  : CalleeProtoTy(calleeProtoTy), CalleeDecl(calleeDecl) {}
55  CGCalleeInfo(const FunctionProtoType *calleeProtoTy)
56  : CalleeProtoTy(calleeProtoTy), CalleeDecl(nullptr) {}
57  CGCalleeInfo(const Decl *calleeDecl)
58  : CalleeProtoTy(nullptr), CalleeDecl(calleeDecl) {}
59 
61  return CalleeProtoTy;
62  }
63  const Decl *getCalleeDecl() const { return CalleeDecl; }
64  };
65 
66  /// All available information about a concrete callee.
67  class CGCallee {
68  enum class SpecialKind : uintptr_t {
69  Invalid,
70  Builtin,
71  PseudoDestructor,
72  Virtual,
73 
74  Last = Virtual
75  };
76 
77  struct BuiltinInfoStorage {
78  const FunctionDecl *Decl;
79  unsigned ID;
80  };
81  struct PseudoDestructorInfoStorage {
83  };
84  struct VirtualInfoStorage {
85  const CallExpr *CE;
86  GlobalDecl MD;
87  Address Addr;
88  llvm::FunctionType *FTy;
89  };
90 
91  SpecialKind KindOrFunctionPointer;
92  union {
94  BuiltinInfoStorage BuiltinInfo;
95  PseudoDestructorInfoStorage PseudoDestructorInfo;
96  VirtualInfoStorage VirtualInfo;
97  };
98 
99  explicit CGCallee(SpecialKind kind) : KindOrFunctionPointer(kind) {}
100 
101  CGCallee(const FunctionDecl *builtinDecl, unsigned builtinID)
102  : KindOrFunctionPointer(SpecialKind::Builtin) {
103  BuiltinInfo.Decl = builtinDecl;
104  BuiltinInfo.ID = builtinID;
105  }
106 
107  public:
108  CGCallee() : KindOrFunctionPointer(SpecialKind::Invalid) {}
109 
110  /// Construct a callee. Call this constructor directly when this
111  /// isn't a direct call.
112  CGCallee(const CGCalleeInfo &abstractInfo, llvm::Value *functionPtr)
113  : KindOrFunctionPointer(SpecialKind(uintptr_t(functionPtr))) {
114  AbstractInfo = abstractInfo;
115  assert(functionPtr && "configuring callee without function pointer");
116  assert(functionPtr->getType()->isPointerTy());
117  assert(functionPtr->getType()->getPointerElementType()->isFunctionTy());
118  }
119 
120  static CGCallee forBuiltin(unsigned builtinID,
121  const FunctionDecl *builtinDecl) {
122  CGCallee result(SpecialKind::Builtin);
123  result.BuiltinInfo.Decl = builtinDecl;
124  result.BuiltinInfo.ID = builtinID;
125  return result;
126  }
127 
129  CGCallee result(SpecialKind::PseudoDestructor);
130  result.PseudoDestructorInfo.Expr = E;
131  return result;
132  }
133 
134  static CGCallee forDirect(llvm::Constant *functionPtr,
135  const CGCalleeInfo &abstractInfo = CGCalleeInfo()) {
136  return CGCallee(abstractInfo, functionPtr);
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  }
152  const FunctionDecl *getBuiltinDecl() const {
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.getDecl();
175  assert(isOrdinary());
176  return AbstractInfo;
177  }
179  assert(isOrdinary());
180  return reinterpret_cast<llvm::Value*>(uintptr_t(KindOrFunctionPointer));
181  }
182  void setFunctionPointer(llvm::Value *functionPtr) {
183  assert(isOrdinary());
184  KindOrFunctionPointer = SpecialKind(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 
203  llvm::FunctionType *getFunctionType() const {
204  if (isVirtual())
205  return VirtualInfo.FTy;
206  return cast<llvm::FunctionType>(
207  getFunctionPointer()->getType()->getPointerElementType());
208  }
209 
210  /// If this is a delayed callee computation of some sort, prepare
211  /// a concrete callee.
212  CGCallee prepareConcreteCallee(CodeGenFunction &CGF) const;
213  };
214 
215  struct CallArg {
216  private:
217  union {
219  LValue LV; /// The argument is semantically a load from this l-value.
220  };
221  bool HasLV;
222 
223  /// A data-flow flag to make sure getRValue and/or copyInto are not
224  /// called twice for duplicated IR emission.
225  mutable bool IsUsed;
226 
227  public:
230  : RV(rv), HasLV(false), IsUsed(false), Ty(ty) {}
232  : LV(lv), HasLV(true), IsUsed(false), Ty(ty) {}
233  bool hasLValue() const { return HasLV; }
234  QualType getType() const { return Ty; }
235 
236  /// \returns an independent RValue. If the CallArg contains an LValue,
237  /// a temporary copy is returned.
238  RValue getRValue(CodeGenFunction &CGF) const;
239 
241  assert(HasLV && !IsUsed);
242  return LV;
243  }
245  assert(!HasLV && !IsUsed);
246  return RV;
247  }
248  void setRValue(RValue _RV) {
249  assert(!HasLV);
250  RV = _RV;
251  }
252 
253  bool isAggregate() const { return HasLV || RV.isAggregate(); }
254 
255  void copyInto(CodeGenFunction &CGF, Address A) const;
256  };
257 
258  /// CallArgList - Type for representing both the value and type of
259  /// arguments in a call.
260  class CallArgList :
261  public SmallVector<CallArg, 8> {
262  public:
263  CallArgList() : StackBase(nullptr) {}
264 
265  struct Writeback {
266  /// The original argument. Note that the argument l-value
267  /// is potentially null.
269 
270  /// The temporary alloca.
272 
273  /// A value to "use" after the writeback, or null.
275  };
276 
277  struct CallArgCleanup {
279 
280  /// The "is active" insertion point. This instruction is temporary and
281  /// will be removed after insertion.
282  llvm::Instruction *IsActiveIP;
283  };
284 
285  void add(RValue rvalue, QualType type) { push_back(CallArg(rvalue, type)); }
286 
288  push_back(CallArg(LV, type));
289  }
290 
291  /// Add all the arguments from another CallArgList to this one. After doing
292  /// this, the old CallArgList retains its list of arguments, but must not
293  /// be used to emit a call.
294  void addFrom(const CallArgList &other) {
295  insert(end(), other.begin(), other.end());
296  Writebacks.insert(Writebacks.end(),
297  other.Writebacks.begin(), other.Writebacks.end());
298  CleanupsToDeactivate.insert(CleanupsToDeactivate.end(),
299  other.CleanupsToDeactivate.begin(),
300  other.CleanupsToDeactivate.end());
301  assert(!(StackBase && other.StackBase) && "can't merge stackbases");
302  if (!StackBase)
303  StackBase = other.StackBase;
304  }
305 
306  void addWriteback(LValue srcLV, Address temporary,
307  llvm::Value *toUse) {
308  Writeback writeback = { srcLV, temporary, toUse };
309  Writebacks.push_back(writeback);
310  }
311 
312  bool hasWritebacks() const { return !Writebacks.empty(); }
313 
314  typedef llvm::iterator_range<SmallVectorImpl<Writeback>::const_iterator>
316 
318  return writeback_const_range(Writebacks.begin(), Writebacks.end());
319  }
320 
322  llvm::Instruction *IsActiveIP) {
323  CallArgCleanup ArgCleanup;
324  ArgCleanup.Cleanup = Cleanup;
325  ArgCleanup.IsActiveIP = IsActiveIP;
326  CleanupsToDeactivate.push_back(ArgCleanup);
327  }
328 
330  return CleanupsToDeactivate;
331  }
332 
333  void allocateArgumentMemory(CodeGenFunction &CGF);
334  llvm::Instruction *getStackBase() const { return StackBase; }
335  void freeArgumentMemory(CodeGenFunction &CGF) const;
336 
337  /// Returns if we're using an inalloca struct to pass arguments in
338  /// memory.
339  bool isUsingInAlloca() const { return StackBase; }
340 
341  private:
342  SmallVector<Writeback, 1> Writebacks;
343 
344  /// Deactivate these cleanups immediately before making the call. This
345  /// is used to cleanup objects that are owned by the callee once the call
346  /// occurs.
347  SmallVector<CallArgCleanup, 1> CleanupsToDeactivate;
348 
349  /// The stacksave call. It dominates all of the argument evaluation.
350  llvm::CallInst *StackBase;
351  };
352 
353  /// FunctionArgList - Type for representing both the decl and type
354  /// of parameters to a function. The decl must be either a
355  /// ParmVarDecl or ImplicitParamDecl.
356  class FunctionArgList : public SmallVector<const VarDecl*, 16> {
357  };
358 
359  /// ReturnValueSlot - Contains the address where the return value of a
360  /// function can be stored, and whether the address is volatile or not.
362  llvm::PointerIntPair<llvm::Value *, 2, unsigned int> Value;
363  CharUnits Alignment;
364 
365  // Return value slot flags
366  enum Flags {
367  IS_VOLATILE = 0x1,
368  IS_UNUSED = 0x2,
369  };
370 
371  public:
373  ReturnValueSlot(Address Addr, bool IsVolatile, bool IsUnused = false)
374  : Value(Addr.isValid() ? Addr.getPointer() : nullptr,
375  (IsVolatile ? IS_VOLATILE : 0) | (IsUnused ? IS_UNUSED : 0)),
376  Alignment(Addr.isValid() ? Addr.getAlignment() : CharUnits::Zero()) {}
377 
378  bool isNull() const { return !getValue().isValid(); }
379 
380  bool isVolatile() const { return Value.getInt() & IS_VOLATILE; }
381  Address getValue() const { return Address(Value.getPointer(), Alignment); }
382  bool isUnused() const { return Value.getInt() & IS_UNUSED; }
383  };
384 
385 } // end namespace CodeGen
386 } // end namespace clang
387 
388 #endif
ReturnValueSlot - Contains the address where the return value of a function can be stored...
Definition: CGCall.h:361
bool isAggregate() const
Definition: CGCall.h:253
Represents a function declaration or definition.
Definition: Decl.h:1714
A (possibly-)qualified type.
Definition: Type.h:655
CGCallee(const CGCalleeInfo &abstractInfo, llvm::Value *functionPtr)
Construct a callee.
Definition: CGCall.h:112
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
Definition: Dominators.h:30
const Decl * getCalleeDecl() const
Definition: CGCall.h:63
C Language Family Type Representation.
static const Builtin::Info BuiltinInfo[]
Definition: Builtins.cpp:21
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
void addUncopiedAggregate(LValue LV, QualType type)
Definition: CGCall.h:287
CallArg(LValue lv, QualType ty)
Definition: CGCall.h:231
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
constexpr XRayInstrMask Function
Definition: XRayInstr.h:39
bool hasWritebacks() const
Definition: CGCall.h:312
llvm::Instruction * getStackBase() const
Definition: CGCall.h:334
llvm::Value * getFunctionPointer() const
Definition: CGCall.h:178
Address getValue() const
Definition: CGCall.h:381
CGCalleeInfo(const Decl *calleeDecl)
Definition: CGCall.h:57
void add(RValue rvalue, QualType type)
Definition: CGCall.h:285
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
VirtualInfoStorage VirtualInfo
Definition: CGCall.h:96
EHScopeStack::stable_iterator Cleanup
Definition: CGCall.h:278
bool isUsingInAlloca() const
Returns if we&#39;re using an inalloca struct to pass arguments in memory.
Definition: CGCall.h:339
bool isOrdinary() const
Definition: CGCall.h:169
bool isVirtual() const
Definition: CGCall.h:187
void addArgCleanupDeactivation(EHScopeStack::stable_iterator Cleanup, llvm::Instruction *IsActiveIP)
Definition: CGCall.h:321
llvm::FunctionType * getFunctionType() const
Definition: CGCall.h:203
const CXXPseudoDestructorExpr * getPseudoDestructorExpr() const
Definition: CGCall.h:164
CharUnits - This is an opaque type for sizes expressed in character units.
Definition: CharUnits.h:38
const FunctionDecl * getBuiltinDecl() const
Definition: CGCall.h:152
void setFunctionPointer(llvm::Value *functionPtr)
Definition: CGCall.h:182
llvm::iterator_range< SmallVectorImpl< Writeback >::const_iterator > writeback_const_range
Definition: CGCall.h:315
QualType getType() const
Definition: CGCall.h:234
const CallExpr * getVirtualCallExpr() const
Definition: CGCall.h:190
Represents a C++ pseudo-destructor (C++ [expr.pseudo]).
Definition: ExprCXX.h:2200
void addFrom(const CallArgList &other)
Add all the arguments from another CallArgList to this one.
Definition: CGCall.h:294
Represents a prototype with parameter type info, e.g.
Definition: Type.h:3369
RValue - This trivial value class is used to represent the result of an expression that is evaluated...
Definition: CGValue.h:39
bool isPseudoDestructor() const
Definition: CGCall.h:161
writeback_const_range writebacks() const
Definition: CGCall.h:317
void addWriteback(LValue srcLV, Address temporary, llvm::Value *toUse)
Definition: CGCall.h:306
PseudoDestructorInfoStorage PseudoDestructorInfo
Definition: CGCall.h:95
Address Temporary
The temporary alloca.
Definition: CGCall.h:271
llvm::Value * ToUse
A value to "use" after the writeback, or null.
Definition: CGCall.h:274
Expr - This represents one expression.
Definition: Expr.h:106
CGCalleeInfo(const FunctionProtoType *calleeProtoTy)
Definition: CGCall.h:55
static CGCallee forDirect(llvm::Constant *functionPtr, const CGCalleeInfo &abstractInfo=CGCalleeInfo())
Definition: CGCall.h:134
Address getThisAddress() const
Definition: CGCall.h:198
static SVal getValue(SVal val, SValBuilder &svalBuilder)
BuiltinInfoStorage BuiltinInfo
Definition: CGCall.h:94
CGCalleeInfo AbstractInfo
Definition: CGCall.h:93
__UINTPTR_TYPE__ uintptr_t
An unsigned integer type with the property that any valid pointer to void can be converted to this ty...
Definition: opencl-c.h:82
GlobalDecl - represents a global declaration.
Definition: GlobalDecl.h:35
LValue getKnownLValue() const
Definition: CGCall.h:240
bool isBuiltin() const
Definition: CGCall.h:149
#define false
Definition: stdbool.h:33
A saved depth on the scope stack.
Definition: EHScopeStack.h:107
void setRValue(RValue _RV)
Definition: CGCall.h:248
GlobalDecl getVirtualMethodDecl() const
Definition: CGCall.h:194
An aligned address.
Definition: Address.h:25
All available information about a concrete callee.
Definition: CGCall.h:67
CGCalleeInfo(const FunctionProtoType *calleeProtoTy, const Decl *calleeDecl)
Definition: CGCall.h:53
FunctionArgList - Type for representing both the decl and type of parameters to a function...
Definition: CGCall.h:356
llvm::Instruction * IsActiveIP
The "is active" insertion point.
Definition: CGCall.h:282
Dataflow Directional Tag Classes.
LValue Source
The original argument.
Definition: CGCall.h:268
static CGCallee forBuiltin(unsigned builtinID, const FunctionDecl *builtinDecl)
Definition: CGCall.h:120
unsigned getBuiltinID() const
Definition: CGCall.h:156
static CGCallee forPseudoDestructor(const CXXPseudoDestructorExpr *E)
Definition: CGCall.h:128
ReturnValueSlot(Address Addr, bool IsVolatile, bool IsUnused=false)
Definition: CGCall.h:373
RValue getKnownRValue() const
Definition: CGCall.h:244
unsigned kind
All of the diagnostics that can be emitted by the frontend.
Definition: DiagnosticIDs.h:61
CallArg(RValue rv, QualType ty)
Definition: CGCall.h:229
static CGCallee forVirtual(const CallExpr *CE, GlobalDecl MD, Address Addr, llvm::FunctionType *FTy)
Definition: CGCall.h:139
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition: Expr.h:2283
bool hasLValue() const
Definition: CGCall.h:233
const FunctionProtoType * getCalleeFunctionProtoType() const
Definition: CGCall.h:60
#define true
Definition: stdbool.h:32
LValue - This represents an lvalue references.
Definition: CGValue.h:167
CGCalleeInfo getAbstractInfo() const
Definition: CGCall.h:172
CallArgList - Type for representing both the value and type of arguments in a call.
Definition: CGCall.h:260
Abstract information about a function or function prototype.
Definition: CGCall.h:45
ArrayRef< CallArgCleanup > getCleanupsToDeactivate() const
Definition: CGCall.h:329