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