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