clang  6.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/Type.h"
22 #include "llvm/IR/Value.h"
23 
24 // FIXME: Restructure so we don't have to expose so much stuff.
25 #include "ABIInfo.h"
26 
27 namespace llvm {
28 class AttributeList;
29 class Function;
30 class Type;
31 class Value;
32 }
33 
34 namespace clang {
35  class ASTContext;
36  class Decl;
37  class FunctionDecl;
38  class ObjCMethodDecl;
39  class VarDecl;
40 
41 namespace CodeGen {
42 
43 /// Abstract information about a function or function prototype.
44 class CGCalleeInfo {
45  /// \brief The function prototype of the callee.
46  const FunctionProtoType *CalleeProtoTy;
47  /// \brief The function declaration of the callee.
48  const Decl *CalleeDecl;
49 
50 public:
51  explicit CGCalleeInfo() : CalleeProtoTy(nullptr), CalleeDecl(nullptr) {}
52  CGCalleeInfo(const FunctionProtoType *calleeProtoTy, const Decl *calleeDecl)
53  : CalleeProtoTy(calleeProtoTy), CalleeDecl(calleeDecl) {}
54  CGCalleeInfo(const FunctionProtoType *calleeProtoTy)
55  : CalleeProtoTy(calleeProtoTy), CalleeDecl(nullptr) {}
56  CGCalleeInfo(const Decl *calleeDecl)
57  : CalleeProtoTy(nullptr), CalleeDecl(calleeDecl) {}
58 
60  return CalleeProtoTy;
61  }
62  const Decl *getCalleeDecl() const { return CalleeDecl; }
63  };
64 
65  /// All available information about a concrete callee.
66  class CGCallee {
67  enum class SpecialKind : uintptr_t {
68  Invalid,
69  Builtin,
70  PseudoDestructor,
71 
72  Last = PseudoDestructor
73  };
74 
75  struct BuiltinInfoStorage {
76  const FunctionDecl *Decl;
77  unsigned ID;
78  };
79  struct PseudoDestructorInfoStorage {
81  };
82 
83  SpecialKind KindOrFunctionPointer;
84  union {
86  BuiltinInfoStorage BuiltinInfo;
87  PseudoDestructorInfoStorage PseudoDestructorInfo;
88  };
89 
90  explicit CGCallee(SpecialKind kind) : KindOrFunctionPointer(kind) {}
91 
92  CGCallee(const FunctionDecl *builtinDecl, unsigned builtinID)
93  : KindOrFunctionPointer(SpecialKind::Builtin) {
94  BuiltinInfo.Decl = builtinDecl;
95  BuiltinInfo.ID = builtinID;
96  }
97 
98  public:
99  CGCallee() : KindOrFunctionPointer(SpecialKind::Invalid) {}
100 
101  /// Construct a callee. Call this constructor directly when this
102  /// isn't a direct call.
103  CGCallee(const CGCalleeInfo &abstractInfo, llvm::Value *functionPtr)
104  : KindOrFunctionPointer(SpecialKind(uintptr_t(functionPtr))) {
105  AbstractInfo = abstractInfo;
106  assert(functionPtr && "configuring callee without function pointer");
107  assert(functionPtr->getType()->isPointerTy());
108  assert(functionPtr->getType()->getPointerElementType()->isFunctionTy());
109  }
110 
111  static CGCallee forBuiltin(unsigned builtinID,
112  const FunctionDecl *builtinDecl) {
113  CGCallee result(SpecialKind::Builtin);
114  result.BuiltinInfo.Decl = builtinDecl;
115  result.BuiltinInfo.ID = builtinID;
116  return result;
117  }
118 
120  CGCallee result(SpecialKind::PseudoDestructor);
121  result.PseudoDestructorInfo.Expr = E;
122  return result;
123  }
124 
125  static CGCallee forDirect(llvm::Constant *functionPtr,
126  const CGCalleeInfo &abstractInfo = CGCalleeInfo()) {
127  return CGCallee(abstractInfo, functionPtr);
128  }
129 
130  bool isBuiltin() const {
131  return KindOrFunctionPointer == SpecialKind::Builtin;
132  }
133  const FunctionDecl *getBuiltinDecl() const {
134  assert(isBuiltin());
135  return BuiltinInfo.Decl;
136  }
137  unsigned getBuiltinID() const {
138  assert(isBuiltin());
139  return BuiltinInfo.ID;
140  }
141 
142  bool isPseudoDestructor() const {
143  return KindOrFunctionPointer == SpecialKind::PseudoDestructor;
144  }
146  assert(isPseudoDestructor());
147  return PseudoDestructorInfo.Expr;
148  }
149 
150  bool isOrdinary() const {
151  return uintptr_t(KindOrFunctionPointer) > uintptr_t(SpecialKind::Last);
152  }
153  const CGCalleeInfo &getAbstractInfo() const {
154  assert(isOrdinary());
155  return AbstractInfo;
156  }
158  assert(isOrdinary());
159  return reinterpret_cast<llvm::Value*>(uintptr_t(KindOrFunctionPointer));
160  }
161  llvm::FunctionType *getFunctionType() const {
162  return cast<llvm::FunctionType>(
163  getFunctionPointer()->getType()->getPointerElementType());
164  }
165  void setFunctionPointer(llvm::Value *functionPtr) {
166  assert(isOrdinary());
167  KindOrFunctionPointer = SpecialKind(uintptr_t(functionPtr));
168  }
169  };
170 
171  struct CallArg {
174  bool NeedsCopy;
175  CallArg(RValue rv, QualType ty, bool needscopy)
176  : RV(rv), Ty(ty), NeedsCopy(needscopy)
177  { }
178  };
179 
180  /// CallArgList - Type for representing both the value and type of
181  /// arguments in a call.
182  class CallArgList :
183  public SmallVector<CallArg, 16> {
184  public:
185  CallArgList() : StackBase(nullptr) {}
186 
187  struct Writeback {
188  /// The original argument. Note that the argument l-value
189  /// is potentially null.
191 
192  /// The temporary alloca.
194 
195  /// A value to "use" after the writeback, or null.
197  };
198 
199  struct CallArgCleanup {
201 
202  /// The "is active" insertion point. This instruction is temporary and
203  /// will be removed after insertion.
204  llvm::Instruction *IsActiveIP;
205  };
206 
207  void add(RValue rvalue, QualType type, bool needscopy = false) {
208  push_back(CallArg(rvalue, type, needscopy));
209  }
210 
211  /// Add all the arguments from another CallArgList to this one. After doing
212  /// this, the old CallArgList retains its list of arguments, but must not
213  /// be used to emit a call.
214  void addFrom(const CallArgList &other) {
215  insert(end(), other.begin(), other.end());
216  Writebacks.insert(Writebacks.end(),
217  other.Writebacks.begin(), other.Writebacks.end());
218  CleanupsToDeactivate.insert(CleanupsToDeactivate.end(),
219  other.CleanupsToDeactivate.begin(),
220  other.CleanupsToDeactivate.end());
221  assert(!(StackBase && other.StackBase) && "can't merge stackbases");
222  if (!StackBase)
223  StackBase = other.StackBase;
224  }
225 
226  void addWriteback(LValue srcLV, Address temporary,
227  llvm::Value *toUse) {
228  Writeback writeback = { srcLV, temporary, toUse };
229  Writebacks.push_back(writeback);
230  }
231 
232  bool hasWritebacks() const { return !Writebacks.empty(); }
233 
234  typedef llvm::iterator_range<SmallVectorImpl<Writeback>::const_iterator>
236 
238  return writeback_const_range(Writebacks.begin(), Writebacks.end());
239  }
240 
242  llvm::Instruction *IsActiveIP) {
243  CallArgCleanup ArgCleanup;
244  ArgCleanup.Cleanup = Cleanup;
245  ArgCleanup.IsActiveIP = IsActiveIP;
246  CleanupsToDeactivate.push_back(ArgCleanup);
247  }
248 
250  return CleanupsToDeactivate;
251  }
252 
253  void allocateArgumentMemory(CodeGenFunction &CGF);
254  llvm::Instruction *getStackBase() const { return StackBase; }
255  void freeArgumentMemory(CodeGenFunction &CGF) const;
256 
257  /// \brief Returns if we're using an inalloca struct to pass arguments in
258  /// memory.
259  bool isUsingInAlloca() const { return StackBase; }
260 
261  private:
262  SmallVector<Writeback, 1> Writebacks;
263 
264  /// Deactivate these cleanups immediately before making the call. This
265  /// is used to cleanup objects that are owned by the callee once the call
266  /// occurs.
267  SmallVector<CallArgCleanup, 1> CleanupsToDeactivate;
268 
269  /// The stacksave call. It dominates all of the argument evaluation.
270  llvm::CallInst *StackBase;
271  };
272 
273  /// FunctionArgList - Type for representing both the decl and type
274  /// of parameters to a function. The decl must be either a
275  /// ParmVarDecl or ImplicitParamDecl.
276  class FunctionArgList : public SmallVector<const VarDecl*, 16> {
277  };
278 
279  /// ReturnValueSlot - Contains the address where the return value of a
280  /// function can be stored, and whether the address is volatile or not.
282  llvm::PointerIntPair<llvm::Value *, 2, unsigned int> Value;
283  CharUnits Alignment;
284 
285  // Return value slot flags
286  enum Flags {
287  IS_VOLATILE = 0x1,
288  IS_UNUSED = 0x2,
289  };
290 
291  public:
293  ReturnValueSlot(Address Addr, bool IsVolatile, bool IsUnused = false)
294  : Value(Addr.isValid() ? Addr.getPointer() : nullptr,
295  (IsVolatile ? IS_VOLATILE : 0) | (IsUnused ? IS_UNUSED : 0)),
296  Alignment(Addr.isValid() ? Addr.getAlignment() : CharUnits::Zero()) {}
297 
298  bool isNull() const { return !getValue().isValid(); }
299 
300  bool isVolatile() const { return Value.getInt() & IS_VOLATILE; }
301  Address getValue() const { return Address(Value.getPointer(), Alignment); }
302  bool isUnused() const { return Value.getInt() & IS_UNUSED; }
303  };
304 
305 } // end namespace CodeGen
306 } // end namespace clang
307 
308 #endif
ReturnValueSlot - Contains the address where the return value of a function can be stored...
Definition: CGCall.h:281
FunctionDecl - An instance of this class is created to represent a function declaration or definition...
Definition: Decl.h:1698
A (possibly-)qualified type.
Definition: Type.h:653
CGCallee(const CGCalleeInfo &abstractInfo, llvm::Value *functionPtr)
Construct a callee.
Definition: CGCall.h:103
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
Definition: Dominators.h:26
const Decl * getCalleeDecl() const
Definition: CGCall.h:62
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:232
llvm::Instruction * getStackBase() const
Definition: CGCall.h:254
llvm::Value * getFunctionPointer() const
Definition: CGCall.h:157
Address getValue() const
Definition: CGCall.h:301
CGCalleeInfo(const Decl *calleeDecl)
Definition: CGCall.h:56
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
EHScopeStack::stable_iterator Cleanup
Definition: CGCall.h:200
bool isUsingInAlloca() const
Returns if we&#39;re using an inalloca struct to pass arguments in memory.
Definition: CGCall.h:259
bool isOrdinary() const
Definition: CGCall.h:150
void addArgCleanupDeactivation(EHScopeStack::stable_iterator Cleanup, llvm::Instruction *IsActiveIP)
Definition: CGCall.h:241
llvm::FunctionType * getFunctionType() const
Definition: CGCall.h:161
const CXXPseudoDestructorExpr * getPseudoDestructorExpr() const
Definition: CGCall.h:145
CharUnits - This is an opaque type for sizes expressed in character units.
Definition: CharUnits.h:38
const FunctionDecl * getBuiltinDecl() const
Definition: CGCall.h:133
CallArg(RValue rv, QualType ty, bool needscopy)
Definition: CGCall.h:175
void setFunctionPointer(llvm::Value *functionPtr)
Definition: CGCall.h:165
llvm::iterator_range< SmallVectorImpl< Writeback >::const_iterator > writeback_const_range
Definition: CGCall.h:235
Represents a C++ pseudo-destructor (C++ [expr.pseudo]).
Definition: ExprCXX.h:2197
void addFrom(const CallArgList &other)
Add all the arguments from another CallArgList to this one.
Definition: CGCall.h:214
Represents a prototype with parameter type info, e.g.
Definition: Type.h:3270
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:142
writeback_const_range writebacks() const
Definition: CGCall.h:237
void addWriteback(LValue srcLV, Address temporary, llvm::Value *toUse)
Definition: CGCall.h:226
PseudoDestructorInfoStorage PseudoDestructorInfo
Definition: CGCall.h:87
Address Temporary
The temporary alloca.
Definition: CGCall.h:193
llvm::Value * ToUse
A value to "use" after the writeback, or null.
Definition: CGCall.h:196
Expr - This represents one expression.
Definition: Expr.h:106
CGCalleeInfo(const FunctionProtoType *calleeProtoTy)
Definition: CGCall.h:54
static CGCallee forDirect(llvm::Constant *functionPtr, const CGCalleeInfo &abstractInfo=CGCalleeInfo())
Definition: CGCall.h:125
void add(RValue rvalue, QualType type, bool needscopy=false)
Definition: CGCall.h:207
static SVal getValue(SVal val, SValBuilder &svalBuilder)
BuiltinInfoStorage BuiltinInfo
Definition: CGCall.h:86
CGCalleeInfo AbstractInfo
Definition: CGCall.h:85
__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
The l-value was considered opaque, so the alignment was determined from a type.
bool isBuiltin() const
Definition: CGCall.h:130
A saved depth on the scope stack.
Definition: EHScopeStack.h:107
An aligned address.
Definition: Address.h:25
All available information about a concrete callee.
Definition: CGCall.h:66
CGCalleeInfo(const FunctionProtoType *calleeProtoTy, const Decl *calleeDecl)
Definition: CGCall.h:52
FunctionArgList - Type for representing both the decl and type of parameters to a function...
Definition: CGCall.h:276
const CGCalleeInfo & getAbstractInfo() const
Definition: CGCall.h:153
llvm::Instruction * IsActiveIP
The "is active" insertion point.
Definition: CGCall.h:204
Dataflow Directional Tag Classes.
LValue Source
The original argument.
Definition: CGCall.h:190
static CGCallee forBuiltin(unsigned builtinID, const FunctionDecl *builtinDecl)
Definition: CGCall.h:111
unsigned getBuiltinID() const
Definition: CGCall.h:137
static CGCallee forPseudoDestructor(const CXXPseudoDestructorExpr *E)
Definition: CGCall.h:119
ReturnValueSlot(Address Addr, bool IsVolatile, bool IsUnused=false)
Definition: CGCall.h:293
unsigned kind
All of the diagnostics that can be emitted by the frontend.
Definition: DiagnosticIDs.h:61
const FunctionProtoType * getCalleeFunctionProtoType() const
Definition: CGCall.h:59
LValue - This represents an lvalue references.
Definition: CGValue.h:167
CallArgList - Type for representing both the value and type of arguments in a call.
Definition: CGCall.h:182
Abstract information about a function or function prototype.
Definition: CGCall.h:44
ArrayRef< CallArgCleanup > getCleanupsToDeactivate() const
Definition: CGCall.h:249