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