1 //===----- CGCall.h - Encapsulate calling convention details ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // These classes wrap the information about a call or function
11 // definition used to handle ABI compliancy.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_LIB_CODEGEN_CGCALL_H
16 #define LLVM_CLANG_LIB_CODEGEN_CGCALL_H
19 #include "EHScopeStack.h"
20 #include "clang/AST/CanonicalType.h"
21 #include "clang/AST/Type.h"
22 #include "llvm/IR/Value.h"
24 // FIXME: Restructure so we don't have to expose so much stuff.
43 /// Abstract information about a function or function prototype.
45 /// \brief The function prototype of the callee.
46 const FunctionProtoType *CalleeProtoTy;
47 /// \brief The function declaration of the callee.
48 const Decl *CalleeDecl;
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) {}
59 const FunctionProtoType *getCalleeFunctionProtoType() const {
62 const Decl *getCalleeDecl() const { return CalleeDecl; }
65 /// All available information about a concrete callee.
67 enum class SpecialKind : uintptr_t {
72 Last = PseudoDestructor
75 struct BuiltinInfoStorage {
76 const FunctionDecl *Decl;
79 struct PseudoDestructorInfoStorage {
80 const CXXPseudoDestructorExpr *Expr;
83 SpecialKind KindOrFunctionPointer;
85 CGCalleeInfo AbstractInfo;
86 BuiltinInfoStorage BuiltinInfo;
87 PseudoDestructorInfoStorage PseudoDestructorInfo;
90 explicit CGCallee(SpecialKind kind) : KindOrFunctionPointer(kind) {}
92 CGCallee(const FunctionDecl *builtinDecl, unsigned builtinID)
93 : KindOrFunctionPointer(SpecialKind::Builtin) {
94 BuiltinInfo.Decl = builtinDecl;
95 BuiltinInfo.ID = builtinID;
99 CGCallee() : KindOrFunctionPointer(SpecialKind::Invalid) {}
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());
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;
119 static CGCallee forPseudoDestructor(const CXXPseudoDestructorExpr *E) {
120 CGCallee result(SpecialKind::PseudoDestructor);
121 result.PseudoDestructorInfo.Expr = E;
125 static CGCallee forDirect(llvm::Constant *functionPtr,
126 const CGCalleeInfo &abstractInfo = CGCalleeInfo()) {
127 return CGCallee(abstractInfo, functionPtr);
130 bool isBuiltin() const {
131 return KindOrFunctionPointer == SpecialKind::Builtin;
133 const FunctionDecl *getBuiltinDecl() const {
135 return BuiltinInfo.Decl;
137 unsigned getBuiltinID() const {
139 return BuiltinInfo.ID;
142 bool isPseudoDestructor() const {
143 return KindOrFunctionPointer == SpecialKind::PseudoDestructor;
145 const CXXPseudoDestructorExpr *getPseudoDestructorExpr() const {
146 assert(isPseudoDestructor());
147 return PseudoDestructorInfo.Expr;
150 bool isOrdinary() const {
151 return uintptr_t(KindOrFunctionPointer) > uintptr_t(SpecialKind::Last);
153 const CGCalleeInfo &getAbstractInfo() const {
154 assert(isOrdinary());
157 llvm::Value *getFunctionPointer() const {
158 assert(isOrdinary());
159 return reinterpret_cast<llvm::Value*>(uintptr_t(KindOrFunctionPointer));
161 llvm::FunctionType *getFunctionType() const {
162 return cast<llvm::FunctionType>(
163 getFunctionPointer()->getType()->getPointerElementType());
165 void setFunctionPointer(llvm::Value *functionPtr) {
166 assert(isOrdinary());
167 KindOrFunctionPointer = SpecialKind(uintptr_t(functionPtr));
175 CallArg(RValue rv, QualType ty, bool needscopy)
176 : RV(rv), Ty(ty), NeedsCopy(needscopy)
180 /// CallArgList - Type for representing both the value and type of
181 /// arguments in a call.
183 public SmallVector<CallArg, 16> {
185 CallArgList() : StackBase(nullptr) {}
188 /// The original argument. Note that the argument l-value
189 /// is potentially null.
192 /// The temporary alloca.
195 /// A value to "use" after the writeback, or null.
199 struct CallArgCleanup {
200 EHScopeStack::stable_iterator Cleanup;
202 /// The "is active" insertion point. This instruction is temporary and
203 /// will be removed after insertion.
204 llvm::Instruction *IsActiveIP;
207 void add(RValue rvalue, QualType type, bool needscopy = false) {
208 push_back(CallArg(rvalue, type, needscopy));
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");
223 StackBase = other.StackBase;
226 void addWriteback(LValue srcLV, Address temporary,
227 llvm::Value *toUse) {
228 Writeback writeback = { srcLV, temporary, toUse };
229 Writebacks.push_back(writeback);
232 bool hasWritebacks() const { return !Writebacks.empty(); }
234 typedef llvm::iterator_range<SmallVectorImpl<Writeback>::const_iterator>
235 writeback_const_range;
237 writeback_const_range writebacks() const {
238 return writeback_const_range(Writebacks.begin(), Writebacks.end());
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);
249 ArrayRef<CallArgCleanup> getCleanupsToDeactivate() const {
250 return CleanupsToDeactivate;
253 void allocateArgumentMemory(CodeGenFunction &CGF);
254 llvm::Instruction *getStackBase() const { return StackBase; }
255 void freeArgumentMemory(CodeGenFunction &CGF) const;
257 /// \brief Returns if we're using an inalloca struct to pass arguments in
259 bool isUsingInAlloca() const { return StackBase; }
262 SmallVector<Writeback, 1> Writebacks;
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
267 SmallVector<CallArgCleanup, 1> CleanupsToDeactivate;
269 /// The stacksave call. It dominates all of the argument evaluation.
270 llvm::CallInst *StackBase;
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> {
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;
285 // Return value slot flags
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()) {}
298 bool isNull() const { return !getValue().isValid(); }
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; }
305 } // end namespace CodeGen
306 } // end namespace clang