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