Nick Lewycky | 5d4a755 | 2013-10-01 21:51:38 +0000 | [diff] [blame] | 1 | //===--- CGCall.cpp - Encapsulate calling convention details --------------===// |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 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 | #include "CGCall.h" |
Chris Lattner | ce93399 | 2010-06-29 16:40:28 +0000 | [diff] [blame] | 16 | #include "ABIInfo.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 17 | #include "CGCXXABI.h" |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 18 | #include "CodeGenFunction.h" |
Daniel Dunbar | b768807 | 2008-09-10 00:41:16 +0000 | [diff] [blame] | 19 | #include "CodeGenModule.h" |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 20 | #include "TargetInfo.h" |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 21 | #include "clang/AST/Decl.h" |
Anders Carlsson | f6f8ae5 | 2009-04-03 22:48:58 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclCXX.h" |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 23 | #include "clang/AST/DeclObjC.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 24 | #include "clang/Basic/TargetInfo.h" |
Mark Lacey | 8b54999 | 2013-10-30 21:53:58 +0000 | [diff] [blame] | 25 | #include "clang/CodeGen/CGFunctionInfo.h" |
Chandler Carruth | 06057ce | 2010-06-15 23:19:56 +0000 | [diff] [blame] | 26 | #include "clang/Frontend/CodeGenOptions.h" |
Bill Wendling | be9e8bf | 2013-02-28 22:49:57 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/StringExtras.h" |
Chandler Carruth | 3b844ba | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 28 | #include "llvm/IR/Attributes.h" |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 29 | #include "llvm/IR/CallSite.h" |
Chandler Carruth | 3b844ba | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 30 | #include "llvm/IR/DataLayout.h" |
| 31 | #include "llvm/IR/InlineAsm.h" |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 32 | #include "llvm/IR/Intrinsics.h" |
Eli Friedman | 97cb5a4 | 2011-06-15 22:09:18 +0000 | [diff] [blame] | 33 | #include "llvm/Transforms/Utils/Local.h" |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 34 | using namespace clang; |
| 35 | using namespace CodeGen; |
| 36 | |
| 37 | /***/ |
| 38 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 39 | static unsigned ClangCallConvToLLVMCallConv(CallingConv CC) { |
| 40 | switch (CC) { |
| 41 | default: return llvm::CallingConv::C; |
| 42 | case CC_X86StdCall: return llvm::CallingConv::X86_StdCall; |
| 43 | case CC_X86FastCall: return llvm::CallingConv::X86_FastCall; |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 44 | case CC_X86ThisCall: return llvm::CallingConv::X86_ThisCall; |
Charles Davis | e8519c3 | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 45 | case CC_X86_64Win64: return llvm::CallingConv::X86_64_Win64; |
| 46 | case CC_X86_64SysV: return llvm::CallingConv::X86_64_SysV; |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 47 | case CC_AAPCS: return llvm::CallingConv::ARM_AAPCS; |
| 48 | case CC_AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Guy Benyei | 3898008 | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 49 | case CC_IntelOclBicc: return llvm::CallingConv::Intel_OCL_BI; |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 50 | // TODO: add support for CC_X86Pascal to llvm |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | |
John McCall | 0b0ef0a | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 54 | /// Derives the 'this' type for codegen purposes, i.e. ignoring method |
| 55 | /// qualification. |
| 56 | /// FIXME: address space qualification? |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 57 | static CanQualType GetThisType(ASTContext &Context, const CXXRecordDecl *RD) { |
| 58 | QualType RecTy = Context.getTagDeclType(RD)->getCanonicalTypeInternal(); |
| 59 | return Context.getPointerType(CanQualType::CreateUnsafe(RecTy)); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 60 | } |
| 61 | |
John McCall | 0b0ef0a | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 62 | /// Returns the canonical formal type of the given C++ method. |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 63 | static CanQual<FunctionProtoType> GetFormalType(const CXXMethodDecl *MD) { |
| 64 | return MD->getType()->getCanonicalTypeUnqualified() |
| 65 | .getAs<FunctionProtoType>(); |
John McCall | 0b0ef0a | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /// Returns the "extra-canonicalized" return type, which discards |
| 69 | /// qualifiers on the return type. Codegen doesn't care about them, |
| 70 | /// and it makes ABI code a little easier to be able to assume that |
| 71 | /// all parameter and return types are top-level unqualified. |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 72 | static CanQualType GetReturnType(QualType RetTy) { |
| 73 | return RetTy->getCanonicalTypeUnqualified().getUnqualifiedType(); |
John McCall | 0b0ef0a | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 74 | } |
| 75 | |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 76 | /// Arrange the argument and result information for a value of the given |
| 77 | /// unprototyped freestanding function type. |
John McCall | 0b0ef0a | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 78 | const CGFunctionInfo & |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 79 | CodeGenTypes::arrangeFreeFunctionType(CanQual<FunctionNoProtoType> FTNP) { |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 80 | // When translating an unprototyped function type, always use a |
| 81 | // variadic type. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 82 | return arrangeLLVMFunctionInfo(FTNP->getReturnType().getUnqualifiedType(), |
| 83 | false, None, FTNP->getExtInfo(), |
| 84 | RequiredArgs(0)); |
John McCall | 0b0ef0a | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 85 | } |
| 86 | |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 87 | /// Arrange the LLVM function layout for a value of the given function |
| 88 | /// type, on top of any implicit parameters already stored. Use the |
| 89 | /// given ExtInfo instead of the ExtInfo from the function type. |
| 90 | static const CGFunctionInfo &arrangeLLVMFunctionInfo(CodeGenTypes &CGT, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 91 | bool IsInstanceMethod, |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 92 | SmallVectorImpl<CanQualType> &prefix, |
| 93 | CanQual<FunctionProtoType> FTP, |
| 94 | FunctionType::ExtInfo extInfo) { |
| 95 | RequiredArgs required = RequiredArgs::forPrototypePlus(FTP, prefix.size()); |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 96 | // FIXME: Kill copy. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 97 | for (unsigned i = 0, e = FTP->getNumParams(); i != e; ++i) |
| 98 | prefix.push_back(FTP->getParamType(i)); |
| 99 | CanQualType resultType = FTP->getReturnType().getUnqualifiedType(); |
| 100 | return CGT.arrangeLLVMFunctionInfo(resultType, IsInstanceMethod, prefix, |
| 101 | extInfo, required); |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /// Arrange the argument and result information for a free function (i.e. |
| 105 | /// not a C++ or ObjC instance method) of the given type. |
| 106 | static const CGFunctionInfo &arrangeFreeFunctionType(CodeGenTypes &CGT, |
| 107 | SmallVectorImpl<CanQualType> &prefix, |
| 108 | CanQual<FunctionProtoType> FTP) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 109 | return arrangeLLVMFunctionInfo(CGT, false, prefix, FTP, FTP->getExtInfo()); |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 110 | } |
| 111 | |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 112 | /// Arrange the argument and result information for a free function (i.e. |
| 113 | /// not a C++ or ObjC instance method) of the given type. |
| 114 | static const CGFunctionInfo &arrangeCXXMethodType(CodeGenTypes &CGT, |
| 115 | SmallVectorImpl<CanQualType> &prefix, |
| 116 | CanQual<FunctionProtoType> FTP) { |
| 117 | FunctionType::ExtInfo extInfo = FTP->getExtInfo(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 118 | return arrangeLLVMFunctionInfo(CGT, true, prefix, FTP, extInfo); |
John McCall | 0b0ef0a | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 119 | } |
| 120 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 121 | /// Arrange the argument and result information for a value of the |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 122 | /// given freestanding function type. |
John McCall | 0b0ef0a | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 123 | const CGFunctionInfo & |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 124 | CodeGenTypes::arrangeFreeFunctionType(CanQual<FunctionProtoType> FTP) { |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 125 | SmallVector<CanQualType, 16> argTypes; |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 126 | return ::arrangeFreeFunctionType(*this, argTypes, FTP); |
Daniel Dunbar | bac7c25 | 2009-09-11 22:24:53 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 129 | static CallingConv getCallingConventionForDecl(const Decl *D, bool IsWindows) { |
Daniel Dunbar | bac7c25 | 2009-09-11 22:24:53 +0000 | [diff] [blame] | 130 | // Set the appropriate calling convention for the Function. |
| 131 | if (D->hasAttr<StdCallAttr>()) |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 132 | return CC_X86StdCall; |
Daniel Dunbar | bac7c25 | 2009-09-11 22:24:53 +0000 | [diff] [blame] | 133 | |
| 134 | if (D->hasAttr<FastCallAttr>()) |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 135 | return CC_X86FastCall; |
Daniel Dunbar | bac7c25 | 2009-09-11 22:24:53 +0000 | [diff] [blame] | 136 | |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 137 | if (D->hasAttr<ThisCallAttr>()) |
| 138 | return CC_X86ThisCall; |
| 139 | |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 140 | if (D->hasAttr<PascalAttr>()) |
| 141 | return CC_X86Pascal; |
| 142 | |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 143 | if (PcsAttr *PCS = D->getAttr<PcsAttr>()) |
| 144 | return (PCS->getPCS() == PcsAttr::AAPCS ? CC_AAPCS : CC_AAPCS_VFP); |
| 145 | |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 146 | if (D->hasAttr<PnaclCallAttr>()) |
| 147 | return CC_PnaclCall; |
| 148 | |
Guy Benyei | 3898008 | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 149 | if (D->hasAttr<IntelOclBiccAttr>()) |
| 150 | return CC_IntelOclBicc; |
| 151 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 152 | if (D->hasAttr<MSABIAttr>()) |
| 153 | return IsWindows ? CC_C : CC_X86_64Win64; |
| 154 | |
| 155 | if (D->hasAttr<SysVABIAttr>()) |
| 156 | return IsWindows ? CC_X86_64SysV : CC_C; |
| 157 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 158 | return CC_C; |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 159 | } |
| 160 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 161 | /// Arrange the argument and result information for a call to an |
| 162 | /// unknown C++ non-static member function of the given abstract type. |
Timur Iskhodzhanov | 8f189a9 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 163 | /// (Zero value of RD means we don't have any meaningful "this" argument type, |
| 164 | /// so fall back to a generic pointer type). |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 165 | /// The member function must be an ordinary function, i.e. not a |
| 166 | /// constructor or destructor. |
| 167 | const CGFunctionInfo & |
| 168 | CodeGenTypes::arrangeCXXMethodType(const CXXRecordDecl *RD, |
| 169 | const FunctionProtoType *FTP) { |
| 170 | SmallVector<CanQualType, 16> argTypes; |
John McCall | 0b0ef0a | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 171 | |
Anders Carlsson | 375c31c | 2009-10-03 19:43:08 +0000 | [diff] [blame] | 172 | // Add the 'this' pointer. |
Timur Iskhodzhanov | 8f189a9 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 173 | if (RD) |
| 174 | argTypes.push_back(GetThisType(Context, RD)); |
| 175 | else |
| 176 | argTypes.push_back(Context.VoidPtrTy); |
John McCall | 0b0ef0a | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 177 | |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 178 | return ::arrangeCXXMethodType(*this, argTypes, |
Tilmann Scheller | 9c6082f | 2011-03-02 21:36:49 +0000 | [diff] [blame] | 179 | FTP->getCanonicalTypeUnqualified().getAs<FunctionProtoType>()); |
Anders Carlsson | 375c31c | 2009-10-03 19:43:08 +0000 | [diff] [blame] | 180 | } |
| 181 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 182 | /// Arrange the argument and result information for a declaration or |
| 183 | /// definition of the given C++ non-static member function. The |
| 184 | /// member function must be an ordinary function, i.e. not a |
| 185 | /// constructor or destructor. |
| 186 | const CGFunctionInfo & |
| 187 | CodeGenTypes::arrangeCXXMethodDeclaration(const CXXMethodDecl *MD) { |
Benjamin Kramer | e575359 | 2013-09-09 14:48:42 +0000 | [diff] [blame] | 188 | assert(!isa<CXXConstructorDecl>(MD) && "wrong method for constructors!"); |
John McCall | fc40028 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 189 | assert(!isa<CXXDestructorDecl>(MD) && "wrong method for destructors!"); |
| 190 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 191 | CanQual<FunctionProtoType> prototype = GetFormalType(MD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 192 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 193 | if (MD->isInstance()) { |
| 194 | // The abstract case is perfectly fine. |
Mark Lacey | 2529660 | 2013-10-02 20:35:23 +0000 | [diff] [blame] | 195 | const CXXRecordDecl *ThisType = TheCXXABI.getThisArgumentTypeForMethod(MD); |
Timur Iskhodzhanov | 8f189a9 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 196 | return arrangeCXXMethodType(ThisType, prototype.getTypePtr()); |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 197 | } |
| 198 | |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 199 | return arrangeFreeFunctionType(prototype); |
Anders Carlsson | f6f8ae5 | 2009-04-03 22:48:58 +0000 | [diff] [blame] | 200 | } |
| 201 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 202 | /// Arrange the argument and result information for a declaration |
| 203 | /// or definition to the given constructor variant. |
| 204 | const CGFunctionInfo & |
| 205 | CodeGenTypes::arrangeCXXConstructorDeclaration(const CXXConstructorDecl *D, |
| 206 | CXXCtorType ctorKind) { |
| 207 | SmallVector<CanQualType, 16> argTypes; |
| 208 | argTypes.push_back(GetThisType(Context, D->getParent())); |
Stephen Lin | 3b50e8d | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 209 | |
| 210 | GlobalDecl GD(D, ctorKind); |
| 211 | CanQualType resultType = |
| 212 | TheCXXABI.HasThisReturn(GD) ? argTypes.front() : Context.VoidTy; |
Anders Carlsson | f6c56e2 | 2009-11-25 03:15:49 +0000 | [diff] [blame] | 213 | |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 214 | CanQual<FunctionProtoType> FTP = GetFormalType(D); |
| 215 | |
| 216 | // Add the formal parameters. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 217 | for (unsigned i = 0, e = FTP->getNumParams(); i != e; ++i) |
| 218 | argTypes.push_back(FTP->getParamType(i)); |
| 219 | |
| 220 | TheCXXABI.BuildConstructorSignature(D, ctorKind, resultType, argTypes); |
| 221 | |
| 222 | RequiredArgs required = |
| 223 | (D->isVariadic() ? RequiredArgs(argTypes.size()) : RequiredArgs::All); |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 224 | |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 225 | FunctionType::ExtInfo extInfo = FTP->getExtInfo(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 226 | return arrangeLLVMFunctionInfo(resultType, true, argTypes, extInfo, required); |
| 227 | } |
| 228 | |
| 229 | /// Arrange a call to a C++ method, passing the given arguments. |
| 230 | const CGFunctionInfo & |
| 231 | CodeGenTypes::arrangeCXXConstructorCall(const CallArgList &args, |
| 232 | const CXXConstructorDecl *D, |
| 233 | CXXCtorType CtorKind, |
| 234 | unsigned ExtraArgs) { |
| 235 | // FIXME: Kill copy. |
| 236 | SmallVector<CanQualType, 16> ArgTypes; |
| 237 | for (CallArgList::const_iterator i = args.begin(), e = args.end(); i != e; |
| 238 | ++i) |
| 239 | ArgTypes.push_back(Context.getCanonicalParamType(i->Ty)); |
| 240 | |
| 241 | CanQual<FunctionProtoType> FPT = GetFormalType(D); |
| 242 | RequiredArgs Required = RequiredArgs::forPrototypePlus(FPT, 1 + ExtraArgs); |
| 243 | GlobalDecl GD(D, CtorKind); |
| 244 | CanQualType ResultType = |
| 245 | TheCXXABI.HasThisReturn(GD) ? ArgTypes.front() : Context.VoidTy; |
| 246 | |
| 247 | FunctionType::ExtInfo Info = FPT->getExtInfo(); |
| 248 | return arrangeLLVMFunctionInfo(ResultType, true, ArgTypes, Info, Required); |
Anders Carlsson | f6c56e2 | 2009-11-25 03:15:49 +0000 | [diff] [blame] | 249 | } |
| 250 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 251 | /// Arrange the argument and result information for a declaration, |
| 252 | /// definition, or call to the given destructor variant. It so |
| 253 | /// happens that all three cases produce the same information. |
| 254 | const CGFunctionInfo & |
| 255 | CodeGenTypes::arrangeCXXDestructor(const CXXDestructorDecl *D, |
| 256 | CXXDtorType dtorKind) { |
| 257 | SmallVector<CanQualType, 2> argTypes; |
| 258 | argTypes.push_back(GetThisType(Context, D->getParent())); |
Stephen Lin | 3b50e8d | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 259 | |
| 260 | GlobalDecl GD(D, dtorKind); |
| 261 | CanQualType resultType = |
| 262 | TheCXXABI.HasThisReturn(GD) ? argTypes.front() : Context.VoidTy; |
John McCall | 0b0ef0a | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 263 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 264 | TheCXXABI.BuildDestructorSignature(D, dtorKind, resultType, argTypes); |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 265 | |
| 266 | CanQual<FunctionProtoType> FTP = GetFormalType(D); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 267 | assert(FTP->getNumParams() == 0 && "dtor with formal parameters"); |
Timur Iskhodzhanov | 8f88a1d | 2012-07-12 09:50:54 +0000 | [diff] [blame] | 268 | assert(FTP->isVariadic() == 0 && "dtor with formal parameters"); |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 269 | |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 270 | FunctionType::ExtInfo extInfo = FTP->getExtInfo(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 271 | return arrangeLLVMFunctionInfo(resultType, true, argTypes, extInfo, |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 272 | RequiredArgs::All); |
Anders Carlsson | f6c56e2 | 2009-11-25 03:15:49 +0000 | [diff] [blame] | 273 | } |
| 274 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 275 | /// Arrange the argument and result information for the declaration or |
| 276 | /// definition of the given function. |
| 277 | const CGFunctionInfo & |
| 278 | CodeGenTypes::arrangeFunctionDeclaration(const FunctionDecl *FD) { |
Chris Lattner | 3eb67ca | 2009-05-12 20:27:19 +0000 | [diff] [blame] | 279 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) |
Anders Carlsson | f6f8ae5 | 2009-04-03 22:48:58 +0000 | [diff] [blame] | 280 | if (MD->isInstance()) |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 281 | return arrangeCXXMethodDeclaration(MD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 282 | |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 283 | CanQualType FTy = FD->getType()->getCanonicalTypeUnqualified(); |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 284 | |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 285 | assert(isa<FunctionType>(FTy)); |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 286 | |
| 287 | // When declaring a function without a prototype, always use a |
| 288 | // non-variadic type. |
| 289 | if (isa<FunctionNoProtoType>(FTy)) { |
| 290 | CanQual<FunctionNoProtoType> noProto = FTy.getAs<FunctionNoProtoType>(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 291 | return arrangeLLVMFunctionInfo(noProto->getReturnType(), false, None, |
Dmitri Gribenko | 5543169 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 292 | noProto->getExtInfo(), RequiredArgs::All); |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 293 | } |
| 294 | |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 295 | assert(isa<FunctionProtoType>(FTy)); |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 296 | return arrangeFreeFunctionType(FTy.getAs<FunctionProtoType>()); |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 297 | } |
| 298 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 299 | /// Arrange the argument and result information for the declaration or |
| 300 | /// definition of an Objective-C method. |
| 301 | const CGFunctionInfo & |
| 302 | CodeGenTypes::arrangeObjCMethodDeclaration(const ObjCMethodDecl *MD) { |
| 303 | // It happens that this is the same as a call with no optional |
| 304 | // arguments, except also using the formal 'self' type. |
| 305 | return arrangeObjCMessageSendSignature(MD, MD->getSelfDecl()->getType()); |
| 306 | } |
| 307 | |
| 308 | /// Arrange the argument and result information for the function type |
| 309 | /// through which to perform a send to the given Objective-C method, |
| 310 | /// using the given receiver type. The receiver type is not always |
| 311 | /// the 'self' type of the method or even an Objective-C pointer type. |
| 312 | /// This is *not* the right method for actually performing such a |
| 313 | /// message send, due to the possibility of optional arguments. |
| 314 | const CGFunctionInfo & |
| 315 | CodeGenTypes::arrangeObjCMessageSendSignature(const ObjCMethodDecl *MD, |
| 316 | QualType receiverType) { |
| 317 | SmallVector<CanQualType, 16> argTys; |
| 318 | argTys.push_back(Context.getCanonicalParamType(receiverType)); |
| 319 | argTys.push_back(Context.getCanonicalParamType(Context.getObjCSelType())); |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 320 | // FIXME: Kill copy? |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 321 | for (const auto *I : MD->params()) { |
| 322 | argTys.push_back(Context.getCanonicalParamType(I->getType())); |
John McCall | 0b0ef0a | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 323 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 324 | |
| 325 | FunctionType::ExtInfo einfo; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 326 | bool IsWindows = getContext().getTargetInfo().getTriple().isOSWindows(); |
| 327 | einfo = einfo.withCallingConv(getCallingConventionForDecl(MD, IsWindows)); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 328 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 329 | if (getContext().getLangOpts().ObjCAutoRefCount && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 330 | MD->hasAttr<NSReturnsRetainedAttr>()) |
| 331 | einfo = einfo.withProducesResult(true); |
| 332 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 333 | RequiredArgs required = |
| 334 | (MD->isVariadic() ? RequiredArgs(argTys.size()) : RequiredArgs::All); |
| 335 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 336 | return arrangeLLVMFunctionInfo(GetReturnType(MD->getReturnType()), false, |
| 337 | argTys, einfo, required); |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 338 | } |
| 339 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 340 | const CGFunctionInfo & |
| 341 | CodeGenTypes::arrangeGlobalDeclaration(GlobalDecl GD) { |
Anders Carlsson | b2bcf1c | 2010-02-06 02:44:09 +0000 | [diff] [blame] | 342 | // FIXME: Do we need to handle ObjCMethodDecl? |
| 343 | const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl()); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 344 | |
Anders Carlsson | b2bcf1c | 2010-02-06 02:44:09 +0000 | [diff] [blame] | 345 | if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 346 | return arrangeCXXConstructorDeclaration(CD, GD.getCtorType()); |
Anders Carlsson | b2bcf1c | 2010-02-06 02:44:09 +0000 | [diff] [blame] | 347 | |
| 348 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD)) |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 349 | return arrangeCXXDestructor(DD, GD.getDtorType()); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 350 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 351 | return arrangeFunctionDeclaration(FD); |
Anders Carlsson | b2bcf1c | 2010-02-06 02:44:09 +0000 | [diff] [blame] | 352 | } |
| 353 | |
John McCall | e56bb36 | 2012-12-07 07:03:17 +0000 | [diff] [blame] | 354 | /// Arrange a call as unto a free function, except possibly with an |
| 355 | /// additional number of formal parameters considered required. |
| 356 | static const CGFunctionInfo & |
| 357 | arrangeFreeFunctionLikeCall(CodeGenTypes &CGT, |
Mark Lacey | c3f7fd6 | 2013-10-10 20:57:00 +0000 | [diff] [blame] | 358 | CodeGenModule &CGM, |
John McCall | e56bb36 | 2012-12-07 07:03:17 +0000 | [diff] [blame] | 359 | const CallArgList &args, |
| 360 | const FunctionType *fnType, |
| 361 | unsigned numExtraRequiredArgs) { |
| 362 | assert(args.size() >= numExtraRequiredArgs); |
| 363 | |
| 364 | // In most cases, there are no optional arguments. |
| 365 | RequiredArgs required = RequiredArgs::All; |
| 366 | |
| 367 | // If we have a variadic prototype, the required arguments are the |
| 368 | // extra prefix plus the arguments in the prototype. |
| 369 | if (const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(fnType)) { |
| 370 | if (proto->isVariadic()) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 371 | required = RequiredArgs(proto->getNumParams() + numExtraRequiredArgs); |
John McCall | e56bb36 | 2012-12-07 07:03:17 +0000 | [diff] [blame] | 372 | |
| 373 | // If we don't have a prototype at all, but we're supposed to |
| 374 | // explicitly use the variadic convention for unprototyped calls, |
| 375 | // treat all of the arguments as required but preserve the nominal |
| 376 | // possibility of variadics. |
Mark Lacey | c3f7fd6 | 2013-10-10 20:57:00 +0000 | [diff] [blame] | 377 | } else if (CGM.getTargetCodeGenInfo() |
| 378 | .isNoProtoCallVariadic(args, |
| 379 | cast<FunctionNoProtoType>(fnType))) { |
John McCall | e56bb36 | 2012-12-07 07:03:17 +0000 | [diff] [blame] | 380 | required = RequiredArgs(args.size()); |
| 381 | } |
| 382 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 383 | return CGT.arrangeFreeFunctionCall(fnType->getReturnType(), args, |
John McCall | e56bb36 | 2012-12-07 07:03:17 +0000 | [diff] [blame] | 384 | fnType->getExtInfo(), required); |
| 385 | } |
| 386 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 387 | /// Figure out the rules for calling a function with the given formal |
| 388 | /// type using the given arguments. The arguments are necessary |
| 389 | /// because the function might be unprototyped, in which case it's |
| 390 | /// target-dependent in crazy ways. |
| 391 | const CGFunctionInfo & |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 392 | CodeGenTypes::arrangeFreeFunctionCall(const CallArgList &args, |
| 393 | const FunctionType *fnType) { |
Mark Lacey | c3f7fd6 | 2013-10-10 20:57:00 +0000 | [diff] [blame] | 394 | return arrangeFreeFunctionLikeCall(*this, CGM, args, fnType, 0); |
John McCall | e56bb36 | 2012-12-07 07:03:17 +0000 | [diff] [blame] | 395 | } |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 396 | |
John McCall | e56bb36 | 2012-12-07 07:03:17 +0000 | [diff] [blame] | 397 | /// A block function call is essentially a free-function call with an |
| 398 | /// extra implicit argument. |
| 399 | const CGFunctionInfo & |
| 400 | CodeGenTypes::arrangeBlockFunctionCall(const CallArgList &args, |
| 401 | const FunctionType *fnType) { |
Mark Lacey | c3f7fd6 | 2013-10-10 20:57:00 +0000 | [diff] [blame] | 402 | return arrangeFreeFunctionLikeCall(*this, CGM, args, fnType, 1); |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | const CGFunctionInfo & |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 406 | CodeGenTypes::arrangeFreeFunctionCall(QualType resultType, |
| 407 | const CallArgList &args, |
| 408 | FunctionType::ExtInfo info, |
| 409 | RequiredArgs required) { |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 410 | // FIXME: Kill copy. |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 411 | SmallVector<CanQualType, 16> argTypes; |
| 412 | for (CallArgList::const_iterator i = args.begin(), e = args.end(); |
Daniel Dunbar | 725ad31 | 2009-01-31 02:19:00 +0000 | [diff] [blame] | 413 | i != e; ++i) |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 414 | argTypes.push_back(Context.getCanonicalParamType(i->Ty)); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 415 | return arrangeLLVMFunctionInfo(GetReturnType(resultType), false, argTypes, |
| 416 | info, required); |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | /// Arrange a call to a C++ method, passing the given arguments. |
| 420 | const CGFunctionInfo & |
| 421 | CodeGenTypes::arrangeCXXMethodCall(const CallArgList &args, |
| 422 | const FunctionProtoType *FPT, |
| 423 | RequiredArgs required) { |
| 424 | // FIXME: Kill copy. |
| 425 | SmallVector<CanQualType, 16> argTypes; |
| 426 | for (CallArgList::const_iterator i = args.begin(), e = args.end(); |
| 427 | i != e; ++i) |
| 428 | argTypes.push_back(Context.getCanonicalParamType(i->Ty)); |
| 429 | |
| 430 | FunctionType::ExtInfo info = FPT->getExtInfo(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 431 | return arrangeLLVMFunctionInfo(GetReturnType(FPT->getReturnType()), true, |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 432 | argTypes, info, required); |
Daniel Dunbar | 725ad31 | 2009-01-31 02:19:00 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 435 | const CGFunctionInfo &CodeGenTypes::arrangeFreeFunctionDeclaration( |
| 436 | QualType resultType, const FunctionArgList &args, |
| 437 | const FunctionType::ExtInfo &info, bool isVariadic) { |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 438 | // FIXME: Kill copy. |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 439 | SmallVector<CanQualType, 16> argTypes; |
| 440 | for (FunctionArgList::const_iterator i = args.begin(), e = args.end(); |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 441 | i != e; ++i) |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 442 | argTypes.push_back(Context.getCanonicalParamType((*i)->getType())); |
| 443 | |
| 444 | RequiredArgs required = |
| 445 | (isVariadic ? RequiredArgs(args.size()) : RequiredArgs::All); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 446 | return arrangeLLVMFunctionInfo(GetReturnType(resultType), false, argTypes, info, |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 447 | required); |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 448 | } |
| 449 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 450 | const CGFunctionInfo &CodeGenTypes::arrangeNullaryFunction() { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 451 | return arrangeLLVMFunctionInfo(getContext().VoidTy, false, None, |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 452 | FunctionType::ExtInfo(), RequiredArgs::All); |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 453 | } |
| 454 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 455 | /// Arrange the argument and result information for an abstract value |
| 456 | /// of a given function type. This is the method which all of the |
| 457 | /// above functions ultimately defer to. |
| 458 | const CGFunctionInfo & |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 459 | CodeGenTypes::arrangeLLVMFunctionInfo(CanQualType resultType, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 460 | bool IsInstanceMethod, |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 461 | ArrayRef<CanQualType> argTypes, |
| 462 | FunctionType::ExtInfo info, |
| 463 | RequiredArgs required) { |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 464 | #ifndef NDEBUG |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 465 | for (ArrayRef<CanQualType>::const_iterator |
| 466 | I = argTypes.begin(), E = argTypes.end(); I != E; ++I) |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 467 | assert(I->isCanonicalAsParam()); |
| 468 | #endif |
| 469 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 470 | unsigned CC = ClangCallConvToLLVMCallConv(info.getCC()); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 471 | |
Daniel Dunbar | 40a6be6 | 2009-02-03 00:07:12 +0000 | [diff] [blame] | 472 | // Lookup or create unique function info. |
| 473 | llvm::FoldingSetNodeID ID; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 474 | CGFunctionInfo::Profile(ID, IsInstanceMethod, info, required, resultType, |
| 475 | argTypes); |
Daniel Dunbar | 40a6be6 | 2009-02-03 00:07:12 +0000 | [diff] [blame] | 476 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 477 | void *insertPos = 0; |
| 478 | CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, insertPos); |
Daniel Dunbar | 40a6be6 | 2009-02-03 00:07:12 +0000 | [diff] [blame] | 479 | if (FI) |
| 480 | return *FI; |
| 481 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 482 | // Construct the function info. We co-allocate the ArgInfos. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 483 | FI = CGFunctionInfo::create(CC, IsInstanceMethod, info, resultType, argTypes, |
| 484 | required); |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 485 | FunctionInfos.InsertNode(FI, insertPos); |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 486 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 487 | bool inserted = FunctionsBeingProcessed.insert(FI); (void)inserted; |
| 488 | assert(inserted && "Recursively being processed?"); |
Chris Lattner | 71305cc | 2011-07-15 05:16:14 +0000 | [diff] [blame] | 489 | |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 490 | // Compute ABI information. |
Chris Lattner | ee5dcd0 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 491 | getABIInfo().computeInfo(*FI); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 492 | |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 493 | // Loop over all of the computed argument and return value info. If any of |
| 494 | // them are direct or extend without a specified coerce type, specify the |
| 495 | // default now. |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 496 | ABIArgInfo &retInfo = FI->getReturnInfo(); |
| 497 | if (retInfo.canHaveCoerceToType() && retInfo.getCoerceToType() == 0) |
| 498 | retInfo.setCoerceToType(ConvertType(FI->getReturnType())); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 499 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 500 | for (auto &I : FI->arguments()) |
| 501 | if (I.info.canHaveCoerceToType() && I.info.getCoerceToType() == 0) |
| 502 | I.info.setCoerceToType(ConvertType(I.type)); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 503 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 504 | bool erased = FunctionsBeingProcessed.erase(FI); (void)erased; |
| 505 | assert(erased && "Not in set?"); |
Chris Lattner | d26c071 | 2011-07-15 06:41:05 +0000 | [diff] [blame] | 506 | |
Daniel Dunbar | 40a6be6 | 2009-02-03 00:07:12 +0000 | [diff] [blame] | 507 | return *FI; |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 508 | } |
| 509 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 510 | CGFunctionInfo *CGFunctionInfo::create(unsigned llvmCC, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 511 | bool IsInstanceMethod, |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 512 | const FunctionType::ExtInfo &info, |
| 513 | CanQualType resultType, |
| 514 | ArrayRef<CanQualType> argTypes, |
| 515 | RequiredArgs required) { |
| 516 | void *buffer = operator new(sizeof(CGFunctionInfo) + |
| 517 | sizeof(ArgInfo) * (argTypes.size() + 1)); |
| 518 | CGFunctionInfo *FI = new(buffer) CGFunctionInfo(); |
| 519 | FI->CallingConvention = llvmCC; |
| 520 | FI->EffectiveCallingConvention = llvmCC; |
| 521 | FI->ASTCallingConvention = info.getCC(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 522 | FI->InstanceMethod = IsInstanceMethod; |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 523 | FI->NoReturn = info.getNoReturn(); |
| 524 | FI->ReturnsRetained = info.getProducesResult(); |
| 525 | FI->Required = required; |
| 526 | FI->HasRegParm = info.getHasRegParm(); |
| 527 | FI->RegParm = info.getRegParm(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 528 | FI->ArgStruct = 0; |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 529 | FI->NumArgs = argTypes.size(); |
| 530 | FI->getArgsBuffer()[0].type = resultType; |
| 531 | for (unsigned i = 0, e = argTypes.size(); i != e; ++i) |
| 532 | FI->getArgsBuffer()[i + 1].type = argTypes[i]; |
| 533 | return FI; |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | /***/ |
| 537 | |
John McCall | 42e0611 | 2011-05-15 02:19:42 +0000 | [diff] [blame] | 538 | void CodeGenTypes::GetExpandedTypes(QualType type, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 539 | SmallVectorImpl<llvm::Type*> &expandedTypes) { |
Bob Wilson | 194f06a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 540 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(type)) { |
| 541 | uint64_t NumElts = AT->getSize().getZExtValue(); |
| 542 | for (uint64_t Elt = 0; Elt < NumElts; ++Elt) |
| 543 | GetExpandedTypes(AT->getElementType(), expandedTypes); |
Anton Korobeynikov | eaf856d | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 544 | } else if (const RecordType *RT = type->getAs<RecordType>()) { |
Bob Wilson | 194f06a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 545 | const RecordDecl *RD = RT->getDecl(); |
| 546 | assert(!RD->hasFlexibleArrayMember() && |
| 547 | "Cannot expand structure with flexible array."); |
Anton Korobeynikov | eaf856d | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 548 | if (RD->isUnion()) { |
| 549 | // Unions can be here only in degenerative cases - all the fields are same |
| 550 | // after flattening. Thus we have to use the "largest" field. |
| 551 | const FieldDecl *LargestFD = 0; |
| 552 | CharUnits UnionSize = CharUnits::Zero(); |
| 553 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 554 | for (const auto *FD : RD->fields()) { |
Anton Korobeynikov | eaf856d | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 555 | assert(!FD->isBitField() && |
| 556 | "Cannot expand structure with bit-field members."); |
| 557 | CharUnits FieldSize = getContext().getTypeSizeInChars(FD->getType()); |
| 558 | if (UnionSize < FieldSize) { |
| 559 | UnionSize = FieldSize; |
| 560 | LargestFD = FD; |
| 561 | } |
| 562 | } |
| 563 | if (LargestFD) |
| 564 | GetExpandedTypes(LargestFD->getType(), expandedTypes); |
| 565 | } else { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 566 | for (const auto *I : RD->fields()) { |
| 567 | assert(!I->isBitField() && |
Anton Korobeynikov | eaf856d | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 568 | "Cannot expand structure with bit-field members."); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 569 | GetExpandedTypes(I->getType(), expandedTypes); |
Anton Korobeynikov | eaf856d | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 570 | } |
Bob Wilson | 194f06a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 571 | } |
| 572 | } else if (const ComplexType *CT = type->getAs<ComplexType>()) { |
| 573 | llvm::Type *EltTy = ConvertType(CT->getElementType()); |
| 574 | expandedTypes.push_back(EltTy); |
| 575 | expandedTypes.push_back(EltTy); |
| 576 | } else |
| 577 | expandedTypes.push_back(ConvertType(type)); |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 578 | } |
| 579 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 580 | llvm::Function::arg_iterator |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 581 | CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV, |
| 582 | llvm::Function::arg_iterator AI) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 583 | assert(LV.isSimple() && |
| 584 | "Unexpected non-simple lvalue during struct expansion."); |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 585 | |
Bob Wilson | 194f06a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 586 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
| 587 | unsigned NumElts = AT->getSize().getZExtValue(); |
| 588 | QualType EltTy = AT->getElementType(); |
| 589 | for (unsigned Elt = 0; Elt < NumElts; ++Elt) { |
Eli Friedman | 377ecc7 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 590 | llvm::Value *EltAddr = Builder.CreateConstGEP2_32(LV.getAddress(), 0, Elt); |
Bob Wilson | 194f06a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 591 | LValue LV = MakeAddrLValue(EltAddr, EltTy); |
| 592 | AI = ExpandTypeFromArgs(EltTy, LV, AI); |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 593 | } |
Anton Korobeynikov | eaf856d | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 594 | } else if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Bob Wilson | 194f06a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 595 | RecordDecl *RD = RT->getDecl(); |
Anton Korobeynikov | eaf856d | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 596 | if (RD->isUnion()) { |
| 597 | // Unions can be here only in degenerative cases - all the fields are same |
| 598 | // after flattening. Thus we have to use the "largest" field. |
| 599 | const FieldDecl *LargestFD = 0; |
| 600 | CharUnits UnionSize = CharUnits::Zero(); |
Bob Wilson | 194f06a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 601 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 602 | for (const auto *FD : RD->fields()) { |
Anton Korobeynikov | eaf856d | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 603 | assert(!FD->isBitField() && |
| 604 | "Cannot expand structure with bit-field members."); |
| 605 | CharUnits FieldSize = getContext().getTypeSizeInChars(FD->getType()); |
| 606 | if (UnionSize < FieldSize) { |
| 607 | UnionSize = FieldSize; |
| 608 | LargestFD = FD; |
| 609 | } |
| 610 | } |
| 611 | if (LargestFD) { |
| 612 | // FIXME: What are the right qualifiers here? |
Eli Friedman | 377ecc7 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 613 | LValue SubLV = EmitLValueForField(LV, LargestFD); |
| 614 | AI = ExpandTypeFromArgs(LargestFD->getType(), SubLV, AI); |
Anton Korobeynikov | eaf856d | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 615 | } |
| 616 | } else { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 617 | for (const auto *FD : RD->fields()) { |
Anton Korobeynikov | eaf856d | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 618 | QualType FT = FD->getType(); |
| 619 | |
| 620 | // FIXME: What are the right qualifiers here? |
Eli Friedman | 377ecc7 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 621 | LValue SubLV = EmitLValueForField(LV, FD); |
| 622 | AI = ExpandTypeFromArgs(FT, SubLV, AI); |
Anton Korobeynikov | eaf856d | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 623 | } |
Bob Wilson | 194f06a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 624 | } |
| 625 | } else if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
| 626 | QualType EltTy = CT->getElementType(); |
Eli Friedman | 377ecc7 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 627 | llvm::Value *RealAddr = Builder.CreateStructGEP(LV.getAddress(), 0, "real"); |
Bob Wilson | 194f06a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 628 | EmitStoreThroughLValue(RValue::get(AI++), MakeAddrLValue(RealAddr, EltTy)); |
Eli Friedman | 377ecc7 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 629 | llvm::Value *ImagAddr = Builder.CreateStructGEP(LV.getAddress(), 1, "imag"); |
Bob Wilson | 194f06a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 630 | EmitStoreThroughLValue(RValue::get(AI++), MakeAddrLValue(ImagAddr, EltTy)); |
| 631 | } else { |
| 632 | EmitStoreThroughLValue(RValue::get(AI), LV); |
| 633 | ++AI; |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | return AI; |
| 637 | } |
| 638 | |
Chris Lattner | e7bb777 | 2010-06-27 06:04:18 +0000 | [diff] [blame] | 639 | /// EnterStructPointerForCoercedAccess - Given a struct pointer that we are |
Chris Lattner | 08dd2a0 | 2010-06-27 05:56:15 +0000 | [diff] [blame] | 640 | /// accessing some number of bytes out of it, try to gep into the struct to get |
| 641 | /// at its inner goodness. Dive as deep as possible without entering an element |
| 642 | /// with an in-memory size smaller than DstSize. |
| 643 | static llvm::Value * |
Chris Lattner | e7bb777 | 2010-06-27 06:04:18 +0000 | [diff] [blame] | 644 | EnterStructPointerForCoercedAccess(llvm::Value *SrcPtr, |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 645 | llvm::StructType *SrcSTy, |
Chris Lattner | e7bb777 | 2010-06-27 06:04:18 +0000 | [diff] [blame] | 646 | uint64_t DstSize, CodeGenFunction &CGF) { |
Chris Lattner | 08dd2a0 | 2010-06-27 05:56:15 +0000 | [diff] [blame] | 647 | // We can't dive into a zero-element struct. |
| 648 | if (SrcSTy->getNumElements() == 0) return SrcPtr; |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 649 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 650 | llvm::Type *FirstElt = SrcSTy->getElementType(0); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 651 | |
Chris Lattner | 08dd2a0 | 2010-06-27 05:56:15 +0000 | [diff] [blame] | 652 | // If the first elt is at least as large as what we're looking for, or if the |
| 653 | // first element is the same size as the whole struct, we can enter it. |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 654 | uint64_t FirstEltSize = |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 655 | CGF.CGM.getDataLayout().getTypeAllocSize(FirstElt); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 656 | if (FirstEltSize < DstSize && |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 657 | FirstEltSize < CGF.CGM.getDataLayout().getTypeAllocSize(SrcSTy)) |
Chris Lattner | 08dd2a0 | 2010-06-27 05:56:15 +0000 | [diff] [blame] | 658 | return SrcPtr; |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 659 | |
Chris Lattner | 08dd2a0 | 2010-06-27 05:56:15 +0000 | [diff] [blame] | 660 | // GEP into the first element. |
| 661 | SrcPtr = CGF.Builder.CreateConstGEP2_32(SrcPtr, 0, 0, "coerce.dive"); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 662 | |
Chris Lattner | 08dd2a0 | 2010-06-27 05:56:15 +0000 | [diff] [blame] | 663 | // If the first element is a struct, recurse. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 664 | llvm::Type *SrcTy = |
Chris Lattner | 08dd2a0 | 2010-06-27 05:56:15 +0000 | [diff] [blame] | 665 | cast<llvm::PointerType>(SrcPtr->getType())->getElementType(); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 666 | if (llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy)) |
Chris Lattner | e7bb777 | 2010-06-27 06:04:18 +0000 | [diff] [blame] | 667 | return EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF); |
Chris Lattner | 08dd2a0 | 2010-06-27 05:56:15 +0000 | [diff] [blame] | 668 | |
| 669 | return SrcPtr; |
| 670 | } |
| 671 | |
Chris Lattner | 6d11cdb | 2010-06-27 06:26:04 +0000 | [diff] [blame] | 672 | /// CoerceIntOrPtrToIntOrPtr - Convert a value Val to the specific Ty where both |
| 673 | /// are either integers or pointers. This does a truncation of the value if it |
| 674 | /// is too large or a zero extension if it is too small. |
Jakob Stoklund Olesen | 7e9f52f | 2013-06-05 03:00:13 +0000 | [diff] [blame] | 675 | /// |
| 676 | /// This behaves as if the value were coerced through memory, so on big-endian |
| 677 | /// targets the high bits are preserved in a truncation, while little-endian |
| 678 | /// targets preserve the low bits. |
Chris Lattner | 6d11cdb | 2010-06-27 06:26:04 +0000 | [diff] [blame] | 679 | static llvm::Value *CoerceIntOrPtrToIntOrPtr(llvm::Value *Val, |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 680 | llvm::Type *Ty, |
Chris Lattner | 6d11cdb | 2010-06-27 06:26:04 +0000 | [diff] [blame] | 681 | CodeGenFunction &CGF) { |
| 682 | if (Val->getType() == Ty) |
| 683 | return Val; |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 684 | |
Chris Lattner | 6d11cdb | 2010-06-27 06:26:04 +0000 | [diff] [blame] | 685 | if (isa<llvm::PointerType>(Val->getType())) { |
| 686 | // If this is Pointer->Pointer avoid conversion to and from int. |
| 687 | if (isa<llvm::PointerType>(Ty)) |
| 688 | return CGF.Builder.CreateBitCast(Val, Ty, "coerce.val"); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 689 | |
Chris Lattner | 6d11cdb | 2010-06-27 06:26:04 +0000 | [diff] [blame] | 690 | // Convert the pointer to an integer so we can play with its width. |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 691 | Val = CGF.Builder.CreatePtrToInt(Val, CGF.IntPtrTy, "coerce.val.pi"); |
Chris Lattner | 6d11cdb | 2010-06-27 06:26:04 +0000 | [diff] [blame] | 692 | } |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 693 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 694 | llvm::Type *DestIntTy = Ty; |
Chris Lattner | 6d11cdb | 2010-06-27 06:26:04 +0000 | [diff] [blame] | 695 | if (isa<llvm::PointerType>(DestIntTy)) |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 696 | DestIntTy = CGF.IntPtrTy; |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 697 | |
Jakob Stoklund Olesen | 7e9f52f | 2013-06-05 03:00:13 +0000 | [diff] [blame] | 698 | if (Val->getType() != DestIntTy) { |
| 699 | const llvm::DataLayout &DL = CGF.CGM.getDataLayout(); |
| 700 | if (DL.isBigEndian()) { |
| 701 | // Preserve the high bits on big-endian targets. |
| 702 | // That is what memory coercion does. |
| 703 | uint64_t SrcSize = DL.getTypeAllocSizeInBits(Val->getType()); |
| 704 | uint64_t DstSize = DL.getTypeAllocSizeInBits(DestIntTy); |
| 705 | if (SrcSize > DstSize) { |
| 706 | Val = CGF.Builder.CreateLShr(Val, SrcSize - DstSize, "coerce.highbits"); |
| 707 | Val = CGF.Builder.CreateTrunc(Val, DestIntTy, "coerce.val.ii"); |
| 708 | } else { |
| 709 | Val = CGF.Builder.CreateZExt(Val, DestIntTy, "coerce.val.ii"); |
| 710 | Val = CGF.Builder.CreateShl(Val, DstSize - SrcSize, "coerce.highbits"); |
| 711 | } |
| 712 | } else { |
| 713 | // Little-endian targets preserve the low bits. No shifts required. |
| 714 | Val = CGF.Builder.CreateIntCast(Val, DestIntTy, false, "coerce.val.ii"); |
| 715 | } |
| 716 | } |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 717 | |
Chris Lattner | 6d11cdb | 2010-06-27 06:26:04 +0000 | [diff] [blame] | 718 | if (isa<llvm::PointerType>(Ty)) |
| 719 | Val = CGF.Builder.CreateIntToPtr(Val, Ty, "coerce.val.ip"); |
| 720 | return Val; |
| 721 | } |
| 722 | |
Chris Lattner | 08dd2a0 | 2010-06-27 05:56:15 +0000 | [diff] [blame] | 723 | |
| 724 | |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 725 | /// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as |
| 726 | /// a pointer to an object of type \arg Ty. |
| 727 | /// |
| 728 | /// This safely handles the case when the src type is smaller than the |
| 729 | /// destination type; in this situation the values of bits which not |
| 730 | /// present in the src are undefined. |
| 731 | static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr, |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 732 | llvm::Type *Ty, |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 733 | CodeGenFunction &CGF) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 734 | llvm::Type *SrcTy = |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 735 | cast<llvm::PointerType>(SrcPtr->getType())->getElementType(); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 736 | |
Chris Lattner | 6ae0069 | 2010-06-28 22:51:39 +0000 | [diff] [blame] | 737 | // If SrcTy and Ty are the same, just do a load. |
| 738 | if (SrcTy == Ty) |
| 739 | return CGF.Builder.CreateLoad(SrcPtr); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 740 | |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 741 | uint64_t DstSize = CGF.CGM.getDataLayout().getTypeAllocSize(Ty); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 742 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 743 | if (llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy)) { |
Chris Lattner | e7bb777 | 2010-06-27 06:04:18 +0000 | [diff] [blame] | 744 | SrcPtr = EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF); |
Chris Lattner | 08dd2a0 | 2010-06-27 05:56:15 +0000 | [diff] [blame] | 745 | SrcTy = cast<llvm::PointerType>(SrcPtr->getType())->getElementType(); |
| 746 | } |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 747 | |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 748 | uint64_t SrcSize = CGF.CGM.getDataLayout().getTypeAllocSize(SrcTy); |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 749 | |
Chris Lattner | 6d11cdb | 2010-06-27 06:26:04 +0000 | [diff] [blame] | 750 | // If the source and destination are integer or pointer types, just do an |
| 751 | // extension or truncation to the desired type. |
| 752 | if ((isa<llvm::IntegerType>(Ty) || isa<llvm::PointerType>(Ty)) && |
| 753 | (isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy))) { |
| 754 | llvm::LoadInst *Load = CGF.Builder.CreateLoad(SrcPtr); |
| 755 | return CoerceIntOrPtrToIntOrPtr(Load, Ty, CGF); |
| 756 | } |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 757 | |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 758 | // If load is legal, just bitcast the src pointer. |
Daniel Dunbar | 7ef455b | 2009-05-13 18:54:26 +0000 | [diff] [blame] | 759 | if (SrcSize >= DstSize) { |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 760 | // Generally SrcSize is never greater than DstSize, since this means we are |
| 761 | // losing bits. However, this can happen in cases where the structure has |
| 762 | // additional padding, for example due to a user specified alignment. |
Daniel Dunbar | 7ef455b | 2009-05-13 18:54:26 +0000 | [diff] [blame] | 763 | // |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 764 | // FIXME: Assert that we aren't truncating non-padding bits when have access |
| 765 | // to that information. |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 766 | llvm::Value *Casted = |
| 767 | CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty)); |
Daniel Dunbar | 386621f | 2009-02-07 02:46:03 +0000 | [diff] [blame] | 768 | llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted); |
| 769 | // FIXME: Use better alignment / avoid requiring aligned load. |
| 770 | Load->setAlignment(1); |
| 771 | return Load; |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 772 | } |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 773 | |
Chris Lattner | 35b21b8 | 2010-06-27 01:06:27 +0000 | [diff] [blame] | 774 | // Otherwise do coercion through memory. This is stupid, but |
| 775 | // simple. |
| 776 | llvm::Value *Tmp = CGF.CreateTempAlloca(Ty); |
Manman Ren | f51c61c | 2012-11-28 22:08:52 +0000 | [diff] [blame] | 777 | llvm::Type *I8PtrTy = CGF.Builder.getInt8PtrTy(); |
| 778 | llvm::Value *Casted = CGF.Builder.CreateBitCast(Tmp, I8PtrTy); |
| 779 | llvm::Value *SrcCasted = CGF.Builder.CreateBitCast(SrcPtr, I8PtrTy); |
Manman Ren | 060f34d | 2012-11-28 22:29:41 +0000 | [diff] [blame] | 780 | // FIXME: Use better alignment. |
Manman Ren | f51c61c | 2012-11-28 22:08:52 +0000 | [diff] [blame] | 781 | CGF.Builder.CreateMemCpy(Casted, SrcCasted, |
| 782 | llvm::ConstantInt::get(CGF.IntPtrTy, SrcSize), |
| 783 | 1, false); |
Chris Lattner | 35b21b8 | 2010-06-27 01:06:27 +0000 | [diff] [blame] | 784 | return CGF.Builder.CreateLoad(Tmp); |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 785 | } |
| 786 | |
Eli Friedman | badea57 | 2011-05-17 21:08:01 +0000 | [diff] [blame] | 787 | // Function to store a first-class aggregate into memory. We prefer to |
| 788 | // store the elements rather than the aggregate to be more friendly to |
| 789 | // fast-isel. |
| 790 | // FIXME: Do we need to recurse here? |
| 791 | static void BuildAggStore(CodeGenFunction &CGF, llvm::Value *Val, |
| 792 | llvm::Value *DestPtr, bool DestIsVolatile, |
| 793 | bool LowAlignment) { |
| 794 | // Prefer scalar stores to first-class aggregate stores. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 795 | if (llvm::StructType *STy = |
Eli Friedman | badea57 | 2011-05-17 21:08:01 +0000 | [diff] [blame] | 796 | dyn_cast<llvm::StructType>(Val->getType())) { |
| 797 | for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { |
| 798 | llvm::Value *EltPtr = CGF.Builder.CreateConstGEP2_32(DestPtr, 0, i); |
| 799 | llvm::Value *Elt = CGF.Builder.CreateExtractValue(Val, i); |
| 800 | llvm::StoreInst *SI = CGF.Builder.CreateStore(Elt, EltPtr, |
| 801 | DestIsVolatile); |
| 802 | if (LowAlignment) |
| 803 | SI->setAlignment(1); |
| 804 | } |
| 805 | } else { |
Bill Wendling | 0821263 | 2012-03-16 21:45:12 +0000 | [diff] [blame] | 806 | llvm::StoreInst *SI = CGF.Builder.CreateStore(Val, DestPtr, DestIsVolatile); |
| 807 | if (LowAlignment) |
| 808 | SI->setAlignment(1); |
Eli Friedman | badea57 | 2011-05-17 21:08:01 +0000 | [diff] [blame] | 809 | } |
| 810 | } |
| 811 | |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 812 | /// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src, |
| 813 | /// where the source and destination may have different types. |
| 814 | /// |
| 815 | /// This safely handles the case when the src type is larger than the |
| 816 | /// destination type; the upper bits of the src will be lost. |
| 817 | static void CreateCoercedStore(llvm::Value *Src, |
| 818 | llvm::Value *DstPtr, |
Anders Carlsson | d2490a9 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 819 | bool DstIsVolatile, |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 820 | CodeGenFunction &CGF) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 821 | llvm::Type *SrcTy = Src->getType(); |
| 822 | llvm::Type *DstTy = |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 823 | cast<llvm::PointerType>(DstPtr->getType())->getElementType(); |
Chris Lattner | 6ae0069 | 2010-06-28 22:51:39 +0000 | [diff] [blame] | 824 | if (SrcTy == DstTy) { |
| 825 | CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile); |
| 826 | return; |
| 827 | } |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 828 | |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 829 | uint64_t SrcSize = CGF.CGM.getDataLayout().getTypeAllocSize(SrcTy); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 830 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 831 | if (llvm::StructType *DstSTy = dyn_cast<llvm::StructType>(DstTy)) { |
Chris Lattner | e7bb777 | 2010-06-27 06:04:18 +0000 | [diff] [blame] | 832 | DstPtr = EnterStructPointerForCoercedAccess(DstPtr, DstSTy, SrcSize, CGF); |
| 833 | DstTy = cast<llvm::PointerType>(DstPtr->getType())->getElementType(); |
| 834 | } |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 835 | |
Chris Lattner | 6d11cdb | 2010-06-27 06:26:04 +0000 | [diff] [blame] | 836 | // If the source and destination are integer or pointer types, just do an |
| 837 | // extension or truncation to the desired type. |
| 838 | if ((isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy)) && |
| 839 | (isa<llvm::IntegerType>(DstTy) || isa<llvm::PointerType>(DstTy))) { |
| 840 | Src = CoerceIntOrPtrToIntOrPtr(Src, DstTy, CGF); |
| 841 | CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile); |
| 842 | return; |
| 843 | } |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 844 | |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 845 | uint64_t DstSize = CGF.CGM.getDataLayout().getTypeAllocSize(DstTy); |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 846 | |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 847 | // If store is legal, just bitcast the src pointer. |
Daniel Dunbar | fdf4986 | 2009-06-05 07:58:54 +0000 | [diff] [blame] | 848 | if (SrcSize <= DstSize) { |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 849 | llvm::Value *Casted = |
| 850 | CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy)); |
Daniel Dunbar | 386621f | 2009-02-07 02:46:03 +0000 | [diff] [blame] | 851 | // FIXME: Use better alignment / avoid requiring aligned store. |
Eli Friedman | badea57 | 2011-05-17 21:08:01 +0000 | [diff] [blame] | 852 | BuildAggStore(CGF, Src, Casted, DstIsVolatile, true); |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 853 | } else { |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 854 | // Otherwise do coercion through memory. This is stupid, but |
| 855 | // simple. |
Daniel Dunbar | fdf4986 | 2009-06-05 07:58:54 +0000 | [diff] [blame] | 856 | |
| 857 | // Generally SrcSize is never greater than DstSize, since this means we are |
| 858 | // losing bits. However, this can happen in cases where the structure has |
| 859 | // additional padding, for example due to a user specified alignment. |
| 860 | // |
| 861 | // FIXME: Assert that we aren't truncating non-padding bits when have access |
| 862 | // to that information. |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 863 | llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy); |
| 864 | CGF.Builder.CreateStore(Src, Tmp); |
Manman Ren | f51c61c | 2012-11-28 22:08:52 +0000 | [diff] [blame] | 865 | llvm::Type *I8PtrTy = CGF.Builder.getInt8PtrTy(); |
| 866 | llvm::Value *Casted = CGF.Builder.CreateBitCast(Tmp, I8PtrTy); |
| 867 | llvm::Value *DstCasted = CGF.Builder.CreateBitCast(DstPtr, I8PtrTy); |
Manman Ren | 060f34d | 2012-11-28 22:29:41 +0000 | [diff] [blame] | 868 | // FIXME: Use better alignment. |
Manman Ren | f51c61c | 2012-11-28 22:08:52 +0000 | [diff] [blame] | 869 | CGF.Builder.CreateMemCpy(DstCasted, Casted, |
| 870 | llvm::ConstantInt::get(CGF.IntPtrTy, DstSize), |
| 871 | 1, false); |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 872 | } |
| 873 | } |
| 874 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 875 | /***/ |
| 876 | |
Daniel Dunbar | dacf9dd | 2010-07-14 23:39:36 +0000 | [diff] [blame] | 877 | bool CodeGenModule::ReturnTypeUsesSRet(const CGFunctionInfo &FI) { |
Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 878 | return FI.getReturnInfo().isIndirect(); |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 879 | } |
| 880 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 881 | bool CodeGenModule::ReturnSlotInterferesWithArgs(const CGFunctionInfo &FI) { |
| 882 | return ReturnTypeUsesSRet(FI) && |
| 883 | getTargetCodeGenInfo().doesReturnSlotInterfereWithArgs(); |
| 884 | } |
| 885 | |
Daniel Dunbar | dacf9dd | 2010-07-14 23:39:36 +0000 | [diff] [blame] | 886 | bool CodeGenModule::ReturnTypeUsesFPRet(QualType ResultType) { |
| 887 | if (const BuiltinType *BT = ResultType->getAs<BuiltinType>()) { |
| 888 | switch (BT->getKind()) { |
| 889 | default: |
| 890 | return false; |
| 891 | case BuiltinType::Float: |
John McCall | 64aa4b3 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 892 | return getTarget().useObjCFPRetForRealType(TargetInfo::Float); |
Daniel Dunbar | dacf9dd | 2010-07-14 23:39:36 +0000 | [diff] [blame] | 893 | case BuiltinType::Double: |
John McCall | 64aa4b3 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 894 | return getTarget().useObjCFPRetForRealType(TargetInfo::Double); |
Daniel Dunbar | dacf9dd | 2010-07-14 23:39:36 +0000 | [diff] [blame] | 895 | case BuiltinType::LongDouble: |
John McCall | 64aa4b3 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 896 | return getTarget().useObjCFPRetForRealType(TargetInfo::LongDouble); |
Daniel Dunbar | dacf9dd | 2010-07-14 23:39:36 +0000 | [diff] [blame] | 897 | } |
| 898 | } |
| 899 | |
| 900 | return false; |
| 901 | } |
| 902 | |
Anders Carlsson | eea6480 | 2011-10-31 16:27:11 +0000 | [diff] [blame] | 903 | bool CodeGenModule::ReturnTypeUsesFP2Ret(QualType ResultType) { |
| 904 | if (const ComplexType *CT = ResultType->getAs<ComplexType>()) { |
| 905 | if (const BuiltinType *BT = CT->getElementType()->getAs<BuiltinType>()) { |
| 906 | if (BT->getKind() == BuiltinType::LongDouble) |
John McCall | 64aa4b3 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 907 | return getTarget().useObjCFP2RetForComplexLongDouble(); |
Anders Carlsson | eea6480 | 2011-10-31 16:27:11 +0000 | [diff] [blame] | 908 | } |
| 909 | } |
| 910 | |
| 911 | return false; |
| 912 | } |
| 913 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 914 | llvm::FunctionType *CodeGenTypes::GetFunctionType(GlobalDecl GD) { |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 915 | const CGFunctionInfo &FI = arrangeGlobalDeclaration(GD); |
| 916 | return GetFunctionType(FI); |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 917 | } |
| 918 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 919 | llvm::FunctionType * |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 920 | CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI) { |
Chris Lattner | 71305cc | 2011-07-15 05:16:14 +0000 | [diff] [blame] | 921 | |
| 922 | bool Inserted = FunctionsBeingProcessed.insert(&FI); (void)Inserted; |
| 923 | assert(Inserted && "Recursively being processed?"); |
| 924 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 925 | SmallVector<llvm::Type*, 8> argTypes; |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 926 | llvm::Type *resultType = 0; |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 927 | |
John McCall | 42e0611 | 2011-05-15 02:19:42 +0000 | [diff] [blame] | 928 | const ABIArgInfo &retAI = FI.getReturnInfo(); |
| 929 | switch (retAI.getKind()) { |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 930 | case ABIArgInfo::Expand: |
John McCall | 42e0611 | 2011-05-15 02:19:42 +0000 | [diff] [blame] | 931 | llvm_unreachable("Invalid ABI kind for return argument"); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 932 | |
Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 933 | case ABIArgInfo::Extend: |
Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 934 | case ABIArgInfo::Direct: |
John McCall | 42e0611 | 2011-05-15 02:19:42 +0000 | [diff] [blame] | 935 | resultType = retAI.getCoerceToType(); |
Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 936 | break; |
| 937 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 938 | case ABIArgInfo::InAlloca: |
| 939 | if (retAI.getInAllocaSRet()) { |
| 940 | // sret things on win32 aren't void, they return the sret pointer. |
| 941 | QualType ret = FI.getReturnType(); |
| 942 | llvm::Type *ty = ConvertType(ret); |
| 943 | unsigned addressSpace = Context.getTargetAddressSpace(ret); |
| 944 | resultType = llvm::PointerType::get(ty, addressSpace); |
| 945 | } else { |
| 946 | resultType = llvm::Type::getVoidTy(getLLVMContext()); |
| 947 | } |
| 948 | break; |
| 949 | |
Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 950 | case ABIArgInfo::Indirect: { |
John McCall | 42e0611 | 2011-05-15 02:19:42 +0000 | [diff] [blame] | 951 | assert(!retAI.getIndirectAlign() && "Align unused on indirect return."); |
| 952 | resultType = llvm::Type::getVoidTy(getLLVMContext()); |
| 953 | |
| 954 | QualType ret = FI.getReturnType(); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 955 | llvm::Type *ty = ConvertType(ret); |
John McCall | 42e0611 | 2011-05-15 02:19:42 +0000 | [diff] [blame] | 956 | unsigned addressSpace = Context.getTargetAddressSpace(ret); |
| 957 | argTypes.push_back(llvm::PointerType::get(ty, addressSpace)); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 958 | break; |
| 959 | } |
| 960 | |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 961 | case ABIArgInfo::Ignore: |
John McCall | 42e0611 | 2011-05-15 02:19:42 +0000 | [diff] [blame] | 962 | resultType = llvm::Type::getVoidTy(getLLVMContext()); |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 963 | break; |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 964 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 965 | |
John McCall | e56bb36 | 2012-12-07 07:03:17 +0000 | [diff] [blame] | 966 | // Add in all of the required arguments. |
| 967 | CGFunctionInfo::const_arg_iterator it = FI.arg_begin(), ie; |
| 968 | if (FI.isVariadic()) { |
| 969 | ie = it + FI.getRequiredArgs().getNumRequiredArgs(); |
| 970 | } else { |
| 971 | ie = FI.arg_end(); |
| 972 | } |
| 973 | for (; it != ie; ++it) { |
John McCall | 42e0611 | 2011-05-15 02:19:42 +0000 | [diff] [blame] | 974 | const ABIArgInfo &argAI = it->info; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 975 | |
Rafael Espindola | e4aeeaa | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 976 | // Insert a padding type to ensure proper alignment. |
| 977 | if (llvm::Type *PaddingType = argAI.getPaddingType()) |
| 978 | argTypes.push_back(PaddingType); |
| 979 | |
John McCall | 42e0611 | 2011-05-15 02:19:42 +0000 | [diff] [blame] | 980 | switch (argAI.getKind()) { |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 981 | case ABIArgInfo::Ignore: |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 982 | case ABIArgInfo::InAlloca: |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 983 | break; |
| 984 | |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 985 | case ABIArgInfo::Indirect: { |
| 986 | // indirect arguments are always on the stack, which is addr space #0. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 987 | llvm::Type *LTy = ConvertTypeForMem(it->type); |
John McCall | 42e0611 | 2011-05-15 02:19:42 +0000 | [diff] [blame] | 988 | argTypes.push_back(LTy->getPointerTo()); |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 989 | break; |
| 990 | } |
| 991 | |
| 992 | case ABIArgInfo::Extend: |
Chris Lattner | 1ed7267 | 2010-07-29 06:44:09 +0000 | [diff] [blame] | 993 | case ABIArgInfo::Direct: { |
Chris Lattner | ce70016 | 2010-06-28 23:44:11 +0000 | [diff] [blame] | 994 | // If the coerce-to type is a first class aggregate, flatten it. Either |
| 995 | // way is semantically identical, but fast-isel and the optimizer |
| 996 | // generally likes scalar values better than FCAs. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 997 | llvm::Type *argType = argAI.getCoerceToType(); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 998 | if (llvm::StructType *st = dyn_cast<llvm::StructType>(argType)) { |
John McCall | 42e0611 | 2011-05-15 02:19:42 +0000 | [diff] [blame] | 999 | for (unsigned i = 0, e = st->getNumElements(); i != e; ++i) |
| 1000 | argTypes.push_back(st->getElementType(i)); |
Chris Lattner | ce70016 | 2010-06-28 23:44:11 +0000 | [diff] [blame] | 1001 | } else { |
John McCall | 42e0611 | 2011-05-15 02:19:42 +0000 | [diff] [blame] | 1002 | argTypes.push_back(argType); |
Chris Lattner | ce70016 | 2010-06-28 23:44:11 +0000 | [diff] [blame] | 1003 | } |
Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 1004 | break; |
Chris Lattner | 1ed7267 | 2010-07-29 06:44:09 +0000 | [diff] [blame] | 1005 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1006 | |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1007 | case ABIArgInfo::Expand: |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1008 | GetExpandedTypes(it->type, argTypes); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1009 | break; |
| 1010 | } |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1011 | } |
| 1012 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1013 | // Add the inalloca struct as the last parameter type. |
| 1014 | if (llvm::StructType *ArgStruct = FI.getArgStruct()) |
| 1015 | argTypes.push_back(ArgStruct->getPointerTo()); |
| 1016 | |
Chris Lattner | 71305cc | 2011-07-15 05:16:14 +0000 | [diff] [blame] | 1017 | bool Erased = FunctionsBeingProcessed.erase(&FI); (void)Erased; |
| 1018 | assert(Erased && "Not in set?"); |
| 1019 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1020 | return llvm::FunctionType::get(resultType, argTypes, FI.isVariadic()); |
Daniel Dunbar | 3913f18 | 2008-09-09 23:48:28 +0000 | [diff] [blame] | 1021 | } |
| 1022 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1023 | llvm::Type *CodeGenTypes::GetFunctionTypeForVTable(GlobalDecl GD) { |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 1024 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
Anders Carlsson | ecf282b | 2009-11-24 05:08:52 +0000 | [diff] [blame] | 1025 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1026 | |
Chris Lattner | f742eb0 | 2011-07-10 00:18:59 +0000 | [diff] [blame] | 1027 | if (!isFuncTypeConvertible(FPT)) |
| 1028 | return llvm::StructType::get(getLLVMContext()); |
| 1029 | |
| 1030 | const CGFunctionInfo *Info; |
| 1031 | if (isa<CXXDestructorDecl>(MD)) |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1032 | Info = &arrangeCXXDestructor(cast<CXXDestructorDecl>(MD), GD.getDtorType()); |
Chris Lattner | f742eb0 | 2011-07-10 00:18:59 +0000 | [diff] [blame] | 1033 | else |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1034 | Info = &arrangeCXXMethodDeclaration(MD); |
| 1035 | return GetFunctionType(*Info); |
Anders Carlsson | ecf282b | 2009-11-24 05:08:52 +0000 | [diff] [blame] | 1036 | } |
| 1037 | |
Daniel Dunbar | a0a99e0 | 2009-02-02 23:43:58 +0000 | [diff] [blame] | 1038 | void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI, |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1039 | const Decl *TargetDecl, |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1040 | AttributeListType &PAL, |
Bill Wendling | 94236e7 | 2013-02-22 00:13:35 +0000 | [diff] [blame] | 1041 | unsigned &CallingConv, |
| 1042 | bool AttrOnCallSite) { |
Bill Wendling | 0d58339 | 2012-10-15 20:36:26 +0000 | [diff] [blame] | 1043 | llvm::AttrBuilder FuncAttrs; |
| 1044 | llvm::AttrBuilder RetAttrs; |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1045 | |
Daniel Dunbar | ca6408c | 2009-09-12 00:59:20 +0000 | [diff] [blame] | 1046 | CallingConv = FI.getEffectiveCallingConvention(); |
| 1047 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1048 | if (FI.isNoReturn()) |
Bill Wendling | 72390b3 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 1049 | FuncAttrs.addAttribute(llvm::Attribute::NoReturn); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1050 | |
Anton Korobeynikov | 1102f42 | 2009-04-04 00:49:24 +0000 | [diff] [blame] | 1051 | // FIXME: handle sseregparm someday... |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1052 | if (TargetDecl) { |
Rafael Espindola | 6700415 | 2011-10-12 19:51:18 +0000 | [diff] [blame] | 1053 | if (TargetDecl->hasAttr<ReturnsTwiceAttr>()) |
Bill Wendling | 72390b3 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 1054 | FuncAttrs.addAttribute(llvm::Attribute::ReturnsTwice); |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1055 | if (TargetDecl->hasAttr<NoThrowAttr>()) |
Bill Wendling | 72390b3 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 1056 | FuncAttrs.addAttribute(llvm::Attribute::NoUnwind); |
Richard Smith | 7586a6e | 2013-01-30 05:45:05 +0000 | [diff] [blame] | 1057 | if (TargetDecl->hasAttr<NoReturnAttr>()) |
| 1058 | FuncAttrs.addAttribute(llvm::Attribute::NoReturn); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1059 | if (TargetDecl->hasAttr<NoDuplicateAttr>()) |
| 1060 | FuncAttrs.addAttribute(llvm::Attribute::NoDuplicate); |
Richard Smith | 7586a6e | 2013-01-30 05:45:05 +0000 | [diff] [blame] | 1061 | |
| 1062 | if (const FunctionDecl *Fn = dyn_cast<FunctionDecl>(TargetDecl)) { |
John McCall | 9c0c1f3 | 2010-07-08 06:48:12 +0000 | [diff] [blame] | 1063 | const FunctionProtoType *FPT = Fn->getType()->getAs<FunctionProtoType>(); |
Sebastian Redl | 8026f6d | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 1064 | if (FPT && FPT->isNothrow(getContext())) |
Bill Wendling | 72390b3 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 1065 | FuncAttrs.addAttribute(llvm::Attribute::NoUnwind); |
Richard Smith | 3c5cd15 | 2013-03-05 08:30:04 +0000 | [diff] [blame] | 1066 | // Don't use [[noreturn]] or _Noreturn for a call to a virtual function. |
| 1067 | // These attributes are not inherited by overloads. |
| 1068 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Fn); |
| 1069 | if (Fn->isNoReturn() && !(AttrOnCallSite && MD && MD->isVirtual())) |
Richard Smith | 7586a6e | 2013-01-30 05:45:05 +0000 | [diff] [blame] | 1070 | FuncAttrs.addAttribute(llvm::Attribute::NoReturn); |
John McCall | 9c0c1f3 | 2010-07-08 06:48:12 +0000 | [diff] [blame] | 1071 | } |
| 1072 | |
Eric Christopher | 041087c | 2011-08-15 22:38:22 +0000 | [diff] [blame] | 1073 | // 'const' and 'pure' attribute functions are also nounwind. |
| 1074 | if (TargetDecl->hasAttr<ConstAttr>()) { |
Bill Wendling | 72390b3 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 1075 | FuncAttrs.addAttribute(llvm::Attribute::ReadNone); |
| 1076 | FuncAttrs.addAttribute(llvm::Attribute::NoUnwind); |
Eric Christopher | 041087c | 2011-08-15 22:38:22 +0000 | [diff] [blame] | 1077 | } else if (TargetDecl->hasAttr<PureAttr>()) { |
Bill Wendling | 72390b3 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 1078 | FuncAttrs.addAttribute(llvm::Attribute::ReadOnly); |
| 1079 | FuncAttrs.addAttribute(llvm::Attribute::NoUnwind); |
Eric Christopher | 041087c | 2011-08-15 22:38:22 +0000 | [diff] [blame] | 1080 | } |
Ryan Flynn | 76168e2 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1081 | if (TargetDecl->hasAttr<MallocAttr>()) |
Bill Wendling | 72390b3 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 1082 | RetAttrs.addAttribute(llvm::Attribute::NoAlias); |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1083 | } |
| 1084 | |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 1085 | if (CodeGenOpts.OptimizeSize) |
Bill Wendling | 72390b3 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 1086 | FuncAttrs.addAttribute(llvm::Attribute::OptimizeForSize); |
Quentin Colombet | 9046768 | 2012-10-26 00:29:48 +0000 | [diff] [blame] | 1087 | if (CodeGenOpts.OptimizeSize == 2) |
Bill Wendling | 72390b3 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 1088 | FuncAttrs.addAttribute(llvm::Attribute::MinSize); |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 1089 | if (CodeGenOpts.DisableRedZone) |
Bill Wendling | 72390b3 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 1090 | FuncAttrs.addAttribute(llvm::Attribute::NoRedZone); |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 1091 | if (CodeGenOpts.NoImplicitFloat) |
Bill Wendling | 72390b3 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 1092 | FuncAttrs.addAttribute(llvm::Attribute::NoImplicitFloat); |
Devang Patel | 24095da | 2009-06-04 23:32:02 +0000 | [diff] [blame] | 1093 | |
Bill Wendling | 93e4bff | 2013-02-22 20:53:29 +0000 | [diff] [blame] | 1094 | if (AttrOnCallSite) { |
| 1095 | // Attributes that should go on the call site only. |
| 1096 | if (!CodeGenOpts.SimplifyLibCalls) |
| 1097 | FuncAttrs.addAttribute(llvm::Attribute::NoBuiltin); |
Bill Wendling | be9e8bf | 2013-02-28 22:49:57 +0000 | [diff] [blame] | 1098 | } else { |
| 1099 | // Attributes that should go on the function, but not the call site. |
Bill Wendling | be9e8bf | 2013-02-28 22:49:57 +0000 | [diff] [blame] | 1100 | if (!CodeGenOpts.DisableFPElim) { |
Bill Wendling | 4159f05 | 2013-03-13 22:24:33 +0000 | [diff] [blame] | 1101 | FuncAttrs.addAttribute("no-frame-pointer-elim", "false"); |
Bill Wendling | be9e8bf | 2013-02-28 22:49:57 +0000 | [diff] [blame] | 1102 | } else if (CodeGenOpts.OmitLeafFramePointer) { |
Bill Wendling | 4159f05 | 2013-03-13 22:24:33 +0000 | [diff] [blame] | 1103 | FuncAttrs.addAttribute("no-frame-pointer-elim", "false"); |
Bill Wendling | fae228b | 2013-08-22 21:16:51 +0000 | [diff] [blame] | 1104 | FuncAttrs.addAttribute("no-frame-pointer-elim-non-leaf"); |
Bill Wendling | be9e8bf | 2013-02-28 22:49:57 +0000 | [diff] [blame] | 1105 | } else { |
Bill Wendling | 4159f05 | 2013-03-13 22:24:33 +0000 | [diff] [blame] | 1106 | FuncAttrs.addAttribute("no-frame-pointer-elim", "true"); |
Bill Wendling | fae228b | 2013-08-22 21:16:51 +0000 | [diff] [blame] | 1107 | FuncAttrs.addAttribute("no-frame-pointer-elim-non-leaf"); |
Bill Wendling | be9e8bf | 2013-02-28 22:49:57 +0000 | [diff] [blame] | 1108 | } |
| 1109 | |
Bill Wendling | 4159f05 | 2013-03-13 22:24:33 +0000 | [diff] [blame] | 1110 | FuncAttrs.addAttribute("less-precise-fpmad", |
Bill Wendling | 52d08fe | 2013-07-26 21:51:11 +0000 | [diff] [blame] | 1111 | llvm::toStringRef(CodeGenOpts.LessPreciseFPMAD)); |
Bill Wendling | 4159f05 | 2013-03-13 22:24:33 +0000 | [diff] [blame] | 1112 | FuncAttrs.addAttribute("no-infs-fp-math", |
Bill Wendling | 52d08fe | 2013-07-26 21:51:11 +0000 | [diff] [blame] | 1113 | llvm::toStringRef(CodeGenOpts.NoInfsFPMath)); |
Bill Wendling | 4159f05 | 2013-03-13 22:24:33 +0000 | [diff] [blame] | 1114 | FuncAttrs.addAttribute("no-nans-fp-math", |
Bill Wendling | 52d08fe | 2013-07-26 21:51:11 +0000 | [diff] [blame] | 1115 | llvm::toStringRef(CodeGenOpts.NoNaNsFPMath)); |
Bill Wendling | 4159f05 | 2013-03-13 22:24:33 +0000 | [diff] [blame] | 1116 | FuncAttrs.addAttribute("unsafe-fp-math", |
Bill Wendling | 52d08fe | 2013-07-26 21:51:11 +0000 | [diff] [blame] | 1117 | llvm::toStringRef(CodeGenOpts.UnsafeFPMath)); |
Bill Wendling | 4159f05 | 2013-03-13 22:24:33 +0000 | [diff] [blame] | 1118 | FuncAttrs.addAttribute("use-soft-float", |
Bill Wendling | 52d08fe | 2013-07-26 21:51:11 +0000 | [diff] [blame] | 1119 | llvm::toStringRef(CodeGenOpts.SoftFloat)); |
Bill Wendling | 45ccf28 | 2013-07-22 20:15:41 +0000 | [diff] [blame] | 1120 | FuncAttrs.addAttribute("stack-protector-buffer-size", |
Bill Wendling | 8d230b4 | 2013-07-12 22:26:07 +0000 | [diff] [blame] | 1121 | llvm::utostr(CodeGenOpts.SSPBufferSize)); |
Bill Wendling | cab4a09 | 2013-07-25 00:32:41 +0000 | [diff] [blame] | 1122 | |
Bill Wendling | 1cf9ab8 | 2013-08-01 21:41:02 +0000 | [diff] [blame] | 1123 | if (!CodeGenOpts.StackRealignment) |
| 1124 | FuncAttrs.addAttribute("no-realign-stack"); |
Bill Wendling | c0dcc2d | 2013-02-15 21:30:01 +0000 | [diff] [blame] | 1125 | } |
| 1126 | |
Daniel Dunbar | a0a99e0 | 2009-02-02 23:43:58 +0000 | [diff] [blame] | 1127 | QualType RetTy = FI.getReturnType(); |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1128 | unsigned Index = 1; |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 1129 | const ABIArgInfo &RetAI = FI.getReturnInfo(); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1130 | switch (RetAI.getKind()) { |
Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 1131 | case ABIArgInfo::Extend: |
Jakob Stoklund Olesen | 5baefa8 | 2013-05-29 03:57:23 +0000 | [diff] [blame] | 1132 | if (RetTy->hasSignedIntegerRepresentation()) |
| 1133 | RetAttrs.addAttribute(llvm::Attribute::SExt); |
| 1134 | else if (RetTy->hasUnsignedIntegerRepresentation()) |
| 1135 | RetAttrs.addAttribute(llvm::Attribute::ZExt); |
Jakob Stoklund Olesen | 90f9ec0 | 2013-06-05 03:00:09 +0000 | [diff] [blame] | 1136 | // FALL THROUGH |
Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 1137 | case ABIArgInfo::Direct: |
Jakob Stoklund Olesen | 90f9ec0 | 2013-06-05 03:00:09 +0000 | [diff] [blame] | 1138 | if (RetAI.getInReg()) |
| 1139 | RetAttrs.addAttribute(llvm::Attribute::InReg); |
| 1140 | break; |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1141 | case ABIArgInfo::Ignore: |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1142 | break; |
| 1143 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1144 | case ABIArgInfo::InAlloca: { |
| 1145 | // inalloca disables readnone and readonly |
| 1146 | FuncAttrs.removeAttribute(llvm::Attribute::ReadOnly) |
| 1147 | .removeAttribute(llvm::Attribute::ReadNone); |
| 1148 | break; |
| 1149 | } |
| 1150 | |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1151 | case ABIArgInfo::Indirect: { |
Bill Wendling | 0d58339 | 2012-10-15 20:36:26 +0000 | [diff] [blame] | 1152 | llvm::AttrBuilder SRETAttrs; |
Bill Wendling | 72390b3 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 1153 | SRETAttrs.addAttribute(llvm::Attribute::StructRet); |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1154 | if (RetAI.getInReg()) |
Bill Wendling | 72390b3 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 1155 | SRETAttrs.addAttribute(llvm::Attribute::InReg); |
Bill Wendling | 603571a | 2012-10-10 07:36:56 +0000 | [diff] [blame] | 1156 | PAL.push_back(llvm:: |
Bill Wendling | b263bdf | 2013-01-27 02:46:53 +0000 | [diff] [blame] | 1157 | AttributeSet::get(getLLVMContext(), Index, SRETAttrs)); |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1158 | |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1159 | ++Index; |
Daniel Dunbar | 0ac86f0 | 2009-03-18 19:51:01 +0000 | [diff] [blame] | 1160 | // sret disables readnone and readonly |
Bill Wendling | 72390b3 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 1161 | FuncAttrs.removeAttribute(llvm::Attribute::ReadOnly) |
| 1162 | .removeAttribute(llvm::Attribute::ReadNone); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1163 | break; |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1164 | } |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1165 | |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1166 | case ABIArgInfo::Expand: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1167 | llvm_unreachable("Invalid ABI kind for return argument"); |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1168 | } |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1169 | |
Bill Wendling | 603571a | 2012-10-10 07:36:56 +0000 | [diff] [blame] | 1170 | if (RetAttrs.hasAttributes()) |
| 1171 | PAL.push_back(llvm:: |
Bill Wendling | b263bdf | 2013-01-27 02:46:53 +0000 | [diff] [blame] | 1172 | AttributeSet::get(getLLVMContext(), |
| 1173 | llvm::AttributeSet::ReturnIndex, |
| 1174 | RetAttrs)); |
Anton Korobeynikov | 1102f42 | 2009-04-04 00:49:24 +0000 | [diff] [blame] | 1175 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1176 | for (const auto &I : FI.arguments()) { |
| 1177 | QualType ParamType = I.type; |
| 1178 | const ABIArgInfo &AI = I.info; |
Bill Wendling | 0d58339 | 2012-10-15 20:36:26 +0000 | [diff] [blame] | 1179 | llvm::AttrBuilder Attrs; |
Anton Korobeynikov | 1102f42 | 2009-04-04 00:49:24 +0000 | [diff] [blame] | 1180 | |
Rafael Espindola | e4aeeaa | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1181 | if (AI.getPaddingType()) { |
Bill Wendling | b263bdf | 2013-01-27 02:46:53 +0000 | [diff] [blame] | 1182 | if (AI.getPaddingInReg()) |
| 1183 | PAL.push_back(llvm::AttributeSet::get(getLLVMContext(), Index, |
| 1184 | llvm::Attribute::InReg)); |
Rafael Espindola | e4aeeaa | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1185 | // Increment Index if there is padding. |
| 1186 | ++Index; |
| 1187 | } |
| 1188 | |
John McCall | d8e10d2 | 2010-03-27 00:47:27 +0000 | [diff] [blame] | 1189 | // 'restrict' -> 'noalias' is done in EmitFunctionProlog when we |
| 1190 | // have the corresponding parameter variable. It doesn't make |
Daniel Dunbar | 7f6890e | 2011-02-10 18:10:07 +0000 | [diff] [blame] | 1191 | // sense to do it here because parameters are so messed up. |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1192 | switch (AI.getKind()) { |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1193 | case ABIArgInfo::Extend: |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 1194 | if (ParamType->isSignedIntegerOrEnumerationType()) |
Bill Wendling | 72390b3 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 1195 | Attrs.addAttribute(llvm::Attribute::SExt); |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 1196 | else if (ParamType->isUnsignedIntegerOrEnumerationType()) |
Bill Wendling | 72390b3 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 1197 | Attrs.addAttribute(llvm::Attribute::ZExt); |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1198 | // FALL THROUGH |
| 1199 | case ABIArgInfo::Direct: |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1200 | if (AI.getInReg()) |
Bill Wendling | 72390b3 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 1201 | Attrs.addAttribute(llvm::Attribute::InReg); |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1202 | |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1203 | // FIXME: handle sseregparm someday... |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1204 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1205 | if (llvm::StructType *STy = |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1206 | dyn_cast<llvm::StructType>(AI.getCoerceToType())) { |
| 1207 | unsigned Extra = STy->getNumElements()-1; // 1 will be added below. |
Bill Wendling | 603571a | 2012-10-10 07:36:56 +0000 | [diff] [blame] | 1208 | if (Attrs.hasAttributes()) |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1209 | for (unsigned I = 0; I < Extra; ++I) |
Bill Wendling | b263bdf | 2013-01-27 02:46:53 +0000 | [diff] [blame] | 1210 | PAL.push_back(llvm::AttributeSet::get(getLLVMContext(), Index + I, |
| 1211 | Attrs)); |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1212 | Index += Extra; |
| 1213 | } |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1214 | break; |
Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 1215 | |
Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 1216 | case ABIArgInfo::Indirect: |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1217 | if (AI.getInReg()) |
Bill Wendling | 72390b3 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 1218 | Attrs.addAttribute(llvm::Attribute::InReg); |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1219 | |
Anders Carlsson | 0a8f847 | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 1220 | if (AI.getIndirectByVal()) |
Bill Wendling | 72390b3 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 1221 | Attrs.addAttribute(llvm::Attribute::ByVal); |
Anders Carlsson | 0a8f847 | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 1222 | |
Bill Wendling | 603571a | 2012-10-10 07:36:56 +0000 | [diff] [blame] | 1223 | Attrs.addAlignmentAttr(AI.getIndirectAlign()); |
| 1224 | |
Daniel Dunbar | 0ac86f0 | 2009-03-18 19:51:01 +0000 | [diff] [blame] | 1225 | // byval disables readnone and readonly. |
Bill Wendling | 72390b3 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 1226 | FuncAttrs.removeAttribute(llvm::Attribute::ReadOnly) |
| 1227 | .removeAttribute(llvm::Attribute::ReadNone); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1228 | break; |
Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 1229 | |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1230 | case ABIArgInfo::Ignore: |
| 1231 | // Skip increment, no matching LLVM parameter. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1232 | continue; |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1233 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1234 | case ABIArgInfo::InAlloca: |
| 1235 | // inalloca disables readnone and readonly. |
| 1236 | FuncAttrs.removeAttribute(llvm::Attribute::ReadOnly) |
| 1237 | .removeAttribute(llvm::Attribute::ReadNone); |
| 1238 | // Skip increment, no matching LLVM parameter. |
| 1239 | continue; |
| 1240 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1241 | case ABIArgInfo::Expand: { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1242 | SmallVector<llvm::Type*, 8> types; |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1243 | // FIXME: This is rather inefficient. Do we ever actually need to do |
| 1244 | // anything here? The result should be just reconstructed on the other |
| 1245 | // side, so extension should be a non-issue. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1246 | getTypes().GetExpandedTypes(ParamType, types); |
John McCall | 42e0611 | 2011-05-15 02:19:42 +0000 | [diff] [blame] | 1247 | Index += types.size(); |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1248 | continue; |
| 1249 | } |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1250 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1251 | |
Bill Wendling | 603571a | 2012-10-10 07:36:56 +0000 | [diff] [blame] | 1252 | if (Attrs.hasAttributes()) |
Bill Wendling | b263bdf | 2013-01-27 02:46:53 +0000 | [diff] [blame] | 1253 | PAL.push_back(llvm::AttributeSet::get(getLLVMContext(), Index, Attrs)); |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1254 | ++Index; |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1255 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1256 | |
| 1257 | // Add the inalloca attribute to the trailing inalloca parameter if present. |
| 1258 | if (FI.usesInAlloca()) { |
| 1259 | llvm::AttrBuilder Attrs; |
| 1260 | Attrs.addAttribute(llvm::Attribute::InAlloca); |
| 1261 | PAL.push_back(llvm::AttributeSet::get(getLLVMContext(), Index, Attrs)); |
| 1262 | } |
| 1263 | |
Bill Wendling | 603571a | 2012-10-10 07:36:56 +0000 | [diff] [blame] | 1264 | if (FuncAttrs.hasAttributes()) |
Bill Wendling | 75d37b4 | 2012-10-15 07:31:59 +0000 | [diff] [blame] | 1265 | PAL.push_back(llvm:: |
Bill Wendling | b263bdf | 2013-01-27 02:46:53 +0000 | [diff] [blame] | 1266 | AttributeSet::get(getLLVMContext(), |
| 1267 | llvm::AttributeSet::FunctionIndex, |
| 1268 | FuncAttrs)); |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1269 | } |
| 1270 | |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1271 | /// An argument came in as a promoted argument; demote it back to its |
| 1272 | /// declared type. |
| 1273 | static llvm::Value *emitArgumentDemotion(CodeGenFunction &CGF, |
| 1274 | const VarDecl *var, |
| 1275 | llvm::Value *value) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1276 | llvm::Type *varType = CGF.ConvertType(var->getType()); |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1277 | |
| 1278 | // This can happen with promotions that actually don't change the |
| 1279 | // underlying type, like the enum promotions. |
| 1280 | if (value->getType() == varType) return value; |
| 1281 | |
| 1282 | assert((varType->isIntegerTy() || varType->isFloatingPointTy()) |
| 1283 | && "unexpected promotion type"); |
| 1284 | |
| 1285 | if (isa<llvm::IntegerType>(varType)) |
| 1286 | return CGF.Builder.CreateTrunc(value, varType, "arg.unpromote"); |
| 1287 | |
| 1288 | return CGF.Builder.CreateFPCast(value, varType, "arg.unpromote"); |
| 1289 | } |
| 1290 | |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1291 | void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI, |
| 1292 | llvm::Function *Fn, |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1293 | const FunctionArgList &Args) { |
John McCall | 0cfeb63 | 2009-07-28 01:00:58 +0000 | [diff] [blame] | 1294 | // If this is an implicit-return-zero function, go ahead and |
| 1295 | // initialize the return value. TODO: it might be nice to have |
| 1296 | // a more general mechanism for this that didn't require synthesized |
| 1297 | // return statements. |
John McCall | f5ebf9b | 2013-05-03 07:33:41 +0000 | [diff] [blame] | 1298 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurCodeDecl)) { |
John McCall | 0cfeb63 | 2009-07-28 01:00:58 +0000 | [diff] [blame] | 1299 | if (FD->hasImplicitReturnZero()) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1300 | QualType RetTy = FD->getReturnType().getUnqualifiedType(); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1301 | llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy); |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 1302 | llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy); |
John McCall | 0cfeb63 | 2009-07-28 01:00:58 +0000 | [diff] [blame] | 1303 | Builder.CreateStore(Zero, ReturnValue); |
| 1304 | } |
| 1305 | } |
| 1306 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1307 | // FIXME: We no longer need the types from FunctionArgList; lift up and |
| 1308 | // simplify. |
Daniel Dunbar | 5251afa | 2009-02-03 06:02:10 +0000 | [diff] [blame] | 1309 | |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1310 | // Emit allocs for param decls. Give the LLVM Argument nodes names. |
| 1311 | llvm::Function::arg_iterator AI = Fn->arg_begin(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1312 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1313 | // If we're using inalloca, all the memory arguments are GEPs off of the last |
| 1314 | // parameter, which is a pointer to the complete memory area. |
| 1315 | llvm::Value *ArgStruct = 0; |
| 1316 | if (FI.usesInAlloca()) { |
| 1317 | llvm::Function::arg_iterator EI = Fn->arg_end(); |
| 1318 | --EI; |
| 1319 | ArgStruct = EI; |
| 1320 | assert(ArgStruct->getType() == FI.getArgStruct()->getPointerTo()); |
| 1321 | } |
| 1322 | |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1323 | // Name the struct return argument. |
Daniel Dunbar | dacf9dd | 2010-07-14 23:39:36 +0000 | [diff] [blame] | 1324 | if (CGM.ReturnTypeUsesSRet(FI)) { |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1325 | AI->setName("agg.result"); |
Bill Wendling | 89530e4 | 2013-01-23 06:15:10 +0000 | [diff] [blame] | 1326 | AI->addAttr(llvm::AttributeSet::get(getLLVMContext(), |
| 1327 | AI->getArgNo() + 1, |
| 1328 | llvm::Attribute::NoAlias)); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1329 | ++AI; |
| 1330 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1331 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1332 | // Track if we received the parameter as a pointer (indirect, byval, or |
| 1333 | // inalloca). If already have a pointer, EmitParmDecl doesn't need to copy it |
| 1334 | // into a local alloca for us. |
| 1335 | enum ValOrPointer { HaveValue = 0, HavePointer = 1 }; |
| 1336 | typedef llvm::PointerIntPair<llvm::Value *, 1> ValueAndIsPtr; |
| 1337 | SmallVector<ValueAndIsPtr, 16> ArgVals; |
| 1338 | ArgVals.reserve(Args.size()); |
| 1339 | |
| 1340 | // Create a pointer value for every parameter declaration. This usually |
| 1341 | // entails copying one or more LLVM IR arguments into an alloca. Don't push |
| 1342 | // any cleanups or do anything that might unwind. We do that separately, so |
| 1343 | // we can push the cleanups in the correct order for the ABI. |
Daniel Dunbar | 4b5f0a4 | 2009-02-04 21:17:21 +0000 | [diff] [blame] | 1344 | assert(FI.arg_size() == Args.size() && |
| 1345 | "Mismatch between function signature & arguments."); |
Devang Patel | 093ac46 | 2011-03-03 20:13:15 +0000 | [diff] [blame] | 1346 | unsigned ArgNo = 1; |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 1347 | CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin(); |
Devang Patel | 093ac46 | 2011-03-03 20:13:15 +0000 | [diff] [blame] | 1348 | for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end(); |
| 1349 | i != e; ++i, ++info_it, ++ArgNo) { |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1350 | const VarDecl *Arg = *i; |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 1351 | QualType Ty = info_it->type; |
| 1352 | const ABIArgInfo &ArgI = info_it->info; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1353 | |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1354 | bool isPromoted = |
| 1355 | isa<ParmVarDecl>(Arg) && cast<ParmVarDecl>(Arg)->isKNRPromoted(); |
| 1356 | |
Rafael Espindola | e4aeeaa | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1357 | // Skip the dummy padding argument. |
| 1358 | if (ArgI.getPaddingType()) |
| 1359 | ++AI; |
| 1360 | |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1361 | switch (ArgI.getKind()) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1362 | case ABIArgInfo::InAlloca: { |
| 1363 | llvm::Value *V = Builder.CreateStructGEP( |
| 1364 | ArgStruct, ArgI.getInAllocaFieldIndex(), Arg->getName()); |
| 1365 | ArgVals.push_back(ValueAndIsPtr(V, HavePointer)); |
| 1366 | continue; // Don't increment AI! |
| 1367 | } |
| 1368 | |
Daniel Dunbar | 1f74598 | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 1369 | case ABIArgInfo::Indirect: { |
Chris Lattner | ce70016 | 2010-06-28 23:44:11 +0000 | [diff] [blame] | 1370 | llvm::Value *V = AI; |
Daniel Dunbar | cf3b6f2 | 2010-09-16 20:42:02 +0000 | [diff] [blame] | 1371 | |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 1372 | if (!hasScalarEvaluationKind(Ty)) { |
Daniel Dunbar | cf3b6f2 | 2010-09-16 20:42:02 +0000 | [diff] [blame] | 1373 | // Aggregates and complex variables are accessed by reference. All we |
| 1374 | // need to do is realign the value, if requested |
| 1375 | if (ArgI.getIndirectRealign()) { |
| 1376 | llvm::Value *AlignedTemp = CreateMemTemp(Ty, "coerce"); |
| 1377 | |
| 1378 | // Copy from the incoming argument pointer to the temporary with the |
| 1379 | // appropriate alignment. |
| 1380 | // |
| 1381 | // FIXME: We should have a common utility for generating an aggregate |
| 1382 | // copy. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1383 | llvm::Type *I8PtrTy = Builder.getInt8PtrTy(); |
Ken Dyck | fe71008 | 2011-01-19 01:58:38 +0000 | [diff] [blame] | 1384 | CharUnits Size = getContext().getTypeSizeInChars(Ty); |
NAKAMURA Takumi | c95a8fc | 2011-03-10 14:02:21 +0000 | [diff] [blame] | 1385 | llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy); |
| 1386 | llvm::Value *Src = Builder.CreateBitCast(V, I8PtrTy); |
| 1387 | Builder.CreateMemCpy(Dst, |
| 1388 | Src, |
Ken Dyck | fe71008 | 2011-01-19 01:58:38 +0000 | [diff] [blame] | 1389 | llvm::ConstantInt::get(IntPtrTy, |
| 1390 | Size.getQuantity()), |
Benjamin Kramer | 9f0c7cc | 2010-12-30 00:13:21 +0000 | [diff] [blame] | 1391 | ArgI.getIndirectAlign(), |
| 1392 | false); |
Daniel Dunbar | cf3b6f2 | 2010-09-16 20:42:02 +0000 | [diff] [blame] | 1393 | V = AlignedTemp; |
| 1394 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1395 | ArgVals.push_back(ValueAndIsPtr(V, HavePointer)); |
Daniel Dunbar | 1f74598 | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 1396 | } else { |
| 1397 | // Load scalar value from indirect argument. |
Ken Dyck | fe71008 | 2011-01-19 01:58:38 +0000 | [diff] [blame] | 1398 | CharUnits Alignment = getContext().getTypeAlignInChars(Ty); |
Nick Lewycky | 4ee7dc2 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 1399 | V = EmitLoadOfScalar(V, false, Alignment.getQuantity(), Ty, |
| 1400 | Arg->getLocStart()); |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1401 | |
| 1402 | if (isPromoted) |
| 1403 | V = emitArgumentDemotion(*this, Arg, V); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1404 | ArgVals.push_back(ValueAndIsPtr(V, HaveValue)); |
Daniel Dunbar | 1f74598 | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 1405 | } |
Daniel Dunbar | 1f74598 | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 1406 | break; |
| 1407 | } |
Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 1408 | |
| 1409 | case ABIArgInfo::Extend: |
Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 1410 | case ABIArgInfo::Direct: { |
Akira Hatanaka | 4ba3fd4 | 2012-01-09 19:08:06 +0000 | [diff] [blame] | 1411 | |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1412 | // If we have the trivial case, handle it with no muss and fuss. |
| 1413 | if (!isa<llvm::StructType>(ArgI.getCoerceToType()) && |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 1414 | ArgI.getCoerceToType() == ConvertType(Ty) && |
| 1415 | ArgI.getDirectOffset() == 0) { |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1416 | assert(AI != Fn->arg_end() && "Argument mismatch!"); |
| 1417 | llvm::Value *V = AI; |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1418 | |
Bill Wendling | a637556 | 2012-10-16 05:23:44 +0000 | [diff] [blame] | 1419 | if (Arg->getType().isRestrictQualified()) |
Bill Wendling | 89530e4 | 2013-01-23 06:15:10 +0000 | [diff] [blame] | 1420 | AI->addAttr(llvm::AttributeSet::get(getLLVMContext(), |
| 1421 | AI->getArgNo() + 1, |
| 1422 | llvm::Attribute::NoAlias)); |
John McCall | d8e10d2 | 2010-03-27 00:47:27 +0000 | [diff] [blame] | 1423 | |
Chris Lattner | b13eab9 | 2011-07-20 06:29:00 +0000 | [diff] [blame] | 1424 | // Ensure the argument is the correct type. |
| 1425 | if (V->getType() != ArgI.getCoerceToType()) |
| 1426 | V = Builder.CreateBitCast(V, ArgI.getCoerceToType()); |
| 1427 | |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1428 | if (isPromoted) |
| 1429 | V = emitArgumentDemotion(*this, Arg, V); |
Rafael Espindola | 8b8a09e | 2012-11-29 16:09:03 +0000 | [diff] [blame] | 1430 | |
Nick Lewycky | 5d4a755 | 2013-10-01 21:51:38 +0000 | [diff] [blame] | 1431 | if (const CXXMethodDecl *MD = |
| 1432 | dyn_cast_or_null<CXXMethodDecl>(CurCodeDecl)) { |
Timur Iskhodzhanov | 8f189a9 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 1433 | if (MD->isVirtual() && Arg == CXXABIThisDecl) |
Nick Lewycky | 5d4a755 | 2013-10-01 21:51:38 +0000 | [diff] [blame] | 1434 | V = CGM.getCXXABI(). |
| 1435 | adjustThisParameterInVirtualFunctionPrologue(*this, CurGD, V); |
Timur Iskhodzhanov | 8f189a9 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 1436 | } |
| 1437 | |
Rafael Espindola | 8b8a09e | 2012-11-29 16:09:03 +0000 | [diff] [blame] | 1438 | // Because of merging of function types from multiple decls it is |
| 1439 | // possible for the type of an argument to not match the corresponding |
| 1440 | // type in the function type. Since we are codegening the callee |
| 1441 | // in here, add a cast to the argument type. |
| 1442 | llvm::Type *LTy = ConvertType(Arg->getType()); |
| 1443 | if (V->getType() != LTy) |
| 1444 | V = Builder.CreateBitCast(V, LTy); |
| 1445 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1446 | ArgVals.push_back(ValueAndIsPtr(V, HaveValue)); |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1447 | break; |
Daniel Dunbar | 8b979d9 | 2009-02-10 00:06:49 +0000 | [diff] [blame] | 1448 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1449 | |
Evgeniy Stepanov | a6ce20e | 2012-02-10 09:30:15 +0000 | [diff] [blame] | 1450 | llvm::AllocaInst *Alloca = CreateMemTemp(Ty, Arg->getName()); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1451 | |
Chris Lattner | deabde2 | 2010-07-28 18:24:28 +0000 | [diff] [blame] | 1452 | // The alignment we need to use is the max of the requested alignment for |
| 1453 | // the argument plus the alignment required by our access code below. |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1454 | unsigned AlignmentToUse = |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 1455 | CGM.getDataLayout().getABITypeAlignment(ArgI.getCoerceToType()); |
Chris Lattner | deabde2 | 2010-07-28 18:24:28 +0000 | [diff] [blame] | 1456 | AlignmentToUse = std::max(AlignmentToUse, |
| 1457 | (unsigned)getContext().getDeclAlign(Arg).getQuantity()); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1458 | |
Chris Lattner | deabde2 | 2010-07-28 18:24:28 +0000 | [diff] [blame] | 1459 | Alloca->setAlignment(AlignmentToUse); |
Chris Lattner | 121b3fa | 2010-07-05 20:21:00 +0000 | [diff] [blame] | 1460 | llvm::Value *V = Alloca; |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 1461 | llvm::Value *Ptr = V; // Pointer to store into. |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1462 | |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 1463 | // If the value is offset in memory, apply the offset now. |
| 1464 | if (unsigned Offs = ArgI.getDirectOffset()) { |
| 1465 | Ptr = Builder.CreateBitCast(Ptr, Builder.getInt8PtrTy()); |
| 1466 | Ptr = Builder.CreateConstGEP1_32(Ptr, Offs); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1467 | Ptr = Builder.CreateBitCast(Ptr, |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 1468 | llvm::PointerType::getUnqual(ArgI.getCoerceToType())); |
| 1469 | } |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1470 | |
Chris Lattner | 309c59f | 2010-06-29 00:06:42 +0000 | [diff] [blame] | 1471 | // If the coerce-to type is a first class aggregate, we flatten it and |
| 1472 | // pass the elements. Either way is semantically identical, but fast-isel |
| 1473 | // and the optimizer generally likes scalar values better than FCAs. |
Evgeniy Stepanov | a6ce20e | 2012-02-10 09:30:15 +0000 | [diff] [blame] | 1474 | llvm::StructType *STy = dyn_cast<llvm::StructType>(ArgI.getCoerceToType()); |
| 1475 | if (STy && STy->getNumElements() > 1) { |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 1476 | uint64_t SrcSize = CGM.getDataLayout().getTypeAllocSize(STy); |
Evgeniy Stepanov | a6ce20e | 2012-02-10 09:30:15 +0000 | [diff] [blame] | 1477 | llvm::Type *DstTy = |
| 1478 | cast<llvm::PointerType>(Ptr->getType())->getElementType(); |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 1479 | uint64_t DstSize = CGM.getDataLayout().getTypeAllocSize(DstTy); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1480 | |
Evgeniy Stepanov | a6ce20e | 2012-02-10 09:30:15 +0000 | [diff] [blame] | 1481 | if (SrcSize <= DstSize) { |
| 1482 | Ptr = Builder.CreateBitCast(Ptr, llvm::PointerType::getUnqual(STy)); |
| 1483 | |
| 1484 | for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { |
| 1485 | assert(AI != Fn->arg_end() && "Argument mismatch!"); |
| 1486 | AI->setName(Arg->getName() + ".coerce" + Twine(i)); |
| 1487 | llvm::Value *EltPtr = Builder.CreateConstGEP2_32(Ptr, 0, i); |
| 1488 | Builder.CreateStore(AI++, EltPtr); |
| 1489 | } |
| 1490 | } else { |
| 1491 | llvm::AllocaInst *TempAlloca = |
| 1492 | CreateTempAlloca(ArgI.getCoerceToType(), "coerce"); |
| 1493 | TempAlloca->setAlignment(AlignmentToUse); |
| 1494 | llvm::Value *TempV = TempAlloca; |
| 1495 | |
| 1496 | for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { |
| 1497 | assert(AI != Fn->arg_end() && "Argument mismatch!"); |
| 1498 | AI->setName(Arg->getName() + ".coerce" + Twine(i)); |
| 1499 | llvm::Value *EltPtr = Builder.CreateConstGEP2_32(TempV, 0, i); |
| 1500 | Builder.CreateStore(AI++, EltPtr); |
| 1501 | } |
| 1502 | |
| 1503 | Builder.CreateMemCpy(Ptr, TempV, DstSize, AlignmentToUse); |
Chris Lattner | 309c59f | 2010-06-29 00:06:42 +0000 | [diff] [blame] | 1504 | } |
| 1505 | } else { |
| 1506 | // Simple case, just do a coerced store of the argument into the alloca. |
| 1507 | assert(AI != Fn->arg_end() && "Argument mismatch!"); |
Chris Lattner | 225e286 | 2010-06-29 00:14:52 +0000 | [diff] [blame] | 1508 | AI->setName(Arg->getName() + ".coerce"); |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 1509 | CreateCoercedStore(AI++, Ptr, /*DestIsVolatile=*/false, *this); |
Chris Lattner | 309c59f | 2010-06-29 00:06:42 +0000 | [diff] [blame] | 1510 | } |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1511 | |
| 1512 | |
Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 1513 | // Match to what EmitParmDecl is expecting for this type. |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 1514 | if (CodeGenFunction::hasScalarEvaluationKind(Ty)) { |
Nick Lewycky | 4ee7dc2 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 1515 | V = EmitLoadOfScalar(V, false, AlignmentToUse, Ty, Arg->getLocStart()); |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1516 | if (isPromoted) |
| 1517 | V = emitArgumentDemotion(*this, Arg, V); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1518 | ArgVals.push_back(ValueAndIsPtr(V, HaveValue)); |
| 1519 | } else { |
| 1520 | ArgVals.push_back(ValueAndIsPtr(V, HavePointer)); |
Daniel Dunbar | 8b29a38 | 2009-02-04 07:22:24 +0000 | [diff] [blame] | 1521 | } |
Chris Lattner | ce70016 | 2010-06-28 23:44:11 +0000 | [diff] [blame] | 1522 | continue; // Skip ++AI increment, already done. |
Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 1523 | } |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1524 | |
| 1525 | case ABIArgInfo::Expand: { |
| 1526 | // If this structure was expanded into multiple arguments then |
| 1527 | // we need to create a temporary and reconstruct it from the |
| 1528 | // arguments. |
Eli Friedman | 1bb94a4 | 2011-11-03 21:39:02 +0000 | [diff] [blame] | 1529 | llvm::AllocaInst *Alloca = CreateMemTemp(Ty); |
Eli Friedman | 6da2c71 | 2011-12-03 04:14:32 +0000 | [diff] [blame] | 1530 | CharUnits Align = getContext().getDeclAlign(Arg); |
| 1531 | Alloca->setAlignment(Align.getQuantity()); |
| 1532 | LValue LV = MakeAddrLValue(Alloca, Ty, Align); |
Eli Friedman | 1bb94a4 | 2011-11-03 21:39:02 +0000 | [diff] [blame] | 1533 | llvm::Function::arg_iterator End = ExpandTypeFromArgs(Ty, LV, AI); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1534 | ArgVals.push_back(ValueAndIsPtr(Alloca, HavePointer)); |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1535 | |
| 1536 | // Name the arguments used in expansion and increment AI. |
| 1537 | unsigned Index = 0; |
| 1538 | for (; AI != End; ++AI, ++Index) |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1539 | AI->setName(Arg->getName() + "." + Twine(Index)); |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1540 | continue; |
| 1541 | } |
| 1542 | |
| 1543 | case ABIArgInfo::Ignore: |
| 1544 | // Initialize the local variable appropriately. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1545 | if (!hasScalarEvaluationKind(Ty)) { |
| 1546 | ArgVals.push_back(ValueAndIsPtr(CreateMemTemp(Ty), HavePointer)); |
| 1547 | } else { |
| 1548 | llvm::Value *U = llvm::UndefValue::get(ConvertType(Arg->getType())); |
| 1549 | ArgVals.push_back(ValueAndIsPtr(U, HaveValue)); |
| 1550 | } |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1551 | |
| 1552 | // Skip increment, no matching LLVM parameter. |
| 1553 | continue; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1554 | } |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1555 | |
| 1556 | ++AI; |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1557 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1558 | |
| 1559 | if (FI.usesInAlloca()) |
| 1560 | ++AI; |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1561 | assert(AI == Fn->arg_end() && "Argument mismatch!"); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1562 | |
| 1563 | if (getTarget().getCXXABI().areArgsDestroyedLeftToRightInCallee()) { |
| 1564 | for (int I = Args.size() - 1; I >= 0; --I) |
| 1565 | EmitParmDecl(*Args[I], ArgVals[I].getPointer(), ArgVals[I].getInt(), |
| 1566 | I + 1); |
| 1567 | } else { |
| 1568 | for (unsigned I = 0, E = Args.size(); I != E; ++I) |
| 1569 | EmitParmDecl(*Args[I], ArgVals[I].getPointer(), ArgVals[I].getInt(), |
| 1570 | I + 1); |
| 1571 | } |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1572 | } |
| 1573 | |
John McCall | 77fe6cd | 2012-01-29 07:46:59 +0000 | [diff] [blame] | 1574 | static void eraseUnusedBitCasts(llvm::Instruction *insn) { |
| 1575 | while (insn->use_empty()) { |
| 1576 | llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(insn); |
| 1577 | if (!bitcast) return; |
| 1578 | |
| 1579 | // This is "safe" because we would have used a ConstantExpr otherwise. |
| 1580 | insn = cast<llvm::Instruction>(bitcast->getOperand(0)); |
| 1581 | bitcast->eraseFromParent(); |
| 1582 | } |
| 1583 | } |
| 1584 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1585 | /// Try to emit a fused autorelease of a return result. |
| 1586 | static llvm::Value *tryEmitFusedAutoreleaseOfResult(CodeGenFunction &CGF, |
| 1587 | llvm::Value *result) { |
| 1588 | // We must be immediately followed the cast. |
| 1589 | llvm::BasicBlock *BB = CGF.Builder.GetInsertBlock(); |
| 1590 | if (BB->empty()) return 0; |
| 1591 | if (&BB->back() != result) return 0; |
| 1592 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1593 | llvm::Type *resultType = result->getType(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1594 | |
| 1595 | // result is in a BasicBlock and is therefore an Instruction. |
| 1596 | llvm::Instruction *generator = cast<llvm::Instruction>(result); |
| 1597 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1598 | SmallVector<llvm::Instruction*,4> insnsToKill; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1599 | |
| 1600 | // Look for: |
| 1601 | // %generator = bitcast %type1* %generator2 to %type2* |
| 1602 | while (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(generator)) { |
| 1603 | // We would have emitted this as a constant if the operand weren't |
| 1604 | // an Instruction. |
| 1605 | generator = cast<llvm::Instruction>(bitcast->getOperand(0)); |
| 1606 | |
| 1607 | // Require the generator to be immediately followed by the cast. |
| 1608 | if (generator->getNextNode() != bitcast) |
| 1609 | return 0; |
| 1610 | |
| 1611 | insnsToKill.push_back(bitcast); |
| 1612 | } |
| 1613 | |
| 1614 | // Look for: |
| 1615 | // %generator = call i8* @objc_retain(i8* %originalResult) |
| 1616 | // or |
| 1617 | // %generator = call i8* @objc_retainAutoreleasedReturnValue(i8* %originalResult) |
| 1618 | llvm::CallInst *call = dyn_cast<llvm::CallInst>(generator); |
| 1619 | if (!call) return 0; |
| 1620 | |
| 1621 | bool doRetainAutorelease; |
| 1622 | |
| 1623 | if (call->getCalledValue() == CGF.CGM.getARCEntrypoints().objc_retain) { |
| 1624 | doRetainAutorelease = true; |
| 1625 | } else if (call->getCalledValue() == CGF.CGM.getARCEntrypoints() |
| 1626 | .objc_retainAutoreleasedReturnValue) { |
| 1627 | doRetainAutorelease = false; |
| 1628 | |
John McCall | f9fdcc0 | 2012-09-07 23:30:50 +0000 | [diff] [blame] | 1629 | // If we emitted an assembly marker for this call (and the |
| 1630 | // ARCEntrypoints field should have been set if so), go looking |
| 1631 | // for that call. If we can't find it, we can't do this |
| 1632 | // optimization. But it should always be the immediately previous |
| 1633 | // instruction, unless we needed bitcasts around the call. |
| 1634 | if (CGF.CGM.getARCEntrypoints().retainAutoreleasedReturnValueMarker) { |
| 1635 | llvm::Instruction *prev = call->getPrevNode(); |
| 1636 | assert(prev); |
| 1637 | if (isa<llvm::BitCastInst>(prev)) { |
| 1638 | prev = prev->getPrevNode(); |
| 1639 | assert(prev); |
| 1640 | } |
| 1641 | assert(isa<llvm::CallInst>(prev)); |
| 1642 | assert(cast<llvm::CallInst>(prev)->getCalledValue() == |
| 1643 | CGF.CGM.getARCEntrypoints().retainAutoreleasedReturnValueMarker); |
| 1644 | insnsToKill.push_back(prev); |
| 1645 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1646 | } else { |
| 1647 | return 0; |
| 1648 | } |
| 1649 | |
| 1650 | result = call->getArgOperand(0); |
| 1651 | insnsToKill.push_back(call); |
| 1652 | |
| 1653 | // Keep killing bitcasts, for sanity. Note that we no longer care |
| 1654 | // about precise ordering as long as there's exactly one use. |
| 1655 | while (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(result)) { |
| 1656 | if (!bitcast->hasOneUse()) break; |
| 1657 | insnsToKill.push_back(bitcast); |
| 1658 | result = bitcast->getOperand(0); |
| 1659 | } |
| 1660 | |
| 1661 | // Delete all the unnecessary instructions, from latest to earliest. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1662 | for (SmallVectorImpl<llvm::Instruction*>::iterator |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1663 | i = insnsToKill.begin(), e = insnsToKill.end(); i != e; ++i) |
| 1664 | (*i)->eraseFromParent(); |
| 1665 | |
| 1666 | // Do the fused retain/autorelease if we were asked to. |
| 1667 | if (doRetainAutorelease) |
| 1668 | result = CGF.EmitARCRetainAutoreleaseReturnValue(result); |
| 1669 | |
| 1670 | // Cast back to the result type. |
| 1671 | return CGF.Builder.CreateBitCast(result, resultType); |
| 1672 | } |
| 1673 | |
John McCall | 77fe6cd | 2012-01-29 07:46:59 +0000 | [diff] [blame] | 1674 | /// If this is a +1 of the value of an immutable 'self', remove it. |
| 1675 | static llvm::Value *tryRemoveRetainOfSelf(CodeGenFunction &CGF, |
| 1676 | llvm::Value *result) { |
| 1677 | // This is only applicable to a method with an immutable 'self'. |
John McCall | bd9b65a | 2012-07-31 00:33:55 +0000 | [diff] [blame] | 1678 | const ObjCMethodDecl *method = |
| 1679 | dyn_cast_or_null<ObjCMethodDecl>(CGF.CurCodeDecl); |
John McCall | 77fe6cd | 2012-01-29 07:46:59 +0000 | [diff] [blame] | 1680 | if (!method) return 0; |
| 1681 | const VarDecl *self = method->getSelfDecl(); |
| 1682 | if (!self->getType().isConstQualified()) return 0; |
| 1683 | |
| 1684 | // Look for a retain call. |
| 1685 | llvm::CallInst *retainCall = |
| 1686 | dyn_cast<llvm::CallInst>(result->stripPointerCasts()); |
| 1687 | if (!retainCall || |
| 1688 | retainCall->getCalledValue() != CGF.CGM.getARCEntrypoints().objc_retain) |
| 1689 | return 0; |
| 1690 | |
| 1691 | // Look for an ordinary load of 'self'. |
| 1692 | llvm::Value *retainedValue = retainCall->getArgOperand(0); |
| 1693 | llvm::LoadInst *load = |
| 1694 | dyn_cast<llvm::LoadInst>(retainedValue->stripPointerCasts()); |
| 1695 | if (!load || load->isAtomic() || load->isVolatile() || |
| 1696 | load->getPointerOperand() != CGF.GetAddrOfLocalVar(self)) |
| 1697 | return 0; |
| 1698 | |
| 1699 | // Okay! Burn it all down. This relies for correctness on the |
| 1700 | // assumption that the retain is emitted as part of the return and |
| 1701 | // that thereafter everything is used "linearly". |
| 1702 | llvm::Type *resultType = result->getType(); |
| 1703 | eraseUnusedBitCasts(cast<llvm::Instruction>(result)); |
| 1704 | assert(retainCall->use_empty()); |
| 1705 | retainCall->eraseFromParent(); |
| 1706 | eraseUnusedBitCasts(cast<llvm::Instruction>(retainedValue)); |
| 1707 | |
| 1708 | return CGF.Builder.CreateBitCast(load, resultType); |
| 1709 | } |
| 1710 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1711 | /// Emit an ARC autorelease of the result of a function. |
John McCall | 77fe6cd | 2012-01-29 07:46:59 +0000 | [diff] [blame] | 1712 | /// |
| 1713 | /// \return the value to actually return from the function |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1714 | static llvm::Value *emitAutoreleaseOfResult(CodeGenFunction &CGF, |
| 1715 | llvm::Value *result) { |
John McCall | 77fe6cd | 2012-01-29 07:46:59 +0000 | [diff] [blame] | 1716 | // If we're returning 'self', kill the initial retain. This is a |
| 1717 | // heuristic attempt to "encourage correctness" in the really unfortunate |
| 1718 | // case where we have a return of self during a dealloc and we desperately |
| 1719 | // need to avoid the possible autorelease. |
| 1720 | if (llvm::Value *self = tryRemoveRetainOfSelf(CGF, result)) |
| 1721 | return self; |
| 1722 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1723 | // At -O0, try to emit a fused retain/autorelease. |
| 1724 | if (CGF.shouldUseFusedARCCalls()) |
| 1725 | if (llvm::Value *fused = tryEmitFusedAutoreleaseOfResult(CGF, result)) |
| 1726 | return fused; |
| 1727 | |
| 1728 | return CGF.EmitARCAutoreleaseReturnValue(result); |
| 1729 | } |
| 1730 | |
John McCall | f48f796 | 2012-01-29 02:35:02 +0000 | [diff] [blame] | 1731 | /// Heuristically search for a dominating store to the return-value slot. |
| 1732 | static llvm::StoreInst *findDominatingStoreToReturnValue(CodeGenFunction &CGF) { |
| 1733 | // If there are multiple uses of the return-value slot, just check |
| 1734 | // for something immediately preceding the IP. Sometimes this can |
| 1735 | // happen with how we generate implicit-returns; it can also happen |
| 1736 | // with noreturn cleanups. |
| 1737 | if (!CGF.ReturnValue->hasOneUse()) { |
| 1738 | llvm::BasicBlock *IP = CGF.Builder.GetInsertBlock(); |
| 1739 | if (IP->empty()) return 0; |
| 1740 | llvm::StoreInst *store = dyn_cast<llvm::StoreInst>(&IP->back()); |
| 1741 | if (!store) return 0; |
| 1742 | if (store->getPointerOperand() != CGF.ReturnValue) return 0; |
| 1743 | assert(!store->isAtomic() && !store->isVolatile()); // see below |
| 1744 | return store; |
| 1745 | } |
| 1746 | |
| 1747 | llvm::StoreInst *store = |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1748 | dyn_cast<llvm::StoreInst>(CGF.ReturnValue->user_back()); |
John McCall | f48f796 | 2012-01-29 02:35:02 +0000 | [diff] [blame] | 1749 | if (!store) return 0; |
| 1750 | |
| 1751 | // These aren't actually possible for non-coerced returns, and we |
| 1752 | // only care about non-coerced returns on this code path. |
| 1753 | assert(!store->isAtomic() && !store->isVolatile()); |
| 1754 | |
| 1755 | // Now do a first-and-dirty dominance check: just walk up the |
| 1756 | // single-predecessors chain from the current insertion point. |
| 1757 | llvm::BasicBlock *StoreBB = store->getParent(); |
| 1758 | llvm::BasicBlock *IP = CGF.Builder.GetInsertBlock(); |
| 1759 | while (IP != StoreBB) { |
| 1760 | if (!(IP = IP->getSinglePredecessor())) |
| 1761 | return 0; |
| 1762 | } |
| 1763 | |
| 1764 | // Okay, the store's basic block dominates the insertion point; we |
| 1765 | // can do our thing. |
| 1766 | return store; |
| 1767 | } |
| 1768 | |
Adrian Prantl | fa6b079 | 2013-05-02 17:30:20 +0000 | [diff] [blame] | 1769 | void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI, |
Nick Lewycky | 4ee7dc2 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 1770 | bool EmitRetDbgLoc, |
| 1771 | SourceLocation EndLoc) { |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1772 | // Functions with no result always return void. |
Chris Lattner | c6e6dd2 | 2010-06-26 23:13:19 +0000 | [diff] [blame] | 1773 | if (ReturnValue == 0) { |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1774 | Builder.CreateRetVoid(); |
Chris Lattner | c6e6dd2 | 2010-06-26 23:13:19 +0000 | [diff] [blame] | 1775 | return; |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1776 | } |
Daniel Dunbar | 21fcc8f | 2010-06-30 21:27:58 +0000 | [diff] [blame] | 1777 | |
Dan Gohman | 4751a53 | 2010-07-20 20:13:52 +0000 | [diff] [blame] | 1778 | llvm::DebugLoc RetDbgLoc; |
Chris Lattner | c6e6dd2 | 2010-06-26 23:13:19 +0000 | [diff] [blame] | 1779 | llvm::Value *RV = 0; |
| 1780 | QualType RetTy = FI.getReturnType(); |
| 1781 | const ABIArgInfo &RetAI = FI.getReturnInfo(); |
| 1782 | |
| 1783 | switch (RetAI.getKind()) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1784 | case ABIArgInfo::InAlloca: |
| 1785 | // Aggregrates get evaluated directly into the destination. Sometimes we |
| 1786 | // need to return the sret value in a register, though. |
| 1787 | assert(hasAggregateEvaluationKind(RetTy)); |
| 1788 | if (RetAI.getInAllocaSRet()) { |
| 1789 | llvm::Function::arg_iterator EI = CurFn->arg_end(); |
| 1790 | --EI; |
| 1791 | llvm::Value *ArgStruct = EI; |
| 1792 | llvm::Value *SRet = |
| 1793 | Builder.CreateStructGEP(ArgStruct, RetAI.getInAllocaFieldIndex()); |
| 1794 | RV = Builder.CreateLoad(SRet, "sret"); |
| 1795 | } |
| 1796 | break; |
| 1797 | |
Daniel Dunbar | 91a16fa | 2010-08-21 02:24:36 +0000 | [diff] [blame] | 1798 | case ABIArgInfo::Indirect: { |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 1799 | switch (getEvaluationKind(RetTy)) { |
| 1800 | case TEK_Complex: { |
| 1801 | ComplexPairTy RT = |
Nick Lewycky | 4ee7dc2 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 1802 | EmitLoadOfComplex(MakeNaturalAlignAddrLValue(ReturnValue, RetTy), |
| 1803 | EndLoc); |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 1804 | EmitStoreOfComplex(RT, |
| 1805 | MakeNaturalAlignAddrLValue(CurFn->arg_begin(), RetTy), |
| 1806 | /*isInit*/ true); |
| 1807 | break; |
| 1808 | } |
| 1809 | case TEK_Aggregate: |
Chris Lattner | c6e6dd2 | 2010-06-26 23:13:19 +0000 | [diff] [blame] | 1810 | // Do nothing; aggregrates get evaluated directly into the destination. |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 1811 | break; |
| 1812 | case TEK_Scalar: |
| 1813 | EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), |
| 1814 | MakeNaturalAlignAddrLValue(CurFn->arg_begin(), RetTy), |
| 1815 | /*isInit*/ true); |
| 1816 | break; |
Chris Lattner | c6e6dd2 | 2010-06-26 23:13:19 +0000 | [diff] [blame] | 1817 | } |
| 1818 | break; |
Daniel Dunbar | 91a16fa | 2010-08-21 02:24:36 +0000 | [diff] [blame] | 1819 | } |
Chris Lattner | c6e6dd2 | 2010-06-26 23:13:19 +0000 | [diff] [blame] | 1820 | |
| 1821 | case ABIArgInfo::Extend: |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1822 | case ABIArgInfo::Direct: |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 1823 | if (RetAI.getCoerceToType() == ConvertType(RetTy) && |
| 1824 | RetAI.getDirectOffset() == 0) { |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1825 | // The internal return value temp always will have pointer-to-return-type |
| 1826 | // type, just do a load. |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1827 | |
John McCall | f48f796 | 2012-01-29 02:35:02 +0000 | [diff] [blame] | 1828 | // If there is a dominating store to ReturnValue, we can elide |
| 1829 | // the load, zap the store, and usually zap the alloca. |
| 1830 | if (llvm::StoreInst *SI = findDominatingStoreToReturnValue(*this)) { |
Adrian Prantl | 7c731f5 | 2013-05-30 18:12:23 +0000 | [diff] [blame] | 1831 | // Reuse the debug location from the store unless there is |
| 1832 | // cleanup code to be emitted between the store and return |
| 1833 | // instruction. |
| 1834 | if (EmitRetDbgLoc && !AutoreleaseResult) |
Adrian Prantl | fa6b079 | 2013-05-02 17:30:20 +0000 | [diff] [blame] | 1835 | RetDbgLoc = SI->getDebugLoc(); |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1836 | // Get the stored value and nuke the now-dead store. |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1837 | RV = SI->getValueOperand(); |
| 1838 | SI->eraseFromParent(); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1839 | |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1840 | // If that was the only use of the return value, nuke it as well now. |
| 1841 | if (ReturnValue->use_empty() && isa<llvm::AllocaInst>(ReturnValue)) { |
| 1842 | cast<llvm::AllocaInst>(ReturnValue)->eraseFromParent(); |
| 1843 | ReturnValue = 0; |
| 1844 | } |
John McCall | f48f796 | 2012-01-29 02:35:02 +0000 | [diff] [blame] | 1845 | |
| 1846 | // Otherwise, we have to do a simple load. |
| 1847 | } else { |
| 1848 | RV = Builder.CreateLoad(ReturnValue); |
Chris Lattner | 35b21b8 | 2010-06-27 01:06:27 +0000 | [diff] [blame] | 1849 | } |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1850 | } else { |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 1851 | llvm::Value *V = ReturnValue; |
| 1852 | // If the value is offset in memory, apply the offset now. |
| 1853 | if (unsigned Offs = RetAI.getDirectOffset()) { |
| 1854 | V = Builder.CreateBitCast(V, Builder.getInt8PtrTy()); |
| 1855 | V = Builder.CreateConstGEP1_32(V, Offs); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1856 | V = Builder.CreateBitCast(V, |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 1857 | llvm::PointerType::getUnqual(RetAI.getCoerceToType())); |
| 1858 | } |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1859 | |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 1860 | RV = CreateCoercedLoad(V, RetAI.getCoerceToType(), *this); |
Chris Lattner | 35b21b8 | 2010-06-27 01:06:27 +0000 | [diff] [blame] | 1861 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1862 | |
| 1863 | // In ARC, end functions that return a retainable type with a call |
| 1864 | // to objc_autoreleaseReturnValue. |
| 1865 | if (AutoreleaseResult) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1866 | assert(getLangOpts().ObjCAutoRefCount && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1867 | !FI.isReturnsRetained() && |
| 1868 | RetTy->isObjCRetainableType()); |
| 1869 | RV = emitAutoreleaseOfResult(*this, RV); |
| 1870 | } |
| 1871 | |
Chris Lattner | c6e6dd2 | 2010-06-26 23:13:19 +0000 | [diff] [blame] | 1872 | break; |
Chris Lattner | c6e6dd2 | 2010-06-26 23:13:19 +0000 | [diff] [blame] | 1873 | |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1874 | case ABIArgInfo::Ignore: |
Chris Lattner | c6e6dd2 | 2010-06-26 23:13:19 +0000 | [diff] [blame] | 1875 | break; |
| 1876 | |
| 1877 | case ABIArgInfo::Expand: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1878 | llvm_unreachable("Invalid ABI kind for return argument"); |
Chris Lattner | c6e6dd2 | 2010-06-26 23:13:19 +0000 | [diff] [blame] | 1879 | } |
| 1880 | |
Daniel Dunbar | 21fcc8f | 2010-06-30 21:27:58 +0000 | [diff] [blame] | 1881 | llvm::Instruction *Ret = RV ? Builder.CreateRet(RV) : Builder.CreateRetVoid(); |
Devang Patel | d3f265d | 2010-07-21 18:08:50 +0000 | [diff] [blame] | 1882 | if (!RetDbgLoc.isUnknown()) |
| 1883 | Ret->setDebugLoc(RetDbgLoc); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1884 | } |
| 1885 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1886 | static bool isInAllocaArgument(CGCXXABI &ABI, QualType type) { |
| 1887 | const CXXRecordDecl *RD = type->getAsCXXRecordDecl(); |
| 1888 | return RD && ABI.getRecordArgABI(RD) == CGCXXABI::RAA_DirectInMemory; |
| 1889 | } |
| 1890 | |
| 1891 | static AggValueSlot createPlaceholderSlot(CodeGenFunction &CGF, QualType Ty) { |
| 1892 | // FIXME: Generate IR in one pass, rather than going back and fixing up these |
| 1893 | // placeholders. |
| 1894 | llvm::Type *IRTy = CGF.ConvertTypeForMem(Ty); |
| 1895 | llvm::Value *Placeholder = |
| 1896 | llvm::UndefValue::get(IRTy->getPointerTo()->getPointerTo()); |
| 1897 | Placeholder = CGF.Builder.CreateLoad(Placeholder); |
| 1898 | return AggValueSlot::forAddr(Placeholder, CharUnits::Zero(), |
| 1899 | Ty.getQualifiers(), |
| 1900 | AggValueSlot::IsNotDestructed, |
| 1901 | AggValueSlot::DoesNotNeedGCBarriers, |
| 1902 | AggValueSlot::IsNotAliased); |
| 1903 | } |
| 1904 | |
John McCall | 413ebdb | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 1905 | void CodeGenFunction::EmitDelegateCallArg(CallArgList &args, |
Nick Lewycky | 4ee7dc2 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 1906 | const VarDecl *param, |
| 1907 | SourceLocation loc) { |
John McCall | 2736071 | 2010-05-26 22:34:26 +0000 | [diff] [blame] | 1908 | // StartFunction converted the ABI-lowered parameter(s) into a |
| 1909 | // local alloca. We need to turn that into an r-value suitable |
| 1910 | // for EmitCall. |
John McCall | 413ebdb | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 1911 | llvm::Value *local = GetAddrOfLocalVar(param); |
John McCall | 2736071 | 2010-05-26 22:34:26 +0000 | [diff] [blame] | 1912 | |
John McCall | 413ebdb | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 1913 | QualType type = param->getType(); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1914 | |
John McCall | 2736071 | 2010-05-26 22:34:26 +0000 | [diff] [blame] | 1915 | // For the most part, we just need to load the alloca, except: |
| 1916 | // 1) aggregate r-values are actually pointers to temporaries, and |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 1917 | // 2) references to non-scalars are pointers directly to the aggregate. |
| 1918 | // I don't know why references to scalars are different here. |
John McCall | 413ebdb | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 1919 | if (const ReferenceType *ref = type->getAs<ReferenceType>()) { |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 1920 | if (!hasScalarEvaluationKind(ref->getPointeeType())) |
John McCall | 413ebdb | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 1921 | return args.add(RValue::getAggregate(local), type); |
John McCall | 2736071 | 2010-05-26 22:34:26 +0000 | [diff] [blame] | 1922 | |
| 1923 | // Locals which are references to scalars are represented |
| 1924 | // with allocas holding the pointer. |
John McCall | 413ebdb | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 1925 | return args.add(RValue::get(Builder.CreateLoad(local)), type); |
John McCall | 2736071 | 2010-05-26 22:34:26 +0000 | [diff] [blame] | 1926 | } |
| 1927 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1928 | if (isInAllocaArgument(CGM.getCXXABI(), type)) { |
| 1929 | AggValueSlot Slot = createPlaceholderSlot(*this, type); |
| 1930 | Slot.setExternallyDestructed(); |
| 1931 | |
| 1932 | // FIXME: Either emit a copy constructor call, or figure out how to do |
| 1933 | // guaranteed tail calls with perfect forwarding in LLVM. |
| 1934 | CGM.ErrorUnsupported(param, "non-trivial argument copy for thunk"); |
| 1935 | EmitNullInitialization(Slot.getAddr(), type); |
| 1936 | |
| 1937 | RValue RV = Slot.asRValue(); |
| 1938 | args.add(RV, type); |
| 1939 | return; |
| 1940 | } |
| 1941 | |
Nick Lewycky | 4ee7dc2 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 1942 | args.add(convertTempToRValue(local, type, loc), type); |
John McCall | 2736071 | 2010-05-26 22:34:26 +0000 | [diff] [blame] | 1943 | } |
| 1944 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1945 | static bool isProvablyNull(llvm::Value *addr) { |
| 1946 | return isa<llvm::ConstantPointerNull>(addr); |
| 1947 | } |
| 1948 | |
| 1949 | static bool isProvablyNonNull(llvm::Value *addr) { |
| 1950 | return isa<llvm::AllocaInst>(addr); |
| 1951 | } |
| 1952 | |
| 1953 | /// Emit the actual writing-back of a writeback. |
| 1954 | static void emitWriteback(CodeGenFunction &CGF, |
| 1955 | const CallArgList::Writeback &writeback) { |
John McCall | b6a6079 | 2013-03-23 02:35:54 +0000 | [diff] [blame] | 1956 | const LValue &srcLV = writeback.Source; |
| 1957 | llvm::Value *srcAddr = srcLV.getAddress(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1958 | assert(!isProvablyNull(srcAddr) && |
| 1959 | "shouldn't have writeback for provably null argument"); |
| 1960 | |
| 1961 | llvm::BasicBlock *contBB = 0; |
| 1962 | |
| 1963 | // If the argument wasn't provably non-null, we need to null check |
| 1964 | // before doing the store. |
| 1965 | bool provablyNonNull = isProvablyNonNull(srcAddr); |
| 1966 | if (!provablyNonNull) { |
| 1967 | llvm::BasicBlock *writebackBB = CGF.createBasicBlock("icr.writeback"); |
| 1968 | contBB = CGF.createBasicBlock("icr.done"); |
| 1969 | |
| 1970 | llvm::Value *isNull = CGF.Builder.CreateIsNull(srcAddr, "icr.isnull"); |
| 1971 | CGF.Builder.CreateCondBr(isNull, contBB, writebackBB); |
| 1972 | CGF.EmitBlock(writebackBB); |
| 1973 | } |
| 1974 | |
| 1975 | // Load the value to writeback. |
| 1976 | llvm::Value *value = CGF.Builder.CreateLoad(writeback.Temporary); |
| 1977 | |
| 1978 | // Cast it back, in case we're writing an id to a Foo* or something. |
| 1979 | value = CGF.Builder.CreateBitCast(value, |
| 1980 | cast<llvm::PointerType>(srcAddr->getType())->getElementType(), |
| 1981 | "icr.writeback-cast"); |
| 1982 | |
| 1983 | // Perform the writeback. |
John McCall | b6a6079 | 2013-03-23 02:35:54 +0000 | [diff] [blame] | 1984 | |
| 1985 | // If we have a "to use" value, it's something we need to emit a use |
| 1986 | // of. This has to be carefully threaded in: if it's done after the |
| 1987 | // release it's potentially undefined behavior (and the optimizer |
| 1988 | // will ignore it), and if it happens before the retain then the |
| 1989 | // optimizer could move the release there. |
| 1990 | if (writeback.ToUse) { |
| 1991 | assert(srcLV.getObjCLifetime() == Qualifiers::OCL_Strong); |
| 1992 | |
| 1993 | // Retain the new value. No need to block-copy here: the block's |
| 1994 | // being passed up the stack. |
| 1995 | value = CGF.EmitARCRetainNonBlock(value); |
| 1996 | |
| 1997 | // Emit the intrinsic use here. |
| 1998 | CGF.EmitARCIntrinsicUse(writeback.ToUse); |
| 1999 | |
| 2000 | // Load the old value (primitively). |
Nick Lewycky | 4ee7dc2 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 2001 | llvm::Value *oldValue = CGF.EmitLoadOfScalar(srcLV, SourceLocation()); |
John McCall | b6a6079 | 2013-03-23 02:35:54 +0000 | [diff] [blame] | 2002 | |
| 2003 | // Put the new value in place (primitively). |
| 2004 | CGF.EmitStoreOfScalar(value, srcLV, /*init*/ false); |
| 2005 | |
| 2006 | // Release the old value. |
| 2007 | CGF.EmitARCRelease(oldValue, srcLV.isARCPreciseLifetime()); |
| 2008 | |
| 2009 | // Otherwise, we can just do a normal lvalue store. |
| 2010 | } else { |
| 2011 | CGF.EmitStoreThroughLValue(RValue::get(value), srcLV); |
| 2012 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2013 | |
| 2014 | // Jump to the continuation block. |
| 2015 | if (!provablyNonNull) |
| 2016 | CGF.EmitBlock(contBB); |
| 2017 | } |
| 2018 | |
| 2019 | static void emitWritebacks(CodeGenFunction &CGF, |
| 2020 | const CallArgList &args) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2021 | for (const auto &I : args.writebacks()) |
| 2022 | emitWriteback(CGF, I); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2023 | } |
| 2024 | |
Reid Kleckner | 9b60195 | 2013-06-21 12:45:15 +0000 | [diff] [blame] | 2025 | static void deactivateArgCleanupsBeforeCall(CodeGenFunction &CGF, |
| 2026 | const CallArgList &CallArgs) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2027 | assert(CGF.getTarget().getCXXABI().areArgsDestroyedLeftToRightInCallee()); |
Reid Kleckner | 9b60195 | 2013-06-21 12:45:15 +0000 | [diff] [blame] | 2028 | ArrayRef<CallArgList::CallArgCleanup> Cleanups = |
| 2029 | CallArgs.getCleanupsToDeactivate(); |
| 2030 | // Iterate in reverse to increase the likelihood of popping the cleanup. |
| 2031 | for (ArrayRef<CallArgList::CallArgCleanup>::reverse_iterator |
| 2032 | I = Cleanups.rbegin(), E = Cleanups.rend(); I != E; ++I) { |
| 2033 | CGF.DeactivateCleanupBlock(I->Cleanup, I->IsActiveIP); |
| 2034 | I->IsActiveIP->eraseFromParent(); |
| 2035 | } |
| 2036 | } |
| 2037 | |
John McCall | b6a6079 | 2013-03-23 02:35:54 +0000 | [diff] [blame] | 2038 | static const Expr *maybeGetUnaryAddrOfOperand(const Expr *E) { |
| 2039 | if (const UnaryOperator *uop = dyn_cast<UnaryOperator>(E->IgnoreParens())) |
| 2040 | if (uop->getOpcode() == UO_AddrOf) |
| 2041 | return uop->getSubExpr(); |
| 2042 | return 0; |
| 2043 | } |
| 2044 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2045 | /// Emit an argument that's being passed call-by-writeback. That is, |
| 2046 | /// we are passing the address of |
| 2047 | static void emitWritebackArg(CodeGenFunction &CGF, CallArgList &args, |
| 2048 | const ObjCIndirectCopyRestoreExpr *CRE) { |
John McCall | b6a6079 | 2013-03-23 02:35:54 +0000 | [diff] [blame] | 2049 | LValue srcLV; |
| 2050 | |
| 2051 | // Make an optimistic effort to emit the address as an l-value. |
| 2052 | // This can fail if the the argument expression is more complicated. |
| 2053 | if (const Expr *lvExpr = maybeGetUnaryAddrOfOperand(CRE->getSubExpr())) { |
| 2054 | srcLV = CGF.EmitLValue(lvExpr); |
| 2055 | |
| 2056 | // Otherwise, just emit it as a scalar. |
| 2057 | } else { |
| 2058 | llvm::Value *srcAddr = CGF.EmitScalarExpr(CRE->getSubExpr()); |
| 2059 | |
| 2060 | QualType srcAddrType = |
| 2061 | CRE->getSubExpr()->getType()->castAs<PointerType>()->getPointeeType(); |
| 2062 | srcLV = CGF.MakeNaturalAlignAddrLValue(srcAddr, srcAddrType); |
| 2063 | } |
| 2064 | llvm::Value *srcAddr = srcLV.getAddress(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2065 | |
| 2066 | // The dest and src types don't necessarily match in LLVM terms |
| 2067 | // because of the crazy ObjC compatibility rules. |
| 2068 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2069 | llvm::PointerType *destType = |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2070 | cast<llvm::PointerType>(CGF.ConvertType(CRE->getType())); |
| 2071 | |
| 2072 | // If the address is a constant null, just pass the appropriate null. |
| 2073 | if (isProvablyNull(srcAddr)) { |
| 2074 | args.add(RValue::get(llvm::ConstantPointerNull::get(destType)), |
| 2075 | CRE->getType()); |
| 2076 | return; |
| 2077 | } |
| 2078 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2079 | // Create the temporary. |
| 2080 | llvm::Value *temp = CGF.CreateTempAlloca(destType->getElementType(), |
| 2081 | "icr.temp"); |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 2082 | // Loading an l-value can introduce a cleanup if the l-value is __weak, |
| 2083 | // and that cleanup will be conditional if we can't prove that the l-value |
| 2084 | // isn't null, so we need to register a dominating point so that the cleanups |
| 2085 | // system will make valid IR. |
| 2086 | CodeGenFunction::ConditionalEvaluation condEval(CGF); |
| 2087 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2088 | // Zero-initialize it if we're not doing a copy-initialization. |
| 2089 | bool shouldCopy = CRE->shouldCopy(); |
| 2090 | if (!shouldCopy) { |
| 2091 | llvm::Value *null = |
| 2092 | llvm::ConstantPointerNull::get( |
| 2093 | cast<llvm::PointerType>(destType->getElementType())); |
| 2094 | CGF.Builder.CreateStore(null, temp); |
| 2095 | } |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 2096 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2097 | llvm::BasicBlock *contBB = 0; |
John McCall | b6a6079 | 2013-03-23 02:35:54 +0000 | [diff] [blame] | 2098 | llvm::BasicBlock *originBB = 0; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2099 | |
| 2100 | // If the address is *not* known to be non-null, we need to switch. |
| 2101 | llvm::Value *finalArgument; |
| 2102 | |
| 2103 | bool provablyNonNull = isProvablyNonNull(srcAddr); |
| 2104 | if (provablyNonNull) { |
| 2105 | finalArgument = temp; |
| 2106 | } else { |
| 2107 | llvm::Value *isNull = CGF.Builder.CreateIsNull(srcAddr, "icr.isnull"); |
| 2108 | |
| 2109 | finalArgument = CGF.Builder.CreateSelect(isNull, |
| 2110 | llvm::ConstantPointerNull::get(destType), |
| 2111 | temp, "icr.argument"); |
| 2112 | |
| 2113 | // If we need to copy, then the load has to be conditional, which |
| 2114 | // means we need control flow. |
| 2115 | if (shouldCopy) { |
John McCall | b6a6079 | 2013-03-23 02:35:54 +0000 | [diff] [blame] | 2116 | originBB = CGF.Builder.GetInsertBlock(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2117 | contBB = CGF.createBasicBlock("icr.cont"); |
| 2118 | llvm::BasicBlock *copyBB = CGF.createBasicBlock("icr.copy"); |
| 2119 | CGF.Builder.CreateCondBr(isNull, contBB, copyBB); |
| 2120 | CGF.EmitBlock(copyBB); |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 2121 | condEval.begin(CGF); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2122 | } |
| 2123 | } |
| 2124 | |
John McCall | b6a6079 | 2013-03-23 02:35:54 +0000 | [diff] [blame] | 2125 | llvm::Value *valueToUse = 0; |
| 2126 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2127 | // Perform a copy if necessary. |
| 2128 | if (shouldCopy) { |
Nick Lewycky | 4ee7dc2 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 2129 | RValue srcRV = CGF.EmitLoadOfLValue(srcLV, SourceLocation()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2130 | assert(srcRV.isScalar()); |
| 2131 | |
| 2132 | llvm::Value *src = srcRV.getScalarVal(); |
| 2133 | src = CGF.Builder.CreateBitCast(src, destType->getElementType(), |
| 2134 | "icr.cast"); |
| 2135 | |
| 2136 | // Use an ordinary store, not a store-to-lvalue. |
| 2137 | CGF.Builder.CreateStore(src, temp); |
John McCall | b6a6079 | 2013-03-23 02:35:54 +0000 | [diff] [blame] | 2138 | |
| 2139 | // If optimization is enabled, and the value was held in a |
| 2140 | // __strong variable, we need to tell the optimizer that this |
| 2141 | // value has to stay alive until we're doing the store back. |
| 2142 | // This is because the temporary is effectively unretained, |
| 2143 | // and so otherwise we can violate the high-level semantics. |
| 2144 | if (CGF.CGM.getCodeGenOpts().OptimizationLevel != 0 && |
| 2145 | srcLV.getObjCLifetime() == Qualifiers::OCL_Strong) { |
| 2146 | valueToUse = src; |
| 2147 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2148 | } |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 2149 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2150 | // Finish the control flow if we needed it. |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 2151 | if (shouldCopy && !provablyNonNull) { |
John McCall | b6a6079 | 2013-03-23 02:35:54 +0000 | [diff] [blame] | 2152 | llvm::BasicBlock *copyBB = CGF.Builder.GetInsertBlock(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2153 | CGF.EmitBlock(contBB); |
John McCall | b6a6079 | 2013-03-23 02:35:54 +0000 | [diff] [blame] | 2154 | |
| 2155 | // Make a phi for the value to intrinsically use. |
| 2156 | if (valueToUse) { |
| 2157 | llvm::PHINode *phiToUse = CGF.Builder.CreatePHI(valueToUse->getType(), 2, |
| 2158 | "icr.to-use"); |
| 2159 | phiToUse->addIncoming(valueToUse, copyBB); |
| 2160 | phiToUse->addIncoming(llvm::UndefValue::get(valueToUse->getType()), |
| 2161 | originBB); |
| 2162 | valueToUse = phiToUse; |
| 2163 | } |
| 2164 | |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 2165 | condEval.end(CGF); |
| 2166 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2167 | |
John McCall | b6a6079 | 2013-03-23 02:35:54 +0000 | [diff] [blame] | 2168 | args.addWriteback(srcLV, temp, valueToUse); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2169 | args.add(RValue::get(finalArgument), CRE->getType()); |
| 2170 | } |
| 2171 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2172 | void CallArgList::allocateArgumentMemory(CodeGenFunction &CGF) { |
| 2173 | assert(!StackBase && !StackCleanup.isValid()); |
| 2174 | |
| 2175 | // Save the stack. |
| 2176 | llvm::Function *F = CGF.CGM.getIntrinsic(llvm::Intrinsic::stacksave); |
| 2177 | StackBase = CGF.Builder.CreateCall(F, "inalloca.save"); |
| 2178 | |
| 2179 | // Control gets really tied up in landing pads, so we have to spill the |
| 2180 | // stacksave to an alloca to avoid violating SSA form. |
| 2181 | // TODO: This is dead if we never emit the cleanup. We should create the |
| 2182 | // alloca and store lazily on the first cleanup emission. |
| 2183 | StackBaseMem = CGF.CreateTempAlloca(CGF.Int8PtrTy, "inalloca.spmem"); |
| 2184 | CGF.Builder.CreateStore(StackBase, StackBaseMem); |
| 2185 | CGF.pushStackRestore(EHCleanup, StackBaseMem); |
| 2186 | StackCleanup = CGF.EHStack.getInnermostEHScope(); |
| 2187 | assert(StackCleanup.isValid()); |
| 2188 | } |
| 2189 | |
| 2190 | void CallArgList::freeArgumentMemory(CodeGenFunction &CGF) const { |
| 2191 | if (StackBase) { |
| 2192 | CGF.DeactivateCleanupBlock(StackCleanup, StackBase); |
| 2193 | llvm::Value *F = CGF.CGM.getIntrinsic(llvm::Intrinsic::stackrestore); |
| 2194 | // We could load StackBase from StackBaseMem, but in the non-exceptional |
| 2195 | // case we can skip it. |
| 2196 | CGF.Builder.CreateCall(F, StackBase); |
| 2197 | } |
| 2198 | } |
| 2199 | |
| 2200 | void CodeGenFunction::EmitCallArgs(CallArgList &Args, |
| 2201 | ArrayRef<QualType> ArgTypes, |
| 2202 | CallExpr::const_arg_iterator ArgBeg, |
| 2203 | CallExpr::const_arg_iterator ArgEnd, |
| 2204 | bool ForceColumnInfo) { |
| 2205 | CGDebugInfo *DI = getDebugInfo(); |
| 2206 | SourceLocation CallLoc; |
| 2207 | if (DI) CallLoc = DI->getLocation(); |
| 2208 | |
| 2209 | // We *have* to evaluate arguments from right to left in the MS C++ ABI, |
| 2210 | // because arguments are destroyed left to right in the callee. |
| 2211 | if (CGM.getTarget().getCXXABI().areArgsDestroyedLeftToRightInCallee()) { |
| 2212 | // Insert a stack save if we're going to need any inalloca args. |
| 2213 | bool HasInAllocaArgs = false; |
| 2214 | for (ArrayRef<QualType>::iterator I = ArgTypes.begin(), E = ArgTypes.end(); |
| 2215 | I != E && !HasInAllocaArgs; ++I) |
| 2216 | HasInAllocaArgs = isInAllocaArgument(CGM.getCXXABI(), *I); |
| 2217 | if (HasInAllocaArgs) { |
| 2218 | assert(getTarget().getTriple().getArch() == llvm::Triple::x86); |
| 2219 | Args.allocateArgumentMemory(*this); |
| 2220 | } |
| 2221 | |
| 2222 | // Evaluate each argument. |
| 2223 | size_t CallArgsStart = Args.size(); |
| 2224 | for (int I = ArgTypes.size() - 1; I >= 0; --I) { |
| 2225 | CallExpr::const_arg_iterator Arg = ArgBeg + I; |
| 2226 | EmitCallArg(Args, *Arg, ArgTypes[I]); |
| 2227 | // Restore the debug location. |
| 2228 | if (DI) DI->EmitLocation(Builder, CallLoc, ForceColumnInfo); |
| 2229 | } |
| 2230 | |
| 2231 | // Un-reverse the arguments we just evaluated so they match up with the LLVM |
| 2232 | // IR function. |
| 2233 | std::reverse(Args.begin() + CallArgsStart, Args.end()); |
| 2234 | return; |
| 2235 | } |
| 2236 | |
| 2237 | for (unsigned I = 0, E = ArgTypes.size(); I != E; ++I) { |
| 2238 | CallExpr::const_arg_iterator Arg = ArgBeg + I; |
| 2239 | assert(Arg != ArgEnd); |
| 2240 | EmitCallArg(Args, *Arg, ArgTypes[I]); |
| 2241 | // Restore the debug location. |
| 2242 | if (DI) DI->EmitLocation(Builder, CallLoc, ForceColumnInfo); |
| 2243 | } |
| 2244 | } |
| 2245 | |
| 2246 | namespace { |
| 2247 | |
| 2248 | struct DestroyUnpassedArg : EHScopeStack::Cleanup { |
| 2249 | DestroyUnpassedArg(llvm::Value *Addr, QualType Ty) |
| 2250 | : Addr(Addr), Ty(Ty) {} |
| 2251 | |
| 2252 | llvm::Value *Addr; |
| 2253 | QualType Ty; |
| 2254 | |
| 2255 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
| 2256 | const CXXDestructorDecl *Dtor = Ty->getAsCXXRecordDecl()->getDestructor(); |
| 2257 | assert(!Dtor->isTrivial()); |
| 2258 | CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete, /*for vbase*/ false, |
| 2259 | /*Delegating=*/false, Addr); |
| 2260 | } |
| 2261 | }; |
| 2262 | |
| 2263 | } |
| 2264 | |
John McCall | 413ebdb | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 2265 | void CodeGenFunction::EmitCallArg(CallArgList &args, const Expr *E, |
| 2266 | QualType type) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2267 | if (const ObjCIndirectCopyRestoreExpr *CRE |
| 2268 | = dyn_cast<ObjCIndirectCopyRestoreExpr>(E)) { |
Richard Smith | 7edf9e3 | 2012-11-01 22:30:59 +0000 | [diff] [blame] | 2269 | assert(getLangOpts().ObjCAutoRefCount); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2270 | assert(getContext().hasSameType(E->getType(), type)); |
| 2271 | return emitWritebackArg(*this, args, CRE); |
| 2272 | } |
| 2273 | |
John McCall | 8affed5 | 2011-08-26 18:42:59 +0000 | [diff] [blame] | 2274 | assert(type->isReferenceType() == E->isGLValue() && |
| 2275 | "reference binding to unmaterialized r-value!"); |
| 2276 | |
John McCall | cec52f0 | 2011-08-26 21:08:13 +0000 | [diff] [blame] | 2277 | if (E->isGLValue()) { |
| 2278 | assert(E->getObjectKind() == OK_Ordinary); |
Richard Smith | d4ec562 | 2013-06-12 23:38:09 +0000 | [diff] [blame] | 2279 | return args.add(EmitReferenceBindingToExpr(E), type); |
John McCall | cec52f0 | 2011-08-26 21:08:13 +0000 | [diff] [blame] | 2280 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2281 | |
Reid Kleckner | 9b60195 | 2013-06-21 12:45:15 +0000 | [diff] [blame] | 2282 | bool HasAggregateEvalKind = hasAggregateEvaluationKind(type); |
| 2283 | |
| 2284 | // In the Microsoft C++ ABI, aggregate arguments are destructed by the callee. |
| 2285 | // However, we still have to push an EH-only cleanup in case we unwind before |
| 2286 | // we make it to the call. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2287 | if (HasAggregateEvalKind && args.isUsingInAlloca()) { |
| 2288 | assert(getTarget().getTriple().getArch() == llvm::Triple::x86); |
| 2289 | AggValueSlot Slot = createPlaceholderSlot(*this, type); |
| 2290 | Slot.setExternallyDestructed(); |
| 2291 | EmitAggExpr(E, Slot); |
| 2292 | RValue RV = Slot.asRValue(); |
| 2293 | args.add(RV, type); |
Reid Kleckner | 9b60195 | 2013-06-21 12:45:15 +0000 | [diff] [blame] | 2294 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2295 | const CXXRecordDecl *RD = type->getAsCXXRecordDecl(); |
| 2296 | if (RD->hasNonTrivialDestructor()) { |
| 2297 | // Create a no-op GEP between the placeholder and the cleanup so we can |
| 2298 | // RAUW it successfully. It also serves as a marker of the first |
| 2299 | // instruction where the cleanup is active. |
| 2300 | pushFullExprCleanup<DestroyUnpassedArg>(EHCleanup, Slot.getAddr(), type); |
Reid Kleckner | 9b60195 | 2013-06-21 12:45:15 +0000 | [diff] [blame] | 2301 | // This unreachable is a temporary marker which will be removed later. |
| 2302 | llvm::Instruction *IsActive = Builder.CreateUnreachable(); |
| 2303 | args.addArgCleanupDeactivation(EHStack.getInnermostEHScope(), IsActive); |
Reid Kleckner | 9b60195 | 2013-06-21 12:45:15 +0000 | [diff] [blame] | 2304 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2305 | return; |
Reid Kleckner | 9b60195 | 2013-06-21 12:45:15 +0000 | [diff] [blame] | 2306 | } |
| 2307 | |
| 2308 | if (HasAggregateEvalKind && isa<ImplicitCastExpr>(E) && |
Eli Friedman | 55d4848 | 2011-05-26 00:10:27 +0000 | [diff] [blame] | 2309 | cast<CastExpr>(E)->getCastKind() == CK_LValueToRValue) { |
| 2310 | LValue L = EmitLValue(cast<CastExpr>(E)->getSubExpr()); |
| 2311 | assert(L.isSimple()); |
Eli Friedman | d39083d | 2013-06-11 01:08:22 +0000 | [diff] [blame] | 2312 | if (L.getAlignment() >= getContext().getTypeAlignInChars(type)) { |
| 2313 | args.add(L.asAggregateRValue(), type, /*NeedsCopy*/true); |
| 2314 | } else { |
| 2315 | // We can't represent a misaligned lvalue in the CallArgList, so copy |
| 2316 | // to an aligned temporary now. |
| 2317 | llvm::Value *tmp = CreateMemTemp(type); |
| 2318 | EmitAggregateCopy(tmp, L.getAddress(), type, L.isVolatile(), |
| 2319 | L.getAlignment()); |
| 2320 | args.add(RValue::getAggregate(tmp), type); |
| 2321 | } |
Eli Friedman | 55d4848 | 2011-05-26 00:10:27 +0000 | [diff] [blame] | 2322 | return; |
| 2323 | } |
| 2324 | |
John McCall | 413ebdb | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 2325 | args.add(EmitAnyExprToTemp(E), type); |
Anders Carlsson | 0139bb9 | 2009-04-08 20:47:54 +0000 | [diff] [blame] | 2326 | } |
| 2327 | |
Dan Gohman | b49bd27 | 2012-02-16 00:57:37 +0000 | [diff] [blame] | 2328 | // In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC |
| 2329 | // optimizer it can aggressively ignore unwind edges. |
| 2330 | void |
| 2331 | CodeGenFunction::AddObjCARCExceptionMetadata(llvm::Instruction *Inst) { |
| 2332 | if (CGM.getCodeGenOpts().OptimizationLevel != 0 && |
| 2333 | !CGM.getCodeGenOpts().ObjCAutoRefCountExceptions) |
| 2334 | Inst->setMetadata("clang.arc.no_objc_arc_exceptions", |
| 2335 | CGM.getNoObjCARCExceptionsMetadata()); |
| 2336 | } |
| 2337 | |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 2338 | /// Emits a call to the given no-arguments nounwind runtime function. |
| 2339 | llvm::CallInst * |
| 2340 | CodeGenFunction::EmitNounwindRuntimeCall(llvm::Value *callee, |
| 2341 | const llvm::Twine &name) { |
| 2342 | return EmitNounwindRuntimeCall(callee, ArrayRef<llvm::Value*>(), name); |
| 2343 | } |
| 2344 | |
| 2345 | /// Emits a call to the given nounwind runtime function. |
| 2346 | llvm::CallInst * |
| 2347 | CodeGenFunction::EmitNounwindRuntimeCall(llvm::Value *callee, |
| 2348 | ArrayRef<llvm::Value*> args, |
| 2349 | const llvm::Twine &name) { |
| 2350 | llvm::CallInst *call = EmitRuntimeCall(callee, args, name); |
| 2351 | call->setDoesNotThrow(); |
| 2352 | return call; |
| 2353 | } |
| 2354 | |
| 2355 | /// Emits a simple call (never an invoke) to the given no-arguments |
| 2356 | /// runtime function. |
| 2357 | llvm::CallInst * |
| 2358 | CodeGenFunction::EmitRuntimeCall(llvm::Value *callee, |
| 2359 | const llvm::Twine &name) { |
| 2360 | return EmitRuntimeCall(callee, ArrayRef<llvm::Value*>(), name); |
| 2361 | } |
| 2362 | |
| 2363 | /// Emits a simple call (never an invoke) to the given runtime |
| 2364 | /// function. |
| 2365 | llvm::CallInst * |
| 2366 | CodeGenFunction::EmitRuntimeCall(llvm::Value *callee, |
| 2367 | ArrayRef<llvm::Value*> args, |
| 2368 | const llvm::Twine &name) { |
| 2369 | llvm::CallInst *call = Builder.CreateCall(callee, args, name); |
| 2370 | call->setCallingConv(getRuntimeCC()); |
| 2371 | return call; |
| 2372 | } |
| 2373 | |
| 2374 | /// Emits a call or invoke to the given noreturn runtime function. |
| 2375 | void CodeGenFunction::EmitNoreturnRuntimeCallOrInvoke(llvm::Value *callee, |
| 2376 | ArrayRef<llvm::Value*> args) { |
| 2377 | if (getInvokeDest()) { |
| 2378 | llvm::InvokeInst *invoke = |
| 2379 | Builder.CreateInvoke(callee, |
| 2380 | getUnreachableBlock(), |
| 2381 | getInvokeDest(), |
| 2382 | args); |
| 2383 | invoke->setDoesNotReturn(); |
| 2384 | invoke->setCallingConv(getRuntimeCC()); |
| 2385 | } else { |
| 2386 | llvm::CallInst *call = Builder.CreateCall(callee, args); |
| 2387 | call->setDoesNotReturn(); |
| 2388 | call->setCallingConv(getRuntimeCC()); |
| 2389 | Builder.CreateUnreachable(); |
| 2390 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2391 | PGO.setCurrentRegionUnreachable(); |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 2392 | } |
| 2393 | |
| 2394 | /// Emits a call or invoke instruction to the given nullary runtime |
| 2395 | /// function. |
| 2396 | llvm::CallSite |
| 2397 | CodeGenFunction::EmitRuntimeCallOrInvoke(llvm::Value *callee, |
| 2398 | const Twine &name) { |
| 2399 | return EmitRuntimeCallOrInvoke(callee, ArrayRef<llvm::Value*>(), name); |
| 2400 | } |
| 2401 | |
| 2402 | /// Emits a call or invoke instruction to the given runtime function. |
| 2403 | llvm::CallSite |
| 2404 | CodeGenFunction::EmitRuntimeCallOrInvoke(llvm::Value *callee, |
| 2405 | ArrayRef<llvm::Value*> args, |
| 2406 | const Twine &name) { |
| 2407 | llvm::CallSite callSite = EmitCallOrInvoke(callee, args, name); |
| 2408 | callSite.setCallingConv(getRuntimeCC()); |
| 2409 | return callSite; |
| 2410 | } |
| 2411 | |
| 2412 | llvm::CallSite |
| 2413 | CodeGenFunction::EmitCallOrInvoke(llvm::Value *Callee, |
| 2414 | const Twine &Name) { |
| 2415 | return EmitCallOrInvoke(Callee, ArrayRef<llvm::Value *>(), Name); |
| 2416 | } |
| 2417 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2418 | /// Emits a call or invoke instruction to the given function, depending |
| 2419 | /// on the current state of the EH stack. |
| 2420 | llvm::CallSite |
| 2421 | CodeGenFunction::EmitCallOrInvoke(llvm::Value *Callee, |
Chris Lattner | 2d3ba4f | 2011-07-23 17:14:25 +0000 | [diff] [blame] | 2422 | ArrayRef<llvm::Value *> Args, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2423 | const Twine &Name) { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2424 | llvm::BasicBlock *InvokeDest = getInvokeDest(); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2425 | |
Dan Gohman | b49bd27 | 2012-02-16 00:57:37 +0000 | [diff] [blame] | 2426 | llvm::Instruction *Inst; |
| 2427 | if (!InvokeDest) |
| 2428 | Inst = Builder.CreateCall(Callee, Args, Name); |
| 2429 | else { |
| 2430 | llvm::BasicBlock *ContBB = createBasicBlock("invoke.cont"); |
| 2431 | Inst = Builder.CreateInvoke(Callee, ContBB, InvokeDest, Args, Name); |
| 2432 | EmitBlock(ContBB); |
| 2433 | } |
| 2434 | |
| 2435 | // In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC |
| 2436 | // optimizer it can aggressively ignore unwind edges. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2437 | if (CGM.getLangOpts().ObjCAutoRefCount) |
Dan Gohman | b49bd27 | 2012-02-16 00:57:37 +0000 | [diff] [blame] | 2438 | AddObjCARCExceptionMetadata(Inst); |
| 2439 | |
| 2440 | return Inst; |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2441 | } |
| 2442 | |
Chris Lattner | 7085544 | 2011-07-12 04:46:18 +0000 | [diff] [blame] | 2443 | static void checkArgMatches(llvm::Value *Elt, unsigned &ArgNo, |
| 2444 | llvm::FunctionType *FTy) { |
| 2445 | if (ArgNo < FTy->getNumParams()) |
| 2446 | assert(Elt->getType() == FTy->getParamType(ArgNo)); |
| 2447 | else |
| 2448 | assert(FTy->isVarArg()); |
| 2449 | ++ArgNo; |
| 2450 | } |
| 2451 | |
Chris Lattner | 811bf36 | 2011-07-12 06:29:11 +0000 | [diff] [blame] | 2452 | void CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV, |
Craig Topper | 6b9240e | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 2453 | SmallVectorImpl<llvm::Value *> &Args, |
Chris Lattner | 811bf36 | 2011-07-12 06:29:11 +0000 | [diff] [blame] | 2454 | llvm::FunctionType *IRFuncTy) { |
Bob Wilson | 194f06a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 2455 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
| 2456 | unsigned NumElts = AT->getSize().getZExtValue(); |
| 2457 | QualType EltTy = AT->getElementType(); |
| 2458 | llvm::Value *Addr = RV.getAggregateAddr(); |
| 2459 | for (unsigned Elt = 0; Elt < NumElts; ++Elt) { |
| 2460 | llvm::Value *EltAddr = Builder.CreateConstGEP2_32(Addr, 0, Elt); |
Nick Lewycky | 4ee7dc2 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 2461 | RValue EltRV = convertTempToRValue(EltAddr, EltTy, SourceLocation()); |
Bob Wilson | 194f06a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 2462 | ExpandTypeToArgs(EltTy, EltRV, Args, IRFuncTy); |
Chris Lattner | 811bf36 | 2011-07-12 06:29:11 +0000 | [diff] [blame] | 2463 | } |
Anton Korobeynikov | eaf856d | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 2464 | } else if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Bob Wilson | 194f06a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 2465 | RecordDecl *RD = RT->getDecl(); |
| 2466 | assert(RV.isAggregate() && "Unexpected rvalue during struct expansion"); |
Eli Friedman | 377ecc7 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 2467 | LValue LV = MakeAddrLValue(RV.getAggregateAddr(), Ty); |
Anton Korobeynikov | eaf856d | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 2468 | |
| 2469 | if (RD->isUnion()) { |
| 2470 | const FieldDecl *LargestFD = 0; |
| 2471 | CharUnits UnionSize = CharUnits::Zero(); |
| 2472 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2473 | for (const auto *FD : RD->fields()) { |
Anton Korobeynikov | eaf856d | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 2474 | assert(!FD->isBitField() && |
| 2475 | "Cannot expand structure with bit-field members."); |
| 2476 | CharUnits FieldSize = getContext().getTypeSizeInChars(FD->getType()); |
| 2477 | if (UnionSize < FieldSize) { |
| 2478 | UnionSize = FieldSize; |
| 2479 | LargestFD = FD; |
| 2480 | } |
| 2481 | } |
| 2482 | if (LargestFD) { |
Nick Lewycky | 4ee7dc2 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 2483 | RValue FldRV = EmitRValueForField(LV, LargestFD, SourceLocation()); |
Anton Korobeynikov | eaf856d | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 2484 | ExpandTypeToArgs(LargestFD->getType(), FldRV, Args, IRFuncTy); |
| 2485 | } |
| 2486 | } else { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2487 | for (const auto *FD : RD->fields()) { |
Nick Lewycky | 4ee7dc2 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 2488 | RValue FldRV = EmitRValueForField(LV, FD, SourceLocation()); |
Anton Korobeynikov | eaf856d | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 2489 | ExpandTypeToArgs(FD->getType(), FldRV, Args, IRFuncTy); |
| 2490 | } |
Bob Wilson | 194f06a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 2491 | } |
Eli Friedman | ca3d3fc | 2011-11-15 02:46:03 +0000 | [diff] [blame] | 2492 | } else if (Ty->isAnyComplexType()) { |
Bob Wilson | 194f06a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 2493 | ComplexPairTy CV = RV.getComplexVal(); |
| 2494 | Args.push_back(CV.first); |
| 2495 | Args.push_back(CV.second); |
| 2496 | } else { |
Chris Lattner | 811bf36 | 2011-07-12 06:29:11 +0000 | [diff] [blame] | 2497 | assert(RV.isScalar() && |
| 2498 | "Unexpected non-scalar rvalue during struct expansion."); |
| 2499 | |
| 2500 | // Insert a bitcast as needed. |
| 2501 | llvm::Value *V = RV.getScalarVal(); |
| 2502 | if (Args.size() < IRFuncTy->getNumParams() && |
| 2503 | V->getType() != IRFuncTy->getParamType(Args.size())) |
| 2504 | V = Builder.CreateBitCast(V, IRFuncTy->getParamType(Args.size())); |
| 2505 | |
| 2506 | Args.push_back(V); |
| 2507 | } |
| 2508 | } |
| 2509 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2510 | /// \brief Store a non-aggregate value to an address to initialize it. For |
| 2511 | /// initialization, a non-atomic store will be used. |
| 2512 | static void EmitInitStoreOfNonAggregate(CodeGenFunction &CGF, RValue Src, |
| 2513 | LValue Dst) { |
| 2514 | if (Src.isScalar()) |
| 2515 | CGF.EmitStoreOfScalar(Src.getScalarVal(), Dst, /*init=*/true); |
| 2516 | else |
| 2517 | CGF.EmitStoreOfComplex(Src.getComplexVal(), Dst, /*init=*/true); |
| 2518 | } |
| 2519 | |
| 2520 | void CodeGenFunction::deferPlaceholderReplacement(llvm::Instruction *Old, |
| 2521 | llvm::Value *New) { |
| 2522 | DeferredReplacements.push_back(std::make_pair(Old, New)); |
| 2523 | } |
Chris Lattner | 811bf36 | 2011-07-12 06:29:11 +0000 | [diff] [blame] | 2524 | |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 2525 | RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2526 | llvm::Value *Callee, |
Anders Carlsson | f3c47c9 | 2009-12-24 19:25:24 +0000 | [diff] [blame] | 2527 | ReturnValueSlot ReturnValue, |
Daniel Dunbar | c0ef9f5 | 2009-02-20 18:06:48 +0000 | [diff] [blame] | 2528 | const CallArgList &CallArgs, |
David Chisnall | dd5c98f | 2010-05-01 11:15:56 +0000 | [diff] [blame] | 2529 | const Decl *TargetDecl, |
David Chisnall | 4b02afc | 2010-05-02 13:41:58 +0000 | [diff] [blame] | 2530 | llvm::Instruction **callOrInvoke) { |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 2531 | // FIXME: We no longer need the types from CallArgs; lift up and simplify. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2532 | SmallVector<llvm::Value*, 16> Args; |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 2533 | |
| 2534 | // Handle struct-return functions by passing a pointer to the |
| 2535 | // location that we would like to return into. |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 2536 | QualType RetTy = CallInfo.getReturnType(); |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 2537 | const ABIArgInfo &RetAI = CallInfo.getReturnInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2538 | |
Chris Lattner | 7085544 | 2011-07-12 04:46:18 +0000 | [diff] [blame] | 2539 | // IRArgNo - Keep track of the argument number in the callee we're looking at. |
| 2540 | unsigned IRArgNo = 0; |
| 2541 | llvm::FunctionType *IRFuncTy = |
| 2542 | cast<llvm::FunctionType>( |
| 2543 | cast<llvm::PointerType>(Callee->getType())->getElementType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2544 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2545 | // If we're using inalloca, insert the allocation after the stack save. |
| 2546 | // FIXME: Do this earlier rather than hacking it in here! |
| 2547 | llvm::Value *ArgMemory = 0; |
| 2548 | if (llvm::StructType *ArgStruct = CallInfo.getArgStruct()) { |
| 2549 | llvm::AllocaInst *AI = new llvm::AllocaInst( |
| 2550 | ArgStruct, "argmem", CallArgs.getStackBase()->getNextNode()); |
| 2551 | AI->setUsedWithInAlloca(true); |
| 2552 | assert(AI->isUsedWithInAlloca() && !AI->isStaticAlloca()); |
| 2553 | ArgMemory = AI; |
| 2554 | } |
| 2555 | |
Chris Lattner | 5db7ae5 | 2009-06-13 00:26:38 +0000 | [diff] [blame] | 2556 | // If the call returns a temporary with struct return, create a temporary |
Anders Carlsson | d2490a9 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 2557 | // alloca to hold the result, unless one is given to us. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2558 | llvm::Value *SRetPtr = 0; |
| 2559 | if (CGM.ReturnTypeUsesSRet(CallInfo) || RetAI.isInAlloca()) { |
| 2560 | SRetPtr = ReturnValue.getValue(); |
| 2561 | if (!SRetPtr) |
| 2562 | SRetPtr = CreateMemTemp(RetTy); |
| 2563 | if (CGM.ReturnTypeUsesSRet(CallInfo)) { |
| 2564 | Args.push_back(SRetPtr); |
| 2565 | checkArgMatches(SRetPtr, IRArgNo, IRFuncTy); |
| 2566 | } else { |
| 2567 | llvm::Value *Addr = |
| 2568 | Builder.CreateStructGEP(ArgMemory, RetAI.getInAllocaFieldIndex()); |
| 2569 | Builder.CreateStore(SRetPtr, Addr); |
| 2570 | } |
Anders Carlsson | d2490a9 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 2571 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2572 | |
Daniel Dunbar | 4b5f0a4 | 2009-02-04 21:17:21 +0000 | [diff] [blame] | 2573 | assert(CallInfo.arg_size() == CallArgs.size() && |
| 2574 | "Mismatch between function signature & arguments."); |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 2575 | CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2576 | for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end(); |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 2577 | I != E; ++I, ++info_it) { |
| 2578 | const ABIArgInfo &ArgInfo = info_it->info; |
Eli Friedman | c6d0782 | 2011-05-02 18:05:27 +0000 | [diff] [blame] | 2579 | RValue RV = I->RV; |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 2580 | |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 2581 | CharUnits TypeAlign = getContext().getTypeAlignInChars(I->Ty); |
Rafael Espindola | e4aeeaa | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 2582 | |
| 2583 | // Insert a padding argument to ensure proper alignment. |
| 2584 | if (llvm::Type *PaddingType = ArgInfo.getPaddingType()) { |
| 2585 | Args.push_back(llvm::UndefValue::get(PaddingType)); |
| 2586 | ++IRArgNo; |
| 2587 | } |
| 2588 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 2589 | switch (ArgInfo.getKind()) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2590 | case ABIArgInfo::InAlloca: { |
| 2591 | assert(getTarget().getTriple().getArch() == llvm::Triple::x86); |
| 2592 | if (RV.isAggregate()) { |
| 2593 | // Replace the placeholder with the appropriate argument slot GEP. |
| 2594 | llvm::Instruction *Placeholder = |
| 2595 | cast<llvm::Instruction>(RV.getAggregateAddr()); |
| 2596 | CGBuilderTy::InsertPoint IP = Builder.saveIP(); |
| 2597 | Builder.SetInsertPoint(Placeholder); |
| 2598 | llvm::Value *Addr = Builder.CreateStructGEP( |
| 2599 | ArgMemory, ArgInfo.getInAllocaFieldIndex()); |
| 2600 | Builder.restoreIP(IP); |
| 2601 | deferPlaceholderReplacement(Placeholder, Addr); |
| 2602 | } else { |
| 2603 | // Store the RValue into the argument struct. |
| 2604 | llvm::Value *Addr = |
| 2605 | Builder.CreateStructGEP(ArgMemory, ArgInfo.getInAllocaFieldIndex()); |
| 2606 | unsigned AS = Addr->getType()->getPointerAddressSpace(); |
| 2607 | llvm::Type *MemType = ConvertTypeForMem(I->Ty)->getPointerTo(AS); |
| 2608 | // There are some cases where a trivial bitcast is not avoidable. The |
| 2609 | // definition of a type later in a translation unit may change it's type |
| 2610 | // from {}* to (%struct.foo*)*. |
| 2611 | if (Addr->getType() != MemType) |
| 2612 | Addr = Builder.CreateBitCast(Addr, MemType); |
| 2613 | LValue argLV = MakeAddrLValue(Addr, I->Ty, TypeAlign); |
| 2614 | EmitInitStoreOfNonAggregate(*this, RV, argLV); |
| 2615 | } |
| 2616 | break; // Don't increment IRArgNo! |
| 2617 | } |
| 2618 | |
Daniel Dunbar | 91a16fa | 2010-08-21 02:24:36 +0000 | [diff] [blame] | 2619 | case ABIArgInfo::Indirect: { |
Daniel Dunbar | 1f74598 | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 2620 | if (RV.isScalar() || RV.isComplex()) { |
| 2621 | // Make a temporary alloca to pass the argument. |
Eli Friedman | 70cbd2a | 2011-06-15 18:26:32 +0000 | [diff] [blame] | 2622 | llvm::AllocaInst *AI = CreateMemTemp(I->Ty); |
| 2623 | if (ArgInfo.getIndirectAlign() > AI->getAlignment()) |
| 2624 | AI->setAlignment(ArgInfo.getIndirectAlign()); |
| 2625 | Args.push_back(AI); |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 2626 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2627 | LValue argLV = MakeAddrLValue(Args.back(), I->Ty, TypeAlign); |
| 2628 | EmitInitStoreOfNonAggregate(*this, RV, argLV); |
Chris Lattner | 7085544 | 2011-07-12 04:46:18 +0000 | [diff] [blame] | 2629 | |
| 2630 | // Validate argument match. |
| 2631 | checkArgMatches(AI, IRArgNo, IRFuncTy); |
Daniel Dunbar | 1f74598 | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 2632 | } else { |
Eli Friedman | ea5e4da | 2011-06-14 01:37:52 +0000 | [diff] [blame] | 2633 | // We want to avoid creating an unnecessary temporary+copy here; |
Guy Benyei | d436c99 | 2013-03-10 12:59:00 +0000 | [diff] [blame] | 2634 | // however, we need one in three cases: |
Eli Friedman | ea5e4da | 2011-06-14 01:37:52 +0000 | [diff] [blame] | 2635 | // 1. If the argument is not byval, and we are required to copy the |
| 2636 | // source. (This case doesn't occur on any common architecture.) |
| 2637 | // 2. If the argument is byval, RV is not sufficiently aligned, and |
| 2638 | // we cannot force it to be sufficiently aligned. |
Guy Benyei | d436c99 | 2013-03-10 12:59:00 +0000 | [diff] [blame] | 2639 | // 3. If the argument is byval, but RV is located in an address space |
| 2640 | // different than that of the argument (0). |
Eli Friedman | 97cb5a4 | 2011-06-15 22:09:18 +0000 | [diff] [blame] | 2641 | llvm::Value *Addr = RV.getAggregateAddr(); |
| 2642 | unsigned Align = ArgInfo.getIndirectAlign(); |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2643 | const llvm::DataLayout *TD = &CGM.getDataLayout(); |
Guy Benyei | d436c99 | 2013-03-10 12:59:00 +0000 | [diff] [blame] | 2644 | const unsigned RVAddrSpace = Addr->getType()->getPointerAddressSpace(); |
| 2645 | const unsigned ArgAddrSpace = (IRArgNo < IRFuncTy->getNumParams() ? |
| 2646 | IRFuncTy->getParamType(IRArgNo)->getPointerAddressSpace() : 0); |
Eli Friedman | 97cb5a4 | 2011-06-15 22:09:18 +0000 | [diff] [blame] | 2647 | if ((!ArgInfo.getIndirectByVal() && I->NeedsCopy) || |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 2648 | (ArgInfo.getIndirectByVal() && TypeAlign.getQuantity() < Align && |
Guy Benyei | d436c99 | 2013-03-10 12:59:00 +0000 | [diff] [blame] | 2649 | llvm::getOrEnforceKnownAlignment(Addr, Align, TD) < Align) || |
| 2650 | (ArgInfo.getIndirectByVal() && (RVAddrSpace != ArgAddrSpace))) { |
Eli Friedman | ea5e4da | 2011-06-14 01:37:52 +0000 | [diff] [blame] | 2651 | // Create an aligned temporary, and copy to it. |
Eli Friedman | 97cb5a4 | 2011-06-15 22:09:18 +0000 | [diff] [blame] | 2652 | llvm::AllocaInst *AI = CreateMemTemp(I->Ty); |
| 2653 | if (Align > AI->getAlignment()) |
| 2654 | AI->setAlignment(Align); |
Eli Friedman | ea5e4da | 2011-06-14 01:37:52 +0000 | [diff] [blame] | 2655 | Args.push_back(AI); |
Chad Rosier | 649b4a1 | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 2656 | EmitAggregateCopy(AI, Addr, I->Ty, RV.isVolatileQualified()); |
Chris Lattner | 7085544 | 2011-07-12 04:46:18 +0000 | [diff] [blame] | 2657 | |
| 2658 | // Validate argument match. |
| 2659 | checkArgMatches(AI, IRArgNo, IRFuncTy); |
Eli Friedman | ea5e4da | 2011-06-14 01:37:52 +0000 | [diff] [blame] | 2660 | } else { |
| 2661 | // Skip the extra memcpy call. |
Eli Friedman | 97cb5a4 | 2011-06-15 22:09:18 +0000 | [diff] [blame] | 2662 | Args.push_back(Addr); |
Chris Lattner | 7085544 | 2011-07-12 04:46:18 +0000 | [diff] [blame] | 2663 | |
| 2664 | // Validate argument match. |
| 2665 | checkArgMatches(Addr, IRArgNo, IRFuncTy); |
Eli Friedman | ea5e4da | 2011-06-14 01:37:52 +0000 | [diff] [blame] | 2666 | } |
Daniel Dunbar | 1f74598 | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 2667 | } |
| 2668 | break; |
Daniel Dunbar | 91a16fa | 2010-08-21 02:24:36 +0000 | [diff] [blame] | 2669 | } |
Daniel Dunbar | 1f74598 | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 2670 | |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 2671 | case ABIArgInfo::Ignore: |
| 2672 | break; |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2673 | |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2674 | case ABIArgInfo::Extend: |
| 2675 | case ABIArgInfo::Direct: { |
| 2676 | if (!isa<llvm::StructType>(ArgInfo.getCoerceToType()) && |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2677 | ArgInfo.getCoerceToType() == ConvertType(info_it->type) && |
| 2678 | ArgInfo.getDirectOffset() == 0) { |
Chris Lattner | 7085544 | 2011-07-12 04:46:18 +0000 | [diff] [blame] | 2679 | llvm::Value *V; |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2680 | if (RV.isScalar()) |
Chris Lattner | 7085544 | 2011-07-12 04:46:18 +0000 | [diff] [blame] | 2681 | V = RV.getScalarVal(); |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2682 | else |
Chris Lattner | 7085544 | 2011-07-12 04:46:18 +0000 | [diff] [blame] | 2683 | V = Builder.CreateLoad(RV.getAggregateAddr()); |
| 2684 | |
Chris Lattner | 21ca1fd | 2011-07-12 04:53:39 +0000 | [diff] [blame] | 2685 | // If the argument doesn't match, perform a bitcast to coerce it. This |
| 2686 | // can happen due to trivial type mismatches. |
| 2687 | if (IRArgNo < IRFuncTy->getNumParams() && |
| 2688 | V->getType() != IRFuncTy->getParamType(IRArgNo)) |
| 2689 | V = Builder.CreateBitCast(V, IRFuncTy->getParamType(IRArgNo)); |
Chris Lattner | 7085544 | 2011-07-12 04:46:18 +0000 | [diff] [blame] | 2690 | Args.push_back(V); |
| 2691 | |
Chris Lattner | 7085544 | 2011-07-12 04:46:18 +0000 | [diff] [blame] | 2692 | checkArgMatches(V, IRArgNo, IRFuncTy); |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2693 | break; |
| 2694 | } |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 2695 | |
Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 2696 | // FIXME: Avoid the conversion through memory if possible. |
| 2697 | llvm::Value *SrcPtr; |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 2698 | if (RV.isScalar() || RV.isComplex()) { |
Eli Friedman | c6d0782 | 2011-05-02 18:05:27 +0000 | [diff] [blame] | 2699 | SrcPtr = CreateMemTemp(I->Ty, "coerce"); |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 2700 | LValue SrcLV = MakeAddrLValue(SrcPtr, I->Ty, TypeAlign); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2701 | EmitInitStoreOfNonAggregate(*this, RV, SrcLV); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2702 | } else |
Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 2703 | SrcPtr = RV.getAggregateAddr(); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2704 | |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2705 | // If the value is offset in memory, apply the offset now. |
| 2706 | if (unsigned Offs = ArgInfo.getDirectOffset()) { |
| 2707 | SrcPtr = Builder.CreateBitCast(SrcPtr, Builder.getInt8PtrTy()); |
| 2708 | SrcPtr = Builder.CreateConstGEP1_32(SrcPtr, Offs); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2709 | SrcPtr = Builder.CreateBitCast(SrcPtr, |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2710 | llvm::PointerType::getUnqual(ArgInfo.getCoerceToType())); |
| 2711 | |
| 2712 | } |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2713 | |
Chris Lattner | ce70016 | 2010-06-28 23:44:11 +0000 | [diff] [blame] | 2714 | // If the coerce-to type is a first class aggregate, we flatten it and |
| 2715 | // pass the elements. Either way is semantically identical, but fast-isel |
| 2716 | // and the optimizer generally likes scalar values better than FCAs. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2717 | if (llvm::StructType *STy = |
Chris Lattner | 309c59f | 2010-06-29 00:06:42 +0000 | [diff] [blame] | 2718 | dyn_cast<llvm::StructType>(ArgInfo.getCoerceToType())) { |
Chandler Carruth | f82232c | 2012-10-10 11:29:08 +0000 | [diff] [blame] | 2719 | llvm::Type *SrcTy = |
| 2720 | cast<llvm::PointerType>(SrcPtr->getType())->getElementType(); |
| 2721 | uint64_t SrcSize = CGM.getDataLayout().getTypeAllocSize(SrcTy); |
| 2722 | uint64_t DstSize = CGM.getDataLayout().getTypeAllocSize(STy); |
| 2723 | |
| 2724 | // If the source type is smaller than the destination type of the |
| 2725 | // coerce-to logic, copy the source value into a temp alloca the size |
| 2726 | // of the destination type to allow loading all of it. The bits past |
| 2727 | // the source value are left undef. |
| 2728 | if (SrcSize < DstSize) { |
| 2729 | llvm::AllocaInst *TempAlloca |
| 2730 | = CreateTempAlloca(STy, SrcPtr->getName() + ".coerce"); |
| 2731 | Builder.CreateMemCpy(TempAlloca, SrcPtr, SrcSize, 0); |
| 2732 | SrcPtr = TempAlloca; |
| 2733 | } else { |
| 2734 | SrcPtr = Builder.CreateBitCast(SrcPtr, |
| 2735 | llvm::PointerType::getUnqual(STy)); |
| 2736 | } |
| 2737 | |
Chris Lattner | 9282688 | 2010-07-05 20:41:41 +0000 | [diff] [blame] | 2738 | for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { |
| 2739 | llvm::Value *EltPtr = Builder.CreateConstGEP2_32(SrcPtr, 0, i); |
Chris Lattner | deabde2 | 2010-07-28 18:24:28 +0000 | [diff] [blame] | 2740 | llvm::LoadInst *LI = Builder.CreateLoad(EltPtr); |
| 2741 | // We don't know what we're loading from. |
| 2742 | LI->setAlignment(1); |
| 2743 | Args.push_back(LI); |
Chris Lattner | 7085544 | 2011-07-12 04:46:18 +0000 | [diff] [blame] | 2744 | |
| 2745 | // Validate argument match. |
| 2746 | checkArgMatches(LI, IRArgNo, IRFuncTy); |
Chris Lattner | 309c59f | 2010-06-29 00:06:42 +0000 | [diff] [blame] | 2747 | } |
Chris Lattner | ce70016 | 2010-06-28 23:44:11 +0000 | [diff] [blame] | 2748 | } else { |
Chris Lattner | 309c59f | 2010-06-29 00:06:42 +0000 | [diff] [blame] | 2749 | // In the simple case, just pass the coerced loaded value. |
| 2750 | Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(), |
| 2751 | *this)); |
Chris Lattner | 7085544 | 2011-07-12 04:46:18 +0000 | [diff] [blame] | 2752 | |
| 2753 | // Validate argument match. |
| 2754 | checkArgMatches(Args.back(), IRArgNo, IRFuncTy); |
Chris Lattner | ce70016 | 2010-06-28 23:44:11 +0000 | [diff] [blame] | 2755 | } |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2756 | |
Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 2757 | break; |
| 2758 | } |
| 2759 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 2760 | case ABIArgInfo::Expand: |
Chris Lattner | 811bf36 | 2011-07-12 06:29:11 +0000 | [diff] [blame] | 2761 | ExpandTypeToArgs(I->Ty, RV, Args, IRFuncTy); |
Chris Lattner | 7085544 | 2011-07-12 04:46:18 +0000 | [diff] [blame] | 2762 | IRArgNo = Args.size(); |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 2763 | break; |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 2764 | } |
| 2765 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2766 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2767 | if (ArgMemory) { |
| 2768 | llvm::Value *Arg = ArgMemory; |
| 2769 | llvm::Type *LastParamTy = |
| 2770 | IRFuncTy->getParamType(IRFuncTy->getNumParams() - 1); |
| 2771 | if (Arg->getType() != LastParamTy) { |
| 2772 | #ifndef NDEBUG |
| 2773 | // Assert that these structs have equivalent element types. |
| 2774 | llvm::StructType *FullTy = CallInfo.getArgStruct(); |
| 2775 | llvm::StructType *Prefix = cast<llvm::StructType>( |
| 2776 | cast<llvm::PointerType>(LastParamTy)->getElementType()); |
| 2777 | |
| 2778 | // For variadic functions, the caller might supply a larger struct than |
| 2779 | // the callee expects, and that's OK. |
| 2780 | assert(Prefix->getNumElements() == FullTy->getNumElements() || |
| 2781 | (CallInfo.isVariadic() && |
| 2782 | Prefix->getNumElements() <= FullTy->getNumElements())); |
| 2783 | |
| 2784 | for (llvm::StructType::element_iterator PI = Prefix->element_begin(), |
| 2785 | PE = Prefix->element_end(), |
| 2786 | FI = FullTy->element_begin(); |
| 2787 | PI != PE; ++PI, ++FI) |
| 2788 | assert(*PI == *FI); |
| 2789 | #endif |
| 2790 | Arg = Builder.CreateBitCast(Arg, LastParamTy); |
| 2791 | } |
| 2792 | Args.push_back(Arg); |
| 2793 | } |
| 2794 | |
Reid Kleckner | 9b60195 | 2013-06-21 12:45:15 +0000 | [diff] [blame] | 2795 | if (!CallArgs.getCleanupsToDeactivate().empty()) |
| 2796 | deactivateArgCleanupsBeforeCall(*this, CallArgs); |
| 2797 | |
Chris Lattner | 5db7ae5 | 2009-06-13 00:26:38 +0000 | [diff] [blame] | 2798 | // If the callee is a bitcast of a function to a varargs pointer to function |
| 2799 | // type, check to see if we can remove the bitcast. This handles some cases |
| 2800 | // with unprototyped functions. |
| 2801 | if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Callee)) |
| 2802 | if (llvm::Function *CalleeF = dyn_cast<llvm::Function>(CE->getOperand(0))) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2803 | llvm::PointerType *CurPT=cast<llvm::PointerType>(Callee->getType()); |
| 2804 | llvm::FunctionType *CurFT = |
Chris Lattner | 5db7ae5 | 2009-06-13 00:26:38 +0000 | [diff] [blame] | 2805 | cast<llvm::FunctionType>(CurPT->getElementType()); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2806 | llvm::FunctionType *ActualFT = CalleeF->getFunctionType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2807 | |
Chris Lattner | 5db7ae5 | 2009-06-13 00:26:38 +0000 | [diff] [blame] | 2808 | if (CE->getOpcode() == llvm::Instruction::BitCast && |
| 2809 | ActualFT->getReturnType() == CurFT->getReturnType() && |
Chris Lattner | d6bebbf | 2009-06-23 01:38:41 +0000 | [diff] [blame] | 2810 | ActualFT->getNumParams() == CurFT->getNumParams() && |
Fariborz Jahanian | c0ddef2 | 2011-03-01 17:28:13 +0000 | [diff] [blame] | 2811 | ActualFT->getNumParams() == Args.size() && |
| 2812 | (CurFT->isVarArg() || !ActualFT->isVarArg())) { |
Chris Lattner | 5db7ae5 | 2009-06-13 00:26:38 +0000 | [diff] [blame] | 2813 | bool ArgsMatch = true; |
| 2814 | for (unsigned i = 0, e = ActualFT->getNumParams(); i != e; ++i) |
| 2815 | if (ActualFT->getParamType(i) != CurFT->getParamType(i)) { |
| 2816 | ArgsMatch = false; |
| 2817 | break; |
| 2818 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2819 | |
Chris Lattner | 5db7ae5 | 2009-06-13 00:26:38 +0000 | [diff] [blame] | 2820 | // Strip the cast if we can get away with it. This is a nice cleanup, |
| 2821 | // but also allows us to inline the function at -O0 if it is marked |
| 2822 | // always_inline. |
| 2823 | if (ArgsMatch) |
| 2824 | Callee = CalleeF; |
| 2825 | } |
| 2826 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2827 | |
Daniel Dunbar | ca6408c | 2009-09-12 00:59:20 +0000 | [diff] [blame] | 2828 | unsigned CallingConv; |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 2829 | CodeGen::AttributeListType AttributeList; |
Bill Wendling | 94236e7 | 2013-02-22 00:13:35 +0000 | [diff] [blame] | 2830 | CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList, |
| 2831 | CallingConv, true); |
Bill Wendling | 785b778 | 2012-12-07 23:17:26 +0000 | [diff] [blame] | 2832 | llvm::AttributeSet Attrs = llvm::AttributeSet::get(getLLVMContext(), |
Bill Wendling | 94236e7 | 2013-02-22 00:13:35 +0000 | [diff] [blame] | 2833 | AttributeList); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2834 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2835 | llvm::BasicBlock *InvokeDest = 0; |
Bill Wendling | 01ad954 | 2012-12-30 10:32:17 +0000 | [diff] [blame] | 2836 | if (!Attrs.hasAttribute(llvm::AttributeSet::FunctionIndex, |
| 2837 | llvm::Attribute::NoUnwind)) |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2838 | InvokeDest = getInvokeDest(); |
| 2839 | |
Daniel Dunbar | d14151d | 2009-03-02 04:32:35 +0000 | [diff] [blame] | 2840 | llvm::CallSite CS; |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2841 | if (!InvokeDest) { |
Jay Foad | 4c7d9f1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 2842 | CS = Builder.CreateCall(Callee, Args); |
Daniel Dunbar | 9834ffb | 2009-02-23 17:26:39 +0000 | [diff] [blame] | 2843 | } else { |
| 2844 | llvm::BasicBlock *Cont = createBasicBlock("invoke.cont"); |
Jay Foad | 4c7d9f1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 2845 | CS = Builder.CreateInvoke(Callee, Cont, InvokeDest, Args); |
Daniel Dunbar | 9834ffb | 2009-02-23 17:26:39 +0000 | [diff] [blame] | 2846 | EmitBlock(Cont); |
Daniel Dunbar | f4fe0f0 | 2009-02-20 18:54:31 +0000 | [diff] [blame] | 2847 | } |
Chris Lattner | ce93399 | 2010-06-29 16:40:28 +0000 | [diff] [blame] | 2848 | if (callOrInvoke) |
David Chisnall | 4b02afc | 2010-05-02 13:41:58 +0000 | [diff] [blame] | 2849 | *callOrInvoke = CS.getInstruction(); |
Daniel Dunbar | f4fe0f0 | 2009-02-20 18:54:31 +0000 | [diff] [blame] | 2850 | |
Daniel Dunbar | d14151d | 2009-03-02 04:32:35 +0000 | [diff] [blame] | 2851 | CS.setAttributes(Attrs); |
Daniel Dunbar | ca6408c | 2009-09-12 00:59:20 +0000 | [diff] [blame] | 2852 | CS.setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv)); |
Daniel Dunbar | d14151d | 2009-03-02 04:32:35 +0000 | [diff] [blame] | 2853 | |
Dan Gohman | b49bd27 | 2012-02-16 00:57:37 +0000 | [diff] [blame] | 2854 | // In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC |
| 2855 | // optimizer it can aggressively ignore unwind edges. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2856 | if (CGM.getLangOpts().ObjCAutoRefCount) |
Dan Gohman | b49bd27 | 2012-02-16 00:57:37 +0000 | [diff] [blame] | 2857 | AddObjCARCExceptionMetadata(CS.getInstruction()); |
| 2858 | |
Daniel Dunbar | d14151d | 2009-03-02 04:32:35 +0000 | [diff] [blame] | 2859 | // If the call doesn't return, finish the basic block and clear the |
| 2860 | // insertion point; this allows the rest of IRgen to discard |
| 2861 | // unreachable code. |
| 2862 | if (CS.doesNotReturn()) { |
| 2863 | Builder.CreateUnreachable(); |
| 2864 | Builder.ClearInsertionPoint(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2865 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 2866 | // FIXME: For now, emit a dummy basic block because expr emitters in |
| 2867 | // generally are not ready to handle emitting expressions at unreachable |
| 2868 | // points. |
Daniel Dunbar | d14151d | 2009-03-02 04:32:35 +0000 | [diff] [blame] | 2869 | EnsureInsertPoint(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2870 | |
Daniel Dunbar | d14151d | 2009-03-02 04:32:35 +0000 | [diff] [blame] | 2871 | // Return a reasonable RValue. |
| 2872 | return GetUndefRValue(RetTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2873 | } |
Daniel Dunbar | d14151d | 2009-03-02 04:32:35 +0000 | [diff] [blame] | 2874 | |
| 2875 | llvm::Instruction *CI = CS.getInstruction(); |
Benjamin Kramer | ffbb15e | 2009-10-05 13:47:21 +0000 | [diff] [blame] | 2876 | if (Builder.isNamePreserving() && !CI->getType()->isVoidTy()) |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 2877 | CI->setName("call"); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 2878 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2879 | // Emit any writebacks immediately. Arguably this should happen |
| 2880 | // after any return-value munging. |
| 2881 | if (CallArgs.hasWritebacks()) |
| 2882 | emitWritebacks(*this, CallArgs); |
| 2883 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2884 | // The stack cleanup for inalloca arguments has to run out of the normal |
| 2885 | // lexical order, so deactivate it and run it manually here. |
| 2886 | CallArgs.freeArgumentMemory(*this); |
| 2887 | |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 2888 | switch (RetAI.getKind()) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2889 | case ABIArgInfo::InAlloca: |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 2890 | case ABIArgInfo::Indirect: |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2891 | return convertTempToRValue(SRetPtr, RetTy, SourceLocation()); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 2892 | |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 2893 | case ABIArgInfo::Ignore: |
Daniel Dunbar | 0bcc521 | 2009-02-03 06:30:17 +0000 | [diff] [blame] | 2894 | // If we are ignoring an argument that had a result, make sure to |
| 2895 | // construct the appropriate return value for our caller. |
Daniel Dunbar | 13e8173 | 2009-02-05 07:09:07 +0000 | [diff] [blame] | 2896 | return GetUndefRValue(RetTy); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2897 | |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2898 | case ABIArgInfo::Extend: |
| 2899 | case ABIArgInfo::Direct: { |
Chris Lattner | 6af13f3 | 2011-07-13 03:59:32 +0000 | [diff] [blame] | 2900 | llvm::Type *RetIRTy = ConvertType(RetTy); |
| 2901 | if (RetAI.getCoerceToType() == RetIRTy && RetAI.getDirectOffset() == 0) { |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 2902 | switch (getEvaluationKind(RetTy)) { |
| 2903 | case TEK_Complex: { |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2904 | llvm::Value *Real = Builder.CreateExtractValue(CI, 0); |
| 2905 | llvm::Value *Imag = Builder.CreateExtractValue(CI, 1); |
| 2906 | return RValue::getComplex(std::make_pair(Real, Imag)); |
| 2907 | } |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 2908 | case TEK_Aggregate: { |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2909 | llvm::Value *DestPtr = ReturnValue.getValue(); |
| 2910 | bool DestIsVolatile = ReturnValue.isVolatile(); |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 2911 | |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2912 | if (!DestPtr) { |
| 2913 | DestPtr = CreateMemTemp(RetTy, "agg.tmp"); |
| 2914 | DestIsVolatile = false; |
| 2915 | } |
Eli Friedman | badea57 | 2011-05-17 21:08:01 +0000 | [diff] [blame] | 2916 | BuildAggStore(*this, CI, DestPtr, DestIsVolatile, false); |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2917 | return RValue::getAggregate(DestPtr); |
| 2918 | } |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 2919 | case TEK_Scalar: { |
| 2920 | // If the argument doesn't match, perform a bitcast to coerce it. This |
| 2921 | // can happen due to trivial type mismatches. |
| 2922 | llvm::Value *V = CI; |
| 2923 | if (V->getType() != RetIRTy) |
| 2924 | V = Builder.CreateBitCast(V, RetIRTy); |
| 2925 | return RValue::get(V); |
| 2926 | } |
| 2927 | } |
| 2928 | llvm_unreachable("bad evaluation kind"); |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2929 | } |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2930 | |
Anders Carlsson | d2490a9 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 2931 | llvm::Value *DestPtr = ReturnValue.getValue(); |
| 2932 | bool DestIsVolatile = ReturnValue.isVolatile(); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2933 | |
Anders Carlsson | d2490a9 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 2934 | if (!DestPtr) { |
Daniel Dunbar | 195337d | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 2935 | DestPtr = CreateMemTemp(RetTy, "coerce"); |
Anders Carlsson | d2490a9 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 2936 | DestIsVolatile = false; |
| 2937 | } |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2938 | |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2939 | // If the value is offset in memory, apply the offset now. |
| 2940 | llvm::Value *StorePtr = DestPtr; |
| 2941 | if (unsigned Offs = RetAI.getDirectOffset()) { |
| 2942 | StorePtr = Builder.CreateBitCast(StorePtr, Builder.getInt8PtrTy()); |
| 2943 | StorePtr = Builder.CreateConstGEP1_32(StorePtr, Offs); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2944 | StorePtr = Builder.CreateBitCast(StorePtr, |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2945 | llvm::PointerType::getUnqual(RetAI.getCoerceToType())); |
| 2946 | } |
| 2947 | CreateCoercedStore(CI, StorePtr, DestIsVolatile, *this); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2948 | |
Nick Lewycky | 4ee7dc2 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 2949 | return convertTempToRValue(DestPtr, RetTy, SourceLocation()); |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 2950 | } |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 2951 | |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 2952 | case ABIArgInfo::Expand: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2953 | llvm_unreachable("Invalid ABI kind for return argument"); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 2954 | } |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 2955 | |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2956 | llvm_unreachable("Unhandled ABIArgInfo::Kind"); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 2957 | } |
Daniel Dunbar | b4094ea | 2009-02-10 20:44:09 +0000 | [diff] [blame] | 2958 | |
| 2959 | /* VarArg handling */ |
| 2960 | |
| 2961 | llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) { |
| 2962 | return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this); |
| 2963 | } |