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