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 { |
Reid Kleckner | de86482 | 2017-03-21 16:57:30 +0000 | [diff] [blame] | 28 | class AttributeList; |
| 29 | class Function; |
| 30 | class Type; |
| 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 { |
Daniel Dunbar | 3d7c90b | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 42 | |
Reid Kleckner | de86482 | 2017-03-21 16:57:30 +0000 | [diff] [blame] | 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; |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 49 | |
Reid Kleckner | de86482 | 2017-03-21 16:57:30 +0000 | [diff] [blame] | 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) {} |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 58 | |
Reid Kleckner | de86482 | 2017-03-21 16:57:30 +0000 | [diff] [blame] | 59 | const FunctionProtoType *getCalleeFunctionProtoType() const { |
| 60 | return CalleeProtoTy; |
| 61 | } |
| 62 | const Decl *getCalleeDecl() const { return CalleeDecl; } |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 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 | |
John McCall | aaae302 | 2016-11-07 21:13:27 +0000 | [diff] [blame] | 75 | struct BuiltinInfoStorage { |
| 76 | const FunctionDecl *Decl; |
| 77 | unsigned ID; |
| 78 | }; |
| 79 | struct PseudoDestructorInfoStorage { |
| 80 | const CXXPseudoDestructorExpr *Expr; |
| 81 | }; |
| 82 | |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 83 | SpecialKind KindOrFunctionPointer; |
| 84 | union { |
| 85 | CGCalleeInfo AbstractInfo; |
John McCall | aaae302 | 2016-11-07 21:13:27 +0000 | [diff] [blame] | 86 | BuiltinInfoStorage BuiltinInfo; |
| 87 | PseudoDestructorInfoStorage PseudoDestructorInfo; |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 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 | |
Eli Friedman | f4258eb | 2011-05-02 18:05:27 +0000 | [diff] [blame] | 171 | struct CallArg { |
| 172 | RValue RV; |
| 173 | QualType Ty; |
Eli Friedman | df96819 | 2011-05-26 00:10:27 +0000 | [diff] [blame] | 174 | bool NeedsCopy; |
| 175 | CallArg(RValue rv, QualType ty, bool needscopy) |
| 176 | : RV(rv), Ty(ty), NeedsCopy(needscopy) |
Eli Friedman | f4258eb | 2011-05-02 18:05:27 +0000 | [diff] [blame] | 177 | { } |
| 178 | }; |
| 179 | |
Daniel Dunbar | 3d7c90b | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 180 | /// CallArgList - Type for representing both the value and type of |
| 181 | /// arguments in a call. |
John McCall | a738c25 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 182 | class CallArgList : |
Chris Lattner | 01cf8db | 2011-07-20 06:58:45 +0000 | [diff] [blame] | 183 | public SmallVector<CallArg, 16> { |
John McCall | 32ea969 | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 184 | public: |
Reid Kleckner | 7c2f9e8 | 2015-10-08 00:17:45 +0000 | [diff] [blame] | 185 | CallArgList() : StackBase(nullptr) {} |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 186 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 187 | struct Writeback { |
John McCall | eff1884 | 2013-03-23 02:35:54 +0000 | [diff] [blame] | 188 | /// The original argument. Note that the argument l-value |
| 189 | /// is potentially null. |
| 190 | LValue Source; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 191 | |
| 192 | /// The temporary alloca. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 193 | Address Temporary; |
John McCall | eff1884 | 2013-03-23 02:35:54 +0000 | [diff] [blame] | 194 | |
| 195 | /// A value to "use" after the writeback, or null. |
| 196 | llvm::Value *ToUse; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 197 | }; |
| 198 | |
Reid Kleckner | 23f4c4b | 2013-06-21 12:45:15 +0000 | [diff] [blame] | 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 | |
Eli Friedman | df96819 | 2011-05-26 00:10:27 +0000 | [diff] [blame] | 207 | void add(RValue rvalue, QualType type, bool needscopy = false) { |
| 208 | push_back(CallArg(rvalue, type, needscopy)); |
John McCall | 32ea969 | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 209 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 210 | |
Richard Smith | 762672a | 2016-09-28 19:09:10 +0000 | [diff] [blame] | 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. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 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()); |
Richard Smith | 762672a | 2016-09-28 19:09:10 +0000 | [diff] [blame] | 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; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 224 | } |
| 225 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 226 | void addWriteback(LValue srcLV, Address temporary, |
John McCall | eff1884 | 2013-03-23 02:35:54 +0000 | [diff] [blame] | 227 | llvm::Value *toUse) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 228 | Writeback writeback = { srcLV, temporary, toUse }; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 229 | Writebacks.push_back(writeback); |
| 230 | } |
| 231 | |
| 232 | bool hasWritebacks() const { return !Writebacks.empty(); } |
| 233 | |
Aaron Ballman | 36a7fa8 | 2014-03-17 17:22:27 +0000 | [diff] [blame] | 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 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 240 | |
Reid Kleckner | 23f4c4b | 2013-06-21 12:45:15 +0000 | [diff] [blame] | 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 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 253 | void allocateArgumentMemory(CodeGenFunction &CGF); |
| 254 | llvm::Instruction *getStackBase() const { return StackBase; } |
Nico Weber | 8cdb3f9 | 2015-08-25 18:43:32 +0000 | [diff] [blame] | 255 | void freeArgumentMemory(CodeGenFunction &CGF) const; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 256 | |
| 257 | /// \brief Returns if we're using an inalloca struct to pass arguments in |
| 258 | /// memory. |
| 259 | bool isUsingInAlloca() const { return StackBase; } |
| 260 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 261 | private: |
Chris Lattner | 01cf8db | 2011-07-20 06:58:45 +0000 | [diff] [blame] | 262 | SmallVector<Writeback, 1> Writebacks; |
Reid Kleckner | 23f4c4b | 2013-06-21 12:45:15 +0000 | [diff] [blame] | 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; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 268 | |
| 269 | /// The stacksave call. It dominates all of the argument evaluation. |
| 270 | llvm::CallInst *StackBase; |
John McCall | a738c25 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 271 | }; |
Daniel Dunbar | 3d7c90b | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 272 | |
Daniel Dunbar | bc915f4 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 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. |
Chris Lattner | 01cf8db | 2011-07-20 06:58:45 +0000 | [diff] [blame] | 276 | class FunctionArgList : public SmallVector<const VarDecl*, 16> { |
John McCall | a738c25 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 277 | }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 278 | |
Anders Carlsson | 1749083 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 279 | /// ReturnValueSlot - Contains the address where the return value of a |
| 280 | /// function can be stored, and whether the address is volatile or not. |
Anders Carlsson | 0435ed5 | 2009-12-24 19:08:58 +0000 | [diff] [blame] | 281 | class ReturnValueSlot { |
Leny Kholodov | 6aab111 | 2015-06-08 10:23:49 +0000 | [diff] [blame] | 282 | llvm::PointerIntPair<llvm::Value *, 2, unsigned int> Value; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 283 | CharUnits Alignment; |
Leny Kholodov | 6aab111 | 2015-06-08 10:23:49 +0000 | [diff] [blame] | 284 | |
| 285 | // Return value slot flags |
| 286 | enum Flags { |
| 287 | IS_VOLATILE = 0x1, |
| 288 | IS_UNUSED = 0x2, |
| 289 | }; |
Anders Carlsson | 0435ed5 | 2009-12-24 19:08:58 +0000 | [diff] [blame] | 290 | |
| 291 | public: |
| 292 | ReturnValueSlot() {} |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 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()) {} |
Anders Carlsson | 0435ed5 | 2009-12-24 19:08:58 +0000 | [diff] [blame] | 297 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 298 | bool isNull() const { return !getValue().isValid(); } |
Leny Kholodov | 6aab111 | 2015-06-08 10:23:49 +0000 | [diff] [blame] | 299 | |
| 300 | bool isVolatile() const { return Value.getInt() & IS_VOLATILE; } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 301 | Address getValue() const { return Address(Value.getPointer(), Alignment); } |
Leny Kholodov | 6aab111 | 2015-06-08 10:23:49 +0000 | [diff] [blame] | 302 | bool isUnused() const { return Value.getInt() & IS_UNUSED; } |
Anders Carlsson | 0435ed5 | 2009-12-24 19:08:58 +0000 | [diff] [blame] | 303 | }; |
| 304 | |
Daniel Dunbar | 3d7c90b | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 305 | } // end namespace CodeGen |
| 306 | } // end namespace clang |
| 307 | |
| 308 | #endif |