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