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