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