Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1 | //===--- CGCall.cpp - Encapsulate calling convention details ----*- C++ -*-===// |
Daniel Dunbar | 0dbe227 | 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" |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 16 | #include "CGCXXABI.h" |
Chris Lattner | ce93399 | 2010-06-29 16:40:28 +0000 | [diff] [blame] | 17 | #include "ABIInfo.h" |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 18 | #include "CodeGenFunction.h" |
Daniel Dunbar | b768807 | 2008-09-10 00:41:16 +0000 | [diff] [blame] | 19 | #include "CodeGenModule.h" |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 20 | #include "clang/Basic/TargetInfo.h" |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 21 | #include "clang/AST/Decl.h" |
Anders Carlsson | f6f8ae5 | 2009-04-03 22:48:58 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclCXX.h" |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 23 | #include "clang/AST/DeclObjC.h" |
Chandler Carruth | 06057ce | 2010-06-15 23:19:56 +0000 | [diff] [blame] | 24 | #include "clang/Frontend/CodeGenOptions.h" |
Devang Patel | d0646bd | 2008-09-24 01:01:36 +0000 | [diff] [blame] | 25 | #include "llvm/Attributes.h" |
Daniel Dunbar | d14151d | 2009-03-02 04:32:35 +0000 | [diff] [blame] | 26 | #include "llvm/Support/CallSite.h" |
Daniel Dunbar | 54d1ccb | 2009-01-27 01:36:03 +0000 | [diff] [blame] | 27 | #include "llvm/Target/TargetData.h" |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 28 | #include "llvm/InlineAsm.h" |
Eli Friedman | 97cb5a4 | 2011-06-15 22:09:18 +0000 | [diff] [blame] | 29 | #include "llvm/Transforms/Utils/Local.h" |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 30 | using namespace clang; |
| 31 | using namespace CodeGen; |
| 32 | |
| 33 | /***/ |
| 34 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 35 | static unsigned ClangCallConvToLLVMCallConv(CallingConv CC) { |
| 36 | switch (CC) { |
| 37 | default: return llvm::CallingConv::C; |
| 38 | case CC_X86StdCall: return llvm::CallingConv::X86_StdCall; |
| 39 | case CC_X86FastCall: return llvm::CallingConv::X86_FastCall; |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 40 | case CC_X86ThisCall: return llvm::CallingConv::X86_ThisCall; |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 41 | case CC_AAPCS: return llvm::CallingConv::ARM_AAPCS; |
| 42 | case CC_AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 43 | // TODO: add support for CC_X86Pascal to llvm |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 44 | } |
| 45 | } |
| 46 | |
John McCall | 0b0ef0a | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 47 | /// Derives the 'this' type for codegen purposes, i.e. ignoring method |
| 48 | /// qualification. |
| 49 | /// FIXME: address space qualification? |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 50 | static CanQualType GetThisType(ASTContext &Context, const CXXRecordDecl *RD) { |
| 51 | QualType RecTy = Context.getTagDeclType(RD)->getCanonicalTypeInternal(); |
| 52 | return Context.getPointerType(CanQualType::CreateUnsafe(RecTy)); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 53 | } |
| 54 | |
John McCall | 0b0ef0a | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 55 | /// Returns the canonical formal type of the given C++ method. |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 56 | static CanQual<FunctionProtoType> GetFormalType(const CXXMethodDecl *MD) { |
| 57 | return MD->getType()->getCanonicalTypeUnqualified() |
| 58 | .getAs<FunctionProtoType>(); |
John McCall | 0b0ef0a | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /// Returns the "extra-canonicalized" return type, which discards |
| 62 | /// qualifiers on the return type. Codegen doesn't care about them, |
| 63 | /// and it makes ABI code a little easier to be able to assume that |
| 64 | /// all parameter and return types are top-level unqualified. |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 65 | static CanQualType GetReturnType(QualType RetTy) { |
| 66 | return RetTy->getCanonicalTypeUnqualified().getUnqualifiedType(); |
John McCall | 0b0ef0a | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | const CGFunctionInfo & |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 70 | CodeGenTypes::getFunctionInfo(CanQual<FunctionNoProtoType> FTNP) { |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 71 | return getFunctionInfo(FTNP->getResultType().getUnqualifiedType(), |
| 72 | llvm::SmallVector<CanQualType, 16>(), |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 73 | FTNP->getExtInfo()); |
John McCall | 0b0ef0a | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /// \param Args - contains any initial parameters besides those |
| 77 | /// in the formal type |
| 78 | static const CGFunctionInfo &getFunctionInfo(CodeGenTypes &CGT, |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 79 | llvm::SmallVectorImpl<CanQualType> &ArgTys, |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 80 | CanQual<FunctionProtoType> FTP) { |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 81 | // FIXME: Kill copy. |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 82 | for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i) |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 83 | ArgTys.push_back(FTP->getArgType(i)); |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 84 | CanQualType ResTy = FTP->getResultType().getUnqualifiedType(); |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 85 | return CGT.getFunctionInfo(ResTy, ArgTys, FTP->getExtInfo()); |
John McCall | 0b0ef0a | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | const CGFunctionInfo & |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 89 | CodeGenTypes::getFunctionInfo(CanQual<FunctionProtoType> FTP) { |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 90 | llvm::SmallVector<CanQualType, 16> ArgTys; |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 91 | return ::getFunctionInfo(*this, ArgTys, FTP); |
Daniel Dunbar | bac7c25 | 2009-09-11 22:24:53 +0000 | [diff] [blame] | 92 | } |
| 93 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 94 | static CallingConv getCallingConventionForDecl(const Decl *D) { |
Daniel Dunbar | bac7c25 | 2009-09-11 22:24:53 +0000 | [diff] [blame] | 95 | // Set the appropriate calling convention for the Function. |
| 96 | if (D->hasAttr<StdCallAttr>()) |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 97 | return CC_X86StdCall; |
Daniel Dunbar | bac7c25 | 2009-09-11 22:24:53 +0000 | [diff] [blame] | 98 | |
| 99 | if (D->hasAttr<FastCallAttr>()) |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 100 | return CC_X86FastCall; |
Daniel Dunbar | bac7c25 | 2009-09-11 22:24:53 +0000 | [diff] [blame] | 101 | |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 102 | if (D->hasAttr<ThisCallAttr>()) |
| 103 | return CC_X86ThisCall; |
| 104 | |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 105 | if (D->hasAttr<PascalAttr>()) |
| 106 | return CC_X86Pascal; |
| 107 | |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 108 | if (PcsAttr *PCS = D->getAttr<PcsAttr>()) |
| 109 | return (PCS->getPCS() == PcsAttr::AAPCS ? CC_AAPCS : CC_AAPCS_VFP); |
| 110 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 111 | return CC_C; |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Anders Carlsson | 375c31c | 2009-10-03 19:43:08 +0000 | [diff] [blame] | 114 | const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXRecordDecl *RD, |
| 115 | const FunctionProtoType *FTP) { |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 116 | llvm::SmallVector<CanQualType, 16> ArgTys; |
John McCall | 0b0ef0a | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 117 | |
Anders Carlsson | 375c31c | 2009-10-03 19:43:08 +0000 | [diff] [blame] | 118 | // Add the 'this' pointer. |
John McCall | 0b0ef0a | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 119 | ArgTys.push_back(GetThisType(Context, RD)); |
| 120 | |
| 121 | return ::getFunctionInfo(*this, ArgTys, |
Tilmann Scheller | 9c6082f | 2011-03-02 21:36:49 +0000 | [diff] [blame] | 122 | FTP->getCanonicalTypeUnqualified().getAs<FunctionProtoType>()); |
Anders Carlsson | 375c31c | 2009-10-03 19:43:08 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Anders Carlsson | f6f8ae5 | 2009-04-03 22:48:58 +0000 | [diff] [blame] | 125 | const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXMethodDecl *MD) { |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 126 | llvm::SmallVector<CanQualType, 16> ArgTys; |
John McCall | 0b0ef0a | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 127 | |
John McCall | fc40028 | 2010-09-03 01:26:39 +0000 | [diff] [blame] | 128 | assert(!isa<CXXConstructorDecl>(MD) && "wrong method for contructors!"); |
| 129 | assert(!isa<CXXDestructorDecl>(MD) && "wrong method for destructors!"); |
| 130 | |
Chris Lattner | 3eb67ca | 2009-05-12 20:27:19 +0000 | [diff] [blame] | 131 | // Add the 'this' pointer unless this is a static method. |
| 132 | if (MD->isInstance()) |
John McCall | 0b0ef0a | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 133 | ArgTys.push_back(GetThisType(Context, MD->getParent())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 134 | |
Tilmann Scheller | 9c6082f | 2011-03-02 21:36:49 +0000 | [diff] [blame] | 135 | return ::getFunctionInfo(*this, ArgTys, GetFormalType(MD)); |
Anders Carlsson | f6f8ae5 | 2009-04-03 22:48:58 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 138 | const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXConstructorDecl *D, |
Anders Carlsson | f6c56e2 | 2009-11-25 03:15:49 +0000 | [diff] [blame] | 139 | CXXCtorType Type) { |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 140 | llvm::SmallVector<CanQualType, 16> ArgTys; |
John McCall | 0b0ef0a | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 141 | ArgTys.push_back(GetThisType(Context, D->getParent())); |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 142 | CanQualType ResTy = Context.VoidTy; |
Anders Carlsson | f6c56e2 | 2009-11-25 03:15:49 +0000 | [diff] [blame] | 143 | |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 144 | TheCXXABI.BuildConstructorSignature(D, Type, ResTy, ArgTys); |
John McCall | 0b0ef0a | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 145 | |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 146 | CanQual<FunctionProtoType> FTP = GetFormalType(D); |
| 147 | |
| 148 | // Add the formal parameters. |
| 149 | for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i) |
| 150 | ArgTys.push_back(FTP->getArgType(i)); |
| 151 | |
Tilmann Scheller | 9c6082f | 2011-03-02 21:36:49 +0000 | [diff] [blame] | 152 | return getFunctionInfo(ResTy, ArgTys, FTP->getExtInfo()); |
Anders Carlsson | f6c56e2 | 2009-11-25 03:15:49 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXDestructorDecl *D, |
| 156 | CXXDtorType Type) { |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 157 | llvm::SmallVector<CanQualType, 2> ArgTys; |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 158 | ArgTys.push_back(GetThisType(Context, D->getParent())); |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 159 | CanQualType ResTy = Context.VoidTy; |
John McCall | 0b0ef0a | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 160 | |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 161 | TheCXXABI.BuildDestructorSignature(D, Type, ResTy, ArgTys); |
| 162 | |
| 163 | CanQual<FunctionProtoType> FTP = GetFormalType(D); |
| 164 | assert(FTP->getNumArgs() == 0 && "dtor with formal parameters"); |
| 165 | |
Tilmann Scheller | 9c6082f | 2011-03-02 21:36:49 +0000 | [diff] [blame] | 166 | return getFunctionInfo(ResTy, ArgTys, FTP->getExtInfo()); |
Anders Carlsson | f6c56e2 | 2009-11-25 03:15:49 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 169 | const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) { |
Chris Lattner | 3eb67ca | 2009-05-12 20:27:19 +0000 | [diff] [blame] | 170 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) |
Anders Carlsson | f6f8ae5 | 2009-04-03 22:48:58 +0000 | [diff] [blame] | 171 | if (MD->isInstance()) |
| 172 | return getFunctionInfo(MD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 173 | |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 174 | CanQualType FTy = FD->getType()->getCanonicalTypeUnqualified(); |
| 175 | assert(isa<FunctionType>(FTy)); |
John McCall | 0b0ef0a | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 176 | if (isa<FunctionNoProtoType>(FTy)) |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 177 | return getFunctionInfo(FTy.getAs<FunctionNoProtoType>()); |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 178 | assert(isa<FunctionProtoType>(FTy)); |
| 179 | return getFunctionInfo(FTy.getAs<FunctionProtoType>()); |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 182 | const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) { |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 183 | llvm::SmallVector<CanQualType, 16> ArgTys; |
| 184 | ArgTys.push_back(Context.getCanonicalParamType(MD->getSelfDecl()->getType())); |
| 185 | ArgTys.push_back(Context.getCanonicalParamType(Context.getObjCSelType())); |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 186 | // FIXME: Kill copy? |
Chris Lattner | 2073216 | 2009-02-20 06:23:21 +0000 | [diff] [blame] | 187 | for (ObjCMethodDecl::param_iterator i = MD->param_begin(), |
John McCall | 0b0ef0a | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 188 | e = MD->param_end(); i != e; ++i) { |
| 189 | ArgTys.push_back(Context.getCanonicalParamType((*i)->getType())); |
| 190 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 191 | |
| 192 | FunctionType::ExtInfo einfo; |
| 193 | einfo = einfo.withCallingConv(getCallingConventionForDecl(MD)); |
| 194 | |
| 195 | if (getContext().getLangOptions().ObjCAutoRefCount && |
| 196 | MD->hasAttr<NSReturnsRetainedAttr>()) |
| 197 | einfo = einfo.withProducesResult(true); |
| 198 | |
| 199 | return getFunctionInfo(GetReturnType(MD->getResultType()), ArgTys, einfo); |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Anders Carlsson | b2bcf1c | 2010-02-06 02:44:09 +0000 | [diff] [blame] | 202 | const CGFunctionInfo &CodeGenTypes::getFunctionInfo(GlobalDecl GD) { |
| 203 | // FIXME: Do we need to handle ObjCMethodDecl? |
| 204 | const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl()); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 205 | |
Anders Carlsson | b2bcf1c | 2010-02-06 02:44:09 +0000 | [diff] [blame] | 206 | if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) |
| 207 | return getFunctionInfo(CD, GD.getCtorType()); |
| 208 | |
| 209 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD)) |
| 210 | return getFunctionInfo(DD, GD.getDtorType()); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 211 | |
Anders Carlsson | b2bcf1c | 2010-02-06 02:44:09 +0000 | [diff] [blame] | 212 | return getFunctionInfo(FD); |
| 213 | } |
| 214 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 215 | const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy, |
Daniel Dunbar | bac7c25 | 2009-09-11 22:24:53 +0000 | [diff] [blame] | 216 | const CallArgList &Args, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 217 | const FunctionType::ExtInfo &Info) { |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 218 | // FIXME: Kill copy. |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 219 | llvm::SmallVector<CanQualType, 16> ArgTys; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 220 | for (CallArgList::const_iterator i = Args.begin(), e = Args.end(); |
Daniel Dunbar | 725ad31 | 2009-01-31 02:19:00 +0000 | [diff] [blame] | 221 | i != e; ++i) |
Eli Friedman | c6d0782 | 2011-05-02 18:05:27 +0000 | [diff] [blame] | 222 | ArgTys.push_back(Context.getCanonicalParamType(i->Ty)); |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 223 | return getFunctionInfo(GetReturnType(ResTy), ArgTys, Info); |
Daniel Dunbar | 725ad31 | 2009-01-31 02:19:00 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 226 | const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy, |
Daniel Dunbar | bac7c25 | 2009-09-11 22:24:53 +0000 | [diff] [blame] | 227 | const FunctionArgList &Args, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 228 | const FunctionType::ExtInfo &Info) { |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 229 | // FIXME: Kill copy. |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 230 | llvm::SmallVector<CanQualType, 16> ArgTys; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 231 | for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end(); |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 232 | i != e; ++i) |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 233 | ArgTys.push_back(Context.getCanonicalParamType((*i)->getType())); |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 234 | return getFunctionInfo(GetReturnType(ResTy), ArgTys, Info); |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 235 | } |
| 236 | |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 237 | const CGFunctionInfo &CodeGenTypes::getNullaryFunctionInfo() { |
| 238 | llvm::SmallVector<CanQualType, 1> args; |
| 239 | return getFunctionInfo(getContext().VoidTy, args, FunctionType::ExtInfo()); |
| 240 | } |
| 241 | |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 242 | const CGFunctionInfo &CodeGenTypes::getFunctionInfo(CanQualType ResTy, |
| 243 | const llvm::SmallVectorImpl<CanQualType> &ArgTys, |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 244 | const FunctionType::ExtInfo &Info) { |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 245 | #ifndef NDEBUG |
| 246 | for (llvm::SmallVectorImpl<CanQualType>::const_iterator |
| 247 | I = ArgTys.begin(), E = ArgTys.end(); I != E; ++I) |
| 248 | assert(I->isCanonicalAsParam()); |
| 249 | #endif |
| 250 | |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 251 | unsigned CC = ClangCallConvToLLVMCallConv(Info.getCC()); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 252 | |
Daniel Dunbar | 40a6be6 | 2009-02-03 00:07:12 +0000 | [diff] [blame] | 253 | // Lookup or create unique function info. |
| 254 | llvm::FoldingSetNodeID ID; |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 255 | CGFunctionInfo::Profile(ID, Info, ResTy, |
Daniel Dunbar | bac7c25 | 2009-09-11 22:24:53 +0000 | [diff] [blame] | 256 | ArgTys.begin(), ArgTys.end()); |
Daniel Dunbar | 40a6be6 | 2009-02-03 00:07:12 +0000 | [diff] [blame] | 257 | |
| 258 | void *InsertPos = 0; |
| 259 | CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos); |
| 260 | if (FI) |
| 261 | return *FI; |
| 262 | |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 263 | // Construct the function info. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 264 | FI = new CGFunctionInfo(CC, Info.getNoReturn(), Info.getProducesResult(), |
| 265 | Info.getHasRegParm(), Info.getRegParm(), ResTy, |
Tilmann Scheller | 9c6082f | 2011-03-02 21:36:49 +0000 | [diff] [blame] | 266 | ArgTys.data(), ArgTys.size()); |
Daniel Dunbar | 35e67d4 | 2009-02-05 00:00:23 +0000 | [diff] [blame] | 267 | FunctionInfos.InsertNode(FI, InsertPos); |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 268 | |
| 269 | // Compute ABI information. |
Chris Lattner | ee5dcd0 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 270 | getABIInfo().computeInfo(*FI); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 271 | |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 272 | // Loop over all of the computed argument and return value info. If any of |
| 273 | // them are direct or extend without a specified coerce type, specify the |
| 274 | // default now. |
| 275 | ABIArgInfo &RetInfo = FI->getReturnInfo(); |
| 276 | if (RetInfo.canHaveCoerceToType() && RetInfo.getCoerceToType() == 0) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 277 | RetInfo.setCoerceToType(ConvertType(FI->getReturnType())); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 278 | |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 279 | for (CGFunctionInfo::arg_iterator I = FI->arg_begin(), E = FI->arg_end(); |
| 280 | I != E; ++I) |
| 281 | if (I->info.canHaveCoerceToType() && I->info.getCoerceToType() == 0) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 282 | I->info.setCoerceToType(ConvertType(I->type)); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 283 | |
Daniel Dunbar | 40a6be6 | 2009-02-03 00:07:12 +0000 | [diff] [blame] | 284 | return *FI; |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Daniel Dunbar | bac7c25 | 2009-09-11 22:24:53 +0000 | [diff] [blame] | 287 | CGFunctionInfo::CGFunctionInfo(unsigned _CallingConvention, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 288 | bool _NoReturn, bool returnsRetained, |
| 289 | bool _HasRegParm, unsigned _RegParm, |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 290 | CanQualType ResTy, |
Chris Lattner | bb52114 | 2010-06-29 18:13:52 +0000 | [diff] [blame] | 291 | const CanQualType *ArgTys, |
| 292 | unsigned NumArgTys) |
Daniel Dunbar | ca6408c | 2009-09-12 00:59:20 +0000 | [diff] [blame] | 293 | : CallingConvention(_CallingConvention), |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 294 | EffectiveCallingConvention(_CallingConvention), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 295 | NoReturn(_NoReturn), ReturnsRetained(returnsRetained), |
| 296 | HasRegParm(_HasRegParm), RegParm(_RegParm) |
Daniel Dunbar | bac7c25 | 2009-09-11 22:24:53 +0000 | [diff] [blame] | 297 | { |
Chris Lattner | bb52114 | 2010-06-29 18:13:52 +0000 | [diff] [blame] | 298 | NumArgs = NumArgTys; |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 299 | |
Chris Lattner | ce70016 | 2010-06-28 23:44:11 +0000 | [diff] [blame] | 300 | // FIXME: Coallocate with the CGFunctionInfo object. |
Chris Lattner | bb52114 | 2010-06-29 18:13:52 +0000 | [diff] [blame] | 301 | Args = new ArgInfo[1 + NumArgTys]; |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 302 | Args[0].type = ResTy; |
Chris Lattner | bb52114 | 2010-06-29 18:13:52 +0000 | [diff] [blame] | 303 | for (unsigned i = 0; i != NumArgTys; ++i) |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 304 | Args[1 + i].type = ArgTys[i]; |
| 305 | } |
| 306 | |
| 307 | /***/ |
| 308 | |
John McCall | 42e0611 | 2011-05-15 02:19:42 +0000 | [diff] [blame] | 309 | void CodeGenTypes::GetExpandedTypes(QualType type, |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 310 | llvm::SmallVectorImpl<llvm::Type*> &expandedTypes) { |
John McCall | 42e0611 | 2011-05-15 02:19:42 +0000 | [diff] [blame] | 311 | const RecordType *RT = type->getAsStructureType(); |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 312 | assert(RT && "Can only expand structure types."); |
| 313 | const RecordDecl *RD = RT->getDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 314 | assert(!RD->hasFlexibleArrayMember() && |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 315 | "Cannot expand structure with flexible array."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 316 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 317 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 318 | i != e; ++i) { |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 319 | const FieldDecl *FD = *i; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 320 | assert(!FD->isBitField() && |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 321 | "Cannot expand structure with bit-field members."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 322 | |
John McCall | 42e0611 | 2011-05-15 02:19:42 +0000 | [diff] [blame] | 323 | QualType fieldType = FD->getType(); |
| 324 | if (fieldType->isRecordType()) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 325 | GetExpandedTypes(fieldType, expandedTypes); |
Chris Lattner | deabde2 | 2010-07-28 18:24:28 +0000 | [diff] [blame] | 326 | else |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 327 | expandedTypes.push_back(ConvertType(fieldType)); |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 331 | llvm::Function::arg_iterator |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 332 | CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV, |
| 333 | llvm::Function::arg_iterator AI) { |
| 334 | const RecordType *RT = Ty->getAsStructureType(); |
| 335 | assert(RT && "Can only expand structure types."); |
| 336 | |
| 337 | RecordDecl *RD = RT->getDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 338 | assert(LV.isSimple() && |
| 339 | "Unexpected non-simple lvalue during struct expansion."); |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 340 | llvm::Value *Addr = LV.getAddress(); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 341 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 342 | i != e; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 343 | FieldDecl *FD = *i; |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 344 | QualType FT = FD->getType(); |
| 345 | |
| 346 | // FIXME: What are the right qualifiers here? |
Anders Carlsson | e6d2a53 | 2010-01-29 05:05:36 +0000 | [diff] [blame] | 347 | LValue LV = EmitLValueForField(Addr, FD, 0); |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 348 | if (CodeGenFunction::hasAggregateLLVMType(FT)) { |
| 349 | AI = ExpandTypeFromArgs(FT, LV, AI); |
| 350 | } else { |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 351 | EmitStoreThroughLValue(RValue::get(AI), LV); |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 352 | ++AI; |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | return AI; |
| 357 | } |
| 358 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 359 | void |
| 360 | CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV, |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 361 | llvm::SmallVector<llvm::Value*, 16> &Args) { |
| 362 | const RecordType *RT = Ty->getAsStructureType(); |
| 363 | assert(RT && "Can only expand structure types."); |
| 364 | |
| 365 | RecordDecl *RD = RT->getDecl(); |
| 366 | assert(RV.isAggregate() && "Unexpected rvalue during struct expansion"); |
| 367 | llvm::Value *Addr = RV.getAggregateAddr(); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 368 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 369 | i != e; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 370 | FieldDecl *FD = *i; |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 371 | QualType FT = FD->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 372 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 373 | // FIXME: What are the right qualifiers here? |
Anders Carlsson | e6d2a53 | 2010-01-29 05:05:36 +0000 | [diff] [blame] | 374 | LValue LV = EmitLValueForField(Addr, FD, 0); |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 375 | if (CodeGenFunction::hasAggregateLLVMType(FT)) { |
| 376 | ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args); |
| 377 | } else { |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 378 | RValue RV = EmitLoadOfLValue(LV); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 379 | assert(RV.isScalar() && |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 380 | "Unexpected non-scalar rvalue during struct expansion."); |
| 381 | Args.push_back(RV.getScalarVal()); |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | |
Chris Lattner | e7bb777 | 2010-06-27 06:04:18 +0000 | [diff] [blame] | 386 | /// EnterStructPointerForCoercedAccess - Given a struct pointer that we are |
Chris Lattner | 08dd2a0 | 2010-06-27 05:56:15 +0000 | [diff] [blame] | 387 | /// accessing some number of bytes out of it, try to gep into the struct to get |
| 388 | /// at its inner goodness. Dive as deep as possible without entering an element |
| 389 | /// with an in-memory size smaller than DstSize. |
| 390 | static llvm::Value * |
Chris Lattner | e7bb777 | 2010-06-27 06:04:18 +0000 | [diff] [blame] | 391 | EnterStructPointerForCoercedAccess(llvm::Value *SrcPtr, |
| 392 | const llvm::StructType *SrcSTy, |
| 393 | uint64_t DstSize, CodeGenFunction &CGF) { |
Chris Lattner | 08dd2a0 | 2010-06-27 05:56:15 +0000 | [diff] [blame] | 394 | // We can't dive into a zero-element struct. |
| 395 | if (SrcSTy->getNumElements() == 0) return SrcPtr; |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 396 | |
Chris Lattner | 08dd2a0 | 2010-06-27 05:56:15 +0000 | [diff] [blame] | 397 | const llvm::Type *FirstElt = SrcSTy->getElementType(0); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 398 | |
Chris Lattner | 08dd2a0 | 2010-06-27 05:56:15 +0000 | [diff] [blame] | 399 | // If the first elt is at least as large as what we're looking for, or if the |
| 400 | // first element is the same size as the whole struct, we can enter it. |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 401 | uint64_t FirstEltSize = |
Chris Lattner | 08dd2a0 | 2010-06-27 05:56:15 +0000 | [diff] [blame] | 402 | CGF.CGM.getTargetData().getTypeAllocSize(FirstElt); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 403 | if (FirstEltSize < DstSize && |
Chris Lattner | 08dd2a0 | 2010-06-27 05:56:15 +0000 | [diff] [blame] | 404 | FirstEltSize < CGF.CGM.getTargetData().getTypeAllocSize(SrcSTy)) |
| 405 | return SrcPtr; |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 406 | |
Chris Lattner | 08dd2a0 | 2010-06-27 05:56:15 +0000 | [diff] [blame] | 407 | // GEP into the first element. |
| 408 | SrcPtr = CGF.Builder.CreateConstGEP2_32(SrcPtr, 0, 0, "coerce.dive"); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 409 | |
Chris Lattner | 08dd2a0 | 2010-06-27 05:56:15 +0000 | [diff] [blame] | 410 | // If the first element is a struct, recurse. |
| 411 | const llvm::Type *SrcTy = |
| 412 | cast<llvm::PointerType>(SrcPtr->getType())->getElementType(); |
| 413 | if (const llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy)) |
Chris Lattner | e7bb777 | 2010-06-27 06:04:18 +0000 | [diff] [blame] | 414 | return EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF); |
Chris Lattner | 08dd2a0 | 2010-06-27 05:56:15 +0000 | [diff] [blame] | 415 | |
| 416 | return SrcPtr; |
| 417 | } |
| 418 | |
Chris Lattner | 6d11cdb | 2010-06-27 06:26:04 +0000 | [diff] [blame] | 419 | /// CoerceIntOrPtrToIntOrPtr - Convert a value Val to the specific Ty where both |
| 420 | /// are either integers or pointers. This does a truncation of the value if it |
| 421 | /// is too large or a zero extension if it is too small. |
| 422 | static llvm::Value *CoerceIntOrPtrToIntOrPtr(llvm::Value *Val, |
| 423 | const llvm::Type *Ty, |
| 424 | CodeGenFunction &CGF) { |
| 425 | if (Val->getType() == Ty) |
| 426 | return Val; |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 427 | |
Chris Lattner | 6d11cdb | 2010-06-27 06:26:04 +0000 | [diff] [blame] | 428 | if (isa<llvm::PointerType>(Val->getType())) { |
| 429 | // If this is Pointer->Pointer avoid conversion to and from int. |
| 430 | if (isa<llvm::PointerType>(Ty)) |
| 431 | return CGF.Builder.CreateBitCast(Val, Ty, "coerce.val"); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 432 | |
Chris Lattner | 6d11cdb | 2010-06-27 06:26:04 +0000 | [diff] [blame] | 433 | // Convert the pointer to an integer so we can play with its width. |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 434 | Val = CGF.Builder.CreatePtrToInt(Val, CGF.IntPtrTy, "coerce.val.pi"); |
Chris Lattner | 6d11cdb | 2010-06-27 06:26:04 +0000 | [diff] [blame] | 435 | } |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 436 | |
Chris Lattner | 6d11cdb | 2010-06-27 06:26:04 +0000 | [diff] [blame] | 437 | const llvm::Type *DestIntTy = Ty; |
| 438 | if (isa<llvm::PointerType>(DestIntTy)) |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 439 | DestIntTy = CGF.IntPtrTy; |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 440 | |
Chris Lattner | 6d11cdb | 2010-06-27 06:26:04 +0000 | [diff] [blame] | 441 | if (Val->getType() != DestIntTy) |
| 442 | Val = CGF.Builder.CreateIntCast(Val, DestIntTy, false, "coerce.val.ii"); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 443 | |
Chris Lattner | 6d11cdb | 2010-06-27 06:26:04 +0000 | [diff] [blame] | 444 | if (isa<llvm::PointerType>(Ty)) |
| 445 | Val = CGF.Builder.CreateIntToPtr(Val, Ty, "coerce.val.ip"); |
| 446 | return Val; |
| 447 | } |
| 448 | |
Chris Lattner | 08dd2a0 | 2010-06-27 05:56:15 +0000 | [diff] [blame] | 449 | |
| 450 | |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 451 | /// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as |
| 452 | /// a pointer to an object of type \arg Ty. |
| 453 | /// |
| 454 | /// This safely handles the case when the src type is smaller than the |
| 455 | /// destination type; in this situation the values of bits which not |
| 456 | /// present in the src are undefined. |
| 457 | static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr, |
| 458 | const llvm::Type *Ty, |
| 459 | CodeGenFunction &CGF) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 460 | const llvm::Type *SrcTy = |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 461 | cast<llvm::PointerType>(SrcPtr->getType())->getElementType(); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 462 | |
Chris Lattner | 6ae0069 | 2010-06-28 22:51:39 +0000 | [diff] [blame] | 463 | // If SrcTy and Ty are the same, just do a load. |
| 464 | if (SrcTy == Ty) |
| 465 | return CGF.Builder.CreateLoad(SrcPtr); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 466 | |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 467 | uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(Ty); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 468 | |
Chris Lattner | 08dd2a0 | 2010-06-27 05:56:15 +0000 | [diff] [blame] | 469 | if (const llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy)) { |
Chris Lattner | e7bb777 | 2010-06-27 06:04:18 +0000 | [diff] [blame] | 470 | SrcPtr = EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF); |
Chris Lattner | 08dd2a0 | 2010-06-27 05:56:15 +0000 | [diff] [blame] | 471 | SrcTy = cast<llvm::PointerType>(SrcPtr->getType())->getElementType(); |
| 472 | } |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 473 | |
Chris Lattner | 08dd2a0 | 2010-06-27 05:56:15 +0000 | [diff] [blame] | 474 | uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy); |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 475 | |
Chris Lattner | 6d11cdb | 2010-06-27 06:26:04 +0000 | [diff] [blame] | 476 | // If the source and destination are integer or pointer types, just do an |
| 477 | // extension or truncation to the desired type. |
| 478 | if ((isa<llvm::IntegerType>(Ty) || isa<llvm::PointerType>(Ty)) && |
| 479 | (isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy))) { |
| 480 | llvm::LoadInst *Load = CGF.Builder.CreateLoad(SrcPtr); |
| 481 | return CoerceIntOrPtrToIntOrPtr(Load, Ty, CGF); |
| 482 | } |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 483 | |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 484 | // If load is legal, just bitcast the src pointer. |
Daniel Dunbar | 7ef455b | 2009-05-13 18:54:26 +0000 | [diff] [blame] | 485 | if (SrcSize >= DstSize) { |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 486 | // Generally SrcSize is never greater than DstSize, since this means we are |
| 487 | // losing bits. However, this can happen in cases where the structure has |
| 488 | // additional padding, for example due to a user specified alignment. |
Daniel Dunbar | 7ef455b | 2009-05-13 18:54:26 +0000 | [diff] [blame] | 489 | // |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 490 | // FIXME: Assert that we aren't truncating non-padding bits when have access |
| 491 | // to that information. |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 492 | llvm::Value *Casted = |
| 493 | CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty)); |
Daniel Dunbar | 386621f | 2009-02-07 02:46:03 +0000 | [diff] [blame] | 494 | llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted); |
| 495 | // FIXME: Use better alignment / avoid requiring aligned load. |
| 496 | Load->setAlignment(1); |
| 497 | return Load; |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 498 | } |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 499 | |
Chris Lattner | 35b21b8 | 2010-06-27 01:06:27 +0000 | [diff] [blame] | 500 | // Otherwise do coercion through memory. This is stupid, but |
| 501 | // simple. |
| 502 | llvm::Value *Tmp = CGF.CreateTempAlloca(Ty); |
| 503 | llvm::Value *Casted = |
| 504 | CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy)); |
| 505 | llvm::StoreInst *Store = |
| 506 | CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted); |
| 507 | // FIXME: Use better alignment / avoid requiring aligned store. |
| 508 | Store->setAlignment(1); |
| 509 | return CGF.Builder.CreateLoad(Tmp); |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Eli Friedman | badea57 | 2011-05-17 21:08:01 +0000 | [diff] [blame] | 512 | // Function to store a first-class aggregate into memory. We prefer to |
| 513 | // store the elements rather than the aggregate to be more friendly to |
| 514 | // fast-isel. |
| 515 | // FIXME: Do we need to recurse here? |
| 516 | static void BuildAggStore(CodeGenFunction &CGF, llvm::Value *Val, |
| 517 | llvm::Value *DestPtr, bool DestIsVolatile, |
| 518 | bool LowAlignment) { |
| 519 | // Prefer scalar stores to first-class aggregate stores. |
| 520 | if (const llvm::StructType *STy = |
| 521 | dyn_cast<llvm::StructType>(Val->getType())) { |
| 522 | for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { |
| 523 | llvm::Value *EltPtr = CGF.Builder.CreateConstGEP2_32(DestPtr, 0, i); |
| 524 | llvm::Value *Elt = CGF.Builder.CreateExtractValue(Val, i); |
| 525 | llvm::StoreInst *SI = CGF.Builder.CreateStore(Elt, EltPtr, |
| 526 | DestIsVolatile); |
| 527 | if (LowAlignment) |
| 528 | SI->setAlignment(1); |
| 529 | } |
| 530 | } else { |
| 531 | CGF.Builder.CreateStore(Val, DestPtr, DestIsVolatile); |
| 532 | } |
| 533 | } |
| 534 | |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 535 | /// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src, |
| 536 | /// where the source and destination may have different types. |
| 537 | /// |
| 538 | /// This safely handles the case when the src type is larger than the |
| 539 | /// destination type; the upper bits of the src will be lost. |
| 540 | static void CreateCoercedStore(llvm::Value *Src, |
| 541 | llvm::Value *DstPtr, |
Anders Carlsson | d2490a9 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 542 | bool DstIsVolatile, |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 543 | CodeGenFunction &CGF) { |
| 544 | const llvm::Type *SrcTy = Src->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 545 | const llvm::Type *DstTy = |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 546 | cast<llvm::PointerType>(DstPtr->getType())->getElementType(); |
Chris Lattner | 6ae0069 | 2010-06-28 22:51:39 +0000 | [diff] [blame] | 547 | if (SrcTy == DstTy) { |
| 548 | CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile); |
| 549 | return; |
| 550 | } |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 551 | |
Chris Lattner | 6ae0069 | 2010-06-28 22:51:39 +0000 | [diff] [blame] | 552 | uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 553 | |
Chris Lattner | e7bb777 | 2010-06-27 06:04:18 +0000 | [diff] [blame] | 554 | if (const llvm::StructType *DstSTy = dyn_cast<llvm::StructType>(DstTy)) { |
| 555 | DstPtr = EnterStructPointerForCoercedAccess(DstPtr, DstSTy, SrcSize, CGF); |
| 556 | DstTy = cast<llvm::PointerType>(DstPtr->getType())->getElementType(); |
| 557 | } |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 558 | |
Chris Lattner | 6d11cdb | 2010-06-27 06:26:04 +0000 | [diff] [blame] | 559 | // If the source and destination are integer or pointer types, just do an |
| 560 | // extension or truncation to the desired type. |
| 561 | if ((isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy)) && |
| 562 | (isa<llvm::IntegerType>(DstTy) || isa<llvm::PointerType>(DstTy))) { |
| 563 | Src = CoerceIntOrPtrToIntOrPtr(Src, DstTy, CGF); |
| 564 | CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile); |
| 565 | return; |
| 566 | } |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 567 | |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 568 | uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(DstTy); |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 569 | |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 570 | // If store is legal, just bitcast the src pointer. |
Daniel Dunbar | fdf4986 | 2009-06-05 07:58:54 +0000 | [diff] [blame] | 571 | if (SrcSize <= DstSize) { |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 572 | llvm::Value *Casted = |
| 573 | CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy)); |
Daniel Dunbar | 386621f | 2009-02-07 02:46:03 +0000 | [diff] [blame] | 574 | // FIXME: Use better alignment / avoid requiring aligned store. |
Eli Friedman | badea57 | 2011-05-17 21:08:01 +0000 | [diff] [blame] | 575 | BuildAggStore(CGF, Src, Casted, DstIsVolatile, true); |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 576 | } else { |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 577 | // Otherwise do coercion through memory. This is stupid, but |
| 578 | // simple. |
Daniel Dunbar | fdf4986 | 2009-06-05 07:58:54 +0000 | [diff] [blame] | 579 | |
| 580 | // Generally SrcSize is never greater than DstSize, since this means we are |
| 581 | // losing bits. However, this can happen in cases where the structure has |
| 582 | // additional padding, for example due to a user specified alignment. |
| 583 | // |
| 584 | // FIXME: Assert that we aren't truncating non-padding bits when have access |
| 585 | // to that information. |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 586 | llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy); |
| 587 | CGF.Builder.CreateStore(Src, Tmp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 588 | llvm::Value *Casted = |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 589 | CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy)); |
Daniel Dunbar | 386621f | 2009-02-07 02:46:03 +0000 | [diff] [blame] | 590 | llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted); |
| 591 | // FIXME: Use better alignment / avoid requiring aligned load. |
| 592 | Load->setAlignment(1); |
Anders Carlsson | d2490a9 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 593 | CGF.Builder.CreateStore(Load, DstPtr, DstIsVolatile); |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 594 | } |
| 595 | } |
| 596 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 597 | /***/ |
| 598 | |
Daniel Dunbar | dacf9dd | 2010-07-14 23:39:36 +0000 | [diff] [blame] | 599 | bool CodeGenModule::ReturnTypeUsesSRet(const CGFunctionInfo &FI) { |
Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 600 | return FI.getReturnInfo().isIndirect(); |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 601 | } |
| 602 | |
Daniel Dunbar | dacf9dd | 2010-07-14 23:39:36 +0000 | [diff] [blame] | 603 | bool CodeGenModule::ReturnTypeUsesFPRet(QualType ResultType) { |
| 604 | if (const BuiltinType *BT = ResultType->getAs<BuiltinType>()) { |
| 605 | switch (BT->getKind()) { |
| 606 | default: |
| 607 | return false; |
| 608 | case BuiltinType::Float: |
| 609 | return getContext().Target.useObjCFPRetForRealType(TargetInfo::Float); |
| 610 | case BuiltinType::Double: |
| 611 | return getContext().Target.useObjCFPRetForRealType(TargetInfo::Double); |
| 612 | case BuiltinType::LongDouble: |
| 613 | return getContext().Target.useObjCFPRetForRealType( |
| 614 | TargetInfo::LongDouble); |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | return false; |
| 619 | } |
| 620 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 621 | llvm::FunctionType *CodeGenTypes::GetFunctionType(GlobalDecl GD) { |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 622 | const CGFunctionInfo &FI = getFunctionInfo(GD); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 623 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 624 | // For definition purposes, don't consider a K&R function variadic. |
| 625 | bool Variadic = false; |
| 626 | if (const FunctionProtoType *FPT = |
| 627 | cast<FunctionDecl>(GD.getDecl())->getType()->getAs<FunctionProtoType>()) |
| 628 | Variadic = FPT->isVariadic(); |
| 629 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 630 | return GetFunctionType(FI, Variadic); |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 631 | } |
| 632 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 633 | llvm::FunctionType * |
| 634 | CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool isVariadic) { |
| 635 | llvm::SmallVector<llvm::Type*, 8> argTypes; |
John McCall | 42e0611 | 2011-05-15 02:19:42 +0000 | [diff] [blame] | 636 | const llvm::Type *resultType = 0; |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 637 | |
John McCall | 42e0611 | 2011-05-15 02:19:42 +0000 | [diff] [blame] | 638 | const ABIArgInfo &retAI = FI.getReturnInfo(); |
| 639 | switch (retAI.getKind()) { |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 640 | case ABIArgInfo::Expand: |
John McCall | 42e0611 | 2011-05-15 02:19:42 +0000 | [diff] [blame] | 641 | llvm_unreachable("Invalid ABI kind for return argument"); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 642 | |
Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 643 | case ABIArgInfo::Extend: |
Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 644 | case ABIArgInfo::Direct: |
John McCall | 42e0611 | 2011-05-15 02:19:42 +0000 | [diff] [blame] | 645 | resultType = retAI.getCoerceToType(); |
Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 646 | break; |
| 647 | |
Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 648 | case ABIArgInfo::Indirect: { |
John McCall | 42e0611 | 2011-05-15 02:19:42 +0000 | [diff] [blame] | 649 | assert(!retAI.getIndirectAlign() && "Align unused on indirect return."); |
| 650 | resultType = llvm::Type::getVoidTy(getLLVMContext()); |
| 651 | |
| 652 | QualType ret = FI.getReturnType(); |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 653 | const llvm::Type *ty = ConvertType(ret); |
John McCall | 42e0611 | 2011-05-15 02:19:42 +0000 | [diff] [blame] | 654 | unsigned addressSpace = Context.getTargetAddressSpace(ret); |
| 655 | argTypes.push_back(llvm::PointerType::get(ty, addressSpace)); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 656 | break; |
| 657 | } |
| 658 | |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 659 | case ABIArgInfo::Ignore: |
John McCall | 42e0611 | 2011-05-15 02:19:42 +0000 | [diff] [blame] | 660 | resultType = llvm::Type::getVoidTy(getLLVMContext()); |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 661 | break; |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 662 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 663 | |
| 664 | for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(), |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 665 | ie = FI.arg_end(); it != ie; ++it) { |
John McCall | 42e0611 | 2011-05-15 02:19:42 +0000 | [diff] [blame] | 666 | const ABIArgInfo &argAI = it->info; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 667 | |
John McCall | 42e0611 | 2011-05-15 02:19:42 +0000 | [diff] [blame] | 668 | switch (argAI.getKind()) { |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 669 | case ABIArgInfo::Ignore: |
| 670 | break; |
| 671 | |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 672 | case ABIArgInfo::Indirect: { |
| 673 | // indirect arguments are always on the stack, which is addr space #0. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 674 | const llvm::Type *LTy = ConvertTypeForMem(it->type); |
John McCall | 42e0611 | 2011-05-15 02:19:42 +0000 | [diff] [blame] | 675 | argTypes.push_back(LTy->getPointerTo()); |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 676 | break; |
| 677 | } |
| 678 | |
| 679 | case ABIArgInfo::Extend: |
Chris Lattner | 1ed7267 | 2010-07-29 06:44:09 +0000 | [diff] [blame] | 680 | case ABIArgInfo::Direct: { |
Chris Lattner | ce70016 | 2010-06-28 23:44:11 +0000 | [diff] [blame] | 681 | // If the coerce-to type is a first class aggregate, flatten it. Either |
| 682 | // way is semantically identical, but fast-isel and the optimizer |
| 683 | // generally likes scalar values better than FCAs. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 684 | llvm::Type *argType = argAI.getCoerceToType(); |
John McCall | 42e0611 | 2011-05-15 02:19:42 +0000 | [diff] [blame] | 685 | if (const llvm::StructType *st = dyn_cast<llvm::StructType>(argType)) { |
| 686 | for (unsigned i = 0, e = st->getNumElements(); i != e; ++i) |
| 687 | argTypes.push_back(st->getElementType(i)); |
Chris Lattner | ce70016 | 2010-06-28 23:44:11 +0000 | [diff] [blame] | 688 | } else { |
John McCall | 42e0611 | 2011-05-15 02:19:42 +0000 | [diff] [blame] | 689 | argTypes.push_back(argType); |
Chris Lattner | ce70016 | 2010-06-28 23:44:11 +0000 | [diff] [blame] | 690 | } |
Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 691 | break; |
Chris Lattner | 1ed7267 | 2010-07-29 06:44:09 +0000 | [diff] [blame] | 692 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 693 | |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 694 | case ABIArgInfo::Expand: |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 695 | GetExpandedTypes(it->type, argTypes); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 696 | break; |
| 697 | } |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 698 | } |
| 699 | |
John McCall | 42e0611 | 2011-05-15 02:19:42 +0000 | [diff] [blame] | 700 | return llvm::FunctionType::get(resultType, argTypes, isVariadic); |
Daniel Dunbar | 3913f18 | 2008-09-09 23:48:28 +0000 | [diff] [blame] | 701 | } |
| 702 | |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 703 | const llvm::Type *CodeGenTypes::GetFunctionTypeForVTable(GlobalDecl GD) { |
| 704 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
Anders Carlsson | ecf282b | 2009-11-24 05:08:52 +0000 | [diff] [blame] | 705 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 706 | |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 707 | if (!VerifyFuncTypeComplete(FPT)) { |
| 708 | const CGFunctionInfo *Info; |
| 709 | if (isa<CXXDestructorDecl>(MD)) |
| 710 | Info = &getFunctionInfo(cast<CXXDestructorDecl>(MD), GD.getDtorType()); |
| 711 | else |
| 712 | Info = &getFunctionInfo(MD); |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 713 | return GetFunctionType(*Info, FPT->isVariadic()); |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 714 | } |
Anders Carlsson | ecf282b | 2009-11-24 05:08:52 +0000 | [diff] [blame] | 715 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 716 | return llvm::StructType::get(getLLVMContext()); |
Anders Carlsson | ecf282b | 2009-11-24 05:08:52 +0000 | [diff] [blame] | 717 | } |
| 718 | |
Daniel Dunbar | a0a99e0 | 2009-02-02 23:43:58 +0000 | [diff] [blame] | 719 | void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI, |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 720 | const Decl *TargetDecl, |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 721 | AttributeListType &PAL, |
Daniel Dunbar | ca6408c | 2009-09-12 00:59:20 +0000 | [diff] [blame] | 722 | unsigned &CallingConv) { |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 723 | unsigned FuncAttrs = 0; |
Devang Patel | a2c6912 | 2008-09-26 22:53:57 +0000 | [diff] [blame] | 724 | unsigned RetAttrs = 0; |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 725 | |
Daniel Dunbar | ca6408c | 2009-09-12 00:59:20 +0000 | [diff] [blame] | 726 | CallingConv = FI.getEffectiveCallingConvention(); |
| 727 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 728 | if (FI.isNoReturn()) |
| 729 | FuncAttrs |= llvm::Attribute::NoReturn; |
| 730 | |
Anton Korobeynikov | 1102f42 | 2009-04-04 00:49:24 +0000 | [diff] [blame] | 731 | // FIXME: handle sseregparm someday... |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 732 | if (TargetDecl) { |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 733 | if (TargetDecl->hasAttr<NoThrowAttr>()) |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 734 | FuncAttrs |= llvm::Attribute::NoUnwind; |
John McCall | 9c0c1f3 | 2010-07-08 06:48:12 +0000 | [diff] [blame] | 735 | else if (const FunctionDecl *Fn = dyn_cast<FunctionDecl>(TargetDecl)) { |
| 736 | const FunctionProtoType *FPT = Fn->getType()->getAs<FunctionProtoType>(); |
Sebastian Redl | 8026f6d | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 737 | if (FPT && FPT->isNothrow(getContext())) |
John McCall | 9c0c1f3 | 2010-07-08 06:48:12 +0000 | [diff] [blame] | 738 | FuncAttrs |= llvm::Attribute::NoUnwind; |
| 739 | } |
| 740 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 741 | if (TargetDecl->hasAttr<NoReturnAttr>()) |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 742 | FuncAttrs |= llvm::Attribute::NoReturn; |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 743 | if (TargetDecl->hasAttr<ConstAttr>()) |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 744 | FuncAttrs |= llvm::Attribute::ReadNone; |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 745 | else if (TargetDecl->hasAttr<PureAttr>()) |
Daniel Dunbar | 64c2e07 | 2009-04-10 22:14:52 +0000 | [diff] [blame] | 746 | FuncAttrs |= llvm::Attribute::ReadOnly; |
Ryan Flynn | 76168e2 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 747 | if (TargetDecl->hasAttr<MallocAttr>()) |
| 748 | RetAttrs |= llvm::Attribute::NoAlias; |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 749 | } |
| 750 | |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 751 | if (CodeGenOpts.OptimizeSize) |
Daniel Dunbar | 7ab1c3e | 2009-10-27 19:48:08 +0000 | [diff] [blame] | 752 | FuncAttrs |= llvm::Attribute::OptimizeForSize; |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 753 | if (CodeGenOpts.DisableRedZone) |
Devang Patel | 24095da | 2009-06-04 23:32:02 +0000 | [diff] [blame] | 754 | FuncAttrs |= llvm::Attribute::NoRedZone; |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 755 | if (CodeGenOpts.NoImplicitFloat) |
Devang Patel | acebb39 | 2009-06-05 22:05:48 +0000 | [diff] [blame] | 756 | FuncAttrs |= llvm::Attribute::NoImplicitFloat; |
Devang Patel | 24095da | 2009-06-04 23:32:02 +0000 | [diff] [blame] | 757 | |
Daniel Dunbar | a0a99e0 | 2009-02-02 23:43:58 +0000 | [diff] [blame] | 758 | QualType RetTy = FI.getReturnType(); |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 759 | unsigned Index = 1; |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 760 | const ABIArgInfo &RetAI = FI.getReturnInfo(); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 761 | switch (RetAI.getKind()) { |
Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 762 | case ABIArgInfo::Extend: |
Chris Lattner | 2eb9cdd | 2010-07-28 23:46:15 +0000 | [diff] [blame] | 763 | if (RetTy->hasSignedIntegerRepresentation()) |
Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 764 | RetAttrs |= llvm::Attribute::SExt; |
Chris Lattner | 2eb9cdd | 2010-07-28 23:46:15 +0000 | [diff] [blame] | 765 | else if (RetTy->hasUnsignedIntegerRepresentation()) |
Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 766 | RetAttrs |= llvm::Attribute::ZExt; |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 767 | break; |
Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 768 | case ABIArgInfo::Direct: |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 769 | case ABIArgInfo::Ignore: |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 770 | break; |
| 771 | |
Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 772 | case ABIArgInfo::Indirect: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 773 | PAL.push_back(llvm::AttributeWithIndex::get(Index, |
Chris Lattner | fb97cf2 | 2010-04-20 05:44:43 +0000 | [diff] [blame] | 774 | llvm::Attribute::StructRet)); |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 775 | ++Index; |
Daniel Dunbar | 0ac86f0 | 2009-03-18 19:51:01 +0000 | [diff] [blame] | 776 | // sret disables readnone and readonly |
| 777 | FuncAttrs &= ~(llvm::Attribute::ReadOnly | |
| 778 | llvm::Attribute::ReadNone); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 779 | break; |
| 780 | |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 781 | case ABIArgInfo::Expand: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 782 | assert(0 && "Invalid ABI kind for return argument"); |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 783 | } |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 784 | |
Devang Patel | a2c6912 | 2008-09-26 22:53:57 +0000 | [diff] [blame] | 785 | if (RetAttrs) |
| 786 | PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs)); |
Anton Korobeynikov | 1102f42 | 2009-04-04 00:49:24 +0000 | [diff] [blame] | 787 | |
Daniel Dunbar | 17d3fea | 2011-02-09 17:54:19 +0000 | [diff] [blame] | 788 | // FIXME: RegParm should be reduced in case of global register variable. |
Eli Friedman | a49218e | 2011-04-09 08:18:08 +0000 | [diff] [blame] | 789 | signed RegParm; |
| 790 | if (FI.getHasRegParm()) |
| 791 | RegParm = FI.getRegParm(); |
| 792 | else |
Daniel Dunbar | 17d3fea | 2011-02-09 17:54:19 +0000 | [diff] [blame] | 793 | RegParm = CodeGenOpts.NumRegisterParameters; |
Anton Korobeynikov | 1102f42 | 2009-04-04 00:49:24 +0000 | [diff] [blame] | 794 | |
| 795 | unsigned PointerWidth = getContext().Target.getPointerWidth(0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 796 | for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(), |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 797 | ie = FI.arg_end(); it != ie; ++it) { |
| 798 | QualType ParamType = it->type; |
| 799 | const ABIArgInfo &AI = it->info; |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 800 | unsigned Attributes = 0; |
Anton Korobeynikov | 1102f42 | 2009-04-04 00:49:24 +0000 | [diff] [blame] | 801 | |
John McCall | d8e10d2 | 2010-03-27 00:47:27 +0000 | [diff] [blame] | 802 | // 'restrict' -> 'noalias' is done in EmitFunctionProlog when we |
| 803 | // have the corresponding parameter variable. It doesn't make |
Daniel Dunbar | 7f6890e | 2011-02-10 18:10:07 +0000 | [diff] [blame] | 804 | // sense to do it here because parameters are so messed up. |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 805 | switch (AI.getKind()) { |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 806 | case ABIArgInfo::Extend: |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 807 | if (ParamType->isSignedIntegerOrEnumerationType()) |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 808 | Attributes |= llvm::Attribute::SExt; |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 809 | else if (ParamType->isUnsignedIntegerOrEnumerationType()) |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 810 | Attributes |= llvm::Attribute::ZExt; |
| 811 | // FALL THROUGH |
| 812 | case ABIArgInfo::Direct: |
| 813 | if (RegParm > 0 && |
| 814 | (ParamType->isIntegerType() || ParamType->isPointerType())) { |
| 815 | RegParm -= |
| 816 | (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth; |
| 817 | if (RegParm >= 0) |
| 818 | Attributes |= llvm::Attribute::InReg; |
| 819 | } |
| 820 | // FIXME: handle sseregparm someday... |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 821 | |
Chris Lattner | ce70016 | 2010-06-28 23:44:11 +0000 | [diff] [blame] | 822 | if (const llvm::StructType *STy = |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 823 | dyn_cast<llvm::StructType>(AI.getCoerceToType())) |
| 824 | Index += STy->getNumElements()-1; // 1 will be added below. |
| 825 | break; |
Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 826 | |
Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 827 | case ABIArgInfo::Indirect: |
Anders Carlsson | 0a8f847 | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 828 | if (AI.getIndirectByVal()) |
| 829 | Attributes |= llvm::Attribute::ByVal; |
| 830 | |
Anton Korobeynikov | 1102f42 | 2009-04-04 00:49:24 +0000 | [diff] [blame] | 831 | Attributes |= |
Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 832 | llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign()); |
Daniel Dunbar | 0ac86f0 | 2009-03-18 19:51:01 +0000 | [diff] [blame] | 833 | // byval disables readnone and readonly. |
| 834 | FuncAttrs &= ~(llvm::Attribute::ReadOnly | |
| 835 | llvm::Attribute::ReadNone); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 836 | break; |
Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 837 | |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 838 | case ABIArgInfo::Ignore: |
| 839 | // Skip increment, no matching LLVM parameter. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 840 | continue; |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 841 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 842 | case ABIArgInfo::Expand: { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 843 | llvm::SmallVector<llvm::Type*, 8> types; |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 844 | // FIXME: This is rather inefficient. Do we ever actually need to do |
| 845 | // anything here? The result should be just reconstructed on the other |
| 846 | // side, so extension should be a non-issue. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 847 | getTypes().GetExpandedTypes(ParamType, types); |
John McCall | 42e0611 | 2011-05-15 02:19:42 +0000 | [diff] [blame] | 848 | Index += types.size(); |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 849 | continue; |
| 850 | } |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 851 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 852 | |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 853 | if (Attributes) |
| 854 | PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes)); |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 855 | ++Index; |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 856 | } |
Devang Patel | a2c6912 | 2008-09-26 22:53:57 +0000 | [diff] [blame] | 857 | if (FuncAttrs) |
| 858 | PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs)); |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 859 | } |
| 860 | |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 861 | /// An argument came in as a promoted argument; demote it back to its |
| 862 | /// declared type. |
| 863 | static llvm::Value *emitArgumentDemotion(CodeGenFunction &CGF, |
| 864 | const VarDecl *var, |
| 865 | llvm::Value *value) { |
| 866 | const llvm::Type *varType = CGF.ConvertType(var->getType()); |
| 867 | |
| 868 | // This can happen with promotions that actually don't change the |
| 869 | // underlying type, like the enum promotions. |
| 870 | if (value->getType() == varType) return value; |
| 871 | |
| 872 | assert((varType->isIntegerTy() || varType->isFloatingPointTy()) |
| 873 | && "unexpected promotion type"); |
| 874 | |
| 875 | if (isa<llvm::IntegerType>(varType)) |
| 876 | return CGF.Builder.CreateTrunc(value, varType, "arg.unpromote"); |
| 877 | |
| 878 | return CGF.Builder.CreateFPCast(value, varType, "arg.unpromote"); |
| 879 | } |
| 880 | |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 881 | void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI, |
| 882 | llvm::Function *Fn, |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 883 | const FunctionArgList &Args) { |
John McCall | 0cfeb63 | 2009-07-28 01:00:58 +0000 | [diff] [blame] | 884 | // If this is an implicit-return-zero function, go ahead and |
| 885 | // initialize the return value. TODO: it might be nice to have |
| 886 | // a more general mechanism for this that didn't require synthesized |
| 887 | // return statements. |
Chris Lattner | 121b3fa | 2010-07-05 20:21:00 +0000 | [diff] [blame] | 888 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl)) { |
John McCall | 0cfeb63 | 2009-07-28 01:00:58 +0000 | [diff] [blame] | 889 | if (FD->hasImplicitReturnZero()) { |
| 890 | QualType RetTy = FD->getResultType().getUnqualifiedType(); |
| 891 | const llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy); |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 892 | llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy); |
John McCall | 0cfeb63 | 2009-07-28 01:00:58 +0000 | [diff] [blame] | 893 | Builder.CreateStore(Zero, ReturnValue); |
| 894 | } |
| 895 | } |
| 896 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 897 | // FIXME: We no longer need the types from FunctionArgList; lift up and |
| 898 | // simplify. |
Daniel Dunbar | 5251afa | 2009-02-03 06:02:10 +0000 | [diff] [blame] | 899 | |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 900 | // Emit allocs for param decls. Give the LLVM Argument nodes names. |
| 901 | llvm::Function::arg_iterator AI = Fn->arg_begin(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 902 | |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 903 | // Name the struct return argument. |
Daniel Dunbar | dacf9dd | 2010-07-14 23:39:36 +0000 | [diff] [blame] | 904 | if (CGM.ReturnTypeUsesSRet(FI)) { |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 905 | AI->setName("agg.result"); |
| 906 | ++AI; |
| 907 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 908 | |
Daniel Dunbar | 4b5f0a4 | 2009-02-04 21:17:21 +0000 | [diff] [blame] | 909 | assert(FI.arg_size() == Args.size() && |
| 910 | "Mismatch between function signature & arguments."); |
Devang Patel | 093ac46 | 2011-03-03 20:13:15 +0000 | [diff] [blame] | 911 | unsigned ArgNo = 1; |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 912 | CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin(); |
Devang Patel | 093ac46 | 2011-03-03 20:13:15 +0000 | [diff] [blame] | 913 | for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end(); |
| 914 | i != e; ++i, ++info_it, ++ArgNo) { |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 915 | const VarDecl *Arg = *i; |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 916 | QualType Ty = info_it->type; |
| 917 | const ABIArgInfo &ArgI = info_it->info; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 918 | |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 919 | bool isPromoted = |
| 920 | isa<ParmVarDecl>(Arg) && cast<ParmVarDecl>(Arg)->isKNRPromoted(); |
| 921 | |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 922 | switch (ArgI.getKind()) { |
Daniel Dunbar | 1f74598 | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 923 | case ABIArgInfo::Indirect: { |
Chris Lattner | ce70016 | 2010-06-28 23:44:11 +0000 | [diff] [blame] | 924 | llvm::Value *V = AI; |
Daniel Dunbar | cf3b6f2 | 2010-09-16 20:42:02 +0000 | [diff] [blame] | 925 | |
Daniel Dunbar | 1f74598 | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 926 | if (hasAggregateLLVMType(Ty)) { |
Daniel Dunbar | cf3b6f2 | 2010-09-16 20:42:02 +0000 | [diff] [blame] | 927 | // Aggregates and complex variables are accessed by reference. All we |
| 928 | // need to do is realign the value, if requested |
| 929 | if (ArgI.getIndirectRealign()) { |
| 930 | llvm::Value *AlignedTemp = CreateMemTemp(Ty, "coerce"); |
| 931 | |
| 932 | // Copy from the incoming argument pointer to the temporary with the |
| 933 | // appropriate alignment. |
| 934 | // |
| 935 | // FIXME: We should have a common utility for generating an aggregate |
| 936 | // copy. |
Benjamin Kramer | 9f0c7cc | 2010-12-30 00:13:21 +0000 | [diff] [blame] | 937 | const llvm::Type *I8PtrTy = Builder.getInt8PtrTy(); |
Ken Dyck | fe71008 | 2011-01-19 01:58:38 +0000 | [diff] [blame] | 938 | CharUnits Size = getContext().getTypeSizeInChars(Ty); |
NAKAMURA Takumi | c95a8fc | 2011-03-10 14:02:21 +0000 | [diff] [blame] | 939 | llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy); |
| 940 | llvm::Value *Src = Builder.CreateBitCast(V, I8PtrTy); |
| 941 | Builder.CreateMemCpy(Dst, |
| 942 | Src, |
Ken Dyck | fe71008 | 2011-01-19 01:58:38 +0000 | [diff] [blame] | 943 | llvm::ConstantInt::get(IntPtrTy, |
| 944 | Size.getQuantity()), |
Benjamin Kramer | 9f0c7cc | 2010-12-30 00:13:21 +0000 | [diff] [blame] | 945 | ArgI.getIndirectAlign(), |
| 946 | false); |
Daniel Dunbar | cf3b6f2 | 2010-09-16 20:42:02 +0000 | [diff] [blame] | 947 | V = AlignedTemp; |
| 948 | } |
Daniel Dunbar | 1f74598 | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 949 | } else { |
| 950 | // Load scalar value from indirect argument. |
Ken Dyck | fe71008 | 2011-01-19 01:58:38 +0000 | [diff] [blame] | 951 | CharUnits Alignment = getContext().getTypeAlignInChars(Ty); |
| 952 | V = EmitLoadOfScalar(V, false, Alignment.getQuantity(), Ty); |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 953 | |
| 954 | if (isPromoted) |
| 955 | V = emitArgumentDemotion(*this, Arg, V); |
Daniel Dunbar | 1f74598 | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 956 | } |
Devang Patel | 093ac46 | 2011-03-03 20:13:15 +0000 | [diff] [blame] | 957 | EmitParmDecl(*Arg, V, ArgNo); |
Daniel Dunbar | 1f74598 | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 958 | break; |
| 959 | } |
Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 960 | |
| 961 | case ABIArgInfo::Extend: |
Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 962 | case ABIArgInfo::Direct: { |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 963 | // If we have the trivial case, handle it with no muss and fuss. |
| 964 | if (!isa<llvm::StructType>(ArgI.getCoerceToType()) && |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 965 | ArgI.getCoerceToType() == ConvertType(Ty) && |
| 966 | ArgI.getDirectOffset() == 0) { |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 967 | assert(AI != Fn->arg_end() && "Argument mismatch!"); |
| 968 | llvm::Value *V = AI; |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 969 | |
John McCall | d8e10d2 | 2010-03-27 00:47:27 +0000 | [diff] [blame] | 970 | if (Arg->getType().isRestrictQualified()) |
| 971 | AI->addAttr(llvm::Attribute::NoAlias); |
| 972 | |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 973 | if (isPromoted) |
| 974 | V = emitArgumentDemotion(*this, Arg, V); |
| 975 | |
Devang Patel | 093ac46 | 2011-03-03 20:13:15 +0000 | [diff] [blame] | 976 | EmitParmDecl(*Arg, V, ArgNo); |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 977 | break; |
Daniel Dunbar | 8b979d9 | 2009-02-10 00:06:49 +0000 | [diff] [blame] | 978 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 979 | |
Chris Lattner | 121b3fa | 2010-07-05 20:21:00 +0000 | [diff] [blame] | 980 | llvm::AllocaInst *Alloca = CreateMemTemp(Ty, "coerce"); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 981 | |
Chris Lattner | deabde2 | 2010-07-28 18:24:28 +0000 | [diff] [blame] | 982 | // The alignment we need to use is the max of the requested alignment for |
| 983 | // the argument plus the alignment required by our access code below. |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 984 | unsigned AlignmentToUse = |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 985 | CGM.getTargetData().getABITypeAlignment(ArgI.getCoerceToType()); |
Chris Lattner | deabde2 | 2010-07-28 18:24:28 +0000 | [diff] [blame] | 986 | AlignmentToUse = std::max(AlignmentToUse, |
| 987 | (unsigned)getContext().getDeclAlign(Arg).getQuantity()); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 988 | |
Chris Lattner | deabde2 | 2010-07-28 18:24:28 +0000 | [diff] [blame] | 989 | Alloca->setAlignment(AlignmentToUse); |
Chris Lattner | 121b3fa | 2010-07-05 20:21:00 +0000 | [diff] [blame] | 990 | llvm::Value *V = Alloca; |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 991 | llvm::Value *Ptr = V; // Pointer to store into. |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 992 | |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 993 | // If the value is offset in memory, apply the offset now. |
| 994 | if (unsigned Offs = ArgI.getDirectOffset()) { |
| 995 | Ptr = Builder.CreateBitCast(Ptr, Builder.getInt8PtrTy()); |
| 996 | Ptr = Builder.CreateConstGEP1_32(Ptr, Offs); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 997 | Ptr = Builder.CreateBitCast(Ptr, |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 998 | llvm::PointerType::getUnqual(ArgI.getCoerceToType())); |
| 999 | } |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1000 | |
Chris Lattner | 309c59f | 2010-06-29 00:06:42 +0000 | [diff] [blame] | 1001 | // If the coerce-to type is a first class aggregate, we flatten it and |
| 1002 | // pass the elements. Either way is semantically identical, but fast-isel |
| 1003 | // and the optimizer generally likes scalar values better than FCAs. |
| 1004 | if (const llvm::StructType *STy = |
| 1005 | dyn_cast<llvm::StructType>(ArgI.getCoerceToType())) { |
Chris Lattner | 9282688 | 2010-07-05 20:41:41 +0000 | [diff] [blame] | 1006 | Ptr = Builder.CreateBitCast(Ptr, llvm::PointerType::getUnqual(STy)); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1007 | |
Chris Lattner | 9282688 | 2010-07-05 20:41:41 +0000 | [diff] [blame] | 1008 | for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { |
| 1009 | assert(AI != Fn->arg_end() && "Argument mismatch!"); |
| 1010 | AI->setName(Arg->getName() + ".coerce" + llvm::Twine(i)); |
| 1011 | llvm::Value *EltPtr = Builder.CreateConstGEP2_32(Ptr, 0, i); |
| 1012 | Builder.CreateStore(AI++, EltPtr); |
Chris Lattner | 309c59f | 2010-06-29 00:06:42 +0000 | [diff] [blame] | 1013 | } |
| 1014 | } else { |
| 1015 | // Simple case, just do a coerced store of the argument into the alloca. |
| 1016 | assert(AI != Fn->arg_end() && "Argument mismatch!"); |
Chris Lattner | 225e286 | 2010-06-29 00:14:52 +0000 | [diff] [blame] | 1017 | AI->setName(Arg->getName() + ".coerce"); |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 1018 | CreateCoercedStore(AI++, Ptr, /*DestIsVolatile=*/false, *this); |
Chris Lattner | 309c59f | 2010-06-29 00:06:42 +0000 | [diff] [blame] | 1019 | } |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1020 | |
| 1021 | |
Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 1022 | // Match to what EmitParmDecl is expecting for this type. |
Daniel Dunbar | 8b29a38 | 2009-02-04 07:22:24 +0000 | [diff] [blame] | 1023 | if (!CodeGenFunction::hasAggregateLLVMType(Ty)) { |
Daniel Dunbar | 91a16fa | 2010-08-21 02:24:36 +0000 | [diff] [blame] | 1024 | V = EmitLoadOfScalar(V, false, AlignmentToUse, Ty); |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1025 | if (isPromoted) |
| 1026 | V = emitArgumentDemotion(*this, Arg, V); |
Daniel Dunbar | 8b29a38 | 2009-02-04 07:22:24 +0000 | [diff] [blame] | 1027 | } |
Devang Patel | 093ac46 | 2011-03-03 20:13:15 +0000 | [diff] [blame] | 1028 | EmitParmDecl(*Arg, V, ArgNo); |
Chris Lattner | ce70016 | 2010-06-28 23:44:11 +0000 | [diff] [blame] | 1029 | continue; // Skip ++AI increment, already done. |
Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 1030 | } |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1031 | |
| 1032 | case ABIArgInfo::Expand: { |
| 1033 | // If this structure was expanded into multiple arguments then |
| 1034 | // we need to create a temporary and reconstruct it from the |
| 1035 | // arguments. |
| 1036 | llvm::Value *Temp = CreateMemTemp(Ty, Arg->getName() + ".addr"); |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1037 | llvm::Function::arg_iterator End = |
Daniel Dunbar | 79c3928 | 2010-08-21 03:15:20 +0000 | [diff] [blame] | 1038 | ExpandTypeFromArgs(Ty, MakeAddrLValue(Temp, Ty), AI); |
Devang Patel | 093ac46 | 2011-03-03 20:13:15 +0000 | [diff] [blame] | 1039 | EmitParmDecl(*Arg, Temp, ArgNo); |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1040 | |
| 1041 | // Name the arguments used in expansion and increment AI. |
| 1042 | unsigned Index = 0; |
| 1043 | for (; AI != End; ++AI, ++Index) |
| 1044 | AI->setName(Arg->getName() + "." + llvm::Twine(Index)); |
| 1045 | continue; |
| 1046 | } |
| 1047 | |
| 1048 | case ABIArgInfo::Ignore: |
| 1049 | // Initialize the local variable appropriately. |
| 1050 | if (hasAggregateLLVMType(Ty)) |
Devang Patel | 093ac46 | 2011-03-03 20:13:15 +0000 | [diff] [blame] | 1051 | EmitParmDecl(*Arg, CreateMemTemp(Ty), ArgNo); |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1052 | else |
Devang Patel | 093ac46 | 2011-03-03 20:13:15 +0000 | [diff] [blame] | 1053 | EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())), |
| 1054 | ArgNo); |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1055 | |
| 1056 | // Skip increment, no matching LLVM parameter. |
| 1057 | continue; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1058 | } |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1059 | |
| 1060 | ++AI; |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1061 | } |
| 1062 | assert(AI == Fn->arg_end() && "Argument mismatch!"); |
| 1063 | } |
| 1064 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1065 | /// Try to emit a fused autorelease of a return result. |
| 1066 | static llvm::Value *tryEmitFusedAutoreleaseOfResult(CodeGenFunction &CGF, |
| 1067 | llvm::Value *result) { |
| 1068 | // We must be immediately followed the cast. |
| 1069 | llvm::BasicBlock *BB = CGF.Builder.GetInsertBlock(); |
| 1070 | if (BB->empty()) return 0; |
| 1071 | if (&BB->back() != result) return 0; |
| 1072 | |
| 1073 | const llvm::Type *resultType = result->getType(); |
| 1074 | |
| 1075 | // result is in a BasicBlock and is therefore an Instruction. |
| 1076 | llvm::Instruction *generator = cast<llvm::Instruction>(result); |
| 1077 | |
| 1078 | llvm::SmallVector<llvm::Instruction*,4> insnsToKill; |
| 1079 | |
| 1080 | // Look for: |
| 1081 | // %generator = bitcast %type1* %generator2 to %type2* |
| 1082 | while (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(generator)) { |
| 1083 | // We would have emitted this as a constant if the operand weren't |
| 1084 | // an Instruction. |
| 1085 | generator = cast<llvm::Instruction>(bitcast->getOperand(0)); |
| 1086 | |
| 1087 | // Require the generator to be immediately followed by the cast. |
| 1088 | if (generator->getNextNode() != bitcast) |
| 1089 | return 0; |
| 1090 | |
| 1091 | insnsToKill.push_back(bitcast); |
| 1092 | } |
| 1093 | |
| 1094 | // Look for: |
| 1095 | // %generator = call i8* @objc_retain(i8* %originalResult) |
| 1096 | // or |
| 1097 | // %generator = call i8* @objc_retainAutoreleasedReturnValue(i8* %originalResult) |
| 1098 | llvm::CallInst *call = dyn_cast<llvm::CallInst>(generator); |
| 1099 | if (!call) return 0; |
| 1100 | |
| 1101 | bool doRetainAutorelease; |
| 1102 | |
| 1103 | if (call->getCalledValue() == CGF.CGM.getARCEntrypoints().objc_retain) { |
| 1104 | doRetainAutorelease = true; |
| 1105 | } else if (call->getCalledValue() == CGF.CGM.getARCEntrypoints() |
| 1106 | .objc_retainAutoreleasedReturnValue) { |
| 1107 | doRetainAutorelease = false; |
| 1108 | |
| 1109 | // Look for an inline asm immediately preceding the call and kill it, too. |
| 1110 | llvm::Instruction *prev = call->getPrevNode(); |
| 1111 | if (llvm::CallInst *asmCall = dyn_cast_or_null<llvm::CallInst>(prev)) |
| 1112 | if (asmCall->getCalledValue() |
| 1113 | == CGF.CGM.getARCEntrypoints().retainAutoreleasedReturnValueMarker) |
| 1114 | insnsToKill.push_back(prev); |
| 1115 | } else { |
| 1116 | return 0; |
| 1117 | } |
| 1118 | |
| 1119 | result = call->getArgOperand(0); |
| 1120 | insnsToKill.push_back(call); |
| 1121 | |
| 1122 | // Keep killing bitcasts, for sanity. Note that we no longer care |
| 1123 | // about precise ordering as long as there's exactly one use. |
| 1124 | while (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(result)) { |
| 1125 | if (!bitcast->hasOneUse()) break; |
| 1126 | insnsToKill.push_back(bitcast); |
| 1127 | result = bitcast->getOperand(0); |
| 1128 | } |
| 1129 | |
| 1130 | // Delete all the unnecessary instructions, from latest to earliest. |
| 1131 | for (llvm::SmallVectorImpl<llvm::Instruction*>::iterator |
| 1132 | i = insnsToKill.begin(), e = insnsToKill.end(); i != e; ++i) |
| 1133 | (*i)->eraseFromParent(); |
| 1134 | |
| 1135 | // Do the fused retain/autorelease if we were asked to. |
| 1136 | if (doRetainAutorelease) |
| 1137 | result = CGF.EmitARCRetainAutoreleaseReturnValue(result); |
| 1138 | |
| 1139 | // Cast back to the result type. |
| 1140 | return CGF.Builder.CreateBitCast(result, resultType); |
| 1141 | } |
| 1142 | |
| 1143 | /// Emit an ARC autorelease of the result of a function. |
| 1144 | static llvm::Value *emitAutoreleaseOfResult(CodeGenFunction &CGF, |
| 1145 | llvm::Value *result) { |
| 1146 | // At -O0, try to emit a fused retain/autorelease. |
| 1147 | if (CGF.shouldUseFusedARCCalls()) |
| 1148 | if (llvm::Value *fused = tryEmitFusedAutoreleaseOfResult(CGF, result)) |
| 1149 | return fused; |
| 1150 | |
| 1151 | return CGF.EmitARCAutoreleaseReturnValue(result); |
| 1152 | } |
| 1153 | |
Chris Lattner | 35b21b8 | 2010-06-27 01:06:27 +0000 | [diff] [blame] | 1154 | void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI) { |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1155 | // Functions with no result always return void. |
Chris Lattner | c6e6dd2 | 2010-06-26 23:13:19 +0000 | [diff] [blame] | 1156 | if (ReturnValue == 0) { |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1157 | Builder.CreateRetVoid(); |
Chris Lattner | c6e6dd2 | 2010-06-26 23:13:19 +0000 | [diff] [blame] | 1158 | return; |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1159 | } |
Daniel Dunbar | 21fcc8f | 2010-06-30 21:27:58 +0000 | [diff] [blame] | 1160 | |
Dan Gohman | 4751a53 | 2010-07-20 20:13:52 +0000 | [diff] [blame] | 1161 | llvm::DebugLoc RetDbgLoc; |
Chris Lattner | c6e6dd2 | 2010-06-26 23:13:19 +0000 | [diff] [blame] | 1162 | llvm::Value *RV = 0; |
| 1163 | QualType RetTy = FI.getReturnType(); |
| 1164 | const ABIArgInfo &RetAI = FI.getReturnInfo(); |
| 1165 | |
| 1166 | switch (RetAI.getKind()) { |
Daniel Dunbar | 91a16fa | 2010-08-21 02:24:36 +0000 | [diff] [blame] | 1167 | case ABIArgInfo::Indirect: { |
| 1168 | unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity(); |
Chris Lattner | c6e6dd2 | 2010-06-26 23:13:19 +0000 | [diff] [blame] | 1169 | if (RetTy->isAnyComplexType()) { |
| 1170 | ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false); |
| 1171 | StoreComplexToAddr(RT, CurFn->arg_begin(), false); |
| 1172 | } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) { |
| 1173 | // Do nothing; aggregrates get evaluated directly into the destination. |
| 1174 | } else { |
| 1175 | EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(), |
Daniel Dunbar | 91a16fa | 2010-08-21 02:24:36 +0000 | [diff] [blame] | 1176 | false, Alignment, RetTy); |
Chris Lattner | c6e6dd2 | 2010-06-26 23:13:19 +0000 | [diff] [blame] | 1177 | } |
| 1178 | break; |
Daniel Dunbar | 91a16fa | 2010-08-21 02:24:36 +0000 | [diff] [blame] | 1179 | } |
Chris Lattner | c6e6dd2 | 2010-06-26 23:13:19 +0000 | [diff] [blame] | 1180 | |
| 1181 | case ABIArgInfo::Extend: |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1182 | case ABIArgInfo::Direct: |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 1183 | if (RetAI.getCoerceToType() == ConvertType(RetTy) && |
| 1184 | RetAI.getDirectOffset() == 0) { |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1185 | // The internal return value temp always will have pointer-to-return-type |
| 1186 | // type, just do a load. |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1187 | |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1188 | // If the instruction right before the insertion point is a store to the |
| 1189 | // return value, we can elide the load, zap the store, and usually zap the |
| 1190 | // alloca. |
| 1191 | llvm::BasicBlock *InsertBB = Builder.GetInsertBlock(); |
| 1192 | llvm::StoreInst *SI = 0; |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1193 | if (InsertBB->empty() || |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1194 | !(SI = dyn_cast<llvm::StoreInst>(&InsertBB->back())) || |
| 1195 | SI->getPointerOperand() != ReturnValue || SI->isVolatile()) { |
| 1196 | RV = Builder.CreateLoad(ReturnValue); |
| 1197 | } else { |
| 1198 | // Get the stored value and nuke the now-dead store. |
| 1199 | RetDbgLoc = SI->getDebugLoc(); |
| 1200 | RV = SI->getValueOperand(); |
| 1201 | SI->eraseFromParent(); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1202 | |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1203 | // If that was the only use of the return value, nuke it as well now. |
| 1204 | if (ReturnValue->use_empty() && isa<llvm::AllocaInst>(ReturnValue)) { |
| 1205 | cast<llvm::AllocaInst>(ReturnValue)->eraseFromParent(); |
| 1206 | ReturnValue = 0; |
| 1207 | } |
Chris Lattner | 35b21b8 | 2010-06-27 01:06:27 +0000 | [diff] [blame] | 1208 | } |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1209 | } else { |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 1210 | llvm::Value *V = ReturnValue; |
| 1211 | // If the value is offset in memory, apply the offset now. |
| 1212 | if (unsigned Offs = RetAI.getDirectOffset()) { |
| 1213 | V = Builder.CreateBitCast(V, Builder.getInt8PtrTy()); |
| 1214 | V = Builder.CreateConstGEP1_32(V, Offs); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1215 | V = Builder.CreateBitCast(V, |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 1216 | llvm::PointerType::getUnqual(RetAI.getCoerceToType())); |
| 1217 | } |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1218 | |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 1219 | RV = CreateCoercedLoad(V, RetAI.getCoerceToType(), *this); |
Chris Lattner | 35b21b8 | 2010-06-27 01:06:27 +0000 | [diff] [blame] | 1220 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1221 | |
| 1222 | // In ARC, end functions that return a retainable type with a call |
| 1223 | // to objc_autoreleaseReturnValue. |
| 1224 | if (AutoreleaseResult) { |
| 1225 | assert(getLangOptions().ObjCAutoRefCount && |
| 1226 | !FI.isReturnsRetained() && |
| 1227 | RetTy->isObjCRetainableType()); |
| 1228 | RV = emitAutoreleaseOfResult(*this, RV); |
| 1229 | } |
| 1230 | |
Chris Lattner | c6e6dd2 | 2010-06-26 23:13:19 +0000 | [diff] [blame] | 1231 | break; |
Chris Lattner | c6e6dd2 | 2010-06-26 23:13:19 +0000 | [diff] [blame] | 1232 | |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1233 | case ABIArgInfo::Ignore: |
Chris Lattner | c6e6dd2 | 2010-06-26 23:13:19 +0000 | [diff] [blame] | 1234 | break; |
| 1235 | |
| 1236 | case ABIArgInfo::Expand: |
| 1237 | assert(0 && "Invalid ABI kind for return argument"); |
| 1238 | } |
| 1239 | |
Daniel Dunbar | 21fcc8f | 2010-06-30 21:27:58 +0000 | [diff] [blame] | 1240 | llvm::Instruction *Ret = RV ? Builder.CreateRet(RV) : Builder.CreateRetVoid(); |
Devang Patel | d3f265d | 2010-07-21 18:08:50 +0000 | [diff] [blame] | 1241 | if (!RetDbgLoc.isUnknown()) |
| 1242 | Ret->setDebugLoc(RetDbgLoc); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1243 | } |
| 1244 | |
John McCall | 413ebdb | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 1245 | void CodeGenFunction::EmitDelegateCallArg(CallArgList &args, |
| 1246 | const VarDecl *param) { |
John McCall | 2736071 | 2010-05-26 22:34:26 +0000 | [diff] [blame] | 1247 | // StartFunction converted the ABI-lowered parameter(s) into a |
| 1248 | // local alloca. We need to turn that into an r-value suitable |
| 1249 | // for EmitCall. |
John McCall | 413ebdb | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 1250 | llvm::Value *local = GetAddrOfLocalVar(param); |
John McCall | 2736071 | 2010-05-26 22:34:26 +0000 | [diff] [blame] | 1251 | |
John McCall | 413ebdb | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 1252 | QualType type = param->getType(); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1253 | |
John McCall | 2736071 | 2010-05-26 22:34:26 +0000 | [diff] [blame] | 1254 | // For the most part, we just need to load the alloca, except: |
| 1255 | // 1) aggregate r-values are actually pointers to temporaries, and |
| 1256 | // 2) references to aggregates are pointers directly to the aggregate. |
| 1257 | // I don't know why references to non-aggregates are different here. |
John McCall | 413ebdb | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 1258 | if (const ReferenceType *ref = type->getAs<ReferenceType>()) { |
| 1259 | if (hasAggregateLLVMType(ref->getPointeeType())) |
| 1260 | return args.add(RValue::getAggregate(local), type); |
John McCall | 2736071 | 2010-05-26 22:34:26 +0000 | [diff] [blame] | 1261 | |
| 1262 | // Locals which are references to scalars are represented |
| 1263 | // with allocas holding the pointer. |
John McCall | 413ebdb | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 1264 | return args.add(RValue::get(Builder.CreateLoad(local)), type); |
John McCall | 2736071 | 2010-05-26 22:34:26 +0000 | [diff] [blame] | 1265 | } |
| 1266 | |
John McCall | 413ebdb | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 1267 | if (type->isAnyComplexType()) { |
| 1268 | ComplexPairTy complex = LoadComplexFromAddr(local, /*volatile*/ false); |
| 1269 | return args.add(RValue::getComplex(complex), type); |
| 1270 | } |
John McCall | 2736071 | 2010-05-26 22:34:26 +0000 | [diff] [blame] | 1271 | |
John McCall | 413ebdb | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 1272 | if (hasAggregateLLVMType(type)) |
| 1273 | return args.add(RValue::getAggregate(local), type); |
John McCall | 2736071 | 2010-05-26 22:34:26 +0000 | [diff] [blame] | 1274 | |
John McCall | 413ebdb | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 1275 | unsigned alignment = getContext().getDeclAlign(param).getQuantity(); |
| 1276 | llvm::Value *value = EmitLoadOfScalar(local, false, alignment, type); |
| 1277 | return args.add(RValue::get(value), type); |
John McCall | 2736071 | 2010-05-26 22:34:26 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1280 | static bool isProvablyNull(llvm::Value *addr) { |
| 1281 | return isa<llvm::ConstantPointerNull>(addr); |
| 1282 | } |
| 1283 | |
| 1284 | static bool isProvablyNonNull(llvm::Value *addr) { |
| 1285 | return isa<llvm::AllocaInst>(addr); |
| 1286 | } |
| 1287 | |
| 1288 | /// Emit the actual writing-back of a writeback. |
| 1289 | static void emitWriteback(CodeGenFunction &CGF, |
| 1290 | const CallArgList::Writeback &writeback) { |
| 1291 | llvm::Value *srcAddr = writeback.Address; |
| 1292 | assert(!isProvablyNull(srcAddr) && |
| 1293 | "shouldn't have writeback for provably null argument"); |
| 1294 | |
| 1295 | llvm::BasicBlock *contBB = 0; |
| 1296 | |
| 1297 | // If the argument wasn't provably non-null, we need to null check |
| 1298 | // before doing the store. |
| 1299 | bool provablyNonNull = isProvablyNonNull(srcAddr); |
| 1300 | if (!provablyNonNull) { |
| 1301 | llvm::BasicBlock *writebackBB = CGF.createBasicBlock("icr.writeback"); |
| 1302 | contBB = CGF.createBasicBlock("icr.done"); |
| 1303 | |
| 1304 | llvm::Value *isNull = CGF.Builder.CreateIsNull(srcAddr, "icr.isnull"); |
| 1305 | CGF.Builder.CreateCondBr(isNull, contBB, writebackBB); |
| 1306 | CGF.EmitBlock(writebackBB); |
| 1307 | } |
| 1308 | |
| 1309 | // Load the value to writeback. |
| 1310 | llvm::Value *value = CGF.Builder.CreateLoad(writeback.Temporary); |
| 1311 | |
| 1312 | // Cast it back, in case we're writing an id to a Foo* or something. |
| 1313 | value = CGF.Builder.CreateBitCast(value, |
| 1314 | cast<llvm::PointerType>(srcAddr->getType())->getElementType(), |
| 1315 | "icr.writeback-cast"); |
| 1316 | |
| 1317 | // Perform the writeback. |
| 1318 | QualType srcAddrType = writeback.AddressType; |
| 1319 | CGF.EmitStoreThroughLValue(RValue::get(value), |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1320 | CGF.MakeAddrLValue(srcAddr, srcAddrType)); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1321 | |
| 1322 | // Jump to the continuation block. |
| 1323 | if (!provablyNonNull) |
| 1324 | CGF.EmitBlock(contBB); |
| 1325 | } |
| 1326 | |
| 1327 | static void emitWritebacks(CodeGenFunction &CGF, |
| 1328 | const CallArgList &args) { |
| 1329 | for (CallArgList::writeback_iterator |
| 1330 | i = args.writeback_begin(), e = args.writeback_end(); i != e; ++i) |
| 1331 | emitWriteback(CGF, *i); |
| 1332 | } |
| 1333 | |
| 1334 | /// Emit an argument that's being passed call-by-writeback. That is, |
| 1335 | /// we are passing the address of |
| 1336 | static void emitWritebackArg(CodeGenFunction &CGF, CallArgList &args, |
| 1337 | const ObjCIndirectCopyRestoreExpr *CRE) { |
| 1338 | llvm::Value *srcAddr = CGF.EmitScalarExpr(CRE->getSubExpr()); |
| 1339 | |
| 1340 | // The dest and src types don't necessarily match in LLVM terms |
| 1341 | // because of the crazy ObjC compatibility rules. |
| 1342 | |
| 1343 | const llvm::PointerType *destType = |
| 1344 | cast<llvm::PointerType>(CGF.ConvertType(CRE->getType())); |
| 1345 | |
| 1346 | // If the address is a constant null, just pass the appropriate null. |
| 1347 | if (isProvablyNull(srcAddr)) { |
| 1348 | args.add(RValue::get(llvm::ConstantPointerNull::get(destType)), |
| 1349 | CRE->getType()); |
| 1350 | return; |
| 1351 | } |
| 1352 | |
| 1353 | QualType srcAddrType = |
| 1354 | CRE->getSubExpr()->getType()->castAs<PointerType>()->getPointeeType(); |
| 1355 | |
| 1356 | // Create the temporary. |
| 1357 | llvm::Value *temp = CGF.CreateTempAlloca(destType->getElementType(), |
| 1358 | "icr.temp"); |
| 1359 | |
| 1360 | // Zero-initialize it if we're not doing a copy-initialization. |
| 1361 | bool shouldCopy = CRE->shouldCopy(); |
| 1362 | if (!shouldCopy) { |
| 1363 | llvm::Value *null = |
| 1364 | llvm::ConstantPointerNull::get( |
| 1365 | cast<llvm::PointerType>(destType->getElementType())); |
| 1366 | CGF.Builder.CreateStore(null, temp); |
| 1367 | } |
| 1368 | |
| 1369 | llvm::BasicBlock *contBB = 0; |
| 1370 | |
| 1371 | // If the address is *not* known to be non-null, we need to switch. |
| 1372 | llvm::Value *finalArgument; |
| 1373 | |
| 1374 | bool provablyNonNull = isProvablyNonNull(srcAddr); |
| 1375 | if (provablyNonNull) { |
| 1376 | finalArgument = temp; |
| 1377 | } else { |
| 1378 | llvm::Value *isNull = CGF.Builder.CreateIsNull(srcAddr, "icr.isnull"); |
| 1379 | |
| 1380 | finalArgument = CGF.Builder.CreateSelect(isNull, |
| 1381 | llvm::ConstantPointerNull::get(destType), |
| 1382 | temp, "icr.argument"); |
| 1383 | |
| 1384 | // If we need to copy, then the load has to be conditional, which |
| 1385 | // means we need control flow. |
| 1386 | if (shouldCopy) { |
| 1387 | contBB = CGF.createBasicBlock("icr.cont"); |
| 1388 | llvm::BasicBlock *copyBB = CGF.createBasicBlock("icr.copy"); |
| 1389 | CGF.Builder.CreateCondBr(isNull, contBB, copyBB); |
| 1390 | CGF.EmitBlock(copyBB); |
| 1391 | } |
| 1392 | } |
| 1393 | |
| 1394 | // Perform a copy if necessary. |
| 1395 | if (shouldCopy) { |
| 1396 | LValue srcLV = CGF.MakeAddrLValue(srcAddr, srcAddrType); |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1397 | RValue srcRV = CGF.EmitLoadOfLValue(srcLV); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1398 | assert(srcRV.isScalar()); |
| 1399 | |
| 1400 | llvm::Value *src = srcRV.getScalarVal(); |
| 1401 | src = CGF.Builder.CreateBitCast(src, destType->getElementType(), |
| 1402 | "icr.cast"); |
| 1403 | |
| 1404 | // Use an ordinary store, not a store-to-lvalue. |
| 1405 | CGF.Builder.CreateStore(src, temp); |
| 1406 | } |
| 1407 | |
| 1408 | // Finish the control flow if we needed it. |
| 1409 | if (shouldCopy && !provablyNonNull) |
| 1410 | CGF.EmitBlock(contBB); |
| 1411 | |
| 1412 | args.addWriteback(srcAddr, srcAddrType, temp); |
| 1413 | args.add(RValue::get(finalArgument), CRE->getType()); |
| 1414 | } |
| 1415 | |
John McCall | 413ebdb | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 1416 | void CodeGenFunction::EmitCallArg(CallArgList &args, const Expr *E, |
| 1417 | QualType type) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1418 | if (const ObjCIndirectCopyRestoreExpr *CRE |
| 1419 | = dyn_cast<ObjCIndirectCopyRestoreExpr>(E)) { |
| 1420 | assert(getContext().getLangOptions().ObjCAutoRefCount); |
| 1421 | assert(getContext().hasSameType(E->getType(), type)); |
| 1422 | return emitWritebackArg(*this, args, CRE); |
| 1423 | } |
| 1424 | |
John McCall | 413ebdb | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 1425 | if (type->isReferenceType()) |
| 1426 | return args.add(EmitReferenceBindingToExpr(E, /*InitializedDecl=*/0), |
| 1427 | type); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1428 | |
Eli Friedman | 70cbd2a | 2011-06-15 18:26:32 +0000 | [diff] [blame] | 1429 | if (hasAggregateLLVMType(type) && !E->getType()->isAnyComplexType() && |
| 1430 | isa<ImplicitCastExpr>(E) && |
Eli Friedman | 55d4848 | 2011-05-26 00:10:27 +0000 | [diff] [blame] | 1431 | cast<CastExpr>(E)->getCastKind() == CK_LValueToRValue) { |
| 1432 | LValue L = EmitLValue(cast<CastExpr>(E)->getSubExpr()); |
| 1433 | assert(L.isSimple()); |
| 1434 | args.add(RValue::getAggregate(L.getAddress(), L.isVolatileQualified()), |
| 1435 | type, /*NeedsCopy*/true); |
| 1436 | return; |
| 1437 | } |
| 1438 | |
John McCall | 413ebdb | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 1439 | args.add(EmitAnyExprToTemp(E), type); |
Anders Carlsson | 0139bb9 | 2009-04-08 20:47:54 +0000 | [diff] [blame] | 1440 | } |
| 1441 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1442 | /// Emits a call or invoke instruction to the given function, depending |
| 1443 | /// on the current state of the EH stack. |
| 1444 | llvm::CallSite |
| 1445 | CodeGenFunction::EmitCallOrInvoke(llvm::Value *Callee, |
| 1446 | llvm::Value * const *ArgBegin, |
| 1447 | llvm::Value * const *ArgEnd, |
| 1448 | const llvm::Twine &Name) { |
| 1449 | llvm::BasicBlock *InvokeDest = getInvokeDest(); |
| 1450 | if (!InvokeDest) |
| 1451 | return Builder.CreateCall(Callee, ArgBegin, ArgEnd, Name); |
| 1452 | |
| 1453 | llvm::BasicBlock *ContBB = createBasicBlock("invoke.cont"); |
| 1454 | llvm::InvokeInst *Invoke = Builder.CreateInvoke(Callee, ContBB, InvokeDest, |
| 1455 | ArgBegin, ArgEnd, Name); |
| 1456 | EmitBlock(ContBB); |
| 1457 | return Invoke; |
| 1458 | } |
| 1459 | |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1460 | RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1461 | llvm::Value *Callee, |
Anders Carlsson | f3c47c9 | 2009-12-24 19:25:24 +0000 | [diff] [blame] | 1462 | ReturnValueSlot ReturnValue, |
Daniel Dunbar | c0ef9f5 | 2009-02-20 18:06:48 +0000 | [diff] [blame] | 1463 | const CallArgList &CallArgs, |
David Chisnall | dd5c98f | 2010-05-01 11:15:56 +0000 | [diff] [blame] | 1464 | const Decl *TargetDecl, |
David Chisnall | 4b02afc | 2010-05-02 13:41:58 +0000 | [diff] [blame] | 1465 | llvm::Instruction **callOrInvoke) { |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1466 | // FIXME: We no longer need the types from CallArgs; lift up and simplify. |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1467 | llvm::SmallVector<llvm::Value*, 16> Args; |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1468 | |
| 1469 | // Handle struct-return functions by passing a pointer to the |
| 1470 | // location that we would like to return into. |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 1471 | QualType RetTy = CallInfo.getReturnType(); |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 1472 | const ABIArgInfo &RetAI = CallInfo.getReturnInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1473 | |
| 1474 | |
Chris Lattner | 5db7ae5 | 2009-06-13 00:26:38 +0000 | [diff] [blame] | 1475 | // If the call returns a temporary with struct return, create a temporary |
Anders Carlsson | d2490a9 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 1476 | // alloca to hold the result, unless one is given to us. |
Daniel Dunbar | dacf9dd | 2010-07-14 23:39:36 +0000 | [diff] [blame] | 1477 | if (CGM.ReturnTypeUsesSRet(CallInfo)) { |
Anders Carlsson | d2490a9 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 1478 | llvm::Value *Value = ReturnValue.getValue(); |
| 1479 | if (!Value) |
Daniel Dunbar | 195337d | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 1480 | Value = CreateMemTemp(RetTy); |
Anders Carlsson | d2490a9 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 1481 | Args.push_back(Value); |
| 1482 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1483 | |
Daniel Dunbar | 4b5f0a4 | 2009-02-04 21:17:21 +0000 | [diff] [blame] | 1484 | assert(CallInfo.arg_size() == CallArgs.size() && |
| 1485 | "Mismatch between function signature & arguments."); |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 1486 | CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1487 | for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end(); |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 1488 | I != E; ++I, ++info_it) { |
| 1489 | const ABIArgInfo &ArgInfo = info_it->info; |
Eli Friedman | c6d0782 | 2011-05-02 18:05:27 +0000 | [diff] [blame] | 1490 | RValue RV = I->RV; |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1491 | |
Eli Friedman | 97cb5a4 | 2011-06-15 22:09:18 +0000 | [diff] [blame] | 1492 | unsigned TypeAlign = |
Eli Friedman | c6d0782 | 2011-05-02 18:05:27 +0000 | [diff] [blame] | 1493 | getContext().getTypeAlignInChars(I->Ty).getQuantity(); |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1494 | switch (ArgInfo.getKind()) { |
Daniel Dunbar | 91a16fa | 2010-08-21 02:24:36 +0000 | [diff] [blame] | 1495 | case ABIArgInfo::Indirect: { |
Daniel Dunbar | 1f74598 | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 1496 | if (RV.isScalar() || RV.isComplex()) { |
| 1497 | // Make a temporary alloca to pass the argument. |
Eli Friedman | 70cbd2a | 2011-06-15 18:26:32 +0000 | [diff] [blame] | 1498 | llvm::AllocaInst *AI = CreateMemTemp(I->Ty); |
| 1499 | if (ArgInfo.getIndirectAlign() > AI->getAlignment()) |
| 1500 | AI->setAlignment(ArgInfo.getIndirectAlign()); |
| 1501 | Args.push_back(AI); |
Daniel Dunbar | 1f74598 | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 1502 | if (RV.isScalar()) |
Daniel Dunbar | 91a16fa | 2010-08-21 02:24:36 +0000 | [diff] [blame] | 1503 | EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false, |
Eli Friedman | 97cb5a4 | 2011-06-15 22:09:18 +0000 | [diff] [blame] | 1504 | TypeAlign, I->Ty); |
Daniel Dunbar | 1f74598 | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 1505 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1506 | StoreComplexToAddr(RV.getComplexVal(), Args.back(), false); |
Daniel Dunbar | 1f74598 | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 1507 | } else { |
Eli Friedman | ea5e4da | 2011-06-14 01:37:52 +0000 | [diff] [blame] | 1508 | // We want to avoid creating an unnecessary temporary+copy here; |
| 1509 | // however, we need one in two cases: |
| 1510 | // 1. If the argument is not byval, and we are required to copy the |
| 1511 | // source. (This case doesn't occur on any common architecture.) |
| 1512 | // 2. If the argument is byval, RV is not sufficiently aligned, and |
| 1513 | // we cannot force it to be sufficiently aligned. |
Eli Friedman | 97cb5a4 | 2011-06-15 22:09:18 +0000 | [diff] [blame] | 1514 | llvm::Value *Addr = RV.getAggregateAddr(); |
| 1515 | unsigned Align = ArgInfo.getIndirectAlign(); |
| 1516 | const llvm::TargetData *TD = &CGM.getTargetData(); |
| 1517 | if ((!ArgInfo.getIndirectByVal() && I->NeedsCopy) || |
| 1518 | (ArgInfo.getIndirectByVal() && TypeAlign < Align && |
| 1519 | llvm::getOrEnforceKnownAlignment(Addr, Align, TD) < Align)) { |
Eli Friedman | ea5e4da | 2011-06-14 01:37:52 +0000 | [diff] [blame] | 1520 | // Create an aligned temporary, and copy to it. |
Eli Friedman | 97cb5a4 | 2011-06-15 22:09:18 +0000 | [diff] [blame] | 1521 | llvm::AllocaInst *AI = CreateMemTemp(I->Ty); |
| 1522 | if (Align > AI->getAlignment()) |
| 1523 | AI->setAlignment(Align); |
Eli Friedman | ea5e4da | 2011-06-14 01:37:52 +0000 | [diff] [blame] | 1524 | Args.push_back(AI); |
Eli Friedman | 97cb5a4 | 2011-06-15 22:09:18 +0000 | [diff] [blame] | 1525 | EmitAggregateCopy(AI, Addr, I->Ty, RV.isVolatileQualified()); |
Eli Friedman | ea5e4da | 2011-06-14 01:37:52 +0000 | [diff] [blame] | 1526 | } else { |
| 1527 | // Skip the extra memcpy call. |
Eli Friedman | 97cb5a4 | 2011-06-15 22:09:18 +0000 | [diff] [blame] | 1528 | Args.push_back(Addr); |
Eli Friedman | ea5e4da | 2011-06-14 01:37:52 +0000 | [diff] [blame] | 1529 | } |
Daniel Dunbar | 1f74598 | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 1530 | } |
| 1531 | break; |
Daniel Dunbar | 91a16fa | 2010-08-21 02:24:36 +0000 | [diff] [blame] | 1532 | } |
Daniel Dunbar | 1f74598 | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 1533 | |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1534 | case ABIArgInfo::Ignore: |
| 1535 | break; |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1536 | |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1537 | case ABIArgInfo::Extend: |
| 1538 | case ABIArgInfo::Direct: { |
| 1539 | if (!isa<llvm::StructType>(ArgInfo.getCoerceToType()) && |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 1540 | ArgInfo.getCoerceToType() == ConvertType(info_it->type) && |
| 1541 | ArgInfo.getDirectOffset() == 0) { |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1542 | if (RV.isScalar()) |
| 1543 | Args.push_back(RV.getScalarVal()); |
| 1544 | else |
| 1545 | Args.push_back(Builder.CreateLoad(RV.getAggregateAddr())); |
| 1546 | break; |
| 1547 | } |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1548 | |
Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 1549 | // FIXME: Avoid the conversion through memory if possible. |
| 1550 | llvm::Value *SrcPtr; |
| 1551 | if (RV.isScalar()) { |
Eli Friedman | c6d0782 | 2011-05-02 18:05:27 +0000 | [diff] [blame] | 1552 | SrcPtr = CreateMemTemp(I->Ty, "coerce"); |
Eli Friedman | 97cb5a4 | 2011-06-15 22:09:18 +0000 | [diff] [blame] | 1553 | EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false, TypeAlign, I->Ty); |
Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 1554 | } else if (RV.isComplex()) { |
Eli Friedman | c6d0782 | 2011-05-02 18:05:27 +0000 | [diff] [blame] | 1555 | SrcPtr = CreateMemTemp(I->Ty, "coerce"); |
Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 1556 | StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1557 | } else |
Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 1558 | SrcPtr = RV.getAggregateAddr(); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1559 | |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 1560 | // If the value is offset in memory, apply the offset now. |
| 1561 | if (unsigned Offs = ArgInfo.getDirectOffset()) { |
| 1562 | SrcPtr = Builder.CreateBitCast(SrcPtr, Builder.getInt8PtrTy()); |
| 1563 | SrcPtr = Builder.CreateConstGEP1_32(SrcPtr, Offs); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1564 | SrcPtr = Builder.CreateBitCast(SrcPtr, |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 1565 | llvm::PointerType::getUnqual(ArgInfo.getCoerceToType())); |
| 1566 | |
| 1567 | } |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1568 | |
Chris Lattner | ce70016 | 2010-06-28 23:44:11 +0000 | [diff] [blame] | 1569 | // If the coerce-to type is a first class aggregate, we flatten it and |
| 1570 | // pass the elements. Either way is semantically identical, but fast-isel |
| 1571 | // and the optimizer generally likes scalar values better than FCAs. |
| 1572 | if (const llvm::StructType *STy = |
Chris Lattner | 309c59f | 2010-06-29 00:06:42 +0000 | [diff] [blame] | 1573 | dyn_cast<llvm::StructType>(ArgInfo.getCoerceToType())) { |
Chris Lattner | 9282688 | 2010-07-05 20:41:41 +0000 | [diff] [blame] | 1574 | SrcPtr = Builder.CreateBitCast(SrcPtr, |
| 1575 | llvm::PointerType::getUnqual(STy)); |
| 1576 | for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { |
| 1577 | llvm::Value *EltPtr = Builder.CreateConstGEP2_32(SrcPtr, 0, i); |
Chris Lattner | deabde2 | 2010-07-28 18:24:28 +0000 | [diff] [blame] | 1578 | llvm::LoadInst *LI = Builder.CreateLoad(EltPtr); |
| 1579 | // We don't know what we're loading from. |
| 1580 | LI->setAlignment(1); |
| 1581 | Args.push_back(LI); |
Chris Lattner | 309c59f | 2010-06-29 00:06:42 +0000 | [diff] [blame] | 1582 | } |
Chris Lattner | ce70016 | 2010-06-28 23:44:11 +0000 | [diff] [blame] | 1583 | } else { |
Chris Lattner | 309c59f | 2010-06-29 00:06:42 +0000 | [diff] [blame] | 1584 | // In the simple case, just pass the coerced loaded value. |
| 1585 | Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(), |
| 1586 | *this)); |
Chris Lattner | ce70016 | 2010-06-28 23:44:11 +0000 | [diff] [blame] | 1587 | } |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1588 | |
Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 1589 | break; |
| 1590 | } |
| 1591 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1592 | case ABIArgInfo::Expand: |
Eli Friedman | c6d0782 | 2011-05-02 18:05:27 +0000 | [diff] [blame] | 1593 | ExpandTypeToArgs(I->Ty, RV, Args); |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1594 | break; |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1595 | } |
| 1596 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1597 | |
Chris Lattner | 5db7ae5 | 2009-06-13 00:26:38 +0000 | [diff] [blame] | 1598 | // If the callee is a bitcast of a function to a varargs pointer to function |
| 1599 | // type, check to see if we can remove the bitcast. This handles some cases |
| 1600 | // with unprototyped functions. |
| 1601 | if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Callee)) |
| 1602 | if (llvm::Function *CalleeF = dyn_cast<llvm::Function>(CE->getOperand(0))) { |
| 1603 | const llvm::PointerType *CurPT=cast<llvm::PointerType>(Callee->getType()); |
| 1604 | const llvm::FunctionType *CurFT = |
| 1605 | cast<llvm::FunctionType>(CurPT->getElementType()); |
| 1606 | const llvm::FunctionType *ActualFT = CalleeF->getFunctionType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1607 | |
Chris Lattner | 5db7ae5 | 2009-06-13 00:26:38 +0000 | [diff] [blame] | 1608 | if (CE->getOpcode() == llvm::Instruction::BitCast && |
| 1609 | ActualFT->getReturnType() == CurFT->getReturnType() && |
Chris Lattner | d6bebbf | 2009-06-23 01:38:41 +0000 | [diff] [blame] | 1610 | ActualFT->getNumParams() == CurFT->getNumParams() && |
Fariborz Jahanian | c0ddef2 | 2011-03-01 17:28:13 +0000 | [diff] [blame] | 1611 | ActualFT->getNumParams() == Args.size() && |
| 1612 | (CurFT->isVarArg() || !ActualFT->isVarArg())) { |
Chris Lattner | 5db7ae5 | 2009-06-13 00:26:38 +0000 | [diff] [blame] | 1613 | bool ArgsMatch = true; |
| 1614 | for (unsigned i = 0, e = ActualFT->getNumParams(); i != e; ++i) |
| 1615 | if (ActualFT->getParamType(i) != CurFT->getParamType(i)) { |
| 1616 | ArgsMatch = false; |
| 1617 | break; |
| 1618 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1619 | |
Chris Lattner | 5db7ae5 | 2009-06-13 00:26:38 +0000 | [diff] [blame] | 1620 | // Strip the cast if we can get away with it. This is a nice cleanup, |
| 1621 | // but also allows us to inline the function at -O0 if it is marked |
| 1622 | // always_inline. |
| 1623 | if (ArgsMatch) |
| 1624 | Callee = CalleeF; |
| 1625 | } |
| 1626 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1627 | |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1628 | |
Daniel Dunbar | ca6408c | 2009-09-12 00:59:20 +0000 | [diff] [blame] | 1629 | unsigned CallingConv; |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1630 | CodeGen::AttributeListType AttributeList; |
Daniel Dunbar | ca6408c | 2009-09-12 00:59:20 +0000 | [diff] [blame] | 1631 | CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList, CallingConv); |
Daniel Dunbar | 9834ffb | 2009-02-23 17:26:39 +0000 | [diff] [blame] | 1632 | llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(), |
| 1633 | AttributeList.end()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1634 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1635 | llvm::BasicBlock *InvokeDest = 0; |
| 1636 | if (!(Attrs.getFnAttributes() & llvm::Attribute::NoUnwind)) |
| 1637 | InvokeDest = getInvokeDest(); |
| 1638 | |
Daniel Dunbar | d14151d | 2009-03-02 04:32:35 +0000 | [diff] [blame] | 1639 | llvm::CallSite CS; |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1640 | if (!InvokeDest) { |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1641 | CS = Builder.CreateCall(Callee, Args.data(), Args.data()+Args.size()); |
Daniel Dunbar | 9834ffb | 2009-02-23 17:26:39 +0000 | [diff] [blame] | 1642 | } else { |
| 1643 | llvm::BasicBlock *Cont = createBasicBlock("invoke.cont"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1644 | CS = Builder.CreateInvoke(Callee, Cont, InvokeDest, |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1645 | Args.data(), Args.data()+Args.size()); |
Daniel Dunbar | 9834ffb | 2009-02-23 17:26:39 +0000 | [diff] [blame] | 1646 | EmitBlock(Cont); |
Daniel Dunbar | f4fe0f0 | 2009-02-20 18:54:31 +0000 | [diff] [blame] | 1647 | } |
Chris Lattner | ce93399 | 2010-06-29 16:40:28 +0000 | [diff] [blame] | 1648 | if (callOrInvoke) |
David Chisnall | 4b02afc | 2010-05-02 13:41:58 +0000 | [diff] [blame] | 1649 | *callOrInvoke = CS.getInstruction(); |
Daniel Dunbar | f4fe0f0 | 2009-02-20 18:54:31 +0000 | [diff] [blame] | 1650 | |
Daniel Dunbar | d14151d | 2009-03-02 04:32:35 +0000 | [diff] [blame] | 1651 | CS.setAttributes(Attrs); |
Daniel Dunbar | ca6408c | 2009-09-12 00:59:20 +0000 | [diff] [blame] | 1652 | CS.setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv)); |
Daniel Dunbar | d14151d | 2009-03-02 04:32:35 +0000 | [diff] [blame] | 1653 | |
| 1654 | // If the call doesn't return, finish the basic block and clear the |
| 1655 | // insertion point; this allows the rest of IRgen to discard |
| 1656 | // unreachable code. |
| 1657 | if (CS.doesNotReturn()) { |
| 1658 | Builder.CreateUnreachable(); |
| 1659 | Builder.ClearInsertionPoint(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1660 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1661 | // FIXME: For now, emit a dummy basic block because expr emitters in |
| 1662 | // generally are not ready to handle emitting expressions at unreachable |
| 1663 | // points. |
Daniel Dunbar | d14151d | 2009-03-02 04:32:35 +0000 | [diff] [blame] | 1664 | EnsureInsertPoint(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1665 | |
Daniel Dunbar | d14151d | 2009-03-02 04:32:35 +0000 | [diff] [blame] | 1666 | // Return a reasonable RValue. |
| 1667 | return GetUndefRValue(RetTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1668 | } |
Daniel Dunbar | d14151d | 2009-03-02 04:32:35 +0000 | [diff] [blame] | 1669 | |
| 1670 | llvm::Instruction *CI = CS.getInstruction(); |
Benjamin Kramer | ffbb15e | 2009-10-05 13:47:21 +0000 | [diff] [blame] | 1671 | if (Builder.isNamePreserving() && !CI->getType()->isVoidTy()) |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1672 | CI->setName("call"); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1673 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1674 | // Emit any writebacks immediately. Arguably this should happen |
| 1675 | // after any return-value munging. |
| 1676 | if (CallArgs.hasWritebacks()) |
| 1677 | emitWritebacks(*this, CallArgs); |
| 1678 | |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1679 | switch (RetAI.getKind()) { |
Daniel Dunbar | 91a16fa | 2010-08-21 02:24:36 +0000 | [diff] [blame] | 1680 | case ABIArgInfo::Indirect: { |
| 1681 | unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity(); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1682 | if (RetTy->isAnyComplexType()) |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1683 | return RValue::getComplex(LoadComplexFromAddr(Args[0], false)); |
Chris Lattner | 3403084 | 2009-03-22 00:32:22 +0000 | [diff] [blame] | 1684 | if (CodeGenFunction::hasAggregateLLVMType(RetTy)) |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1685 | return RValue::getAggregate(Args[0]); |
Daniel Dunbar | 91a16fa | 2010-08-21 02:24:36 +0000 | [diff] [blame] | 1686 | return RValue::get(EmitLoadOfScalar(Args[0], false, Alignment, RetTy)); |
| 1687 | } |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1688 | |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1689 | case ABIArgInfo::Ignore: |
Daniel Dunbar | 0bcc521 | 2009-02-03 06:30:17 +0000 | [diff] [blame] | 1690 | // If we are ignoring an argument that had a result, make sure to |
| 1691 | // construct the appropriate return value for our caller. |
Daniel Dunbar | 13e8173 | 2009-02-05 07:09:07 +0000 | [diff] [blame] | 1692 | return GetUndefRValue(RetTy); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1693 | |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1694 | case ABIArgInfo::Extend: |
| 1695 | case ABIArgInfo::Direct: { |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 1696 | if (RetAI.getCoerceToType() == ConvertType(RetTy) && |
| 1697 | RetAI.getDirectOffset() == 0) { |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1698 | if (RetTy->isAnyComplexType()) { |
| 1699 | llvm::Value *Real = Builder.CreateExtractValue(CI, 0); |
| 1700 | llvm::Value *Imag = Builder.CreateExtractValue(CI, 1); |
| 1701 | return RValue::getComplex(std::make_pair(Real, Imag)); |
| 1702 | } |
| 1703 | if (CodeGenFunction::hasAggregateLLVMType(RetTy)) { |
| 1704 | llvm::Value *DestPtr = ReturnValue.getValue(); |
| 1705 | bool DestIsVolatile = ReturnValue.isVolatile(); |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1706 | |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1707 | if (!DestPtr) { |
| 1708 | DestPtr = CreateMemTemp(RetTy, "agg.tmp"); |
| 1709 | DestIsVolatile = false; |
| 1710 | } |
Eli Friedman | badea57 | 2011-05-17 21:08:01 +0000 | [diff] [blame] | 1711 | BuildAggStore(*this, CI, DestPtr, DestIsVolatile, false); |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1712 | return RValue::getAggregate(DestPtr); |
| 1713 | } |
| 1714 | return RValue::get(CI); |
| 1715 | } |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1716 | |
Anders Carlsson | d2490a9 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 1717 | llvm::Value *DestPtr = ReturnValue.getValue(); |
| 1718 | bool DestIsVolatile = ReturnValue.isVolatile(); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1719 | |
Anders Carlsson | d2490a9 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 1720 | if (!DestPtr) { |
Daniel Dunbar | 195337d | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 1721 | DestPtr = CreateMemTemp(RetTy, "coerce"); |
Anders Carlsson | d2490a9 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 1722 | DestIsVolatile = false; |
| 1723 | } |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1724 | |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 1725 | // If the value is offset in memory, apply the offset now. |
| 1726 | llvm::Value *StorePtr = DestPtr; |
| 1727 | if (unsigned Offs = RetAI.getDirectOffset()) { |
| 1728 | StorePtr = Builder.CreateBitCast(StorePtr, Builder.getInt8PtrTy()); |
| 1729 | StorePtr = Builder.CreateConstGEP1_32(StorePtr, Offs); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1730 | StorePtr = Builder.CreateBitCast(StorePtr, |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 1731 | llvm::PointerType::getUnqual(RetAI.getCoerceToType())); |
| 1732 | } |
| 1733 | CreateCoercedStore(CI, StorePtr, DestIsVolatile, *this); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1734 | |
Daniel Dunbar | 91a16fa | 2010-08-21 02:24:36 +0000 | [diff] [blame] | 1735 | unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity(); |
Anders Carlsson | ad3d691 | 2008-11-25 22:21:48 +0000 | [diff] [blame] | 1736 | if (RetTy->isAnyComplexType()) |
Anders Carlsson | d2490a9 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 1737 | return RValue::getComplex(LoadComplexFromAddr(DestPtr, false)); |
Chris Lattner | 3403084 | 2009-03-22 00:32:22 +0000 | [diff] [blame] | 1738 | if (CodeGenFunction::hasAggregateLLVMType(RetTy)) |
Anders Carlsson | d2490a9 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 1739 | return RValue::getAggregate(DestPtr); |
Daniel Dunbar | 91a16fa | 2010-08-21 02:24:36 +0000 | [diff] [blame] | 1740 | return RValue::get(EmitLoadOfScalar(DestPtr, false, Alignment, RetTy)); |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 1741 | } |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1742 | |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1743 | case ABIArgInfo::Expand: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1744 | assert(0 && "Invalid ABI kind for return argument"); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1745 | } |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1746 | |
| 1747 | assert(0 && "Unhandled ABIArgInfo::Kind"); |
| 1748 | return RValue::get(0); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1749 | } |
Daniel Dunbar | b4094ea | 2009-02-10 20:44:09 +0000 | [diff] [blame] | 1750 | |
| 1751 | /* VarArg handling */ |
| 1752 | |
| 1753 | llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) { |
| 1754 | return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this); |
| 1755 | } |