Daniel Dunbar | 3d7c90b | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 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 | |
Benjamin Kramer | 2f5db8b | 2014-08-13 16:25:19 +0000 | [diff] [blame] | 15 | #ifndef LLVM_CLANG_LIB_CODEGEN_CGCALL_H |
| 16 | #define LLVM_CLANG_LIB_CODEGEN_CGCALL_H |
Daniel Dunbar | 3d7c90b | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 17 | |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 18 | #include "CGValue.h" |
Reid Kleckner | 23f4c4b | 2013-06-21 12:45:15 +0000 | [diff] [blame] | 19 | #include "EHScopeStack.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include "clang/AST/CanonicalType.h" |
| 21 | #include "clang/AST/Type.h" |
Chandler Carruth | ffd5551 | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Value.h" |
Daniel Dunbar | 41cf9de | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 23 | |
Daniel Dunbar | 313321e | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 24 | // FIXME: Restructure so we don't have to expose so much stuff. |
| 25 | #include "ABIInfo.h" |
| 26 | |
Daniel Dunbar | 3d7c90b | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 27 | namespace llvm { |
Bill Wendling | 290d952 | 2013-01-27 02:46:53 +0000 | [diff] [blame] | 28 | class AttributeSet; |
Daniel Dunbar | 313321e | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 29 | class Function; |
| 30 | class Type; |
Daniel Dunbar | 3d7c90b | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 31 | class Value; |
Daniel Dunbar | 3d7c90b | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | namespace clang { |
| 35 | class ASTContext; |
| 36 | class Decl; |
| 37 | class FunctionDecl; |
| 38 | class ObjCMethodDecl; |
Daniel Dunbar | bc915f4 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 39 | class VarDecl; |
Daniel Dunbar | 3d7c90b | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 40 | |
| 41 | namespace CodeGen { |
Bill Wendling | 290d952 | 2013-01-27 02:46:53 +0000 | [diff] [blame] | 42 | typedef SmallVector<llvm::AttributeSet, 8> AttributeListType; |
Daniel Dunbar | 3d7c90b | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 43 | |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 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 | |
| 60 | const FunctionProtoType *getCalleeFunctionProtoType() const { |
| 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 | |
| 73 | Last = PseudoDestructor |
| 74 | }; |
| 75 | |
John McCall | aaae302 | 2016-11-07 21:13:27 +0000 | [diff] [blame] | 76 | struct BuiltinInfoStorage { |
| 77 | const FunctionDecl *Decl; |
| 78 | unsigned ID; |
| 79 | }; |
| 80 | struct PseudoDestructorInfoStorage { |
| 81 | const CXXPseudoDestructorExpr *Expr; |
| 82 | }; |
| 83 | |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 84 | SpecialKind KindOrFunctionPointer; |
| 85 | union { |
| 86 | CGCalleeInfo AbstractInfo; |
John McCall | aaae302 | 2016-11-07 21:13:27 +0000 | [diff] [blame] | 87 | BuiltinInfoStorage BuiltinInfo; |
| 88 | PseudoDestructorInfoStorage PseudoDestructorInfo; |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | explicit CGCallee(SpecialKind kind) : KindOrFunctionPointer(kind) {} |
| 92 | |
| 93 | CGCallee(const FunctionDecl *builtinDecl, unsigned builtinID) |
| 94 | : KindOrFunctionPointer(SpecialKind::Builtin) { |
| 95 | BuiltinInfo.Decl = builtinDecl; |
| 96 | BuiltinInfo.ID = builtinID; |
| 97 | } |
| 98 | |
| 99 | public: |
| 100 | CGCallee() : KindOrFunctionPointer(SpecialKind::Invalid) {} |
| 101 | |
| 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()); |
| 110 | } |
| 111 | |
| 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; |
| 117 | return result; |
| 118 | } |
| 119 | |
| 120 | static CGCallee forPseudoDestructor(const CXXPseudoDestructorExpr *E) { |
| 121 | CGCallee result(SpecialKind::PseudoDestructor); |
| 122 | result.PseudoDestructorInfo.Expr = E; |
| 123 | return result; |
| 124 | } |
| 125 | |
| 126 | static CGCallee forDirect(llvm::Constant *functionPtr, |
| 127 | const CGCalleeInfo &abstractInfo = CGCalleeInfo()) { |
| 128 | return CGCallee(abstractInfo, functionPtr); |
| 129 | } |
| 130 | |
| 131 | bool isBuiltin() const { |
| 132 | return KindOrFunctionPointer == SpecialKind::Builtin; |
| 133 | } |
| 134 | const FunctionDecl *getBuiltinDecl() const { |
| 135 | assert(isBuiltin()); |
| 136 | return BuiltinInfo.Decl; |
| 137 | } |
| 138 | unsigned getBuiltinID() const { |
| 139 | assert(isBuiltin()); |
| 140 | return BuiltinInfo.ID; |
| 141 | } |
| 142 | |
| 143 | bool isPseudoDestructor() const { |
| 144 | return KindOrFunctionPointer == SpecialKind::PseudoDestructor; |
| 145 | } |
| 146 | const CXXPseudoDestructorExpr *getPseudoDestructorExpr() const { |
| 147 | assert(isPseudoDestructor()); |
| 148 | return PseudoDestructorInfo.Expr; |
| 149 | } |
| 150 | |
| 151 | bool isOrdinary() const { |
| 152 | return uintptr_t(KindOrFunctionPointer) > uintptr_t(SpecialKind::Last); |
| 153 | } |
| 154 | const CGCalleeInfo &getAbstractInfo() const { |
| 155 | assert(isOrdinary()); |
| 156 | return AbstractInfo; |
| 157 | } |
| 158 | llvm::Value *getFunctionPointer() const { |
| 159 | assert(isOrdinary()); |
| 160 | return reinterpret_cast<llvm::Value*>(uintptr_t(KindOrFunctionPointer)); |
| 161 | } |
| 162 | llvm::FunctionType *getFunctionType() const { |
| 163 | return cast<llvm::FunctionType>( |
| 164 | getFunctionPointer()->getType()->getPointerElementType()); |
| 165 | } |
| 166 | void setFunctionPointer(llvm::Value *functionPtr) { |
| 167 | assert(isOrdinary()); |
| 168 | KindOrFunctionPointer = SpecialKind(uintptr_t(functionPtr)); |
| 169 | } |
| 170 | }; |
| 171 | |
Eli Friedman | f4258eb | 2011-05-02 18:05:27 +0000 | [diff] [blame] | 172 | struct CallArg { |
| 173 | RValue RV; |
| 174 | QualType Ty; |
Eli Friedman | df96819 | 2011-05-26 00:10:27 +0000 | [diff] [blame] | 175 | bool NeedsCopy; |
| 176 | CallArg(RValue rv, QualType ty, bool needscopy) |
| 177 | : RV(rv), Ty(ty), NeedsCopy(needscopy) |
Eli Friedman | f4258eb | 2011-05-02 18:05:27 +0000 | [diff] [blame] | 178 | { } |
| 179 | }; |
| 180 | |
Daniel Dunbar | 3d7c90b | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 181 | /// CallArgList - Type for representing both the value and type of |
| 182 | /// arguments in a call. |
John McCall | a738c25 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 183 | class CallArgList : |
Chris Lattner | 01cf8db | 2011-07-20 06:58:45 +0000 | [diff] [blame] | 184 | public SmallVector<CallArg, 16> { |
John McCall | 32ea969 | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 185 | public: |
Reid Kleckner | 7c2f9e8 | 2015-10-08 00:17:45 +0000 | [diff] [blame] | 186 | CallArgList() : StackBase(nullptr) {} |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 187 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 188 | struct Writeback { |
John McCall | eff1884 | 2013-03-23 02:35:54 +0000 | [diff] [blame] | 189 | /// The original argument. Note that the argument l-value |
| 190 | /// is potentially null. |
| 191 | LValue Source; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 192 | |
| 193 | /// The temporary alloca. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 194 | Address Temporary; |
John McCall | eff1884 | 2013-03-23 02:35:54 +0000 | [diff] [blame] | 195 | |
| 196 | /// A value to "use" after the writeback, or null. |
| 197 | llvm::Value *ToUse; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 198 | }; |
| 199 | |
Reid Kleckner | 23f4c4b | 2013-06-21 12:45:15 +0000 | [diff] [blame] | 200 | struct CallArgCleanup { |
| 201 | EHScopeStack::stable_iterator Cleanup; |
| 202 | |
| 203 | /// The "is active" insertion point. This instruction is temporary and |
| 204 | /// will be removed after insertion. |
| 205 | llvm::Instruction *IsActiveIP; |
| 206 | }; |
| 207 | |
Eli Friedman | df96819 | 2011-05-26 00:10:27 +0000 | [diff] [blame] | 208 | void add(RValue rvalue, QualType type, bool needscopy = false) { |
| 209 | push_back(CallArg(rvalue, type, needscopy)); |
John McCall | 32ea969 | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 210 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 211 | |
Richard Smith | 762672a | 2016-09-28 19:09:10 +0000 | [diff] [blame] | 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. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 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()); |
Richard Smith | 762672a | 2016-09-28 19:09:10 +0000 | [diff] [blame] | 219 | CleanupsToDeactivate.insert(CleanupsToDeactivate.end(), |
| 220 | other.CleanupsToDeactivate.begin(), |
| 221 | other.CleanupsToDeactivate.end()); |
| 222 | assert(!(StackBase && other.StackBase) && "can't merge stackbases"); |
| 223 | if (!StackBase) |
| 224 | StackBase = other.StackBase; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 225 | } |
| 226 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 227 | void addWriteback(LValue srcLV, Address temporary, |
John McCall | eff1884 | 2013-03-23 02:35:54 +0000 | [diff] [blame] | 228 | llvm::Value *toUse) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 229 | Writeback writeback = { srcLV, temporary, toUse }; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 230 | Writebacks.push_back(writeback); |
| 231 | } |
| 232 | |
| 233 | bool hasWritebacks() const { return !Writebacks.empty(); } |
| 234 | |
Aaron Ballman | 36a7fa8 | 2014-03-17 17:22:27 +0000 | [diff] [blame] | 235 | typedef llvm::iterator_range<SmallVectorImpl<Writeback>::const_iterator> |
| 236 | writeback_const_range; |
| 237 | |
| 238 | writeback_const_range writebacks() const { |
| 239 | return writeback_const_range(Writebacks.begin(), Writebacks.end()); |
| 240 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 241 | |
Reid Kleckner | 23f4c4b | 2013-06-21 12:45:15 +0000 | [diff] [blame] | 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); |
| 248 | } |
| 249 | |
| 250 | ArrayRef<CallArgCleanup> getCleanupsToDeactivate() const { |
| 251 | return CleanupsToDeactivate; |
| 252 | } |
| 253 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 254 | void allocateArgumentMemory(CodeGenFunction &CGF); |
| 255 | llvm::Instruction *getStackBase() const { return StackBase; } |
Nico Weber | 8cdb3f9 | 2015-08-25 18:43:32 +0000 | [diff] [blame] | 256 | void freeArgumentMemory(CodeGenFunction &CGF) const; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 257 | |
| 258 | /// \brief Returns if we're using an inalloca struct to pass arguments in |
| 259 | /// memory. |
| 260 | bool isUsingInAlloca() const { return StackBase; } |
| 261 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 262 | private: |
Chris Lattner | 01cf8db | 2011-07-20 06:58:45 +0000 | [diff] [blame] | 263 | SmallVector<Writeback, 1> Writebacks; |
Reid Kleckner | 23f4c4b | 2013-06-21 12:45:15 +0000 | [diff] [blame] | 264 | |
| 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 |
| 267 | /// occurs. |
| 268 | SmallVector<CallArgCleanup, 1> CleanupsToDeactivate; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 269 | |
| 270 | /// The stacksave call. It dominates all of the argument evaluation. |
| 271 | llvm::CallInst *StackBase; |
John McCall | a738c25 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 272 | }; |
Daniel Dunbar | 3d7c90b | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 273 | |
Daniel Dunbar | bc915f4 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 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. |
Chris Lattner | 01cf8db | 2011-07-20 06:58:45 +0000 | [diff] [blame] | 277 | class FunctionArgList : public SmallVector<const VarDecl*, 16> { |
John McCall | a738c25 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 278 | }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 279 | |
Anders Carlsson | 1749083 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 280 | /// ReturnValueSlot - Contains the address where the return value of a |
| 281 | /// function can be stored, and whether the address is volatile or not. |
Anders Carlsson | 0435ed5 | 2009-12-24 19:08:58 +0000 | [diff] [blame] | 282 | class ReturnValueSlot { |
Leny Kholodov | 6aab111 | 2015-06-08 10:23:49 +0000 | [diff] [blame] | 283 | llvm::PointerIntPair<llvm::Value *, 2, unsigned int> Value; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 284 | CharUnits Alignment; |
Leny Kholodov | 6aab111 | 2015-06-08 10:23:49 +0000 | [diff] [blame] | 285 | |
| 286 | // Return value slot flags |
| 287 | enum Flags { |
| 288 | IS_VOLATILE = 0x1, |
| 289 | IS_UNUSED = 0x2, |
| 290 | }; |
Anders Carlsson | 0435ed5 | 2009-12-24 19:08:58 +0000 | [diff] [blame] | 291 | |
| 292 | public: |
| 293 | ReturnValueSlot() {} |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 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()) {} |
Anders Carlsson | 0435ed5 | 2009-12-24 19:08:58 +0000 | [diff] [blame] | 298 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 299 | bool isNull() const { return !getValue().isValid(); } |
Leny Kholodov | 6aab111 | 2015-06-08 10:23:49 +0000 | [diff] [blame] | 300 | |
| 301 | bool isVolatile() const { return Value.getInt() & IS_VOLATILE; } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 302 | Address getValue() const { return Address(Value.getPointer(), Alignment); } |
Leny Kholodov | 6aab111 | 2015-06-08 10:23:49 +0000 | [diff] [blame] | 303 | bool isUnused() const { return Value.getInt() & IS_UNUSED; } |
Anders Carlsson | 0435ed5 | 2009-12-24 19:08:58 +0000 | [diff] [blame] | 304 | }; |
| 305 | |
Daniel Dunbar | 3d7c90b | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 306 | } // end namespace CodeGen |
| 307 | } // end namespace clang |
| 308 | |
| 309 | #endif |