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