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" |
| 16 | #include "CodeGenFunction.h" |
Daniel Dunbar | b768807 | 2008-09-10 00:41:16 +0000 | [diff] [blame] | 17 | #include "CodeGenModule.h" |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 18 | #include "clang/Basic/TargetInfo.h" |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 19 | #include "clang/AST/Decl.h" |
Anders Carlsson | f6f8ae5 | 2009-04-03 22:48:58 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclCXX.h" |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclObjC.h" |
Chandler Carruth | 06057ce | 2010-06-15 23:19:56 +0000 | [diff] [blame] | 22 | #include "clang/Frontend/CodeGenOptions.h" |
Devang Patel | d0646bd | 2008-09-24 01:01:36 +0000 | [diff] [blame] | 23 | #include "llvm/Attributes.h" |
Daniel Dunbar | d14151d | 2009-03-02 04:32:35 +0000 | [diff] [blame] | 24 | #include "llvm/Support/CallSite.h" |
Daniel Dunbar | 54d1ccb | 2009-01-27 01:36:03 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetData.h" |
Daniel Dunbar | 9eb5c6d | 2009-02-03 01:05:53 +0000 | [diff] [blame] | 26 | |
| 27 | #include "ABIInfo.h" |
| 28 | |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 29 | using namespace clang; |
| 30 | using namespace CodeGen; |
| 31 | |
| 32 | /***/ |
| 33 | |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 34 | // FIXME: Use iterator and sidestep silly type array creation. |
| 35 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 36 | static unsigned ClangCallConvToLLVMCallConv(CallingConv CC) { |
| 37 | switch (CC) { |
| 38 | default: return llvm::CallingConv::C; |
| 39 | case CC_X86StdCall: return llvm::CallingConv::X86_StdCall; |
| 40 | case CC_X86FastCall: return llvm::CallingConv::X86_FastCall; |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 41 | case CC_X86ThisCall: return llvm::CallingConv::X86_ThisCall; |
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 & |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 68 | CodeGenTypes::getFunctionInfo(CanQual<FunctionNoProtoType> FTNP) { |
| 69 | return getFunctionInfo(FTNP->getResultType().getUnqualifiedType(), |
| 70 | llvm::SmallVector<CanQualType, 16>(), |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 71 | FTNP->getExtInfo()); |
John McCall | 0b0ef0a | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /// \param Args - contains any initial parameters besides those |
| 75 | /// in the formal type |
| 76 | static const CGFunctionInfo &getFunctionInfo(CodeGenTypes &CGT, |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 77 | llvm::SmallVectorImpl<CanQualType> &ArgTys, |
| 78 | CanQual<FunctionProtoType> FTP) { |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 79 | // FIXME: Kill copy. |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 80 | for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i) |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 81 | ArgTys.push_back(FTP->getArgType(i)); |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 82 | CanQualType ResTy = FTP->getResultType().getUnqualifiedType(); |
| 83 | return CGT.getFunctionInfo(ResTy, ArgTys, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 84 | FTP->getExtInfo()); |
John McCall | 0b0ef0a | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | const CGFunctionInfo & |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 88 | CodeGenTypes::getFunctionInfo(CanQual<FunctionProtoType> FTP) { |
| 89 | llvm::SmallVector<CanQualType, 16> ArgTys; |
John McCall | 0b0ef0a | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 90 | return ::getFunctionInfo(*this, ArgTys, FTP); |
Daniel Dunbar | bac7c25 | 2009-09-11 22:24:53 +0000 | [diff] [blame] | 91 | } |
| 92 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 93 | static CallingConv getCallingConventionForDecl(const Decl *D) { |
Daniel Dunbar | bac7c25 | 2009-09-11 22:24:53 +0000 | [diff] [blame] | 94 | // Set the appropriate calling convention for the Function. |
| 95 | if (D->hasAttr<StdCallAttr>()) |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 96 | return CC_X86StdCall; |
Daniel Dunbar | bac7c25 | 2009-09-11 22:24:53 +0000 | [diff] [blame] | 97 | |
| 98 | if (D->hasAttr<FastCallAttr>()) |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 99 | return CC_X86FastCall; |
Daniel Dunbar | bac7c25 | 2009-09-11 22:24:53 +0000 | [diff] [blame] | 100 | |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 101 | if (D->hasAttr<ThisCallAttr>()) |
| 102 | return CC_X86ThisCall; |
| 103 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 104 | return CC_C; |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Anders Carlsson | 375c31c | 2009-10-03 19:43:08 +0000 | [diff] [blame] | 107 | const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXRecordDecl *RD, |
| 108 | const FunctionProtoType *FTP) { |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 109 | llvm::SmallVector<CanQualType, 16> ArgTys; |
John McCall | 0b0ef0a | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 110 | |
Anders Carlsson | 375c31c | 2009-10-03 19:43:08 +0000 | [diff] [blame] | 111 | // Add the 'this' pointer. |
John McCall | 0b0ef0a | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 112 | ArgTys.push_back(GetThisType(Context, RD)); |
| 113 | |
| 114 | return ::getFunctionInfo(*this, ArgTys, |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 115 | FTP->getCanonicalTypeUnqualified().getAs<FunctionProtoType>()); |
Anders Carlsson | 375c31c | 2009-10-03 19:43:08 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Anders Carlsson | f6f8ae5 | 2009-04-03 22:48:58 +0000 | [diff] [blame] | 118 | const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXMethodDecl *MD) { |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 119 | llvm::SmallVector<CanQualType, 16> ArgTys; |
John McCall | 0b0ef0a | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 120 | |
Chris Lattner | 3eb67ca | 2009-05-12 20:27:19 +0000 | [diff] [blame] | 121 | // Add the 'this' pointer unless this is a static method. |
| 122 | if (MD->isInstance()) |
John McCall | 0b0ef0a | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 123 | ArgTys.push_back(GetThisType(Context, MD->getParent())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 124 | |
John McCall | 0b0ef0a | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 125 | return ::getFunctionInfo(*this, ArgTys, GetFormalType(MD)); |
Anders Carlsson | f6f8ae5 | 2009-04-03 22:48:58 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Anders Carlsson | f6c56e2 | 2009-11-25 03:15:49 +0000 | [diff] [blame] | 128 | const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXConstructorDecl *D, |
| 129 | CXXCtorType Type) { |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 130 | llvm::SmallVector<CanQualType, 16> ArgTys; |
Anders Carlsson | f6c56e2 | 2009-11-25 03:15:49 +0000 | [diff] [blame] | 131 | |
| 132 | // Add the 'this' pointer. |
John McCall | 0b0ef0a | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 133 | ArgTys.push_back(GetThisType(Context, D->getParent())); |
Anders Carlsson | f6c56e2 | 2009-11-25 03:15:49 +0000 | [diff] [blame] | 134 | |
| 135 | // Check if we need to add a VTT parameter (which has type void **). |
| 136 | if (Type == Ctor_Base && D->getParent()->getNumVBases() != 0) |
| 137 | ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy)); |
John McCall | 0b0ef0a | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 138 | |
| 139 | return ::getFunctionInfo(*this, ArgTys, GetFormalType(D)); |
Anders Carlsson | f6c56e2 | 2009-11-25 03:15:49 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXDestructorDecl *D, |
| 143 | CXXDtorType Type) { |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 144 | llvm::SmallVector<CanQualType, 16> ArgTys; |
Anders Carlsson | f6c56e2 | 2009-11-25 03:15:49 +0000 | [diff] [blame] | 145 | |
| 146 | // Add the 'this' pointer. |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 147 | ArgTys.push_back(GetThisType(Context, D->getParent())); |
Anders Carlsson | f6c56e2 | 2009-11-25 03:15:49 +0000 | [diff] [blame] | 148 | |
| 149 | // Check if we need to add a VTT parameter (which has type void **). |
| 150 | if (Type == Dtor_Base && D->getParent()->getNumVBases() != 0) |
| 151 | ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy)); |
John McCall | 0b0ef0a | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 152 | |
| 153 | return ::getFunctionInfo(*this, ArgTys, GetFormalType(D)); |
Anders Carlsson | f6c56e2 | 2009-11-25 03:15:49 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 156 | const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) { |
Chris Lattner | 3eb67ca | 2009-05-12 20:27:19 +0000 | [diff] [blame] | 157 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) |
Anders Carlsson | f6f8ae5 | 2009-04-03 22:48:58 +0000 | [diff] [blame] | 158 | if (MD->isInstance()) |
| 159 | return getFunctionInfo(MD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 160 | |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 161 | CanQualType FTy = FD->getType()->getCanonicalTypeUnqualified(); |
| 162 | assert(isa<FunctionType>(FTy)); |
John McCall | 0b0ef0a | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 163 | if (isa<FunctionNoProtoType>(FTy)) |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 164 | return getFunctionInfo(FTy.getAs<FunctionNoProtoType>()); |
| 165 | assert(isa<FunctionProtoType>(FTy)); |
| 166 | return getFunctionInfo(FTy.getAs<FunctionProtoType>()); |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 169 | const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) { |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 170 | llvm::SmallVector<CanQualType, 16> ArgTys; |
| 171 | ArgTys.push_back(Context.getCanonicalParamType(MD->getSelfDecl()->getType())); |
| 172 | ArgTys.push_back(Context.getCanonicalParamType(Context.getObjCSelType())); |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 173 | // FIXME: Kill copy? |
Chris Lattner | 2073216 | 2009-02-20 06:23:21 +0000 | [diff] [blame] | 174 | for (ObjCMethodDecl::param_iterator i = MD->param_begin(), |
John McCall | 0b0ef0a | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 175 | e = MD->param_end(); i != e; ++i) { |
| 176 | ArgTys.push_back(Context.getCanonicalParamType((*i)->getType())); |
| 177 | } |
| 178 | return getFunctionInfo(GetReturnType(MD->getResultType()), |
| 179 | ArgTys, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 180 | FunctionType::ExtInfo( |
| 181 | /*NoReturn*/ false, |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 182 | /*RegParm*/ 0, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 183 | getCallingConventionForDecl(MD))); |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Anders Carlsson | b2bcf1c | 2010-02-06 02:44:09 +0000 | [diff] [blame] | 186 | const CGFunctionInfo &CodeGenTypes::getFunctionInfo(GlobalDecl GD) { |
| 187 | // FIXME: Do we need to handle ObjCMethodDecl? |
| 188 | const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl()); |
| 189 | |
| 190 | if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) |
| 191 | return getFunctionInfo(CD, GD.getCtorType()); |
| 192 | |
| 193 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD)) |
| 194 | return getFunctionInfo(DD, GD.getDtorType()); |
| 195 | |
| 196 | return getFunctionInfo(FD); |
| 197 | } |
| 198 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 199 | const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy, |
Daniel Dunbar | bac7c25 | 2009-09-11 22:24:53 +0000 | [diff] [blame] | 200 | const CallArgList &Args, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 201 | const FunctionType::ExtInfo &Info) { |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 202 | // FIXME: Kill copy. |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 203 | llvm::SmallVector<CanQualType, 16> ArgTys; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 204 | for (CallArgList::const_iterator i = Args.begin(), e = Args.end(); |
Daniel Dunbar | 725ad31 | 2009-01-31 02:19:00 +0000 | [diff] [blame] | 205 | i != e; ++i) |
John McCall | 0b0ef0a | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 206 | ArgTys.push_back(Context.getCanonicalParamType(i->second)); |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 207 | return getFunctionInfo(GetReturnType(ResTy), ArgTys, Info); |
Daniel Dunbar | 725ad31 | 2009-01-31 02:19:00 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 210 | const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy, |
Daniel Dunbar | bac7c25 | 2009-09-11 22:24:53 +0000 | [diff] [blame] | 211 | const FunctionArgList &Args, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 212 | const FunctionType::ExtInfo &Info) { |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 213 | // FIXME: Kill copy. |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 214 | llvm::SmallVector<CanQualType, 16> ArgTys; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 215 | for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end(); |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 216 | i != e; ++i) |
John McCall | 0b0ef0a | 2010-02-24 07:14:12 +0000 | [diff] [blame] | 217 | ArgTys.push_back(Context.getCanonicalParamType(i->second)); |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 218 | return getFunctionInfo(GetReturnType(ResTy), ArgTys, Info); |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 219 | } |
| 220 | |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 221 | const CGFunctionInfo &CodeGenTypes::getFunctionInfo(CanQualType ResTy, |
| 222 | const llvm::SmallVectorImpl<CanQualType> &ArgTys, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 223 | const FunctionType::ExtInfo &Info) { |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 224 | #ifndef NDEBUG |
| 225 | for (llvm::SmallVectorImpl<CanQualType>::const_iterator |
| 226 | I = ArgTys.begin(), E = ArgTys.end(); I != E; ++I) |
| 227 | assert(I->isCanonicalAsParam()); |
| 228 | #endif |
| 229 | |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 230 | unsigned CC = ClangCallConvToLLVMCallConv(Info.getCC()); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 231 | |
Daniel Dunbar | 40a6be6 | 2009-02-03 00:07:12 +0000 | [diff] [blame] | 232 | // Lookup or create unique function info. |
| 233 | llvm::FoldingSetNodeID ID; |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 234 | CGFunctionInfo::Profile(ID, Info, ResTy, |
Daniel Dunbar | bac7c25 | 2009-09-11 22:24:53 +0000 | [diff] [blame] | 235 | ArgTys.begin(), ArgTys.end()); |
Daniel Dunbar | 40a6be6 | 2009-02-03 00:07:12 +0000 | [diff] [blame] | 236 | |
| 237 | void *InsertPos = 0; |
| 238 | CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos); |
| 239 | if (FI) |
| 240 | return *FI; |
| 241 | |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 242 | // Construct the function info. |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 243 | FI = new CGFunctionInfo(CC, Info.getNoReturn(), Info.getRegParm(), ResTy, ArgTys); |
Daniel Dunbar | 35e67d4 | 2009-02-05 00:00:23 +0000 | [diff] [blame] | 244 | FunctionInfos.InsertNode(FI, InsertPos); |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 245 | |
| 246 | // Compute ABI information. |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 247 | getABIInfo().computeInfo(*FI, getContext(), TheModule.getContext()); |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 248 | |
Daniel Dunbar | 40a6be6 | 2009-02-03 00:07:12 +0000 | [diff] [blame] | 249 | return *FI; |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Daniel Dunbar | bac7c25 | 2009-09-11 22:24:53 +0000 | [diff] [blame] | 252 | CGFunctionInfo::CGFunctionInfo(unsigned _CallingConvention, |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 253 | bool _NoReturn, |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 254 | unsigned _RegParm, |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 255 | CanQualType ResTy, |
| 256 | const llvm::SmallVectorImpl<CanQualType> &ArgTys) |
Daniel Dunbar | ca6408c | 2009-09-12 00:59:20 +0000 | [diff] [blame] | 257 | : CallingConvention(_CallingConvention), |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 258 | EffectiveCallingConvention(_CallingConvention), |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 259 | NoReturn(_NoReturn), RegParm(_RegParm) |
Daniel Dunbar | bac7c25 | 2009-09-11 22:24:53 +0000 | [diff] [blame] | 260 | { |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 261 | NumArgs = ArgTys.size(); |
| 262 | Args = new ArgInfo[1 + NumArgs]; |
| 263 | Args[0].type = ResTy; |
| 264 | for (unsigned i = 0; i < NumArgs; ++i) |
| 265 | Args[1 + i].type = ArgTys[i]; |
| 266 | } |
| 267 | |
| 268 | /***/ |
| 269 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 270 | void CodeGenTypes::GetExpandedTypes(QualType Ty, |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 271 | std::vector<const llvm::Type*> &ArgTys) { |
| 272 | const RecordType *RT = Ty->getAsStructureType(); |
| 273 | assert(RT && "Can only expand structure types."); |
| 274 | const RecordDecl *RD = RT->getDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 275 | assert(!RD->hasFlexibleArrayMember() && |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 276 | "Cannot expand structure with flexible array."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 277 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 278 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 279 | i != e; ++i) { |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 280 | const FieldDecl *FD = *i; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 281 | assert(!FD->isBitField() && |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 282 | "Cannot expand structure with bit-field members."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 283 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 284 | QualType FT = FD->getType(); |
| 285 | if (CodeGenFunction::hasAggregateLLVMType(FT)) { |
| 286 | GetExpandedTypes(FT, ArgTys); |
| 287 | } else { |
| 288 | ArgTys.push_back(ConvertType(FT)); |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 293 | llvm::Function::arg_iterator |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 294 | CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV, |
| 295 | llvm::Function::arg_iterator AI) { |
| 296 | const RecordType *RT = Ty->getAsStructureType(); |
| 297 | assert(RT && "Can only expand structure types."); |
| 298 | |
| 299 | RecordDecl *RD = RT->getDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 300 | assert(LV.isSimple() && |
| 301 | "Unexpected non-simple lvalue during struct expansion."); |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 302 | llvm::Value *Addr = LV.getAddress(); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 303 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 304 | i != e; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 305 | FieldDecl *FD = *i; |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 306 | QualType FT = FD->getType(); |
| 307 | |
| 308 | // FIXME: What are the right qualifiers here? |
Anders Carlsson | e6d2a53 | 2010-01-29 05:05:36 +0000 | [diff] [blame] | 309 | LValue LV = EmitLValueForField(Addr, FD, 0); |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 310 | if (CodeGenFunction::hasAggregateLLVMType(FT)) { |
| 311 | AI = ExpandTypeFromArgs(FT, LV, AI); |
| 312 | } else { |
| 313 | EmitStoreThroughLValue(RValue::get(AI), LV, FT); |
| 314 | ++AI; |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | return AI; |
| 319 | } |
| 320 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 321 | void |
| 322 | CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV, |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 323 | llvm::SmallVector<llvm::Value*, 16> &Args) { |
| 324 | const RecordType *RT = Ty->getAsStructureType(); |
| 325 | assert(RT && "Can only expand structure types."); |
| 326 | |
| 327 | RecordDecl *RD = RT->getDecl(); |
| 328 | assert(RV.isAggregate() && "Unexpected rvalue during struct expansion"); |
| 329 | llvm::Value *Addr = RV.getAggregateAddr(); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 330 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 331 | i != e; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 332 | FieldDecl *FD = *i; |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 333 | QualType FT = FD->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 334 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 335 | // FIXME: What are the right qualifiers here? |
Anders Carlsson | e6d2a53 | 2010-01-29 05:05:36 +0000 | [diff] [blame] | 336 | LValue LV = EmitLValueForField(Addr, FD, 0); |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 337 | if (CodeGenFunction::hasAggregateLLVMType(FT)) { |
| 338 | ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args); |
| 339 | } else { |
| 340 | RValue RV = EmitLoadOfLValue(LV, FT); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 341 | assert(RV.isScalar() && |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 342 | "Unexpected non-scalar rvalue during struct expansion."); |
| 343 | Args.push_back(RV.getScalarVal()); |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | |
Chris Lattner | e7bb777 | 2010-06-27 06:04:18 +0000 | [diff] [blame^] | 348 | /// EnterStructPointerForCoercedAccess - Given a struct pointer that we are |
Chris Lattner | 08dd2a0 | 2010-06-27 05:56:15 +0000 | [diff] [blame] | 349 | /// accessing some number of bytes out of it, try to gep into the struct to get |
| 350 | /// at its inner goodness. Dive as deep as possible without entering an element |
| 351 | /// with an in-memory size smaller than DstSize. |
| 352 | static llvm::Value * |
Chris Lattner | e7bb777 | 2010-06-27 06:04:18 +0000 | [diff] [blame^] | 353 | EnterStructPointerForCoercedAccess(llvm::Value *SrcPtr, |
| 354 | const llvm::StructType *SrcSTy, |
| 355 | uint64_t DstSize, CodeGenFunction &CGF) { |
Chris Lattner | 08dd2a0 | 2010-06-27 05:56:15 +0000 | [diff] [blame] | 356 | // We can't dive into a zero-element struct. |
| 357 | if (SrcSTy->getNumElements() == 0) return SrcPtr; |
| 358 | |
| 359 | const llvm::Type *FirstElt = SrcSTy->getElementType(0); |
| 360 | |
| 361 | // If the first elt is at least as large as what we're looking for, or if the |
| 362 | // first element is the same size as the whole struct, we can enter it. |
| 363 | uint64_t FirstEltSize = |
| 364 | CGF.CGM.getTargetData().getTypeAllocSize(FirstElt); |
| 365 | if (FirstEltSize < DstSize && |
| 366 | FirstEltSize < CGF.CGM.getTargetData().getTypeAllocSize(SrcSTy)) |
| 367 | return SrcPtr; |
| 368 | |
| 369 | // GEP into the first element. |
| 370 | SrcPtr = CGF.Builder.CreateConstGEP2_32(SrcPtr, 0, 0, "coerce.dive"); |
| 371 | |
| 372 | // If the first element is a struct, recurse. |
| 373 | const llvm::Type *SrcTy = |
| 374 | cast<llvm::PointerType>(SrcPtr->getType())->getElementType(); |
| 375 | if (const llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy)) |
Chris Lattner | e7bb777 | 2010-06-27 06:04:18 +0000 | [diff] [blame^] | 376 | return EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF); |
Chris Lattner | 08dd2a0 | 2010-06-27 05:56:15 +0000 | [diff] [blame] | 377 | |
| 378 | return SrcPtr; |
| 379 | } |
| 380 | |
| 381 | |
| 382 | |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 383 | /// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as |
| 384 | /// a pointer to an object of type \arg Ty. |
| 385 | /// |
| 386 | /// This safely handles the case when the src type is smaller than the |
| 387 | /// destination type; in this situation the values of bits which not |
| 388 | /// present in the src are undefined. |
| 389 | static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr, |
| 390 | const llvm::Type *Ty, |
| 391 | CodeGenFunction &CGF) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 392 | const llvm::Type *SrcTy = |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 393 | cast<llvm::PointerType>(SrcPtr->getType())->getElementType(); |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 394 | uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(Ty); |
Chris Lattner | 08dd2a0 | 2010-06-27 05:56:15 +0000 | [diff] [blame] | 395 | |
| 396 | if (const llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy)) { |
Chris Lattner | e7bb777 | 2010-06-27 06:04:18 +0000 | [diff] [blame^] | 397 | SrcPtr = EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF); |
Chris Lattner | 08dd2a0 | 2010-06-27 05:56:15 +0000 | [diff] [blame] | 398 | SrcTy = cast<llvm::PointerType>(SrcPtr->getType())->getElementType(); |
| 399 | } |
| 400 | |
| 401 | uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy); |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 402 | |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 403 | // If load is legal, just bitcast the src pointer. |
Daniel Dunbar | 7ef455b | 2009-05-13 18:54:26 +0000 | [diff] [blame] | 404 | if (SrcSize >= DstSize) { |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 405 | // Generally SrcSize is never greater than DstSize, since this means we are |
| 406 | // losing bits. However, this can happen in cases where the structure has |
| 407 | // additional padding, for example due to a user specified alignment. |
Daniel Dunbar | 7ef455b | 2009-05-13 18:54:26 +0000 | [diff] [blame] | 408 | // |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 409 | // FIXME: Assert that we aren't truncating non-padding bits when have access |
| 410 | // to that information. |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 411 | llvm::Value *Casted = |
| 412 | CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty)); |
Daniel Dunbar | 386621f | 2009-02-07 02:46:03 +0000 | [diff] [blame] | 413 | llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted); |
| 414 | // FIXME: Use better alignment / avoid requiring aligned load. |
| 415 | Load->setAlignment(1); |
| 416 | return Load; |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 417 | } |
Chris Lattner | 35b21b8 | 2010-06-27 01:06:27 +0000 | [diff] [blame] | 418 | |
| 419 | // Otherwise do coercion through memory. This is stupid, but |
| 420 | // simple. |
| 421 | llvm::Value *Tmp = CGF.CreateTempAlloca(Ty); |
| 422 | llvm::Value *Casted = |
| 423 | CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy)); |
| 424 | llvm::StoreInst *Store = |
| 425 | CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted); |
| 426 | // FIXME: Use better alignment / avoid requiring aligned store. |
| 427 | Store->setAlignment(1); |
| 428 | return CGF.Builder.CreateLoad(Tmp); |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | /// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src, |
| 432 | /// where the source and destination may have different types. |
| 433 | /// |
| 434 | /// This safely handles the case when the src type is larger than the |
| 435 | /// destination type; the upper bits of the src will be lost. |
| 436 | static void CreateCoercedStore(llvm::Value *Src, |
| 437 | llvm::Value *DstPtr, |
Anders Carlsson | d2490a9 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 438 | bool DstIsVolatile, |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 439 | CodeGenFunction &CGF) { |
| 440 | const llvm::Type *SrcTy = Src->getType(); |
Chris Lattner | e7bb777 | 2010-06-27 06:04:18 +0000 | [diff] [blame^] | 441 | uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy); |
| 442 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 443 | const llvm::Type *DstTy = |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 444 | cast<llvm::PointerType>(DstPtr->getType())->getElementType(); |
| 445 | |
Chris Lattner | e7bb777 | 2010-06-27 06:04:18 +0000 | [diff] [blame^] | 446 | if (const llvm::StructType *DstSTy = dyn_cast<llvm::StructType>(DstTy)) { |
| 447 | DstPtr = EnterStructPointerForCoercedAccess(DstPtr, DstSTy, SrcSize, CGF); |
| 448 | DstTy = cast<llvm::PointerType>(DstPtr->getType())->getElementType(); |
| 449 | } |
| 450 | |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 451 | uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(DstTy); |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 452 | |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 453 | // If store is legal, just bitcast the src pointer. |
Daniel Dunbar | fdf4986 | 2009-06-05 07:58:54 +0000 | [diff] [blame] | 454 | if (SrcSize <= DstSize) { |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 455 | llvm::Value *Casted = |
| 456 | CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy)); |
Daniel Dunbar | 386621f | 2009-02-07 02:46:03 +0000 | [diff] [blame] | 457 | // FIXME: Use better alignment / avoid requiring aligned store. |
Anders Carlsson | d2490a9 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 458 | CGF.Builder.CreateStore(Src, Casted, DstIsVolatile)->setAlignment(1); |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 459 | } else { |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 460 | // Otherwise do coercion through memory. This is stupid, but |
| 461 | // simple. |
Daniel Dunbar | fdf4986 | 2009-06-05 07:58:54 +0000 | [diff] [blame] | 462 | |
| 463 | // Generally SrcSize is never greater than DstSize, since this means we are |
| 464 | // losing bits. However, this can happen in cases where the structure has |
| 465 | // additional padding, for example due to a user specified alignment. |
| 466 | // |
| 467 | // FIXME: Assert that we aren't truncating non-padding bits when have access |
| 468 | // to that information. |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 469 | llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy); |
| 470 | CGF.Builder.CreateStore(Src, Tmp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 471 | llvm::Value *Casted = |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 472 | CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy)); |
Daniel Dunbar | 386621f | 2009-02-07 02:46:03 +0000 | [diff] [blame] | 473 | llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted); |
| 474 | // FIXME: Use better alignment / avoid requiring aligned load. |
| 475 | Load->setAlignment(1); |
Anders Carlsson | d2490a9 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 476 | CGF.Builder.CreateStore(Load, DstPtr, DstIsVolatile); |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 480 | /***/ |
| 481 | |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 482 | bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) { |
Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 483 | return FI.getReturnInfo().isIndirect(); |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 484 | } |
| 485 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 486 | const llvm::FunctionType *CodeGenTypes::GetFunctionType(GlobalDecl GD) { |
| 487 | const CGFunctionInfo &FI = getFunctionInfo(GD); |
| 488 | |
| 489 | // For definition purposes, don't consider a K&R function variadic. |
| 490 | bool Variadic = false; |
| 491 | if (const FunctionProtoType *FPT = |
| 492 | cast<FunctionDecl>(GD.getDecl())->getType()->getAs<FunctionProtoType>()) |
| 493 | Variadic = FPT->isVariadic(); |
| 494 | |
| 495 | return GetFunctionType(FI, Variadic); |
| 496 | } |
| 497 | |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 498 | const llvm::FunctionType * |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 499 | CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) { |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 500 | std::vector<const llvm::Type*> ArgTys; |
| 501 | |
| 502 | const llvm::Type *ResultType = 0; |
| 503 | |
Daniel Dunbar | a0a99e0 | 2009-02-02 23:43:58 +0000 | [diff] [blame] | 504 | QualType RetTy = FI.getReturnType(); |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 505 | const ABIArgInfo &RetAI = FI.getReturnInfo(); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 506 | switch (RetAI.getKind()) { |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 507 | case ABIArgInfo::Expand: |
| 508 | assert(0 && "Invalid ABI kind for return argument"); |
| 509 | |
Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 510 | case ABIArgInfo::Extend: |
Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 511 | case ABIArgInfo::Direct: |
| 512 | ResultType = ConvertType(RetTy); |
| 513 | break; |
| 514 | |
Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 515 | case ABIArgInfo::Indirect: { |
| 516 | assert(!RetAI.getIndirectAlign() && "Align unused on indirect return."); |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 517 | ResultType = llvm::Type::getVoidTy(getLLVMContext()); |
Daniel Dunbar | 62d5c1b | 2008-09-10 07:00:50 +0000 | [diff] [blame] | 518 | const llvm::Type *STy = ConvertType(RetTy); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 519 | ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace())); |
| 520 | break; |
| 521 | } |
| 522 | |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 523 | case ABIArgInfo::Ignore: |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 524 | ResultType = llvm::Type::getVoidTy(getLLVMContext()); |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 525 | break; |
| 526 | |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 527 | case ABIArgInfo::Coerce: |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 528 | ResultType = RetAI.getCoerceToType(); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 529 | break; |
| 530 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 531 | |
| 532 | for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(), |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 533 | ie = FI.arg_end(); it != ie; ++it) { |
| 534 | const ABIArgInfo &AI = it->info; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 535 | |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 536 | switch (AI.getKind()) { |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 537 | case ABIArgInfo::Ignore: |
| 538 | break; |
| 539 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 540 | case ABIArgInfo::Coerce: |
Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 541 | ArgTys.push_back(AI.getCoerceToType()); |
| 542 | break; |
| 543 | |
Daniel Dunbar | adc8bdd | 2009-02-10 01:51:39 +0000 | [diff] [blame] | 544 | case ABIArgInfo::Indirect: { |
Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 545 | // indirect arguments are always on the stack, which is addr space #0. |
Daniel Dunbar | adc8bdd | 2009-02-10 01:51:39 +0000 | [diff] [blame] | 546 | const llvm::Type *LTy = ConvertTypeForMem(it->type); |
| 547 | ArgTys.push_back(llvm::PointerType::getUnqual(LTy)); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 548 | break; |
Daniel Dunbar | adc8bdd | 2009-02-10 01:51:39 +0000 | [diff] [blame] | 549 | } |
Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 550 | |
| 551 | case ABIArgInfo::Extend: |
Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 552 | case ABIArgInfo::Direct: |
Daniel Dunbar | 1f74598 | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 553 | ArgTys.push_back(ConvertType(it->type)); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 554 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 555 | |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 556 | case ABIArgInfo::Expand: |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 557 | GetExpandedTypes(it->type, ArgTys); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 558 | break; |
| 559 | } |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 560 | } |
| 561 | |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 562 | return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic); |
Daniel Dunbar | 3913f18 | 2008-09-09 23:48:28 +0000 | [diff] [blame] | 563 | } |
| 564 | |
Anders Carlsson | ecf282b | 2009-11-24 05:08:52 +0000 | [diff] [blame] | 565 | const llvm::Type * |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 566 | CodeGenTypes::GetFunctionTypeForVTable(const CXXMethodDecl *MD) { |
Anders Carlsson | ecf282b | 2009-11-24 05:08:52 +0000 | [diff] [blame] | 567 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
| 568 | |
Eli Friedman | c00129a | 2010-05-30 06:03:20 +0000 | [diff] [blame] | 569 | if (!VerifyFuncTypeComplete(FPT)) |
Anders Carlsson | ecf282b | 2009-11-24 05:08:52 +0000 | [diff] [blame] | 570 | return GetFunctionType(getFunctionInfo(MD), FPT->isVariadic()); |
| 571 | |
| 572 | return llvm::OpaqueType::get(getLLVMContext()); |
| 573 | } |
| 574 | |
Daniel Dunbar | a0a99e0 | 2009-02-02 23:43:58 +0000 | [diff] [blame] | 575 | void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI, |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 576 | const Decl *TargetDecl, |
Daniel Dunbar | ca6408c | 2009-09-12 00:59:20 +0000 | [diff] [blame] | 577 | AttributeListType &PAL, |
| 578 | unsigned &CallingConv) { |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 579 | unsigned FuncAttrs = 0; |
Devang Patel | a2c6912 | 2008-09-26 22:53:57 +0000 | [diff] [blame] | 580 | unsigned RetAttrs = 0; |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 581 | |
Daniel Dunbar | ca6408c | 2009-09-12 00:59:20 +0000 | [diff] [blame] | 582 | CallingConv = FI.getEffectiveCallingConvention(); |
| 583 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 584 | if (FI.isNoReturn()) |
| 585 | FuncAttrs |= llvm::Attribute::NoReturn; |
| 586 | |
Anton Korobeynikov | 1102f42 | 2009-04-04 00:49:24 +0000 | [diff] [blame] | 587 | // FIXME: handle sseregparm someday... |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 588 | if (TargetDecl) { |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 589 | if (TargetDecl->hasAttr<NoThrowAttr>()) |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 590 | FuncAttrs |= llvm::Attribute::NoUnwind; |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 591 | if (TargetDecl->hasAttr<NoReturnAttr>()) |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 592 | FuncAttrs |= llvm::Attribute::NoReturn; |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 593 | if (TargetDecl->hasAttr<ConstAttr>()) |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 594 | FuncAttrs |= llvm::Attribute::ReadNone; |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 595 | else if (TargetDecl->hasAttr<PureAttr>()) |
Daniel Dunbar | 64c2e07 | 2009-04-10 22:14:52 +0000 | [diff] [blame] | 596 | FuncAttrs |= llvm::Attribute::ReadOnly; |
Ryan Flynn | 76168e2 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 597 | if (TargetDecl->hasAttr<MallocAttr>()) |
| 598 | RetAttrs |= llvm::Attribute::NoAlias; |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 599 | } |
| 600 | |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 601 | if (CodeGenOpts.OptimizeSize) |
Daniel Dunbar | 7ab1c3e | 2009-10-27 19:48:08 +0000 | [diff] [blame] | 602 | FuncAttrs |= llvm::Attribute::OptimizeForSize; |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 603 | if (CodeGenOpts.DisableRedZone) |
Devang Patel | 24095da | 2009-06-04 23:32:02 +0000 | [diff] [blame] | 604 | FuncAttrs |= llvm::Attribute::NoRedZone; |
Chandler Carruth | 2811ccf | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 605 | if (CodeGenOpts.NoImplicitFloat) |
Devang Patel | acebb39 | 2009-06-05 22:05:48 +0000 | [diff] [blame] | 606 | FuncAttrs |= llvm::Attribute::NoImplicitFloat; |
Devang Patel | 24095da | 2009-06-04 23:32:02 +0000 | [diff] [blame] | 607 | |
Daniel Dunbar | a0a99e0 | 2009-02-02 23:43:58 +0000 | [diff] [blame] | 608 | QualType RetTy = FI.getReturnType(); |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 609 | unsigned Index = 1; |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 610 | const ABIArgInfo &RetAI = FI.getReturnInfo(); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 611 | switch (RetAI.getKind()) { |
Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 612 | case ABIArgInfo::Extend: |
| 613 | if (RetTy->isSignedIntegerType()) { |
| 614 | RetAttrs |= llvm::Attribute::SExt; |
| 615 | } else if (RetTy->isUnsignedIntegerType()) { |
| 616 | RetAttrs |= llvm::Attribute::ZExt; |
| 617 | } |
| 618 | // FALLTHROUGH |
Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 619 | case ABIArgInfo::Direct: |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 620 | break; |
| 621 | |
Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 622 | case ABIArgInfo::Indirect: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 623 | PAL.push_back(llvm::AttributeWithIndex::get(Index, |
Chris Lattner | fb97cf2 | 2010-04-20 05:44:43 +0000 | [diff] [blame] | 624 | llvm::Attribute::StructRet)); |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 625 | ++Index; |
Daniel Dunbar | 0ac86f0 | 2009-03-18 19:51:01 +0000 | [diff] [blame] | 626 | // sret disables readnone and readonly |
| 627 | FuncAttrs &= ~(llvm::Attribute::ReadOnly | |
| 628 | llvm::Attribute::ReadNone); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 629 | break; |
| 630 | |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 631 | case ABIArgInfo::Ignore: |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 632 | case ABIArgInfo::Coerce: |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 633 | break; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 634 | |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 635 | case ABIArgInfo::Expand: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 636 | assert(0 && "Invalid ABI kind for return argument"); |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 637 | } |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 638 | |
Devang Patel | a2c6912 | 2008-09-26 22:53:57 +0000 | [diff] [blame] | 639 | if (RetAttrs) |
| 640 | PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs)); |
Anton Korobeynikov | 1102f42 | 2009-04-04 00:49:24 +0000 | [diff] [blame] | 641 | |
| 642 | // FIXME: we need to honour command line settings also... |
| 643 | // FIXME: RegParm should be reduced in case of nested functions and/or global |
| 644 | // register variable. |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 645 | signed RegParm = FI.getRegParm(); |
Anton Korobeynikov | 1102f42 | 2009-04-04 00:49:24 +0000 | [diff] [blame] | 646 | |
| 647 | unsigned PointerWidth = getContext().Target.getPointerWidth(0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 648 | for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(), |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 649 | ie = FI.arg_end(); it != ie; ++it) { |
| 650 | QualType ParamType = it->type; |
| 651 | const ABIArgInfo &AI = it->info; |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 652 | unsigned Attributes = 0; |
Anton Korobeynikov | 1102f42 | 2009-04-04 00:49:24 +0000 | [diff] [blame] | 653 | |
John McCall | d8e10d2 | 2010-03-27 00:47:27 +0000 | [diff] [blame] | 654 | // 'restrict' -> 'noalias' is done in EmitFunctionProlog when we |
| 655 | // have the corresponding parameter variable. It doesn't make |
| 656 | // sense to do it here because parameters are so fucked up. |
Nuno Lopes | 079b495 | 2009-12-07 18:30:06 +0000 | [diff] [blame] | 657 | |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 658 | switch (AI.getKind()) { |
Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 659 | case ABIArgInfo::Coerce: |
| 660 | break; |
| 661 | |
Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 662 | case ABIArgInfo::Indirect: |
Anders Carlsson | 0a8f847 | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 663 | if (AI.getIndirectByVal()) |
| 664 | Attributes |= llvm::Attribute::ByVal; |
| 665 | |
Anton Korobeynikov | 1102f42 | 2009-04-04 00:49:24 +0000 | [diff] [blame] | 666 | Attributes |= |
Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 667 | llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign()); |
Daniel Dunbar | 0ac86f0 | 2009-03-18 19:51:01 +0000 | [diff] [blame] | 668 | // byval disables readnone and readonly. |
| 669 | FuncAttrs &= ~(llvm::Attribute::ReadOnly | |
| 670 | llvm::Attribute::ReadNone); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 671 | break; |
Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 672 | |
| 673 | case ABIArgInfo::Extend: |
| 674 | if (ParamType->isSignedIntegerType()) { |
| 675 | Attributes |= llvm::Attribute::SExt; |
| 676 | } else if (ParamType->isUnsignedIntegerType()) { |
| 677 | Attributes |= llvm::Attribute::ZExt; |
| 678 | } |
| 679 | // FALLS THROUGH |
Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 680 | case ABIArgInfo::Direct: |
Anton Korobeynikov | 1102f42 | 2009-04-04 00:49:24 +0000 | [diff] [blame] | 681 | if (RegParm > 0 && |
| 682 | (ParamType->isIntegerType() || ParamType->isPointerType())) { |
| 683 | RegParm -= |
| 684 | (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth; |
| 685 | if (RegParm >= 0) |
| 686 | Attributes |= llvm::Attribute::InReg; |
| 687 | } |
| 688 | // FIXME: handle sseregparm someday... |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 689 | break; |
Anton Korobeynikov | 1102f42 | 2009-04-04 00:49:24 +0000 | [diff] [blame] | 690 | |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 691 | case ABIArgInfo::Ignore: |
| 692 | // Skip increment, no matching LLVM parameter. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 693 | continue; |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 694 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 695 | case ABIArgInfo::Expand: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 696 | std::vector<const llvm::Type*> Tys; |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 697 | // FIXME: This is rather inefficient. Do we ever actually need to do |
| 698 | // anything here? The result should be just reconstructed on the other |
| 699 | // side, so extension should be a non-issue. |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 700 | getTypes().GetExpandedTypes(ParamType, Tys); |
| 701 | Index += Tys.size(); |
| 702 | continue; |
| 703 | } |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 704 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 705 | |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 706 | if (Attributes) |
| 707 | PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes)); |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 708 | ++Index; |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 709 | } |
Devang Patel | a2c6912 | 2008-09-26 22:53:57 +0000 | [diff] [blame] | 710 | if (FuncAttrs) |
| 711 | PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs)); |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 712 | } |
| 713 | |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 714 | void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI, |
| 715 | llvm::Function *Fn, |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 716 | const FunctionArgList &Args) { |
John McCall | 0cfeb63 | 2009-07-28 01:00:58 +0000 | [diff] [blame] | 717 | // If this is an implicit-return-zero function, go ahead and |
| 718 | // initialize the return value. TODO: it might be nice to have |
| 719 | // a more general mechanism for this that didn't require synthesized |
| 720 | // return statements. |
Anders Carlsson | 89ed31d | 2009-08-08 23:24:23 +0000 | [diff] [blame] | 721 | if (const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl)) { |
John McCall | 0cfeb63 | 2009-07-28 01:00:58 +0000 | [diff] [blame] | 722 | if (FD->hasImplicitReturnZero()) { |
| 723 | QualType RetTy = FD->getResultType().getUnqualifiedType(); |
| 724 | const llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy); |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 725 | llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy); |
John McCall | 0cfeb63 | 2009-07-28 01:00:58 +0000 | [diff] [blame] | 726 | Builder.CreateStore(Zero, ReturnValue); |
| 727 | } |
| 728 | } |
| 729 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 730 | // FIXME: We no longer need the types from FunctionArgList; lift up and |
| 731 | // simplify. |
Daniel Dunbar | 5251afa | 2009-02-03 06:02:10 +0000 | [diff] [blame] | 732 | |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 733 | // Emit allocs for param decls. Give the LLVM Argument nodes names. |
| 734 | llvm::Function::arg_iterator AI = Fn->arg_begin(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 735 | |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 736 | // Name the struct return argument. |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 737 | if (CGM.ReturnTypeUsesSret(FI)) { |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 738 | AI->setName("agg.result"); |
| 739 | ++AI; |
| 740 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 741 | |
Daniel Dunbar | 4b5f0a4 | 2009-02-04 21:17:21 +0000 | [diff] [blame] | 742 | assert(FI.arg_size() == Args.size() && |
| 743 | "Mismatch between function signature & arguments."); |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 744 | CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin(); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 745 | for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end(); |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 746 | i != e; ++i, ++info_it) { |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 747 | const VarDecl *Arg = i->first; |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 748 | QualType Ty = info_it->type; |
| 749 | const ABIArgInfo &ArgI = info_it->info; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 750 | |
| 751 | switch (ArgI.getKind()) { |
Daniel Dunbar | 1f74598 | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 752 | case ABIArgInfo::Indirect: { |
| 753 | llvm::Value* V = AI; |
| 754 | if (hasAggregateLLVMType(Ty)) { |
| 755 | // Do nothing, aggregates and complex variables are accessed by |
| 756 | // reference. |
| 757 | } else { |
| 758 | // Load scalar value from indirect argument. |
Daniel Dunbar | adc8bdd | 2009-02-10 01:51:39 +0000 | [diff] [blame] | 759 | V = EmitLoadOfScalar(V, false, Ty); |
Daniel Dunbar | 1f74598 | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 760 | if (!getContext().typesAreCompatible(Ty, Arg->getType())) { |
| 761 | // This must be a promotion, for something like |
| 762 | // "void a(x) short x; {..." |
| 763 | V = EmitScalarConversion(V, Ty, Arg->getType()); |
| 764 | } |
| 765 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 766 | EmitParmDecl(*Arg, V); |
Daniel Dunbar | 1f74598 | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 767 | break; |
| 768 | } |
Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 769 | |
| 770 | case ABIArgInfo::Extend: |
Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 771 | case ABIArgInfo::Direct: { |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 772 | assert(AI != Fn->arg_end() && "Argument mismatch!"); |
| 773 | llvm::Value* V = AI; |
Daniel Dunbar | 2fbf2f5 | 2009-02-05 11:13:54 +0000 | [diff] [blame] | 774 | if (hasAggregateLLVMType(Ty)) { |
| 775 | // Create a temporary alloca to hold the argument; the rest of |
| 776 | // codegen expects to access aggregates & complex values by |
| 777 | // reference. |
Daniel Dunbar | 195337d | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 778 | V = CreateMemTemp(Ty); |
Daniel Dunbar | 2fbf2f5 | 2009-02-05 11:13:54 +0000 | [diff] [blame] | 779 | Builder.CreateStore(AI, V); |
| 780 | } else { |
John McCall | d8e10d2 | 2010-03-27 00:47:27 +0000 | [diff] [blame] | 781 | if (Arg->getType().isRestrictQualified()) |
| 782 | AI->addAttr(llvm::Attribute::NoAlias); |
| 783 | |
Daniel Dunbar | 2fbf2f5 | 2009-02-05 11:13:54 +0000 | [diff] [blame] | 784 | if (!getContext().typesAreCompatible(Ty, Arg->getType())) { |
| 785 | // This must be a promotion, for something like |
| 786 | // "void a(x) short x; {..." |
| 787 | V = EmitScalarConversion(V, Ty, Arg->getType()); |
| 788 | } |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 789 | } |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 790 | EmitParmDecl(*Arg, V); |
| 791 | break; |
| 792 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 793 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 794 | case ABIArgInfo::Expand: { |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 795 | // If this structure was expanded into multiple arguments then |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 796 | // we need to create a temporary and reconstruct it from the |
| 797 | // arguments. |
Daniel Dunbar | 195337d | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 798 | llvm::Value *Temp = CreateMemTemp(Ty, Arg->getName() + ".addr"); |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 799 | // FIXME: What are the right qualifiers here? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 800 | llvm::Function::arg_iterator End = |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 801 | ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp, Qualifiers()), AI); |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 802 | EmitParmDecl(*Arg, Temp); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 803 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 804 | // Name the arguments used in expansion and increment AI. |
| 805 | unsigned Index = 0; |
| 806 | for (; AI != End; ++AI, ++Index) |
Daniel Dunbar | 259e9cc | 2009-10-19 01:21:05 +0000 | [diff] [blame] | 807 | AI->setName(Arg->getName() + "." + llvm::Twine(Index)); |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 808 | continue; |
| 809 | } |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 810 | |
| 811 | case ABIArgInfo::Ignore: |
Daniel Dunbar | 8b979d9 | 2009-02-10 00:06:49 +0000 | [diff] [blame] | 812 | // Initialize the local variable appropriately. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 813 | if (hasAggregateLLVMType(Ty)) { |
Daniel Dunbar | 195337d | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 814 | EmitParmDecl(*Arg, CreateMemTemp(Ty)); |
Daniel Dunbar | 8b979d9 | 2009-02-10 00:06:49 +0000 | [diff] [blame] | 815 | } else { |
| 816 | EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType()))); |
| 817 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 818 | |
Daniel Dunbar | 59e5a0e | 2009-02-03 20:00:13 +0000 | [diff] [blame] | 819 | // Skip increment, no matching LLVM parameter. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 820 | continue; |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 821 | |
Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 822 | case ABIArgInfo::Coerce: { |
| 823 | assert(AI != Fn->arg_end() && "Argument mismatch!"); |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 824 | // FIXME: This is very wasteful; EmitParmDecl is just going to drop the |
| 825 | // result in a new alloca anyway, so we could just store into that |
| 826 | // directly if we broke the abstraction down more. |
Daniel Dunbar | 195337d | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 827 | llvm::Value *V = CreateMemTemp(Ty, "coerce"); |
Anders Carlsson | d2490a9 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 828 | CreateCoercedStore(AI, V, /*DestIsVolatile=*/false, *this); |
Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 829 | // Match to what EmitParmDecl is expecting for this type. |
Daniel Dunbar | 8b29a38 | 2009-02-04 07:22:24 +0000 | [diff] [blame] | 830 | if (!CodeGenFunction::hasAggregateLLVMType(Ty)) { |
Daniel Dunbar | adc8bdd | 2009-02-10 01:51:39 +0000 | [diff] [blame] | 831 | V = EmitLoadOfScalar(V, false, Ty); |
Daniel Dunbar | 8b29a38 | 2009-02-04 07:22:24 +0000 | [diff] [blame] | 832 | if (!getContext().typesAreCompatible(Ty, Arg->getType())) { |
| 833 | // This must be a promotion, for something like |
| 834 | // "void a(x) short x; {..." |
| 835 | V = EmitScalarConversion(V, Ty, Arg->getType()); |
| 836 | } |
| 837 | } |
Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 838 | EmitParmDecl(*Arg, V); |
| 839 | break; |
| 840 | } |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 841 | } |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 842 | |
| 843 | ++AI; |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 844 | } |
| 845 | assert(AI == Fn->arg_end() && "Argument mismatch!"); |
| 846 | } |
| 847 | |
Chris Lattner | 35b21b8 | 2010-06-27 01:06:27 +0000 | [diff] [blame] | 848 | void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI) { |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 849 | // Functions with no result always return void. |
Chris Lattner | c6e6dd2 | 2010-06-26 23:13:19 +0000 | [diff] [blame] | 850 | if (ReturnValue == 0) { |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 851 | Builder.CreateRetVoid(); |
Chris Lattner | c6e6dd2 | 2010-06-26 23:13:19 +0000 | [diff] [blame] | 852 | return; |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 853 | } |
Chris Lattner | c6e6dd2 | 2010-06-26 23:13:19 +0000 | [diff] [blame] | 854 | |
| 855 | llvm::Value *RV = 0; |
| 856 | QualType RetTy = FI.getReturnType(); |
| 857 | const ABIArgInfo &RetAI = FI.getReturnInfo(); |
| 858 | |
| 859 | switch (RetAI.getKind()) { |
| 860 | case ABIArgInfo::Indirect: |
| 861 | if (RetTy->isAnyComplexType()) { |
| 862 | ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false); |
| 863 | StoreComplexToAddr(RT, CurFn->arg_begin(), false); |
| 864 | } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) { |
| 865 | // Do nothing; aggregrates get evaluated directly into the destination. |
| 866 | } else { |
| 867 | EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(), |
| 868 | false, RetTy); |
| 869 | } |
| 870 | break; |
| 871 | |
| 872 | case ABIArgInfo::Extend: |
Chris Lattner | 35b21b8 | 2010-06-27 01:06:27 +0000 | [diff] [blame] | 873 | case ABIArgInfo::Direct: { |
| 874 | // The internal return value temp always will have pointer-to-return-type |
| 875 | // type, just do a load. |
| 876 | |
| 877 | // If the instruction right before the insertion point is a store to the |
| 878 | // return value, we can elide the load, zap the store, and usually zap the |
| 879 | // alloca. |
| 880 | llvm::BasicBlock *InsertBB = Builder.GetInsertBlock(); |
| 881 | llvm::StoreInst *SI = 0; |
| 882 | if (InsertBB->empty() || |
| 883 | !(SI = dyn_cast<llvm::StoreInst>(&InsertBB->back())) || |
| 884 | SI->getPointerOperand() != ReturnValue || SI->isVolatile()) { |
| 885 | RV = Builder.CreateLoad(ReturnValue); |
| 886 | } else { |
| 887 | // Get the stored value and nuke the now-dead store. |
| 888 | RV = SI->getValueOperand(); |
| 889 | SI->eraseFromParent(); |
| 890 | |
| 891 | // If that was the only use of the return value, nuke it as well now. |
| 892 | if (ReturnValue->use_empty() && isa<llvm::AllocaInst>(ReturnValue)) { |
| 893 | cast<llvm::AllocaInst>(ReturnValue)->eraseFromParent(); |
| 894 | ReturnValue = 0; |
| 895 | } |
| 896 | } |
Chris Lattner | c6e6dd2 | 2010-06-26 23:13:19 +0000 | [diff] [blame] | 897 | break; |
Chris Lattner | 35b21b8 | 2010-06-27 01:06:27 +0000 | [diff] [blame] | 898 | } |
Chris Lattner | c6e6dd2 | 2010-06-26 23:13:19 +0000 | [diff] [blame] | 899 | case ABIArgInfo::Ignore: |
| 900 | break; |
| 901 | |
| 902 | case ABIArgInfo::Coerce: |
| 903 | RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this); |
| 904 | break; |
| 905 | |
| 906 | case ABIArgInfo::Expand: |
| 907 | assert(0 && "Invalid ABI kind for return argument"); |
| 908 | } |
| 909 | |
| 910 | if (RV) |
| 911 | Builder.CreateRet(RV); |
| 912 | else |
| 913 | Builder.CreateRetVoid(); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 914 | } |
| 915 | |
John McCall | 2736071 | 2010-05-26 22:34:26 +0000 | [diff] [blame] | 916 | RValue CodeGenFunction::EmitDelegateCallArg(const VarDecl *Param) { |
| 917 | // StartFunction converted the ABI-lowered parameter(s) into a |
| 918 | // local alloca. We need to turn that into an r-value suitable |
| 919 | // for EmitCall. |
| 920 | llvm::Value *Local = GetAddrOfLocalVar(Param); |
| 921 | |
| 922 | QualType ArgType = Param->getType(); |
| 923 | |
| 924 | // For the most part, we just need to load the alloca, except: |
| 925 | // 1) aggregate r-values are actually pointers to temporaries, and |
| 926 | // 2) references to aggregates are pointers directly to the aggregate. |
| 927 | // I don't know why references to non-aggregates are different here. |
| 928 | if (const ReferenceType *RefType = ArgType->getAs<ReferenceType>()) { |
| 929 | if (hasAggregateLLVMType(RefType->getPointeeType())) |
| 930 | return RValue::getAggregate(Local); |
| 931 | |
| 932 | // Locals which are references to scalars are represented |
| 933 | // with allocas holding the pointer. |
| 934 | return RValue::get(Builder.CreateLoad(Local)); |
| 935 | } |
| 936 | |
| 937 | if (ArgType->isAnyComplexType()) |
| 938 | return RValue::getComplex(LoadComplexFromAddr(Local, /*volatile*/ false)); |
| 939 | |
| 940 | if (hasAggregateLLVMType(ArgType)) |
| 941 | return RValue::getAggregate(Local); |
| 942 | |
| 943 | return RValue::get(EmitLoadOfScalar(Local, false, ArgType)); |
| 944 | } |
| 945 | |
Anders Carlsson | 0139bb9 | 2009-04-08 20:47:54 +0000 | [diff] [blame] | 946 | RValue CodeGenFunction::EmitCallArg(const Expr *E, QualType ArgType) { |
Anders Carlsson | 4029ca7 | 2009-05-20 00:24:07 +0000 | [diff] [blame] | 947 | if (ArgType->isReferenceType()) |
Anders Carlsson | 32f36ba | 2010-06-26 16:35:32 +0000 | [diff] [blame] | 948 | return EmitReferenceBindingToExpr(E, /*InitializedDecl=*/0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 949 | |
Anders Carlsson | 0139bb9 | 2009-04-08 20:47:54 +0000 | [diff] [blame] | 950 | return EmitAnyExprToTemp(E); |
| 951 | } |
| 952 | |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 953 | RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 954 | llvm::Value *Callee, |
Anders Carlsson | f3c47c9 | 2009-12-24 19:25:24 +0000 | [diff] [blame] | 955 | ReturnValueSlot ReturnValue, |
Daniel Dunbar | c0ef9f5 | 2009-02-20 18:06:48 +0000 | [diff] [blame] | 956 | const CallArgList &CallArgs, |
David Chisnall | dd5c98f | 2010-05-01 11:15:56 +0000 | [diff] [blame] | 957 | const Decl *TargetDecl, |
David Chisnall | 4b02afc | 2010-05-02 13:41:58 +0000 | [diff] [blame] | 958 | llvm::Instruction **callOrInvoke) { |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 959 | // 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] | 960 | llvm::SmallVector<llvm::Value*, 16> Args; |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 961 | |
| 962 | // Handle struct-return functions by passing a pointer to the |
| 963 | // location that we would like to return into. |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 964 | QualType RetTy = CallInfo.getReturnType(); |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 965 | const ABIArgInfo &RetAI = CallInfo.getReturnInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 966 | |
| 967 | |
Chris Lattner | 5db7ae5 | 2009-06-13 00:26:38 +0000 | [diff] [blame] | 968 | // If the call returns a temporary with struct return, create a temporary |
Anders Carlsson | d2490a9 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 969 | // alloca to hold the result, unless one is given to us. |
| 970 | if (CGM.ReturnTypeUsesSret(CallInfo)) { |
| 971 | llvm::Value *Value = ReturnValue.getValue(); |
| 972 | if (!Value) |
Daniel Dunbar | 195337d | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 973 | Value = CreateMemTemp(RetTy); |
Anders Carlsson | d2490a9 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 974 | Args.push_back(Value); |
| 975 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 976 | |
Daniel Dunbar | 4b5f0a4 | 2009-02-04 21:17:21 +0000 | [diff] [blame] | 977 | assert(CallInfo.arg_size() == CallArgs.size() && |
| 978 | "Mismatch between function signature & arguments."); |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 979 | CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 980 | for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end(); |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 981 | I != E; ++I, ++info_it) { |
| 982 | const ABIArgInfo &ArgInfo = info_it->info; |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 983 | RValue RV = I->first; |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 984 | |
| 985 | switch (ArgInfo.getKind()) { |
Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 986 | case ABIArgInfo::Indirect: |
Daniel Dunbar | 1f74598 | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 987 | if (RV.isScalar() || RV.isComplex()) { |
| 988 | // Make a temporary alloca to pass the argument. |
Daniel Dunbar | 195337d | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 989 | Args.push_back(CreateMemTemp(I->second)); |
Daniel Dunbar | 1f74598 | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 990 | if (RV.isScalar()) |
Anders Carlsson | b4aa466 | 2009-05-19 18:50:41 +0000 | [diff] [blame] | 991 | EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false, I->second); |
Daniel Dunbar | 1f74598 | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 992 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 993 | StoreComplexToAddr(RV.getComplexVal(), Args.back(), false); |
Daniel Dunbar | 1f74598 | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 994 | } else { |
| 995 | Args.push_back(RV.getAggregateAddr()); |
| 996 | } |
| 997 | break; |
| 998 | |
Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 999 | case ABIArgInfo::Extend: |
Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 1000 | case ABIArgInfo::Direct: |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1001 | if (RV.isScalar()) { |
| 1002 | Args.push_back(RV.getScalarVal()); |
| 1003 | } else if (RV.isComplex()) { |
Daniel Dunbar | 2fbf2f5 | 2009-02-05 11:13:54 +0000 | [diff] [blame] | 1004 | llvm::Value *Tmp = llvm::UndefValue::get(ConvertType(I->second)); |
| 1005 | Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().first, 0); |
| 1006 | Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().second, 1); |
| 1007 | Args.push_back(Tmp); |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1008 | } else { |
Daniel Dunbar | 2fbf2f5 | 2009-02-05 11:13:54 +0000 | [diff] [blame] | 1009 | Args.push_back(Builder.CreateLoad(RV.getAggregateAddr())); |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1010 | } |
| 1011 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1012 | |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1013 | case ABIArgInfo::Ignore: |
| 1014 | break; |
| 1015 | |
Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 1016 | case ABIArgInfo::Coerce: { |
| 1017 | // FIXME: Avoid the conversion through memory if possible. |
| 1018 | llvm::Value *SrcPtr; |
| 1019 | if (RV.isScalar()) { |
Daniel Dunbar | 195337d | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 1020 | SrcPtr = CreateMemTemp(I->second, "coerce"); |
Anders Carlsson | b4aa466 | 2009-05-19 18:50:41 +0000 | [diff] [blame] | 1021 | EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false, I->second); |
Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 1022 | } else if (RV.isComplex()) { |
Daniel Dunbar | 195337d | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 1023 | SrcPtr = CreateMemTemp(I->second, "coerce"); |
Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 1024 | StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1025 | } else |
Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 1026 | SrcPtr = RV.getAggregateAddr(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1027 | Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(), |
Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 1028 | *this)); |
| 1029 | break; |
| 1030 | } |
| 1031 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1032 | case ABIArgInfo::Expand: |
| 1033 | ExpandTypeToArgs(I->second, RV, Args); |
| 1034 | break; |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1035 | } |
| 1036 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1037 | |
Chris Lattner | 5db7ae5 | 2009-06-13 00:26:38 +0000 | [diff] [blame] | 1038 | // If the callee is a bitcast of a function to a varargs pointer to function |
| 1039 | // type, check to see if we can remove the bitcast. This handles some cases |
| 1040 | // with unprototyped functions. |
| 1041 | if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Callee)) |
| 1042 | if (llvm::Function *CalleeF = dyn_cast<llvm::Function>(CE->getOperand(0))) { |
| 1043 | const llvm::PointerType *CurPT=cast<llvm::PointerType>(Callee->getType()); |
| 1044 | const llvm::FunctionType *CurFT = |
| 1045 | cast<llvm::FunctionType>(CurPT->getElementType()); |
| 1046 | const llvm::FunctionType *ActualFT = CalleeF->getFunctionType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1047 | |
Chris Lattner | 5db7ae5 | 2009-06-13 00:26:38 +0000 | [diff] [blame] | 1048 | if (CE->getOpcode() == llvm::Instruction::BitCast && |
| 1049 | ActualFT->getReturnType() == CurFT->getReturnType() && |
Chris Lattner | d6bebbf | 2009-06-23 01:38:41 +0000 | [diff] [blame] | 1050 | ActualFT->getNumParams() == CurFT->getNumParams() && |
| 1051 | ActualFT->getNumParams() == Args.size()) { |
Chris Lattner | 5db7ae5 | 2009-06-13 00:26:38 +0000 | [diff] [blame] | 1052 | bool ArgsMatch = true; |
| 1053 | for (unsigned i = 0, e = ActualFT->getNumParams(); i != e; ++i) |
| 1054 | if (ActualFT->getParamType(i) != CurFT->getParamType(i)) { |
| 1055 | ArgsMatch = false; |
| 1056 | break; |
| 1057 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1058 | |
Chris Lattner | 5db7ae5 | 2009-06-13 00:26:38 +0000 | [diff] [blame] | 1059 | // Strip the cast if we can get away with it. This is a nice cleanup, |
| 1060 | // but also allows us to inline the function at -O0 if it is marked |
| 1061 | // always_inline. |
| 1062 | if (ArgsMatch) |
| 1063 | Callee = CalleeF; |
| 1064 | } |
| 1065 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1066 | |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1067 | |
Daniel Dunbar | 9834ffb | 2009-02-23 17:26:39 +0000 | [diff] [blame] | 1068 | llvm::BasicBlock *InvokeDest = getInvokeDest(); |
Daniel Dunbar | ca6408c | 2009-09-12 00:59:20 +0000 | [diff] [blame] | 1069 | unsigned CallingConv; |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1070 | CodeGen::AttributeListType AttributeList; |
Daniel Dunbar | ca6408c | 2009-09-12 00:59:20 +0000 | [diff] [blame] | 1071 | CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList, CallingConv); |
Daniel Dunbar | 9834ffb | 2009-02-23 17:26:39 +0000 | [diff] [blame] | 1072 | llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(), |
| 1073 | AttributeList.end()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1074 | |
Daniel Dunbar | d14151d | 2009-03-02 04:32:35 +0000 | [diff] [blame] | 1075 | llvm::CallSite CS; |
| 1076 | if (!InvokeDest || (Attrs.getFnAttributes() & llvm::Attribute::NoUnwind)) { |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1077 | CS = Builder.CreateCall(Callee, Args.data(), Args.data()+Args.size()); |
Daniel Dunbar | 9834ffb | 2009-02-23 17:26:39 +0000 | [diff] [blame] | 1078 | } else { |
| 1079 | llvm::BasicBlock *Cont = createBasicBlock("invoke.cont"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1080 | CS = Builder.CreateInvoke(Callee, Cont, InvokeDest, |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1081 | Args.data(), Args.data()+Args.size()); |
Daniel Dunbar | 9834ffb | 2009-02-23 17:26:39 +0000 | [diff] [blame] | 1082 | EmitBlock(Cont); |
Daniel Dunbar | f4fe0f0 | 2009-02-20 18:54:31 +0000 | [diff] [blame] | 1083 | } |
David Chisnall | 4b02afc | 2010-05-02 13:41:58 +0000 | [diff] [blame] | 1084 | if (callOrInvoke) { |
| 1085 | *callOrInvoke = CS.getInstruction(); |
David Chisnall | dd5c98f | 2010-05-01 11:15:56 +0000 | [diff] [blame] | 1086 | } |
Daniel Dunbar | f4fe0f0 | 2009-02-20 18:54:31 +0000 | [diff] [blame] | 1087 | |
Daniel Dunbar | d14151d | 2009-03-02 04:32:35 +0000 | [diff] [blame] | 1088 | CS.setAttributes(Attrs); |
Daniel Dunbar | ca6408c | 2009-09-12 00:59:20 +0000 | [diff] [blame] | 1089 | CS.setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv)); |
Daniel Dunbar | d14151d | 2009-03-02 04:32:35 +0000 | [diff] [blame] | 1090 | |
| 1091 | // If the call doesn't return, finish the basic block and clear the |
| 1092 | // insertion point; this allows the rest of IRgen to discard |
| 1093 | // unreachable code. |
| 1094 | if (CS.doesNotReturn()) { |
| 1095 | Builder.CreateUnreachable(); |
| 1096 | Builder.ClearInsertionPoint(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1097 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1098 | // FIXME: For now, emit a dummy basic block because expr emitters in |
| 1099 | // generally are not ready to handle emitting expressions at unreachable |
| 1100 | // points. |
Daniel Dunbar | d14151d | 2009-03-02 04:32:35 +0000 | [diff] [blame] | 1101 | EnsureInsertPoint(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1102 | |
Daniel Dunbar | d14151d | 2009-03-02 04:32:35 +0000 | [diff] [blame] | 1103 | // Return a reasonable RValue. |
| 1104 | return GetUndefRValue(RetTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1105 | } |
Daniel Dunbar | d14151d | 2009-03-02 04:32:35 +0000 | [diff] [blame] | 1106 | |
| 1107 | llvm::Instruction *CI = CS.getInstruction(); |
Benjamin Kramer | ffbb15e | 2009-10-05 13:47:21 +0000 | [diff] [blame] | 1108 | if (Builder.isNamePreserving() && !CI->getType()->isVoidTy()) |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1109 | CI->setName("call"); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1110 | |
| 1111 | switch (RetAI.getKind()) { |
Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 1112 | case ABIArgInfo::Indirect: |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1113 | if (RetTy->isAnyComplexType()) |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1114 | return RValue::getComplex(LoadComplexFromAddr(Args[0], false)); |
Chris Lattner | 3403084 | 2009-03-22 00:32:22 +0000 | [diff] [blame] | 1115 | if (CodeGenFunction::hasAggregateLLVMType(RetTy)) |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1116 | return RValue::getAggregate(Args[0]); |
Chris Lattner | 3403084 | 2009-03-22 00:32:22 +0000 | [diff] [blame] | 1117 | return RValue::get(EmitLoadOfScalar(Args[0], false, RetTy)); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1118 | |
Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 1119 | case ABIArgInfo::Extend: |
Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 1120 | case ABIArgInfo::Direct: |
Daniel Dunbar | 2fbf2f5 | 2009-02-05 11:13:54 +0000 | [diff] [blame] | 1121 | if (RetTy->isAnyComplexType()) { |
| 1122 | llvm::Value *Real = Builder.CreateExtractValue(CI, 0); |
| 1123 | llvm::Value *Imag = Builder.CreateExtractValue(CI, 1); |
| 1124 | return RValue::getComplex(std::make_pair(Real, Imag)); |
Chris Lattner | 3403084 | 2009-03-22 00:32:22 +0000 | [diff] [blame] | 1125 | } |
| 1126 | if (CodeGenFunction::hasAggregateLLVMType(RetTy)) { |
Anders Carlsson | d2490a9 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 1127 | llvm::Value *DestPtr = ReturnValue.getValue(); |
| 1128 | bool DestIsVolatile = ReturnValue.isVolatile(); |
| 1129 | |
| 1130 | if (!DestPtr) { |
Daniel Dunbar | 195337d | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 1131 | DestPtr = CreateMemTemp(RetTy, "agg.tmp"); |
Anders Carlsson | d2490a9 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 1132 | DestIsVolatile = false; |
| 1133 | } |
| 1134 | Builder.CreateStore(CI, DestPtr, DestIsVolatile); |
| 1135 | return RValue::getAggregate(DestPtr); |
Chris Lattner | 3403084 | 2009-03-22 00:32:22 +0000 | [diff] [blame] | 1136 | } |
| 1137 | return RValue::get(CI); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1138 | |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1139 | case ABIArgInfo::Ignore: |
Daniel Dunbar | 0bcc521 | 2009-02-03 06:30:17 +0000 | [diff] [blame] | 1140 | // If we are ignoring an argument that had a result, make sure to |
| 1141 | // construct the appropriate return value for our caller. |
Daniel Dunbar | 13e8173 | 2009-02-05 07:09:07 +0000 | [diff] [blame] | 1142 | return GetUndefRValue(RetTy); |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1143 | |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 1144 | case ABIArgInfo::Coerce: { |
Anders Carlsson | d2490a9 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 1145 | llvm::Value *DestPtr = ReturnValue.getValue(); |
| 1146 | bool DestIsVolatile = ReturnValue.isVolatile(); |
| 1147 | |
| 1148 | if (!DestPtr) { |
Daniel Dunbar | 195337d | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 1149 | DestPtr = CreateMemTemp(RetTy, "coerce"); |
Anders Carlsson | d2490a9 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 1150 | DestIsVolatile = false; |
| 1151 | } |
| 1152 | |
| 1153 | CreateCoercedStore(CI, DestPtr, DestIsVolatile, *this); |
Anders Carlsson | ad3d691 | 2008-11-25 22:21:48 +0000 | [diff] [blame] | 1154 | if (RetTy->isAnyComplexType()) |
Anders Carlsson | d2490a9 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 1155 | return RValue::getComplex(LoadComplexFromAddr(DestPtr, false)); |
Chris Lattner | 3403084 | 2009-03-22 00:32:22 +0000 | [diff] [blame] | 1156 | if (CodeGenFunction::hasAggregateLLVMType(RetTy)) |
Anders Carlsson | d2490a9 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 1157 | return RValue::getAggregate(DestPtr); |
| 1158 | return RValue::get(EmitLoadOfScalar(DestPtr, false, RetTy)); |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 1159 | } |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1160 | |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1161 | case ABIArgInfo::Expand: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1162 | assert(0 && "Invalid ABI kind for return argument"); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1163 | } |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1164 | |
| 1165 | assert(0 && "Unhandled ABIArgInfo::Kind"); |
| 1166 | return RValue::get(0); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1167 | } |
Daniel Dunbar | b4094ea | 2009-02-10 20:44:09 +0000 | [diff] [blame] | 1168 | |
| 1169 | /* VarArg handling */ |
| 1170 | |
| 1171 | llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) { |
| 1172 | return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this); |
| 1173 | } |