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