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  /// \brief The function prototype of the callee.
47  const FunctionProtoType *CalleeProtoTy;
48  /// \brief 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 {
218  bool NeedsCopy;
219  CallArg(RValue rv, QualType ty, bool needscopy)
220  : RV(rv), Ty(ty), NeedsCopy(needscopy)
221  { }
222  };
223 
224  /// CallArgList - Type for representing both the value and type of
225  /// arguments in a call.
226  class CallArgList :
227  public SmallVector<CallArg, 16> {
228  public:
229  CallArgList() : StackBase(nullptr) {}
230 
231  struct Writeback {
232  /// The original argument. Note that the argument l-value
233  /// is potentially null.
235 
236  /// The temporary alloca.
238 
239  /// A value to "use" after the writeback, or null.
241  };
242 
243  struct CallArgCleanup {
245 
246  /// The "is active" insertion point. This instruction is temporary and
247  /// will be removed after insertion.
248  llvm::Instruction *IsActiveIP;
249  };
250 
251  void add(RValue rvalue, QualType type, bool needscopy = false) {
252  push_back(CallArg(rvalue, type, needscopy));
253  }
254 
255  /// Add all the arguments from another CallArgList to this one. After doing
256  /// this, the old CallArgList retains its list of arguments, but must not
257  /// be used to emit a call.
258  void addFrom(const CallArgList &other) {
259  insert(end(), other.begin(), other.end());
260  Writebacks.insert(Writebacks.end(),
261  other.Writebacks.begin(), other.Writebacks.end());
262  CleanupsToDeactivate.insert(CleanupsToDeactivate.end(),
263  other.CleanupsToDeactivate.begin(),
264  other.CleanupsToDeactivate.end());
265  assert(!(StackBase && other.StackBase) && "can't merge stackbases");
266  if (!StackBase)
267  StackBase = other.StackBase;
268  }
269 
270  void addWriteback(LValue srcLV, Address temporary,
271  llvm::Value *toUse) {
272  Writeback writeback = { srcLV, temporary, toUse };
273  Writebacks.push_back(writeback);
274  }
275 
276  bool hasWritebacks() const { return !Writebacks.empty(); }
277 
278  typedef llvm::iterator_range<SmallVectorImpl<Writeback>::const_iterator>
280 
282  return writeback_const_range(Writebacks.begin(), Writebacks.end());
283  }
284 
286  llvm::Instruction *IsActiveIP) {
287  CallArgCleanup ArgCleanup;
288  ArgCleanup.Cleanup = Cleanup;
289  ArgCleanup.IsActiveIP = IsActiveIP;
290  CleanupsToDeactivate.push_back(ArgCleanup);
291  }
292 
294  return CleanupsToDeactivate;
295  }
296 
297  void allocateArgumentMemory(CodeGenFunction &CGF);
298  llvm::Instruction *getStackBase() const { return StackBase; }
299  void freeArgumentMemory(CodeGenFunction &CGF) const;
300 
301  /// \brief Returns if we're using an inalloca struct to pass arguments in
302  /// memory.
303  bool isUsingInAlloca() const { return StackBase; }
304 
305  private:
306  SmallVector<Writeback, 1> Writebacks;
307 
308  /// Deactivate these cleanups immediately before making the call. This
309  /// is used to cleanup objects that are owned by the callee once the call
310  /// occurs.
311  SmallVector<CallArgCleanup, 1> CleanupsToDeactivate;
312 
313  /// The stacksave call. It dominates all of the argument evaluation.
314  llvm::CallInst *StackBase;
315  };
316 
317  /// FunctionArgList - Type for representing both the decl and type
318  /// of parameters to a function. The decl must be either a
319  /// ParmVarDecl or ImplicitParamDecl.
320  class FunctionArgList : public SmallVector<const VarDecl*, 16> {
321  };
322 
323  /// ReturnValueSlot - Contains the address where the return value of a
324  /// function can be stored, and whether the address is volatile or not.
326  llvm::PointerIntPair<llvm::Value *, 2, unsigned int> Value;
327  CharUnits Alignment;
328 
329  // Return value slot flags
330  enum Flags {
331  IS_VOLATILE = 0x1,
332  IS_UNUSED = 0x2,
333  };
334 
335  public:
337  ReturnValueSlot(Address Addr, bool IsVolatile, bool IsUnused = false)
338  : Value(Addr.isValid() ? Addr.getPointer() : nullptr,
339  (IsVolatile ? IS_VOLATILE : 0) | (IsUnused ? IS_UNUSED : 0)),
340  Alignment(Addr.isValid() ? Addr.getAlignment() : CharUnits::Zero()) {}
341 
342  bool isNull() const { return !getValue().isValid(); }
343 
344  bool isVolatile() const { return Value.getInt() & IS_VOLATILE; }
345  Address getValue() const { return Address(Value.getPointer(), Alignment); }
346  bool isUnused() const { return Value.getInt() & IS_UNUSED; }
347  };
348 
349 } // end namespace CodeGen
350 } // end namespace clang
351 
352 #endif
ReturnValueSlot - Contains the address where the return value of a function can be stored...
Definition: CGCall.h:325
Represents a function declaration or definition.
Definition: Decl.h:1696
A (possibly-)qualified type.
Definition: Type.h:653
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:26
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
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
bool hasWritebacks() const
Definition: CGCall.h:276
llvm::Instruction * getStackBase() const
Definition: CGCall.h:298
llvm::Value * getFunctionPointer() const
Definition: CGCall.h:178
Address getValue() const
Definition: CGCall.h:345
CGCalleeInfo(const Decl *calleeDecl)
Definition: CGCall.h:57
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:244
bool isUsingInAlloca() const
Returns if we&#39;re using an inalloca struct to pass arguments in memory.
Definition: CGCall.h:303
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:285
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
CallArg(RValue rv, QualType ty, bool needscopy)
Definition: CGCall.h:219
void setFunctionPointer(llvm::Value *functionPtr)
Definition: CGCall.h:182
llvm::iterator_range< SmallVectorImpl< Writeback >::const_iterator > writeback_const_range
Definition: CGCall.h:279
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:258
Represents a prototype with parameter type info, e.g.
Definition: Type.h:3274
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:281
void addWriteback(LValue srcLV, Address temporary, llvm::Value *toUse)
Definition: CGCall.h:270
PseudoDestructorInfoStorage PseudoDestructorInfo
Definition: CGCall.h:95
Address Temporary
The temporary alloca.
Definition: CGCall.h:237
llvm::Value * ToUse
A value to "use" after the writeback, or null.
Definition: CGCall.h:240
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
void add(RValue rvalue, QualType type, bool needscopy=false)
Definition: CGCall.h:251
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
The l-value was considered opaque, so the alignment was determined from a type.
bool isBuiltin() const
Definition: CGCall.h:149
A saved depth on the scope stack.
Definition: EHScopeStack.h:107
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:320
llvm::Instruction * IsActiveIP
The "is active" insertion point.
Definition: CGCall.h:248
Dataflow Directional Tag Classes.
LValue Source
The original argument.
Definition: CGCall.h:234
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:337
unsigned kind
All of the diagnostics that can be emitted by the frontend.
Definition: DiagnosticIDs.h:61
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:2218
const FunctionProtoType * getCalleeFunctionProtoType() const
Definition: CGCall.h:60
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:226
Abstract information about a function or function prototype.
Definition: CGCall.h:45
ArrayRef< CallArgCleanup > getCleanupsToDeactivate() const
Definition: CGCall.h:293