]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/lib/CodeGen/CGCall.h
Merge llvm, clang, lld and lldb trunk r300890, and update build glue.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / lib / CodeGen / CGCall.h
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"
20 #include "clang/AST/CanonicalType.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
59   const FunctionProtoType *getCalleeFunctionProtoType() const {
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 {
80       const CXXPseudoDestructorExpr *Expr;
81     };
82
83     SpecialKind KindOrFunctionPointer;
84     union {
85       CGCalleeInfo AbstractInfo;
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
119     static CGCallee forPseudoDestructor(const CXXPseudoDestructorExpr *E) {
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     }
145     const CXXPseudoDestructorExpr *getPseudoDestructorExpr() const {
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     }
157     llvm::Value *getFunctionPointer() const {
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 {
172     RValue RV;
173     QualType Ty;
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.
190       LValue Source;
191
192       /// The temporary alloca.
193       Address Temporary;
194
195       /// A value to "use" after the writeback, or null.
196       llvm::Value *ToUse;
197     };
198
199     struct CallArgCleanup {
200       EHScopeStack::stable_iterator Cleanup;
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>
235       writeback_const_range;
236
237     writeback_const_range writebacks() const {
238       return writeback_const_range(Writebacks.begin(), Writebacks.end());
239     }
240
241     void addArgCleanupDeactivation(EHScopeStack::stable_iterator Cleanup,
242                                    llvm::Instruction *IsActiveIP) {
243       CallArgCleanup ArgCleanup;
244       ArgCleanup.Cleanup = Cleanup;
245       ArgCleanup.IsActiveIP = IsActiveIP;
246       CleanupsToDeactivate.push_back(ArgCleanup);
247     }
248
249     ArrayRef<CallArgCleanup> getCleanupsToDeactivate() const {
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.
281   class ReturnValueSlot {
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:
292     ReturnValueSlot() {}
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