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