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