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 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Daniel Dunbar | 3d7c90b | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // These classes wrap the information about a call or function |
| 10 | // definition used to handle ABI compliancy. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CGCall.h" |
Chris Lattner | e70a007 | 2010-06-29 16:40:28 +0000 | [diff] [blame] | 15 | #include "ABIInfo.h" |
Akira Hatanaka | 9d8ac61 | 2016-02-17 21:09:50 +0000 | [diff] [blame] | 16 | #include "CGBlocks.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 17 | #include "CGCXXABI.h" |
David Majnemer | 4e52d6f | 2015-12-12 05:39:21 +0000 | [diff] [blame] | 18 | #include "CGCleanup.h" |
Daniel Dunbar | 3d7c90b | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 19 | #include "CodeGenFunction.h" |
Daniel Dunbar | c68897d | 2008-09-10 00:41:16 +0000 | [diff] [blame] | 20 | #include "CodeGenModule.h" |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 21 | #include "TargetInfo.h" |
Daniel Dunbar | 3d7c90b | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 22 | #include "clang/AST/Decl.h" |
Anders Carlsson | b15b55c | 2009-04-03 22:48:58 +0000 | [diff] [blame] | 23 | #include "clang/AST/DeclCXX.h" |
Daniel Dunbar | 3d7c90b | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 24 | #include "clang/AST/DeclObjC.h" |
Richard Trieu | 6368818 | 2018-12-11 03:18:39 +0000 | [diff] [blame] | 25 | #include "clang/Basic/CodeGenOptions.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" |
Bill Wendling | 706469b | 2013-02-28 22:49:57 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/StringExtras.h" |
David Blaikie | 181a613 | 2018-06-04 21:23:29 +0000 | [diff] [blame] | 31 | #include "llvm/Transforms/Utils/Local.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" |
David Blaikie | f47ca25 | 2018-03-21 22:34:27 +0000 | [diff] [blame] | 34 | #include "llvm/IR/CallingConv.h" |
Chandler Carruth | ffd5551 | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 35 | #include "llvm/IR/DataLayout.h" |
| 36 | #include "llvm/IR/InlineAsm.h" |
Saleem Abdulrasool | 10a4972 | 2016-04-08 16:52:00 +0000 | [diff] [blame] | 37 | #include "llvm/IR/IntrinsicInst.h" |
David Blaikie | f47ca25 | 2018-03-21 22:34:27 +0000 | [diff] [blame] | 38 | #include "llvm/IR/Intrinsics.h" |
Daniel Dunbar | 3d7c90b | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 39 | using namespace clang; |
| 40 | using namespace CodeGen; |
| 41 | |
| 42 | /***/ |
| 43 | |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 44 | unsigned CodeGenTypes::ClangCallConvToLLVMCallConv(CallingConv CC) { |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 45 | switch (CC) { |
| 46 | default: return llvm::CallingConv::C; |
| 47 | case CC_X86StdCall: return llvm::CallingConv::X86_StdCall; |
| 48 | case CC_X86FastCall: return llvm::CallingConv::X86_FastCall; |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 49 | case CC_X86RegCall: return llvm::CallingConv::X86_RegCall; |
Douglas Gregor | a941dca | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 50 | case CC_X86ThisCall: return llvm::CallingConv::X86_ThisCall; |
Martin Storsjo | 022e782 | 2017-07-17 20:49:45 +0000 | [diff] [blame] | 51 | case CC_Win64: return llvm::CallingConv::Win64; |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 52 | case CC_X86_64SysV: return llvm::CallingConv::X86_64_SysV; |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 53 | case CC_AAPCS: return llvm::CallingConv::ARM_AAPCS; |
| 54 | case CC_AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 55 | case CC_IntelOclBicc: return llvm::CallingConv::Intel_OCL_BI; |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 56 | // TODO: Add support for __pascal to LLVM. |
| 57 | case CC_X86Pascal: return llvm::CallingConv::C; |
| 58 | // TODO: Add support for __vectorcall to LLVM. |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 59 | case CC_X86VectorCall: return llvm::CallingConv::X86_VectorCall; |
Sander de Smalen | 44a2253 | 2018-11-26 16:38:37 +0000 | [diff] [blame] | 60 | case CC_AArch64VectorCall: return llvm::CallingConv::AArch64_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 | |
Mikael Nilsson | 9d2872d | 2018-12-13 10:15:27 +0000 | [diff] [blame] | 69 | /// Derives the 'this' type for codegen purposes, i.e. ignoring method CVR |
James Y Knight | b92d290 | 2019-02-05 16:05:50 +0000 | [diff] [blame] | 70 | /// qualification. Either or both of RD and MD may be null. A null RD indicates |
| 71 | /// that there is no meaningful 'this' type, and a null MD can occur when |
| 72 | /// calling a method pointer. |
| 73 | CanQualType CodeGenTypes::DeriveThisType(const CXXRecordDecl *RD, |
| 74 | const CXXMethodDecl *MD) { |
| 75 | QualType RecTy; |
| 76 | if (RD) |
| 77 | RecTy = Context.getTagDeclType(RD)->getCanonicalTypeInternal(); |
| 78 | else |
| 79 | RecTy = Context.VoidTy; |
| 80 | |
Mikael Nilsson | 9d2872d | 2018-12-13 10:15:27 +0000 | [diff] [blame] | 81 | if (MD) |
Anastasia Stulova | c61eaa5 | 2019-01-28 11:37:49 +0000 | [diff] [blame] | 82 | RecTy = Context.getAddrSpaceQualType(RecTy, MD->getMethodQualifiers().getAddressSpace()); |
John McCall | 2da83a3 | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 83 | return Context.getPointerType(CanQualType::CreateUnsafe(RecTy)); |
Daniel Dunbar | 7a95ca3 | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 84 | } |
| 85 | |
John McCall | 8ee376f | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 86 | /// Returns the canonical formal type of the given C++ method. |
John McCall | 2da83a3 | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 87 | static CanQual<FunctionProtoType> GetFormalType(const CXXMethodDecl *MD) { |
| 88 | return MD->getType()->getCanonicalTypeUnqualified() |
| 89 | .getAs<FunctionProtoType>(); |
John McCall | 8ee376f | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | /// Returns the "extra-canonicalized" return type, which discards |
| 93 | /// qualifiers on the return type. Codegen doesn't care about them, |
| 94 | /// and it makes ABI code a little easier to be able to assume that |
| 95 | /// all parameter and return types are top-level unqualified. |
John McCall | 2da83a3 | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 96 | static CanQualType GetReturnType(QualType RetTy) { |
| 97 | return RetTy->getCanonicalTypeUnqualified().getUnqualifiedType(); |
John McCall | 8ee376f | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 98 | } |
| 99 | |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 100 | /// Arrange the argument and result information for a value of the given |
| 101 | /// unprototyped freestanding function type. |
John McCall | 8ee376f | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 102 | const CGFunctionInfo & |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 103 | CodeGenTypes::arrangeFreeFunctionType(CanQual<FunctionNoProtoType> FTNP) { |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 104 | // When translating an unprototyped function type, always use a |
| 105 | // variadic type. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 106 | return arrangeLLVMFunctionInfo(FTNP->getReturnType().getUnqualifiedType(), |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 107 | /*instanceMethod=*/false, |
| 108 | /*chainCall=*/false, None, |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 109 | FTNP->getExtInfo(), {}, RequiredArgs(0)); |
John McCall | 8ee376f | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 110 | } |
| 111 | |
George Burgess IV | b776021 | 2017-02-24 02:49:47 +0000 | [diff] [blame] | 112 | static void addExtParameterInfosForCall( |
| 113 | llvm::SmallVectorImpl<FunctionProtoType::ExtParameterInfo> ¶mInfos, |
| 114 | const FunctionProtoType *proto, |
| 115 | unsigned prefixArgs, |
| 116 | unsigned totalArgs) { |
| 117 | assert(proto->hasExtParameterInfos()); |
| 118 | assert(paramInfos.size() <= prefixArgs); |
| 119 | assert(proto->getNumParams() + prefixArgs <= totalArgs); |
| 120 | |
| 121 | paramInfos.reserve(totalArgs); |
| 122 | |
| 123 | // Add default infos for any prefix args that don't already have infos. |
| 124 | paramInfos.resize(prefixArgs); |
| 125 | |
| 126 | // Add infos for the prototype. |
| 127 | for (const auto &ParamInfo : proto->getExtParameterInfos()) { |
| 128 | paramInfos.push_back(ParamInfo); |
| 129 | // pass_object_size params have no parameter info. |
| 130 | if (ParamInfo.hasPassObjectSize()) |
| 131 | paramInfos.emplace_back(); |
| 132 | } |
| 133 | |
| 134 | assert(paramInfos.size() <= totalArgs && |
| 135 | "Did we forget to insert pass_object_size args?"); |
| 136 | // Add default infos for the variadic and/or suffix arguments. |
| 137 | paramInfos.resize(totalArgs); |
| 138 | } |
| 139 | |
Hiroshi Inoue | c5e54dd | 2017-07-03 08:49:44 +0000 | [diff] [blame] | 140 | /// Adds the formal parameters in FPT to the given prefix. If any parameter in |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 141 | /// FPT has pass_object_size attrs, then we'll add parameters for those, too. |
| 142 | static void appendParameterTypes(const CodeGenTypes &CGT, |
| 143 | SmallVectorImpl<CanQualType> &prefix, |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 144 | SmallVectorImpl<FunctionProtoType::ExtParameterInfo> ¶mInfos, |
George Burgess IV | b776021 | 2017-02-24 02:49:47 +0000 | [diff] [blame] | 145 | CanQual<FunctionProtoType> FPT) { |
| 146 | // Fast path: don't touch param info if we don't need to. |
| 147 | if (!FPT->hasExtParameterInfos()) { |
| 148 | assert(paramInfos.empty() && |
| 149 | "We have paramInfos, but the prototype doesn't?"); |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 150 | prefix.append(FPT->param_type_begin(), FPT->param_type_end()); |
| 151 | return; |
| 152 | } |
| 153 | |
George Burgess IV | b776021 | 2017-02-24 02:49:47 +0000 | [diff] [blame] | 154 | unsigned PrefixSize = prefix.size(); |
| 155 | // In the vast majority of cases, we'll have precisely FPT->getNumParams() |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 156 | // parameters; the only thing that can change this is the presence of |
| 157 | // pass_object_size. So, we preallocate for the common case. |
| 158 | prefix.reserve(prefix.size() + FPT->getNumParams()); |
| 159 | |
George Burgess IV | b776021 | 2017-02-24 02:49:47 +0000 | [diff] [blame] | 160 | auto ExtInfos = FPT->getExtParameterInfos(); |
| 161 | assert(ExtInfos.size() == FPT->getNumParams()); |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 162 | for (unsigned I = 0, E = FPT->getNumParams(); I != E; ++I) { |
| 163 | prefix.push_back(FPT->getParamType(I)); |
George Burgess IV | b776021 | 2017-02-24 02:49:47 +0000 | [diff] [blame] | 164 | if (ExtInfos[I].hasPassObjectSize()) |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 165 | prefix.push_back(CGT.getContext().getSizeType()); |
| 166 | } |
George Burgess IV | b776021 | 2017-02-24 02:49:47 +0000 | [diff] [blame] | 167 | |
| 168 | addExtParameterInfosForCall(paramInfos, FPT.getTypePtr(), PrefixSize, |
| 169 | prefix.size()); |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 170 | } |
| 171 | |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 172 | /// Arrange the LLVM function layout for a value of the given function |
Alexey Samsonov | e5ef3ca | 2014-08-13 23:55:54 +0000 | [diff] [blame] | 173 | /// type, on top of any implicit parameters already stored. |
| 174 | static const CGFunctionInfo & |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 175 | arrangeLLVMFunctionInfo(CodeGenTypes &CGT, bool instanceMethod, |
Alexey Samsonov | e5ef3ca | 2014-08-13 23:55:54 +0000 | [diff] [blame] | 176 | SmallVectorImpl<CanQualType> &prefix, |
James Y Knight | 916db65 | 2019-02-02 01:48:23 +0000 | [diff] [blame] | 177 | CanQual<FunctionProtoType> FTP) { |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 178 | SmallVector<FunctionProtoType::ExtParameterInfo, 16> paramInfos; |
James Y Knight | 916db65 | 2019-02-02 01:48:23 +0000 | [diff] [blame] | 179 | RequiredArgs Required = RequiredArgs::forPrototypePlus(FTP, prefix.size()); |
Daniel Dunbar | bf8c24a | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 180 | // FIXME: Kill copy. |
George Burgess IV | b776021 | 2017-02-24 02:49:47 +0000 | [diff] [blame] | 181 | appendParameterTypes(CGT, prefix, paramInfos, FTP); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 182 | CanQualType resultType = FTP->getReturnType().getUnqualifiedType(); |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 183 | |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 184 | return CGT.arrangeLLVMFunctionInfo(resultType, instanceMethod, |
| 185 | /*chainCall=*/false, prefix, |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 186 | FTP->getExtInfo(), paramInfos, |
George Burgess IV | 419996c | 2016-06-16 23:06:04 +0000 | [diff] [blame] | 187 | Required); |
John McCall | 8ee376f | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 188 | } |
| 189 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 190 | /// Arrange the argument and result information for a value of the |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 191 | /// given freestanding function type. |
John McCall | 8ee376f | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 192 | const CGFunctionInfo & |
James Y Knight | 916db65 | 2019-02-02 01:48:23 +0000 | [diff] [blame] | 193 | CodeGenTypes::arrangeFreeFunctionType(CanQual<FunctionProtoType> FTP) { |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 194 | SmallVector<CanQualType, 16> argTypes; |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 195 | return ::arrangeLLVMFunctionInfo(*this, /*instanceMethod=*/false, argTypes, |
James Y Knight | 916db65 | 2019-02-02 01:48:23 +0000 | [diff] [blame] | 196 | FTP); |
Daniel Dunbar | 7feafc7 | 2009-09-11 22:24:53 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Aaron Ballman | 0362a6d | 2013-12-18 16:23:37 +0000 | [diff] [blame] | 199 | static CallingConv getCallingConventionForDecl(const Decl *D, bool IsWindows) { |
Daniel Dunbar | 7feafc7 | 2009-09-11 22:24:53 +0000 | [diff] [blame] | 200 | // Set the appropriate calling convention for the Function. |
| 201 | if (D->hasAttr<StdCallAttr>()) |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 202 | return CC_X86StdCall; |
Daniel Dunbar | 7feafc7 | 2009-09-11 22:24:53 +0000 | [diff] [blame] | 203 | |
| 204 | if (D->hasAttr<FastCallAttr>()) |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 205 | return CC_X86FastCall; |
Daniel Dunbar | 7feafc7 | 2009-09-11 22:24:53 +0000 | [diff] [blame] | 206 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 207 | if (D->hasAttr<RegCallAttr>()) |
| 208 | return CC_X86RegCall; |
| 209 | |
Douglas Gregor | a941dca | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 210 | if (D->hasAttr<ThisCallAttr>()) |
| 211 | return CC_X86ThisCall; |
| 212 | |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 213 | if (D->hasAttr<VectorCallAttr>()) |
| 214 | return CC_X86VectorCall; |
| 215 | |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 216 | if (D->hasAttr<PascalAttr>()) |
| 217 | return CC_X86Pascal; |
| 218 | |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 219 | if (PcsAttr *PCS = D->getAttr<PcsAttr>()) |
| 220 | return (PCS->getPCS() == PcsAttr::AAPCS ? CC_AAPCS : CC_AAPCS_VFP); |
| 221 | |
Sander de Smalen | 44a2253 | 2018-11-26 16:38:37 +0000 | [diff] [blame] | 222 | if (D->hasAttr<AArch64VectorPcsAttr>()) |
| 223 | return CC_AArch64VectorCall; |
| 224 | |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 225 | if (D->hasAttr<IntelOclBiccAttr>()) |
| 226 | return CC_IntelOclBicc; |
| 227 | |
Aaron Ballman | 0362a6d | 2013-12-18 16:23:37 +0000 | [diff] [blame] | 228 | if (D->hasAttr<MSABIAttr>()) |
Martin Storsjo | 022e782 | 2017-07-17 20:49:45 +0000 | [diff] [blame] | 229 | return IsWindows ? CC_C : CC_Win64; |
Aaron Ballman | 0362a6d | 2013-12-18 16:23:37 +0000 | [diff] [blame] | 230 | |
| 231 | if (D->hasAttr<SysVABIAttr>()) |
| 232 | return IsWindows ? CC_X86_64SysV : CC_C; |
| 233 | |
Roman Levenstein | 35aa5ce | 2016-03-16 18:00:46 +0000 | [diff] [blame] | 234 | if (D->hasAttr<PreserveMostAttr>()) |
| 235 | return CC_PreserveMost; |
| 236 | |
| 237 | if (D->hasAttr<PreserveAllAttr>()) |
| 238 | return CC_PreserveAll; |
| 239 | |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 240 | return CC_C; |
Daniel Dunbar | 7a95ca3 | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 241 | } |
| 242 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 243 | /// Arrange the argument and result information for a call to an |
| 244 | /// unknown C++ non-static member function of the given abstract type. |
James Y Knight | b92d290 | 2019-02-05 16:05:50 +0000 | [diff] [blame] | 245 | /// (A null RD means we don't have any meaningful "this" argument type, |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 246 | /// so fall back to a generic pointer type). |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 247 | /// The member function must be an ordinary function, i.e. not a |
| 248 | /// constructor or destructor. |
| 249 | const CGFunctionInfo & |
| 250 | CodeGenTypes::arrangeCXXMethodType(const CXXRecordDecl *RD, |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 251 | const FunctionProtoType *FTP, |
| 252 | const CXXMethodDecl *MD) { |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 253 | SmallVector<CanQualType, 16> argTypes; |
John McCall | 8ee376f | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 254 | |
Anders Carlsson | 2ee3c01 | 2009-10-03 19:43:08 +0000 | [diff] [blame] | 255 | // Add the 'this' pointer. |
James Y Knight | b92d290 | 2019-02-05 16:05:50 +0000 | [diff] [blame] | 256 | argTypes.push_back(DeriveThisType(RD, MD)); |
John McCall | 8ee376f | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 257 | |
Alexey Samsonov | e5ef3ca | 2014-08-13 23:55:54 +0000 | [diff] [blame] | 258 | return ::arrangeLLVMFunctionInfo( |
| 259 | *this, true, argTypes, |
James Y Knight | 916db65 | 2019-02-02 01:48:23 +0000 | [diff] [blame] | 260 | FTP->getCanonicalTypeUnqualified().getAs<FunctionProtoType>()); |
Anders Carlsson | 2ee3c01 | 2009-10-03 19:43:08 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Yaxun Liu | 6c10a66 | 2018-06-12 00:16:33 +0000 | [diff] [blame] | 263 | /// Set calling convention for CUDA/HIP kernel. |
| 264 | static void setCUDAKernelCallingConvention(CanQualType &FTy, CodeGenModule &CGM, |
| 265 | const FunctionDecl *FD) { |
| 266 | if (FD->hasAttr<CUDAGlobalAttr>()) { |
| 267 | const FunctionType *FT = FTy->getAs<FunctionType>(); |
| 268 | CGM.getTargetCodeGenInfo().setCUDAKernelCallingConvention(FT); |
| 269 | FTy = FT->getCanonicalTypeUnqualified(); |
| 270 | } |
| 271 | } |
| 272 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 273 | /// Arrange the argument and result information for a declaration or |
| 274 | /// definition of the given C++ non-static member function. The |
| 275 | /// member function must be an ordinary function, i.e. not a |
| 276 | /// constructor or destructor. |
| 277 | const CGFunctionInfo & |
| 278 | CodeGenTypes::arrangeCXXMethodDeclaration(const CXXMethodDecl *MD) { |
Benjamin Kramer | 60509af | 2013-09-09 14:48:42 +0000 | [diff] [blame] | 279 | assert(!isa<CXXConstructorDecl>(MD) && "wrong method for constructors!"); |
John McCall | 0d635f5 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 280 | assert(!isa<CXXDestructorDecl>(MD) && "wrong method for destructors!"); |
| 281 | |
Yaxun Liu | 6c10a66 | 2018-06-12 00:16:33 +0000 | [diff] [blame] | 282 | CanQualType FT = GetFormalType(MD).getAs<Type>(); |
| 283 | setCUDAKernelCallingConvention(FT, CGM, MD); |
| 284 | auto prototype = FT.getAs<FunctionProtoType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 285 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 286 | if (MD->isInstance()) { |
| 287 | // The abstract case is perfectly fine. |
Mark Lacey | 5ea993b | 2013-10-02 20:35:23 +0000 | [diff] [blame] | 288 | const CXXRecordDecl *ThisType = TheCXXABI.getThisArgumentTypeForMethod(MD); |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 289 | return arrangeCXXMethodType(ThisType, prototype.getTypePtr(), MD); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 290 | } |
| 291 | |
James Y Knight | 916db65 | 2019-02-02 01:48:23 +0000 | [diff] [blame] | 292 | return arrangeFreeFunctionType(prototype); |
Anders Carlsson | b15b55c | 2009-04-03 22:48:58 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 295 | bool CodeGenTypes::inheritingCtorHasParams( |
| 296 | const InheritedConstructor &Inherited, CXXCtorType Type) { |
| 297 | // Parameters are unnecessary if we're constructing a base class subobject |
| 298 | // and the inherited constructor lives in a virtual base. |
| 299 | return Type == Ctor_Complete || |
| 300 | !Inherited.getShadowDecl()->constructsVirtualBase() || |
| 301 | !Target.getCXXABI().hasConstructorVariants(); |
Peter Collingbourne | d1c5b28 | 2019-03-22 23:05:10 +0000 | [diff] [blame] | 302 | } |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 303 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 304 | const CGFunctionInfo & |
Peter Collingbourne | d1c5b28 | 2019-03-22 23:05:10 +0000 | [diff] [blame] | 305 | CodeGenTypes::arrangeCXXStructorDeclaration(GlobalDecl GD) { |
| 306 | auto *MD = cast<CXXMethodDecl>(GD.getDecl()); |
Rafael Espindola | 8d2a19b | 2014-09-08 16:01:27 +0000 | [diff] [blame] | 307 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 308 | SmallVector<CanQualType, 16> argTypes; |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 309 | SmallVector<FunctionProtoType::ExtParameterInfo, 16> paramInfos; |
James Y Knight | b92d290 | 2019-02-05 16:05:50 +0000 | [diff] [blame] | 310 | argTypes.push_back(DeriveThisType(MD->getParent(), MD)); |
Stephen Lin | 9dc6eef | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 311 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 312 | bool PassParams = true; |
| 313 | |
Rafael Espindola | 8d2a19b | 2014-09-08 16:01:27 +0000 | [diff] [blame] | 314 | if (auto *CD = dyn_cast<CXXConstructorDecl>(MD)) { |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 315 | // A base class inheriting constructor doesn't get forwarded arguments |
| 316 | // needed to construct a virtual base (or base class thereof). |
| 317 | if (auto Inherited = CD->getInheritedConstructor()) |
Peter Collingbourne | d1c5b28 | 2019-03-22 23:05:10 +0000 | [diff] [blame] | 318 | PassParams = inheritingCtorHasParams(Inherited, GD.getCtorType()); |
Rafael Espindola | 8d2a19b | 2014-09-08 16:01:27 +0000 | [diff] [blame] | 319 | } |
Anders Carlsson | 82ba57c | 2009-11-25 03:15:49 +0000 | [diff] [blame] | 320 | |
Rafael Espindola | 8d2a19b | 2014-09-08 16:01:27 +0000 | [diff] [blame] | 321 | CanQual<FunctionProtoType> FTP = GetFormalType(MD); |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 322 | |
| 323 | // Add the formal parameters. |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 324 | if (PassParams) |
George Burgess IV | b776021 | 2017-02-24 02:49:47 +0000 | [diff] [blame] | 325 | appendParameterTypes(*this, argTypes, paramInfos, FTP); |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 326 | |
George Burgess IV | 75b34a9 | 2017-02-22 22:38:25 +0000 | [diff] [blame] | 327 | CGCXXABI::AddedStructorArgs AddedArgs = |
Peter Collingbourne | d1c5b28 | 2019-03-22 23:05:10 +0000 | [diff] [blame] | 328 | TheCXXABI.buildStructorSignature(GD, argTypes); |
George Burgess IV | 75b34a9 | 2017-02-22 22:38:25 +0000 | [diff] [blame] | 329 | if (!paramInfos.empty()) { |
| 330 | // Note: prefix implies after the first param. |
| 331 | if (AddedArgs.Prefix) |
| 332 | paramInfos.insert(paramInfos.begin() + 1, AddedArgs.Prefix, |
| 333 | FunctionProtoType::ExtParameterInfo{}); |
| 334 | if (AddedArgs.Suffix) |
| 335 | paramInfos.append(AddedArgs.Suffix, |
| 336 | FunctionProtoType::ExtParameterInfo{}); |
| 337 | } |
Reid Kleckner | 89077a1 | 2013-12-17 19:46:40 +0000 | [diff] [blame] | 338 | |
| 339 | RequiredArgs required = |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 340 | (PassParams && MD->isVariadic() ? RequiredArgs(argTypes.size()) |
| 341 | : RequiredArgs::All); |
Reid Kleckner | 89077a1 | 2013-12-17 19:46:40 +0000 | [diff] [blame] | 342 | |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 343 | FunctionType::ExtInfo extInfo = FTP->getExtInfo(); |
David Majnemer | 0c0b6d9 | 2014-10-31 20:09:12 +0000 | [diff] [blame] | 344 | CanQualType resultType = TheCXXABI.HasThisReturn(GD) |
| 345 | ? argTypes.front() |
| 346 | : TheCXXABI.hasMostDerivedReturn(GD) |
| 347 | ? CGM.getContext().VoidPtrTy |
| 348 | : Context.VoidTy; |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 349 | return arrangeLLVMFunctionInfo(resultType, /*instanceMethod=*/true, |
| 350 | /*chainCall=*/false, argTypes, extInfo, |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 351 | paramInfos, required); |
| 352 | } |
| 353 | |
| 354 | static SmallVector<CanQualType, 16> |
| 355 | getArgTypesForCall(ASTContext &ctx, const CallArgList &args) { |
| 356 | SmallVector<CanQualType, 16> argTypes; |
| 357 | for (auto &arg : args) |
| 358 | argTypes.push_back(ctx.getCanonicalParamType(arg.Ty)); |
| 359 | return argTypes; |
| 360 | } |
| 361 | |
| 362 | static SmallVector<CanQualType, 16> |
| 363 | getArgTypesForDeclaration(ASTContext &ctx, const FunctionArgList &args) { |
| 364 | SmallVector<CanQualType, 16> argTypes; |
| 365 | for (auto &arg : args) |
| 366 | argTypes.push_back(ctx.getCanonicalParamType(arg->getType())); |
| 367 | return argTypes; |
| 368 | } |
| 369 | |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 370 | static llvm::SmallVector<FunctionProtoType::ExtParameterInfo, 16> |
| 371 | getExtParameterInfosForCall(const FunctionProtoType *proto, |
| 372 | unsigned prefixArgs, unsigned totalArgs) { |
| 373 | llvm::SmallVector<FunctionProtoType::ExtParameterInfo, 16> result; |
| 374 | if (proto->hasExtParameterInfos()) { |
| 375 | addExtParameterInfosForCall(result, proto, prefixArgs, totalArgs); |
| 376 | } |
| 377 | return result; |
Anders Carlsson | 82ba57c | 2009-11-25 03:15:49 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 380 | /// Arrange a call to a C++ method, passing the given arguments. |
George Burgess IV | d0a9e80 | 2017-02-23 22:07:35 +0000 | [diff] [blame] | 381 | /// |
| 382 | /// ExtraPrefixArgs is the number of ABI-specific args passed after the `this` |
| 383 | /// parameter. |
| 384 | /// ExtraSuffixArgs is the number of ABI-specific args passed at the end of |
| 385 | /// args. |
| 386 | /// PassProtoArgs indicates whether `args` has args for the parameters in the |
| 387 | /// given CXXConstructorDecl. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 388 | const CGFunctionInfo & |
| 389 | CodeGenTypes::arrangeCXXConstructorCall(const CallArgList &args, |
| 390 | const CXXConstructorDecl *D, |
| 391 | CXXCtorType CtorKind, |
George Burgess IV | d0a9e80 | 2017-02-23 22:07:35 +0000 | [diff] [blame] | 392 | unsigned ExtraPrefixArgs, |
| 393 | unsigned ExtraSuffixArgs, |
| 394 | bool PassProtoArgs) { |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 395 | // FIXME: Kill copy. |
| 396 | SmallVector<CanQualType, 16> ArgTypes; |
Alexey Samsonov | 3551e31 | 2014-08-13 20:06:24 +0000 | [diff] [blame] | 397 | for (const auto &Arg : args) |
| 398 | ArgTypes.push_back(Context.getCanonicalParamType(Arg.Ty)); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 399 | |
George Burgess IV | d0a9e80 | 2017-02-23 22:07:35 +0000 | [diff] [blame] | 400 | // +1 for implicit this, which should always be args[0]. |
| 401 | unsigned TotalPrefixArgs = 1 + ExtraPrefixArgs; |
| 402 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 403 | CanQual<FunctionProtoType> FPT = GetFormalType(D); |
James Y Knight | b92d290 | 2019-02-05 16:05:50 +0000 | [diff] [blame] | 404 | RequiredArgs Required = PassProtoArgs |
| 405 | ? RequiredArgs::forPrototypePlus( |
| 406 | FPT, TotalPrefixArgs + ExtraSuffixArgs) |
| 407 | : RequiredArgs::All; |
| 408 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 409 | GlobalDecl GD(D, CtorKind); |
David Majnemer | 0c0b6d9 | 2014-10-31 20:09:12 +0000 | [diff] [blame] | 410 | CanQualType ResultType = TheCXXABI.HasThisReturn(GD) |
| 411 | ? ArgTypes.front() |
| 412 | : TheCXXABI.hasMostDerivedReturn(GD) |
| 413 | ? CGM.getContext().VoidPtrTy |
| 414 | : Context.VoidTy; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 415 | |
| 416 | FunctionType::ExtInfo Info = FPT->getExtInfo(); |
George Burgess IV | d0a9e80 | 2017-02-23 22:07:35 +0000 | [diff] [blame] | 417 | llvm::SmallVector<FunctionProtoType::ExtParameterInfo, 16> ParamInfos; |
| 418 | // If the prototype args are elided, we should only have ABI-specific args, |
| 419 | // which never have param info. |
| 420 | if (PassProtoArgs && FPT->hasExtParameterInfos()) { |
| 421 | // ABI-specific suffix arguments are treated the same as variadic arguments. |
| 422 | addExtParameterInfosForCall(ParamInfos, FPT.getTypePtr(), TotalPrefixArgs, |
| 423 | ArgTypes.size()); |
| 424 | } |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 425 | return arrangeLLVMFunctionInfo(ResultType, /*instanceMethod=*/true, |
| 426 | /*chainCall=*/false, ArgTypes, Info, |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 427 | ParamInfos, Required); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 428 | } |
| 429 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 430 | /// Arrange the argument and result information for the declaration or |
| 431 | /// definition of the given function. |
| 432 | const CGFunctionInfo & |
| 433 | CodeGenTypes::arrangeFunctionDeclaration(const FunctionDecl *FD) { |
Chris Lattner | bea5b62 | 2009-05-12 20:27:19 +0000 | [diff] [blame] | 434 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) |
Anders Carlsson | b15b55c | 2009-04-03 22:48:58 +0000 | [diff] [blame] | 435 | if (MD->isInstance()) |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 436 | return arrangeCXXMethodDeclaration(MD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 437 | |
John McCall | 2da83a3 | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 438 | CanQualType FTy = FD->getType()->getCanonicalTypeUnqualified(); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 439 | |
John McCall | 2da83a3 | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 440 | assert(isa<FunctionType>(FTy)); |
Yaxun Liu | 6c10a66 | 2018-06-12 00:16:33 +0000 | [diff] [blame] | 441 | setCUDAKernelCallingConvention(FTy, CGM, FD); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 442 | |
| 443 | // When declaring a function without a prototype, always use a |
| 444 | // non-variadic type. |
George Burgess IV | 35cfca2 | 2017-01-06 19:10:48 +0000 | [diff] [blame] | 445 | if (CanQual<FunctionNoProtoType> noProto = FTy.getAs<FunctionNoProtoType>()) { |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 446 | return arrangeLLVMFunctionInfo( |
| 447 | noProto->getReturnType(), /*instanceMethod=*/false, |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 448 | /*chainCall=*/false, None, noProto->getExtInfo(), {},RequiredArgs::All); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 449 | } |
| 450 | |
James Y Knight | 916db65 | 2019-02-02 01:48:23 +0000 | [diff] [blame] | 451 | return arrangeFreeFunctionType(FTy.castAs<FunctionProtoType>()); |
Daniel Dunbar | 3d7c90b | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 452 | } |
| 453 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 454 | /// Arrange the argument and result information for the declaration or |
| 455 | /// definition of an Objective-C method. |
| 456 | const CGFunctionInfo & |
| 457 | CodeGenTypes::arrangeObjCMethodDeclaration(const ObjCMethodDecl *MD) { |
| 458 | // It happens that this is the same as a call with no optional |
| 459 | // arguments, except also using the formal 'self' type. |
| 460 | return arrangeObjCMessageSendSignature(MD, MD->getSelfDecl()->getType()); |
| 461 | } |
| 462 | |
| 463 | /// Arrange the argument and result information for the function type |
| 464 | /// through which to perform a send to the given Objective-C method, |
| 465 | /// using the given receiver type. The receiver type is not always |
| 466 | /// the 'self' type of the method or even an Objective-C pointer type. |
| 467 | /// This is *not* the right method for actually performing such a |
| 468 | /// message send, due to the possibility of optional arguments. |
| 469 | const CGFunctionInfo & |
| 470 | CodeGenTypes::arrangeObjCMessageSendSignature(const ObjCMethodDecl *MD, |
| 471 | QualType receiverType) { |
| 472 | SmallVector<CanQualType, 16> argTys; |
Akira Hatanaka | 98a4933 | 2017-09-22 00:41:05 +0000 | [diff] [blame] | 473 | SmallVector<FunctionProtoType::ExtParameterInfo, 4> extParamInfos(2); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 474 | argTys.push_back(Context.getCanonicalParamType(receiverType)); |
| 475 | argTys.push_back(Context.getCanonicalParamType(Context.getObjCSelType())); |
Daniel Dunbar | bf8c24a | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 476 | // FIXME: Kill copy? |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 477 | for (const auto *I : MD->parameters()) { |
Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 478 | argTys.push_back(Context.getCanonicalParamType(I->getType())); |
Akira Hatanaka | 98a4933 | 2017-09-22 00:41:05 +0000 | [diff] [blame] | 479 | auto extParamInfo = FunctionProtoType::ExtParameterInfo().withIsNoEscape( |
| 480 | I->hasAttr<NoEscapeAttr>()); |
| 481 | extParamInfos.push_back(extParamInfo); |
John McCall | 8ee376f | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 482 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 483 | |
| 484 | FunctionType::ExtInfo einfo; |
Aaron Ballman | 0362a6d | 2013-12-18 16:23:37 +0000 | [diff] [blame] | 485 | bool IsWindows = getContext().getTargetInfo().getTriple().isOSWindows(); |
| 486 | einfo = einfo.withCallingConv(getCallingConventionForDecl(MD, IsWindows)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 487 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 488 | if (getContext().getLangOpts().ObjCAutoRefCount && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 489 | MD->hasAttr<NSReturnsRetainedAttr>()) |
| 490 | einfo = einfo.withProducesResult(true); |
| 491 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 492 | RequiredArgs required = |
| 493 | (MD->isVariadic() ? RequiredArgs(argTys.size()) : RequiredArgs::All); |
| 494 | |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 495 | return arrangeLLVMFunctionInfo( |
| 496 | GetReturnType(MD->getReturnType()), /*instanceMethod=*/false, |
Akira Hatanaka | 98a4933 | 2017-09-22 00:41:05 +0000 | [diff] [blame] | 497 | /*chainCall=*/false, argTys, einfo, extParamInfos, required); |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | const CGFunctionInfo & |
| 501 | CodeGenTypes::arrangeUnprototypedObjCMessageSend(QualType returnType, |
| 502 | const CallArgList &args) { |
| 503 | auto argTypes = getArgTypesForCall(Context, args); |
| 504 | FunctionType::ExtInfo einfo; |
| 505 | |
| 506 | return arrangeLLVMFunctionInfo( |
| 507 | GetReturnType(returnType), /*instanceMethod=*/false, |
| 508 | /*chainCall=*/false, argTypes, einfo, {}, RequiredArgs::All); |
Daniel Dunbar | 3d7c90b | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 509 | } |
| 510 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 511 | const CGFunctionInfo & |
| 512 | CodeGenTypes::arrangeGlobalDeclaration(GlobalDecl GD) { |
Anders Carlsson | 6710c53 | 2010-02-06 02:44:09 +0000 | [diff] [blame] | 513 | // FIXME: Do we need to handle ObjCMethodDecl? |
| 514 | const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl()); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 515 | |
Peter Collingbourne | d1c5b28 | 2019-03-22 23:05:10 +0000 | [diff] [blame] | 516 | if (isa<CXXConstructorDecl>(GD.getDecl()) || |
| 517 | isa<CXXDestructorDecl>(GD.getDecl())) |
| 518 | return arrangeCXXStructorDeclaration(GD); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 519 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 520 | return arrangeFunctionDeclaration(FD); |
Anders Carlsson | 6710c53 | 2010-02-06 02:44:09 +0000 | [diff] [blame] | 521 | } |
| 522 | |
Reid Kleckner | c347351 | 2014-08-29 21:43:29 +0000 | [diff] [blame] | 523 | /// Arrange a thunk that takes 'this' as the first parameter followed by |
| 524 | /// varargs. Return a void pointer, regardless of the actual return type. |
| 525 | /// The body of the thunk will end in a musttail call to a function of the |
| 526 | /// correct type, and the caller will bitcast the function to the correct |
| 527 | /// prototype. |
| 528 | const CGFunctionInfo & |
Reid Kleckner | 399d96e | 2018-04-02 20:20:33 +0000 | [diff] [blame] | 529 | CodeGenTypes::arrangeUnprototypedMustTailThunk(const CXXMethodDecl *MD) { |
| 530 | assert(MD->isVirtual() && "only methods have thunks"); |
Reid Kleckner | c347351 | 2014-08-29 21:43:29 +0000 | [diff] [blame] | 531 | CanQual<FunctionProtoType> FTP = GetFormalType(MD); |
James Y Knight | b92d290 | 2019-02-05 16:05:50 +0000 | [diff] [blame] | 532 | CanQualType ArgTys[] = {DeriveThisType(MD->getParent(), MD)}; |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 533 | return arrangeLLVMFunctionInfo(Context.VoidTy, /*instanceMethod=*/false, |
| 534 | /*chainCall=*/false, ArgTys, |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 535 | FTP->getExtInfo(), {}, RequiredArgs(1)); |
Reid Kleckner | c347351 | 2014-08-29 21:43:29 +0000 | [diff] [blame] | 536 | } |
| 537 | |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 538 | const CGFunctionInfo & |
David Majnemer | 37fd66e | 2015-03-13 22:36:55 +0000 | [diff] [blame] | 539 | CodeGenTypes::arrangeMSCtorClosure(const CXXConstructorDecl *CD, |
| 540 | CXXCtorType CT) { |
| 541 | assert(CT == Ctor_CopyingClosure || CT == Ctor_DefaultClosure); |
| 542 | |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 543 | CanQual<FunctionProtoType> FTP = GetFormalType(CD); |
| 544 | SmallVector<CanQualType, 2> ArgTys; |
| 545 | const CXXRecordDecl *RD = CD->getParent(); |
James Y Knight | b92d290 | 2019-02-05 16:05:50 +0000 | [diff] [blame] | 546 | ArgTys.push_back(DeriveThisType(RD, CD)); |
David Majnemer | 37fd66e | 2015-03-13 22:36:55 +0000 | [diff] [blame] | 547 | if (CT == Ctor_CopyingClosure) |
| 548 | ArgTys.push_back(*FTP->param_type_begin()); |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 549 | if (RD->getNumVBases() > 0) |
| 550 | ArgTys.push_back(Context.IntTy); |
| 551 | CallingConv CC = Context.getDefaultCallingConvention( |
| 552 | /*IsVariadic=*/false, /*IsCXXMethod=*/true); |
| 553 | return arrangeLLVMFunctionInfo(Context.VoidTy, /*instanceMethod=*/true, |
| 554 | /*chainCall=*/false, ArgTys, |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 555 | FunctionType::ExtInfo(CC), {}, |
| 556 | RequiredArgs::All); |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 557 | } |
| 558 | |
John McCall | c818bbb | 2012-12-07 07:03:17 +0000 | [diff] [blame] | 559 | /// Arrange a call as unto a free function, except possibly with an |
| 560 | /// additional number of formal parameters considered required. |
| 561 | static const CGFunctionInfo & |
| 562 | arrangeFreeFunctionLikeCall(CodeGenTypes &CGT, |
Mark Lacey | 2345575 | 2013-10-10 20:57:00 +0000 | [diff] [blame] | 563 | CodeGenModule &CGM, |
John McCall | c818bbb | 2012-12-07 07:03:17 +0000 | [diff] [blame] | 564 | const CallArgList &args, |
| 565 | const FunctionType *fnType, |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 566 | unsigned numExtraRequiredArgs, |
| 567 | bool chainCall) { |
John McCall | c818bbb | 2012-12-07 07:03:17 +0000 | [diff] [blame] | 568 | assert(args.size() >= numExtraRequiredArgs); |
| 569 | |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 570 | llvm::SmallVector<FunctionProtoType::ExtParameterInfo, 16> paramInfos; |
| 571 | |
John McCall | c818bbb | 2012-12-07 07:03:17 +0000 | [diff] [blame] | 572 | // In most cases, there are no optional arguments. |
| 573 | RequiredArgs required = RequiredArgs::All; |
| 574 | |
| 575 | // If we have a variadic prototype, the required arguments are the |
| 576 | // extra prefix plus the arguments in the prototype. |
| 577 | if (const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(fnType)) { |
| 578 | if (proto->isVariadic()) |
James Y Knight | b92d290 | 2019-02-05 16:05:50 +0000 | [diff] [blame] | 579 | required = RequiredArgs::forPrototypePlus(proto, numExtraRequiredArgs); |
John McCall | c818bbb | 2012-12-07 07:03:17 +0000 | [diff] [blame] | 580 | |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 581 | if (proto->hasExtParameterInfos()) |
| 582 | addExtParameterInfosForCall(paramInfos, proto, numExtraRequiredArgs, |
| 583 | args.size()); |
| 584 | |
John McCall | c818bbb | 2012-12-07 07:03:17 +0000 | [diff] [blame] | 585 | // If we don't have a prototype at all, but we're supposed to |
| 586 | // explicitly use the variadic convention for unprototyped calls, |
| 587 | // treat all of the arguments as required but preserve the nominal |
| 588 | // possibility of variadics. |
Mark Lacey | 2345575 | 2013-10-10 20:57:00 +0000 | [diff] [blame] | 589 | } else if (CGM.getTargetCodeGenInfo() |
| 590 | .isNoProtoCallVariadic(args, |
| 591 | cast<FunctionNoProtoType>(fnType))) { |
John McCall | c818bbb | 2012-12-07 07:03:17 +0000 | [diff] [blame] | 592 | required = RequiredArgs(args.size()); |
| 593 | } |
| 594 | |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 595 | // FIXME: Kill copy. |
| 596 | SmallVector<CanQualType, 16> argTypes; |
| 597 | for (const auto &arg : args) |
| 598 | argTypes.push_back(CGT.getContext().getCanonicalParamType(arg.Ty)); |
| 599 | return CGT.arrangeLLVMFunctionInfo(GetReturnType(fnType->getReturnType()), |
| 600 | /*instanceMethod=*/false, chainCall, |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 601 | argTypes, fnType->getExtInfo(), paramInfos, |
| 602 | required); |
John McCall | c818bbb | 2012-12-07 07:03:17 +0000 | [diff] [blame] | 603 | } |
| 604 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 605 | /// Figure out the rules for calling a function with the given formal |
| 606 | /// type using the given arguments. The arguments are necessary |
| 607 | /// because the function might be unprototyped, in which case it's |
| 608 | /// target-dependent in crazy ways. |
| 609 | const CGFunctionInfo & |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 610 | CodeGenTypes::arrangeFreeFunctionCall(const CallArgList &args, |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 611 | const FunctionType *fnType, |
| 612 | bool chainCall) { |
| 613 | return arrangeFreeFunctionLikeCall(*this, CGM, args, fnType, |
| 614 | chainCall ? 1 : 0, chainCall); |
John McCall | c818bbb | 2012-12-07 07:03:17 +0000 | [diff] [blame] | 615 | } |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 616 | |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 617 | /// A block function is essentially a free function with an |
John McCall | c818bbb | 2012-12-07 07:03:17 +0000 | [diff] [blame] | 618 | /// extra implicit argument. |
| 619 | const CGFunctionInfo & |
| 620 | CodeGenTypes::arrangeBlockFunctionCall(const CallArgList &args, |
| 621 | const FunctionType *fnType) { |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 622 | return arrangeFreeFunctionLikeCall(*this, CGM, args, fnType, 1, |
| 623 | /*chainCall=*/false); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | const CGFunctionInfo & |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 627 | CodeGenTypes::arrangeBlockFunctionDeclaration(const FunctionProtoType *proto, |
| 628 | const FunctionArgList ¶ms) { |
| 629 | auto paramInfos = getExtParameterInfosForCall(proto, 1, params.size()); |
| 630 | auto argTypes = getArgTypesForDeclaration(Context, params); |
| 631 | |
James Y Knight | 916db65 | 2019-02-02 01:48:23 +0000 | [diff] [blame] | 632 | return arrangeLLVMFunctionInfo(GetReturnType(proto->getReturnType()), |
| 633 | /*instanceMethod*/ false, /*chainCall*/ false, |
| 634 | argTypes, proto->getExtInfo(), paramInfos, |
| 635 | RequiredArgs::forPrototypePlus(proto, 1)); |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | const CGFunctionInfo & |
| 639 | CodeGenTypes::arrangeBuiltinFunctionCall(QualType resultType, |
| 640 | const CallArgList &args) { |
Daniel Dunbar | bf8c24a | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 641 | // FIXME: Kill copy. |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 642 | SmallVector<CanQualType, 16> argTypes; |
Alexey Samsonov | 3551e31 | 2014-08-13 20:06:24 +0000 | [diff] [blame] | 643 | for (const auto &Arg : args) |
| 644 | argTypes.push_back(Context.getCanonicalParamType(Arg.Ty)); |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 645 | return arrangeLLVMFunctionInfo( |
| 646 | GetReturnType(resultType), /*instanceMethod=*/false, |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 647 | /*chainCall=*/false, argTypes, FunctionType::ExtInfo(), |
| 648 | /*paramInfos=*/ {}, RequiredArgs::All); |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 649 | } |
| 650 | |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 651 | const CGFunctionInfo & |
| 652 | CodeGenTypes::arrangeBuiltinFunctionDeclaration(QualType resultType, |
| 653 | const FunctionArgList &args) { |
| 654 | auto argTypes = getArgTypesForDeclaration(Context, args); |
| 655 | |
| 656 | return arrangeLLVMFunctionInfo( |
| 657 | GetReturnType(resultType), /*instanceMethod=*/false, /*chainCall=*/false, |
| 658 | argTypes, FunctionType::ExtInfo(), {}, RequiredArgs::All); |
| 659 | } |
| 660 | |
| 661 | const CGFunctionInfo & |
| 662 | CodeGenTypes::arrangeBuiltinFunctionDeclaration(CanQualType resultType, |
| 663 | ArrayRef<CanQualType> argTypes) { |
| 664 | return arrangeLLVMFunctionInfo( |
| 665 | resultType, /*instanceMethod=*/false, /*chainCall=*/false, |
| 666 | argTypes, FunctionType::ExtInfo(), {}, RequiredArgs::All); |
| 667 | } |
| 668 | |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 669 | /// Arrange a call to a C++ method, passing the given arguments. |
George Burgess IV | d0a9e80 | 2017-02-23 22:07:35 +0000 | [diff] [blame] | 670 | /// |
| 671 | /// numPrefixArgs is the number of ABI-specific prefix arguments we have. It |
| 672 | /// does not count `this`. |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 673 | const CGFunctionInfo & |
| 674 | CodeGenTypes::arrangeCXXMethodCall(const CallArgList &args, |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 675 | const FunctionProtoType *proto, |
George Burgess IV | d0a9e80 | 2017-02-23 22:07:35 +0000 | [diff] [blame] | 676 | RequiredArgs required, |
| 677 | unsigned numPrefixArgs) { |
| 678 | assert(numPrefixArgs + 1 <= args.size() && |
| 679 | "Emitting a call with less args than the required prefix?"); |
| 680 | // Add one to account for `this`. It's a bit awkward here, but we don't count |
| 681 | // `this` in similar places elsewhere. |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 682 | auto paramInfos = |
George Burgess IV | d0a9e80 | 2017-02-23 22:07:35 +0000 | [diff] [blame] | 683 | getExtParameterInfosForCall(proto, numPrefixArgs + 1, args.size()); |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 684 | |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 685 | // FIXME: Kill copy. |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 686 | auto argTypes = getArgTypesForCall(Context, args); |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 687 | |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 688 | FunctionType::ExtInfo info = proto->getExtInfo(); |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 689 | return arrangeLLVMFunctionInfo( |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 690 | GetReturnType(proto->getReturnType()), /*instanceMethod=*/true, |
| 691 | /*chainCall=*/false, argTypes, info, paramInfos, required); |
Daniel Dunbar | bf8c24a | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 692 | } |
| 693 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 694 | const CGFunctionInfo &CodeGenTypes::arrangeNullaryFunction() { |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 695 | return arrangeLLVMFunctionInfo( |
| 696 | getContext().VoidTy, /*instanceMethod=*/false, /*chainCall=*/false, |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 697 | None, FunctionType::ExtInfo(), {}, RequiredArgs::All); |
| 698 | } |
| 699 | |
| 700 | const CGFunctionInfo & |
| 701 | CodeGenTypes::arrangeCall(const CGFunctionInfo &signature, |
| 702 | const CallArgList &args) { |
| 703 | assert(signature.arg_size() <= args.size()); |
| 704 | if (signature.arg_size() == args.size()) |
| 705 | return signature; |
| 706 | |
| 707 | SmallVector<FunctionProtoType::ExtParameterInfo, 16> paramInfos; |
| 708 | auto sigParamInfos = signature.getExtParameterInfos(); |
| 709 | if (!sigParamInfos.empty()) { |
| 710 | paramInfos.append(sigParamInfos.begin(), sigParamInfos.end()); |
| 711 | paramInfos.resize(args.size()); |
| 712 | } |
| 713 | |
| 714 | auto argTypes = getArgTypesForCall(Context, args); |
| 715 | |
| 716 | assert(signature.getRequiredArgs().allowsOptionalArgs()); |
| 717 | return arrangeLLVMFunctionInfo(signature.getReturnType(), |
| 718 | signature.isInstanceMethod(), |
| 719 | signature.isChainCall(), |
| 720 | argTypes, |
| 721 | signature.getExtInfo(), |
| 722 | paramInfos, |
| 723 | signature.getRequiredArgs()); |
John McCall | a738c25 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 724 | } |
| 725 | |
Pekka Jaaskelainen | fc2629a | 2017-06-01 07:18:49 +0000 | [diff] [blame] | 726 | namespace clang { |
| 727 | namespace CodeGen { |
| 728 | void computeSPIRKernelABIInfo(CodeGenModule &CGM, CGFunctionInfo &FI); |
| 729 | } |
| 730 | } |
| 731 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 732 | /// Arrange the argument and result information for an abstract value |
| 733 | /// of a given function type. This is the method which all of the |
| 734 | /// above functions ultimately defer to. |
| 735 | const CGFunctionInfo & |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 736 | CodeGenTypes::arrangeLLVMFunctionInfo(CanQualType resultType, |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 737 | bool instanceMethod, |
| 738 | bool chainCall, |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 739 | ArrayRef<CanQualType> argTypes, |
| 740 | FunctionType::ExtInfo info, |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 741 | ArrayRef<FunctionProtoType::ExtParameterInfo> paramInfos, |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 742 | RequiredArgs required) { |
Fangrui Song | 3117b17 | 2018-10-20 17:53:42 +0000 | [diff] [blame] | 743 | assert(llvm::all_of(argTypes, |
| 744 | [](CanQualType T) { return T.isCanonicalAsParam(); })); |
John McCall | 2da83a3 | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 745 | |
Daniel Dunbar | e0be829 | 2009-02-03 00:07:12 +0000 | [diff] [blame] | 746 | // Lookup or create unique function info. |
| 747 | llvm::FoldingSetNodeID ID; |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 748 | CGFunctionInfo::Profile(ID, instanceMethod, chainCall, info, paramInfos, |
| 749 | required, resultType, argTypes); |
Daniel Dunbar | e0be829 | 2009-02-03 00:07:12 +0000 | [diff] [blame] | 750 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 751 | void *insertPos = nullptr; |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 752 | CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, insertPos); |
Daniel Dunbar | e0be829 | 2009-02-03 00:07:12 +0000 | [diff] [blame] | 753 | if (FI) |
| 754 | return *FI; |
| 755 | |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 756 | unsigned CC = ClangCallConvToLLVMCallConv(info.getCC()); |
| 757 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 758 | // Construct the function info. We co-allocate the ArgInfos. |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 759 | FI = CGFunctionInfo::create(CC, instanceMethod, chainCall, info, |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 760 | paramInfos, resultType, argTypes, required); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 761 | FunctionInfos.InsertNode(FI, insertPos); |
Daniel Dunbar | 313321e | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 762 | |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 763 | bool inserted = FunctionsBeingProcessed.insert(FI).second; |
| 764 | (void)inserted; |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 765 | assert(inserted && "Recursively being processed?"); |
Pekka Jaaskelainen | fc2629a | 2017-06-01 07:18:49 +0000 | [diff] [blame] | 766 | |
Daniel Dunbar | 313321e | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 767 | // Compute ABI information. |
Pekka Jaaskelainen | fc2629a | 2017-06-01 07:18:49 +0000 | [diff] [blame] | 768 | if (CC == llvm::CallingConv::SPIR_KERNEL) { |
| 769 | // Force target independent argument handling for the host visible |
| 770 | // kernel functions. |
| 771 | computeSPIRKernelABIInfo(CGM, *FI); |
| 772 | } else if (info.getCC() == CC_Swift) { |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 773 | swiftcall::computeABIInfo(CGM, *FI); |
Pekka Jaaskelainen | fc2629a | 2017-06-01 07:18:49 +0000 | [diff] [blame] | 774 | } else { |
| 775 | getABIInfo().computeInfo(*FI); |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 776 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 777 | |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 778 | // Loop over all of the computed argument and return value info. If any of |
| 779 | // them are direct or extend without a specified coerce type, specify the |
| 780 | // default now. |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 781 | ABIArgInfo &retInfo = FI->getReturnInfo(); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 782 | if (retInfo.canHaveCoerceToType() && retInfo.getCoerceToType() == nullptr) |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 783 | retInfo.setCoerceToType(ConvertType(FI->getReturnType())); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 784 | |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 785 | for (auto &I : FI->arguments()) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 786 | if (I.info.canHaveCoerceToType() && I.info.getCoerceToType() == nullptr) |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 787 | I.info.setCoerceToType(ConvertType(I.type)); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 788 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 789 | bool erased = FunctionsBeingProcessed.erase(FI); (void)erased; |
| 790 | assert(erased && "Not in set?"); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 791 | |
Daniel Dunbar | e0be829 | 2009-02-03 00:07:12 +0000 | [diff] [blame] | 792 | return *FI; |
Daniel Dunbar | bf8c24a | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 793 | } |
| 794 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 795 | CGFunctionInfo *CGFunctionInfo::create(unsigned llvmCC, |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 796 | bool instanceMethod, |
| 797 | bool chainCall, |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 798 | const FunctionType::ExtInfo &info, |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 799 | ArrayRef<ExtParameterInfo> paramInfos, |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 800 | CanQualType resultType, |
| 801 | ArrayRef<CanQualType> argTypes, |
| 802 | RequiredArgs required) { |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 803 | assert(paramInfos.empty() || paramInfos.size() == argTypes.size()); |
James Y Knight | b92d290 | 2019-02-05 16:05:50 +0000 | [diff] [blame] | 804 | assert(!required.allowsOptionalArgs() || |
| 805 | required.getNumRequiredArgs() <= argTypes.size()); |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 806 | |
| 807 | void *buffer = |
| 808 | operator new(totalSizeToAlloc<ArgInfo, ExtParameterInfo>( |
| 809 | argTypes.size() + 1, paramInfos.size())); |
| 810 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 811 | CGFunctionInfo *FI = new(buffer) CGFunctionInfo(); |
| 812 | FI->CallingConvention = llvmCC; |
| 813 | FI->EffectiveCallingConvention = llvmCC; |
| 814 | FI->ASTCallingConvention = info.getCC(); |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 815 | FI->InstanceMethod = instanceMethod; |
| 816 | FI->ChainCall = chainCall; |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 817 | FI->NoReturn = info.getNoReturn(); |
| 818 | FI->ReturnsRetained = info.getProducesResult(); |
Oren Ben Simhon | 318a6ea | 2017-04-27 12:01:00 +0000 | [diff] [blame] | 819 | FI->NoCallerSavedRegs = info.getNoCallerSavedRegs(); |
Oren Ben Simhon | 220671a | 2018-03-17 13:31:35 +0000 | [diff] [blame] | 820 | FI->NoCfCheck = info.getNoCfCheck(); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 821 | FI->Required = required; |
| 822 | FI->HasRegParm = info.getHasRegParm(); |
| 823 | FI->RegParm = info.getRegParm(); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 824 | FI->ArgStruct = nullptr; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 825 | FI->ArgStructAlign = 0; |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 826 | FI->NumArgs = argTypes.size(); |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 827 | FI->HasExtParameterInfos = !paramInfos.empty(); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 828 | FI->getArgsBuffer()[0].type = resultType; |
| 829 | for (unsigned i = 0, e = argTypes.size(); i != e; ++i) |
| 830 | FI->getArgsBuffer()[i + 1].type = argTypes[i]; |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 831 | for (unsigned i = 0, e = paramInfos.size(); i != e; ++i) |
| 832 | FI->getExtParameterInfosBuffer()[i] = paramInfos[i]; |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 833 | return FI; |
Daniel Dunbar | 313321e | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | /***/ |
| 837 | |
Alexey Samsonov | 8a0bad0 | 2014-09-29 18:41:28 +0000 | [diff] [blame] | 838 | namespace { |
| 839 | // ABIArgInfo::Expand implementation. |
| 840 | |
| 841 | // Specifies the way QualType passed as ABIArgInfo::Expand is expanded. |
| 842 | struct TypeExpansion { |
| 843 | enum TypeExpansionKind { |
| 844 | // Elements of constant arrays are expanded recursively. |
| 845 | TEK_ConstantArray, |
| 846 | // Record fields are expanded recursively (but if record is a union, only |
| 847 | // the field with the largest size is expanded). |
| 848 | TEK_Record, |
| 849 | // For complex types, real and imaginary parts are expanded recursively. |
| 850 | TEK_Complex, |
| 851 | // All other types are not expandable. |
| 852 | TEK_None |
| 853 | }; |
| 854 | |
| 855 | const TypeExpansionKind Kind; |
| 856 | |
| 857 | TypeExpansion(TypeExpansionKind K) : Kind(K) {} |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 858 | virtual ~TypeExpansion() {} |
Alexey Samsonov | 8a0bad0 | 2014-09-29 18:41:28 +0000 | [diff] [blame] | 859 | }; |
| 860 | |
| 861 | struct ConstantArrayExpansion : TypeExpansion { |
| 862 | QualType EltTy; |
| 863 | uint64_t NumElts; |
| 864 | |
| 865 | ConstantArrayExpansion(QualType EltTy, uint64_t NumElts) |
| 866 | : TypeExpansion(TEK_ConstantArray), EltTy(EltTy), NumElts(NumElts) {} |
| 867 | static bool classof(const TypeExpansion *TE) { |
| 868 | return TE->Kind == TEK_ConstantArray; |
| 869 | } |
| 870 | }; |
| 871 | |
| 872 | struct RecordExpansion : TypeExpansion { |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 873 | SmallVector<const CXXBaseSpecifier *, 1> Bases; |
| 874 | |
Alexey Samsonov | 8a0bad0 | 2014-09-29 18:41:28 +0000 | [diff] [blame] | 875 | SmallVector<const FieldDecl *, 1> Fields; |
| 876 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 877 | RecordExpansion(SmallVector<const CXXBaseSpecifier *, 1> &&Bases, |
| 878 | SmallVector<const FieldDecl *, 1> &&Fields) |
Benjamin Kramer | 0bb9774 | 2016-02-13 16:00:13 +0000 | [diff] [blame] | 879 | : TypeExpansion(TEK_Record), Bases(std::move(Bases)), |
| 880 | Fields(std::move(Fields)) {} |
Alexey Samsonov | 8a0bad0 | 2014-09-29 18:41:28 +0000 | [diff] [blame] | 881 | static bool classof(const TypeExpansion *TE) { |
| 882 | return TE->Kind == TEK_Record; |
| 883 | } |
| 884 | }; |
| 885 | |
| 886 | struct ComplexExpansion : TypeExpansion { |
| 887 | QualType EltTy; |
| 888 | |
| 889 | ComplexExpansion(QualType EltTy) : TypeExpansion(TEK_Complex), EltTy(EltTy) {} |
| 890 | static bool classof(const TypeExpansion *TE) { |
| 891 | return TE->Kind == TEK_Complex; |
| 892 | } |
| 893 | }; |
| 894 | |
| 895 | struct NoExpansion : TypeExpansion { |
| 896 | NoExpansion() : TypeExpansion(TEK_None) {} |
| 897 | static bool classof(const TypeExpansion *TE) { |
| 898 | return TE->Kind == TEK_None; |
| 899 | } |
| 900 | }; |
| 901 | } // namespace |
| 902 | |
| 903 | static std::unique_ptr<TypeExpansion> |
| 904 | getTypeExpansion(QualType Ty, const ASTContext &Context) { |
| 905 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { |
Jonas Devlieghere | 2b3d49b | 2019-08-14 23:04:18 +0000 | [diff] [blame] | 906 | return std::make_unique<ConstantArrayExpansion>( |
Alexey Samsonov | 8a0bad0 | 2014-09-29 18:41:28 +0000 | [diff] [blame] | 907 | AT->getElementType(), AT->getSize().getZExtValue()); |
| 908 | } |
| 909 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 910 | SmallVector<const CXXBaseSpecifier *, 1> Bases; |
Alexey Samsonov | 8a0bad0 | 2014-09-29 18:41:28 +0000 | [diff] [blame] | 911 | SmallVector<const FieldDecl *, 1> Fields; |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 912 | const RecordDecl *RD = RT->getDecl(); |
| 913 | assert(!RD->hasFlexibleArrayMember() && |
| 914 | "Cannot expand structure with flexible array."); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 915 | if (RD->isUnion()) { |
| 916 | // Unions can be here only in degenerative cases - all the fields are same |
| 917 | // after flattening. Thus we have to use the "largest" field. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 918 | const FieldDecl *LargestFD = nullptr; |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 919 | CharUnits UnionSize = CharUnits::Zero(); |
| 920 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 921 | for (const auto *FD : RD->fields()) { |
Richard Smith | 866dee4 | 2018-04-02 18:29:43 +0000 | [diff] [blame] | 922 | if (FD->isZeroLengthBitField(Context)) |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 923 | continue; |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 924 | assert(!FD->isBitField() && |
| 925 | "Cannot expand structure with bit-field members."); |
Alexey Samsonov | 8a0bad0 | 2014-09-29 18:41:28 +0000 | [diff] [blame] | 926 | CharUnits FieldSize = Context.getTypeSizeInChars(FD->getType()); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 927 | if (UnionSize < FieldSize) { |
| 928 | UnionSize = FieldSize; |
| 929 | LargestFD = FD; |
| 930 | } |
| 931 | } |
| 932 | if (LargestFD) |
Alexey Samsonov | 8a0bad0 | 2014-09-29 18:41:28 +0000 | [diff] [blame] | 933 | Fields.push_back(LargestFD); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 934 | } else { |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 935 | if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 936 | assert(!CXXRD->isDynamicClass() && |
| 937 | "cannot expand vtable pointers in dynamic classes"); |
| 938 | for (const CXXBaseSpecifier &BS : CXXRD->bases()) |
| 939 | Bases.push_back(&BS); |
| 940 | } |
| 941 | |
Alexey Samsonov | 8a0bad0 | 2014-09-29 18:41:28 +0000 | [diff] [blame] | 942 | for (const auto *FD : RD->fields()) { |
Richard Smith | 866dee4 | 2018-04-02 18:29:43 +0000 | [diff] [blame] | 943 | if (FD->isZeroLengthBitField(Context)) |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 944 | continue; |
Alexey Samsonov | 8a0bad0 | 2014-09-29 18:41:28 +0000 | [diff] [blame] | 945 | assert(!FD->isBitField() && |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 946 | "Cannot expand structure with bit-field members."); |
Alexey Samsonov | 8a0bad0 | 2014-09-29 18:41:28 +0000 | [diff] [blame] | 947 | Fields.push_back(FD); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 948 | } |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 949 | } |
Jonas Devlieghere | 2b3d49b | 2019-08-14 23:04:18 +0000 | [diff] [blame] | 950 | return std::make_unique<RecordExpansion>(std::move(Bases), |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 951 | std::move(Fields)); |
Alexey Samsonov | 8a0bad0 | 2014-09-29 18:41:28 +0000 | [diff] [blame] | 952 | } |
| 953 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
Jonas Devlieghere | 2b3d49b | 2019-08-14 23:04:18 +0000 | [diff] [blame] | 954 | return std::make_unique<ComplexExpansion>(CT->getElementType()); |
Alexey Samsonov | 8a0bad0 | 2014-09-29 18:41:28 +0000 | [diff] [blame] | 955 | } |
Jonas Devlieghere | 2b3d49b | 2019-08-14 23:04:18 +0000 | [diff] [blame] | 956 | return std::make_unique<NoExpansion>(); |
Alexey Samsonov | 8a0bad0 | 2014-09-29 18:41:28 +0000 | [diff] [blame] | 957 | } |
| 958 | |
Alexey Samsonov | 52c0f6a | 2014-09-29 20:30:22 +0000 | [diff] [blame] | 959 | static int getExpansionSize(QualType Ty, const ASTContext &Context) { |
| 960 | auto Exp = getTypeExpansion(Ty, Context); |
| 961 | if (auto CAExp = dyn_cast<ConstantArrayExpansion>(Exp.get())) { |
| 962 | return CAExp->NumElts * getExpansionSize(CAExp->EltTy, Context); |
| 963 | } |
| 964 | if (auto RExp = dyn_cast<RecordExpansion>(Exp.get())) { |
| 965 | int Res = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 966 | for (auto BS : RExp->Bases) |
| 967 | Res += getExpansionSize(BS->getType(), Context); |
Alexey Samsonov | 52c0f6a | 2014-09-29 20:30:22 +0000 | [diff] [blame] | 968 | for (auto FD : RExp->Fields) |
| 969 | Res += getExpansionSize(FD->getType(), Context); |
| 970 | return Res; |
| 971 | } |
| 972 | if (isa<ComplexExpansion>(Exp.get())) |
| 973 | return 2; |
| 974 | assert(isa<NoExpansion>(Exp.get())); |
| 975 | return 1; |
| 976 | } |
| 977 | |
Alexey Samsonov | 153004f | 2014-09-29 22:08:00 +0000 | [diff] [blame] | 978 | void |
| 979 | CodeGenTypes::getExpandedTypes(QualType Ty, |
| 980 | SmallVectorImpl<llvm::Type *>::iterator &TI) { |
| 981 | auto Exp = getTypeExpansion(Ty, Context); |
Alexey Samsonov | 8a0bad0 | 2014-09-29 18:41:28 +0000 | [diff] [blame] | 982 | if (auto CAExp = dyn_cast<ConstantArrayExpansion>(Exp.get())) { |
| 983 | for (int i = 0, n = CAExp->NumElts; i < n; i++) { |
Alexey Samsonov | 153004f | 2014-09-29 22:08:00 +0000 | [diff] [blame] | 984 | getExpandedTypes(CAExp->EltTy, TI); |
Alexey Samsonov | 8a0bad0 | 2014-09-29 18:41:28 +0000 | [diff] [blame] | 985 | } |
| 986 | } else if (auto RExp = dyn_cast<RecordExpansion>(Exp.get())) { |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 987 | for (auto BS : RExp->Bases) |
| 988 | getExpandedTypes(BS->getType(), TI); |
| 989 | for (auto FD : RExp->Fields) |
Alexey Samsonov | 153004f | 2014-09-29 22:08:00 +0000 | [diff] [blame] | 990 | getExpandedTypes(FD->getType(), TI); |
Alexey Samsonov | 8a0bad0 | 2014-09-29 18:41:28 +0000 | [diff] [blame] | 991 | } else if (auto CExp = dyn_cast<ComplexExpansion>(Exp.get())) { |
| 992 | llvm::Type *EltTy = ConvertType(CExp->EltTy); |
Alexey Samsonov | 153004f | 2014-09-29 22:08:00 +0000 | [diff] [blame] | 993 | *TI++ = EltTy; |
| 994 | *TI++ = EltTy; |
Alexey Samsonov | 8a0bad0 | 2014-09-29 18:41:28 +0000 | [diff] [blame] | 995 | } else { |
| 996 | assert(isa<NoExpansion>(Exp.get())); |
Alexey Samsonov | 153004f | 2014-09-29 22:08:00 +0000 | [diff] [blame] | 997 | *TI++ = ConvertType(Ty); |
Alexey Samsonov | 8a0bad0 | 2014-09-29 18:41:28 +0000 | [diff] [blame] | 998 | } |
Daniel Dunbar | 8fc81b0 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 999 | } |
| 1000 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1001 | static void forConstantArrayExpansion(CodeGenFunction &CGF, |
| 1002 | ConstantArrayExpansion *CAE, |
| 1003 | Address BaseAddr, |
| 1004 | llvm::function_ref<void(Address)> Fn) { |
| 1005 | CharUnits EltSize = CGF.getContext().getTypeSizeInChars(CAE->EltTy); |
| 1006 | CharUnits EltAlign = |
| 1007 | BaseAddr.getAlignment().alignmentOfArrayElement(EltSize); |
| 1008 | |
| 1009 | for (int i = 0, n = CAE->NumElts; i < n; i++) { |
| 1010 | llvm::Value *EltAddr = |
| 1011 | CGF.Builder.CreateConstGEP2_32(nullptr, BaseAddr.getPointer(), 0, i); |
| 1012 | Fn(Address(EltAddr, EltAlign)); |
| 1013 | } |
| 1014 | } |
| 1015 | |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 1016 | void CodeGenFunction::ExpandTypeFromArgs( |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 1017 | QualType Ty, LValue LV, SmallVectorImpl<llvm::Value *>::iterator &AI) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1018 | assert(LV.isSimple() && |
| 1019 | "Unexpected non-simple lvalue during struct expansion."); |
Daniel Dunbar | 8fc81b0 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1020 | |
Alexey Samsonov | 8a0bad0 | 2014-09-29 18:41:28 +0000 | [diff] [blame] | 1021 | auto Exp = getTypeExpansion(Ty, getContext()); |
| 1022 | if (auto CAExp = dyn_cast<ConstantArrayExpansion>(Exp.get())) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1023 | forConstantArrayExpansion(*this, CAExp, LV.getAddress(), |
| 1024 | [&](Address EltAddr) { |
Alexey Samsonov | 8a0bad0 | 2014-09-29 18:41:28 +0000 | [diff] [blame] | 1025 | LValue LV = MakeAddrLValue(EltAddr, CAExp->EltTy); |
| 1026 | ExpandTypeFromArgs(CAExp->EltTy, LV, AI); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1027 | }); |
Alexey Samsonov | 8a0bad0 | 2014-09-29 18:41:28 +0000 | [diff] [blame] | 1028 | } else if (auto RExp = dyn_cast<RecordExpansion>(Exp.get())) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1029 | Address This = LV.getAddress(); |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 1030 | for (const CXXBaseSpecifier *BS : RExp->Bases) { |
| 1031 | // Perform a single step derived-to-base conversion. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1032 | Address Base = |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 1033 | GetAddressOfBaseClass(This, Ty->getAsCXXRecordDecl(), &BS, &BS + 1, |
| 1034 | /*NullCheckValue=*/false, SourceLocation()); |
| 1035 | LValue SubLV = MakeAddrLValue(Base, BS->getType()); |
| 1036 | |
| 1037 | // Recurse onto bases. |
| 1038 | ExpandTypeFromArgs(BS->getType(), SubLV, AI); |
| 1039 | } |
Alexey Samsonov | 8a0bad0 | 2014-09-29 18:41:28 +0000 | [diff] [blame] | 1040 | for (auto FD : RExp->Fields) { |
| 1041 | // FIXME: What are the right qualifiers here? |
Reid Kleckner | 9d03109 | 2016-05-02 22:42:34 +0000 | [diff] [blame] | 1042 | LValue SubLV = EmitLValueForFieldInitialization(LV, FD); |
Alexey Samsonov | 8a0bad0 | 2014-09-29 18:41:28 +0000 | [diff] [blame] | 1043 | ExpandTypeFromArgs(FD->getType(), SubLV, AI); |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 1044 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1045 | } else if (isa<ComplexExpansion>(Exp.get())) { |
| 1046 | auto realValue = *AI++; |
| 1047 | auto imagValue = *AI++; |
| 1048 | EmitStoreOfComplex(ComplexPairTy(realValue, imagValue), LV, /*init*/ true); |
Alexey Samsonov | 8a0bad0 | 2014-09-29 18:41:28 +0000 | [diff] [blame] | 1049 | } else { |
| 1050 | assert(isa<NoExpansion>(Exp.get())); |
| 1051 | EmitStoreThroughLValue(RValue::get(*AI++), LV); |
Daniel Dunbar | 8fc81b0 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1052 | } |
Alexey Samsonov | 8a0bad0 | 2014-09-29 18:41:28 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | void CodeGenFunction::ExpandTypeToArgs( |
Yaxun Liu | 5b330e8 | 2018-03-15 15:25:19 +0000 | [diff] [blame] | 1056 | QualType Ty, CallArg Arg, llvm::FunctionType *IRFuncTy, |
Alexey Samsonov | 8a0bad0 | 2014-09-29 18:41:28 +0000 | [diff] [blame] | 1057 | SmallVectorImpl<llvm::Value *> &IRCallArgs, unsigned &IRCallArgPos) { |
| 1058 | auto Exp = getTypeExpansion(Ty, getContext()); |
| 1059 | if (auto CAExp = dyn_cast<ConstantArrayExpansion>(Exp.get())) { |
Yaxun Liu | 5b330e8 | 2018-03-15 15:25:19 +0000 | [diff] [blame] | 1060 | Address Addr = Arg.hasLValue() ? Arg.getKnownLValue().getAddress() |
| 1061 | : Arg.getKnownRValue().getAggregateAddress(); |
| 1062 | forConstantArrayExpansion( |
| 1063 | *this, CAExp, Addr, [&](Address EltAddr) { |
| 1064 | CallArg EltArg = CallArg( |
| 1065 | convertTempToRValue(EltAddr, CAExp->EltTy, SourceLocation()), |
| 1066 | CAExp->EltTy); |
| 1067 | ExpandTypeToArgs(CAExp->EltTy, EltArg, IRFuncTy, IRCallArgs, |
| 1068 | IRCallArgPos); |
| 1069 | }); |
Alexey Samsonov | 8a0bad0 | 2014-09-29 18:41:28 +0000 | [diff] [blame] | 1070 | } else if (auto RExp = dyn_cast<RecordExpansion>(Exp.get())) { |
Yaxun Liu | 5b330e8 | 2018-03-15 15:25:19 +0000 | [diff] [blame] | 1071 | Address This = Arg.hasLValue() ? Arg.getKnownLValue().getAddress() |
| 1072 | : Arg.getKnownRValue().getAggregateAddress(); |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 1073 | for (const CXXBaseSpecifier *BS : RExp->Bases) { |
| 1074 | // Perform a single step derived-to-base conversion. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1075 | Address Base = |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 1076 | GetAddressOfBaseClass(This, Ty->getAsCXXRecordDecl(), &BS, &BS + 1, |
| 1077 | /*NullCheckValue=*/false, SourceLocation()); |
Yaxun Liu | 5b330e8 | 2018-03-15 15:25:19 +0000 | [diff] [blame] | 1078 | CallArg BaseArg = CallArg(RValue::getAggregate(Base), BS->getType()); |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 1079 | |
| 1080 | // Recurse onto bases. |
Yaxun Liu | 5b330e8 | 2018-03-15 15:25:19 +0000 | [diff] [blame] | 1081 | ExpandTypeToArgs(BS->getType(), BaseArg, IRFuncTy, IRCallArgs, |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 1082 | IRCallArgPos); |
| 1083 | } |
| 1084 | |
| 1085 | LValue LV = MakeAddrLValue(This, Ty); |
Alexey Samsonov | 8a0bad0 | 2014-09-29 18:41:28 +0000 | [diff] [blame] | 1086 | for (auto FD : RExp->Fields) { |
Yaxun Liu | 5b330e8 | 2018-03-15 15:25:19 +0000 | [diff] [blame] | 1087 | CallArg FldArg = |
| 1088 | CallArg(EmitRValueForField(LV, FD, SourceLocation()), FD->getType()); |
| 1089 | ExpandTypeToArgs(FD->getType(), FldArg, IRFuncTy, IRCallArgs, |
Alexey Samsonov | 8a0bad0 | 2014-09-29 18:41:28 +0000 | [diff] [blame] | 1090 | IRCallArgPos); |
| 1091 | } |
| 1092 | } else if (isa<ComplexExpansion>(Exp.get())) { |
Yaxun Liu | 5b330e8 | 2018-03-15 15:25:19 +0000 | [diff] [blame] | 1093 | ComplexPairTy CV = Arg.getKnownRValue().getComplexVal(); |
Alexey Samsonov | 8a0bad0 | 2014-09-29 18:41:28 +0000 | [diff] [blame] | 1094 | IRCallArgs[IRCallArgPos++] = CV.first; |
| 1095 | IRCallArgs[IRCallArgPos++] = CV.second; |
| 1096 | } else { |
| 1097 | assert(isa<NoExpansion>(Exp.get())); |
Yaxun Liu | 5b330e8 | 2018-03-15 15:25:19 +0000 | [diff] [blame] | 1098 | auto RV = Arg.getKnownRValue(); |
Alexey Samsonov | 8a0bad0 | 2014-09-29 18:41:28 +0000 | [diff] [blame] | 1099 | assert(RV.isScalar() && |
| 1100 | "Unexpected non-scalar rvalue during struct expansion."); |
| 1101 | |
| 1102 | // Insert a bitcast as needed. |
| 1103 | llvm::Value *V = RV.getScalarVal(); |
| 1104 | if (IRCallArgPos < IRFuncTy->getNumParams() && |
| 1105 | V->getType() != IRFuncTy->getParamType(IRCallArgPos)) |
| 1106 | V = Builder.CreateBitCast(V, IRFuncTy->getParamType(IRCallArgPos)); |
| 1107 | |
| 1108 | IRCallArgs[IRCallArgPos++] = V; |
| 1109 | } |
Daniel Dunbar | 8fc81b0 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1112 | /// Create a temporary allocation for the purposes of coercion. |
| 1113 | static Address CreateTempAllocaForCoercion(CodeGenFunction &CGF, llvm::Type *Ty, |
| 1114 | CharUnits MinAlign) { |
| 1115 | // Don't use an alignment that's worse than what LLVM would prefer. |
| 1116 | auto PrefAlign = CGF.CGM.getDataLayout().getPrefTypeAlignment(Ty); |
| 1117 | CharUnits Align = std::max(MinAlign, CharUnits::fromQuantity(PrefAlign)); |
| 1118 | |
| 1119 | return CGF.CreateTempAlloca(Ty, Align); |
| 1120 | } |
| 1121 | |
Chris Lattner | 895c52b | 2010-06-27 06:04:18 +0000 | [diff] [blame] | 1122 | /// EnterStructPointerForCoercedAccess - Given a struct pointer that we are |
Chris Lattner | 1cd6698 | 2010-06-27 05:56:15 +0000 | [diff] [blame] | 1123 | /// accessing some number of bytes out of it, try to gep into the struct to get |
| 1124 | /// at its inner goodness. Dive as deep as possible without entering an element |
| 1125 | /// with an in-memory size smaller than DstSize. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1126 | static Address |
| 1127 | EnterStructPointerForCoercedAccess(Address SrcPtr, |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1128 | llvm::StructType *SrcSTy, |
Chris Lattner | 895c52b | 2010-06-27 06:04:18 +0000 | [diff] [blame] | 1129 | uint64_t DstSize, CodeGenFunction &CGF) { |
Chris Lattner | 1cd6698 | 2010-06-27 05:56:15 +0000 | [diff] [blame] | 1130 | // We can't dive into a zero-element struct. |
| 1131 | if (SrcSTy->getNumElements() == 0) return SrcPtr; |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1132 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1133 | llvm::Type *FirstElt = SrcSTy->getElementType(0); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1134 | |
Chris Lattner | 1cd6698 | 2010-06-27 05:56:15 +0000 | [diff] [blame] | 1135 | // 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] | 1136 | // first element is the same size as the whole struct, we can enter it. The |
| 1137 | // comparison must be made on the store size and not the alloca size. Using |
| 1138 | // the alloca size may overstate the size of the load. |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1139 | uint64_t FirstEltSize = |
James Molloy | 90d6101 | 2014-08-29 10:17:52 +0000 | [diff] [blame] | 1140 | CGF.CGM.getDataLayout().getTypeStoreSize(FirstElt); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1141 | if (FirstEltSize < DstSize && |
James Molloy | 90d6101 | 2014-08-29 10:17:52 +0000 | [diff] [blame] | 1142 | FirstEltSize < CGF.CGM.getDataLayout().getTypeStoreSize(SrcSTy)) |
Chris Lattner | 1cd6698 | 2010-06-27 05:56:15 +0000 | [diff] [blame] | 1143 | return SrcPtr; |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1144 | |
Chris Lattner | 1cd6698 | 2010-06-27 05:56:15 +0000 | [diff] [blame] | 1145 | // GEP into the first element. |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 1146 | SrcPtr = CGF.Builder.CreateStructGEP(SrcPtr, 0, "coerce.dive"); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1147 | |
Chris Lattner | 1cd6698 | 2010-06-27 05:56:15 +0000 | [diff] [blame] | 1148 | // If the first element is a struct, recurse. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1149 | llvm::Type *SrcTy = SrcPtr.getElementType(); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1150 | if (llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy)) |
Chris Lattner | 895c52b | 2010-06-27 06:04:18 +0000 | [diff] [blame] | 1151 | return EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF); |
Chris Lattner | 1cd6698 | 2010-06-27 05:56:15 +0000 | [diff] [blame] | 1152 | |
| 1153 | return SrcPtr; |
| 1154 | } |
| 1155 | |
Chris Lattner | 055097f | 2010-06-27 06:26:04 +0000 | [diff] [blame] | 1156 | /// CoerceIntOrPtrToIntOrPtr - Convert a value Val to the specific Ty where both |
| 1157 | /// are either integers or pointers. This does a truncation of the value if it |
| 1158 | /// 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] | 1159 | /// |
| 1160 | /// This behaves as if the value were coerced through memory, so on big-endian |
| 1161 | /// targets the high bits are preserved in a truncation, while little-endian |
| 1162 | /// targets preserve the low bits. |
Chris Lattner | 055097f | 2010-06-27 06:26:04 +0000 | [diff] [blame] | 1163 | static llvm::Value *CoerceIntOrPtrToIntOrPtr(llvm::Value *Val, |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1164 | llvm::Type *Ty, |
Chris Lattner | 055097f | 2010-06-27 06:26:04 +0000 | [diff] [blame] | 1165 | CodeGenFunction &CGF) { |
| 1166 | if (Val->getType() == Ty) |
| 1167 | return Val; |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1168 | |
Chris Lattner | 055097f | 2010-06-27 06:26:04 +0000 | [diff] [blame] | 1169 | if (isa<llvm::PointerType>(Val->getType())) { |
| 1170 | // If this is Pointer->Pointer avoid conversion to and from int. |
| 1171 | if (isa<llvm::PointerType>(Ty)) |
| 1172 | return CGF.Builder.CreateBitCast(Val, Ty, "coerce.val"); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1173 | |
Chris Lattner | 055097f | 2010-06-27 06:26:04 +0000 | [diff] [blame] | 1174 | // 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] | 1175 | Val = CGF.Builder.CreatePtrToInt(Val, CGF.IntPtrTy, "coerce.val.pi"); |
Chris Lattner | 055097f | 2010-06-27 06:26:04 +0000 | [diff] [blame] | 1176 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1177 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1178 | llvm::Type *DestIntTy = Ty; |
Chris Lattner | 055097f | 2010-06-27 06:26:04 +0000 | [diff] [blame] | 1179 | if (isa<llvm::PointerType>(DestIntTy)) |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 1180 | DestIntTy = CGF.IntPtrTy; |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1181 | |
Jakob Stoklund Olesen | 36af252 | 2013-06-05 03:00:13 +0000 | [diff] [blame] | 1182 | if (Val->getType() != DestIntTy) { |
| 1183 | const llvm::DataLayout &DL = CGF.CGM.getDataLayout(); |
| 1184 | if (DL.isBigEndian()) { |
| 1185 | // Preserve the high bits on big-endian targets. |
| 1186 | // That is what memory coercion does. |
James Molloy | 491cefb | 2014-05-07 17:41:15 +0000 | [diff] [blame] | 1187 | uint64_t SrcSize = DL.getTypeSizeInBits(Val->getType()); |
| 1188 | uint64_t DstSize = DL.getTypeSizeInBits(DestIntTy); |
| 1189 | |
Jakob Stoklund Olesen | 36af252 | 2013-06-05 03:00:13 +0000 | [diff] [blame] | 1190 | if (SrcSize > DstSize) { |
| 1191 | Val = CGF.Builder.CreateLShr(Val, SrcSize - DstSize, "coerce.highbits"); |
| 1192 | Val = CGF.Builder.CreateTrunc(Val, DestIntTy, "coerce.val.ii"); |
| 1193 | } else { |
| 1194 | Val = CGF.Builder.CreateZExt(Val, DestIntTy, "coerce.val.ii"); |
| 1195 | Val = CGF.Builder.CreateShl(Val, DstSize - SrcSize, "coerce.highbits"); |
| 1196 | } |
| 1197 | } else { |
| 1198 | // Little-endian targets preserve the low bits. No shifts required. |
| 1199 | Val = CGF.Builder.CreateIntCast(Val, DestIntTy, false, "coerce.val.ii"); |
| 1200 | } |
| 1201 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1202 | |
Chris Lattner | 055097f | 2010-06-27 06:26:04 +0000 | [diff] [blame] | 1203 | if (isa<llvm::PointerType>(Ty)) |
| 1204 | Val = CGF.Builder.CreateIntToPtr(Val, Ty, "coerce.val.ip"); |
| 1205 | return Val; |
| 1206 | } |
| 1207 | |
Chris Lattner | 1cd6698 | 2010-06-27 05:56:15 +0000 | [diff] [blame] | 1208 | |
| 1209 | |
Daniel Dunbar | f5589ac | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 1210 | /// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as |
Ulrich Weigand | 6e2cea6 | 2015-07-10 11:31:43 +0000 | [diff] [blame] | 1211 | /// a pointer to an object of type \arg Ty, known to be aligned to |
| 1212 | /// \arg SrcAlign bytes. |
Daniel Dunbar | f5589ac | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 1213 | /// |
| 1214 | /// This safely handles the case when the src type is smaller than the |
| 1215 | /// destination type; in this situation the values of bits which not |
| 1216 | /// present in the src are undefined. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1217 | static llvm::Value *CreateCoercedLoad(Address Src, llvm::Type *Ty, |
Daniel Dunbar | f5589ac | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 1218 | CodeGenFunction &CGF) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1219 | llvm::Type *SrcTy = Src.getElementType(); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1220 | |
Chris Lattner | d200eda | 2010-06-28 22:51:39 +0000 | [diff] [blame] | 1221 | // If SrcTy and Ty are the same, just do a load. |
| 1222 | if (SrcTy == Ty) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1223 | return CGF.Builder.CreateLoad(Src); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1224 | |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 1225 | uint64_t DstSize = CGF.CGM.getDataLayout().getTypeAllocSize(Ty); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1226 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1227 | if (llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1228 | Src = EnterStructPointerForCoercedAccess(Src, SrcSTy, DstSize, CGF); |
| 1229 | SrcTy = Src.getType()->getElementType(); |
Chris Lattner | 1cd6698 | 2010-06-27 05:56:15 +0000 | [diff] [blame] | 1230 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1231 | |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 1232 | uint64_t SrcSize = CGF.CGM.getDataLayout().getTypeAllocSize(SrcTy); |
Daniel Dunbar | f5589ac | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 1233 | |
Chris Lattner | 055097f | 2010-06-27 06:26:04 +0000 | [diff] [blame] | 1234 | // If the source and destination are integer or pointer types, just do an |
| 1235 | // extension or truncation to the desired type. |
| 1236 | if ((isa<llvm::IntegerType>(Ty) || isa<llvm::PointerType>(Ty)) && |
| 1237 | (isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy))) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1238 | llvm::Value *Load = CGF.Builder.CreateLoad(Src); |
Chris Lattner | 055097f | 2010-06-27 06:26:04 +0000 | [diff] [blame] | 1239 | return CoerceIntOrPtrToIntOrPtr(Load, Ty, CGF); |
| 1240 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1241 | |
Daniel Dunbar | b52d077 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 1242 | // If load is legal, just bitcast the src pointer. |
Daniel Dunbar | ffdb843 | 2009-05-13 18:54:26 +0000 | [diff] [blame] | 1243 | if (SrcSize >= DstSize) { |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1244 | // Generally SrcSize is never greater than DstSize, since this means we are |
| 1245 | // losing bits. However, this can happen in cases where the structure has |
| 1246 | // additional padding, for example due to a user specified alignment. |
Daniel Dunbar | ffdb843 | 2009-05-13 18:54:26 +0000 | [diff] [blame] | 1247 | // |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1248 | // FIXME: Assert that we aren't truncating non-padding bits when have access |
| 1249 | // to that information. |
Matt Arsenault | 7a124f3 | 2017-08-01 20:36:57 +0000 | [diff] [blame] | 1250 | Src = CGF.Builder.CreateBitCast(Src, |
| 1251 | Ty->getPointerTo(Src.getAddressSpace())); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1252 | return CGF.Builder.CreateLoad(Src); |
Daniel Dunbar | f5589ac | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 1253 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1254 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1255 | // Otherwise do coercion through memory. This is stupid, but simple. |
| 1256 | Address Tmp = CreateTempAllocaForCoercion(CGF, Ty, Src.getAlignment()); |
Yaxun Liu | 4bbdebc | 2018-11-08 16:55:46 +0000 | [diff] [blame] | 1257 | Address Casted = CGF.Builder.CreateElementBitCast(Tmp,CGF.Int8Ty); |
| 1258 | Address SrcCasted = CGF.Builder.CreateElementBitCast(Src,CGF.Int8Ty); |
Manman Ren | 84b921f | 2012-11-28 22:08:52 +0000 | [diff] [blame] | 1259 | CGF.Builder.CreateMemCpy(Casted, SrcCasted, |
| 1260 | llvm::ConstantInt::get(CGF.IntPtrTy, SrcSize), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1261 | false); |
| 1262 | return CGF.Builder.CreateLoad(Tmp); |
Daniel Dunbar | f5589ac | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
Eli Friedman | af9b325 | 2011-05-17 21:08:01 +0000 | [diff] [blame] | 1265 | // Function to store a first-class aggregate into memory. We prefer to |
| 1266 | // store the elements rather than the aggregate to be more friendly to |
| 1267 | // fast-isel. |
| 1268 | // FIXME: Do we need to recurse here? |
| 1269 | static void BuildAggStore(CodeGenFunction &CGF, llvm::Value *Val, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1270 | Address Dest, bool DestIsVolatile) { |
Eli Friedman | af9b325 | 2011-05-17 21:08:01 +0000 | [diff] [blame] | 1271 | // Prefer scalar stores to first-class aggregate stores. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1272 | if (llvm::StructType *STy = |
Eli Friedman | af9b325 | 2011-05-17 21:08:01 +0000 | [diff] [blame] | 1273 | dyn_cast<llvm::StructType>(Val->getType())) { |
| 1274 | for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 1275 | Address EltPtr = CGF.Builder.CreateStructGEP(Dest, i); |
Eli Friedman | af9b325 | 2011-05-17 21:08:01 +0000 | [diff] [blame] | 1276 | llvm::Value *Elt = CGF.Builder.CreateExtractValue(Val, i); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1277 | CGF.Builder.CreateStore(Elt, EltPtr, DestIsVolatile); |
Eli Friedman | af9b325 | 2011-05-17 21:08:01 +0000 | [diff] [blame] | 1278 | } |
| 1279 | } else { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1280 | CGF.Builder.CreateStore(Val, Dest, DestIsVolatile); |
Eli Friedman | af9b325 | 2011-05-17 21:08:01 +0000 | [diff] [blame] | 1281 | } |
| 1282 | } |
| 1283 | |
Daniel Dunbar | f5589ac | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 1284 | /// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src, |
Ulrich Weigand | 6e2cea6 | 2015-07-10 11:31:43 +0000 | [diff] [blame] | 1285 | /// where the source and destination may have different types. The |
| 1286 | /// destination is known to be aligned to \arg DstAlign bytes. |
Daniel Dunbar | f5589ac | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 1287 | /// |
| 1288 | /// This safely handles the case when the src type is larger than the |
| 1289 | /// destination type; the upper bits of the src will be lost. |
| 1290 | static void CreateCoercedStore(llvm::Value *Src, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1291 | Address Dst, |
Anders Carlsson | 1749083 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 1292 | bool DstIsVolatile, |
Daniel Dunbar | f5589ac | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 1293 | CodeGenFunction &CGF) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1294 | llvm::Type *SrcTy = Src->getType(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1295 | llvm::Type *DstTy = Dst.getType()->getElementType(); |
Chris Lattner | d200eda | 2010-06-28 22:51:39 +0000 | [diff] [blame] | 1296 | if (SrcTy == DstTy) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1297 | CGF.Builder.CreateStore(Src, Dst, DstIsVolatile); |
Chris Lattner | d200eda | 2010-06-28 22:51:39 +0000 | [diff] [blame] | 1298 | return; |
| 1299 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1300 | |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 1301 | uint64_t SrcSize = CGF.CGM.getDataLayout().getTypeAllocSize(SrcTy); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1302 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1303 | if (llvm::StructType *DstSTy = dyn_cast<llvm::StructType>(DstTy)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1304 | Dst = EnterStructPointerForCoercedAccess(Dst, DstSTy, SrcSize, CGF); |
| 1305 | DstTy = Dst.getType()->getElementType(); |
Chris Lattner | 895c52b | 2010-06-27 06:04:18 +0000 | [diff] [blame] | 1306 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1307 | |
Chris Lattner | 055097f | 2010-06-27 06:26:04 +0000 | [diff] [blame] | 1308 | // If the source and destination are integer or pointer types, just do an |
| 1309 | // extension or truncation to the desired type. |
| 1310 | if ((isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy)) && |
| 1311 | (isa<llvm::IntegerType>(DstTy) || isa<llvm::PointerType>(DstTy))) { |
| 1312 | Src = CoerceIntOrPtrToIntOrPtr(Src, DstTy, CGF); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1313 | CGF.Builder.CreateStore(Src, Dst, DstIsVolatile); |
Chris Lattner | 055097f | 2010-06-27 06:26:04 +0000 | [diff] [blame] | 1314 | return; |
| 1315 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1316 | |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 1317 | uint64_t DstSize = CGF.CGM.getDataLayout().getTypeAllocSize(DstTy); |
Daniel Dunbar | f5589ac | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 1318 | |
Daniel Dunbar | 313321e | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 1319 | // If store is legal, just bitcast the src pointer. |
Daniel Dunbar | 4be99ff | 2009-06-05 07:58:54 +0000 | [diff] [blame] | 1320 | if (SrcSize <= DstSize) { |
Yaxun Liu | e9e5c4f | 2017-06-29 18:47:45 +0000 | [diff] [blame] | 1321 | Dst = CGF.Builder.CreateElementBitCast(Dst, SrcTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1322 | BuildAggStore(CGF, Src, Dst, DstIsVolatile); |
Daniel Dunbar | f5589ac | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 1323 | } else { |
Daniel Dunbar | f5589ac | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 1324 | // Otherwise do coercion through memory. This is stupid, but |
| 1325 | // simple. |
Daniel Dunbar | 4be99ff | 2009-06-05 07:58:54 +0000 | [diff] [blame] | 1326 | |
| 1327 | // Generally SrcSize is never greater than DstSize, since this means we are |
| 1328 | // losing bits. However, this can happen in cases where the structure has |
| 1329 | // additional padding, for example due to a user specified alignment. |
| 1330 | // |
| 1331 | // FIXME: Assert that we aren't truncating non-padding bits when have access |
| 1332 | // to that information. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1333 | Address Tmp = CreateTempAllocaForCoercion(CGF, SrcTy, Dst.getAlignment()); |
| 1334 | CGF.Builder.CreateStore(Src, Tmp); |
Yaxun Liu | 4bbdebc | 2018-11-08 16:55:46 +0000 | [diff] [blame] | 1335 | Address Casted = CGF.Builder.CreateElementBitCast(Tmp,CGF.Int8Ty); |
| 1336 | Address DstCasted = CGF.Builder.CreateElementBitCast(Dst,CGF.Int8Ty); |
Manman Ren | 84b921f | 2012-11-28 22:08:52 +0000 | [diff] [blame] | 1337 | CGF.Builder.CreateMemCpy(DstCasted, Casted, |
| 1338 | llvm::ConstantInt::get(CGF.IntPtrTy, DstSize), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1339 | false); |
Daniel Dunbar | f5589ac | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 1340 | } |
| 1341 | } |
| 1342 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1343 | static Address emitAddressAtOffset(CodeGenFunction &CGF, Address addr, |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1344 | const ABIArgInfo &info) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1345 | if (unsigned offset = info.getDirectOffset()) { |
| 1346 | addr = CGF.Builder.CreateElementBitCast(addr, CGF.Int8Ty); |
| 1347 | addr = CGF.Builder.CreateConstInBoundsByteGEP(addr, |
| 1348 | CharUnits::fromQuantity(offset)); |
| 1349 | addr = CGF.Builder.CreateElementBitCast(addr, info.getCoerceToType()); |
| 1350 | } |
| 1351 | return addr; |
| 1352 | } |
| 1353 | |
Alexey Samsonov | 153004f | 2014-09-29 22:08:00 +0000 | [diff] [blame] | 1354 | namespace { |
| 1355 | |
| 1356 | /// Encapsulates information about the way function arguments from |
| 1357 | /// CGFunctionInfo should be passed to actual LLVM IR function. |
| 1358 | class ClangToLLVMArgMapping { |
| 1359 | static const unsigned InvalidIndex = ~0U; |
| 1360 | unsigned InallocaArgNo; |
| 1361 | unsigned SRetArgNo; |
| 1362 | unsigned TotalIRArgs; |
| 1363 | |
| 1364 | /// Arguments of LLVM IR function corresponding to single Clang argument. |
| 1365 | struct IRArgs { |
| 1366 | unsigned PaddingArgIndex; |
| 1367 | // Argument is expanded to IR arguments at positions |
| 1368 | // [FirstArgIndex, FirstArgIndex + NumberOfArgs). |
| 1369 | unsigned FirstArgIndex; |
| 1370 | unsigned NumberOfArgs; |
| 1371 | |
| 1372 | IRArgs() |
| 1373 | : PaddingArgIndex(InvalidIndex), FirstArgIndex(InvalidIndex), |
| 1374 | NumberOfArgs(0) {} |
| 1375 | }; |
| 1376 | |
| 1377 | SmallVector<IRArgs, 8> ArgInfo; |
| 1378 | |
| 1379 | public: |
| 1380 | ClangToLLVMArgMapping(const ASTContext &Context, const CGFunctionInfo &FI, |
| 1381 | bool OnlyRequiredArgs = false) |
| 1382 | : InallocaArgNo(InvalidIndex), SRetArgNo(InvalidIndex), TotalIRArgs(0), |
| 1383 | ArgInfo(OnlyRequiredArgs ? FI.getNumRequiredArgs() : FI.arg_size()) { |
| 1384 | construct(Context, FI, OnlyRequiredArgs); |
| 1385 | } |
| 1386 | |
| 1387 | bool hasInallocaArg() const { return InallocaArgNo != InvalidIndex; } |
| 1388 | unsigned getInallocaArgNo() const { |
| 1389 | assert(hasInallocaArg()); |
| 1390 | return InallocaArgNo; |
| 1391 | } |
| 1392 | |
| 1393 | bool hasSRetArg() const { return SRetArgNo != InvalidIndex; } |
| 1394 | unsigned getSRetArgNo() const { |
| 1395 | assert(hasSRetArg()); |
| 1396 | return SRetArgNo; |
| 1397 | } |
| 1398 | |
| 1399 | unsigned totalIRArgs() const { return TotalIRArgs; } |
| 1400 | |
| 1401 | bool hasPaddingArg(unsigned ArgNo) const { |
| 1402 | assert(ArgNo < ArgInfo.size()); |
| 1403 | return ArgInfo[ArgNo].PaddingArgIndex != InvalidIndex; |
| 1404 | } |
| 1405 | unsigned getPaddingArgNo(unsigned ArgNo) const { |
| 1406 | assert(hasPaddingArg(ArgNo)); |
| 1407 | return ArgInfo[ArgNo].PaddingArgIndex; |
| 1408 | } |
| 1409 | |
| 1410 | /// Returns index of first IR argument corresponding to ArgNo, and their |
| 1411 | /// quantity. |
| 1412 | std::pair<unsigned, unsigned> getIRArgs(unsigned ArgNo) const { |
| 1413 | assert(ArgNo < ArgInfo.size()); |
| 1414 | return std::make_pair(ArgInfo[ArgNo].FirstArgIndex, |
| 1415 | ArgInfo[ArgNo].NumberOfArgs); |
| 1416 | } |
| 1417 | |
| 1418 | private: |
| 1419 | void construct(const ASTContext &Context, const CGFunctionInfo &FI, |
| 1420 | bool OnlyRequiredArgs); |
| 1421 | }; |
| 1422 | |
| 1423 | void ClangToLLVMArgMapping::construct(const ASTContext &Context, |
| 1424 | const CGFunctionInfo &FI, |
| 1425 | bool OnlyRequiredArgs) { |
| 1426 | unsigned IRArgNo = 0; |
| 1427 | bool SwapThisWithSRet = false; |
| 1428 | const ABIArgInfo &RetAI = FI.getReturnInfo(); |
| 1429 | |
| 1430 | if (RetAI.getKind() == ABIArgInfo::Indirect) { |
| 1431 | SwapThisWithSRet = RetAI.isSRetAfterThis(); |
| 1432 | SRetArgNo = SwapThisWithSRet ? 1 : IRArgNo++; |
| 1433 | } |
| 1434 | |
| 1435 | unsigned ArgNo = 0; |
| 1436 | unsigned NumArgs = OnlyRequiredArgs ? FI.getNumRequiredArgs() : FI.arg_size(); |
| 1437 | for (CGFunctionInfo::const_arg_iterator I = FI.arg_begin(); ArgNo < NumArgs; |
| 1438 | ++I, ++ArgNo) { |
| 1439 | assert(I != FI.arg_end()); |
| 1440 | QualType ArgType = I->type; |
| 1441 | const ABIArgInfo &AI = I->info; |
| 1442 | // Collect data about IR arguments corresponding to Clang argument ArgNo. |
| 1443 | auto &IRArgs = ArgInfo[ArgNo]; |
| 1444 | |
| 1445 | if (AI.getPaddingType()) |
| 1446 | IRArgs.PaddingArgIndex = IRArgNo++; |
| 1447 | |
| 1448 | switch (AI.getKind()) { |
| 1449 | case ABIArgInfo::Extend: |
| 1450 | case ABIArgInfo::Direct: { |
| 1451 | // FIXME: handle sseregparm someday... |
| 1452 | llvm::StructType *STy = dyn_cast<llvm::StructType>(AI.getCoerceToType()); |
| 1453 | if (AI.isDirect() && AI.getCanBeFlattened() && STy) { |
| 1454 | IRArgs.NumberOfArgs = STy->getNumElements(); |
| 1455 | } else { |
| 1456 | IRArgs.NumberOfArgs = 1; |
| 1457 | } |
| 1458 | break; |
| 1459 | } |
| 1460 | case ABIArgInfo::Indirect: |
| 1461 | IRArgs.NumberOfArgs = 1; |
| 1462 | break; |
| 1463 | case ABIArgInfo::Ignore: |
| 1464 | case ABIArgInfo::InAlloca: |
| 1465 | // ignore and inalloca doesn't have matching LLVM parameters. |
| 1466 | IRArgs.NumberOfArgs = 0; |
| 1467 | break; |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 1468 | case ABIArgInfo::CoerceAndExpand: |
| 1469 | IRArgs.NumberOfArgs = AI.getCoerceAndExpandTypeSequence().size(); |
| 1470 | break; |
| 1471 | case ABIArgInfo::Expand: |
Alexey Samsonov | 153004f | 2014-09-29 22:08:00 +0000 | [diff] [blame] | 1472 | IRArgs.NumberOfArgs = getExpansionSize(ArgType, Context); |
| 1473 | break; |
| 1474 | } |
Alexey Samsonov | 153004f | 2014-09-29 22:08:00 +0000 | [diff] [blame] | 1475 | |
| 1476 | if (IRArgs.NumberOfArgs > 0) { |
| 1477 | IRArgs.FirstArgIndex = IRArgNo; |
| 1478 | IRArgNo += IRArgs.NumberOfArgs; |
| 1479 | } |
| 1480 | |
| 1481 | // Skip over the sret parameter when it comes second. We already handled it |
| 1482 | // above. |
| 1483 | if (IRArgNo == 1 && SwapThisWithSRet) |
| 1484 | IRArgNo++; |
| 1485 | } |
| 1486 | assert(ArgNo == ArgInfo.size()); |
| 1487 | |
| 1488 | if (FI.usesInAlloca()) |
| 1489 | InallocaArgNo = IRArgNo++; |
| 1490 | |
| 1491 | TotalIRArgs = IRArgNo; |
| 1492 | } |
| 1493 | } // namespace |
| 1494 | |
Daniel Dunbar | 8fc81b0 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1495 | /***/ |
| 1496 | |
Daniel Dunbar | 6f2e839 | 2010-07-14 23:39:36 +0000 | [diff] [blame] | 1497 | bool CodeGenModule::ReturnTypeUsesSRet(const CGFunctionInfo &FI) { |
Saleem Abdulrasool | f181f1a | 2018-02-28 20:16:12 +0000 | [diff] [blame] | 1498 | const auto &RI = FI.getReturnInfo(); |
| 1499 | return RI.isIndirect() || (RI.isInAlloca() && RI.getInAllocaSRet()); |
Daniel Dunbar | 7633cbf | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 1500 | } |
| 1501 | |
Tim Northover | e77cc39 | 2014-03-29 13:28:05 +0000 | [diff] [blame] | 1502 | bool CodeGenModule::ReturnSlotInterferesWithArgs(const CGFunctionInfo &FI) { |
| 1503 | return ReturnTypeUsesSRet(FI) && |
| 1504 | getTargetCodeGenInfo().doesReturnSlotInterfereWithArgs(); |
| 1505 | } |
| 1506 | |
Daniel Dunbar | 6f2e839 | 2010-07-14 23:39:36 +0000 | [diff] [blame] | 1507 | bool CodeGenModule::ReturnTypeUsesFPRet(QualType ResultType) { |
| 1508 | if (const BuiltinType *BT = ResultType->getAs<BuiltinType>()) { |
| 1509 | switch (BT->getKind()) { |
| 1510 | default: |
| 1511 | return false; |
| 1512 | case BuiltinType::Float: |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1513 | return getTarget().useObjCFPRetForRealType(TargetInfo::Float); |
Daniel Dunbar | 6f2e839 | 2010-07-14 23:39:36 +0000 | [diff] [blame] | 1514 | case BuiltinType::Double: |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1515 | return getTarget().useObjCFPRetForRealType(TargetInfo::Double); |
Daniel Dunbar | 6f2e839 | 2010-07-14 23:39:36 +0000 | [diff] [blame] | 1516 | case BuiltinType::LongDouble: |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1517 | return getTarget().useObjCFPRetForRealType(TargetInfo::LongDouble); |
Daniel Dunbar | 6f2e839 | 2010-07-14 23:39:36 +0000 | [diff] [blame] | 1518 | } |
| 1519 | } |
| 1520 | |
| 1521 | return false; |
| 1522 | } |
| 1523 | |
Anders Carlsson | 2f1a6c3 | 2011-10-31 16:27:11 +0000 | [diff] [blame] | 1524 | bool CodeGenModule::ReturnTypeUsesFP2Ret(QualType ResultType) { |
| 1525 | if (const ComplexType *CT = ResultType->getAs<ComplexType>()) { |
| 1526 | if (const BuiltinType *BT = CT->getElementType()->getAs<BuiltinType>()) { |
| 1527 | if (BT->getKind() == BuiltinType::LongDouble) |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1528 | return getTarget().useObjCFP2RetForComplexLongDouble(); |
Anders Carlsson | 2f1a6c3 | 2011-10-31 16:27:11 +0000 | [diff] [blame] | 1529 | } |
| 1530 | } |
| 1531 | |
| 1532 | return false; |
| 1533 | } |
| 1534 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1535 | llvm::FunctionType *CodeGenTypes::GetFunctionType(GlobalDecl GD) { |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1536 | const CGFunctionInfo &FI = arrangeGlobalDeclaration(GD); |
| 1537 | return GetFunctionType(FI); |
John McCall | f8ff7b9 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 1538 | } |
| 1539 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1540 | llvm::FunctionType * |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1541 | CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI) { |
Alexey Samsonov | 153004f | 2014-09-29 22:08:00 +0000 | [diff] [blame] | 1542 | |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 1543 | bool Inserted = FunctionsBeingProcessed.insert(&FI).second; |
| 1544 | (void)Inserted; |
Chris Lattner | 6fb0ccf | 2011-07-15 05:16:14 +0000 | [diff] [blame] | 1545 | assert(Inserted && "Recursively being processed?"); |
Daniel Dunbar | 7a95ca3 | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1546 | |
Alexey Samsonov | 153004f | 2014-09-29 22:08:00 +0000 | [diff] [blame] | 1547 | llvm::Type *resultType = nullptr; |
John McCall | 85dd2c5 | 2011-05-15 02:19:42 +0000 | [diff] [blame] | 1548 | const ABIArgInfo &retAI = FI.getReturnInfo(); |
| 1549 | switch (retAI.getKind()) { |
Daniel Dunbar | d3674e6 | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1550 | case ABIArgInfo::Expand: |
John McCall | 85dd2c5 | 2011-05-15 02:19:42 +0000 | [diff] [blame] | 1551 | llvm_unreachable("Invalid ABI kind for return argument"); |
Daniel Dunbar | d3674e6 | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1552 | |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 1553 | case ABIArgInfo::Extend: |
Daniel Dunbar | 67dace89 | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 1554 | case ABIArgInfo::Direct: |
John McCall | 85dd2c5 | 2011-05-15 02:19:42 +0000 | [diff] [blame] | 1555 | resultType = retAI.getCoerceToType(); |
Daniel Dunbar | 67dace89 | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 1556 | break; |
| 1557 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1558 | case ABIArgInfo::InAlloca: |
Reid Kleckner | fab1e89 | 2014-02-25 00:59:14 +0000 | [diff] [blame] | 1559 | if (retAI.getInAllocaSRet()) { |
| 1560 | // sret things on win32 aren't void, they return the sret pointer. |
| 1561 | QualType ret = FI.getReturnType(); |
| 1562 | llvm::Type *ty = ConvertType(ret); |
| 1563 | unsigned addressSpace = Context.getTargetAddressSpace(ret); |
| 1564 | resultType = llvm::PointerType::get(ty, addressSpace); |
| 1565 | } else { |
| 1566 | resultType = llvm::Type::getVoidTy(getLLVMContext()); |
| 1567 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1568 | break; |
| 1569 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1570 | case ABIArgInfo::Indirect: |
Daniel Dunbar | 94a6f25 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1571 | case ABIArgInfo::Ignore: |
John McCall | 85dd2c5 | 2011-05-15 02:19:42 +0000 | [diff] [blame] | 1572 | resultType = llvm::Type::getVoidTy(getLLVMContext()); |
Daniel Dunbar | 94a6f25 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1573 | break; |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 1574 | |
| 1575 | case ABIArgInfo::CoerceAndExpand: |
| 1576 | resultType = retAI.getUnpaddedCoerceAndExpandType(); |
| 1577 | break; |
Daniel Dunbar | 7a95ca3 | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1578 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1579 | |
Alexey Samsonov | 153004f | 2014-09-29 22:08:00 +0000 | [diff] [blame] | 1580 | ClangToLLVMArgMapping IRFunctionArgs(getContext(), FI, true); |
| 1581 | SmallVector<llvm::Type*, 8> ArgTypes(IRFunctionArgs.totalIRArgs()); |
| 1582 | |
| 1583 | // Add type for sret argument. |
| 1584 | if (IRFunctionArgs.hasSRetArg()) { |
| 1585 | QualType Ret = FI.getReturnType(); |
| 1586 | llvm::Type *Ty = ConvertType(Ret); |
| 1587 | unsigned AddressSpace = Context.getTargetAddressSpace(Ret); |
| 1588 | ArgTypes[IRFunctionArgs.getSRetArgNo()] = |
| 1589 | llvm::PointerType::get(Ty, AddressSpace); |
| 1590 | } |
| 1591 | |
| 1592 | // Add type for inalloca argument. |
| 1593 | if (IRFunctionArgs.hasInallocaArg()) { |
| 1594 | auto ArgStruct = FI.getArgStruct(); |
| 1595 | assert(ArgStruct); |
| 1596 | ArgTypes[IRFunctionArgs.getInallocaArgNo()] = ArgStruct->getPointerTo(); |
| 1597 | } |
| 1598 | |
John McCall | c818bbb | 2012-12-07 07:03:17 +0000 | [diff] [blame] | 1599 | // Add in all of the required arguments. |
Alexey Samsonov | 153004f | 2014-09-29 22:08:00 +0000 | [diff] [blame] | 1600 | unsigned ArgNo = 0; |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 1601 | CGFunctionInfo::const_arg_iterator it = FI.arg_begin(), |
| 1602 | ie = it + FI.getNumRequiredArgs(); |
Alexey Samsonov | 153004f | 2014-09-29 22:08:00 +0000 | [diff] [blame] | 1603 | for (; it != ie; ++it, ++ArgNo) { |
| 1604 | const ABIArgInfo &ArgInfo = it->info; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1605 | |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1606 | // Insert a padding type to ensure proper alignment. |
Alexey Samsonov | 153004f | 2014-09-29 22:08:00 +0000 | [diff] [blame] | 1607 | if (IRFunctionArgs.hasPaddingArg(ArgNo)) |
| 1608 | ArgTypes[IRFunctionArgs.getPaddingArgNo(ArgNo)] = |
| 1609 | ArgInfo.getPaddingType(); |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1610 | |
Alexey Samsonov | 153004f | 2014-09-29 22:08:00 +0000 | [diff] [blame] | 1611 | unsigned FirstIRArg, NumIRArgs; |
| 1612 | std::tie(FirstIRArg, NumIRArgs) = IRFunctionArgs.getIRArgs(ArgNo); |
| 1613 | |
| 1614 | switch (ArgInfo.getKind()) { |
Daniel Dunbar | 94a6f25 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1615 | case ABIArgInfo::Ignore: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1616 | case ABIArgInfo::InAlloca: |
Alexey Samsonov | 153004f | 2014-09-29 22:08:00 +0000 | [diff] [blame] | 1617 | assert(NumIRArgs == 0); |
Daniel Dunbar | 94a6f25 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1618 | break; |
| 1619 | |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1620 | case ABIArgInfo::Indirect: { |
Alexey Samsonov | 153004f | 2014-09-29 22:08:00 +0000 | [diff] [blame] | 1621 | assert(NumIRArgs == 1); |
Yaxun Liu | d752328 | 2017-04-17 20:10:44 +0000 | [diff] [blame] | 1622 | // indirect arguments are always on the stack, which is alloca addr space. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1623 | llvm::Type *LTy = ConvertTypeForMem(it->type); |
Yaxun Liu | d752328 | 2017-04-17 20:10:44 +0000 | [diff] [blame] | 1624 | ArgTypes[FirstIRArg] = LTy->getPointerTo( |
| 1625 | CGM.getDataLayout().getAllocaAddrSpace()); |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1626 | break; |
| 1627 | } |
| 1628 | |
| 1629 | case ABIArgInfo::Extend: |
Chris Lattner | 2cdfda4 | 2010-07-29 06:44:09 +0000 | [diff] [blame] | 1630 | case ABIArgInfo::Direct: { |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 1631 | // Fast-isel and the optimizer generally like scalar values better than |
| 1632 | // 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] | 1633 | llvm::Type *argType = ArgInfo.getCoerceToType(); |
James Molloy | 6f244b6 | 2014-05-09 16:21:39 +0000 | [diff] [blame] | 1634 | llvm::StructType *st = dyn_cast<llvm::StructType>(argType); |
Alexey Samsonov | 153004f | 2014-09-29 22:08:00 +0000 | [diff] [blame] | 1635 | if (st && ArgInfo.isDirect() && ArgInfo.getCanBeFlattened()) { |
| 1636 | assert(NumIRArgs == st->getNumElements()); |
John McCall | 85dd2c5 | 2011-05-15 02:19:42 +0000 | [diff] [blame] | 1637 | for (unsigned i = 0, e = st->getNumElements(); i != e; ++i) |
Alexey Samsonov | 153004f | 2014-09-29 22:08:00 +0000 | [diff] [blame] | 1638 | ArgTypes[FirstIRArg + i] = st->getElementType(i); |
Chris Lattner | 3dd716c | 2010-06-28 23:44:11 +0000 | [diff] [blame] | 1639 | } else { |
Alexey Samsonov | 153004f | 2014-09-29 22:08:00 +0000 | [diff] [blame] | 1640 | assert(NumIRArgs == 1); |
| 1641 | ArgTypes[FirstIRArg] = argType; |
Chris Lattner | 3dd716c | 2010-06-28 23:44:11 +0000 | [diff] [blame] | 1642 | } |
Daniel Dunbar | 2f219b0 | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 1643 | break; |
Chris Lattner | 2cdfda4 | 2010-07-29 06:44:09 +0000 | [diff] [blame] | 1644 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1645 | |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 1646 | case ABIArgInfo::CoerceAndExpand: { |
| 1647 | auto ArgTypesIter = ArgTypes.begin() + FirstIRArg; |
| 1648 | for (auto EltTy : ArgInfo.getCoerceAndExpandTypeSequence()) { |
| 1649 | *ArgTypesIter++ = EltTy; |
| 1650 | } |
| 1651 | assert(ArgTypesIter == ArgTypes.begin() + FirstIRArg + NumIRArgs); |
| 1652 | break; |
| 1653 | } |
| 1654 | |
Daniel Dunbar | d3674e6 | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1655 | case ABIArgInfo::Expand: |
Alexey Samsonov | 153004f | 2014-09-29 22:08:00 +0000 | [diff] [blame] | 1656 | auto ArgTypesIter = ArgTypes.begin() + FirstIRArg; |
| 1657 | getExpandedTypes(it->type, ArgTypesIter); |
| 1658 | assert(ArgTypesIter == ArgTypes.begin() + FirstIRArg + NumIRArgs); |
Daniel Dunbar | d3674e6 | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1659 | break; |
| 1660 | } |
Daniel Dunbar | 7a95ca3 | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1661 | } |
| 1662 | |
Chris Lattner | 6fb0ccf | 2011-07-15 05:16:14 +0000 | [diff] [blame] | 1663 | bool Erased = FunctionsBeingProcessed.erase(&FI); (void)Erased; |
| 1664 | assert(Erased && "Not in set?"); |
Alexey Samsonov | 153004f | 2014-09-29 22:08:00 +0000 | [diff] [blame] | 1665 | |
| 1666 | return llvm::FunctionType::get(resultType, ArgTypes, FI.isVariadic()); |
Daniel Dunbar | 81cf67f | 2008-09-09 23:48:28 +0000 | [diff] [blame] | 1667 | } |
| 1668 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1669 | llvm::Type *CodeGenTypes::GetFunctionTypeForVTable(GlobalDecl GD) { |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 1670 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
Anders Carlsson | 6445773 | 2009-11-24 05:08:52 +0000 | [diff] [blame] | 1671 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1672 | |
Chris Lattner | 8806e32 | 2011-07-10 00:18:59 +0000 | [diff] [blame] | 1673 | if (!isFuncTypeConvertible(FPT)) |
| 1674 | return llvm::StructType::get(getLLVMContext()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1675 | |
Peter Collingbourne | d1c5b28 | 2019-03-22 23:05:10 +0000 | [diff] [blame] | 1676 | return GetFunctionType(GD); |
Anders Carlsson | 6445773 | 2009-11-24 05:08:52 +0000 | [diff] [blame] | 1677 | } |
| 1678 | |
Samuel Antao | 798f11c | 2015-11-23 22:04:44 +0000 | [diff] [blame] | 1679 | static void AddAttributesFromFunctionProtoType(ASTContext &Ctx, |
| 1680 | llvm::AttrBuilder &FuncAttrs, |
| 1681 | const FunctionProtoType *FPT) { |
| 1682 | if (!FPT) |
| 1683 | return; |
| 1684 | |
| 1685 | if (!isUnresolvedExceptionSpec(FPT->getExceptionSpecType()) && |
Richard Smith | eaf11ad | 2018-05-03 03:58:32 +0000 | [diff] [blame] | 1686 | FPT->isNothrow()) |
Samuel Antao | 798f11c | 2015-11-23 22:04:44 +0000 | [diff] [blame] | 1687 | FuncAttrs.addAttribute(llvm::Attribute::NoUnwind); |
| 1688 | } |
| 1689 | |
Justin Lebar | b080b63 | 2017-01-25 21:29:48 +0000 | [diff] [blame] | 1690 | void CodeGenModule::ConstructDefaultFnAttrList(StringRef Name, bool HasOptnone, |
| 1691 | bool AttrOnCallSite, |
| 1692 | llvm::AttrBuilder &FuncAttrs) { |
| 1693 | // OptimizeNoneAttr takes precedence over -Os or -Oz. No warning needed. |
| 1694 | if (!HasOptnone) { |
| 1695 | if (CodeGenOpts.OptimizeSize) |
| 1696 | FuncAttrs.addAttribute(llvm::Attribute::OptimizeForSize); |
| 1697 | if (CodeGenOpts.OptimizeSize == 2) |
| 1698 | FuncAttrs.addAttribute(llvm::Attribute::MinSize); |
| 1699 | } |
| 1700 | |
| 1701 | if (CodeGenOpts.DisableRedZone) |
| 1702 | FuncAttrs.addAttribute(llvm::Attribute::NoRedZone); |
Kristina Brooks | 7f569b7 | 2018-10-18 14:07:02 +0000 | [diff] [blame] | 1703 | if (CodeGenOpts.IndirectTlsSegRefs) |
| 1704 | FuncAttrs.addAttribute("indirect-tls-seg-refs"); |
Justin Lebar | b080b63 | 2017-01-25 21:29:48 +0000 | [diff] [blame] | 1705 | if (CodeGenOpts.NoImplicitFloat) |
| 1706 | FuncAttrs.addAttribute(llvm::Attribute::NoImplicitFloat); |
| 1707 | |
| 1708 | if (AttrOnCallSite) { |
| 1709 | // Attributes that should go on the call site only. |
| 1710 | if (!CodeGenOpts.SimplifyLibCalls || |
| 1711 | CodeGenOpts.isNoBuiltinFunc(Name.data())) |
| 1712 | FuncAttrs.addAttribute(llvm::Attribute::NoBuiltin); |
| 1713 | if (!CodeGenOpts.TrapFuncName.empty()) |
| 1714 | FuncAttrs.addAttribute("trap-func-name", CodeGenOpts.TrapFuncName); |
| 1715 | } else { |
Yuanfang Chen | ff22ec3 | 2019-07-20 22:50:50 +0000 | [diff] [blame] | 1716 | StringRef FpKind; |
| 1717 | switch (CodeGenOpts.getFramePointer()) { |
| 1718 | case CodeGenOptions::FramePointerKind::None: |
| 1719 | FpKind = "none"; |
| 1720 | break; |
| 1721 | case CodeGenOptions::FramePointerKind::NonLeaf: |
| 1722 | FpKind = "non-leaf"; |
| 1723 | break; |
| 1724 | case CodeGenOptions::FramePointerKind::All: |
| 1725 | FpKind = "all"; |
| 1726 | break; |
Justin Lebar | b080b63 | 2017-01-25 21:29:48 +0000 | [diff] [blame] | 1727 | } |
Yuanfang Chen | ff22ec3 | 2019-07-20 22:50:50 +0000 | [diff] [blame] | 1728 | FuncAttrs.addAttribute("frame-pointer", FpKind); |
Justin Lebar | b080b63 | 2017-01-25 21:29:48 +0000 | [diff] [blame] | 1729 | |
| 1730 | FuncAttrs.addAttribute("less-precise-fpmad", |
| 1731 | llvm::toStringRef(CodeGenOpts.LessPreciseFPMAD)); |
| 1732 | |
Manoj Gupta | da08f6a | 2018-07-19 00:44:52 +0000 | [diff] [blame] | 1733 | if (CodeGenOpts.NullPointerIsValid) |
| 1734 | FuncAttrs.addAttribute("null-pointer-is-valid", "true"); |
Justin Lebar | b080b63 | 2017-01-25 21:29:48 +0000 | [diff] [blame] | 1735 | if (!CodeGenOpts.FPDenormalMode.empty()) |
| 1736 | FuncAttrs.addAttribute("denormal-fp-math", CodeGenOpts.FPDenormalMode); |
| 1737 | |
| 1738 | FuncAttrs.addAttribute("no-trapping-math", |
| 1739 | llvm::toStringRef(CodeGenOpts.NoTrappingMath)); |
| 1740 | |
Sanjay Patel | c81450e | 2018-04-30 18:19:03 +0000 | [diff] [blame] | 1741 | // Strict (compliant) code is the default, so only add this attribute to |
| 1742 | // indicate that we are trying to workaround a problem case. |
| 1743 | if (!CodeGenOpts.StrictFloatCastOverflow) |
| 1744 | FuncAttrs.addAttribute("strict-float-cast-overflow", "false"); |
Sanjay Patel | d175476 | 2018-04-27 14:22:48 +0000 | [diff] [blame] | 1745 | |
Justin Lebar | b080b63 | 2017-01-25 21:29:48 +0000 | [diff] [blame] | 1746 | // TODO: Are these all needed? |
| 1747 | // unsafe/inf/nan/nsz are handled by instruction-level FastMathFlags. |
| 1748 | FuncAttrs.addAttribute("no-infs-fp-math", |
| 1749 | llvm::toStringRef(CodeGenOpts.NoInfsFPMath)); |
| 1750 | FuncAttrs.addAttribute("no-nans-fp-math", |
| 1751 | llvm::toStringRef(CodeGenOpts.NoNaNsFPMath)); |
| 1752 | FuncAttrs.addAttribute("unsafe-fp-math", |
| 1753 | llvm::toStringRef(CodeGenOpts.UnsafeFPMath)); |
| 1754 | FuncAttrs.addAttribute("use-soft-float", |
| 1755 | llvm::toStringRef(CodeGenOpts.SoftFloat)); |
| 1756 | FuncAttrs.addAttribute("stack-protector-buffer-size", |
| 1757 | llvm::utostr(CodeGenOpts.SSPBufferSize)); |
| 1758 | FuncAttrs.addAttribute("no-signed-zeros-fp-math", |
| 1759 | llvm::toStringRef(CodeGenOpts.NoSignedZeros)); |
| 1760 | FuncAttrs.addAttribute( |
| 1761 | "correctly-rounded-divide-sqrt-fp-math", |
| 1762 | llvm::toStringRef(CodeGenOpts.CorrectlyRoundedDivSqrt)); |
| 1763 | |
Alexey Sotkin | 3858e26 | 2018-04-20 08:08:04 +0000 | [diff] [blame] | 1764 | if (getLangOpts().OpenCL) |
| 1765 | FuncAttrs.addAttribute("denorms-are-zero", |
| 1766 | llvm::toStringRef(CodeGenOpts.FlushDenorm)); |
| 1767 | |
Justin Lebar | b080b63 | 2017-01-25 21:29:48 +0000 | [diff] [blame] | 1768 | // TODO: Reciprocal estimate codegen options should apply to instructions? |
Craig Topper | 402b431 | 2017-11-20 17:09:22 +0000 | [diff] [blame] | 1769 | const std::vector<std::string> &Recips = CodeGenOpts.Reciprocals; |
Justin Lebar | b080b63 | 2017-01-25 21:29:48 +0000 | [diff] [blame] | 1770 | if (!Recips.empty()) |
| 1771 | FuncAttrs.addAttribute("reciprocal-estimates", |
Erich Keane | 857ac59 | 2017-10-27 18:45:06 +0000 | [diff] [blame] | 1772 | llvm::join(Recips, ",")); |
Justin Lebar | b080b63 | 2017-01-25 21:29:48 +0000 | [diff] [blame] | 1773 | |
Craig Topper | 9a724aa | 2017-12-11 21:09:19 +0000 | [diff] [blame] | 1774 | if (!CodeGenOpts.PreferVectorWidth.empty() && |
| 1775 | CodeGenOpts.PreferVectorWidth != "none") |
| 1776 | FuncAttrs.addAttribute("prefer-vector-width", |
| 1777 | CodeGenOpts.PreferVectorWidth); |
| 1778 | |
Justin Lebar | b080b63 | 2017-01-25 21:29:48 +0000 | [diff] [blame] | 1779 | if (CodeGenOpts.StackRealignment) |
| 1780 | FuncAttrs.addAttribute("stackrealign"); |
| 1781 | if (CodeGenOpts.Backchain) |
| 1782 | FuncAttrs.addAttribute("backchain"); |
Chandler Carruth | 664aa86 | 2018-09-04 12:38:00 +0000 | [diff] [blame] | 1783 | |
| 1784 | if (CodeGenOpts.SpeculativeLoadHardening) |
| 1785 | FuncAttrs.addAttribute(llvm::Attribute::SpeculativeLoadHardening); |
Justin Lebar | b080b63 | 2017-01-25 21:29:48 +0000 | [diff] [blame] | 1786 | } |
| 1787 | |
Matt Arsenault | a1cf61b | 2017-10-06 19:34:40 +0000 | [diff] [blame] | 1788 | if (getLangOpts().assumeFunctionsAreConvergent()) { |
| 1789 | // Conservatively, mark all functions and calls in CUDA and OpenCL as |
| 1790 | // convergent (meaning, they may call an intrinsically convergent op, such |
| 1791 | // as __syncthreads() / barrier(), and so can't have certain optimizations |
| 1792 | // applied around them). LLVM will remove this attribute where it safely |
| 1793 | // can. |
Justin Lebar | b080b63 | 2017-01-25 21:29:48 +0000 | [diff] [blame] | 1794 | FuncAttrs.addAttribute(llvm::Attribute::Convergent); |
Matt Arsenault | a1cf61b | 2017-10-06 19:34:40 +0000 | [diff] [blame] | 1795 | } |
Justin Lebar | b080b63 | 2017-01-25 21:29:48 +0000 | [diff] [blame] | 1796 | |
Matt Arsenault | a1cf61b | 2017-10-06 19:34:40 +0000 | [diff] [blame] | 1797 | if (getLangOpts().CUDA && getLangOpts().CUDAIsDevice) { |
Justin Lebar | b080b63 | 2017-01-25 21:29:48 +0000 | [diff] [blame] | 1798 | // Exceptions aren't supported in CUDA device code. |
| 1799 | FuncAttrs.addAttribute(llvm::Attribute::NoUnwind); |
| 1800 | |
| 1801 | // Respect -fcuda-flush-denormals-to-zero. |
Yaxun Liu | e1bfbc5 | 2018-07-21 02:02:22 +0000 | [diff] [blame] | 1802 | if (CodeGenOpts.FlushDenorm) |
Justin Lebar | b080b63 | 2017-01-25 21:29:48 +0000 | [diff] [blame] | 1803 | FuncAttrs.addAttribute("nvptx-f32ftz", "true"); |
| 1804 | } |
Peter Collingbourne | 87f477b | 2019-01-04 19:27:04 +0000 | [diff] [blame] | 1805 | |
| 1806 | for (StringRef Attr : CodeGenOpts.DefaultFunctionAttrs) { |
| 1807 | StringRef Var, Value; |
| 1808 | std::tie(Var, Value) = Attr.split('='); |
| 1809 | FuncAttrs.addAttribute(Var, Value); |
| 1810 | } |
Justin Lebar | b080b63 | 2017-01-25 21:29:48 +0000 | [diff] [blame] | 1811 | } |
| 1812 | |
| 1813 | void CodeGenModule::AddDefaultFnAttrs(llvm::Function &F) { |
| 1814 | llvm::AttrBuilder FuncAttrs; |
Evandro Menezes | 85bd397 | 2019-04-04 22:40:06 +0000 | [diff] [blame] | 1815 | ConstructDefaultFnAttrList(F.getName(), F.hasOptNone(), |
Rui Ueyama | 49a3ad2 | 2019-07-16 04:46:31 +0000 | [diff] [blame] | 1816 | /* AttrOnCallSite = */ false, FuncAttrs); |
Reid Kleckner | ee4930b | 2017-05-02 22:07:37 +0000 | [diff] [blame] | 1817 | F.addAttributes(llvm::AttributeList::FunctionIndex, FuncAttrs); |
Justin Lebar | b080b63 | 2017-01-25 21:29:48 +0000 | [diff] [blame] | 1818 | } |
| 1819 | |
Chad Rosier | 7dbc9cf | 2016-01-06 14:35:46 +0000 | [diff] [blame] | 1820 | void CodeGenModule::ConstructAttributeList( |
| 1821 | StringRef Name, const CGFunctionInfo &FI, CGCalleeInfo CalleeInfo, |
Reid Kleckner | cdd2679 | 2017-04-18 23:50:03 +0000 | [diff] [blame] | 1822 | llvm::AttributeList &AttrList, unsigned &CallingConv, bool AttrOnCallSite) { |
Bill Wendling | a514ebc | 2012-10-15 20:36:26 +0000 | [diff] [blame] | 1823 | llvm::AttrBuilder FuncAttrs; |
| 1824 | llvm::AttrBuilder RetAttrs; |
Daniel Dunbar | 76c8eb7 | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1825 | |
Daniel Dunbar | 0ef3479 | 2009-09-12 00:59:20 +0000 | [diff] [blame] | 1826 | CallingConv = FI.getEffectiveCallingConvention(); |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1827 | if (FI.isNoReturn()) |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 1828 | FuncAttrs.addAttribute(llvm::Attribute::NoReturn); |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1829 | |
Samuel Antao | 798f11c | 2015-11-23 22:04:44 +0000 | [diff] [blame] | 1830 | // If we have information about the function prototype, we can learn |
Craig Topper | 13d759f | 2018-04-30 22:02:48 +0000 | [diff] [blame] | 1831 | // attributes from there. |
Samuel Antao | 798f11c | 2015-11-23 22:04:44 +0000 | [diff] [blame] | 1832 | AddAttributesFromFunctionProtoType(getContext(), FuncAttrs, |
| 1833 | CalleeInfo.getCalleeFunctionProtoType()); |
| 1834 | |
Erich Keane | de6480a3 | 2018-11-13 15:48:08 +0000 | [diff] [blame] | 1835 | const Decl *TargetDecl = CalleeInfo.getCalleeDecl().getDecl(); |
Samuel Antao | 798f11c | 2015-11-23 22:04:44 +0000 | [diff] [blame] | 1836 | |
Justin Lebar | b080b63 | 2017-01-25 21:29:48 +0000 | [diff] [blame] | 1837 | bool HasOptnone = false; |
Anton Korobeynikov | c847824 | 2009-04-04 00:49:24 +0000 | [diff] [blame] | 1838 | // FIXME: handle sseregparm someday... |
Daniel Dunbar | 76c8eb7 | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1839 | if (TargetDecl) { |
Rafael Espindola | 2d21ab0 | 2011-10-12 19:51:18 +0000 | [diff] [blame] | 1840 | if (TargetDecl->hasAttr<ReturnsTwiceAttr>()) |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 1841 | FuncAttrs.addAttribute(llvm::Attribute::ReturnsTwice); |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1842 | if (TargetDecl->hasAttr<NoThrowAttr>()) |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 1843 | FuncAttrs.addAttribute(llvm::Attribute::NoUnwind); |
Richard Smith | debc59d | 2013-01-30 05:45:05 +0000 | [diff] [blame] | 1844 | if (TargetDecl->hasAttr<NoReturnAttr>()) |
| 1845 | FuncAttrs.addAttribute(llvm::Attribute::NoReturn); |
Xinliang David Li | 4ec3606 | 2017-06-13 21:14:07 +0000 | [diff] [blame] | 1846 | if (TargetDecl->hasAttr<ColdAttr>()) |
| 1847 | FuncAttrs.addAttribute(llvm::Attribute::Cold); |
Aaron Ballman | 7c19ab1 | 2014-02-22 16:59:24 +0000 | [diff] [blame] | 1848 | if (TargetDecl->hasAttr<NoDuplicateAttr>()) |
| 1849 | FuncAttrs.addAttribute(llvm::Attribute::NoDuplicate); |
Yaxun Liu | 7d07ae7 | 2016-11-01 18:45:32 +0000 | [diff] [blame] | 1850 | if (TargetDecl->hasAttr<ConvergentAttr>()) |
| 1851 | FuncAttrs.addAttribute(llvm::Attribute::Convergent); |
Richard Smith | debc59d | 2013-01-30 05:45:05 +0000 | [diff] [blame] | 1852 | |
Chandler Carruth | 45bbe01 | 2017-03-24 09:11:57 +0000 | [diff] [blame] | 1853 | if (const FunctionDecl *Fn = dyn_cast<FunctionDecl>(TargetDecl)) { |
Samuel Antao | 798f11c | 2015-11-23 22:04:44 +0000 | [diff] [blame] | 1854 | AddAttributesFromFunctionProtoType( |
Chandler Carruth | 45bbe01 | 2017-03-24 09:11:57 +0000 | [diff] [blame] | 1855 | getContext(), FuncAttrs, Fn->getType()->getAs<FunctionProtoType>()); |
Richard Smith | 49af629 | 2013-03-05 08:30:04 +0000 | [diff] [blame] | 1856 | // Don't use [[noreturn]] or _Noreturn for a call to a virtual function. |
| 1857 | // These attributes are not inherited by overloads. |
Chandler Carruth | 45bbe01 | 2017-03-24 09:11:57 +0000 | [diff] [blame] | 1858 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Fn); |
| 1859 | if (Fn->isNoReturn() && !(AttrOnCallSite && MD && MD->isVirtual())) |
Richard Smith | debc59d | 2013-01-30 05:45:05 +0000 | [diff] [blame] | 1860 | FuncAttrs.addAttribute(llvm::Attribute::NoReturn); |
John McCall | be349de | 2010-07-08 06:48:12 +0000 | [diff] [blame] | 1861 | } |
| 1862 | |
David Majnemer | 1bf0f8e | 2015-07-20 22:51:52 +0000 | [diff] [blame] | 1863 | // 'const', 'pure' and 'noalias' attributed functions are also nounwind. |
Eric Christopher | bf005ec | 2011-08-15 22:38:22 +0000 | [diff] [blame] | 1864 | if (TargetDecl->hasAttr<ConstAttr>()) { |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 1865 | FuncAttrs.addAttribute(llvm::Attribute::ReadNone); |
| 1866 | FuncAttrs.addAttribute(llvm::Attribute::NoUnwind); |
Eric Christopher | bf005ec | 2011-08-15 22:38:22 +0000 | [diff] [blame] | 1867 | } else if (TargetDecl->hasAttr<PureAttr>()) { |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 1868 | FuncAttrs.addAttribute(llvm::Attribute::ReadOnly); |
| 1869 | FuncAttrs.addAttribute(llvm::Attribute::NoUnwind); |
David Majnemer | 1bf0f8e | 2015-07-20 22:51:52 +0000 | [diff] [blame] | 1870 | } else if (TargetDecl->hasAttr<NoAliasAttr>()) { |
| 1871 | FuncAttrs.addAttribute(llvm::Attribute::ArgMemOnly); |
| 1872 | FuncAttrs.addAttribute(llvm::Attribute::NoUnwind); |
Eric Christopher | bf005ec | 2011-08-15 22:38:22 +0000 | [diff] [blame] | 1873 | } |
David Majnemer | 631a90b | 2015-02-04 07:23:21 +0000 | [diff] [blame] | 1874 | if (TargetDecl->hasAttr<RestrictAttr>()) |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 1875 | RetAttrs.addAttribute(llvm::Attribute::NoAlias); |
Manoj Gupta | da08f6a | 2018-07-19 00:44:52 +0000 | [diff] [blame] | 1876 | if (TargetDecl->hasAttr<ReturnsNonNullAttr>() && |
| 1877 | !CodeGenOpts.NullPointerIsValid) |
Hal Finkel | d8442b1 | 2014-07-12 04:51:04 +0000 | [diff] [blame] | 1878 | RetAttrs.addAttribute(llvm::Attribute::NonNull); |
Oren Ben Simhon | 318a6ea | 2017-04-27 12:01:00 +0000 | [diff] [blame] | 1879 | if (TargetDecl->hasAttr<AnyX86NoCallerSavedRegistersAttr>()) |
| 1880 | FuncAttrs.addAttribute("no_caller_saved_registers"); |
Oren Ben Simhon | 220671a | 2018-03-17 13:31:35 +0000 | [diff] [blame] | 1881 | if (TargetDecl->hasAttr<AnyX86NoCfCheckAttr>()) |
| 1882 | FuncAttrs.addAttribute(llvm::Attribute::NoCfCheck); |
Paul Robinson | 0855695 | 2014-12-11 20:14:04 +0000 | [diff] [blame] | 1883 | |
| 1884 | HasOptnone = TargetDecl->hasAttr<OptimizeNoneAttr>(); |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 1885 | if (auto *AllocSize = TargetDecl->getAttr<AllocSizeAttr>()) { |
| 1886 | Optional<unsigned> NumElemsParam; |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1887 | if (AllocSize->getNumElemsParam().isValid()) |
| 1888 | NumElemsParam = AllocSize->getNumElemsParam().getLLVMIndex(); |
| 1889 | FuncAttrs.addAllocSizeAttr(AllocSize->getElemSizeParam().getLLVMIndex(), |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 1890 | NumElemsParam); |
| 1891 | } |
Daniel Dunbar | 76c8eb7 | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1892 | } |
| 1893 | |
Justin Lebar | b080b63 | 2017-01-25 21:29:48 +0000 | [diff] [blame] | 1894 | ConstructDefaultFnAttrList(Name, HasOptnone, AttrOnCallSite, FuncAttrs); |
Paul Robinson | 0855695 | 2014-12-11 20:14:04 +0000 | [diff] [blame] | 1895 | |
Zola Bridges | 826ef59 | 2019-01-18 17:20:46 +0000 | [diff] [blame] | 1896 | // This must run after constructing the default function attribute list |
| 1897 | // to ensure that the speculative load hardening attribute is removed |
| 1898 | // in the case where the -mspeculative-load-hardening flag was passed. |
| 1899 | if (TargetDecl) { |
| 1900 | if (TargetDecl->hasAttr<NoSpeculativeLoadHardeningAttr>()) |
| 1901 | FuncAttrs.removeAttribute(llvm::Attribute::SpeculativeLoadHardening); |
| 1902 | if (TargetDecl->hasAttr<SpeculativeLoadHardeningAttr>()) |
| 1903 | FuncAttrs.addAttribute(llvm::Attribute::SpeculativeLoadHardening); |
| 1904 | } |
| 1905 | |
Peter Collingbourne | b4728c1 | 2014-05-19 22:14:34 +0000 | [diff] [blame] | 1906 | if (CodeGenOpts.EnableSegmentedStacks && |
| 1907 | !(TargetDecl && TargetDecl->hasAttr<NoSplitStackAttr>())) |
Reid Kleckner | fb873af | 2014-04-10 22:59:13 +0000 | [diff] [blame] | 1908 | FuncAttrs.addAttribute("split-stack"); |
Devang Patel | 6e467b1 | 2009-06-04 23:32:02 +0000 | [diff] [blame] | 1909 | |
Sriraman Tallam | 5c65148 | 2017-11-07 19:37:51 +0000 | [diff] [blame] | 1910 | // Add NonLazyBind attribute to function declarations when -fno-plt |
| 1911 | // is used. |
| 1912 | if (TargetDecl && CodeGenOpts.NoPLT) { |
| 1913 | if (auto *Fn = dyn_cast<FunctionDecl>(TargetDecl)) { |
| 1914 | if (!Fn->isDefined() && !AttrOnCallSite) { |
| 1915 | FuncAttrs.addAttribute(llvm::Attribute::NonLazyBind); |
| 1916 | } |
| 1917 | } |
| 1918 | } |
| 1919 | |
Alexey Sotkin | 20f6592 | 2018-02-22 11:54:14 +0000 | [diff] [blame] | 1920 | if (TargetDecl && TargetDecl->hasAttr<OpenCLKernelAttr>()) { |
| 1921 | if (getLangOpts().OpenCLVersion <= 120) { |
| 1922 | // OpenCL v1.2 Work groups are always uniform |
| 1923 | FuncAttrs.addAttribute("uniform-work-group-size", "true"); |
| 1924 | } else { |
| 1925 | // OpenCL v2.0 Work groups may be whether uniform or not. |
| 1926 | // '-cl-uniform-work-group-size' compile option gets a hint |
| 1927 | // to the compiler that the global work-size be a multiple of |
| 1928 | // the work-group size specified to clEnqueueNDRangeKernel |
| 1929 | // (i.e. work groups are uniform). |
| 1930 | FuncAttrs.addAttribute("uniform-work-group-size", |
| 1931 | llvm::toStringRef(CodeGenOpts.UniformWGSize)); |
| 1932 | } |
| 1933 | } |
| 1934 | |
Justin Lebar | b080b63 | 2017-01-25 21:29:48 +0000 | [diff] [blame] | 1935 | if (!AttrOnCallSite) { |
Akira Hatanaka | 627586b | 2018-03-02 01:53:15 +0000 | [diff] [blame] | 1936 | bool DisableTailCalls = false; |
| 1937 | |
| 1938 | if (CodeGenOpts.DisableTailCalls) |
| 1939 | DisableTailCalls = true; |
| 1940 | else if (TargetDecl) { |
| 1941 | if (TargetDecl->hasAttr<DisableTailCallsAttr>() || |
| 1942 | TargetDecl->hasAttr<AnyX86InterruptAttr>()) |
| 1943 | DisableTailCalls = true; |
| 1944 | else if (CodeGenOpts.NoEscapingBlockTailCalls) { |
| 1945 | if (const auto *BD = dyn_cast<BlockDecl>(TargetDecl)) |
| 1946 | if (!BD->doesNotEscape()) |
| 1947 | DisableTailCalls = true; |
| 1948 | } |
| 1949 | } |
| 1950 | |
Justin Lebar | b080b63 | 2017-01-25 21:29:48 +0000 | [diff] [blame] | 1951 | FuncAttrs.addAttribute("disable-tail-calls", |
| 1952 | llvm::toStringRef(DisableTailCalls)); |
Erich Keane | de6480a3 | 2018-11-13 15:48:08 +0000 | [diff] [blame] | 1953 | GetCPUAndFeaturesAttributes(CalleeInfo.getCalleeDecl(), FuncAttrs); |
Bill Wendling | 985d1c5 | 2013-02-15 21:30:01 +0000 | [diff] [blame] | 1954 | } |
| 1955 | |
Alexey Samsonov | 153004f | 2014-09-29 22:08:00 +0000 | [diff] [blame] | 1956 | ClangToLLVMArgMapping IRFunctionArgs(getContext(), FI); |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 1957 | |
Daniel Dunbar | 3668cb2 | 2009-02-02 23:43:58 +0000 | [diff] [blame] | 1958 | QualType RetTy = FI.getReturnType(); |
Daniel Dunbar | b52d077 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 1959 | const ABIArgInfo &RetAI = FI.getReturnInfo(); |
Daniel Dunbar | 7a95ca3 | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1960 | switch (RetAI.getKind()) { |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 1961 | case ABIArgInfo::Extend: |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 1962 | if (RetAI.isSignExt()) |
Jakob Stoklund Olesen | d7bf293 | 2013-05-29 03:57:23 +0000 | [diff] [blame] | 1963 | RetAttrs.addAttribute(llvm::Attribute::SExt); |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 1964 | else |
Jakob Stoklund Olesen | d7bf293 | 2013-05-29 03:57:23 +0000 | [diff] [blame] | 1965 | RetAttrs.addAttribute(llvm::Attribute::ZExt); |
Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 1966 | LLVM_FALLTHROUGH; |
Daniel Dunbar | 67dace89 | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 1967 | case ABIArgInfo::Direct: |
Jakob Stoklund Olesen | a366114 | 2013-06-05 03:00:09 +0000 | [diff] [blame] | 1968 | if (RetAI.getInReg()) |
| 1969 | RetAttrs.addAttribute(llvm::Attribute::InReg); |
| 1970 | break; |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1971 | case ABIArgInfo::Ignore: |
Daniel Dunbar | a72d4ae | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1972 | break; |
| 1973 | |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 1974 | case ABIArgInfo::InAlloca: |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1975 | case ABIArgInfo::Indirect: { |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 1976 | // inalloca and sret disable readnone and readonly |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 1977 | FuncAttrs.removeAttribute(llvm::Attribute::ReadOnly) |
| 1978 | .removeAttribute(llvm::Attribute::ReadNone); |
Daniel Dunbar | a72d4ae | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1979 | break; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1980 | } |
Daniel Dunbar | a72d4ae | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1981 | |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 1982 | case ABIArgInfo::CoerceAndExpand: |
| 1983 | break; |
| 1984 | |
Daniel Dunbar | d3674e6 | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1985 | case ABIArgInfo::Expand: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1986 | llvm_unreachable("Invalid ABI kind for return argument"); |
Daniel Dunbar | 76c8eb7 | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1987 | } |
Daniel Dunbar | a72d4ae | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1988 | |
Hal Finkel | a2347ba | 2014-07-18 15:52:10 +0000 | [diff] [blame] | 1989 | if (const auto *RefTy = RetTy->getAs<ReferenceType>()) { |
| 1990 | QualType PTy = RefTy->getPointeeType(); |
David Majnemer | 9df5637 | 2015-09-10 21:52:00 +0000 | [diff] [blame] | 1991 | if (!PTy->isIncompleteType() && PTy->isConstantSizeType()) |
Hal Finkel | a2347ba | 2014-07-18 15:52:10 +0000 | [diff] [blame] | 1992 | RetAttrs.addDereferenceableAttr(getContext().getTypeSizeInChars(PTy) |
| 1993 | .getQuantity()); |
Manoj Gupta | da08f6a | 2018-07-19 00:44:52 +0000 | [diff] [blame] | 1994 | else if (getContext().getTargetAddressSpace(PTy) == 0 && |
| 1995 | !CodeGenOpts.NullPointerIsValid) |
Hal Finkel | a2347ba | 2014-07-18 15:52:10 +0000 | [diff] [blame] | 1996 | RetAttrs.addAttribute(llvm::Attribute::NonNull); |
| 1997 | } |
Nick Lewycky | 9b46eb8 | 2014-05-28 09:56:42 +0000 | [diff] [blame] | 1998 | |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 1999 | bool hasUsedSRet = false; |
Reid Kleckner | cdd2679 | 2017-04-18 23:50:03 +0000 | [diff] [blame] | 2000 | SmallVector<llvm::AttributeSet, 4> ArgAttrs(IRFunctionArgs.totalIRArgs()); |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 2001 | |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 2002 | // Attach attributes to sret. |
| 2003 | if (IRFunctionArgs.hasSRetArg()) { |
| 2004 | llvm::AttrBuilder SRETAttrs; |
Mandeep Singh Grang | 85a0f8f | 2019-05-03 21:12:24 +0000 | [diff] [blame] | 2005 | SRETAttrs.addAttribute(llvm::Attribute::StructRet); |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 2006 | hasUsedSRet = true; |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 2007 | if (RetAI.getInReg()) |
| 2008 | SRETAttrs.addAttribute(llvm::Attribute::InReg); |
Reid Kleckner | cdd2679 | 2017-04-18 23:50:03 +0000 | [diff] [blame] | 2009 | ArgAttrs[IRFunctionArgs.getSRetArgNo()] = |
| 2010 | llvm::AttributeSet::get(getLLVMContext(), SRETAttrs); |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 2011 | } |
| 2012 | |
| 2013 | // Attach attributes to inalloca argument. |
| 2014 | if (IRFunctionArgs.hasInallocaArg()) { |
| 2015 | llvm::AttrBuilder Attrs; |
| 2016 | Attrs.addAttribute(llvm::Attribute::InAlloca); |
Reid Kleckner | cdd2679 | 2017-04-18 23:50:03 +0000 | [diff] [blame] | 2017 | ArgAttrs[IRFunctionArgs.getInallocaArgNo()] = |
| 2018 | llvm::AttributeSet::get(getLLVMContext(), Attrs); |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 2019 | } |
| 2020 | |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 2021 | unsigned ArgNo = 0; |
| 2022 | for (CGFunctionInfo::const_arg_iterator I = FI.arg_begin(), |
| 2023 | E = FI.arg_end(); |
| 2024 | I != E; ++I, ++ArgNo) { |
| 2025 | QualType ParamType = I->type; |
| 2026 | const ABIArgInfo &AI = I->info; |
Bill Wendling | a514ebc | 2012-10-15 20:36:26 +0000 | [diff] [blame] | 2027 | llvm::AttrBuilder Attrs; |
Anton Korobeynikov | c847824 | 2009-04-04 00:49:24 +0000 | [diff] [blame] | 2028 | |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 2029 | // Add attribute for padding argument, if necessary. |
| 2030 | if (IRFunctionArgs.hasPaddingArg(ArgNo)) { |
Reid Kleckner | cdd2679 | 2017-04-18 23:50:03 +0000 | [diff] [blame] | 2031 | if (AI.getPaddingInReg()) { |
| 2032 | ArgAttrs[IRFunctionArgs.getPaddingArgNo(ArgNo)] = |
| 2033 | llvm::AttributeSet::get( |
| 2034 | getLLVMContext(), |
| 2035 | llvm::AttrBuilder().addAttribute(llvm::Attribute::InReg)); |
| 2036 | } |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 2037 | } |
| 2038 | |
John McCall | 39ec71f | 2010-03-27 00:47:27 +0000 | [diff] [blame] | 2039 | // 'restrict' -> 'noalias' is done in EmitFunctionProlog when we |
| 2040 | // have the corresponding parameter variable. It doesn't make |
Daniel Dunbar | cb2b3d0 | 2011-02-10 18:10:07 +0000 | [diff] [blame] | 2041 | // sense to do it here because parameters are so messed up. |
Daniel Dunbar | d3674e6 | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 2042 | switch (AI.getKind()) { |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2043 | case ABIArgInfo::Extend: |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 2044 | if (AI.isSignExt()) |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 2045 | Attrs.addAttribute(llvm::Attribute::SExt); |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 2046 | else |
| 2047 | Attrs.addAttribute(llvm::Attribute::ZExt); |
Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 2048 | LLVM_FALLTHROUGH; |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 2049 | case ABIArgInfo::Direct: |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 2050 | if (ArgNo == 0 && FI.isChainCall()) |
| 2051 | Attrs.addAttribute(llvm::Attribute::Nest); |
| 2052 | else if (AI.getInReg()) |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 2053 | Attrs.addAttribute(llvm::Attribute::InReg); |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2054 | break; |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 2055 | |
James Y Knight | 7160857 | 2015-08-21 18:19:06 +0000 | [diff] [blame] | 2056 | case ABIArgInfo::Indirect: { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 2057 | if (AI.getInReg()) |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 2058 | Attrs.addAttribute(llvm::Attribute::InReg); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 2059 | |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2060 | if (AI.getIndirectByVal()) |
Tim Northover | c46827c | 2019-06-05 21:12:14 +0000 | [diff] [blame] | 2061 | Attrs.addByValAttr(getTypes().ConvertTypeForMem(ParamType)); |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2062 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2063 | CharUnits Align = AI.getIndirectAlign(); |
James Y Knight | 7160857 | 2015-08-21 18:19:06 +0000 | [diff] [blame] | 2064 | |
| 2065 | // In a byval argument, it is important that the required |
| 2066 | // alignment of the type is honored, as LLVM might be creating a |
| 2067 | // *new* stack object, and needs to know what alignment to give |
| 2068 | // it. (Sometimes it can deduce a sensible alignment on its own, |
| 2069 | // but not if clang decides it must emit a packed struct, or the |
| 2070 | // user specifies increased alignment requirements.) |
| 2071 | // |
| 2072 | // This is different from indirect *not* byval, where the object |
| 2073 | // exists already, and the align attribute is purely |
| 2074 | // informative. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2075 | assert(!Align.isZero()); |
James Y Knight | 7160857 | 2015-08-21 18:19:06 +0000 | [diff] [blame] | 2076 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2077 | // For now, only add this when we have a byval argument. |
| 2078 | // TODO: be less lazy about updating test cases. |
| 2079 | if (AI.getIndirectByVal()) |
| 2080 | Attrs.addAlignmentAttr(Align.getQuantity()); |
Bill Wendling | a7912f8 | 2012-10-10 07:36:56 +0000 | [diff] [blame] | 2081 | |
Daniel Dunbar | c230443 | 2009-03-18 19:51:01 +0000 | [diff] [blame] | 2082 | // byval disables readnone and readonly. |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 2083 | FuncAttrs.removeAttribute(llvm::Attribute::ReadOnly) |
| 2084 | .removeAttribute(llvm::Attribute::ReadNone); |
Daniel Dunbar | d3674e6 | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 2085 | break; |
James Y Knight | 7160857 | 2015-08-21 18:19:06 +0000 | [diff] [blame] | 2086 | } |
Daniel Dunbar | 94a6f25 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 2087 | case ABIArgInfo::Ignore: |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 2088 | case ABIArgInfo::Expand: |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 2089 | case ABIArgInfo::CoerceAndExpand: |
| 2090 | break; |
Daniel Dunbar | 94a6f25 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 2091 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 2092 | case ABIArgInfo::InAlloca: |
| 2093 | // inalloca disables readnone and readonly. |
| 2094 | FuncAttrs.removeAttribute(llvm::Attribute::ReadOnly) |
| 2095 | .removeAttribute(llvm::Attribute::ReadNone); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 2096 | continue; |
Daniel Dunbar | 76c8eb7 | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 2097 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2098 | |
Hal Finkel | a2347ba | 2014-07-18 15:52:10 +0000 | [diff] [blame] | 2099 | if (const auto *RefTy = ParamType->getAs<ReferenceType>()) { |
| 2100 | QualType PTy = RefTy->getPointeeType(); |
David Majnemer | 9df5637 | 2015-09-10 21:52:00 +0000 | [diff] [blame] | 2101 | if (!PTy->isIncompleteType() && PTy->isConstantSizeType()) |
Hal Finkel | a2347ba | 2014-07-18 15:52:10 +0000 | [diff] [blame] | 2102 | Attrs.addDereferenceableAttr(getContext().getTypeSizeInChars(PTy) |
| 2103 | .getQuantity()); |
Manoj Gupta | da08f6a | 2018-07-19 00:44:52 +0000 | [diff] [blame] | 2104 | else if (getContext().getTargetAddressSpace(PTy) == 0 && |
| 2105 | !CodeGenOpts.NullPointerIsValid) |
Hal Finkel | a2347ba | 2014-07-18 15:52:10 +0000 | [diff] [blame] | 2106 | Attrs.addAttribute(llvm::Attribute::NonNull); |
| 2107 | } |
Nick Lewycky | 9b46eb8 | 2014-05-28 09:56:42 +0000 | [diff] [blame] | 2108 | |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 2109 | switch (FI.getExtParameterInfo(ArgNo).getABI()) { |
| 2110 | case ParameterABI::Ordinary: |
| 2111 | break; |
| 2112 | |
| 2113 | case ParameterABI::SwiftIndirectResult: { |
| 2114 | // Add 'sret' if we haven't already used it for something, but |
| 2115 | // only if the result is void. |
| 2116 | if (!hasUsedSRet && RetTy->isVoidType()) { |
| 2117 | Attrs.addAttribute(llvm::Attribute::StructRet); |
| 2118 | hasUsedSRet = true; |
| 2119 | } |
| 2120 | |
| 2121 | // Add 'noalias' in either case. |
| 2122 | Attrs.addAttribute(llvm::Attribute::NoAlias); |
| 2123 | |
| 2124 | // Add 'dereferenceable' and 'alignment'. |
| 2125 | auto PTy = ParamType->getPointeeType(); |
| 2126 | if (!PTy->isIncompleteType() && PTy->isConstantSizeType()) { |
| 2127 | auto info = getContext().getTypeInfoInChars(PTy); |
| 2128 | Attrs.addDereferenceableAttr(info.first.getQuantity()); |
| 2129 | Attrs.addAttribute(llvm::Attribute::getWithAlignment(getLLVMContext(), |
| 2130 | info.second.getQuantity())); |
| 2131 | } |
| 2132 | break; |
| 2133 | } |
| 2134 | |
| 2135 | case ParameterABI::SwiftErrorResult: |
| 2136 | Attrs.addAttribute(llvm::Attribute::SwiftError); |
| 2137 | break; |
| 2138 | |
| 2139 | case ParameterABI::SwiftContext: |
| 2140 | Attrs.addAttribute(llvm::Attribute::SwiftSelf); |
| 2141 | break; |
| 2142 | } |
| 2143 | |
Akira Hatanaka | 98a4933 | 2017-09-22 00:41:05 +0000 | [diff] [blame] | 2144 | if (FI.getExtParameterInfo(ArgNo).isNoEscape()) |
| 2145 | Attrs.addAttribute(llvm::Attribute::NoCapture); |
| 2146 | |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 2147 | if (Attrs.hasAttributes()) { |
| 2148 | unsigned FirstIRArg, NumIRArgs; |
| 2149 | std::tie(FirstIRArg, NumIRArgs) = IRFunctionArgs.getIRArgs(ArgNo); |
| 2150 | for (unsigned i = 0; i < NumIRArgs; i++) |
Reid Kleckner | cdd2679 | 2017-04-18 23:50:03 +0000 | [diff] [blame] | 2151 | ArgAttrs[FirstIRArg + i] = |
| 2152 | llvm::AttributeSet::get(getLLVMContext(), Attrs); |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 2153 | } |
Daniel Dunbar | 76c8eb7 | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 2154 | } |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 2155 | assert(ArgNo == FI.arg_size()); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 2156 | |
Reid Kleckner | cdd2679 | 2017-04-18 23:50:03 +0000 | [diff] [blame] | 2157 | AttrList = llvm::AttributeList::get( |
| 2158 | getLLVMContext(), llvm::AttributeSet::get(getLLVMContext(), FuncAttrs), |
| 2159 | llvm::AttributeSet::get(getLLVMContext(), RetAttrs), ArgAttrs); |
Daniel Dunbar | 76c8eb7 | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 2160 | } |
| 2161 | |
John McCall | a738c25 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 2162 | /// An argument came in as a promoted argument; demote it back to its |
| 2163 | /// declared type. |
| 2164 | static llvm::Value *emitArgumentDemotion(CodeGenFunction &CGF, |
| 2165 | const VarDecl *var, |
| 2166 | llvm::Value *value) { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2167 | llvm::Type *varType = CGF.ConvertType(var->getType()); |
John McCall | a738c25 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 2168 | |
| 2169 | // This can happen with promotions that actually don't change the |
| 2170 | // underlying type, like the enum promotions. |
| 2171 | if (value->getType() == varType) return value; |
| 2172 | |
| 2173 | assert((varType->isIntegerTy() || varType->isFloatingPointTy()) |
| 2174 | && "unexpected promotion type"); |
| 2175 | |
| 2176 | if (isa<llvm::IntegerType>(varType)) |
| 2177 | return CGF.Builder.CreateTrunc(value, varType, "arg.unpromote"); |
| 2178 | |
| 2179 | return CGF.Builder.CreateFPCast(value, varType, "arg.unpromote"); |
| 2180 | } |
| 2181 | |
Chandler Carruth | 45bbe01 | 2017-03-24 09:11:57 +0000 | [diff] [blame] | 2182 | /// Returns the attribute (either parameter attribute, or function |
| 2183 | /// attribute), which declares argument ArgNo to be non-null. |
| 2184 | static const NonNullAttr *getNonNullAttr(const Decl *FD, const ParmVarDecl *PVD, |
| 2185 | QualType ArgType, unsigned ArgNo) { |
| 2186 | // FIXME: __attribute__((nonnull)) can also be applied to: |
| 2187 | // - references to pointers, where the pointee is known to be |
| 2188 | // nonnull (apparently a Clang extension) |
| 2189 | // - transparent unions containing pointers |
| 2190 | // In the former case, LLVM IR cannot represent the constraint. In |
| 2191 | // the latter case, we have no guarantee that the transparent union |
| 2192 | // is in fact passed as a pointer. |
| 2193 | if (!ArgType->isAnyPointerType() && !ArgType->isBlockPointerType()) |
| 2194 | return nullptr; |
| 2195 | // First, check attribute on parameter itself. |
| 2196 | if (PVD) { |
| 2197 | if (auto ParmNNAttr = PVD->getAttr<NonNullAttr>()) |
| 2198 | return ParmNNAttr; |
| 2199 | } |
| 2200 | // Check function attributes. |
| 2201 | if (!FD) |
| 2202 | return nullptr; |
| 2203 | for (const auto *NNAttr : FD->specific_attrs<NonNullAttr>()) { |
| 2204 | if (NNAttr->isNonNull(ArgNo)) |
| 2205 | return NNAttr; |
| 2206 | } |
| 2207 | return nullptr; |
| 2208 | } |
| 2209 | |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 2210 | namespace { |
| 2211 | struct CopyBackSwiftError final : EHScopeStack::Cleanup { |
| 2212 | Address Temp; |
| 2213 | Address Arg; |
| 2214 | CopyBackSwiftError(Address temp, Address arg) : Temp(temp), Arg(arg) {} |
| 2215 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
| 2216 | llvm::Value *errorValue = CGF.Builder.CreateLoad(Temp); |
| 2217 | CGF.Builder.CreateStore(errorValue, Arg); |
| 2218 | } |
| 2219 | }; |
| 2220 | } |
| 2221 | |
Daniel Dunbar | d931a87 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 2222 | void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI, |
| 2223 | llvm::Function *Fn, |
Daniel Dunbar | 613855c | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 2224 | const FunctionArgList &Args) { |
Hans Wennborg | d71907d | 2014-09-04 22:16:33 +0000 | [diff] [blame] | 2225 | if (CurCodeDecl && CurCodeDecl->hasAttr<NakedAttr>()) |
| 2226 | // Naked functions don't have prologues. |
| 2227 | return; |
| 2228 | |
John McCall | caa1945 | 2009-07-28 01:00:58 +0000 | [diff] [blame] | 2229 | // If this is an implicit-return-zero function, go ahead and |
| 2230 | // initialize the return value. TODO: it might be nice to have |
| 2231 | // a more general mechanism for this that didn't require synthesized |
| 2232 | // return statements. |
John McCall | dec348f7 | 2013-05-03 07:33:41 +0000 | [diff] [blame] | 2233 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurCodeDecl)) { |
John McCall | caa1945 | 2009-07-28 01:00:58 +0000 | [diff] [blame] | 2234 | if (FD->hasImplicitReturnZero()) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2235 | QualType RetTy = FD->getReturnType().getUnqualifiedType(); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2236 | llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy); |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2237 | llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy); |
John McCall | caa1945 | 2009-07-28 01:00:58 +0000 | [diff] [blame] | 2238 | Builder.CreateStore(Zero, ReturnValue); |
| 2239 | } |
| 2240 | } |
| 2241 | |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 2242 | // FIXME: We no longer need the types from FunctionArgList; lift up and |
| 2243 | // simplify. |
Daniel Dunbar | 5a0acdc9 | 2009-02-03 06:02:10 +0000 | [diff] [blame] | 2244 | |
Alexey Samsonov | 153004f | 2014-09-29 22:08:00 +0000 | [diff] [blame] | 2245 | ClangToLLVMArgMapping IRFunctionArgs(CGM.getContext(), FI); |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 2246 | // Flattened function arguments. |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 2247 | SmallVector<llvm::Value *, 16> FnArgs; |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 2248 | FnArgs.reserve(IRFunctionArgs.totalIRArgs()); |
| 2249 | for (auto &Arg : Fn->args()) { |
| 2250 | FnArgs.push_back(&Arg); |
| 2251 | } |
| 2252 | assert(FnArgs.size() == IRFunctionArgs.totalIRArgs()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2253 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 2254 | // If we're using inalloca, all the memory arguments are GEPs off of the last |
| 2255 | // parameter, which is a pointer to the complete memory area. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2256 | Address ArgStruct = Address::invalid(); |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 2257 | if (IRFunctionArgs.hasInallocaArg()) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2258 | ArgStruct = Address(FnArgs[IRFunctionArgs.getInallocaArgNo()], |
| 2259 | FI.getArgStructAlignment()); |
| 2260 | |
| 2261 | assert(ArgStruct.getType() == FI.getArgStruct()->getPointerTo()); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 2262 | } |
| 2263 | |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 2264 | // Name the struct return parameter. |
| 2265 | if (IRFunctionArgs.hasSRetArg()) { |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 2266 | auto AI = cast<llvm::Argument>(FnArgs[IRFunctionArgs.getSRetArgNo()]); |
Daniel Dunbar | 613855c | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 2267 | AI->setName("agg.result"); |
Reid Kleckner | cdd2679 | 2017-04-18 23:50:03 +0000 | [diff] [blame] | 2268 | AI->addAttr(llvm::Attribute::NoAlias); |
Daniel Dunbar | 613855c | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 2269 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2270 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 2271 | // Track if we received the parameter as a pointer (indirect, byval, or |
| 2272 | // inalloca). If already have a pointer, EmitParmDecl doesn't need to copy it |
| 2273 | // into a local alloca for us. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2274 | SmallVector<ParamValue, 16> ArgVals; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 2275 | ArgVals.reserve(Args.size()); |
| 2276 | |
Reid Kleckner | 739756c | 2013-12-04 19:23:12 +0000 | [diff] [blame] | 2277 | // Create a pointer value for every parameter declaration. This usually |
| 2278 | // entails copying one or more LLVM IR arguments into an alloca. Don't push |
| 2279 | // any cleanups or do anything that might unwind. We do that separately, so |
| 2280 | // we can push the cleanups in the correct order for the ABI. |
Daniel Dunbar | a45bdbb | 2009-02-04 21:17:21 +0000 | [diff] [blame] | 2281 | assert(FI.arg_size() == Args.size() && |
| 2282 | "Mismatch between function signature & arguments."); |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 2283 | unsigned ArgNo = 0; |
Daniel Dunbar | b52d077 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 2284 | CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin(); |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 2285 | for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end(); |
Devang Patel | 68a1525 | 2011-03-03 20:13:15 +0000 | [diff] [blame] | 2286 | i != e; ++i, ++info_it, ++ArgNo) { |
John McCall | a738c25 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 2287 | const VarDecl *Arg = *i; |
Daniel Dunbar | b52d077 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 2288 | const ABIArgInfo &ArgI = info_it->info; |
Daniel Dunbar | d3674e6 | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 2289 | |
John McCall | a738c25 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 2290 | bool isPromoted = |
| 2291 | isa<ParmVarDecl>(Arg) && cast<ParmVarDecl>(Arg)->isKNRPromoted(); |
Volodymyr Sapsai | 17ebdb2 | 2018-01-22 22:29:24 +0000 | [diff] [blame] | 2292 | // We are converting from ABIArgInfo type to VarDecl type directly, unless |
| 2293 | // the parameter is promoted. In this case we convert to |
| 2294 | // CGFunctionInfo::ArgInfo type with subsequent argument demotion. |
| 2295 | QualType Ty = isPromoted ? info_it->type : Arg->getType(); |
| 2296 | assert(hasScalarEvaluationKind(Ty) == |
| 2297 | hasScalarEvaluationKind(Arg->getType())); |
John McCall | a738c25 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 2298 | |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 2299 | unsigned FirstIRArg, NumIRArgs; |
| 2300 | std::tie(FirstIRArg, NumIRArgs) = IRFunctionArgs.getIRArgs(ArgNo); |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 2301 | |
Daniel Dunbar | d3674e6 | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 2302 | switch (ArgI.getKind()) { |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 2303 | case ABIArgInfo::InAlloca: { |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 2304 | assert(NumIRArgs == 0); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2305 | auto FieldIndex = ArgI.getInAllocaFieldIndex(); |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 2306 | Address V = |
| 2307 | Builder.CreateStructGEP(ArgStruct, FieldIndex, Arg->getName()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2308 | ArgVals.push_back(ParamValue::forIndirect(V)); |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 2309 | break; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 2310 | } |
| 2311 | |
Daniel Dunbar | 747865a | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 2312 | case ABIArgInfo::Indirect: { |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 2313 | assert(NumIRArgs == 1); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2314 | Address ParamAddr = Address(FnArgs[FirstIRArg], ArgI.getIndirectAlign()); |
Daniel Dunbar | 7b7c293 | 2010-09-16 20:42:02 +0000 | [diff] [blame] | 2315 | |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 2316 | if (!hasScalarEvaluationKind(Ty)) { |
Daniel Dunbar | 7b7c293 | 2010-09-16 20:42:02 +0000 | [diff] [blame] | 2317 | // Aggregates and complex variables are accessed by reference. All we |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2318 | // need to do is realign the value, if requested. |
| 2319 | Address V = ParamAddr; |
Daniel Dunbar | 7b7c293 | 2010-09-16 20:42:02 +0000 | [diff] [blame] | 2320 | if (ArgI.getIndirectRealign()) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2321 | Address AlignedTemp = CreateMemTemp(Ty, "coerce"); |
Daniel Dunbar | 7b7c293 | 2010-09-16 20:42:02 +0000 | [diff] [blame] | 2322 | |
| 2323 | // Copy from the incoming argument pointer to the temporary with the |
| 2324 | // appropriate alignment. |
| 2325 | // |
| 2326 | // FIXME: We should have a common utility for generating an aggregate |
| 2327 | // copy. |
Ken Dyck | 705ba07 | 2011-01-19 01:58:38 +0000 | [diff] [blame] | 2328 | CharUnits Size = getContext().getTypeSizeInChars(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2329 | auto SizeVal = llvm::ConstantInt::get(IntPtrTy, Size.getQuantity()); |
| 2330 | Address Dst = Builder.CreateBitCast(AlignedTemp, Int8PtrTy); |
| 2331 | Address Src = Builder.CreateBitCast(ParamAddr, Int8PtrTy); |
| 2332 | Builder.CreateMemCpy(Dst, Src, SizeVal, false); |
Daniel Dunbar | 7b7c293 | 2010-09-16 20:42:02 +0000 | [diff] [blame] | 2333 | V = AlignedTemp; |
| 2334 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2335 | ArgVals.push_back(ParamValue::forIndirect(V)); |
Daniel Dunbar | 747865a | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 2336 | } else { |
| 2337 | // Load scalar value from indirect argument. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2338 | llvm::Value *V = |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2339 | EmitLoadOfScalar(ParamAddr, false, Ty, Arg->getBeginLoc()); |
John McCall | a738c25 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 2340 | |
| 2341 | if (isPromoted) |
| 2342 | V = emitArgumentDemotion(*this, Arg, V); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2343 | ArgVals.push_back(ParamValue::forDirect(V)); |
Daniel Dunbar | 747865a | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 2344 | } |
Daniel Dunbar | 747865a | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 2345 | break; |
| 2346 | } |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 2347 | |
| 2348 | case ABIArgInfo::Extend: |
Daniel Dunbar | 67dace89 | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 2349 | case ABIArgInfo::Direct: { |
Akira Hatanaka | 18334dd | 2012-01-09 19:08:06 +0000 | [diff] [blame] | 2350 | |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2351 | // If we have the trivial case, handle it with no muss and fuss. |
| 2352 | if (!isa<llvm::StructType>(ArgI.getCoerceToType()) && |
Volodymyr Sapsai | 22b00ec | 2017-12-21 20:52:59 +0000 | [diff] [blame] | 2353 | ArgI.getCoerceToType() == ConvertType(Ty) && |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2354 | ArgI.getDirectOffset() == 0) { |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 2355 | assert(NumIRArgs == 1); |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 2356 | llvm::Value *V = FnArgs[FirstIRArg]; |
| 2357 | auto AI = cast<llvm::Argument>(V); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2358 | |
Hal Finkel | 48d53e2 | 2014-07-19 01:41:07 +0000 | [diff] [blame] | 2359 | if (const ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(Arg)) { |
Alexey Samsonov | 8e1162c | 2014-09-08 17:22:45 +0000 | [diff] [blame] | 2360 | if (getNonNullAttr(CurCodeDecl, PVD, PVD->getType(), |
Manoj Gupta | da08f6a | 2018-07-19 00:44:52 +0000 | [diff] [blame] | 2361 | PVD->getFunctionScopeIndex()) && |
| 2362 | !CGM.getCodeGenOpts().NullPointerIsValid) |
Reid Kleckner | cdd2679 | 2017-04-18 23:50:03 +0000 | [diff] [blame] | 2363 | AI->addAttr(llvm::Attribute::NonNull); |
Hal Finkel | 82504f0 | 2014-07-11 17:35:21 +0000 | [diff] [blame] | 2364 | |
Hal Finkel | 48d53e2 | 2014-07-19 01:41:07 +0000 | [diff] [blame] | 2365 | QualType OTy = PVD->getOriginalType(); |
| 2366 | if (const auto *ArrTy = |
| 2367 | getContext().getAsConstantArrayType(OTy)) { |
| 2368 | // A C99 array parameter declaration with the static keyword also |
| 2369 | // indicates dereferenceability, and if the size is constant we can |
| 2370 | // use the dereferenceable attribute (which requires the size in |
| 2371 | // bytes). |
Hal Finkel | 16e394a | 2014-07-19 02:13:40 +0000 | [diff] [blame] | 2372 | if (ArrTy->getSizeModifier() == ArrayType::Static) { |
Hal Finkel | 48d53e2 | 2014-07-19 01:41:07 +0000 | [diff] [blame] | 2373 | QualType ETy = ArrTy->getElementType(); |
| 2374 | uint64_t ArrSize = ArrTy->getSize().getZExtValue(); |
| 2375 | if (!ETy->isIncompleteType() && ETy->isConstantSizeType() && |
| 2376 | ArrSize) { |
| 2377 | llvm::AttrBuilder Attrs; |
| 2378 | Attrs.addDereferenceableAttr( |
| 2379 | getContext().getTypeSizeInChars(ETy).getQuantity()*ArrSize); |
Reid Kleckner | 9d16fa0 | 2017-04-19 17:28:52 +0000 | [diff] [blame] | 2380 | AI->addAttrs(Attrs); |
Manoj Gupta | da08f6a | 2018-07-19 00:44:52 +0000 | [diff] [blame] | 2381 | } else if (getContext().getTargetAddressSpace(ETy) == 0 && |
| 2382 | !CGM.getCodeGenOpts().NullPointerIsValid) { |
Reid Kleckner | cdd2679 | 2017-04-18 23:50:03 +0000 | [diff] [blame] | 2383 | AI->addAttr(llvm::Attribute::NonNull); |
Hal Finkel | 48d53e2 | 2014-07-19 01:41:07 +0000 | [diff] [blame] | 2384 | } |
| 2385 | } |
| 2386 | } else if (const auto *ArrTy = |
| 2387 | getContext().getAsVariableArrayType(OTy)) { |
| 2388 | // For C99 VLAs with the static keyword, we don't know the size so |
| 2389 | // we can't use the dereferenceable attribute, but in addrspace(0) |
| 2390 | // we know that it must be nonnull. |
| 2391 | if (ArrTy->getSizeModifier() == VariableArrayType::Static && |
Manoj Gupta | da08f6a | 2018-07-19 00:44:52 +0000 | [diff] [blame] | 2392 | !getContext().getTargetAddressSpace(ArrTy->getElementType()) && |
| 2393 | !CGM.getCodeGenOpts().NullPointerIsValid) |
Reid Kleckner | cdd2679 | 2017-04-18 23:50:03 +0000 | [diff] [blame] | 2394 | AI->addAttr(llvm::Attribute::NonNull); |
Hal Finkel | 48d53e2 | 2014-07-19 01:41:07 +0000 | [diff] [blame] | 2395 | } |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 2396 | |
| 2397 | const auto *AVAttr = PVD->getAttr<AlignValueAttr>(); |
| 2398 | if (!AVAttr) |
| 2399 | if (const auto *TOTy = dyn_cast<TypedefType>(OTy)) |
| 2400 | AVAttr = TOTy->getDecl()->getAttr<AlignValueAttr>(); |
Roman Lebedev | bd1c087 | 2019-01-15 09:44:25 +0000 | [diff] [blame] | 2401 | if (AVAttr && !SanOpts.has(SanitizerKind::Alignment)) { |
| 2402 | // If alignment-assumption sanitizer is enabled, we do *not* add |
| 2403 | // alignment attribute here, but emit normal alignment assumption, |
| 2404 | // so the UBSAN check could function. |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 2405 | llvm::Value *AlignmentValue = |
| 2406 | EmitScalarExpr(AVAttr->getAlignment()); |
| 2407 | llvm::ConstantInt *AlignmentCI = |
| 2408 | cast<llvm::ConstantInt>(AlignmentValue); |
Reid Kleckner | 9d16fa0 | 2017-04-19 17:28:52 +0000 | [diff] [blame] | 2409 | unsigned Alignment = std::min((unsigned)AlignmentCI->getZExtValue(), |
| 2410 | +llvm::Value::MaximumAlignment); |
| 2411 | AI->addAttrs(llvm::AttrBuilder().addAlignmentAttr(Alignment)); |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 2412 | } |
Hal Finkel | 48d53e2 | 2014-07-19 01:41:07 +0000 | [diff] [blame] | 2413 | } |
| 2414 | |
Bill Wendling | 507c351 | 2012-10-16 05:23:44 +0000 | [diff] [blame] | 2415 | if (Arg->getType().isRestrictQualified()) |
Reid Kleckner | cdd2679 | 2017-04-18 23:50:03 +0000 | [diff] [blame] | 2416 | AI->addAttr(llvm::Attribute::NoAlias); |
John McCall | 39ec71f | 2010-03-27 00:47:27 +0000 | [diff] [blame] | 2417 | |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 2418 | // LLVM expects swifterror parameters to be used in very restricted |
| 2419 | // ways. Copy the value into a less-restricted temporary. |
| 2420 | if (FI.getExtParameterInfo(ArgNo).getABI() |
| 2421 | == ParameterABI::SwiftErrorResult) { |
| 2422 | QualType pointeeTy = Ty->getPointeeType(); |
| 2423 | assert(pointeeTy->isPointerType()); |
| 2424 | Address temp = |
| 2425 | CreateMemTemp(pointeeTy, getPointerAlign(), "swifterror.temp"); |
| 2426 | Address arg = Address(V, getContext().getTypeAlignInChars(pointeeTy)); |
| 2427 | llvm::Value *incomingErrorValue = Builder.CreateLoad(arg); |
| 2428 | Builder.CreateStore(incomingErrorValue, temp); |
| 2429 | V = temp.getPointer(); |
| 2430 | |
| 2431 | // Push a cleanup to copy the value back at the end of the function. |
| 2432 | // The convention does not guarantee that the value will be written |
| 2433 | // back if the function exits with an unwind exception. |
| 2434 | EHStack.pushCleanup<CopyBackSwiftError>(NormalCleanup, temp, arg); |
| 2435 | } |
| 2436 | |
Chris Lattner | 7369c14 | 2011-07-20 06:29:00 +0000 | [diff] [blame] | 2437 | // Ensure the argument is the correct type. |
| 2438 | if (V->getType() != ArgI.getCoerceToType()) |
| 2439 | V = Builder.CreateBitCast(V, ArgI.getCoerceToType()); |
| 2440 | |
John McCall | a738c25 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 2441 | if (isPromoted) |
| 2442 | V = emitArgumentDemotion(*this, Arg, V); |
Rafael Espindola | 8778c28 | 2012-11-29 16:09:03 +0000 | [diff] [blame] | 2443 | |
| 2444 | // Because of merging of function types from multiple decls it is |
| 2445 | // possible for the type of an argument to not match the corresponding |
| 2446 | // type in the function type. Since we are codegening the callee |
| 2447 | // in here, add a cast to the argument type. |
| 2448 | llvm::Type *LTy = ConvertType(Arg->getType()); |
| 2449 | if (V->getType() != LTy) |
| 2450 | V = Builder.CreateBitCast(V, LTy); |
| 2451 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2452 | ArgVals.push_back(ParamValue::forDirect(V)); |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2453 | break; |
Daniel Dunbar | d5f1f55 | 2009-02-10 00:06:49 +0000 | [diff] [blame] | 2454 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2455 | |
Volodymyr Sapsai | 22b00ec | 2017-12-21 20:52:59 +0000 | [diff] [blame] | 2456 | Address Alloca = CreateMemTemp(Ty, getContext().getDeclAlign(Arg), |
| 2457 | Arg->getName()); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2458 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2459 | // Pointer to store into. |
| 2460 | Address Ptr = emitAddressAtOffset(*this, Alloca, ArgI); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2461 | |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 2462 | // Fast-isel and the optimizer generally like scalar values better than |
| 2463 | // 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] | 2464 | llvm::StructType *STy = dyn_cast<llvm::StructType>(ArgI.getCoerceToType()); |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 2465 | if (ArgI.isDirect() && ArgI.getCanBeFlattened() && STy && |
| 2466 | STy->getNumElements() > 1) { |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2467 | uint64_t SrcSize = CGM.getDataLayout().getTypeAllocSize(STy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2468 | llvm::Type *DstTy = Ptr.getElementType(); |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2469 | uint64_t DstSize = CGM.getDataLayout().getTypeAllocSize(DstTy); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2470 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2471 | Address AddrToStoreInto = Address::invalid(); |
Evgeniy Stepanov | 3fae4ae | 2012-02-10 09:30:15 +0000 | [diff] [blame] | 2472 | if (SrcSize <= DstSize) { |
Yaxun Liu | e9e5c4f | 2017-06-29 18:47:45 +0000 | [diff] [blame] | 2473 | AddrToStoreInto = Builder.CreateElementBitCast(Ptr, STy); |
Evgeniy Stepanov | 3fae4ae | 2012-02-10 09:30:15 +0000 | [diff] [blame] | 2474 | } else { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2475 | AddrToStoreInto = |
| 2476 | CreateTempAlloca(STy, Alloca.getAlignment(), "coerce"); |
Chris Lattner | 15ec361 | 2010-06-29 00:06:42 +0000 | [diff] [blame] | 2477 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2478 | |
| 2479 | assert(STy->getNumElements() == NumIRArgs); |
| 2480 | for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { |
| 2481 | auto AI = FnArgs[FirstIRArg + i]; |
| 2482 | AI->setName(Arg->getName() + ".coerce" + Twine(i)); |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 2483 | Address EltPtr = Builder.CreateStructGEP(AddrToStoreInto, i); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2484 | Builder.CreateStore(AI, EltPtr); |
| 2485 | } |
| 2486 | |
| 2487 | if (SrcSize > DstSize) { |
| 2488 | Builder.CreateMemCpy(Ptr, AddrToStoreInto, DstSize); |
| 2489 | } |
| 2490 | |
Chris Lattner | 15ec361 | 2010-06-29 00:06:42 +0000 | [diff] [blame] | 2491 | } else { |
| 2492 | // 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] | 2493 | assert(NumIRArgs == 1); |
| 2494 | auto AI = FnArgs[FirstIRArg]; |
Chris Lattner | 9e748e9 | 2010-06-29 00:14:52 +0000 | [diff] [blame] | 2495 | AI->setName(Arg->getName() + ".coerce"); |
Rui Ueyama | 49a3ad2 | 2019-07-16 04:46:31 +0000 | [diff] [blame] | 2496 | CreateCoercedStore(AI, Ptr, /*DstIsVolatile=*/false, *this); |
Chris Lattner | 15ec361 | 2010-06-29 00:06:42 +0000 | [diff] [blame] | 2497 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2498 | |
Daniel Dunbar | 2f219b0 | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 2499 | // Match to what EmitParmDecl is expecting for this type. |
Volodymyr Sapsai | 22b00ec | 2017-12-21 20:52:59 +0000 | [diff] [blame] | 2500 | if (CodeGenFunction::hasScalarEvaluationKind(Ty)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2501 | llvm::Value *V = |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2502 | EmitLoadOfScalar(Alloca, false, Ty, Arg->getBeginLoc()); |
John McCall | a738c25 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 2503 | if (isPromoted) |
| 2504 | V = emitArgumentDemotion(*this, Arg, V); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2505 | ArgVals.push_back(ParamValue::forDirect(V)); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 2506 | } else { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2507 | ArgVals.push_back(ParamValue::forIndirect(Alloca)); |
Daniel Dunbar | 6e3b7df | 2009-02-04 07:22:24 +0000 | [diff] [blame] | 2508 | } |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 2509 | break; |
Daniel Dunbar | 2f219b0 | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 2510 | } |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2511 | |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 2512 | case ABIArgInfo::CoerceAndExpand: { |
| 2513 | // Reconstruct into a temporary. |
| 2514 | Address alloca = CreateMemTemp(Ty, getContext().getDeclAlign(Arg)); |
| 2515 | ArgVals.push_back(ParamValue::forIndirect(alloca)); |
| 2516 | |
| 2517 | auto coercionType = ArgI.getCoerceAndExpandType(); |
| 2518 | alloca = Builder.CreateElementBitCast(alloca, coercionType); |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 2519 | |
| 2520 | unsigned argIndex = FirstIRArg; |
| 2521 | for (unsigned i = 0, e = coercionType->getNumElements(); i != e; ++i) { |
| 2522 | llvm::Type *eltType = coercionType->getElementType(i); |
| 2523 | if (ABIArgInfo::isPaddingForCoerceAndExpand(eltType)) |
| 2524 | continue; |
| 2525 | |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 2526 | auto eltAddr = Builder.CreateStructGEP(alloca, i); |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 2527 | auto elt = FnArgs[argIndex++]; |
| 2528 | Builder.CreateStore(elt, eltAddr); |
| 2529 | } |
| 2530 | assert(argIndex == FirstIRArg + NumIRArgs); |
| 2531 | break; |
| 2532 | } |
| 2533 | |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2534 | case ABIArgInfo::Expand: { |
| 2535 | // If this structure was expanded into multiple arguments then |
| 2536 | // we need to create a temporary and reconstruct it from the |
| 2537 | // arguments. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2538 | Address Alloca = CreateMemTemp(Ty, getContext().getDeclAlign(Arg)); |
| 2539 | LValue LV = MakeAddrLValue(Alloca, Ty); |
| 2540 | ArgVals.push_back(ParamValue::forIndirect(Alloca)); |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2541 | |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 2542 | auto FnArgIter = FnArgs.begin() + FirstIRArg; |
| 2543 | ExpandTypeFromArgs(Ty, LV, FnArgIter); |
| 2544 | assert(FnArgIter == FnArgs.begin() + FirstIRArg + NumIRArgs); |
| 2545 | for (unsigned i = 0, e = NumIRArgs; i != e; ++i) { |
| 2546 | auto AI = FnArgs[FirstIRArg + i]; |
| 2547 | AI->setName(Arg->getName() + "." + Twine(i)); |
| 2548 | } |
| 2549 | break; |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2550 | } |
| 2551 | |
| 2552 | case ABIArgInfo::Ignore: |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 2553 | assert(NumIRArgs == 0); |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2554 | // Initialize the local variable appropriately. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 2555 | if (!hasScalarEvaluationKind(Ty)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2556 | ArgVals.push_back(ParamValue::forIndirect(CreateMemTemp(Ty))); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 2557 | } else { |
| 2558 | llvm::Value *U = llvm::UndefValue::get(ConvertType(Arg->getType())); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2559 | ArgVals.push_back(ParamValue::forDirect(U)); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 2560 | } |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 2561 | break; |
Daniel Dunbar | d3674e6 | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 2562 | } |
Daniel Dunbar | 613855c | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 2563 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 2564 | |
Reid Kleckner | 739756c | 2013-12-04 19:23:12 +0000 | [diff] [blame] | 2565 | if (getTarget().getCXXABI().areArgsDestroyedLeftToRightInCallee()) { |
| 2566 | for (int I = Args.size() - 1; I >= 0; --I) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2567 | EmitParmDecl(*Args[I], ArgVals[I], I + 1); |
Reid Kleckner | 739756c | 2013-12-04 19:23:12 +0000 | [diff] [blame] | 2568 | } else { |
| 2569 | for (unsigned I = 0, E = Args.size(); I != E; ++I) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2570 | EmitParmDecl(*Args[I], ArgVals[I], I + 1); |
Reid Kleckner | 739756c | 2013-12-04 19:23:12 +0000 | [diff] [blame] | 2571 | } |
Daniel Dunbar | 613855c | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 2572 | } |
| 2573 | |
John McCall | ffa2c1a | 2012-01-29 07:46:59 +0000 | [diff] [blame] | 2574 | static void eraseUnusedBitCasts(llvm::Instruction *insn) { |
| 2575 | while (insn->use_empty()) { |
| 2576 | llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(insn); |
| 2577 | if (!bitcast) return; |
| 2578 | |
| 2579 | // This is "safe" because we would have used a ConstantExpr otherwise. |
| 2580 | insn = cast<llvm::Instruction>(bitcast->getOperand(0)); |
| 2581 | bitcast->eraseFromParent(); |
| 2582 | } |
| 2583 | } |
| 2584 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2585 | /// Try to emit a fused autorelease of a return result. |
| 2586 | static llvm::Value *tryEmitFusedAutoreleaseOfResult(CodeGenFunction &CGF, |
| 2587 | llvm::Value *result) { |
| 2588 | // We must be immediately followed the cast. |
| 2589 | llvm::BasicBlock *BB = CGF.Builder.GetInsertBlock(); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2590 | if (BB->empty()) return nullptr; |
| 2591 | if (&BB->back() != result) return nullptr; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2592 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2593 | llvm::Type *resultType = result->getType(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2594 | |
| 2595 | // result is in a BasicBlock and is therefore an Instruction. |
| 2596 | llvm::Instruction *generator = cast<llvm::Instruction>(result); |
| 2597 | |
Justin Bogner | 882f861 | 2016-08-18 21:46:54 +0000 | [diff] [blame] | 2598 | SmallVector<llvm::Instruction *, 4> InstsToKill; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2599 | |
| 2600 | // Look for: |
| 2601 | // %generator = bitcast %type1* %generator2 to %type2* |
| 2602 | while (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(generator)) { |
| 2603 | // We would have emitted this as a constant if the operand weren't |
| 2604 | // an Instruction. |
| 2605 | generator = cast<llvm::Instruction>(bitcast->getOperand(0)); |
| 2606 | |
| 2607 | // Require the generator to be immediately followed by the cast. |
| 2608 | if (generator->getNextNode() != bitcast) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2609 | return nullptr; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2610 | |
Justin Bogner | 882f861 | 2016-08-18 21:46:54 +0000 | [diff] [blame] | 2611 | InstsToKill.push_back(bitcast); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2612 | } |
| 2613 | |
| 2614 | // Look for: |
| 2615 | // %generator = call i8* @objc_retain(i8* %originalResult) |
| 2616 | // or |
| 2617 | // %generator = call i8* @objc_retainAutoreleasedReturnValue(i8* %originalResult) |
| 2618 | llvm::CallInst *call = dyn_cast<llvm::CallInst>(generator); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2619 | if (!call) return nullptr; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2620 | |
| 2621 | bool doRetainAutorelease; |
| 2622 | |
John McCall | b04ecb7 | 2015-10-21 18:06:43 +0000 | [diff] [blame] | 2623 | if (call->getCalledValue() == CGF.CGM.getObjCEntrypoints().objc_retain) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2624 | doRetainAutorelease = true; |
John McCall | b04ecb7 | 2015-10-21 18:06:43 +0000 | [diff] [blame] | 2625 | } else if (call->getCalledValue() == CGF.CGM.getObjCEntrypoints() |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2626 | .objc_retainAutoreleasedReturnValue) { |
| 2627 | doRetainAutorelease = false; |
| 2628 | |
John McCall | cfa4e9b | 2012-09-07 23:30:50 +0000 | [diff] [blame] | 2629 | // If we emitted an assembly marker for this call (and the |
| 2630 | // ARCEntrypoints field should have been set if so), go looking |
| 2631 | // for that call. If we can't find it, we can't do this |
| 2632 | // optimization. But it should always be the immediately previous |
| 2633 | // instruction, unless we needed bitcasts around the call. |
John McCall | b04ecb7 | 2015-10-21 18:06:43 +0000 | [diff] [blame] | 2634 | if (CGF.CGM.getObjCEntrypoints().retainAutoreleasedReturnValueMarker) { |
John McCall | cfa4e9b | 2012-09-07 23:30:50 +0000 | [diff] [blame] | 2635 | llvm::Instruction *prev = call->getPrevNode(); |
| 2636 | assert(prev); |
| 2637 | if (isa<llvm::BitCastInst>(prev)) { |
| 2638 | prev = prev->getPrevNode(); |
| 2639 | assert(prev); |
| 2640 | } |
| 2641 | assert(isa<llvm::CallInst>(prev)); |
| 2642 | assert(cast<llvm::CallInst>(prev)->getCalledValue() == |
John McCall | b04ecb7 | 2015-10-21 18:06:43 +0000 | [diff] [blame] | 2643 | CGF.CGM.getObjCEntrypoints().retainAutoreleasedReturnValueMarker); |
Justin Bogner | 882f861 | 2016-08-18 21:46:54 +0000 | [diff] [blame] | 2644 | InstsToKill.push_back(prev); |
John McCall | cfa4e9b | 2012-09-07 23:30:50 +0000 | [diff] [blame] | 2645 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2646 | } else { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2647 | return nullptr; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2648 | } |
| 2649 | |
| 2650 | result = call->getArgOperand(0); |
Justin Bogner | 882f861 | 2016-08-18 21:46:54 +0000 | [diff] [blame] | 2651 | InstsToKill.push_back(call); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2652 | |
| 2653 | // Keep killing bitcasts, for sanity. Note that we no longer care |
| 2654 | // about precise ordering as long as there's exactly one use. |
| 2655 | while (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(result)) { |
| 2656 | if (!bitcast->hasOneUse()) break; |
Justin Bogner | 882f861 | 2016-08-18 21:46:54 +0000 | [diff] [blame] | 2657 | InstsToKill.push_back(bitcast); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2658 | result = bitcast->getOperand(0); |
| 2659 | } |
| 2660 | |
| 2661 | // Delete all the unnecessary instructions, from latest to earliest. |
Justin Bogner | 882f861 | 2016-08-18 21:46:54 +0000 | [diff] [blame] | 2662 | for (auto *I : InstsToKill) |
Saleem Abdulrasool | be25c48 | 2016-08-18 21:40:06 +0000 | [diff] [blame] | 2663 | I->eraseFromParent(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2664 | |
| 2665 | // Do the fused retain/autorelease if we were asked to. |
| 2666 | if (doRetainAutorelease) |
| 2667 | result = CGF.EmitARCRetainAutoreleaseReturnValue(result); |
| 2668 | |
| 2669 | // Cast back to the result type. |
| 2670 | return CGF.Builder.CreateBitCast(result, resultType); |
| 2671 | } |
| 2672 | |
John McCall | ffa2c1a | 2012-01-29 07:46:59 +0000 | [diff] [blame] | 2673 | /// If this is a +1 of the value of an immutable 'self', remove it. |
| 2674 | static llvm::Value *tryRemoveRetainOfSelf(CodeGenFunction &CGF, |
| 2675 | llvm::Value *result) { |
| 2676 | // This is only applicable to a method with an immutable 'self'. |
John McCall | ff755cd | 2012-07-31 00:33:55 +0000 | [diff] [blame] | 2677 | const ObjCMethodDecl *method = |
| 2678 | dyn_cast_or_null<ObjCMethodDecl>(CGF.CurCodeDecl); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2679 | if (!method) return nullptr; |
John McCall | ffa2c1a | 2012-01-29 07:46:59 +0000 | [diff] [blame] | 2680 | const VarDecl *self = method->getSelfDecl(); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2681 | if (!self->getType().isConstQualified()) return nullptr; |
John McCall | ffa2c1a | 2012-01-29 07:46:59 +0000 | [diff] [blame] | 2682 | |
| 2683 | // Look for a retain call. |
| 2684 | llvm::CallInst *retainCall = |
| 2685 | dyn_cast<llvm::CallInst>(result->stripPointerCasts()); |
| 2686 | if (!retainCall || |
John McCall | b04ecb7 | 2015-10-21 18:06:43 +0000 | [diff] [blame] | 2687 | retainCall->getCalledValue() != CGF.CGM.getObjCEntrypoints().objc_retain) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2688 | return nullptr; |
John McCall | ffa2c1a | 2012-01-29 07:46:59 +0000 | [diff] [blame] | 2689 | |
| 2690 | // Look for an ordinary load of 'self'. |
| 2691 | llvm::Value *retainedValue = retainCall->getArgOperand(0); |
| 2692 | llvm::LoadInst *load = |
| 2693 | dyn_cast<llvm::LoadInst>(retainedValue->stripPointerCasts()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2694 | if (!load || load->isAtomic() || load->isVolatile() || |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2695 | load->getPointerOperand() != CGF.GetAddrOfLocalVar(self).getPointer()) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2696 | return nullptr; |
John McCall | ffa2c1a | 2012-01-29 07:46:59 +0000 | [diff] [blame] | 2697 | |
| 2698 | // Okay! Burn it all down. This relies for correctness on the |
| 2699 | // assumption that the retain is emitted as part of the return and |
| 2700 | // that thereafter everything is used "linearly". |
| 2701 | llvm::Type *resultType = result->getType(); |
| 2702 | eraseUnusedBitCasts(cast<llvm::Instruction>(result)); |
| 2703 | assert(retainCall->use_empty()); |
| 2704 | retainCall->eraseFromParent(); |
| 2705 | eraseUnusedBitCasts(cast<llvm::Instruction>(retainedValue)); |
| 2706 | |
| 2707 | return CGF.Builder.CreateBitCast(load, resultType); |
| 2708 | } |
| 2709 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2710 | /// Emit an ARC autorelease of the result of a function. |
John McCall | ffa2c1a | 2012-01-29 07:46:59 +0000 | [diff] [blame] | 2711 | /// |
| 2712 | /// \return the value to actually return from the function |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2713 | static llvm::Value *emitAutoreleaseOfResult(CodeGenFunction &CGF, |
| 2714 | llvm::Value *result) { |
John McCall | ffa2c1a | 2012-01-29 07:46:59 +0000 | [diff] [blame] | 2715 | // If we're returning 'self', kill the initial retain. This is a |
| 2716 | // heuristic attempt to "encourage correctness" in the really unfortunate |
| 2717 | // case where we have a return of self during a dealloc and we desperately |
| 2718 | // need to avoid the possible autorelease. |
| 2719 | if (llvm::Value *self = tryRemoveRetainOfSelf(CGF, result)) |
| 2720 | return self; |
| 2721 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2722 | // At -O0, try to emit a fused retain/autorelease. |
| 2723 | if (CGF.shouldUseFusedARCCalls()) |
| 2724 | if (llvm::Value *fused = tryEmitFusedAutoreleaseOfResult(CGF, result)) |
| 2725 | return fused; |
| 2726 | |
| 2727 | return CGF.EmitARCAutoreleaseReturnValue(result); |
| 2728 | } |
| 2729 | |
John McCall | 6e1c012 | 2012-01-29 02:35:02 +0000 | [diff] [blame] | 2730 | /// Heuristically search for a dominating store to the return-value slot. |
| 2731 | static llvm::StoreInst *findDominatingStoreToReturnValue(CodeGenFunction &CGF) { |
Jakub Kuderski | f50ab0f | 2015-09-08 10:36:42 +0000 | [diff] [blame] | 2732 | // Check if a User is a store which pointerOperand is the ReturnValue. |
| 2733 | // We are looking for stores to the ReturnValue, not for stores of the |
| 2734 | // ReturnValue to some other location. |
| 2735 | auto GetStoreIfValid = [&CGF](llvm::User *U) -> llvm::StoreInst * { |
| 2736 | auto *SI = dyn_cast<llvm::StoreInst>(U); |
| 2737 | if (!SI || SI->getPointerOperand() != CGF.ReturnValue.getPointer()) |
| 2738 | return nullptr; |
| 2739 | // These aren't actually possible for non-coerced returns, and we |
| 2740 | // only care about non-coerced returns on this code path. |
| 2741 | assert(!SI->isAtomic() && !SI->isVolatile()); |
| 2742 | return SI; |
| 2743 | }; |
John McCall | 6e1c012 | 2012-01-29 02:35:02 +0000 | [diff] [blame] | 2744 | // If there are multiple uses of the return-value slot, just check |
| 2745 | // for something immediately preceding the IP. Sometimes this can |
| 2746 | // happen with how we generate implicit-returns; it can also happen |
| 2747 | // with noreturn cleanups. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2748 | if (!CGF.ReturnValue.getPointer()->hasOneUse()) { |
John McCall | 6e1c012 | 2012-01-29 02:35:02 +0000 | [diff] [blame] | 2749 | llvm::BasicBlock *IP = CGF.Builder.GetInsertBlock(); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2750 | if (IP->empty()) return nullptr; |
David Majnemer | dc012fa | 2015-04-22 21:38:15 +0000 | [diff] [blame] | 2751 | llvm::Instruction *I = &IP->back(); |
| 2752 | |
| 2753 | // Skip lifetime markers |
| 2754 | for (llvm::BasicBlock::reverse_iterator II = IP->rbegin(), |
| 2755 | IE = IP->rend(); |
| 2756 | II != IE; ++II) { |
| 2757 | if (llvm::IntrinsicInst *Intrinsic = |
| 2758 | dyn_cast<llvm::IntrinsicInst>(&*II)) { |
| 2759 | if (Intrinsic->getIntrinsicID() == llvm::Intrinsic::lifetime_end) { |
| 2760 | const llvm::Value *CastAddr = Intrinsic->getArgOperand(1); |
| 2761 | ++II; |
Alexey Samsonov | 1054420 | 2015-06-12 21:05:32 +0000 | [diff] [blame] | 2762 | if (II == IE) |
| 2763 | break; |
| 2764 | if (isa<llvm::BitCastInst>(&*II) && (CastAddr == &*II)) |
| 2765 | continue; |
David Majnemer | dc012fa | 2015-04-22 21:38:15 +0000 | [diff] [blame] | 2766 | } |
| 2767 | } |
| 2768 | I = &*II; |
| 2769 | break; |
| 2770 | } |
| 2771 | |
Jakub Kuderski | f50ab0f | 2015-09-08 10:36:42 +0000 | [diff] [blame] | 2772 | return GetStoreIfValid(I); |
John McCall | 6e1c012 | 2012-01-29 02:35:02 +0000 | [diff] [blame] | 2773 | } |
| 2774 | |
| 2775 | llvm::StoreInst *store = |
Jakub Kuderski | f50ab0f | 2015-09-08 10:36:42 +0000 | [diff] [blame] | 2776 | GetStoreIfValid(CGF.ReturnValue.getPointer()->user_back()); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2777 | if (!store) return nullptr; |
John McCall | 6e1c012 | 2012-01-29 02:35:02 +0000 | [diff] [blame] | 2778 | |
John McCall | 6e1c012 | 2012-01-29 02:35:02 +0000 | [diff] [blame] | 2779 | // Now do a first-and-dirty dominance check: just walk up the |
| 2780 | // single-predecessors chain from the current insertion point. |
| 2781 | llvm::BasicBlock *StoreBB = store->getParent(); |
| 2782 | llvm::BasicBlock *IP = CGF.Builder.GetInsertBlock(); |
| 2783 | while (IP != StoreBB) { |
| 2784 | if (!(IP = IP->getSinglePredecessor())) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2785 | return nullptr; |
John McCall | 6e1c012 | 2012-01-29 02:35:02 +0000 | [diff] [blame] | 2786 | } |
| 2787 | |
| 2788 | // Okay, the store's basic block dominates the insertion point; we |
| 2789 | // can do our thing. |
| 2790 | return store; |
| 2791 | } |
| 2792 | |
Adrian Prantl | 3be1054 | 2013-05-02 17:30:20 +0000 | [diff] [blame] | 2793 | void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI, |
Nick Lewycky | 2d84e84 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 2794 | bool EmitRetDbgLoc, |
| 2795 | SourceLocation EndLoc) { |
Vedant Kumar | 09b5bfd | 2017-12-21 00:10:25 +0000 | [diff] [blame] | 2796 | if (FI.isNoReturn()) { |
| 2797 | // Noreturn functions don't return. |
| 2798 | EmitUnreachable(EndLoc); |
| 2799 | return; |
| 2800 | } |
| 2801 | |
Hans Wennborg | d71907d | 2014-09-04 22:16:33 +0000 | [diff] [blame] | 2802 | if (CurCodeDecl && CurCodeDecl->hasAttr<NakedAttr>()) { |
| 2803 | // Naked functions don't have epilogues. |
| 2804 | Builder.CreateUnreachable(); |
| 2805 | return; |
| 2806 | } |
| 2807 | |
Daniel Dunbar | a72d4ae | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 2808 | // Functions with no result always return void. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2809 | if (!ReturnValue.isValid()) { |
Daniel Dunbar | a72d4ae | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 2810 | Builder.CreateRetVoid(); |
Chris Lattner | 726b3d0 | 2010-06-26 23:13:19 +0000 | [diff] [blame] | 2811 | return; |
Daniel Dunbar | a72d4ae | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 2812 | } |
Daniel Dunbar | 6696e22 | 2010-06-30 21:27:58 +0000 | [diff] [blame] | 2813 | |
Dan Gohman | 481e40c | 2010-07-20 20:13:52 +0000 | [diff] [blame] | 2814 | llvm::DebugLoc RetDbgLoc; |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2815 | llvm::Value *RV = nullptr; |
Chris Lattner | 726b3d0 | 2010-06-26 23:13:19 +0000 | [diff] [blame] | 2816 | QualType RetTy = FI.getReturnType(); |
| 2817 | const ABIArgInfo &RetAI = FI.getReturnInfo(); |
| 2818 | |
| 2819 | switch (RetAI.getKind()) { |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 2820 | case ABIArgInfo::InAlloca: |
Reid Kleckner | fab1e89 | 2014-02-25 00:59:14 +0000 | [diff] [blame] | 2821 | // Aggregrates get evaluated directly into the destination. Sometimes we |
| 2822 | // need to return the sret value in a register, though. |
| 2823 | assert(hasAggregateEvaluationKind(RetTy)); |
| 2824 | if (RetAI.getInAllocaSRet()) { |
| 2825 | llvm::Function::arg_iterator EI = CurFn->arg_end(); |
| 2826 | --EI; |
Duncan P. N. Exon Smith | 9f5260a | 2015-11-06 23:00:41 +0000 | [diff] [blame] | 2827 | llvm::Value *ArgStruct = &*EI; |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 2828 | llvm::Value *SRet = Builder.CreateStructGEP( |
| 2829 | nullptr, ArgStruct, RetAI.getInAllocaFieldIndex()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2830 | RV = Builder.CreateAlignedLoad(SRet, getPointerAlign(), "sret"); |
Reid Kleckner | fab1e89 | 2014-02-25 00:59:14 +0000 | [diff] [blame] | 2831 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 2832 | break; |
| 2833 | |
Daniel Dunbar | 0381634 | 2010-08-21 02:24:36 +0000 | [diff] [blame] | 2834 | case ABIArgInfo::Indirect: { |
Reid Kleckner | 37abaca | 2014-05-09 22:46:15 +0000 | [diff] [blame] | 2835 | auto AI = CurFn->arg_begin(); |
| 2836 | if (RetAI.isSRetAfterThis()) |
| 2837 | ++AI; |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 2838 | switch (getEvaluationKind(RetTy)) { |
| 2839 | case TEK_Complex: { |
| 2840 | ComplexPairTy RT = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2841 | EmitLoadOfComplex(MakeAddrLValue(ReturnValue, RetTy), EndLoc); |
Duncan P. N. Exon Smith | 9f5260a | 2015-11-06 23:00:41 +0000 | [diff] [blame] | 2842 | EmitStoreOfComplex(RT, MakeNaturalAlignAddrLValue(&*AI, RetTy), |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 2843 | /*isInit*/ true); |
| 2844 | break; |
| 2845 | } |
| 2846 | case TEK_Aggregate: |
Chris Lattner | 726b3d0 | 2010-06-26 23:13:19 +0000 | [diff] [blame] | 2847 | // Do nothing; aggregrates get evaluated directly into the destination. |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 2848 | break; |
| 2849 | case TEK_Scalar: |
| 2850 | EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), |
Duncan P. N. Exon Smith | 9f5260a | 2015-11-06 23:00:41 +0000 | [diff] [blame] | 2851 | MakeNaturalAlignAddrLValue(&*AI, RetTy), |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 2852 | /*isInit*/ true); |
| 2853 | break; |
Chris Lattner | 726b3d0 | 2010-06-26 23:13:19 +0000 | [diff] [blame] | 2854 | } |
| 2855 | break; |
Daniel Dunbar | 0381634 | 2010-08-21 02:24:36 +0000 | [diff] [blame] | 2856 | } |
Chris Lattner | 726b3d0 | 2010-06-26 23:13:19 +0000 | [diff] [blame] | 2857 | |
| 2858 | case ABIArgInfo::Extend: |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2859 | case ABIArgInfo::Direct: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2860 | if (RetAI.getCoerceToType() == ConvertType(RetTy) && |
| 2861 | RetAI.getDirectOffset() == 0) { |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2862 | // The internal return value temp always will have pointer-to-return-type |
| 2863 | // type, just do a load. |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2864 | |
John McCall | 6e1c012 | 2012-01-29 02:35:02 +0000 | [diff] [blame] | 2865 | // If there is a dominating store to ReturnValue, we can elide |
| 2866 | // the load, zap the store, and usually zap the alloca. |
David Majnemer | dc012fa | 2015-04-22 21:38:15 +0000 | [diff] [blame] | 2867 | if (llvm::StoreInst *SI = |
| 2868 | findDominatingStoreToReturnValue(*this)) { |
Adrian Prantl | 4c9a38a | 2013-05-30 18:12:23 +0000 | [diff] [blame] | 2869 | // Reuse the debug location from the store unless there is |
| 2870 | // cleanup code to be emitted between the store and return |
| 2871 | // instruction. |
| 2872 | if (EmitRetDbgLoc && !AutoreleaseResult) |
Adrian Prantl | 3be1054 | 2013-05-02 17:30:20 +0000 | [diff] [blame] | 2873 | RetDbgLoc = SI->getDebugLoc(); |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2874 | // Get the stored value and nuke the now-dead store. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2875 | RV = SI->getValueOperand(); |
| 2876 | SI->eraseFromParent(); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2877 | |
John McCall | 6e1c012 | 2012-01-29 02:35:02 +0000 | [diff] [blame] | 2878 | // Otherwise, we have to do a simple load. |
| 2879 | } else { |
| 2880 | RV = Builder.CreateLoad(ReturnValue); |
Chris Lattner | 3fcc790 | 2010-06-27 01:06:27 +0000 | [diff] [blame] | 2881 | } |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2882 | } else { |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2883 | // If the value is offset in memory, apply the offset now. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2884 | Address V = emitAddressAtOffset(*this, ReturnValue, RetAI); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2885 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2886 | RV = CreateCoercedLoad(V, RetAI.getCoerceToType(), *this); |
Chris Lattner | 3fcc790 | 2010-06-27 01:06:27 +0000 | [diff] [blame] | 2887 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2888 | |
| 2889 | // In ARC, end functions that return a retainable type with a call |
| 2890 | // to objc_autoreleaseReturnValue. |
| 2891 | if (AutoreleaseResult) { |
Akira Hatanaka | 9d8ac61 | 2016-02-17 21:09:50 +0000 | [diff] [blame] | 2892 | #ifndef NDEBUG |
| 2893 | // Type::isObjCRetainabletype has to be called on a QualType that hasn't |
| 2894 | // been stripped of the typedefs, so we cannot use RetTy here. Get the |
| 2895 | // original return type of FunctionDecl, CurCodeDecl, and BlockDecl from |
| 2896 | // CurCodeDecl or BlockInfo. |
| 2897 | QualType RT; |
| 2898 | |
| 2899 | if (auto *FD = dyn_cast<FunctionDecl>(CurCodeDecl)) |
| 2900 | RT = FD->getReturnType(); |
| 2901 | else if (auto *MD = dyn_cast<ObjCMethodDecl>(CurCodeDecl)) |
| 2902 | RT = MD->getReturnType(); |
| 2903 | else if (isa<BlockDecl>(CurCodeDecl)) |
| 2904 | RT = BlockInfo->BlockExpression->getFunctionType()->getReturnType(); |
| 2905 | else |
| 2906 | llvm_unreachable("Unexpected function/method type"); |
| 2907 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2908 | assert(getLangOpts().ObjCAutoRefCount && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2909 | !FI.isReturnsRetained() && |
Akira Hatanaka | 9d8ac61 | 2016-02-17 21:09:50 +0000 | [diff] [blame] | 2910 | RT->isObjCRetainableType()); |
| 2911 | #endif |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2912 | RV = emitAutoreleaseOfResult(*this, RV); |
| 2913 | } |
| 2914 | |
Chris Lattner | 726b3d0 | 2010-06-26 23:13:19 +0000 | [diff] [blame] | 2915 | break; |
Chris Lattner | 726b3d0 | 2010-06-26 23:13:19 +0000 | [diff] [blame] | 2916 | |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2917 | case ABIArgInfo::Ignore: |
Chris Lattner | 726b3d0 | 2010-06-26 23:13:19 +0000 | [diff] [blame] | 2918 | break; |
| 2919 | |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 2920 | case ABIArgInfo::CoerceAndExpand: { |
| 2921 | auto coercionType = RetAI.getCoerceAndExpandType(); |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 2922 | |
| 2923 | // Load all of the coerced elements out into results. |
| 2924 | llvm::SmallVector<llvm::Value*, 4> results; |
| 2925 | Address addr = Builder.CreateElementBitCast(ReturnValue, coercionType); |
| 2926 | for (unsigned i = 0, e = coercionType->getNumElements(); i != e; ++i) { |
| 2927 | auto coercedEltType = coercionType->getElementType(i); |
| 2928 | if (ABIArgInfo::isPaddingForCoerceAndExpand(coercedEltType)) |
| 2929 | continue; |
| 2930 | |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 2931 | auto eltAddr = Builder.CreateStructGEP(addr, i); |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 2932 | auto elt = Builder.CreateLoad(eltAddr); |
| 2933 | results.push_back(elt); |
| 2934 | } |
| 2935 | |
| 2936 | // If we have one result, it's the single direct result type. |
| 2937 | if (results.size() == 1) { |
| 2938 | RV = results[0]; |
| 2939 | |
| 2940 | // Otherwise, we need to make a first-class aggregate. |
| 2941 | } else { |
| 2942 | // Construct a return type that lacks padding elements. |
| 2943 | llvm::Type *returnType = RetAI.getUnpaddedCoerceAndExpandType(); |
| 2944 | |
| 2945 | RV = llvm::UndefValue::get(returnType); |
| 2946 | for (unsigned i = 0, e = results.size(); i != e; ++i) { |
| 2947 | RV = Builder.CreateInsertValue(RV, results[i], i); |
| 2948 | } |
| 2949 | } |
| 2950 | break; |
| 2951 | } |
| 2952 | |
Chris Lattner | 726b3d0 | 2010-06-26 23:13:19 +0000 | [diff] [blame] | 2953 | case ABIArgInfo::Expand: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2954 | llvm_unreachable("Invalid ABI kind for return argument"); |
Chris Lattner | 726b3d0 | 2010-06-26 23:13:19 +0000 | [diff] [blame] | 2955 | } |
| 2956 | |
Alexey Samsonov | de443c5 | 2014-08-13 00:26:40 +0000 | [diff] [blame] | 2957 | llvm::Instruction *Ret; |
| 2958 | if (RV) { |
Vedant Kumar | c34d343 | 2017-06-23 21:32:38 +0000 | [diff] [blame] | 2959 | EmitReturnValueCheck(RV); |
Alexey Samsonov | de443c5 | 2014-08-13 00:26:40 +0000 | [diff] [blame] | 2960 | Ret = Builder.CreateRet(RV); |
| 2961 | } else { |
| 2962 | Ret = Builder.CreateRetVoid(); |
| 2963 | } |
| 2964 | |
Duncan P. N. Exon Smith | 2809cc7 | 2015-03-30 20:01:41 +0000 | [diff] [blame] | 2965 | if (RetDbgLoc) |
Benjamin Kramer | 0327866 | 2015-02-07 13:15:54 +0000 | [diff] [blame] | 2966 | Ret->setDebugLoc(std::move(RetDbgLoc)); |
Daniel Dunbar | 613855c | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 2967 | } |
| 2968 | |
Vedant Kumar | c34d343 | 2017-06-23 21:32:38 +0000 | [diff] [blame] | 2969 | void CodeGenFunction::EmitReturnValueCheck(llvm::Value *RV) { |
Vedant Kumar | 42c17ec | 2017-03-14 01:56:34 +0000 | [diff] [blame] | 2970 | // A current decl may not be available when emitting vtable thunks. |
| 2971 | if (!CurCodeDecl) |
| 2972 | return; |
| 2973 | |
| 2974 | ReturnsNonNullAttr *RetNNAttr = nullptr; |
| 2975 | if (SanOpts.has(SanitizerKind::ReturnsNonnullAttribute)) |
| 2976 | RetNNAttr = CurCodeDecl->getAttr<ReturnsNonNullAttr>(); |
| 2977 | |
| 2978 | if (!RetNNAttr && !requiresReturnValueNullabilityCheck()) |
| 2979 | return; |
| 2980 | |
| 2981 | // Prefer the returns_nonnull attribute if it's present. |
| 2982 | SourceLocation AttrLoc; |
| 2983 | SanitizerMask CheckKind; |
Vedant Kumar | 2b9f48a | 2017-03-14 16:48:29 +0000 | [diff] [blame] | 2984 | SanitizerHandler Handler; |
Vedant Kumar | 42c17ec | 2017-03-14 01:56:34 +0000 | [diff] [blame] | 2985 | if (RetNNAttr) { |
| 2986 | assert(!requiresReturnValueNullabilityCheck() && |
| 2987 | "Cannot check nullability and the nonnull attribute"); |
| 2988 | AttrLoc = RetNNAttr->getLocation(); |
| 2989 | CheckKind = SanitizerKind::ReturnsNonnullAttribute; |
Vedant Kumar | 2b9f48a | 2017-03-14 16:48:29 +0000 | [diff] [blame] | 2990 | Handler = SanitizerHandler::NonnullReturn; |
Vedant Kumar | 42c17ec | 2017-03-14 01:56:34 +0000 | [diff] [blame] | 2991 | } else { |
Vedant Kumar | 42c17ec | 2017-03-14 01:56:34 +0000 | [diff] [blame] | 2992 | if (auto *DD = dyn_cast<DeclaratorDecl>(CurCodeDecl)) |
| 2993 | if (auto *TSI = DD->getTypeSourceInfo()) |
| 2994 | if (auto FTL = TSI->getTypeLoc().castAs<FunctionTypeLoc>()) |
| 2995 | AttrLoc = FTL.getReturnLoc().findNullabilityLoc(); |
| 2996 | CheckKind = SanitizerKind::NullabilityReturn; |
Vedant Kumar | 2b9f48a | 2017-03-14 16:48:29 +0000 | [diff] [blame] | 2997 | Handler = SanitizerHandler::NullabilityReturn; |
Vedant Kumar | 42c17ec | 2017-03-14 01:56:34 +0000 | [diff] [blame] | 2998 | } |
| 2999 | |
| 3000 | SanitizerScope SanScope(this); |
| 3001 | |
Vedant Kumar | c34d343 | 2017-06-23 21:32:38 +0000 | [diff] [blame] | 3002 | // Make sure the "return" source location is valid. If we're checking a |
| 3003 | // nullability annotation, make sure the preconditions for the check are met. |
| 3004 | llvm::BasicBlock *Check = createBasicBlock("nullcheck"); |
| 3005 | llvm::BasicBlock *NoCheck = createBasicBlock("no.nullcheck"); |
| 3006 | llvm::Value *SLocPtr = Builder.CreateLoad(ReturnLocation, "return.sloc.load"); |
| 3007 | llvm::Value *CanNullCheck = Builder.CreateIsNotNull(SLocPtr); |
Vedant Kumar | 42c17ec | 2017-03-14 01:56:34 +0000 | [diff] [blame] | 3008 | if (requiresReturnValueNullabilityCheck()) |
Vedant Kumar | c34d343 | 2017-06-23 21:32:38 +0000 | [diff] [blame] | 3009 | CanNullCheck = |
| 3010 | Builder.CreateAnd(CanNullCheck, RetValNullabilityPrecondition); |
| 3011 | Builder.CreateCondBr(CanNullCheck, Check, NoCheck); |
| 3012 | EmitBlock(Check); |
| 3013 | |
| 3014 | // Now do the null check. |
| 3015 | llvm::Value *Cond = Builder.CreateIsNotNull(RV); |
| 3016 | llvm::Constant *StaticData[] = {EmitCheckSourceLocation(AttrLoc)}; |
| 3017 | llvm::Value *DynamicData[] = {SLocPtr}; |
| 3018 | EmitCheck(std::make_pair(Cond, CheckKind), Handler, StaticData, DynamicData); |
| 3019 | |
| 3020 | EmitBlock(NoCheck); |
| 3021 | |
| 3022 | #ifndef NDEBUG |
| 3023 | // The return location should not be used after the check has been emitted. |
| 3024 | ReturnLocation = Address::invalid(); |
| 3025 | #endif |
Vedant Kumar | 42c17ec | 2017-03-14 01:56:34 +0000 | [diff] [blame] | 3026 | } |
| 3027 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 3028 | static bool isInAllocaArgument(CGCXXABI &ABI, QualType type) { |
| 3029 | const CXXRecordDecl *RD = type->getAsCXXRecordDecl(); |
| 3030 | return RD && ABI.getRecordArgABI(RD) == CGCXXABI::RAA_DirectInMemory; |
| 3031 | } |
| 3032 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3033 | static AggValueSlot createPlaceholderSlot(CodeGenFunction &CGF, |
| 3034 | QualType Ty) { |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 3035 | // FIXME: Generate IR in one pass, rather than going back and fixing up these |
| 3036 | // placeholders. |
| 3037 | llvm::Type *IRTy = CGF.ConvertTypeForMem(Ty); |
Peter Collingbourne | b367c56 | 2016-11-28 22:30:21 +0000 | [diff] [blame] | 3038 | llvm::Type *IRPtrTy = IRTy->getPointerTo(); |
| 3039 | llvm::Value *Placeholder = llvm::UndefValue::get(IRPtrTy->getPointerTo()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3040 | |
| 3041 | // FIXME: When we generate this IR in one pass, we shouldn't need |
| 3042 | // this win32-specific alignment hack. |
| 3043 | CharUnits Align = CharUnits::fromQuantity(4); |
Peter Collingbourne | b367c56 | 2016-11-28 22:30:21 +0000 | [diff] [blame] | 3044 | Placeholder = CGF.Builder.CreateAlignedLoad(IRPtrTy, Placeholder, Align); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3045 | |
| 3046 | return AggValueSlot::forAddr(Address(Placeholder, Align), |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 3047 | Ty.getQualifiers(), |
| 3048 | AggValueSlot::IsNotDestructed, |
| 3049 | AggValueSlot::DoesNotNeedGCBarriers, |
Richard Smith | e78fac5 | 2018-04-05 20:52:58 +0000 | [diff] [blame] | 3050 | AggValueSlot::IsNotAliased, |
| 3051 | AggValueSlot::DoesNotOverlap); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 3052 | } |
| 3053 | |
John McCall | 32ea969 | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 3054 | void CodeGenFunction::EmitDelegateCallArg(CallArgList &args, |
Nick Lewycky | 2d84e84 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 3055 | const VarDecl *param, |
| 3056 | SourceLocation loc) { |
John McCall | 23f6626 | 2010-05-26 22:34:26 +0000 | [diff] [blame] | 3057 | // StartFunction converted the ABI-lowered parameter(s) into a |
| 3058 | // local alloca. We need to turn that into an r-value suitable |
| 3059 | // for EmitCall. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3060 | Address local = GetAddrOfLocalVar(param); |
John McCall | 23f6626 | 2010-05-26 22:34:26 +0000 | [diff] [blame] | 3061 | |
John McCall | 32ea969 | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 3062 | QualType type = param->getType(); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3063 | |
Reid Kleckner | 25b5602 | 2018-12-12 23:46:06 +0000 | [diff] [blame] | 3064 | if (isInAllocaArgument(CGM.getCXXABI(), type)) { |
| 3065 | CGM.ErrorUnsupported(param, "forwarded non-trivially copyable parameter"); |
| 3066 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 3067 | |
John McCall | 811b291 | 2016-11-18 01:08:24 +0000 | [diff] [blame] | 3068 | // GetAddrOfLocalVar returns a pointer-to-pointer for references, |
| 3069 | // but the argument needs to be the original pointer. |
| 3070 | if (type->isReferenceType()) { |
| 3071 | args.add(RValue::get(Builder.CreateLoad(local)), type); |
| 3072 | |
| 3073 | // In ARC, move out of consumed arguments so that the release cleanup |
| 3074 | // entered by StartFunction doesn't cause an over-release. This isn't |
| 3075 | // optimal -O0 code generation, but it should get cleaned up when |
| 3076 | // optimization is enabled. This also assumes that delegate calls are |
| 3077 | // performed exactly once for a set of arguments, but that should be safe. |
| 3078 | } else if (getLangOpts().ObjCAutoRefCount && |
| 3079 | param->hasAttr<NSConsumedAttr>() && |
| 3080 | type->isObjCRetainableType()) { |
| 3081 | llvm::Value *ptr = Builder.CreateLoad(local); |
| 3082 | auto null = |
| 3083 | llvm::ConstantPointerNull::get(cast<llvm::PointerType>(ptr->getType())); |
| 3084 | Builder.CreateStore(null, local); |
| 3085 | args.add(RValue::get(ptr), type); |
| 3086 | |
Richard Smith | d62d498 | 2016-06-14 01:13:21 +0000 | [diff] [blame] | 3087 | // For the most part, we just need to load the alloca, except that |
| 3088 | // aggregate r-values are actually pointers to temporaries. |
John McCall | 811b291 | 2016-11-18 01:08:24 +0000 | [diff] [blame] | 3089 | } else { |
Richard Smith | d62d498 | 2016-06-14 01:13:21 +0000 | [diff] [blame] | 3090 | args.add(convertTempToRValue(local, type, loc), type); |
John McCall | 811b291 | 2016-11-18 01:08:24 +0000 | [diff] [blame] | 3091 | } |
Akira Hatanaka | ccda3d2 | 2018-04-27 06:57:00 +0000 | [diff] [blame] | 3092 | |
| 3093 | // Deactivate the cleanup for the callee-destructed param that was pushed. |
| 3094 | if (hasAggregateEvaluationKind(type) && !CurFuncIsThunk && |
Simon Pilgrim | 1cd399c | 2019-10-03 11:22:48 +0000 | [diff] [blame^] | 3095 | type->castAs<RecordType>()->getDecl()->isParamDestroyedInCallee() && |
Richard Smith | 2b4fa53 | 2019-09-29 05:08:46 +0000 | [diff] [blame] | 3096 | param->needsDestruction(getContext())) { |
Akira Hatanaka | ccda3d2 | 2018-04-27 06:57:00 +0000 | [diff] [blame] | 3097 | EHScopeStack::stable_iterator cleanup = |
| 3098 | CalleeDestructedParamCleanups.lookup(cast<ParmVarDecl>(param)); |
| 3099 | assert(cleanup.isValid() && |
| 3100 | "cleanup for callee-destructed param not recorded"); |
| 3101 | // This unreachable is a temporary marker which will be removed later. |
| 3102 | llvm::Instruction *isActive = Builder.CreateUnreachable(); |
| 3103 | args.addArgCleanupDeactivation(cleanup, isActive); |
| 3104 | } |
John McCall | 23f6626 | 2010-05-26 22:34:26 +0000 | [diff] [blame] | 3105 | } |
| 3106 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3107 | static bool isProvablyNull(llvm::Value *addr) { |
| 3108 | return isa<llvm::ConstantPointerNull>(addr); |
| 3109 | } |
| 3110 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3111 | /// Emit the actual writing-back of a writeback. |
| 3112 | static void emitWriteback(CodeGenFunction &CGF, |
| 3113 | const CallArgList::Writeback &writeback) { |
John McCall | eff1884 | 2013-03-23 02:35:54 +0000 | [diff] [blame] | 3114 | const LValue &srcLV = writeback.Source; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3115 | Address srcAddr = srcLV.getAddress(); |
| 3116 | assert(!isProvablyNull(srcAddr.getPointer()) && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3117 | "shouldn't have writeback for provably null argument"); |
| 3118 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3119 | llvm::BasicBlock *contBB = nullptr; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3120 | |
| 3121 | // If the argument wasn't provably non-null, we need to null check |
| 3122 | // before doing the store. |
Nuno Lopes | 9211cee | 2017-09-09 18:25:36 +0000 | [diff] [blame] | 3123 | bool provablyNonNull = llvm::isKnownNonZero(srcAddr.getPointer(), |
| 3124 | CGF.CGM.getDataLayout()); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3125 | if (!provablyNonNull) { |
| 3126 | llvm::BasicBlock *writebackBB = CGF.createBasicBlock("icr.writeback"); |
| 3127 | contBB = CGF.createBasicBlock("icr.done"); |
| 3128 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3129 | llvm::Value *isNull = |
| 3130 | CGF.Builder.CreateIsNull(srcAddr.getPointer(), "icr.isnull"); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3131 | CGF.Builder.CreateCondBr(isNull, contBB, writebackBB); |
| 3132 | CGF.EmitBlock(writebackBB); |
| 3133 | } |
| 3134 | |
| 3135 | // Load the value to writeback. |
| 3136 | llvm::Value *value = CGF.Builder.CreateLoad(writeback.Temporary); |
| 3137 | |
| 3138 | // 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] | 3139 | value = CGF.Builder.CreateBitCast(value, srcAddr.getElementType(), |
| 3140 | "icr.writeback-cast"); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3141 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3142 | // Perform the writeback. |
John McCall | eff1884 | 2013-03-23 02:35:54 +0000 | [diff] [blame] | 3143 | |
| 3144 | // If we have a "to use" value, it's something we need to emit a use |
| 3145 | // of. This has to be carefully threaded in: if it's done after the |
| 3146 | // release it's potentially undefined behavior (and the optimizer |
| 3147 | // will ignore it), and if it happens before the retain then the |
| 3148 | // optimizer could move the release there. |
| 3149 | if (writeback.ToUse) { |
| 3150 | assert(srcLV.getObjCLifetime() == Qualifiers::OCL_Strong); |
| 3151 | |
| 3152 | // Retain the new value. No need to block-copy here: the block's |
| 3153 | // being passed up the stack. |
| 3154 | value = CGF.EmitARCRetainNonBlock(value); |
| 3155 | |
| 3156 | // Emit the intrinsic use here. |
| 3157 | CGF.EmitARCIntrinsicUse(writeback.ToUse); |
| 3158 | |
| 3159 | // Load the old value (primitively). |
Nick Lewycky | 2d84e84 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 3160 | llvm::Value *oldValue = CGF.EmitLoadOfScalar(srcLV, SourceLocation()); |
John McCall | eff1884 | 2013-03-23 02:35:54 +0000 | [diff] [blame] | 3161 | |
| 3162 | // Put the new value in place (primitively). |
| 3163 | CGF.EmitStoreOfScalar(value, srcLV, /*init*/ false); |
| 3164 | |
| 3165 | // Release the old value. |
| 3166 | CGF.EmitARCRelease(oldValue, srcLV.isARCPreciseLifetime()); |
| 3167 | |
| 3168 | // Otherwise, we can just do a normal lvalue store. |
| 3169 | } else { |
| 3170 | CGF.EmitStoreThroughLValue(RValue::get(value), srcLV); |
| 3171 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3172 | |
| 3173 | // Jump to the continuation block. |
| 3174 | if (!provablyNonNull) |
| 3175 | CGF.EmitBlock(contBB); |
| 3176 | } |
| 3177 | |
| 3178 | static void emitWritebacks(CodeGenFunction &CGF, |
| 3179 | const CallArgList &args) { |
Aaron Ballman | 36a7fa8 | 2014-03-17 17:22:27 +0000 | [diff] [blame] | 3180 | for (const auto &I : args.writebacks()) |
| 3181 | emitWriteback(CGF, I); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3182 | } |
| 3183 | |
Reid Kleckner | 23f4c4b | 2013-06-21 12:45:15 +0000 | [diff] [blame] | 3184 | static void deactivateArgCleanupsBeforeCall(CodeGenFunction &CGF, |
| 3185 | const CallArgList &CallArgs) { |
Reid Kleckner | 23f4c4b | 2013-06-21 12:45:15 +0000 | [diff] [blame] | 3186 | ArrayRef<CallArgList::CallArgCleanup> Cleanups = |
| 3187 | CallArgs.getCleanupsToDeactivate(); |
| 3188 | // Iterate in reverse to increase the likelihood of popping the cleanup. |
Pete Cooper | 57d3f14 | 2015-07-30 17:22:52 +0000 | [diff] [blame] | 3189 | for (const auto &I : llvm::reverse(Cleanups)) { |
| 3190 | CGF.DeactivateCleanupBlock(I.Cleanup, I.IsActiveIP); |
| 3191 | I.IsActiveIP->eraseFromParent(); |
Reid Kleckner | 23f4c4b | 2013-06-21 12:45:15 +0000 | [diff] [blame] | 3192 | } |
| 3193 | } |
| 3194 | |
John McCall | eff1884 | 2013-03-23 02:35:54 +0000 | [diff] [blame] | 3195 | static const Expr *maybeGetUnaryAddrOfOperand(const Expr *E) { |
| 3196 | if (const UnaryOperator *uop = dyn_cast<UnaryOperator>(E->IgnoreParens())) |
| 3197 | if (uop->getOpcode() == UO_AddrOf) |
| 3198 | return uop->getSubExpr(); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3199 | return nullptr; |
John McCall | eff1884 | 2013-03-23 02:35:54 +0000 | [diff] [blame] | 3200 | } |
| 3201 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3202 | /// Emit an argument that's being passed call-by-writeback. That is, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3203 | /// we are passing the address of an __autoreleased temporary; it |
| 3204 | /// might be copy-initialized with the current value of the given |
| 3205 | /// address, but it will definitely be copied out of after the call. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3206 | static void emitWritebackArg(CodeGenFunction &CGF, CallArgList &args, |
| 3207 | const ObjCIndirectCopyRestoreExpr *CRE) { |
John McCall | eff1884 | 2013-03-23 02:35:54 +0000 | [diff] [blame] | 3208 | LValue srcLV; |
| 3209 | |
| 3210 | // Make an optimistic effort to emit the address as an l-value. |
Eric Christopher | 2c4555a | 2015-06-19 01:52:53 +0000 | [diff] [blame] | 3211 | // This can fail if the argument expression is more complicated. |
John McCall | eff1884 | 2013-03-23 02:35:54 +0000 | [diff] [blame] | 3212 | if (const Expr *lvExpr = maybeGetUnaryAddrOfOperand(CRE->getSubExpr())) { |
| 3213 | srcLV = CGF.EmitLValue(lvExpr); |
| 3214 | |
| 3215 | // Otherwise, just emit it as a scalar. |
| 3216 | } else { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3217 | Address srcAddr = CGF.EmitPointerWithAlignment(CRE->getSubExpr()); |
John McCall | eff1884 | 2013-03-23 02:35:54 +0000 | [diff] [blame] | 3218 | |
| 3219 | QualType srcAddrType = |
| 3220 | CRE->getSubExpr()->getType()->castAs<PointerType>()->getPointeeType(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3221 | srcLV = CGF.MakeAddrLValue(srcAddr, srcAddrType); |
John McCall | eff1884 | 2013-03-23 02:35:54 +0000 | [diff] [blame] | 3222 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3223 | Address srcAddr = srcLV.getAddress(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3224 | |
| 3225 | // The dest and src types don't necessarily match in LLVM terms |
| 3226 | // because of the crazy ObjC compatibility rules. |
| 3227 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3228 | llvm::PointerType *destType = |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3229 | cast<llvm::PointerType>(CGF.ConvertType(CRE->getType())); |
| 3230 | |
| 3231 | // If the address is a constant null, just pass the appropriate null. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3232 | if (isProvablyNull(srcAddr.getPointer())) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3233 | args.add(RValue::get(llvm::ConstantPointerNull::get(destType)), |
| 3234 | CRE->getType()); |
| 3235 | return; |
| 3236 | } |
| 3237 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3238 | // Create the temporary. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3239 | Address temp = CGF.CreateTempAlloca(destType->getElementType(), |
| 3240 | CGF.getPointerAlign(), |
| 3241 | "icr.temp"); |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 3242 | // Loading an l-value can introduce a cleanup if the l-value is __weak, |
| 3243 | // and that cleanup will be conditional if we can't prove that the l-value |
| 3244 | // isn't null, so we need to register a dominating point so that the cleanups |
| 3245 | // system will make valid IR. |
| 3246 | CodeGenFunction::ConditionalEvaluation condEval(CGF); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3247 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3248 | // Zero-initialize it if we're not doing a copy-initialization. |
| 3249 | bool shouldCopy = CRE->shouldCopy(); |
| 3250 | if (!shouldCopy) { |
| 3251 | llvm::Value *null = |
| 3252 | llvm::ConstantPointerNull::get( |
| 3253 | cast<llvm::PointerType>(destType->getElementType())); |
| 3254 | CGF.Builder.CreateStore(null, temp); |
| 3255 | } |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3256 | |
| 3257 | llvm::BasicBlock *contBB = nullptr; |
| 3258 | llvm::BasicBlock *originBB = nullptr; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3259 | |
| 3260 | // If the address is *not* known to be non-null, we need to switch. |
| 3261 | llvm::Value *finalArgument; |
| 3262 | |
Nuno Lopes | 9211cee | 2017-09-09 18:25:36 +0000 | [diff] [blame] | 3263 | bool provablyNonNull = llvm::isKnownNonZero(srcAddr.getPointer(), |
| 3264 | CGF.CGM.getDataLayout()); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3265 | if (provablyNonNull) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3266 | finalArgument = temp.getPointer(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3267 | } else { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3268 | llvm::Value *isNull = |
| 3269 | CGF.Builder.CreateIsNull(srcAddr.getPointer(), "icr.isnull"); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3270 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3271 | finalArgument = CGF.Builder.CreateSelect(isNull, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3272 | llvm::ConstantPointerNull::get(destType), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3273 | temp.getPointer(), "icr.argument"); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3274 | |
| 3275 | // If we need to copy, then the load has to be conditional, which |
| 3276 | // means we need control flow. |
| 3277 | if (shouldCopy) { |
John McCall | eff1884 | 2013-03-23 02:35:54 +0000 | [diff] [blame] | 3278 | originBB = CGF.Builder.GetInsertBlock(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3279 | contBB = CGF.createBasicBlock("icr.cont"); |
| 3280 | llvm::BasicBlock *copyBB = CGF.createBasicBlock("icr.copy"); |
| 3281 | CGF.Builder.CreateCondBr(isNull, contBB, copyBB); |
| 3282 | CGF.EmitBlock(copyBB); |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 3283 | condEval.begin(CGF); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3284 | } |
| 3285 | } |
| 3286 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3287 | llvm::Value *valueToUse = nullptr; |
John McCall | eff1884 | 2013-03-23 02:35:54 +0000 | [diff] [blame] | 3288 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3289 | // Perform a copy if necessary. |
| 3290 | if (shouldCopy) { |
Nick Lewycky | 2d84e84 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 3291 | RValue srcRV = CGF.EmitLoadOfLValue(srcLV, SourceLocation()); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3292 | assert(srcRV.isScalar()); |
| 3293 | |
| 3294 | llvm::Value *src = srcRV.getScalarVal(); |
| 3295 | src = CGF.Builder.CreateBitCast(src, destType->getElementType(), |
| 3296 | "icr.cast"); |
| 3297 | |
| 3298 | // Use an ordinary store, not a store-to-lvalue. |
| 3299 | CGF.Builder.CreateStore(src, temp); |
John McCall | eff1884 | 2013-03-23 02:35:54 +0000 | [diff] [blame] | 3300 | |
| 3301 | // If optimization is enabled, and the value was held in a |
| 3302 | // __strong variable, we need to tell the optimizer that this |
| 3303 | // value has to stay alive until we're doing the store back. |
| 3304 | // This is because the temporary is effectively unretained, |
| 3305 | // and so otherwise we can violate the high-level semantics. |
| 3306 | if (CGF.CGM.getCodeGenOpts().OptimizationLevel != 0 && |
| 3307 | srcLV.getObjCLifetime() == Qualifiers::OCL_Strong) { |
| 3308 | valueToUse = src; |
| 3309 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3310 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3311 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3312 | // Finish the control flow if we needed it. |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 3313 | if (shouldCopy && !provablyNonNull) { |
John McCall | eff1884 | 2013-03-23 02:35:54 +0000 | [diff] [blame] | 3314 | llvm::BasicBlock *copyBB = CGF.Builder.GetInsertBlock(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3315 | CGF.EmitBlock(contBB); |
John McCall | eff1884 | 2013-03-23 02:35:54 +0000 | [diff] [blame] | 3316 | |
| 3317 | // Make a phi for the value to intrinsically use. |
| 3318 | if (valueToUse) { |
| 3319 | llvm::PHINode *phiToUse = CGF.Builder.CreatePHI(valueToUse->getType(), 2, |
| 3320 | "icr.to-use"); |
| 3321 | phiToUse->addIncoming(valueToUse, copyBB); |
| 3322 | phiToUse->addIncoming(llvm::UndefValue::get(valueToUse->getType()), |
| 3323 | originBB); |
| 3324 | valueToUse = phiToUse; |
| 3325 | } |
| 3326 | |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 3327 | condEval.end(CGF); |
| 3328 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3329 | |
John McCall | eff1884 | 2013-03-23 02:35:54 +0000 | [diff] [blame] | 3330 | args.addWriteback(srcLV, temp, valueToUse); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3331 | args.add(RValue::get(finalArgument), CRE->getType()); |
| 3332 | } |
| 3333 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 3334 | void CallArgList::allocateArgumentMemory(CodeGenFunction &CGF) { |
Richard Smith | 762672a | 2016-09-28 19:09:10 +0000 | [diff] [blame] | 3335 | assert(!StackBase); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 3336 | |
| 3337 | // Save the stack. |
| 3338 | llvm::Function *F = CGF.CGM.getIntrinsic(llvm::Intrinsic::stacksave); |
David Blaikie | 43f9bb7 | 2015-05-18 22:14:03 +0000 | [diff] [blame] | 3339 | StackBase = CGF.Builder.CreateCall(F, {}, "inalloca.save"); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 3340 | } |
| 3341 | |
Nico Weber | 8cdb3f9 | 2015-08-25 18:43:32 +0000 | [diff] [blame] | 3342 | void CallArgList::freeArgumentMemory(CodeGenFunction &CGF) const { |
| 3343 | if (StackBase) { |
Reid Kleckner | 7c2f9e8 | 2015-10-08 00:17:45 +0000 | [diff] [blame] | 3344 | // Restore the stack after the call. |
James Y Knight | 8799cae | 2019-02-03 21:53:49 +0000 | [diff] [blame] | 3345 | llvm::Function *F = CGF.CGM.getIntrinsic(llvm::Intrinsic::stackrestore); |
Nico Weber | 8cdb3f9 | 2015-08-25 18:43:32 +0000 | [diff] [blame] | 3346 | CGF.Builder.CreateCall(F, StackBase); |
| 3347 | } |
| 3348 | } |
| 3349 | |
Nuno Lopes | 1ba2d78 | 2015-05-30 16:11:40 +0000 | [diff] [blame] | 3350 | void CodeGenFunction::EmitNonNullArgCheck(RValue RV, QualType ArgType, |
| 3351 | SourceLocation ArgLoc, |
Vedant Kumar | ed00ea0 | 2017-03-06 05:28:22 +0000 | [diff] [blame] | 3352 | AbstractCallee AC, |
Nuno Lopes | 1ba2d78 | 2015-05-30 16:11:40 +0000 | [diff] [blame] | 3353 | unsigned ParmNum) { |
Vedant Kumar | 42c17ec | 2017-03-14 01:56:34 +0000 | [diff] [blame] | 3354 | if (!AC.getDecl() || !(SanOpts.has(SanitizerKind::NonnullAttribute) || |
| 3355 | SanOpts.has(SanitizerKind::NullabilityArg))) |
Alexey Samsonov | 8e1162c | 2014-09-08 17:22:45 +0000 | [diff] [blame] | 3356 | return; |
Vedant Kumar | 42c17ec | 2017-03-14 01:56:34 +0000 | [diff] [blame] | 3357 | |
| 3358 | // The param decl may be missing in a variadic function. |
Vedant Kumar | ed00ea0 | 2017-03-06 05:28:22 +0000 | [diff] [blame] | 3359 | auto PVD = ParmNum < AC.getNumParams() ? AC.getParamDecl(ParmNum) : nullptr; |
Alexey Samsonov | 8e1162c | 2014-09-08 17:22:45 +0000 | [diff] [blame] | 3360 | unsigned ArgNo = PVD ? PVD->getFunctionScopeIndex() : ParmNum; |
Vedant Kumar | 42c17ec | 2017-03-14 01:56:34 +0000 | [diff] [blame] | 3361 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3362 | // Prefer the nonnull attribute if it's present. |
Vedant Kumar | 42c17ec | 2017-03-14 01:56:34 +0000 | [diff] [blame] | 3363 | const NonNullAttr *NNAttr = nullptr; |
| 3364 | if (SanOpts.has(SanitizerKind::NonnullAttribute)) |
| 3365 | NNAttr = getNonNullAttr(AC.getDecl(), PVD, ArgType, ArgNo); |
| 3366 | |
| 3367 | bool CanCheckNullability = false; |
| 3368 | if (SanOpts.has(SanitizerKind::NullabilityArg) && !NNAttr && PVD) { |
| 3369 | auto Nullability = PVD->getType()->getNullability(getContext()); |
| 3370 | CanCheckNullability = Nullability && |
| 3371 | *Nullability == NullabilityKind::NonNull && |
| 3372 | PVD->getTypeSourceInfo(); |
| 3373 | } |
| 3374 | |
| 3375 | if (!NNAttr && !CanCheckNullability) |
Alexey Samsonov | 8e1162c | 2014-09-08 17:22:45 +0000 | [diff] [blame] | 3376 | return; |
Vedant Kumar | 42c17ec | 2017-03-14 01:56:34 +0000 | [diff] [blame] | 3377 | |
| 3378 | SourceLocation AttrLoc; |
| 3379 | SanitizerMask CheckKind; |
Vedant Kumar | 2b9f48a | 2017-03-14 16:48:29 +0000 | [diff] [blame] | 3380 | SanitizerHandler Handler; |
Vedant Kumar | 42c17ec | 2017-03-14 01:56:34 +0000 | [diff] [blame] | 3381 | if (NNAttr) { |
| 3382 | AttrLoc = NNAttr->getLocation(); |
| 3383 | CheckKind = SanitizerKind::NonnullAttribute; |
Vedant Kumar | 2b9f48a | 2017-03-14 16:48:29 +0000 | [diff] [blame] | 3384 | Handler = SanitizerHandler::NonnullArg; |
Vedant Kumar | 42c17ec | 2017-03-14 01:56:34 +0000 | [diff] [blame] | 3385 | } else { |
| 3386 | AttrLoc = PVD->getTypeSourceInfo()->getTypeLoc().findNullabilityLoc(); |
| 3387 | CheckKind = SanitizerKind::NullabilityArg; |
Vedant Kumar | 2b9f48a | 2017-03-14 16:48:29 +0000 | [diff] [blame] | 3388 | Handler = SanitizerHandler::NullabilityArg; |
Vedant Kumar | 42c17ec | 2017-03-14 01:56:34 +0000 | [diff] [blame] | 3389 | } |
| 3390 | |
Nuno Lopes | 1ba2d78 | 2015-05-30 16:11:40 +0000 | [diff] [blame] | 3391 | SanitizerScope SanScope(this); |
Alexey Samsonov | 8e1162c | 2014-09-08 17:22:45 +0000 | [diff] [blame] | 3392 | assert(RV.isScalar()); |
| 3393 | llvm::Value *V = RV.getScalarVal(); |
| 3394 | llvm::Value *Cond = |
Nuno Lopes | 1ba2d78 | 2015-05-30 16:11:40 +0000 | [diff] [blame] | 3395 | Builder.CreateICmpNE(V, llvm::Constant::getNullValue(V->getType())); |
Alexey Samsonov | 8e1162c | 2014-09-08 17:22:45 +0000 | [diff] [blame] | 3396 | llvm::Constant *StaticData[] = { |
Vedant Kumar | 42c17ec | 2017-03-14 01:56:34 +0000 | [diff] [blame] | 3397 | EmitCheckSourceLocation(ArgLoc), EmitCheckSourceLocation(AttrLoc), |
Nuno Lopes | 1ba2d78 | 2015-05-30 16:11:40 +0000 | [diff] [blame] | 3398 | llvm::ConstantInt::get(Int32Ty, ArgNo + 1), |
Alexey Samsonov | 8e1162c | 2014-09-08 17:22:45 +0000 | [diff] [blame] | 3399 | }; |
Vedant Kumar | 2b9f48a | 2017-03-14 16:48:29 +0000 | [diff] [blame] | 3400 | EmitCheck(std::make_pair(Cond, CheckKind), Handler, StaticData, None); |
Alexey Samsonov | 8e1162c | 2014-09-08 17:22:45 +0000 | [diff] [blame] | 3401 | } |
| 3402 | |
David Blaikie | f05779e | 2015-07-21 18:37:18 +0000 | [diff] [blame] | 3403 | void CodeGenFunction::EmitCallArgs( |
| 3404 | CallArgList &Args, ArrayRef<QualType> ArgTypes, |
| 3405 | llvm::iterator_range<CallExpr::const_arg_iterator> ArgRange, |
Vedant Kumar | ed00ea0 | 2017-03-06 05:28:22 +0000 | [diff] [blame] | 3406 | AbstractCallee AC, unsigned ParamsToSkip, EvaluationOrder Order) { |
David Blaikie | f05779e | 2015-07-21 18:37:18 +0000 | [diff] [blame] | 3407 | assert((int)ArgTypes.size() == (ArgRange.end() - ArgRange.begin())); |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 3408 | |
Reid Kleckner | 739756c | 2013-12-04 19:23:12 +0000 | [diff] [blame] | 3409 | // 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] | 3410 | // because arguments are destroyed left to right in the callee. As a special |
| 3411 | // case, there are certain language constructs that require left-to-right |
| 3412 | // evaluation, and in those cases we consider the evaluation order requirement |
| 3413 | // to trump the "destruction order is reverse construction order" guarantee. |
| 3414 | bool LeftToRight = |
| 3415 | CGM.getTarget().getCXXABI().areArgsDestroyedLeftToRightInCallee() |
| 3416 | ? Order == EvaluationOrder::ForceLeftToRight |
| 3417 | : Order != EvaluationOrder::ForceRightToLeft; |
| 3418 | |
George Burgess IV | 0d6592a | 2017-02-23 05:59:56 +0000 | [diff] [blame] | 3419 | auto MaybeEmitImplicitObjectSize = [&](unsigned I, const Expr *Arg, |
| 3420 | RValue EmittedArg) { |
Vedant Kumar | ed00ea0 | 2017-03-06 05:28:22 +0000 | [diff] [blame] | 3421 | if (!AC.hasFunctionDecl() || I >= AC.getNumParams()) |
George Burgess IV | 0d6592a | 2017-02-23 05:59:56 +0000 | [diff] [blame] | 3422 | return; |
Vedant Kumar | ed00ea0 | 2017-03-06 05:28:22 +0000 | [diff] [blame] | 3423 | auto *PS = AC.getParamDecl(I)->getAttr<PassObjectSizeAttr>(); |
George Burgess IV | 0d6592a | 2017-02-23 05:59:56 +0000 | [diff] [blame] | 3424 | if (PS == nullptr) |
| 3425 | return; |
| 3426 | |
| 3427 | const auto &Context = getContext(); |
| 3428 | auto SizeTy = Context.getSizeType(); |
| 3429 | auto T = Builder.getIntNTy(Context.getTypeSize(SizeTy)); |
| 3430 | assert(EmittedArg.getScalarVal() && "We emitted nothing for the arg?"); |
| 3431 | llvm::Value *V = evaluateOrEmitBuiltinObjectSize(Arg, PS->getType(), T, |
Erik Pilkington | 9c3b588 | 2019-01-30 20:34:53 +0000 | [diff] [blame] | 3432 | EmittedArg.getScalarVal(), |
Erik Pilkington | 02d5fb1 | 2019-03-19 20:44:18 +0000 | [diff] [blame] | 3433 | PS->isDynamic()); |
George Burgess IV | 0d6592a | 2017-02-23 05:59:56 +0000 | [diff] [blame] | 3434 | Args.add(RValue::get(V), SizeTy); |
| 3435 | // If we're emitting args in reverse, be sure to do so with |
| 3436 | // pass_object_size, as well. |
| 3437 | if (!LeftToRight) |
| 3438 | std::swap(Args.back(), *(&Args.back() - 1)); |
| 3439 | }; |
| 3440 | |
Richard Smith | a560ccf | 2016-09-29 21:30:12 +0000 | [diff] [blame] | 3441 | // Insert a stack save if we're going to need any inalloca args. |
| 3442 | bool HasInAllocaArgs = false; |
| 3443 | if (CGM.getTarget().getCXXABI().isMicrosoft()) { |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 3444 | for (ArrayRef<QualType>::iterator I = ArgTypes.begin(), E = ArgTypes.end(); |
| 3445 | I != E && !HasInAllocaArgs; ++I) |
| 3446 | HasInAllocaArgs = isInAllocaArgument(CGM.getCXXABI(), *I); |
| 3447 | if (HasInAllocaArgs) { |
| 3448 | assert(getTarget().getTriple().getArch() == llvm::Triple::x86); |
| 3449 | Args.allocateArgumentMemory(*this); |
| 3450 | } |
Richard Smith | a560ccf | 2016-09-29 21:30:12 +0000 | [diff] [blame] | 3451 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 3452 | |
Richard Smith | a560ccf | 2016-09-29 21:30:12 +0000 | [diff] [blame] | 3453 | // Evaluate each argument in the appropriate order. |
| 3454 | size_t CallArgsStart = Args.size(); |
| 3455 | for (unsigned I = 0, E = ArgTypes.size(); I != E; ++I) { |
| 3456 | unsigned Idx = LeftToRight ? I : E - I - 1; |
| 3457 | CallExpr::const_arg_iterator Arg = ArgRange.begin() + Idx; |
George Burgess IV | 0d6592a | 2017-02-23 05:59:56 +0000 | [diff] [blame] | 3458 | unsigned InitialArgSize = Args.size(); |
Akira Hatanaka | 46dd7dbc | 2017-06-28 00:42:48 +0000 | [diff] [blame] | 3459 | // If *Arg is an ObjCIndirectCopyRestoreExpr, check that either the types of |
| 3460 | // the argument and parameter match or the objc method is parameterized. |
| 3461 | assert((!isa<ObjCIndirectCopyRestoreExpr>(*Arg) || |
| 3462 | getContext().hasSameUnqualifiedType((*Arg)->getType(), |
| 3463 | ArgTypes[Idx]) || |
| 3464 | (isa<ObjCMethodDecl>(AC.getDecl()) && |
| 3465 | isObjCMethodWithTypeParams(cast<ObjCMethodDecl>(AC.getDecl())))) && |
| 3466 | "Argument and parameter types don't match"); |
Richard Smith | a560ccf | 2016-09-29 21:30:12 +0000 | [diff] [blame] | 3467 | EmitCallArg(Args, *Arg, ArgTypes[Idx]); |
George Burgess IV | 0d6592a | 2017-02-23 05:59:56 +0000 | [diff] [blame] | 3468 | // In particular, we depend on it being the last arg in Args, and the |
| 3469 | // objectsize bits depend on there only being one arg if !LeftToRight. |
| 3470 | assert(InitialArgSize + 1 == Args.size() && |
| 3471 | "The code below depends on only adding one arg per EmitCallArg"); |
| 3472 | (void)InitialArgSize; |
Yaxun Liu | 5b330e8 | 2018-03-15 15:25:19 +0000 | [diff] [blame] | 3473 | // Since pointer argument are never emitted as LValue, it is safe to emit |
| 3474 | // non-null argument check for r-value only. |
| 3475 | if (!Args.back().hasLValue()) { |
| 3476 | RValue RVArg = Args.back().getKnownRValue(); |
| 3477 | EmitNonNullArgCheck(RVArg, ArgTypes[Idx], (*Arg)->getExprLoc(), AC, |
| 3478 | ParamsToSkip + Idx); |
| 3479 | // @llvm.objectsize should never have side-effects and shouldn't need |
| 3480 | // destruction/cleanups, so we can safely "emit" it after its arg, |
| 3481 | // regardless of right-to-leftness |
| 3482 | MaybeEmitImplicitObjectSize(Idx, *Arg, RVArg); |
| 3483 | } |
Richard Smith | a560ccf | 2016-09-29 21:30:12 +0000 | [diff] [blame] | 3484 | } |
Reid Kleckner | 739756c | 2013-12-04 19:23:12 +0000 | [diff] [blame] | 3485 | |
Richard Smith | a560ccf | 2016-09-29 21:30:12 +0000 | [diff] [blame] | 3486 | if (!LeftToRight) { |
Reid Kleckner | 739756c | 2013-12-04 19:23:12 +0000 | [diff] [blame] | 3487 | // Un-reverse the arguments we just evaluated so they match up with the LLVM |
| 3488 | // IR function. |
| 3489 | std::reverse(Args.begin() + CallArgsStart, Args.end()); |
Reid Kleckner | 739756c | 2013-12-04 19:23:12 +0000 | [diff] [blame] | 3490 | } |
| 3491 | } |
| 3492 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 3493 | namespace { |
| 3494 | |
David Blaikie | 7e70d68 | 2015-08-18 22:40:54 +0000 | [diff] [blame] | 3495 | struct DestroyUnpassedArg final : EHScopeStack::Cleanup { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3496 | DestroyUnpassedArg(Address Addr, QualType Ty) |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 3497 | : Addr(Addr), Ty(Ty) {} |
| 3498 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3499 | Address Addr; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 3500 | QualType Ty; |
| 3501 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3502 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
Akira Hatanaka | 7275da0 | 2018-02-28 07:15:55 +0000 | [diff] [blame] | 3503 | QualType::DestructionKind DtorKind = Ty.isDestructedType(); |
| 3504 | if (DtorKind == QualType::DK_cxx_destructor) { |
| 3505 | const CXXDestructorDecl *Dtor = Ty->getAsCXXRecordDecl()->getDestructor(); |
| 3506 | assert(!Dtor->isTrivial()); |
| 3507 | CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete, /*for vbase*/ false, |
Marco Antognini | 8855963 | 2019-07-22 09:39:13 +0000 | [diff] [blame] | 3508 | /*Delegating=*/false, Addr, Ty); |
Akira Hatanaka | 7275da0 | 2018-02-28 07:15:55 +0000 | [diff] [blame] | 3509 | } else { |
| 3510 | CGF.callCStructDestructor(CGF.MakeAddrLValue(Addr, Ty)); |
| 3511 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 3512 | } |
| 3513 | }; |
| 3514 | |
David Blaikie | 38b2591 | 2015-02-09 19:13:51 +0000 | [diff] [blame] | 3515 | struct DisableDebugLocationUpdates { |
| 3516 | CodeGenFunction &CGF; |
| 3517 | bool disabledDebugInfo; |
| 3518 | DisableDebugLocationUpdates(CodeGenFunction &CGF, const Expr *E) : CGF(CGF) { |
| 3519 | if ((disabledDebugInfo = isa<CXXDefaultArgExpr>(E) && CGF.getDebugInfo())) |
| 3520 | CGF.disableDebugInfo(); |
| 3521 | } |
| 3522 | ~DisableDebugLocationUpdates() { |
| 3523 | if (disabledDebugInfo) |
| 3524 | CGF.enableDebugInfo(); |
| 3525 | } |
| 3526 | }; |
| 3527 | |
Benjamin Kramer | 5b4296a | 2015-10-28 17:16:26 +0000 | [diff] [blame] | 3528 | } // end anonymous namespace |
| 3529 | |
Yaxun Liu | 5b330e8 | 2018-03-15 15:25:19 +0000 | [diff] [blame] | 3530 | RValue CallArg::getRValue(CodeGenFunction &CGF) const { |
| 3531 | if (!HasLV) |
| 3532 | return RV; |
| 3533 | LValue Copy = CGF.MakeAddrLValue(CGF.CreateMemTemp(Ty), Ty); |
Richard Smith | e78fac5 | 2018-04-05 20:52:58 +0000 | [diff] [blame] | 3534 | CGF.EmitAggregateCopy(Copy, LV, Ty, AggValueSlot::DoesNotOverlap, |
| 3535 | LV.isVolatile()); |
Yaxun Liu | 5b330e8 | 2018-03-15 15:25:19 +0000 | [diff] [blame] | 3536 | IsUsed = true; |
| 3537 | return RValue::getAggregate(Copy.getAddress()); |
| 3538 | } |
| 3539 | |
| 3540 | void CallArg::copyInto(CodeGenFunction &CGF, Address Addr) const { |
| 3541 | LValue Dst = CGF.MakeAddrLValue(Addr, Ty); |
| 3542 | if (!HasLV && RV.isScalar()) |
Rui Ueyama | 49a3ad2 | 2019-07-16 04:46:31 +0000 | [diff] [blame] | 3543 | CGF.EmitStoreOfScalar(RV.getScalarVal(), Dst, /*isInit=*/true); |
Yaxun Liu | 5b330e8 | 2018-03-15 15:25:19 +0000 | [diff] [blame] | 3544 | else if (!HasLV && RV.isComplex()) |
| 3545 | CGF.EmitStoreOfComplex(RV.getComplexVal(), Dst, /*init=*/true); |
| 3546 | else { |
| 3547 | auto Addr = HasLV ? LV.getAddress() : RV.getAggregateAddress(); |
| 3548 | LValue SrcLV = CGF.MakeAddrLValue(Addr, Ty); |
Richard Smith | e78fac5 | 2018-04-05 20:52:58 +0000 | [diff] [blame] | 3549 | // We assume that call args are never copied into subobjects. |
| 3550 | CGF.EmitAggregateCopy(Dst, SrcLV, Ty, AggValueSlot::DoesNotOverlap, |
Yaxun Liu | 5b330e8 | 2018-03-15 15:25:19 +0000 | [diff] [blame] | 3551 | HasLV ? LV.isVolatileQualified() |
| 3552 | : RV.isVolatileQualified()); |
| 3553 | } |
| 3554 | IsUsed = true; |
| 3555 | } |
| 3556 | |
John McCall | 32ea969 | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 3557 | void CodeGenFunction::EmitCallArg(CallArgList &args, const Expr *E, |
| 3558 | QualType type) { |
David Blaikie | 38b2591 | 2015-02-09 19:13:51 +0000 | [diff] [blame] | 3559 | DisableDebugLocationUpdates Dis(*this, E); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3560 | if (const ObjCIndirectCopyRestoreExpr *CRE |
| 3561 | = dyn_cast<ObjCIndirectCopyRestoreExpr>(E)) { |
Richard Smith | 9c6890a | 2012-11-01 22:30:59 +0000 | [diff] [blame] | 3562 | assert(getLangOpts().ObjCAutoRefCount); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3563 | return emitWritebackArg(*this, args, CRE); |
| 3564 | } |
| 3565 | |
John McCall | 0a76c0c | 2011-08-26 18:42:59 +0000 | [diff] [blame] | 3566 | assert(type->isReferenceType() == E->isGLValue() && |
| 3567 | "reference binding to unmaterialized r-value!"); |
| 3568 | |
John McCall | 17054bd6 | 2011-08-26 21:08:13 +0000 | [diff] [blame] | 3569 | if (E->isGLValue()) { |
| 3570 | assert(E->getObjectKind() == OK_Ordinary); |
Richard Smith | a1c9d4d | 2013-06-12 23:38:09 +0000 | [diff] [blame] | 3571 | return args.add(EmitReferenceBindingToExpr(E), type); |
John McCall | 17054bd6 | 2011-08-26 21:08:13 +0000 | [diff] [blame] | 3572 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3573 | |
Reid Kleckner | 23f4c4b | 2013-06-21 12:45:15 +0000 | [diff] [blame] | 3574 | bool HasAggregateEvalKind = hasAggregateEvaluationKind(type); |
| 3575 | |
| 3576 | // In the Microsoft C++ ABI, aggregate arguments are destructed by the callee. |
| 3577 | // However, we still have to push an EH-only cleanup in case we unwind before |
| 3578 | // we make it to the call. |
Akira Hatanaka | 8528297 | 2018-05-15 21:00:30 +0000 | [diff] [blame] | 3579 | if (HasAggregateEvalKind && |
Simon Pilgrim | 1cd399c | 2019-10-03 11:22:48 +0000 | [diff] [blame^] | 3580 | type->castAs<RecordType>()->getDecl()->isParamDestroyedInCallee()) { |
Reid Kleckner | ac64060 | 2014-05-01 03:07:18 +0000 | [diff] [blame] | 3581 | // If we're using inalloca, use the argument memory. Otherwise, use a |
Reid Kleckner | e39ee21 | 2014-05-03 00:33:28 +0000 | [diff] [blame] | 3582 | // temporary. |
Reid Kleckner | ac64060 | 2014-05-01 03:07:18 +0000 | [diff] [blame] | 3583 | AggValueSlot Slot; |
| 3584 | if (args.isUsingInAlloca()) |
| 3585 | Slot = createPlaceholderSlot(*this, type); |
| 3586 | else |
| 3587 | Slot = CreateAggTemp(type, "agg.tmp"); |
Reid Kleckner | e39ee21 | 2014-05-03 00:33:28 +0000 | [diff] [blame] | 3588 | |
Akira Hatanaka | 4ce0e5a | 2018-04-18 23:33:15 +0000 | [diff] [blame] | 3589 | bool DestroyedInCallee = true, NeedsEHCleanup = true; |
| 3590 | if (const auto *RD = type->getAsCXXRecordDecl()) |
| 3591 | DestroyedInCallee = RD->hasNonTrivialDestructor(); |
| 3592 | else |
| 3593 | NeedsEHCleanup = needsEHCleanup(type.isDestructedType()); |
| 3594 | |
| 3595 | if (DestroyedInCallee) |
| 3596 | Slot.setExternallyDestructed(); |
Reid Kleckner | e39ee21 | 2014-05-03 00:33:28 +0000 | [diff] [blame] | 3597 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 3598 | EmitAggExpr(E, Slot); |
| 3599 | RValue RV = Slot.asRValue(); |
| 3600 | args.add(RV, type); |
Reid Kleckner | 23f4c4b | 2013-06-21 12:45:15 +0000 | [diff] [blame] | 3601 | |
Akira Hatanaka | 4ce0e5a | 2018-04-18 23:33:15 +0000 | [diff] [blame] | 3602 | if (DestroyedInCallee && NeedsEHCleanup) { |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 3603 | // Create a no-op GEP between the placeholder and the cleanup so we can |
| 3604 | // RAUW it successfully. It also serves as a marker of the first |
| 3605 | // instruction where the cleanup is active. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3606 | pushFullExprCleanup<DestroyUnpassedArg>(EHCleanup, Slot.getAddress(), |
| 3607 | type); |
Reid Kleckner | 23f4c4b | 2013-06-21 12:45:15 +0000 | [diff] [blame] | 3608 | // This unreachable is a temporary marker which will be removed later. |
| 3609 | llvm::Instruction *IsActive = Builder.CreateUnreachable(); |
| 3610 | args.addArgCleanupDeactivation(EHStack.getInnermostEHScope(), IsActive); |
Reid Kleckner | 23f4c4b | 2013-06-21 12:45:15 +0000 | [diff] [blame] | 3611 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 3612 | return; |
Reid Kleckner | 23f4c4b | 2013-06-21 12:45:15 +0000 | [diff] [blame] | 3613 | } |
| 3614 | |
| 3615 | if (HasAggregateEvalKind && isa<ImplicitCastExpr>(E) && |
Eli Friedman | df96819 | 2011-05-26 00:10:27 +0000 | [diff] [blame] | 3616 | cast<CastExpr>(E)->getCastKind() == CK_LValueToRValue) { |
| 3617 | LValue L = EmitLValue(cast<CastExpr>(E)->getSubExpr()); |
| 3618 | assert(L.isSimple()); |
Yaxun Liu | 5b330e8 | 2018-03-15 15:25:19 +0000 | [diff] [blame] | 3619 | args.addUncopiedAggregate(L, type); |
Eli Friedman | df96819 | 2011-05-26 00:10:27 +0000 | [diff] [blame] | 3620 | return; |
| 3621 | } |
| 3622 | |
John McCall | 32ea969 | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 3623 | args.add(EmitAnyExprToTemp(E), type); |
Anders Carlsson | 60ce3fe | 2009-04-08 20:47:54 +0000 | [diff] [blame] | 3624 | } |
| 3625 | |
Reid Kleckner | 79b0fd7 | 2014-10-10 00:05:45 +0000 | [diff] [blame] | 3626 | QualType CodeGenFunction::getVarArgType(const Expr *Arg) { |
| 3627 | // System headers on Windows define NULL to 0 instead of 0LL on Win64. MSVC |
| 3628 | // implicitly widens null pointer constants that are arguments to varargs |
| 3629 | // functions to pointer-sized ints. |
| 3630 | if (!getTarget().getTriple().isOSWindows()) |
| 3631 | return Arg->getType(); |
| 3632 | |
| 3633 | if (Arg->getType()->isIntegerType() && |
| 3634 | getContext().getTypeSize(Arg->getType()) < |
| 3635 | getContext().getTargetInfo().getPointerWidth(0) && |
| 3636 | Arg->isNullPointerConstant(getContext(), |
| 3637 | Expr::NPC_ValueDependentIsNotNull)) { |
| 3638 | return getContext().getIntPtrType(); |
| 3639 | } |
| 3640 | |
| 3641 | return Arg->getType(); |
| 3642 | } |
| 3643 | |
Dan Gohman | 515a60d | 2012-02-16 00:57:37 +0000 | [diff] [blame] | 3644 | // In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC |
| 3645 | // optimizer it can aggressively ignore unwind edges. |
| 3646 | void |
| 3647 | CodeGenFunction::AddObjCARCExceptionMetadata(llvm::Instruction *Inst) { |
| 3648 | if (CGM.getCodeGenOpts().OptimizationLevel != 0 && |
| 3649 | !CGM.getCodeGenOpts().ObjCAutoRefCountExceptions) |
| 3650 | Inst->setMetadata("clang.arc.no_objc_arc_exceptions", |
| 3651 | CGM.getNoObjCARCExceptionsMetadata()); |
| 3652 | } |
| 3653 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3654 | /// Emits a call to the given no-arguments nounwind runtime function. |
| 3655 | llvm::CallInst * |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 3656 | CodeGenFunction::EmitNounwindRuntimeCall(llvm::FunctionCallee callee, |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3657 | const llvm::Twine &name) { |
Craig Topper | 5fc8fc2 | 2014-08-27 06:28:36 +0000 | [diff] [blame] | 3658 | return EmitNounwindRuntimeCall(callee, None, name); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3659 | } |
| 3660 | |
| 3661 | /// Emits a call to the given nounwind runtime function. |
| 3662 | llvm::CallInst * |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 3663 | CodeGenFunction::EmitNounwindRuntimeCall(llvm::FunctionCallee callee, |
| 3664 | ArrayRef<llvm::Value *> args, |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3665 | const llvm::Twine &name) { |
| 3666 | llvm::CallInst *call = EmitRuntimeCall(callee, args, name); |
| 3667 | call->setDoesNotThrow(); |
| 3668 | return call; |
| 3669 | } |
| 3670 | |
| 3671 | /// Emits a simple call (never an invoke) to the given no-arguments |
| 3672 | /// runtime function. |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 3673 | llvm::CallInst *CodeGenFunction::EmitRuntimeCall(llvm::FunctionCallee callee, |
| 3674 | const llvm::Twine &name) { |
Craig Topper | 5fc8fc2 | 2014-08-27 06:28:36 +0000 | [diff] [blame] | 3675 | return EmitRuntimeCall(callee, None, name); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3676 | } |
| 3677 | |
David Majnemer | 0b17d44 | 2015-12-15 21:27:59 +0000 | [diff] [blame] | 3678 | // Calls which may throw must have operand bundles indicating which funclet |
| 3679 | // they are nested within. |
Reid Kleckner | b75a3f0 | 2018-02-09 00:16:41 +0000 | [diff] [blame] | 3680 | SmallVector<llvm::OperandBundleDef, 1> |
| 3681 | CodeGenFunction::getBundlesForFunclet(llvm::Value *Callee) { |
| 3682 | SmallVector<llvm::OperandBundleDef, 1> BundleList; |
Sanjay Patel | 846b63b | 2016-01-18 22:15:33 +0000 | [diff] [blame] | 3683 | // There is no need for a funclet operand bundle if we aren't inside a |
| 3684 | // funclet. |
David Majnemer | 0b17d44 | 2015-12-15 21:27:59 +0000 | [diff] [blame] | 3685 | if (!CurrentFuncletPad) |
Reid Kleckner | b75a3f0 | 2018-02-09 00:16:41 +0000 | [diff] [blame] | 3686 | return BundleList; |
David Majnemer | 0b17d44 | 2015-12-15 21:27:59 +0000 | [diff] [blame] | 3687 | |
| 3688 | // Skip intrinsics which cannot throw. |
| 3689 | auto *CalleeFn = dyn_cast<llvm::Function>(Callee->stripPointerCasts()); |
| 3690 | if (CalleeFn && CalleeFn->isIntrinsic() && CalleeFn->doesNotThrow()) |
Reid Kleckner | b75a3f0 | 2018-02-09 00:16:41 +0000 | [diff] [blame] | 3691 | return BundleList; |
David Majnemer | 0b17d44 | 2015-12-15 21:27:59 +0000 | [diff] [blame] | 3692 | |
| 3693 | BundleList.emplace_back("funclet", CurrentFuncletPad); |
Reid Kleckner | b75a3f0 | 2018-02-09 00:16:41 +0000 | [diff] [blame] | 3694 | return BundleList; |
David Majnemer | 0b17d44 | 2015-12-15 21:27:59 +0000 | [diff] [blame] | 3695 | } |
| 3696 | |
David Majnemer | 971d31b | 2016-02-24 17:02:45 +0000 | [diff] [blame] | 3697 | /// Emits a simple call (never an invoke) to the given runtime function. |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 3698 | llvm::CallInst *CodeGenFunction::EmitRuntimeCall(llvm::FunctionCallee callee, |
| 3699 | ArrayRef<llvm::Value *> args, |
| 3700 | const llvm::Twine &name) { |
| 3701 | llvm::CallInst *call = Builder.CreateCall( |
| 3702 | callee, args, getBundlesForFunclet(callee.getCallee()), name); |
David Majnemer | 971d31b | 2016-02-24 17:02:45 +0000 | [diff] [blame] | 3703 | call->setCallingConv(getRuntimeCC()); |
| 3704 | return call; |
| 3705 | } |
| 3706 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3707 | /// Emits a call or invoke to the given noreturn runtime function. |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 3708 | void CodeGenFunction::EmitNoreturnRuntimeCallOrInvoke( |
| 3709 | llvm::FunctionCallee callee, ArrayRef<llvm::Value *> args) { |
Reid Kleckner | b75a3f0 | 2018-02-09 00:16:41 +0000 | [diff] [blame] | 3710 | SmallVector<llvm::OperandBundleDef, 1> BundleList = |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 3711 | getBundlesForFunclet(callee.getCallee()); |
David Majnemer | 0b17d44 | 2015-12-15 21:27:59 +0000 | [diff] [blame] | 3712 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3713 | if (getInvokeDest()) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3714 | llvm::InvokeInst *invoke = |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3715 | Builder.CreateInvoke(callee, |
| 3716 | getUnreachableBlock(), |
| 3717 | getInvokeDest(), |
David Majnemer | 0b17d44 | 2015-12-15 21:27:59 +0000 | [diff] [blame] | 3718 | args, |
| 3719 | BundleList); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3720 | invoke->setDoesNotReturn(); |
| 3721 | invoke->setCallingConv(getRuntimeCC()); |
| 3722 | } else { |
David Majnemer | 0b17d44 | 2015-12-15 21:27:59 +0000 | [diff] [blame] | 3723 | llvm::CallInst *call = Builder.CreateCall(callee, args, BundleList); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3724 | call->setDoesNotReturn(); |
| 3725 | call->setCallingConv(getRuntimeCC()); |
| 3726 | Builder.CreateUnreachable(); |
| 3727 | } |
| 3728 | } |
| 3729 | |
Sanjay Patel | 846b63b | 2016-01-18 22:15:33 +0000 | [diff] [blame] | 3730 | /// Emits a call or invoke instruction to the given nullary runtime function. |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 3731 | llvm::CallBase * |
| 3732 | CodeGenFunction::EmitRuntimeCallOrInvoke(llvm::FunctionCallee callee, |
| 3733 | const Twine &name) { |
Craig Topper | 5fc8fc2 | 2014-08-27 06:28:36 +0000 | [diff] [blame] | 3734 | return EmitRuntimeCallOrInvoke(callee, None, name); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3735 | } |
| 3736 | |
| 3737 | /// Emits a call or invoke instruction to the given runtime function. |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 3738 | llvm::CallBase * |
| 3739 | CodeGenFunction::EmitRuntimeCallOrInvoke(llvm::FunctionCallee callee, |
| 3740 | ArrayRef<llvm::Value *> args, |
| 3741 | const Twine &name) { |
James Y Knight | 3933add | 2019-01-30 02:54:28 +0000 | [diff] [blame] | 3742 | llvm::CallBase *call = EmitCallOrInvoke(callee, args, name); |
| 3743 | call->setCallingConv(getRuntimeCC()); |
| 3744 | return call; |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3745 | } |
| 3746 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3747 | /// Emits a call or invoke instruction to the given function, depending |
| 3748 | /// on the current state of the EH stack. |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 3749 | llvm::CallBase *CodeGenFunction::EmitCallOrInvoke(llvm::FunctionCallee Callee, |
James Y Knight | 3933add | 2019-01-30 02:54:28 +0000 | [diff] [blame] | 3750 | ArrayRef<llvm::Value *> Args, |
| 3751 | const Twine &Name) { |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3752 | llvm::BasicBlock *InvokeDest = getInvokeDest(); |
Reid Kleckner | b75a3f0 | 2018-02-09 00:16:41 +0000 | [diff] [blame] | 3753 | SmallVector<llvm::OperandBundleDef, 1> BundleList = |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 3754 | getBundlesForFunclet(Callee.getCallee()); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3755 | |
James Y Knight | 3933add | 2019-01-30 02:54:28 +0000 | [diff] [blame] | 3756 | llvm::CallBase *Inst; |
Dan Gohman | 515a60d | 2012-02-16 00:57:37 +0000 | [diff] [blame] | 3757 | if (!InvokeDest) |
David Majnemer | 3df77bc | 2016-01-26 23:14:47 +0000 | [diff] [blame] | 3758 | Inst = Builder.CreateCall(Callee, Args, BundleList, Name); |
Dan Gohman | 515a60d | 2012-02-16 00:57:37 +0000 | [diff] [blame] | 3759 | else { |
| 3760 | llvm::BasicBlock *ContBB = createBasicBlock("invoke.cont"); |
David Majnemer | 3df77bc | 2016-01-26 23:14:47 +0000 | [diff] [blame] | 3761 | Inst = Builder.CreateInvoke(Callee, ContBB, InvokeDest, Args, BundleList, |
| 3762 | Name); |
Dan Gohman | 515a60d | 2012-02-16 00:57:37 +0000 | [diff] [blame] | 3763 | EmitBlock(ContBB); |
| 3764 | } |
| 3765 | |
| 3766 | // In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC |
| 3767 | // optimizer it can aggressively ignore unwind edges. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3768 | if (CGM.getLangOpts().ObjCAutoRefCount) |
Dan Gohman | 515a60d | 2012-02-16 00:57:37 +0000 | [diff] [blame] | 3769 | AddObjCARCExceptionMetadata(Inst); |
| 3770 | |
James Y Knight | 3933add | 2019-01-30 02:54:28 +0000 | [diff] [blame] | 3771 | return Inst; |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3772 | } |
| 3773 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 3774 | void CodeGenFunction::deferPlaceholderReplacement(llvm::Instruction *Old, |
| 3775 | llvm::Value *New) { |
| 3776 | DeferredReplacements.push_back(std::make_pair(Old, New)); |
| 3777 | } |
Chris Lattner | d59d867 | 2011-07-12 06:29:11 +0000 | [diff] [blame] | 3778 | |
Daniel Dunbar | d931a87 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 3779 | RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo, |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 3780 | const CGCallee &Callee, |
Anders Carlsson | 61a401c | 2009-12-24 19:25:24 +0000 | [diff] [blame] | 3781 | ReturnValueSlot ReturnValue, |
Daniel Dunbar | cdbb5e3 | 2009-02-20 18:06:48 +0000 | [diff] [blame] | 3782 | const CallArgList &CallArgs, |
James Y Knight | 3933add | 2019-01-30 02:54:28 +0000 | [diff] [blame] | 3783 | llvm::CallBase **callOrInvoke, |
Vedant Kumar | 09b5bfd | 2017-12-21 00:10:25 +0000 | [diff] [blame] | 3784 | SourceLocation Loc) { |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 3785 | // 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] | 3786 | |
Peter Collingbourne | ea21100 | 2018-02-05 23:09:13 +0000 | [diff] [blame] | 3787 | assert(Callee.isOrdinary() || Callee.isVirtual()); |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 3788 | |
Daniel Dunbar | 613855c | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 3789 | // Handle struct-return functions by passing a pointer to the |
| 3790 | // location that we would like to return into. |
Daniel Dunbar | 7633cbf | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 3791 | QualType RetTy = CallInfo.getReturnType(); |
Daniel Dunbar | b52d077 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 3792 | const ABIArgInfo &RetAI = CallInfo.getReturnInfo(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3793 | |
James Y Knight | cfe8cd7 | 2019-02-07 01:15:41 +0000 | [diff] [blame] | 3794 | llvm::FunctionType *IRFuncTy = getTypes().GetFunctionType(CallInfo); |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 3795 | |
Amy Huang | 0d0334f | 2019-04-12 20:25:30 +0000 | [diff] [blame] | 3796 | const Decl *TargetDecl = Callee.getAbstractInfo().getCalleeDecl().getDecl(); |
Erich Keane | 3617624 | 2019-06-21 22:29:32 +0000 | [diff] [blame] | 3797 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(TargetDecl)) |
| 3798 | // We can only guarantee that a function is called from the correct |
| 3799 | // context/function based on the appropriate target attributes, |
| 3800 | // so only check in the case where we have both always_inline and target |
| 3801 | // since otherwise we could be making a conditional call after a check for |
| 3802 | // the proper cpu features (and it won't cause code generation issues due to |
| 3803 | // function based code generation). |
| 3804 | if (TargetDecl->hasAttr<AlwaysInlineAttr>() && |
| 3805 | TargetDecl->hasAttr<TargetAttr>()) |
| 3806 | checkTargetFeatures(Loc, FD); |
Amy Huang | 0d0334f | 2019-04-12 20:25:30 +0000 | [diff] [blame] | 3807 | |
James Y Knight | b92d290 | 2019-02-05 16:05:50 +0000 | [diff] [blame] | 3808 | #ifndef NDEBUG |
| 3809 | if (!(CallInfo.isVariadic() && CallInfo.getArgStruct())) { |
| 3810 | // For an inalloca varargs function, we don't expect CallInfo to match the |
| 3811 | // function pointer's type, because the inalloca struct a will have extra |
| 3812 | // fields in it for the varargs parameters. Code later in this function |
| 3813 | // bitcasts the function pointer to the type derived from CallInfo. |
| 3814 | // |
| 3815 | // In other cases, we assert that the types match up (until pointers stop |
| 3816 | // having pointee types). |
James Y Knight | cfe8cd7 | 2019-02-07 01:15:41 +0000 | [diff] [blame] | 3817 | llvm::Type *TypeFromVal; |
| 3818 | if (Callee.isVirtual()) |
| 3819 | TypeFromVal = Callee.getVirtualFunctionType(); |
| 3820 | else |
| 3821 | TypeFromVal = |
| 3822 | Callee.getFunctionPointer()->getType()->getPointerElementType(); |
| 3823 | assert(IRFuncTy == TypeFromVal); |
James Y Knight | b92d290 | 2019-02-05 16:05:50 +0000 | [diff] [blame] | 3824 | } |
| 3825 | #endif |
| 3826 | |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 3827 | // 1. Set up the arguments. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3828 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 3829 | // If we're using inalloca, insert the allocation after the stack save. |
| 3830 | // FIXME: Do this earlier rather than hacking it in here! |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3831 | Address ArgMemory = Address::invalid(); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 3832 | if (llvm::StructType *ArgStruct = CallInfo.getArgStruct()) { |
Matt Arsenault | 502ad60 | 2017-04-10 22:28:02 +0000 | [diff] [blame] | 3833 | const llvm::DataLayout &DL = CGM.getDataLayout(); |
Reid Kleckner | 9df1d97 | 2014-04-10 01:40:15 +0000 | [diff] [blame] | 3834 | llvm::Instruction *IP = CallArgs.getStackBase(); |
| 3835 | llvm::AllocaInst *AI; |
| 3836 | if (IP) { |
| 3837 | IP = IP->getNextNode(); |
Matt Arsenault | 502ad60 | 2017-04-10 22:28:02 +0000 | [diff] [blame] | 3838 | AI = new llvm::AllocaInst(ArgStruct, DL.getAllocaAddrSpace(), |
| 3839 | "argmem", IP); |
Reid Kleckner | 9df1d97 | 2014-04-10 01:40:15 +0000 | [diff] [blame] | 3840 | } else { |
Reid Kleckner | 966abe7 | 2014-05-15 23:01:46 +0000 | [diff] [blame] | 3841 | AI = CreateTempAlloca(ArgStruct, "argmem"); |
Reid Kleckner | 9df1d97 | 2014-04-10 01:40:15 +0000 | [diff] [blame] | 3842 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3843 | auto Align = CallInfo.getArgStructAlignment(); |
Guillaume Chatelet | ab11b91 | 2019-09-30 13:34:44 +0000 | [diff] [blame] | 3844 | AI->setAlignment(llvm::MaybeAlign(Align.getQuantity())); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 3845 | AI->setUsedWithInAlloca(true); |
| 3846 | assert(AI->isUsedWithInAlloca() && !AI->isStaticAlloca()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3847 | ArgMemory = Address(AI, Align); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 3848 | } |
| 3849 | |
Alexey Samsonov | 153004f | 2014-09-29 22:08:00 +0000 | [diff] [blame] | 3850 | ClangToLLVMArgMapping IRFunctionArgs(CGM.getContext(), CallInfo); |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 3851 | SmallVector<llvm::Value *, 16> IRCallArgs(IRFunctionArgs.totalIRArgs()); |
| 3852 | |
Chris Lattner | 4ca97c3 | 2009-06-13 00:26:38 +0000 | [diff] [blame] | 3853 | // If the call returns a temporary with struct return, create a temporary |
Anders Carlsson | 1749083 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 3854 | // alloca to hold the result, unless one is given to us. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3855 | Address SRetPtr = Address::invalid(); |
Yaxun Liu | a2a9cfa | 2018-05-17 11:16:35 +0000 | [diff] [blame] | 3856 | Address SRetAlloca = Address::invalid(); |
George Burgess IV | 003be7c | 2018-03-08 05:32:30 +0000 | [diff] [blame] | 3857 | llvm::Value *UnusedReturnSizePtr = nullptr; |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 3858 | if (RetAI.isIndirect() || RetAI.isInAlloca() || RetAI.isCoerceAndExpand()) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3859 | if (!ReturnValue.isNull()) { |
| 3860 | SRetPtr = ReturnValue.getValue(); |
| 3861 | } else { |
Yaxun Liu | a2a9cfa | 2018-05-17 11:16:35 +0000 | [diff] [blame] | 3862 | SRetPtr = CreateMemTemp(RetTy, "tmp", &SRetAlloca); |
Leny Kholodov | 6aab111 | 2015-06-08 10:23:49 +0000 | [diff] [blame] | 3863 | if (HaveInsertPoint() && ReturnValue.isUnused()) { |
| 3864 | uint64_t size = |
| 3865 | CGM.getDataLayout().getTypeAllocSize(ConvertTypeForMem(RetTy)); |
Yaxun Liu | a2a9cfa | 2018-05-17 11:16:35 +0000 | [diff] [blame] | 3866 | UnusedReturnSizePtr = EmitLifetimeStart(size, SRetAlloca.getPointer()); |
Leny Kholodov | 6aab111 | 2015-06-08 10:23:49 +0000 | [diff] [blame] | 3867 | } |
| 3868 | } |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 3869 | if (IRFunctionArgs.hasSRetArg()) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3870 | IRCallArgs[IRFunctionArgs.getSRetArgNo()] = SRetPtr.getPointer(); |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 3871 | } else if (RetAI.isInAlloca()) { |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 3872 | Address Addr = |
| 3873 | Builder.CreateStructGEP(ArgMemory, RetAI.getInAllocaFieldIndex()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3874 | Builder.CreateStore(SRetPtr.getPointer(), Addr); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 3875 | } |
Anders Carlsson | 1749083 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 3876 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3877 | |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 3878 | Address swiftErrorTemp = Address::invalid(); |
| 3879 | Address swiftErrorArg = Address::invalid(); |
| 3880 | |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 3881 | // Translate all of the arguments as necessary to match the IR lowering. |
Daniel Dunbar | a45bdbb | 2009-02-04 21:17:21 +0000 | [diff] [blame] | 3882 | assert(CallInfo.arg_size() == CallArgs.size() && |
| 3883 | "Mismatch between function signature & arguments."); |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 3884 | unsigned ArgNo = 0; |
Daniel Dunbar | b52d077 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 3885 | CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3886 | for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end(); |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 3887 | I != E; ++I, ++info_it, ++ArgNo) { |
Daniel Dunbar | b52d077 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 3888 | const ABIArgInfo &ArgInfo = info_it->info; |
Daniel Dunbar | 8fc81b0 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 3889 | |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 3890 | // Insert a padding argument to ensure proper alignment. |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 3891 | if (IRFunctionArgs.hasPaddingArg(ArgNo)) |
| 3892 | IRCallArgs[IRFunctionArgs.getPaddingArgNo(ArgNo)] = |
| 3893 | llvm::UndefValue::get(ArgInfo.getPaddingType()); |
| 3894 | |
| 3895 | unsigned FirstIRArg, NumIRArgs; |
| 3896 | std::tie(FirstIRArg, NumIRArgs) = IRFunctionArgs.getIRArgs(ArgNo); |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 3897 | |
Daniel Dunbar | 8fc81b0 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 3898 | switch (ArgInfo.getKind()) { |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 3899 | case ABIArgInfo::InAlloca: { |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 3900 | assert(NumIRArgs == 0); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 3901 | assert(getTarget().getTriple().getArch() == llvm::Triple::x86); |
Yaxun Liu | 5b330e8 | 2018-03-15 15:25:19 +0000 | [diff] [blame] | 3902 | if (I->isAggregate()) { |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 3903 | // Replace the placeholder with the appropriate argument slot GEP. |
Yaxun Liu | 5b330e8 | 2018-03-15 15:25:19 +0000 | [diff] [blame] | 3904 | Address Addr = I->hasLValue() |
| 3905 | ? I->getKnownLValue().getAddress() |
| 3906 | : I->getKnownRValue().getAggregateAddress(); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 3907 | llvm::Instruction *Placeholder = |
Yaxun Liu | 5b330e8 | 2018-03-15 15:25:19 +0000 | [diff] [blame] | 3908 | cast<llvm::Instruction>(Addr.getPointer()); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 3909 | CGBuilderTy::InsertPoint IP = Builder.saveIP(); |
| 3910 | Builder.SetInsertPoint(Placeholder); |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 3911 | Addr = |
| 3912 | Builder.CreateStructGEP(ArgMemory, ArgInfo.getInAllocaFieldIndex()); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 3913 | Builder.restoreIP(IP); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3914 | deferPlaceholderReplacement(Placeholder, Addr.getPointer()); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 3915 | } else { |
| 3916 | // Store the RValue into the argument struct. |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 3917 | Address Addr = |
| 3918 | Builder.CreateStructGEP(ArgMemory, ArgInfo.getInAllocaFieldIndex()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3919 | unsigned AS = Addr.getType()->getPointerAddressSpace(); |
David Majnemer | 32b57b0 | 2014-03-31 16:12:47 +0000 | [diff] [blame] | 3920 | llvm::Type *MemType = ConvertTypeForMem(I->Ty)->getPointerTo(AS); |
| 3921 | // There are some cases where a trivial bitcast is not avoidable. The |
| 3922 | // definition of a type later in a translation unit may change it's type |
| 3923 | // from {}* to (%struct.foo*)*. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3924 | if (Addr.getType() != MemType) |
David Majnemer | 32b57b0 | 2014-03-31 16:12:47 +0000 | [diff] [blame] | 3925 | Addr = Builder.CreateBitCast(Addr, MemType); |
Yaxun Liu | 5b330e8 | 2018-03-15 15:25:19 +0000 | [diff] [blame] | 3926 | I->copyInto(*this, Addr); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 3927 | } |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 3928 | break; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 3929 | } |
| 3930 | |
Daniel Dunbar | 0381634 | 2010-08-21 02:24:36 +0000 | [diff] [blame] | 3931 | case ABIArgInfo::Indirect: { |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 3932 | assert(NumIRArgs == 1); |
Yaxun Liu | 5b330e8 | 2018-03-15 15:25:19 +0000 | [diff] [blame] | 3933 | if (!I->isAggregate()) { |
Daniel Dunbar | 747865a | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 3934 | // Make a temporary alloca to pass the argument. |
Yaxun Liu | aefdb8e | 2018-06-15 15:33:22 +0000 | [diff] [blame] | 3935 | Address Addr = CreateMemTempWithoutCast( |
| 3936 | I->Ty, ArgInfo.getIndirectAlign(), "indirect-arg-temp"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3937 | IRCallArgs[FirstIRArg] = Addr.getPointer(); |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 3938 | |
Yaxun Liu | 5b330e8 | 2018-03-15 15:25:19 +0000 | [diff] [blame] | 3939 | I->copyInto(*this, Addr); |
Daniel Dunbar | 747865a | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 3940 | } else { |
Eli Friedman | eb7fab6 | 2011-06-14 01:37:52 +0000 | [diff] [blame] | 3941 | // We want to avoid creating an unnecessary temporary+copy here; |
Guy Benyei | 3832bfd | 2013-03-10 12:59:00 +0000 | [diff] [blame] | 3942 | // however, we need one in three cases: |
Eli Friedman | eb7fab6 | 2011-06-14 01:37:52 +0000 | [diff] [blame] | 3943 | // 1. If the argument is not byval, and we are required to copy the |
| 3944 | // source. (This case doesn't occur on any common architecture.) |
| 3945 | // 2. If the argument is byval, RV is not sufficiently aligned, and |
| 3946 | // we cannot force it to be sufficiently aligned. |
Yaxun Liu | 5b330e8 | 2018-03-15 15:25:19 +0000 | [diff] [blame] | 3947 | // 3. If the argument is byval, but RV is not located in default |
| 3948 | // or alloca address space. |
| 3949 | Address Addr = I->hasLValue() |
| 3950 | ? I->getKnownLValue().getAddress() |
| 3951 | : I->getKnownRValue().getAggregateAddress(); |
| 3952 | llvm::Value *V = Addr.getPointer(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3953 | CharUnits Align = ArgInfo.getIndirectAlign(); |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3954 | const llvm::DataLayout *TD = &CGM.getDataLayout(); |
Yaxun Liu | 5b330e8 | 2018-03-15 15:25:19 +0000 | [diff] [blame] | 3955 | |
| 3956 | assert((FirstIRArg >= IRFuncTy->getNumParams() || |
| 3957 | IRFuncTy->getParamType(FirstIRArg)->getPointerAddressSpace() == |
| 3958 | TD->getAllocaAddrSpace()) && |
| 3959 | "indirect argument must be in alloca address space"); |
| 3960 | |
| 3961 | bool NeedCopy = false; |
| 3962 | |
| 3963 | if (Addr.getAlignment() < Align && |
| 3964 | llvm::getOrEnforceKnownAlignment(V, Align.getQuantity(), *TD) < |
| 3965 | Align.getQuantity()) { |
| 3966 | NeedCopy = true; |
| 3967 | } else if (I->hasLValue()) { |
| 3968 | auto LV = I->getKnownLValue(); |
| 3969 | auto AS = LV.getAddressSpace(); |
Andrew Savonichev | 1bf1a15 | 2018-12-10 12:03:00 +0000 | [diff] [blame] | 3970 | |
Yaxun Liu | 5b330e8 | 2018-03-15 15:25:19 +0000 | [diff] [blame] | 3971 | if ((!ArgInfo.getIndirectByVal() && |
| 3972 | (LV.getAlignment() >= |
Andrew Savonichev | 1bf1a15 | 2018-12-10 12:03:00 +0000 | [diff] [blame] | 3973 | getContext().getTypeAlignInChars(I->Ty)))) { |
| 3974 | NeedCopy = true; |
| 3975 | } |
| 3976 | if (!getLangOpts().OpenCL) { |
| 3977 | if ((ArgInfo.getIndirectByVal() && |
| 3978 | (AS != LangAS::Default && |
| 3979 | AS != CGM.getASTAllocaAddressSpace()))) { |
| 3980 | NeedCopy = true; |
| 3981 | } |
| 3982 | } |
| 3983 | // For OpenCL even if RV is located in default or alloca address space |
| 3984 | // we don't want to perform address space cast for it. |
| 3985 | else if ((ArgInfo.getIndirectByVal() && |
| 3986 | Addr.getType()->getAddressSpace() != IRFuncTy-> |
| 3987 | getParamType(FirstIRArg)->getPointerAddressSpace())) { |
Yaxun Liu | 5b330e8 | 2018-03-15 15:25:19 +0000 | [diff] [blame] | 3988 | NeedCopy = true; |
| 3989 | } |
| 3990 | } |
Andrew Savonichev | 1bf1a15 | 2018-12-10 12:03:00 +0000 | [diff] [blame] | 3991 | |
Yaxun Liu | 5b330e8 | 2018-03-15 15:25:19 +0000 | [diff] [blame] | 3992 | if (NeedCopy) { |
Eli Friedman | eb7fab6 | 2011-06-14 01:37:52 +0000 | [diff] [blame] | 3993 | // Create an aligned temporary, and copy to it. |
Yaxun Liu | aefdb8e | 2018-06-15 15:33:22 +0000 | [diff] [blame] | 3994 | Address AI = CreateMemTempWithoutCast( |
| 3995 | I->Ty, ArgInfo.getIndirectAlign(), "byval-temp"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3996 | IRCallArgs[FirstIRArg] = AI.getPointer(); |
Yaxun Liu | 5b330e8 | 2018-03-15 15:25:19 +0000 | [diff] [blame] | 3997 | I->copyInto(*this, AI); |
Eli Friedman | eb7fab6 | 2011-06-14 01:37:52 +0000 | [diff] [blame] | 3998 | } else { |
| 3999 | // Skip the extra memcpy call. |
Yaxun Liu | 5b330e8 | 2018-03-15 15:25:19 +0000 | [diff] [blame] | 4000 | auto *T = V->getType()->getPointerElementType()->getPointerTo( |
| 4001 | CGM.getDataLayout().getAllocaAddrSpace()); |
| 4002 | IRCallArgs[FirstIRArg] = getTargetHooks().performAddrSpaceCast( |
| 4003 | *this, V, LangAS::Default, CGM.getASTAllocaAddressSpace(), T, |
| 4004 | true); |
Eli Friedman | eb7fab6 | 2011-06-14 01:37:52 +0000 | [diff] [blame] | 4005 | } |
Daniel Dunbar | 747865a | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 4006 | } |
| 4007 | break; |
Daniel Dunbar | 0381634 | 2010-08-21 02:24:36 +0000 | [diff] [blame] | 4008 | } |
Daniel Dunbar | 747865a | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 4009 | |
Daniel Dunbar | 94a6f25 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 4010 | case ABIArgInfo::Ignore: |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 4011 | assert(NumIRArgs == 0); |
Daniel Dunbar | 94a6f25 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 4012 | break; |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 4013 | |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 4014 | case ABIArgInfo::Extend: |
| 4015 | case ABIArgInfo::Direct: { |
| 4016 | if (!isa<llvm::StructType>(ArgInfo.getCoerceToType()) && |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 4017 | ArgInfo.getCoerceToType() == ConvertType(info_it->type) && |
| 4018 | ArgInfo.getDirectOffset() == 0) { |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 4019 | assert(NumIRArgs == 1); |
Chris Lattner | bb1952c | 2011-07-12 04:46:18 +0000 | [diff] [blame] | 4020 | llvm::Value *V; |
Yaxun Liu | 5b330e8 | 2018-03-15 15:25:19 +0000 | [diff] [blame] | 4021 | if (!I->isAggregate()) |
| 4022 | V = I->getKnownRValue().getScalarVal(); |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 4023 | else |
Yaxun Liu | 5b330e8 | 2018-03-15 15:25:19 +0000 | [diff] [blame] | 4024 | V = Builder.CreateLoad( |
| 4025 | I->hasLValue() ? I->getKnownLValue().getAddress() |
| 4026 | : I->getKnownRValue().getAggregateAddress()); |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 4027 | |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4028 | // Implement swifterror by copying into a new swifterror argument. |
| 4029 | // We'll write back in the normal path out of the call. |
| 4030 | if (CallInfo.getExtParameterInfo(ArgNo).getABI() |
| 4031 | == ParameterABI::SwiftErrorResult) { |
| 4032 | assert(!swiftErrorTemp.isValid() && "multiple swifterror args"); |
| 4033 | |
| 4034 | QualType pointeeTy = I->Ty->getPointeeType(); |
| 4035 | swiftErrorArg = |
| 4036 | Address(V, getContext().getTypeAlignInChars(pointeeTy)); |
| 4037 | |
| 4038 | swiftErrorTemp = |
| 4039 | CreateMemTemp(pointeeTy, getPointerAlign(), "swifterror.temp"); |
| 4040 | V = swiftErrorTemp.getPointer(); |
| 4041 | cast<llvm::AllocaInst>(V)->setSwiftError(true); |
| 4042 | |
| 4043 | llvm::Value *errorValue = Builder.CreateLoad(swiftErrorArg); |
| 4044 | Builder.CreateStore(errorValue, swiftErrorTemp); |
| 4045 | } |
| 4046 | |
Reid Kleckner | 79b0fd7 | 2014-10-10 00:05:45 +0000 | [diff] [blame] | 4047 | // We might have to widen integers, but we should never truncate. |
| 4048 | if (ArgInfo.getCoerceToType() != V->getType() && |
| 4049 | V->getType()->isIntegerTy()) |
| 4050 | V = Builder.CreateZExt(V, ArgInfo.getCoerceToType()); |
| 4051 | |
Chris Lattner | 3ce8668 | 2011-07-12 04:53:39 +0000 | [diff] [blame] | 4052 | // If the argument doesn't match, perform a bitcast to coerce it. This |
| 4053 | // can happen due to trivial type mismatches. |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 4054 | if (FirstIRArg < IRFuncTy->getNumParams() && |
| 4055 | V->getType() != IRFuncTy->getParamType(FirstIRArg)) |
| 4056 | V = Builder.CreateBitCast(V, IRFuncTy->getParamType(FirstIRArg)); |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4057 | |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 4058 | IRCallArgs[FirstIRArg] = V; |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 4059 | break; |
| 4060 | } |
Daniel Dunbar | 94a6f25 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 4061 | |
Daniel Dunbar | 2f219b0 | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 4062 | // FIXME: Avoid the conversion through memory if possible. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4063 | Address Src = Address::invalid(); |
Yaxun Liu | 5b330e8 | 2018-03-15 15:25:19 +0000 | [diff] [blame] | 4064 | if (!I->isAggregate()) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4065 | Src = CreateMemTemp(I->Ty, "coerce"); |
Yaxun Liu | 5b330e8 | 2018-03-15 15:25:19 +0000 | [diff] [blame] | 4066 | I->copyInto(*this, Src); |
Ulrich Weigand | 6e2cea6 | 2015-07-10 11:31:43 +0000 | [diff] [blame] | 4067 | } else { |
Yaxun Liu | 5b330e8 | 2018-03-15 15:25:19 +0000 | [diff] [blame] | 4068 | Src = I->hasLValue() ? I->getKnownLValue().getAddress() |
| 4069 | : I->getKnownRValue().getAggregateAddress(); |
Ulrich Weigand | 6e2cea6 | 2015-07-10 11:31:43 +0000 | [diff] [blame] | 4070 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 4071 | |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 4072 | // If the value is offset in memory, apply the offset now. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4073 | Src = emitAddressAtOffset(*this, Src, ArgInfo); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 4074 | |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 4075 | // Fast-isel and the optimizer generally like scalar values better than |
| 4076 | // 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] | 4077 | llvm::StructType *STy = |
| 4078 | dyn_cast<llvm::StructType>(ArgInfo.getCoerceToType()); |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 4079 | if (STy && ArgInfo.isDirect() && ArgInfo.getCanBeFlattened()) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4080 | llvm::Type *SrcTy = Src.getType()->getElementType(); |
Chandler Carruth | a6399a5 | 2012-10-10 11:29:08 +0000 | [diff] [blame] | 4081 | uint64_t SrcSize = CGM.getDataLayout().getTypeAllocSize(SrcTy); |
| 4082 | uint64_t DstSize = CGM.getDataLayout().getTypeAllocSize(STy); |
| 4083 | |
| 4084 | // If the source type is smaller than the destination type of the |
| 4085 | // coerce-to logic, copy the source value into a temp alloca the size |
| 4086 | // of the destination type to allow loading all of it. The bits past |
| 4087 | // the source value are left undef. |
| 4088 | if (SrcSize < DstSize) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4089 | Address TempAlloca |
| 4090 | = CreateTempAlloca(STy, Src.getAlignment(), |
| 4091 | Src.getName() + ".coerce"); |
| 4092 | Builder.CreateMemCpy(TempAlloca, Src, SrcSize); |
| 4093 | Src = TempAlloca; |
Chandler Carruth | a6399a5 | 2012-10-10 11:29:08 +0000 | [diff] [blame] | 4094 | } else { |
Matt Arsenault | 7a124f3 | 2017-08-01 20:36:57 +0000 | [diff] [blame] | 4095 | Src = Builder.CreateBitCast(Src, |
| 4096 | STy->getPointerTo(Src.getAddressSpace())); |
Chandler Carruth | a6399a5 | 2012-10-10 11:29:08 +0000 | [diff] [blame] | 4097 | } |
| 4098 | |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 4099 | assert(NumIRArgs == STy->getNumElements()); |
Chris Lattner | ceddafb | 2010-07-05 20:41:41 +0000 | [diff] [blame] | 4100 | for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 4101 | Address EltPtr = Builder.CreateStructGEP(Src, i); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4102 | llvm::Value *LI = Builder.CreateLoad(EltPtr); |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 4103 | IRCallArgs[FirstIRArg + i] = LI; |
Chris Lattner | 15ec361 | 2010-06-29 00:06:42 +0000 | [diff] [blame] | 4104 | } |
Chris Lattner | 3dd716c | 2010-06-28 23:44:11 +0000 | [diff] [blame] | 4105 | } else { |
Chris Lattner | 15ec361 | 2010-06-29 00:06:42 +0000 | [diff] [blame] | 4106 | // In the simple case, just pass the coerced loaded value. |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 4107 | assert(NumIRArgs == 1); |
| 4108 | IRCallArgs[FirstIRArg] = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4109 | CreateCoercedLoad(Src, ArgInfo.getCoerceToType(), *this); |
Chris Lattner | 3dd716c | 2010-06-28 23:44:11 +0000 | [diff] [blame] | 4110 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 4111 | |
Daniel Dunbar | 2f219b0 | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 4112 | break; |
| 4113 | } |
| 4114 | |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 4115 | case ABIArgInfo::CoerceAndExpand: { |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 4116 | auto coercionType = ArgInfo.getCoerceAndExpandType(); |
| 4117 | auto layout = CGM.getDataLayout().getStructLayout(coercionType); |
| 4118 | |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4119 | llvm::Value *tempSize = nullptr; |
| 4120 | Address addr = Address::invalid(); |
Yaxun Liu | a2a9cfa | 2018-05-17 11:16:35 +0000 | [diff] [blame] | 4121 | Address AllocaAddr = Address::invalid(); |
Yaxun Liu | 5b330e8 | 2018-03-15 15:25:19 +0000 | [diff] [blame] | 4122 | if (I->isAggregate()) { |
| 4123 | addr = I->hasLValue() ? I->getKnownLValue().getAddress() |
| 4124 | : I->getKnownRValue().getAggregateAddress(); |
| 4125 | |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4126 | } else { |
Yaxun Liu | 5b330e8 | 2018-03-15 15:25:19 +0000 | [diff] [blame] | 4127 | RValue RV = I->getKnownRValue(); |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4128 | assert(RV.isScalar()); // complex should always just be direct |
| 4129 | |
| 4130 | llvm::Type *scalarType = RV.getScalarVal()->getType(); |
| 4131 | auto scalarSize = CGM.getDataLayout().getTypeAllocSize(scalarType); |
| 4132 | auto scalarAlign = CGM.getDataLayout().getPrefTypeAlignment(scalarType); |
| 4133 | |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4134 | // Materialize to a temporary. |
Guillaume Chatelet | f7caf3c | 2019-09-23 09:04:12 +0000 | [diff] [blame] | 4135 | addr = CreateTempAlloca( |
| 4136 | RV.getScalarVal()->getType(), |
| 4137 | CharUnits::fromQuantity(std::max( |
| 4138 | (unsigned)layout->getAlignment().value(), scalarAlign)), |
| 4139 | "tmp", |
| 4140 | /*ArraySize=*/nullptr, &AllocaAddr); |
Yaxun Liu | a2a9cfa | 2018-05-17 11:16:35 +0000 | [diff] [blame] | 4141 | tempSize = EmitLifetimeStart(scalarSize, AllocaAddr.getPointer()); |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4142 | |
| 4143 | Builder.CreateStore(RV.getScalarVal(), addr); |
| 4144 | } |
| 4145 | |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 4146 | addr = Builder.CreateElementBitCast(addr, coercionType); |
| 4147 | |
| 4148 | unsigned IRArgPos = FirstIRArg; |
| 4149 | for (unsigned i = 0, e = coercionType->getNumElements(); i != e; ++i) { |
| 4150 | llvm::Type *eltType = coercionType->getElementType(i); |
| 4151 | if (ABIArgInfo::isPaddingForCoerceAndExpand(eltType)) continue; |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 4152 | Address eltAddr = Builder.CreateStructGEP(addr, i); |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 4153 | llvm::Value *elt = Builder.CreateLoad(eltAddr); |
| 4154 | IRCallArgs[IRArgPos++] = elt; |
| 4155 | } |
| 4156 | assert(IRArgPos == FirstIRArg + NumIRArgs); |
| 4157 | |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4158 | if (tempSize) { |
Yaxun Liu | a2a9cfa | 2018-05-17 11:16:35 +0000 | [diff] [blame] | 4159 | EmitLifetimeEnd(tempSize, AllocaAddr.getPointer()); |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4160 | } |
| 4161 | |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 4162 | break; |
| 4163 | } |
| 4164 | |
Daniel Dunbar | 8fc81b0 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 4165 | case ABIArgInfo::Expand: |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 4166 | unsigned IRArgPos = FirstIRArg; |
Yaxun Liu | 5b330e8 | 2018-03-15 15:25:19 +0000 | [diff] [blame] | 4167 | ExpandTypeToArgs(I->Ty, *I, IRFuncTy, IRCallArgs, IRArgPos); |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 4168 | assert(IRArgPos == FirstIRArg + NumIRArgs); |
Daniel Dunbar | 8fc81b0 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 4169 | break; |
Daniel Dunbar | 613855c | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 4170 | } |
| 4171 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4172 | |
John McCall | 9831b84 | 2018-02-06 18:52:44 +0000 | [diff] [blame] | 4173 | const CGCallee &ConcreteCallee = Callee.prepareConcreteCallee(*this); |
| 4174 | llvm::Value *CalleePtr = ConcreteCallee.getFunctionPointer(); |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 4175 | |
| 4176 | // If we're using inalloca, set up that argument. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4177 | if (ArgMemory.isValid()) { |
| 4178 | llvm::Value *Arg = ArgMemory.getPointer(); |
Reid Kleckner | afba553e | 2014-07-08 02:24:27 +0000 | [diff] [blame] | 4179 | if (CallInfo.isVariadic()) { |
| 4180 | // When passing non-POD arguments by value to variadic functions, we will |
| 4181 | // end up with a variadic prototype and an inalloca call site. In such |
| 4182 | // cases, we can't do any parameter mismatch checks. Give up and bitcast |
| 4183 | // the callee. |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 4184 | unsigned CalleeAS = CalleePtr->getType()->getPointerAddressSpace(); |
James Y Knight | cfe8cd7 | 2019-02-07 01:15:41 +0000 | [diff] [blame] | 4185 | CalleePtr = |
| 4186 | Builder.CreateBitCast(CalleePtr, IRFuncTy->getPointerTo(CalleeAS)); |
Reid Kleckner | afba553e | 2014-07-08 02:24:27 +0000 | [diff] [blame] | 4187 | } else { |
| 4188 | llvm::Type *LastParamTy = |
| 4189 | IRFuncTy->getParamType(IRFuncTy->getNumParams() - 1); |
| 4190 | if (Arg->getType() != LastParamTy) { |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 4191 | #ifndef NDEBUG |
Reid Kleckner | afba553e | 2014-07-08 02:24:27 +0000 | [diff] [blame] | 4192 | // Assert that these structs have equivalent element types. |
| 4193 | llvm::StructType *FullTy = CallInfo.getArgStruct(); |
| 4194 | llvm::StructType *DeclaredTy = cast<llvm::StructType>( |
| 4195 | cast<llvm::PointerType>(LastParamTy)->getElementType()); |
| 4196 | assert(DeclaredTy->getNumElements() == FullTy->getNumElements()); |
| 4197 | for (llvm::StructType::element_iterator DI = DeclaredTy->element_begin(), |
| 4198 | DE = DeclaredTy->element_end(), |
| 4199 | FI = FullTy->element_begin(); |
| 4200 | DI != DE; ++DI, ++FI) |
| 4201 | assert(*DI == *FI); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 4202 | #endif |
Reid Kleckner | afba553e | 2014-07-08 02:24:27 +0000 | [diff] [blame] | 4203 | Arg = Builder.CreateBitCast(Arg, LastParamTy); |
| 4204 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 4205 | } |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 4206 | assert(IRFunctionArgs.hasInallocaArg()); |
| 4207 | IRCallArgs[IRFunctionArgs.getInallocaArgNo()] = Arg; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 4208 | } |
| 4209 | |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 4210 | // 2. Prepare the function pointer. |
| 4211 | |
| 4212 | // If the callee is a bitcast of a non-variadic function to have a |
| 4213 | // variadic function pointer type, check to see if we can remove the |
| 4214 | // bitcast. This comes up with unprototyped functions. |
| 4215 | // |
| 4216 | // This makes the IR nicer, but more importantly it ensures that we |
| 4217 | // can inline the function at -O0 if it is marked always_inline. |
James Y Knight | cfe8cd7 | 2019-02-07 01:15:41 +0000 | [diff] [blame] | 4218 | auto simplifyVariadicCallee = [](llvm::FunctionType *CalleeFT, |
| 4219 | llvm::Value *Ptr) -> llvm::Function * { |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 4220 | if (!CalleeFT->isVarArg()) |
James Y Knight | cfe8cd7 | 2019-02-07 01:15:41 +0000 | [diff] [blame] | 4221 | return nullptr; |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 4222 | |
James Y Knight | cfe8cd7 | 2019-02-07 01:15:41 +0000 | [diff] [blame] | 4223 | // Get underlying value if it's a bitcast |
| 4224 | if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Ptr)) { |
| 4225 | if (CE->getOpcode() == llvm::Instruction::BitCast) |
| 4226 | Ptr = CE->getOperand(0); |
| 4227 | } |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 4228 | |
James Y Knight | cfe8cd7 | 2019-02-07 01:15:41 +0000 | [diff] [blame] | 4229 | llvm::Function *OrigFn = dyn_cast<llvm::Function>(Ptr); |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 4230 | if (!OrigFn) |
James Y Knight | cfe8cd7 | 2019-02-07 01:15:41 +0000 | [diff] [blame] | 4231 | return nullptr; |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 4232 | |
| 4233 | llvm::FunctionType *OrigFT = OrigFn->getFunctionType(); |
| 4234 | |
| 4235 | // If the original type is variadic, or if any of the component types |
| 4236 | // disagree, we cannot remove the cast. |
| 4237 | if (OrigFT->isVarArg() || |
| 4238 | OrigFT->getNumParams() != CalleeFT->getNumParams() || |
| 4239 | OrigFT->getReturnType() != CalleeFT->getReturnType()) |
James Y Knight | cfe8cd7 | 2019-02-07 01:15:41 +0000 | [diff] [blame] | 4240 | return nullptr; |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 4241 | |
| 4242 | for (unsigned i = 0, e = OrigFT->getNumParams(); i != e; ++i) |
| 4243 | if (OrigFT->getParamType(i) != CalleeFT->getParamType(i)) |
James Y Knight | cfe8cd7 | 2019-02-07 01:15:41 +0000 | [diff] [blame] | 4244 | return nullptr; |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 4245 | |
| 4246 | return OrigFn; |
| 4247 | }; |
James Y Knight | cfe8cd7 | 2019-02-07 01:15:41 +0000 | [diff] [blame] | 4248 | |
| 4249 | if (llvm::Function *OrigFn = simplifyVariadicCallee(IRFuncTy, CalleePtr)) { |
| 4250 | CalleePtr = OrigFn; |
| 4251 | IRFuncTy = OrigFn->getFunctionType(); |
| 4252 | } |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 4253 | |
| 4254 | // 3. Perform the actual call. |
| 4255 | |
| 4256 | // Deactivate any cleanups that we're supposed to do immediately before |
| 4257 | // the call. |
Reid Kleckner | 23f4c4b | 2013-06-21 12:45:15 +0000 | [diff] [blame] | 4258 | if (!CallArgs.getCleanupsToDeactivate().empty()) |
| 4259 | deactivateArgCleanupsBeforeCall(*this, CallArgs); |
| 4260 | |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 4261 | // Assert that the arguments we computed match up. The IR verifier |
| 4262 | // will catch this, but this is a common enough source of problems |
| 4263 | // during IRGen changes that it's way better for debugging to catch |
| 4264 | // it ourselves here. |
| 4265 | #ifndef NDEBUG |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 4266 | assert(IRCallArgs.size() == IRFuncTy->getNumParams() || IRFuncTy->isVarArg()); |
| 4267 | for (unsigned i = 0; i < IRCallArgs.size(); ++i) { |
| 4268 | // Inalloca argument can have different type. |
| 4269 | if (IRFunctionArgs.hasInallocaArg() && |
| 4270 | i == IRFunctionArgs.getInallocaArgNo()) |
| 4271 | continue; |
| 4272 | if (i < IRFuncTy->getNumParams()) |
| 4273 | assert(IRCallArgs[i]->getType() == IRFuncTy->getParamType(i)); |
| 4274 | } |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 4275 | #endif |
Alexey Samsonov | 91cf455 | 2014-08-22 01:06:06 +0000 | [diff] [blame] | 4276 | |
Craig Topper | 3113ec3 | 2018-10-24 17:42:17 +0000 | [diff] [blame] | 4277 | // Update the largest vector width if any arguments have vector types. |
| 4278 | for (unsigned i = 0; i < IRCallArgs.size(); ++i) { |
| 4279 | if (auto *VT = dyn_cast<llvm::VectorType>(IRCallArgs[i]->getType())) |
| 4280 | LargestVectorWidth = std::max(LargestVectorWidth, |
| 4281 | VT->getPrimitiveSizeInBits()); |
| 4282 | } |
| 4283 | |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 4284 | // Compute the calling convention and attributes. |
Daniel Dunbar | 0ef3479 | 2009-09-12 00:59:20 +0000 | [diff] [blame] | 4285 | unsigned CallingConv; |
Reid Kleckner | cdd2679 | 2017-04-18 23:50:03 +0000 | [diff] [blame] | 4286 | llvm::AttributeList Attrs; |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 4287 | CGM.ConstructAttributeList(CalleePtr->getName(), CallInfo, |
Reid Kleckner | cdd2679 | 2017-04-18 23:50:03 +0000 | [diff] [blame] | 4288 | Callee.getAbstractInfo(), Attrs, CallingConv, |
Chad Rosier | 7dbc9cf | 2016-01-06 14:35:46 +0000 | [diff] [blame] | 4289 | /*AttrOnCallSite=*/true); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4290 | |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 4291 | // Apply some call-site-specific attributes. |
| 4292 | // TODO: work this into building the attribute set. |
| 4293 | |
| 4294 | // Apply always_inline to all calls within flatten functions. |
| 4295 | // FIXME: should this really take priority over __try, below? |
| 4296 | if (CurCodeDecl && CurCodeDecl->hasAttr<FlattenAttr>() && |
Amy Huang | 0d0334f | 2019-04-12 20:25:30 +0000 | [diff] [blame] | 4297 | !(TargetDecl && TargetDecl->hasAttr<NoInlineAttr>())) { |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 4298 | Attrs = |
Reid Kleckner | de86482 | 2017-03-21 16:57:30 +0000 | [diff] [blame] | 4299 | Attrs.addAttribute(getLLVMContext(), llvm::AttributeList::FunctionIndex, |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 4300 | llvm::Attribute::AlwaysInline); |
| 4301 | } |
| 4302 | |
| 4303 | // Disable inlining inside SEH __try blocks. |
| 4304 | if (isSEHTryScope()) { |
| 4305 | Attrs = |
Reid Kleckner | de86482 | 2017-03-21 16:57:30 +0000 | [diff] [blame] | 4306 | Attrs.addAttribute(getLLVMContext(), llvm::AttributeList::FunctionIndex, |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 4307 | llvm::Attribute::NoInline); |
| 4308 | } |
| 4309 | |
| 4310 | // Decide whether to use a call or an invoke. |
David Majnemer | 4e52d6f | 2015-12-12 05:39:21 +0000 | [diff] [blame] | 4311 | bool CannotThrow; |
| 4312 | if (currentFunctionUsesSEHTry()) { |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 4313 | // SEH cares about asynchronous exceptions, so everything can "throw." |
David Majnemer | 4e52d6f | 2015-12-12 05:39:21 +0000 | [diff] [blame] | 4314 | CannotThrow = false; |
| 4315 | } else if (isCleanupPadScope() && |
| 4316 | EHPersonality::get(*this).isMSVCXXPersonality()) { |
| 4317 | // The MSVC++ personality will implicitly terminate the program if an |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 4318 | // exception is thrown during a cleanup outside of a try/catch. |
| 4319 | // 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] | 4320 | CannotThrow = true; |
| 4321 | } else { |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 4322 | // Otherwise, nounwind call sites will never throw. |
Reid Kleckner | de86482 | 2017-03-21 16:57:30 +0000 | [diff] [blame] | 4323 | CannotThrow = Attrs.hasAttribute(llvm::AttributeList::FunctionIndex, |
David Majnemer | 4e52d6f | 2015-12-12 05:39:21 +0000 | [diff] [blame] | 4324 | llvm::Attribute::NoUnwind); |
| 4325 | } |
George Burgess IV | 003be7c | 2018-03-08 05:32:30 +0000 | [diff] [blame] | 4326 | |
| 4327 | // If we made a temporary, be sure to clean up after ourselves. Note that we |
| 4328 | // can't depend on being inside of an ExprWithCleanups, so we need to manually |
| 4329 | // pop this cleanup later on. Being eager about this is OK, since this |
| 4330 | // temporary is 'invisible' outside of the callee. |
| 4331 | if (UnusedReturnSizePtr) |
Yaxun Liu | a2a9cfa | 2018-05-17 11:16:35 +0000 | [diff] [blame] | 4332 | pushFullExprCleanup<CallLifetimeEnd>(NormalEHLifetimeMarker, SRetAlloca, |
George Burgess IV | 003be7c | 2018-03-08 05:32:30 +0000 | [diff] [blame] | 4333 | UnusedReturnSizePtr); |
| 4334 | |
David Majnemer | 4e52d6f | 2015-12-12 05:39:21 +0000 | [diff] [blame] | 4335 | llvm::BasicBlock *InvokeDest = CannotThrow ? nullptr : getInvokeDest(); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4336 | |
Reid Kleckner | b75a3f0 | 2018-02-09 00:16:41 +0000 | [diff] [blame] | 4337 | SmallVector<llvm::OperandBundleDef, 1> BundleList = |
| 4338 | getBundlesForFunclet(CalleePtr); |
David Majnemer | 0b17d44 | 2015-12-15 21:27:59 +0000 | [diff] [blame] | 4339 | |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 4340 | // Emit the actual call/invoke instruction. |
James Y Knight | 3933add | 2019-01-30 02:54:28 +0000 | [diff] [blame] | 4341 | llvm::CallBase *CI; |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4342 | if (!InvokeDest) { |
James Y Knight | cfe8cd7 | 2019-02-07 01:15:41 +0000 | [diff] [blame] | 4343 | CI = Builder.CreateCall(IRFuncTy, CalleePtr, IRCallArgs, BundleList); |
Daniel Dunbar | 1234749 | 2009-02-23 17:26:39 +0000 | [diff] [blame] | 4344 | } else { |
| 4345 | llvm::BasicBlock *Cont = createBasicBlock("invoke.cont"); |
James Y Knight | cfe8cd7 | 2019-02-07 01:15:41 +0000 | [diff] [blame] | 4346 | CI = Builder.CreateInvoke(IRFuncTy, CalleePtr, Cont, InvokeDest, IRCallArgs, |
David Majnemer | 0b17d44 | 2015-12-15 21:27:59 +0000 | [diff] [blame] | 4347 | BundleList); |
Daniel Dunbar | 1234749 | 2009-02-23 17:26:39 +0000 | [diff] [blame] | 4348 | EmitBlock(Cont); |
Daniel Dunbar | 5006f4a | 2009-02-20 18:54:31 +0000 | [diff] [blame] | 4349 | } |
Chris Lattner | e70a007 | 2010-06-29 16:40:28 +0000 | [diff] [blame] | 4350 | if (callOrInvoke) |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 4351 | *callOrInvoke = CI; |
Daniel Dunbar | 5006f4a | 2009-02-20 18:54:31 +0000 | [diff] [blame] | 4352 | |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 4353 | // Apply the attributes and calling convention. |
James Y Knight | 3933add | 2019-01-30 02:54:28 +0000 | [diff] [blame] | 4354 | CI->setAttributes(Attrs); |
| 4355 | CI->setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv)); |
Daniel Dunbar | b960b7b | 2009-03-02 04:32:35 +0000 | [diff] [blame] | 4356 | |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 4357 | // Apply various metadata. |
| 4358 | |
| 4359 | if (!CI->getType()->isVoidTy()) |
| 4360 | CI->setName("call"); |
| 4361 | |
Craig Topper | 3113ec3 | 2018-10-24 17:42:17 +0000 | [diff] [blame] | 4362 | // Update largest vector width from the return type. |
| 4363 | if (auto *VT = dyn_cast<llvm::VectorType>(CI->getType())) |
| 4364 | LargestVectorWidth = std::max(LargestVectorWidth, |
| 4365 | VT->getPrimitiveSizeInBits()); |
| 4366 | |
Adam Nemet | 1e217bc | 2016-03-28 22:18:53 +0000 | [diff] [blame] | 4367 | // Insert instrumentation or attach profile metadata at indirect call sites. |
| 4368 | // For more details, see the comment before the definition of |
| 4369 | // IPVK_IndirectCallTarget in InstrProfData.inc. |
James Y Knight | 3933add | 2019-01-30 02:54:28 +0000 | [diff] [blame] | 4370 | if (!CI->getCalledFunction()) |
Betul Buyukkurt | 518276a | 2016-01-23 22:50:44 +0000 | [diff] [blame] | 4371 | PGO.valueProfile(Builder, llvm::IPVK_IndirectCallTarget, |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 4372 | CI, CalleePtr); |
Betul Buyukkurt | 518276a | 2016-01-23 22:50:44 +0000 | [diff] [blame] | 4373 | |
Dan Gohman | 515a60d | 2012-02-16 00:57:37 +0000 | [diff] [blame] | 4374 | // In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC |
| 4375 | // optimizer it can aggressively ignore unwind edges. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4376 | if (CGM.getLangOpts().ObjCAutoRefCount) |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 4377 | AddObjCARCExceptionMetadata(CI); |
| 4378 | |
| 4379 | // Suppress tail calls if requested. |
| 4380 | if (llvm::CallInst *Call = dyn_cast<llvm::CallInst>(CI)) { |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 4381 | if (TargetDecl && TargetDecl->hasAttr<NotTailCalledAttr>()) |
| 4382 | Call->setTailCallKind(llvm::CallInst::TCK_NoTail); |
| 4383 | } |
| 4384 | |
Amy Huang | 0d0334f | 2019-04-12 20:25:30 +0000 | [diff] [blame] | 4385 | // Add metadata for calls to MSAllocator functions |
Amy Huang | 0d0334f | 2019-04-12 20:25:30 +0000 | [diff] [blame] | 4386 | if (getDebugInfo() && TargetDecl && |
| 4387 | TargetDecl->hasAttr<MSAllocatorAttr>()) |
| 4388 | getDebugInfo()->addHeapAllocSiteMetadata(CI, RetTy, Loc); |
| 4389 | |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 4390 | // 4. Finish the call. |
Dan Gohman | 515a60d | 2012-02-16 00:57:37 +0000 | [diff] [blame] | 4391 | |
Daniel Dunbar | b960b7b | 2009-03-02 04:32:35 +0000 | [diff] [blame] | 4392 | // 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] | 4393 | // insertion point; this allows the rest of IRGen to discard |
Daniel Dunbar | b960b7b | 2009-03-02 04:32:35 +0000 | [diff] [blame] | 4394 | // unreachable code. |
James Y Knight | 3933add | 2019-01-30 02:54:28 +0000 | [diff] [blame] | 4395 | if (CI->doesNotReturn()) { |
George Burgess IV | 003be7c | 2018-03-08 05:32:30 +0000 | [diff] [blame] | 4396 | if (UnusedReturnSizePtr) |
| 4397 | PopCleanupBlock(); |
Leny Kholodov | 6aab111 | 2015-06-08 10:23:49 +0000 | [diff] [blame] | 4398 | |
Julian Lettner | b62e9dc | 2019-01-24 18:04:21 +0000 | [diff] [blame] | 4399 | // Strip away the noreturn attribute to better diagnose unreachable UB. |
Vedant Kumar | 09b5bfd | 2017-12-21 00:10:25 +0000 | [diff] [blame] | 4400 | if (SanOpts.has(SanitizerKind::Unreachable)) { |
Julian Lettner | f82d892 | 2019-02-02 02:05:16 +0000 | [diff] [blame] | 4401 | // Also remove from function since CallBase::hasFnAttr additionally checks |
| 4402 | // attributes of the called function. |
James Y Knight | 3933add | 2019-01-30 02:54:28 +0000 | [diff] [blame] | 4403 | if (auto *F = CI->getCalledFunction()) |
Vedant Kumar | 09b5bfd | 2017-12-21 00:10:25 +0000 | [diff] [blame] | 4404 | F->removeFnAttr(llvm::Attribute::NoReturn); |
James Y Knight | 3933add | 2019-01-30 02:54:28 +0000 | [diff] [blame] | 4405 | CI->removeAttribute(llvm::AttributeList::FunctionIndex, |
| 4406 | llvm::Attribute::NoReturn); |
Julian Lettner | b6c06dc | 2019-02-01 02:51:00 +0000 | [diff] [blame] | 4407 | |
| 4408 | // Avoid incompatibility with ASan which relies on the `noreturn` |
| 4409 | // attribute to insert handler calls. |
Julian Lettner | 98b9f5b | 2019-02-04 23:37:50 +0000 | [diff] [blame] | 4410 | if (SanOpts.hasOneOf(SanitizerKind::Address | |
| 4411 | SanitizerKind::KernelAddress)) { |
Julian Lettner | b6c06dc | 2019-02-01 02:51:00 +0000 | [diff] [blame] | 4412 | SanitizerScope SanScope(this); |
| 4413 | llvm::IRBuilder<>::InsertPointGuard IPGuard(Builder); |
| 4414 | Builder.SetInsertPoint(CI); |
| 4415 | auto *FnType = llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false); |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 4416 | llvm::FunctionCallee Fn = |
| 4417 | CGM.CreateRuntimeFunction(FnType, "__asan_handle_no_return"); |
Julian Lettner | b6c06dc | 2019-02-01 02:51:00 +0000 | [diff] [blame] | 4418 | EmitNounwindRuntimeCall(Fn); |
| 4419 | } |
Vedant Kumar | 09b5bfd | 2017-12-21 00:10:25 +0000 | [diff] [blame] | 4420 | } |
| 4421 | |
| 4422 | EmitUnreachable(Loc); |
Daniel Dunbar | b960b7b | 2009-03-02 04:32:35 +0000 | [diff] [blame] | 4423 | Builder.ClearInsertionPoint(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4424 | |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 4425 | // FIXME: For now, emit a dummy basic block because expr emitters in |
| 4426 | // generally are not ready to handle emitting expressions at unreachable |
| 4427 | // points. |
Daniel Dunbar | b960b7b | 2009-03-02 04:32:35 +0000 | [diff] [blame] | 4428 | EnsureInsertPoint(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4429 | |
Daniel Dunbar | b960b7b | 2009-03-02 04:32:35 +0000 | [diff] [blame] | 4430 | // Return a reasonable RValue. |
| 4431 | return GetUndefRValue(RetTy); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4432 | } |
Daniel Dunbar | b960b7b | 2009-03-02 04:32:35 +0000 | [diff] [blame] | 4433 | |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4434 | // Perform the swifterror writeback. |
| 4435 | if (swiftErrorTemp.isValid()) { |
| 4436 | llvm::Value *errorResult = Builder.CreateLoad(swiftErrorTemp); |
| 4437 | Builder.CreateStore(errorResult, swiftErrorArg); |
| 4438 | } |
| 4439 | |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 4440 | // Emit any call-associated writebacks immediately. Arguably this |
| 4441 | // should happen after any return-value munging. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4442 | if (CallArgs.hasWritebacks()) |
| 4443 | emitWritebacks(*this, CallArgs); |
| 4444 | |
Nico Weber | 8cdb3f9 | 2015-08-25 18:43:32 +0000 | [diff] [blame] | 4445 | // The stack cleanup for inalloca arguments has to run out of the normal |
| 4446 | // lexical order, so deactivate it and run it manually here. |
| 4447 | CallArgs.freeArgumentMemory(*this); |
| 4448 | |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 4449 | // Extract the return value. |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 4450 | RValue Ret = [&] { |
| 4451 | switch (RetAI.getKind()) { |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 4452 | case ABIArgInfo::CoerceAndExpand: { |
| 4453 | auto coercionType = RetAI.getCoerceAndExpandType(); |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 4454 | |
| 4455 | Address addr = SRetPtr; |
| 4456 | addr = Builder.CreateElementBitCast(addr, coercionType); |
| 4457 | |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4458 | assert(CI->getType() == RetAI.getUnpaddedCoerceAndExpandType()); |
| 4459 | bool requiresExtract = isa<llvm::StructType>(CI->getType()); |
| 4460 | |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 4461 | unsigned unpaddedIndex = 0; |
| 4462 | for (unsigned i = 0, e = coercionType->getNumElements(); i != e; ++i) { |
| 4463 | llvm::Type *eltType = coercionType->getElementType(i); |
| 4464 | if (ABIArgInfo::isPaddingForCoerceAndExpand(eltType)) continue; |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 4465 | Address eltAddr = Builder.CreateStructGEP(addr, i); |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4466 | llvm::Value *elt = CI; |
| 4467 | if (requiresExtract) |
| 4468 | elt = Builder.CreateExtractValue(elt, unpaddedIndex++); |
| 4469 | else |
| 4470 | assert(unpaddedIndex == 0); |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 4471 | Builder.CreateStore(elt, eltAddr); |
| 4472 | } |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4473 | // FALLTHROUGH |
Galina Kistanova | 0872d6c | 2017-06-03 06:30:46 +0000 | [diff] [blame] | 4474 | LLVM_FALLTHROUGH; |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4475 | } |
| 4476 | |
| 4477 | case ABIArgInfo::InAlloca: |
| 4478 | case ABIArgInfo::Indirect: { |
| 4479 | RValue ret = convertTempToRValue(SRetPtr, RetTy, SourceLocation()); |
George Burgess IV | 003be7c | 2018-03-08 05:32:30 +0000 | [diff] [blame] | 4480 | if (UnusedReturnSizePtr) |
| 4481 | PopCleanupBlock(); |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4482 | return ret; |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 4483 | } |
| 4484 | |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 4485 | case ABIArgInfo::Ignore: |
| 4486 | // If we are ignoring an argument that had a result, make sure to |
| 4487 | // construct the appropriate return value for our caller. |
| 4488 | return GetUndefRValue(RetTy); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 4489 | |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 4490 | case ABIArgInfo::Extend: |
| 4491 | case ABIArgInfo::Direct: { |
| 4492 | llvm::Type *RetIRTy = ConvertType(RetTy); |
| 4493 | if (RetAI.getCoerceToType() == RetIRTy && RetAI.getDirectOffset() == 0) { |
| 4494 | switch (getEvaluationKind(RetTy)) { |
| 4495 | case TEK_Complex: { |
| 4496 | llvm::Value *Real = Builder.CreateExtractValue(CI, 0); |
| 4497 | llvm::Value *Imag = Builder.CreateExtractValue(CI, 1); |
| 4498 | return RValue::getComplex(std::make_pair(Real, Imag)); |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 4499 | } |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 4500 | case TEK_Aggregate: { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4501 | Address DestPtr = ReturnValue.getValue(); |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 4502 | bool DestIsVolatile = ReturnValue.isVolatile(); |
| 4503 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4504 | if (!DestPtr.isValid()) { |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 4505 | DestPtr = CreateMemTemp(RetTy, "agg.tmp"); |
| 4506 | DestIsVolatile = false; |
| 4507 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4508 | BuildAggStore(*this, CI, DestPtr, DestIsVolatile); |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 4509 | return RValue::getAggregate(DestPtr); |
| 4510 | } |
| 4511 | case TEK_Scalar: { |
| 4512 | // If the argument doesn't match, perform a bitcast to coerce it. This |
| 4513 | // can happen due to trivial type mismatches. |
| 4514 | llvm::Value *V = CI; |
| 4515 | if (V->getType() != RetIRTy) |
| 4516 | V = Builder.CreateBitCast(V, RetIRTy); |
| 4517 | return RValue::get(V); |
| 4518 | } |
| 4519 | } |
| 4520 | llvm_unreachable("bad evaluation kind"); |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 4521 | } |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 4522 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4523 | Address DestPtr = ReturnValue.getValue(); |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 4524 | bool DestIsVolatile = ReturnValue.isVolatile(); |
| 4525 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4526 | if (!DestPtr.isValid()) { |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 4527 | DestPtr = CreateMemTemp(RetTy, "coerce"); |
| 4528 | DestIsVolatile = false; |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 4529 | } |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 4530 | |
| 4531 | // If the value is offset in memory, apply the offset now. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4532 | Address StorePtr = emitAddressAtOffset(*this, DestPtr, RetAI); |
| 4533 | CreateCoercedStore(CI, StorePtr, DestIsVolatile, *this); |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 4534 | |
| 4535 | return convertTempToRValue(DestPtr, RetTy, SourceLocation()); |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 4536 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 4537 | |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 4538 | case ABIArgInfo::Expand: |
| 4539 | llvm_unreachable("Invalid ABI kind for return argument"); |
Anders Carlsson | 1749083 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 4540 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 4541 | |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 4542 | llvm_unreachable("Unhandled ABIArgInfo::Kind"); |
| 4543 | } (); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 4544 | |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 4545 | // Emit the assume_aligned check on the return value. |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 4546 | if (Ret.isScalar() && TargetDecl) { |
| 4547 | if (const auto *AA = TargetDecl->getAttr<AssumeAlignedAttr>()) { |
| 4548 | llvm::Value *OffsetValue = nullptr; |
| 4549 | if (const auto *Offset = AA->getOffset()) |
| 4550 | OffsetValue = EmitScalarExpr(Offset); |
| 4551 | |
| 4552 | llvm::Value *Alignment = EmitScalarExpr(AA->getAlignment()); |
| 4553 | llvm::ConstantInt *AlignmentCI = cast<llvm::ConstantInt>(Alignment); |
Roman Lebedev | bd1c087 | 2019-01-15 09:44:25 +0000 | [diff] [blame] | 4554 | EmitAlignmentAssumption(Ret.getScalarVal(), RetTy, Loc, AA->getLocation(), |
| 4555 | AlignmentCI->getZExtValue(), OffsetValue); |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 4556 | } else if (const auto *AA = TargetDecl->getAttr<AllocAlignAttr>()) { |
Roman Lebedev | bd1c087 | 2019-01-15 09:44:25 +0000 | [diff] [blame] | 4557 | llvm::Value *AlignmentVal = CallArgs[AA->getParamIndex().getLLVMIndex()] |
| 4558 | .getRValue(*this) |
| 4559 | .getScalarVal(); |
| 4560 | EmitAlignmentAssumption(Ret.getScalarVal(), RetTy, Loc, AA->getLocation(), |
| 4561 | AlignmentVal); |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 4562 | } |
Daniel Dunbar | 573884e | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 4563 | } |
Daniel Dunbar | d3674e6 | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 4564 | |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 4565 | return Ret; |
Daniel Dunbar | 613855c | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 4566 | } |
Daniel Dunbar | 2d0746f | 2009-02-10 20:44:09 +0000 | [diff] [blame] | 4567 | |
John McCall | 9831b84 | 2018-02-06 18:52:44 +0000 | [diff] [blame] | 4568 | CGCallee CGCallee::prepareConcreteCallee(CodeGenFunction &CGF) const { |
| 4569 | if (isVirtual()) { |
| 4570 | const CallExpr *CE = getVirtualCallExpr(); |
| 4571 | return CGF.CGM.getCXXABI().getVirtualFunctionPointer( |
James Y Knight | cfe8cd7 | 2019-02-07 01:15:41 +0000 | [diff] [blame] | 4572 | CGF, getVirtualMethodDecl(), getThisAddress(), getVirtualFunctionType(), |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4573 | CE ? CE->getBeginLoc() : SourceLocation()); |
John McCall | 9831b84 | 2018-02-06 18:52:44 +0000 | [diff] [blame] | 4574 | } |
| 4575 | |
| 4576 | return *this; |
| 4577 | } |
| 4578 | |
Daniel Dunbar | 2d0746f | 2009-02-10 20:44:09 +0000 | [diff] [blame] | 4579 | /* VarArg handling */ |
| 4580 | |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 4581 | Address CodeGenFunction::EmitVAArg(VAArgExpr *VE, Address &VAListAddr) { |
| 4582 | VAListAddr = VE->isMicrosoftABI() |
| 4583 | ? EmitMSVAListRef(VE->getSubExpr()) |
| 4584 | : EmitVAListRef(VE->getSubExpr()); |
| 4585 | QualType Ty = VE->getType(); |
| 4586 | if (VE->isMicrosoftABI()) |
| 4587 | return CGM.getTypes().getABIInfo().EmitMSVAArg(*this, VAListAddr, Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4588 | return CGM.getTypes().getABIInfo().EmitVAArg(*this, VAListAddr, Ty); |
Daniel Dunbar | 2d0746f | 2009-02-10 20:44:09 +0000 | [diff] [blame] | 4589 | } |