Nick Lewycky | 5fa40c3 | 2013-10-01 21:51:38 +0000 | [diff] [blame] | 1 | //===--- CGCall.cpp - Encapsulate calling convention details --------------===// |
Daniel Dunbar | 3d7c90b | 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 | e70a007 | 2010-06-29 16:40:28 +0000 | [diff] [blame] | 16 | #include "ABIInfo.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 17 | #include "CGCXXABI.h" |
Daniel Dunbar | 3d7c90b | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 18 | #include "CodeGenFunction.h" |
Daniel Dunbar | c68897d | 2008-09-10 00:41:16 +0000 | [diff] [blame] | 19 | #include "CodeGenModule.h" |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 20 | #include "TargetInfo.h" |
Daniel Dunbar | 3d7c90b | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 21 | #include "clang/AST/Decl.h" |
Anders Carlsson | b15b55c | 2009-04-03 22:48:58 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclCXX.h" |
Daniel Dunbar | 3d7c90b | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 23 | #include "clang/AST/DeclObjC.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 24 | #include "clang/Basic/TargetInfo.h" |
Mark Lacey | a8e7df3 | 2013-10-30 21:53:58 +0000 | [diff] [blame] | 25 | #include "clang/CodeGen/CGFunctionInfo.h" |
Chandler Carruth | 8509824 | 2010-06-15 23:19:56 +0000 | [diff] [blame] | 26 | #include "clang/Frontend/CodeGenOptions.h" |
Bill Wendling | 706469b | 2013-02-28 22:49:57 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/StringExtras.h" |
Chandler Carruth | ffd5551 | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 28 | #include "llvm/IR/Attributes.h" |
Chandler Carruth | c80ceea | 2014-03-04 11:02:08 +0000 | [diff] [blame] | 29 | #include "llvm/IR/CallSite.h" |
Chandler Carruth | ffd5551 | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 30 | #include "llvm/IR/DataLayout.h" |
| 31 | #include "llvm/IR/InlineAsm.h" |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 32 | #include "llvm/IR/Intrinsics.h" |
Eli Friedman | f745619 | 2011-06-15 22:09:18 +0000 | [diff] [blame] | 33 | #include "llvm/Transforms/Utils/Local.h" |
Daniel Dunbar | 3d7c90b | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 34 | using namespace clang; |
| 35 | using namespace CodeGen; |
| 36 | |
| 37 | /***/ |
| 38 | |
John McCall | ab26cfa | 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 | a941dca | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 44 | case CC_X86ThisCall: return llvm::CallingConv::X86_ThisCall; |
Charles Davis | b5a214e | 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 | 231e875 | 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 | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 49 | case CC_IntelOclBicc: return llvm::CallingConv::Intel_OCL_BI; |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 50 | // TODO: Add support for __pascal to LLVM. |
| 51 | case CC_X86Pascal: return llvm::CallingConv::C; |
| 52 | // TODO: Add support for __vectorcall to LLVM. |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 53 | case CC_X86VectorCall: return llvm::CallingConv::X86_VectorCall; |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 54 | } |
| 55 | } |
| 56 | |
John McCall | 8ee376f | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 57 | /// Derives the 'this' type for codegen purposes, i.e. ignoring method |
| 58 | /// qualification. |
| 59 | /// FIXME: address space qualification? |
John McCall | 2da83a3 | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 60 | static CanQualType GetThisType(ASTContext &Context, const CXXRecordDecl *RD) { |
| 61 | QualType RecTy = Context.getTagDeclType(RD)->getCanonicalTypeInternal(); |
| 62 | return Context.getPointerType(CanQualType::CreateUnsafe(RecTy)); |
Daniel Dunbar | 7a95ca3 | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 63 | } |
| 64 | |
John McCall | 8ee376f | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 65 | /// Returns the canonical formal type of the given C++ method. |
John McCall | 2da83a3 | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 66 | static CanQual<FunctionProtoType> GetFormalType(const CXXMethodDecl *MD) { |
| 67 | return MD->getType()->getCanonicalTypeUnqualified() |
| 68 | .getAs<FunctionProtoType>(); |
John McCall | 8ee376f | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /// Returns the "extra-canonicalized" return type, which discards |
| 72 | /// qualifiers on the return type. Codegen doesn't care about them, |
| 73 | /// and it makes ABI code a little easier to be able to assume that |
| 74 | /// all parameter and return types are top-level unqualified. |
John McCall | 2da83a3 | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 75 | static CanQualType GetReturnType(QualType RetTy) { |
| 76 | return RetTy->getCanonicalTypeUnqualified().getUnqualifiedType(); |
John McCall | 8ee376f | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 77 | } |
| 78 | |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 79 | /// Arrange the argument and result information for a value of the given |
| 80 | /// unprototyped freestanding function type. |
John McCall | 8ee376f | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 81 | const CGFunctionInfo & |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 82 | CodeGenTypes::arrangeFreeFunctionType(CanQual<FunctionNoProtoType> FTNP) { |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 83 | // When translating an unprototyped function type, always use a |
| 84 | // variadic type. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 85 | return arrangeLLVMFunctionInfo(FTNP->getReturnType().getUnqualifiedType(), |
Reid Kleckner | 4982b82 | 2014-01-31 22:54:50 +0000 | [diff] [blame] | 86 | false, None, FTNP->getExtInfo(), |
| 87 | RequiredArgs(0)); |
John McCall | 8ee376f | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 88 | } |
| 89 | |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 90 | /// Arrange the LLVM function layout for a value of the given function |
Alexey Samsonov | e5ef3ca | 2014-08-13 23:55:54 +0000 | [diff] [blame] | 91 | /// type, on top of any implicit parameters already stored. |
| 92 | static const CGFunctionInfo & |
| 93 | arrangeLLVMFunctionInfo(CodeGenTypes &CGT, bool IsInstanceMethod, |
| 94 | SmallVectorImpl<CanQualType> &prefix, |
| 95 | CanQual<FunctionProtoType> FTP) { |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 96 | RequiredArgs required = RequiredArgs::forPrototypePlus(FTP, prefix.size()); |
Daniel Dunbar | bf8c24a | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 97 | // FIXME: Kill copy. |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 98 | for (unsigned i = 0, e = FTP->getNumParams(); i != e; ++i) |
| 99 | prefix.push_back(FTP->getParamType(i)); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 100 | CanQualType resultType = FTP->getReturnType().getUnqualifiedType(); |
Reid Kleckner | 4982b82 | 2014-01-31 22:54:50 +0000 | [diff] [blame] | 101 | return CGT.arrangeLLVMFunctionInfo(resultType, IsInstanceMethod, prefix, |
Alexey Samsonov | e5ef3ca | 2014-08-13 23:55:54 +0000 | [diff] [blame] | 102 | FTP->getExtInfo(), required); |
John McCall | 8ee376f | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 103 | } |
| 104 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 105 | /// Arrange the argument and result information for a value of the |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 106 | /// given freestanding function type. |
John McCall | 8ee376f | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 107 | const CGFunctionInfo & |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 108 | CodeGenTypes::arrangeFreeFunctionType(CanQual<FunctionProtoType> FTP) { |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 109 | SmallVector<CanQualType, 16> argTypes; |
Alexey Samsonov | e5ef3ca | 2014-08-13 23:55:54 +0000 | [diff] [blame] | 110 | return ::arrangeLLVMFunctionInfo(*this, false, argTypes, FTP); |
Daniel Dunbar | 7feafc7 | 2009-09-11 22:24:53 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Aaron Ballman | 0362a6d | 2013-12-18 16:23:37 +0000 | [diff] [blame] | 113 | static CallingConv getCallingConventionForDecl(const Decl *D, bool IsWindows) { |
Daniel Dunbar | 7feafc7 | 2009-09-11 22:24:53 +0000 | [diff] [blame] | 114 | // Set the appropriate calling convention for the Function. |
| 115 | if (D->hasAttr<StdCallAttr>()) |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 116 | return CC_X86StdCall; |
Daniel Dunbar | 7feafc7 | 2009-09-11 22:24:53 +0000 | [diff] [blame] | 117 | |
| 118 | if (D->hasAttr<FastCallAttr>()) |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 119 | return CC_X86FastCall; |
Daniel Dunbar | 7feafc7 | 2009-09-11 22:24:53 +0000 | [diff] [blame] | 120 | |
Douglas Gregor | a941dca | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 121 | if (D->hasAttr<ThisCallAttr>()) |
| 122 | return CC_X86ThisCall; |
| 123 | |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 124 | if (D->hasAttr<VectorCallAttr>()) |
| 125 | return CC_X86VectorCall; |
| 126 | |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 127 | if (D->hasAttr<PascalAttr>()) |
| 128 | return CC_X86Pascal; |
| 129 | |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 130 | if (PcsAttr *PCS = D->getAttr<PcsAttr>()) |
| 131 | return (PCS->getPCS() == PcsAttr::AAPCS ? CC_AAPCS : CC_AAPCS_VFP); |
| 132 | |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 133 | if (D->hasAttr<PnaclCallAttr>()) |
| 134 | return CC_PnaclCall; |
| 135 | |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 136 | if (D->hasAttr<IntelOclBiccAttr>()) |
| 137 | return CC_IntelOclBicc; |
| 138 | |
Aaron Ballman | 0362a6d | 2013-12-18 16:23:37 +0000 | [diff] [blame] | 139 | if (D->hasAttr<MSABIAttr>()) |
| 140 | return IsWindows ? CC_C : CC_X86_64Win64; |
| 141 | |
| 142 | if (D->hasAttr<SysVABIAttr>()) |
| 143 | return IsWindows ? CC_X86_64SysV : CC_C; |
| 144 | |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 145 | return CC_C; |
Daniel Dunbar | 7a95ca3 | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 146 | } |
| 147 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 148 | /// Arrange the argument and result information for a call to an |
| 149 | /// unknown C++ non-static member function of the given abstract type. |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 150 | /// (Zero value of RD means we don't have any meaningful "this" argument type, |
| 151 | /// so fall back to a generic pointer type). |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 152 | /// The member function must be an ordinary function, i.e. not a |
| 153 | /// constructor or destructor. |
| 154 | const CGFunctionInfo & |
| 155 | CodeGenTypes::arrangeCXXMethodType(const CXXRecordDecl *RD, |
| 156 | const FunctionProtoType *FTP) { |
| 157 | SmallVector<CanQualType, 16> argTypes; |
John McCall | 8ee376f | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 158 | |
Anders Carlsson | 2ee3c01 | 2009-10-03 19:43:08 +0000 | [diff] [blame] | 159 | // Add the 'this' pointer. |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 160 | if (RD) |
| 161 | argTypes.push_back(GetThisType(Context, RD)); |
| 162 | else |
| 163 | argTypes.push_back(Context.VoidPtrTy); |
John McCall | 8ee376f | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 164 | |
Alexey Samsonov | e5ef3ca | 2014-08-13 23:55:54 +0000 | [diff] [blame] | 165 | return ::arrangeLLVMFunctionInfo( |
| 166 | *this, true, argTypes, |
| 167 | FTP->getCanonicalTypeUnqualified().getAs<FunctionProtoType>()); |
Anders Carlsson | 2ee3c01 | 2009-10-03 19:43:08 +0000 | [diff] [blame] | 168 | } |
| 169 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 170 | /// Arrange the argument and result information for a declaration or |
| 171 | /// definition of the given C++ non-static member function. The |
| 172 | /// member function must be an ordinary function, i.e. not a |
| 173 | /// constructor or destructor. |
| 174 | const CGFunctionInfo & |
| 175 | CodeGenTypes::arrangeCXXMethodDeclaration(const CXXMethodDecl *MD) { |
Benjamin Kramer | 60509af | 2013-09-09 14:48:42 +0000 | [diff] [blame] | 176 | assert(!isa<CXXConstructorDecl>(MD) && "wrong method for constructors!"); |
John McCall | 0d635f5 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 177 | assert(!isa<CXXDestructorDecl>(MD) && "wrong method for destructors!"); |
| 178 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 179 | CanQual<FunctionProtoType> prototype = GetFormalType(MD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 180 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 181 | if (MD->isInstance()) { |
| 182 | // The abstract case is perfectly fine. |
Mark Lacey | 5ea993b | 2013-10-02 20:35:23 +0000 | [diff] [blame] | 183 | const CXXRecordDecl *ThisType = TheCXXABI.getThisArgumentTypeForMethod(MD); |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 184 | return arrangeCXXMethodType(ThisType, prototype.getTypePtr()); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 185 | } |
| 186 | |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 187 | return arrangeFreeFunctionType(prototype); |
Anders Carlsson | b15b55c | 2009-04-03 22:48:58 +0000 | [diff] [blame] | 188 | } |
| 189 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 190 | const CGFunctionInfo & |
Rafael Espindola | 8d2a19b | 2014-09-08 16:01:27 +0000 | [diff] [blame] | 191 | CodeGenTypes::arrangeCXXStructorDeclaration(const CXXMethodDecl *MD, |
| 192 | StructorType Type) { |
| 193 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 194 | SmallVector<CanQualType, 16> argTypes; |
Rafael Espindola | 8d2a19b | 2014-09-08 16:01:27 +0000 | [diff] [blame] | 195 | argTypes.push_back(GetThisType(Context, MD->getParent())); |
Stephen Lin | 9dc6eef | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 196 | |
Rafael Espindola | 8d2a19b | 2014-09-08 16:01:27 +0000 | [diff] [blame] | 197 | GlobalDecl GD; |
| 198 | if (auto *CD = dyn_cast<CXXConstructorDecl>(MD)) { |
| 199 | GD = GlobalDecl(CD, toCXXCtorType(Type)); |
| 200 | } else { |
| 201 | auto *DD = dyn_cast<CXXDestructorDecl>(MD); |
| 202 | GD = GlobalDecl(DD, toCXXDtorType(Type)); |
| 203 | } |
Anders Carlsson | 82ba57c | 2009-11-25 03:15:49 +0000 | [diff] [blame] | 204 | |
Rafael Espindola | 8d2a19b | 2014-09-08 16:01:27 +0000 | [diff] [blame] | 205 | CanQual<FunctionProtoType> FTP = GetFormalType(MD); |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 206 | |
| 207 | // Add the formal parameters. |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 208 | for (unsigned i = 0, e = FTP->getNumParams(); i != e; ++i) |
| 209 | argTypes.push_back(FTP->getParamType(i)); |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 210 | |
Rafael Espindola | 8d2a19b | 2014-09-08 16:01:27 +0000 | [diff] [blame] | 211 | TheCXXABI.buildStructorSignature(MD, Type, argTypes); |
Reid Kleckner | 89077a1 | 2013-12-17 19:46:40 +0000 | [diff] [blame] | 212 | |
| 213 | RequiredArgs required = |
Rafael Espindola | 8d2a19b | 2014-09-08 16:01:27 +0000 | [diff] [blame] | 214 | (MD->isVariadic() ? RequiredArgs(argTypes.size()) : RequiredArgs::All); |
Reid Kleckner | 89077a1 | 2013-12-17 19:46:40 +0000 | [diff] [blame] | 215 | |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 216 | FunctionType::ExtInfo extInfo = FTP->getExtInfo(); |
David Majnemer | 0c0b6d9 | 2014-10-31 20:09:12 +0000 | [diff] [blame] | 217 | CanQualType resultType = TheCXXABI.HasThisReturn(GD) |
| 218 | ? argTypes.front() |
| 219 | : TheCXXABI.hasMostDerivedReturn(GD) |
| 220 | ? CGM.getContext().VoidPtrTy |
| 221 | : Context.VoidTy; |
Reid Kleckner | 4982b82 | 2014-01-31 22:54:50 +0000 | [diff] [blame] | 222 | return arrangeLLVMFunctionInfo(resultType, true, argTypes, extInfo, required); |
Anders Carlsson | 82ba57c | 2009-11-25 03:15:49 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 225 | /// Arrange a call to a C++ method, passing the given arguments. |
| 226 | const CGFunctionInfo & |
| 227 | CodeGenTypes::arrangeCXXConstructorCall(const CallArgList &args, |
| 228 | const CXXConstructorDecl *D, |
| 229 | CXXCtorType CtorKind, |
| 230 | unsigned ExtraArgs) { |
| 231 | // FIXME: Kill copy. |
| 232 | SmallVector<CanQualType, 16> ArgTypes; |
Alexey Samsonov | 3551e31 | 2014-08-13 20:06:24 +0000 | [diff] [blame] | 233 | for (const auto &Arg : args) |
| 234 | ArgTypes.push_back(Context.getCanonicalParamType(Arg.Ty)); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 235 | |
| 236 | CanQual<FunctionProtoType> FPT = GetFormalType(D); |
| 237 | RequiredArgs Required = RequiredArgs::forPrototypePlus(FPT, 1 + ExtraArgs); |
| 238 | GlobalDecl GD(D, CtorKind); |
David Majnemer | 0c0b6d9 | 2014-10-31 20:09:12 +0000 | [diff] [blame] | 239 | CanQualType ResultType = TheCXXABI.HasThisReturn(GD) |
| 240 | ? ArgTypes.front() |
| 241 | : TheCXXABI.hasMostDerivedReturn(GD) |
| 242 | ? CGM.getContext().VoidPtrTy |
| 243 | : Context.VoidTy; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 244 | |
| 245 | FunctionType::ExtInfo Info = FPT->getExtInfo(); |
| 246 | return arrangeLLVMFunctionInfo(ResultType, true, ArgTypes, Info, Required); |
| 247 | } |
| 248 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 249 | /// Arrange the argument and result information for the declaration or |
| 250 | /// definition of the given function. |
| 251 | const CGFunctionInfo & |
| 252 | CodeGenTypes::arrangeFunctionDeclaration(const FunctionDecl *FD) { |
Chris Lattner | bea5b62 | 2009-05-12 20:27:19 +0000 | [diff] [blame] | 253 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) |
Anders Carlsson | b15b55c | 2009-04-03 22:48:58 +0000 | [diff] [blame] | 254 | if (MD->isInstance()) |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 255 | return arrangeCXXMethodDeclaration(MD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 256 | |
John McCall | 2da83a3 | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 257 | CanQualType FTy = FD->getType()->getCanonicalTypeUnqualified(); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 258 | |
John McCall | 2da83a3 | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 259 | assert(isa<FunctionType>(FTy)); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 260 | |
| 261 | // When declaring a function without a prototype, always use a |
| 262 | // non-variadic type. |
| 263 | if (isa<FunctionNoProtoType>(FTy)) { |
| 264 | CanQual<FunctionNoProtoType> noProto = FTy.getAs<FunctionNoProtoType>(); |
Reid Kleckner | 4982b82 | 2014-01-31 22:54:50 +0000 | [diff] [blame] | 265 | return arrangeLLVMFunctionInfo(noProto->getReturnType(), false, None, |
Dmitri Gribenko | 44ebbd5 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 266 | noProto->getExtInfo(), RequiredArgs::All); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 267 | } |
| 268 | |
John McCall | 2da83a3 | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 269 | assert(isa<FunctionProtoType>(FTy)); |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 270 | return arrangeFreeFunctionType(FTy.getAs<FunctionProtoType>()); |
Daniel Dunbar | 3d7c90b | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 271 | } |
| 272 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 273 | /// Arrange the argument and result information for the declaration or |
| 274 | /// definition of an Objective-C method. |
| 275 | const CGFunctionInfo & |
| 276 | CodeGenTypes::arrangeObjCMethodDeclaration(const ObjCMethodDecl *MD) { |
| 277 | // It happens that this is the same as a call with no optional |
| 278 | // arguments, except also using the formal 'self' type. |
| 279 | return arrangeObjCMessageSendSignature(MD, MD->getSelfDecl()->getType()); |
| 280 | } |
| 281 | |
| 282 | /// Arrange the argument and result information for the function type |
| 283 | /// through which to perform a send to the given Objective-C method, |
| 284 | /// using the given receiver type. The receiver type is not always |
| 285 | /// the 'self' type of the method or even an Objective-C pointer type. |
| 286 | /// This is *not* the right method for actually performing such a |
| 287 | /// message send, due to the possibility of optional arguments. |
| 288 | const CGFunctionInfo & |
| 289 | CodeGenTypes::arrangeObjCMessageSendSignature(const ObjCMethodDecl *MD, |
| 290 | QualType receiverType) { |
| 291 | SmallVector<CanQualType, 16> argTys; |
| 292 | argTys.push_back(Context.getCanonicalParamType(receiverType)); |
| 293 | argTys.push_back(Context.getCanonicalParamType(Context.getObjCSelType())); |
Daniel Dunbar | bf8c24a | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 294 | // FIXME: Kill copy? |
Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 295 | for (const auto *I : MD->params()) { |
| 296 | argTys.push_back(Context.getCanonicalParamType(I->getType())); |
John McCall | 8ee376f | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 297 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 298 | |
| 299 | FunctionType::ExtInfo einfo; |
Aaron Ballman | 0362a6d | 2013-12-18 16:23:37 +0000 | [diff] [blame] | 300 | bool IsWindows = getContext().getTargetInfo().getTriple().isOSWindows(); |
| 301 | einfo = einfo.withCallingConv(getCallingConventionForDecl(MD, IsWindows)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 302 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 303 | if (getContext().getLangOpts().ObjCAutoRefCount && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 304 | MD->hasAttr<NSReturnsRetainedAttr>()) |
| 305 | einfo = einfo.withProducesResult(true); |
| 306 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 307 | RequiredArgs required = |
| 308 | (MD->isVariadic() ? RequiredArgs(argTys.size()) : RequiredArgs::All); |
| 309 | |
Reid Kleckner | 4982b82 | 2014-01-31 22:54:50 +0000 | [diff] [blame] | 310 | return arrangeLLVMFunctionInfo(GetReturnType(MD->getReturnType()), false, |
| 311 | argTys, einfo, required); |
Daniel Dunbar | 3d7c90b | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 312 | } |
| 313 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 314 | const CGFunctionInfo & |
| 315 | CodeGenTypes::arrangeGlobalDeclaration(GlobalDecl GD) { |
Anders Carlsson | 6710c53 | 2010-02-06 02:44:09 +0000 | [diff] [blame] | 316 | // FIXME: Do we need to handle ObjCMethodDecl? |
| 317 | const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl()); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 318 | |
Anders Carlsson | 6710c53 | 2010-02-06 02:44:09 +0000 | [diff] [blame] | 319 | if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) |
Rafael Espindola | 8d2a19b | 2014-09-08 16:01:27 +0000 | [diff] [blame] | 320 | return arrangeCXXStructorDeclaration(CD, getFromCtorType(GD.getCtorType())); |
Anders Carlsson | 6710c53 | 2010-02-06 02:44:09 +0000 | [diff] [blame] | 321 | |
| 322 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD)) |
Rafael Espindola | 8d2a19b | 2014-09-08 16:01:27 +0000 | [diff] [blame] | 323 | return arrangeCXXStructorDeclaration(DD, getFromDtorType(GD.getDtorType())); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 324 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 325 | return arrangeFunctionDeclaration(FD); |
Anders Carlsson | 6710c53 | 2010-02-06 02:44:09 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Reid Kleckner | c347351 | 2014-08-29 21:43:29 +0000 | [diff] [blame] | 328 | /// Arrange a thunk that takes 'this' as the first parameter followed by |
| 329 | /// varargs. Return a void pointer, regardless of the actual return type. |
| 330 | /// The body of the thunk will end in a musttail call to a function of the |
| 331 | /// correct type, and the caller will bitcast the function to the correct |
| 332 | /// prototype. |
| 333 | const CGFunctionInfo & |
| 334 | CodeGenTypes::arrangeMSMemberPointerThunk(const CXXMethodDecl *MD) { |
| 335 | assert(MD->isVirtual() && "only virtual memptrs have thunks"); |
| 336 | CanQual<FunctionProtoType> FTP = GetFormalType(MD); |
| 337 | CanQualType ArgTys[] = { GetThisType(Context, MD->getParent()) }; |
| 338 | return arrangeLLVMFunctionInfo(Context.VoidTy, false, ArgTys, |
| 339 | FTP->getExtInfo(), RequiredArgs(1)); |
| 340 | } |
| 341 | |
John McCall | c818bbb | 2012-12-07 07:03:17 +0000 | [diff] [blame] | 342 | /// Arrange a call as unto a free function, except possibly with an |
| 343 | /// additional number of formal parameters considered required. |
| 344 | static const CGFunctionInfo & |
| 345 | arrangeFreeFunctionLikeCall(CodeGenTypes &CGT, |
Mark Lacey | 2345575 | 2013-10-10 20:57:00 +0000 | [diff] [blame] | 346 | CodeGenModule &CGM, |
John McCall | c818bbb | 2012-12-07 07:03:17 +0000 | [diff] [blame] | 347 | const CallArgList &args, |
| 348 | const FunctionType *fnType, |
| 349 | unsigned numExtraRequiredArgs) { |
| 350 | assert(args.size() >= numExtraRequiredArgs); |
| 351 | |
| 352 | // In most cases, there are no optional arguments. |
| 353 | RequiredArgs required = RequiredArgs::All; |
| 354 | |
| 355 | // If we have a variadic prototype, the required arguments are the |
| 356 | // extra prefix plus the arguments in the prototype. |
| 357 | if (const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(fnType)) { |
| 358 | if (proto->isVariadic()) |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 359 | required = RequiredArgs(proto->getNumParams() + numExtraRequiredArgs); |
John McCall | c818bbb | 2012-12-07 07:03:17 +0000 | [diff] [blame] | 360 | |
| 361 | // If we don't have a prototype at all, but we're supposed to |
| 362 | // explicitly use the variadic convention for unprototyped calls, |
| 363 | // treat all of the arguments as required but preserve the nominal |
| 364 | // possibility of variadics. |
Mark Lacey | 2345575 | 2013-10-10 20:57:00 +0000 | [diff] [blame] | 365 | } else if (CGM.getTargetCodeGenInfo() |
| 366 | .isNoProtoCallVariadic(args, |
| 367 | cast<FunctionNoProtoType>(fnType))) { |
John McCall | c818bbb | 2012-12-07 07:03:17 +0000 | [diff] [blame] | 368 | required = RequiredArgs(args.size()); |
| 369 | } |
| 370 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 371 | return CGT.arrangeFreeFunctionCall(fnType->getReturnType(), args, |
John McCall | c818bbb | 2012-12-07 07:03:17 +0000 | [diff] [blame] | 372 | fnType->getExtInfo(), required); |
| 373 | } |
| 374 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 375 | /// Figure out the rules for calling a function with the given formal |
| 376 | /// type using the given arguments. The arguments are necessary |
| 377 | /// because the function might be unprototyped, in which case it's |
| 378 | /// target-dependent in crazy ways. |
| 379 | const CGFunctionInfo & |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 380 | CodeGenTypes::arrangeFreeFunctionCall(const CallArgList &args, |
| 381 | const FunctionType *fnType) { |
Mark Lacey | 2345575 | 2013-10-10 20:57:00 +0000 | [diff] [blame] | 382 | return arrangeFreeFunctionLikeCall(*this, CGM, args, fnType, 0); |
John McCall | c818bbb | 2012-12-07 07:03:17 +0000 | [diff] [blame] | 383 | } |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 384 | |
John McCall | c818bbb | 2012-12-07 07:03:17 +0000 | [diff] [blame] | 385 | /// A block function call is essentially a free-function call with an |
| 386 | /// extra implicit argument. |
| 387 | const CGFunctionInfo & |
| 388 | CodeGenTypes::arrangeBlockFunctionCall(const CallArgList &args, |
| 389 | const FunctionType *fnType) { |
Mark Lacey | 2345575 | 2013-10-10 20:57:00 +0000 | [diff] [blame] | 390 | return arrangeFreeFunctionLikeCall(*this, CGM, args, fnType, 1); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | const CGFunctionInfo & |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 394 | CodeGenTypes::arrangeFreeFunctionCall(QualType resultType, |
| 395 | const CallArgList &args, |
| 396 | FunctionType::ExtInfo info, |
| 397 | RequiredArgs required) { |
Daniel Dunbar | bf8c24a | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 398 | // FIXME: Kill copy. |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 399 | SmallVector<CanQualType, 16> argTypes; |
Alexey Samsonov | 3551e31 | 2014-08-13 20:06:24 +0000 | [diff] [blame] | 400 | for (const auto &Arg : args) |
| 401 | argTypes.push_back(Context.getCanonicalParamType(Arg.Ty)); |
Reid Kleckner | 4982b82 | 2014-01-31 22:54:50 +0000 | [diff] [blame] | 402 | return arrangeLLVMFunctionInfo(GetReturnType(resultType), false, argTypes, |
| 403 | info, required); |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | /// Arrange a call to a C++ method, passing the given arguments. |
| 407 | const CGFunctionInfo & |
| 408 | CodeGenTypes::arrangeCXXMethodCall(const CallArgList &args, |
| 409 | const FunctionProtoType *FPT, |
| 410 | RequiredArgs required) { |
| 411 | // FIXME: Kill copy. |
| 412 | SmallVector<CanQualType, 16> argTypes; |
Alexey Samsonov | 3551e31 | 2014-08-13 20:06:24 +0000 | [diff] [blame] | 413 | for (const auto &Arg : args) |
| 414 | argTypes.push_back(Context.getCanonicalParamType(Arg.Ty)); |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 415 | |
| 416 | FunctionType::ExtInfo info = FPT->getExtInfo(); |
Reid Kleckner | 4982b82 | 2014-01-31 22:54:50 +0000 | [diff] [blame] | 417 | return arrangeLLVMFunctionInfo(GetReturnType(FPT->getReturnType()), true, |
| 418 | argTypes, info, required); |
Daniel Dunbar | 3cd2063 | 2009-01-31 02:19:00 +0000 | [diff] [blame] | 419 | } |
| 420 | |
Reid Kleckner | 4982b82 | 2014-01-31 22:54:50 +0000 | [diff] [blame] | 421 | const CGFunctionInfo &CodeGenTypes::arrangeFreeFunctionDeclaration( |
| 422 | QualType resultType, const FunctionArgList &args, |
| 423 | const FunctionType::ExtInfo &info, bool isVariadic) { |
Daniel Dunbar | bf8c24a | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 424 | // FIXME: Kill copy. |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 425 | SmallVector<CanQualType, 16> argTypes; |
Alexey Samsonov | 3551e31 | 2014-08-13 20:06:24 +0000 | [diff] [blame] | 426 | for (auto Arg : args) |
| 427 | argTypes.push_back(Context.getCanonicalParamType(Arg->getType())); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 428 | |
| 429 | RequiredArgs required = |
| 430 | (isVariadic ? RequiredArgs(args.size()) : RequiredArgs::All); |
Reid Kleckner | 4982b82 | 2014-01-31 22:54:50 +0000 | [diff] [blame] | 431 | return arrangeLLVMFunctionInfo(GetReturnType(resultType), false, argTypes, info, |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 432 | required); |
Daniel Dunbar | bf8c24a | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 433 | } |
| 434 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 435 | const CGFunctionInfo &CodeGenTypes::arrangeNullaryFunction() { |
Reid Kleckner | 4982b82 | 2014-01-31 22:54:50 +0000 | [diff] [blame] | 436 | return arrangeLLVMFunctionInfo(getContext().VoidTy, false, None, |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 437 | FunctionType::ExtInfo(), RequiredArgs::All); |
John McCall | a738c25 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 438 | } |
| 439 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 440 | /// Arrange the argument and result information for an abstract value |
| 441 | /// of a given function type. This is the method which all of the |
| 442 | /// above functions ultimately defer to. |
| 443 | const CGFunctionInfo & |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 444 | CodeGenTypes::arrangeLLVMFunctionInfo(CanQualType resultType, |
Reid Kleckner | 4982b82 | 2014-01-31 22:54:50 +0000 | [diff] [blame] | 445 | bool IsInstanceMethod, |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 446 | ArrayRef<CanQualType> argTypes, |
| 447 | FunctionType::ExtInfo info, |
| 448 | RequiredArgs required) { |
Saleem Abdulrasool | 32d1a96 | 2014-11-25 03:49:50 +0000 | [diff] [blame] | 449 | assert(std::all_of(argTypes.begin(), argTypes.end(), |
| 450 | std::mem_fun_ref(&CanQualType::isCanonicalAsParam))); |
John McCall | 2da83a3 | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 451 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 452 | unsigned CC = ClangCallConvToLLVMCallConv(info.getCC()); |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 453 | |
Daniel Dunbar | e0be829 | 2009-02-03 00:07:12 +0000 | [diff] [blame] | 454 | // Lookup or create unique function info. |
| 455 | llvm::FoldingSetNodeID ID; |
Reid Kleckner | 4982b82 | 2014-01-31 22:54:50 +0000 | [diff] [blame] | 456 | CGFunctionInfo::Profile(ID, IsInstanceMethod, info, required, resultType, |
| 457 | argTypes); |
Daniel Dunbar | e0be829 | 2009-02-03 00:07:12 +0000 | [diff] [blame] | 458 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 459 | void *insertPos = nullptr; |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 460 | CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, insertPos); |
Daniel Dunbar | e0be829 | 2009-02-03 00:07:12 +0000 | [diff] [blame] | 461 | if (FI) |
| 462 | return *FI; |
| 463 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 464 | // Construct the function info. We co-allocate the ArgInfos. |
Reid Kleckner | 4982b82 | 2014-01-31 22:54:50 +0000 | [diff] [blame] | 465 | FI = CGFunctionInfo::create(CC, IsInstanceMethod, info, resultType, argTypes, |
| 466 | required); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 467 | FunctionInfos.InsertNode(FI, insertPos); |
Daniel Dunbar | 313321e | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 468 | |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 469 | bool inserted = FunctionsBeingProcessed.insert(FI).second; |
| 470 | (void)inserted; |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 471 | assert(inserted && "Recursively being processed?"); |
Chris Lattner | 6fb0ccf | 2011-07-15 05:16:14 +0000 | [diff] [blame] | 472 | |
Daniel Dunbar | 313321e | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 473 | // Compute ABI information. |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 474 | getABIInfo().computeInfo(*FI); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 475 | |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 476 | // Loop over all of the computed argument and return value info. If any of |
| 477 | // them are direct or extend without a specified coerce type, specify the |
| 478 | // default now. |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 479 | ABIArgInfo &retInfo = FI->getReturnInfo(); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 480 | if (retInfo.canHaveCoerceToType() && retInfo.getCoerceToType() == nullptr) |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 481 | retInfo.setCoerceToType(ConvertType(FI->getReturnType())); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 482 | |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 483 | for (auto &I : FI->arguments()) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 484 | if (I.info.canHaveCoerceToType() && I.info.getCoerceToType() == nullptr) |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 485 | I.info.setCoerceToType(ConvertType(I.type)); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 486 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 487 | bool erased = FunctionsBeingProcessed.erase(FI); (void)erased; |
| 488 | assert(erased && "Not in set?"); |
Chris Lattner | 1a65133 | 2011-07-15 06:41:05 +0000 | [diff] [blame] | 489 | |
Daniel Dunbar | e0be829 | 2009-02-03 00:07:12 +0000 | [diff] [blame] | 490 | return *FI; |
Daniel Dunbar | bf8c24a | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 491 | } |
| 492 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 493 | CGFunctionInfo *CGFunctionInfo::create(unsigned llvmCC, |
Reid Kleckner | 4982b82 | 2014-01-31 22:54:50 +0000 | [diff] [blame] | 494 | bool IsInstanceMethod, |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 495 | const FunctionType::ExtInfo &info, |
| 496 | CanQualType resultType, |
| 497 | ArrayRef<CanQualType> argTypes, |
| 498 | RequiredArgs required) { |
| 499 | void *buffer = operator new(sizeof(CGFunctionInfo) + |
| 500 | sizeof(ArgInfo) * (argTypes.size() + 1)); |
| 501 | CGFunctionInfo *FI = new(buffer) CGFunctionInfo(); |
| 502 | FI->CallingConvention = llvmCC; |
| 503 | FI->EffectiveCallingConvention = llvmCC; |
| 504 | FI->ASTCallingConvention = info.getCC(); |
Reid Kleckner | 4982b82 | 2014-01-31 22:54:50 +0000 | [diff] [blame] | 505 | FI->InstanceMethod = IsInstanceMethod; |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 506 | FI->NoReturn = info.getNoReturn(); |
| 507 | FI->ReturnsRetained = info.getProducesResult(); |
| 508 | FI->Required = required; |
| 509 | FI->HasRegParm = info.getHasRegParm(); |
| 510 | FI->RegParm = info.getRegParm(); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 511 | FI->ArgStruct = nullptr; |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 512 | FI->NumArgs = argTypes.size(); |
| 513 | FI->getArgsBuffer()[0].type = resultType; |
| 514 | for (unsigned i = 0, e = argTypes.size(); i != e; ++i) |
| 515 | FI->getArgsBuffer()[i + 1].type = argTypes[i]; |
| 516 | return FI; |
Daniel Dunbar | 313321e | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | /***/ |
| 520 | |
Alexey Samsonov | 8a0bad0 | 2014-09-29 18:41:28 +0000 | [diff] [blame] | 521 | namespace { |
| 522 | // ABIArgInfo::Expand implementation. |
| 523 | |
| 524 | // Specifies the way QualType passed as ABIArgInfo::Expand is expanded. |
| 525 | struct TypeExpansion { |
| 526 | enum TypeExpansionKind { |
| 527 | // Elements of constant arrays are expanded recursively. |
| 528 | TEK_ConstantArray, |
| 529 | // Record fields are expanded recursively (but if record is a union, only |
| 530 | // the field with the largest size is expanded). |
| 531 | TEK_Record, |
| 532 | // For complex types, real and imaginary parts are expanded recursively. |
| 533 | TEK_Complex, |
| 534 | // All other types are not expandable. |
| 535 | TEK_None |
| 536 | }; |
| 537 | |
| 538 | const TypeExpansionKind Kind; |
| 539 | |
| 540 | TypeExpansion(TypeExpansionKind K) : Kind(K) {} |
| 541 | virtual ~TypeExpansion() {} |
| 542 | }; |
| 543 | |
| 544 | struct ConstantArrayExpansion : TypeExpansion { |
| 545 | QualType EltTy; |
| 546 | uint64_t NumElts; |
| 547 | |
| 548 | ConstantArrayExpansion(QualType EltTy, uint64_t NumElts) |
| 549 | : TypeExpansion(TEK_ConstantArray), EltTy(EltTy), NumElts(NumElts) {} |
| 550 | static bool classof(const TypeExpansion *TE) { |
| 551 | return TE->Kind == TEK_ConstantArray; |
| 552 | } |
| 553 | }; |
| 554 | |
| 555 | struct RecordExpansion : TypeExpansion { |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 556 | SmallVector<const CXXBaseSpecifier *, 1> Bases; |
| 557 | |
Alexey Samsonov | 8a0bad0 | 2014-09-29 18:41:28 +0000 | [diff] [blame] | 558 | SmallVector<const FieldDecl *, 1> Fields; |
| 559 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 560 | RecordExpansion(SmallVector<const CXXBaseSpecifier *, 1> &&Bases, |
| 561 | SmallVector<const FieldDecl *, 1> &&Fields) |
| 562 | : TypeExpansion(TEK_Record), Bases(Bases), Fields(Fields) {} |
Alexey Samsonov | 8a0bad0 | 2014-09-29 18:41:28 +0000 | [diff] [blame] | 563 | static bool classof(const TypeExpansion *TE) { |
| 564 | return TE->Kind == TEK_Record; |
| 565 | } |
| 566 | }; |
| 567 | |
| 568 | struct ComplexExpansion : TypeExpansion { |
| 569 | QualType EltTy; |
| 570 | |
| 571 | ComplexExpansion(QualType EltTy) : TypeExpansion(TEK_Complex), EltTy(EltTy) {} |
| 572 | static bool classof(const TypeExpansion *TE) { |
| 573 | return TE->Kind == TEK_Complex; |
| 574 | } |
| 575 | }; |
| 576 | |
| 577 | struct NoExpansion : TypeExpansion { |
| 578 | NoExpansion() : TypeExpansion(TEK_None) {} |
| 579 | static bool classof(const TypeExpansion *TE) { |
| 580 | return TE->Kind == TEK_None; |
| 581 | } |
| 582 | }; |
| 583 | } // namespace |
| 584 | |
| 585 | static std::unique_ptr<TypeExpansion> |
| 586 | getTypeExpansion(QualType Ty, const ASTContext &Context) { |
| 587 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { |
| 588 | return llvm::make_unique<ConstantArrayExpansion>( |
| 589 | AT->getElementType(), AT->getSize().getZExtValue()); |
| 590 | } |
| 591 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 592 | SmallVector<const CXXBaseSpecifier *, 1> Bases; |
Alexey Samsonov | 8a0bad0 | 2014-09-29 18:41:28 +0000 | [diff] [blame] | 593 | SmallVector<const FieldDecl *, 1> Fields; |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 594 | const RecordDecl *RD = RT->getDecl(); |
| 595 | assert(!RD->hasFlexibleArrayMember() && |
| 596 | "Cannot expand structure with flexible array."); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 597 | if (RD->isUnion()) { |
| 598 | // Unions can be here only in degenerative cases - all the fields are same |
| 599 | // after flattening. Thus we have to use the "largest" field. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 600 | const FieldDecl *LargestFD = nullptr; |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 601 | CharUnits UnionSize = CharUnits::Zero(); |
| 602 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 603 | for (const auto *FD : RD->fields()) { |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 604 | // Skip zero length bitfields. |
| 605 | if (FD->isBitField() && FD->getBitWidthValue(Context) == 0) |
| 606 | continue; |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 607 | assert(!FD->isBitField() && |
| 608 | "Cannot expand structure with bit-field members."); |
Alexey Samsonov | 8a0bad0 | 2014-09-29 18:41:28 +0000 | [diff] [blame] | 609 | CharUnits FieldSize = Context.getTypeSizeInChars(FD->getType()); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 610 | if (UnionSize < FieldSize) { |
| 611 | UnionSize = FieldSize; |
| 612 | LargestFD = FD; |
| 613 | } |
| 614 | } |
| 615 | if (LargestFD) |
Alexey Samsonov | 8a0bad0 | 2014-09-29 18:41:28 +0000 | [diff] [blame] | 616 | Fields.push_back(LargestFD); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 617 | } else { |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 618 | if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 619 | assert(!CXXRD->isDynamicClass() && |
| 620 | "cannot expand vtable pointers in dynamic classes"); |
| 621 | for (const CXXBaseSpecifier &BS : CXXRD->bases()) |
| 622 | Bases.push_back(&BS); |
| 623 | } |
| 624 | |
Alexey Samsonov | 8a0bad0 | 2014-09-29 18:41:28 +0000 | [diff] [blame] | 625 | for (const auto *FD : RD->fields()) { |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 626 | // Skip zero length bitfields. |
| 627 | if (FD->isBitField() && FD->getBitWidthValue(Context) == 0) |
| 628 | continue; |
Alexey Samsonov | 8a0bad0 | 2014-09-29 18:41:28 +0000 | [diff] [blame] | 629 | assert(!FD->isBitField() && |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 630 | "Cannot expand structure with bit-field members."); |
Alexey Samsonov | 8a0bad0 | 2014-09-29 18:41:28 +0000 | [diff] [blame] | 631 | Fields.push_back(FD); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 632 | } |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 633 | } |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 634 | return llvm::make_unique<RecordExpansion>(std::move(Bases), |
| 635 | std::move(Fields)); |
Alexey Samsonov | 8a0bad0 | 2014-09-29 18:41:28 +0000 | [diff] [blame] | 636 | } |
| 637 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
| 638 | return llvm::make_unique<ComplexExpansion>(CT->getElementType()); |
| 639 | } |
| 640 | return llvm::make_unique<NoExpansion>(); |
| 641 | } |
| 642 | |
Alexey Samsonov | 52c0f6a | 2014-09-29 20:30:22 +0000 | [diff] [blame] | 643 | static int getExpansionSize(QualType Ty, const ASTContext &Context) { |
| 644 | auto Exp = getTypeExpansion(Ty, Context); |
| 645 | if (auto CAExp = dyn_cast<ConstantArrayExpansion>(Exp.get())) { |
| 646 | return CAExp->NumElts * getExpansionSize(CAExp->EltTy, Context); |
| 647 | } |
| 648 | if (auto RExp = dyn_cast<RecordExpansion>(Exp.get())) { |
| 649 | int Res = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 650 | for (auto BS : RExp->Bases) |
| 651 | Res += getExpansionSize(BS->getType(), Context); |
Alexey Samsonov | 52c0f6a | 2014-09-29 20:30:22 +0000 | [diff] [blame] | 652 | for (auto FD : RExp->Fields) |
| 653 | Res += getExpansionSize(FD->getType(), Context); |
| 654 | return Res; |
| 655 | } |
| 656 | if (isa<ComplexExpansion>(Exp.get())) |
| 657 | return 2; |
| 658 | assert(isa<NoExpansion>(Exp.get())); |
| 659 | return 1; |
| 660 | } |
| 661 | |
Alexey Samsonov | 153004f | 2014-09-29 22:08:00 +0000 | [diff] [blame] | 662 | void |
| 663 | CodeGenTypes::getExpandedTypes(QualType Ty, |
| 664 | SmallVectorImpl<llvm::Type *>::iterator &TI) { |
| 665 | auto Exp = getTypeExpansion(Ty, Context); |
Alexey Samsonov | 8a0bad0 | 2014-09-29 18:41:28 +0000 | [diff] [blame] | 666 | if (auto CAExp = dyn_cast<ConstantArrayExpansion>(Exp.get())) { |
| 667 | for (int i = 0, n = CAExp->NumElts; i < n; i++) { |
Alexey Samsonov | 153004f | 2014-09-29 22:08:00 +0000 | [diff] [blame] | 668 | getExpandedTypes(CAExp->EltTy, TI); |
Alexey Samsonov | 8a0bad0 | 2014-09-29 18:41:28 +0000 | [diff] [blame] | 669 | } |
| 670 | } else if (auto RExp = dyn_cast<RecordExpansion>(Exp.get())) { |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 671 | for (auto BS : RExp->Bases) |
| 672 | getExpandedTypes(BS->getType(), TI); |
| 673 | for (auto FD : RExp->Fields) |
Alexey Samsonov | 153004f | 2014-09-29 22:08:00 +0000 | [diff] [blame] | 674 | getExpandedTypes(FD->getType(), TI); |
Alexey Samsonov | 8a0bad0 | 2014-09-29 18:41:28 +0000 | [diff] [blame] | 675 | } else if (auto CExp = dyn_cast<ComplexExpansion>(Exp.get())) { |
| 676 | llvm::Type *EltTy = ConvertType(CExp->EltTy); |
Alexey Samsonov | 153004f | 2014-09-29 22:08:00 +0000 | [diff] [blame] | 677 | *TI++ = EltTy; |
| 678 | *TI++ = EltTy; |
Alexey Samsonov | 8a0bad0 | 2014-09-29 18:41:28 +0000 | [diff] [blame] | 679 | } else { |
| 680 | assert(isa<NoExpansion>(Exp.get())); |
Alexey Samsonov | 153004f | 2014-09-29 22:08:00 +0000 | [diff] [blame] | 681 | *TI++ = ConvertType(Ty); |
Alexey Samsonov | 8a0bad0 | 2014-09-29 18:41:28 +0000 | [diff] [blame] | 682 | } |
Daniel Dunbar | 8fc81b0 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 683 | } |
| 684 | |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 685 | void CodeGenFunction::ExpandTypeFromArgs( |
| 686 | QualType Ty, LValue LV, SmallVectorImpl<llvm::Argument *>::iterator &AI) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 687 | assert(LV.isSimple() && |
| 688 | "Unexpected non-simple lvalue during struct expansion."); |
Daniel Dunbar | 8fc81b0 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 689 | |
Alexey Samsonov | 8a0bad0 | 2014-09-29 18:41:28 +0000 | [diff] [blame] | 690 | auto Exp = getTypeExpansion(Ty, getContext()); |
| 691 | if (auto CAExp = dyn_cast<ConstantArrayExpansion>(Exp.get())) { |
| 692 | for (int i = 0, n = CAExp->NumElts; i < n; i++) { |
| 693 | llvm::Value *EltAddr = Builder.CreateConstGEP2_32(LV.getAddress(), 0, i); |
| 694 | LValue LV = MakeAddrLValue(EltAddr, CAExp->EltTy); |
| 695 | ExpandTypeFromArgs(CAExp->EltTy, LV, AI); |
Daniel Dunbar | 8fc81b0 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 696 | } |
Alexey Samsonov | 8a0bad0 | 2014-09-29 18:41:28 +0000 | [diff] [blame] | 697 | } else if (auto RExp = dyn_cast<RecordExpansion>(Exp.get())) { |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 698 | llvm::Value *This = LV.getAddress(); |
| 699 | for (const CXXBaseSpecifier *BS : RExp->Bases) { |
| 700 | // Perform a single step derived-to-base conversion. |
| 701 | llvm::Value *Base = |
| 702 | GetAddressOfBaseClass(This, Ty->getAsCXXRecordDecl(), &BS, &BS + 1, |
| 703 | /*NullCheckValue=*/false, SourceLocation()); |
| 704 | LValue SubLV = MakeAddrLValue(Base, BS->getType()); |
| 705 | |
| 706 | // Recurse onto bases. |
| 707 | ExpandTypeFromArgs(BS->getType(), SubLV, AI); |
| 708 | } |
Alexey Samsonov | 8a0bad0 | 2014-09-29 18:41:28 +0000 | [diff] [blame] | 709 | for (auto FD : RExp->Fields) { |
| 710 | // FIXME: What are the right qualifiers here? |
| 711 | LValue SubLV = EmitLValueForField(LV, FD); |
| 712 | ExpandTypeFromArgs(FD->getType(), SubLV, AI); |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 713 | } |
Alexey Samsonov | 8a0bad0 | 2014-09-29 18:41:28 +0000 | [diff] [blame] | 714 | } else if (auto CExp = dyn_cast<ComplexExpansion>(Exp.get())) { |
Eli Friedman | 7f1ff60 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 715 | llvm::Value *RealAddr = Builder.CreateStructGEP(LV.getAddress(), 0, "real"); |
Alexey Samsonov | 8a0bad0 | 2014-09-29 18:41:28 +0000 | [diff] [blame] | 716 | EmitStoreThroughLValue(RValue::get(*AI++), |
| 717 | MakeAddrLValue(RealAddr, CExp->EltTy)); |
Eli Friedman | 7f1ff60 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 718 | llvm::Value *ImagAddr = Builder.CreateStructGEP(LV.getAddress(), 1, "imag"); |
Alexey Samsonov | 8a0bad0 | 2014-09-29 18:41:28 +0000 | [diff] [blame] | 719 | EmitStoreThroughLValue(RValue::get(*AI++), |
| 720 | MakeAddrLValue(ImagAddr, CExp->EltTy)); |
| 721 | } else { |
| 722 | assert(isa<NoExpansion>(Exp.get())); |
| 723 | EmitStoreThroughLValue(RValue::get(*AI++), LV); |
Daniel Dunbar | 8fc81b0 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 724 | } |
Alexey Samsonov | 8a0bad0 | 2014-09-29 18:41:28 +0000 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | void CodeGenFunction::ExpandTypeToArgs( |
| 728 | QualType Ty, RValue RV, llvm::FunctionType *IRFuncTy, |
| 729 | SmallVectorImpl<llvm::Value *> &IRCallArgs, unsigned &IRCallArgPos) { |
| 730 | auto Exp = getTypeExpansion(Ty, getContext()); |
| 731 | if (auto CAExp = dyn_cast<ConstantArrayExpansion>(Exp.get())) { |
| 732 | llvm::Value *Addr = RV.getAggregateAddr(); |
| 733 | for (int i = 0, n = CAExp->NumElts; i < n; i++) { |
| 734 | llvm::Value *EltAddr = Builder.CreateConstGEP2_32(Addr, 0, i); |
| 735 | RValue EltRV = |
| 736 | convertTempToRValue(EltAddr, CAExp->EltTy, SourceLocation()); |
| 737 | ExpandTypeToArgs(CAExp->EltTy, EltRV, IRFuncTy, IRCallArgs, IRCallArgPos); |
| 738 | } |
| 739 | } else if (auto RExp = dyn_cast<RecordExpansion>(Exp.get())) { |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 740 | llvm::Value *This = RV.getAggregateAddr(); |
| 741 | for (const CXXBaseSpecifier *BS : RExp->Bases) { |
| 742 | // Perform a single step derived-to-base conversion. |
| 743 | llvm::Value *Base = |
| 744 | GetAddressOfBaseClass(This, Ty->getAsCXXRecordDecl(), &BS, &BS + 1, |
| 745 | /*NullCheckValue=*/false, SourceLocation()); |
| 746 | RValue BaseRV = RValue::getAggregate(Base); |
| 747 | |
| 748 | // Recurse onto bases. |
| 749 | ExpandTypeToArgs(BS->getType(), BaseRV, IRFuncTy, IRCallArgs, |
| 750 | IRCallArgPos); |
| 751 | } |
| 752 | |
| 753 | LValue LV = MakeAddrLValue(This, Ty); |
Alexey Samsonov | 8a0bad0 | 2014-09-29 18:41:28 +0000 | [diff] [blame] | 754 | for (auto FD : RExp->Fields) { |
| 755 | RValue FldRV = EmitRValueForField(LV, FD, SourceLocation()); |
| 756 | ExpandTypeToArgs(FD->getType(), FldRV, IRFuncTy, IRCallArgs, |
| 757 | IRCallArgPos); |
| 758 | } |
| 759 | } else if (isa<ComplexExpansion>(Exp.get())) { |
| 760 | ComplexPairTy CV = RV.getComplexVal(); |
| 761 | IRCallArgs[IRCallArgPos++] = CV.first; |
| 762 | IRCallArgs[IRCallArgPos++] = CV.second; |
| 763 | } else { |
| 764 | assert(isa<NoExpansion>(Exp.get())); |
| 765 | assert(RV.isScalar() && |
| 766 | "Unexpected non-scalar rvalue during struct expansion."); |
| 767 | |
| 768 | // Insert a bitcast as needed. |
| 769 | llvm::Value *V = RV.getScalarVal(); |
| 770 | if (IRCallArgPos < IRFuncTy->getNumParams() && |
| 771 | V->getType() != IRFuncTy->getParamType(IRCallArgPos)) |
| 772 | V = Builder.CreateBitCast(V, IRFuncTy->getParamType(IRCallArgPos)); |
| 773 | |
| 774 | IRCallArgs[IRCallArgPos++] = V; |
| 775 | } |
Daniel Dunbar | 8fc81b0 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 776 | } |
| 777 | |
Chris Lattner | 895c52b | 2010-06-27 06:04:18 +0000 | [diff] [blame] | 778 | /// EnterStructPointerForCoercedAccess - Given a struct pointer that we are |
Chris Lattner | 1cd6698 | 2010-06-27 05:56:15 +0000 | [diff] [blame] | 779 | /// accessing some number of bytes out of it, try to gep into the struct to get |
| 780 | /// at its inner goodness. Dive as deep as possible without entering an element |
| 781 | /// with an in-memory size smaller than DstSize. |
| 782 | static llvm::Value * |
Chris Lattner | 895c52b | 2010-06-27 06:04:18 +0000 | [diff] [blame] | 783 | EnterStructPointerForCoercedAccess(llvm::Value *SrcPtr, |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 784 | llvm::StructType *SrcSTy, |
Chris Lattner | 895c52b | 2010-06-27 06:04:18 +0000 | [diff] [blame] | 785 | uint64_t DstSize, CodeGenFunction &CGF) { |
Chris Lattner | 1cd6698 | 2010-06-27 05:56:15 +0000 | [diff] [blame] | 786 | // We can't dive into a zero-element struct. |
| 787 | if (SrcSTy->getNumElements() == 0) return SrcPtr; |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 788 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 789 | llvm::Type *FirstElt = SrcSTy->getElementType(0); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 790 | |
Chris Lattner | 1cd6698 | 2010-06-27 05:56:15 +0000 | [diff] [blame] | 791 | // If the first elt is at least as large as what we're looking for, or if the |
James Molloy | 90d6101 | 2014-08-29 10:17:52 +0000 | [diff] [blame] | 792 | // first element is the same size as the whole struct, we can enter it. The |
| 793 | // comparison must be made on the store size and not the alloca size. Using |
| 794 | // the alloca size may overstate the size of the load. |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 795 | uint64_t FirstEltSize = |
James Molloy | 90d6101 | 2014-08-29 10:17:52 +0000 | [diff] [blame] | 796 | CGF.CGM.getDataLayout().getTypeStoreSize(FirstElt); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 797 | if (FirstEltSize < DstSize && |
James Molloy | 90d6101 | 2014-08-29 10:17:52 +0000 | [diff] [blame] | 798 | FirstEltSize < CGF.CGM.getDataLayout().getTypeStoreSize(SrcSTy)) |
Chris Lattner | 1cd6698 | 2010-06-27 05:56:15 +0000 | [diff] [blame] | 799 | return SrcPtr; |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 800 | |
Chris Lattner | 1cd6698 | 2010-06-27 05:56:15 +0000 | [diff] [blame] | 801 | // GEP into the first element. |
| 802 | SrcPtr = CGF.Builder.CreateConstGEP2_32(SrcPtr, 0, 0, "coerce.dive"); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 803 | |
Chris Lattner | 1cd6698 | 2010-06-27 05:56:15 +0000 | [diff] [blame] | 804 | // If the first element is a struct, recurse. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 805 | llvm::Type *SrcTy = |
Chris Lattner | 1cd6698 | 2010-06-27 05:56:15 +0000 | [diff] [blame] | 806 | cast<llvm::PointerType>(SrcPtr->getType())->getElementType(); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 807 | if (llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy)) |
Chris Lattner | 895c52b | 2010-06-27 06:04:18 +0000 | [diff] [blame] | 808 | return EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF); |
Chris Lattner | 1cd6698 | 2010-06-27 05:56:15 +0000 | [diff] [blame] | 809 | |
| 810 | return SrcPtr; |
| 811 | } |
| 812 | |
Chris Lattner | 055097f | 2010-06-27 06:26:04 +0000 | [diff] [blame] | 813 | /// CoerceIntOrPtrToIntOrPtr - Convert a value Val to the specific Ty where both |
| 814 | /// are either integers or pointers. This does a truncation of the value if it |
| 815 | /// is too large or a zero extension if it is too small. |
Jakob Stoklund Olesen | 36af252 | 2013-06-05 03:00:13 +0000 | [diff] [blame] | 816 | /// |
| 817 | /// This behaves as if the value were coerced through memory, so on big-endian |
| 818 | /// targets the high bits are preserved in a truncation, while little-endian |
| 819 | /// targets preserve the low bits. |
Chris Lattner | 055097f | 2010-06-27 06:26:04 +0000 | [diff] [blame] | 820 | static llvm::Value *CoerceIntOrPtrToIntOrPtr(llvm::Value *Val, |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 821 | llvm::Type *Ty, |
Chris Lattner | 055097f | 2010-06-27 06:26:04 +0000 | [diff] [blame] | 822 | CodeGenFunction &CGF) { |
| 823 | if (Val->getType() == Ty) |
| 824 | return Val; |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 825 | |
Chris Lattner | 055097f | 2010-06-27 06:26:04 +0000 | [diff] [blame] | 826 | if (isa<llvm::PointerType>(Val->getType())) { |
| 827 | // If this is Pointer->Pointer avoid conversion to and from int. |
| 828 | if (isa<llvm::PointerType>(Ty)) |
| 829 | return CGF.Builder.CreateBitCast(Val, Ty, "coerce.val"); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 830 | |
Chris Lattner | 055097f | 2010-06-27 06:26:04 +0000 | [diff] [blame] | 831 | // Convert the pointer to an integer so we can play with its width. |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 832 | Val = CGF.Builder.CreatePtrToInt(Val, CGF.IntPtrTy, "coerce.val.pi"); |
Chris Lattner | 055097f | 2010-06-27 06:26:04 +0000 | [diff] [blame] | 833 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 834 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 835 | llvm::Type *DestIntTy = Ty; |
Chris Lattner | 055097f | 2010-06-27 06:26:04 +0000 | [diff] [blame] | 836 | if (isa<llvm::PointerType>(DestIntTy)) |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 837 | DestIntTy = CGF.IntPtrTy; |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 838 | |
Jakob Stoklund Olesen | 36af252 | 2013-06-05 03:00:13 +0000 | [diff] [blame] | 839 | if (Val->getType() != DestIntTy) { |
| 840 | const llvm::DataLayout &DL = CGF.CGM.getDataLayout(); |
| 841 | if (DL.isBigEndian()) { |
| 842 | // Preserve the high bits on big-endian targets. |
| 843 | // That is what memory coercion does. |
James Molloy | 491cefb | 2014-05-07 17:41:15 +0000 | [diff] [blame] | 844 | uint64_t SrcSize = DL.getTypeSizeInBits(Val->getType()); |
| 845 | uint64_t DstSize = DL.getTypeSizeInBits(DestIntTy); |
| 846 | |
Jakob Stoklund Olesen | 36af252 | 2013-06-05 03:00:13 +0000 | [diff] [blame] | 847 | if (SrcSize > DstSize) { |
| 848 | Val = CGF.Builder.CreateLShr(Val, SrcSize - DstSize, "coerce.highbits"); |
| 849 | Val = CGF.Builder.CreateTrunc(Val, DestIntTy, "coerce.val.ii"); |
| 850 | } else { |
| 851 | Val = CGF.Builder.CreateZExt(Val, DestIntTy, "coerce.val.ii"); |
| 852 | Val = CGF.Builder.CreateShl(Val, DstSize - SrcSize, "coerce.highbits"); |
| 853 | } |
| 854 | } else { |
| 855 | // Little-endian targets preserve the low bits. No shifts required. |
| 856 | Val = CGF.Builder.CreateIntCast(Val, DestIntTy, false, "coerce.val.ii"); |
| 857 | } |
| 858 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 859 | |
Chris Lattner | 055097f | 2010-06-27 06:26:04 +0000 | [diff] [blame] | 860 | if (isa<llvm::PointerType>(Ty)) |
| 861 | Val = CGF.Builder.CreateIntToPtr(Val, Ty, "coerce.val.ip"); |
| 862 | return Val; |
| 863 | } |
| 864 | |
Chris Lattner | 1cd6698 | 2010-06-27 05:56:15 +0000 | [diff] [blame] | 865 | |
| 866 | |
Daniel Dunbar | f5589ac | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 867 | /// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as |
| 868 | /// a pointer to an object of type \arg Ty. |
| 869 | /// |
| 870 | /// This safely handles the case when the src type is smaller than the |
| 871 | /// destination type; in this situation the values of bits which not |
| 872 | /// present in the src are undefined. |
| 873 | static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr, |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 874 | llvm::Type *Ty, |
Daniel Dunbar | f5589ac | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 875 | CodeGenFunction &CGF) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 876 | llvm::Type *SrcTy = |
Daniel Dunbar | f5589ac | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 877 | cast<llvm::PointerType>(SrcPtr->getType())->getElementType(); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 878 | |
Chris Lattner | d200eda | 2010-06-28 22:51:39 +0000 | [diff] [blame] | 879 | // If SrcTy and Ty are the same, just do a load. |
| 880 | if (SrcTy == Ty) |
| 881 | return CGF.Builder.CreateLoad(SrcPtr); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 882 | |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 883 | uint64_t DstSize = CGF.CGM.getDataLayout().getTypeAllocSize(Ty); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 884 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 885 | if (llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy)) { |
Chris Lattner | 895c52b | 2010-06-27 06:04:18 +0000 | [diff] [blame] | 886 | SrcPtr = EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF); |
Chris Lattner | 1cd6698 | 2010-06-27 05:56:15 +0000 | [diff] [blame] | 887 | SrcTy = cast<llvm::PointerType>(SrcPtr->getType())->getElementType(); |
| 888 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 889 | |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 890 | uint64_t SrcSize = CGF.CGM.getDataLayout().getTypeAllocSize(SrcTy); |
Daniel Dunbar | f5589ac | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 891 | |
Chris Lattner | 055097f | 2010-06-27 06:26:04 +0000 | [diff] [blame] | 892 | // If the source and destination are integer or pointer types, just do an |
| 893 | // extension or truncation to the desired type. |
| 894 | if ((isa<llvm::IntegerType>(Ty) || isa<llvm::PointerType>(Ty)) && |
| 895 | (isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy))) { |
| 896 | llvm::LoadInst *Load = CGF.Builder.CreateLoad(SrcPtr); |
| 897 | return CoerceIntOrPtrToIntOrPtr(Load, Ty, CGF); |
| 898 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 899 | |
Daniel Dunbar | b52d077 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 900 | // If load is legal, just bitcast the src pointer. |
Daniel Dunbar | ffdb843 | 2009-05-13 18:54:26 +0000 | [diff] [blame] | 901 | if (SrcSize >= DstSize) { |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 902 | // Generally SrcSize is never greater than DstSize, since this means we are |
| 903 | // losing bits. However, this can happen in cases where the structure has |
| 904 | // additional padding, for example due to a user specified alignment. |
Daniel Dunbar | ffdb843 | 2009-05-13 18:54:26 +0000 | [diff] [blame] | 905 | // |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 906 | // FIXME: Assert that we aren't truncating non-padding bits when have access |
| 907 | // to that information. |
Daniel Dunbar | f5589ac | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 908 | llvm::Value *Casted = |
| 909 | CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty)); |
Daniel Dunbar | ee9e4c2 | 2009-02-07 02:46:03 +0000 | [diff] [blame] | 910 | llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted); |
| 911 | // FIXME: Use better alignment / avoid requiring aligned load. |
| 912 | Load->setAlignment(1); |
| 913 | return Load; |
Daniel Dunbar | f5589ac | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 914 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 915 | |
Chris Lattner | 3fcc790 | 2010-06-27 01:06:27 +0000 | [diff] [blame] | 916 | // Otherwise do coercion through memory. This is stupid, but |
| 917 | // simple. |
| 918 | llvm::Value *Tmp = CGF.CreateTempAlloca(Ty); |
Manman Ren | 84b921f | 2012-11-28 22:08:52 +0000 | [diff] [blame] | 919 | llvm::Type *I8PtrTy = CGF.Builder.getInt8PtrTy(); |
| 920 | llvm::Value *Casted = CGF.Builder.CreateBitCast(Tmp, I8PtrTy); |
| 921 | llvm::Value *SrcCasted = CGF.Builder.CreateBitCast(SrcPtr, I8PtrTy); |
Manman Ren | 836a93b | 2012-11-28 22:29:41 +0000 | [diff] [blame] | 922 | // FIXME: Use better alignment. |
Manman Ren | 84b921f | 2012-11-28 22:08:52 +0000 | [diff] [blame] | 923 | CGF.Builder.CreateMemCpy(Casted, SrcCasted, |
| 924 | llvm::ConstantInt::get(CGF.IntPtrTy, SrcSize), |
| 925 | 1, false); |
Chris Lattner | 3fcc790 | 2010-06-27 01:06:27 +0000 | [diff] [blame] | 926 | return CGF.Builder.CreateLoad(Tmp); |
Daniel Dunbar | f5589ac | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 927 | } |
| 928 | |
Eli Friedman | af9b325 | 2011-05-17 21:08:01 +0000 | [diff] [blame] | 929 | // Function to store a first-class aggregate into memory. We prefer to |
| 930 | // store the elements rather than the aggregate to be more friendly to |
| 931 | // fast-isel. |
| 932 | // FIXME: Do we need to recurse here? |
| 933 | static void BuildAggStore(CodeGenFunction &CGF, llvm::Value *Val, |
| 934 | llvm::Value *DestPtr, bool DestIsVolatile, |
| 935 | bool LowAlignment) { |
| 936 | // Prefer scalar stores to first-class aggregate stores. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 937 | if (llvm::StructType *STy = |
Eli Friedman | af9b325 | 2011-05-17 21:08:01 +0000 | [diff] [blame] | 938 | dyn_cast<llvm::StructType>(Val->getType())) { |
| 939 | for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { |
| 940 | llvm::Value *EltPtr = CGF.Builder.CreateConstGEP2_32(DestPtr, 0, i); |
| 941 | llvm::Value *Elt = CGF.Builder.CreateExtractValue(Val, i); |
| 942 | llvm::StoreInst *SI = CGF.Builder.CreateStore(Elt, EltPtr, |
| 943 | DestIsVolatile); |
| 944 | if (LowAlignment) |
| 945 | SI->setAlignment(1); |
| 946 | } |
| 947 | } else { |
Bill Wendling | f6af30f | 2012-03-16 21:45:12 +0000 | [diff] [blame] | 948 | llvm::StoreInst *SI = CGF.Builder.CreateStore(Val, DestPtr, DestIsVolatile); |
| 949 | if (LowAlignment) |
| 950 | SI->setAlignment(1); |
Eli Friedman | af9b325 | 2011-05-17 21:08:01 +0000 | [diff] [blame] | 951 | } |
| 952 | } |
| 953 | |
Daniel Dunbar | f5589ac | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 954 | /// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src, |
| 955 | /// where the source and destination may have different types. |
| 956 | /// |
| 957 | /// This safely handles the case when the src type is larger than the |
| 958 | /// destination type; the upper bits of the src will be lost. |
| 959 | static void CreateCoercedStore(llvm::Value *Src, |
| 960 | llvm::Value *DstPtr, |
Anders Carlsson | 1749083 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 961 | bool DstIsVolatile, |
Daniel Dunbar | f5589ac | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 962 | CodeGenFunction &CGF) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 963 | llvm::Type *SrcTy = Src->getType(); |
| 964 | llvm::Type *DstTy = |
Daniel Dunbar | f5589ac | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 965 | cast<llvm::PointerType>(DstPtr->getType())->getElementType(); |
Chris Lattner | d200eda | 2010-06-28 22:51:39 +0000 | [diff] [blame] | 966 | if (SrcTy == DstTy) { |
| 967 | CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile); |
| 968 | return; |
| 969 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 970 | |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 971 | uint64_t SrcSize = CGF.CGM.getDataLayout().getTypeAllocSize(SrcTy); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 972 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 973 | if (llvm::StructType *DstSTy = dyn_cast<llvm::StructType>(DstTy)) { |
Chris Lattner | 895c52b | 2010-06-27 06:04:18 +0000 | [diff] [blame] | 974 | DstPtr = EnterStructPointerForCoercedAccess(DstPtr, DstSTy, SrcSize, CGF); |
| 975 | DstTy = cast<llvm::PointerType>(DstPtr->getType())->getElementType(); |
| 976 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 977 | |
Chris Lattner | 055097f | 2010-06-27 06:26:04 +0000 | [diff] [blame] | 978 | // If the source and destination are integer or pointer types, just do an |
| 979 | // extension or truncation to the desired type. |
| 980 | if ((isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy)) && |
| 981 | (isa<llvm::IntegerType>(DstTy) || isa<llvm::PointerType>(DstTy))) { |
| 982 | Src = CoerceIntOrPtrToIntOrPtr(Src, DstTy, CGF); |
| 983 | CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile); |
| 984 | return; |
| 985 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 986 | |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 987 | uint64_t DstSize = CGF.CGM.getDataLayout().getTypeAllocSize(DstTy); |
Daniel Dunbar | f5589ac | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 988 | |
Daniel Dunbar | 313321e | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 989 | // If store is legal, just bitcast the src pointer. |
Daniel Dunbar | 4be99ff | 2009-06-05 07:58:54 +0000 | [diff] [blame] | 990 | if (SrcSize <= DstSize) { |
Daniel Dunbar | f5589ac | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 991 | llvm::Value *Casted = |
| 992 | CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy)); |
Daniel Dunbar | ee9e4c2 | 2009-02-07 02:46:03 +0000 | [diff] [blame] | 993 | // FIXME: Use better alignment / avoid requiring aligned store. |
Eli Friedman | af9b325 | 2011-05-17 21:08:01 +0000 | [diff] [blame] | 994 | BuildAggStore(CGF, Src, Casted, DstIsVolatile, true); |
Daniel Dunbar | f5589ac | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 995 | } else { |
Daniel Dunbar | f5589ac | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 996 | // Otherwise do coercion through memory. This is stupid, but |
| 997 | // simple. |
Daniel Dunbar | 4be99ff | 2009-06-05 07:58:54 +0000 | [diff] [blame] | 998 | |
| 999 | // Generally SrcSize is never greater than DstSize, since this means we are |
| 1000 | // losing bits. However, this can happen in cases where the structure has |
| 1001 | // additional padding, for example due to a user specified alignment. |
| 1002 | // |
| 1003 | // FIXME: Assert that we aren't truncating non-padding bits when have access |
| 1004 | // to that information. |
Daniel Dunbar | f5589ac | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 1005 | llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy); |
| 1006 | CGF.Builder.CreateStore(Src, Tmp); |
Manman Ren | 84b921f | 2012-11-28 22:08:52 +0000 | [diff] [blame] | 1007 | llvm::Type *I8PtrTy = CGF.Builder.getInt8PtrTy(); |
| 1008 | llvm::Value *Casted = CGF.Builder.CreateBitCast(Tmp, I8PtrTy); |
| 1009 | llvm::Value *DstCasted = CGF.Builder.CreateBitCast(DstPtr, I8PtrTy); |
Manman Ren | 836a93b | 2012-11-28 22:29:41 +0000 | [diff] [blame] | 1010 | // FIXME: Use better alignment. |
Manman Ren | 84b921f | 2012-11-28 22:08:52 +0000 | [diff] [blame] | 1011 | CGF.Builder.CreateMemCpy(DstCasted, Casted, |
| 1012 | llvm::ConstantInt::get(CGF.IntPtrTy, DstSize), |
| 1013 | 1, false); |
Daniel Dunbar | f5589ac | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 1014 | } |
| 1015 | } |
| 1016 | |
Alexey Samsonov | 153004f | 2014-09-29 22:08:00 +0000 | [diff] [blame] | 1017 | namespace { |
| 1018 | |
| 1019 | /// Encapsulates information about the way function arguments from |
| 1020 | /// CGFunctionInfo should be passed to actual LLVM IR function. |
| 1021 | class ClangToLLVMArgMapping { |
| 1022 | static const unsigned InvalidIndex = ~0U; |
| 1023 | unsigned InallocaArgNo; |
| 1024 | unsigned SRetArgNo; |
| 1025 | unsigned TotalIRArgs; |
| 1026 | |
| 1027 | /// Arguments of LLVM IR function corresponding to single Clang argument. |
| 1028 | struct IRArgs { |
| 1029 | unsigned PaddingArgIndex; |
| 1030 | // Argument is expanded to IR arguments at positions |
| 1031 | // [FirstArgIndex, FirstArgIndex + NumberOfArgs). |
| 1032 | unsigned FirstArgIndex; |
| 1033 | unsigned NumberOfArgs; |
| 1034 | |
| 1035 | IRArgs() |
| 1036 | : PaddingArgIndex(InvalidIndex), FirstArgIndex(InvalidIndex), |
| 1037 | NumberOfArgs(0) {} |
| 1038 | }; |
| 1039 | |
| 1040 | SmallVector<IRArgs, 8> ArgInfo; |
| 1041 | |
| 1042 | public: |
| 1043 | ClangToLLVMArgMapping(const ASTContext &Context, const CGFunctionInfo &FI, |
| 1044 | bool OnlyRequiredArgs = false) |
| 1045 | : InallocaArgNo(InvalidIndex), SRetArgNo(InvalidIndex), TotalIRArgs(0), |
| 1046 | ArgInfo(OnlyRequiredArgs ? FI.getNumRequiredArgs() : FI.arg_size()) { |
| 1047 | construct(Context, FI, OnlyRequiredArgs); |
| 1048 | } |
| 1049 | |
| 1050 | bool hasInallocaArg() const { return InallocaArgNo != InvalidIndex; } |
| 1051 | unsigned getInallocaArgNo() const { |
| 1052 | assert(hasInallocaArg()); |
| 1053 | return InallocaArgNo; |
| 1054 | } |
| 1055 | |
| 1056 | bool hasSRetArg() const { return SRetArgNo != InvalidIndex; } |
| 1057 | unsigned getSRetArgNo() const { |
| 1058 | assert(hasSRetArg()); |
| 1059 | return SRetArgNo; |
| 1060 | } |
| 1061 | |
| 1062 | unsigned totalIRArgs() const { return TotalIRArgs; } |
| 1063 | |
| 1064 | bool hasPaddingArg(unsigned ArgNo) const { |
| 1065 | assert(ArgNo < ArgInfo.size()); |
| 1066 | return ArgInfo[ArgNo].PaddingArgIndex != InvalidIndex; |
| 1067 | } |
| 1068 | unsigned getPaddingArgNo(unsigned ArgNo) const { |
| 1069 | assert(hasPaddingArg(ArgNo)); |
| 1070 | return ArgInfo[ArgNo].PaddingArgIndex; |
| 1071 | } |
| 1072 | |
| 1073 | /// Returns index of first IR argument corresponding to ArgNo, and their |
| 1074 | /// quantity. |
| 1075 | std::pair<unsigned, unsigned> getIRArgs(unsigned ArgNo) const { |
| 1076 | assert(ArgNo < ArgInfo.size()); |
| 1077 | return std::make_pair(ArgInfo[ArgNo].FirstArgIndex, |
| 1078 | ArgInfo[ArgNo].NumberOfArgs); |
| 1079 | } |
| 1080 | |
| 1081 | private: |
| 1082 | void construct(const ASTContext &Context, const CGFunctionInfo &FI, |
| 1083 | bool OnlyRequiredArgs); |
| 1084 | }; |
| 1085 | |
| 1086 | void ClangToLLVMArgMapping::construct(const ASTContext &Context, |
| 1087 | const CGFunctionInfo &FI, |
| 1088 | bool OnlyRequiredArgs) { |
| 1089 | unsigned IRArgNo = 0; |
| 1090 | bool SwapThisWithSRet = false; |
| 1091 | const ABIArgInfo &RetAI = FI.getReturnInfo(); |
| 1092 | |
| 1093 | if (RetAI.getKind() == ABIArgInfo::Indirect) { |
| 1094 | SwapThisWithSRet = RetAI.isSRetAfterThis(); |
| 1095 | SRetArgNo = SwapThisWithSRet ? 1 : IRArgNo++; |
| 1096 | } |
| 1097 | |
| 1098 | unsigned ArgNo = 0; |
| 1099 | unsigned NumArgs = OnlyRequiredArgs ? FI.getNumRequiredArgs() : FI.arg_size(); |
| 1100 | for (CGFunctionInfo::const_arg_iterator I = FI.arg_begin(); ArgNo < NumArgs; |
| 1101 | ++I, ++ArgNo) { |
| 1102 | assert(I != FI.arg_end()); |
| 1103 | QualType ArgType = I->type; |
| 1104 | const ABIArgInfo &AI = I->info; |
| 1105 | // Collect data about IR arguments corresponding to Clang argument ArgNo. |
| 1106 | auto &IRArgs = ArgInfo[ArgNo]; |
| 1107 | |
| 1108 | if (AI.getPaddingType()) |
| 1109 | IRArgs.PaddingArgIndex = IRArgNo++; |
| 1110 | |
| 1111 | switch (AI.getKind()) { |
| 1112 | case ABIArgInfo::Extend: |
| 1113 | case ABIArgInfo::Direct: { |
| 1114 | // FIXME: handle sseregparm someday... |
| 1115 | llvm::StructType *STy = dyn_cast<llvm::StructType>(AI.getCoerceToType()); |
| 1116 | if (AI.isDirect() && AI.getCanBeFlattened() && STy) { |
| 1117 | IRArgs.NumberOfArgs = STy->getNumElements(); |
| 1118 | } else { |
| 1119 | IRArgs.NumberOfArgs = 1; |
| 1120 | } |
| 1121 | break; |
| 1122 | } |
| 1123 | case ABIArgInfo::Indirect: |
| 1124 | IRArgs.NumberOfArgs = 1; |
| 1125 | break; |
| 1126 | case ABIArgInfo::Ignore: |
| 1127 | case ABIArgInfo::InAlloca: |
| 1128 | // ignore and inalloca doesn't have matching LLVM parameters. |
| 1129 | IRArgs.NumberOfArgs = 0; |
| 1130 | break; |
| 1131 | case ABIArgInfo::Expand: { |
| 1132 | IRArgs.NumberOfArgs = getExpansionSize(ArgType, Context); |
| 1133 | break; |
| 1134 | } |
| 1135 | } |
| 1136 | |
| 1137 | if (IRArgs.NumberOfArgs > 0) { |
| 1138 | IRArgs.FirstArgIndex = IRArgNo; |
| 1139 | IRArgNo += IRArgs.NumberOfArgs; |
| 1140 | } |
| 1141 | |
| 1142 | // Skip over the sret parameter when it comes second. We already handled it |
| 1143 | // above. |
| 1144 | if (IRArgNo == 1 && SwapThisWithSRet) |
| 1145 | IRArgNo++; |
| 1146 | } |
| 1147 | assert(ArgNo == ArgInfo.size()); |
| 1148 | |
| 1149 | if (FI.usesInAlloca()) |
| 1150 | InallocaArgNo = IRArgNo++; |
| 1151 | |
| 1152 | TotalIRArgs = IRArgNo; |
| 1153 | } |
| 1154 | } // namespace |
| 1155 | |
Daniel Dunbar | 8fc81b0 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1156 | /***/ |
| 1157 | |
Daniel Dunbar | 6f2e839 | 2010-07-14 23:39:36 +0000 | [diff] [blame] | 1158 | bool CodeGenModule::ReturnTypeUsesSRet(const CGFunctionInfo &FI) { |
Daniel Dunbar | b8b1c67 | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 1159 | return FI.getReturnInfo().isIndirect(); |
Daniel Dunbar | 7633cbf | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
Tim Northover | e77cc39 | 2014-03-29 13:28:05 +0000 | [diff] [blame] | 1162 | bool CodeGenModule::ReturnSlotInterferesWithArgs(const CGFunctionInfo &FI) { |
| 1163 | return ReturnTypeUsesSRet(FI) && |
| 1164 | getTargetCodeGenInfo().doesReturnSlotInterfereWithArgs(); |
| 1165 | } |
| 1166 | |
Daniel Dunbar | 6f2e839 | 2010-07-14 23:39:36 +0000 | [diff] [blame] | 1167 | bool CodeGenModule::ReturnTypeUsesFPRet(QualType ResultType) { |
| 1168 | if (const BuiltinType *BT = ResultType->getAs<BuiltinType>()) { |
| 1169 | switch (BT->getKind()) { |
| 1170 | default: |
| 1171 | return false; |
| 1172 | case BuiltinType::Float: |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1173 | return getTarget().useObjCFPRetForRealType(TargetInfo::Float); |
Daniel Dunbar | 6f2e839 | 2010-07-14 23:39:36 +0000 | [diff] [blame] | 1174 | case BuiltinType::Double: |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1175 | return getTarget().useObjCFPRetForRealType(TargetInfo::Double); |
Daniel Dunbar | 6f2e839 | 2010-07-14 23:39:36 +0000 | [diff] [blame] | 1176 | case BuiltinType::LongDouble: |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1177 | return getTarget().useObjCFPRetForRealType(TargetInfo::LongDouble); |
Daniel Dunbar | 6f2e839 | 2010-07-14 23:39:36 +0000 | [diff] [blame] | 1178 | } |
| 1179 | } |
| 1180 | |
| 1181 | return false; |
| 1182 | } |
| 1183 | |
Anders Carlsson | 2f1a6c3 | 2011-10-31 16:27:11 +0000 | [diff] [blame] | 1184 | bool CodeGenModule::ReturnTypeUsesFP2Ret(QualType ResultType) { |
| 1185 | if (const ComplexType *CT = ResultType->getAs<ComplexType>()) { |
| 1186 | if (const BuiltinType *BT = CT->getElementType()->getAs<BuiltinType>()) { |
| 1187 | if (BT->getKind() == BuiltinType::LongDouble) |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1188 | return getTarget().useObjCFP2RetForComplexLongDouble(); |
Anders Carlsson | 2f1a6c3 | 2011-10-31 16:27:11 +0000 | [diff] [blame] | 1189 | } |
| 1190 | } |
| 1191 | |
| 1192 | return false; |
| 1193 | } |
| 1194 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1195 | llvm::FunctionType *CodeGenTypes::GetFunctionType(GlobalDecl GD) { |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1196 | const CGFunctionInfo &FI = arrangeGlobalDeclaration(GD); |
| 1197 | return GetFunctionType(FI); |
John McCall | f8ff7b9 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 1198 | } |
| 1199 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1200 | llvm::FunctionType * |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1201 | CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI) { |
Alexey Samsonov | 153004f | 2014-09-29 22:08:00 +0000 | [diff] [blame] | 1202 | |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 1203 | bool Inserted = FunctionsBeingProcessed.insert(&FI).second; |
| 1204 | (void)Inserted; |
Chris Lattner | 6fb0ccf | 2011-07-15 05:16:14 +0000 | [diff] [blame] | 1205 | assert(Inserted && "Recursively being processed?"); |
Daniel Dunbar | 7a95ca3 | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1206 | |
Alexey Samsonov | 153004f | 2014-09-29 22:08:00 +0000 | [diff] [blame] | 1207 | llvm::Type *resultType = nullptr; |
John McCall | 85dd2c5 | 2011-05-15 02:19:42 +0000 | [diff] [blame] | 1208 | const ABIArgInfo &retAI = FI.getReturnInfo(); |
| 1209 | switch (retAI.getKind()) { |
Daniel Dunbar | d3674e6 | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1210 | case ABIArgInfo::Expand: |
John McCall | 85dd2c5 | 2011-05-15 02:19:42 +0000 | [diff] [blame] | 1211 | llvm_unreachable("Invalid ABI kind for return argument"); |
Daniel Dunbar | d3674e6 | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1212 | |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 1213 | case ABIArgInfo::Extend: |
Daniel Dunbar | 67dace89 | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 1214 | case ABIArgInfo::Direct: |
John McCall | 85dd2c5 | 2011-05-15 02:19:42 +0000 | [diff] [blame] | 1215 | resultType = retAI.getCoerceToType(); |
Daniel Dunbar | 67dace89 | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 1216 | break; |
| 1217 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1218 | case ABIArgInfo::InAlloca: |
Reid Kleckner | fab1e89 | 2014-02-25 00:59:14 +0000 | [diff] [blame] | 1219 | if (retAI.getInAllocaSRet()) { |
| 1220 | // sret things on win32 aren't void, they return the sret pointer. |
| 1221 | QualType ret = FI.getReturnType(); |
| 1222 | llvm::Type *ty = ConvertType(ret); |
| 1223 | unsigned addressSpace = Context.getTargetAddressSpace(ret); |
| 1224 | resultType = llvm::PointerType::get(ty, addressSpace); |
| 1225 | } else { |
| 1226 | resultType = llvm::Type::getVoidTy(getLLVMContext()); |
| 1227 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1228 | break; |
| 1229 | |
Daniel Dunbar | b8b1c67 | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 1230 | case ABIArgInfo::Indirect: { |
John McCall | 85dd2c5 | 2011-05-15 02:19:42 +0000 | [diff] [blame] | 1231 | assert(!retAI.getIndirectAlign() && "Align unused on indirect return."); |
| 1232 | resultType = llvm::Type::getVoidTy(getLLVMContext()); |
Daniel Dunbar | 7a95ca3 | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1233 | break; |
| 1234 | } |
| 1235 | |
Daniel Dunbar | 94a6f25 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1236 | case ABIArgInfo::Ignore: |
John McCall | 85dd2c5 | 2011-05-15 02:19:42 +0000 | [diff] [blame] | 1237 | resultType = llvm::Type::getVoidTy(getLLVMContext()); |
Daniel Dunbar | 94a6f25 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1238 | break; |
Daniel Dunbar | 7a95ca3 | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1239 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1240 | |
Alexey Samsonov | 153004f | 2014-09-29 22:08:00 +0000 | [diff] [blame] | 1241 | ClangToLLVMArgMapping IRFunctionArgs(getContext(), FI, true); |
| 1242 | SmallVector<llvm::Type*, 8> ArgTypes(IRFunctionArgs.totalIRArgs()); |
| 1243 | |
| 1244 | // Add type for sret argument. |
| 1245 | if (IRFunctionArgs.hasSRetArg()) { |
| 1246 | QualType Ret = FI.getReturnType(); |
| 1247 | llvm::Type *Ty = ConvertType(Ret); |
| 1248 | unsigned AddressSpace = Context.getTargetAddressSpace(Ret); |
| 1249 | ArgTypes[IRFunctionArgs.getSRetArgNo()] = |
| 1250 | llvm::PointerType::get(Ty, AddressSpace); |
| 1251 | } |
| 1252 | |
| 1253 | // Add type for inalloca argument. |
| 1254 | if (IRFunctionArgs.hasInallocaArg()) { |
| 1255 | auto ArgStruct = FI.getArgStruct(); |
| 1256 | assert(ArgStruct); |
| 1257 | ArgTypes[IRFunctionArgs.getInallocaArgNo()] = ArgStruct->getPointerTo(); |
| 1258 | } |
| 1259 | |
John McCall | c818bbb | 2012-12-07 07:03:17 +0000 | [diff] [blame] | 1260 | // Add in all of the required arguments. |
Alexey Samsonov | 153004f | 2014-09-29 22:08:00 +0000 | [diff] [blame] | 1261 | unsigned ArgNo = 0; |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 1262 | CGFunctionInfo::const_arg_iterator it = FI.arg_begin(), |
| 1263 | ie = it + FI.getNumRequiredArgs(); |
Alexey Samsonov | 153004f | 2014-09-29 22:08:00 +0000 | [diff] [blame] | 1264 | for (; it != ie; ++it, ++ArgNo) { |
| 1265 | const ABIArgInfo &ArgInfo = it->info; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1266 | |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1267 | // Insert a padding type to ensure proper alignment. |
Alexey Samsonov | 153004f | 2014-09-29 22:08:00 +0000 | [diff] [blame] | 1268 | if (IRFunctionArgs.hasPaddingArg(ArgNo)) |
| 1269 | ArgTypes[IRFunctionArgs.getPaddingArgNo(ArgNo)] = |
| 1270 | ArgInfo.getPaddingType(); |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1271 | |
Alexey Samsonov | 153004f | 2014-09-29 22:08:00 +0000 | [diff] [blame] | 1272 | unsigned FirstIRArg, NumIRArgs; |
| 1273 | std::tie(FirstIRArg, NumIRArgs) = IRFunctionArgs.getIRArgs(ArgNo); |
| 1274 | |
| 1275 | switch (ArgInfo.getKind()) { |
Daniel Dunbar | 94a6f25 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1276 | case ABIArgInfo::Ignore: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1277 | case ABIArgInfo::InAlloca: |
Alexey Samsonov | 153004f | 2014-09-29 22:08:00 +0000 | [diff] [blame] | 1278 | assert(NumIRArgs == 0); |
Daniel Dunbar | 94a6f25 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1279 | break; |
| 1280 | |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1281 | case ABIArgInfo::Indirect: { |
Alexey Samsonov | 153004f | 2014-09-29 22:08:00 +0000 | [diff] [blame] | 1282 | assert(NumIRArgs == 1); |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1283 | // indirect arguments are always on the stack, which is addr space #0. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1284 | llvm::Type *LTy = ConvertTypeForMem(it->type); |
Alexey Samsonov | 153004f | 2014-09-29 22:08:00 +0000 | [diff] [blame] | 1285 | ArgTypes[FirstIRArg] = LTy->getPointerTo(); |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1286 | break; |
| 1287 | } |
| 1288 | |
| 1289 | case ABIArgInfo::Extend: |
Chris Lattner | 2cdfda4 | 2010-07-29 06:44:09 +0000 | [diff] [blame] | 1290 | case ABIArgInfo::Direct: { |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 1291 | // Fast-isel and the optimizer generally like scalar values better than |
| 1292 | // FCAs, so we flatten them if this is safe to do for this argument. |
Alexey Samsonov | 153004f | 2014-09-29 22:08:00 +0000 | [diff] [blame] | 1293 | llvm::Type *argType = ArgInfo.getCoerceToType(); |
James Molloy | 6f244b6 | 2014-05-09 16:21:39 +0000 | [diff] [blame] | 1294 | llvm::StructType *st = dyn_cast<llvm::StructType>(argType); |
Alexey Samsonov | 153004f | 2014-09-29 22:08:00 +0000 | [diff] [blame] | 1295 | if (st && ArgInfo.isDirect() && ArgInfo.getCanBeFlattened()) { |
| 1296 | assert(NumIRArgs == st->getNumElements()); |
John McCall | 85dd2c5 | 2011-05-15 02:19:42 +0000 | [diff] [blame] | 1297 | for (unsigned i = 0, e = st->getNumElements(); i != e; ++i) |
Alexey Samsonov | 153004f | 2014-09-29 22:08:00 +0000 | [diff] [blame] | 1298 | ArgTypes[FirstIRArg + i] = st->getElementType(i); |
Chris Lattner | 3dd716c | 2010-06-28 23:44:11 +0000 | [diff] [blame] | 1299 | } else { |
Alexey Samsonov | 153004f | 2014-09-29 22:08:00 +0000 | [diff] [blame] | 1300 | assert(NumIRArgs == 1); |
| 1301 | ArgTypes[FirstIRArg] = argType; |
Chris Lattner | 3dd716c | 2010-06-28 23:44:11 +0000 | [diff] [blame] | 1302 | } |
Daniel Dunbar | 2f219b0 | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 1303 | break; |
Chris Lattner | 2cdfda4 | 2010-07-29 06:44:09 +0000 | [diff] [blame] | 1304 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1305 | |
Daniel Dunbar | d3674e6 | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1306 | case ABIArgInfo::Expand: |
Alexey Samsonov | 153004f | 2014-09-29 22:08:00 +0000 | [diff] [blame] | 1307 | auto ArgTypesIter = ArgTypes.begin() + FirstIRArg; |
| 1308 | getExpandedTypes(it->type, ArgTypesIter); |
| 1309 | assert(ArgTypesIter == ArgTypes.begin() + FirstIRArg + NumIRArgs); |
Daniel Dunbar | d3674e6 | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1310 | break; |
| 1311 | } |
Daniel Dunbar | 7a95ca3 | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1312 | } |
| 1313 | |
Chris Lattner | 6fb0ccf | 2011-07-15 05:16:14 +0000 | [diff] [blame] | 1314 | bool Erased = FunctionsBeingProcessed.erase(&FI); (void)Erased; |
| 1315 | assert(Erased && "Not in set?"); |
Alexey Samsonov | 153004f | 2014-09-29 22:08:00 +0000 | [diff] [blame] | 1316 | |
| 1317 | return llvm::FunctionType::get(resultType, ArgTypes, FI.isVariadic()); |
Daniel Dunbar | 81cf67f | 2008-09-09 23:48:28 +0000 | [diff] [blame] | 1318 | } |
| 1319 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1320 | llvm::Type *CodeGenTypes::GetFunctionTypeForVTable(GlobalDecl GD) { |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 1321 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
Anders Carlsson | 6445773 | 2009-11-24 05:08:52 +0000 | [diff] [blame] | 1322 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1323 | |
Chris Lattner | 8806e32 | 2011-07-10 00:18:59 +0000 | [diff] [blame] | 1324 | if (!isFuncTypeConvertible(FPT)) |
| 1325 | return llvm::StructType::get(getLLVMContext()); |
| 1326 | |
| 1327 | const CGFunctionInfo *Info; |
| 1328 | if (isa<CXXDestructorDecl>(MD)) |
Rafael Espindola | 8d2a19b | 2014-09-08 16:01:27 +0000 | [diff] [blame] | 1329 | Info = |
| 1330 | &arrangeCXXStructorDeclaration(MD, getFromDtorType(GD.getDtorType())); |
Chris Lattner | 8806e32 | 2011-07-10 00:18:59 +0000 | [diff] [blame] | 1331 | else |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1332 | Info = &arrangeCXXMethodDeclaration(MD); |
| 1333 | return GetFunctionType(*Info); |
Anders Carlsson | 6445773 | 2009-11-24 05:08:52 +0000 | [diff] [blame] | 1334 | } |
| 1335 | |
Daniel Dunbar | 3668cb2 | 2009-02-02 23:43:58 +0000 | [diff] [blame] | 1336 | void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI, |
Daniel Dunbar | d931a87 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1337 | const Decl *TargetDecl, |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1338 | AttributeListType &PAL, |
Bill Wendling | f4d64cb | 2013-02-22 00:13:35 +0000 | [diff] [blame] | 1339 | unsigned &CallingConv, |
| 1340 | bool AttrOnCallSite) { |
Bill Wendling | a514ebc | 2012-10-15 20:36:26 +0000 | [diff] [blame] | 1341 | llvm::AttrBuilder FuncAttrs; |
| 1342 | llvm::AttrBuilder RetAttrs; |
Paul Robinson | 0855695 | 2014-12-11 20:14:04 +0000 | [diff] [blame^] | 1343 | bool HasOptnone = false; |
Daniel Dunbar | 76c8eb7 | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1344 | |
Daniel Dunbar | 0ef3479 | 2009-09-12 00:59:20 +0000 | [diff] [blame] | 1345 | CallingConv = FI.getEffectiveCallingConvention(); |
| 1346 | |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1347 | if (FI.isNoReturn()) |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 1348 | FuncAttrs.addAttribute(llvm::Attribute::NoReturn); |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1349 | |
Anton Korobeynikov | c847824 | 2009-04-04 00:49:24 +0000 | [diff] [blame] | 1350 | // FIXME: handle sseregparm someday... |
Daniel Dunbar | 76c8eb7 | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1351 | if (TargetDecl) { |
Rafael Espindola | 2d21ab0 | 2011-10-12 19:51:18 +0000 | [diff] [blame] | 1352 | if (TargetDecl->hasAttr<ReturnsTwiceAttr>()) |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 1353 | FuncAttrs.addAttribute(llvm::Attribute::ReturnsTwice); |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1354 | if (TargetDecl->hasAttr<NoThrowAttr>()) |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 1355 | FuncAttrs.addAttribute(llvm::Attribute::NoUnwind); |
Richard Smith | debc59d | 2013-01-30 05:45:05 +0000 | [diff] [blame] | 1356 | if (TargetDecl->hasAttr<NoReturnAttr>()) |
| 1357 | FuncAttrs.addAttribute(llvm::Attribute::NoReturn); |
Aaron Ballman | 7c19ab1 | 2014-02-22 16:59:24 +0000 | [diff] [blame] | 1358 | if (TargetDecl->hasAttr<NoDuplicateAttr>()) |
| 1359 | FuncAttrs.addAttribute(llvm::Attribute::NoDuplicate); |
Richard Smith | debc59d | 2013-01-30 05:45:05 +0000 | [diff] [blame] | 1360 | |
| 1361 | if (const FunctionDecl *Fn = dyn_cast<FunctionDecl>(TargetDecl)) { |
John McCall | be349de | 2010-07-08 06:48:12 +0000 | [diff] [blame] | 1362 | const FunctionProtoType *FPT = Fn->getType()->getAs<FunctionProtoType>(); |
Sebastian Redl | 31ad754 | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 1363 | if (FPT && FPT->isNothrow(getContext())) |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 1364 | FuncAttrs.addAttribute(llvm::Attribute::NoUnwind); |
Richard Smith | 49af629 | 2013-03-05 08:30:04 +0000 | [diff] [blame] | 1365 | // Don't use [[noreturn]] or _Noreturn for a call to a virtual function. |
| 1366 | // These attributes are not inherited by overloads. |
| 1367 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Fn); |
| 1368 | if (Fn->isNoReturn() && !(AttrOnCallSite && MD && MD->isVirtual())) |
Richard Smith | debc59d | 2013-01-30 05:45:05 +0000 | [diff] [blame] | 1369 | FuncAttrs.addAttribute(llvm::Attribute::NoReturn); |
John McCall | be349de | 2010-07-08 06:48:12 +0000 | [diff] [blame] | 1370 | } |
| 1371 | |
Eric Christopher | bf005ec | 2011-08-15 22:38:22 +0000 | [diff] [blame] | 1372 | // 'const' and 'pure' attribute functions are also nounwind. |
| 1373 | if (TargetDecl->hasAttr<ConstAttr>()) { |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 1374 | FuncAttrs.addAttribute(llvm::Attribute::ReadNone); |
| 1375 | FuncAttrs.addAttribute(llvm::Attribute::NoUnwind); |
Eric Christopher | bf005ec | 2011-08-15 22:38:22 +0000 | [diff] [blame] | 1376 | } else if (TargetDecl->hasAttr<PureAttr>()) { |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 1377 | FuncAttrs.addAttribute(llvm::Attribute::ReadOnly); |
| 1378 | FuncAttrs.addAttribute(llvm::Attribute::NoUnwind); |
Eric Christopher | bf005ec | 2011-08-15 22:38:22 +0000 | [diff] [blame] | 1379 | } |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1380 | if (TargetDecl->hasAttr<MallocAttr>()) |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 1381 | RetAttrs.addAttribute(llvm::Attribute::NoAlias); |
Hal Finkel | d8442b1 | 2014-07-12 04:51:04 +0000 | [diff] [blame] | 1382 | if (TargetDecl->hasAttr<ReturnsNonNullAttr>()) |
| 1383 | RetAttrs.addAttribute(llvm::Attribute::NonNull); |
Paul Robinson | 0855695 | 2014-12-11 20:14:04 +0000 | [diff] [blame^] | 1384 | |
| 1385 | HasOptnone = TargetDecl->hasAttr<OptimizeNoneAttr>(); |
Daniel Dunbar | 76c8eb7 | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1386 | } |
| 1387 | |
Paul Robinson | 0855695 | 2014-12-11 20:14:04 +0000 | [diff] [blame^] | 1388 | // OptimizeNoneAttr takes precedence over -Os or -Oz. No warning needed. |
| 1389 | if (!HasOptnone) { |
| 1390 | if (CodeGenOpts.OptimizeSize) |
| 1391 | FuncAttrs.addAttribute(llvm::Attribute::OptimizeForSize); |
| 1392 | if (CodeGenOpts.OptimizeSize == 2) |
| 1393 | FuncAttrs.addAttribute(llvm::Attribute::MinSize); |
| 1394 | } |
| 1395 | |
Chandler Carruth | bc55fe2 | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 1396 | if (CodeGenOpts.DisableRedZone) |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 1397 | FuncAttrs.addAttribute(llvm::Attribute::NoRedZone); |
Chandler Carruth | bc55fe2 | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 1398 | if (CodeGenOpts.NoImplicitFloat) |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 1399 | FuncAttrs.addAttribute(llvm::Attribute::NoImplicitFloat); |
Peter Collingbourne | b4728c1 | 2014-05-19 22:14:34 +0000 | [diff] [blame] | 1400 | if (CodeGenOpts.EnableSegmentedStacks && |
| 1401 | !(TargetDecl && TargetDecl->hasAttr<NoSplitStackAttr>())) |
Reid Kleckner | fb873af | 2014-04-10 22:59:13 +0000 | [diff] [blame] | 1402 | FuncAttrs.addAttribute("split-stack"); |
Devang Patel | 6e467b1 | 2009-06-04 23:32:02 +0000 | [diff] [blame] | 1403 | |
Bill Wendling | 2f81db6 | 2013-02-22 20:53:29 +0000 | [diff] [blame] | 1404 | if (AttrOnCallSite) { |
| 1405 | // Attributes that should go on the call site only. |
| 1406 | if (!CodeGenOpts.SimplifyLibCalls) |
| 1407 | FuncAttrs.addAttribute(llvm::Attribute::NoBuiltin); |
Bill Wendling | 706469b | 2013-02-28 22:49:57 +0000 | [diff] [blame] | 1408 | } else { |
| 1409 | // Attributes that should go on the function, but not the call site. |
Bill Wendling | 706469b | 2013-02-28 22:49:57 +0000 | [diff] [blame] | 1410 | if (!CodeGenOpts.DisableFPElim) { |
Bill Wendling | dabafea | 2013-03-13 22:24:33 +0000 | [diff] [blame] | 1411 | FuncAttrs.addAttribute("no-frame-pointer-elim", "false"); |
Bill Wendling | 706469b | 2013-02-28 22:49:57 +0000 | [diff] [blame] | 1412 | } else if (CodeGenOpts.OmitLeafFramePointer) { |
Bill Wendling | dabafea | 2013-03-13 22:24:33 +0000 | [diff] [blame] | 1413 | FuncAttrs.addAttribute("no-frame-pointer-elim", "false"); |
Bill Wendling | 17d1b614 | 2013-08-22 21:16:51 +0000 | [diff] [blame] | 1414 | FuncAttrs.addAttribute("no-frame-pointer-elim-non-leaf"); |
Bill Wendling | 706469b | 2013-02-28 22:49:57 +0000 | [diff] [blame] | 1415 | } else { |
Bill Wendling | dabafea | 2013-03-13 22:24:33 +0000 | [diff] [blame] | 1416 | FuncAttrs.addAttribute("no-frame-pointer-elim", "true"); |
Bill Wendling | 17d1b614 | 2013-08-22 21:16:51 +0000 | [diff] [blame] | 1417 | FuncAttrs.addAttribute("no-frame-pointer-elim-non-leaf"); |
Bill Wendling | 706469b | 2013-02-28 22:49:57 +0000 | [diff] [blame] | 1418 | } |
| 1419 | |
Bill Wendling | dabafea | 2013-03-13 22:24:33 +0000 | [diff] [blame] | 1420 | FuncAttrs.addAttribute("less-precise-fpmad", |
Bill Wendling | f69f594 | 2013-07-26 21:51:11 +0000 | [diff] [blame] | 1421 | llvm::toStringRef(CodeGenOpts.LessPreciseFPMAD)); |
Bill Wendling | dabafea | 2013-03-13 22:24:33 +0000 | [diff] [blame] | 1422 | FuncAttrs.addAttribute("no-infs-fp-math", |
Bill Wendling | f69f594 | 2013-07-26 21:51:11 +0000 | [diff] [blame] | 1423 | llvm::toStringRef(CodeGenOpts.NoInfsFPMath)); |
Bill Wendling | dabafea | 2013-03-13 22:24:33 +0000 | [diff] [blame] | 1424 | FuncAttrs.addAttribute("no-nans-fp-math", |
Bill Wendling | f69f594 | 2013-07-26 21:51:11 +0000 | [diff] [blame] | 1425 | llvm::toStringRef(CodeGenOpts.NoNaNsFPMath)); |
Bill Wendling | dabafea | 2013-03-13 22:24:33 +0000 | [diff] [blame] | 1426 | FuncAttrs.addAttribute("unsafe-fp-math", |
Bill Wendling | f69f594 | 2013-07-26 21:51:11 +0000 | [diff] [blame] | 1427 | llvm::toStringRef(CodeGenOpts.UnsafeFPMath)); |
Bill Wendling | dabafea | 2013-03-13 22:24:33 +0000 | [diff] [blame] | 1428 | FuncAttrs.addAttribute("use-soft-float", |
Bill Wendling | f69f594 | 2013-07-26 21:51:11 +0000 | [diff] [blame] | 1429 | llvm::toStringRef(CodeGenOpts.SoftFloat)); |
Bill Wendling | b321972 | 2013-07-22 20:15:41 +0000 | [diff] [blame] | 1430 | FuncAttrs.addAttribute("stack-protector-buffer-size", |
Bill Wendling | 021c8de | 2013-07-12 22:26:07 +0000 | [diff] [blame] | 1431 | llvm::utostr(CodeGenOpts.SSPBufferSize)); |
Bill Wendling | a9cc8c0 | 2013-07-25 00:32:41 +0000 | [diff] [blame] | 1432 | |
Bill Wendling | d8f4950 | 2013-08-01 21:41:02 +0000 | [diff] [blame] | 1433 | if (!CodeGenOpts.StackRealignment) |
| 1434 | FuncAttrs.addAttribute("no-realign-stack"); |
Bill Wendling | 985d1c5 | 2013-02-15 21:30:01 +0000 | [diff] [blame] | 1435 | } |
| 1436 | |
Alexey Samsonov | 153004f | 2014-09-29 22:08:00 +0000 | [diff] [blame] | 1437 | ClangToLLVMArgMapping IRFunctionArgs(getContext(), FI); |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 1438 | |
Daniel Dunbar | 3668cb2 | 2009-02-02 23:43:58 +0000 | [diff] [blame] | 1439 | QualType RetTy = FI.getReturnType(); |
Daniel Dunbar | b52d077 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 1440 | const ABIArgInfo &RetAI = FI.getReturnInfo(); |
Daniel Dunbar | 7a95ca3 | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1441 | switch (RetAI.getKind()) { |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 1442 | case ABIArgInfo::Extend: |
Jakob Stoklund Olesen | d7bf293 | 2013-05-29 03:57:23 +0000 | [diff] [blame] | 1443 | if (RetTy->hasSignedIntegerRepresentation()) |
| 1444 | RetAttrs.addAttribute(llvm::Attribute::SExt); |
| 1445 | else if (RetTy->hasUnsignedIntegerRepresentation()) |
| 1446 | RetAttrs.addAttribute(llvm::Attribute::ZExt); |
Jakob Stoklund Olesen | a366114 | 2013-06-05 03:00:09 +0000 | [diff] [blame] | 1447 | // FALL THROUGH |
Daniel Dunbar | 67dace89 | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 1448 | case ABIArgInfo::Direct: |
Jakob Stoklund Olesen | a366114 | 2013-06-05 03:00:09 +0000 | [diff] [blame] | 1449 | if (RetAI.getInReg()) |
| 1450 | RetAttrs.addAttribute(llvm::Attribute::InReg); |
| 1451 | break; |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1452 | case ABIArgInfo::Ignore: |
Daniel Dunbar | a72d4ae | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1453 | break; |
| 1454 | |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 1455 | case ABIArgInfo::InAlloca: |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1456 | case ABIArgInfo::Indirect: { |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 1457 | // inalloca and sret disable readnone and readonly |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 1458 | FuncAttrs.removeAttribute(llvm::Attribute::ReadOnly) |
| 1459 | .removeAttribute(llvm::Attribute::ReadNone); |
Daniel Dunbar | a72d4ae | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1460 | break; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1461 | } |
Daniel Dunbar | a72d4ae | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1462 | |
Daniel Dunbar | d3674e6 | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1463 | case ABIArgInfo::Expand: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1464 | llvm_unreachable("Invalid ABI kind for return argument"); |
Daniel Dunbar | 76c8eb7 | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1465 | } |
Daniel Dunbar | a72d4ae | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1466 | |
Hal Finkel | a2347ba | 2014-07-18 15:52:10 +0000 | [diff] [blame] | 1467 | if (const auto *RefTy = RetTy->getAs<ReferenceType>()) { |
| 1468 | QualType PTy = RefTy->getPointeeType(); |
| 1469 | if (!PTy->isIncompleteType() && PTy->isConstantSizeType()) |
| 1470 | RetAttrs.addDereferenceableAttr(getContext().getTypeSizeInChars(PTy) |
| 1471 | .getQuantity()); |
| 1472 | else if (getContext().getTargetAddressSpace(PTy) == 0) |
| 1473 | RetAttrs.addAttribute(llvm::Attribute::NonNull); |
| 1474 | } |
Nick Lewycky | 9b46eb8 | 2014-05-28 09:56:42 +0000 | [diff] [blame] | 1475 | |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 1476 | // Attach return attributes. |
| 1477 | if (RetAttrs.hasAttributes()) { |
| 1478 | PAL.push_back(llvm::AttributeSet::get( |
| 1479 | getLLVMContext(), llvm::AttributeSet::ReturnIndex, RetAttrs)); |
| 1480 | } |
Anton Korobeynikov | c847824 | 2009-04-04 00:49:24 +0000 | [diff] [blame] | 1481 | |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 1482 | // Attach attributes to sret. |
| 1483 | if (IRFunctionArgs.hasSRetArg()) { |
| 1484 | llvm::AttrBuilder SRETAttrs; |
| 1485 | SRETAttrs.addAttribute(llvm::Attribute::StructRet); |
| 1486 | if (RetAI.getInReg()) |
| 1487 | SRETAttrs.addAttribute(llvm::Attribute::InReg); |
| 1488 | PAL.push_back(llvm::AttributeSet::get( |
| 1489 | getLLVMContext(), IRFunctionArgs.getSRetArgNo() + 1, SRETAttrs)); |
| 1490 | } |
| 1491 | |
| 1492 | // Attach attributes to inalloca argument. |
| 1493 | if (IRFunctionArgs.hasInallocaArg()) { |
| 1494 | llvm::AttrBuilder Attrs; |
| 1495 | Attrs.addAttribute(llvm::Attribute::InAlloca); |
| 1496 | PAL.push_back(llvm::AttributeSet::get( |
| 1497 | getLLVMContext(), IRFunctionArgs.getInallocaArgNo() + 1, Attrs)); |
| 1498 | } |
| 1499 | |
| 1500 | |
| 1501 | unsigned ArgNo = 0; |
| 1502 | for (CGFunctionInfo::const_arg_iterator I = FI.arg_begin(), |
| 1503 | E = FI.arg_end(); |
| 1504 | I != E; ++I, ++ArgNo) { |
| 1505 | QualType ParamType = I->type; |
| 1506 | const ABIArgInfo &AI = I->info; |
Bill Wendling | a514ebc | 2012-10-15 20:36:26 +0000 | [diff] [blame] | 1507 | llvm::AttrBuilder Attrs; |
Anton Korobeynikov | c847824 | 2009-04-04 00:49:24 +0000 | [diff] [blame] | 1508 | |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 1509 | // Add attribute for padding argument, if necessary. |
| 1510 | if (IRFunctionArgs.hasPaddingArg(ArgNo)) { |
Bill Wendling | 290d952 | 2013-01-27 02:46:53 +0000 | [diff] [blame] | 1511 | if (AI.getPaddingInReg()) |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 1512 | PAL.push_back(llvm::AttributeSet::get( |
| 1513 | getLLVMContext(), IRFunctionArgs.getPaddingArgNo(ArgNo) + 1, |
| 1514 | llvm::Attribute::InReg)); |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1515 | } |
| 1516 | |
John McCall | 39ec71f | 2010-03-27 00:47:27 +0000 | [diff] [blame] | 1517 | // 'restrict' -> 'noalias' is done in EmitFunctionProlog when we |
| 1518 | // have the corresponding parameter variable. It doesn't make |
Daniel Dunbar | cb2b3d0 | 2011-02-10 18:10:07 +0000 | [diff] [blame] | 1519 | // sense to do it here because parameters are so messed up. |
Daniel Dunbar | d3674e6 | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1520 | switch (AI.getKind()) { |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1521 | case ABIArgInfo::Extend: |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 1522 | if (ParamType->isSignedIntegerOrEnumerationType()) |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 1523 | Attrs.addAttribute(llvm::Attribute::SExt); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 1524 | else if (ParamType->isUnsignedIntegerOrEnumerationType()) |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 1525 | Attrs.addAttribute(llvm::Attribute::ZExt); |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1526 | // FALL THROUGH |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 1527 | case ABIArgInfo::Direct: |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1528 | if (AI.getInReg()) |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 1529 | Attrs.addAttribute(llvm::Attribute::InReg); |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1530 | break; |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 1531 | |
Daniel Dunbar | b8b1c67 | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 1532 | case ABIArgInfo::Indirect: |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1533 | if (AI.getInReg()) |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 1534 | Attrs.addAttribute(llvm::Attribute::InReg); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1535 | |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 1536 | if (AI.getIndirectByVal()) |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 1537 | Attrs.addAttribute(llvm::Attribute::ByVal); |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 1538 | |
Bill Wendling | a7912f8 | 2012-10-10 07:36:56 +0000 | [diff] [blame] | 1539 | Attrs.addAlignmentAttr(AI.getIndirectAlign()); |
| 1540 | |
Daniel Dunbar | c230443 | 2009-03-18 19:51:01 +0000 | [diff] [blame] | 1541 | // byval disables readnone and readonly. |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 1542 | FuncAttrs.removeAttribute(llvm::Attribute::ReadOnly) |
| 1543 | .removeAttribute(llvm::Attribute::ReadNone); |
Daniel Dunbar | d3674e6 | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1544 | break; |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 1545 | |
Daniel Dunbar | 94a6f25 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1546 | case ABIArgInfo::Ignore: |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 1547 | case ABIArgInfo::Expand: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1548 | continue; |
Daniel Dunbar | 94a6f25 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1549 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1550 | case ABIArgInfo::InAlloca: |
| 1551 | // inalloca disables readnone and readonly. |
| 1552 | FuncAttrs.removeAttribute(llvm::Attribute::ReadOnly) |
| 1553 | .removeAttribute(llvm::Attribute::ReadNone); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1554 | continue; |
Daniel Dunbar | 76c8eb7 | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1555 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1556 | |
Hal Finkel | a2347ba | 2014-07-18 15:52:10 +0000 | [diff] [blame] | 1557 | if (const auto *RefTy = ParamType->getAs<ReferenceType>()) { |
| 1558 | QualType PTy = RefTy->getPointeeType(); |
| 1559 | if (!PTy->isIncompleteType() && PTy->isConstantSizeType()) |
| 1560 | Attrs.addDereferenceableAttr(getContext().getTypeSizeInChars(PTy) |
| 1561 | .getQuantity()); |
| 1562 | else if (getContext().getTargetAddressSpace(PTy) == 0) |
| 1563 | Attrs.addAttribute(llvm::Attribute::NonNull); |
| 1564 | } |
Nick Lewycky | 9b46eb8 | 2014-05-28 09:56:42 +0000 | [diff] [blame] | 1565 | |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 1566 | if (Attrs.hasAttributes()) { |
| 1567 | unsigned FirstIRArg, NumIRArgs; |
| 1568 | std::tie(FirstIRArg, NumIRArgs) = IRFunctionArgs.getIRArgs(ArgNo); |
| 1569 | for (unsigned i = 0; i < NumIRArgs; i++) |
| 1570 | PAL.push_back(llvm::AttributeSet::get(getLLVMContext(), |
| 1571 | FirstIRArg + i + 1, Attrs)); |
| 1572 | } |
Daniel Dunbar | 76c8eb7 | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1573 | } |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 1574 | assert(ArgNo == FI.arg_size()); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1575 | |
Bill Wendling | a7912f8 | 2012-10-10 07:36:56 +0000 | [diff] [blame] | 1576 | if (FuncAttrs.hasAttributes()) |
Bill Wendling | 4f0c080 | 2012-10-15 07:31:59 +0000 | [diff] [blame] | 1577 | PAL.push_back(llvm:: |
Bill Wendling | 290d952 | 2013-01-27 02:46:53 +0000 | [diff] [blame] | 1578 | AttributeSet::get(getLLVMContext(), |
| 1579 | llvm::AttributeSet::FunctionIndex, |
| 1580 | FuncAttrs)); |
Daniel Dunbar | 76c8eb7 | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1581 | } |
| 1582 | |
John McCall | a738c25 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1583 | /// An argument came in as a promoted argument; demote it back to its |
| 1584 | /// declared type. |
| 1585 | static llvm::Value *emitArgumentDemotion(CodeGenFunction &CGF, |
| 1586 | const VarDecl *var, |
| 1587 | llvm::Value *value) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1588 | llvm::Type *varType = CGF.ConvertType(var->getType()); |
John McCall | a738c25 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1589 | |
| 1590 | // This can happen with promotions that actually don't change the |
| 1591 | // underlying type, like the enum promotions. |
| 1592 | if (value->getType() == varType) return value; |
| 1593 | |
| 1594 | assert((varType->isIntegerTy() || varType->isFloatingPointTy()) |
| 1595 | && "unexpected promotion type"); |
| 1596 | |
| 1597 | if (isa<llvm::IntegerType>(varType)) |
| 1598 | return CGF.Builder.CreateTrunc(value, varType, "arg.unpromote"); |
| 1599 | |
| 1600 | return CGF.Builder.CreateFPCast(value, varType, "arg.unpromote"); |
| 1601 | } |
| 1602 | |
Alexey Samsonov | 8e1162c | 2014-09-08 17:22:45 +0000 | [diff] [blame] | 1603 | /// Returns the attribute (either parameter attribute, or function |
| 1604 | /// attribute), which declares argument ArgNo to be non-null. |
| 1605 | static const NonNullAttr *getNonNullAttr(const Decl *FD, const ParmVarDecl *PVD, |
| 1606 | QualType ArgType, unsigned ArgNo) { |
Alexey Samsonov | 9fc9bf8 | 2014-08-28 00:53:20 +0000 | [diff] [blame] | 1607 | // FIXME: __attribute__((nonnull)) can also be applied to: |
| 1608 | // - references to pointers, where the pointee is known to be |
| 1609 | // nonnull (apparently a Clang extension) |
| 1610 | // - transparent unions containing pointers |
| 1611 | // In the former case, LLVM IR cannot represent the constraint. In |
| 1612 | // the latter case, we have no guarantee that the transparent union |
| 1613 | // is in fact passed as a pointer. |
Alexey Samsonov | 8e1162c | 2014-09-08 17:22:45 +0000 | [diff] [blame] | 1614 | if (!ArgType->isAnyPointerType() && !ArgType->isBlockPointerType()) |
| 1615 | return nullptr; |
Alexey Samsonov | 9fc9bf8 | 2014-08-28 00:53:20 +0000 | [diff] [blame] | 1616 | // First, check attribute on parameter itself. |
Alexey Samsonov | 8e1162c | 2014-09-08 17:22:45 +0000 | [diff] [blame] | 1617 | if (PVD) { |
| 1618 | if (auto ParmNNAttr = PVD->getAttr<NonNullAttr>()) |
| 1619 | return ParmNNAttr; |
| 1620 | } |
Alexey Samsonov | 9fc9bf8 | 2014-08-28 00:53:20 +0000 | [diff] [blame] | 1621 | // Check function attributes. |
| 1622 | if (!FD) |
Alexey Samsonov | 8e1162c | 2014-09-08 17:22:45 +0000 | [diff] [blame] | 1623 | return nullptr; |
Alexey Samsonov | 9fc9bf8 | 2014-08-28 00:53:20 +0000 | [diff] [blame] | 1624 | for (const auto *NNAttr : FD->specific_attrs<NonNullAttr>()) { |
Alexey Samsonov | 8e1162c | 2014-09-08 17:22:45 +0000 | [diff] [blame] | 1625 | if (NNAttr->isNonNull(ArgNo)) |
| 1626 | return NNAttr; |
Alexey Samsonov | 9fc9bf8 | 2014-08-28 00:53:20 +0000 | [diff] [blame] | 1627 | } |
Alexey Samsonov | 8e1162c | 2014-09-08 17:22:45 +0000 | [diff] [blame] | 1628 | return nullptr; |
Alexey Samsonov | 9fc9bf8 | 2014-08-28 00:53:20 +0000 | [diff] [blame] | 1629 | } |
| 1630 | |
Daniel Dunbar | d931a87 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1631 | void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI, |
| 1632 | llvm::Function *Fn, |
Daniel Dunbar | 613855c | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1633 | const FunctionArgList &Args) { |
Hans Wennborg | d71907d | 2014-09-04 22:16:33 +0000 | [diff] [blame] | 1634 | if (CurCodeDecl && CurCodeDecl->hasAttr<NakedAttr>()) |
| 1635 | // Naked functions don't have prologues. |
| 1636 | return; |
| 1637 | |
John McCall | caa1945 | 2009-07-28 01:00:58 +0000 | [diff] [blame] | 1638 | // If this is an implicit-return-zero function, go ahead and |
| 1639 | // initialize the return value. TODO: it might be nice to have |
| 1640 | // a more general mechanism for this that didn't require synthesized |
| 1641 | // return statements. |
John McCall | dec348f7 | 2013-05-03 07:33:41 +0000 | [diff] [blame] | 1642 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurCodeDecl)) { |
John McCall | caa1945 | 2009-07-28 01:00:58 +0000 | [diff] [blame] | 1643 | if (FD->hasImplicitReturnZero()) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1644 | QualType RetTy = FD->getReturnType().getUnqualifiedType(); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1645 | llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy); |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 1646 | llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy); |
John McCall | caa1945 | 2009-07-28 01:00:58 +0000 | [diff] [blame] | 1647 | Builder.CreateStore(Zero, ReturnValue); |
| 1648 | } |
| 1649 | } |
| 1650 | |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1651 | // FIXME: We no longer need the types from FunctionArgList; lift up and |
| 1652 | // simplify. |
Daniel Dunbar | 5a0acdc9 | 2009-02-03 06:02:10 +0000 | [diff] [blame] | 1653 | |
Alexey Samsonov | 153004f | 2014-09-29 22:08:00 +0000 | [diff] [blame] | 1654 | ClangToLLVMArgMapping IRFunctionArgs(CGM.getContext(), FI); |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 1655 | // Flattened function arguments. |
| 1656 | SmallVector<llvm::Argument *, 16> FnArgs; |
| 1657 | FnArgs.reserve(IRFunctionArgs.totalIRArgs()); |
| 1658 | for (auto &Arg : Fn->args()) { |
| 1659 | FnArgs.push_back(&Arg); |
| 1660 | } |
| 1661 | assert(FnArgs.size() == IRFunctionArgs.totalIRArgs()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1662 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1663 | // If we're using inalloca, all the memory arguments are GEPs off of the last |
| 1664 | // parameter, which is a pointer to the complete memory area. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1665 | llvm::Value *ArgStruct = nullptr; |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 1666 | if (IRFunctionArgs.hasInallocaArg()) { |
| 1667 | ArgStruct = FnArgs[IRFunctionArgs.getInallocaArgNo()]; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1668 | assert(ArgStruct->getType() == FI.getArgStruct()->getPointerTo()); |
| 1669 | } |
| 1670 | |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 1671 | // Name the struct return parameter. |
| 1672 | if (IRFunctionArgs.hasSRetArg()) { |
| 1673 | auto AI = FnArgs[IRFunctionArgs.getSRetArgNo()]; |
Daniel Dunbar | 613855c | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1674 | AI->setName("agg.result"); |
Reid Kleckner | 37abaca | 2014-05-09 22:46:15 +0000 | [diff] [blame] | 1675 | AI->addAttr(llvm::AttributeSet::get(getLLVMContext(), AI->getArgNo() + 1, |
Bill Wendling | ce2f9c5 | 2013-01-23 06:15:10 +0000 | [diff] [blame] | 1676 | llvm::Attribute::NoAlias)); |
Daniel Dunbar | 613855c | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1677 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1678 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1679 | // Track if we received the parameter as a pointer (indirect, byval, or |
| 1680 | // inalloca). If already have a pointer, EmitParmDecl doesn't need to copy it |
| 1681 | // into a local alloca for us. |
| 1682 | enum ValOrPointer { HaveValue = 0, HavePointer = 1 }; |
Reid Kleckner | 8ae1627 | 2014-02-01 00:23:22 +0000 | [diff] [blame] | 1683 | typedef llvm::PointerIntPair<llvm::Value *, 1> ValueAndIsPtr; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1684 | SmallVector<ValueAndIsPtr, 16> ArgVals; |
| 1685 | ArgVals.reserve(Args.size()); |
| 1686 | |
Reid Kleckner | 739756c | 2013-12-04 19:23:12 +0000 | [diff] [blame] | 1687 | // Create a pointer value for every parameter declaration. This usually |
| 1688 | // entails copying one or more LLVM IR arguments into an alloca. Don't push |
| 1689 | // any cleanups or do anything that might unwind. We do that separately, so |
| 1690 | // we can push the cleanups in the correct order for the ABI. |
Daniel Dunbar | a45bdbb | 2009-02-04 21:17:21 +0000 | [diff] [blame] | 1691 | assert(FI.arg_size() == Args.size() && |
| 1692 | "Mismatch between function signature & arguments."); |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 1693 | unsigned ArgNo = 0; |
Daniel Dunbar | b52d077 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 1694 | CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin(); |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 1695 | for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end(); |
Devang Patel | 68a1525 | 2011-03-03 20:13:15 +0000 | [diff] [blame] | 1696 | i != e; ++i, ++info_it, ++ArgNo) { |
John McCall | a738c25 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1697 | const VarDecl *Arg = *i; |
Daniel Dunbar | b52d077 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 1698 | QualType Ty = info_it->type; |
| 1699 | const ABIArgInfo &ArgI = info_it->info; |
Daniel Dunbar | d3674e6 | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1700 | |
John McCall | a738c25 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1701 | bool isPromoted = |
| 1702 | isa<ParmVarDecl>(Arg) && cast<ParmVarDecl>(Arg)->isKNRPromoted(); |
| 1703 | |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 1704 | unsigned FirstIRArg, NumIRArgs; |
| 1705 | std::tie(FirstIRArg, NumIRArgs) = IRFunctionArgs.getIRArgs(ArgNo); |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1706 | |
Daniel Dunbar | d3674e6 | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1707 | switch (ArgI.getKind()) { |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1708 | case ABIArgInfo::InAlloca: { |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 1709 | assert(NumIRArgs == 0); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1710 | llvm::Value *V = Builder.CreateStructGEP( |
| 1711 | ArgStruct, ArgI.getInAllocaFieldIndex(), Arg->getName()); |
| 1712 | ArgVals.push_back(ValueAndIsPtr(V, HavePointer)); |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 1713 | break; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1714 | } |
| 1715 | |
Daniel Dunbar | 747865a | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 1716 | case ABIArgInfo::Indirect: { |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 1717 | assert(NumIRArgs == 1); |
| 1718 | llvm::Value *V = FnArgs[FirstIRArg]; |
Daniel Dunbar | 7b7c293 | 2010-09-16 20:42:02 +0000 | [diff] [blame] | 1719 | |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 1720 | if (!hasScalarEvaluationKind(Ty)) { |
Daniel Dunbar | 7b7c293 | 2010-09-16 20:42:02 +0000 | [diff] [blame] | 1721 | // Aggregates and complex variables are accessed by reference. All we |
| 1722 | // need to do is realign the value, if requested |
| 1723 | if (ArgI.getIndirectRealign()) { |
| 1724 | llvm::Value *AlignedTemp = CreateMemTemp(Ty, "coerce"); |
| 1725 | |
| 1726 | // Copy from the incoming argument pointer to the temporary with the |
| 1727 | // appropriate alignment. |
| 1728 | // |
| 1729 | // FIXME: We should have a common utility for generating an aggregate |
| 1730 | // copy. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1731 | llvm::Type *I8PtrTy = Builder.getInt8PtrTy(); |
Ken Dyck | 705ba07 | 2011-01-19 01:58:38 +0000 | [diff] [blame] | 1732 | CharUnits Size = getContext().getTypeSizeInChars(Ty); |
NAKAMURA Takumi | dd63436 | 2011-03-10 14:02:21 +0000 | [diff] [blame] | 1733 | llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy); |
| 1734 | llvm::Value *Src = Builder.CreateBitCast(V, I8PtrTy); |
| 1735 | Builder.CreateMemCpy(Dst, |
| 1736 | Src, |
Ken Dyck | 705ba07 | 2011-01-19 01:58:38 +0000 | [diff] [blame] | 1737 | llvm::ConstantInt::get(IntPtrTy, |
| 1738 | Size.getQuantity()), |
Benjamin Kramer | acc6b4e | 2010-12-30 00:13:21 +0000 | [diff] [blame] | 1739 | ArgI.getIndirectAlign(), |
| 1740 | false); |
Daniel Dunbar | 7b7c293 | 2010-09-16 20:42:02 +0000 | [diff] [blame] | 1741 | V = AlignedTemp; |
| 1742 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1743 | ArgVals.push_back(ValueAndIsPtr(V, HavePointer)); |
Daniel Dunbar | 747865a | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 1744 | } else { |
| 1745 | // Load scalar value from indirect argument. |
Ken Dyck | 705ba07 | 2011-01-19 01:58:38 +0000 | [diff] [blame] | 1746 | CharUnits Alignment = getContext().getTypeAlignInChars(Ty); |
Nick Lewycky | 2d84e84 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 1747 | V = EmitLoadOfScalar(V, false, Alignment.getQuantity(), Ty, |
| 1748 | Arg->getLocStart()); |
John McCall | a738c25 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1749 | |
| 1750 | if (isPromoted) |
| 1751 | V = emitArgumentDemotion(*this, Arg, V); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1752 | ArgVals.push_back(ValueAndIsPtr(V, HaveValue)); |
Daniel Dunbar | 747865a | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 1753 | } |
Daniel Dunbar | 747865a | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 1754 | break; |
| 1755 | } |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 1756 | |
| 1757 | case ABIArgInfo::Extend: |
Daniel Dunbar | 67dace89 | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 1758 | case ABIArgInfo::Direct: { |
Akira Hatanaka | 18334dd | 2012-01-09 19:08:06 +0000 | [diff] [blame] | 1759 | |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1760 | // If we have the trivial case, handle it with no muss and fuss. |
| 1761 | if (!isa<llvm::StructType>(ArgI.getCoerceToType()) && |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 1762 | ArgI.getCoerceToType() == ConvertType(Ty) && |
| 1763 | ArgI.getDirectOffset() == 0) { |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 1764 | assert(NumIRArgs == 1); |
| 1765 | auto AI = FnArgs[FirstIRArg]; |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1766 | llvm::Value *V = AI; |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1767 | |
Hal Finkel | 48d53e2 | 2014-07-19 01:41:07 +0000 | [diff] [blame] | 1768 | if (const ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(Arg)) { |
Alexey Samsonov | 8e1162c | 2014-09-08 17:22:45 +0000 | [diff] [blame] | 1769 | if (getNonNullAttr(CurCodeDecl, PVD, PVD->getType(), |
| 1770 | PVD->getFunctionScopeIndex())) |
Hal Finkel | 82504f0 | 2014-07-11 17:35:21 +0000 | [diff] [blame] | 1771 | AI->addAttr(llvm::AttributeSet::get(getLLVMContext(), |
| 1772 | AI->getArgNo() + 1, |
| 1773 | llvm::Attribute::NonNull)); |
| 1774 | |
Hal Finkel | 48d53e2 | 2014-07-19 01:41:07 +0000 | [diff] [blame] | 1775 | QualType OTy = PVD->getOriginalType(); |
| 1776 | if (const auto *ArrTy = |
| 1777 | getContext().getAsConstantArrayType(OTy)) { |
| 1778 | // A C99 array parameter declaration with the static keyword also |
| 1779 | // indicates dereferenceability, and if the size is constant we can |
| 1780 | // use the dereferenceable attribute (which requires the size in |
| 1781 | // bytes). |
Hal Finkel | 16e394a | 2014-07-19 02:13:40 +0000 | [diff] [blame] | 1782 | if (ArrTy->getSizeModifier() == ArrayType::Static) { |
Hal Finkel | 48d53e2 | 2014-07-19 01:41:07 +0000 | [diff] [blame] | 1783 | QualType ETy = ArrTy->getElementType(); |
| 1784 | uint64_t ArrSize = ArrTy->getSize().getZExtValue(); |
| 1785 | if (!ETy->isIncompleteType() && ETy->isConstantSizeType() && |
| 1786 | ArrSize) { |
| 1787 | llvm::AttrBuilder Attrs; |
| 1788 | Attrs.addDereferenceableAttr( |
| 1789 | getContext().getTypeSizeInChars(ETy).getQuantity()*ArrSize); |
| 1790 | AI->addAttr(llvm::AttributeSet::get(getLLVMContext(), |
| 1791 | AI->getArgNo() + 1, Attrs)); |
| 1792 | } else if (getContext().getTargetAddressSpace(ETy) == 0) { |
| 1793 | AI->addAttr(llvm::AttributeSet::get(getLLVMContext(), |
| 1794 | AI->getArgNo() + 1, |
| 1795 | llvm::Attribute::NonNull)); |
| 1796 | } |
| 1797 | } |
| 1798 | } else if (const auto *ArrTy = |
| 1799 | getContext().getAsVariableArrayType(OTy)) { |
| 1800 | // For C99 VLAs with the static keyword, we don't know the size so |
| 1801 | // we can't use the dereferenceable attribute, but in addrspace(0) |
| 1802 | // we know that it must be nonnull. |
| 1803 | if (ArrTy->getSizeModifier() == VariableArrayType::Static && |
| 1804 | !getContext().getTargetAddressSpace(ArrTy->getElementType())) |
| 1805 | AI->addAttr(llvm::AttributeSet::get(getLLVMContext(), |
| 1806 | AI->getArgNo() + 1, |
| 1807 | llvm::Attribute::NonNull)); |
| 1808 | } |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 1809 | |
| 1810 | const auto *AVAttr = PVD->getAttr<AlignValueAttr>(); |
| 1811 | if (!AVAttr) |
| 1812 | if (const auto *TOTy = dyn_cast<TypedefType>(OTy)) |
| 1813 | AVAttr = TOTy->getDecl()->getAttr<AlignValueAttr>(); |
| 1814 | if (AVAttr) { |
| 1815 | llvm::Value *AlignmentValue = |
| 1816 | EmitScalarExpr(AVAttr->getAlignment()); |
| 1817 | llvm::ConstantInt *AlignmentCI = |
| 1818 | cast<llvm::ConstantInt>(AlignmentValue); |
| 1819 | unsigned Alignment = |
| 1820 | std::min((unsigned) AlignmentCI->getZExtValue(), |
| 1821 | +llvm::Value::MaximumAlignment); |
| 1822 | |
| 1823 | llvm::AttrBuilder Attrs; |
| 1824 | Attrs.addAlignmentAttr(Alignment); |
| 1825 | AI->addAttr(llvm::AttributeSet::get(getLLVMContext(), |
| 1826 | AI->getArgNo() + 1, Attrs)); |
| 1827 | } |
Hal Finkel | 48d53e2 | 2014-07-19 01:41:07 +0000 | [diff] [blame] | 1828 | } |
| 1829 | |
Bill Wendling | 507c351 | 2012-10-16 05:23:44 +0000 | [diff] [blame] | 1830 | if (Arg->getType().isRestrictQualified()) |
Bill Wendling | ce2f9c5 | 2013-01-23 06:15:10 +0000 | [diff] [blame] | 1831 | AI->addAttr(llvm::AttributeSet::get(getLLVMContext(), |
| 1832 | AI->getArgNo() + 1, |
| 1833 | llvm::Attribute::NoAlias)); |
John McCall | 39ec71f | 2010-03-27 00:47:27 +0000 | [diff] [blame] | 1834 | |
Chris Lattner | 7369c14 | 2011-07-20 06:29:00 +0000 | [diff] [blame] | 1835 | // Ensure the argument is the correct type. |
| 1836 | if (V->getType() != ArgI.getCoerceToType()) |
| 1837 | V = Builder.CreateBitCast(V, ArgI.getCoerceToType()); |
| 1838 | |
John McCall | a738c25 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1839 | if (isPromoted) |
| 1840 | V = emitArgumentDemotion(*this, Arg, V); |
Rafael Espindola | 8778c28 | 2012-11-29 16:09:03 +0000 | [diff] [blame] | 1841 | |
Nick Lewycky | 5fa40c3 | 2013-10-01 21:51:38 +0000 | [diff] [blame] | 1842 | if (const CXXMethodDecl *MD = |
| 1843 | dyn_cast_or_null<CXXMethodDecl>(CurCodeDecl)) { |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 1844 | if (MD->isVirtual() && Arg == CXXABIThisDecl) |
Nick Lewycky | 5fa40c3 | 2013-10-01 21:51:38 +0000 | [diff] [blame] | 1845 | V = CGM.getCXXABI(). |
| 1846 | adjustThisParameterInVirtualFunctionPrologue(*this, CurGD, V); |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 1847 | } |
| 1848 | |
Rafael Espindola | 8778c28 | 2012-11-29 16:09:03 +0000 | [diff] [blame] | 1849 | // Because of merging of function types from multiple decls it is |
| 1850 | // possible for the type of an argument to not match the corresponding |
| 1851 | // type in the function type. Since we are codegening the callee |
| 1852 | // in here, add a cast to the argument type. |
| 1853 | llvm::Type *LTy = ConvertType(Arg->getType()); |
| 1854 | if (V->getType() != LTy) |
| 1855 | V = Builder.CreateBitCast(V, LTy); |
| 1856 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1857 | ArgVals.push_back(ValueAndIsPtr(V, HaveValue)); |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1858 | break; |
Daniel Dunbar | d5f1f55 | 2009-02-10 00:06:49 +0000 | [diff] [blame] | 1859 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1860 | |
Evgeniy Stepanov | 3fae4ae | 2012-02-10 09:30:15 +0000 | [diff] [blame] | 1861 | llvm::AllocaInst *Alloca = CreateMemTemp(Ty, Arg->getName()); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1862 | |
Chris Lattner | ff941a6 | 2010-07-28 18:24:28 +0000 | [diff] [blame] | 1863 | // The alignment we need to use is the max of the requested alignment for |
| 1864 | // the argument plus the alignment required by our access code below. |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1865 | unsigned AlignmentToUse = |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 1866 | CGM.getDataLayout().getABITypeAlignment(ArgI.getCoerceToType()); |
Chris Lattner | ff941a6 | 2010-07-28 18:24:28 +0000 | [diff] [blame] | 1867 | AlignmentToUse = std::max(AlignmentToUse, |
| 1868 | (unsigned)getContext().getDeclAlign(Arg).getQuantity()); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1869 | |
Chris Lattner | ff941a6 | 2010-07-28 18:24:28 +0000 | [diff] [blame] | 1870 | Alloca->setAlignment(AlignmentToUse); |
Chris Lattner | c401de9 | 2010-07-05 20:21:00 +0000 | [diff] [blame] | 1871 | llvm::Value *V = Alloca; |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 1872 | llvm::Value *Ptr = V; // Pointer to store into. |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1873 | |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 1874 | // If the value is offset in memory, apply the offset now. |
| 1875 | if (unsigned Offs = ArgI.getDirectOffset()) { |
| 1876 | Ptr = Builder.CreateBitCast(Ptr, Builder.getInt8PtrTy()); |
| 1877 | Ptr = Builder.CreateConstGEP1_32(Ptr, Offs); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1878 | Ptr = Builder.CreateBitCast(Ptr, |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 1879 | llvm::PointerType::getUnqual(ArgI.getCoerceToType())); |
| 1880 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1881 | |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 1882 | // Fast-isel and the optimizer generally like scalar values better than |
| 1883 | // FCAs, so we flatten them if this is safe to do for this argument. |
Evgeniy Stepanov | 3fae4ae | 2012-02-10 09:30:15 +0000 | [diff] [blame] | 1884 | llvm::StructType *STy = dyn_cast<llvm::StructType>(ArgI.getCoerceToType()); |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 1885 | if (ArgI.isDirect() && ArgI.getCanBeFlattened() && STy && |
| 1886 | STy->getNumElements() > 1) { |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 1887 | uint64_t SrcSize = CGM.getDataLayout().getTypeAllocSize(STy); |
Evgeniy Stepanov | 3fae4ae | 2012-02-10 09:30:15 +0000 | [diff] [blame] | 1888 | llvm::Type *DstTy = |
| 1889 | cast<llvm::PointerType>(Ptr->getType())->getElementType(); |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 1890 | uint64_t DstSize = CGM.getDataLayout().getTypeAllocSize(DstTy); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1891 | |
Evgeniy Stepanov | 3fae4ae | 2012-02-10 09:30:15 +0000 | [diff] [blame] | 1892 | if (SrcSize <= DstSize) { |
| 1893 | Ptr = Builder.CreateBitCast(Ptr, llvm::PointerType::getUnqual(STy)); |
| 1894 | |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 1895 | assert(STy->getNumElements() == NumIRArgs); |
Evgeniy Stepanov | 3fae4ae | 2012-02-10 09:30:15 +0000 | [diff] [blame] | 1896 | for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 1897 | auto AI = FnArgs[FirstIRArg + i]; |
Evgeniy Stepanov | 3fae4ae | 2012-02-10 09:30:15 +0000 | [diff] [blame] | 1898 | AI->setName(Arg->getName() + ".coerce" + Twine(i)); |
| 1899 | llvm::Value *EltPtr = Builder.CreateConstGEP2_32(Ptr, 0, i); |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 1900 | Builder.CreateStore(AI, EltPtr); |
Evgeniy Stepanov | 3fae4ae | 2012-02-10 09:30:15 +0000 | [diff] [blame] | 1901 | } |
| 1902 | } else { |
| 1903 | llvm::AllocaInst *TempAlloca = |
| 1904 | CreateTempAlloca(ArgI.getCoerceToType(), "coerce"); |
| 1905 | TempAlloca->setAlignment(AlignmentToUse); |
| 1906 | llvm::Value *TempV = TempAlloca; |
| 1907 | |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 1908 | assert(STy->getNumElements() == NumIRArgs); |
Evgeniy Stepanov | 3fae4ae | 2012-02-10 09:30:15 +0000 | [diff] [blame] | 1909 | for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 1910 | auto AI = FnArgs[FirstIRArg + i]; |
Evgeniy Stepanov | 3fae4ae | 2012-02-10 09:30:15 +0000 | [diff] [blame] | 1911 | AI->setName(Arg->getName() + ".coerce" + Twine(i)); |
| 1912 | llvm::Value *EltPtr = Builder.CreateConstGEP2_32(TempV, 0, i); |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 1913 | Builder.CreateStore(AI, EltPtr); |
Evgeniy Stepanov | 3fae4ae | 2012-02-10 09:30:15 +0000 | [diff] [blame] | 1914 | } |
| 1915 | |
| 1916 | Builder.CreateMemCpy(Ptr, TempV, DstSize, AlignmentToUse); |
Chris Lattner | 15ec361 | 2010-06-29 00:06:42 +0000 | [diff] [blame] | 1917 | } |
| 1918 | } else { |
| 1919 | // Simple case, just do a coerced store of the argument into the alloca. |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 1920 | assert(NumIRArgs == 1); |
| 1921 | auto AI = FnArgs[FirstIRArg]; |
Chris Lattner | 9e748e9 | 2010-06-29 00:14:52 +0000 | [diff] [blame] | 1922 | AI->setName(Arg->getName() + ".coerce"); |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 1923 | CreateCoercedStore(AI, Ptr, /*DestIsVolatile=*/false, *this); |
Chris Lattner | 15ec361 | 2010-06-29 00:06:42 +0000 | [diff] [blame] | 1924 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1925 | |
| 1926 | |
Daniel Dunbar | 2f219b0 | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 1927 | // Match to what EmitParmDecl is expecting for this type. |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 1928 | if (CodeGenFunction::hasScalarEvaluationKind(Ty)) { |
Nick Lewycky | 2d84e84 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 1929 | V = EmitLoadOfScalar(V, false, AlignmentToUse, Ty, Arg->getLocStart()); |
John McCall | a738c25 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1930 | if (isPromoted) |
| 1931 | V = emitArgumentDemotion(*this, Arg, V); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1932 | ArgVals.push_back(ValueAndIsPtr(V, HaveValue)); |
| 1933 | } else { |
| 1934 | ArgVals.push_back(ValueAndIsPtr(V, HavePointer)); |
Daniel Dunbar | 6e3b7df | 2009-02-04 07:22:24 +0000 | [diff] [blame] | 1935 | } |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 1936 | break; |
Daniel Dunbar | 2f219b0 | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 1937 | } |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1938 | |
| 1939 | case ABIArgInfo::Expand: { |
| 1940 | // If this structure was expanded into multiple arguments then |
| 1941 | // we need to create a temporary and reconstruct it from the |
| 1942 | // arguments. |
Eli Friedman | 3d9f47f | 2011-11-03 21:39:02 +0000 | [diff] [blame] | 1943 | llvm::AllocaInst *Alloca = CreateMemTemp(Ty); |
Eli Friedman | a0544d6 | 2011-12-03 04:14:32 +0000 | [diff] [blame] | 1944 | CharUnits Align = getContext().getDeclAlign(Arg); |
| 1945 | Alloca->setAlignment(Align.getQuantity()); |
| 1946 | LValue LV = MakeAddrLValue(Alloca, Ty, Align); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1947 | ArgVals.push_back(ValueAndIsPtr(Alloca, HavePointer)); |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1948 | |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 1949 | auto FnArgIter = FnArgs.begin() + FirstIRArg; |
| 1950 | ExpandTypeFromArgs(Ty, LV, FnArgIter); |
| 1951 | assert(FnArgIter == FnArgs.begin() + FirstIRArg + NumIRArgs); |
| 1952 | for (unsigned i = 0, e = NumIRArgs; i != e; ++i) { |
| 1953 | auto AI = FnArgs[FirstIRArg + i]; |
| 1954 | AI->setName(Arg->getName() + "." + Twine(i)); |
| 1955 | } |
| 1956 | break; |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1957 | } |
| 1958 | |
| 1959 | case ABIArgInfo::Ignore: |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 1960 | assert(NumIRArgs == 0); |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1961 | // Initialize the local variable appropriately. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1962 | if (!hasScalarEvaluationKind(Ty)) { |
| 1963 | ArgVals.push_back(ValueAndIsPtr(CreateMemTemp(Ty), HavePointer)); |
| 1964 | } else { |
| 1965 | llvm::Value *U = llvm::UndefValue::get(ConvertType(Arg->getType())); |
| 1966 | ArgVals.push_back(ValueAndIsPtr(U, HaveValue)); |
| 1967 | } |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 1968 | break; |
Daniel Dunbar | d3674e6 | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1969 | } |
Daniel Dunbar | 613855c | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1970 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1971 | |
Reid Kleckner | 739756c | 2013-12-04 19:23:12 +0000 | [diff] [blame] | 1972 | if (getTarget().getCXXABI().areArgsDestroyedLeftToRightInCallee()) { |
| 1973 | for (int I = Args.size() - 1; I >= 0; --I) |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1974 | EmitParmDecl(*Args[I], ArgVals[I].getPointer(), ArgVals[I].getInt(), |
| 1975 | I + 1); |
Reid Kleckner | 739756c | 2013-12-04 19:23:12 +0000 | [diff] [blame] | 1976 | } else { |
| 1977 | for (unsigned I = 0, E = Args.size(); I != E; ++I) |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1978 | EmitParmDecl(*Args[I], ArgVals[I].getPointer(), ArgVals[I].getInt(), |
| 1979 | I + 1); |
Reid Kleckner | 739756c | 2013-12-04 19:23:12 +0000 | [diff] [blame] | 1980 | } |
Daniel Dunbar | 613855c | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1981 | } |
| 1982 | |
John McCall | ffa2c1a | 2012-01-29 07:46:59 +0000 | [diff] [blame] | 1983 | static void eraseUnusedBitCasts(llvm::Instruction *insn) { |
| 1984 | while (insn->use_empty()) { |
| 1985 | llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(insn); |
| 1986 | if (!bitcast) return; |
| 1987 | |
| 1988 | // This is "safe" because we would have used a ConstantExpr otherwise. |
| 1989 | insn = cast<llvm::Instruction>(bitcast->getOperand(0)); |
| 1990 | bitcast->eraseFromParent(); |
| 1991 | } |
| 1992 | } |
| 1993 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1994 | /// Try to emit a fused autorelease of a return result. |
| 1995 | static llvm::Value *tryEmitFusedAutoreleaseOfResult(CodeGenFunction &CGF, |
| 1996 | llvm::Value *result) { |
| 1997 | // We must be immediately followed the cast. |
| 1998 | llvm::BasicBlock *BB = CGF.Builder.GetInsertBlock(); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1999 | if (BB->empty()) return nullptr; |
| 2000 | if (&BB->back() != result) return nullptr; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2001 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2002 | llvm::Type *resultType = result->getType(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2003 | |
| 2004 | // result is in a BasicBlock and is therefore an Instruction. |
| 2005 | llvm::Instruction *generator = cast<llvm::Instruction>(result); |
| 2006 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2007 | SmallVector<llvm::Instruction*,4> insnsToKill; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2008 | |
| 2009 | // Look for: |
| 2010 | // %generator = bitcast %type1* %generator2 to %type2* |
| 2011 | while (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(generator)) { |
| 2012 | // We would have emitted this as a constant if the operand weren't |
| 2013 | // an Instruction. |
| 2014 | generator = cast<llvm::Instruction>(bitcast->getOperand(0)); |
| 2015 | |
| 2016 | // Require the generator to be immediately followed by the cast. |
| 2017 | if (generator->getNextNode() != bitcast) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2018 | return nullptr; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2019 | |
| 2020 | insnsToKill.push_back(bitcast); |
| 2021 | } |
| 2022 | |
| 2023 | // Look for: |
| 2024 | // %generator = call i8* @objc_retain(i8* %originalResult) |
| 2025 | // or |
| 2026 | // %generator = call i8* @objc_retainAutoreleasedReturnValue(i8* %originalResult) |
| 2027 | llvm::CallInst *call = dyn_cast<llvm::CallInst>(generator); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2028 | if (!call) return nullptr; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2029 | |
| 2030 | bool doRetainAutorelease; |
| 2031 | |
| 2032 | if (call->getCalledValue() == CGF.CGM.getARCEntrypoints().objc_retain) { |
| 2033 | doRetainAutorelease = true; |
| 2034 | } else if (call->getCalledValue() == CGF.CGM.getARCEntrypoints() |
| 2035 | .objc_retainAutoreleasedReturnValue) { |
| 2036 | doRetainAutorelease = false; |
| 2037 | |
John McCall | cfa4e9b | 2012-09-07 23:30:50 +0000 | [diff] [blame] | 2038 | // If we emitted an assembly marker for this call (and the |
| 2039 | // ARCEntrypoints field should have been set if so), go looking |
| 2040 | // for that call. If we can't find it, we can't do this |
| 2041 | // optimization. But it should always be the immediately previous |
| 2042 | // instruction, unless we needed bitcasts around the call. |
| 2043 | if (CGF.CGM.getARCEntrypoints().retainAutoreleasedReturnValueMarker) { |
| 2044 | llvm::Instruction *prev = call->getPrevNode(); |
| 2045 | assert(prev); |
| 2046 | if (isa<llvm::BitCastInst>(prev)) { |
| 2047 | prev = prev->getPrevNode(); |
| 2048 | assert(prev); |
| 2049 | } |
| 2050 | assert(isa<llvm::CallInst>(prev)); |
| 2051 | assert(cast<llvm::CallInst>(prev)->getCalledValue() == |
| 2052 | CGF.CGM.getARCEntrypoints().retainAutoreleasedReturnValueMarker); |
| 2053 | insnsToKill.push_back(prev); |
| 2054 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2055 | } else { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2056 | return nullptr; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2057 | } |
| 2058 | |
| 2059 | result = call->getArgOperand(0); |
| 2060 | insnsToKill.push_back(call); |
| 2061 | |
| 2062 | // Keep killing bitcasts, for sanity. Note that we no longer care |
| 2063 | // about precise ordering as long as there's exactly one use. |
| 2064 | while (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(result)) { |
| 2065 | if (!bitcast->hasOneUse()) break; |
| 2066 | insnsToKill.push_back(bitcast); |
| 2067 | result = bitcast->getOperand(0); |
| 2068 | } |
| 2069 | |
| 2070 | // Delete all the unnecessary instructions, from latest to earliest. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2071 | for (SmallVectorImpl<llvm::Instruction*>::iterator |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2072 | i = insnsToKill.begin(), e = insnsToKill.end(); i != e; ++i) |
| 2073 | (*i)->eraseFromParent(); |
| 2074 | |
| 2075 | // Do the fused retain/autorelease if we were asked to. |
| 2076 | if (doRetainAutorelease) |
| 2077 | result = CGF.EmitARCRetainAutoreleaseReturnValue(result); |
| 2078 | |
| 2079 | // Cast back to the result type. |
| 2080 | return CGF.Builder.CreateBitCast(result, resultType); |
| 2081 | } |
| 2082 | |
John McCall | ffa2c1a | 2012-01-29 07:46:59 +0000 | [diff] [blame] | 2083 | /// If this is a +1 of the value of an immutable 'self', remove it. |
| 2084 | static llvm::Value *tryRemoveRetainOfSelf(CodeGenFunction &CGF, |
| 2085 | llvm::Value *result) { |
| 2086 | // This is only applicable to a method with an immutable 'self'. |
John McCall | ff755cd | 2012-07-31 00:33:55 +0000 | [diff] [blame] | 2087 | const ObjCMethodDecl *method = |
| 2088 | dyn_cast_or_null<ObjCMethodDecl>(CGF.CurCodeDecl); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2089 | if (!method) return nullptr; |
John McCall | ffa2c1a | 2012-01-29 07:46:59 +0000 | [diff] [blame] | 2090 | const VarDecl *self = method->getSelfDecl(); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2091 | if (!self->getType().isConstQualified()) return nullptr; |
John McCall | ffa2c1a | 2012-01-29 07:46:59 +0000 | [diff] [blame] | 2092 | |
| 2093 | // Look for a retain call. |
| 2094 | llvm::CallInst *retainCall = |
| 2095 | dyn_cast<llvm::CallInst>(result->stripPointerCasts()); |
| 2096 | if (!retainCall || |
| 2097 | retainCall->getCalledValue() != CGF.CGM.getARCEntrypoints().objc_retain) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2098 | return nullptr; |
John McCall | ffa2c1a | 2012-01-29 07:46:59 +0000 | [diff] [blame] | 2099 | |
| 2100 | // Look for an ordinary load of 'self'. |
| 2101 | llvm::Value *retainedValue = retainCall->getArgOperand(0); |
| 2102 | llvm::LoadInst *load = |
| 2103 | dyn_cast<llvm::LoadInst>(retainedValue->stripPointerCasts()); |
| 2104 | if (!load || load->isAtomic() || load->isVolatile() || |
| 2105 | load->getPointerOperand() != CGF.GetAddrOfLocalVar(self)) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2106 | return nullptr; |
John McCall | ffa2c1a | 2012-01-29 07:46:59 +0000 | [diff] [blame] | 2107 | |
| 2108 | // Okay! Burn it all down. This relies for correctness on the |
| 2109 | // assumption that the retain is emitted as part of the return and |
| 2110 | // that thereafter everything is used "linearly". |
| 2111 | llvm::Type *resultType = result->getType(); |
| 2112 | eraseUnusedBitCasts(cast<llvm::Instruction>(result)); |
| 2113 | assert(retainCall->use_empty()); |
| 2114 | retainCall->eraseFromParent(); |
| 2115 | eraseUnusedBitCasts(cast<llvm::Instruction>(retainedValue)); |
| 2116 | |
| 2117 | return CGF.Builder.CreateBitCast(load, resultType); |
| 2118 | } |
| 2119 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2120 | /// Emit an ARC autorelease of the result of a function. |
John McCall | ffa2c1a | 2012-01-29 07:46:59 +0000 | [diff] [blame] | 2121 | /// |
| 2122 | /// \return the value to actually return from the function |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2123 | static llvm::Value *emitAutoreleaseOfResult(CodeGenFunction &CGF, |
| 2124 | llvm::Value *result) { |
John McCall | ffa2c1a | 2012-01-29 07:46:59 +0000 | [diff] [blame] | 2125 | // If we're returning 'self', kill the initial retain. This is a |
| 2126 | // heuristic attempt to "encourage correctness" in the really unfortunate |
| 2127 | // case where we have a return of self during a dealloc and we desperately |
| 2128 | // need to avoid the possible autorelease. |
| 2129 | if (llvm::Value *self = tryRemoveRetainOfSelf(CGF, result)) |
| 2130 | return self; |
| 2131 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2132 | // At -O0, try to emit a fused retain/autorelease. |
| 2133 | if (CGF.shouldUseFusedARCCalls()) |
| 2134 | if (llvm::Value *fused = tryEmitFusedAutoreleaseOfResult(CGF, result)) |
| 2135 | return fused; |
| 2136 | |
| 2137 | return CGF.EmitARCAutoreleaseReturnValue(result); |
| 2138 | } |
| 2139 | |
John McCall | 6e1c012 | 2012-01-29 02:35:02 +0000 | [diff] [blame] | 2140 | /// Heuristically search for a dominating store to the return-value slot. |
| 2141 | static llvm::StoreInst *findDominatingStoreToReturnValue(CodeGenFunction &CGF) { |
| 2142 | // If there are multiple uses of the return-value slot, just check |
| 2143 | // for something immediately preceding the IP. Sometimes this can |
| 2144 | // happen with how we generate implicit-returns; it can also happen |
| 2145 | // with noreturn cleanups. |
| 2146 | if (!CGF.ReturnValue->hasOneUse()) { |
| 2147 | llvm::BasicBlock *IP = CGF.Builder.GetInsertBlock(); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2148 | if (IP->empty()) return nullptr; |
John McCall | 6e1c012 | 2012-01-29 02:35:02 +0000 | [diff] [blame] | 2149 | llvm::StoreInst *store = dyn_cast<llvm::StoreInst>(&IP->back()); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2150 | if (!store) return nullptr; |
| 2151 | if (store->getPointerOperand() != CGF.ReturnValue) return nullptr; |
John McCall | 6e1c012 | 2012-01-29 02:35:02 +0000 | [diff] [blame] | 2152 | assert(!store->isAtomic() && !store->isVolatile()); // see below |
| 2153 | return store; |
| 2154 | } |
| 2155 | |
| 2156 | llvm::StoreInst *store = |
Chandler Carruth | 4d01fff | 2014-03-09 03:16:50 +0000 | [diff] [blame] | 2157 | dyn_cast<llvm::StoreInst>(CGF.ReturnValue->user_back()); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2158 | if (!store) return nullptr; |
John McCall | 6e1c012 | 2012-01-29 02:35:02 +0000 | [diff] [blame] | 2159 | |
| 2160 | // These aren't actually possible for non-coerced returns, and we |
| 2161 | // only care about non-coerced returns on this code path. |
| 2162 | assert(!store->isAtomic() && !store->isVolatile()); |
| 2163 | |
| 2164 | // Now do a first-and-dirty dominance check: just walk up the |
| 2165 | // single-predecessors chain from the current insertion point. |
| 2166 | llvm::BasicBlock *StoreBB = store->getParent(); |
| 2167 | llvm::BasicBlock *IP = CGF.Builder.GetInsertBlock(); |
| 2168 | while (IP != StoreBB) { |
| 2169 | if (!(IP = IP->getSinglePredecessor())) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2170 | return nullptr; |
John McCall | 6e1c012 | 2012-01-29 02:35:02 +0000 | [diff] [blame] | 2171 | } |
| 2172 | |
| 2173 | // Okay, the store's basic block dominates the insertion point; we |
| 2174 | // can do our thing. |
| 2175 | return store; |
| 2176 | } |
| 2177 | |
Adrian Prantl | 3be1054 | 2013-05-02 17:30:20 +0000 | [diff] [blame] | 2178 | void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI, |
Nick Lewycky | 2d84e84 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 2179 | bool EmitRetDbgLoc, |
| 2180 | SourceLocation EndLoc) { |
Hans Wennborg | d71907d | 2014-09-04 22:16:33 +0000 | [diff] [blame] | 2181 | if (CurCodeDecl && CurCodeDecl->hasAttr<NakedAttr>()) { |
| 2182 | // Naked functions don't have epilogues. |
| 2183 | Builder.CreateUnreachable(); |
| 2184 | return; |
| 2185 | } |
| 2186 | |
Daniel Dunbar | a72d4ae | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 2187 | // Functions with no result always return void. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2188 | if (!ReturnValue) { |
Daniel Dunbar | a72d4ae | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 2189 | Builder.CreateRetVoid(); |
Chris Lattner | 726b3d0 | 2010-06-26 23:13:19 +0000 | [diff] [blame] | 2190 | return; |
Daniel Dunbar | a72d4ae | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 2191 | } |
Daniel Dunbar | 6696e22 | 2010-06-30 21:27:58 +0000 | [diff] [blame] | 2192 | |
Dan Gohman | 481e40c | 2010-07-20 20:13:52 +0000 | [diff] [blame] | 2193 | llvm::DebugLoc RetDbgLoc; |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2194 | llvm::Value *RV = nullptr; |
Chris Lattner | 726b3d0 | 2010-06-26 23:13:19 +0000 | [diff] [blame] | 2195 | QualType RetTy = FI.getReturnType(); |
| 2196 | const ABIArgInfo &RetAI = FI.getReturnInfo(); |
| 2197 | |
| 2198 | switch (RetAI.getKind()) { |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 2199 | case ABIArgInfo::InAlloca: |
Reid Kleckner | fab1e89 | 2014-02-25 00:59:14 +0000 | [diff] [blame] | 2200 | // Aggregrates get evaluated directly into the destination. Sometimes we |
| 2201 | // need to return the sret value in a register, though. |
| 2202 | assert(hasAggregateEvaluationKind(RetTy)); |
| 2203 | if (RetAI.getInAllocaSRet()) { |
| 2204 | llvm::Function::arg_iterator EI = CurFn->arg_end(); |
| 2205 | --EI; |
| 2206 | llvm::Value *ArgStruct = EI; |
| 2207 | llvm::Value *SRet = |
| 2208 | Builder.CreateStructGEP(ArgStruct, RetAI.getInAllocaFieldIndex()); |
| 2209 | RV = Builder.CreateLoad(SRet, "sret"); |
| 2210 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 2211 | break; |
| 2212 | |
Daniel Dunbar | 0381634 | 2010-08-21 02:24:36 +0000 | [diff] [blame] | 2213 | case ABIArgInfo::Indirect: { |
Reid Kleckner | 37abaca | 2014-05-09 22:46:15 +0000 | [diff] [blame] | 2214 | auto AI = CurFn->arg_begin(); |
| 2215 | if (RetAI.isSRetAfterThis()) |
| 2216 | ++AI; |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 2217 | switch (getEvaluationKind(RetTy)) { |
| 2218 | case TEK_Complex: { |
| 2219 | ComplexPairTy RT = |
Nick Lewycky | 2d84e84 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 2220 | EmitLoadOfComplex(MakeNaturalAlignAddrLValue(ReturnValue, RetTy), |
| 2221 | EndLoc); |
Reid Kleckner | 37abaca | 2014-05-09 22:46:15 +0000 | [diff] [blame] | 2222 | EmitStoreOfComplex(RT, MakeNaturalAlignAddrLValue(AI, RetTy), |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 2223 | /*isInit*/ true); |
| 2224 | break; |
| 2225 | } |
| 2226 | case TEK_Aggregate: |
Chris Lattner | 726b3d0 | 2010-06-26 23:13:19 +0000 | [diff] [blame] | 2227 | // Do nothing; aggregrates get evaluated directly into the destination. |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 2228 | break; |
| 2229 | case TEK_Scalar: |
| 2230 | EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), |
Reid Kleckner | 37abaca | 2014-05-09 22:46:15 +0000 | [diff] [blame] | 2231 | MakeNaturalAlignAddrLValue(AI, RetTy), |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 2232 | /*isInit*/ true); |
| 2233 | break; |
Chris Lattner | 726b3d0 | 2010-06-26 23:13:19 +0000 | [diff] [blame] | 2234 | } |
| 2235 | break; |
Daniel Dunbar | 0381634 | 2010-08-21 02:24:36 +0000 | [diff] [blame] | 2236 | } |
Chris Lattner | 726b3d0 | 2010-06-26 23:13:19 +0000 | [diff] [blame] | 2237 | |
| 2238 | case ABIArgInfo::Extend: |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2239 | case ABIArgInfo::Direct: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2240 | if (RetAI.getCoerceToType() == ConvertType(RetTy) && |
| 2241 | RetAI.getDirectOffset() == 0) { |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2242 | // The internal return value temp always will have pointer-to-return-type |
| 2243 | // type, just do a load. |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2244 | |
John McCall | 6e1c012 | 2012-01-29 02:35:02 +0000 | [diff] [blame] | 2245 | // If there is a dominating store to ReturnValue, we can elide |
| 2246 | // the load, zap the store, and usually zap the alloca. |
| 2247 | if (llvm::StoreInst *SI = findDominatingStoreToReturnValue(*this)) { |
Adrian Prantl | 4c9a38a | 2013-05-30 18:12:23 +0000 | [diff] [blame] | 2248 | // Reuse the debug location from the store unless there is |
| 2249 | // cleanup code to be emitted between the store and return |
| 2250 | // instruction. |
| 2251 | if (EmitRetDbgLoc && !AutoreleaseResult) |
Adrian Prantl | 3be1054 | 2013-05-02 17:30:20 +0000 | [diff] [blame] | 2252 | RetDbgLoc = SI->getDebugLoc(); |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2253 | // Get the stored value and nuke the now-dead store. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2254 | RV = SI->getValueOperand(); |
| 2255 | SI->eraseFromParent(); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2256 | |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2257 | // If that was the only use of the return value, nuke it as well now. |
| 2258 | if (ReturnValue->use_empty() && isa<llvm::AllocaInst>(ReturnValue)) { |
| 2259 | cast<llvm::AllocaInst>(ReturnValue)->eraseFromParent(); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2260 | ReturnValue = nullptr; |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2261 | } |
John McCall | 6e1c012 | 2012-01-29 02:35:02 +0000 | [diff] [blame] | 2262 | |
| 2263 | // Otherwise, we have to do a simple load. |
| 2264 | } else { |
| 2265 | RV = Builder.CreateLoad(ReturnValue); |
Chris Lattner | 3fcc790 | 2010-06-27 01:06:27 +0000 | [diff] [blame] | 2266 | } |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2267 | } else { |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2268 | llvm::Value *V = ReturnValue; |
| 2269 | // If the value is offset in memory, apply the offset now. |
| 2270 | if (unsigned Offs = RetAI.getDirectOffset()) { |
| 2271 | V = Builder.CreateBitCast(V, Builder.getInt8PtrTy()); |
| 2272 | V = Builder.CreateConstGEP1_32(V, Offs); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2273 | V = Builder.CreateBitCast(V, |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2274 | llvm::PointerType::getUnqual(RetAI.getCoerceToType())); |
| 2275 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2276 | |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2277 | RV = CreateCoercedLoad(V, RetAI.getCoerceToType(), *this); |
Chris Lattner | 3fcc790 | 2010-06-27 01:06:27 +0000 | [diff] [blame] | 2278 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2279 | |
| 2280 | // In ARC, end functions that return a retainable type with a call |
| 2281 | // to objc_autoreleaseReturnValue. |
| 2282 | if (AutoreleaseResult) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2283 | assert(getLangOpts().ObjCAutoRefCount && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2284 | !FI.isReturnsRetained() && |
| 2285 | RetTy->isObjCRetainableType()); |
| 2286 | RV = emitAutoreleaseOfResult(*this, RV); |
| 2287 | } |
| 2288 | |
Chris Lattner | 726b3d0 | 2010-06-26 23:13:19 +0000 | [diff] [blame] | 2289 | break; |
Chris Lattner | 726b3d0 | 2010-06-26 23:13:19 +0000 | [diff] [blame] | 2290 | |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2291 | case ABIArgInfo::Ignore: |
Chris Lattner | 726b3d0 | 2010-06-26 23:13:19 +0000 | [diff] [blame] | 2292 | break; |
| 2293 | |
| 2294 | case ABIArgInfo::Expand: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2295 | llvm_unreachable("Invalid ABI kind for return argument"); |
Chris Lattner | 726b3d0 | 2010-06-26 23:13:19 +0000 | [diff] [blame] | 2296 | } |
| 2297 | |
Alexey Samsonov | de443c5 | 2014-08-13 00:26:40 +0000 | [diff] [blame] | 2298 | llvm::Instruction *Ret; |
| 2299 | if (RV) { |
Alexey Samsonov | edf99a9 | 2014-11-07 22:29:38 +0000 | [diff] [blame] | 2300 | if (SanOpts.has(SanitizerKind::ReturnsNonnullAttribute)) { |
Alexey Samsonov | 90452df | 2014-09-08 20:17:19 +0000 | [diff] [blame] | 2301 | if (auto RetNNAttr = CurGD.getDecl()->getAttr<ReturnsNonNullAttr>()) { |
| 2302 | SanitizerScope SanScope(this); |
| 2303 | llvm::Value *Cond = Builder.CreateICmpNE( |
| 2304 | RV, llvm::Constant::getNullValue(RV->getType())); |
| 2305 | llvm::Constant *StaticData[] = { |
| 2306 | EmitCheckSourceLocation(EndLoc), |
| 2307 | EmitCheckSourceLocation(RetNNAttr->getLocation()), |
| 2308 | }; |
Alexey Samsonov | e396bfc | 2014-11-11 22:03:54 +0000 | [diff] [blame] | 2309 | EmitCheck(std::make_pair(Cond, SanitizerKind::ReturnsNonnullAttribute), |
| 2310 | "nonnull_return", StaticData, None); |
Alexey Samsonov | 90452df | 2014-09-08 20:17:19 +0000 | [diff] [blame] | 2311 | } |
Alexey Samsonov | de443c5 | 2014-08-13 00:26:40 +0000 | [diff] [blame] | 2312 | } |
| 2313 | Ret = Builder.CreateRet(RV); |
| 2314 | } else { |
| 2315 | Ret = Builder.CreateRetVoid(); |
| 2316 | } |
| 2317 | |
Devang Patel | 6549758 | 2010-07-21 18:08:50 +0000 | [diff] [blame] | 2318 | if (!RetDbgLoc.isUnknown()) |
| 2319 | Ret->setDebugLoc(RetDbgLoc); |
Daniel Dunbar | 613855c | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 2320 | } |
| 2321 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 2322 | static bool isInAllocaArgument(CGCXXABI &ABI, QualType type) { |
| 2323 | const CXXRecordDecl *RD = type->getAsCXXRecordDecl(); |
| 2324 | return RD && ABI.getRecordArgABI(RD) == CGCXXABI::RAA_DirectInMemory; |
| 2325 | } |
| 2326 | |
| 2327 | static AggValueSlot createPlaceholderSlot(CodeGenFunction &CGF, QualType Ty) { |
| 2328 | // FIXME: Generate IR in one pass, rather than going back and fixing up these |
| 2329 | // placeholders. |
| 2330 | llvm::Type *IRTy = CGF.ConvertTypeForMem(Ty); |
| 2331 | llvm::Value *Placeholder = |
| 2332 | llvm::UndefValue::get(IRTy->getPointerTo()->getPointerTo()); |
| 2333 | Placeholder = CGF.Builder.CreateLoad(Placeholder); |
| 2334 | return AggValueSlot::forAddr(Placeholder, CharUnits::Zero(), |
| 2335 | Ty.getQualifiers(), |
| 2336 | AggValueSlot::IsNotDestructed, |
| 2337 | AggValueSlot::DoesNotNeedGCBarriers, |
| 2338 | AggValueSlot::IsNotAliased); |
| 2339 | } |
| 2340 | |
John McCall | 32ea969 | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 2341 | void CodeGenFunction::EmitDelegateCallArg(CallArgList &args, |
Nick Lewycky | 2d84e84 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 2342 | const VarDecl *param, |
| 2343 | SourceLocation loc) { |
John McCall | 23f6626 | 2010-05-26 22:34:26 +0000 | [diff] [blame] | 2344 | // StartFunction converted the ABI-lowered parameter(s) into a |
| 2345 | // local alloca. We need to turn that into an r-value suitable |
| 2346 | // for EmitCall. |
John McCall | 32ea969 | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 2347 | llvm::Value *local = GetAddrOfLocalVar(param); |
John McCall | 23f6626 | 2010-05-26 22:34:26 +0000 | [diff] [blame] | 2348 | |
John McCall | 32ea969 | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 2349 | QualType type = param->getType(); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2350 | |
John McCall | 23f6626 | 2010-05-26 22:34:26 +0000 | [diff] [blame] | 2351 | // For the most part, we just need to load the alloca, except: |
| 2352 | // 1) aggregate r-values are actually pointers to temporaries, and |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 2353 | // 2) references to non-scalars are pointers directly to the aggregate. |
| 2354 | // I don't know why references to scalars are different here. |
John McCall | 32ea969 | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 2355 | if (const ReferenceType *ref = type->getAs<ReferenceType>()) { |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 2356 | if (!hasScalarEvaluationKind(ref->getPointeeType())) |
John McCall | 32ea969 | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 2357 | return args.add(RValue::getAggregate(local), type); |
John McCall | 23f6626 | 2010-05-26 22:34:26 +0000 | [diff] [blame] | 2358 | |
| 2359 | // Locals which are references to scalars are represented |
| 2360 | // with allocas holding the pointer. |
John McCall | 32ea969 | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 2361 | return args.add(RValue::get(Builder.CreateLoad(local)), type); |
John McCall | 23f6626 | 2010-05-26 22:34:26 +0000 | [diff] [blame] | 2362 | } |
| 2363 | |
Reid Kleckner | ab2090d | 2014-07-26 01:34:32 +0000 | [diff] [blame] | 2364 | assert(!isInAllocaArgument(CGM.getCXXABI(), type) && |
| 2365 | "cannot emit delegate call arguments for inalloca arguments!"); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 2366 | |
Nick Lewycky | 2d84e84 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 2367 | args.add(convertTempToRValue(local, type, loc), type); |
John McCall | 23f6626 | 2010-05-26 22:34:26 +0000 | [diff] [blame] | 2368 | } |
| 2369 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2370 | static bool isProvablyNull(llvm::Value *addr) { |
| 2371 | return isa<llvm::ConstantPointerNull>(addr); |
| 2372 | } |
| 2373 | |
| 2374 | static bool isProvablyNonNull(llvm::Value *addr) { |
| 2375 | return isa<llvm::AllocaInst>(addr); |
| 2376 | } |
| 2377 | |
| 2378 | /// Emit the actual writing-back of a writeback. |
| 2379 | static void emitWriteback(CodeGenFunction &CGF, |
| 2380 | const CallArgList::Writeback &writeback) { |
John McCall | eff1884 | 2013-03-23 02:35:54 +0000 | [diff] [blame] | 2381 | const LValue &srcLV = writeback.Source; |
| 2382 | llvm::Value *srcAddr = srcLV.getAddress(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2383 | assert(!isProvablyNull(srcAddr) && |
| 2384 | "shouldn't have writeback for provably null argument"); |
| 2385 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2386 | llvm::BasicBlock *contBB = nullptr; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2387 | |
| 2388 | // If the argument wasn't provably non-null, we need to null check |
| 2389 | // before doing the store. |
| 2390 | bool provablyNonNull = isProvablyNonNull(srcAddr); |
| 2391 | if (!provablyNonNull) { |
| 2392 | llvm::BasicBlock *writebackBB = CGF.createBasicBlock("icr.writeback"); |
| 2393 | contBB = CGF.createBasicBlock("icr.done"); |
| 2394 | |
| 2395 | llvm::Value *isNull = CGF.Builder.CreateIsNull(srcAddr, "icr.isnull"); |
| 2396 | CGF.Builder.CreateCondBr(isNull, contBB, writebackBB); |
| 2397 | CGF.EmitBlock(writebackBB); |
| 2398 | } |
| 2399 | |
| 2400 | // Load the value to writeback. |
| 2401 | llvm::Value *value = CGF.Builder.CreateLoad(writeback.Temporary); |
| 2402 | |
| 2403 | // Cast it back, in case we're writing an id to a Foo* or something. |
| 2404 | value = CGF.Builder.CreateBitCast(value, |
| 2405 | cast<llvm::PointerType>(srcAddr->getType())->getElementType(), |
| 2406 | "icr.writeback-cast"); |
| 2407 | |
| 2408 | // Perform the writeback. |
John McCall | eff1884 | 2013-03-23 02:35:54 +0000 | [diff] [blame] | 2409 | |
| 2410 | // If we have a "to use" value, it's something we need to emit a use |
| 2411 | // of. This has to be carefully threaded in: if it's done after the |
| 2412 | // release it's potentially undefined behavior (and the optimizer |
| 2413 | // will ignore it), and if it happens before the retain then the |
| 2414 | // optimizer could move the release there. |
| 2415 | if (writeback.ToUse) { |
| 2416 | assert(srcLV.getObjCLifetime() == Qualifiers::OCL_Strong); |
| 2417 | |
| 2418 | // Retain the new value. No need to block-copy here: the block's |
| 2419 | // being passed up the stack. |
| 2420 | value = CGF.EmitARCRetainNonBlock(value); |
| 2421 | |
| 2422 | // Emit the intrinsic use here. |
| 2423 | CGF.EmitARCIntrinsicUse(writeback.ToUse); |
| 2424 | |
| 2425 | // Load the old value (primitively). |
Nick Lewycky | 2d84e84 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 2426 | llvm::Value *oldValue = CGF.EmitLoadOfScalar(srcLV, SourceLocation()); |
John McCall | eff1884 | 2013-03-23 02:35:54 +0000 | [diff] [blame] | 2427 | |
| 2428 | // Put the new value in place (primitively). |
| 2429 | CGF.EmitStoreOfScalar(value, srcLV, /*init*/ false); |
| 2430 | |
| 2431 | // Release the old value. |
| 2432 | CGF.EmitARCRelease(oldValue, srcLV.isARCPreciseLifetime()); |
| 2433 | |
| 2434 | // Otherwise, we can just do a normal lvalue store. |
| 2435 | } else { |
| 2436 | CGF.EmitStoreThroughLValue(RValue::get(value), srcLV); |
| 2437 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2438 | |
| 2439 | // Jump to the continuation block. |
| 2440 | if (!provablyNonNull) |
| 2441 | CGF.EmitBlock(contBB); |
| 2442 | } |
| 2443 | |
| 2444 | static void emitWritebacks(CodeGenFunction &CGF, |
| 2445 | const CallArgList &args) { |
Aaron Ballman | 36a7fa8 | 2014-03-17 17:22:27 +0000 | [diff] [blame] | 2446 | for (const auto &I : args.writebacks()) |
| 2447 | emitWriteback(CGF, I); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2448 | } |
| 2449 | |
Reid Kleckner | 23f4c4b | 2013-06-21 12:45:15 +0000 | [diff] [blame] | 2450 | static void deactivateArgCleanupsBeforeCall(CodeGenFunction &CGF, |
| 2451 | const CallArgList &CallArgs) { |
Reid Kleckner | 739756c | 2013-12-04 19:23:12 +0000 | [diff] [blame] | 2452 | assert(CGF.getTarget().getCXXABI().areArgsDestroyedLeftToRightInCallee()); |
Reid Kleckner | 23f4c4b | 2013-06-21 12:45:15 +0000 | [diff] [blame] | 2453 | ArrayRef<CallArgList::CallArgCleanup> Cleanups = |
| 2454 | CallArgs.getCleanupsToDeactivate(); |
| 2455 | // Iterate in reverse to increase the likelihood of popping the cleanup. |
| 2456 | for (ArrayRef<CallArgList::CallArgCleanup>::reverse_iterator |
| 2457 | I = Cleanups.rbegin(), E = Cleanups.rend(); I != E; ++I) { |
| 2458 | CGF.DeactivateCleanupBlock(I->Cleanup, I->IsActiveIP); |
| 2459 | I->IsActiveIP->eraseFromParent(); |
| 2460 | } |
| 2461 | } |
| 2462 | |
John McCall | eff1884 | 2013-03-23 02:35:54 +0000 | [diff] [blame] | 2463 | static const Expr *maybeGetUnaryAddrOfOperand(const Expr *E) { |
| 2464 | if (const UnaryOperator *uop = dyn_cast<UnaryOperator>(E->IgnoreParens())) |
| 2465 | if (uop->getOpcode() == UO_AddrOf) |
| 2466 | return uop->getSubExpr(); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2467 | return nullptr; |
John McCall | eff1884 | 2013-03-23 02:35:54 +0000 | [diff] [blame] | 2468 | } |
| 2469 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2470 | /// Emit an argument that's being passed call-by-writeback. That is, |
| 2471 | /// we are passing the address of |
| 2472 | static void emitWritebackArg(CodeGenFunction &CGF, CallArgList &args, |
| 2473 | const ObjCIndirectCopyRestoreExpr *CRE) { |
John McCall | eff1884 | 2013-03-23 02:35:54 +0000 | [diff] [blame] | 2474 | LValue srcLV; |
| 2475 | |
| 2476 | // Make an optimistic effort to emit the address as an l-value. |
| 2477 | // This can fail if the the argument expression is more complicated. |
| 2478 | if (const Expr *lvExpr = maybeGetUnaryAddrOfOperand(CRE->getSubExpr())) { |
| 2479 | srcLV = CGF.EmitLValue(lvExpr); |
| 2480 | |
| 2481 | // Otherwise, just emit it as a scalar. |
| 2482 | } else { |
| 2483 | llvm::Value *srcAddr = CGF.EmitScalarExpr(CRE->getSubExpr()); |
| 2484 | |
| 2485 | QualType srcAddrType = |
| 2486 | CRE->getSubExpr()->getType()->castAs<PointerType>()->getPointeeType(); |
| 2487 | srcLV = CGF.MakeNaturalAlignAddrLValue(srcAddr, srcAddrType); |
| 2488 | } |
| 2489 | llvm::Value *srcAddr = srcLV.getAddress(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2490 | |
| 2491 | // The dest and src types don't necessarily match in LLVM terms |
| 2492 | // because of the crazy ObjC compatibility rules. |
| 2493 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2494 | llvm::PointerType *destType = |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2495 | cast<llvm::PointerType>(CGF.ConvertType(CRE->getType())); |
| 2496 | |
| 2497 | // If the address is a constant null, just pass the appropriate null. |
| 2498 | if (isProvablyNull(srcAddr)) { |
| 2499 | args.add(RValue::get(llvm::ConstantPointerNull::get(destType)), |
| 2500 | CRE->getType()); |
| 2501 | return; |
| 2502 | } |
| 2503 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2504 | // Create the temporary. |
| 2505 | llvm::Value *temp = CGF.CreateTempAlloca(destType->getElementType(), |
| 2506 | "icr.temp"); |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 2507 | // Loading an l-value can introduce a cleanup if the l-value is __weak, |
| 2508 | // and that cleanup will be conditional if we can't prove that the l-value |
| 2509 | // isn't null, so we need to register a dominating point so that the cleanups |
| 2510 | // system will make valid IR. |
| 2511 | CodeGenFunction::ConditionalEvaluation condEval(CGF); |
| 2512 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2513 | // Zero-initialize it if we're not doing a copy-initialization. |
| 2514 | bool shouldCopy = CRE->shouldCopy(); |
| 2515 | if (!shouldCopy) { |
| 2516 | llvm::Value *null = |
| 2517 | llvm::ConstantPointerNull::get( |
| 2518 | cast<llvm::PointerType>(destType->getElementType())); |
| 2519 | CGF.Builder.CreateStore(null, temp); |
| 2520 | } |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2521 | |
| 2522 | llvm::BasicBlock *contBB = nullptr; |
| 2523 | llvm::BasicBlock *originBB = nullptr; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2524 | |
| 2525 | // If the address is *not* known to be non-null, we need to switch. |
| 2526 | llvm::Value *finalArgument; |
| 2527 | |
| 2528 | bool provablyNonNull = isProvablyNonNull(srcAddr); |
| 2529 | if (provablyNonNull) { |
| 2530 | finalArgument = temp; |
| 2531 | } else { |
| 2532 | llvm::Value *isNull = CGF.Builder.CreateIsNull(srcAddr, "icr.isnull"); |
| 2533 | |
| 2534 | finalArgument = CGF.Builder.CreateSelect(isNull, |
| 2535 | llvm::ConstantPointerNull::get(destType), |
| 2536 | temp, "icr.argument"); |
| 2537 | |
| 2538 | // If we need to copy, then the load has to be conditional, which |
| 2539 | // means we need control flow. |
| 2540 | if (shouldCopy) { |
John McCall | eff1884 | 2013-03-23 02:35:54 +0000 | [diff] [blame] | 2541 | originBB = CGF.Builder.GetInsertBlock(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2542 | contBB = CGF.createBasicBlock("icr.cont"); |
| 2543 | llvm::BasicBlock *copyBB = CGF.createBasicBlock("icr.copy"); |
| 2544 | CGF.Builder.CreateCondBr(isNull, contBB, copyBB); |
| 2545 | CGF.EmitBlock(copyBB); |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 2546 | condEval.begin(CGF); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2547 | } |
| 2548 | } |
| 2549 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2550 | llvm::Value *valueToUse = nullptr; |
John McCall | eff1884 | 2013-03-23 02:35:54 +0000 | [diff] [blame] | 2551 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2552 | // Perform a copy if necessary. |
| 2553 | if (shouldCopy) { |
Nick Lewycky | 2d84e84 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 2554 | RValue srcRV = CGF.EmitLoadOfLValue(srcLV, SourceLocation()); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2555 | assert(srcRV.isScalar()); |
| 2556 | |
| 2557 | llvm::Value *src = srcRV.getScalarVal(); |
| 2558 | src = CGF.Builder.CreateBitCast(src, destType->getElementType(), |
| 2559 | "icr.cast"); |
| 2560 | |
| 2561 | // Use an ordinary store, not a store-to-lvalue. |
| 2562 | CGF.Builder.CreateStore(src, temp); |
John McCall | eff1884 | 2013-03-23 02:35:54 +0000 | [diff] [blame] | 2563 | |
| 2564 | // If optimization is enabled, and the value was held in a |
| 2565 | // __strong variable, we need to tell the optimizer that this |
| 2566 | // value has to stay alive until we're doing the store back. |
| 2567 | // This is because the temporary is effectively unretained, |
| 2568 | // and so otherwise we can violate the high-level semantics. |
| 2569 | if (CGF.CGM.getCodeGenOpts().OptimizationLevel != 0 && |
| 2570 | srcLV.getObjCLifetime() == Qualifiers::OCL_Strong) { |
| 2571 | valueToUse = src; |
| 2572 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2573 | } |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 2574 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2575 | // Finish the control flow if we needed it. |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 2576 | if (shouldCopy && !provablyNonNull) { |
John McCall | eff1884 | 2013-03-23 02:35:54 +0000 | [diff] [blame] | 2577 | llvm::BasicBlock *copyBB = CGF.Builder.GetInsertBlock(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2578 | CGF.EmitBlock(contBB); |
John McCall | eff1884 | 2013-03-23 02:35:54 +0000 | [diff] [blame] | 2579 | |
| 2580 | // Make a phi for the value to intrinsically use. |
| 2581 | if (valueToUse) { |
| 2582 | llvm::PHINode *phiToUse = CGF.Builder.CreatePHI(valueToUse->getType(), 2, |
| 2583 | "icr.to-use"); |
| 2584 | phiToUse->addIncoming(valueToUse, copyBB); |
| 2585 | phiToUse->addIncoming(llvm::UndefValue::get(valueToUse->getType()), |
| 2586 | originBB); |
| 2587 | valueToUse = phiToUse; |
| 2588 | } |
| 2589 | |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 2590 | condEval.end(CGF); |
| 2591 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2592 | |
John McCall | eff1884 | 2013-03-23 02:35:54 +0000 | [diff] [blame] | 2593 | args.addWriteback(srcLV, temp, valueToUse); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2594 | args.add(RValue::get(finalArgument), CRE->getType()); |
| 2595 | } |
| 2596 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 2597 | void CallArgList::allocateArgumentMemory(CodeGenFunction &CGF) { |
| 2598 | assert(!StackBase && !StackCleanup.isValid()); |
| 2599 | |
| 2600 | // Save the stack. |
| 2601 | llvm::Function *F = CGF.CGM.getIntrinsic(llvm::Intrinsic::stacksave); |
| 2602 | StackBase = CGF.Builder.CreateCall(F, "inalloca.save"); |
| 2603 | |
| 2604 | // Control gets really tied up in landing pads, so we have to spill the |
| 2605 | // stacksave to an alloca to avoid violating SSA form. |
| 2606 | // TODO: This is dead if we never emit the cleanup. We should create the |
| 2607 | // alloca and store lazily on the first cleanup emission. |
| 2608 | StackBaseMem = CGF.CreateTempAlloca(CGF.Int8PtrTy, "inalloca.spmem"); |
| 2609 | CGF.Builder.CreateStore(StackBase, StackBaseMem); |
| 2610 | CGF.pushStackRestore(EHCleanup, StackBaseMem); |
| 2611 | StackCleanup = CGF.EHStack.getInnermostEHScope(); |
| 2612 | assert(StackCleanup.isValid()); |
| 2613 | } |
| 2614 | |
| 2615 | void CallArgList::freeArgumentMemory(CodeGenFunction &CGF) const { |
| 2616 | if (StackBase) { |
| 2617 | CGF.DeactivateCleanupBlock(StackCleanup, StackBase); |
| 2618 | llvm::Value *F = CGF.CGM.getIntrinsic(llvm::Intrinsic::stackrestore); |
| 2619 | // We could load StackBase from StackBaseMem, but in the non-exceptional |
| 2620 | // case we can skip it. |
| 2621 | CGF.Builder.CreateCall(F, StackBase); |
| 2622 | } |
| 2623 | } |
| 2624 | |
Alexey Samsonov | 8e1162c | 2014-09-08 17:22:45 +0000 | [diff] [blame] | 2625 | static void emitNonNullArgCheck(CodeGenFunction &CGF, RValue RV, |
| 2626 | QualType ArgType, SourceLocation ArgLoc, |
| 2627 | const FunctionDecl *FD, unsigned ParmNum) { |
Alexey Samsonov | edf99a9 | 2014-11-07 22:29:38 +0000 | [diff] [blame] | 2628 | if (!CGF.SanOpts.has(SanitizerKind::NonnullAttribute) || !FD) |
Alexey Samsonov | 8e1162c | 2014-09-08 17:22:45 +0000 | [diff] [blame] | 2629 | return; |
| 2630 | auto PVD = ParmNum < FD->getNumParams() ? FD->getParamDecl(ParmNum) : nullptr; |
| 2631 | unsigned ArgNo = PVD ? PVD->getFunctionScopeIndex() : ParmNum; |
| 2632 | auto NNAttr = getNonNullAttr(FD, PVD, ArgType, ArgNo); |
| 2633 | if (!NNAttr) |
| 2634 | return; |
| 2635 | CodeGenFunction::SanitizerScope SanScope(&CGF); |
| 2636 | assert(RV.isScalar()); |
| 2637 | llvm::Value *V = RV.getScalarVal(); |
| 2638 | llvm::Value *Cond = |
| 2639 | CGF.Builder.CreateICmpNE(V, llvm::Constant::getNullValue(V->getType())); |
| 2640 | llvm::Constant *StaticData[] = { |
| 2641 | CGF.EmitCheckSourceLocation(ArgLoc), |
| 2642 | CGF.EmitCheckSourceLocation(NNAttr->getLocation()), |
| 2643 | llvm::ConstantInt::get(CGF.Int32Ty, ArgNo + 1), |
| 2644 | }; |
Alexey Samsonov | e396bfc | 2014-11-11 22:03:54 +0000 | [diff] [blame] | 2645 | CGF.EmitCheck(std::make_pair(Cond, SanitizerKind::NonnullAttribute), |
| 2646 | "nonnull_arg", StaticData, None); |
Alexey Samsonov | 8e1162c | 2014-09-08 17:22:45 +0000 | [diff] [blame] | 2647 | } |
| 2648 | |
Reid Kleckner | 739756c | 2013-12-04 19:23:12 +0000 | [diff] [blame] | 2649 | void CodeGenFunction::EmitCallArgs(CallArgList &Args, |
| 2650 | ArrayRef<QualType> ArgTypes, |
| 2651 | CallExpr::const_arg_iterator ArgBeg, |
| 2652 | CallExpr::const_arg_iterator ArgEnd, |
Alexey Samsonov | 8e1162c | 2014-09-08 17:22:45 +0000 | [diff] [blame] | 2653 | const FunctionDecl *CalleeDecl, |
| 2654 | unsigned ParamsToSkip, |
Reid Kleckner | 739756c | 2013-12-04 19:23:12 +0000 | [diff] [blame] | 2655 | bool ForceColumnInfo) { |
| 2656 | CGDebugInfo *DI = getDebugInfo(); |
| 2657 | SourceLocation CallLoc; |
| 2658 | if (DI) CallLoc = DI->getLocation(); |
| 2659 | |
| 2660 | // We *have* to evaluate arguments from right to left in the MS C++ ABI, |
| 2661 | // because arguments are destroyed left to right in the callee. |
| 2662 | if (CGM.getTarget().getCXXABI().areArgsDestroyedLeftToRightInCallee()) { |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 2663 | // Insert a stack save if we're going to need any inalloca args. |
| 2664 | bool HasInAllocaArgs = false; |
| 2665 | for (ArrayRef<QualType>::iterator I = ArgTypes.begin(), E = ArgTypes.end(); |
| 2666 | I != E && !HasInAllocaArgs; ++I) |
| 2667 | HasInAllocaArgs = isInAllocaArgument(CGM.getCXXABI(), *I); |
| 2668 | if (HasInAllocaArgs) { |
| 2669 | assert(getTarget().getTriple().getArch() == llvm::Triple::x86); |
| 2670 | Args.allocateArgumentMemory(*this); |
| 2671 | } |
| 2672 | |
| 2673 | // Evaluate each argument. |
Reid Kleckner | 739756c | 2013-12-04 19:23:12 +0000 | [diff] [blame] | 2674 | size_t CallArgsStart = Args.size(); |
| 2675 | for (int I = ArgTypes.size() - 1; I >= 0; --I) { |
| 2676 | CallExpr::const_arg_iterator Arg = ArgBeg + I; |
| 2677 | EmitCallArg(Args, *Arg, ArgTypes[I]); |
Alexey Samsonov | 8e1162c | 2014-09-08 17:22:45 +0000 | [diff] [blame] | 2678 | emitNonNullArgCheck(*this, Args.back().RV, ArgTypes[I], Arg->getExprLoc(), |
| 2679 | CalleeDecl, ParamsToSkip + I); |
Reid Kleckner | 739756c | 2013-12-04 19:23:12 +0000 | [diff] [blame] | 2680 | // Restore the debug location. |
| 2681 | if (DI) DI->EmitLocation(Builder, CallLoc, ForceColumnInfo); |
| 2682 | } |
| 2683 | |
| 2684 | // Un-reverse the arguments we just evaluated so they match up with the LLVM |
| 2685 | // IR function. |
| 2686 | std::reverse(Args.begin() + CallArgsStart, Args.end()); |
| 2687 | return; |
| 2688 | } |
| 2689 | |
| 2690 | for (unsigned I = 0, E = ArgTypes.size(); I != E; ++I) { |
| 2691 | CallExpr::const_arg_iterator Arg = ArgBeg + I; |
| 2692 | assert(Arg != ArgEnd); |
| 2693 | EmitCallArg(Args, *Arg, ArgTypes[I]); |
Alexey Samsonov | 8e1162c | 2014-09-08 17:22:45 +0000 | [diff] [blame] | 2694 | emitNonNullArgCheck(*this, Args.back().RV, ArgTypes[I], Arg->getExprLoc(), |
| 2695 | CalleeDecl, ParamsToSkip + I); |
Reid Kleckner | 739756c | 2013-12-04 19:23:12 +0000 | [diff] [blame] | 2696 | // Restore the debug location. |
| 2697 | if (DI) DI->EmitLocation(Builder, CallLoc, ForceColumnInfo); |
| 2698 | } |
| 2699 | } |
| 2700 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 2701 | namespace { |
| 2702 | |
| 2703 | struct DestroyUnpassedArg : EHScopeStack::Cleanup { |
| 2704 | DestroyUnpassedArg(llvm::Value *Addr, QualType Ty) |
| 2705 | : Addr(Addr), Ty(Ty) {} |
| 2706 | |
| 2707 | llvm::Value *Addr; |
| 2708 | QualType Ty; |
| 2709 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2710 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 2711 | const CXXDestructorDecl *Dtor = Ty->getAsCXXRecordDecl()->getDestructor(); |
| 2712 | assert(!Dtor->isTrivial()); |
| 2713 | CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete, /*for vbase*/ false, |
| 2714 | /*Delegating=*/false, Addr); |
| 2715 | } |
| 2716 | }; |
| 2717 | |
| 2718 | } |
| 2719 | |
John McCall | 32ea969 | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 2720 | void CodeGenFunction::EmitCallArg(CallArgList &args, const Expr *E, |
| 2721 | QualType type) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2722 | if (const ObjCIndirectCopyRestoreExpr *CRE |
| 2723 | = dyn_cast<ObjCIndirectCopyRestoreExpr>(E)) { |
Richard Smith | 9c6890a | 2012-11-01 22:30:59 +0000 | [diff] [blame] | 2724 | assert(getLangOpts().ObjCAutoRefCount); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2725 | assert(getContext().hasSameType(E->getType(), type)); |
| 2726 | return emitWritebackArg(*this, args, CRE); |
| 2727 | } |
| 2728 | |
John McCall | 0a76c0c | 2011-08-26 18:42:59 +0000 | [diff] [blame] | 2729 | assert(type->isReferenceType() == E->isGLValue() && |
| 2730 | "reference binding to unmaterialized r-value!"); |
| 2731 | |
John McCall | 17054bd6 | 2011-08-26 21:08:13 +0000 | [diff] [blame] | 2732 | if (E->isGLValue()) { |
| 2733 | assert(E->getObjectKind() == OK_Ordinary); |
Richard Smith | a1c9d4d | 2013-06-12 23:38:09 +0000 | [diff] [blame] | 2734 | return args.add(EmitReferenceBindingToExpr(E), type); |
John McCall | 17054bd6 | 2011-08-26 21:08:13 +0000 | [diff] [blame] | 2735 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2736 | |
Reid Kleckner | 23f4c4b | 2013-06-21 12:45:15 +0000 | [diff] [blame] | 2737 | bool HasAggregateEvalKind = hasAggregateEvaluationKind(type); |
| 2738 | |
| 2739 | // In the Microsoft C++ ABI, aggregate arguments are destructed by the callee. |
| 2740 | // However, we still have to push an EH-only cleanup in case we unwind before |
| 2741 | // we make it to the call. |
Reid Kleckner | ac64060 | 2014-05-01 03:07:18 +0000 | [diff] [blame] | 2742 | if (HasAggregateEvalKind && |
| 2743 | CGM.getTarget().getCXXABI().areArgsDestroyedLeftToRightInCallee()) { |
| 2744 | // If we're using inalloca, use the argument memory. Otherwise, use a |
Reid Kleckner | e39ee21 | 2014-05-03 00:33:28 +0000 | [diff] [blame] | 2745 | // temporary. |
Reid Kleckner | ac64060 | 2014-05-01 03:07:18 +0000 | [diff] [blame] | 2746 | AggValueSlot Slot; |
| 2747 | if (args.isUsingInAlloca()) |
| 2748 | Slot = createPlaceholderSlot(*this, type); |
| 2749 | else |
| 2750 | Slot = CreateAggTemp(type, "agg.tmp"); |
Reid Kleckner | e39ee21 | 2014-05-03 00:33:28 +0000 | [diff] [blame] | 2751 | |
| 2752 | const CXXRecordDecl *RD = type->getAsCXXRecordDecl(); |
| 2753 | bool DestroyedInCallee = |
| 2754 | RD && RD->hasNonTrivialDestructor() && |
| 2755 | CGM.getCXXABI().getRecordArgABI(RD) != CGCXXABI::RAA_Default; |
| 2756 | if (DestroyedInCallee) |
| 2757 | Slot.setExternallyDestructed(); |
| 2758 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 2759 | EmitAggExpr(E, Slot); |
| 2760 | RValue RV = Slot.asRValue(); |
| 2761 | args.add(RV, type); |
Reid Kleckner | 23f4c4b | 2013-06-21 12:45:15 +0000 | [diff] [blame] | 2762 | |
Reid Kleckner | e39ee21 | 2014-05-03 00:33:28 +0000 | [diff] [blame] | 2763 | if (DestroyedInCallee) { |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 2764 | // Create a no-op GEP between the placeholder and the cleanup so we can |
| 2765 | // RAUW it successfully. It also serves as a marker of the first |
| 2766 | // instruction where the cleanup is active. |
| 2767 | pushFullExprCleanup<DestroyUnpassedArg>(EHCleanup, Slot.getAddr(), type); |
Reid Kleckner | 23f4c4b | 2013-06-21 12:45:15 +0000 | [diff] [blame] | 2768 | // This unreachable is a temporary marker which will be removed later. |
| 2769 | llvm::Instruction *IsActive = Builder.CreateUnreachable(); |
| 2770 | args.addArgCleanupDeactivation(EHStack.getInnermostEHScope(), IsActive); |
Reid Kleckner | 23f4c4b | 2013-06-21 12:45:15 +0000 | [diff] [blame] | 2771 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 2772 | return; |
Reid Kleckner | 23f4c4b | 2013-06-21 12:45:15 +0000 | [diff] [blame] | 2773 | } |
| 2774 | |
| 2775 | if (HasAggregateEvalKind && isa<ImplicitCastExpr>(E) && |
Eli Friedman | df96819 | 2011-05-26 00:10:27 +0000 | [diff] [blame] | 2776 | cast<CastExpr>(E)->getCastKind() == CK_LValueToRValue) { |
| 2777 | LValue L = EmitLValue(cast<CastExpr>(E)->getSubExpr()); |
| 2778 | assert(L.isSimple()); |
Eli Friedman | 61f615a | 2013-06-11 01:08:22 +0000 | [diff] [blame] | 2779 | if (L.getAlignment() >= getContext().getTypeAlignInChars(type)) { |
| 2780 | args.add(L.asAggregateRValue(), type, /*NeedsCopy*/true); |
| 2781 | } else { |
| 2782 | // We can't represent a misaligned lvalue in the CallArgList, so copy |
| 2783 | // to an aligned temporary now. |
| 2784 | llvm::Value *tmp = CreateMemTemp(type); |
| 2785 | EmitAggregateCopy(tmp, L.getAddress(), type, L.isVolatile(), |
| 2786 | L.getAlignment()); |
| 2787 | args.add(RValue::getAggregate(tmp), type); |
| 2788 | } |
Eli Friedman | df96819 | 2011-05-26 00:10:27 +0000 | [diff] [blame] | 2789 | return; |
| 2790 | } |
| 2791 | |
John McCall | 32ea969 | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 2792 | args.add(EmitAnyExprToTemp(E), type); |
Anders Carlsson | 60ce3fe | 2009-04-08 20:47:54 +0000 | [diff] [blame] | 2793 | } |
| 2794 | |
Reid Kleckner | 79b0fd7 | 2014-10-10 00:05:45 +0000 | [diff] [blame] | 2795 | QualType CodeGenFunction::getVarArgType(const Expr *Arg) { |
| 2796 | // System headers on Windows define NULL to 0 instead of 0LL on Win64. MSVC |
| 2797 | // implicitly widens null pointer constants that are arguments to varargs |
| 2798 | // functions to pointer-sized ints. |
| 2799 | if (!getTarget().getTriple().isOSWindows()) |
| 2800 | return Arg->getType(); |
| 2801 | |
| 2802 | if (Arg->getType()->isIntegerType() && |
| 2803 | getContext().getTypeSize(Arg->getType()) < |
| 2804 | getContext().getTargetInfo().getPointerWidth(0) && |
| 2805 | Arg->isNullPointerConstant(getContext(), |
| 2806 | Expr::NPC_ValueDependentIsNotNull)) { |
| 2807 | return getContext().getIntPtrType(); |
| 2808 | } |
| 2809 | |
| 2810 | return Arg->getType(); |
| 2811 | } |
| 2812 | |
Dan Gohman | 515a60d | 2012-02-16 00:57:37 +0000 | [diff] [blame] | 2813 | // In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC |
| 2814 | // optimizer it can aggressively ignore unwind edges. |
| 2815 | void |
| 2816 | CodeGenFunction::AddObjCARCExceptionMetadata(llvm::Instruction *Inst) { |
| 2817 | if (CGM.getCodeGenOpts().OptimizationLevel != 0 && |
| 2818 | !CGM.getCodeGenOpts().ObjCAutoRefCountExceptions) |
| 2819 | Inst->setMetadata("clang.arc.no_objc_arc_exceptions", |
| 2820 | CGM.getNoObjCARCExceptionsMetadata()); |
| 2821 | } |
| 2822 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 2823 | /// Emits a call to the given no-arguments nounwind runtime function. |
| 2824 | llvm::CallInst * |
| 2825 | CodeGenFunction::EmitNounwindRuntimeCall(llvm::Value *callee, |
| 2826 | const llvm::Twine &name) { |
Craig Topper | 5fc8fc2 | 2014-08-27 06:28:36 +0000 | [diff] [blame] | 2827 | return EmitNounwindRuntimeCall(callee, None, name); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 2828 | } |
| 2829 | |
| 2830 | /// Emits a call to the given nounwind runtime function. |
| 2831 | llvm::CallInst * |
| 2832 | CodeGenFunction::EmitNounwindRuntimeCall(llvm::Value *callee, |
| 2833 | ArrayRef<llvm::Value*> args, |
| 2834 | const llvm::Twine &name) { |
| 2835 | llvm::CallInst *call = EmitRuntimeCall(callee, args, name); |
| 2836 | call->setDoesNotThrow(); |
| 2837 | return call; |
| 2838 | } |
| 2839 | |
| 2840 | /// Emits a simple call (never an invoke) to the given no-arguments |
| 2841 | /// runtime function. |
| 2842 | llvm::CallInst * |
| 2843 | CodeGenFunction::EmitRuntimeCall(llvm::Value *callee, |
| 2844 | const llvm::Twine &name) { |
Craig Topper | 5fc8fc2 | 2014-08-27 06:28:36 +0000 | [diff] [blame] | 2845 | return EmitRuntimeCall(callee, None, name); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 2846 | } |
| 2847 | |
| 2848 | /// Emits a simple call (never an invoke) to the given runtime |
| 2849 | /// function. |
| 2850 | llvm::CallInst * |
| 2851 | CodeGenFunction::EmitRuntimeCall(llvm::Value *callee, |
| 2852 | ArrayRef<llvm::Value*> args, |
| 2853 | const llvm::Twine &name) { |
| 2854 | llvm::CallInst *call = Builder.CreateCall(callee, args, name); |
| 2855 | call->setCallingConv(getRuntimeCC()); |
| 2856 | return call; |
| 2857 | } |
| 2858 | |
| 2859 | /// Emits a call or invoke to the given noreturn runtime function. |
| 2860 | void CodeGenFunction::EmitNoreturnRuntimeCallOrInvoke(llvm::Value *callee, |
| 2861 | ArrayRef<llvm::Value*> args) { |
| 2862 | if (getInvokeDest()) { |
| 2863 | llvm::InvokeInst *invoke = |
| 2864 | Builder.CreateInvoke(callee, |
| 2865 | getUnreachableBlock(), |
| 2866 | getInvokeDest(), |
| 2867 | args); |
| 2868 | invoke->setDoesNotReturn(); |
| 2869 | invoke->setCallingConv(getRuntimeCC()); |
| 2870 | } else { |
| 2871 | llvm::CallInst *call = Builder.CreateCall(callee, args); |
| 2872 | call->setDoesNotReturn(); |
| 2873 | call->setCallingConv(getRuntimeCC()); |
| 2874 | Builder.CreateUnreachable(); |
| 2875 | } |
Justin Bogner | 06bd6d0 | 2014-01-13 21:24:18 +0000 | [diff] [blame] | 2876 | PGO.setCurrentRegionUnreachable(); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 2877 | } |
| 2878 | |
| 2879 | /// Emits a call or invoke instruction to the given nullary runtime |
| 2880 | /// function. |
| 2881 | llvm::CallSite |
| 2882 | CodeGenFunction::EmitRuntimeCallOrInvoke(llvm::Value *callee, |
| 2883 | const Twine &name) { |
Craig Topper | 5fc8fc2 | 2014-08-27 06:28:36 +0000 | [diff] [blame] | 2884 | return EmitRuntimeCallOrInvoke(callee, None, name); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 2885 | } |
| 2886 | |
| 2887 | /// Emits a call or invoke instruction to the given runtime function. |
| 2888 | llvm::CallSite |
| 2889 | CodeGenFunction::EmitRuntimeCallOrInvoke(llvm::Value *callee, |
| 2890 | ArrayRef<llvm::Value*> args, |
| 2891 | const Twine &name) { |
| 2892 | llvm::CallSite callSite = EmitCallOrInvoke(callee, args, name); |
| 2893 | callSite.setCallingConv(getRuntimeCC()); |
| 2894 | return callSite; |
| 2895 | } |
| 2896 | |
| 2897 | llvm::CallSite |
| 2898 | CodeGenFunction::EmitCallOrInvoke(llvm::Value *Callee, |
| 2899 | const Twine &Name) { |
Craig Topper | 5fc8fc2 | 2014-08-27 06:28:36 +0000 | [diff] [blame] | 2900 | return EmitCallOrInvoke(Callee, None, Name); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 2901 | } |
| 2902 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2903 | /// Emits a call or invoke instruction to the given function, depending |
| 2904 | /// on the current state of the EH stack. |
| 2905 | llvm::CallSite |
| 2906 | CodeGenFunction::EmitCallOrInvoke(llvm::Value *Callee, |
Chris Lattner | 54b1677 | 2011-07-23 17:14:25 +0000 | [diff] [blame] | 2907 | ArrayRef<llvm::Value *> Args, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2908 | const Twine &Name) { |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2909 | llvm::BasicBlock *InvokeDest = getInvokeDest(); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2910 | |
Dan Gohman | 515a60d | 2012-02-16 00:57:37 +0000 | [diff] [blame] | 2911 | llvm::Instruction *Inst; |
| 2912 | if (!InvokeDest) |
| 2913 | Inst = Builder.CreateCall(Callee, Args, Name); |
| 2914 | else { |
| 2915 | llvm::BasicBlock *ContBB = createBasicBlock("invoke.cont"); |
| 2916 | Inst = Builder.CreateInvoke(Callee, ContBB, InvokeDest, Args, Name); |
| 2917 | EmitBlock(ContBB); |
| 2918 | } |
| 2919 | |
| 2920 | // In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC |
| 2921 | // optimizer it can aggressively ignore unwind edges. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2922 | if (CGM.getLangOpts().ObjCAutoRefCount) |
Dan Gohman | 515a60d | 2012-02-16 00:57:37 +0000 | [diff] [blame] | 2923 | AddObjCARCExceptionMetadata(Inst); |
| 2924 | |
| 2925 | return Inst; |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2926 | } |
| 2927 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 2928 | /// \brief Store a non-aggregate value to an address to initialize it. For |
| 2929 | /// initialization, a non-atomic store will be used. |
| 2930 | static void EmitInitStoreOfNonAggregate(CodeGenFunction &CGF, RValue Src, |
| 2931 | LValue Dst) { |
| 2932 | if (Src.isScalar()) |
| 2933 | CGF.EmitStoreOfScalar(Src.getScalarVal(), Dst, /*init=*/true); |
| 2934 | else |
| 2935 | CGF.EmitStoreOfComplex(Src.getComplexVal(), Dst, /*init=*/true); |
| 2936 | } |
| 2937 | |
| 2938 | void CodeGenFunction::deferPlaceholderReplacement(llvm::Instruction *Old, |
| 2939 | llvm::Value *New) { |
| 2940 | DeferredReplacements.push_back(std::make_pair(Old, New)); |
| 2941 | } |
Chris Lattner | d59d867 | 2011-07-12 06:29:11 +0000 | [diff] [blame] | 2942 | |
Daniel Dunbar | d931a87 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 2943 | RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2944 | llvm::Value *Callee, |
Anders Carlsson | 61a401c | 2009-12-24 19:25:24 +0000 | [diff] [blame] | 2945 | ReturnValueSlot ReturnValue, |
Daniel Dunbar | cdbb5e3 | 2009-02-20 18:06:48 +0000 | [diff] [blame] | 2946 | const CallArgList &CallArgs, |
David Chisnall | 9eecafa | 2010-05-01 11:15:56 +0000 | [diff] [blame] | 2947 | const Decl *TargetDecl, |
David Chisnall | ff5f88c | 2010-05-02 13:41:58 +0000 | [diff] [blame] | 2948 | llvm::Instruction **callOrInvoke) { |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 2949 | // FIXME: We no longer need the types from CallArgs; lift up and simplify. |
Daniel Dunbar | 613855c | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 2950 | |
| 2951 | // Handle struct-return functions by passing a pointer to the |
| 2952 | // location that we would like to return into. |
Daniel Dunbar | 7633cbf | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 2953 | QualType RetTy = CallInfo.getReturnType(); |
Daniel Dunbar | b52d077 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 2954 | const ABIArgInfo &RetAI = CallInfo.getReturnInfo(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2955 | |
Chris Lattner | bb1952c | 2011-07-12 04:46:18 +0000 | [diff] [blame] | 2956 | llvm::FunctionType *IRFuncTy = |
| 2957 | cast<llvm::FunctionType>( |
| 2958 | cast<llvm::PointerType>(Callee->getType())->getElementType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2959 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 2960 | // If we're using inalloca, insert the allocation after the stack save. |
| 2961 | // FIXME: Do this earlier rather than hacking it in here! |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2962 | llvm::Value *ArgMemory = nullptr; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 2963 | if (llvm::StructType *ArgStruct = CallInfo.getArgStruct()) { |
Reid Kleckner | 9df1d97 | 2014-04-10 01:40:15 +0000 | [diff] [blame] | 2964 | llvm::Instruction *IP = CallArgs.getStackBase(); |
| 2965 | llvm::AllocaInst *AI; |
| 2966 | if (IP) { |
| 2967 | IP = IP->getNextNode(); |
| 2968 | AI = new llvm::AllocaInst(ArgStruct, "argmem", IP); |
| 2969 | } else { |
Reid Kleckner | 966abe7 | 2014-05-15 23:01:46 +0000 | [diff] [blame] | 2970 | AI = CreateTempAlloca(ArgStruct, "argmem"); |
Reid Kleckner | 9df1d97 | 2014-04-10 01:40:15 +0000 | [diff] [blame] | 2971 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 2972 | AI->setUsedWithInAlloca(true); |
| 2973 | assert(AI->isUsedWithInAlloca() && !AI->isStaticAlloca()); |
| 2974 | ArgMemory = AI; |
| 2975 | } |
| 2976 | |
Alexey Samsonov | 153004f | 2014-09-29 22:08:00 +0000 | [diff] [blame] | 2977 | ClangToLLVMArgMapping IRFunctionArgs(CGM.getContext(), CallInfo); |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 2978 | SmallVector<llvm::Value *, 16> IRCallArgs(IRFunctionArgs.totalIRArgs()); |
| 2979 | |
Chris Lattner | 4ca97c3 | 2009-06-13 00:26:38 +0000 | [diff] [blame] | 2980 | // If the call returns a temporary with struct return, create a temporary |
Anders Carlsson | 1749083 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 2981 | // alloca to hold the result, unless one is given to us. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2982 | llvm::Value *SRetPtr = nullptr; |
Reid Kleckner | 37abaca | 2014-05-09 22:46:15 +0000 | [diff] [blame] | 2983 | if (RetAI.isIndirect() || RetAI.isInAlloca()) { |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 2984 | SRetPtr = ReturnValue.getValue(); |
| 2985 | if (!SRetPtr) |
| 2986 | SRetPtr = CreateMemTemp(RetTy); |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 2987 | if (IRFunctionArgs.hasSRetArg()) { |
| 2988 | IRCallArgs[IRFunctionArgs.getSRetArgNo()] = SRetPtr; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 2989 | } else { |
| 2990 | llvm::Value *Addr = |
| 2991 | Builder.CreateStructGEP(ArgMemory, RetAI.getInAllocaFieldIndex()); |
| 2992 | Builder.CreateStore(SRetPtr, Addr); |
| 2993 | } |
Anders Carlsson | 1749083 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 2994 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2995 | |
Daniel Dunbar | a45bdbb | 2009-02-04 21:17:21 +0000 | [diff] [blame] | 2996 | assert(CallInfo.arg_size() == CallArgs.size() && |
| 2997 | "Mismatch between function signature & arguments."); |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 2998 | unsigned ArgNo = 0; |
Daniel Dunbar | b52d077 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 2999 | CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3000 | for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end(); |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 3001 | I != E; ++I, ++info_it, ++ArgNo) { |
Daniel Dunbar | b52d077 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 3002 | const ABIArgInfo &ArgInfo = info_it->info; |
Eli Friedman | f4258eb | 2011-05-02 18:05:27 +0000 | [diff] [blame] | 3003 | RValue RV = I->RV; |
Daniel Dunbar | 8fc81b0 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 3004 | |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 3005 | CharUnits TypeAlign = getContext().getTypeAlignInChars(I->Ty); |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 3006 | |
| 3007 | // Insert a padding argument to ensure proper alignment. |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 3008 | if (IRFunctionArgs.hasPaddingArg(ArgNo)) |
| 3009 | IRCallArgs[IRFunctionArgs.getPaddingArgNo(ArgNo)] = |
| 3010 | llvm::UndefValue::get(ArgInfo.getPaddingType()); |
| 3011 | |
| 3012 | unsigned FirstIRArg, NumIRArgs; |
| 3013 | std::tie(FirstIRArg, NumIRArgs) = IRFunctionArgs.getIRArgs(ArgNo); |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 3014 | |
Daniel Dunbar | 8fc81b0 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 3015 | switch (ArgInfo.getKind()) { |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 3016 | case ABIArgInfo::InAlloca: { |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 3017 | assert(NumIRArgs == 0); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 3018 | assert(getTarget().getTriple().getArch() == llvm::Triple::x86); |
| 3019 | if (RV.isAggregate()) { |
| 3020 | // Replace the placeholder with the appropriate argument slot GEP. |
| 3021 | llvm::Instruction *Placeholder = |
| 3022 | cast<llvm::Instruction>(RV.getAggregateAddr()); |
| 3023 | CGBuilderTy::InsertPoint IP = Builder.saveIP(); |
| 3024 | Builder.SetInsertPoint(Placeholder); |
| 3025 | llvm::Value *Addr = Builder.CreateStructGEP( |
| 3026 | ArgMemory, ArgInfo.getInAllocaFieldIndex()); |
| 3027 | Builder.restoreIP(IP); |
| 3028 | deferPlaceholderReplacement(Placeholder, Addr); |
| 3029 | } else { |
| 3030 | // Store the RValue into the argument struct. |
| 3031 | llvm::Value *Addr = |
| 3032 | Builder.CreateStructGEP(ArgMemory, ArgInfo.getInAllocaFieldIndex()); |
David Majnemer | 32b57b0 | 2014-03-31 16:12:47 +0000 | [diff] [blame] | 3033 | unsigned AS = Addr->getType()->getPointerAddressSpace(); |
| 3034 | llvm::Type *MemType = ConvertTypeForMem(I->Ty)->getPointerTo(AS); |
| 3035 | // There are some cases where a trivial bitcast is not avoidable. The |
| 3036 | // definition of a type later in a translation unit may change it's type |
| 3037 | // from {}* to (%struct.foo*)*. |
| 3038 | if (Addr->getType() != MemType) |
| 3039 | Addr = Builder.CreateBitCast(Addr, MemType); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 3040 | LValue argLV = MakeAddrLValue(Addr, I->Ty, TypeAlign); |
| 3041 | EmitInitStoreOfNonAggregate(*this, RV, argLV); |
| 3042 | } |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 3043 | break; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 3044 | } |
| 3045 | |
Daniel Dunbar | 0381634 | 2010-08-21 02:24:36 +0000 | [diff] [blame] | 3046 | case ABIArgInfo::Indirect: { |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 3047 | assert(NumIRArgs == 1); |
Daniel Dunbar | 747865a | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 3048 | if (RV.isScalar() || RV.isComplex()) { |
| 3049 | // Make a temporary alloca to pass the argument. |
Eli Friedman | 7e68c88 | 2011-06-15 18:26:32 +0000 | [diff] [blame] | 3050 | llvm::AllocaInst *AI = CreateMemTemp(I->Ty); |
| 3051 | if (ArgInfo.getIndirectAlign() > AI->getAlignment()) |
| 3052 | AI->setAlignment(ArgInfo.getIndirectAlign()); |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 3053 | IRCallArgs[FirstIRArg] = AI; |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 3054 | |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 3055 | LValue argLV = MakeAddrLValue(AI, I->Ty, TypeAlign); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 3056 | EmitInitStoreOfNonAggregate(*this, RV, argLV); |
Daniel Dunbar | 747865a | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 3057 | } else { |
Eli Friedman | eb7fab6 | 2011-06-14 01:37:52 +0000 | [diff] [blame] | 3058 | // We want to avoid creating an unnecessary temporary+copy here; |
Guy Benyei | 3832bfd | 2013-03-10 12:59:00 +0000 | [diff] [blame] | 3059 | // however, we need one in three cases: |
Eli Friedman | eb7fab6 | 2011-06-14 01:37:52 +0000 | [diff] [blame] | 3060 | // 1. If the argument is not byval, and we are required to copy the |
| 3061 | // source. (This case doesn't occur on any common architecture.) |
| 3062 | // 2. If the argument is byval, RV is not sufficiently aligned, and |
| 3063 | // we cannot force it to be sufficiently aligned. |
Guy Benyei | 3832bfd | 2013-03-10 12:59:00 +0000 | [diff] [blame] | 3064 | // 3. If the argument is byval, but RV is located in an address space |
| 3065 | // different than that of the argument (0). |
Eli Friedman | f745619 | 2011-06-15 22:09:18 +0000 | [diff] [blame] | 3066 | llvm::Value *Addr = RV.getAggregateAddr(); |
| 3067 | unsigned Align = ArgInfo.getIndirectAlign(); |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3068 | const llvm::DataLayout *TD = &CGM.getDataLayout(); |
Guy Benyei | 3832bfd | 2013-03-10 12:59:00 +0000 | [diff] [blame] | 3069 | const unsigned RVAddrSpace = Addr->getType()->getPointerAddressSpace(); |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 3070 | const unsigned ArgAddrSpace = |
| 3071 | (FirstIRArg < IRFuncTy->getNumParams() |
| 3072 | ? IRFuncTy->getParamType(FirstIRArg)->getPointerAddressSpace() |
| 3073 | : 0); |
Eli Friedman | f745619 | 2011-06-15 22:09:18 +0000 | [diff] [blame] | 3074 | if ((!ArgInfo.getIndirectByVal() && I->NeedsCopy) || |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 3075 | (ArgInfo.getIndirectByVal() && TypeAlign.getQuantity() < Align && |
Guy Benyei | 3832bfd | 2013-03-10 12:59:00 +0000 | [diff] [blame] | 3076 | llvm::getOrEnforceKnownAlignment(Addr, Align, TD) < Align) || |
| 3077 | (ArgInfo.getIndirectByVal() && (RVAddrSpace != ArgAddrSpace))) { |
Eli Friedman | eb7fab6 | 2011-06-14 01:37:52 +0000 | [diff] [blame] | 3078 | // Create an aligned temporary, and copy to it. |
Eli Friedman | f745619 | 2011-06-15 22:09:18 +0000 | [diff] [blame] | 3079 | llvm::AllocaInst *AI = CreateMemTemp(I->Ty); |
| 3080 | if (Align > AI->getAlignment()) |
| 3081 | AI->setAlignment(Align); |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 3082 | IRCallArgs[FirstIRArg] = AI; |
Chad Rosier | 615ed1a | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 3083 | EmitAggregateCopy(AI, Addr, I->Ty, RV.isVolatileQualified()); |
Eli Friedman | eb7fab6 | 2011-06-14 01:37:52 +0000 | [diff] [blame] | 3084 | } else { |
| 3085 | // Skip the extra memcpy call. |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 3086 | IRCallArgs[FirstIRArg] = Addr; |
Eli Friedman | eb7fab6 | 2011-06-14 01:37:52 +0000 | [diff] [blame] | 3087 | } |
Daniel Dunbar | 747865a | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 3088 | } |
| 3089 | break; |
Daniel Dunbar | 0381634 | 2010-08-21 02:24:36 +0000 | [diff] [blame] | 3090 | } |
Daniel Dunbar | 747865a | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 3091 | |
Daniel Dunbar | 94a6f25 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 3092 | case ABIArgInfo::Ignore: |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 3093 | assert(NumIRArgs == 0); |
Daniel Dunbar | 94a6f25 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 3094 | break; |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3095 | |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 3096 | case ABIArgInfo::Extend: |
| 3097 | case ABIArgInfo::Direct: { |
| 3098 | if (!isa<llvm::StructType>(ArgInfo.getCoerceToType()) && |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 3099 | ArgInfo.getCoerceToType() == ConvertType(info_it->type) && |
| 3100 | ArgInfo.getDirectOffset() == 0) { |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 3101 | assert(NumIRArgs == 1); |
Chris Lattner | bb1952c | 2011-07-12 04:46:18 +0000 | [diff] [blame] | 3102 | llvm::Value *V; |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 3103 | if (RV.isScalar()) |
Chris Lattner | bb1952c | 2011-07-12 04:46:18 +0000 | [diff] [blame] | 3104 | V = RV.getScalarVal(); |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 3105 | else |
Chris Lattner | bb1952c | 2011-07-12 04:46:18 +0000 | [diff] [blame] | 3106 | V = Builder.CreateLoad(RV.getAggregateAddr()); |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 3107 | |
Reid Kleckner | 79b0fd7 | 2014-10-10 00:05:45 +0000 | [diff] [blame] | 3108 | // We might have to widen integers, but we should never truncate. |
| 3109 | if (ArgInfo.getCoerceToType() != V->getType() && |
| 3110 | V->getType()->isIntegerTy()) |
| 3111 | V = Builder.CreateZExt(V, ArgInfo.getCoerceToType()); |
| 3112 | |
Chris Lattner | 3ce8668 | 2011-07-12 04:53:39 +0000 | [diff] [blame] | 3113 | // If the argument doesn't match, perform a bitcast to coerce it. This |
| 3114 | // can happen due to trivial type mismatches. |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 3115 | if (FirstIRArg < IRFuncTy->getNumParams() && |
| 3116 | V->getType() != IRFuncTy->getParamType(FirstIRArg)) |
| 3117 | V = Builder.CreateBitCast(V, IRFuncTy->getParamType(FirstIRArg)); |
| 3118 | IRCallArgs[FirstIRArg] = V; |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 3119 | break; |
| 3120 | } |
Daniel Dunbar | 94a6f25 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 3121 | |
Daniel Dunbar | 2f219b0 | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 3122 | // FIXME: Avoid the conversion through memory if possible. |
| 3123 | llvm::Value *SrcPtr; |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 3124 | if (RV.isScalar() || RV.isComplex()) { |
Eli Friedman | f4258eb | 2011-05-02 18:05:27 +0000 | [diff] [blame] | 3125 | SrcPtr = CreateMemTemp(I->Ty, "coerce"); |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 3126 | LValue SrcLV = MakeAddrLValue(SrcPtr, I->Ty, TypeAlign); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 3127 | EmitInitStoreOfNonAggregate(*this, RV, SrcLV); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3128 | } else |
Daniel Dunbar | 2f219b0 | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 3129 | SrcPtr = RV.getAggregateAddr(); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3130 | |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 3131 | // If the value is offset in memory, apply the offset now. |
| 3132 | if (unsigned Offs = ArgInfo.getDirectOffset()) { |
| 3133 | SrcPtr = Builder.CreateBitCast(SrcPtr, Builder.getInt8PtrTy()); |
| 3134 | SrcPtr = Builder.CreateConstGEP1_32(SrcPtr, Offs); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3135 | SrcPtr = Builder.CreateBitCast(SrcPtr, |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 3136 | llvm::PointerType::getUnqual(ArgInfo.getCoerceToType())); |
| 3137 | |
| 3138 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3139 | |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 3140 | // Fast-isel and the optimizer generally like scalar values better than |
| 3141 | // FCAs, so we flatten them if this is safe to do for this argument. |
James Molloy | 6f244b6 | 2014-05-09 16:21:39 +0000 | [diff] [blame] | 3142 | llvm::StructType *STy = |
| 3143 | dyn_cast<llvm::StructType>(ArgInfo.getCoerceToType()); |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 3144 | if (STy && ArgInfo.isDirect() && ArgInfo.getCanBeFlattened()) { |
Chandler Carruth | a6399a5 | 2012-10-10 11:29:08 +0000 | [diff] [blame] | 3145 | llvm::Type *SrcTy = |
| 3146 | cast<llvm::PointerType>(SrcPtr->getType())->getElementType(); |
| 3147 | uint64_t SrcSize = CGM.getDataLayout().getTypeAllocSize(SrcTy); |
| 3148 | uint64_t DstSize = CGM.getDataLayout().getTypeAllocSize(STy); |
| 3149 | |
| 3150 | // If the source type is smaller than the destination type of the |
| 3151 | // coerce-to logic, copy the source value into a temp alloca the size |
| 3152 | // of the destination type to allow loading all of it. The bits past |
| 3153 | // the source value are left undef. |
| 3154 | if (SrcSize < DstSize) { |
| 3155 | llvm::AllocaInst *TempAlloca |
| 3156 | = CreateTempAlloca(STy, SrcPtr->getName() + ".coerce"); |
| 3157 | Builder.CreateMemCpy(TempAlloca, SrcPtr, SrcSize, 0); |
| 3158 | SrcPtr = TempAlloca; |
| 3159 | } else { |
| 3160 | SrcPtr = Builder.CreateBitCast(SrcPtr, |
| 3161 | llvm::PointerType::getUnqual(STy)); |
| 3162 | } |
| 3163 | |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 3164 | assert(NumIRArgs == STy->getNumElements()); |
Chris Lattner | ceddafb | 2010-07-05 20:41:41 +0000 | [diff] [blame] | 3165 | for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { |
| 3166 | llvm::Value *EltPtr = Builder.CreateConstGEP2_32(SrcPtr, 0, i); |
Chris Lattner | ff941a6 | 2010-07-28 18:24:28 +0000 | [diff] [blame] | 3167 | llvm::LoadInst *LI = Builder.CreateLoad(EltPtr); |
| 3168 | // We don't know what we're loading from. |
| 3169 | LI->setAlignment(1); |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 3170 | IRCallArgs[FirstIRArg + i] = LI; |
Chris Lattner | 15ec361 | 2010-06-29 00:06:42 +0000 | [diff] [blame] | 3171 | } |
Chris Lattner | 3dd716c | 2010-06-28 23:44:11 +0000 | [diff] [blame] | 3172 | } else { |
Chris Lattner | 15ec361 | 2010-06-29 00:06:42 +0000 | [diff] [blame] | 3173 | // In the simple case, just pass the coerced loaded value. |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 3174 | assert(NumIRArgs == 1); |
| 3175 | IRCallArgs[FirstIRArg] = |
| 3176 | CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(), *this); |
Chris Lattner | 3dd716c | 2010-06-28 23:44:11 +0000 | [diff] [blame] | 3177 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3178 | |
Daniel Dunbar | 2f219b0 | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 3179 | break; |
| 3180 | } |
| 3181 | |
Daniel Dunbar | 8fc81b0 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 3182 | case ABIArgInfo::Expand: |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 3183 | unsigned IRArgPos = FirstIRArg; |
| 3184 | ExpandTypeToArgs(I->Ty, RV, IRFuncTy, IRCallArgs, IRArgPos); |
| 3185 | assert(IRArgPos == FirstIRArg + NumIRArgs); |
Daniel Dunbar | 8fc81b0 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 3186 | break; |
Daniel Dunbar | 613855c | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 3187 | } |
| 3188 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3189 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 3190 | if (ArgMemory) { |
| 3191 | llvm::Value *Arg = ArgMemory; |
Reid Kleckner | afba553e | 2014-07-08 02:24:27 +0000 | [diff] [blame] | 3192 | if (CallInfo.isVariadic()) { |
| 3193 | // When passing non-POD arguments by value to variadic functions, we will |
| 3194 | // end up with a variadic prototype and an inalloca call site. In such |
| 3195 | // cases, we can't do any parameter mismatch checks. Give up and bitcast |
| 3196 | // the callee. |
| 3197 | unsigned CalleeAS = |
| 3198 | cast<llvm::PointerType>(Callee->getType())->getAddressSpace(); |
| 3199 | Callee = Builder.CreateBitCast( |
| 3200 | Callee, getTypes().GetFunctionType(CallInfo)->getPointerTo(CalleeAS)); |
| 3201 | } else { |
| 3202 | llvm::Type *LastParamTy = |
| 3203 | IRFuncTy->getParamType(IRFuncTy->getNumParams() - 1); |
| 3204 | if (Arg->getType() != LastParamTy) { |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 3205 | #ifndef NDEBUG |
Reid Kleckner | afba553e | 2014-07-08 02:24:27 +0000 | [diff] [blame] | 3206 | // Assert that these structs have equivalent element types. |
| 3207 | llvm::StructType *FullTy = CallInfo.getArgStruct(); |
| 3208 | llvm::StructType *DeclaredTy = cast<llvm::StructType>( |
| 3209 | cast<llvm::PointerType>(LastParamTy)->getElementType()); |
| 3210 | assert(DeclaredTy->getNumElements() == FullTy->getNumElements()); |
| 3211 | for (llvm::StructType::element_iterator DI = DeclaredTy->element_begin(), |
| 3212 | DE = DeclaredTy->element_end(), |
| 3213 | FI = FullTy->element_begin(); |
| 3214 | DI != DE; ++DI, ++FI) |
| 3215 | assert(*DI == *FI); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 3216 | #endif |
Reid Kleckner | afba553e | 2014-07-08 02:24:27 +0000 | [diff] [blame] | 3217 | Arg = Builder.CreateBitCast(Arg, LastParamTy); |
| 3218 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 3219 | } |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 3220 | assert(IRFunctionArgs.hasInallocaArg()); |
| 3221 | IRCallArgs[IRFunctionArgs.getInallocaArgNo()] = Arg; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 3222 | } |
| 3223 | |
Reid Kleckner | 23f4c4b | 2013-06-21 12:45:15 +0000 | [diff] [blame] | 3224 | if (!CallArgs.getCleanupsToDeactivate().empty()) |
| 3225 | deactivateArgCleanupsBeforeCall(*this, CallArgs); |
| 3226 | |
Chris Lattner | 4ca97c3 | 2009-06-13 00:26:38 +0000 | [diff] [blame] | 3227 | // If the callee is a bitcast of a function to a varargs pointer to function |
| 3228 | // type, check to see if we can remove the bitcast. This handles some cases |
| 3229 | // with unprototyped functions. |
| 3230 | if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Callee)) |
| 3231 | if (llvm::Function *CalleeF = dyn_cast<llvm::Function>(CE->getOperand(0))) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3232 | llvm::PointerType *CurPT=cast<llvm::PointerType>(Callee->getType()); |
| 3233 | llvm::FunctionType *CurFT = |
Chris Lattner | 4ca97c3 | 2009-06-13 00:26:38 +0000 | [diff] [blame] | 3234 | cast<llvm::FunctionType>(CurPT->getElementType()); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3235 | llvm::FunctionType *ActualFT = CalleeF->getFunctionType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3236 | |
Chris Lattner | 4ca97c3 | 2009-06-13 00:26:38 +0000 | [diff] [blame] | 3237 | if (CE->getOpcode() == llvm::Instruction::BitCast && |
| 3238 | ActualFT->getReturnType() == CurFT->getReturnType() && |
Chris Lattner | 4c8da96 | 2009-06-23 01:38:41 +0000 | [diff] [blame] | 3239 | ActualFT->getNumParams() == CurFT->getNumParams() && |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 3240 | ActualFT->getNumParams() == IRCallArgs.size() && |
Fariborz Jahanian | cf7f66f | 2011-03-01 17:28:13 +0000 | [diff] [blame] | 3241 | (CurFT->isVarArg() || !ActualFT->isVarArg())) { |
Chris Lattner | 4ca97c3 | 2009-06-13 00:26:38 +0000 | [diff] [blame] | 3242 | bool ArgsMatch = true; |
| 3243 | for (unsigned i = 0, e = ActualFT->getNumParams(); i != e; ++i) |
| 3244 | if (ActualFT->getParamType(i) != CurFT->getParamType(i)) { |
| 3245 | ArgsMatch = false; |
| 3246 | break; |
| 3247 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3248 | |
Chris Lattner | 4ca97c3 | 2009-06-13 00:26:38 +0000 | [diff] [blame] | 3249 | // Strip the cast if we can get away with it. This is a nice cleanup, |
| 3250 | // but also allows us to inline the function at -O0 if it is marked |
| 3251 | // always_inline. |
| 3252 | if (ArgsMatch) |
| 3253 | Callee = CalleeF; |
| 3254 | } |
| 3255 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3256 | |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 3257 | assert(IRCallArgs.size() == IRFuncTy->getNumParams() || IRFuncTy->isVarArg()); |
| 3258 | for (unsigned i = 0; i < IRCallArgs.size(); ++i) { |
| 3259 | // Inalloca argument can have different type. |
| 3260 | if (IRFunctionArgs.hasInallocaArg() && |
| 3261 | i == IRFunctionArgs.getInallocaArgNo()) |
| 3262 | continue; |
| 3263 | if (i < IRFuncTy->getNumParams()) |
| 3264 | assert(IRCallArgs[i]->getType() == IRFuncTy->getParamType(i)); |
| 3265 | } |
| 3266 | |
Daniel Dunbar | 0ef3479 | 2009-09-12 00:59:20 +0000 | [diff] [blame] | 3267 | unsigned CallingConv; |
Devang Patel | 322300d | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 3268 | CodeGen::AttributeListType AttributeList; |
Bill Wendling | f4d64cb | 2013-02-22 00:13:35 +0000 | [diff] [blame] | 3269 | CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList, |
| 3270 | CallingConv, true); |
Bill Wendling | 3087d02 | 2012-12-07 23:17:26 +0000 | [diff] [blame] | 3271 | llvm::AttributeSet Attrs = llvm::AttributeSet::get(getLLVMContext(), |
Bill Wendling | f4d64cb | 2013-02-22 00:13:35 +0000 | [diff] [blame] | 3272 | AttributeList); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3273 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3274 | llvm::BasicBlock *InvokeDest = nullptr; |
Bill Wendling | 5e85be4 | 2012-12-30 10:32:17 +0000 | [diff] [blame] | 3275 | if (!Attrs.hasAttribute(llvm::AttributeSet::FunctionIndex, |
| 3276 | llvm::Attribute::NoUnwind)) |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3277 | InvokeDest = getInvokeDest(); |
| 3278 | |
Daniel Dunbar | b960b7b | 2009-03-02 04:32:35 +0000 | [diff] [blame] | 3279 | llvm::CallSite CS; |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3280 | if (!InvokeDest) { |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 3281 | CS = Builder.CreateCall(Callee, IRCallArgs); |
Daniel Dunbar | 1234749 | 2009-02-23 17:26:39 +0000 | [diff] [blame] | 3282 | } else { |
| 3283 | llvm::BasicBlock *Cont = createBasicBlock("invoke.cont"); |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 3284 | CS = Builder.CreateInvoke(Callee, Cont, InvokeDest, IRCallArgs); |
Daniel Dunbar | 1234749 | 2009-02-23 17:26:39 +0000 | [diff] [blame] | 3285 | EmitBlock(Cont); |
Daniel Dunbar | 5006f4a | 2009-02-20 18:54:31 +0000 | [diff] [blame] | 3286 | } |
Chris Lattner | e70a007 | 2010-06-29 16:40:28 +0000 | [diff] [blame] | 3287 | if (callOrInvoke) |
David Chisnall | ff5f88c | 2010-05-02 13:41:58 +0000 | [diff] [blame] | 3288 | *callOrInvoke = CS.getInstruction(); |
Daniel Dunbar | 5006f4a | 2009-02-20 18:54:31 +0000 | [diff] [blame] | 3289 | |
Peter Collingbourne | 41af7c2 | 2014-05-20 17:12:51 +0000 | [diff] [blame] | 3290 | if (CurCodeDecl && CurCodeDecl->hasAttr<FlattenAttr>() && |
| 3291 | !CS.hasFnAttr(llvm::Attribute::NoInline)) |
| 3292 | Attrs = |
| 3293 | Attrs.addAttribute(getLLVMContext(), llvm::AttributeSet::FunctionIndex, |
| 3294 | llvm::Attribute::AlwaysInline); |
| 3295 | |
Daniel Dunbar | b960b7b | 2009-03-02 04:32:35 +0000 | [diff] [blame] | 3296 | CS.setAttributes(Attrs); |
Daniel Dunbar | 0ef3479 | 2009-09-12 00:59:20 +0000 | [diff] [blame] | 3297 | CS.setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv)); |
Daniel Dunbar | b960b7b | 2009-03-02 04:32:35 +0000 | [diff] [blame] | 3298 | |
Dan Gohman | 515a60d | 2012-02-16 00:57:37 +0000 | [diff] [blame] | 3299 | // In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC |
| 3300 | // optimizer it can aggressively ignore unwind edges. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3301 | if (CGM.getLangOpts().ObjCAutoRefCount) |
Dan Gohman | 515a60d | 2012-02-16 00:57:37 +0000 | [diff] [blame] | 3302 | AddObjCARCExceptionMetadata(CS.getInstruction()); |
| 3303 | |
Daniel Dunbar | b960b7b | 2009-03-02 04:32:35 +0000 | [diff] [blame] | 3304 | // If the call doesn't return, finish the basic block and clear the |
| 3305 | // insertion point; this allows the rest of IRgen to discard |
| 3306 | // unreachable code. |
| 3307 | if (CS.doesNotReturn()) { |
| 3308 | Builder.CreateUnreachable(); |
| 3309 | Builder.ClearInsertionPoint(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3310 | |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 3311 | // FIXME: For now, emit a dummy basic block because expr emitters in |
| 3312 | // generally are not ready to handle emitting expressions at unreachable |
| 3313 | // points. |
Daniel Dunbar | b960b7b | 2009-03-02 04:32:35 +0000 | [diff] [blame] | 3314 | EnsureInsertPoint(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3315 | |
Daniel Dunbar | b960b7b | 2009-03-02 04:32:35 +0000 | [diff] [blame] | 3316 | // Return a reasonable RValue. |
| 3317 | return GetUndefRValue(RetTy); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3318 | } |
Daniel Dunbar | b960b7b | 2009-03-02 04:32:35 +0000 | [diff] [blame] | 3319 | |
| 3320 | llvm::Instruction *CI = CS.getInstruction(); |
Benjamin Kramer | dde0fee | 2009-10-05 13:47:21 +0000 | [diff] [blame] | 3321 | if (Builder.isNamePreserving() && !CI->getType()->isVoidTy()) |
Daniel Dunbar | 613855c | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 3322 | CI->setName("call"); |
Daniel Dunbar | a72d4ae | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 3323 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3324 | // Emit any writebacks immediately. Arguably this should happen |
| 3325 | // after any return-value munging. |
| 3326 | if (CallArgs.hasWritebacks()) |
| 3327 | emitWritebacks(*this, CallArgs); |
| 3328 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 3329 | // The stack cleanup for inalloca arguments has to run out of the normal |
| 3330 | // lexical order, so deactivate it and run it manually here. |
| 3331 | CallArgs.freeArgumentMemory(*this); |
| 3332 | |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 3333 | RValue Ret = [&] { |
| 3334 | switch (RetAI.getKind()) { |
| 3335 | case ABIArgInfo::InAlloca: |
| 3336 | case ABIArgInfo::Indirect: |
| 3337 | return convertTempToRValue(SRetPtr, RetTy, SourceLocation()); |
Daniel Dunbar | d3674e6 | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 3338 | |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 3339 | case ABIArgInfo::Ignore: |
| 3340 | // If we are ignoring an argument that had a result, make sure to |
| 3341 | // construct the appropriate return value for our caller. |
| 3342 | return GetUndefRValue(RetTy); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3343 | |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 3344 | case ABIArgInfo::Extend: |
| 3345 | case ABIArgInfo::Direct: { |
| 3346 | llvm::Type *RetIRTy = ConvertType(RetTy); |
| 3347 | if (RetAI.getCoerceToType() == RetIRTy && RetAI.getDirectOffset() == 0) { |
| 3348 | switch (getEvaluationKind(RetTy)) { |
| 3349 | case TEK_Complex: { |
| 3350 | llvm::Value *Real = Builder.CreateExtractValue(CI, 0); |
| 3351 | llvm::Value *Imag = Builder.CreateExtractValue(CI, 1); |
| 3352 | return RValue::getComplex(std::make_pair(Real, Imag)); |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 3353 | } |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 3354 | case TEK_Aggregate: { |
| 3355 | llvm::Value *DestPtr = ReturnValue.getValue(); |
| 3356 | bool DestIsVolatile = ReturnValue.isVolatile(); |
| 3357 | |
| 3358 | if (!DestPtr) { |
| 3359 | DestPtr = CreateMemTemp(RetTy, "agg.tmp"); |
| 3360 | DestIsVolatile = false; |
| 3361 | } |
| 3362 | BuildAggStore(*this, CI, DestPtr, DestIsVolatile, false); |
| 3363 | return RValue::getAggregate(DestPtr); |
| 3364 | } |
| 3365 | case TEK_Scalar: { |
| 3366 | // If the argument doesn't match, perform a bitcast to coerce it. This |
| 3367 | // can happen due to trivial type mismatches. |
| 3368 | llvm::Value *V = CI; |
| 3369 | if (V->getType() != RetIRTy) |
| 3370 | V = Builder.CreateBitCast(V, RetIRTy); |
| 3371 | return RValue::get(V); |
| 3372 | } |
| 3373 | } |
| 3374 | llvm_unreachable("bad evaluation kind"); |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 3375 | } |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 3376 | |
| 3377 | llvm::Value *DestPtr = ReturnValue.getValue(); |
| 3378 | bool DestIsVolatile = ReturnValue.isVolatile(); |
| 3379 | |
| 3380 | if (!DestPtr) { |
| 3381 | DestPtr = CreateMemTemp(RetTy, "coerce"); |
| 3382 | DestIsVolatile = false; |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 3383 | } |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 3384 | |
| 3385 | // If the value is offset in memory, apply the offset now. |
| 3386 | llvm::Value *StorePtr = DestPtr; |
| 3387 | if (unsigned Offs = RetAI.getDirectOffset()) { |
| 3388 | StorePtr = Builder.CreateBitCast(StorePtr, Builder.getInt8PtrTy()); |
| 3389 | StorePtr = Builder.CreateConstGEP1_32(StorePtr, Offs); |
| 3390 | StorePtr = Builder.CreateBitCast(StorePtr, |
| 3391 | llvm::PointerType::getUnqual(RetAI.getCoerceToType())); |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 3392 | } |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 3393 | CreateCoercedStore(CI, StorePtr, DestIsVolatile, *this); |
| 3394 | |
| 3395 | return convertTempToRValue(DestPtr, RetTy, SourceLocation()); |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 3396 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3397 | |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 3398 | case ABIArgInfo::Expand: |
| 3399 | llvm_unreachable("Invalid ABI kind for return argument"); |
Anders Carlsson | 1749083 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 3400 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3401 | |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 3402 | llvm_unreachable("Unhandled ABIArgInfo::Kind"); |
| 3403 | } (); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3404 | |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 3405 | if (Ret.isScalar() && TargetDecl) { |
| 3406 | if (const auto *AA = TargetDecl->getAttr<AssumeAlignedAttr>()) { |
| 3407 | llvm::Value *OffsetValue = nullptr; |
| 3408 | if (const auto *Offset = AA->getOffset()) |
| 3409 | OffsetValue = EmitScalarExpr(Offset); |
| 3410 | |
| 3411 | llvm::Value *Alignment = EmitScalarExpr(AA->getAlignment()); |
| 3412 | llvm::ConstantInt *AlignmentCI = cast<llvm::ConstantInt>(Alignment); |
| 3413 | EmitAlignmentAssumption(Ret.getScalarVal(), AlignmentCI->getZExtValue(), |
| 3414 | OffsetValue); |
| 3415 | } |
Daniel Dunbar | 573884e | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 3416 | } |
Daniel Dunbar | d3674e6 | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 3417 | |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 3418 | return Ret; |
Daniel Dunbar | 613855c | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 3419 | } |
Daniel Dunbar | 2d0746f | 2009-02-10 20:44:09 +0000 | [diff] [blame] | 3420 | |
| 3421 | /* VarArg handling */ |
| 3422 | |
| 3423 | llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) { |
| 3424 | return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this); |
| 3425 | } |