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