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