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.
42 typedef SmallVector<llvm::AttributeSet, 8> AttributeListType;
44 /// Abstract information about a function or function prototype.
46 /// \brief The function prototype of the callee.
47 const FunctionProtoType *CalleeProtoTy;
48 /// \brief The function declaration of the callee.
49 const Decl *CalleeDecl;
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) {}
60 const FunctionProtoType *getCalleeFunctionProtoType() const {
63 const Decl *getCalleeDecl() const { return CalleeDecl; }
66 /// All available information about a concrete callee.
68 enum class SpecialKind : uintptr_t {
73 Last = PseudoDestructor
76 struct BuiltinInfoStorage {
77 const FunctionDecl *Decl;
80 struct PseudoDestructorInfoStorage {
81 const CXXPseudoDestructorExpr *Expr;
84 SpecialKind KindOrFunctionPointer;
86 CGCalleeInfo AbstractInfo;
87 BuiltinInfoStorage BuiltinInfo;
88 PseudoDestructorInfoStorage PseudoDestructorInfo;
91 explicit CGCallee(SpecialKind kind) : KindOrFunctionPointer(kind) {}
93 CGCallee(const FunctionDecl *builtinDecl, unsigned builtinID)
94 : KindOrFunctionPointer(SpecialKind::Builtin) {
95 BuiltinInfo.Decl = builtinDecl;
96 BuiltinInfo.ID = builtinID;
100 CGCallee() : KindOrFunctionPointer(SpecialKind::Invalid) {}
102 /// Construct a callee. Call this constructor directly when this
103 /// isn't a direct call.
104 CGCallee(const CGCalleeInfo &abstractInfo, llvm::Value *functionPtr)
105 : KindOrFunctionPointer(SpecialKind(uintptr_t(functionPtr))) {
106 AbstractInfo = abstractInfo;
107 assert(functionPtr && "configuring callee without function pointer");
108 assert(functionPtr->getType()->isPointerTy());
109 assert(functionPtr->getType()->getPointerElementType()->isFunctionTy());
112 static CGCallee forBuiltin(unsigned builtinID,
113 const FunctionDecl *builtinDecl) {
114 CGCallee result(SpecialKind::Builtin);
115 result.BuiltinInfo.Decl = builtinDecl;
116 result.BuiltinInfo.ID = builtinID;
120 static CGCallee forPseudoDestructor(const CXXPseudoDestructorExpr *E) {
121 CGCallee result(SpecialKind::PseudoDestructor);
122 result.PseudoDestructorInfo.Expr = E;
126 static CGCallee forDirect(llvm::Constant *functionPtr,
127 const CGCalleeInfo &abstractInfo = CGCalleeInfo()) {
128 return CGCallee(abstractInfo, functionPtr);
131 bool isBuiltin() const {
132 return KindOrFunctionPointer == SpecialKind::Builtin;
134 const FunctionDecl *getBuiltinDecl() const {
136 return BuiltinInfo.Decl;
138 unsigned getBuiltinID() const {
140 return BuiltinInfo.ID;
143 bool isPseudoDestructor() const {
144 return KindOrFunctionPointer == SpecialKind::PseudoDestructor;
146 const CXXPseudoDestructorExpr *getPseudoDestructorExpr() const {
147 assert(isPseudoDestructor());
148 return PseudoDestructorInfo.Expr;
151 bool isOrdinary() const {
152 return uintptr_t(KindOrFunctionPointer) > uintptr_t(SpecialKind::Last);
154 const CGCalleeInfo &getAbstractInfo() const {
155 assert(isOrdinary());
158 llvm::Value *getFunctionPointer() const {
159 assert(isOrdinary());
160 return reinterpret_cast<llvm::Value*>(uintptr_t(KindOrFunctionPointer));
162 llvm::FunctionType *getFunctionType() const {
163 return cast<llvm::FunctionType>(
164 getFunctionPointer()->getType()->getPointerElementType());
166 void setFunctionPointer(llvm::Value *functionPtr) {
167 assert(isOrdinary());
168 KindOrFunctionPointer = SpecialKind(uintptr_t(functionPtr));
176 CallArg(RValue rv, QualType ty, bool needscopy)
177 : RV(rv), Ty(ty), NeedsCopy(needscopy)
181 /// CallArgList - Type for representing both the value and type of
182 /// arguments in a call.
184 public SmallVector<CallArg, 16> {
186 CallArgList() : StackBase(nullptr) {}
189 /// The original argument. Note that the argument l-value
190 /// is potentially null.
193 /// The temporary alloca.
196 /// A value to "use" after the writeback, or null.
200 struct CallArgCleanup {
201 EHScopeStack::stable_iterator Cleanup;
203 /// The "is active" insertion point. This instruction is temporary and
204 /// will be removed after insertion.
205 llvm::Instruction *IsActiveIP;
208 void add(RValue rvalue, QualType type, bool needscopy = false) {
209 push_back(CallArg(rvalue, type, needscopy));
212 /// Add all the arguments from another CallArgList to this one. After doing
213 /// this, the old CallArgList retains its list of arguments, but must not
214 /// be used to emit a call.
215 void addFrom(const CallArgList &other) {
216 insert(end(), other.begin(), other.end());
217 Writebacks.insert(Writebacks.end(),
218 other.Writebacks.begin(), other.Writebacks.end());
219 CleanupsToDeactivate.insert(CleanupsToDeactivate.end(),
220 other.CleanupsToDeactivate.begin(),
221 other.CleanupsToDeactivate.end());
222 assert(!(StackBase && other.StackBase) && "can't merge stackbases");
224 StackBase = other.StackBase;
227 void addWriteback(LValue srcLV, Address temporary,
228 llvm::Value *toUse) {
229 Writeback writeback = { srcLV, temporary, toUse };
230 Writebacks.push_back(writeback);
233 bool hasWritebacks() const { return !Writebacks.empty(); }
235 typedef llvm::iterator_range<SmallVectorImpl<Writeback>::const_iterator>
236 writeback_const_range;
238 writeback_const_range writebacks() const {
239 return writeback_const_range(Writebacks.begin(), Writebacks.end());
242 void addArgCleanupDeactivation(EHScopeStack::stable_iterator Cleanup,
243 llvm::Instruction *IsActiveIP) {
244 CallArgCleanup ArgCleanup;
245 ArgCleanup.Cleanup = Cleanup;
246 ArgCleanup.IsActiveIP = IsActiveIP;
247 CleanupsToDeactivate.push_back(ArgCleanup);
250 ArrayRef<CallArgCleanup> getCleanupsToDeactivate() const {
251 return CleanupsToDeactivate;
254 void allocateArgumentMemory(CodeGenFunction &CGF);
255 llvm::Instruction *getStackBase() const { return StackBase; }
256 void freeArgumentMemory(CodeGenFunction &CGF) const;
258 /// \brief Returns if we're using an inalloca struct to pass arguments in
260 bool isUsingInAlloca() const { return StackBase; }
263 SmallVector<Writeback, 1> Writebacks;
265 /// Deactivate these cleanups immediately before making the call. This
266 /// is used to cleanup objects that are owned by the callee once the call
268 SmallVector<CallArgCleanup, 1> CleanupsToDeactivate;
270 /// The stacksave call. It dominates all of the argument evaluation.
271 llvm::CallInst *StackBase;
274 /// FunctionArgList - Type for representing both the decl and type
275 /// of parameters to a function. The decl must be either a
276 /// ParmVarDecl or ImplicitParamDecl.
277 class FunctionArgList : public SmallVector<const VarDecl*, 16> {
280 /// ReturnValueSlot - Contains the address where the return value of a
281 /// function can be stored, and whether the address is volatile or not.
282 class ReturnValueSlot {
283 llvm::PointerIntPair<llvm::Value *, 2, unsigned int> Value;
286 // Return value slot flags
294 ReturnValueSlot(Address Addr, bool IsVolatile, bool IsUnused = false)
295 : Value(Addr.isValid() ? Addr.getPointer() : nullptr,
296 (IsVolatile ? IS_VOLATILE : 0) | (IsUnused ? IS_UNUSED : 0)),
297 Alignment(Addr.isValid() ? Addr.getAlignment() : CharUnits::Zero()) {}
299 bool isNull() const { return !getValue().isValid(); }
301 bool isVolatile() const { return Value.getInt() & IS_VOLATILE; }
302 Address getValue() const { return Address(Value.getPointer(), Alignment); }
303 bool isUnused() const { return Value.getInt() & IS_UNUSED; }
306 } // end namespace CodeGen
307 } // end namespace clang