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