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